blob: 1011b5f7f21f667f051c27ea86ccc79f16c8afc4 [file] [log] [blame]
Len Brown103a8fe2010-10-22 23:53:03 -04001/*
2 * turbostat -- show CPU frequency and C-state residency
3 * on modern Intel turbo-capable processors.
4 *
Len Brown144b44b2013-11-09 00:30:16 -05005 * Copyright (c) 2013 Intel Corporation.
Len Brown103a8fe2010-10-22 23:53:03 -04006 * Len Brown <len.brown@intel.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
Len Brown88c32812012-03-29 21:44:40 -040022#define _GNU_SOURCE
Josh Triplettb731f312013-08-20 17:20:12 -070023#include MSRHEADER
Len Brown869ce692016-06-16 23:22:37 -040024#include INTEL_FAMILY_HEADER
Josh Triplett95aebc42013-08-20 17:20:16 -070025#include <stdarg.h>
Len Brown103a8fe2010-10-22 23:53:03 -040026#include <stdio.h>
Josh Triplettb2c95d92013-08-20 17:20:18 -070027#include <err.h>
Len Brown103a8fe2010-10-22 23:53:03 -040028#include <unistd.h>
29#include <sys/types.h>
30#include <sys/wait.h>
31#include <sys/stat.h>
32#include <sys/resource.h>
33#include <fcntl.h>
34#include <signal.h>
35#include <sys/time.h>
36#include <stdlib.h>
Len Brownd8af6f52015-02-10 01:56:38 -050037#include <getopt.h>
Len Brown103a8fe2010-10-22 23:53:03 -040038#include <dirent.h>
39#include <string.h>
40#include <ctype.h>
Len Brown88c32812012-03-29 21:44:40 -040041#include <sched.h>
Len Brown2a0609c2016-02-12 22:44:48 -050042#include <time.h>
Josh Triplett2b928652013-08-20 17:20:14 -070043#include <cpuid.h>
Len Brown98481e72014-08-15 00:36:50 -040044#include <linux/capability.h>
45#include <errno.h>
Len Brown103a8fe2010-10-22 23:53:03 -040046
Len Brown103a8fe2010-10-22 23:53:03 -040047char *proc_stat = "/proc/stat";
Len Brownb7d8c142016-02-13 23:36:17 -050048FILE *outf;
Len Brown36229892016-02-26 20:51:02 -050049int *fd_percpu;
Len Brown2a0609c2016-02-12 22:44:48 -050050struct timespec interval_ts = {5, 0};
Len Brownd8af6f52015-02-10 01:56:38 -050051unsigned int debug;
52unsigned int rapl_joules;
53unsigned int summary_only;
54unsigned int dump_only;
Len Brown103a8fe2010-10-22 23:53:03 -040055unsigned int do_snb_cstates;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -070056unsigned int do_knl_cstates;
Len Brownee7e38e2015-02-09 23:39:45 -050057unsigned int do_pc2;
58unsigned int do_pc3;
59unsigned int do_pc6;
60unsigned int do_pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -080061unsigned int do_c8_c9_c10;
Len Brown0b2bb692015-03-26 00:50:30 -040062unsigned int do_skl_residency;
Len Brown144b44b2013-11-09 00:30:16 -050063unsigned int do_slm_cstates;
64unsigned int use_c1_residency_msr;
Len Brown103a8fe2010-10-22 23:53:03 -040065unsigned int has_aperf;
Len Brown889facb2012-11-08 00:48:57 -050066unsigned int has_epb;
Len Brown5a634262016-04-06 17:15:55 -040067unsigned int do_irtl_snb;
68unsigned int do_irtl_hsw;
Len Brownfc04cc62014-02-06 00:55:19 -050069unsigned int units = 1000000; /* MHz etc */
Len Brown103a8fe2010-10-22 23:53:03 -040070unsigned int genuine_intel;
71unsigned int has_invariant_tsc;
Len Brownd7899442015-01-23 00:12:33 -050072unsigned int do_nhm_platform_info;
Len Browncf4cbe52017-01-01 13:08:33 -050073unsigned int no_MSR_MISC_PWR_MGMT;
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +020074unsigned int aperf_mperf_multiplier = 1;
Len Brown103a8fe2010-10-22 23:53:03 -040075double bclk;
Len Browna2b7b742015-09-26 00:12:38 -040076double base_hz;
Len Brown21ed5572015-10-19 22:37:40 -040077unsigned int has_base_hz;
Len Browna2b7b742015-09-26 00:12:38 -040078double tsc_tweak = 1.0;
Len Brownc98d5d92012-06-04 00:56:40 -040079unsigned int show_pkg_only;
80unsigned int show_core_only;
81char *output_buffer, *outp;
Len Brown889facb2012-11-08 00:48:57 -050082unsigned int do_rapl;
83unsigned int do_dts;
84unsigned int do_ptm;
Len Brownfdf676e2016-02-27 01:28:12 -050085unsigned long long gfx_cur_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -050086unsigned int gfx_cur_mhz;
Len Brown889facb2012-11-08 00:48:57 -050087unsigned int tcc_activation_temp;
88unsigned int tcc_activation_temp_override;
Andrey Semin40ee8e32014-12-05 00:07:00 -050089double rapl_power_units, rapl_time_units;
90double rapl_dram_energy_units, rapl_energy_units;
Len Brown889facb2012-11-08 00:48:57 -050091double rapl_joule_counter_range;
Len Brown3a9a9412014-08-15 02:39:52 -040092unsigned int do_core_perf_limit_reasons;
93unsigned int do_gfx_perf_limit_reasons;
94unsigned int do_ring_perf_limit_reasons;
Len Brown8a5bdf42015-04-01 21:02:57 -040095unsigned int crystal_hz;
96unsigned long long tsc_hz;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -040097int base_cpu;
Len Brown21ed5572015-10-19 22:37:40 -040098double discover_bclk(unsigned int family, unsigned int model);
Len Brown7f5c2582015-12-01 01:36:39 -050099unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */
100 /* IA32_HWP_REQUEST, IA32_HWP_STATUS */
101unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */
102unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */
103unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */
104unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */
Len Brown889facb2012-11-08 00:48:57 -0500105
Len Browne6f9bb32013-12-03 02:19:19 -0500106#define RAPL_PKG (1 << 0)
107 /* 0x610 MSR_PKG_POWER_LIMIT */
108 /* 0x611 MSR_PKG_ENERGY_STATUS */
109#define RAPL_PKG_PERF_STATUS (1 << 1)
110 /* 0x613 MSR_PKG_PERF_STATUS */
111#define RAPL_PKG_POWER_INFO (1 << 2)
112 /* 0x614 MSR_PKG_POWER_INFO */
113
114#define RAPL_DRAM (1 << 3)
115 /* 0x618 MSR_DRAM_POWER_LIMIT */
116 /* 0x619 MSR_DRAM_ENERGY_STATUS */
Len Browne6f9bb32013-12-03 02:19:19 -0500117#define RAPL_DRAM_PERF_STATUS (1 << 4)
118 /* 0x61b MSR_DRAM_PERF_STATUS */
Len Brown0b2bb692015-03-26 00:50:30 -0400119#define RAPL_DRAM_POWER_INFO (1 << 5)
120 /* 0x61c MSR_DRAM_POWER_INFO */
Len Browne6f9bb32013-12-03 02:19:19 -0500121
Jacob Pan91484942016-06-16 09:48:20 -0700122#define RAPL_CORES_POWER_LIMIT (1 << 6)
Len Browne6f9bb32013-12-03 02:19:19 -0500123 /* 0x638 MSR_PP0_POWER_LIMIT */
Len Brown0b2bb692015-03-26 00:50:30 -0400124#define RAPL_CORE_POLICY (1 << 7)
Len Browne6f9bb32013-12-03 02:19:19 -0500125 /* 0x63a MSR_PP0_POLICY */
126
Len Brown0b2bb692015-03-26 00:50:30 -0400127#define RAPL_GFX (1 << 8)
Len Browne6f9bb32013-12-03 02:19:19 -0500128 /* 0x640 MSR_PP1_POWER_LIMIT */
129 /* 0x641 MSR_PP1_ENERGY_STATUS */
130 /* 0x642 MSR_PP1_POLICY */
Jacob Pan91484942016-06-16 09:48:20 -0700131
132#define RAPL_CORES_ENERGY_STATUS (1 << 9)
133 /* 0x639 MSR_PP0_ENERGY_STATUS */
134#define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT)
Len Brown889facb2012-11-08 00:48:57 -0500135#define TJMAX_DEFAULT 100
136
137#define MAX(a, b) ((a) > (b) ? (a) : (b))
Len Brown103a8fe2010-10-22 23:53:03 -0400138
Len Brown388e9c82016-12-22 23:57:55 -0500139/*
140 * buffer size used by sscanf() for added column names
141 * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters
142 */
143#define NAME_BYTES 20
144
Len Brown103a8fe2010-10-22 23:53:03 -0400145int backwards_count;
146char *progname;
Len Brown103a8fe2010-10-22 23:53:03 -0400147
Len Brownc98d5d92012-06-04 00:56:40 -0400148cpu_set_t *cpu_present_set, *cpu_affinity_set;
149size_t cpu_present_setsize, cpu_affinity_setsize;
Len Brown678a3bd2017-02-09 22:22:13 -0500150#define MAX_ADDED_COUNTERS 16
Len Brown103a8fe2010-10-22 23:53:03 -0400151
Len Brownc98d5d92012-06-04 00:56:40 -0400152struct thread_data {
153 unsigned long long tsc;
154 unsigned long long aperf;
155 unsigned long long mperf;
Len Brown144b44b2013-11-09 00:30:16 -0500156 unsigned long long c1;
Len Brown562a2d32016-02-26 23:48:05 -0500157 unsigned int irq_count;
Len Brown1ed51012013-02-10 17:19:24 -0500158 unsigned int smi_count;
Len Brownc98d5d92012-06-04 00:56:40 -0400159 unsigned int cpu_id;
160 unsigned int flags;
161#define CPU_IS_FIRST_THREAD_IN_CORE 0x2
162#define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
Len Brown678a3bd2017-02-09 22:22:13 -0500163 unsigned long long counter[MAX_ADDED_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400164} *thread_even, *thread_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400165
Len Brownc98d5d92012-06-04 00:56:40 -0400166struct core_data {
167 unsigned long long c3;
168 unsigned long long c6;
169 unsigned long long c7;
Len Brown0539ba12017-02-10 00:27:20 -0500170 unsigned long long mc6_us; /* duplicate as per-core for now, even though per module */
Len Brown889facb2012-11-08 00:48:57 -0500171 unsigned int core_temp_c;
Len Brownc98d5d92012-06-04 00:56:40 -0400172 unsigned int core_id;
Len Brown678a3bd2017-02-09 22:22:13 -0500173 unsigned long long counter[MAX_ADDED_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400174} *core_even, *core_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400175
Len Brownc98d5d92012-06-04 00:56:40 -0400176struct pkg_data {
177 unsigned long long pc2;
178 unsigned long long pc3;
179 unsigned long long pc6;
180 unsigned long long pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800181 unsigned long long pc8;
182 unsigned long long pc9;
183 unsigned long long pc10;
Len Brown0b2bb692015-03-26 00:50:30 -0400184 unsigned long long pkg_wtd_core_c0;
185 unsigned long long pkg_any_core_c0;
186 unsigned long long pkg_any_gfxe_c0;
187 unsigned long long pkg_both_core_gfxe_c0;
Len Brown9185e982016-04-06 17:16:00 -0400188 long long gfx_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -0500189 unsigned int gfx_mhz;
Len Brownc98d5d92012-06-04 00:56:40 -0400190 unsigned int package_id;
Len Brown889facb2012-11-08 00:48:57 -0500191 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
192 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
193 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
194 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
195 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
196 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
197 unsigned int pkg_temp_c;
Len Brown678a3bd2017-02-09 22:22:13 -0500198 unsigned long long counter[MAX_ADDED_COUNTERS];
Len Brownc98d5d92012-06-04 00:56:40 -0400199} *package_even, *package_odd;
200
201#define ODD_COUNTERS thread_odd, core_odd, package_odd
202#define EVEN_COUNTERS thread_even, core_even, package_even
203
204#define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
205 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
206 topo.num_threads_per_core + \
207 (core_no) * topo.num_threads_per_core + (thread_no))
208#define GET_CORE(core_base, core_no, pkg_no) \
209 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
210#define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
211
Len Brown388e9c82016-12-22 23:57:55 -0500212enum counter_scope {SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE};
213enum counter_type {COUNTER_CYCLES, COUNTER_SECONDS};
214enum counter_format {FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT};
215
216struct msr_counter {
217 unsigned int msr_num;
218 char name[NAME_BYTES];
219 unsigned int width;
220 enum counter_type type;
221 enum counter_format format;
222 struct msr_counter *next;
Len Brown812db3f2017-02-10 00:25:41 -0500223 unsigned int flags;
224#define FLAGS_HIDE (1 << 0)
225#define FLAGS_SHOW (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" },
350 { 0x0, "SMI", 32, 0, FORMAT_DELTA, NULL},
351 { 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" },
366 { 0x0, "PkgWatt" },
367 { 0x0, "CorWatt" },
368 { 0x0, "GFXWatt" },
369 { 0x0, "PkgCnt" },
370 { 0x0, "RAMWatt" },
371 { 0x0, "PKG_%" },
372 { 0x0, "RAM_%" },
373 { 0x0, "Pkg_J" },
374 { 0x0, "Cor_J" },
375 { 0x0, "GFX_J" },
376 { 0x0, "RAM_J" },
377 { 0x0, "Core" },
378 { 0x0, "CPU" },
Len Brown0539ba12017-02-10 00:27:20 -0500379 { 0x0, "Mod%c6" },
Len Brown812db3f2017-02-10 00:25:41 -0500380};
381
382#define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter))
383#define BIC_Package (1ULL << 0)
384#define BIC_Avg_MHz (1ULL << 1)
385#define BIC_Bzy_MHz (1ULL << 2)
386#define BIC_TSC_MHz (1ULL << 3)
387#define BIC_IRQ (1ULL << 4)
388#define BIC_SMI (1ULL << 5)
389#define BIC_Busy (1ULL << 6)
390#define BIC_CPU_c1 (1ULL << 7)
391#define BIC_CPU_c3 (1ULL << 8)
392#define BIC_CPU_c6 (1ULL << 9)
393#define BIC_CPU_c7 (1ULL << 10)
394#define BIC_ThreadC (1ULL << 11)
395#define BIC_CoreTmp (1ULL << 12)
396#define BIC_CoreCnt (1ULL << 13)
397#define BIC_PkgTmp (1ULL << 14)
398#define BIC_GFX_rc6 (1ULL << 15)
399#define BIC_GFXMHz (1ULL << 16)
400#define BIC_Pkgpc2 (1ULL << 17)
401#define BIC_Pkgpc3 (1ULL << 18)
402#define BIC_Pkgpc6 (1ULL << 19)
403#define BIC_Pkgpc7 (1ULL << 20)
404#define BIC_PkgWatt (1ULL << 21)
405#define BIC_CorWatt (1ULL << 22)
406#define BIC_GFXWatt (1ULL << 23)
407#define BIC_PkgCnt (1ULL << 24)
408#define BIC_RAMWatt (1ULL << 27)
409#define BIC_PKG__ (1ULL << 28)
410#define BIC_RAM__ (1ULL << 29)
411#define BIC_Pkg_J (1ULL << 30)
412#define BIC_Cor_J (1ULL << 31)
413#define BIC_GFX_J (1ULL << 30)
414#define BIC_RAM_J (1ULL << 31)
415#define BIC_Core (1ULL << 32)
416#define BIC_CPU (1ULL << 33)
Len Brown0539ba12017-02-10 00:27:20 -0500417#define BIC_Mod_c6 (1ULL << 34)
Len Brown812db3f2017-02-10 00:25:41 -0500418
419unsigned long long bic_enabled = 0xFFFFFFFFFFFFFFFFULL;
420unsigned long long bic_present;
421
422#define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME)
423#define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT)
424
425/*
426 * bic_lookup
427 * for all the strings in comma separate name_list,
428 * set the approprate bit in return value.
429 */
430unsigned long long bic_lookup(char *name_list)
431{
432 int i;
433 unsigned long long retval = 0;
434
435 while (name_list) {
436 char *comma;
437
438 comma = strchr(name_list, ',');
439
440 if (comma)
441 *comma = '\0';
442
443 for (i = 0; i < MAX_BIC; ++i) {
444 if (!strcmp(name_list, bic[i].name)) {
445 retval |= (1ULL << i);
446 break;
447 }
448 }
449 if (i == MAX_BIC) {
450 fprintf(stderr, "Invalid counter name: %s\n", name_list);
451 exit(-1);
452 }
453
454 name_list = comma;
455 if (name_list)
456 name_list++;
457
458 }
459 return retval;
460}
Len Brownfc04cc62014-02-06 00:55:19 -0500461
Len Browna829eb42011-02-10 23:36:34 -0500462void print_header(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400463{
Len Brown388e9c82016-12-22 23:57:55 -0500464 struct msr_counter *mp;
465
Len Brown812db3f2017-02-10 00:25:41 -0500466 if (DO_BIC(BIC_Package))
Len Brown3d109de2016-04-22 23:24:27 -0400467 outp += sprintf(outp, "\tPackage");
Len Brown812db3f2017-02-10 00:25:41 -0500468 if (DO_BIC(BIC_Core))
Len Brown3d109de2016-04-22 23:24:27 -0400469 outp += sprintf(outp, "\tCore");
Len Brown812db3f2017-02-10 00:25:41 -0500470 if (DO_BIC(BIC_CPU))
Len Brown3d109de2016-04-22 23:24:27 -0400471 outp += sprintf(outp, "\tCPU");
Len Brown812db3f2017-02-10 00:25:41 -0500472 if (DO_BIC(BIC_Avg_MHz))
Len Brown3d109de2016-04-22 23:24:27 -0400473 outp += sprintf(outp, "\tAvg_MHz");
Len Brown812db3f2017-02-10 00:25:41 -0500474 if (DO_BIC(BIC_Busy))
Len Brown3d109de2016-04-22 23:24:27 -0400475 outp += sprintf(outp, "\tBusy%%");
Len Brown812db3f2017-02-10 00:25:41 -0500476 if (DO_BIC(BIC_Bzy_MHz))
Len Brown3d109de2016-04-22 23:24:27 -0400477 outp += sprintf(outp, "\tBzy_MHz");
Len Brown812db3f2017-02-10 00:25:41 -0500478 if (DO_BIC(BIC_TSC_MHz))
479 outp += sprintf(outp, "\tTSC_MHz");
Len Brown1cc21f72015-02-23 00:34:57 -0500480
Len Brown1cc21f72015-02-23 00:34:57 -0500481 if (!debug)
482 goto done;
483
Len Brown812db3f2017-02-10 00:25:41 -0500484 if (DO_BIC(BIC_IRQ))
Len Brown3d109de2016-04-22 23:24:27 -0400485 outp += sprintf(outp, "\tIRQ");
Len Brown812db3f2017-02-10 00:25:41 -0500486 if (DO_BIC(BIC_SMI))
Len Brown3d109de2016-04-22 23:24:27 -0400487 outp += sprintf(outp, "\tSMI");
Len Brown1cc21f72015-02-23 00:34:57 -0500488
Len Brown812db3f2017-02-10 00:25:41 -0500489 if (DO_BIC(BIC_CPU_c1))
Len Brown3d109de2016-04-22 23:24:27 -0400490 outp += sprintf(outp, "\tCPU%%c1");
Len Brown889facb2012-11-08 00:48:57 -0500491
Len Brown388e9c82016-12-22 23:57:55 -0500492 for (mp = sys.tp; mp; mp = mp->next) {
493 if (mp->format == FORMAT_RAW) {
494 if (mp->width == 64)
495 outp += sprintf(outp, "\t%18.18s", mp->name);
496 else
497 outp += sprintf(outp, "\t%10.10s", mp->name);
498 } else {
499 outp += sprintf(outp, "\t%-7.7s", mp->name);
500 }
501 }
502
Len Brown812db3f2017-02-10 00:25:41 -0500503 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates)
Len Brown678a3bd2017-02-09 22:22:13 -0500504 outp += sprintf(outp, "\tCPU%%c3");
Len Brown812db3f2017-02-10 00:25:41 -0500505 if (DO_BIC(BIC_CPU_c6))
Len Brown678a3bd2017-02-09 22:22:13 -0500506 outp += sprintf(outp, "\tCPU%%c6");
Len Brown812db3f2017-02-10 00:25:41 -0500507 if (DO_BIC(BIC_CPU_c7))
Len Brown678a3bd2017-02-09 22:22:13 -0500508 outp += sprintf(outp, "\tCPU%%c7");
509
Len Brown0539ba12017-02-10 00:27:20 -0500510 if (DO_BIC(BIC_Mod_c6))
511 outp += sprintf(outp, "\tMod%%c6");
Len Brown678a3bd2017-02-09 22:22:13 -0500512
Len Brown812db3f2017-02-10 00:25:41 -0500513 if (DO_BIC(BIC_CoreTmp))
Len Brown3d109de2016-04-22 23:24:27 -0400514 outp += sprintf(outp, "\tCoreTmp");
Len Brown388e9c82016-12-22 23:57:55 -0500515
516 for (mp = sys.cp; mp; mp = mp->next) {
517 if (mp->format == FORMAT_RAW) {
518 if (mp->width == 64)
519 outp += sprintf(outp, "\t%18.18s", mp->name);
520 else
521 outp += sprintf(outp, "\t%10.10s", mp->name);
522 } else {
523 outp += sprintf(outp, "\t%-7.7s", mp->name);
524 }
525 }
526
Len Brown812db3f2017-02-10 00:25:41 -0500527 if (DO_BIC(BIC_PkgTmp))
Len Brown3d109de2016-04-22 23:24:27 -0400528 outp += sprintf(outp, "\tPkgTmp");
Len Brown889facb2012-11-08 00:48:57 -0500529
Len Brown812db3f2017-02-10 00:25:41 -0500530 if (DO_BIC(BIC_GFX_rc6))
Len Brown3d109de2016-04-22 23:24:27 -0400531 outp += sprintf(outp, "\tGFX%%rc6");
Len Brownfdf676e2016-02-27 01:28:12 -0500532
Len Brown812db3f2017-02-10 00:25:41 -0500533 if (DO_BIC(BIC_GFXMHz))
Len Brown3d109de2016-04-22 23:24:27 -0400534 outp += sprintf(outp, "\tGFXMHz");
Len Brown27d47352016-02-27 00:37:54 -0500535
Len Brown0b2bb692015-03-26 00:50:30 -0400536 if (do_skl_residency) {
Len Brown3d109de2016-04-22 23:24:27 -0400537 outp += sprintf(outp, "\tTotl%%C0");
538 outp += sprintf(outp, "\tAny%%C0");
539 outp += sprintf(outp, "\tGFX%%C0");
540 outp += sprintf(outp, "\tCPUGFX%%");
Len Brown0b2bb692015-03-26 00:50:30 -0400541 }
542
Len Brownee7e38e2015-02-09 23:39:45 -0500543 if (do_pc2)
Len Brown3d109de2016-04-22 23:24:27 -0400544 outp += sprintf(outp, "\tPkg%%pc2");
Len Brownee7e38e2015-02-09 23:39:45 -0500545 if (do_pc3)
Len Brown3d109de2016-04-22 23:24:27 -0400546 outp += sprintf(outp, "\tPkg%%pc3");
Len Brownee7e38e2015-02-09 23:39:45 -0500547 if (do_pc6)
Len Brown3d109de2016-04-22 23:24:27 -0400548 outp += sprintf(outp, "\tPkg%%pc6");
Len Brownee7e38e2015-02-09 23:39:45 -0500549 if (do_pc7)
Len Brown3d109de2016-04-22 23:24:27 -0400550 outp += sprintf(outp, "\tPkg%%pc7");
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800551 if (do_c8_c9_c10) {
Len Brown3d109de2016-04-22 23:24:27 -0400552 outp += sprintf(outp, "\tPkg%%pc8");
553 outp += sprintf(outp, "\tPkg%%pc9");
554 outp += sprintf(outp, "\tPk%%pc10");
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800555 }
Len Brown103a8fe2010-10-22 23:53:03 -0400556
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800557 if (do_rapl && !rapl_joules) {
Len Brown812db3f2017-02-10 00:25:41 -0500558 if (DO_BIC(BIC_PkgWatt))
Len Brown3d109de2016-04-22 23:24:27 -0400559 outp += sprintf(outp, "\tPkgWatt");
Len Brown812db3f2017-02-10 00:25:41 -0500560 if (DO_BIC(BIC_CorWatt))
Len Brown3d109de2016-04-22 23:24:27 -0400561 outp += sprintf(outp, "\tCorWatt");
Len Brown812db3f2017-02-10 00:25:41 -0500562 if (DO_BIC(BIC_GFXWatt))
Len Brown3d109de2016-04-22 23:24:27 -0400563 outp += sprintf(outp, "\tGFXWatt");
Len Brown812db3f2017-02-10 00:25:41 -0500564 if (DO_BIC(BIC_RAMWatt))
Len Brown3d109de2016-04-22 23:24:27 -0400565 outp += sprintf(outp, "\tRAMWatt");
Len Brown812db3f2017-02-10 00:25:41 -0500566 if (DO_BIC(BIC_PKG__))
Len Brown3d109de2016-04-22 23:24:27 -0400567 outp += sprintf(outp, "\tPKG_%%");
Len Brown812db3f2017-02-10 00:25:41 -0500568 if (DO_BIC(BIC_RAM__))
Len Brown3d109de2016-04-22 23:24:27 -0400569 outp += sprintf(outp, "\tRAM_%%");
Len Brownd7899442015-01-23 00:12:33 -0500570 } else if (do_rapl && rapl_joules) {
Len Brown812db3f2017-02-10 00:25:41 -0500571 if (DO_BIC(BIC_Pkg_J))
Len Brown3d109de2016-04-22 23:24:27 -0400572 outp += sprintf(outp, "\tPkg_J");
Len Brown812db3f2017-02-10 00:25:41 -0500573 if (DO_BIC(BIC_Cor_J))
Len Brown3d109de2016-04-22 23:24:27 -0400574 outp += sprintf(outp, "\tCor_J");
Len Brown812db3f2017-02-10 00:25:41 -0500575 if (DO_BIC(BIC_GFX_J))
Len Brown3d109de2016-04-22 23:24:27 -0400576 outp += sprintf(outp, "\tGFX_J");
Len Brown812db3f2017-02-10 00:25:41 -0500577 if (DO_BIC(BIC_RAM_J))
Len Brown3d109de2016-04-22 23:24:27 -0400578 outp += sprintf(outp, "\tRAM_J");
Len Brown812db3f2017-02-10 00:25:41 -0500579 if (DO_BIC(BIC_PKG__))
Len Brown3d109de2016-04-22 23:24:27 -0400580 outp += sprintf(outp, "\tPKG_%%");
Len Brown812db3f2017-02-10 00:25:41 -0500581 if (DO_BIC(BIC_RAM__))
Len Brown3d109de2016-04-22 23:24:27 -0400582 outp += sprintf(outp, "\tRAM_%%");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800583 }
Len Brown388e9c82016-12-22 23:57:55 -0500584 for (mp = sys.pp; mp; mp = mp->next) {
585 if (mp->format == FORMAT_RAW) {
586 if (mp->width == 64)
587 outp += sprintf(outp, "\t%18.18s", mp->name);
588 else
589 outp += sprintf(outp, "\t%10.10s", mp->name);
590 } else {
591 outp += sprintf(outp, "\t%-7.7s", mp->name);
592 }
593 }
594
595done:
Len Brownc98d5d92012-06-04 00:56:40 -0400596 outp += sprintf(outp, "\n");
Len Brown103a8fe2010-10-22 23:53:03 -0400597}
598
Len Brownc98d5d92012-06-04 00:56:40 -0400599int dump_counters(struct thread_data *t, struct core_data *c,
600 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400601{
Len Brown388e9c82016-12-22 23:57:55 -0500602 int i;
603 struct msr_counter *mp;
604
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200605 outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400606
Len Brownc98d5d92012-06-04 00:56:40 -0400607 if (t) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200608 outp += sprintf(outp, "CPU: %d flags 0x%x\n",
609 t->cpu_id, t->flags);
610 outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
611 outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
612 outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
613 outp += sprintf(outp, "c1: %016llX\n", t->c1);
Len Brown6886fee2016-12-24 15:18:37 -0500614
Len Brown812db3f2017-02-10 00:25:41 -0500615 if (DO_BIC(BIC_IRQ))
Len Brown562a2d32016-02-26 23:48:05 -0500616 outp += sprintf(outp, "IRQ: %08X\n", t->irq_count);
Len Brown812db3f2017-02-10 00:25:41 -0500617 if (DO_BIC(BIC_SMI))
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200618 outp += sprintf(outp, "SMI: %08X\n", t->smi_count);
Len Brown388e9c82016-12-22 23:57:55 -0500619
620 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
621 outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n",
622 i, mp->msr_num, t->counter[i]);
623 }
Len Brownc98d5d92012-06-04 00:56:40 -0400624 }
Len Brown103a8fe2010-10-22 23:53:03 -0400625
Len Brownc98d5d92012-06-04 00:56:40 -0400626 if (c) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200627 outp += sprintf(outp, "core: %d\n", c->core_id);
628 outp += sprintf(outp, "c3: %016llX\n", c->c3);
629 outp += sprintf(outp, "c6: %016llX\n", c->c6);
630 outp += sprintf(outp, "c7: %016llX\n", c->c7);
631 outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
Len Brown388e9c82016-12-22 23:57:55 -0500632
633 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
634 outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n",
635 i, mp->msr_num, c->counter[i]);
636 }
Len Brown0539ba12017-02-10 00:27:20 -0500637 outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us);
Len Brownc98d5d92012-06-04 00:56:40 -0400638 }
639
640 if (p) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200641 outp += sprintf(outp, "package: %d\n", p->package_id);
Len Brown0b2bb692015-03-26 00:50:30 -0400642
643 outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
644 outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
645 outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
646 outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
647
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200648 outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
Len Brownee7e38e2015-02-09 23:39:45 -0500649 if (do_pc3)
650 outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
651 if (do_pc6)
652 outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
653 if (do_pc7)
654 outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200655 outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
656 outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
657 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
658 outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
659 outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
660 outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
661 outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
662 outp += sprintf(outp, "Throttle PKG: %0X\n",
663 p->rapl_pkg_perf_status);
664 outp += sprintf(outp, "Throttle RAM: %0X\n",
665 p->rapl_dram_perf_status);
666 outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
Len Brown388e9c82016-12-22 23:57:55 -0500667
668 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
669 outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n",
670 i, mp->msr_num, p->counter[i]);
671 }
Len Brownc98d5d92012-06-04 00:56:40 -0400672 }
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200673
674 outp += sprintf(outp, "\n");
675
Len Brownc98d5d92012-06-04 00:56:40 -0400676 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400677}
678
Len Browne23da032012-02-06 18:37:16 -0500679/*
680 * column formatting convention & formats
Len Browne23da032012-02-06 18:37:16 -0500681 */
Len Brownc98d5d92012-06-04 00:56:40 -0400682int format_counters(struct thread_data *t, struct core_data *c,
683 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400684{
685 double interval_float;
Len Brownfc04cc62014-02-06 00:55:19 -0500686 char *fmt8;
Len Brown388e9c82016-12-22 23:57:55 -0500687 int i;
688 struct msr_counter *mp;
Len Brown103a8fe2010-10-22 23:53:03 -0400689
Len Brownc98d5d92012-06-04 00:56:40 -0400690 /* if showing only 1st thread in core and this isn't one, bail out */
691 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
692 return 0;
693
694 /* if showing only 1st thread in pkg and this isn't one, bail out */
695 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
696 return 0;
697
Len Brown103a8fe2010-10-22 23:53:03 -0400698 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
699
Len Brownc98d5d92012-06-04 00:56:40 -0400700 /* topo columns, print blanks on 1st (average) line */
701 if (t == &average.threads) {
Len Brown812db3f2017-02-10 00:25:41 -0500702 if (DO_BIC(BIC_Package))
Len Brown3d109de2016-04-22 23:24:27 -0400703 outp += sprintf(outp, "\t-");
Len Brown812db3f2017-02-10 00:25:41 -0500704 if (DO_BIC(BIC_Core))
Len Brown3d109de2016-04-22 23:24:27 -0400705 outp += sprintf(outp, "\t-");
Len Brown812db3f2017-02-10 00:25:41 -0500706 if (DO_BIC(BIC_CPU))
Len Brown3d109de2016-04-22 23:24:27 -0400707 outp += sprintf(outp, "\t-");
Len Brown103a8fe2010-10-22 23:53:03 -0400708 } else {
Len Brown812db3f2017-02-10 00:25:41 -0500709 if (DO_BIC(BIC_Package)) {
Len Brownc98d5d92012-06-04 00:56:40 -0400710 if (p)
Len Brown3d109de2016-04-22 23:24:27 -0400711 outp += sprintf(outp, "\t%d", p->package_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400712 else
Len Brown3d109de2016-04-22 23:24:27 -0400713 outp += sprintf(outp, "\t-");
Len Brownc98d5d92012-06-04 00:56:40 -0400714 }
Len Brown812db3f2017-02-10 00:25:41 -0500715 if (DO_BIC(BIC_Core)) {
Len Brownc98d5d92012-06-04 00:56:40 -0400716 if (c)
Len Brown3d109de2016-04-22 23:24:27 -0400717 outp += sprintf(outp, "\t%d", c->core_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400718 else
Len Brown3d109de2016-04-22 23:24:27 -0400719 outp += sprintf(outp, "\t-");
Len Brownc98d5d92012-06-04 00:56:40 -0400720 }
Len Brown812db3f2017-02-10 00:25:41 -0500721 if (DO_BIC(BIC_CPU))
Len Brown3d109de2016-04-22 23:24:27 -0400722 outp += sprintf(outp, "\t%d", t->cpu_id);
Len Brown103a8fe2010-10-22 23:53:03 -0400723 }
Len Brownfc04cc62014-02-06 00:55:19 -0500724
Len Brown812db3f2017-02-10 00:25:41 -0500725 if (DO_BIC(BIC_Avg_MHz))
Len Brown3d109de2016-04-22 23:24:27 -0400726 outp += sprintf(outp, "\t%.0f",
Len Brownfc04cc62014-02-06 00:55:19 -0500727 1.0 / units * t->aperf / interval_float);
728
Len Brown812db3f2017-02-10 00:25:41 -0500729 if (DO_BIC(BIC_Busy))
Len Brown3d109de2016-04-22 23:24:27 -0400730 outp += sprintf(outp, "\t%.2f", 100.0 * t->mperf/t->tsc/tsc_tweak);
Len Brown103a8fe2010-10-22 23:53:03 -0400731
Len Brown812db3f2017-02-10 00:25:41 -0500732 if (DO_BIC(BIC_Bzy_MHz)) {
Len Brown21ed5572015-10-19 22:37:40 -0400733 if (has_base_hz)
Len Brown3d109de2016-04-22 23:24:27 -0400734 outp += sprintf(outp, "\t%.0f", base_hz / units * t->aperf / t->mperf);
Len Brown21ed5572015-10-19 22:37:40 -0400735 else
Len Brown3d109de2016-04-22 23:24:27 -0400736 outp += sprintf(outp, "\t%.0f",
Len Brown21ed5572015-10-19 22:37:40 -0400737 1.0 * t->tsc / units * t->aperf / t->mperf / interval_float);
738 }
Len Brown103a8fe2010-10-22 23:53:03 -0400739
Len Brown812db3f2017-02-10 00:25:41 -0500740 if (DO_BIC(BIC_TSC_MHz))
741 outp += sprintf(outp, "\t%.0f", 1.0 * t->tsc/units/interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400742
Len Brown1cc21f72015-02-23 00:34:57 -0500743 if (!debug)
744 goto done;
745
Len Brown562a2d32016-02-26 23:48:05 -0500746 /* IRQ */
Len Brown812db3f2017-02-10 00:25:41 -0500747 if (DO_BIC(BIC_IRQ))
Len Brown3d109de2016-04-22 23:24:27 -0400748 outp += sprintf(outp, "\t%d", t->irq_count);
Len Brown562a2d32016-02-26 23:48:05 -0500749
Len Brown1cc21f72015-02-23 00:34:57 -0500750 /* SMI */
Len Brown812db3f2017-02-10 00:25:41 -0500751 if (DO_BIC(BIC_SMI))
Len Brown3d109de2016-04-22 23:24:27 -0400752 outp += sprintf(outp, "\t%d", t->smi_count);
Len Brown1cc21f72015-02-23 00:34:57 -0500753
Len Brown678a3bd2017-02-09 22:22:13 -0500754 /* C1 */
Len Brown812db3f2017-02-10 00:25:41 -0500755 if (DO_BIC(BIC_CPU_c1))
Len Brown3d109de2016-04-22 23:24:27 -0400756 outp += sprintf(outp, "\t%.2f", 100.0 * t->c1/t->tsc);
Len Brownc98d5d92012-06-04 00:56:40 -0400757
Len Brown678a3bd2017-02-09 22:22:13 -0500758 /* Added counters */
759 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
760 if (mp->format == FORMAT_RAW) {
761 if (mp->width == 32)
762 outp += sprintf(outp, "\t0x%08lx", (unsigned long) t->counter[i]);
763 else
764 outp += sprintf(outp, "\t0x%016llx", t->counter[i]);
765 } else if (mp->format == FORMAT_DELTA) {
766 outp += sprintf(outp, "\t%lld", t->counter[i]);
767 } else if (mp->format == FORMAT_PERCENT) {
768 outp += sprintf(outp, "\t%.2f", 100.0 * t->counter[i]/t->tsc);
769 }
770 }
771
Len Brownc98d5d92012-06-04 00:56:40 -0400772 /* print per-core data only for 1st thread in core */
773 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
774 goto done;
775
Len Brown812db3f2017-02-10 00:25:41 -0500776 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates)
Len Brown3d109de2016-04-22 23:24:27 -0400777 outp += sprintf(outp, "\t%.2f", 100.0 * c->c3/t->tsc);
Len Brown812db3f2017-02-10 00:25:41 -0500778 if (DO_BIC(BIC_CPU_c6))
Len Brown3d109de2016-04-22 23:24:27 -0400779 outp += sprintf(outp, "\t%.2f", 100.0 * c->c6/t->tsc);
Len Brown812db3f2017-02-10 00:25:41 -0500780 if (DO_BIC(BIC_CPU_c7))
Len Brown3d109de2016-04-22 23:24:27 -0400781 outp += sprintf(outp, "\t%.2f", 100.0 * c->c7/t->tsc);
Len Brownc98d5d92012-06-04 00:56:40 -0400782
Len Brown0539ba12017-02-10 00:27:20 -0500783 /* Mod%c6 */
784 if (DO_BIC(BIC_Mod_c6))
785 outp += sprintf(outp, "\t%.2f", 100.0 * c->mc6_us / t->tsc);
786
Len Brown812db3f2017-02-10 00:25:41 -0500787 if (DO_BIC(BIC_CoreTmp))
Len Brown3d109de2016-04-22 23:24:27 -0400788 outp += sprintf(outp, "\t%d", c->core_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500789
Len Brown388e9c82016-12-22 23:57:55 -0500790 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
791 if (mp->format == FORMAT_RAW) {
792 if (mp->width == 32)
793 outp += sprintf(outp, "\t0x%08lx", (unsigned long) c->counter[i]);
794 else
795 outp += sprintf(outp, "\t0x%016llx", c->counter[i]);
796 } else if (mp->format == FORMAT_DELTA) {
Len Brown678a3bd2017-02-09 22:22:13 -0500797 outp += sprintf(outp, "\t%lld", c->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500798 } else if (mp->format == FORMAT_PERCENT) {
799 outp += sprintf(outp, "\t%.2f", 100.0 * c->counter[i]/t->tsc);
800 }
801 }
802
Len Brownc98d5d92012-06-04 00:56:40 -0400803 /* print per-package data only for 1st core in package */
804 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
805 goto done;
806
Len Brown0b2bb692015-03-26 00:50:30 -0400807 /* PkgTmp */
Len Brown812db3f2017-02-10 00:25:41 -0500808 if (DO_BIC(BIC_PkgTmp))
Len Brown3d109de2016-04-22 23:24:27 -0400809 outp += sprintf(outp, "\t%d", p->pkg_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500810
Len Brownfdf676e2016-02-27 01:28:12 -0500811 /* GFXrc6 */
Len Brown812db3f2017-02-10 00:25:41 -0500812 if (DO_BIC(BIC_GFX_rc6)) {
Len Brownba3dec92016-04-22 20:31:46 -0400813 if (p->gfx_rc6_ms == -1) { /* detect GFX counter reset */
Len Brown3d109de2016-04-22 23:24:27 -0400814 outp += sprintf(outp, "\t**.**");
Len Brown9185e982016-04-06 17:16:00 -0400815 } else {
Len Brown3d109de2016-04-22 23:24:27 -0400816 outp += sprintf(outp, "\t%.2f",
Len Brown9185e982016-04-06 17:16:00 -0400817 p->gfx_rc6_ms / 10.0 / interval_float);
818 }
819 }
Len Brownfdf676e2016-02-27 01:28:12 -0500820
Len Brown27d47352016-02-27 00:37:54 -0500821 /* GFXMHz */
Len Brown812db3f2017-02-10 00:25:41 -0500822 if (DO_BIC(BIC_GFXMHz))
Len Brown3d109de2016-04-22 23:24:27 -0400823 outp += sprintf(outp, "\t%d", p->gfx_mhz);
Len Brown27d47352016-02-27 00:37:54 -0500824
Len Brown0b2bb692015-03-26 00:50:30 -0400825 /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
826 if (do_skl_residency) {
Len Brown3d109de2016-04-22 23:24:27 -0400827 outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_wtd_core_c0/t->tsc);
828 outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_any_core_c0/t->tsc);
829 outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_any_gfxe_c0/t->tsc);
830 outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_both_core_gfxe_c0/t->tsc);
Len Brown0b2bb692015-03-26 00:50:30 -0400831 }
832
Len Brownee7e38e2015-02-09 23:39:45 -0500833 if (do_pc2)
Len Brown3d109de2016-04-22 23:24:27 -0400834 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc2/t->tsc);
Len Brownee7e38e2015-02-09 23:39:45 -0500835 if (do_pc3)
Len Brown3d109de2016-04-22 23:24:27 -0400836 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc3/t->tsc);
Len Brownee7e38e2015-02-09 23:39:45 -0500837 if (do_pc6)
Len Brown3d109de2016-04-22 23:24:27 -0400838 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc6/t->tsc);
Len Brownee7e38e2015-02-09 23:39:45 -0500839 if (do_pc7)
Len Brown3d109de2016-04-22 23:24:27 -0400840 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc7/t->tsc);
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800841 if (do_c8_c9_c10) {
Len Brown3d109de2016-04-22 23:24:27 -0400842 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc8/t->tsc);
843 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc9/t->tsc);
844 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc10/t->tsc);
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800845 }
Len Brown889facb2012-11-08 00:48:57 -0500846
847 /*
848 * If measurement interval exceeds minimum RAPL Joule Counter range,
849 * indicate that results are suspect by printing "**" in fraction place.
850 */
Len Brownfc04cc62014-02-06 00:55:19 -0500851 if (interval_float < rapl_joule_counter_range)
Len Brown3d109de2016-04-22 23:24:27 -0400852 fmt8 = "\t%.2f";
Len Brownfc04cc62014-02-06 00:55:19 -0500853 else
Len Browne975db52016-04-06 23:56:02 -0400854 fmt8 = "%6.0f**";
Len Brown889facb2012-11-08 00:48:57 -0500855
Len Brown812db3f2017-02-10 00:25:41 -0500856 if (DO_BIC(BIC_PkgWatt))
857 outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units / interval_float);
858 if (DO_BIC(BIC_CorWatt))
859 outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units / interval_float);
860 if (DO_BIC(BIC_GFXWatt))
861 outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units / interval_float);
862 if (DO_BIC(BIC_RAMWatt))
863 outp += sprintf(outp, fmt8, p->energy_dram * rapl_dram_energy_units / interval_float);
864 if (DO_BIC(BIC_Pkg_J))
865 outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units);
866 if (DO_BIC(BIC_Cor_J))
867 outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units);
868 if (DO_BIC(BIC_GFX_J))
869 outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units);
870 if (DO_BIC(BIC_RAM_J))
871 outp += sprintf(outp, fmt8, p->energy_dram * rapl_dram_energy_units);
872 if (DO_BIC(BIC_PKG__))
873 outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
874 if (DO_BIC(BIC_RAM__))
875 outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
876
Len Brown388e9c82016-12-22 23:57:55 -0500877 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
878 if (mp->format == FORMAT_RAW) {
879 if (mp->width == 32)
880 outp += sprintf(outp, "\t0x%08lx", (unsigned long) p->counter[i]);
881 else
882 outp += sprintf(outp, "\t0x%016llx", p->counter[i]);
883 } else if (mp->format == FORMAT_DELTA) {
Len Brown678a3bd2017-02-09 22:22:13 -0500884 outp += sprintf(outp, "\t%lld", p->counter[i]);
Len Brown388e9c82016-12-22 23:57:55 -0500885 } else if (mp->format == FORMAT_PERCENT) {
886 outp += sprintf(outp, "\t%.2f", 100.0 * p->counter[i]/t->tsc);
887 }
888 }
889
Len Brownc98d5d92012-06-04 00:56:40 -0400890done:
Len Brownc98d5d92012-06-04 00:56:40 -0400891 outp += sprintf(outp, "\n");
892
893 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400894}
895
Len Brownb7d8c142016-02-13 23:36:17 -0500896void flush_output_stdout(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400897{
Len Brownb7d8c142016-02-13 23:36:17 -0500898 FILE *filep;
899
900 if (outf == stderr)
901 filep = stdout;
902 else
903 filep = outf;
904
905 fputs(output_buffer, filep);
906 fflush(filep);
907
Len Brownc98d5d92012-06-04 00:56:40 -0400908 outp = output_buffer;
909}
Len Brownb7d8c142016-02-13 23:36:17 -0500910void flush_output_stderr(void)
Len Brownc98d5d92012-06-04 00:56:40 -0400911{
Len Brownb7d8c142016-02-13 23:36:17 -0500912 fputs(output_buffer, outf);
913 fflush(outf);
Len Brownc98d5d92012-06-04 00:56:40 -0400914 outp = output_buffer;
915}
916void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
917{
Len Browne23da032012-02-06 18:37:16 -0500918 static int printed;
Len Brown103a8fe2010-10-22 23:53:03 -0400919
Len Browne23da032012-02-06 18:37:16 -0500920 if (!printed || !summary_only)
921 print_header();
Len Brown103a8fe2010-10-22 23:53:03 -0400922
Len Brownc98d5d92012-06-04 00:56:40 -0400923 if (topo.num_cpus > 1)
924 format_counters(&average.threads, &average.cores,
925 &average.packages);
Len Brown103a8fe2010-10-22 23:53:03 -0400926
Len Browne23da032012-02-06 18:37:16 -0500927 printed = 1;
928
929 if (summary_only)
930 return;
931
Len Brownc98d5d92012-06-04 00:56:40 -0400932 for_all_cpus(format_counters, t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400933}
934
Len Brown889facb2012-11-08 00:48:57 -0500935#define DELTA_WRAP32(new, old) \
936 if (new > old) { \
937 old = new - old; \
938 } else { \
939 old = 0x100000000 + new - old; \
940 }
941
Len Brownba3dec92016-04-22 20:31:46 -0400942int
Len Brownc98d5d92012-06-04 00:56:40 -0400943delta_package(struct pkg_data *new, struct pkg_data *old)
Len Brown103a8fe2010-10-22 23:53:03 -0400944{
Len Brown388e9c82016-12-22 23:57:55 -0500945 int i;
946 struct msr_counter *mp;
Len Brown0b2bb692015-03-26 00:50:30 -0400947
948 if (do_skl_residency) {
949 old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
950 old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
951 old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
952 old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0;
953 }
Len Brownc98d5d92012-06-04 00:56:40 -0400954 old->pc2 = new->pc2 - old->pc2;
Len Brownee7e38e2015-02-09 23:39:45 -0500955 if (do_pc3)
956 old->pc3 = new->pc3 - old->pc3;
957 if (do_pc6)
958 old->pc6 = new->pc6 - old->pc6;
959 if (do_pc7)
960 old->pc7 = new->pc7 - old->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800961 old->pc8 = new->pc8 - old->pc8;
962 old->pc9 = new->pc9 - old->pc9;
963 old->pc10 = new->pc10 - old->pc10;
Len Brown889facb2012-11-08 00:48:57 -0500964 old->pkg_temp_c = new->pkg_temp_c;
965
Len Brown9185e982016-04-06 17:16:00 -0400966 /* flag an error when rc6 counter resets/wraps */
967 if (old->gfx_rc6_ms > new->gfx_rc6_ms)
968 old->gfx_rc6_ms = -1;
969 else
970 old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms;
971
Len Brown27d47352016-02-27 00:37:54 -0500972 old->gfx_mhz = new->gfx_mhz;
973
Len Brown889facb2012-11-08 00:48:57 -0500974 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
975 DELTA_WRAP32(new->energy_cores, old->energy_cores);
976 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
977 DELTA_WRAP32(new->energy_dram, old->energy_dram);
978 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
979 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
Len Brownba3dec92016-04-22 20:31:46 -0400980
Len Brown388e9c82016-12-22 23:57:55 -0500981 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
982 if (mp->format == FORMAT_RAW)
983 old->counter[i] = new->counter[i];
984 else
985 old->counter[i] = new->counter[i] - old->counter[i];
986 }
987
Len Brownba3dec92016-04-22 20:31:46 -0400988 return 0;
Len Brownc98d5d92012-06-04 00:56:40 -0400989}
Len Brown103a8fe2010-10-22 23:53:03 -0400990
Len Brownc98d5d92012-06-04 00:56:40 -0400991void
992delta_core(struct core_data *new, struct core_data *old)
993{
Len Brown388e9c82016-12-22 23:57:55 -0500994 int i;
995 struct msr_counter *mp;
996
Len Brownc98d5d92012-06-04 00:56:40 -0400997 old->c3 = new->c3 - old->c3;
998 old->c6 = new->c6 - old->c6;
999 old->c7 = new->c7 - old->c7;
Len Brown889facb2012-11-08 00:48:57 -05001000 old->core_temp_c = new->core_temp_c;
Len Brown0539ba12017-02-10 00:27:20 -05001001 old->mc6_us = new->mc6_us - old->mc6_us;
Len Brown388e9c82016-12-22 23:57:55 -05001002
1003 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1004 if (mp->format == FORMAT_RAW)
1005 old->counter[i] = new->counter[i];
1006 else
1007 old->counter[i] = new->counter[i] - old->counter[i];
1008 }
Len Brownc98d5d92012-06-04 00:56:40 -04001009}
Len Brown103a8fe2010-10-22 23:53:03 -04001010
Len Brownc3ae3312012-06-13 21:31:46 -04001011/*
1012 * old = new - old
1013 */
Len Brownba3dec92016-04-22 20:31:46 -04001014int
Len Brownc98d5d92012-06-04 00:56:40 -04001015delta_thread(struct thread_data *new, struct thread_data *old,
1016 struct core_data *core_delta)
1017{
Len Brown388e9c82016-12-22 23:57:55 -05001018 int i;
1019 struct msr_counter *mp;
1020
Len Brownc98d5d92012-06-04 00:56:40 -04001021 old->tsc = new->tsc - old->tsc;
Len Brown103a8fe2010-10-22 23:53:03 -04001022
Len Brownc98d5d92012-06-04 00:56:40 -04001023 /* check for TSC < 1 Mcycles over interval */
Josh Triplettb2c95d92013-08-20 17:20:18 -07001024 if (old->tsc < (1000 * 1000))
1025 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
1026 "You can disable all c-states by booting with \"idle=poll\"\n"
1027 "or just the deep ones with \"processor.max_cstate=1\"");
Len Brown103a8fe2010-10-22 23:53:03 -04001028
Len Brownc98d5d92012-06-04 00:56:40 -04001029 old->c1 = new->c1 - old->c1;
Len Brown103a8fe2010-10-22 23:53:03 -04001030
Len Brown812db3f2017-02-10 00:25:41 -05001031 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
Len Browna7296172015-01-23 01:33:58 -05001032 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
1033 old->aperf = new->aperf - old->aperf;
1034 old->mperf = new->mperf - old->mperf;
1035 } else {
Len Brownba3dec92016-04-22 20:31:46 -04001036 return -1;
Len Brownc98d5d92012-06-04 00:56:40 -04001037 }
Len Brownc98d5d92012-06-04 00:56:40 -04001038 }
Len Brown103a8fe2010-10-22 23:53:03 -04001039
Len Brown103a8fe2010-10-22 23:53:03 -04001040
Len Brown144b44b2013-11-09 00:30:16 -05001041 if (use_c1_residency_msr) {
1042 /*
1043 * Some models have a dedicated C1 residency MSR,
1044 * which should be more accurate than the derivation below.
1045 */
1046 } else {
1047 /*
1048 * As counter collection is not atomic,
1049 * it is possible for mperf's non-halted cycles + idle states
1050 * to exceed TSC's all cycles: show c1 = 0% in that case.
1051 */
1052 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
1053 old->c1 = 0;
1054 else {
1055 /* normal case, derive c1 */
1056 old->c1 = old->tsc - old->mperf - core_delta->c3
Len Brownc98d5d92012-06-04 00:56:40 -04001057 - core_delta->c6 - core_delta->c7;
Len Brown144b44b2013-11-09 00:30:16 -05001058 }
Len Brownc98d5d92012-06-04 00:56:40 -04001059 }
Len Brownc3ae3312012-06-13 21:31:46 -04001060
Len Brownc98d5d92012-06-04 00:56:40 -04001061 if (old->mperf == 0) {
Len Brownb7d8c142016-02-13 23:36:17 -05001062 if (debug > 1)
1063 fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
Len Brownc98d5d92012-06-04 00:56:40 -04001064 old->mperf = 1; /* divide by 0 protection */
1065 }
1066
Len Brown812db3f2017-02-10 00:25:41 -05001067 if (DO_BIC(BIC_IRQ))
Len Brown562a2d32016-02-26 23:48:05 -05001068 old->irq_count = new->irq_count - old->irq_count;
1069
Len Brown812db3f2017-02-10 00:25:41 -05001070 if (DO_BIC(BIC_SMI))
Len Brown1ed51012013-02-10 17:19:24 -05001071 old->smi_count = new->smi_count - old->smi_count;
Len Brownba3dec92016-04-22 20:31:46 -04001072
Len Brown388e9c82016-12-22 23:57:55 -05001073 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1074 if (mp->format == FORMAT_RAW)
1075 old->counter[i] = new->counter[i];
1076 else
1077 old->counter[i] = new->counter[i] - old->counter[i];
1078 }
Len Brownba3dec92016-04-22 20:31:46 -04001079 return 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001080}
1081
1082int delta_cpu(struct thread_data *t, struct core_data *c,
1083 struct pkg_data *p, struct thread_data *t2,
1084 struct core_data *c2, struct pkg_data *p2)
1085{
Len Brownba3dec92016-04-22 20:31:46 -04001086 int retval = 0;
1087
Len Brownc98d5d92012-06-04 00:56:40 -04001088 /* calculate core delta only for 1st thread in core */
1089 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
1090 delta_core(c, c2);
1091
1092 /* always calculate thread delta */
Len Brownba3dec92016-04-22 20:31:46 -04001093 retval = delta_thread(t, t2, c2); /* c2 is core delta */
1094 if (retval)
1095 return retval;
Len Brownc98d5d92012-06-04 00:56:40 -04001096
1097 /* calculate package delta only for 1st core in package */
1098 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
Len Brownba3dec92016-04-22 20:31:46 -04001099 retval = delta_package(p, p2);
Len Brownc98d5d92012-06-04 00:56:40 -04001100
Len Brownba3dec92016-04-22 20:31:46 -04001101 return retval;
Len Brownc98d5d92012-06-04 00:56:40 -04001102}
1103
1104void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1105{
Len Brown388e9c82016-12-22 23:57:55 -05001106 int i;
1107 struct msr_counter *mp;
1108
Len Brownc98d5d92012-06-04 00:56:40 -04001109 t->tsc = 0;
1110 t->aperf = 0;
1111 t->mperf = 0;
1112 t->c1 = 0;
1113
Len Brown562a2d32016-02-26 23:48:05 -05001114 t->irq_count = 0;
1115 t->smi_count = 0;
1116
Len Brownc98d5d92012-06-04 00:56:40 -04001117 /* tells format_counters to dump all fields from this set */
1118 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
1119
1120 c->c3 = 0;
1121 c->c6 = 0;
1122 c->c7 = 0;
Len Brown0539ba12017-02-10 00:27:20 -05001123 c->mc6_us = 0;
Len Brown889facb2012-11-08 00:48:57 -05001124 c->core_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001125
Len Brown0b2bb692015-03-26 00:50:30 -04001126 p->pkg_wtd_core_c0 = 0;
1127 p->pkg_any_core_c0 = 0;
1128 p->pkg_any_gfxe_c0 = 0;
1129 p->pkg_both_core_gfxe_c0 = 0;
1130
Len Brownc98d5d92012-06-04 00:56:40 -04001131 p->pc2 = 0;
Len Brownee7e38e2015-02-09 23:39:45 -05001132 if (do_pc3)
1133 p->pc3 = 0;
1134 if (do_pc6)
1135 p->pc6 = 0;
1136 if (do_pc7)
1137 p->pc7 = 0;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001138 p->pc8 = 0;
1139 p->pc9 = 0;
1140 p->pc10 = 0;
Len Brown889facb2012-11-08 00:48:57 -05001141
1142 p->energy_pkg = 0;
1143 p->energy_dram = 0;
1144 p->energy_cores = 0;
1145 p->energy_gfx = 0;
1146 p->rapl_pkg_perf_status = 0;
1147 p->rapl_dram_perf_status = 0;
1148 p->pkg_temp_c = 0;
Len Brown27d47352016-02-27 00:37:54 -05001149
Len Brownfdf676e2016-02-27 01:28:12 -05001150 p->gfx_rc6_ms = 0;
Len Brown27d47352016-02-27 00:37:54 -05001151 p->gfx_mhz = 0;
Len Brown388e9c82016-12-22 23:57:55 -05001152 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next)
1153 t->counter[i] = 0;
1154
1155 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next)
1156 c->counter[i] = 0;
1157
1158 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next)
1159 p->counter[i] = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001160}
1161int sum_counters(struct thread_data *t, struct core_data *c,
1162 struct pkg_data *p)
1163{
Len Brown388e9c82016-12-22 23:57:55 -05001164 int i;
1165 struct msr_counter *mp;
1166
Len Brownc98d5d92012-06-04 00:56:40 -04001167 average.threads.tsc += t->tsc;
1168 average.threads.aperf += t->aperf;
1169 average.threads.mperf += t->mperf;
1170 average.threads.c1 += t->c1;
1171
Len Brown562a2d32016-02-26 23:48:05 -05001172 average.threads.irq_count += t->irq_count;
1173 average.threads.smi_count += t->smi_count;
1174
Len Brown388e9c82016-12-22 23:57:55 -05001175 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1176 if (mp->format == FORMAT_RAW)
1177 continue;
1178 average.threads.counter[i] += t->counter[i];
1179 }
1180
Len Brownc98d5d92012-06-04 00:56:40 -04001181 /* sum per-core values only for 1st thread in core */
1182 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1183 return 0;
1184
1185 average.cores.c3 += c->c3;
1186 average.cores.c6 += c->c6;
1187 average.cores.c7 += c->c7;
Len Brown0539ba12017-02-10 00:27:20 -05001188 average.cores.mc6_us += c->mc6_us;
Len Brownc98d5d92012-06-04 00:56:40 -04001189
Len Brown889facb2012-11-08 00:48:57 -05001190 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
1191
Len Brown388e9c82016-12-22 23:57:55 -05001192 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1193 if (mp->format == FORMAT_RAW)
1194 continue;
1195 average.cores.counter[i] += c->counter[i];
1196 }
1197
Len Brownc98d5d92012-06-04 00:56:40 -04001198 /* sum per-pkg values only for 1st core in pkg */
1199 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1200 return 0;
1201
Len Brown0b2bb692015-03-26 00:50:30 -04001202 if (do_skl_residency) {
1203 average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
1204 average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
1205 average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
1206 average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
1207 }
1208
Len Brownc98d5d92012-06-04 00:56:40 -04001209 average.packages.pc2 += p->pc2;
Len Brownee7e38e2015-02-09 23:39:45 -05001210 if (do_pc3)
1211 average.packages.pc3 += p->pc3;
1212 if (do_pc6)
1213 average.packages.pc6 += p->pc6;
1214 if (do_pc7)
1215 average.packages.pc7 += p->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001216 average.packages.pc8 += p->pc8;
1217 average.packages.pc9 += p->pc9;
1218 average.packages.pc10 += p->pc10;
Len Brownc98d5d92012-06-04 00:56:40 -04001219
Len Brown889facb2012-11-08 00:48:57 -05001220 average.packages.energy_pkg += p->energy_pkg;
1221 average.packages.energy_dram += p->energy_dram;
1222 average.packages.energy_cores += p->energy_cores;
1223 average.packages.energy_gfx += p->energy_gfx;
1224
Len Brownfdf676e2016-02-27 01:28:12 -05001225 average.packages.gfx_rc6_ms = p->gfx_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -05001226 average.packages.gfx_mhz = p->gfx_mhz;
1227
Len Brown889facb2012-11-08 00:48:57 -05001228 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
1229
1230 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
1231 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
Len Brown388e9c82016-12-22 23:57:55 -05001232
1233 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1234 if (mp->format == FORMAT_RAW)
1235 continue;
1236 average.packages.counter[i] += p->counter[i];
1237 }
Len Brownc98d5d92012-06-04 00:56:40 -04001238 return 0;
1239}
1240/*
1241 * sum the counters for all cpus in the system
1242 * compute the weighted average
1243 */
1244void compute_average(struct thread_data *t, struct core_data *c,
1245 struct pkg_data *p)
1246{
Len Brown388e9c82016-12-22 23:57:55 -05001247 int i;
1248 struct msr_counter *mp;
1249
Len Brownc98d5d92012-06-04 00:56:40 -04001250 clear_counters(&average.threads, &average.cores, &average.packages);
1251
1252 for_all_cpus(sum_counters, t, c, p);
1253
1254 average.threads.tsc /= topo.num_cpus;
1255 average.threads.aperf /= topo.num_cpus;
1256 average.threads.mperf /= topo.num_cpus;
1257 average.threads.c1 /= topo.num_cpus;
1258
1259 average.cores.c3 /= topo.num_cores;
1260 average.cores.c6 /= topo.num_cores;
1261 average.cores.c7 /= topo.num_cores;
Len Brown0539ba12017-02-10 00:27:20 -05001262 average.cores.mc6_us /= topo.num_cores;
Len Brownc98d5d92012-06-04 00:56:40 -04001263
Len Brown0b2bb692015-03-26 00:50:30 -04001264 if (do_skl_residency) {
1265 average.packages.pkg_wtd_core_c0 /= topo.num_packages;
1266 average.packages.pkg_any_core_c0 /= topo.num_packages;
1267 average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
1268 average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
1269 }
1270
Len Brownc98d5d92012-06-04 00:56:40 -04001271 average.packages.pc2 /= topo.num_packages;
Len Brownee7e38e2015-02-09 23:39:45 -05001272 if (do_pc3)
1273 average.packages.pc3 /= topo.num_packages;
1274 if (do_pc6)
1275 average.packages.pc6 /= topo.num_packages;
1276 if (do_pc7)
1277 average.packages.pc7 /= topo.num_packages;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001278
1279 average.packages.pc8 /= topo.num_packages;
1280 average.packages.pc9 /= topo.num_packages;
1281 average.packages.pc10 /= topo.num_packages;
Len Brown388e9c82016-12-22 23:57:55 -05001282
1283 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1284 if (mp->format == FORMAT_RAW)
1285 continue;
1286 average.threads.counter[i] /= topo.num_cpus;
1287 }
1288 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1289 if (mp->format == FORMAT_RAW)
1290 continue;
1291 average.cores.counter[i] /= topo.num_cores;
1292 }
1293 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1294 if (mp->format == FORMAT_RAW)
1295 continue;
1296 average.packages.counter[i] /= topo.num_packages;
1297 }
Len Brownc98d5d92012-06-04 00:56:40 -04001298}
1299
1300static unsigned long long rdtsc(void)
1301{
1302 unsigned int low, high;
1303
1304 asm volatile("rdtsc" : "=a" (low), "=d" (high));
1305
1306 return low | ((unsigned long long)high) << 32;
1307}
1308
Len Brownc98d5d92012-06-04 00:56:40 -04001309/*
1310 * get_counters(...)
1311 * migrate to cpu
1312 * acquire and record local counters for that cpu
1313 */
1314int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1315{
1316 int cpu = t->cpu_id;
Len Brown889facb2012-11-08 00:48:57 -05001317 unsigned long long msr;
Len Brown0102b062016-02-27 03:11:29 -05001318 int aperf_mperf_retry_count = 0;
Len Brown388e9c82016-12-22 23:57:55 -05001319 struct msr_counter *mp;
1320 int i;
Len Brownc98d5d92012-06-04 00:56:40 -04001321
Len Browne52966c2012-11-08 22:38:05 -05001322 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05001323 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brownc98d5d92012-06-04 00:56:40 -04001324 return -1;
Len Browne52966c2012-11-08 22:38:05 -05001325 }
Len Brownc98d5d92012-06-04 00:56:40 -04001326
Len Brown0102b062016-02-27 03:11:29 -05001327retry:
Len Brownc98d5d92012-06-04 00:56:40 -04001328 t->tsc = rdtsc(); /* we are running on local CPU of interest */
1329
Len Brown812db3f2017-02-10 00:25:41 -05001330 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
Len Brown0102b062016-02-27 03:11:29 -05001331 unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time;
1332
1333 /*
1334 * The TSC, APERF and MPERF must be read together for
1335 * APERF/MPERF and MPERF/TSC to give accurate results.
1336 *
1337 * Unfortunately, APERF and MPERF are read by
1338 * individual system call, so delays may occur
1339 * between them. If the time to read them
1340 * varies by a large amount, we re-read them.
1341 */
1342
1343 /*
1344 * This initial dummy APERF read has been seen to
1345 * reduce jitter in the subsequent reads.
1346 */
1347
Len Brown9c63a652012-10-31 01:29:52 -04001348 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001349 return -3;
Len Brown0102b062016-02-27 03:11:29 -05001350
1351 t->tsc = rdtsc(); /* re-read close to APERF */
1352
1353 tsc_before = t->tsc;
1354
1355 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
1356 return -3;
1357
1358 tsc_between = rdtsc();
1359
Len Brown9c63a652012-10-31 01:29:52 -04001360 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001361 return -4;
Len Brown0102b062016-02-27 03:11:29 -05001362
1363 tsc_after = rdtsc();
1364
1365 aperf_time = tsc_between - tsc_before;
1366 mperf_time = tsc_after - tsc_between;
1367
1368 /*
1369 * If the system call latency to read APERF and MPERF
1370 * differ by more than 2x, then try again.
1371 */
1372 if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) {
1373 aperf_mperf_retry_count++;
1374 if (aperf_mperf_retry_count < 5)
1375 goto retry;
1376 else
1377 warnx("cpu%d jitter %lld %lld",
1378 cpu, aperf_time, mperf_time);
1379 }
1380 aperf_mperf_retry_count = 0;
1381
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02001382 t->aperf = t->aperf * aperf_mperf_multiplier;
1383 t->mperf = t->mperf * aperf_mperf_multiplier;
Len Brownc98d5d92012-06-04 00:56:40 -04001384 }
1385
Len Brown812db3f2017-02-10 00:25:41 -05001386 if (DO_BIC(BIC_IRQ))
Len Brown562a2d32016-02-26 23:48:05 -05001387 t->irq_count = irqs_per_cpu[cpu];
Len Brown812db3f2017-02-10 00:25:41 -05001388 if (DO_BIC(BIC_SMI)) {
Len Brown1ed51012013-02-10 17:19:24 -05001389 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
1390 return -5;
1391 t->smi_count = msr & 0xFFFFFFFF;
1392 }
Len Brown0539ba12017-02-10 00:27:20 -05001393 if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) {
Len Brown144b44b2013-11-09 00:30:16 -05001394 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1395 return -6;
1396 }
1397
Len Brown388e9c82016-12-22 23:57:55 -05001398 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1399 if (get_msr(cpu, mp->msr_num, &t->counter[i]))
1400 return -10;
1401 }
1402
1403
Len Brownc98d5d92012-06-04 00:56:40 -04001404 /* collect core counters only for 1st thread in core */
1405 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1406 return 0;
1407
Len Brown812db3f2017-02-10 00:25:41 -05001408 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001409 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1410 return -6;
Len Brown144b44b2013-11-09 00:30:16 -05001411 }
1412
Len Brown812db3f2017-02-10 00:25:41 -05001413 if (DO_BIC(BIC_CPU_c6) && !do_knl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001414 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1415 return -7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001416 } else if (do_knl_cstates) {
1417 if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1418 return -7;
Len Brownc98d5d92012-06-04 00:56:40 -04001419 }
1420
Len Brown812db3f2017-02-10 00:25:41 -05001421 if (DO_BIC(BIC_CPU_c7))
Len Brownc98d5d92012-06-04 00:56:40 -04001422 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1423 return -8;
1424
Len Brown0539ba12017-02-10 00:27:20 -05001425 if (DO_BIC(BIC_Mod_c6))
1426 if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us))
1427 return -8;
1428
Len Brown812db3f2017-02-10 00:25:41 -05001429 if (DO_BIC(BIC_CoreTmp)) {
Len Brown889facb2012-11-08 00:48:57 -05001430 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1431 return -9;
1432 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1433 }
1434
Len Brown388e9c82016-12-22 23:57:55 -05001435 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1436 if (get_msr(cpu, mp->msr_num, &c->counter[i]))
1437 return -10;
1438 }
Len Brown889facb2012-11-08 00:48:57 -05001439
Len Brownc98d5d92012-06-04 00:56:40 -04001440 /* collect package counters only for 1st core in package */
1441 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1442 return 0;
1443
Len Brown0b2bb692015-03-26 00:50:30 -04001444 if (do_skl_residency) {
1445 if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
1446 return -10;
1447 if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
1448 return -11;
1449 if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
1450 return -12;
1451 if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
1452 return -13;
1453 }
Len Brownee7e38e2015-02-09 23:39:45 -05001454 if (do_pc3)
Len Brownc98d5d92012-06-04 00:56:40 -04001455 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1456 return -9;
Len Brown0539ba12017-02-10 00:27:20 -05001457 if (do_pc6) {
1458 if (do_slm_cstates) {
1459 if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6))
1460 return -10;
1461 } else {
1462 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1463 return -10;
1464 }
1465 }
1466
Len Brownee7e38e2015-02-09 23:39:45 -05001467 if (do_pc2)
Len Brownc98d5d92012-06-04 00:56:40 -04001468 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1469 return -11;
Len Brownee7e38e2015-02-09 23:39:45 -05001470 if (do_pc7)
Len Brownc98d5d92012-06-04 00:56:40 -04001471 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1472 return -12;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001473 if (do_c8_c9_c10) {
1474 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1475 return -13;
1476 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1477 return -13;
1478 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1479 return -13;
1480 }
Len Brown889facb2012-11-08 00:48:57 -05001481 if (do_rapl & RAPL_PKG) {
1482 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1483 return -13;
1484 p->energy_pkg = msr & 0xFFFFFFFF;
1485 }
Jacob Pan91484942016-06-16 09:48:20 -07001486 if (do_rapl & RAPL_CORES_ENERGY_STATUS) {
Len Brown889facb2012-11-08 00:48:57 -05001487 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1488 return -14;
1489 p->energy_cores = msr & 0xFFFFFFFF;
1490 }
1491 if (do_rapl & RAPL_DRAM) {
1492 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1493 return -15;
1494 p->energy_dram = msr & 0xFFFFFFFF;
1495 }
1496 if (do_rapl & RAPL_GFX) {
1497 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1498 return -16;
1499 p->energy_gfx = msr & 0xFFFFFFFF;
1500 }
1501 if (do_rapl & RAPL_PKG_PERF_STATUS) {
1502 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1503 return -16;
1504 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1505 }
1506 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1507 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1508 return -16;
1509 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1510 }
Len Brown812db3f2017-02-10 00:25:41 -05001511 if (DO_BIC(BIC_PkgTmp)) {
Len Brown889facb2012-11-08 00:48:57 -05001512 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1513 return -17;
1514 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1515 }
Len Brownfdf676e2016-02-27 01:28:12 -05001516
Len Brown812db3f2017-02-10 00:25:41 -05001517 if (DO_BIC(BIC_GFX_rc6))
Len Brownfdf676e2016-02-27 01:28:12 -05001518 p->gfx_rc6_ms = gfx_cur_rc6_ms;
1519
Len Brown812db3f2017-02-10 00:25:41 -05001520 if (DO_BIC(BIC_GFXMHz))
Len Brown27d47352016-02-27 00:37:54 -05001521 p->gfx_mhz = gfx_cur_mhz;
1522
Len Brown388e9c82016-12-22 23:57:55 -05001523 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1524 if (get_msr(cpu, mp->msr_num, &p->counter[i]))
1525 return -10;
1526 }
1527
Len Brown103a8fe2010-10-22 23:53:03 -04001528 return 0;
1529}
1530
Len Brownee7e38e2015-02-09 23:39:45 -05001531/*
1532 * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1533 * If you change the values, note they are used both in comparisons
1534 * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1535 */
1536
1537#define PCLUKN 0 /* Unknown */
1538#define PCLRSV 1 /* Reserved */
1539#define PCL__0 2 /* PC0 */
1540#define PCL__1 3 /* PC1 */
1541#define PCL__2 4 /* PC2 */
1542#define PCL__3 5 /* PC3 */
1543#define PCL__4 6 /* PC4 */
1544#define PCL__6 7 /* PC6 */
1545#define PCL_6N 8 /* PC6 No Retention */
1546#define PCL_6R 9 /* PC6 Retention */
1547#define PCL__7 10 /* PC7 */
1548#define PCL_7S 11 /* PC7 Shrink */
Len Brown0b2bb692015-03-26 00:50:30 -04001549#define PCL__8 12 /* PC8 */
1550#define PCL__9 13 /* PC9 */
1551#define PCLUNL 14 /* Unlimited */
Len Brownee7e38e2015-02-09 23:39:45 -05001552
1553int pkg_cstate_limit = PCLUKN;
1554char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
Len Brown0b2bb692015-03-26 00:50:30 -04001555 "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"};
Len Brownee7e38e2015-02-09 23:39:45 -05001556
Len Browne9257f52015-04-01 21:02:57 -04001557int 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};
1558int 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};
1559int 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 -05001560int 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 -05001561int 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 -04001562int 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 -04001563int 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 -05001564int 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 -05001565
Len Browna2b7b742015-09-26 00:12:38 -04001566
1567static void
1568calculate_tsc_tweak()
1569{
Len Browna2b7b742015-09-26 00:12:38 -04001570 tsc_tweak = base_hz / tsc_hz;
1571}
1572
Len Brownfcd17212015-03-23 20:29:09 -04001573static void
1574dump_nhm_platform_info(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001575{
1576 unsigned long long msr;
1577 unsigned int ratio;
1578
Len Brownec0adc52015-11-12 02:42:31 -05001579 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001580
Len Brownb7d8c142016-02-13 23:36:17 -05001581 fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001582
Len Brown103a8fe2010-10-22 23:53:03 -04001583 ratio = (msr >> 40) & 0xFF;
Len Brown710f2732017-01-11 22:12:25 -05001584 fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001585 ratio, bclk, ratio * bclk);
1586
1587 ratio = (msr >> 8) & 0xFF;
Len Brown710f2732017-01-11 22:12:25 -05001588 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001589 ratio, bclk, ratio * bclk);
1590
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001591 get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001592 fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
Len Brownbfae2052015-06-17 12:27:21 -04001593 base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
Len Brown67920412013-01-31 15:22:15 -05001594
Len Brownfcd17212015-03-23 20:29:09 -04001595 return;
1596}
1597
1598static void
1599dump_hsw_turbo_ratio_limits(void)
1600{
1601 unsigned long long msr;
1602 unsigned int ratio;
1603
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001604 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04001605
Len Brownb7d8c142016-02-13 23:36:17 -05001606 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04001607
1608 ratio = (msr >> 8) & 0xFF;
1609 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001610 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04001611 ratio, bclk, ratio * bclk);
1612
1613 ratio = (msr >> 0) & 0xFF;
1614 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001615 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04001616 ratio, bclk, ratio * bclk);
1617 return;
1618}
1619
1620static void
1621dump_ivt_turbo_ratio_limits(void)
1622{
1623 unsigned long long msr;
1624 unsigned int ratio;
Len Brown6574a5d2012-09-21 00:01:31 -04001625
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001626 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001627
Len Brownb7d8c142016-02-13 23:36:17 -05001628 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001629
1630 ratio = (msr >> 56) & 0xFF;
1631 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001632 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001633 ratio, bclk, ratio * bclk);
1634
1635 ratio = (msr >> 48) & 0xFF;
1636 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001637 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001638 ratio, bclk, ratio * bclk);
1639
1640 ratio = (msr >> 40) & 0xFF;
1641 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001642 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001643 ratio, bclk, ratio * bclk);
1644
1645 ratio = (msr >> 32) & 0xFF;
1646 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001647 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001648 ratio, bclk, ratio * bclk);
1649
1650 ratio = (msr >> 24) & 0xFF;
1651 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001652 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001653 ratio, bclk, ratio * bclk);
1654
1655 ratio = (msr >> 16) & 0xFF;
1656 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001657 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001658 ratio, bclk, ratio * bclk);
1659
1660 ratio = (msr >> 8) & 0xFF;
1661 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001662 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001663 ratio, bclk, ratio * bclk);
1664
1665 ratio = (msr >> 0) & 0xFF;
1666 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001667 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001668 ratio, bclk, ratio * bclk);
Len Brownfcd17212015-03-23 20:29:09 -04001669 return;
1670}
Len Brown6574a5d2012-09-21 00:01:31 -04001671
Len Brownfcd17212015-03-23 20:29:09 -04001672static void
1673dump_nhm_turbo_ratio_limits(void)
1674{
1675 unsigned long long msr;
1676 unsigned int ratio;
Len Brown103a8fe2010-10-22 23:53:03 -04001677
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001678 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001679
Len Brownb7d8c142016-02-13 23:36:17 -05001680 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001681
1682 ratio = (msr >> 56) & 0xFF;
1683 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001684 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 8 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001685 ratio, bclk, ratio * bclk);
1686
1687 ratio = (msr >> 48) & 0xFF;
1688 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001689 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 7 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001690 ratio, bclk, ratio * bclk);
1691
1692 ratio = (msr >> 40) & 0xFF;
1693 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001694 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 6 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001695 ratio, bclk, ratio * bclk);
1696
1697 ratio = (msr >> 32) & 0xFF;
1698 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001699 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 5 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001700 ratio, bclk, ratio * bclk);
1701
Len Brown103a8fe2010-10-22 23:53:03 -04001702 ratio = (msr >> 24) & 0xFF;
1703 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001704 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001705 ratio, bclk, ratio * bclk);
1706
1707 ratio = (msr >> 16) & 0xFF;
1708 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001709 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001710 ratio, bclk, ratio * bclk);
1711
1712 ratio = (msr >> 8) & 0xFF;
1713 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001714 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001715 ratio, bclk, ratio * bclk);
1716
1717 ratio = (msr >> 0) & 0xFF;
1718 if (ratio)
Len Brown710f2732017-01-11 22:12:25 -05001719 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active cores\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001720 ratio, bclk, ratio * bclk);
Len Brownfcd17212015-03-23 20:29:09 -04001721 return;
1722}
Len Brown3a9a9412014-08-15 02:39:52 -04001723
Len Brownfcd17212015-03-23 20:29:09 -04001724static void
Len Brown0f7887c2017-01-12 23:49:18 -05001725dump_atom_turbo_ratio_limits(void)
1726{
1727 unsigned long long msr;
1728 unsigned int ratio;
1729
1730 get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr);
1731 fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
1732
1733 ratio = (msr >> 0) & 0x3F;
1734 if (ratio)
1735 fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n",
1736 ratio, bclk, ratio * bclk);
1737
1738 ratio = (msr >> 8) & 0x3F;
1739 if (ratio)
1740 fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n",
1741 ratio, bclk, ratio * bclk);
1742
1743 ratio = (msr >> 16) & 0x3F;
1744 if (ratio)
1745 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
1746 ratio, bclk, ratio * bclk);
1747
1748 get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr);
1749 fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
1750
1751 ratio = (msr >> 24) & 0x3F;
1752 if (ratio)
1753 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n",
1754 ratio, bclk, ratio * bclk);
1755
1756 ratio = (msr >> 16) & 0x3F;
1757 if (ratio)
1758 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n",
1759 ratio, bclk, ratio * bclk);
1760
1761 ratio = (msr >> 8) & 0x3F;
1762 if (ratio)
1763 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n",
1764 ratio, bclk, ratio * bclk);
1765
1766 ratio = (msr >> 0) & 0x3F;
1767 if (ratio)
1768 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n",
1769 ratio, bclk, ratio * bclk);
1770}
1771
1772static void
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001773dump_knl_turbo_ratio_limits(void)
1774{
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001775 const unsigned int buckets_no = 7;
1776
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001777 unsigned long long msr;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001778 int delta_cores, delta_ratio;
1779 int i, b_nr;
1780 unsigned int cores[buckets_no];
1781 unsigned int ratio[buckets_no];
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001782
Srinivas Pandruvadaebf59262016-07-06 16:07:56 -07001783 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001784
Len Brownb7d8c142016-02-13 23:36:17 -05001785 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n",
Len Brownbfae2052015-06-17 12:27:21 -04001786 base_cpu, msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001787
1788 /**
1789 * Turbo encoding in KNL is as follows:
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001790 * [0] -- Reserved
1791 * [7:1] -- Base value of number of active cores of bucket 1.
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001792 * [15:8] -- Base value of freq ratio of bucket 1.
1793 * [20:16] -- +ve delta of number of active cores of bucket 2.
1794 * i.e. active cores of bucket 2 =
1795 * active cores of bucket 1 + delta
1796 * [23:21] -- Negative delta of freq ratio of bucket 2.
1797 * i.e. freq ratio of bucket 2 =
1798 * freq ratio of bucket 1 - delta
1799 * [28:24]-- +ve delta of number of active cores of bucket 3.
1800 * [31:29]-- -ve delta of freq ratio of bucket 3.
1801 * [36:32]-- +ve delta of number of active cores of bucket 4.
1802 * [39:37]-- -ve delta of freq ratio of bucket 4.
1803 * [44:40]-- +ve delta of number of active cores of bucket 5.
1804 * [47:45]-- -ve delta of freq ratio of bucket 5.
1805 * [52:48]-- +ve delta of number of active cores of bucket 6.
1806 * [55:53]-- -ve delta of freq ratio of bucket 6.
1807 * [60:56]-- +ve delta of number of active cores of bucket 7.
1808 * [63:61]-- -ve delta of freq ratio of bucket 7.
1809 */
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001810
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001811 b_nr = 0;
1812 cores[b_nr] = (msr & 0xFF) >> 1;
1813 ratio[b_nr] = (msr >> 8) & 0xFF;
1814
1815 for (i = 16; i < 64; i += 8) {
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001816 delta_cores = (msr >> i) & 0x1F;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001817 delta_ratio = (msr >> (i + 5)) & 0x7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001818
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001819 cores[b_nr + 1] = cores[b_nr] + delta_cores;
1820 ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
1821 b_nr++;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001822 }
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001823
1824 for (i = buckets_no - 1; i >= 0; i--)
1825 if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
Len Brownb7d8c142016-02-13 23:36:17 -05001826 fprintf(outf,
Len Brown710f2732017-01-11 22:12:25 -05001827 "%d * %.1f = %.1f MHz max turbo %d active cores\n",
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001828 ratio[i], bclk, ratio[i] * bclk, cores[i]);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001829}
1830
1831static void
Len Brownfcd17212015-03-23 20:29:09 -04001832dump_nhm_cst_cfg(void)
1833{
1834 unsigned long long msr;
1835
Len Brown1df2e552017-01-07 23:24:57 -05001836 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04001837
1838#define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
1839#define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
1840
Len Brown1df2e552017-01-07 23:24:57 -05001841 fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04001842
Len Brownb7d8c142016-02-13 23:36:17 -05001843 fprintf(outf, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: %s)\n",
Len Brownfcd17212015-03-23 20:29:09 -04001844 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
1845 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
1846 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
1847 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
1848 (msr & (1 << 15)) ? "" : "UN",
Len Brown6c34f162016-03-13 03:21:22 -04001849 (unsigned int)msr & 0xF,
Len Brownfcd17212015-03-23 20:29:09 -04001850 pkg_cstate_limit_strings[pkg_cstate_limit]);
1851 return;
Len Brown103a8fe2010-10-22 23:53:03 -04001852}
1853
Len Brown6fb31432015-06-17 16:23:45 -04001854static void
1855dump_config_tdp(void)
1856{
1857 unsigned long long msr;
1858
1859 get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001860 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
Chen Yu685b5352015-12-13 21:09:31 +08001861 fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF);
Len Brown6fb31432015-06-17 16:23:45 -04001862
1863 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001864 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04001865 if (msr) {
Chen Yu685b5352015-12-13 21:09:31 +08001866 fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
1867 fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
1868 fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
1869 fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF);
Len Brown6fb31432015-06-17 16:23:45 -04001870 }
Len Brownb7d8c142016-02-13 23:36:17 -05001871 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04001872
1873 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001874 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04001875 if (msr) {
Chen Yu685b5352015-12-13 21:09:31 +08001876 fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
1877 fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
1878 fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
1879 fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF);
Len Brown6fb31432015-06-17 16:23:45 -04001880 }
Len Brownb7d8c142016-02-13 23:36:17 -05001881 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04001882
1883 get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001884 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04001885 if ((msr) & 0x3)
Len Brownb7d8c142016-02-13 23:36:17 -05001886 fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
1887 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
1888 fprintf(outf, ")\n");
Len Brown36229892016-02-26 20:51:02 -05001889
Len Brown6fb31432015-06-17 16:23:45 -04001890 get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001891 fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
Chen Yu685b5352015-12-13 21:09:31 +08001892 fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF);
Len Brownb7d8c142016-02-13 23:36:17 -05001893 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
1894 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04001895}
Len Brown5a634262016-04-06 17:15:55 -04001896
1897unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
1898
1899void print_irtl(void)
1900{
1901 unsigned long long msr;
1902
1903 get_msr(base_cpu, MSR_PKGC3_IRTL, &msr);
1904 fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr);
1905 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
1906 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
1907
1908 get_msr(base_cpu, MSR_PKGC6_IRTL, &msr);
1909 fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr);
1910 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
1911 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
1912
1913 get_msr(base_cpu, MSR_PKGC7_IRTL, &msr);
1914 fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr);
1915 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
1916 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
1917
1918 if (!do_irtl_hsw)
1919 return;
1920
1921 get_msr(base_cpu, MSR_PKGC8_IRTL, &msr);
1922 fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr);
1923 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
1924 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
1925
1926 get_msr(base_cpu, MSR_PKGC9_IRTL, &msr);
1927 fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr);
1928 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
1929 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
1930
1931 get_msr(base_cpu, MSR_PKGC10_IRTL, &msr);
1932 fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr);
1933 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
1934 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
1935
1936}
Len Brown36229892016-02-26 20:51:02 -05001937void free_fd_percpu(void)
1938{
1939 int i;
1940
Mika Westerberg01a67ad2016-04-22 11:13:23 +03001941 for (i = 0; i < topo.max_cpu_num + 1; ++i) {
Len Brown36229892016-02-26 20:51:02 -05001942 if (fd_percpu[i] != 0)
1943 close(fd_percpu[i]);
1944 }
1945
1946 free(fd_percpu);
Len Brown6fb31432015-06-17 16:23:45 -04001947}
1948
Len Brownc98d5d92012-06-04 00:56:40 -04001949void free_all_buffers(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001950{
Len Brownc98d5d92012-06-04 00:56:40 -04001951 CPU_FREE(cpu_present_set);
1952 cpu_present_set = NULL;
Len Brown36229892016-02-26 20:51:02 -05001953 cpu_present_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001954
Len Brownc98d5d92012-06-04 00:56:40 -04001955 CPU_FREE(cpu_affinity_set);
1956 cpu_affinity_set = NULL;
1957 cpu_affinity_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001958
Len Brownc98d5d92012-06-04 00:56:40 -04001959 free(thread_even);
1960 free(core_even);
1961 free(package_even);
1962
1963 thread_even = NULL;
1964 core_even = NULL;
1965 package_even = NULL;
1966
1967 free(thread_odd);
1968 free(core_odd);
1969 free(package_odd);
1970
1971 thread_odd = NULL;
1972 core_odd = NULL;
1973 package_odd = NULL;
1974
1975 free(output_buffer);
1976 output_buffer = NULL;
1977 outp = NULL;
Len Brown36229892016-02-26 20:51:02 -05001978
1979 free_fd_percpu();
Len Brown562a2d32016-02-26 23:48:05 -05001980
1981 free(irq_column_2_cpu);
1982 free(irqs_per_cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001983}
1984
Len Brownc98d5d92012-06-04 00:56:40 -04001985/*
Josh Triplett57a42a32013-08-20 17:20:17 -07001986 * Open a file, and exit on failure
1987 */
1988FILE *fopen_or_die(const char *path, const char *mode)
1989{
Len Brownb7d8c142016-02-13 23:36:17 -05001990 FILE *filep = fopen(path, mode);
Josh Triplettb2c95d92013-08-20 17:20:18 -07001991 if (!filep)
1992 err(1, "%s: open failed", path);
Josh Triplett57a42a32013-08-20 17:20:17 -07001993 return filep;
1994}
1995
1996/*
Josh Triplett95aebc42013-08-20 17:20:16 -07001997 * Parse a file containing a single int.
1998 */
1999int parse_int_file(const char *fmt, ...)
2000{
2001 va_list args;
2002 char path[PATH_MAX];
2003 FILE *filep;
2004 int value;
2005
2006 va_start(args, fmt);
2007 vsnprintf(path, sizeof(path), fmt, args);
2008 va_end(args);
Josh Triplett57a42a32013-08-20 17:20:17 -07002009 filep = fopen_or_die(path, "r");
Josh Triplettb2c95d92013-08-20 17:20:18 -07002010 if (fscanf(filep, "%d", &value) != 1)
2011 err(1, "%s: failed to parse number from file", path);
Josh Triplett95aebc42013-08-20 17:20:16 -07002012 fclose(filep);
2013 return value;
2014}
2015
2016/*
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002017 * get_cpu_position_in_core(cpu)
2018 * return the position of the CPU among its HT siblings in the core
2019 * return -1 if the sibling is not in list
Len Brownc98d5d92012-06-04 00:56:40 -04002020 */
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002021int get_cpu_position_in_core(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002022{
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002023 char path[64];
2024 FILE *filep;
2025 int this_cpu;
2026 char character;
2027 int i;
2028
2029 sprintf(path,
2030 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list",
2031 cpu);
2032 filep = fopen(path, "r");
2033 if (filep == NULL) {
2034 perror(path);
2035 exit(1);
2036 }
2037
2038 for (i = 0; i < topo.num_threads_per_core; i++) {
2039 fscanf(filep, "%d", &this_cpu);
2040 if (this_cpu == cpu) {
2041 fclose(filep);
2042 return i;
2043 }
2044
2045 /* Account for no separator after last thread*/
2046 if (i != (topo.num_threads_per_core - 1))
2047 fscanf(filep, "%c", &character);
2048 }
2049
2050 fclose(filep);
2051 return -1;
Len Brown103a8fe2010-10-22 23:53:03 -04002052}
2053
Len Brownc98d5d92012-06-04 00:56:40 -04002054/*
2055 * cpu_is_first_core_in_package(cpu)
2056 * return 1 if given CPU is 1st core in package
2057 */
2058int cpu_is_first_core_in_package(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002059{
Josh Triplett95aebc42013-08-20 17:20:16 -07002060 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002061}
2062
2063int get_physical_package_id(int cpu)
2064{
Josh Triplett95aebc42013-08-20 17:20:16 -07002065 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002066}
2067
2068int get_core_id(int cpu)
2069{
Josh Triplett95aebc42013-08-20 17:20:16 -07002070 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04002071}
2072
Len Brownc98d5d92012-06-04 00:56:40 -04002073int get_num_ht_siblings(int cpu)
2074{
2075 char path[80];
2076 FILE *filep;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002077 int sib1;
2078 int matches = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04002079 char character;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002080 char str[100];
2081 char *ch;
Len Brownc98d5d92012-06-04 00:56:40 -04002082
2083 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
Josh Triplett57a42a32013-08-20 17:20:17 -07002084 filep = fopen_or_die(path, "r");
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002085
Len Brownc98d5d92012-06-04 00:56:40 -04002086 /*
2087 * file format:
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002088 * A ',' separated or '-' separated set of numbers
2089 * (eg 1-2 or 1,3,4,5)
Len Brownc98d5d92012-06-04 00:56:40 -04002090 */
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002091 fscanf(filep, "%d%c\n", &sib1, &character);
2092 fseek(filep, 0, SEEK_SET);
2093 fgets(str, 100, filep);
2094 ch = strchr(str, character);
2095 while (ch != NULL) {
2096 matches++;
2097 ch = strchr(ch+1, character);
2098 }
Len Brownc98d5d92012-06-04 00:56:40 -04002099
2100 fclose(filep);
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002101 return matches+1;
Len Brownc98d5d92012-06-04 00:56:40 -04002102}
2103
Len Brown103a8fe2010-10-22 23:53:03 -04002104/*
Len Brownc98d5d92012-06-04 00:56:40 -04002105 * run func(thread, core, package) in topology order
2106 * skip non-present cpus
Len Brown103a8fe2010-10-22 23:53:03 -04002107 */
2108
Len Brownc98d5d92012-06-04 00:56:40 -04002109int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
2110 struct pkg_data *, struct thread_data *, struct core_data *,
2111 struct pkg_data *), struct thread_data *thread_base,
2112 struct core_data *core_base, struct pkg_data *pkg_base,
2113 struct thread_data *thread_base2, struct core_data *core_base2,
2114 struct pkg_data *pkg_base2)
2115{
2116 int retval, pkg_no, core_no, thread_no;
2117
2118 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
2119 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
2120 for (thread_no = 0; thread_no <
2121 topo.num_threads_per_core; ++thread_no) {
2122 struct thread_data *t, *t2;
2123 struct core_data *c, *c2;
2124 struct pkg_data *p, *p2;
2125
2126 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
2127
2128 if (cpu_is_not_present(t->cpu_id))
2129 continue;
2130
2131 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
2132
2133 c = GET_CORE(core_base, core_no, pkg_no);
2134 c2 = GET_CORE(core_base2, core_no, pkg_no);
2135
2136 p = GET_PKG(pkg_base, pkg_no);
2137 p2 = GET_PKG(pkg_base2, pkg_no);
2138
2139 retval = func(t, c, p, t2, c2, p2);
2140 if (retval)
2141 return retval;
2142 }
2143 }
2144 }
2145 return 0;
2146}
2147
2148/*
2149 * run func(cpu) on every cpu in /proc/stat
2150 * return max_cpu number
2151 */
2152int for_all_proc_cpus(int (func)(int))
Len Brown103a8fe2010-10-22 23:53:03 -04002153{
2154 FILE *fp;
Len Brownc98d5d92012-06-04 00:56:40 -04002155 int cpu_num;
Len Brown103a8fe2010-10-22 23:53:03 -04002156 int retval;
2157
Josh Triplett57a42a32013-08-20 17:20:17 -07002158 fp = fopen_or_die(proc_stat, "r");
Len Brown103a8fe2010-10-22 23:53:03 -04002159
2160 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
Josh Triplettb2c95d92013-08-20 17:20:18 -07002161 if (retval != 0)
2162 err(1, "%s: failed to parse format", proc_stat);
Len Brown103a8fe2010-10-22 23:53:03 -04002163
Len Brownc98d5d92012-06-04 00:56:40 -04002164 while (1) {
2165 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 -04002166 if (retval != 1)
2167 break;
2168
Len Brownc98d5d92012-06-04 00:56:40 -04002169 retval = func(cpu_num);
2170 if (retval) {
2171 fclose(fp);
2172 return(retval);
2173 }
Len Brown103a8fe2010-10-22 23:53:03 -04002174 }
2175 fclose(fp);
Len Brownc98d5d92012-06-04 00:56:40 -04002176 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002177}
2178
2179void re_initialize(void)
2180{
Len Brownc98d5d92012-06-04 00:56:40 -04002181 free_all_buffers();
2182 setup_all_buffers();
2183 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04002184}
2185
Len Brownc98d5d92012-06-04 00:56:40 -04002186
Len Brown103a8fe2010-10-22 23:53:03 -04002187/*
Len Brownc98d5d92012-06-04 00:56:40 -04002188 * count_cpus()
2189 * remember the last one seen, it will be the max
Len Brown103a8fe2010-10-22 23:53:03 -04002190 */
Len Brownc98d5d92012-06-04 00:56:40 -04002191int count_cpus(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002192{
Len Brownc98d5d92012-06-04 00:56:40 -04002193 if (topo.max_cpu_num < cpu)
2194 topo.max_cpu_num = cpu;
Len Brown103a8fe2010-10-22 23:53:03 -04002195
Len Brownc98d5d92012-06-04 00:56:40 -04002196 topo.num_cpus += 1;
2197 return 0;
2198}
2199int mark_cpu_present(int cpu)
2200{
2201 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brown15aaa342012-03-29 22:19:58 -04002202 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002203}
2204
Len Brown562a2d32016-02-26 23:48:05 -05002205/*
2206 * snapshot_proc_interrupts()
2207 *
2208 * read and record summary of /proc/interrupts
2209 *
2210 * return 1 if config change requires a restart, else return 0
2211 */
2212int snapshot_proc_interrupts(void)
2213{
2214 static FILE *fp;
2215 int column, retval;
2216
2217 if (fp == NULL)
2218 fp = fopen_or_die("/proc/interrupts", "r");
2219 else
2220 rewind(fp);
2221
2222 /* read 1st line of /proc/interrupts to get cpu* name for each column */
2223 for (column = 0; column < topo.num_cpus; ++column) {
2224 int cpu_number;
2225
2226 retval = fscanf(fp, " CPU%d", &cpu_number);
2227 if (retval != 1)
2228 break;
2229
2230 if (cpu_number > topo.max_cpu_num) {
2231 warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num);
2232 return 1;
2233 }
2234
2235 irq_column_2_cpu[column] = cpu_number;
2236 irqs_per_cpu[cpu_number] = 0;
2237 }
2238
2239 /* read /proc/interrupt count lines and sum up irqs per cpu */
2240 while (1) {
2241 int column;
2242 char buf[64];
2243
2244 retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */
2245 if (retval != 1)
2246 break;
2247
2248 /* read the count per cpu */
2249 for (column = 0; column < topo.num_cpus; ++column) {
2250
2251 int cpu_number, irq_count;
2252
2253 retval = fscanf(fp, " %d", &irq_count);
2254 if (retval != 1)
2255 break;
2256
2257 cpu_number = irq_column_2_cpu[column];
2258 irqs_per_cpu[cpu_number] += irq_count;
2259
2260 }
2261
2262 while (getc(fp) != '\n')
2263 ; /* flush interrupt description */
2264
2265 }
2266 return 0;
2267}
Len Brown27d47352016-02-27 00:37:54 -05002268/*
Len Brownfdf676e2016-02-27 01:28:12 -05002269 * snapshot_gfx_rc6_ms()
2270 *
2271 * record snapshot of
2272 * /sys/class/drm/card0/power/rc6_residency_ms
2273 *
2274 * return 1 if config change requires a restart, else return 0
2275 */
2276int snapshot_gfx_rc6_ms(void)
2277{
2278 FILE *fp;
2279 int retval;
2280
2281 fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r");
2282
2283 retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms);
2284 if (retval != 1)
2285 err(1, "GFX rc6");
2286
2287 fclose(fp);
2288
2289 return 0;
2290}
2291/*
Len Brown27d47352016-02-27 00:37:54 -05002292 * snapshot_gfx_mhz()
2293 *
2294 * record snapshot of
2295 * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz
2296 *
2297 * return 1 if config change requires a restart, else return 0
2298 */
2299int snapshot_gfx_mhz(void)
2300{
2301 static FILE *fp;
2302 int retval;
2303
2304 if (fp == NULL)
2305 fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r");
2306 else
2307 rewind(fp);
2308
2309 retval = fscanf(fp, "%d", &gfx_cur_mhz);
2310 if (retval != 1)
2311 err(1, "GFX MHz");
2312
2313 return 0;
2314}
Len Brown562a2d32016-02-26 23:48:05 -05002315
2316/*
2317 * snapshot /proc and /sys files
2318 *
2319 * return 1 if configuration restart needed, else return 0
2320 */
2321int snapshot_proc_sysfs_files(void)
2322{
2323 if (snapshot_proc_interrupts())
2324 return 1;
2325
Len Brown812db3f2017-02-10 00:25:41 -05002326 if (DO_BIC(BIC_GFX_rc6))
Len Brownfdf676e2016-02-27 01:28:12 -05002327 snapshot_gfx_rc6_ms();
2328
Len Brown812db3f2017-02-10 00:25:41 -05002329 if (DO_BIC(BIC_GFXMHz))
Len Brown27d47352016-02-27 00:37:54 -05002330 snapshot_gfx_mhz();
2331
Len Brown562a2d32016-02-26 23:48:05 -05002332 return 0;
2333}
2334
Len Brown103a8fe2010-10-22 23:53:03 -04002335void turbostat_loop()
2336{
Len Brownc98d5d92012-06-04 00:56:40 -04002337 int retval;
Len Browne52966c2012-11-08 22:38:05 -05002338 int restarted = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04002339
Len Brown103a8fe2010-10-22 23:53:03 -04002340restart:
Len Browne52966c2012-11-08 22:38:05 -05002341 restarted++;
2342
Len Brown562a2d32016-02-26 23:48:05 -05002343 snapshot_proc_sysfs_files();
Len Brownc98d5d92012-06-04 00:56:40 -04002344 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002345 if (retval < -1) {
2346 exit(retval);
2347 } else if (retval == -1) {
Len Browne52966c2012-11-08 22:38:05 -05002348 if (restarted > 1) {
2349 exit(retval);
2350 }
Len Brownc98d5d92012-06-04 00:56:40 -04002351 re_initialize();
2352 goto restart;
2353 }
Len Browne52966c2012-11-08 22:38:05 -05002354 restarted = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002355 gettimeofday(&tv_even, (struct timezone *)NULL);
2356
2357 while (1) {
Len Brownc98d5d92012-06-04 00:56:40 -04002358 if (for_all_proc_cpus(cpu_is_not_present)) {
Len Brown103a8fe2010-10-22 23:53:03 -04002359 re_initialize();
2360 goto restart;
2361 }
Len Brown2a0609c2016-02-12 22:44:48 -05002362 nanosleep(&interval_ts, NULL);
Len Brown562a2d32016-02-26 23:48:05 -05002363 if (snapshot_proc_sysfs_files())
2364 goto restart;
Len Brownc98d5d92012-06-04 00:56:40 -04002365 retval = for_all_cpus(get_counters, ODD_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002366 if (retval < -1) {
2367 exit(retval);
2368 } else if (retval == -1) {
Len Brown15aaa342012-03-29 22:19:58 -04002369 re_initialize();
2370 goto restart;
2371 }
Len Brown103a8fe2010-10-22 23:53:03 -04002372 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002373 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04002374 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) {
2375 re_initialize();
2376 goto restart;
2377 }
Len Brownc98d5d92012-06-04 00:56:40 -04002378 compute_average(EVEN_COUNTERS);
2379 format_all_counters(EVEN_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05002380 flush_output_stdout();
Len Brown2a0609c2016-02-12 22:44:48 -05002381 nanosleep(&interval_ts, NULL);
Len Brown562a2d32016-02-26 23:48:05 -05002382 if (snapshot_proc_sysfs_files())
2383 goto restart;
Len Brownc98d5d92012-06-04 00:56:40 -04002384 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002385 if (retval < -1) {
2386 exit(retval);
2387 } else if (retval == -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04002388 re_initialize();
2389 goto restart;
2390 }
Len Brown103a8fe2010-10-22 23:53:03 -04002391 gettimeofday(&tv_even, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002392 timersub(&tv_even, &tv_odd, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04002393 if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) {
2394 re_initialize();
2395 goto restart;
2396 }
Len Brownc98d5d92012-06-04 00:56:40 -04002397 compute_average(ODD_COUNTERS);
2398 format_all_counters(ODD_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05002399 flush_output_stdout();
Len Brown103a8fe2010-10-22 23:53:03 -04002400 }
2401}
2402
2403void check_dev_msr()
2404{
2405 struct stat sb;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002406 char pathname[32];
Len Brown103a8fe2010-10-22 23:53:03 -04002407
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002408 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2409 if (stat(pathname, &sb))
Len Browna21d38c2015-03-24 16:37:35 -04002410 if (system("/sbin/modprobe msr > /dev/null 2>&1"))
2411 err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
Len Brown103a8fe2010-10-22 23:53:03 -04002412}
2413
Len Brown98481e72014-08-15 00:36:50 -04002414void check_permissions()
Len Brown103a8fe2010-10-22 23:53:03 -04002415{
Len Brown98481e72014-08-15 00:36:50 -04002416 struct __user_cap_header_struct cap_header_data;
2417 cap_user_header_t cap_header = &cap_header_data;
2418 struct __user_cap_data_struct cap_data_data;
2419 cap_user_data_t cap_data = &cap_data_data;
2420 extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
2421 int do_exit = 0;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002422 char pathname[32];
Len Brown98481e72014-08-15 00:36:50 -04002423
2424 /* check for CAP_SYS_RAWIO */
2425 cap_header->pid = getpid();
2426 cap_header->version = _LINUX_CAPABILITY_VERSION;
2427 if (capget(cap_header, cap_data) < 0)
2428 err(-6, "capget(2) failed");
2429
2430 if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
2431 do_exit++;
2432 warnx("capget(CAP_SYS_RAWIO) failed,"
2433 " try \"# setcap cap_sys_rawio=ep %s\"", progname);
2434 }
2435
2436 /* test file permissions */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002437 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2438 if (euidaccess(pathname, R_OK)) {
Len Brown98481e72014-08-15 00:36:50 -04002439 do_exit++;
2440 warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
2441 }
2442
2443 /* if all else fails, thell them to be root */
2444 if (do_exit)
2445 if (getuid() != 0)
Len Brownd7899442015-01-23 00:12:33 -05002446 warnx("... or simply run as root");
Len Brown98481e72014-08-15 00:36:50 -04002447
2448 if (do_exit)
2449 exit(-6);
Len Brown103a8fe2010-10-22 23:53:03 -04002450}
2451
Len Brownd7899442015-01-23 00:12:33 -05002452/*
2453 * NHM adds support for additional MSRs:
2454 *
2455 * MSR_SMI_COUNT 0x00000034
2456 *
Len Brownec0adc52015-11-12 02:42:31 -05002457 * MSR_PLATFORM_INFO 0x000000ce
Len Brown1df2e552017-01-07 23:24:57 -05002458 * MSR_PKG_CST_CONFIG_CONTROL 0x000000e2
Len Brownd7899442015-01-23 00:12:33 -05002459 *
Len Browncf4cbe52017-01-01 13:08:33 -05002460 * MSR_MISC_PWR_MGMT 0x000001aa
2461 *
Len Brownd7899442015-01-23 00:12:33 -05002462 * MSR_PKG_C3_RESIDENCY 0x000003f8
2463 * MSR_PKG_C6_RESIDENCY 0x000003f9
2464 * MSR_CORE_C3_RESIDENCY 0x000003fc
2465 * MSR_CORE_C6_RESIDENCY 0x000003fd
2466 *
Len Brownee7e38e2015-02-09 23:39:45 -05002467 * Side effect:
Len Brown1df2e552017-01-07 23:24:57 -05002468 * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL
Len Brownd7899442015-01-23 00:12:33 -05002469 */
Len Brownee7e38e2015-02-09 23:39:45 -05002470int probe_nhm_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04002471{
Len Brownee7e38e2015-02-09 23:39:45 -05002472 unsigned long long msr;
Len Brown21ed5572015-10-19 22:37:40 -04002473 unsigned int base_ratio;
Len Brownee7e38e2015-02-09 23:39:45 -05002474 int *pkg_cstate_limits;
2475
Len Brown103a8fe2010-10-22 23:53:03 -04002476 if (!genuine_intel)
2477 return 0;
2478
2479 if (family != 6)
2480 return 0;
2481
Len Brown21ed5572015-10-19 22:37:40 -04002482 bclk = discover_bclk(family, model);
2483
Len Brown103a8fe2010-10-22 23:53:03 -04002484 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002485 case INTEL_FAM6_NEHALEM_EP: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
2486 case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
Len Brown103a8fe2010-10-22 23:53:03 -04002487 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
Len Brown869ce692016-06-16 23:22:37 -04002488 case INTEL_FAM6_WESTMERE: /* Westmere Client - Clarkdale, Arrandale */
2489 case INTEL_FAM6_WESTMERE_EP: /* Westmere EP - Gulftown */
2490 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
2491 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
Len Brownee7e38e2015-02-09 23:39:45 -05002492 pkg_cstate_limits = nhm_pkg_cstate_limits;
2493 break;
Len Brown869ce692016-06-16 23:22:37 -04002494 case INTEL_FAM6_SANDYBRIDGE: /* SNB */
2495 case INTEL_FAM6_SANDYBRIDGE_X: /* SNB Xeon */
2496 case INTEL_FAM6_IVYBRIDGE: /* IVB */
2497 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
Len Brownee7e38e2015-02-09 23:39:45 -05002498 pkg_cstate_limits = snb_pkg_cstate_limits;
2499 break;
Len Brown869ce692016-06-16 23:22:37 -04002500 case INTEL_FAM6_HASWELL_CORE: /* HSW */
2501 case INTEL_FAM6_HASWELL_X: /* HSX */
2502 case INTEL_FAM6_HASWELL_ULT: /* HSW */
2503 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
2504 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
2505 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
2506 case INTEL_FAM6_BROADWELL_X: /* BDX */
2507 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
2508 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
2509 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
2510 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
2511 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Brownee7e38e2015-02-09 23:39:45 -05002512 pkg_cstate_limits = hsw_pkg_cstate_limits;
2513 break;
Len Brownd8ebb442016-12-01 20:27:46 -05002514 case INTEL_FAM6_SKYLAKE_X: /* SKX */
2515 pkg_cstate_limits = skx_pkg_cstate_limits;
2516 break;
Len Brown869ce692016-06-16 23:22:37 -04002517 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
Len Browncf4cbe52017-01-01 13:08:33 -05002518 no_MSR_MISC_PWR_MGMT = 1;
Len Brown869ce692016-06-16 23:22:37 -04002519 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Len Brownee7e38e2015-02-09 23:39:45 -05002520 pkg_cstate_limits = slv_pkg_cstate_limits;
2521 break;
Len Brown869ce692016-06-16 23:22:37 -04002522 case INTEL_FAM6_ATOM_AIRMONT: /* AMT */
Len Brownee7e38e2015-02-09 23:39:45 -05002523 pkg_cstate_limits = amt_pkg_cstate_limits;
Len Browncf4cbe52017-01-01 13:08:33 -05002524 no_MSR_MISC_PWR_MGMT = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002525 break;
Len Brown869ce692016-06-16 23:22:37 -04002526 case INTEL_FAM6_XEON_PHI_KNL: /* PHI */
Len Brown005c82d2016-12-01 01:35:38 -05002527 case INTEL_FAM6_XEON_PHI_KNM:
Len Brownee7e38e2015-02-09 23:39:45 -05002528 pkg_cstate_limits = phi_pkg_cstate_limits;
2529 break;
Len Brown869ce692016-06-16 23:22:37 -04002530 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
2531 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Browne4085d52016-04-06 17:15:56 -04002532 pkg_cstate_limits = bxt_pkg_cstate_limits;
2533 break;
Len Brown103a8fe2010-10-22 23:53:03 -04002534 default:
2535 return 0;
2536 }
Len Brown1df2e552017-01-07 23:24:57 -05002537 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
Len Browne9257f52015-04-01 21:02:57 -04002538 pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
Len Brownee7e38e2015-02-09 23:39:45 -05002539
Len Brownec0adc52015-11-12 02:42:31 -05002540 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown21ed5572015-10-19 22:37:40 -04002541 base_ratio = (msr >> 8) & 0xFF;
2542
2543 base_hz = base_ratio * bclk * 1000000;
2544 has_base_hz = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002545 return 1;
Len Brown103a8fe2010-10-22 23:53:03 -04002546}
Len Brown0f7887c2017-01-12 23:49:18 -05002547/*
2548 * SLV client has supporet for unique MSRs:
2549 *
2550 * MSR_CC6_DEMOTION_POLICY_CONFIG
2551 * MSR_MC6_DEMOTION_POLICY_CONFIG
2552 */
2553
2554int has_slv_msrs(unsigned int family, unsigned int model)
2555{
2556 if (!genuine_intel)
2557 return 0;
2558
2559 switch (model) {
2560 case INTEL_FAM6_ATOM_SILVERMONT1:
2561 case INTEL_FAM6_ATOM_MERRIFIELD:
2562 case INTEL_FAM6_ATOM_MOOREFIELD:
2563 return 1;
2564 }
2565 return 0;
2566}
2567
Len Brownd7899442015-01-23 00:12:33 -05002568int has_nhm_turbo_ratio_limit(unsigned int family, unsigned int model)
2569{
Len Brown0f7887c2017-01-12 23:49:18 -05002570 if (has_slv_msrs(family, model))
2571 return 0;
2572
Len Brownd7899442015-01-23 00:12:33 -05002573 switch (model) {
2574 /* Nehalem compatible, but do not include turbo-ratio limit support */
Len Brown869ce692016-06-16 23:22:37 -04002575 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
2576 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
2577 case INTEL_FAM6_XEON_PHI_KNL: /* PHI - Knights Landing (different MSR definition) */
Len Brown005c82d2016-12-01 01:35:38 -05002578 case INTEL_FAM6_XEON_PHI_KNM:
Len Brownd7899442015-01-23 00:12:33 -05002579 return 0;
2580 default:
2581 return 1;
2582 }
2583}
Len Brown0f7887c2017-01-12 23:49:18 -05002584int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model)
2585{
2586 if (has_slv_msrs(family, model))
2587 return 1;
2588
2589 return 0;
2590}
Len Brown6574a5d2012-09-21 00:01:31 -04002591int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
2592{
2593 if (!genuine_intel)
2594 return 0;
2595
2596 if (family != 6)
2597 return 0;
2598
2599 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002600 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
2601 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
Len Brown6574a5d2012-09-21 00:01:31 -04002602 return 1;
2603 default:
2604 return 0;
2605 }
2606}
Len Brownfcd17212015-03-23 20:29:09 -04002607int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
2608{
2609 if (!genuine_intel)
2610 return 0;
2611
2612 if (family != 6)
2613 return 0;
2614
2615 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002616 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
Len Brownfcd17212015-03-23 20:29:09 -04002617 return 1;
2618 default:
2619 return 0;
2620 }
2621}
2622
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002623int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
2624{
2625 if (!genuine_intel)
2626 return 0;
2627
2628 if (family != 6)
2629 return 0;
2630
2631 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002632 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
Len Brown005c82d2016-12-01 01:35:38 -05002633 case INTEL_FAM6_XEON_PHI_KNM:
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002634 return 1;
2635 default:
2636 return 0;
2637 }
2638}
Len Brown6fb31432015-06-17 16:23:45 -04002639int has_config_tdp(unsigned int family, unsigned int model)
2640{
2641 if (!genuine_intel)
2642 return 0;
2643
2644 if (family != 6)
2645 return 0;
2646
2647 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002648 case INTEL_FAM6_IVYBRIDGE: /* IVB */
2649 case INTEL_FAM6_HASWELL_CORE: /* HSW */
2650 case INTEL_FAM6_HASWELL_X: /* HSX */
2651 case INTEL_FAM6_HASWELL_ULT: /* HSW */
2652 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
2653 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
2654 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
2655 case INTEL_FAM6_BROADWELL_X: /* BDX */
2656 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
2657 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
2658 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
2659 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
2660 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
2661 case INTEL_FAM6_SKYLAKE_X: /* SKX */
Len Brown6fb31432015-06-17 16:23:45 -04002662
Len Brown869ce692016-06-16 23:22:37 -04002663 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
Len Brown005c82d2016-12-01 01:35:38 -05002664 case INTEL_FAM6_XEON_PHI_KNM:
Len Brown6fb31432015-06-17 16:23:45 -04002665 return 1;
2666 default:
2667 return 0;
2668 }
2669}
2670
Len Brownfcd17212015-03-23 20:29:09 -04002671static void
Colin Ian King1b693172016-03-02 13:50:25 +00002672dump_cstate_pstate_config_info(unsigned int family, unsigned int model)
Len Brownfcd17212015-03-23 20:29:09 -04002673{
2674 if (!do_nhm_platform_info)
2675 return;
2676
2677 dump_nhm_platform_info();
2678
2679 if (has_hsw_turbo_ratio_limit(family, model))
2680 dump_hsw_turbo_ratio_limits();
2681
2682 if (has_ivt_turbo_ratio_limit(family, model))
2683 dump_ivt_turbo_ratio_limits();
2684
2685 if (has_nhm_turbo_ratio_limit(family, model))
2686 dump_nhm_turbo_ratio_limits();
2687
Len Brown0f7887c2017-01-12 23:49:18 -05002688 if (has_atom_turbo_ratio_limit(family, model))
2689 dump_atom_turbo_ratio_limits();
2690
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002691 if (has_knl_turbo_ratio_limit(family, model))
2692 dump_knl_turbo_ratio_limits();
2693
Len Brown6fb31432015-06-17 16:23:45 -04002694 if (has_config_tdp(family, model))
2695 dump_config_tdp();
2696
Len Brownfcd17212015-03-23 20:29:09 -04002697 dump_nhm_cst_cfg();
2698}
2699
Len Brown6574a5d2012-09-21 00:01:31 -04002700
Len Brown889facb2012-11-08 00:48:57 -05002701/*
2702 * print_epb()
2703 * Decode the ENERGY_PERF_BIAS MSR
2704 */
2705int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2706{
2707 unsigned long long msr;
2708 char *epb_string;
2709 int cpu;
2710
2711 if (!has_epb)
2712 return 0;
2713
2714 cpu = t->cpu_id;
2715
2716 /* EPB is per-package */
2717 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2718 return 0;
2719
2720 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05002721 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05002722 return -1;
2723 }
2724
2725 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
2726 return 0;
2727
Len Browne9be7dd2015-05-26 12:19:37 -04002728 switch (msr & 0xF) {
Len Brown889facb2012-11-08 00:48:57 -05002729 case ENERGY_PERF_BIAS_PERFORMANCE:
2730 epb_string = "performance";
2731 break;
2732 case ENERGY_PERF_BIAS_NORMAL:
2733 epb_string = "balanced";
2734 break;
2735 case ENERGY_PERF_BIAS_POWERSAVE:
2736 epb_string = "powersave";
2737 break;
2738 default:
2739 epb_string = "custom";
2740 break;
2741 }
Len Brownb7d8c142016-02-13 23:36:17 -05002742 fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
Len Brown889facb2012-11-08 00:48:57 -05002743
2744 return 0;
2745}
Len Brown7f5c2582015-12-01 01:36:39 -05002746/*
2747 * print_hwp()
2748 * Decode the MSR_HWP_CAPABILITIES
2749 */
2750int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2751{
2752 unsigned long long msr;
2753 int cpu;
2754
2755 if (!has_hwp)
2756 return 0;
2757
2758 cpu = t->cpu_id;
2759
2760 /* MSR_HWP_CAPABILITIES is per-package */
2761 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2762 return 0;
2763
2764 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05002765 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown7f5c2582015-12-01 01:36:39 -05002766 return -1;
2767 }
2768
2769 if (get_msr(cpu, MSR_PM_ENABLE, &msr))
2770 return 0;
2771
Len Brownb7d8c142016-02-13 23:36:17 -05002772 fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05002773 cpu, msr, (msr & (1 << 0)) ? "" : "No-");
2774
2775 /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
2776 if ((msr & (1 << 0)) == 0)
2777 return 0;
2778
2779 if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
2780 return 0;
2781
Len Brownb7d8c142016-02-13 23:36:17 -05002782 fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002783 "(high 0x%x guar 0x%x eff 0x%x low 0x%x)\n",
2784 cpu, msr,
2785 (unsigned int)HWP_HIGHEST_PERF(msr),
2786 (unsigned int)HWP_GUARANTEED_PERF(msr),
2787 (unsigned int)HWP_MOSTEFFICIENT_PERF(msr),
2788 (unsigned int)HWP_LOWEST_PERF(msr));
2789
2790 if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
2791 return 0;
2792
Len Brownb7d8c142016-02-13 23:36:17 -05002793 fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002794 "(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x pkg 0x%x)\n",
2795 cpu, msr,
2796 (unsigned int)(((msr) >> 0) & 0xff),
2797 (unsigned int)(((msr) >> 8) & 0xff),
2798 (unsigned int)(((msr) >> 16) & 0xff),
2799 (unsigned int)(((msr) >> 24) & 0xff),
2800 (unsigned int)(((msr) >> 32) & 0xff3),
2801 (unsigned int)(((msr) >> 42) & 0x1));
2802
2803 if (has_hwp_pkg) {
2804 if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
2805 return 0;
2806
Len Brownb7d8c142016-02-13 23:36:17 -05002807 fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002808 "(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x)\n",
2809 cpu, msr,
2810 (unsigned int)(((msr) >> 0) & 0xff),
2811 (unsigned int)(((msr) >> 8) & 0xff),
2812 (unsigned int)(((msr) >> 16) & 0xff),
2813 (unsigned int)(((msr) >> 24) & 0xff),
2814 (unsigned int)(((msr) >> 32) & 0xff3));
2815 }
2816 if (has_hwp_notify) {
2817 if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
2818 return 0;
2819
Len Brownb7d8c142016-02-13 23:36:17 -05002820 fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002821 "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
2822 cpu, msr,
2823 ((msr) & 0x1) ? "EN" : "Dis",
2824 ((msr) & 0x2) ? "EN" : "Dis");
2825 }
2826 if (get_msr(cpu, MSR_HWP_STATUS, &msr))
2827 return 0;
2828
Len Brownb7d8c142016-02-13 23:36:17 -05002829 fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002830 "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
2831 cpu, msr,
2832 ((msr) & 0x1) ? "" : "No-",
2833 ((msr) & 0x2) ? "" : "No-");
Len Brown889facb2012-11-08 00:48:57 -05002834
2835 return 0;
2836}
2837
Len Brown3a9a9412014-08-15 02:39:52 -04002838/*
2839 * print_perf_limit()
2840 */
2841int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2842{
2843 unsigned long long msr;
2844 int cpu;
2845
2846 cpu = t->cpu_id;
2847
2848 /* per-package */
2849 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2850 return 0;
2851
2852 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05002853 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown3a9a9412014-08-15 02:39:52 -04002854 return -1;
2855 }
2856
2857 if (do_core_perf_limit_reasons) {
2858 get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002859 fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2860 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
Len Browne33cbe82015-03-13 16:30:57 -04002861 (msr & 1 << 15) ? "bit15, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04002862 (msr & 1 << 14) ? "bit14, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04002863 (msr & 1 << 13) ? "Transitions, " : "",
2864 (msr & 1 << 12) ? "MultiCoreTurbo, " : "",
2865 (msr & 1 << 11) ? "PkgPwrL2, " : "",
2866 (msr & 1 << 10) ? "PkgPwrL1, " : "",
2867 (msr & 1 << 9) ? "CorePwr, " : "",
2868 (msr & 1 << 8) ? "Amps, " : "",
2869 (msr & 1 << 6) ? "VR-Therm, " : "",
2870 (msr & 1 << 5) ? "Auto-HWP, " : "",
2871 (msr & 1 << 4) ? "Graphics, " : "",
2872 (msr & 1 << 2) ? "bit2, " : "",
2873 (msr & 1 << 1) ? "ThermStatus, " : "",
2874 (msr & 1 << 0) ? "PROCHOT, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05002875 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 -04002876 (msr & 1 << 31) ? "bit31, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04002877 (msr & 1 << 30) ? "bit30, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04002878 (msr & 1 << 29) ? "Transitions, " : "",
2879 (msr & 1 << 28) ? "MultiCoreTurbo, " : "",
2880 (msr & 1 << 27) ? "PkgPwrL2, " : "",
2881 (msr & 1 << 26) ? "PkgPwrL1, " : "",
2882 (msr & 1 << 25) ? "CorePwr, " : "",
2883 (msr & 1 << 24) ? "Amps, " : "",
2884 (msr & 1 << 22) ? "VR-Therm, " : "",
2885 (msr & 1 << 21) ? "Auto-HWP, " : "",
2886 (msr & 1 << 20) ? "Graphics, " : "",
2887 (msr & 1 << 18) ? "bit18, " : "",
2888 (msr & 1 << 17) ? "ThermStatus, " : "",
2889 (msr & 1 << 16) ? "PROCHOT, " : "");
Len Brown3a9a9412014-08-15 02:39:52 -04002890
2891 }
2892 if (do_gfx_perf_limit_reasons) {
2893 get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002894 fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2895 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
Len Brown3a9a9412014-08-15 02:39:52 -04002896 (msr & 1 << 0) ? "PROCHOT, " : "",
2897 (msr & 1 << 1) ? "ThermStatus, " : "",
2898 (msr & 1 << 4) ? "Graphics, " : "",
2899 (msr & 1 << 6) ? "VR-Therm, " : "",
2900 (msr & 1 << 8) ? "Amps, " : "",
2901 (msr & 1 << 9) ? "GFXPwr, " : "",
2902 (msr & 1 << 10) ? "PkgPwrL1, " : "",
2903 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05002904 fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04002905 (msr & 1 << 16) ? "PROCHOT, " : "",
2906 (msr & 1 << 17) ? "ThermStatus, " : "",
2907 (msr & 1 << 20) ? "Graphics, " : "",
2908 (msr & 1 << 22) ? "VR-Therm, " : "",
2909 (msr & 1 << 24) ? "Amps, " : "",
2910 (msr & 1 << 25) ? "GFXPwr, " : "",
2911 (msr & 1 << 26) ? "PkgPwrL1, " : "",
2912 (msr & 1 << 27) ? "PkgPwrL2, " : "");
2913 }
2914 if (do_ring_perf_limit_reasons) {
2915 get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002916 fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2917 fprintf(outf, " (Active: %s%s%s%s%s%s)",
Len Brown3a9a9412014-08-15 02:39:52 -04002918 (msr & 1 << 0) ? "PROCHOT, " : "",
2919 (msr & 1 << 1) ? "ThermStatus, " : "",
2920 (msr & 1 << 6) ? "VR-Therm, " : "",
2921 (msr & 1 << 8) ? "Amps, " : "",
2922 (msr & 1 << 10) ? "PkgPwrL1, " : "",
2923 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05002924 fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04002925 (msr & 1 << 16) ? "PROCHOT, " : "",
2926 (msr & 1 << 17) ? "ThermStatus, " : "",
2927 (msr & 1 << 22) ? "VR-Therm, " : "",
2928 (msr & 1 << 24) ? "Amps, " : "",
2929 (msr & 1 << 26) ? "PkgPwrL1, " : "",
2930 (msr & 1 << 27) ? "PkgPwrL2, " : "");
2931 }
2932 return 0;
2933}
2934
Len Brown889facb2012-11-08 00:48:57 -05002935#define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
2936#define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
2937
Colin Ian King1b693172016-03-02 13:50:25 +00002938double get_tdp(unsigned int model)
Len Brown144b44b2013-11-09 00:30:16 -05002939{
2940 unsigned long long msr;
2941
2942 if (do_rapl & RAPL_PKG_POWER_INFO)
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002943 if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
Len Brown144b44b2013-11-09 00:30:16 -05002944 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
2945
2946 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002947 case INTEL_FAM6_ATOM_SILVERMONT1:
2948 case INTEL_FAM6_ATOM_SILVERMONT2:
Len Brown144b44b2013-11-09 00:30:16 -05002949 return 30.0;
2950 default:
2951 return 135.0;
2952 }
2953}
2954
Andrey Semin40ee8e32014-12-05 00:07:00 -05002955/*
2956 * rapl_dram_energy_units_probe()
2957 * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
2958 */
2959static double
2960rapl_dram_energy_units_probe(int model, double rapl_energy_units)
2961{
2962 /* only called for genuine_intel, family 6 */
2963
2964 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002965 case INTEL_FAM6_HASWELL_X: /* HSX */
2966 case INTEL_FAM6_BROADWELL_X: /* BDX */
2967 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
2968 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05002969 case INTEL_FAM6_XEON_PHI_KNM:
Andrey Semin40ee8e32014-12-05 00:07:00 -05002970 return (rapl_dram_energy_units = 15.3 / 1000000);
2971 default:
2972 return (rapl_energy_units);
2973 }
2974}
2975
Len Brown144b44b2013-11-09 00:30:16 -05002976
Len Brown889facb2012-11-08 00:48:57 -05002977/*
2978 * rapl_probe()
2979 *
Len Brown144b44b2013-11-09 00:30:16 -05002980 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
Len Brown889facb2012-11-08 00:48:57 -05002981 */
2982void rapl_probe(unsigned int family, unsigned int model)
2983{
2984 unsigned long long msr;
Len Brown144b44b2013-11-09 00:30:16 -05002985 unsigned int time_unit;
Len Brown889facb2012-11-08 00:48:57 -05002986 double tdp;
2987
2988 if (!genuine_intel)
2989 return;
2990
2991 if (family != 6)
2992 return;
2993
2994 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002995 case INTEL_FAM6_SANDYBRIDGE:
2996 case INTEL_FAM6_IVYBRIDGE:
2997 case INTEL_FAM6_HASWELL_CORE: /* HSW */
2998 case INTEL_FAM6_HASWELL_ULT: /* HSW */
2999 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3000 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3001 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
Len Brown144b44b2013-11-09 00:30:16 -05003002 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
Len Brown812db3f2017-02-10 00:25:41 -05003003 if (rapl_joules) {
3004 BIC_PRESENT(BIC_Pkg_J);
3005 BIC_PRESENT(BIC_Cor_J);
3006 BIC_PRESENT(BIC_GFX_J);
3007 } else {
3008 BIC_PRESENT(BIC_PkgWatt);
3009 BIC_PRESENT(BIC_CorWatt);
3010 BIC_PRESENT(BIC_GFXWatt);
3011 }
Len Brown889facb2012-11-08 00:48:57 -05003012 break;
Len Brown869ce692016-06-16 23:22:37 -04003013 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Browne4085d52016-04-06 17:15:56 -04003014 do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO;
Len Brown812db3f2017-02-10 00:25:41 -05003015 if (rapl_joules)
3016 BIC_PRESENT(BIC_Pkg_J);
3017 else
3018 BIC_PRESENT(BIC_PkgWatt);
Len Browne4085d52016-04-06 17:15:56 -04003019 break;
Len Brown869ce692016-06-16 23:22:37 -04003020 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3021 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3022 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3023 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Brown0b2bb692015-03-26 00:50:30 -04003024 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 -05003025 BIC_PRESENT(BIC_PKG__);
3026 BIC_PRESENT(BIC_RAM__);
3027 if (rapl_joules) {
3028 BIC_PRESENT(BIC_Pkg_J);
3029 BIC_PRESENT(BIC_Cor_J);
3030 BIC_PRESENT(BIC_RAM_J);
3031 } else {
3032 BIC_PRESENT(BIC_PkgWatt);
3033 BIC_PRESENT(BIC_CorWatt);
3034 BIC_PRESENT(BIC_RAMWatt);
3035 }
Len Brown0b2bb692015-03-26 00:50:30 -04003036 break;
Len Brown869ce692016-06-16 23:22:37 -04003037 case INTEL_FAM6_HASWELL_X: /* HSX */
3038 case INTEL_FAM6_BROADWELL_X: /* BDX */
3039 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3040 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3041 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003042 case INTEL_FAM6_XEON_PHI_KNM:
Len Brown0b2bb692015-03-26 00:50:30 -04003043 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 -05003044 BIC_PRESENT(BIC_PKG__);
3045 BIC_PRESENT(BIC_RAM__);
3046 if (rapl_joules) {
3047 BIC_PRESENT(BIC_Pkg_J);
3048 BIC_PRESENT(BIC_RAM_J);
3049 } else {
3050 BIC_PRESENT(BIC_PkgWatt);
3051 BIC_PRESENT(BIC_RAMWatt);
3052 }
Len Browne6f9bb32013-12-03 02:19:19 -05003053 break;
Len Brown869ce692016-06-16 23:22:37 -04003054 case INTEL_FAM6_SANDYBRIDGE_X:
3055 case INTEL_FAM6_IVYBRIDGE_X:
Len Brown0b2bb692015-03-26 00:50:30 -04003056 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 -05003057 BIC_PRESENT(BIC_PKG__);
3058 BIC_PRESENT(BIC_RAM__);
3059 if (rapl_joules) {
3060 BIC_PRESENT(BIC_Pkg_J);
3061 BIC_PRESENT(BIC_Cor_J);
3062 BIC_PRESENT(BIC_RAM_J);
3063 } else {
3064 BIC_PRESENT(BIC_PkgWatt);
3065 BIC_PRESENT(BIC_CorWatt);
3066 BIC_PRESENT(BIC_RAMWatt);
3067 }
Len Brown144b44b2013-11-09 00:30:16 -05003068 break;
Len Brown869ce692016-06-16 23:22:37 -04003069 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
3070 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Jacob Pan91484942016-06-16 09:48:20 -07003071 do_rapl = RAPL_PKG | RAPL_CORES;
Len Brown812db3f2017-02-10 00:25:41 -05003072 if (rapl_joules) {
3073 BIC_PRESENT(BIC_Pkg_J);
3074 BIC_PRESENT(BIC_Cor_J);
3075 } else {
3076 BIC_PRESENT(BIC_PkgWatt);
3077 BIC_PRESENT(BIC_CorWatt);
3078 }
Len Brown889facb2012-11-08 00:48:57 -05003079 break;
Len Brown869ce692016-06-16 23:22:37 -04003080 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Jacob Pan0f644902016-06-16 09:48:22 -07003081 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 -05003082 BIC_PRESENT(BIC_PKG__);
3083 BIC_PRESENT(BIC_RAM__);
3084 if (rapl_joules) {
3085 BIC_PRESENT(BIC_Pkg_J);
3086 BIC_PRESENT(BIC_Cor_J);
3087 BIC_PRESENT(BIC_RAM_J);
3088 } else {
3089 BIC_PRESENT(BIC_PkgWatt);
3090 BIC_PRESENT(BIC_CorWatt);
3091 BIC_PRESENT(BIC_RAMWatt);
3092 }
Jacob Pan0f644902016-06-16 09:48:22 -07003093 break;
Len Brown889facb2012-11-08 00:48:57 -05003094 default:
3095 return;
3096 }
3097
3098 /* units on package 0, verify later other packages match */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003099 if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
Len Brown889facb2012-11-08 00:48:57 -05003100 return;
3101
3102 rapl_power_units = 1.0 / (1 << (msr & 0xF));
Len Brown869ce692016-06-16 23:22:37 -04003103 if (model == INTEL_FAM6_ATOM_SILVERMONT1)
Len Brown144b44b2013-11-09 00:30:16 -05003104 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
3105 else
3106 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
Len Brown889facb2012-11-08 00:48:57 -05003107
Andrey Semin40ee8e32014-12-05 00:07:00 -05003108 rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
3109
Len Brown144b44b2013-11-09 00:30:16 -05003110 time_unit = msr >> 16 & 0xF;
3111 if (time_unit == 0)
3112 time_unit = 0xA;
Len Brown889facb2012-11-08 00:48:57 -05003113
Len Brown144b44b2013-11-09 00:30:16 -05003114 rapl_time_units = 1.0 / (1 << (time_unit));
3115
3116 tdp = get_tdp(model);
Len Brown889facb2012-11-08 00:48:57 -05003117
3118 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
Len Brownd8af6f52015-02-10 01:56:38 -05003119 if (debug)
Len Brownb7d8c142016-02-13 23:36:17 -05003120 fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
Len Brown889facb2012-11-08 00:48:57 -05003121
3122 return;
3123}
3124
Colin Ian King1b693172016-03-02 13:50:25 +00003125void perf_limit_reasons_probe(unsigned int family, unsigned int model)
Len Brown3a9a9412014-08-15 02:39:52 -04003126{
3127 if (!genuine_intel)
3128 return;
3129
3130 if (family != 6)
3131 return;
3132
3133 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003134 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3135 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3136 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
Len Brown3a9a9412014-08-15 02:39:52 -04003137 do_gfx_perf_limit_reasons = 1;
Len Brown869ce692016-06-16 23:22:37 -04003138 case INTEL_FAM6_HASWELL_X: /* HSX */
Len Brown3a9a9412014-08-15 02:39:52 -04003139 do_core_perf_limit_reasons = 1;
3140 do_ring_perf_limit_reasons = 1;
3141 default:
3142 return;
3143 }
3144}
3145
Len Brown889facb2012-11-08 00:48:57 -05003146int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3147{
3148 unsigned long long msr;
3149 unsigned int dts;
3150 int cpu;
3151
3152 if (!(do_dts || do_ptm))
3153 return 0;
3154
3155 cpu = t->cpu_id;
3156
3157 /* DTS is per-core, no need to print for each thread */
Len Brown388e9c82016-12-22 23:57:55 -05003158 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
Len Brown889facb2012-11-08 00:48:57 -05003159 return 0;
3160
3161 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003162 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003163 return -1;
3164 }
3165
3166 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
3167 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
3168 return 0;
3169
3170 dts = (msr >> 16) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003171 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003172 cpu, msr, tcc_activation_temp - dts);
3173
3174#ifdef THERM_DEBUG
3175 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
3176 return 0;
3177
3178 dts = (msr >> 16) & 0x7F;
3179 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003180 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003181 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
3182#endif
3183 }
3184
3185
3186 if (do_dts) {
3187 unsigned int resolution;
3188
3189 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
3190 return 0;
3191
3192 dts = (msr >> 16) & 0x7F;
3193 resolution = (msr >> 27) & 0xF;
Len Brownb7d8c142016-02-13 23:36:17 -05003194 fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
Len Brown889facb2012-11-08 00:48:57 -05003195 cpu, msr, tcc_activation_temp - dts, resolution);
3196
3197#ifdef THERM_DEBUG
3198 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
3199 return 0;
3200
3201 dts = (msr >> 16) & 0x7F;
3202 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05003203 fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003204 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
3205#endif
3206 }
3207
3208 return 0;
3209}
Len Brown36229892016-02-26 20:51:02 -05003210
Len Brown889facb2012-11-08 00:48:57 -05003211void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
3212{
Len Brownb7d8c142016-02-13 23:36:17 -05003213 fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05003214 cpu, label,
3215 ((msr >> 15) & 1) ? "EN" : "DIS",
3216 ((msr >> 0) & 0x7FFF) * rapl_power_units,
3217 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
3218 (((msr >> 16) & 1) ? "EN" : "DIS"));
3219
3220 return;
3221}
3222
3223int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3224{
3225 unsigned long long msr;
3226 int cpu;
Len Brown889facb2012-11-08 00:48:57 -05003227
3228 if (!do_rapl)
3229 return 0;
3230
3231 /* RAPL counters are per package, so print only for 1st thread/package */
3232 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3233 return 0;
3234
3235 cpu = t->cpu_id;
3236 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003237 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003238 return -1;
3239 }
3240
3241 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
3242 return -1;
3243
Len Brownd8af6f52015-02-10 01:56:38 -05003244 if (debug) {
Len Brownb7d8c142016-02-13 23:36:17 -05003245 fprintf(outf, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx "
Len Brown889facb2012-11-08 00:48:57 -05003246 "(%f Watts, %f Joules, %f sec.)\n", cpu, msr,
Len Brown144b44b2013-11-09 00:30:16 -05003247 rapl_power_units, rapl_energy_units, rapl_time_units);
Len Brown889facb2012-11-08 00:48:57 -05003248 }
Len Brown144b44b2013-11-09 00:30:16 -05003249 if (do_rapl & RAPL_PKG_POWER_INFO) {
3250
Len Brown889facb2012-11-08 00:48:57 -05003251 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
3252 return -5;
3253
3254
Len Brownb7d8c142016-02-13 23:36:17 -05003255 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 -05003256 cpu, msr,
3257 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3258 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3259 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3260 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
3261
Len Brown144b44b2013-11-09 00:30:16 -05003262 }
3263 if (do_rapl & RAPL_PKG) {
3264
Len Brown889facb2012-11-08 00:48:57 -05003265 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
3266 return -9;
3267
Len Brownb7d8c142016-02-13 23:36:17 -05003268 fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown889facb2012-11-08 00:48:57 -05003269 cpu, msr, (msr >> 63) & 1 ? "": "UN");
3270
3271 print_power_limit_msr(cpu, msr, "PKG Limit #1");
Len Brownb7d8c142016-02-13 23:36:17 -05003272 fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05003273 cpu,
3274 ((msr >> 47) & 1) ? "EN" : "DIS",
3275 ((msr >> 32) & 0x7FFF) * rapl_power_units,
3276 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
3277 ((msr >> 48) & 1) ? "EN" : "DIS");
3278 }
3279
Len Brown0b2bb692015-03-26 00:50:30 -04003280 if (do_rapl & RAPL_DRAM_POWER_INFO) {
Len Brown889facb2012-11-08 00:48:57 -05003281 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
3282 return -6;
3283
Len Brownb7d8c142016-02-13 23:36:17 -05003284 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 -05003285 cpu, msr,
3286 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3287 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3288 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3289 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
Len Brown0b2bb692015-03-26 00:50:30 -04003290 }
3291 if (do_rapl & RAPL_DRAM) {
Len Brown889facb2012-11-08 00:48:57 -05003292 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
3293 return -9;
Len Brownb7d8c142016-02-13 23:36:17 -05003294 fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown889facb2012-11-08 00:48:57 -05003295 cpu, msr, (msr >> 31) & 1 ? "": "UN");
3296
3297 print_power_limit_msr(cpu, msr, "DRAM Limit");
3298 }
Len Brown144b44b2013-11-09 00:30:16 -05003299 if (do_rapl & RAPL_CORE_POLICY) {
Len Brownd8af6f52015-02-10 01:56:38 -05003300 if (debug) {
Len Brown889facb2012-11-08 00:48:57 -05003301 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
3302 return -7;
3303
Len Brownb7d8c142016-02-13 23:36:17 -05003304 fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown144b44b2013-11-09 00:30:16 -05003305 }
3306 }
Jacob Pan91484942016-06-16 09:48:20 -07003307 if (do_rapl & RAPL_CORES_POWER_LIMIT) {
Len Brownd8af6f52015-02-10 01:56:38 -05003308 if (debug) {
Len Brown889facb2012-11-08 00:48:57 -05003309 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
3310 return -9;
Len Brownb7d8c142016-02-13 23:36:17 -05003311 fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown889facb2012-11-08 00:48:57 -05003312 cpu, msr, (msr >> 31) & 1 ? "": "UN");
3313 print_power_limit_msr(cpu, msr, "Cores Limit");
3314 }
3315 }
3316 if (do_rapl & RAPL_GFX) {
Len Brownd8af6f52015-02-10 01:56:38 -05003317 if (debug) {
Len Brown889facb2012-11-08 00:48:57 -05003318 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
3319 return -8;
3320
Len Brownb7d8c142016-02-13 23:36:17 -05003321 fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown889facb2012-11-08 00:48:57 -05003322
3323 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
3324 return -9;
Len Brownb7d8c142016-02-13 23:36:17 -05003325 fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown889facb2012-11-08 00:48:57 -05003326 cpu, msr, (msr >> 31) & 1 ? "": "UN");
3327 print_power_limit_msr(cpu, msr, "GFX Limit");
3328 }
3329 }
3330 return 0;
3331}
3332
Len Brownd7899442015-01-23 00:12:33 -05003333/*
3334 * SNB adds support for additional MSRs:
3335 *
3336 * MSR_PKG_C7_RESIDENCY 0x000003fa
3337 * MSR_CORE_C7_RESIDENCY 0x000003fe
3338 * MSR_PKG_C2_RESIDENCY 0x0000060d
3339 */
Len Brown103a8fe2010-10-22 23:53:03 -04003340
Len Brownd7899442015-01-23 00:12:33 -05003341int has_snb_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04003342{
3343 if (!genuine_intel)
3344 return 0;
3345
3346 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003347 case INTEL_FAM6_SANDYBRIDGE:
3348 case INTEL_FAM6_SANDYBRIDGE_X:
3349 case INTEL_FAM6_IVYBRIDGE: /* IVB */
3350 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
3351 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3352 case INTEL_FAM6_HASWELL_X: /* HSW */
3353 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3354 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3355 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3356 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
3357 case INTEL_FAM6_BROADWELL_X: /* BDX */
3358 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3359 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3360 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3361 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3362 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
3363 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3364 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Xiaolong Wang5bbac262016-09-30 17:53:40 +08003365 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Brown103a8fe2010-10-22 23:53:03 -04003366 return 1;
3367 }
3368 return 0;
3369}
3370
Len Brownd7899442015-01-23 00:12:33 -05003371/*
3372 * HSW adds support for additional MSRs:
3373 *
Len Brown5a634262016-04-06 17:15:55 -04003374 * MSR_PKG_C8_RESIDENCY 0x00000630
3375 * MSR_PKG_C9_RESIDENCY 0x00000631
3376 * MSR_PKG_C10_RESIDENCY 0x00000632
3377 *
3378 * MSR_PKGC8_IRTL 0x00000633
3379 * MSR_PKGC9_IRTL 0x00000634
3380 * MSR_PKGC10_IRTL 0x00000635
3381 *
Len Brownd7899442015-01-23 00:12:33 -05003382 */
3383int has_hsw_msrs(unsigned int family, unsigned int model)
Kristen Carlson Accardica587102012-11-21 05:22:43 -08003384{
3385 if (!genuine_intel)
3386 return 0;
3387
3388 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003389 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3390 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3391 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3392 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3393 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3394 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
3395 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Kristen Carlson Accardica587102012-11-21 05:22:43 -08003396 return 1;
3397 }
3398 return 0;
3399}
3400
Len Brown0b2bb692015-03-26 00:50:30 -04003401/*
3402 * SKL adds support for additional MSRS:
3403 *
3404 * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658
3405 * MSR_PKG_ANY_CORE_C0_RES 0x00000659
3406 * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A
3407 * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B
3408 */
3409int has_skl_msrs(unsigned int family, unsigned int model)
3410{
3411 if (!genuine_intel)
3412 return 0;
3413
3414 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003415 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3416 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3417 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3418 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Brown0b2bb692015-03-26 00:50:30 -04003419 return 1;
3420 }
3421 return 0;
3422}
3423
Len Brown144b44b2013-11-09 00:30:16 -05003424int is_slm(unsigned int family, unsigned int model)
3425{
3426 if (!genuine_intel)
3427 return 0;
3428 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003429 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
3430 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Len Brown144b44b2013-11-09 00:30:16 -05003431 return 1;
3432 }
3433 return 0;
3434}
3435
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003436int is_knl(unsigned int family, unsigned int model)
3437{
3438 if (!genuine_intel)
3439 return 0;
3440 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003441 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003442 case INTEL_FAM6_XEON_PHI_KNM:
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003443 return 1;
3444 }
3445 return 0;
3446}
3447
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02003448unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
3449{
3450 if (is_knl(family, model))
3451 return 1024;
3452 return 1;
3453}
3454
Len Brown144b44b2013-11-09 00:30:16 -05003455#define SLM_BCLK_FREQS 5
3456double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
3457
3458double slm_bclk(void)
3459{
3460 unsigned long long msr = 3;
3461 unsigned int i;
3462 double freq;
3463
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003464 if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
Len Brownb7d8c142016-02-13 23:36:17 -05003465 fprintf(outf, "SLM BCLK: unknown\n");
Len Brown144b44b2013-11-09 00:30:16 -05003466
3467 i = msr & 0xf;
3468 if (i >= SLM_BCLK_FREQS) {
Len Brownb7d8c142016-02-13 23:36:17 -05003469 fprintf(outf, "SLM BCLK[%d] invalid\n", i);
Colin Ian King0a91e552016-04-25 13:03:15 +01003470 i = 3;
Len Brown144b44b2013-11-09 00:30:16 -05003471 }
3472 freq = slm_freq_table[i];
3473
Len Brown8f6196c2017-01-07 22:40:23 -05003474 if (debug)
3475 fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
Len Brown144b44b2013-11-09 00:30:16 -05003476
3477 return freq;
3478}
3479
Len Brown103a8fe2010-10-22 23:53:03 -04003480double discover_bclk(unsigned int family, unsigned int model)
3481{
Chrzaniuk, Hubert121b48b2016-02-10 16:35:17 +01003482 if (has_snb_msrs(family, model) || is_knl(family, model))
Len Brown103a8fe2010-10-22 23:53:03 -04003483 return 100.00;
Len Brown144b44b2013-11-09 00:30:16 -05003484 else if (is_slm(family, model))
3485 return slm_bclk();
Len Brown103a8fe2010-10-22 23:53:03 -04003486 else
3487 return 133.33;
3488}
3489
Len Brown889facb2012-11-08 00:48:57 -05003490/*
3491 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
3492 * the Thermal Control Circuit (TCC) activates.
3493 * This is usually equal to tjMax.
3494 *
3495 * Older processors do not have this MSR, so there we guess,
3496 * but also allow cmdline over-ride with -T.
3497 *
3498 * Several MSR temperature values are in units of degrees-C
3499 * below this value, including the Digital Thermal Sensor (DTS),
3500 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
3501 */
3502int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3503{
3504 unsigned long long msr;
3505 unsigned int target_c_local;
3506 int cpu;
3507
3508 /* tcc_activation_temp is used only for dts or ptm */
3509 if (!(do_dts || do_ptm))
3510 return 0;
3511
3512 /* this is a per-package concept */
3513 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3514 return 0;
3515
3516 cpu = t->cpu_id;
3517 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003518 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003519 return -1;
3520 }
3521
3522 if (tcc_activation_temp_override != 0) {
3523 tcc_activation_temp = tcc_activation_temp_override;
Len Brownb7d8c142016-02-13 23:36:17 -05003524 fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003525 cpu, tcc_activation_temp);
3526 return 0;
3527 }
3528
3529 /* Temperature Target MSR is Nehalem and newer only */
Len Brownd7899442015-01-23 00:12:33 -05003530 if (!do_nhm_platform_info)
Len Brown889facb2012-11-08 00:48:57 -05003531 goto guess;
3532
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003533 if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
Len Brown889facb2012-11-08 00:48:57 -05003534 goto guess;
3535
Jean Delvare34821242014-05-01 11:40:19 +02003536 target_c_local = (msr >> 16) & 0xFF;
Len Brown889facb2012-11-08 00:48:57 -05003537
Len Brownd8af6f52015-02-10 01:56:38 -05003538 if (debug)
Len Brownb7d8c142016-02-13 23:36:17 -05003539 fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003540 cpu, msr, target_c_local);
3541
Jean Delvare34821242014-05-01 11:40:19 +02003542 if (!target_c_local)
Len Brown889facb2012-11-08 00:48:57 -05003543 goto guess;
3544
3545 tcc_activation_temp = target_c_local;
3546
3547 return 0;
3548
3549guess:
3550 tcc_activation_temp = TJMAX_DEFAULT;
Len Brownb7d8c142016-02-13 23:36:17 -05003551 fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
Len Brown889facb2012-11-08 00:48:57 -05003552 cpu, tcc_activation_temp);
3553
3554 return 0;
3555}
Len Brown69807a62015-11-21 12:22:47 -05003556
Len Brownaa8d8cc2016-03-11 13:26:03 -05003557void decode_feature_control_msr(void)
3558{
3559 unsigned long long msr;
3560
3561 if (!get_msr(base_cpu, MSR_IA32_FEATURE_CONTROL, &msr))
3562 fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
3563 base_cpu, msr,
3564 msr & FEATURE_CONTROL_LOCKED ? "" : "UN-",
3565 msr & (1 << 18) ? "SGX" : "");
3566}
3567
Len Brown69807a62015-11-21 12:22:47 -05003568void decode_misc_enable_msr(void)
3569{
3570 unsigned long long msr;
3571
3572 if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
Len Browne6512622017-01-11 23:17:24 -05003573 fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n",
Len Brown69807a62015-11-21 12:22:47 -05003574 base_cpu, msr,
Len Browne6512622017-01-11 23:17:24 -05003575 msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-",
3576 msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-",
3577 msr & MSR_IA32_MISC_ENABLE_MWAIT ? "No-" : "",
3578 msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "",
3579 msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : "");
Len Brown69807a62015-11-21 12:22:47 -05003580}
3581
Len Brownf0057312015-12-03 01:35:36 -05003582/*
3583 * Decode MSR_MISC_PWR_MGMT
3584 *
3585 * Decode the bits according to the Nehalem documentation
3586 * bit[0] seems to continue to have same meaning going forward
3587 * bit[1] less so...
3588 */
3589void decode_misc_pwr_mgmt_msr(void)
3590{
3591 unsigned long long msr;
3592
3593 if (!do_nhm_platform_info)
3594 return;
3595
Len Browncf4cbe52017-01-01 13:08:33 -05003596 if (no_MSR_MISC_PWR_MGMT)
3597 return;
3598
Len Brownf0057312015-12-03 01:35:36 -05003599 if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
Srinivas Pandruvadaddadb8a2016-11-11 14:29:48 -08003600 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 -05003601 base_cpu, msr,
3602 msr & (1 << 0) ? "DIS" : "EN",
Srinivas Pandruvadaddadb8a2016-11-11 14:29:48 -08003603 msr & (1 << 1) ? "EN" : "DIS",
3604 msr & (1 << 8) ? "EN" : "DIS");
Len Brownf0057312015-12-03 01:35:36 -05003605}
Len Brown71616c82017-01-07 22:37:48 -05003606/*
3607 * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG
3608 *
3609 * This MSRs are present on Silvermont processors,
3610 * Intel Atom processor E3000 series (Baytrail), and friends.
3611 */
3612void decode_c6_demotion_policy_msr(void)
3613{
3614 unsigned long long msr;
3615
3616 if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr))
3617 fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n",
3618 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
3619
3620 if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr))
3621 fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n",
3622 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
3623}
Len Brown7f5c2582015-12-01 01:36:39 -05003624
Len Brownfcd17212015-03-23 20:29:09 -04003625void process_cpuid()
Len Brown103a8fe2010-10-22 23:53:03 -04003626{
Len Brown61a87ba2015-11-23 02:30:51 -05003627 unsigned int eax, ebx, ecx, edx, max_level, max_extended_level;
Len Brown103a8fe2010-10-22 23:53:03 -04003628 unsigned int fms, family, model, stepping;
Len Brownb3a34e92017-01-21 00:50:08 -05003629 unsigned int has_turbo;
Len Brown103a8fe2010-10-22 23:53:03 -04003630
3631 eax = ebx = ecx = edx = 0;
3632
Len Brown5aea2f72016-03-13 03:14:35 -04003633 __cpuid(0, max_level, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04003634
3635 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
3636 genuine_intel = 1;
3637
Len Brownd8af6f52015-02-10 01:56:38 -05003638 if (debug)
Len Brownb7d8c142016-02-13 23:36:17 -05003639 fprintf(outf, "CPUID(0): %.4s%.4s%.4s ",
Len Brown103a8fe2010-10-22 23:53:03 -04003640 (char *)&ebx, (char *)&edx, (char *)&ecx);
3641
Len Brown5aea2f72016-03-13 03:14:35 -04003642 __cpuid(1, fms, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04003643 family = (fms >> 8) & 0xf;
3644 model = (fms >> 4) & 0xf;
3645 stepping = fms & 0xf;
3646 if (family == 6 || family == 0xf)
3647 model += ((fms >> 16) & 0xf) << 4;
3648
Len Brown69807a62015-11-21 12:22:47 -05003649 if (debug) {
Len Brownb7d8c142016-02-13 23:36:17 -05003650 fprintf(outf, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
Len Brown103a8fe2010-10-22 23:53:03 -04003651 max_level, family, model, stepping, family, model, stepping);
Len Brownaa8d8cc2016-03-11 13:26:03 -05003652 fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s\n",
Len Brown69807a62015-11-21 12:22:47 -05003653 ecx & (1 << 0) ? "SSE3" : "-",
3654 ecx & (1 << 3) ? "MONITOR" : "-",
Len Brownaa8d8cc2016-03-11 13:26:03 -05003655 ecx & (1 << 6) ? "SMX" : "-",
Len Brown69807a62015-11-21 12:22:47 -05003656 ecx & (1 << 7) ? "EIST" : "-",
3657 ecx & (1 << 8) ? "TM2" : "-",
3658 edx & (1 << 4) ? "TSC" : "-",
3659 edx & (1 << 5) ? "MSR" : "-",
3660 edx & (1 << 22) ? "ACPI-TM" : "-",
3661 edx & (1 << 29) ? "TM" : "-");
3662 }
Len Brown103a8fe2010-10-22 23:53:03 -04003663
Josh Triplettb2c95d92013-08-20 17:20:18 -07003664 if (!(edx & (1 << 5)))
3665 errx(1, "CPUID: no MSR");
Len Brown103a8fe2010-10-22 23:53:03 -04003666
3667 /*
3668 * check max extended function levels of CPUID.
3669 * This is needed to check for invariant TSC.
3670 * This check is valid for both Intel and AMD.
3671 */
3672 ebx = ecx = edx = 0;
Len Brown5aea2f72016-03-13 03:14:35 -04003673 __cpuid(0x80000000, max_extended_level, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04003674
Len Brown61a87ba2015-11-23 02:30:51 -05003675 if (max_extended_level >= 0x80000007) {
Len Brown103a8fe2010-10-22 23:53:03 -04003676
Len Brownd7899442015-01-23 00:12:33 -05003677 /*
3678 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
3679 * this check is valid for both Intel and AMD
3680 */
Len Brown5aea2f72016-03-13 03:14:35 -04003681 __cpuid(0x80000007, eax, ebx, ecx, edx);
Len Brownd7899442015-01-23 00:12:33 -05003682 has_invariant_tsc = edx & (1 << 8);
3683 }
Len Brown103a8fe2010-10-22 23:53:03 -04003684
3685 /*
3686 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
3687 * this check is valid for both Intel and AMD
3688 */
3689
Len Brown5aea2f72016-03-13 03:14:35 -04003690 __cpuid(0x6, eax, ebx, ecx, edx);
Thomas Renninger8209e052011-01-21 15:11:19 +01003691 has_aperf = ecx & (1 << 0);
Len Brown812db3f2017-02-10 00:25:41 -05003692 if (has_aperf) {
3693 BIC_PRESENT(BIC_Avg_MHz);
3694 BIC_PRESENT(BIC_Busy);
3695 BIC_PRESENT(BIC_Bzy_MHz);
3696 }
Len Brown889facb2012-11-08 00:48:57 -05003697 do_dts = eax & (1 << 0);
Len Brown812db3f2017-02-10 00:25:41 -05003698 if (do_dts)
3699 BIC_PRESENT(BIC_CoreTmp);
Len Brownb3a34e92017-01-21 00:50:08 -05003700 has_turbo = eax & (1 << 1);
Len Brown889facb2012-11-08 00:48:57 -05003701 do_ptm = eax & (1 << 6);
Len Brown812db3f2017-02-10 00:25:41 -05003702 if (do_ptm)
3703 BIC_PRESENT(BIC_PkgTmp);
Len Brown7f5c2582015-12-01 01:36:39 -05003704 has_hwp = eax & (1 << 7);
3705 has_hwp_notify = eax & (1 << 8);
3706 has_hwp_activity_window = eax & (1 << 9);
3707 has_hwp_epp = eax & (1 << 10);
3708 has_hwp_pkg = eax & (1 << 11);
Len Brown889facb2012-11-08 00:48:57 -05003709 has_epb = ecx & (1 << 3);
3710
Len Brownd8af6f52015-02-10 01:56:38 -05003711 if (debug)
Len Brownb3a34e92017-01-21 00:50:08 -05003712 fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, "
Len Brown7f5c2582015-12-01 01:36:39 -05003713 "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
3714 has_aperf ? "" : "No-",
Len Brownb3a34e92017-01-21 00:50:08 -05003715 has_turbo ? "" : "No-",
Len Brown7f5c2582015-12-01 01:36:39 -05003716 do_dts ? "" : "No-",
3717 do_ptm ? "" : "No-",
3718 has_hwp ? "" : "No-",
3719 has_hwp_notify ? "" : "No-",
3720 has_hwp_activity_window ? "" : "No-",
3721 has_hwp_epp ? "" : "No-",
3722 has_hwp_pkg ? "" : "No-",
3723 has_epb ? "" : "No-");
Len Brown103a8fe2010-10-22 23:53:03 -04003724
Len Brown69807a62015-11-21 12:22:47 -05003725 if (debug)
3726 decode_misc_enable_msr();
3727
Len Brown8ae72252016-04-06 17:15:54 -04003728 if (max_level >= 0x7 && debug) {
Len Brownaa8d8cc2016-03-11 13:26:03 -05003729 int has_sgx;
3730
3731 ecx = 0;
3732
3733 __cpuid_count(0x7, 0, eax, ebx, ecx, edx);
3734
3735 has_sgx = ebx & (1 << 2);
3736 fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-");
3737
3738 if (has_sgx)
3739 decode_feature_control_msr();
3740 }
3741
Len Brown61a87ba2015-11-23 02:30:51 -05003742 if (max_level >= 0x15) {
Len Brown8a5bdf42015-04-01 21:02:57 -04003743 unsigned int eax_crystal;
3744 unsigned int ebx_tsc;
3745
3746 /*
3747 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
3748 */
3749 eax_crystal = ebx_tsc = crystal_hz = edx = 0;
Len Brown5aea2f72016-03-13 03:14:35 -04003750 __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx);
Len Brown8a5bdf42015-04-01 21:02:57 -04003751
3752 if (ebx_tsc != 0) {
3753
3754 if (debug && (ebx != 0))
Len Brownb7d8c142016-02-13 23:36:17 -05003755 fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04003756 eax_crystal, ebx_tsc, crystal_hz);
3757
3758 if (crystal_hz == 0)
3759 switch(model) {
Len Brown869ce692016-06-16 23:22:37 -04003760 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3761 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3762 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3763 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Browne8efbc82016-04-06 17:15:57 -04003764 crystal_hz = 24000000; /* 24.0 MHz */
3765 break;
Len Brown869ce692016-06-16 23:22:37 -04003766 case INTEL_FAM6_SKYLAKE_X: /* SKX */
Len Brown7268d402016-12-01 23:10:39 -05003767 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Brownec53e592016-04-06 17:15:58 -04003768 crystal_hz = 25000000; /* 25.0 MHz */
3769 break;
Len Brown869ce692016-06-16 23:22:37 -04003770 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Browne8efbc82016-04-06 17:15:57 -04003771 crystal_hz = 19200000; /* 19.2 MHz */
Len Brown8a5bdf42015-04-01 21:02:57 -04003772 break;
3773 default:
3774 crystal_hz = 0;
3775 }
3776
3777 if (crystal_hz) {
3778 tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
3779 if (debug)
Len Brownb7d8c142016-02-13 23:36:17 -05003780 fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04003781 tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal);
3782 }
3783 }
3784 }
Len Brown61a87ba2015-11-23 02:30:51 -05003785 if (max_level >= 0x16) {
3786 unsigned int base_mhz, max_mhz, bus_mhz, edx;
3787
3788 /*
3789 * CPUID 16H Base MHz, Max MHz, Bus MHz
3790 */
3791 base_mhz = max_mhz = bus_mhz = edx = 0;
3792
Len Brown5aea2f72016-03-13 03:14:35 -04003793 __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx);
Len Brown61a87ba2015-11-23 02:30:51 -05003794 if (debug)
Len Brownb7d8c142016-02-13 23:36:17 -05003795 fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
Len Brown61a87ba2015-11-23 02:30:51 -05003796 base_mhz, max_mhz, bus_mhz);
3797 }
Len Brown8a5bdf42015-04-01 21:02:57 -04003798
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02003799 if (has_aperf)
3800 aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
3801
Len Brown812db3f2017-02-10 00:25:41 -05003802 BIC_PRESENT(BIC_IRQ);
3803 BIC_PRESENT(BIC_TSC_MHz);
3804
3805 if (probe_nhm_msrs(family, model)) {
3806 do_nhm_platform_info = 1;
3807 BIC_PRESENT(BIC_CPU_c1);
3808 BIC_PRESENT(BIC_CPU_c3);
3809 BIC_PRESENT(BIC_CPU_c6);
3810 BIC_PRESENT(BIC_SMI);
3811 }
Len Brownd7899442015-01-23 00:12:33 -05003812 do_snb_cstates = has_snb_msrs(family, model);
Len Brown812db3f2017-02-10 00:25:41 -05003813
3814 if (do_snb_cstates)
3815 BIC_PRESENT(BIC_CPU_c7);
3816
Len Brown5a634262016-04-06 17:15:55 -04003817 do_irtl_snb = has_snb_msrs(family, model);
Len Brownee7e38e2015-02-09 23:39:45 -05003818 do_pc2 = do_snb_cstates && (pkg_cstate_limit >= PCL__2);
3819 do_pc3 = (pkg_cstate_limit >= PCL__3);
3820 do_pc6 = (pkg_cstate_limit >= PCL__6);
3821 do_pc7 = do_snb_cstates && (pkg_cstate_limit >= PCL__7);
Len Brown0539ba12017-02-10 00:27:20 -05003822 if (has_slv_msrs(family, model)) {
3823 do_pc2 = do_pc3 = do_pc7 = 0;
3824 do_pc6 = 1;
3825 BIC_PRESENT(BIC_Mod_c6);
3826 use_c1_residency_msr = 1;
3827 }
Len Brownd7899442015-01-23 00:12:33 -05003828 do_c8_c9_c10 = has_hsw_msrs(family, model);
Len Brown5a634262016-04-06 17:15:55 -04003829 do_irtl_hsw = has_hsw_msrs(family, model);
Len Brown0b2bb692015-03-26 00:50:30 -04003830 do_skl_residency = has_skl_msrs(family, model);
Len Brown144b44b2013-11-09 00:30:16 -05003831 do_slm_cstates = is_slm(family, model);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003832 do_knl_cstates = is_knl(family, model);
Len Brown103a8fe2010-10-22 23:53:03 -04003833
Len Brownf0057312015-12-03 01:35:36 -05003834 if (debug)
3835 decode_misc_pwr_mgmt_msr();
3836
Len Brown71616c82017-01-07 22:37:48 -05003837 if (debug && has_slv_msrs(family, model))
3838 decode_c6_demotion_policy_msr();
3839
Len Brown889facb2012-11-08 00:48:57 -05003840 rapl_probe(family, model);
Len Brown3a9a9412014-08-15 02:39:52 -04003841 perf_limit_reasons_probe(family, model);
Len Brown889facb2012-11-08 00:48:57 -05003842
Len Brownfcd17212015-03-23 20:29:09 -04003843 if (debug)
Colin Ian King1b693172016-03-02 13:50:25 +00003844 dump_cstate_pstate_config_info(family, model);
Len Brownfcd17212015-03-23 20:29:09 -04003845
Len Browna2b7b742015-09-26 00:12:38 -04003846 if (has_skl_msrs(family, model))
3847 calculate_tsc_tweak();
3848
Len Brown812db3f2017-02-10 00:25:41 -05003849 if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK))
3850 BIC_PRESENT(BIC_GFX_rc6);
Len Brownfdf676e2016-02-27 01:28:12 -05003851
Len Brown812db3f2017-02-10 00:25:41 -05003852 if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK))
3853 BIC_PRESENT(BIC_GFXMHz);
Len Brown27d47352016-02-27 00:37:54 -05003854
Len Brown889facb2012-11-08 00:48:57 -05003855 return;
Len Brown103a8fe2010-10-22 23:53:03 -04003856}
3857
Len Brownd8af6f52015-02-10 01:56:38 -05003858void help()
Len Brown103a8fe2010-10-22 23:53:03 -04003859{
Len Brownb7d8c142016-02-13 23:36:17 -05003860 fprintf(outf,
Len Brownd8af6f52015-02-10 01:56:38 -05003861 "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
3862 "\n"
3863 "Turbostat forks the specified COMMAND and prints statistics\n"
3864 "when COMMAND completes.\n"
3865 "If no COMMAND is specified, turbostat wakes every 5-seconds\n"
3866 "to print statistics, until interrupted.\n"
Len Brown388e9c82016-12-22 23:57:55 -05003867 "--add add a counter\n"
3868 " eg. --add msr0x10,u64,cpu,delta,MY_TSC\n"
Len Brownd8af6f52015-02-10 01:56:38 -05003869 "--debug run in \"debug\" mode\n"
3870 "--interval sec Override default 5-second measurement interval\n"
3871 "--help print this help message\n"
Len Brownb7d8c142016-02-13 23:36:17 -05003872 "--out file create or truncate \"file\" for all output\n"
Len Brownd8af6f52015-02-10 01:56:38 -05003873 "--version print version information\n"
3874 "\n"
3875 "For more help, run \"man turbostat\"\n");
Len Brown103a8fe2010-10-22 23:53:03 -04003876}
3877
3878
3879/*
3880 * in /dev/cpu/ return success for names that are numbers
3881 * ie. filter out ".", "..", "microcode".
3882 */
3883int dir_filter(const struct dirent *dirp)
3884{
3885 if (isdigit(dirp->d_name[0]))
3886 return 1;
3887 else
3888 return 0;
3889}
3890
3891int open_dev_cpu_msr(int dummy1)
3892{
3893 return 0;
3894}
3895
Len Brownc98d5d92012-06-04 00:56:40 -04003896void topology_probe()
3897{
3898 int i;
3899 int max_core_id = 0;
3900 int max_package_id = 0;
3901 int max_siblings = 0;
3902 struct cpu_topology {
3903 int core_id;
3904 int physical_package_id;
3905 } *cpus;
3906
3907 /* Initialize num_cpus, max_cpu_num */
3908 topo.num_cpus = 0;
3909 topo.max_cpu_num = 0;
3910 for_all_proc_cpus(count_cpus);
3911 if (!summary_only && topo.num_cpus > 1)
Len Brown812db3f2017-02-10 00:25:41 -05003912 BIC_PRESENT(BIC_CPU);
Len Brownc98d5d92012-06-04 00:56:40 -04003913
Len Brownd8af6f52015-02-10 01:56:38 -05003914 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05003915 fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
Len Brownc98d5d92012-06-04 00:56:40 -04003916
3917 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
Josh Triplettb2c95d92013-08-20 17:20:18 -07003918 if (cpus == NULL)
3919 err(1, "calloc cpus");
Len Brownc98d5d92012-06-04 00:56:40 -04003920
3921 /*
3922 * Allocate and initialize cpu_present_set
3923 */
3924 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07003925 if (cpu_present_set == NULL)
3926 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04003927 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
3928 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
3929 for_all_proc_cpus(mark_cpu_present);
3930
3931 /*
3932 * Allocate and initialize cpu_affinity_set
3933 */
3934 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07003935 if (cpu_affinity_set == NULL)
3936 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04003937 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
3938 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
3939
3940
3941 /*
3942 * For online cpus
3943 * find max_core_id, max_package_id
3944 */
3945 for (i = 0; i <= topo.max_cpu_num; ++i) {
3946 int siblings;
3947
3948 if (cpu_is_not_present(i)) {
Len Brownd8af6f52015-02-10 01:56:38 -05003949 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05003950 fprintf(outf, "cpu%d NOT PRESENT\n", i);
Len Brownc98d5d92012-06-04 00:56:40 -04003951 continue;
3952 }
3953 cpus[i].core_id = get_core_id(i);
3954 if (cpus[i].core_id > max_core_id)
3955 max_core_id = cpus[i].core_id;
3956
3957 cpus[i].physical_package_id = get_physical_package_id(i);
3958 if (cpus[i].physical_package_id > max_package_id)
3959 max_package_id = cpus[i].physical_package_id;
3960
3961 siblings = get_num_ht_siblings(i);
3962 if (siblings > max_siblings)
3963 max_siblings = siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05003964 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05003965 fprintf(outf, "cpu %d pkg %d core %d\n",
Len Brownc98d5d92012-06-04 00:56:40 -04003966 i, cpus[i].physical_package_id, cpus[i].core_id);
3967 }
3968 topo.num_cores_per_pkg = max_core_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05003969 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05003970 fprintf(outf, "max_core_id %d, sizing for %d cores per package\n",
Len Brownc98d5d92012-06-04 00:56:40 -04003971 max_core_id, topo.num_cores_per_pkg);
Len Brown1cc21f72015-02-23 00:34:57 -05003972 if (debug && !summary_only && topo.num_cores_per_pkg > 1)
Len Brown812db3f2017-02-10 00:25:41 -05003973 BIC_PRESENT(BIC_Core);
Len Brownc98d5d92012-06-04 00:56:40 -04003974
3975 topo.num_packages = max_package_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05003976 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05003977 fprintf(outf, "max_package_id %d, sizing for %d packages\n",
Len Brownc98d5d92012-06-04 00:56:40 -04003978 max_package_id, topo.num_packages);
Len Brown1cc21f72015-02-23 00:34:57 -05003979 if (debug && !summary_only && topo.num_packages > 1)
Len Brown812db3f2017-02-10 00:25:41 -05003980 BIC_PRESENT(BIC_Package);
Len Brownc98d5d92012-06-04 00:56:40 -04003981
3982 topo.num_threads_per_core = max_siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05003983 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05003984 fprintf(outf, "max_siblings %d\n", max_siblings);
Len Brownc98d5d92012-06-04 00:56:40 -04003985
3986 free(cpus);
3987}
3988
3989void
3990allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
3991{
3992 int i;
3993
3994 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
Len Brown678a3bd2017-02-09 22:22:13 -05003995 topo.num_packages, sizeof(struct thread_data));
Len Brownc98d5d92012-06-04 00:56:40 -04003996 if (*t == NULL)
3997 goto error;
3998
3999 for (i = 0; i < topo.num_threads_per_core *
4000 topo.num_cores_per_pkg * topo.num_packages; i++)
4001 (*t)[i].cpu_id = -1;
4002
4003 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
Len Brown678a3bd2017-02-09 22:22:13 -05004004 sizeof(struct core_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004005 if (*c == NULL)
4006 goto error;
4007
4008 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
4009 (*c)[i].core_id = -1;
4010
Len Brown678a3bd2017-02-09 22:22:13 -05004011 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
Len Brownc98d5d92012-06-04 00:56:40 -04004012 if (*p == NULL)
4013 goto error;
4014
4015 for (i = 0; i < topo.num_packages; i++)
4016 (*p)[i].package_id = i;
4017
4018 return;
4019error:
Josh Triplettb2c95d92013-08-20 17:20:18 -07004020 err(1, "calloc counters");
Len Brownc98d5d92012-06-04 00:56:40 -04004021}
4022/*
4023 * init_counter()
4024 *
4025 * set cpu_id, core_num, pkg_num
4026 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
4027 *
4028 * increment topo.num_cores when 1st core in pkg seen
4029 */
4030void init_counter(struct thread_data *thread_base, struct core_data *core_base,
4031 struct pkg_data *pkg_base, int thread_num, int core_num,
4032 int pkg_num, int cpu_id)
4033{
4034 struct thread_data *t;
4035 struct core_data *c;
4036 struct pkg_data *p;
4037
4038 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
4039 c = GET_CORE(core_base, core_num, pkg_num);
4040 p = GET_PKG(pkg_base, pkg_num);
4041
4042 t->cpu_id = cpu_id;
4043 if (thread_num == 0) {
4044 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
4045 if (cpu_is_first_core_in_package(cpu_id))
4046 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
4047 }
4048
4049 c->core_id = core_num;
4050 p->package_id = pkg_num;
4051}
4052
4053
4054int initialize_counters(int cpu_id)
4055{
4056 int my_thread_id, my_core_id, my_package_id;
4057
4058 my_package_id = get_physical_package_id(cpu_id);
4059 my_core_id = get_core_id(cpu_id);
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07004060 my_thread_id = get_cpu_position_in_core(cpu_id);
4061 if (!my_thread_id)
Len Brownc98d5d92012-06-04 00:56:40 -04004062 topo.num_cores++;
Len Brownc98d5d92012-06-04 00:56:40 -04004063
4064 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
4065 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
4066 return 0;
4067}
4068
4069void allocate_output_buffer()
4070{
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004071 output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
Len Brownc98d5d92012-06-04 00:56:40 -04004072 outp = output_buffer;
Josh Triplettb2c95d92013-08-20 17:20:18 -07004073 if (outp == NULL)
4074 err(-1, "calloc output buffer");
Len Brownc98d5d92012-06-04 00:56:40 -04004075}
Len Brown36229892016-02-26 20:51:02 -05004076void allocate_fd_percpu(void)
4077{
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004078 fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
Len Brown36229892016-02-26 20:51:02 -05004079 if (fd_percpu == NULL)
4080 err(-1, "calloc fd_percpu");
4081}
Len Brown562a2d32016-02-26 23:48:05 -05004082void allocate_irq_buffers(void)
4083{
4084 irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int));
4085 if (irq_column_2_cpu == NULL)
4086 err(-1, "calloc %d", topo.num_cpus);
Len Brownc98d5d92012-06-04 00:56:40 -04004087
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004088 irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int));
Len Brown562a2d32016-02-26 23:48:05 -05004089 if (irqs_per_cpu == NULL)
Mika Westerberg01a67ad2016-04-22 11:13:23 +03004090 err(-1, "calloc %d", topo.max_cpu_num + 1);
Len Brown562a2d32016-02-26 23:48:05 -05004091}
Len Brownc98d5d92012-06-04 00:56:40 -04004092void setup_all_buffers(void)
4093{
4094 topology_probe();
Len Brown562a2d32016-02-26 23:48:05 -05004095 allocate_irq_buffers();
Len Brown36229892016-02-26 20:51:02 -05004096 allocate_fd_percpu();
Len Brownc98d5d92012-06-04 00:56:40 -04004097 allocate_counters(&thread_even, &core_even, &package_even);
4098 allocate_counters(&thread_odd, &core_odd, &package_odd);
4099 allocate_output_buffer();
4100 for_all_proc_cpus(initialize_counters);
4101}
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004102
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004103void set_base_cpu(void)
4104{
4105 base_cpu = sched_getcpu();
4106 if (base_cpu < 0)
4107 err(-ENODEV, "No valid cpus found");
4108
4109 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05004110 fprintf(outf, "base_cpu = %d\n", base_cpu);
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004111}
4112
Len Brown103a8fe2010-10-22 23:53:03 -04004113void turbostat_init()
4114{
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04004115 setup_all_buffers();
4116 set_base_cpu();
Len Brown103a8fe2010-10-22 23:53:03 -04004117 check_dev_msr();
Len Brown98481e72014-08-15 00:36:50 -04004118 check_permissions();
Len Brownfcd17212015-03-23 20:29:09 -04004119 process_cpuid();
Len Brown103a8fe2010-10-22 23:53:03 -04004120
Len Brown103a8fe2010-10-22 23:53:03 -04004121
Len Brownd8af6f52015-02-10 01:56:38 -05004122 if (debug)
Len Brown7f5c2582015-12-01 01:36:39 -05004123 for_all_cpus(print_hwp, ODD_COUNTERS);
4124
4125 if (debug)
Len Brown889facb2012-11-08 00:48:57 -05004126 for_all_cpus(print_epb, ODD_COUNTERS);
4127
Len Brownd8af6f52015-02-10 01:56:38 -05004128 if (debug)
Len Brown3a9a9412014-08-15 02:39:52 -04004129 for_all_cpus(print_perf_limit, ODD_COUNTERS);
4130
Len Brownd8af6f52015-02-10 01:56:38 -05004131 if (debug)
Len Brown889facb2012-11-08 00:48:57 -05004132 for_all_cpus(print_rapl, ODD_COUNTERS);
4133
4134 for_all_cpus(set_temperature_target, ODD_COUNTERS);
4135
Len Brownd8af6f52015-02-10 01:56:38 -05004136 if (debug)
Len Brown889facb2012-11-08 00:48:57 -05004137 for_all_cpus(print_thermal, ODD_COUNTERS);
Len Brown5a634262016-04-06 17:15:55 -04004138
4139 if (debug && do_irtl_snb)
4140 print_irtl();
Len Brown103a8fe2010-10-22 23:53:03 -04004141}
4142
4143int fork_it(char **argv)
4144{
Len Brown103a8fe2010-10-22 23:53:03 -04004145 pid_t child_pid;
Len Brownd91bb172012-11-01 00:08:19 -04004146 int status;
Len Brownd15cf7c2012-06-03 23:24:00 -04004147
Len Brownd91bb172012-11-01 00:08:19 -04004148 status = for_all_cpus(get_counters, EVEN_COUNTERS);
4149 if (status)
4150 exit(status);
Len Brownc98d5d92012-06-04 00:56:40 -04004151 /* clear affinity side-effect of get_counters() */
4152 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
Len Brown103a8fe2010-10-22 23:53:03 -04004153 gettimeofday(&tv_even, (struct timezone *)NULL);
4154
4155 child_pid = fork();
4156 if (!child_pid) {
4157 /* child */
4158 execvp(argv[0], argv);
4159 } else {
Len Brown103a8fe2010-10-22 23:53:03 -04004160
4161 /* parent */
Josh Triplettb2c95d92013-08-20 17:20:18 -07004162 if (child_pid == -1)
4163 err(1, "fork");
Len Brown103a8fe2010-10-22 23:53:03 -04004164
4165 signal(SIGINT, SIG_IGN);
4166 signal(SIGQUIT, SIG_IGN);
Josh Triplettb2c95d92013-08-20 17:20:18 -07004167 if (waitpid(child_pid, &status, 0) == -1)
4168 err(status, "waitpid");
Len Brown103a8fe2010-10-22 23:53:03 -04004169 }
Len Brownc98d5d92012-06-04 00:56:40 -04004170 /*
4171 * n.b. fork_it() does not check for errors from for_all_cpus()
4172 * because re-starting is problematic when forking
4173 */
4174 for_all_cpus(get_counters, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04004175 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04004176 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04004177 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS))
4178 fprintf(outf, "%s: Counter reset detected\n", progname);
4179 else {
4180 compute_average(EVEN_COUNTERS);
4181 format_all_counters(EVEN_COUNTERS);
4182 }
Len Brown103a8fe2010-10-22 23:53:03 -04004183
Len Brownb7d8c142016-02-13 23:36:17 -05004184 fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
4185
4186 flush_output_stderr();
Len Brown103a8fe2010-10-22 23:53:03 -04004187
Len Brownd91bb172012-11-01 00:08:19 -04004188 return status;
Len Brown103a8fe2010-10-22 23:53:03 -04004189}
4190
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004191int get_and_dump_counters(void)
4192{
4193 int status;
4194
4195 status = for_all_cpus(get_counters, ODD_COUNTERS);
4196 if (status)
4197 return status;
4198
4199 status = for_all_cpus(dump_counters, ODD_COUNTERS);
4200 if (status)
4201 return status;
4202
Len Brownb7d8c142016-02-13 23:36:17 -05004203 flush_output_stdout();
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004204
4205 return status;
4206}
4207
Len Brownd8af6f52015-02-10 01:56:38 -05004208void print_version() {
Len Brown0539ba12017-02-10 00:27:20 -05004209 fprintf(outf, "turbostat version 4.17 10 Jan 2017"
Len Brownd8af6f52015-02-10 01:56:38 -05004210 " - Len Brown <lenb@kernel.org>\n");
4211}
4212
Len Brown388e9c82016-12-22 23:57:55 -05004213int add_counter(unsigned int msr_num, char *name, unsigned int width,
4214 enum counter_scope scope, enum counter_type type,
4215 enum counter_format format)
4216{
4217 struct msr_counter *msrp;
4218
4219 msrp = calloc(1, sizeof(struct msr_counter));
4220 if (msrp == NULL) {
4221 perror("calloc");
4222 exit(1);
4223 }
4224
4225 msrp->msr_num = msr_num;
4226 strncpy(msrp->name, name, NAME_BYTES);
4227 msrp->width = width;
4228 msrp->type = type;
4229 msrp->format = format;
4230
4231 switch (scope) {
4232
4233 case SCOPE_CPU:
Len Brown388e9c82016-12-22 23:57:55 -05004234 msrp->next = sys.tp;
4235 sys.tp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05004236 sys.added_thread_counters++;
4237 if (sys.added_thread_counters > MAX_ADDED_COUNTERS) {
4238 fprintf(stderr, "exceeded max %d added thread counters\n",
4239 MAX_ADDED_COUNTERS);
4240 exit(-1);
4241 }
Len Brown388e9c82016-12-22 23:57:55 -05004242 break;
4243
4244 case SCOPE_CORE:
Len Brown388e9c82016-12-22 23:57:55 -05004245 msrp->next = sys.cp;
4246 sys.cp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05004247 sys.added_core_counters++;
4248 if (sys.added_core_counters > MAX_ADDED_COUNTERS) {
4249 fprintf(stderr, "exceeded max %d added core counters\n",
4250 MAX_ADDED_COUNTERS);
4251 exit(-1);
4252 }
Len Brown388e9c82016-12-22 23:57:55 -05004253 break;
4254
4255 case SCOPE_PACKAGE:
Len Brown388e9c82016-12-22 23:57:55 -05004256 msrp->next = sys.pp;
4257 sys.pp = msrp;
Len Brown678a3bd2017-02-09 22:22:13 -05004258 sys.added_package_counters++;
4259 if (sys.added_package_counters > MAX_ADDED_COUNTERS) {
4260 fprintf(stderr, "exceeded max %d added package counters\n",
4261 MAX_ADDED_COUNTERS);
4262 exit(-1);
4263 }
Len Brown388e9c82016-12-22 23:57:55 -05004264 break;
4265 }
4266
4267 return 0;
4268}
4269
4270void parse_add_command(char *add_command)
4271{
4272 int msr_num = 0;
4273 char name_buffer[NAME_BYTES];
4274 int width = 64;
4275 int fail = 0;
4276 enum counter_scope scope = SCOPE_CPU;
4277 enum counter_type type = COUNTER_CYCLES;
4278 enum counter_format format = FORMAT_DELTA;
4279
4280 while (add_command) {
4281
4282 if (sscanf(add_command, "msr0x%x", &msr_num) == 1)
4283 goto next;
4284
4285 if (sscanf(add_command, "msr%d", &msr_num) == 1)
4286 goto next;
4287
4288 if (sscanf(add_command, "u%d", &width) == 1) {
4289 if ((width == 32) || (width == 64))
4290 goto next;
4291 width = 64;
4292 }
4293 if (!strncmp(add_command, "cpu", strlen("cpu"))) {
4294 scope = SCOPE_CPU;
4295 goto next;
4296 }
4297 if (!strncmp(add_command, "core", strlen("core"))) {
4298 scope = SCOPE_CORE;
4299 goto next;
4300 }
4301 if (!strncmp(add_command, "package", strlen("package"))) {
4302 scope = SCOPE_PACKAGE;
4303 goto next;
4304 }
4305 if (!strncmp(add_command, "cycles", strlen("cycles"))) {
4306 type = COUNTER_CYCLES;
4307 goto next;
4308 }
4309 if (!strncmp(add_command, "seconds", strlen("seconds"))) {
4310 type = COUNTER_SECONDS;
4311 goto next;
4312 }
4313 if (!strncmp(add_command, "raw", strlen("raw"))) {
4314 format = FORMAT_RAW;
4315 goto next;
4316 }
4317 if (!strncmp(add_command, "delta", strlen("delta"))) {
4318 format = FORMAT_DELTA;
4319 goto next;
4320 }
4321 if (!strncmp(add_command, "percent", strlen("percent"))) {
4322 format = FORMAT_PERCENT;
4323 goto next;
4324 }
4325
4326 if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) { /* 18 < NAME_BYTES */
4327 char *eos;
4328
4329 eos = strchr(name_buffer, ',');
4330 if (eos)
4331 *eos = '\0';
4332 goto next;
4333 }
4334
4335next:
4336 add_command = strchr(add_command, ',');
4337 if (add_command)
4338 add_command++;
4339
4340 }
4341 if (msr_num == 0) {
4342 fprintf(stderr, "--add: (msrDDD | msr0xXXX) required\n");
4343 fail++;
4344 }
4345
4346 /* generate default column header */
4347 if (*name_buffer == '\0') {
4348 if (format == FORMAT_RAW) {
4349 if (width == 32)
4350 sprintf(name_buffer, "msr%d", msr_num);
4351 else
4352 sprintf(name_buffer, "MSR%d", msr_num);
4353 } else if (format == FORMAT_DELTA) {
4354 if (width == 32)
4355 sprintf(name_buffer, "cnt%d", msr_num);
4356 else
4357 sprintf(name_buffer, "CNT%d", msr_num);
4358 } else if (format == FORMAT_PERCENT) {
4359 if (width == 32)
4360 sprintf(name_buffer, "msr%d%%", msr_num);
4361 else
4362 sprintf(name_buffer, "MSR%d%%", msr_num);
4363 }
4364 }
4365
4366 if (add_counter(msr_num, name_buffer, width, scope, type, format))
4367 fail++;
4368
4369 if (fail) {
4370 help();
4371 exit(1);
4372 }
4373}
Len Brown812db3f2017-02-10 00:25:41 -05004374/*
4375 * HIDE_LIST - hide this list of counters, show the rest [default]
4376 * SHOW_LIST - show this list of counters, hide the rest
4377 */
4378enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST;
4379
4380int shown;
4381/*
4382 * parse_show_hide() - process cmdline to set default counter action
4383 */
4384void parse_show_hide(char *optarg, enum show_hide_mode new_mode)
4385{
4386 /*
4387 * --show: show only those specified
4388 * The 1st invocation will clear and replace the enabled mask
4389 * subsequent invocations can add to it.
4390 */
4391 if (new_mode == SHOW_LIST) {
4392 if (shown == 0)
4393 bic_enabled = bic_lookup(optarg);
4394 else
4395 bic_enabled |= bic_lookup(optarg);
4396 shown = 1;
4397
4398 return;
4399 }
4400
4401 /*
4402 * --hide: do not show those specified
4403 * multiple invocations simply clear more bits in enabled mask
4404 */
4405 bic_enabled &= ~bic_lookup(optarg);
4406}
4407
Len Brown103a8fe2010-10-22 23:53:03 -04004408void cmdline(int argc, char **argv)
4409{
4410 int opt;
Len Brownd8af6f52015-02-10 01:56:38 -05004411 int option_index = 0;
4412 static struct option long_options[] = {
Len Brown388e9c82016-12-22 23:57:55 -05004413 {"add", required_argument, 0, 'a'},
Len Brownd8af6f52015-02-10 01:56:38 -05004414 {"Dump", no_argument, 0, 'D'},
4415 {"debug", no_argument, 0, 'd'},
4416 {"interval", required_argument, 0, 'i'},
4417 {"help", no_argument, 0, 'h'},
Len Brown812db3f2017-02-10 00:25:41 -05004418 {"hide", required_argument, 0, 'H'}, // meh, -h taken by --help
Len Brownd8af6f52015-02-10 01:56:38 -05004419 {"Joules", no_argument, 0, 'J'},
Len Brownb7d8c142016-02-13 23:36:17 -05004420 {"out", required_argument, 0, 'o'},
Len Brownd8af6f52015-02-10 01:56:38 -05004421 {"Package", no_argument, 0, 'p'},
4422 {"processor", no_argument, 0, 'p'},
Len Brown812db3f2017-02-10 00:25:41 -05004423 {"show", required_argument, 0, 's'},
Len Brownd8af6f52015-02-10 01:56:38 -05004424 {"Summary", no_argument, 0, 'S'},
4425 {"TCC", required_argument, 0, 'T'},
4426 {"version", no_argument, 0, 'v' },
4427 {0, 0, 0, 0 }
4428 };
Len Brown103a8fe2010-10-22 23:53:03 -04004429
4430 progname = argv[0];
4431
Len Brownb7d8c142016-02-13 23:36:17 -05004432 while ((opt = getopt_long_only(argc, argv, "+C:c:Ddhi:JM:m:o:PpST:v",
Len Brownd8af6f52015-02-10 01:56:38 -05004433 long_options, &option_index)) != -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04004434 switch (opt) {
Len Brown388e9c82016-12-22 23:57:55 -05004435 case 'a':
4436 parse_add_command(optarg);
4437 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004438 case 'D':
4439 dump_only++;
Len Brown8e180f32012-09-22 01:25:08 -04004440 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004441 case 'd':
4442 debug++;
Len Brown2f32edf2012-09-21 23:45:46 -04004443 break;
Len Brown812db3f2017-02-10 00:25:41 -05004444 case 'H':
4445 parse_show_hide(optarg, HIDE_LIST);
4446 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004447 case 'h':
4448 default:
4449 help();
4450 exit(1);
4451 case 'i':
Len Brown2a0609c2016-02-12 22:44:48 -05004452 {
4453 double interval = strtod(optarg, NULL);
4454
4455 if (interval < 0.001) {
Len Brownb7d8c142016-02-13 23:36:17 -05004456 fprintf(outf, "interval %f seconds is too small\n",
Len Brown2a0609c2016-02-12 22:44:48 -05004457 interval);
4458 exit(2);
4459 }
4460
4461 interval_ts.tv_sec = interval;
4462 interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
4463 }
Len Brown889facb2012-11-08 00:48:57 -05004464 break;
Dirk Brandewie5c56be92013-12-16 10:23:41 -08004465 case 'J':
4466 rapl_joules++;
4467 break;
Len Brownb7d8c142016-02-13 23:36:17 -05004468 case 'o':
4469 outf = fopen_or_die(optarg, "w");
4470 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004471 case 'P':
4472 show_pkg_only++;
4473 break;
4474 case 'p':
4475 show_core_only++;
4476 break;
Len Brown812db3f2017-02-10 00:25:41 -05004477 case 's':
4478 parse_show_hide(optarg, SHOW_LIST);
4479 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004480 case 'S':
4481 summary_only++;
4482 break;
4483 case 'T':
4484 tcc_activation_temp_override = atoi(optarg);
4485 break;
4486 case 'v':
4487 print_version();
4488 exit(0);
4489 break;
Len Brown103a8fe2010-10-22 23:53:03 -04004490 }
4491 }
4492}
4493
4494int main(int argc, char **argv)
4495{
Len Brownb7d8c142016-02-13 23:36:17 -05004496 outf = stderr;
4497
Len Brown103a8fe2010-10-22 23:53:03 -04004498 cmdline(argc, argv);
4499
Len Brownd8af6f52015-02-10 01:56:38 -05004500 if (debug)
4501 print_version();
Len Brown103a8fe2010-10-22 23:53:03 -04004502
4503 turbostat_init();
4504
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004505 /* dump counters and exit */
4506 if (dump_only)
4507 return get_and_dump_counters();
4508
Len Brown103a8fe2010-10-22 23:53:03 -04004509 /*
4510 * if any params left, it must be a command to fork
4511 */
4512 if (argc - optind)
4513 return fork_it(argv + optind);
4514 else
4515 turbostat_loop();
4516
4517 return 0;
4518}