blob: f13f61b065c699f2364d8d033d3a4f3a36110533 [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_nhm_cstates;
56unsigned int do_snb_cstates;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -070057unsigned int do_knl_cstates;
Len Brownee7e38e2015-02-09 23:39:45 -050058unsigned int do_pc2;
59unsigned int do_pc3;
60unsigned int do_pc6;
61unsigned int do_pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -080062unsigned int do_c8_c9_c10;
Len Brown0b2bb692015-03-26 00:50:30 -040063unsigned int do_skl_residency;
Len Brown144b44b2013-11-09 00:30:16 -050064unsigned int do_slm_cstates;
65unsigned int use_c1_residency_msr;
Len Brown103a8fe2010-10-22 23:53:03 -040066unsigned int has_aperf;
Len Brown889facb2012-11-08 00:48:57 -050067unsigned int has_epb;
Len Brown5a634262016-04-06 17:15:55 -040068unsigned int do_irtl_snb;
69unsigned int do_irtl_hsw;
Len Brownfc04cc62014-02-06 00:55:19 -050070unsigned int units = 1000000; /* MHz etc */
Len Brown103a8fe2010-10-22 23:53:03 -040071unsigned int genuine_intel;
72unsigned int has_invariant_tsc;
Len Brownd7899442015-01-23 00:12:33 -050073unsigned int do_nhm_platform_info;
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +020074unsigned int aperf_mperf_multiplier = 1;
Len Brown562a2d32016-02-26 23:48:05 -050075int do_irq = 1;
Len Brown1ed51012013-02-10 17:19:24 -050076int do_smi;
Len Brown103a8fe2010-10-22 23:53:03 -040077double bclk;
Len Browna2b7b742015-09-26 00:12:38 -040078double base_hz;
Len Brown21ed5572015-10-19 22:37:40 -040079unsigned int has_base_hz;
Len Browna2b7b742015-09-26 00:12:38 -040080double tsc_tweak = 1.0;
Len Brown103a8fe2010-10-22 23:53:03 -040081unsigned int show_pkg;
82unsigned int show_core;
83unsigned int show_cpu;
Len Brownc98d5d92012-06-04 00:56:40 -040084unsigned int show_pkg_only;
85unsigned int show_core_only;
86char *output_buffer, *outp;
Len Brown889facb2012-11-08 00:48:57 -050087unsigned int do_rapl;
88unsigned int do_dts;
89unsigned int do_ptm;
Len Brownfdf676e2016-02-27 01:28:12 -050090unsigned int do_gfx_rc6_ms;
91unsigned long long gfx_cur_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -050092unsigned int do_gfx_mhz;
93unsigned int gfx_cur_mhz;
Len Brown889facb2012-11-08 00:48:57 -050094unsigned int tcc_activation_temp;
95unsigned int tcc_activation_temp_override;
Andrey Semin40ee8e32014-12-05 00:07:00 -050096double rapl_power_units, rapl_time_units;
97double rapl_dram_energy_units, rapl_energy_units;
Len Brown889facb2012-11-08 00:48:57 -050098double rapl_joule_counter_range;
Len Brown3a9a9412014-08-15 02:39:52 -040099unsigned int do_core_perf_limit_reasons;
100unsigned int do_gfx_perf_limit_reasons;
101unsigned int do_ring_perf_limit_reasons;
Len Brown8a5bdf42015-04-01 21:02:57 -0400102unsigned int crystal_hz;
103unsigned long long tsc_hz;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -0400104int base_cpu;
Len Brown21ed5572015-10-19 22:37:40 -0400105double discover_bclk(unsigned int family, unsigned int model);
Len Brown7f5c2582015-12-01 01:36:39 -0500106unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */
107 /* IA32_HWP_REQUEST, IA32_HWP_STATUS */
108unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */
109unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */
110unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */
111unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */
Len Brown889facb2012-11-08 00:48:57 -0500112
Len Browne6f9bb32013-12-03 02:19:19 -0500113#define RAPL_PKG (1 << 0)
114 /* 0x610 MSR_PKG_POWER_LIMIT */
115 /* 0x611 MSR_PKG_ENERGY_STATUS */
116#define RAPL_PKG_PERF_STATUS (1 << 1)
117 /* 0x613 MSR_PKG_PERF_STATUS */
118#define RAPL_PKG_POWER_INFO (1 << 2)
119 /* 0x614 MSR_PKG_POWER_INFO */
120
121#define RAPL_DRAM (1 << 3)
122 /* 0x618 MSR_DRAM_POWER_LIMIT */
123 /* 0x619 MSR_DRAM_ENERGY_STATUS */
Len Browne6f9bb32013-12-03 02:19:19 -0500124#define RAPL_DRAM_PERF_STATUS (1 << 4)
125 /* 0x61b MSR_DRAM_PERF_STATUS */
Len Brown0b2bb692015-03-26 00:50:30 -0400126#define RAPL_DRAM_POWER_INFO (1 << 5)
127 /* 0x61c MSR_DRAM_POWER_INFO */
Len Browne6f9bb32013-12-03 02:19:19 -0500128
Jacob Pan91484942016-06-16 09:48:20 -0700129#define RAPL_CORES_POWER_LIMIT (1 << 6)
Len Browne6f9bb32013-12-03 02:19:19 -0500130 /* 0x638 MSR_PP0_POWER_LIMIT */
Len Brown0b2bb692015-03-26 00:50:30 -0400131#define RAPL_CORE_POLICY (1 << 7)
Len Browne6f9bb32013-12-03 02:19:19 -0500132 /* 0x63a MSR_PP0_POLICY */
133
Len Brown0b2bb692015-03-26 00:50:30 -0400134#define RAPL_GFX (1 << 8)
Len Browne6f9bb32013-12-03 02:19:19 -0500135 /* 0x640 MSR_PP1_POWER_LIMIT */
136 /* 0x641 MSR_PP1_ENERGY_STATUS */
137 /* 0x642 MSR_PP1_POLICY */
Jacob Pan91484942016-06-16 09:48:20 -0700138
139#define RAPL_CORES_ENERGY_STATUS (1 << 9)
140 /* 0x639 MSR_PP0_ENERGY_STATUS */
141#define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT)
Len Brown889facb2012-11-08 00:48:57 -0500142#define TJMAX_DEFAULT 100
143
144#define MAX(a, b) ((a) > (b) ? (a) : (b))
Len Brown103a8fe2010-10-22 23:53:03 -0400145
Len Brown388e9c82016-12-22 23:57:55 -0500146/*
147 * buffer size used by sscanf() for added column names
148 * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters
149 */
150#define NAME_BYTES 20
151
Len Brown103a8fe2010-10-22 23:53:03 -0400152int backwards_count;
153char *progname;
Len Brown103a8fe2010-10-22 23:53:03 -0400154
Len Brownc98d5d92012-06-04 00:56:40 -0400155cpu_set_t *cpu_present_set, *cpu_affinity_set;
156size_t cpu_present_setsize, cpu_affinity_setsize;
Len Brown103a8fe2010-10-22 23:53:03 -0400157
Len Brownc98d5d92012-06-04 00:56:40 -0400158struct thread_data {
159 unsigned long long tsc;
160 unsigned long long aperf;
161 unsigned long long mperf;
Len Brown144b44b2013-11-09 00:30:16 -0500162 unsigned long long c1;
Len Brown562a2d32016-02-26 23:48:05 -0500163 unsigned int irq_count;
Len Brown1ed51012013-02-10 17:19:24 -0500164 unsigned int smi_count;
Len Brownc98d5d92012-06-04 00:56:40 -0400165 unsigned int cpu_id;
166 unsigned int flags;
167#define CPU_IS_FIRST_THREAD_IN_CORE 0x2
168#define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
Len Brown388e9c82016-12-22 23:57:55 -0500169 unsigned long long counter[1];
Len Brownc98d5d92012-06-04 00:56:40 -0400170} *thread_even, *thread_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400171
Len Brownc98d5d92012-06-04 00:56:40 -0400172struct core_data {
173 unsigned long long c3;
174 unsigned long long c6;
175 unsigned long long c7;
Len Brown889facb2012-11-08 00:48:57 -0500176 unsigned int core_temp_c;
Len Brownc98d5d92012-06-04 00:56:40 -0400177 unsigned int core_id;
Len Brown388e9c82016-12-22 23:57:55 -0500178 unsigned long long counter[1];
Len Brownc98d5d92012-06-04 00:56:40 -0400179} *core_even, *core_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400180
Len Brownc98d5d92012-06-04 00:56:40 -0400181struct pkg_data {
182 unsigned long long pc2;
183 unsigned long long pc3;
184 unsigned long long pc6;
185 unsigned long long pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800186 unsigned long long pc8;
187 unsigned long long pc9;
188 unsigned long long pc10;
Len Brown0b2bb692015-03-26 00:50:30 -0400189 unsigned long long pkg_wtd_core_c0;
190 unsigned long long pkg_any_core_c0;
191 unsigned long long pkg_any_gfxe_c0;
192 unsigned long long pkg_both_core_gfxe_c0;
Len Brown9185e982016-04-06 17:16:00 -0400193 long long gfx_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -0500194 unsigned int gfx_mhz;
Len Brownc98d5d92012-06-04 00:56:40 -0400195 unsigned int package_id;
Len Brown889facb2012-11-08 00:48:57 -0500196 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
197 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
198 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
199 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
200 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
201 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
202 unsigned int pkg_temp_c;
Len Brown388e9c82016-12-22 23:57:55 -0500203 unsigned long long counter[1];
Len Brownc98d5d92012-06-04 00:56:40 -0400204} *package_even, *package_odd;
205
206#define ODD_COUNTERS thread_odd, core_odd, package_odd
207#define EVEN_COUNTERS thread_even, core_even, package_even
208
209#define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
210 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
211 topo.num_threads_per_core + \
212 (core_no) * topo.num_threads_per_core + (thread_no))
213#define GET_CORE(core_base, core_no, pkg_no) \
214 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
215#define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
216
Len Brown388e9c82016-12-22 23:57:55 -0500217enum counter_scope {SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE};
218enum counter_type {COUNTER_CYCLES, COUNTER_SECONDS};
219enum counter_format {FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT};
220
221struct msr_counter {
222 unsigned int msr_num;
223 char name[NAME_BYTES];
224 unsigned int width;
225 enum counter_type type;
226 enum counter_format format;
227 struct msr_counter *next;
228};
229
230struct sys_counters {
231 unsigned int thread_counter_bytes;
232 unsigned int core_counter_bytes;
233 unsigned int package_counter_bytes;
234 struct msr_counter *tp;
235 struct msr_counter *cp;
236 struct msr_counter *pp;
237} sys;
238
Len Brownc98d5d92012-06-04 00:56:40 -0400239struct system_summary {
240 struct thread_data threads;
241 struct core_data cores;
242 struct pkg_data packages;
Len Brown388e9c82016-12-22 23:57:55 -0500243} average;
Len Brownc98d5d92012-06-04 00:56:40 -0400244
245
246struct topo_params {
247 int num_packages;
248 int num_cpus;
249 int num_cores;
250 int max_cpu_num;
251 int num_cores_per_pkg;
252 int num_threads_per_core;
253} topo;
254
255struct timeval tv_even, tv_odd, tv_delta;
256
Len Brown562a2d32016-02-26 23:48:05 -0500257int *irq_column_2_cpu; /* /proc/interrupts column numbers */
258int *irqs_per_cpu; /* indexed by cpu_num */
259
Len Brownc98d5d92012-06-04 00:56:40 -0400260void setup_all_buffers(void);
261
262int cpu_is_not_present(int cpu)
Len Brownd15cf7c2012-06-03 23:24:00 -0400263{
Len Brownc98d5d92012-06-04 00:56:40 -0400264 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brownd15cf7c2012-06-03 23:24:00 -0400265}
Len Brown88c32812012-03-29 21:44:40 -0400266/*
Len Brownc98d5d92012-06-04 00:56:40 -0400267 * run func(thread, core, package) in topology order
268 * skip non-present cpus
Len Brown88c32812012-03-29 21:44:40 -0400269 */
Len Brownd15cf7c2012-06-03 23:24:00 -0400270
Len Brownc98d5d92012-06-04 00:56:40 -0400271int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
272 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
Len Brown88c32812012-03-29 21:44:40 -0400273{
Len Brownc98d5d92012-06-04 00:56:40 -0400274 int retval, pkg_no, core_no, thread_no;
275
276 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
277 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
278 for (thread_no = 0; thread_no <
279 topo.num_threads_per_core; ++thread_no) {
280 struct thread_data *t;
281 struct core_data *c;
282 struct pkg_data *p;
283
284 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
285
286 if (cpu_is_not_present(t->cpu_id))
287 continue;
288
289 c = GET_CORE(core_base, core_no, pkg_no);
290 p = GET_PKG(pkg_base, pkg_no);
291
292 retval = func(t, c, p);
293 if (retval)
294 return retval;
295 }
296 }
297 }
298 return 0;
Len Brown88c32812012-03-29 21:44:40 -0400299}
300
301int cpu_migrate(int cpu)
302{
Len Brownc98d5d92012-06-04 00:56:40 -0400303 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
304 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
305 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
Len Brown88c32812012-03-29 21:44:40 -0400306 return -1;
307 else
308 return 0;
309}
Len Brown36229892016-02-26 20:51:02 -0500310int get_msr_fd(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -0400311{
Len Brown103a8fe2010-10-22 23:53:03 -0400312 char pathname[32];
313 int fd;
314
Len Brown36229892016-02-26 20:51:02 -0500315 fd = fd_percpu[cpu];
316
317 if (fd)
318 return fd;
319
Len Brown103a8fe2010-10-22 23:53:03 -0400320 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
321 fd = open(pathname, O_RDONLY);
Len Brown15aaa342012-03-29 22:19:58 -0400322 if (fd < 0)
Len Brown98481e72014-08-15 00:36:50 -0400323 err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname);
Len Brown103a8fe2010-10-22 23:53:03 -0400324
Len Brown36229892016-02-26 20:51:02 -0500325 fd_percpu[cpu] = fd;
326
327 return fd;
328}
329
330int get_msr(int cpu, off_t offset, unsigned long long *msr)
331{
332 ssize_t retval;
333
334 retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset);
Len Brown15aaa342012-03-29 22:19:58 -0400335
Len Brown98481e72014-08-15 00:36:50 -0400336 if (retval != sizeof *msr)
Len Brown36229892016-02-26 20:51:02 -0500337 err(-1, "msr %d offset 0x%llx read failed", cpu, (unsigned long long)offset);
Len Brown15aaa342012-03-29 22:19:58 -0400338
339 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400340}
341
Len Brownfc04cc62014-02-06 00:55:19 -0500342/*
343 * Example Format w/ field column widths:
344 *
Len Brown388e9c82016-12-22 23:57:55 -0500345 * Package Core CPU Avg_MHz Bzy_MHz TSC_MHz IRQ SMI Busy% CPU_%c1 CPU_%c3 CPU_%c6 CPU_%c7 ThreadC CoreTmp CoreCnt PkgTmp GFXMHz Pkg%pc2 Pkg%pc3 Pkg%pc6 Pkg%pc7 PkgWatt CorWatt GFXWatt PkgCnt
Len Brown562a2d32016-02-26 23:48:05 -0500346 * 12345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678
Len Brownfc04cc62014-02-06 00:55:19 -0500347 */
348
Len Browna829eb42011-02-10 23:36:34 -0500349void print_header(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400350{
Len Brown388e9c82016-12-22 23:57:55 -0500351 struct msr_counter *mp;
352
Len Brown103a8fe2010-10-22 23:53:03 -0400353 if (show_pkg)
Len Brown3d109de2016-04-22 23:24:27 -0400354 outp += sprintf(outp, "\tPackage");
Len Brown103a8fe2010-10-22 23:53:03 -0400355 if (show_core)
Len Brown3d109de2016-04-22 23:24:27 -0400356 outp += sprintf(outp, "\tCore");
Len Brown103a8fe2010-10-22 23:53:03 -0400357 if (show_cpu)
Len Brown3d109de2016-04-22 23:24:27 -0400358 outp += sprintf(outp, "\tCPU");
Len Brown103a8fe2010-10-22 23:53:03 -0400359 if (has_aperf)
Len Brown3d109de2016-04-22 23:24:27 -0400360 outp += sprintf(outp, "\tAvg_MHz");
Len Brownd7899442015-01-23 00:12:33 -0500361 if (has_aperf)
Len Brown3d109de2016-04-22 23:24:27 -0400362 outp += sprintf(outp, "\tBusy%%");
Len Brownfc04cc62014-02-06 00:55:19 -0500363 if (has_aperf)
Len Brown3d109de2016-04-22 23:24:27 -0400364 outp += sprintf(outp, "\tBzy_MHz");
365 outp += sprintf(outp, "\tTSC_MHz");
Len Brown1cc21f72015-02-23 00:34:57 -0500366
Len Brown1cc21f72015-02-23 00:34:57 -0500367 if (!debug)
368 goto done;
369
Len Brown562a2d32016-02-26 23:48:05 -0500370 if (do_irq)
Len Brown3d109de2016-04-22 23:24:27 -0400371 outp += sprintf(outp, "\tIRQ");
Len Brown1cc21f72015-02-23 00:34:57 -0500372 if (do_smi)
Len Brown3d109de2016-04-22 23:24:27 -0400373 outp += sprintf(outp, "\tSMI");
Len Brown1cc21f72015-02-23 00:34:57 -0500374
Len Brown103a8fe2010-10-22 23:53:03 -0400375 if (do_nhm_cstates)
Len Brown3d109de2016-04-22 23:24:27 -0400376 outp += sprintf(outp, "\tCPU%%c1");
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -0700377 if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates)
Len Brown3d109de2016-04-22 23:24:27 -0400378 outp += sprintf(outp, "\tCPU%%c3");
Len Brown103a8fe2010-10-22 23:53:03 -0400379 if (do_nhm_cstates)
Len Brown3d109de2016-04-22 23:24:27 -0400380 outp += sprintf(outp, "\tCPU%%c6");
Len Brown103a8fe2010-10-22 23:53:03 -0400381 if (do_snb_cstates)
Len Brown3d109de2016-04-22 23:24:27 -0400382 outp += sprintf(outp, "\tCPU%%c7");
Len Brown889facb2012-11-08 00:48:57 -0500383
Len Brown388e9c82016-12-22 23:57:55 -0500384 for (mp = sys.tp; mp; mp = mp->next) {
385 if (mp->format == FORMAT_RAW) {
386 if (mp->width == 64)
387 outp += sprintf(outp, "\t%18.18s", mp->name);
388 else
389 outp += sprintf(outp, "\t%10.10s", mp->name);
390 } else {
391 outp += sprintf(outp, "\t%-7.7s", mp->name);
392 }
393 }
394
Len Brown889facb2012-11-08 00:48:57 -0500395 if (do_dts)
Len Brown3d109de2016-04-22 23:24:27 -0400396 outp += sprintf(outp, "\tCoreTmp");
Len Brown388e9c82016-12-22 23:57:55 -0500397
398 for (mp = sys.cp; mp; mp = mp->next) {
399 if (mp->format == FORMAT_RAW) {
400 if (mp->width == 64)
401 outp += sprintf(outp, "\t%18.18s", mp->name);
402 else
403 outp += sprintf(outp, "\t%10.10s", mp->name);
404 } else {
405 outp += sprintf(outp, "\t%-7.7s", mp->name);
406 }
407 }
408
Len Brown889facb2012-11-08 00:48:57 -0500409 if (do_ptm)
Len Brown3d109de2016-04-22 23:24:27 -0400410 outp += sprintf(outp, "\tPkgTmp");
Len Brown889facb2012-11-08 00:48:57 -0500411
Len Brownfdf676e2016-02-27 01:28:12 -0500412 if (do_gfx_rc6_ms)
Len Brown3d109de2016-04-22 23:24:27 -0400413 outp += sprintf(outp, "\tGFX%%rc6");
Len Brownfdf676e2016-02-27 01:28:12 -0500414
Len Brown27d47352016-02-27 00:37:54 -0500415 if (do_gfx_mhz)
Len Brown3d109de2016-04-22 23:24:27 -0400416 outp += sprintf(outp, "\tGFXMHz");
Len Brown27d47352016-02-27 00:37:54 -0500417
Len Brown0b2bb692015-03-26 00:50:30 -0400418 if (do_skl_residency) {
Len Brown3d109de2016-04-22 23:24:27 -0400419 outp += sprintf(outp, "\tTotl%%C0");
420 outp += sprintf(outp, "\tAny%%C0");
421 outp += sprintf(outp, "\tGFX%%C0");
422 outp += sprintf(outp, "\tCPUGFX%%");
Len Brown0b2bb692015-03-26 00:50:30 -0400423 }
424
Len Brownee7e38e2015-02-09 23:39:45 -0500425 if (do_pc2)
Len Brown3d109de2016-04-22 23:24:27 -0400426 outp += sprintf(outp, "\tPkg%%pc2");
Len Brownee7e38e2015-02-09 23:39:45 -0500427 if (do_pc3)
Len Brown3d109de2016-04-22 23:24:27 -0400428 outp += sprintf(outp, "\tPkg%%pc3");
Len Brownee7e38e2015-02-09 23:39:45 -0500429 if (do_pc6)
Len Brown3d109de2016-04-22 23:24:27 -0400430 outp += sprintf(outp, "\tPkg%%pc6");
Len Brownee7e38e2015-02-09 23:39:45 -0500431 if (do_pc7)
Len Brown3d109de2016-04-22 23:24:27 -0400432 outp += sprintf(outp, "\tPkg%%pc7");
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800433 if (do_c8_c9_c10) {
Len Brown3d109de2016-04-22 23:24:27 -0400434 outp += sprintf(outp, "\tPkg%%pc8");
435 outp += sprintf(outp, "\tPkg%%pc9");
436 outp += sprintf(outp, "\tPk%%pc10");
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800437 }
Len Brown103a8fe2010-10-22 23:53:03 -0400438
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800439 if (do_rapl && !rapl_joules) {
440 if (do_rapl & RAPL_PKG)
Len Brown3d109de2016-04-22 23:24:27 -0400441 outp += sprintf(outp, "\tPkgWatt");
Jacob Pan91484942016-06-16 09:48:20 -0700442 if (do_rapl & RAPL_CORES_ENERGY_STATUS)
Len Brown3d109de2016-04-22 23:24:27 -0400443 outp += sprintf(outp, "\tCorWatt");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800444 if (do_rapl & RAPL_GFX)
Len Brown3d109de2016-04-22 23:24:27 -0400445 outp += sprintf(outp, "\tGFXWatt");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800446 if (do_rapl & RAPL_DRAM)
Len Brown3d109de2016-04-22 23:24:27 -0400447 outp += sprintf(outp, "\tRAMWatt");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800448 if (do_rapl & RAPL_PKG_PERF_STATUS)
Len Brown3d109de2016-04-22 23:24:27 -0400449 outp += sprintf(outp, "\tPKG_%%");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800450 if (do_rapl & RAPL_DRAM_PERF_STATUS)
Len Brown3d109de2016-04-22 23:24:27 -0400451 outp += sprintf(outp, "\tRAM_%%");
Len Brownd7899442015-01-23 00:12:33 -0500452 } else if (do_rapl && rapl_joules) {
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800453 if (do_rapl & RAPL_PKG)
Len Brown3d109de2016-04-22 23:24:27 -0400454 outp += sprintf(outp, "\tPkg_J");
Jacob Pan91484942016-06-16 09:48:20 -0700455 if (do_rapl & RAPL_CORES_ENERGY_STATUS)
Len Brown3d109de2016-04-22 23:24:27 -0400456 outp += sprintf(outp, "\tCor_J");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800457 if (do_rapl & RAPL_GFX)
Len Brown3d109de2016-04-22 23:24:27 -0400458 outp += sprintf(outp, "\tGFX_J");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800459 if (do_rapl & RAPL_DRAM)
Len Brown3d109de2016-04-22 23:24:27 -0400460 outp += sprintf(outp, "\tRAM_J");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800461 if (do_rapl & RAPL_PKG_PERF_STATUS)
Len Brown3d109de2016-04-22 23:24:27 -0400462 outp += sprintf(outp, "\tPKG_%%");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800463 if (do_rapl & RAPL_DRAM_PERF_STATUS)
Len Brown3d109de2016-04-22 23:24:27 -0400464 outp += sprintf(outp, "\tRAM_%%");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800465 }
Len Brown388e9c82016-12-22 23:57:55 -0500466 for (mp = sys.pp; mp; mp = mp->next) {
467 if (mp->format == FORMAT_RAW) {
468 if (mp->width == 64)
469 outp += sprintf(outp, "\t%18.18s", mp->name);
470 else
471 outp += sprintf(outp, "\t%10.10s", mp->name);
472 } else {
473 outp += sprintf(outp, "\t%-7.7s", mp->name);
474 }
475 }
476
477done:
Len Brownc98d5d92012-06-04 00:56:40 -0400478 outp += sprintf(outp, "\n");
Len Brown103a8fe2010-10-22 23:53:03 -0400479}
480
Len Brownc98d5d92012-06-04 00:56:40 -0400481int dump_counters(struct thread_data *t, struct core_data *c,
482 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400483{
Len Brown388e9c82016-12-22 23:57:55 -0500484 int i;
485 struct msr_counter *mp;
486
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200487 outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400488
Len Brownc98d5d92012-06-04 00:56:40 -0400489 if (t) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200490 outp += sprintf(outp, "CPU: %d flags 0x%x\n",
491 t->cpu_id, t->flags);
492 outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
493 outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
494 outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
495 outp += sprintf(outp, "c1: %016llX\n", t->c1);
Len Brown6886fee2016-12-24 15:18:37 -0500496
Len Brown562a2d32016-02-26 23:48:05 -0500497 if (do_irq)
498 outp += sprintf(outp, "IRQ: %08X\n", t->irq_count);
Len Brown1ed51012013-02-10 17:19:24 -0500499 if (do_smi)
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200500 outp += sprintf(outp, "SMI: %08X\n", t->smi_count);
Len Brown388e9c82016-12-22 23:57:55 -0500501
502 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
503 outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n",
504 i, mp->msr_num, t->counter[i]);
505 }
Len Brownc98d5d92012-06-04 00:56:40 -0400506 }
Len Brown103a8fe2010-10-22 23:53:03 -0400507
Len Brownc98d5d92012-06-04 00:56:40 -0400508 if (c) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200509 outp += sprintf(outp, "core: %d\n", c->core_id);
510 outp += sprintf(outp, "c3: %016llX\n", c->c3);
511 outp += sprintf(outp, "c6: %016llX\n", c->c6);
512 outp += sprintf(outp, "c7: %016llX\n", c->c7);
513 outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
Len Brown388e9c82016-12-22 23:57:55 -0500514
515 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
516 outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n",
517 i, mp->msr_num, c->counter[i]);
518 }
Len Brownc98d5d92012-06-04 00:56:40 -0400519 }
520
521 if (p) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200522 outp += sprintf(outp, "package: %d\n", p->package_id);
Len Brown0b2bb692015-03-26 00:50:30 -0400523
524 outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
525 outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
526 outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
527 outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
528
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200529 outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
Len Brownee7e38e2015-02-09 23:39:45 -0500530 if (do_pc3)
531 outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
532 if (do_pc6)
533 outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
534 if (do_pc7)
535 outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200536 outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
537 outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
538 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
539 outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
540 outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
541 outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
542 outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
543 outp += sprintf(outp, "Throttle PKG: %0X\n",
544 p->rapl_pkg_perf_status);
545 outp += sprintf(outp, "Throttle RAM: %0X\n",
546 p->rapl_dram_perf_status);
547 outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
Len Brown388e9c82016-12-22 23:57:55 -0500548
549 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
550 outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n",
551 i, mp->msr_num, p->counter[i]);
552 }
Len Brownc98d5d92012-06-04 00:56:40 -0400553 }
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200554
555 outp += sprintf(outp, "\n");
556
Len Brownc98d5d92012-06-04 00:56:40 -0400557 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400558}
559
Len Browne23da032012-02-06 18:37:16 -0500560/*
561 * column formatting convention & formats
Len Browne23da032012-02-06 18:37:16 -0500562 */
Len Brownc98d5d92012-06-04 00:56:40 -0400563int format_counters(struct thread_data *t, struct core_data *c,
564 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400565{
566 double interval_float;
Len Brownfc04cc62014-02-06 00:55:19 -0500567 char *fmt8;
Len Brown388e9c82016-12-22 23:57:55 -0500568 int i;
569 struct msr_counter *mp;
Len Brown103a8fe2010-10-22 23:53:03 -0400570
Len Brownc98d5d92012-06-04 00:56:40 -0400571 /* if showing only 1st thread in core and this isn't one, bail out */
572 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
573 return 0;
574
575 /* if showing only 1st thread in pkg and this isn't one, bail out */
576 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
577 return 0;
578
Len Brown103a8fe2010-10-22 23:53:03 -0400579 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
580
Len Brownc98d5d92012-06-04 00:56:40 -0400581 /* topo columns, print blanks on 1st (average) line */
582 if (t == &average.threads) {
Len Brown103a8fe2010-10-22 23:53:03 -0400583 if (show_pkg)
Len Brown3d109de2016-04-22 23:24:27 -0400584 outp += sprintf(outp, "\t-");
Len Brown103a8fe2010-10-22 23:53:03 -0400585 if (show_core)
Len Brown3d109de2016-04-22 23:24:27 -0400586 outp += sprintf(outp, "\t-");
Len Brown103a8fe2010-10-22 23:53:03 -0400587 if (show_cpu)
Len Brown3d109de2016-04-22 23:24:27 -0400588 outp += sprintf(outp, "\t-");
Len Brown103a8fe2010-10-22 23:53:03 -0400589 } else {
Len Brownc98d5d92012-06-04 00:56:40 -0400590 if (show_pkg) {
591 if (p)
Len Brown3d109de2016-04-22 23:24:27 -0400592 outp += sprintf(outp, "\t%d", p->package_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400593 else
Len Brown3d109de2016-04-22 23:24:27 -0400594 outp += sprintf(outp, "\t-");
Len Brownc98d5d92012-06-04 00:56:40 -0400595 }
Len Brownc98d5d92012-06-04 00:56:40 -0400596 if (show_core) {
597 if (c)
Len Brown3d109de2016-04-22 23:24:27 -0400598 outp += sprintf(outp, "\t%d", c->core_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400599 else
Len Brown3d109de2016-04-22 23:24:27 -0400600 outp += sprintf(outp, "\t-");
Len Brownc98d5d92012-06-04 00:56:40 -0400601 }
Len Brown103a8fe2010-10-22 23:53:03 -0400602 if (show_cpu)
Len Brown3d109de2016-04-22 23:24:27 -0400603 outp += sprintf(outp, "\t%d", t->cpu_id);
Len Brown103a8fe2010-10-22 23:53:03 -0400604 }
Len Brownfc04cc62014-02-06 00:55:19 -0500605
Len Brownd7899442015-01-23 00:12:33 -0500606 /* Avg_MHz */
Len Brownfc04cc62014-02-06 00:55:19 -0500607 if (has_aperf)
Len Brown3d109de2016-04-22 23:24:27 -0400608 outp += sprintf(outp, "\t%.0f",
Len Brownfc04cc62014-02-06 00:55:19 -0500609 1.0 / units * t->aperf / interval_float);
610
Len Brown75d2e442016-02-13 23:41:53 -0500611 /* Busy% */
Len Brownba3dec92016-04-22 20:31:46 -0400612 if (has_aperf)
Len Brown3d109de2016-04-22 23:24:27 -0400613 outp += sprintf(outp, "\t%.2f", 100.0 * t->mperf/t->tsc/tsc_tweak);
Len Brown103a8fe2010-10-22 23:53:03 -0400614
Len Brownd7899442015-01-23 00:12:33 -0500615 /* Bzy_MHz */
Len Brown21ed5572015-10-19 22:37:40 -0400616 if (has_aperf) {
617 if (has_base_hz)
Len Brown3d109de2016-04-22 23:24:27 -0400618 outp += sprintf(outp, "\t%.0f", base_hz / units * t->aperf / t->mperf);
Len Brown21ed5572015-10-19 22:37:40 -0400619 else
Len Brown3d109de2016-04-22 23:24:27 -0400620 outp += sprintf(outp, "\t%.0f",
Len Brown21ed5572015-10-19 22:37:40 -0400621 1.0 * t->tsc / units * t->aperf / t->mperf / interval_float);
622 }
Len Brown103a8fe2010-10-22 23:53:03 -0400623
Len Brownd7899442015-01-23 00:12:33 -0500624 /* TSC_MHz */
Len Brown3d109de2016-04-22 23:24:27 -0400625 outp += sprintf(outp, "\t%.0f", 1.0 * t->tsc/units/interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400626
Len Brown1cc21f72015-02-23 00:34:57 -0500627 if (!debug)
628 goto done;
629
Len Brown562a2d32016-02-26 23:48:05 -0500630 /* IRQ */
631 if (do_irq)
Len Brown3d109de2016-04-22 23:24:27 -0400632 outp += sprintf(outp, "\t%d", t->irq_count);
Len Brown562a2d32016-02-26 23:48:05 -0500633
Len Brown1cc21f72015-02-23 00:34:57 -0500634 /* SMI */
635 if (do_smi)
Len Brown3d109de2016-04-22 23:24:27 -0400636 outp += sprintf(outp, "\t%d", t->smi_count);
Len Brown1cc21f72015-02-23 00:34:57 -0500637
Len Brownba3dec92016-04-22 20:31:46 -0400638 if (do_nhm_cstates)
Len Brown3d109de2016-04-22 23:24:27 -0400639 outp += sprintf(outp, "\t%.2f", 100.0 * t->c1/t->tsc);
Len Brownc98d5d92012-06-04 00:56:40 -0400640
641 /* print per-core data only for 1st thread in core */
642 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
643 goto done;
644
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -0700645 if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates)
Len Brown3d109de2016-04-22 23:24:27 -0400646 outp += sprintf(outp, "\t%.2f", 100.0 * c->c3/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400647 if (do_nhm_cstates)
Len Brown3d109de2016-04-22 23:24:27 -0400648 outp += sprintf(outp, "\t%.2f", 100.0 * c->c6/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400649 if (do_snb_cstates)
Len Brown3d109de2016-04-22 23:24:27 -0400650 outp += sprintf(outp, "\t%.2f", 100.0 * c->c7/t->tsc);
Len Brownc98d5d92012-06-04 00:56:40 -0400651
Len Brown388e9c82016-12-22 23:57:55 -0500652 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
653 if (mp->format == FORMAT_RAW) {
654 if (mp->width == 32)
655 outp += sprintf(outp, "\t0x%08lx", (unsigned long) t->counter[i]);
656 else
657 outp += sprintf(outp, "\t0x%016llx", t->counter[i]);
658 } else if (mp->format == FORMAT_DELTA) {
659 outp += sprintf(outp, "\t%8lld", t->counter[i]);
660 } else if (mp->format == FORMAT_PERCENT) {
661 outp += sprintf(outp, "\t%.2f", 100.0 * t->counter[i]/t->tsc);
662 }
663 }
664
665
Len Brown889facb2012-11-08 00:48:57 -0500666 if (do_dts)
Len Brown3d109de2016-04-22 23:24:27 -0400667 outp += sprintf(outp, "\t%d", c->core_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500668
Len Brown388e9c82016-12-22 23:57:55 -0500669 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
670 if (mp->format == FORMAT_RAW) {
671 if (mp->width == 32)
672 outp += sprintf(outp, "\t0x%08lx", (unsigned long) c->counter[i]);
673 else
674 outp += sprintf(outp, "\t0x%016llx", c->counter[i]);
675 } else if (mp->format == FORMAT_DELTA) {
676 outp += sprintf(outp, "\t%8lld", c->counter[i]);
677 } else if (mp->format == FORMAT_PERCENT) {
678 outp += sprintf(outp, "\t%.2f", 100.0 * c->counter[i]/t->tsc);
679 }
680 }
681
Len Brownc98d5d92012-06-04 00:56:40 -0400682 /* print per-package data only for 1st core in package */
683 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
684 goto done;
685
Len Brown0b2bb692015-03-26 00:50:30 -0400686 /* PkgTmp */
Len Brown889facb2012-11-08 00:48:57 -0500687 if (do_ptm)
Len Brown3d109de2016-04-22 23:24:27 -0400688 outp += sprintf(outp, "\t%d", p->pkg_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500689
Len Brownfdf676e2016-02-27 01:28:12 -0500690 /* GFXrc6 */
Len Brown9185e982016-04-06 17:16:00 -0400691 if (do_gfx_rc6_ms) {
Len Brownba3dec92016-04-22 20:31:46 -0400692 if (p->gfx_rc6_ms == -1) { /* detect GFX counter reset */
Len Brown3d109de2016-04-22 23:24:27 -0400693 outp += sprintf(outp, "\t**.**");
Len Brown9185e982016-04-06 17:16:00 -0400694 } else {
Len Brown3d109de2016-04-22 23:24:27 -0400695 outp += sprintf(outp, "\t%.2f",
Len Brown9185e982016-04-06 17:16:00 -0400696 p->gfx_rc6_ms / 10.0 / interval_float);
697 }
698 }
Len Brownfdf676e2016-02-27 01:28:12 -0500699
Len Brown27d47352016-02-27 00:37:54 -0500700 /* GFXMHz */
701 if (do_gfx_mhz)
Len Brown3d109de2016-04-22 23:24:27 -0400702 outp += sprintf(outp, "\t%d", p->gfx_mhz);
Len Brown27d47352016-02-27 00:37:54 -0500703
Len Brown0b2bb692015-03-26 00:50:30 -0400704 /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
705 if (do_skl_residency) {
Len Brown3d109de2016-04-22 23:24:27 -0400706 outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_wtd_core_c0/t->tsc);
707 outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_any_core_c0/t->tsc);
708 outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_any_gfxe_c0/t->tsc);
709 outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_both_core_gfxe_c0/t->tsc);
Len Brown0b2bb692015-03-26 00:50:30 -0400710 }
711
Len Brownee7e38e2015-02-09 23:39:45 -0500712 if (do_pc2)
Len Brown3d109de2016-04-22 23:24:27 -0400713 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc2/t->tsc);
Len Brownee7e38e2015-02-09 23:39:45 -0500714 if (do_pc3)
Len Brown3d109de2016-04-22 23:24:27 -0400715 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc3/t->tsc);
Len Brownee7e38e2015-02-09 23:39:45 -0500716 if (do_pc6)
Len Brown3d109de2016-04-22 23:24:27 -0400717 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc6/t->tsc);
Len Brownee7e38e2015-02-09 23:39:45 -0500718 if (do_pc7)
Len Brown3d109de2016-04-22 23:24:27 -0400719 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc7/t->tsc);
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800720 if (do_c8_c9_c10) {
Len Brown3d109de2016-04-22 23:24:27 -0400721 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc8/t->tsc);
722 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc9/t->tsc);
723 outp += sprintf(outp, "\t%.2f", 100.0 * p->pc10/t->tsc);
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800724 }
Len Brown889facb2012-11-08 00:48:57 -0500725
726 /*
727 * If measurement interval exceeds minimum RAPL Joule Counter range,
728 * indicate that results are suspect by printing "**" in fraction place.
729 */
Len Brownfc04cc62014-02-06 00:55:19 -0500730 if (interval_float < rapl_joule_counter_range)
Len Brown3d109de2016-04-22 23:24:27 -0400731 fmt8 = "\t%.2f";
Len Brownfc04cc62014-02-06 00:55:19 -0500732 else
Len Browne975db52016-04-06 23:56:02 -0400733 fmt8 = "%6.0f**";
Len Brown889facb2012-11-08 00:48:57 -0500734
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800735 if (do_rapl && !rapl_joules) {
736 if (do_rapl & RAPL_PKG)
Len Brownfc04cc62014-02-06 00:55:19 -0500737 outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units / interval_float);
Jacob Pan91484942016-06-16 09:48:20 -0700738 if (do_rapl & RAPL_CORES_ENERGY_STATUS)
Len Brownfc04cc62014-02-06 00:55:19 -0500739 outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800740 if (do_rapl & RAPL_GFX)
Len Brownfc04cc62014-02-06 00:55:19 -0500741 outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800742 if (do_rapl & RAPL_DRAM)
Andrey Semin40ee8e32014-12-05 00:07:00 -0500743 outp += sprintf(outp, fmt8, p->energy_dram * rapl_dram_energy_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800744 if (do_rapl & RAPL_PKG_PERF_STATUS)
Len Brownfc04cc62014-02-06 00:55:19 -0500745 outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800746 if (do_rapl & RAPL_DRAM_PERF_STATUS)
Len Brownfc04cc62014-02-06 00:55:19 -0500747 outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
Len Brownd7899442015-01-23 00:12:33 -0500748 } else if (do_rapl && rapl_joules) {
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800749 if (do_rapl & RAPL_PKG)
Len Brownfc04cc62014-02-06 00:55:19 -0500750 outp += sprintf(outp, fmt8,
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800751 p->energy_pkg * rapl_energy_units);
752 if (do_rapl & RAPL_CORES)
Len Brownfc04cc62014-02-06 00:55:19 -0500753 outp += sprintf(outp, fmt8,
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800754 p->energy_cores * rapl_energy_units);
755 if (do_rapl & RAPL_GFX)
Len Brownfc04cc62014-02-06 00:55:19 -0500756 outp += sprintf(outp, fmt8,
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800757 p->energy_gfx * rapl_energy_units);
758 if (do_rapl & RAPL_DRAM)
Len Brownfc04cc62014-02-06 00:55:19 -0500759 outp += sprintf(outp, fmt8,
Andrey Semin40ee8e32014-12-05 00:07:00 -0500760 p->energy_dram * rapl_dram_energy_units);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800761 if (do_rapl & RAPL_PKG_PERF_STATUS)
Len Brownfc04cc62014-02-06 00:55:19 -0500762 outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800763 if (do_rapl & RAPL_DRAM_PERF_STATUS)
Len Brownfc04cc62014-02-06 00:55:19 -0500764 outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800765 }
Len Brown388e9c82016-12-22 23:57:55 -0500766 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
767 if (mp->format == FORMAT_RAW) {
768 if (mp->width == 32)
769 outp += sprintf(outp, "\t0x%08lx", (unsigned long) p->counter[i]);
770 else
771 outp += sprintf(outp, "\t0x%016llx", p->counter[i]);
772 } else if (mp->format == FORMAT_DELTA) {
773 outp += sprintf(outp, "\t%8lld", p->counter[i]);
774 } else if (mp->format == FORMAT_PERCENT) {
775 outp += sprintf(outp, "\t%.2f", 100.0 * p->counter[i]/t->tsc);
776 }
777 }
778
Len Brownc98d5d92012-06-04 00:56:40 -0400779done:
Len Brownc98d5d92012-06-04 00:56:40 -0400780 outp += sprintf(outp, "\n");
781
782 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400783}
784
Len Brownb7d8c142016-02-13 23:36:17 -0500785void flush_output_stdout(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400786{
Len Brownb7d8c142016-02-13 23:36:17 -0500787 FILE *filep;
788
789 if (outf == stderr)
790 filep = stdout;
791 else
792 filep = outf;
793
794 fputs(output_buffer, filep);
795 fflush(filep);
796
Len Brownc98d5d92012-06-04 00:56:40 -0400797 outp = output_buffer;
798}
Len Brownb7d8c142016-02-13 23:36:17 -0500799void flush_output_stderr(void)
Len Brownc98d5d92012-06-04 00:56:40 -0400800{
Len Brownb7d8c142016-02-13 23:36:17 -0500801 fputs(output_buffer, outf);
802 fflush(outf);
Len Brownc98d5d92012-06-04 00:56:40 -0400803 outp = output_buffer;
804}
805void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
806{
Len Browne23da032012-02-06 18:37:16 -0500807 static int printed;
Len Brown103a8fe2010-10-22 23:53:03 -0400808
Len Browne23da032012-02-06 18:37:16 -0500809 if (!printed || !summary_only)
810 print_header();
Len Brown103a8fe2010-10-22 23:53:03 -0400811
Len Brownc98d5d92012-06-04 00:56:40 -0400812 if (topo.num_cpus > 1)
813 format_counters(&average.threads, &average.cores,
814 &average.packages);
Len Brown103a8fe2010-10-22 23:53:03 -0400815
Len Browne23da032012-02-06 18:37:16 -0500816 printed = 1;
817
818 if (summary_only)
819 return;
820
Len Brownc98d5d92012-06-04 00:56:40 -0400821 for_all_cpus(format_counters, t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400822}
823
Len Brown889facb2012-11-08 00:48:57 -0500824#define DELTA_WRAP32(new, old) \
825 if (new > old) { \
826 old = new - old; \
827 } else { \
828 old = 0x100000000 + new - old; \
829 }
830
Len Brownba3dec92016-04-22 20:31:46 -0400831int
Len Brownc98d5d92012-06-04 00:56:40 -0400832delta_package(struct pkg_data *new, struct pkg_data *old)
Len Brown103a8fe2010-10-22 23:53:03 -0400833{
Len Brown388e9c82016-12-22 23:57:55 -0500834 int i;
835 struct msr_counter *mp;
Len Brown0b2bb692015-03-26 00:50:30 -0400836
837 if (do_skl_residency) {
838 old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
839 old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
840 old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
841 old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0;
842 }
Len Brownc98d5d92012-06-04 00:56:40 -0400843 old->pc2 = new->pc2 - old->pc2;
Len Brownee7e38e2015-02-09 23:39:45 -0500844 if (do_pc3)
845 old->pc3 = new->pc3 - old->pc3;
846 if (do_pc6)
847 old->pc6 = new->pc6 - old->pc6;
848 if (do_pc7)
849 old->pc7 = new->pc7 - old->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800850 old->pc8 = new->pc8 - old->pc8;
851 old->pc9 = new->pc9 - old->pc9;
852 old->pc10 = new->pc10 - old->pc10;
Len Brown889facb2012-11-08 00:48:57 -0500853 old->pkg_temp_c = new->pkg_temp_c;
854
Len Brown9185e982016-04-06 17:16:00 -0400855 /* flag an error when rc6 counter resets/wraps */
856 if (old->gfx_rc6_ms > new->gfx_rc6_ms)
857 old->gfx_rc6_ms = -1;
858 else
859 old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms;
860
Len Brown27d47352016-02-27 00:37:54 -0500861 old->gfx_mhz = new->gfx_mhz;
862
Len Brown889facb2012-11-08 00:48:57 -0500863 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
864 DELTA_WRAP32(new->energy_cores, old->energy_cores);
865 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
866 DELTA_WRAP32(new->energy_dram, old->energy_dram);
867 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
868 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
Len Brownba3dec92016-04-22 20:31:46 -0400869
Len Brown388e9c82016-12-22 23:57:55 -0500870 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
871 if (mp->format == FORMAT_RAW)
872 old->counter[i] = new->counter[i];
873 else
874 old->counter[i] = new->counter[i] - old->counter[i];
875 }
876
Len Brownba3dec92016-04-22 20:31:46 -0400877 return 0;
Len Brownc98d5d92012-06-04 00:56:40 -0400878}
Len Brown103a8fe2010-10-22 23:53:03 -0400879
Len Brownc98d5d92012-06-04 00:56:40 -0400880void
881delta_core(struct core_data *new, struct core_data *old)
882{
Len Brown388e9c82016-12-22 23:57:55 -0500883 int i;
884 struct msr_counter *mp;
885
Len Brownc98d5d92012-06-04 00:56:40 -0400886 old->c3 = new->c3 - old->c3;
887 old->c6 = new->c6 - old->c6;
888 old->c7 = new->c7 - old->c7;
Len Brown889facb2012-11-08 00:48:57 -0500889 old->core_temp_c = new->core_temp_c;
Len Brown388e9c82016-12-22 23:57:55 -0500890
891 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
892 if (mp->format == FORMAT_RAW)
893 old->counter[i] = new->counter[i];
894 else
895 old->counter[i] = new->counter[i] - old->counter[i];
896 }
Len Brownc98d5d92012-06-04 00:56:40 -0400897}
Len Brown103a8fe2010-10-22 23:53:03 -0400898
Len Brownc3ae3312012-06-13 21:31:46 -0400899/*
900 * old = new - old
901 */
Len Brownba3dec92016-04-22 20:31:46 -0400902int
Len Brownc98d5d92012-06-04 00:56:40 -0400903delta_thread(struct thread_data *new, struct thread_data *old,
904 struct core_data *core_delta)
905{
Len Brown388e9c82016-12-22 23:57:55 -0500906 int i;
907 struct msr_counter *mp;
908
Len Brownc98d5d92012-06-04 00:56:40 -0400909 old->tsc = new->tsc - old->tsc;
Len Brown103a8fe2010-10-22 23:53:03 -0400910
Len Brownc98d5d92012-06-04 00:56:40 -0400911 /* check for TSC < 1 Mcycles over interval */
Josh Triplettb2c95d92013-08-20 17:20:18 -0700912 if (old->tsc < (1000 * 1000))
913 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
914 "You can disable all c-states by booting with \"idle=poll\"\n"
915 "or just the deep ones with \"processor.max_cstate=1\"");
Len Brown103a8fe2010-10-22 23:53:03 -0400916
Len Brownc98d5d92012-06-04 00:56:40 -0400917 old->c1 = new->c1 - old->c1;
Len Brown103a8fe2010-10-22 23:53:03 -0400918
Len Browna7296172015-01-23 01:33:58 -0500919 if (has_aperf) {
920 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
921 old->aperf = new->aperf - old->aperf;
922 old->mperf = new->mperf - old->mperf;
923 } else {
Len Brownba3dec92016-04-22 20:31:46 -0400924 return -1;
Len Brownc98d5d92012-06-04 00:56:40 -0400925 }
Len Brownc98d5d92012-06-04 00:56:40 -0400926 }
Len Brown103a8fe2010-10-22 23:53:03 -0400927
Len Brown103a8fe2010-10-22 23:53:03 -0400928
Len Brown144b44b2013-11-09 00:30:16 -0500929 if (use_c1_residency_msr) {
930 /*
931 * Some models have a dedicated C1 residency MSR,
932 * which should be more accurate than the derivation below.
933 */
934 } else {
935 /*
936 * As counter collection is not atomic,
937 * it is possible for mperf's non-halted cycles + idle states
938 * to exceed TSC's all cycles: show c1 = 0% in that case.
939 */
940 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
941 old->c1 = 0;
942 else {
943 /* normal case, derive c1 */
944 old->c1 = old->tsc - old->mperf - core_delta->c3
Len Brownc98d5d92012-06-04 00:56:40 -0400945 - core_delta->c6 - core_delta->c7;
Len Brown144b44b2013-11-09 00:30:16 -0500946 }
Len Brownc98d5d92012-06-04 00:56:40 -0400947 }
Len Brownc3ae3312012-06-13 21:31:46 -0400948
Len Brownc98d5d92012-06-04 00:56:40 -0400949 if (old->mperf == 0) {
Len Brownb7d8c142016-02-13 23:36:17 -0500950 if (debug > 1)
951 fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400952 old->mperf = 1; /* divide by 0 protection */
953 }
954
Len Brown562a2d32016-02-26 23:48:05 -0500955 if (do_irq)
956 old->irq_count = new->irq_count - old->irq_count;
957
Len Brown1ed51012013-02-10 17:19:24 -0500958 if (do_smi)
959 old->smi_count = new->smi_count - old->smi_count;
Len Brownba3dec92016-04-22 20:31:46 -0400960
Len Brown388e9c82016-12-22 23:57:55 -0500961 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
962 if (mp->format == FORMAT_RAW)
963 old->counter[i] = new->counter[i];
964 else
965 old->counter[i] = new->counter[i] - old->counter[i];
966 }
Len Brownba3dec92016-04-22 20:31:46 -0400967 return 0;
Len Brownc98d5d92012-06-04 00:56:40 -0400968}
969
970int delta_cpu(struct thread_data *t, struct core_data *c,
971 struct pkg_data *p, struct thread_data *t2,
972 struct core_data *c2, struct pkg_data *p2)
973{
Len Brownba3dec92016-04-22 20:31:46 -0400974 int retval = 0;
975
Len Brownc98d5d92012-06-04 00:56:40 -0400976 /* calculate core delta only for 1st thread in core */
977 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
978 delta_core(c, c2);
979
980 /* always calculate thread delta */
Len Brownba3dec92016-04-22 20:31:46 -0400981 retval = delta_thread(t, t2, c2); /* c2 is core delta */
982 if (retval)
983 return retval;
Len Brownc98d5d92012-06-04 00:56:40 -0400984
985 /* calculate package delta only for 1st core in package */
986 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
Len Brownba3dec92016-04-22 20:31:46 -0400987 retval = delta_package(p, p2);
Len Brownc98d5d92012-06-04 00:56:40 -0400988
Len Brownba3dec92016-04-22 20:31:46 -0400989 return retval;
Len Brownc98d5d92012-06-04 00:56:40 -0400990}
991
992void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
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 t->tsc = 0;
998 t->aperf = 0;
999 t->mperf = 0;
1000 t->c1 = 0;
1001
Len Brown562a2d32016-02-26 23:48:05 -05001002 t->irq_count = 0;
1003 t->smi_count = 0;
1004
Len Brownc98d5d92012-06-04 00:56:40 -04001005 /* tells format_counters to dump all fields from this set */
1006 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
1007
1008 c->c3 = 0;
1009 c->c6 = 0;
1010 c->c7 = 0;
Len Brown889facb2012-11-08 00:48:57 -05001011 c->core_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001012
Len Brown0b2bb692015-03-26 00:50:30 -04001013 p->pkg_wtd_core_c0 = 0;
1014 p->pkg_any_core_c0 = 0;
1015 p->pkg_any_gfxe_c0 = 0;
1016 p->pkg_both_core_gfxe_c0 = 0;
1017
Len Brownc98d5d92012-06-04 00:56:40 -04001018 p->pc2 = 0;
Len Brownee7e38e2015-02-09 23:39:45 -05001019 if (do_pc3)
1020 p->pc3 = 0;
1021 if (do_pc6)
1022 p->pc6 = 0;
1023 if (do_pc7)
1024 p->pc7 = 0;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001025 p->pc8 = 0;
1026 p->pc9 = 0;
1027 p->pc10 = 0;
Len Brown889facb2012-11-08 00:48:57 -05001028
1029 p->energy_pkg = 0;
1030 p->energy_dram = 0;
1031 p->energy_cores = 0;
1032 p->energy_gfx = 0;
1033 p->rapl_pkg_perf_status = 0;
1034 p->rapl_dram_perf_status = 0;
1035 p->pkg_temp_c = 0;
Len Brown27d47352016-02-27 00:37:54 -05001036
Len Brownfdf676e2016-02-27 01:28:12 -05001037 p->gfx_rc6_ms = 0;
Len Brown27d47352016-02-27 00:37:54 -05001038 p->gfx_mhz = 0;
Len Brown388e9c82016-12-22 23:57:55 -05001039
1040 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next)
1041 t->counter[i] = 0;
1042
1043 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next)
1044 c->counter[i] = 0;
1045
1046 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next)
1047 p->counter[i] = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001048}
1049int sum_counters(struct thread_data *t, struct core_data *c,
1050 struct pkg_data *p)
1051{
Len Brown388e9c82016-12-22 23:57:55 -05001052 int i;
1053 struct msr_counter *mp;
1054
Len Brownc98d5d92012-06-04 00:56:40 -04001055 average.threads.tsc += t->tsc;
1056 average.threads.aperf += t->aperf;
1057 average.threads.mperf += t->mperf;
1058 average.threads.c1 += t->c1;
1059
Len Brown562a2d32016-02-26 23:48:05 -05001060 average.threads.irq_count += t->irq_count;
1061 average.threads.smi_count += t->smi_count;
1062
Len Brown388e9c82016-12-22 23:57:55 -05001063 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1064 if (mp->format == FORMAT_RAW)
1065 continue;
1066 average.threads.counter[i] += t->counter[i];
1067 }
1068
Len Brownc98d5d92012-06-04 00:56:40 -04001069 /* sum per-core values only for 1st thread in core */
1070 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1071 return 0;
1072
1073 average.cores.c3 += c->c3;
1074 average.cores.c6 += c->c6;
1075 average.cores.c7 += c->c7;
1076
Len Brown889facb2012-11-08 00:48:57 -05001077 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
1078
Len Brown388e9c82016-12-22 23:57:55 -05001079 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1080 if (mp->format == FORMAT_RAW)
1081 continue;
1082 average.cores.counter[i] += c->counter[i];
1083 }
1084
Len Brownc98d5d92012-06-04 00:56:40 -04001085 /* sum per-pkg values only for 1st core in pkg */
1086 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1087 return 0;
1088
Len Brown0b2bb692015-03-26 00:50:30 -04001089 if (do_skl_residency) {
1090 average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
1091 average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
1092 average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
1093 average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
1094 }
1095
Len Brownc98d5d92012-06-04 00:56:40 -04001096 average.packages.pc2 += p->pc2;
Len Brownee7e38e2015-02-09 23:39:45 -05001097 if (do_pc3)
1098 average.packages.pc3 += p->pc3;
1099 if (do_pc6)
1100 average.packages.pc6 += p->pc6;
1101 if (do_pc7)
1102 average.packages.pc7 += p->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001103 average.packages.pc8 += p->pc8;
1104 average.packages.pc9 += p->pc9;
1105 average.packages.pc10 += p->pc10;
Len Brownc98d5d92012-06-04 00:56:40 -04001106
Len Brown889facb2012-11-08 00:48:57 -05001107 average.packages.energy_pkg += p->energy_pkg;
1108 average.packages.energy_dram += p->energy_dram;
1109 average.packages.energy_cores += p->energy_cores;
1110 average.packages.energy_gfx += p->energy_gfx;
1111
Len Brownfdf676e2016-02-27 01:28:12 -05001112 average.packages.gfx_rc6_ms = p->gfx_rc6_ms;
Len Brown27d47352016-02-27 00:37:54 -05001113 average.packages.gfx_mhz = p->gfx_mhz;
1114
Len Brown889facb2012-11-08 00:48:57 -05001115 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
1116
1117 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
1118 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
Len Brown388e9c82016-12-22 23:57:55 -05001119
1120 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1121 if (mp->format == FORMAT_RAW)
1122 continue;
1123 average.packages.counter[i] += p->counter[i];
1124 }
Len Brownc98d5d92012-06-04 00:56:40 -04001125 return 0;
1126}
1127/*
1128 * sum the counters for all cpus in the system
1129 * compute the weighted average
1130 */
1131void compute_average(struct thread_data *t, struct core_data *c,
1132 struct pkg_data *p)
1133{
Len Brown388e9c82016-12-22 23:57:55 -05001134 int i;
1135 struct msr_counter *mp;
1136
Len Brownc98d5d92012-06-04 00:56:40 -04001137 clear_counters(&average.threads, &average.cores, &average.packages);
1138
1139 for_all_cpus(sum_counters, t, c, p);
1140
1141 average.threads.tsc /= topo.num_cpus;
1142 average.threads.aperf /= topo.num_cpus;
1143 average.threads.mperf /= topo.num_cpus;
1144 average.threads.c1 /= topo.num_cpus;
1145
1146 average.cores.c3 /= topo.num_cores;
1147 average.cores.c6 /= topo.num_cores;
1148 average.cores.c7 /= topo.num_cores;
1149
Len Brown0b2bb692015-03-26 00:50:30 -04001150 if (do_skl_residency) {
1151 average.packages.pkg_wtd_core_c0 /= topo.num_packages;
1152 average.packages.pkg_any_core_c0 /= topo.num_packages;
1153 average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
1154 average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
1155 }
1156
Len Brownc98d5d92012-06-04 00:56:40 -04001157 average.packages.pc2 /= topo.num_packages;
Len Brownee7e38e2015-02-09 23:39:45 -05001158 if (do_pc3)
1159 average.packages.pc3 /= topo.num_packages;
1160 if (do_pc6)
1161 average.packages.pc6 /= topo.num_packages;
1162 if (do_pc7)
1163 average.packages.pc7 /= topo.num_packages;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001164
1165 average.packages.pc8 /= topo.num_packages;
1166 average.packages.pc9 /= topo.num_packages;
1167 average.packages.pc10 /= topo.num_packages;
Len Brown388e9c82016-12-22 23:57:55 -05001168
1169 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1170 if (mp->format == FORMAT_RAW)
1171 continue;
1172 average.threads.counter[i] /= topo.num_cpus;
1173 }
1174 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1175 if (mp->format == FORMAT_RAW)
1176 continue;
1177 average.cores.counter[i] /= topo.num_cores;
1178 }
1179 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1180 if (mp->format == FORMAT_RAW)
1181 continue;
1182 average.packages.counter[i] /= topo.num_packages;
1183 }
Len Brownc98d5d92012-06-04 00:56:40 -04001184}
1185
1186static unsigned long long rdtsc(void)
1187{
1188 unsigned int low, high;
1189
1190 asm volatile("rdtsc" : "=a" (low), "=d" (high));
1191
1192 return low | ((unsigned long long)high) << 32;
1193}
1194
Len Brownc98d5d92012-06-04 00:56:40 -04001195/*
1196 * get_counters(...)
1197 * migrate to cpu
1198 * acquire and record local counters for that cpu
1199 */
1200int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1201{
1202 int cpu = t->cpu_id;
Len Brown889facb2012-11-08 00:48:57 -05001203 unsigned long long msr;
Len Brown0102b062016-02-27 03:11:29 -05001204 int aperf_mperf_retry_count = 0;
Len Brown388e9c82016-12-22 23:57:55 -05001205 struct msr_counter *mp;
1206 int i;
Len Brownc98d5d92012-06-04 00:56:40 -04001207
Len Browne52966c2012-11-08 22:38:05 -05001208 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05001209 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brownc98d5d92012-06-04 00:56:40 -04001210 return -1;
Len Browne52966c2012-11-08 22:38:05 -05001211 }
Len Brownc98d5d92012-06-04 00:56:40 -04001212
Len Brown0102b062016-02-27 03:11:29 -05001213retry:
Len Brownc98d5d92012-06-04 00:56:40 -04001214 t->tsc = rdtsc(); /* we are running on local CPU of interest */
1215
1216 if (has_aperf) {
Len Brown0102b062016-02-27 03:11:29 -05001217 unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time;
1218
1219 /*
1220 * The TSC, APERF and MPERF must be read together for
1221 * APERF/MPERF and MPERF/TSC to give accurate results.
1222 *
1223 * Unfortunately, APERF and MPERF are read by
1224 * individual system call, so delays may occur
1225 * between them. If the time to read them
1226 * varies by a large amount, we re-read them.
1227 */
1228
1229 /*
1230 * This initial dummy APERF read has been seen to
1231 * reduce jitter in the subsequent reads.
1232 */
1233
Len Brown9c63a652012-10-31 01:29:52 -04001234 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001235 return -3;
Len Brown0102b062016-02-27 03:11:29 -05001236
1237 t->tsc = rdtsc(); /* re-read close to APERF */
1238
1239 tsc_before = t->tsc;
1240
1241 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
1242 return -3;
1243
1244 tsc_between = rdtsc();
1245
Len Brown9c63a652012-10-31 01:29:52 -04001246 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001247 return -4;
Len Brown0102b062016-02-27 03:11:29 -05001248
1249 tsc_after = rdtsc();
1250
1251 aperf_time = tsc_between - tsc_before;
1252 mperf_time = tsc_after - tsc_between;
1253
1254 /*
1255 * If the system call latency to read APERF and MPERF
1256 * differ by more than 2x, then try again.
1257 */
1258 if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) {
1259 aperf_mperf_retry_count++;
1260 if (aperf_mperf_retry_count < 5)
1261 goto retry;
1262 else
1263 warnx("cpu%d jitter %lld %lld",
1264 cpu, aperf_time, mperf_time);
1265 }
1266 aperf_mperf_retry_count = 0;
1267
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02001268 t->aperf = t->aperf * aperf_mperf_multiplier;
1269 t->mperf = t->mperf * aperf_mperf_multiplier;
Len Brownc98d5d92012-06-04 00:56:40 -04001270 }
1271
Len Brown562a2d32016-02-26 23:48:05 -05001272 if (do_irq)
1273 t->irq_count = irqs_per_cpu[cpu];
Len Brown1ed51012013-02-10 17:19:24 -05001274 if (do_smi) {
1275 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
1276 return -5;
1277 t->smi_count = msr & 0xFFFFFFFF;
1278 }
Len Brownc98d5d92012-06-04 00:56:40 -04001279
Len Brown144b44b2013-11-09 00:30:16 -05001280 if (use_c1_residency_msr) {
1281 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1282 return -6;
1283 }
1284
Len Brown388e9c82016-12-22 23:57:55 -05001285 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1286 if (get_msr(cpu, mp->msr_num, &t->counter[i]))
1287 return -10;
1288 }
1289
1290
Len Brownc98d5d92012-06-04 00:56:40 -04001291 /* collect core counters only for 1st thread in core */
1292 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1293 return 0;
1294
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001295 if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001296 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1297 return -6;
Len Brown144b44b2013-11-09 00:30:16 -05001298 }
1299
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001300 if (do_nhm_cstates && !do_knl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001301 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1302 return -7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001303 } else if (do_knl_cstates) {
1304 if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1305 return -7;
Len Brownc98d5d92012-06-04 00:56:40 -04001306 }
1307
1308 if (do_snb_cstates)
1309 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1310 return -8;
1311
Len Brown889facb2012-11-08 00:48:57 -05001312 if (do_dts) {
1313 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1314 return -9;
1315 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1316 }
1317
Len Brown388e9c82016-12-22 23:57:55 -05001318 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1319 if (get_msr(cpu, mp->msr_num, &c->counter[i]))
1320 return -10;
1321 }
Len Brown889facb2012-11-08 00:48:57 -05001322
Len Brownc98d5d92012-06-04 00:56:40 -04001323 /* collect package counters only for 1st core in package */
1324 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1325 return 0;
1326
Len Brown0b2bb692015-03-26 00:50:30 -04001327 if (do_skl_residency) {
1328 if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
1329 return -10;
1330 if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
1331 return -11;
1332 if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
1333 return -12;
1334 if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
1335 return -13;
1336 }
Len Brownee7e38e2015-02-09 23:39:45 -05001337 if (do_pc3)
Len Brownc98d5d92012-06-04 00:56:40 -04001338 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1339 return -9;
Len Brownee7e38e2015-02-09 23:39:45 -05001340 if (do_pc6)
Len Brownc98d5d92012-06-04 00:56:40 -04001341 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1342 return -10;
Len Brownee7e38e2015-02-09 23:39:45 -05001343 if (do_pc2)
Len Brownc98d5d92012-06-04 00:56:40 -04001344 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1345 return -11;
Len Brownee7e38e2015-02-09 23:39:45 -05001346 if (do_pc7)
Len Brownc98d5d92012-06-04 00:56:40 -04001347 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1348 return -12;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001349 if (do_c8_c9_c10) {
1350 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1351 return -13;
1352 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1353 return -13;
1354 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1355 return -13;
1356 }
Len Brown889facb2012-11-08 00:48:57 -05001357 if (do_rapl & RAPL_PKG) {
1358 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1359 return -13;
1360 p->energy_pkg = msr & 0xFFFFFFFF;
1361 }
Jacob Pan91484942016-06-16 09:48:20 -07001362 if (do_rapl & RAPL_CORES_ENERGY_STATUS) {
Len Brown889facb2012-11-08 00:48:57 -05001363 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1364 return -14;
1365 p->energy_cores = msr & 0xFFFFFFFF;
1366 }
1367 if (do_rapl & RAPL_DRAM) {
1368 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1369 return -15;
1370 p->energy_dram = msr & 0xFFFFFFFF;
1371 }
1372 if (do_rapl & RAPL_GFX) {
1373 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1374 return -16;
1375 p->energy_gfx = msr & 0xFFFFFFFF;
1376 }
1377 if (do_rapl & RAPL_PKG_PERF_STATUS) {
1378 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1379 return -16;
1380 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1381 }
1382 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1383 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1384 return -16;
1385 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1386 }
1387 if (do_ptm) {
1388 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1389 return -17;
1390 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1391 }
Len Brownfdf676e2016-02-27 01:28:12 -05001392
1393 if (do_gfx_rc6_ms)
1394 p->gfx_rc6_ms = gfx_cur_rc6_ms;
1395
Len Brown27d47352016-02-27 00:37:54 -05001396 if (do_gfx_mhz)
1397 p->gfx_mhz = gfx_cur_mhz;
1398
Len Brown388e9c82016-12-22 23:57:55 -05001399 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1400 if (get_msr(cpu, mp->msr_num, &p->counter[i]))
1401 return -10;
1402 }
1403
Len Brown103a8fe2010-10-22 23:53:03 -04001404 return 0;
1405}
1406
Len Brownee7e38e2015-02-09 23:39:45 -05001407/*
1408 * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1409 * If you change the values, note they are used both in comparisons
1410 * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1411 */
1412
1413#define PCLUKN 0 /* Unknown */
1414#define PCLRSV 1 /* Reserved */
1415#define PCL__0 2 /* PC0 */
1416#define PCL__1 3 /* PC1 */
1417#define PCL__2 4 /* PC2 */
1418#define PCL__3 5 /* PC3 */
1419#define PCL__4 6 /* PC4 */
1420#define PCL__6 7 /* PC6 */
1421#define PCL_6N 8 /* PC6 No Retention */
1422#define PCL_6R 9 /* PC6 Retention */
1423#define PCL__7 10 /* PC7 */
1424#define PCL_7S 11 /* PC7 Shrink */
Len Brown0b2bb692015-03-26 00:50:30 -04001425#define PCL__8 12 /* PC8 */
1426#define PCL__9 13 /* PC9 */
1427#define PCLUNL 14 /* Unlimited */
Len Brownee7e38e2015-02-09 23:39:45 -05001428
1429int pkg_cstate_limit = PCLUKN;
1430char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
Len Brown0b2bb692015-03-26 00:50:30 -04001431 "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"};
Len Brownee7e38e2015-02-09 23:39:45 -05001432
Len Browne9257f52015-04-01 21:02:57 -04001433int 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};
1434int 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};
1435int 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};
1436int slv_pkg_cstate_limits[16] = {PCL__0, PCL__1, PCLRSV, PCLRSV, PCL__4, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1437int amt_pkg_cstate_limits[16] = {PCL__0, PCL__1, PCL__2, PCLRSV, PCLRSV, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1438int 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 -04001439int 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 -05001440int 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 -05001441
Len Browna2b7b742015-09-26 00:12:38 -04001442
1443static void
1444calculate_tsc_tweak()
1445{
Len Browna2b7b742015-09-26 00:12:38 -04001446 tsc_tweak = base_hz / tsc_hz;
1447}
1448
Len Brownfcd17212015-03-23 20:29:09 -04001449static void
1450dump_nhm_platform_info(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001451{
1452 unsigned long long msr;
1453 unsigned int ratio;
1454
Len Brownec0adc52015-11-12 02:42:31 -05001455 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001456
Len Brownb7d8c142016-02-13 23:36:17 -05001457 fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001458
Len Brown103a8fe2010-10-22 23:53:03 -04001459 ratio = (msr >> 40) & 0xFF;
Len Brownb7d8c142016-02-13 23:36:17 -05001460 fprintf(outf, "%d * %.0f = %.0f MHz max efficiency frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001461 ratio, bclk, ratio * bclk);
1462
1463 ratio = (msr >> 8) & 0xFF;
Len Brownb7d8c142016-02-13 23:36:17 -05001464 fprintf(outf, "%d * %.0f = %.0f MHz base frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001465 ratio, bclk, ratio * bclk);
1466
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001467 get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001468 fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
Len Brownbfae2052015-06-17 12:27:21 -04001469 base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
Len Brown67920412013-01-31 15:22:15 -05001470
Len Brownfcd17212015-03-23 20:29:09 -04001471 return;
1472}
1473
1474static void
1475dump_hsw_turbo_ratio_limits(void)
1476{
1477 unsigned long long msr;
1478 unsigned int ratio;
1479
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001480 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04001481
Len Brownb7d8c142016-02-13 23:36:17 -05001482 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04001483
1484 ratio = (msr >> 8) & 0xFF;
1485 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001486 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 18 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04001487 ratio, bclk, ratio * bclk);
1488
1489 ratio = (msr >> 0) & 0xFF;
1490 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001491 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 17 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04001492 ratio, bclk, ratio * bclk);
1493 return;
1494}
1495
1496static void
1497dump_ivt_turbo_ratio_limits(void)
1498{
1499 unsigned long long msr;
1500 unsigned int ratio;
Len Brown6574a5d2012-09-21 00:01:31 -04001501
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001502 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001503
Len Brownb7d8c142016-02-13 23:36:17 -05001504 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001505
1506 ratio = (msr >> 56) & 0xFF;
1507 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001508 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 16 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001509 ratio, bclk, ratio * bclk);
1510
1511 ratio = (msr >> 48) & 0xFF;
1512 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001513 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 15 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001514 ratio, bclk, ratio * bclk);
1515
1516 ratio = (msr >> 40) & 0xFF;
1517 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001518 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 14 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001519 ratio, bclk, ratio * bclk);
1520
1521 ratio = (msr >> 32) & 0xFF;
1522 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001523 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 13 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001524 ratio, bclk, ratio * bclk);
1525
1526 ratio = (msr >> 24) & 0xFF;
1527 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001528 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 12 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001529 ratio, bclk, ratio * bclk);
1530
1531 ratio = (msr >> 16) & 0xFF;
1532 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001533 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 11 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001534 ratio, bclk, ratio * bclk);
1535
1536 ratio = (msr >> 8) & 0xFF;
1537 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001538 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 10 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001539 ratio, bclk, ratio * bclk);
1540
1541 ratio = (msr >> 0) & 0xFF;
1542 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001543 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 9 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001544 ratio, bclk, ratio * bclk);
Len Brownfcd17212015-03-23 20:29:09 -04001545 return;
1546}
Len Brown6574a5d2012-09-21 00:01:31 -04001547
Len Brownfcd17212015-03-23 20:29:09 -04001548static void
1549dump_nhm_turbo_ratio_limits(void)
1550{
1551 unsigned long long msr;
1552 unsigned int ratio;
Len Brown103a8fe2010-10-22 23:53:03 -04001553
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001554 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001555
Len Brownb7d8c142016-02-13 23:36:17 -05001556 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001557
1558 ratio = (msr >> 56) & 0xFF;
1559 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001560 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 8 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001561 ratio, bclk, ratio * bclk);
1562
1563 ratio = (msr >> 48) & 0xFF;
1564 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001565 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 7 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001566 ratio, bclk, ratio * bclk);
1567
1568 ratio = (msr >> 40) & 0xFF;
1569 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001570 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 6 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001571 ratio, bclk, ratio * bclk);
1572
1573 ratio = (msr >> 32) & 0xFF;
1574 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001575 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 5 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001576 ratio, bclk, ratio * bclk);
1577
Len Brown103a8fe2010-10-22 23:53:03 -04001578 ratio = (msr >> 24) & 0xFF;
1579 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001580 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001581 ratio, bclk, ratio * bclk);
1582
1583 ratio = (msr >> 16) & 0xFF;
1584 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001585 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001586 ratio, bclk, ratio * bclk);
1587
1588 ratio = (msr >> 8) & 0xFF;
1589 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001590 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001591 ratio, bclk, ratio * bclk);
1592
1593 ratio = (msr >> 0) & 0xFF;
1594 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001595 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001596 ratio, bclk, ratio * bclk);
Len Brownfcd17212015-03-23 20:29:09 -04001597 return;
1598}
Len Brown3a9a9412014-08-15 02:39:52 -04001599
Len Brownfcd17212015-03-23 20:29:09 -04001600static void
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001601dump_knl_turbo_ratio_limits(void)
1602{
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001603 const unsigned int buckets_no = 7;
1604
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001605 unsigned long long msr;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001606 int delta_cores, delta_ratio;
1607 int i, b_nr;
1608 unsigned int cores[buckets_no];
1609 unsigned int ratio[buckets_no];
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001610
Srinivas Pandruvadaebf59262016-07-06 16:07:56 -07001611 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001612
Len Brownb7d8c142016-02-13 23:36:17 -05001613 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n",
Len Brownbfae2052015-06-17 12:27:21 -04001614 base_cpu, msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001615
1616 /**
1617 * Turbo encoding in KNL is as follows:
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001618 * [0] -- Reserved
1619 * [7:1] -- Base value of number of active cores of bucket 1.
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001620 * [15:8] -- Base value of freq ratio of bucket 1.
1621 * [20:16] -- +ve delta of number of active cores of bucket 2.
1622 * i.e. active cores of bucket 2 =
1623 * active cores of bucket 1 + delta
1624 * [23:21] -- Negative delta of freq ratio of bucket 2.
1625 * i.e. freq ratio of bucket 2 =
1626 * freq ratio of bucket 1 - delta
1627 * [28:24]-- +ve delta of number of active cores of bucket 3.
1628 * [31:29]-- -ve delta of freq ratio of bucket 3.
1629 * [36:32]-- +ve delta of number of active cores of bucket 4.
1630 * [39:37]-- -ve delta of freq ratio of bucket 4.
1631 * [44:40]-- +ve delta of number of active cores of bucket 5.
1632 * [47:45]-- -ve delta of freq ratio of bucket 5.
1633 * [52:48]-- +ve delta of number of active cores of bucket 6.
1634 * [55:53]-- -ve delta of freq ratio of bucket 6.
1635 * [60:56]-- +ve delta of number of active cores of bucket 7.
1636 * [63:61]-- -ve delta of freq ratio of bucket 7.
1637 */
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001638
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001639 b_nr = 0;
1640 cores[b_nr] = (msr & 0xFF) >> 1;
1641 ratio[b_nr] = (msr >> 8) & 0xFF;
1642
1643 for (i = 16; i < 64; i += 8) {
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001644 delta_cores = (msr >> i) & 0x1F;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001645 delta_ratio = (msr >> (i + 5)) & 0x7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001646
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001647 cores[b_nr + 1] = cores[b_nr] + delta_cores;
1648 ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
1649 b_nr++;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001650 }
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001651
1652 for (i = buckets_no - 1; i >= 0; i--)
1653 if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
Len Brownb7d8c142016-02-13 23:36:17 -05001654 fprintf(outf,
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001655 "%d * %.0f = %.0f MHz max turbo %d active cores\n",
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001656 ratio[i], bclk, ratio[i] * bclk, cores[i]);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001657}
1658
1659static void
Len Brownfcd17212015-03-23 20:29:09 -04001660dump_nhm_cst_cfg(void)
1661{
1662 unsigned long long msr;
1663
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001664 get_msr(base_cpu, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04001665
1666#define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
1667#define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
1668
Len Brownb7d8c142016-02-13 23:36:17 -05001669 fprintf(outf, "cpu%d: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04001670
Len Brownb7d8c142016-02-13 23:36:17 -05001671 fprintf(outf, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: %s)\n",
Len Brownfcd17212015-03-23 20:29:09 -04001672 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
1673 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
1674 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
1675 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
1676 (msr & (1 << 15)) ? "" : "UN",
Len Brown6c34f162016-03-13 03:21:22 -04001677 (unsigned int)msr & 0xF,
Len Brownfcd17212015-03-23 20:29:09 -04001678 pkg_cstate_limit_strings[pkg_cstate_limit]);
1679 return;
Len Brown103a8fe2010-10-22 23:53:03 -04001680}
1681
Len Brown6fb31432015-06-17 16:23:45 -04001682static void
1683dump_config_tdp(void)
1684{
1685 unsigned long long msr;
1686
1687 get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001688 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
Chen Yu685b5352015-12-13 21:09:31 +08001689 fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF);
Len Brown6fb31432015-06-17 16:23:45 -04001690
1691 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001692 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04001693 if (msr) {
Chen Yu685b5352015-12-13 21:09:31 +08001694 fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
1695 fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
1696 fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
1697 fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF);
Len Brown6fb31432015-06-17 16:23:45 -04001698 }
Len Brownb7d8c142016-02-13 23:36:17 -05001699 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04001700
1701 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001702 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04001703 if (msr) {
Chen Yu685b5352015-12-13 21:09:31 +08001704 fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
1705 fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
1706 fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
1707 fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF);
Len Brown6fb31432015-06-17 16:23:45 -04001708 }
Len Brownb7d8c142016-02-13 23:36:17 -05001709 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04001710
1711 get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001712 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04001713 if ((msr) & 0x3)
Len Brownb7d8c142016-02-13 23:36:17 -05001714 fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
1715 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
1716 fprintf(outf, ")\n");
Len Brown36229892016-02-26 20:51:02 -05001717
Len Brown6fb31432015-06-17 16:23:45 -04001718 get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001719 fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
Chen Yu685b5352015-12-13 21:09:31 +08001720 fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF);
Len Brownb7d8c142016-02-13 23:36:17 -05001721 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
1722 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04001723}
Len Brown5a634262016-04-06 17:15:55 -04001724
1725unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
1726
1727void print_irtl(void)
1728{
1729 unsigned long long msr;
1730
1731 get_msr(base_cpu, MSR_PKGC3_IRTL, &msr);
1732 fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr);
1733 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
1734 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
1735
1736 get_msr(base_cpu, MSR_PKGC6_IRTL, &msr);
1737 fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr);
1738 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
1739 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
1740
1741 get_msr(base_cpu, MSR_PKGC7_IRTL, &msr);
1742 fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr);
1743 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
1744 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
1745
1746 if (!do_irtl_hsw)
1747 return;
1748
1749 get_msr(base_cpu, MSR_PKGC8_IRTL, &msr);
1750 fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr);
1751 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
1752 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
1753
1754 get_msr(base_cpu, MSR_PKGC9_IRTL, &msr);
1755 fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr);
1756 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
1757 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
1758
1759 get_msr(base_cpu, MSR_PKGC10_IRTL, &msr);
1760 fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr);
1761 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
1762 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
1763
1764}
Len Brown36229892016-02-26 20:51:02 -05001765void free_fd_percpu(void)
1766{
1767 int i;
1768
Mika Westerberg01a67ad2016-04-22 11:13:23 +03001769 for (i = 0; i < topo.max_cpu_num + 1; ++i) {
Len Brown36229892016-02-26 20:51:02 -05001770 if (fd_percpu[i] != 0)
1771 close(fd_percpu[i]);
1772 }
1773
1774 free(fd_percpu);
Len Brown6fb31432015-06-17 16:23:45 -04001775}
1776
Len Brownc98d5d92012-06-04 00:56:40 -04001777void free_all_buffers(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001778{
Len Brownc98d5d92012-06-04 00:56:40 -04001779 CPU_FREE(cpu_present_set);
1780 cpu_present_set = NULL;
Len Brown36229892016-02-26 20:51:02 -05001781 cpu_present_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001782
Len Brownc98d5d92012-06-04 00:56:40 -04001783 CPU_FREE(cpu_affinity_set);
1784 cpu_affinity_set = NULL;
1785 cpu_affinity_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001786
Len Brownc98d5d92012-06-04 00:56:40 -04001787 free(thread_even);
1788 free(core_even);
1789 free(package_even);
1790
1791 thread_even = NULL;
1792 core_even = NULL;
1793 package_even = NULL;
1794
1795 free(thread_odd);
1796 free(core_odd);
1797 free(package_odd);
1798
1799 thread_odd = NULL;
1800 core_odd = NULL;
1801 package_odd = NULL;
1802
1803 free(output_buffer);
1804 output_buffer = NULL;
1805 outp = NULL;
Len Brown36229892016-02-26 20:51:02 -05001806
1807 free_fd_percpu();
Len Brown562a2d32016-02-26 23:48:05 -05001808
1809 free(irq_column_2_cpu);
1810 free(irqs_per_cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001811}
1812
Len Brownc98d5d92012-06-04 00:56:40 -04001813/*
Josh Triplett57a42a32013-08-20 17:20:17 -07001814 * Open a file, and exit on failure
1815 */
1816FILE *fopen_or_die(const char *path, const char *mode)
1817{
Len Brownb7d8c142016-02-13 23:36:17 -05001818 FILE *filep = fopen(path, mode);
Josh Triplettb2c95d92013-08-20 17:20:18 -07001819 if (!filep)
1820 err(1, "%s: open failed", path);
Josh Triplett57a42a32013-08-20 17:20:17 -07001821 return filep;
1822}
1823
1824/*
Josh Triplett95aebc42013-08-20 17:20:16 -07001825 * Parse a file containing a single int.
1826 */
1827int parse_int_file(const char *fmt, ...)
1828{
1829 va_list args;
1830 char path[PATH_MAX];
1831 FILE *filep;
1832 int value;
1833
1834 va_start(args, fmt);
1835 vsnprintf(path, sizeof(path), fmt, args);
1836 va_end(args);
Josh Triplett57a42a32013-08-20 17:20:17 -07001837 filep = fopen_or_die(path, "r");
Josh Triplettb2c95d92013-08-20 17:20:18 -07001838 if (fscanf(filep, "%d", &value) != 1)
1839 err(1, "%s: failed to parse number from file", path);
Josh Triplett95aebc42013-08-20 17:20:16 -07001840 fclose(filep);
1841 return value;
1842}
1843
1844/*
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001845 * get_cpu_position_in_core(cpu)
1846 * return the position of the CPU among its HT siblings in the core
1847 * return -1 if the sibling is not in list
Len Brownc98d5d92012-06-04 00:56:40 -04001848 */
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001849int get_cpu_position_in_core(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001850{
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001851 char path[64];
1852 FILE *filep;
1853 int this_cpu;
1854 char character;
1855 int i;
1856
1857 sprintf(path,
1858 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list",
1859 cpu);
1860 filep = fopen(path, "r");
1861 if (filep == NULL) {
1862 perror(path);
1863 exit(1);
1864 }
1865
1866 for (i = 0; i < topo.num_threads_per_core; i++) {
1867 fscanf(filep, "%d", &this_cpu);
1868 if (this_cpu == cpu) {
1869 fclose(filep);
1870 return i;
1871 }
1872
1873 /* Account for no separator after last thread*/
1874 if (i != (topo.num_threads_per_core - 1))
1875 fscanf(filep, "%c", &character);
1876 }
1877
1878 fclose(filep);
1879 return -1;
Len Brown103a8fe2010-10-22 23:53:03 -04001880}
1881
Len Brownc98d5d92012-06-04 00:56:40 -04001882/*
1883 * cpu_is_first_core_in_package(cpu)
1884 * return 1 if given CPU is 1st core in package
1885 */
1886int cpu_is_first_core_in_package(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001887{
Josh Triplett95aebc42013-08-20 17:20:16 -07001888 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001889}
1890
1891int get_physical_package_id(int cpu)
1892{
Josh Triplett95aebc42013-08-20 17:20:16 -07001893 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001894}
1895
1896int get_core_id(int cpu)
1897{
Josh Triplett95aebc42013-08-20 17:20:16 -07001898 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001899}
1900
Len Brownc98d5d92012-06-04 00:56:40 -04001901int get_num_ht_siblings(int cpu)
1902{
1903 char path[80];
1904 FILE *filep;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001905 int sib1;
1906 int matches = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001907 char character;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001908 char str[100];
1909 char *ch;
Len Brownc98d5d92012-06-04 00:56:40 -04001910
1911 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
Josh Triplett57a42a32013-08-20 17:20:17 -07001912 filep = fopen_or_die(path, "r");
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001913
Len Brownc98d5d92012-06-04 00:56:40 -04001914 /*
1915 * file format:
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001916 * A ',' separated or '-' separated set of numbers
1917 * (eg 1-2 or 1,3,4,5)
Len Brownc98d5d92012-06-04 00:56:40 -04001918 */
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001919 fscanf(filep, "%d%c\n", &sib1, &character);
1920 fseek(filep, 0, SEEK_SET);
1921 fgets(str, 100, filep);
1922 ch = strchr(str, character);
1923 while (ch != NULL) {
1924 matches++;
1925 ch = strchr(ch+1, character);
1926 }
Len Brownc98d5d92012-06-04 00:56:40 -04001927
1928 fclose(filep);
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001929 return matches+1;
Len Brownc98d5d92012-06-04 00:56:40 -04001930}
1931
Len Brown103a8fe2010-10-22 23:53:03 -04001932/*
Len Brownc98d5d92012-06-04 00:56:40 -04001933 * run func(thread, core, package) in topology order
1934 * skip non-present cpus
Len Brown103a8fe2010-10-22 23:53:03 -04001935 */
1936
Len Brownc98d5d92012-06-04 00:56:40 -04001937int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
1938 struct pkg_data *, struct thread_data *, struct core_data *,
1939 struct pkg_data *), struct thread_data *thread_base,
1940 struct core_data *core_base, struct pkg_data *pkg_base,
1941 struct thread_data *thread_base2, struct core_data *core_base2,
1942 struct pkg_data *pkg_base2)
1943{
1944 int retval, pkg_no, core_no, thread_no;
1945
1946 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
1947 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
1948 for (thread_no = 0; thread_no <
1949 topo.num_threads_per_core; ++thread_no) {
1950 struct thread_data *t, *t2;
1951 struct core_data *c, *c2;
1952 struct pkg_data *p, *p2;
1953
1954 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
1955
1956 if (cpu_is_not_present(t->cpu_id))
1957 continue;
1958
1959 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
1960
1961 c = GET_CORE(core_base, core_no, pkg_no);
1962 c2 = GET_CORE(core_base2, core_no, pkg_no);
1963
1964 p = GET_PKG(pkg_base, pkg_no);
1965 p2 = GET_PKG(pkg_base2, pkg_no);
1966
1967 retval = func(t, c, p, t2, c2, p2);
1968 if (retval)
1969 return retval;
1970 }
1971 }
1972 }
1973 return 0;
1974}
1975
1976/*
1977 * run func(cpu) on every cpu in /proc/stat
1978 * return max_cpu number
1979 */
1980int for_all_proc_cpus(int (func)(int))
Len Brown103a8fe2010-10-22 23:53:03 -04001981{
1982 FILE *fp;
Len Brownc98d5d92012-06-04 00:56:40 -04001983 int cpu_num;
Len Brown103a8fe2010-10-22 23:53:03 -04001984 int retval;
1985
Josh Triplett57a42a32013-08-20 17:20:17 -07001986 fp = fopen_or_die(proc_stat, "r");
Len Brown103a8fe2010-10-22 23:53:03 -04001987
1988 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
Josh Triplettb2c95d92013-08-20 17:20:18 -07001989 if (retval != 0)
1990 err(1, "%s: failed to parse format", proc_stat);
Len Brown103a8fe2010-10-22 23:53:03 -04001991
Len Brownc98d5d92012-06-04 00:56:40 -04001992 while (1) {
1993 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 -04001994 if (retval != 1)
1995 break;
1996
Len Brownc98d5d92012-06-04 00:56:40 -04001997 retval = func(cpu_num);
1998 if (retval) {
1999 fclose(fp);
2000 return(retval);
2001 }
Len Brown103a8fe2010-10-22 23:53:03 -04002002 }
2003 fclose(fp);
Len Brownc98d5d92012-06-04 00:56:40 -04002004 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002005}
2006
2007void re_initialize(void)
2008{
Len Brownc98d5d92012-06-04 00:56:40 -04002009 free_all_buffers();
2010 setup_all_buffers();
2011 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04002012}
2013
Len Brownc98d5d92012-06-04 00:56:40 -04002014
Len Brown103a8fe2010-10-22 23:53:03 -04002015/*
Len Brownc98d5d92012-06-04 00:56:40 -04002016 * count_cpus()
2017 * remember the last one seen, it will be the max
Len Brown103a8fe2010-10-22 23:53:03 -04002018 */
Len Brownc98d5d92012-06-04 00:56:40 -04002019int count_cpus(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04002020{
Len Brownc98d5d92012-06-04 00:56:40 -04002021 if (topo.max_cpu_num < cpu)
2022 topo.max_cpu_num = cpu;
Len Brown103a8fe2010-10-22 23:53:03 -04002023
Len Brownc98d5d92012-06-04 00:56:40 -04002024 topo.num_cpus += 1;
2025 return 0;
2026}
2027int mark_cpu_present(int cpu)
2028{
2029 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brown15aaa342012-03-29 22:19:58 -04002030 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002031}
2032
Len Brown562a2d32016-02-26 23:48:05 -05002033/*
2034 * snapshot_proc_interrupts()
2035 *
2036 * read and record summary of /proc/interrupts
2037 *
2038 * return 1 if config change requires a restart, else return 0
2039 */
2040int snapshot_proc_interrupts(void)
2041{
2042 static FILE *fp;
2043 int column, retval;
2044
2045 if (fp == NULL)
2046 fp = fopen_or_die("/proc/interrupts", "r");
2047 else
2048 rewind(fp);
2049
2050 /* read 1st line of /proc/interrupts to get cpu* name for each column */
2051 for (column = 0; column < topo.num_cpus; ++column) {
2052 int cpu_number;
2053
2054 retval = fscanf(fp, " CPU%d", &cpu_number);
2055 if (retval != 1)
2056 break;
2057
2058 if (cpu_number > topo.max_cpu_num) {
2059 warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num);
2060 return 1;
2061 }
2062
2063 irq_column_2_cpu[column] = cpu_number;
2064 irqs_per_cpu[cpu_number] = 0;
2065 }
2066
2067 /* read /proc/interrupt count lines and sum up irqs per cpu */
2068 while (1) {
2069 int column;
2070 char buf[64];
2071
2072 retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */
2073 if (retval != 1)
2074 break;
2075
2076 /* read the count per cpu */
2077 for (column = 0; column < topo.num_cpus; ++column) {
2078
2079 int cpu_number, irq_count;
2080
2081 retval = fscanf(fp, " %d", &irq_count);
2082 if (retval != 1)
2083 break;
2084
2085 cpu_number = irq_column_2_cpu[column];
2086 irqs_per_cpu[cpu_number] += irq_count;
2087
2088 }
2089
2090 while (getc(fp) != '\n')
2091 ; /* flush interrupt description */
2092
2093 }
2094 return 0;
2095}
Len Brown27d47352016-02-27 00:37:54 -05002096/*
Len Brownfdf676e2016-02-27 01:28:12 -05002097 * snapshot_gfx_rc6_ms()
2098 *
2099 * record snapshot of
2100 * /sys/class/drm/card0/power/rc6_residency_ms
2101 *
2102 * return 1 if config change requires a restart, else return 0
2103 */
2104int snapshot_gfx_rc6_ms(void)
2105{
2106 FILE *fp;
2107 int retval;
2108
2109 fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r");
2110
2111 retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms);
2112 if (retval != 1)
2113 err(1, "GFX rc6");
2114
2115 fclose(fp);
2116
2117 return 0;
2118}
2119/*
Len Brown27d47352016-02-27 00:37:54 -05002120 * snapshot_gfx_mhz()
2121 *
2122 * record snapshot of
2123 * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz
2124 *
2125 * return 1 if config change requires a restart, else return 0
2126 */
2127int snapshot_gfx_mhz(void)
2128{
2129 static FILE *fp;
2130 int retval;
2131
2132 if (fp == NULL)
2133 fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r");
2134 else
2135 rewind(fp);
2136
2137 retval = fscanf(fp, "%d", &gfx_cur_mhz);
2138 if (retval != 1)
2139 err(1, "GFX MHz");
2140
2141 return 0;
2142}
Len Brown562a2d32016-02-26 23:48:05 -05002143
2144/*
2145 * snapshot /proc and /sys files
2146 *
2147 * return 1 if configuration restart needed, else return 0
2148 */
2149int snapshot_proc_sysfs_files(void)
2150{
2151 if (snapshot_proc_interrupts())
2152 return 1;
2153
Len Brownfdf676e2016-02-27 01:28:12 -05002154 if (do_gfx_rc6_ms)
2155 snapshot_gfx_rc6_ms();
2156
Len Brown27d47352016-02-27 00:37:54 -05002157 if (do_gfx_mhz)
2158 snapshot_gfx_mhz();
2159
Len Brown562a2d32016-02-26 23:48:05 -05002160 return 0;
2161}
2162
Len Brown103a8fe2010-10-22 23:53:03 -04002163void turbostat_loop()
2164{
Len Brownc98d5d92012-06-04 00:56:40 -04002165 int retval;
Len Browne52966c2012-11-08 22:38:05 -05002166 int restarted = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04002167
Len Brown103a8fe2010-10-22 23:53:03 -04002168restart:
Len Browne52966c2012-11-08 22:38:05 -05002169 restarted++;
2170
Len Brown562a2d32016-02-26 23:48:05 -05002171 snapshot_proc_sysfs_files();
Len Brownc98d5d92012-06-04 00:56:40 -04002172 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002173 if (retval < -1) {
2174 exit(retval);
2175 } else if (retval == -1) {
Len Browne52966c2012-11-08 22:38:05 -05002176 if (restarted > 1) {
2177 exit(retval);
2178 }
Len Brownc98d5d92012-06-04 00:56:40 -04002179 re_initialize();
2180 goto restart;
2181 }
Len Browne52966c2012-11-08 22:38:05 -05002182 restarted = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04002183 gettimeofday(&tv_even, (struct timezone *)NULL);
2184
2185 while (1) {
Len Brownc98d5d92012-06-04 00:56:40 -04002186 if (for_all_proc_cpus(cpu_is_not_present)) {
Len Brown103a8fe2010-10-22 23:53:03 -04002187 re_initialize();
2188 goto restart;
2189 }
Len Brown2a0609c2016-02-12 22:44:48 -05002190 nanosleep(&interval_ts, NULL);
Len Brown562a2d32016-02-26 23:48:05 -05002191 if (snapshot_proc_sysfs_files())
2192 goto restart;
Len Brownc98d5d92012-06-04 00:56:40 -04002193 retval = for_all_cpus(get_counters, ODD_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002194 if (retval < -1) {
2195 exit(retval);
2196 } else if (retval == -1) {
Len Brown15aaa342012-03-29 22:19:58 -04002197 re_initialize();
2198 goto restart;
2199 }
Len Brown103a8fe2010-10-22 23:53:03 -04002200 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002201 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04002202 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) {
2203 re_initialize();
2204 goto restart;
2205 }
Len Brownc98d5d92012-06-04 00:56:40 -04002206 compute_average(EVEN_COUNTERS);
2207 format_all_counters(EVEN_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05002208 flush_output_stdout();
Len Brown2a0609c2016-02-12 22:44:48 -05002209 nanosleep(&interval_ts, NULL);
Len Brown562a2d32016-02-26 23:48:05 -05002210 if (snapshot_proc_sysfs_files())
2211 goto restart;
Len Brownc98d5d92012-06-04 00:56:40 -04002212 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04002213 if (retval < -1) {
2214 exit(retval);
2215 } else if (retval == -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04002216 re_initialize();
2217 goto restart;
2218 }
Len Brown103a8fe2010-10-22 23:53:03 -04002219 gettimeofday(&tv_even, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002220 timersub(&tv_even, &tv_odd, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04002221 if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) {
2222 re_initialize();
2223 goto restart;
2224 }
Len Brownc98d5d92012-06-04 00:56:40 -04002225 compute_average(ODD_COUNTERS);
2226 format_all_counters(ODD_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05002227 flush_output_stdout();
Len Brown103a8fe2010-10-22 23:53:03 -04002228 }
2229}
2230
2231void check_dev_msr()
2232{
2233 struct stat sb;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002234 char pathname[32];
Len Brown103a8fe2010-10-22 23:53:03 -04002235
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002236 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2237 if (stat(pathname, &sb))
Len Browna21d38c2015-03-24 16:37:35 -04002238 if (system("/sbin/modprobe msr > /dev/null 2>&1"))
2239 err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
Len Brown103a8fe2010-10-22 23:53:03 -04002240}
2241
Len Brown98481e72014-08-15 00:36:50 -04002242void check_permissions()
Len Brown103a8fe2010-10-22 23:53:03 -04002243{
Len Brown98481e72014-08-15 00:36:50 -04002244 struct __user_cap_header_struct cap_header_data;
2245 cap_user_header_t cap_header = &cap_header_data;
2246 struct __user_cap_data_struct cap_data_data;
2247 cap_user_data_t cap_data = &cap_data_data;
2248 extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
2249 int do_exit = 0;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002250 char pathname[32];
Len Brown98481e72014-08-15 00:36:50 -04002251
2252 /* check for CAP_SYS_RAWIO */
2253 cap_header->pid = getpid();
2254 cap_header->version = _LINUX_CAPABILITY_VERSION;
2255 if (capget(cap_header, cap_data) < 0)
2256 err(-6, "capget(2) failed");
2257
2258 if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
2259 do_exit++;
2260 warnx("capget(CAP_SYS_RAWIO) failed,"
2261 " try \"# setcap cap_sys_rawio=ep %s\"", progname);
2262 }
2263
2264 /* test file permissions */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002265 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2266 if (euidaccess(pathname, R_OK)) {
Len Brown98481e72014-08-15 00:36:50 -04002267 do_exit++;
2268 warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
2269 }
2270
2271 /* if all else fails, thell them to be root */
2272 if (do_exit)
2273 if (getuid() != 0)
Len Brownd7899442015-01-23 00:12:33 -05002274 warnx("... or simply run as root");
Len Brown98481e72014-08-15 00:36:50 -04002275
2276 if (do_exit)
2277 exit(-6);
Len Brown103a8fe2010-10-22 23:53:03 -04002278}
2279
Len Brownd7899442015-01-23 00:12:33 -05002280/*
2281 * NHM adds support for additional MSRs:
2282 *
2283 * MSR_SMI_COUNT 0x00000034
2284 *
Len Brownec0adc52015-11-12 02:42:31 -05002285 * MSR_PLATFORM_INFO 0x000000ce
Len Brownd7899442015-01-23 00:12:33 -05002286 * MSR_NHM_SNB_PKG_CST_CFG_CTL 0x000000e2
2287 *
2288 * MSR_PKG_C3_RESIDENCY 0x000003f8
2289 * MSR_PKG_C6_RESIDENCY 0x000003f9
2290 * MSR_CORE_C3_RESIDENCY 0x000003fc
2291 * MSR_CORE_C6_RESIDENCY 0x000003fd
2292 *
Len Brownee7e38e2015-02-09 23:39:45 -05002293 * Side effect:
2294 * sets global pkg_cstate_limit to decode MSR_NHM_SNB_PKG_CST_CFG_CTL
Len Brownd7899442015-01-23 00:12:33 -05002295 */
Len Brownee7e38e2015-02-09 23:39:45 -05002296int probe_nhm_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04002297{
Len Brownee7e38e2015-02-09 23:39:45 -05002298 unsigned long long msr;
Len Brown21ed5572015-10-19 22:37:40 -04002299 unsigned int base_ratio;
Len Brownee7e38e2015-02-09 23:39:45 -05002300 int *pkg_cstate_limits;
2301
Len Brown103a8fe2010-10-22 23:53:03 -04002302 if (!genuine_intel)
2303 return 0;
2304
2305 if (family != 6)
2306 return 0;
2307
Len Brown21ed5572015-10-19 22:37:40 -04002308 bclk = discover_bclk(family, model);
2309
Len Brown103a8fe2010-10-22 23:53:03 -04002310 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002311 case INTEL_FAM6_NEHALEM_EP: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
2312 case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
Len Brown103a8fe2010-10-22 23:53:03 -04002313 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
Len Brown869ce692016-06-16 23:22:37 -04002314 case INTEL_FAM6_WESTMERE: /* Westmere Client - Clarkdale, Arrandale */
2315 case INTEL_FAM6_WESTMERE_EP: /* Westmere EP - Gulftown */
2316 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
2317 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
Len Brownee7e38e2015-02-09 23:39:45 -05002318 pkg_cstate_limits = nhm_pkg_cstate_limits;
2319 break;
Len Brown869ce692016-06-16 23:22:37 -04002320 case INTEL_FAM6_SANDYBRIDGE: /* SNB */
2321 case INTEL_FAM6_SANDYBRIDGE_X: /* SNB Xeon */
2322 case INTEL_FAM6_IVYBRIDGE: /* IVB */
2323 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
Len Brownee7e38e2015-02-09 23:39:45 -05002324 pkg_cstate_limits = snb_pkg_cstate_limits;
2325 break;
Len Brown869ce692016-06-16 23:22:37 -04002326 case INTEL_FAM6_HASWELL_CORE: /* HSW */
2327 case INTEL_FAM6_HASWELL_X: /* HSX */
2328 case INTEL_FAM6_HASWELL_ULT: /* HSW */
2329 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
2330 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
2331 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
2332 case INTEL_FAM6_BROADWELL_X: /* BDX */
2333 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
2334 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
2335 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
2336 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
2337 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Brownee7e38e2015-02-09 23:39:45 -05002338 pkg_cstate_limits = hsw_pkg_cstate_limits;
2339 break;
Len Brownd8ebb442016-12-01 20:27:46 -05002340 case INTEL_FAM6_SKYLAKE_X: /* SKX */
2341 pkg_cstate_limits = skx_pkg_cstate_limits;
2342 break;
Len Brown869ce692016-06-16 23:22:37 -04002343 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
2344 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Len Brownee7e38e2015-02-09 23:39:45 -05002345 pkg_cstate_limits = slv_pkg_cstate_limits;
2346 break;
Len Brown869ce692016-06-16 23:22:37 -04002347 case INTEL_FAM6_ATOM_AIRMONT: /* AMT */
Len Brownee7e38e2015-02-09 23:39:45 -05002348 pkg_cstate_limits = amt_pkg_cstate_limits;
2349 break;
Len Brown869ce692016-06-16 23:22:37 -04002350 case INTEL_FAM6_XEON_PHI_KNL: /* PHI */
Len Brown005c82d2016-12-01 01:35:38 -05002351 case INTEL_FAM6_XEON_PHI_KNM:
Len Brownee7e38e2015-02-09 23:39:45 -05002352 pkg_cstate_limits = phi_pkg_cstate_limits;
2353 break;
Len Brown869ce692016-06-16 23:22:37 -04002354 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
2355 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Browne4085d52016-04-06 17:15:56 -04002356 pkg_cstate_limits = bxt_pkg_cstate_limits;
2357 break;
Len Brown103a8fe2010-10-22 23:53:03 -04002358 default:
2359 return 0;
2360 }
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002361 get_msr(base_cpu, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
Len Browne9257f52015-04-01 21:02:57 -04002362 pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
Len Brownee7e38e2015-02-09 23:39:45 -05002363
Len Brownec0adc52015-11-12 02:42:31 -05002364 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown21ed5572015-10-19 22:37:40 -04002365 base_ratio = (msr >> 8) & 0xFF;
2366
2367 base_hz = base_ratio * bclk * 1000000;
2368 has_base_hz = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05002369 return 1;
Len Brown103a8fe2010-10-22 23:53:03 -04002370}
Len Brownd7899442015-01-23 00:12:33 -05002371int has_nhm_turbo_ratio_limit(unsigned int family, unsigned int model)
2372{
Len Brownd7899442015-01-23 00:12:33 -05002373 switch (model) {
2374 /* Nehalem compatible, but do not include turbo-ratio limit support */
Len Brown869ce692016-06-16 23:22:37 -04002375 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
2376 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
2377 case INTEL_FAM6_XEON_PHI_KNL: /* PHI - Knights Landing (different MSR definition) */
Len Brown005c82d2016-12-01 01:35:38 -05002378 case INTEL_FAM6_XEON_PHI_KNM:
Len Brownd7899442015-01-23 00:12:33 -05002379 return 0;
2380 default:
2381 return 1;
2382 }
2383}
Len Brown6574a5d2012-09-21 00:01:31 -04002384int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
2385{
2386 if (!genuine_intel)
2387 return 0;
2388
2389 if (family != 6)
2390 return 0;
2391
2392 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002393 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
2394 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
Len Brown6574a5d2012-09-21 00:01:31 -04002395 return 1;
2396 default:
2397 return 0;
2398 }
2399}
Len Brownfcd17212015-03-23 20:29:09 -04002400int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
2401{
2402 if (!genuine_intel)
2403 return 0;
2404
2405 if (family != 6)
2406 return 0;
2407
2408 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002409 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
Len Brownfcd17212015-03-23 20:29:09 -04002410 return 1;
2411 default:
2412 return 0;
2413 }
2414}
2415
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002416int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
2417{
2418 if (!genuine_intel)
2419 return 0;
2420
2421 if (family != 6)
2422 return 0;
2423
2424 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002425 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
Len Brown005c82d2016-12-01 01:35:38 -05002426 case INTEL_FAM6_XEON_PHI_KNM:
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002427 return 1;
2428 default:
2429 return 0;
2430 }
2431}
Len Brown6fb31432015-06-17 16:23:45 -04002432int has_config_tdp(unsigned int family, unsigned int model)
2433{
2434 if (!genuine_intel)
2435 return 0;
2436
2437 if (family != 6)
2438 return 0;
2439
2440 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002441 case INTEL_FAM6_IVYBRIDGE: /* IVB */
2442 case INTEL_FAM6_HASWELL_CORE: /* HSW */
2443 case INTEL_FAM6_HASWELL_X: /* HSX */
2444 case INTEL_FAM6_HASWELL_ULT: /* HSW */
2445 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
2446 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
2447 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
2448 case INTEL_FAM6_BROADWELL_X: /* BDX */
2449 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
2450 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
2451 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
2452 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
2453 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
2454 case INTEL_FAM6_SKYLAKE_X: /* SKX */
Len Brown6fb31432015-06-17 16:23:45 -04002455
Len Brown869ce692016-06-16 23:22:37 -04002456 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
Len Brown005c82d2016-12-01 01:35:38 -05002457 case INTEL_FAM6_XEON_PHI_KNM:
Len Brown6fb31432015-06-17 16:23:45 -04002458 return 1;
2459 default:
2460 return 0;
2461 }
2462}
2463
Len Brownfcd17212015-03-23 20:29:09 -04002464static void
Colin Ian King1b693172016-03-02 13:50:25 +00002465dump_cstate_pstate_config_info(unsigned int family, unsigned int model)
Len Brownfcd17212015-03-23 20:29:09 -04002466{
2467 if (!do_nhm_platform_info)
2468 return;
2469
2470 dump_nhm_platform_info();
2471
2472 if (has_hsw_turbo_ratio_limit(family, model))
2473 dump_hsw_turbo_ratio_limits();
2474
2475 if (has_ivt_turbo_ratio_limit(family, model))
2476 dump_ivt_turbo_ratio_limits();
2477
2478 if (has_nhm_turbo_ratio_limit(family, model))
2479 dump_nhm_turbo_ratio_limits();
2480
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002481 if (has_knl_turbo_ratio_limit(family, model))
2482 dump_knl_turbo_ratio_limits();
2483
Len Brown6fb31432015-06-17 16:23:45 -04002484 if (has_config_tdp(family, model))
2485 dump_config_tdp();
2486
Len Brownfcd17212015-03-23 20:29:09 -04002487 dump_nhm_cst_cfg();
2488}
2489
Len Brown6574a5d2012-09-21 00:01:31 -04002490
Len Brown889facb2012-11-08 00:48:57 -05002491/*
2492 * print_epb()
2493 * Decode the ENERGY_PERF_BIAS MSR
2494 */
2495int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2496{
2497 unsigned long long msr;
2498 char *epb_string;
2499 int cpu;
2500
2501 if (!has_epb)
2502 return 0;
2503
2504 cpu = t->cpu_id;
2505
2506 /* EPB is per-package */
2507 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2508 return 0;
2509
2510 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05002511 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05002512 return -1;
2513 }
2514
2515 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
2516 return 0;
2517
Len Browne9be7dd2015-05-26 12:19:37 -04002518 switch (msr & 0xF) {
Len Brown889facb2012-11-08 00:48:57 -05002519 case ENERGY_PERF_BIAS_PERFORMANCE:
2520 epb_string = "performance";
2521 break;
2522 case ENERGY_PERF_BIAS_NORMAL:
2523 epb_string = "balanced";
2524 break;
2525 case ENERGY_PERF_BIAS_POWERSAVE:
2526 epb_string = "powersave";
2527 break;
2528 default:
2529 epb_string = "custom";
2530 break;
2531 }
Len Brownb7d8c142016-02-13 23:36:17 -05002532 fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
Len Brown889facb2012-11-08 00:48:57 -05002533
2534 return 0;
2535}
Len Brown7f5c2582015-12-01 01:36:39 -05002536/*
2537 * print_hwp()
2538 * Decode the MSR_HWP_CAPABILITIES
2539 */
2540int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2541{
2542 unsigned long long msr;
2543 int cpu;
2544
2545 if (!has_hwp)
2546 return 0;
2547
2548 cpu = t->cpu_id;
2549
2550 /* MSR_HWP_CAPABILITIES is per-package */
2551 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2552 return 0;
2553
2554 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05002555 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown7f5c2582015-12-01 01:36:39 -05002556 return -1;
2557 }
2558
2559 if (get_msr(cpu, MSR_PM_ENABLE, &msr))
2560 return 0;
2561
Len Brownb7d8c142016-02-13 23:36:17 -05002562 fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05002563 cpu, msr, (msr & (1 << 0)) ? "" : "No-");
2564
2565 /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
2566 if ((msr & (1 << 0)) == 0)
2567 return 0;
2568
2569 if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
2570 return 0;
2571
Len Brownb7d8c142016-02-13 23:36:17 -05002572 fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002573 "(high 0x%x guar 0x%x eff 0x%x low 0x%x)\n",
2574 cpu, msr,
2575 (unsigned int)HWP_HIGHEST_PERF(msr),
2576 (unsigned int)HWP_GUARANTEED_PERF(msr),
2577 (unsigned int)HWP_MOSTEFFICIENT_PERF(msr),
2578 (unsigned int)HWP_LOWEST_PERF(msr));
2579
2580 if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
2581 return 0;
2582
Len Brownb7d8c142016-02-13 23:36:17 -05002583 fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002584 "(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x pkg 0x%x)\n",
2585 cpu, msr,
2586 (unsigned int)(((msr) >> 0) & 0xff),
2587 (unsigned int)(((msr) >> 8) & 0xff),
2588 (unsigned int)(((msr) >> 16) & 0xff),
2589 (unsigned int)(((msr) >> 24) & 0xff),
2590 (unsigned int)(((msr) >> 32) & 0xff3),
2591 (unsigned int)(((msr) >> 42) & 0x1));
2592
2593 if (has_hwp_pkg) {
2594 if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
2595 return 0;
2596
Len Brownb7d8c142016-02-13 23:36:17 -05002597 fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002598 "(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x)\n",
2599 cpu, msr,
2600 (unsigned int)(((msr) >> 0) & 0xff),
2601 (unsigned int)(((msr) >> 8) & 0xff),
2602 (unsigned int)(((msr) >> 16) & 0xff),
2603 (unsigned int)(((msr) >> 24) & 0xff),
2604 (unsigned int)(((msr) >> 32) & 0xff3));
2605 }
2606 if (has_hwp_notify) {
2607 if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
2608 return 0;
2609
Len Brownb7d8c142016-02-13 23:36:17 -05002610 fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002611 "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
2612 cpu, msr,
2613 ((msr) & 0x1) ? "EN" : "Dis",
2614 ((msr) & 0x2) ? "EN" : "Dis");
2615 }
2616 if (get_msr(cpu, MSR_HWP_STATUS, &msr))
2617 return 0;
2618
Len Brownb7d8c142016-02-13 23:36:17 -05002619 fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002620 "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
2621 cpu, msr,
2622 ((msr) & 0x1) ? "" : "No-",
2623 ((msr) & 0x2) ? "" : "No-");
Len Brown889facb2012-11-08 00:48:57 -05002624
2625 return 0;
2626}
2627
Len Brown3a9a9412014-08-15 02:39:52 -04002628/*
2629 * print_perf_limit()
2630 */
2631int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2632{
2633 unsigned long long msr;
2634 int cpu;
2635
2636 cpu = t->cpu_id;
2637
2638 /* per-package */
2639 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2640 return 0;
2641
2642 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05002643 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown3a9a9412014-08-15 02:39:52 -04002644 return -1;
2645 }
2646
2647 if (do_core_perf_limit_reasons) {
2648 get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002649 fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2650 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
Len Browne33cbe82015-03-13 16:30:57 -04002651 (msr & 1 << 15) ? "bit15, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04002652 (msr & 1 << 14) ? "bit14, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04002653 (msr & 1 << 13) ? "Transitions, " : "",
2654 (msr & 1 << 12) ? "MultiCoreTurbo, " : "",
2655 (msr & 1 << 11) ? "PkgPwrL2, " : "",
2656 (msr & 1 << 10) ? "PkgPwrL1, " : "",
2657 (msr & 1 << 9) ? "CorePwr, " : "",
2658 (msr & 1 << 8) ? "Amps, " : "",
2659 (msr & 1 << 6) ? "VR-Therm, " : "",
2660 (msr & 1 << 5) ? "Auto-HWP, " : "",
2661 (msr & 1 << 4) ? "Graphics, " : "",
2662 (msr & 1 << 2) ? "bit2, " : "",
2663 (msr & 1 << 1) ? "ThermStatus, " : "",
2664 (msr & 1 << 0) ? "PROCHOT, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05002665 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 -04002666 (msr & 1 << 31) ? "bit31, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04002667 (msr & 1 << 30) ? "bit30, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04002668 (msr & 1 << 29) ? "Transitions, " : "",
2669 (msr & 1 << 28) ? "MultiCoreTurbo, " : "",
2670 (msr & 1 << 27) ? "PkgPwrL2, " : "",
2671 (msr & 1 << 26) ? "PkgPwrL1, " : "",
2672 (msr & 1 << 25) ? "CorePwr, " : "",
2673 (msr & 1 << 24) ? "Amps, " : "",
2674 (msr & 1 << 22) ? "VR-Therm, " : "",
2675 (msr & 1 << 21) ? "Auto-HWP, " : "",
2676 (msr & 1 << 20) ? "Graphics, " : "",
2677 (msr & 1 << 18) ? "bit18, " : "",
2678 (msr & 1 << 17) ? "ThermStatus, " : "",
2679 (msr & 1 << 16) ? "PROCHOT, " : "");
Len Brown3a9a9412014-08-15 02:39:52 -04002680
2681 }
2682 if (do_gfx_perf_limit_reasons) {
2683 get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002684 fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2685 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
Len Brown3a9a9412014-08-15 02:39:52 -04002686 (msr & 1 << 0) ? "PROCHOT, " : "",
2687 (msr & 1 << 1) ? "ThermStatus, " : "",
2688 (msr & 1 << 4) ? "Graphics, " : "",
2689 (msr & 1 << 6) ? "VR-Therm, " : "",
2690 (msr & 1 << 8) ? "Amps, " : "",
2691 (msr & 1 << 9) ? "GFXPwr, " : "",
2692 (msr & 1 << 10) ? "PkgPwrL1, " : "",
2693 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05002694 fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04002695 (msr & 1 << 16) ? "PROCHOT, " : "",
2696 (msr & 1 << 17) ? "ThermStatus, " : "",
2697 (msr & 1 << 20) ? "Graphics, " : "",
2698 (msr & 1 << 22) ? "VR-Therm, " : "",
2699 (msr & 1 << 24) ? "Amps, " : "",
2700 (msr & 1 << 25) ? "GFXPwr, " : "",
2701 (msr & 1 << 26) ? "PkgPwrL1, " : "",
2702 (msr & 1 << 27) ? "PkgPwrL2, " : "");
2703 }
2704 if (do_ring_perf_limit_reasons) {
2705 get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002706 fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2707 fprintf(outf, " (Active: %s%s%s%s%s%s)",
Len Brown3a9a9412014-08-15 02:39:52 -04002708 (msr & 1 << 0) ? "PROCHOT, " : "",
2709 (msr & 1 << 1) ? "ThermStatus, " : "",
2710 (msr & 1 << 6) ? "VR-Therm, " : "",
2711 (msr & 1 << 8) ? "Amps, " : "",
2712 (msr & 1 << 10) ? "PkgPwrL1, " : "",
2713 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05002714 fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04002715 (msr & 1 << 16) ? "PROCHOT, " : "",
2716 (msr & 1 << 17) ? "ThermStatus, " : "",
2717 (msr & 1 << 22) ? "VR-Therm, " : "",
2718 (msr & 1 << 24) ? "Amps, " : "",
2719 (msr & 1 << 26) ? "PkgPwrL1, " : "",
2720 (msr & 1 << 27) ? "PkgPwrL2, " : "");
2721 }
2722 return 0;
2723}
2724
Len Brown889facb2012-11-08 00:48:57 -05002725#define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
2726#define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
2727
Colin Ian King1b693172016-03-02 13:50:25 +00002728double get_tdp(unsigned int model)
Len Brown144b44b2013-11-09 00:30:16 -05002729{
2730 unsigned long long msr;
2731
2732 if (do_rapl & RAPL_PKG_POWER_INFO)
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002733 if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
Len Brown144b44b2013-11-09 00:30:16 -05002734 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
2735
2736 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002737 case INTEL_FAM6_ATOM_SILVERMONT1:
2738 case INTEL_FAM6_ATOM_SILVERMONT2:
Len Brown144b44b2013-11-09 00:30:16 -05002739 return 30.0;
2740 default:
2741 return 135.0;
2742 }
2743}
2744
Andrey Semin40ee8e32014-12-05 00:07:00 -05002745/*
2746 * rapl_dram_energy_units_probe()
2747 * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
2748 */
2749static double
2750rapl_dram_energy_units_probe(int model, double rapl_energy_units)
2751{
2752 /* only called for genuine_intel, family 6 */
2753
2754 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002755 case INTEL_FAM6_HASWELL_X: /* HSX */
2756 case INTEL_FAM6_BROADWELL_X: /* BDX */
2757 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
2758 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05002759 case INTEL_FAM6_XEON_PHI_KNM:
Andrey Semin40ee8e32014-12-05 00:07:00 -05002760 return (rapl_dram_energy_units = 15.3 / 1000000);
2761 default:
2762 return (rapl_energy_units);
2763 }
2764}
2765
Len Brown144b44b2013-11-09 00:30:16 -05002766
Len Brown889facb2012-11-08 00:48:57 -05002767/*
2768 * rapl_probe()
2769 *
Len Brown144b44b2013-11-09 00:30:16 -05002770 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
Len Brown889facb2012-11-08 00:48:57 -05002771 */
2772void rapl_probe(unsigned int family, unsigned int model)
2773{
2774 unsigned long long msr;
Len Brown144b44b2013-11-09 00:30:16 -05002775 unsigned int time_unit;
Len Brown889facb2012-11-08 00:48:57 -05002776 double tdp;
2777
2778 if (!genuine_intel)
2779 return;
2780
2781 if (family != 6)
2782 return;
2783
2784 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002785 case INTEL_FAM6_SANDYBRIDGE:
2786 case INTEL_FAM6_IVYBRIDGE:
2787 case INTEL_FAM6_HASWELL_CORE: /* HSW */
2788 case INTEL_FAM6_HASWELL_ULT: /* HSW */
2789 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
2790 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
2791 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
Len Brown144b44b2013-11-09 00:30:16 -05002792 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
Len Brown889facb2012-11-08 00:48:57 -05002793 break;
Len Brown869ce692016-06-16 23:22:37 -04002794 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Browne4085d52016-04-06 17:15:56 -04002795 do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO;
2796 break;
Len Brown869ce692016-06-16 23:22:37 -04002797 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
2798 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
2799 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
2800 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Brown0b2bb692015-03-26 00:50:30 -04002801 do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
2802 break;
Len Brown869ce692016-06-16 23:22:37 -04002803 case INTEL_FAM6_HASWELL_X: /* HSX */
2804 case INTEL_FAM6_BROADWELL_X: /* BDX */
2805 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
2806 case INTEL_FAM6_SKYLAKE_X: /* SKX */
2807 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05002808 case INTEL_FAM6_XEON_PHI_KNM:
Len Brown0b2bb692015-03-26 00:50:30 -04002809 do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
Len Browne6f9bb32013-12-03 02:19:19 -05002810 break;
Len Brown869ce692016-06-16 23:22:37 -04002811 case INTEL_FAM6_SANDYBRIDGE_X:
2812 case INTEL_FAM6_IVYBRIDGE_X:
Len Brown0b2bb692015-03-26 00:50:30 -04002813 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 Brown144b44b2013-11-09 00:30:16 -05002814 break;
Len Brown869ce692016-06-16 23:22:37 -04002815 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
2816 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Jacob Pan91484942016-06-16 09:48:20 -07002817 do_rapl = RAPL_PKG | RAPL_CORES;
Len Brown889facb2012-11-08 00:48:57 -05002818 break;
Len Brown869ce692016-06-16 23:22:37 -04002819 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Jacob Pan0f644902016-06-16 09:48:22 -07002820 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;
2821 break;
Len Brown889facb2012-11-08 00:48:57 -05002822 default:
2823 return;
2824 }
2825
2826 /* units on package 0, verify later other packages match */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002827 if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
Len Brown889facb2012-11-08 00:48:57 -05002828 return;
2829
2830 rapl_power_units = 1.0 / (1 << (msr & 0xF));
Len Brown869ce692016-06-16 23:22:37 -04002831 if (model == INTEL_FAM6_ATOM_SILVERMONT1)
Len Brown144b44b2013-11-09 00:30:16 -05002832 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
2833 else
2834 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
Len Brown889facb2012-11-08 00:48:57 -05002835
Andrey Semin40ee8e32014-12-05 00:07:00 -05002836 rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
2837
Len Brown144b44b2013-11-09 00:30:16 -05002838 time_unit = msr >> 16 & 0xF;
2839 if (time_unit == 0)
2840 time_unit = 0xA;
Len Brown889facb2012-11-08 00:48:57 -05002841
Len Brown144b44b2013-11-09 00:30:16 -05002842 rapl_time_units = 1.0 / (1 << (time_unit));
2843
2844 tdp = get_tdp(model);
Len Brown889facb2012-11-08 00:48:57 -05002845
2846 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
Len Brownd8af6f52015-02-10 01:56:38 -05002847 if (debug)
Len Brownb7d8c142016-02-13 23:36:17 -05002848 fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
Len Brown889facb2012-11-08 00:48:57 -05002849
2850 return;
2851}
2852
Colin Ian King1b693172016-03-02 13:50:25 +00002853void perf_limit_reasons_probe(unsigned int family, unsigned int model)
Len Brown3a9a9412014-08-15 02:39:52 -04002854{
2855 if (!genuine_intel)
2856 return;
2857
2858 if (family != 6)
2859 return;
2860
2861 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04002862 case INTEL_FAM6_HASWELL_CORE: /* HSW */
2863 case INTEL_FAM6_HASWELL_ULT: /* HSW */
2864 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
Len Brown3a9a9412014-08-15 02:39:52 -04002865 do_gfx_perf_limit_reasons = 1;
Len Brown869ce692016-06-16 23:22:37 -04002866 case INTEL_FAM6_HASWELL_X: /* HSX */
Len Brown3a9a9412014-08-15 02:39:52 -04002867 do_core_perf_limit_reasons = 1;
2868 do_ring_perf_limit_reasons = 1;
2869 default:
2870 return;
2871 }
2872}
2873
Len Brown889facb2012-11-08 00:48:57 -05002874int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2875{
2876 unsigned long long msr;
2877 unsigned int dts;
2878 int cpu;
2879
2880 if (!(do_dts || do_ptm))
2881 return 0;
2882
2883 cpu = t->cpu_id;
2884
2885 /* DTS is per-core, no need to print for each thread */
Len Brown388e9c82016-12-22 23:57:55 -05002886 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
Len Brown889facb2012-11-08 00:48:57 -05002887 return 0;
2888
2889 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05002890 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05002891 return -1;
2892 }
2893
2894 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
2895 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
2896 return 0;
2897
2898 dts = (msr >> 16) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05002899 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05002900 cpu, msr, tcc_activation_temp - dts);
2901
2902#ifdef THERM_DEBUG
2903 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
2904 return 0;
2905
2906 dts = (msr >> 16) & 0x7F;
2907 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05002908 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05002909 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
2910#endif
2911 }
2912
2913
2914 if (do_dts) {
2915 unsigned int resolution;
2916
2917 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
2918 return 0;
2919
2920 dts = (msr >> 16) & 0x7F;
2921 resolution = (msr >> 27) & 0xF;
Len Brownb7d8c142016-02-13 23:36:17 -05002922 fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
Len Brown889facb2012-11-08 00:48:57 -05002923 cpu, msr, tcc_activation_temp - dts, resolution);
2924
2925#ifdef THERM_DEBUG
2926 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
2927 return 0;
2928
2929 dts = (msr >> 16) & 0x7F;
2930 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05002931 fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05002932 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
2933#endif
2934 }
2935
2936 return 0;
2937}
Len Brown36229892016-02-26 20:51:02 -05002938
Len Brown889facb2012-11-08 00:48:57 -05002939void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
2940{
Len Brownb7d8c142016-02-13 23:36:17 -05002941 fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05002942 cpu, label,
2943 ((msr >> 15) & 1) ? "EN" : "DIS",
2944 ((msr >> 0) & 0x7FFF) * rapl_power_units,
2945 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
2946 (((msr >> 16) & 1) ? "EN" : "DIS"));
2947
2948 return;
2949}
2950
2951int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2952{
2953 unsigned long long msr;
2954 int cpu;
Len Brown889facb2012-11-08 00:48:57 -05002955
2956 if (!do_rapl)
2957 return 0;
2958
2959 /* RAPL counters are per package, so print only for 1st thread/package */
2960 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2961 return 0;
2962
2963 cpu = t->cpu_id;
2964 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05002965 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05002966 return -1;
2967 }
2968
2969 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
2970 return -1;
2971
Len Brownd8af6f52015-02-10 01:56:38 -05002972 if (debug) {
Len Brownb7d8c142016-02-13 23:36:17 -05002973 fprintf(outf, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx "
Len Brown889facb2012-11-08 00:48:57 -05002974 "(%f Watts, %f Joules, %f sec.)\n", cpu, msr,
Len Brown144b44b2013-11-09 00:30:16 -05002975 rapl_power_units, rapl_energy_units, rapl_time_units);
Len Brown889facb2012-11-08 00:48:57 -05002976 }
Len Brown144b44b2013-11-09 00:30:16 -05002977 if (do_rapl & RAPL_PKG_POWER_INFO) {
2978
Len Brown889facb2012-11-08 00:48:57 -05002979 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
2980 return -5;
2981
2982
Len Brownb7d8c142016-02-13 23:36:17 -05002983 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 -05002984 cpu, msr,
2985 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2986 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2987 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2988 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
2989
Len Brown144b44b2013-11-09 00:30:16 -05002990 }
2991 if (do_rapl & RAPL_PKG) {
2992
Len Brown889facb2012-11-08 00:48:57 -05002993 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
2994 return -9;
2995
Len Brownb7d8c142016-02-13 23:36:17 -05002996 fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown889facb2012-11-08 00:48:57 -05002997 cpu, msr, (msr >> 63) & 1 ? "": "UN");
2998
2999 print_power_limit_msr(cpu, msr, "PKG Limit #1");
Len Brownb7d8c142016-02-13 23:36:17 -05003000 fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05003001 cpu,
3002 ((msr >> 47) & 1) ? "EN" : "DIS",
3003 ((msr >> 32) & 0x7FFF) * rapl_power_units,
3004 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
3005 ((msr >> 48) & 1) ? "EN" : "DIS");
3006 }
3007
Len Brown0b2bb692015-03-26 00:50:30 -04003008 if (do_rapl & RAPL_DRAM_POWER_INFO) {
Len Brown889facb2012-11-08 00:48:57 -05003009 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
3010 return -6;
3011
Len Brownb7d8c142016-02-13 23:36:17 -05003012 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 -05003013 cpu, msr,
3014 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3015 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3016 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3017 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
Len Brown0b2bb692015-03-26 00:50:30 -04003018 }
3019 if (do_rapl & RAPL_DRAM) {
Len Brown889facb2012-11-08 00:48:57 -05003020 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
3021 return -9;
Len Brownb7d8c142016-02-13 23:36:17 -05003022 fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown889facb2012-11-08 00:48:57 -05003023 cpu, msr, (msr >> 31) & 1 ? "": "UN");
3024
3025 print_power_limit_msr(cpu, msr, "DRAM Limit");
3026 }
Len Brown144b44b2013-11-09 00:30:16 -05003027 if (do_rapl & RAPL_CORE_POLICY) {
Len Brownd8af6f52015-02-10 01:56:38 -05003028 if (debug) {
Len Brown889facb2012-11-08 00:48:57 -05003029 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
3030 return -7;
3031
Len Brownb7d8c142016-02-13 23:36:17 -05003032 fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown144b44b2013-11-09 00:30:16 -05003033 }
3034 }
Jacob Pan91484942016-06-16 09:48:20 -07003035 if (do_rapl & RAPL_CORES_POWER_LIMIT) {
Len Brownd8af6f52015-02-10 01:56:38 -05003036 if (debug) {
Len Brown889facb2012-11-08 00:48:57 -05003037 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
3038 return -9;
Len Brownb7d8c142016-02-13 23:36:17 -05003039 fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown889facb2012-11-08 00:48:57 -05003040 cpu, msr, (msr >> 31) & 1 ? "": "UN");
3041 print_power_limit_msr(cpu, msr, "Cores Limit");
3042 }
3043 }
3044 if (do_rapl & RAPL_GFX) {
Len Brownd8af6f52015-02-10 01:56:38 -05003045 if (debug) {
Len Brown889facb2012-11-08 00:48:57 -05003046 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
3047 return -8;
3048
Len Brownb7d8c142016-02-13 23:36:17 -05003049 fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown889facb2012-11-08 00:48:57 -05003050
3051 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
3052 return -9;
Len Brownb7d8c142016-02-13 23:36:17 -05003053 fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown889facb2012-11-08 00:48:57 -05003054 cpu, msr, (msr >> 31) & 1 ? "": "UN");
3055 print_power_limit_msr(cpu, msr, "GFX Limit");
3056 }
3057 }
3058 return 0;
3059}
3060
Len Brownd7899442015-01-23 00:12:33 -05003061/*
3062 * SNB adds support for additional MSRs:
3063 *
3064 * MSR_PKG_C7_RESIDENCY 0x000003fa
3065 * MSR_CORE_C7_RESIDENCY 0x000003fe
3066 * MSR_PKG_C2_RESIDENCY 0x0000060d
3067 */
Len Brown103a8fe2010-10-22 23:53:03 -04003068
Len Brownd7899442015-01-23 00:12:33 -05003069int has_snb_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04003070{
3071 if (!genuine_intel)
3072 return 0;
3073
3074 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003075 case INTEL_FAM6_SANDYBRIDGE:
3076 case INTEL_FAM6_SANDYBRIDGE_X:
3077 case INTEL_FAM6_IVYBRIDGE: /* IVB */
3078 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
3079 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3080 case INTEL_FAM6_HASWELL_X: /* HSW */
3081 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3082 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3083 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3084 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
3085 case INTEL_FAM6_BROADWELL_X: /* BDX */
3086 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3087 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3088 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3089 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3090 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
3091 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3092 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Xiaolong Wang5bbac262016-09-30 17:53:40 +08003093 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Brown103a8fe2010-10-22 23:53:03 -04003094 return 1;
3095 }
3096 return 0;
3097}
3098
Len Brownd7899442015-01-23 00:12:33 -05003099/*
3100 * HSW adds support for additional MSRs:
3101 *
Len Brown5a634262016-04-06 17:15:55 -04003102 * MSR_PKG_C8_RESIDENCY 0x00000630
3103 * MSR_PKG_C9_RESIDENCY 0x00000631
3104 * MSR_PKG_C10_RESIDENCY 0x00000632
3105 *
3106 * MSR_PKGC8_IRTL 0x00000633
3107 * MSR_PKGC9_IRTL 0x00000634
3108 * MSR_PKGC10_IRTL 0x00000635
3109 *
Len Brownd7899442015-01-23 00:12:33 -05003110 */
3111int has_hsw_msrs(unsigned int family, unsigned int model)
Kristen Carlson Accardica587102012-11-21 05:22:43 -08003112{
3113 if (!genuine_intel)
3114 return 0;
3115
3116 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003117 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3118 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3119 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3120 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3121 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3122 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
3123 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Kristen Carlson Accardica587102012-11-21 05:22:43 -08003124 return 1;
3125 }
3126 return 0;
3127}
3128
Len Brown0b2bb692015-03-26 00:50:30 -04003129/*
3130 * SKL adds support for additional MSRS:
3131 *
3132 * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658
3133 * MSR_PKG_ANY_CORE_C0_RES 0x00000659
3134 * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A
3135 * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B
3136 */
3137int has_skl_msrs(unsigned int family, unsigned int model)
3138{
3139 if (!genuine_intel)
3140 return 0;
3141
3142 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003143 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3144 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3145 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3146 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Brown0b2bb692015-03-26 00:50:30 -04003147 return 1;
3148 }
3149 return 0;
3150}
3151
3152
Kristen Carlson Accardica587102012-11-21 05:22:43 -08003153
Len Brown144b44b2013-11-09 00:30:16 -05003154int is_slm(unsigned int family, unsigned int model)
3155{
3156 if (!genuine_intel)
3157 return 0;
3158 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003159 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
3160 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
Len Brown144b44b2013-11-09 00:30:16 -05003161 return 1;
3162 }
3163 return 0;
3164}
3165
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003166int is_knl(unsigned int family, unsigned int model)
3167{
3168 if (!genuine_intel)
3169 return 0;
3170 switch (model) {
Len Brown869ce692016-06-16 23:22:37 -04003171 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
Len Brown005c82d2016-12-01 01:35:38 -05003172 case INTEL_FAM6_XEON_PHI_KNM:
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003173 return 1;
3174 }
3175 return 0;
3176}
3177
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02003178unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
3179{
3180 if (is_knl(family, model))
3181 return 1024;
3182 return 1;
3183}
3184
Len Brown144b44b2013-11-09 00:30:16 -05003185#define SLM_BCLK_FREQS 5
3186double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
3187
3188double slm_bclk(void)
3189{
3190 unsigned long long msr = 3;
3191 unsigned int i;
3192 double freq;
3193
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003194 if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
Len Brownb7d8c142016-02-13 23:36:17 -05003195 fprintf(outf, "SLM BCLK: unknown\n");
Len Brown144b44b2013-11-09 00:30:16 -05003196
3197 i = msr & 0xf;
3198 if (i >= SLM_BCLK_FREQS) {
Len Brownb7d8c142016-02-13 23:36:17 -05003199 fprintf(outf, "SLM BCLK[%d] invalid\n", i);
Colin Ian King0a91e552016-04-25 13:03:15 +01003200 i = 3;
Len Brown144b44b2013-11-09 00:30:16 -05003201 }
3202 freq = slm_freq_table[i];
3203
Len Brownb7d8c142016-02-13 23:36:17 -05003204 fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
Len Brown144b44b2013-11-09 00:30:16 -05003205
3206 return freq;
3207}
3208
Len Brown103a8fe2010-10-22 23:53:03 -04003209double discover_bclk(unsigned int family, unsigned int model)
3210{
Chrzaniuk, Hubert121b48b2016-02-10 16:35:17 +01003211 if (has_snb_msrs(family, model) || is_knl(family, model))
Len Brown103a8fe2010-10-22 23:53:03 -04003212 return 100.00;
Len Brown144b44b2013-11-09 00:30:16 -05003213 else if (is_slm(family, model))
3214 return slm_bclk();
Len Brown103a8fe2010-10-22 23:53:03 -04003215 else
3216 return 133.33;
3217}
3218
Len Brown889facb2012-11-08 00:48:57 -05003219/*
3220 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
3221 * the Thermal Control Circuit (TCC) activates.
3222 * This is usually equal to tjMax.
3223 *
3224 * Older processors do not have this MSR, so there we guess,
3225 * but also allow cmdline over-ride with -T.
3226 *
3227 * Several MSR temperature values are in units of degrees-C
3228 * below this value, including the Digital Thermal Sensor (DTS),
3229 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
3230 */
3231int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3232{
3233 unsigned long long msr;
3234 unsigned int target_c_local;
3235 int cpu;
3236
3237 /* tcc_activation_temp is used only for dts or ptm */
3238 if (!(do_dts || do_ptm))
3239 return 0;
3240
3241 /* this is a per-package concept */
3242 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3243 return 0;
3244
3245 cpu = t->cpu_id;
3246 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05003247 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05003248 return -1;
3249 }
3250
3251 if (tcc_activation_temp_override != 0) {
3252 tcc_activation_temp = tcc_activation_temp_override;
Len Brownb7d8c142016-02-13 23:36:17 -05003253 fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003254 cpu, tcc_activation_temp);
3255 return 0;
3256 }
3257
3258 /* Temperature Target MSR is Nehalem and newer only */
Len Brownd7899442015-01-23 00:12:33 -05003259 if (!do_nhm_platform_info)
Len Brown889facb2012-11-08 00:48:57 -05003260 goto guess;
3261
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003262 if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
Len Brown889facb2012-11-08 00:48:57 -05003263 goto guess;
3264
Jean Delvare34821242014-05-01 11:40:19 +02003265 target_c_local = (msr >> 16) & 0xFF;
Len Brown889facb2012-11-08 00:48:57 -05003266
Len Brownd8af6f52015-02-10 01:56:38 -05003267 if (debug)
Len Brownb7d8c142016-02-13 23:36:17 -05003268 fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05003269 cpu, msr, target_c_local);
3270
Jean Delvare34821242014-05-01 11:40:19 +02003271 if (!target_c_local)
Len Brown889facb2012-11-08 00:48:57 -05003272 goto guess;
3273
3274 tcc_activation_temp = target_c_local;
3275
3276 return 0;
3277
3278guess:
3279 tcc_activation_temp = TJMAX_DEFAULT;
Len Brownb7d8c142016-02-13 23:36:17 -05003280 fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
Len Brown889facb2012-11-08 00:48:57 -05003281 cpu, tcc_activation_temp);
3282
3283 return 0;
3284}
Len Brown69807a62015-11-21 12:22:47 -05003285
Len Brownaa8d8cc2016-03-11 13:26:03 -05003286void decode_feature_control_msr(void)
3287{
3288 unsigned long long msr;
3289
3290 if (!get_msr(base_cpu, MSR_IA32_FEATURE_CONTROL, &msr))
3291 fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
3292 base_cpu, msr,
3293 msr & FEATURE_CONTROL_LOCKED ? "" : "UN-",
3294 msr & (1 << 18) ? "SGX" : "");
3295}
3296
Len Brown69807a62015-11-21 12:22:47 -05003297void decode_misc_enable_msr(void)
3298{
3299 unsigned long long msr;
3300
3301 if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
Len Brownb7d8c142016-02-13 23:36:17 -05003302 fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%s %s %s)\n",
Len Brown69807a62015-11-21 12:22:47 -05003303 base_cpu, msr,
3304 msr & (1 << 3) ? "TCC" : "",
3305 msr & (1 << 16) ? "EIST" : "",
3306 msr & (1 << 18) ? "MONITOR" : "");
3307}
3308
Len Brownf0057312015-12-03 01:35:36 -05003309/*
3310 * Decode MSR_MISC_PWR_MGMT
3311 *
3312 * Decode the bits according to the Nehalem documentation
3313 * bit[0] seems to continue to have same meaning going forward
3314 * bit[1] less so...
3315 */
3316void decode_misc_pwr_mgmt_msr(void)
3317{
3318 unsigned long long msr;
3319
3320 if (!do_nhm_platform_info)
3321 return;
3322
3323 if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
Srinivas Pandruvadaddadb8a2016-11-11 14:29:48 -08003324 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 -05003325 base_cpu, msr,
3326 msr & (1 << 0) ? "DIS" : "EN",
Srinivas Pandruvadaddadb8a2016-11-11 14:29:48 -08003327 msr & (1 << 1) ? "EN" : "DIS",
3328 msr & (1 << 8) ? "EN" : "DIS");
Len Brownf0057312015-12-03 01:35:36 -05003329}
Len Brown7f5c2582015-12-01 01:36:39 -05003330
Len Brownfcd17212015-03-23 20:29:09 -04003331void process_cpuid()
Len Brown103a8fe2010-10-22 23:53:03 -04003332{
Len Brown61a87ba2015-11-23 02:30:51 -05003333 unsigned int eax, ebx, ecx, edx, max_level, max_extended_level;
Len Brown103a8fe2010-10-22 23:53:03 -04003334 unsigned int fms, family, model, stepping;
3335
3336 eax = ebx = ecx = edx = 0;
3337
Len Brown5aea2f72016-03-13 03:14:35 -04003338 __cpuid(0, max_level, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04003339
3340 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
3341 genuine_intel = 1;
3342
Len Brownd8af6f52015-02-10 01:56:38 -05003343 if (debug)
Len Brownb7d8c142016-02-13 23:36:17 -05003344 fprintf(outf, "CPUID(0): %.4s%.4s%.4s ",
Len Brown103a8fe2010-10-22 23:53:03 -04003345 (char *)&ebx, (char *)&edx, (char *)&ecx);
3346
Len Brown5aea2f72016-03-13 03:14:35 -04003347 __cpuid(1, fms, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04003348 family = (fms >> 8) & 0xf;
3349 model = (fms >> 4) & 0xf;
3350 stepping = fms & 0xf;
3351 if (family == 6 || family == 0xf)
3352 model += ((fms >> 16) & 0xf) << 4;
3353
Len Brown69807a62015-11-21 12:22:47 -05003354 if (debug) {
Len Brownb7d8c142016-02-13 23:36:17 -05003355 fprintf(outf, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
Len Brown103a8fe2010-10-22 23:53:03 -04003356 max_level, family, model, stepping, family, model, stepping);
Len Brownaa8d8cc2016-03-11 13:26:03 -05003357 fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s\n",
Len Brown69807a62015-11-21 12:22:47 -05003358 ecx & (1 << 0) ? "SSE3" : "-",
3359 ecx & (1 << 3) ? "MONITOR" : "-",
Len Brownaa8d8cc2016-03-11 13:26:03 -05003360 ecx & (1 << 6) ? "SMX" : "-",
Len Brown69807a62015-11-21 12:22:47 -05003361 ecx & (1 << 7) ? "EIST" : "-",
3362 ecx & (1 << 8) ? "TM2" : "-",
3363 edx & (1 << 4) ? "TSC" : "-",
3364 edx & (1 << 5) ? "MSR" : "-",
3365 edx & (1 << 22) ? "ACPI-TM" : "-",
3366 edx & (1 << 29) ? "TM" : "-");
3367 }
Len Brown103a8fe2010-10-22 23:53:03 -04003368
Josh Triplettb2c95d92013-08-20 17:20:18 -07003369 if (!(edx & (1 << 5)))
3370 errx(1, "CPUID: no MSR");
Len Brown103a8fe2010-10-22 23:53:03 -04003371
3372 /*
3373 * check max extended function levels of CPUID.
3374 * This is needed to check for invariant TSC.
3375 * This check is valid for both Intel and AMD.
3376 */
3377 ebx = ecx = edx = 0;
Len Brown5aea2f72016-03-13 03:14:35 -04003378 __cpuid(0x80000000, max_extended_level, ebx, ecx, edx);
Len Brown103a8fe2010-10-22 23:53:03 -04003379
Len Brown61a87ba2015-11-23 02:30:51 -05003380 if (max_extended_level >= 0x80000007) {
Len Brown103a8fe2010-10-22 23:53:03 -04003381
Len Brownd7899442015-01-23 00:12:33 -05003382 /*
3383 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
3384 * this check is valid for both Intel and AMD
3385 */
Len Brown5aea2f72016-03-13 03:14:35 -04003386 __cpuid(0x80000007, eax, ebx, ecx, edx);
Len Brownd7899442015-01-23 00:12:33 -05003387 has_invariant_tsc = edx & (1 << 8);
3388 }
Len Brown103a8fe2010-10-22 23:53:03 -04003389
3390 /*
3391 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
3392 * this check is valid for both Intel and AMD
3393 */
3394
Len Brown5aea2f72016-03-13 03:14:35 -04003395 __cpuid(0x6, eax, ebx, ecx, edx);
Thomas Renninger8209e052011-01-21 15:11:19 +01003396 has_aperf = ecx & (1 << 0);
Len Brown889facb2012-11-08 00:48:57 -05003397 do_dts = eax & (1 << 0);
3398 do_ptm = eax & (1 << 6);
Len Brown7f5c2582015-12-01 01:36:39 -05003399 has_hwp = eax & (1 << 7);
3400 has_hwp_notify = eax & (1 << 8);
3401 has_hwp_activity_window = eax & (1 << 9);
3402 has_hwp_epp = eax & (1 << 10);
3403 has_hwp_pkg = eax & (1 << 11);
Len Brown889facb2012-11-08 00:48:57 -05003404 has_epb = ecx & (1 << 3);
3405
Len Brownd8af6f52015-02-10 01:56:38 -05003406 if (debug)
Len Brownb7d8c142016-02-13 23:36:17 -05003407 fprintf(outf, "CPUID(6): %sAPERF, %sDTS, %sPTM, %sHWP, "
Len Brown7f5c2582015-12-01 01:36:39 -05003408 "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
3409 has_aperf ? "" : "No-",
3410 do_dts ? "" : "No-",
3411 do_ptm ? "" : "No-",
3412 has_hwp ? "" : "No-",
3413 has_hwp_notify ? "" : "No-",
3414 has_hwp_activity_window ? "" : "No-",
3415 has_hwp_epp ? "" : "No-",
3416 has_hwp_pkg ? "" : "No-",
3417 has_epb ? "" : "No-");
Len Brown103a8fe2010-10-22 23:53:03 -04003418
Len Brown69807a62015-11-21 12:22:47 -05003419 if (debug)
3420 decode_misc_enable_msr();
3421
Len Brown8ae72252016-04-06 17:15:54 -04003422 if (max_level >= 0x7 && debug) {
Len Brownaa8d8cc2016-03-11 13:26:03 -05003423 int has_sgx;
3424
3425 ecx = 0;
3426
3427 __cpuid_count(0x7, 0, eax, ebx, ecx, edx);
3428
3429 has_sgx = ebx & (1 << 2);
3430 fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-");
3431
3432 if (has_sgx)
3433 decode_feature_control_msr();
3434 }
3435
Len Brown61a87ba2015-11-23 02:30:51 -05003436 if (max_level >= 0x15) {
Len Brown8a5bdf42015-04-01 21:02:57 -04003437 unsigned int eax_crystal;
3438 unsigned int ebx_tsc;
3439
3440 /*
3441 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
3442 */
3443 eax_crystal = ebx_tsc = crystal_hz = edx = 0;
Len Brown5aea2f72016-03-13 03:14:35 -04003444 __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx);
Len Brown8a5bdf42015-04-01 21:02:57 -04003445
3446 if (ebx_tsc != 0) {
3447
3448 if (debug && (ebx != 0))
Len Brownb7d8c142016-02-13 23:36:17 -05003449 fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04003450 eax_crystal, ebx_tsc, crystal_hz);
3451
3452 if (crystal_hz == 0)
3453 switch(model) {
Len Brown869ce692016-06-16 23:22:37 -04003454 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3455 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3456 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3457 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
Len Browne8efbc82016-04-06 17:15:57 -04003458 crystal_hz = 24000000; /* 24.0 MHz */
3459 break;
Len Brown869ce692016-06-16 23:22:37 -04003460 case INTEL_FAM6_SKYLAKE_X: /* SKX */
Len Brown7268d402016-12-01 23:10:39 -05003461 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
Len Brownec53e592016-04-06 17:15:58 -04003462 crystal_hz = 25000000; /* 25.0 MHz */
3463 break;
Len Brown869ce692016-06-16 23:22:37 -04003464 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
Len Browne8efbc82016-04-06 17:15:57 -04003465 crystal_hz = 19200000; /* 19.2 MHz */
Len Brown8a5bdf42015-04-01 21:02:57 -04003466 break;
3467 default:
3468 crystal_hz = 0;
3469 }
3470
3471 if (crystal_hz) {
3472 tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
3473 if (debug)
Len Brownb7d8c142016-02-13 23:36:17 -05003474 fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04003475 tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal);
3476 }
3477 }
3478 }
Len Brown61a87ba2015-11-23 02:30:51 -05003479 if (max_level >= 0x16) {
3480 unsigned int base_mhz, max_mhz, bus_mhz, edx;
3481
3482 /*
3483 * CPUID 16H Base MHz, Max MHz, Bus MHz
3484 */
3485 base_mhz = max_mhz = bus_mhz = edx = 0;
3486
Len Brown5aea2f72016-03-13 03:14:35 -04003487 __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx);
Len Brown61a87ba2015-11-23 02:30:51 -05003488 if (debug)
Len Brownb7d8c142016-02-13 23:36:17 -05003489 fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
Len Brown61a87ba2015-11-23 02:30:51 -05003490 base_mhz, max_mhz, bus_mhz);
3491 }
Len Brown8a5bdf42015-04-01 21:02:57 -04003492
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02003493 if (has_aperf)
3494 aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
3495
Len Brownee7e38e2015-02-09 23:39:45 -05003496 do_nhm_platform_info = do_nhm_cstates = do_smi = probe_nhm_msrs(family, model);
Len Brownd7899442015-01-23 00:12:33 -05003497 do_snb_cstates = has_snb_msrs(family, model);
Len Brown5a634262016-04-06 17:15:55 -04003498 do_irtl_snb = has_snb_msrs(family, model);
Len Brownee7e38e2015-02-09 23:39:45 -05003499 do_pc2 = do_snb_cstates && (pkg_cstate_limit >= PCL__2);
3500 do_pc3 = (pkg_cstate_limit >= PCL__3);
3501 do_pc6 = (pkg_cstate_limit >= PCL__6);
3502 do_pc7 = do_snb_cstates && (pkg_cstate_limit >= PCL__7);
Len Brownd7899442015-01-23 00:12:33 -05003503 do_c8_c9_c10 = has_hsw_msrs(family, model);
Len Brown5a634262016-04-06 17:15:55 -04003504 do_irtl_hsw = has_hsw_msrs(family, model);
Len Brown0b2bb692015-03-26 00:50:30 -04003505 do_skl_residency = has_skl_msrs(family, model);
Len Brown144b44b2013-11-09 00:30:16 -05003506 do_slm_cstates = is_slm(family, model);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07003507 do_knl_cstates = is_knl(family, model);
Len Brown103a8fe2010-10-22 23:53:03 -04003508
Len Brownf0057312015-12-03 01:35:36 -05003509 if (debug)
3510 decode_misc_pwr_mgmt_msr();
3511
Len Brown889facb2012-11-08 00:48:57 -05003512 rapl_probe(family, model);
Len Brown3a9a9412014-08-15 02:39:52 -04003513 perf_limit_reasons_probe(family, model);
Len Brown889facb2012-11-08 00:48:57 -05003514
Len Brownfcd17212015-03-23 20:29:09 -04003515 if (debug)
Colin Ian King1b693172016-03-02 13:50:25 +00003516 dump_cstate_pstate_config_info(family, model);
Len Brownfcd17212015-03-23 20:29:09 -04003517
Len Browna2b7b742015-09-26 00:12:38 -04003518 if (has_skl_msrs(family, model))
3519 calculate_tsc_tweak();
3520
Len Brownfdf676e2016-02-27 01:28:12 -05003521 do_gfx_rc6_ms = !access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK);
3522
Len Brown27d47352016-02-27 00:37:54 -05003523 do_gfx_mhz = !access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK);
3524
Len Brown889facb2012-11-08 00:48:57 -05003525 return;
Len Brown103a8fe2010-10-22 23:53:03 -04003526}
3527
Len Brownd8af6f52015-02-10 01:56:38 -05003528void help()
Len Brown103a8fe2010-10-22 23:53:03 -04003529{
Len Brownb7d8c142016-02-13 23:36:17 -05003530 fprintf(outf,
Len Brownd8af6f52015-02-10 01:56:38 -05003531 "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
3532 "\n"
3533 "Turbostat forks the specified COMMAND and prints statistics\n"
3534 "when COMMAND completes.\n"
3535 "If no COMMAND is specified, turbostat wakes every 5-seconds\n"
3536 "to print statistics, until interrupted.\n"
Len Brown388e9c82016-12-22 23:57:55 -05003537 "--add add a counter\n"
3538 " eg. --add msr0x10,u64,cpu,delta,MY_TSC\n"
Len Brownd8af6f52015-02-10 01:56:38 -05003539 "--debug run in \"debug\" mode\n"
3540 "--interval sec Override default 5-second measurement interval\n"
3541 "--help print this help message\n"
Len Brownb7d8c142016-02-13 23:36:17 -05003542 "--out file create or truncate \"file\" for all output\n"
Len Brownd8af6f52015-02-10 01:56:38 -05003543 "--version print version information\n"
3544 "\n"
3545 "For more help, run \"man turbostat\"\n");
Len Brown103a8fe2010-10-22 23:53:03 -04003546}
3547
3548
3549/*
3550 * in /dev/cpu/ return success for names that are numbers
3551 * ie. filter out ".", "..", "microcode".
3552 */
3553int dir_filter(const struct dirent *dirp)
3554{
3555 if (isdigit(dirp->d_name[0]))
3556 return 1;
3557 else
3558 return 0;
3559}
3560
3561int open_dev_cpu_msr(int dummy1)
3562{
3563 return 0;
3564}
3565
Len Brownc98d5d92012-06-04 00:56:40 -04003566void topology_probe()
3567{
3568 int i;
3569 int max_core_id = 0;
3570 int max_package_id = 0;
3571 int max_siblings = 0;
3572 struct cpu_topology {
3573 int core_id;
3574 int physical_package_id;
3575 } *cpus;
3576
3577 /* Initialize num_cpus, max_cpu_num */
3578 topo.num_cpus = 0;
3579 topo.max_cpu_num = 0;
3580 for_all_proc_cpus(count_cpus);
3581 if (!summary_only && topo.num_cpus > 1)
3582 show_cpu = 1;
3583
Len Brownd8af6f52015-02-10 01:56:38 -05003584 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05003585 fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
Len Brownc98d5d92012-06-04 00:56:40 -04003586
3587 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
Josh Triplettb2c95d92013-08-20 17:20:18 -07003588 if (cpus == NULL)
3589 err(1, "calloc cpus");
Len Brownc98d5d92012-06-04 00:56:40 -04003590
3591 /*
3592 * Allocate and initialize cpu_present_set
3593 */
3594 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07003595 if (cpu_present_set == NULL)
3596 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04003597 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
3598 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
3599 for_all_proc_cpus(mark_cpu_present);
3600
3601 /*
3602 * Allocate and initialize cpu_affinity_set
3603 */
3604 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07003605 if (cpu_affinity_set == NULL)
3606 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04003607 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
3608 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
3609
3610
3611 /*
3612 * For online cpus
3613 * find max_core_id, max_package_id
3614 */
3615 for (i = 0; i <= topo.max_cpu_num; ++i) {
3616 int siblings;
3617
3618 if (cpu_is_not_present(i)) {
Len Brownd8af6f52015-02-10 01:56:38 -05003619 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05003620 fprintf(outf, "cpu%d NOT PRESENT\n", i);
Len Brownc98d5d92012-06-04 00:56:40 -04003621 continue;
3622 }
3623 cpus[i].core_id = get_core_id(i);
3624 if (cpus[i].core_id > max_core_id)
3625 max_core_id = cpus[i].core_id;
3626
3627 cpus[i].physical_package_id = get_physical_package_id(i);
3628 if (cpus[i].physical_package_id > max_package_id)
3629 max_package_id = cpus[i].physical_package_id;
3630
3631 siblings = get_num_ht_siblings(i);
3632 if (siblings > max_siblings)
3633 max_siblings = siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05003634 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05003635 fprintf(outf, "cpu %d pkg %d core %d\n",
Len Brownc98d5d92012-06-04 00:56:40 -04003636 i, cpus[i].physical_package_id, cpus[i].core_id);
3637 }
3638 topo.num_cores_per_pkg = max_core_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05003639 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05003640 fprintf(outf, "max_core_id %d, sizing for %d cores per package\n",
Len Brownc98d5d92012-06-04 00:56:40 -04003641 max_core_id, topo.num_cores_per_pkg);
Len Brown1cc21f72015-02-23 00:34:57 -05003642 if (debug && !summary_only && topo.num_cores_per_pkg > 1)
Len Brownc98d5d92012-06-04 00:56:40 -04003643 show_core = 1;
3644
3645 topo.num_packages = max_package_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05003646 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05003647 fprintf(outf, "max_package_id %d, sizing for %d packages\n",
Len Brownc98d5d92012-06-04 00:56:40 -04003648 max_package_id, topo.num_packages);
Len Brown1cc21f72015-02-23 00:34:57 -05003649 if (debug && !summary_only && topo.num_packages > 1)
Len Brownc98d5d92012-06-04 00:56:40 -04003650 show_pkg = 1;
3651
3652 topo.num_threads_per_core = max_siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05003653 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05003654 fprintf(outf, "max_siblings %d\n", max_siblings);
Len Brownc98d5d92012-06-04 00:56:40 -04003655
3656 free(cpus);
3657}
3658
3659void
3660allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
3661{
3662 int i;
3663
3664 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
Len Brown388e9c82016-12-22 23:57:55 -05003665 topo.num_packages, sizeof(struct thread_data) + sys.thread_counter_bytes);
Len Brownc98d5d92012-06-04 00:56:40 -04003666 if (*t == NULL)
3667 goto error;
3668
3669 for (i = 0; i < topo.num_threads_per_core *
3670 topo.num_cores_per_pkg * topo.num_packages; i++)
3671 (*t)[i].cpu_id = -1;
3672
3673 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
Len Brown388e9c82016-12-22 23:57:55 -05003674 sizeof(struct core_data) + sys.core_counter_bytes);
Len Brownc98d5d92012-06-04 00:56:40 -04003675 if (*c == NULL)
3676 goto error;
3677
3678 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
3679 (*c)[i].core_id = -1;
3680
Len Brown388e9c82016-12-22 23:57:55 -05003681 *p = calloc(topo.num_packages, sizeof(struct pkg_data) + sys.package_counter_bytes);
Len Brownc98d5d92012-06-04 00:56:40 -04003682 if (*p == NULL)
3683 goto error;
3684
3685 for (i = 0; i < topo.num_packages; i++)
3686 (*p)[i].package_id = i;
3687
3688 return;
3689error:
Josh Triplettb2c95d92013-08-20 17:20:18 -07003690 err(1, "calloc counters");
Len Brownc98d5d92012-06-04 00:56:40 -04003691}
3692/*
3693 * init_counter()
3694 *
3695 * set cpu_id, core_num, pkg_num
3696 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
3697 *
3698 * increment topo.num_cores when 1st core in pkg seen
3699 */
3700void init_counter(struct thread_data *thread_base, struct core_data *core_base,
3701 struct pkg_data *pkg_base, int thread_num, int core_num,
3702 int pkg_num, int cpu_id)
3703{
3704 struct thread_data *t;
3705 struct core_data *c;
3706 struct pkg_data *p;
3707
3708 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
3709 c = GET_CORE(core_base, core_num, pkg_num);
3710 p = GET_PKG(pkg_base, pkg_num);
3711
3712 t->cpu_id = cpu_id;
3713 if (thread_num == 0) {
3714 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
3715 if (cpu_is_first_core_in_package(cpu_id))
3716 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
3717 }
3718
3719 c->core_id = core_num;
3720 p->package_id = pkg_num;
3721}
3722
3723
3724int initialize_counters(int cpu_id)
3725{
3726 int my_thread_id, my_core_id, my_package_id;
3727
3728 my_package_id = get_physical_package_id(cpu_id);
3729 my_core_id = get_core_id(cpu_id);
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07003730 my_thread_id = get_cpu_position_in_core(cpu_id);
3731 if (!my_thread_id)
Len Brownc98d5d92012-06-04 00:56:40 -04003732 topo.num_cores++;
Len Brownc98d5d92012-06-04 00:56:40 -04003733
3734 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
3735 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
3736 return 0;
3737}
3738
3739void allocate_output_buffer()
3740{
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02003741 output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
Len Brownc98d5d92012-06-04 00:56:40 -04003742 outp = output_buffer;
Josh Triplettb2c95d92013-08-20 17:20:18 -07003743 if (outp == NULL)
3744 err(-1, "calloc output buffer");
Len Brownc98d5d92012-06-04 00:56:40 -04003745}
Len Brown36229892016-02-26 20:51:02 -05003746void allocate_fd_percpu(void)
3747{
Mika Westerberg01a67ad2016-04-22 11:13:23 +03003748 fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
Len Brown36229892016-02-26 20:51:02 -05003749 if (fd_percpu == NULL)
3750 err(-1, "calloc fd_percpu");
3751}
Len Brown562a2d32016-02-26 23:48:05 -05003752void allocate_irq_buffers(void)
3753{
3754 irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int));
3755 if (irq_column_2_cpu == NULL)
3756 err(-1, "calloc %d", topo.num_cpus);
Len Brownc98d5d92012-06-04 00:56:40 -04003757
Mika Westerberg01a67ad2016-04-22 11:13:23 +03003758 irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int));
Len Brown562a2d32016-02-26 23:48:05 -05003759 if (irqs_per_cpu == NULL)
Mika Westerberg01a67ad2016-04-22 11:13:23 +03003760 err(-1, "calloc %d", topo.max_cpu_num + 1);
Len Brown562a2d32016-02-26 23:48:05 -05003761}
Len Brownc98d5d92012-06-04 00:56:40 -04003762void setup_all_buffers(void)
3763{
3764 topology_probe();
Len Brown562a2d32016-02-26 23:48:05 -05003765 allocate_irq_buffers();
Len Brown36229892016-02-26 20:51:02 -05003766 allocate_fd_percpu();
Len Brownc98d5d92012-06-04 00:56:40 -04003767 allocate_counters(&thread_even, &core_even, &package_even);
3768 allocate_counters(&thread_odd, &core_odd, &package_odd);
3769 allocate_output_buffer();
3770 for_all_proc_cpus(initialize_counters);
3771}
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02003772
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003773void set_base_cpu(void)
3774{
3775 base_cpu = sched_getcpu();
3776 if (base_cpu < 0)
3777 err(-ENODEV, "No valid cpus found");
3778
3779 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05003780 fprintf(outf, "base_cpu = %d\n", base_cpu);
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003781}
3782
Len Brown103a8fe2010-10-22 23:53:03 -04003783void turbostat_init()
3784{
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003785 setup_all_buffers();
3786 set_base_cpu();
Len Brown103a8fe2010-10-22 23:53:03 -04003787 check_dev_msr();
Len Brown98481e72014-08-15 00:36:50 -04003788 check_permissions();
Len Brownfcd17212015-03-23 20:29:09 -04003789 process_cpuid();
Len Brown103a8fe2010-10-22 23:53:03 -04003790
Len Brown103a8fe2010-10-22 23:53:03 -04003791
Len Brownd8af6f52015-02-10 01:56:38 -05003792 if (debug)
Len Brown7f5c2582015-12-01 01:36:39 -05003793 for_all_cpus(print_hwp, ODD_COUNTERS);
3794
3795 if (debug)
Len Brown889facb2012-11-08 00:48:57 -05003796 for_all_cpus(print_epb, ODD_COUNTERS);
3797
Len Brownd8af6f52015-02-10 01:56:38 -05003798 if (debug)
Len Brown3a9a9412014-08-15 02:39:52 -04003799 for_all_cpus(print_perf_limit, ODD_COUNTERS);
3800
Len Brownd8af6f52015-02-10 01:56:38 -05003801 if (debug)
Len Brown889facb2012-11-08 00:48:57 -05003802 for_all_cpus(print_rapl, ODD_COUNTERS);
3803
3804 for_all_cpus(set_temperature_target, ODD_COUNTERS);
3805
Len Brownd8af6f52015-02-10 01:56:38 -05003806 if (debug)
Len Brown889facb2012-11-08 00:48:57 -05003807 for_all_cpus(print_thermal, ODD_COUNTERS);
Len Brown5a634262016-04-06 17:15:55 -04003808
3809 if (debug && do_irtl_snb)
3810 print_irtl();
Len Brown103a8fe2010-10-22 23:53:03 -04003811}
3812
3813int fork_it(char **argv)
3814{
Len Brown103a8fe2010-10-22 23:53:03 -04003815 pid_t child_pid;
Len Brownd91bb172012-11-01 00:08:19 -04003816 int status;
Len Brownd15cf7c2012-06-03 23:24:00 -04003817
Len Brownd91bb172012-11-01 00:08:19 -04003818 status = for_all_cpus(get_counters, EVEN_COUNTERS);
3819 if (status)
3820 exit(status);
Len Brownc98d5d92012-06-04 00:56:40 -04003821 /* clear affinity side-effect of get_counters() */
3822 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
Len Brown103a8fe2010-10-22 23:53:03 -04003823 gettimeofday(&tv_even, (struct timezone *)NULL);
3824
3825 child_pid = fork();
3826 if (!child_pid) {
3827 /* child */
3828 execvp(argv[0], argv);
3829 } else {
Len Brown103a8fe2010-10-22 23:53:03 -04003830
3831 /* parent */
Josh Triplettb2c95d92013-08-20 17:20:18 -07003832 if (child_pid == -1)
3833 err(1, "fork");
Len Brown103a8fe2010-10-22 23:53:03 -04003834
3835 signal(SIGINT, SIG_IGN);
3836 signal(SIGQUIT, SIG_IGN);
Josh Triplettb2c95d92013-08-20 17:20:18 -07003837 if (waitpid(child_pid, &status, 0) == -1)
3838 err(status, "waitpid");
Len Brown103a8fe2010-10-22 23:53:03 -04003839 }
Len Brownc98d5d92012-06-04 00:56:40 -04003840 /*
3841 * n.b. fork_it() does not check for errors from for_all_cpus()
3842 * because re-starting is problematic when forking
3843 */
3844 for_all_cpus(get_counters, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04003845 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04003846 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownba3dec92016-04-22 20:31:46 -04003847 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS))
3848 fprintf(outf, "%s: Counter reset detected\n", progname);
3849 else {
3850 compute_average(EVEN_COUNTERS);
3851 format_all_counters(EVEN_COUNTERS);
3852 }
Len Brown103a8fe2010-10-22 23:53:03 -04003853
Len Brownb7d8c142016-02-13 23:36:17 -05003854 fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
3855
3856 flush_output_stderr();
Len Brown103a8fe2010-10-22 23:53:03 -04003857
Len Brownd91bb172012-11-01 00:08:19 -04003858 return status;
Len Brown103a8fe2010-10-22 23:53:03 -04003859}
3860
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02003861int get_and_dump_counters(void)
3862{
3863 int status;
3864
3865 status = for_all_cpus(get_counters, ODD_COUNTERS);
3866 if (status)
3867 return status;
3868
3869 status = for_all_cpus(dump_counters, ODD_COUNTERS);
3870 if (status)
3871 return status;
3872
Len Brownb7d8c142016-02-13 23:36:17 -05003873 flush_output_stdout();
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02003874
3875 return status;
3876}
3877
Len Brownd8af6f52015-02-10 01:56:38 -05003878void print_version() {
Len Brown6886fee2016-12-24 15:18:37 -05003879 fprintf(outf, "turbostat version 4.16 24 Dec 2016"
Len Brownd8af6f52015-02-10 01:56:38 -05003880 " - Len Brown <lenb@kernel.org>\n");
3881}
3882
Len Brown388e9c82016-12-22 23:57:55 -05003883int add_counter(unsigned int msr_num, char *name, unsigned int width,
3884 enum counter_scope scope, enum counter_type type,
3885 enum counter_format format)
3886{
3887 struct msr_counter *msrp;
3888
3889 msrp = calloc(1, sizeof(struct msr_counter));
3890 if (msrp == NULL) {
3891 perror("calloc");
3892 exit(1);
3893 }
3894
3895 msrp->msr_num = msr_num;
3896 strncpy(msrp->name, name, NAME_BYTES);
3897 msrp->width = width;
3898 msrp->type = type;
3899 msrp->format = format;
3900
3901 switch (scope) {
3902
3903 case SCOPE_CPU:
3904 sys.thread_counter_bytes += 64;
3905 msrp->next = sys.tp;
3906 sys.tp = msrp;
3907 sys.thread_counter_bytes += sizeof(unsigned long long);
3908 break;
3909
3910 case SCOPE_CORE:
3911 sys.core_counter_bytes += 64;
3912 msrp->next = sys.cp;
3913 sys.cp = msrp;
3914 sys.core_counter_bytes += sizeof(unsigned long long);
3915 break;
3916
3917 case SCOPE_PACKAGE:
3918 sys.package_counter_bytes += 64;
3919 msrp->next = sys.pp;
3920 sys.pp = msrp;
3921 sys.package_counter_bytes += sizeof(unsigned long long);
3922 break;
3923 }
3924
3925 return 0;
3926}
3927
3928void parse_add_command(char *add_command)
3929{
3930 int msr_num = 0;
3931 char name_buffer[NAME_BYTES];
3932 int width = 64;
3933 int fail = 0;
3934 enum counter_scope scope = SCOPE_CPU;
3935 enum counter_type type = COUNTER_CYCLES;
3936 enum counter_format format = FORMAT_DELTA;
3937
3938 while (add_command) {
3939
3940 if (sscanf(add_command, "msr0x%x", &msr_num) == 1)
3941 goto next;
3942
3943 if (sscanf(add_command, "msr%d", &msr_num) == 1)
3944 goto next;
3945
3946 if (sscanf(add_command, "u%d", &width) == 1) {
3947 if ((width == 32) || (width == 64))
3948 goto next;
3949 width = 64;
3950 }
3951 if (!strncmp(add_command, "cpu", strlen("cpu"))) {
3952 scope = SCOPE_CPU;
3953 goto next;
3954 }
3955 if (!strncmp(add_command, "core", strlen("core"))) {
3956 scope = SCOPE_CORE;
3957 goto next;
3958 }
3959 if (!strncmp(add_command, "package", strlen("package"))) {
3960 scope = SCOPE_PACKAGE;
3961 goto next;
3962 }
3963 if (!strncmp(add_command, "cycles", strlen("cycles"))) {
3964 type = COUNTER_CYCLES;
3965 goto next;
3966 }
3967 if (!strncmp(add_command, "seconds", strlen("seconds"))) {
3968 type = COUNTER_SECONDS;
3969 goto next;
3970 }
3971 if (!strncmp(add_command, "raw", strlen("raw"))) {
3972 format = FORMAT_RAW;
3973 goto next;
3974 }
3975 if (!strncmp(add_command, "delta", strlen("delta"))) {
3976 format = FORMAT_DELTA;
3977 goto next;
3978 }
3979 if (!strncmp(add_command, "percent", strlen("percent"))) {
3980 format = FORMAT_PERCENT;
3981 goto next;
3982 }
3983
3984 if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) { /* 18 < NAME_BYTES */
3985 char *eos;
3986
3987 eos = strchr(name_buffer, ',');
3988 if (eos)
3989 *eos = '\0';
3990 goto next;
3991 }
3992
3993next:
3994 add_command = strchr(add_command, ',');
3995 if (add_command)
3996 add_command++;
3997
3998 }
3999 if (msr_num == 0) {
4000 fprintf(stderr, "--add: (msrDDD | msr0xXXX) required\n");
4001 fail++;
4002 }
4003
4004 /* generate default column header */
4005 if (*name_buffer == '\0') {
4006 if (format == FORMAT_RAW) {
4007 if (width == 32)
4008 sprintf(name_buffer, "msr%d", msr_num);
4009 else
4010 sprintf(name_buffer, "MSR%d", msr_num);
4011 } else if (format == FORMAT_DELTA) {
4012 if (width == 32)
4013 sprintf(name_buffer, "cnt%d", msr_num);
4014 else
4015 sprintf(name_buffer, "CNT%d", msr_num);
4016 } else if (format == FORMAT_PERCENT) {
4017 if (width == 32)
4018 sprintf(name_buffer, "msr%d%%", msr_num);
4019 else
4020 sprintf(name_buffer, "MSR%d%%", msr_num);
4021 }
4022 }
4023
4024 if (add_counter(msr_num, name_buffer, width, scope, type, format))
4025 fail++;
4026
4027 if (fail) {
4028 help();
4029 exit(1);
4030 }
4031}
Len Brown103a8fe2010-10-22 23:53:03 -04004032void cmdline(int argc, char **argv)
4033{
4034 int opt;
Len Brownd8af6f52015-02-10 01:56:38 -05004035 int option_index = 0;
4036 static struct option long_options[] = {
Len Brown388e9c82016-12-22 23:57:55 -05004037 {"add", required_argument, 0, 'a'},
Len Brownd8af6f52015-02-10 01:56:38 -05004038 {"Dump", no_argument, 0, 'D'},
4039 {"debug", no_argument, 0, 'd'},
4040 {"interval", required_argument, 0, 'i'},
4041 {"help", no_argument, 0, 'h'},
4042 {"Joules", no_argument, 0, 'J'},
Len Brownb7d8c142016-02-13 23:36:17 -05004043 {"out", required_argument, 0, 'o'},
Len Brownd8af6f52015-02-10 01:56:38 -05004044 {"Package", no_argument, 0, 'p'},
4045 {"processor", no_argument, 0, 'p'},
4046 {"Summary", no_argument, 0, 'S'},
4047 {"TCC", required_argument, 0, 'T'},
4048 {"version", no_argument, 0, 'v' },
4049 {0, 0, 0, 0 }
4050 };
Len Brown103a8fe2010-10-22 23:53:03 -04004051
4052 progname = argv[0];
4053
Len Brownb7d8c142016-02-13 23:36:17 -05004054 while ((opt = getopt_long_only(argc, argv, "+C:c:Ddhi:JM:m:o:PpST:v",
Len Brownd8af6f52015-02-10 01:56:38 -05004055 long_options, &option_index)) != -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04004056 switch (opt) {
Len Brown388e9c82016-12-22 23:57:55 -05004057 case 'a':
4058 parse_add_command(optarg);
4059 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004060 case 'D':
4061 dump_only++;
Len Brown8e180f32012-09-22 01:25:08 -04004062 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004063 case 'd':
4064 debug++;
Len Brown2f32edf2012-09-21 23:45:46 -04004065 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004066 case 'h':
4067 default:
4068 help();
4069 exit(1);
4070 case 'i':
Len Brown2a0609c2016-02-12 22:44:48 -05004071 {
4072 double interval = strtod(optarg, NULL);
4073
4074 if (interval < 0.001) {
Len Brownb7d8c142016-02-13 23:36:17 -05004075 fprintf(outf, "interval %f seconds is too small\n",
Len Brown2a0609c2016-02-12 22:44:48 -05004076 interval);
4077 exit(2);
4078 }
4079
4080 interval_ts.tv_sec = interval;
4081 interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
4082 }
Len Brown889facb2012-11-08 00:48:57 -05004083 break;
Dirk Brandewie5c56be92013-12-16 10:23:41 -08004084 case 'J':
4085 rapl_joules++;
4086 break;
Len Brownb7d8c142016-02-13 23:36:17 -05004087 case 'o':
4088 outf = fopen_or_die(optarg, "w");
4089 break;
Len Brownd8af6f52015-02-10 01:56:38 -05004090 case 'P':
4091 show_pkg_only++;
4092 break;
4093 case 'p':
4094 show_core_only++;
4095 break;
4096 case 'S':
4097 summary_only++;
4098 break;
4099 case 'T':
4100 tcc_activation_temp_override = atoi(optarg);
4101 break;
4102 case 'v':
4103 print_version();
4104 exit(0);
4105 break;
Len Brown103a8fe2010-10-22 23:53:03 -04004106 }
4107 }
4108}
4109
4110int main(int argc, char **argv)
4111{
Len Brownb7d8c142016-02-13 23:36:17 -05004112 outf = stderr;
4113
Len Brown103a8fe2010-10-22 23:53:03 -04004114 cmdline(argc, argv);
4115
Len Brownd8af6f52015-02-10 01:56:38 -05004116 if (debug)
4117 print_version();
Len Brown103a8fe2010-10-22 23:53:03 -04004118
4119 turbostat_init();
4120
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02004121 /* dump counters and exit */
4122 if (dump_only)
4123 return get_and_dump_counters();
4124
Len Brown103a8fe2010-10-22 23:53:03 -04004125 /*
4126 * if any params left, it must be a command to fork
4127 */
4128 if (argc - optind)
4129 return fork_it(argv + optind);
4130 else
4131 turbostat_loop();
4132
4133 return 0;
4134}