blob: 5f9f41a3bf91ceb0648308823918289fa2a3ad29 [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
Josh Triplett95aebc42013-08-20 17:20:16 -070024#include <stdarg.h>
Len Brown103a8fe2010-10-22 23:53:03 -040025#include <stdio.h>
Josh Triplettb2c95d92013-08-20 17:20:18 -070026#include <err.h>
Len Brown103a8fe2010-10-22 23:53:03 -040027#include <unistd.h>
28#include <sys/types.h>
29#include <sys/wait.h>
30#include <sys/stat.h>
31#include <sys/resource.h>
32#include <fcntl.h>
33#include <signal.h>
34#include <sys/time.h>
35#include <stdlib.h>
Len Brownd8af6f52015-02-10 01:56:38 -050036#include <getopt.h>
Len Brown103a8fe2010-10-22 23:53:03 -040037#include <dirent.h>
38#include <string.h>
39#include <ctype.h>
Len Brown88c32812012-03-29 21:44:40 -040040#include <sched.h>
Len Brown2a0609c2016-02-12 22:44:48 -050041#include <time.h>
Josh Triplett2b928652013-08-20 17:20:14 -070042#include <cpuid.h>
Len Brown98481e72014-08-15 00:36:50 -040043#include <linux/capability.h>
44#include <errno.h>
Len Brown103a8fe2010-10-22 23:53:03 -040045
Len Brown103a8fe2010-10-22 23:53:03 -040046char *proc_stat = "/proc/stat";
Len Brownb7d8c142016-02-13 23:36:17 -050047FILE *outf;
Len Brown2a0609c2016-02-12 22:44:48 -050048struct timespec interval_ts = {5, 0};
Len Brownd8af6f52015-02-10 01:56:38 -050049unsigned int debug;
50unsigned int rapl_joules;
51unsigned int summary_only;
52unsigned int dump_only;
Len Brown103a8fe2010-10-22 23:53:03 -040053unsigned int skip_c0;
54unsigned int skip_c1;
55unsigned 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 Brownfc04cc62014-02-06 00:55:19 -050068unsigned int units = 1000000; /* MHz etc */
Len Brown103a8fe2010-10-22 23:53:03 -040069unsigned int genuine_intel;
70unsigned int has_invariant_tsc;
Len Brownd7899442015-01-23 00:12:33 -050071unsigned int do_nhm_platform_info;
Len Brown2f32edf2012-09-21 23:45:46 -040072unsigned int extra_msr_offset32;
73unsigned int extra_msr_offset64;
Len Brown8e180f32012-09-22 01:25:08 -040074unsigned int extra_delta_offset32;
75unsigned int extra_delta_offset64;
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +020076unsigned int aperf_mperf_multiplier = 1;
Len Brown1ed51012013-02-10 17:19:24 -050077int do_smi;
Len Brown103a8fe2010-10-22 23:53:03 -040078double bclk;
Len Browna2b7b742015-09-26 00:12:38 -040079double base_hz;
Len Brown21ed5572015-10-19 22:37:40 -040080unsigned int has_base_hz;
Len Browna2b7b742015-09-26 00:12:38 -040081double tsc_tweak = 1.0;
Len Brown103a8fe2010-10-22 23:53:03 -040082unsigned int show_pkg;
83unsigned int show_core;
84unsigned int show_cpu;
Len Brownc98d5d92012-06-04 00:56:40 -040085unsigned int show_pkg_only;
86unsigned int show_core_only;
87char *output_buffer, *outp;
Len Brown889facb2012-11-08 00:48:57 -050088unsigned int do_rapl;
89unsigned int do_dts;
90unsigned int do_ptm;
91unsigned int tcc_activation_temp;
92unsigned int tcc_activation_temp_override;
Andrey Semin40ee8e32014-12-05 00:07:00 -050093double rapl_power_units, rapl_time_units;
94double rapl_dram_energy_units, rapl_energy_units;
Len Brown889facb2012-11-08 00:48:57 -050095double rapl_joule_counter_range;
Len Brown3a9a9412014-08-15 02:39:52 -040096unsigned int do_core_perf_limit_reasons;
97unsigned int do_gfx_perf_limit_reasons;
98unsigned int do_ring_perf_limit_reasons;
Len Brown8a5bdf42015-04-01 21:02:57 -040099unsigned int crystal_hz;
100unsigned long long tsc_hz;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -0400101int base_cpu;
Len Brown21ed5572015-10-19 22:37:40 -0400102double discover_bclk(unsigned int family, unsigned int model);
Len Brown7f5c2582015-12-01 01:36:39 -0500103unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */
104 /* IA32_HWP_REQUEST, IA32_HWP_STATUS */
105unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */
106unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */
107unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */
108unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */
Len Brown889facb2012-11-08 00:48:57 -0500109
Len Browne6f9bb32013-12-03 02:19:19 -0500110#define RAPL_PKG (1 << 0)
111 /* 0x610 MSR_PKG_POWER_LIMIT */
112 /* 0x611 MSR_PKG_ENERGY_STATUS */
113#define RAPL_PKG_PERF_STATUS (1 << 1)
114 /* 0x613 MSR_PKG_PERF_STATUS */
115#define RAPL_PKG_POWER_INFO (1 << 2)
116 /* 0x614 MSR_PKG_POWER_INFO */
117
118#define RAPL_DRAM (1 << 3)
119 /* 0x618 MSR_DRAM_POWER_LIMIT */
120 /* 0x619 MSR_DRAM_ENERGY_STATUS */
Len Browne6f9bb32013-12-03 02:19:19 -0500121#define RAPL_DRAM_PERF_STATUS (1 << 4)
122 /* 0x61b MSR_DRAM_PERF_STATUS */
Len Brown0b2bb692015-03-26 00:50:30 -0400123#define RAPL_DRAM_POWER_INFO (1 << 5)
124 /* 0x61c MSR_DRAM_POWER_INFO */
Len Browne6f9bb32013-12-03 02:19:19 -0500125
Len Brown0b2bb692015-03-26 00:50:30 -0400126#define RAPL_CORES (1 << 6)
Len Browne6f9bb32013-12-03 02:19:19 -0500127 /* 0x638 MSR_PP0_POWER_LIMIT */
128 /* 0x639 MSR_PP0_ENERGY_STATUS */
Len Brown0b2bb692015-03-26 00:50:30 -0400129#define RAPL_CORE_POLICY (1 << 7)
Len Browne6f9bb32013-12-03 02:19:19 -0500130 /* 0x63a MSR_PP0_POLICY */
131
Len Brown0b2bb692015-03-26 00:50:30 -0400132#define RAPL_GFX (1 << 8)
Len Browne6f9bb32013-12-03 02:19:19 -0500133 /* 0x640 MSR_PP1_POWER_LIMIT */
134 /* 0x641 MSR_PP1_ENERGY_STATUS */
135 /* 0x642 MSR_PP1_POLICY */
Len Brown889facb2012-11-08 00:48:57 -0500136#define TJMAX_DEFAULT 100
137
138#define MAX(a, b) ((a) > (b) ? (a) : (b))
Len Brown103a8fe2010-10-22 23:53:03 -0400139
140int aperf_mperf_unstable;
141int backwards_count;
142char *progname;
Len Brown103a8fe2010-10-22 23:53:03 -0400143
Len Brownc98d5d92012-06-04 00:56:40 -0400144cpu_set_t *cpu_present_set, *cpu_affinity_set;
145size_t cpu_present_setsize, cpu_affinity_setsize;
Len Brown103a8fe2010-10-22 23:53:03 -0400146
Len Brownc98d5d92012-06-04 00:56:40 -0400147struct thread_data {
148 unsigned long long tsc;
149 unsigned long long aperf;
150 unsigned long long mperf;
Len Brown144b44b2013-11-09 00:30:16 -0500151 unsigned long long c1;
Len Brown2f32edf2012-09-21 23:45:46 -0400152 unsigned long long extra_msr64;
Len Brown8e180f32012-09-22 01:25:08 -0400153 unsigned long long extra_delta64;
154 unsigned long long extra_msr32;
155 unsigned long long extra_delta32;
Len Brown1ed51012013-02-10 17:19:24 -0500156 unsigned int smi_count;
Len Brownc98d5d92012-06-04 00:56:40 -0400157 unsigned int cpu_id;
158 unsigned int flags;
159#define CPU_IS_FIRST_THREAD_IN_CORE 0x2
160#define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
161} *thread_even, *thread_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400162
Len Brownc98d5d92012-06-04 00:56:40 -0400163struct core_data {
164 unsigned long long c3;
165 unsigned long long c6;
166 unsigned long long c7;
Len Brown889facb2012-11-08 00:48:57 -0500167 unsigned int core_temp_c;
Len Brownc98d5d92012-06-04 00:56:40 -0400168 unsigned int core_id;
169} *core_even, *core_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400170
Len Brownc98d5d92012-06-04 00:56:40 -0400171struct pkg_data {
172 unsigned long long pc2;
173 unsigned long long pc3;
174 unsigned long long pc6;
175 unsigned long long pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800176 unsigned long long pc8;
177 unsigned long long pc9;
178 unsigned long long pc10;
Len Brown0b2bb692015-03-26 00:50:30 -0400179 unsigned long long pkg_wtd_core_c0;
180 unsigned long long pkg_any_core_c0;
181 unsigned long long pkg_any_gfxe_c0;
182 unsigned long long pkg_both_core_gfxe_c0;
Len Brownc98d5d92012-06-04 00:56:40 -0400183 unsigned int package_id;
Len Brown889facb2012-11-08 00:48:57 -0500184 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
185 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
186 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
187 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
188 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
189 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
190 unsigned int pkg_temp_c;
191
Len Brownc98d5d92012-06-04 00:56:40 -0400192} *package_even, *package_odd;
193
194#define ODD_COUNTERS thread_odd, core_odd, package_odd
195#define EVEN_COUNTERS thread_even, core_even, package_even
196
197#define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
198 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
199 topo.num_threads_per_core + \
200 (core_no) * topo.num_threads_per_core + (thread_no))
201#define GET_CORE(core_base, core_no, pkg_no) \
202 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
203#define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
204
205struct system_summary {
206 struct thread_data threads;
207 struct core_data cores;
208 struct pkg_data packages;
209} sum, average;
210
211
212struct topo_params {
213 int num_packages;
214 int num_cpus;
215 int num_cores;
216 int max_cpu_num;
217 int num_cores_per_pkg;
218 int num_threads_per_core;
219} topo;
220
221struct timeval tv_even, tv_odd, tv_delta;
222
223void setup_all_buffers(void);
224
225int cpu_is_not_present(int cpu)
Len Brownd15cf7c2012-06-03 23:24:00 -0400226{
Len Brownc98d5d92012-06-04 00:56:40 -0400227 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brownd15cf7c2012-06-03 23:24:00 -0400228}
Len Brown88c32812012-03-29 21:44:40 -0400229/*
Len Brownc98d5d92012-06-04 00:56:40 -0400230 * run func(thread, core, package) in topology order
231 * skip non-present cpus
Len Brown88c32812012-03-29 21:44:40 -0400232 */
Len Brownd15cf7c2012-06-03 23:24:00 -0400233
Len Brownc98d5d92012-06-04 00:56:40 -0400234int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
235 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
Len Brown88c32812012-03-29 21:44:40 -0400236{
Len Brownc98d5d92012-06-04 00:56:40 -0400237 int retval, pkg_no, core_no, thread_no;
238
239 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
240 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
241 for (thread_no = 0; thread_no <
242 topo.num_threads_per_core; ++thread_no) {
243 struct thread_data *t;
244 struct core_data *c;
245 struct pkg_data *p;
246
247 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
248
249 if (cpu_is_not_present(t->cpu_id))
250 continue;
251
252 c = GET_CORE(core_base, core_no, pkg_no);
253 p = GET_PKG(pkg_base, pkg_no);
254
255 retval = func(t, c, p);
256 if (retval)
257 return retval;
258 }
259 }
260 }
261 return 0;
Len Brown88c32812012-03-29 21:44:40 -0400262}
263
264int cpu_migrate(int cpu)
265{
Len Brownc98d5d92012-06-04 00:56:40 -0400266 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
267 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
268 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
Len Brown88c32812012-03-29 21:44:40 -0400269 return -1;
270 else
271 return 0;
272}
273
Len Brown15aaa342012-03-29 22:19:58 -0400274int get_msr(int cpu, off_t offset, unsigned long long *msr)
Len Brown103a8fe2010-10-22 23:53:03 -0400275{
276 ssize_t retval;
Len Brown103a8fe2010-10-22 23:53:03 -0400277 char pathname[32];
278 int fd;
279
280 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
281 fd = open(pathname, O_RDONLY);
Len Brown15aaa342012-03-29 22:19:58 -0400282 if (fd < 0)
Len Brown98481e72014-08-15 00:36:50 -0400283 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 -0400284
Len Brown15aaa342012-03-29 22:19:58 -0400285 retval = pread(fd, msr, sizeof *msr, offset);
Len Brown103a8fe2010-10-22 23:53:03 -0400286 close(fd);
Len Brown15aaa342012-03-29 22:19:58 -0400287
Len Brown98481e72014-08-15 00:36:50 -0400288 if (retval != sizeof *msr)
289 err(-1, "%s offset 0x%llx read failed", pathname, (unsigned long long)offset);
Len Brown15aaa342012-03-29 22:19:58 -0400290
291 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400292}
293
Len Brownfc04cc62014-02-06 00:55:19 -0500294/*
295 * Example Format w/ field column widths:
296 *
Len Brown75d2e442016-02-13 23:41:53 -0500297 * Package Core CPU Avg_MHz Bzy_MHz TSC_MHz SMI Busy% CPU_%c1 CPU_%c3 CPU_%c6 CPU_%c7 CoreTmp PkgTmp Pkg%pc2 Pkg%pc3 Pkg%pc6 Pkg%pc7 PkgWatt CorWatt GFXWatt
Len Browne7c95ff2014-08-14 21:22:13 -0400298 * 123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678
Len Brownfc04cc62014-02-06 00:55:19 -0500299 */
300
Len Browna829eb42011-02-10 23:36:34 -0500301void print_header(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400302{
303 if (show_pkg)
Len Browne7c95ff2014-08-14 21:22:13 -0400304 outp += sprintf(outp, " Package");
Len Brown103a8fe2010-10-22 23:53:03 -0400305 if (show_core)
Len Browne7c95ff2014-08-14 21:22:13 -0400306 outp += sprintf(outp, " Core");
Len Brown103a8fe2010-10-22 23:53:03 -0400307 if (show_cpu)
Len Browne7c95ff2014-08-14 21:22:13 -0400308 outp += sprintf(outp, " CPU");
Len Brown103a8fe2010-10-22 23:53:03 -0400309 if (has_aperf)
Len Browne7c95ff2014-08-14 21:22:13 -0400310 outp += sprintf(outp, " Avg_MHz");
Len Brownd7899442015-01-23 00:12:33 -0500311 if (has_aperf)
Len Brown75d2e442016-02-13 23:41:53 -0500312 outp += sprintf(outp, " Busy%%");
Len Brownfc04cc62014-02-06 00:55:19 -0500313 if (has_aperf)
Len Browne7c95ff2014-08-14 21:22:13 -0400314 outp += sprintf(outp, " Bzy_MHz");
315 outp += sprintf(outp, " TSC_MHz");
Len Brown1cc21f72015-02-23 00:34:57 -0500316
Len Brown8e180f32012-09-22 01:25:08 -0400317 if (extra_delta_offset32)
Len Browne7c95ff2014-08-14 21:22:13 -0400318 outp += sprintf(outp, " count 0x%03X", extra_delta_offset32);
Len Brown8e180f32012-09-22 01:25:08 -0400319 if (extra_delta_offset64)
Len Browne7c95ff2014-08-14 21:22:13 -0400320 outp += sprintf(outp, " COUNT 0x%03X", extra_delta_offset64);
Len Brown2f32edf2012-09-21 23:45:46 -0400321 if (extra_msr_offset32)
Len Browne7c95ff2014-08-14 21:22:13 -0400322 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset32);
Len Brown2f32edf2012-09-21 23:45:46 -0400323 if (extra_msr_offset64)
Len Browne7c95ff2014-08-14 21:22:13 -0400324 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset64);
Len Brown1cc21f72015-02-23 00:34:57 -0500325
326 if (!debug)
327 goto done;
328
329 if (do_smi)
330 outp += sprintf(outp, " SMI");
331
Len Brown103a8fe2010-10-22 23:53:03 -0400332 if (do_nhm_cstates)
Len Browne7c95ff2014-08-14 21:22:13 -0400333 outp += sprintf(outp, " CPU%%c1");
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -0700334 if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates)
Len Browne7c95ff2014-08-14 21:22:13 -0400335 outp += sprintf(outp, " CPU%%c3");
Len Brown103a8fe2010-10-22 23:53:03 -0400336 if (do_nhm_cstates)
Len Browne7c95ff2014-08-14 21:22:13 -0400337 outp += sprintf(outp, " CPU%%c6");
Len Brown103a8fe2010-10-22 23:53:03 -0400338 if (do_snb_cstates)
Len Browne7c95ff2014-08-14 21:22:13 -0400339 outp += sprintf(outp, " CPU%%c7");
Len Brown889facb2012-11-08 00:48:57 -0500340
341 if (do_dts)
Len Browne7c95ff2014-08-14 21:22:13 -0400342 outp += sprintf(outp, " CoreTmp");
Len Brown889facb2012-11-08 00:48:57 -0500343 if (do_ptm)
Len Browne7c95ff2014-08-14 21:22:13 -0400344 outp += sprintf(outp, " PkgTmp");
Len Brown889facb2012-11-08 00:48:57 -0500345
Len Brown0b2bb692015-03-26 00:50:30 -0400346 if (do_skl_residency) {
347 outp += sprintf(outp, " Totl%%C0");
348 outp += sprintf(outp, " Any%%C0");
349 outp += sprintf(outp, " GFX%%C0");
350 outp += sprintf(outp, " CPUGFX%%");
351 }
352
Len Brownee7e38e2015-02-09 23:39:45 -0500353 if (do_pc2)
Len Browne7c95ff2014-08-14 21:22:13 -0400354 outp += sprintf(outp, " Pkg%%pc2");
Len Brownee7e38e2015-02-09 23:39:45 -0500355 if (do_pc3)
Len Browne7c95ff2014-08-14 21:22:13 -0400356 outp += sprintf(outp, " Pkg%%pc3");
Len Brownee7e38e2015-02-09 23:39:45 -0500357 if (do_pc6)
Len Browne7c95ff2014-08-14 21:22:13 -0400358 outp += sprintf(outp, " Pkg%%pc6");
Len Brownee7e38e2015-02-09 23:39:45 -0500359 if (do_pc7)
Len Browne7c95ff2014-08-14 21:22:13 -0400360 outp += sprintf(outp, " Pkg%%pc7");
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800361 if (do_c8_c9_c10) {
Len Browne7c95ff2014-08-14 21:22:13 -0400362 outp += sprintf(outp, " Pkg%%pc8");
363 outp += sprintf(outp, " Pkg%%pc9");
364 outp += sprintf(outp, " Pk%%pc10");
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800365 }
Len Brown103a8fe2010-10-22 23:53:03 -0400366
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800367 if (do_rapl && !rapl_joules) {
368 if (do_rapl & RAPL_PKG)
Len Browne7c95ff2014-08-14 21:22:13 -0400369 outp += sprintf(outp, " PkgWatt");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800370 if (do_rapl & RAPL_CORES)
Len Browne7c95ff2014-08-14 21:22:13 -0400371 outp += sprintf(outp, " CorWatt");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800372 if (do_rapl & RAPL_GFX)
Len Browne7c95ff2014-08-14 21:22:13 -0400373 outp += sprintf(outp, " GFXWatt");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800374 if (do_rapl & RAPL_DRAM)
Len Browne7c95ff2014-08-14 21:22:13 -0400375 outp += sprintf(outp, " RAMWatt");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800376 if (do_rapl & RAPL_PKG_PERF_STATUS)
Len Browne7c95ff2014-08-14 21:22:13 -0400377 outp += sprintf(outp, " PKG_%%");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800378 if (do_rapl & RAPL_DRAM_PERF_STATUS)
Len Browne7c95ff2014-08-14 21:22:13 -0400379 outp += sprintf(outp, " RAM_%%");
Len Brownd7899442015-01-23 00:12:33 -0500380 } else if (do_rapl && rapl_joules) {
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800381 if (do_rapl & RAPL_PKG)
Len Browne7c95ff2014-08-14 21:22:13 -0400382 outp += sprintf(outp, " Pkg_J");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800383 if (do_rapl & RAPL_CORES)
Len Browne7c95ff2014-08-14 21:22:13 -0400384 outp += sprintf(outp, " Cor_J");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800385 if (do_rapl & RAPL_GFX)
Len Browne7c95ff2014-08-14 21:22:13 -0400386 outp += sprintf(outp, " GFX_J");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800387 if (do_rapl & RAPL_DRAM)
Len Brownbd6906e2015-07-24 10:35:23 -0400388 outp += sprintf(outp, " RAM_J");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800389 if (do_rapl & RAPL_PKG_PERF_STATUS)
Len Browne7c95ff2014-08-14 21:22:13 -0400390 outp += sprintf(outp, " PKG_%%");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800391 if (do_rapl & RAPL_DRAM_PERF_STATUS)
Len Browne7c95ff2014-08-14 21:22:13 -0400392 outp += sprintf(outp, " RAM_%%");
393 outp += sprintf(outp, " time");
Len Brown889facb2012-11-08 00:48:57 -0500394
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800395 }
Len Brown1cc21f72015-02-23 00:34:57 -0500396 done:
Len Brownc98d5d92012-06-04 00:56:40 -0400397 outp += sprintf(outp, "\n");
Len Brown103a8fe2010-10-22 23:53:03 -0400398}
399
Len Brownc98d5d92012-06-04 00:56:40 -0400400int dump_counters(struct thread_data *t, struct core_data *c,
401 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400402{
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200403 outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400404
Len Brownc98d5d92012-06-04 00:56:40 -0400405 if (t) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200406 outp += sprintf(outp, "CPU: %d flags 0x%x\n",
407 t->cpu_id, t->flags);
408 outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
409 outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
410 outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
411 outp += sprintf(outp, "c1: %016llX\n", t->c1);
412 outp += sprintf(outp, "msr0x%x: %08llX\n",
Len Brown8e180f32012-09-22 01:25:08 -0400413 extra_delta_offset32, t->extra_delta32);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200414 outp += sprintf(outp, "msr0x%x: %016llX\n",
Len Brown8e180f32012-09-22 01:25:08 -0400415 extra_delta_offset64, t->extra_delta64);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200416 outp += sprintf(outp, "msr0x%x: %08llX\n",
Len Brown2f32edf2012-09-21 23:45:46 -0400417 extra_msr_offset32, t->extra_msr32);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200418 outp += sprintf(outp, "msr0x%x: %016llX\n",
Len Brown2f32edf2012-09-21 23:45:46 -0400419 extra_msr_offset64, t->extra_msr64);
Len Brown1ed51012013-02-10 17:19:24 -0500420 if (do_smi)
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200421 outp += sprintf(outp, "SMI: %08X\n", t->smi_count);
Len Brownc98d5d92012-06-04 00:56:40 -0400422 }
Len Brown103a8fe2010-10-22 23:53:03 -0400423
Len Brownc98d5d92012-06-04 00:56:40 -0400424 if (c) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200425 outp += sprintf(outp, "core: %d\n", c->core_id);
426 outp += sprintf(outp, "c3: %016llX\n", c->c3);
427 outp += sprintf(outp, "c6: %016llX\n", c->c6);
428 outp += sprintf(outp, "c7: %016llX\n", c->c7);
429 outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
Len Brownc98d5d92012-06-04 00:56:40 -0400430 }
431
432 if (p) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200433 outp += sprintf(outp, "package: %d\n", p->package_id);
Len Brown0b2bb692015-03-26 00:50:30 -0400434
435 outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
436 outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
437 outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
438 outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
439
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200440 outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
Len Brownee7e38e2015-02-09 23:39:45 -0500441 if (do_pc3)
442 outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
443 if (do_pc6)
444 outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
445 if (do_pc7)
446 outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200447 outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
448 outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
449 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
450 outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
451 outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
452 outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
453 outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
454 outp += sprintf(outp, "Throttle PKG: %0X\n",
455 p->rapl_pkg_perf_status);
456 outp += sprintf(outp, "Throttle RAM: %0X\n",
457 p->rapl_dram_perf_status);
458 outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
Len Brownc98d5d92012-06-04 00:56:40 -0400459 }
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200460
461 outp += sprintf(outp, "\n");
462
Len Brownc98d5d92012-06-04 00:56:40 -0400463 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400464}
465
Len Browne23da032012-02-06 18:37:16 -0500466/*
467 * column formatting convention & formats
Len Browne23da032012-02-06 18:37:16 -0500468 */
Len Brownc98d5d92012-06-04 00:56:40 -0400469int format_counters(struct thread_data *t, struct core_data *c,
470 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400471{
472 double interval_float;
Len Brownfc04cc62014-02-06 00:55:19 -0500473 char *fmt8;
Len Brown103a8fe2010-10-22 23:53:03 -0400474
Len Brownc98d5d92012-06-04 00:56:40 -0400475 /* if showing only 1st thread in core and this isn't one, bail out */
476 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
477 return 0;
478
479 /* if showing only 1st thread in pkg and this isn't one, bail out */
480 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
481 return 0;
482
Len Brown103a8fe2010-10-22 23:53:03 -0400483 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
484
Len Brownc98d5d92012-06-04 00:56:40 -0400485 /* topo columns, print blanks on 1st (average) line */
486 if (t == &average.threads) {
Len Brown103a8fe2010-10-22 23:53:03 -0400487 if (show_pkg)
Len Brownfc04cc62014-02-06 00:55:19 -0500488 outp += sprintf(outp, " -");
Len Brown103a8fe2010-10-22 23:53:03 -0400489 if (show_core)
Len Brownfc04cc62014-02-06 00:55:19 -0500490 outp += sprintf(outp, " -");
Len Brown103a8fe2010-10-22 23:53:03 -0400491 if (show_cpu)
Len Brownfc04cc62014-02-06 00:55:19 -0500492 outp += sprintf(outp, " -");
Len Brown103a8fe2010-10-22 23:53:03 -0400493 } else {
Len Brownc98d5d92012-06-04 00:56:40 -0400494 if (show_pkg) {
495 if (p)
Len Brownfc04cc62014-02-06 00:55:19 -0500496 outp += sprintf(outp, "%8d", p->package_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400497 else
Len Brownfc04cc62014-02-06 00:55:19 -0500498 outp += sprintf(outp, " -");
Len Brownc98d5d92012-06-04 00:56:40 -0400499 }
Len Brownc98d5d92012-06-04 00:56:40 -0400500 if (show_core) {
501 if (c)
Len Brownfc04cc62014-02-06 00:55:19 -0500502 outp += sprintf(outp, "%8d", c->core_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400503 else
Len Brownfc04cc62014-02-06 00:55:19 -0500504 outp += sprintf(outp, " -");
Len Brownc98d5d92012-06-04 00:56:40 -0400505 }
Len Brown103a8fe2010-10-22 23:53:03 -0400506 if (show_cpu)
Len Brownfc04cc62014-02-06 00:55:19 -0500507 outp += sprintf(outp, "%8d", t->cpu_id);
Len Brown103a8fe2010-10-22 23:53:03 -0400508 }
Len Brownfc04cc62014-02-06 00:55:19 -0500509
Len Brownd7899442015-01-23 00:12:33 -0500510 /* Avg_MHz */
Len Brownfc04cc62014-02-06 00:55:19 -0500511 if (has_aperf)
512 outp += sprintf(outp, "%8.0f",
513 1.0 / units * t->aperf / interval_float);
514
Len Brown75d2e442016-02-13 23:41:53 -0500515 /* Busy% */
Len Brownd7899442015-01-23 00:12:33 -0500516 if (has_aperf) {
Len Brown103a8fe2010-10-22 23:53:03 -0400517 if (!skip_c0)
Len Browna2b7b742015-09-26 00:12:38 -0400518 outp += sprintf(outp, "%8.2f", 100.0 * t->mperf/t->tsc/tsc_tweak);
Len Brown103a8fe2010-10-22 23:53:03 -0400519 else
Len Brownfc04cc62014-02-06 00:55:19 -0500520 outp += sprintf(outp, "********");
Len Brown103a8fe2010-10-22 23:53:03 -0400521 }
522
Len Brownd7899442015-01-23 00:12:33 -0500523 /* Bzy_MHz */
Len Brown21ed5572015-10-19 22:37:40 -0400524 if (has_aperf) {
525 if (has_base_hz)
526 outp += sprintf(outp, "%8.0f", base_hz / units * t->aperf / t->mperf);
527 else
528 outp += sprintf(outp, "%8.0f",
529 1.0 * t->tsc / units * t->aperf / t->mperf / interval_float);
530 }
Len Brown103a8fe2010-10-22 23:53:03 -0400531
Len Brownd7899442015-01-23 00:12:33 -0500532 /* TSC_MHz */
Len Brownfc04cc62014-02-06 00:55:19 -0500533 outp += sprintf(outp, "%8.0f", 1.0 * t->tsc/units/interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400534
Len Brown8e180f32012-09-22 01:25:08 -0400535 /* delta */
536 if (extra_delta_offset32)
537 outp += sprintf(outp, " %11llu", t->extra_delta32);
538
539 /* DELTA */
540 if (extra_delta_offset64)
541 outp += sprintf(outp, " %11llu", t->extra_delta64);
Len Brown2f32edf2012-09-21 23:45:46 -0400542 /* msr */
543 if (extra_msr_offset32)
Len Brown8e180f32012-09-22 01:25:08 -0400544 outp += sprintf(outp, " 0x%08llx", t->extra_msr32);
Len Brown2f32edf2012-09-21 23:45:46 -0400545
Len Brown130ff302012-09-21 22:56:06 -0400546 /* MSR */
Len Brown2f32edf2012-09-21 23:45:46 -0400547 if (extra_msr_offset64)
548 outp += sprintf(outp, " 0x%016llx", t->extra_msr64);
Len Brown130ff302012-09-21 22:56:06 -0400549
Len Brown1cc21f72015-02-23 00:34:57 -0500550 if (!debug)
551 goto done;
552
553 /* SMI */
554 if (do_smi)
555 outp += sprintf(outp, "%8d", t->smi_count);
556
Len Brown103a8fe2010-10-22 23:53:03 -0400557 if (do_nhm_cstates) {
558 if (!skip_c1)
Len Brownfc04cc62014-02-06 00:55:19 -0500559 outp += sprintf(outp, "%8.2f", 100.0 * t->c1/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400560 else
Len Brownfc04cc62014-02-06 00:55:19 -0500561 outp += sprintf(outp, "********");
Len Brown103a8fe2010-10-22 23:53:03 -0400562 }
Len Brownc98d5d92012-06-04 00:56:40 -0400563
564 /* print per-core data only for 1st thread in core */
565 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
566 goto done;
567
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -0700568 if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates)
Len Brownfc04cc62014-02-06 00:55:19 -0500569 outp += sprintf(outp, "%8.2f", 100.0 * c->c3/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400570 if (do_nhm_cstates)
Len Brownfc04cc62014-02-06 00:55:19 -0500571 outp += sprintf(outp, "%8.2f", 100.0 * c->c6/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400572 if (do_snb_cstates)
Len Brownfc04cc62014-02-06 00:55:19 -0500573 outp += sprintf(outp, "%8.2f", 100.0 * c->c7/t->tsc);
Len Brownc98d5d92012-06-04 00:56:40 -0400574
Len Brown889facb2012-11-08 00:48:57 -0500575 if (do_dts)
Len Brownfc04cc62014-02-06 00:55:19 -0500576 outp += sprintf(outp, "%8d", c->core_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500577
Len Brownc98d5d92012-06-04 00:56:40 -0400578 /* print per-package data only for 1st core in package */
579 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
580 goto done;
581
Len Brown0b2bb692015-03-26 00:50:30 -0400582 /* PkgTmp */
Len Brown889facb2012-11-08 00:48:57 -0500583 if (do_ptm)
Len Brownfc04cc62014-02-06 00:55:19 -0500584 outp += sprintf(outp, "%8d", p->pkg_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500585
Len Brown0b2bb692015-03-26 00:50:30 -0400586 /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
587 if (do_skl_residency) {
588 outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_wtd_core_c0/t->tsc);
589 outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_any_core_c0/t->tsc);
590 outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_any_gfxe_c0/t->tsc);
591 outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_both_core_gfxe_c0/t->tsc);
592 }
593
Len Brownee7e38e2015-02-09 23:39:45 -0500594 if (do_pc2)
Len Brownfc04cc62014-02-06 00:55:19 -0500595 outp += sprintf(outp, "%8.2f", 100.0 * p->pc2/t->tsc);
Len Brownee7e38e2015-02-09 23:39:45 -0500596 if (do_pc3)
Len Brownfc04cc62014-02-06 00:55:19 -0500597 outp += sprintf(outp, "%8.2f", 100.0 * p->pc3/t->tsc);
Len Brownee7e38e2015-02-09 23:39:45 -0500598 if (do_pc6)
Len Brownfc04cc62014-02-06 00:55:19 -0500599 outp += sprintf(outp, "%8.2f", 100.0 * p->pc6/t->tsc);
Len Brownee7e38e2015-02-09 23:39:45 -0500600 if (do_pc7)
Len Brownfc04cc62014-02-06 00:55:19 -0500601 outp += sprintf(outp, "%8.2f", 100.0 * p->pc7/t->tsc);
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800602 if (do_c8_c9_c10) {
Len Brownfc04cc62014-02-06 00:55:19 -0500603 outp += sprintf(outp, "%8.2f", 100.0 * p->pc8/t->tsc);
604 outp += sprintf(outp, "%8.2f", 100.0 * p->pc9/t->tsc);
605 outp += sprintf(outp, "%8.2f", 100.0 * p->pc10/t->tsc);
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800606 }
Len Brown889facb2012-11-08 00:48:57 -0500607
608 /*
609 * If measurement interval exceeds minimum RAPL Joule Counter range,
610 * indicate that results are suspect by printing "**" in fraction place.
611 */
Len Brownfc04cc62014-02-06 00:55:19 -0500612 if (interval_float < rapl_joule_counter_range)
613 fmt8 = "%8.2f";
614 else
615 fmt8 = " %6.0f**";
Len Brown889facb2012-11-08 00:48:57 -0500616
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800617 if (do_rapl && !rapl_joules) {
618 if (do_rapl & RAPL_PKG)
Len Brownfc04cc62014-02-06 00:55:19 -0500619 outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800620 if (do_rapl & RAPL_CORES)
Len Brownfc04cc62014-02-06 00:55:19 -0500621 outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800622 if (do_rapl & RAPL_GFX)
Len Brownfc04cc62014-02-06 00:55:19 -0500623 outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800624 if (do_rapl & RAPL_DRAM)
Andrey Semin40ee8e32014-12-05 00:07:00 -0500625 outp += sprintf(outp, fmt8, p->energy_dram * rapl_dram_energy_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800626 if (do_rapl & RAPL_PKG_PERF_STATUS)
Len Brownfc04cc62014-02-06 00:55:19 -0500627 outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800628 if (do_rapl & RAPL_DRAM_PERF_STATUS)
Len Brownfc04cc62014-02-06 00:55:19 -0500629 outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
Len Brownd7899442015-01-23 00:12:33 -0500630 } else if (do_rapl && rapl_joules) {
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800631 if (do_rapl & RAPL_PKG)
Len Brownfc04cc62014-02-06 00:55:19 -0500632 outp += sprintf(outp, fmt8,
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800633 p->energy_pkg * rapl_energy_units);
634 if (do_rapl & RAPL_CORES)
Len Brownfc04cc62014-02-06 00:55:19 -0500635 outp += sprintf(outp, fmt8,
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800636 p->energy_cores * rapl_energy_units);
637 if (do_rapl & RAPL_GFX)
Len Brownfc04cc62014-02-06 00:55:19 -0500638 outp += sprintf(outp, fmt8,
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800639 p->energy_gfx * rapl_energy_units);
640 if (do_rapl & RAPL_DRAM)
Len Brownfc04cc62014-02-06 00:55:19 -0500641 outp += sprintf(outp, fmt8,
Andrey Semin40ee8e32014-12-05 00:07:00 -0500642 p->energy_dram * rapl_dram_energy_units);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800643 if (do_rapl & RAPL_PKG_PERF_STATUS)
Len Brownfc04cc62014-02-06 00:55:19 -0500644 outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800645 if (do_rapl & RAPL_DRAM_PERF_STATUS)
Len Brownfc04cc62014-02-06 00:55:19 -0500646 outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
Len Brown889facb2012-11-08 00:48:57 -0500647
Len Brownd7899442015-01-23 00:12:33 -0500648 outp += sprintf(outp, fmt8, interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800649 }
Len Brownc98d5d92012-06-04 00:56:40 -0400650done:
Len Brownc98d5d92012-06-04 00:56:40 -0400651 outp += sprintf(outp, "\n");
652
653 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400654}
655
Len Brownb7d8c142016-02-13 23:36:17 -0500656void flush_output_stdout(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400657{
Len Brownb7d8c142016-02-13 23:36:17 -0500658 FILE *filep;
659
660 if (outf == stderr)
661 filep = stdout;
662 else
663 filep = outf;
664
665 fputs(output_buffer, filep);
666 fflush(filep);
667
Len Brownc98d5d92012-06-04 00:56:40 -0400668 outp = output_buffer;
669}
Len Brownb7d8c142016-02-13 23:36:17 -0500670void flush_output_stderr(void)
Len Brownc98d5d92012-06-04 00:56:40 -0400671{
Len Brownb7d8c142016-02-13 23:36:17 -0500672 fputs(output_buffer, outf);
673 fflush(outf);
Len Brownc98d5d92012-06-04 00:56:40 -0400674 outp = output_buffer;
675}
676void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
677{
Len Browne23da032012-02-06 18:37:16 -0500678 static int printed;
Len Brown103a8fe2010-10-22 23:53:03 -0400679
Len Browne23da032012-02-06 18:37:16 -0500680 if (!printed || !summary_only)
681 print_header();
Len Brown103a8fe2010-10-22 23:53:03 -0400682
Len Brownc98d5d92012-06-04 00:56:40 -0400683 if (topo.num_cpus > 1)
684 format_counters(&average.threads, &average.cores,
685 &average.packages);
Len Brown103a8fe2010-10-22 23:53:03 -0400686
Len Browne23da032012-02-06 18:37:16 -0500687 printed = 1;
688
689 if (summary_only)
690 return;
691
Len Brownc98d5d92012-06-04 00:56:40 -0400692 for_all_cpus(format_counters, t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400693}
694
Len Brown889facb2012-11-08 00:48:57 -0500695#define DELTA_WRAP32(new, old) \
696 if (new > old) { \
697 old = new - old; \
698 } else { \
699 old = 0x100000000 + new - old; \
700 }
701
Len Brownc98d5d92012-06-04 00:56:40 -0400702void
703delta_package(struct pkg_data *new, struct pkg_data *old)
Len Brown103a8fe2010-10-22 23:53:03 -0400704{
Len Brown0b2bb692015-03-26 00:50:30 -0400705
706 if (do_skl_residency) {
707 old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
708 old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
709 old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
710 old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0;
711 }
Len Brownc98d5d92012-06-04 00:56:40 -0400712 old->pc2 = new->pc2 - old->pc2;
Len Brownee7e38e2015-02-09 23:39:45 -0500713 if (do_pc3)
714 old->pc3 = new->pc3 - old->pc3;
715 if (do_pc6)
716 old->pc6 = new->pc6 - old->pc6;
717 if (do_pc7)
718 old->pc7 = new->pc7 - old->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800719 old->pc8 = new->pc8 - old->pc8;
720 old->pc9 = new->pc9 - old->pc9;
721 old->pc10 = new->pc10 - old->pc10;
Len Brown889facb2012-11-08 00:48:57 -0500722 old->pkg_temp_c = new->pkg_temp_c;
723
724 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
725 DELTA_WRAP32(new->energy_cores, old->energy_cores);
726 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
727 DELTA_WRAP32(new->energy_dram, old->energy_dram);
728 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
729 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
Len Brownc98d5d92012-06-04 00:56:40 -0400730}
Len Brown103a8fe2010-10-22 23:53:03 -0400731
Len Brownc98d5d92012-06-04 00:56:40 -0400732void
733delta_core(struct core_data *new, struct core_data *old)
734{
735 old->c3 = new->c3 - old->c3;
736 old->c6 = new->c6 - old->c6;
737 old->c7 = new->c7 - old->c7;
Len Brown889facb2012-11-08 00:48:57 -0500738 old->core_temp_c = new->core_temp_c;
Len Brownc98d5d92012-06-04 00:56:40 -0400739}
Len Brown103a8fe2010-10-22 23:53:03 -0400740
Len Brownc3ae3312012-06-13 21:31:46 -0400741/*
742 * old = new - old
743 */
Len Brownc98d5d92012-06-04 00:56:40 -0400744void
745delta_thread(struct thread_data *new, struct thread_data *old,
746 struct core_data *core_delta)
747{
748 old->tsc = new->tsc - old->tsc;
Len Brown103a8fe2010-10-22 23:53:03 -0400749
Len Brownc98d5d92012-06-04 00:56:40 -0400750 /* check for TSC < 1 Mcycles over interval */
Josh Triplettb2c95d92013-08-20 17:20:18 -0700751 if (old->tsc < (1000 * 1000))
752 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
753 "You can disable all c-states by booting with \"idle=poll\"\n"
754 "or just the deep ones with \"processor.max_cstate=1\"");
Len Brown103a8fe2010-10-22 23:53:03 -0400755
Len Brownc98d5d92012-06-04 00:56:40 -0400756 old->c1 = new->c1 - old->c1;
Len Brown103a8fe2010-10-22 23:53:03 -0400757
Len Browna7296172015-01-23 01:33:58 -0500758 if (has_aperf) {
759 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
760 old->aperf = new->aperf - old->aperf;
761 old->mperf = new->mperf - old->mperf;
762 } else {
Len Brown103a8fe2010-10-22 23:53:03 -0400763
Len Browna7296172015-01-23 01:33:58 -0500764 if (!aperf_mperf_unstable) {
Len Brownb7d8c142016-02-13 23:36:17 -0500765 fprintf(outf, "%s: APERF or MPERF went backwards *\n", progname);
766 fprintf(outf, "* Frequency results do not cover entire interval *\n");
767 fprintf(outf, "* fix this by running Linux-2.6.30 or later *\n");
Len Brownc98d5d92012-06-04 00:56:40 -0400768
Len Browna7296172015-01-23 01:33:58 -0500769 aperf_mperf_unstable = 1;
770 }
771 /*
772 * mperf delta is likely a huge "positive" number
773 * can not use it for calculating c0 time
774 */
775 skip_c0 = 1;
776 skip_c1 = 1;
Len Brownc98d5d92012-06-04 00:56:40 -0400777 }
Len Brownc98d5d92012-06-04 00:56:40 -0400778 }
Len Brown103a8fe2010-10-22 23:53:03 -0400779
Len Brown103a8fe2010-10-22 23:53:03 -0400780
Len Brown144b44b2013-11-09 00:30:16 -0500781 if (use_c1_residency_msr) {
782 /*
783 * Some models have a dedicated C1 residency MSR,
784 * which should be more accurate than the derivation below.
785 */
786 } else {
787 /*
788 * As counter collection is not atomic,
789 * it is possible for mperf's non-halted cycles + idle states
790 * to exceed TSC's all cycles: show c1 = 0% in that case.
791 */
792 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
793 old->c1 = 0;
794 else {
795 /* normal case, derive c1 */
796 old->c1 = old->tsc - old->mperf - core_delta->c3
Len Brownc98d5d92012-06-04 00:56:40 -0400797 - core_delta->c6 - core_delta->c7;
Len Brown144b44b2013-11-09 00:30:16 -0500798 }
Len Brownc98d5d92012-06-04 00:56:40 -0400799 }
Len Brownc3ae3312012-06-13 21:31:46 -0400800
Len Brownc98d5d92012-06-04 00:56:40 -0400801 if (old->mperf == 0) {
Len Brownb7d8c142016-02-13 23:36:17 -0500802 if (debug > 1)
803 fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400804 old->mperf = 1; /* divide by 0 protection */
805 }
806
Len Brown8e180f32012-09-22 01:25:08 -0400807 old->extra_delta32 = new->extra_delta32 - old->extra_delta32;
808 old->extra_delta32 &= 0xFFFFFFFF;
809
810 old->extra_delta64 = new->extra_delta64 - old->extra_delta64;
811
Len Brownc98d5d92012-06-04 00:56:40 -0400812 /*
Len Brown8e180f32012-09-22 01:25:08 -0400813 * Extra MSR is just a snapshot, simply copy latest w/o subtracting
Len Brownc98d5d92012-06-04 00:56:40 -0400814 */
Len Brown2f32edf2012-09-21 23:45:46 -0400815 old->extra_msr32 = new->extra_msr32;
816 old->extra_msr64 = new->extra_msr64;
Len Brown1ed51012013-02-10 17:19:24 -0500817
818 if (do_smi)
819 old->smi_count = new->smi_count - old->smi_count;
Len Brownc98d5d92012-06-04 00:56:40 -0400820}
821
822int delta_cpu(struct thread_data *t, struct core_data *c,
823 struct pkg_data *p, struct thread_data *t2,
824 struct core_data *c2, struct pkg_data *p2)
825{
826 /* calculate core delta only for 1st thread in core */
827 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
828 delta_core(c, c2);
829
830 /* always calculate thread delta */
831 delta_thread(t, t2, c2); /* c2 is core delta */
832
833 /* calculate package delta only for 1st core in package */
834 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
835 delta_package(p, p2);
836
837 return 0;
838}
839
840void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
841{
842 t->tsc = 0;
843 t->aperf = 0;
844 t->mperf = 0;
845 t->c1 = 0;
846
Len Brown1ed51012013-02-10 17:19:24 -0500847 t->smi_count = 0;
Len Brown8e180f32012-09-22 01:25:08 -0400848 t->extra_delta32 = 0;
849 t->extra_delta64 = 0;
850
Len Brownc98d5d92012-06-04 00:56:40 -0400851 /* tells format_counters to dump all fields from this set */
852 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
853
854 c->c3 = 0;
855 c->c6 = 0;
856 c->c7 = 0;
Len Brown889facb2012-11-08 00:48:57 -0500857 c->core_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -0400858
Len Brown0b2bb692015-03-26 00:50:30 -0400859 p->pkg_wtd_core_c0 = 0;
860 p->pkg_any_core_c0 = 0;
861 p->pkg_any_gfxe_c0 = 0;
862 p->pkg_both_core_gfxe_c0 = 0;
863
Len Brownc98d5d92012-06-04 00:56:40 -0400864 p->pc2 = 0;
Len Brownee7e38e2015-02-09 23:39:45 -0500865 if (do_pc3)
866 p->pc3 = 0;
867 if (do_pc6)
868 p->pc6 = 0;
869 if (do_pc7)
870 p->pc7 = 0;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800871 p->pc8 = 0;
872 p->pc9 = 0;
873 p->pc10 = 0;
Len Brown889facb2012-11-08 00:48:57 -0500874
875 p->energy_pkg = 0;
876 p->energy_dram = 0;
877 p->energy_cores = 0;
878 p->energy_gfx = 0;
879 p->rapl_pkg_perf_status = 0;
880 p->rapl_dram_perf_status = 0;
881 p->pkg_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -0400882}
883int sum_counters(struct thread_data *t, struct core_data *c,
884 struct pkg_data *p)
885{
886 average.threads.tsc += t->tsc;
887 average.threads.aperf += t->aperf;
888 average.threads.mperf += t->mperf;
889 average.threads.c1 += t->c1;
890
Len Brown8e180f32012-09-22 01:25:08 -0400891 average.threads.extra_delta32 += t->extra_delta32;
892 average.threads.extra_delta64 += t->extra_delta64;
893
Len Brownc98d5d92012-06-04 00:56:40 -0400894 /* sum per-core values only for 1st thread in core */
895 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
896 return 0;
897
898 average.cores.c3 += c->c3;
899 average.cores.c6 += c->c6;
900 average.cores.c7 += c->c7;
901
Len Brown889facb2012-11-08 00:48:57 -0500902 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
903
Len Brownc98d5d92012-06-04 00:56:40 -0400904 /* sum per-pkg values only for 1st core in pkg */
905 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
906 return 0;
907
Len Brown0b2bb692015-03-26 00:50:30 -0400908 if (do_skl_residency) {
909 average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
910 average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
911 average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
912 average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
913 }
914
Len Brownc98d5d92012-06-04 00:56:40 -0400915 average.packages.pc2 += p->pc2;
Len Brownee7e38e2015-02-09 23:39:45 -0500916 if (do_pc3)
917 average.packages.pc3 += p->pc3;
918 if (do_pc6)
919 average.packages.pc6 += p->pc6;
920 if (do_pc7)
921 average.packages.pc7 += p->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800922 average.packages.pc8 += p->pc8;
923 average.packages.pc9 += p->pc9;
924 average.packages.pc10 += p->pc10;
Len Brownc98d5d92012-06-04 00:56:40 -0400925
Len Brown889facb2012-11-08 00:48:57 -0500926 average.packages.energy_pkg += p->energy_pkg;
927 average.packages.energy_dram += p->energy_dram;
928 average.packages.energy_cores += p->energy_cores;
929 average.packages.energy_gfx += p->energy_gfx;
930
931 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
932
933 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
934 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
Len Brownc98d5d92012-06-04 00:56:40 -0400935 return 0;
936}
937/*
938 * sum the counters for all cpus in the system
939 * compute the weighted average
940 */
941void compute_average(struct thread_data *t, struct core_data *c,
942 struct pkg_data *p)
943{
944 clear_counters(&average.threads, &average.cores, &average.packages);
945
946 for_all_cpus(sum_counters, t, c, p);
947
948 average.threads.tsc /= topo.num_cpus;
949 average.threads.aperf /= topo.num_cpus;
950 average.threads.mperf /= topo.num_cpus;
951 average.threads.c1 /= topo.num_cpus;
952
Len Brown8e180f32012-09-22 01:25:08 -0400953 average.threads.extra_delta32 /= topo.num_cpus;
954 average.threads.extra_delta32 &= 0xFFFFFFFF;
955
956 average.threads.extra_delta64 /= topo.num_cpus;
957
Len Brownc98d5d92012-06-04 00:56:40 -0400958 average.cores.c3 /= topo.num_cores;
959 average.cores.c6 /= topo.num_cores;
960 average.cores.c7 /= topo.num_cores;
961
Len Brown0b2bb692015-03-26 00:50:30 -0400962 if (do_skl_residency) {
963 average.packages.pkg_wtd_core_c0 /= topo.num_packages;
964 average.packages.pkg_any_core_c0 /= topo.num_packages;
965 average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
966 average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
967 }
968
Len Brownc98d5d92012-06-04 00:56:40 -0400969 average.packages.pc2 /= topo.num_packages;
Len Brownee7e38e2015-02-09 23:39:45 -0500970 if (do_pc3)
971 average.packages.pc3 /= topo.num_packages;
972 if (do_pc6)
973 average.packages.pc6 /= topo.num_packages;
974 if (do_pc7)
975 average.packages.pc7 /= topo.num_packages;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800976
977 average.packages.pc8 /= topo.num_packages;
978 average.packages.pc9 /= topo.num_packages;
979 average.packages.pc10 /= topo.num_packages;
Len Brownc98d5d92012-06-04 00:56:40 -0400980}
981
982static unsigned long long rdtsc(void)
983{
984 unsigned int low, high;
985
986 asm volatile("rdtsc" : "=a" (low), "=d" (high));
987
988 return low | ((unsigned long long)high) << 32;
989}
990
991
992/*
993 * get_counters(...)
994 * migrate to cpu
995 * acquire and record local counters for that cpu
996 */
997int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
998{
999 int cpu = t->cpu_id;
Len Brown889facb2012-11-08 00:48:57 -05001000 unsigned long long msr;
Len Brownc98d5d92012-06-04 00:56:40 -04001001
Len Browne52966c2012-11-08 22:38:05 -05001002 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05001003 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brownc98d5d92012-06-04 00:56:40 -04001004 return -1;
Len Browne52966c2012-11-08 22:38:05 -05001005 }
Len Brownc98d5d92012-06-04 00:56:40 -04001006
1007 t->tsc = rdtsc(); /* we are running on local CPU of interest */
1008
1009 if (has_aperf) {
Len Brown9c63a652012-10-31 01:29:52 -04001010 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001011 return -3;
Len Brown9c63a652012-10-31 01:29:52 -04001012 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001013 return -4;
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02001014 t->aperf = t->aperf * aperf_mperf_multiplier;
1015 t->mperf = t->mperf * aperf_mperf_multiplier;
Len Brownc98d5d92012-06-04 00:56:40 -04001016 }
1017
Len Brown1ed51012013-02-10 17:19:24 -05001018 if (do_smi) {
1019 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
1020 return -5;
1021 t->smi_count = msr & 0xFFFFFFFF;
1022 }
Len Brown8e180f32012-09-22 01:25:08 -04001023 if (extra_delta_offset32) {
Len Brown889facb2012-11-08 00:48:57 -05001024 if (get_msr(cpu, extra_delta_offset32, &msr))
Len Brown2f32edf2012-09-21 23:45:46 -04001025 return -5;
Len Brown889facb2012-11-08 00:48:57 -05001026 t->extra_delta32 = msr & 0xFFFFFFFF;
Len Brown8e180f32012-09-22 01:25:08 -04001027 }
1028
1029 if (extra_delta_offset64)
1030 if (get_msr(cpu, extra_delta_offset64, &t->extra_delta64))
1031 return -5;
1032
1033 if (extra_msr_offset32) {
Len Brown889facb2012-11-08 00:48:57 -05001034 if (get_msr(cpu, extra_msr_offset32, &msr))
Len Brown8e180f32012-09-22 01:25:08 -04001035 return -5;
Len Brown889facb2012-11-08 00:48:57 -05001036 t->extra_msr32 = msr & 0xFFFFFFFF;
Len Brown8e180f32012-09-22 01:25:08 -04001037 }
Len Brown2f32edf2012-09-21 23:45:46 -04001038
1039 if (extra_msr_offset64)
1040 if (get_msr(cpu, extra_msr_offset64, &t->extra_msr64))
Len Brownc98d5d92012-06-04 00:56:40 -04001041 return -5;
1042
Len Brown144b44b2013-11-09 00:30:16 -05001043 if (use_c1_residency_msr) {
1044 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1045 return -6;
1046 }
1047
Len Brownc98d5d92012-06-04 00:56:40 -04001048 /* collect core counters only for 1st thread in core */
1049 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1050 return 0;
1051
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001052 if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001053 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1054 return -6;
Len Brown144b44b2013-11-09 00:30:16 -05001055 }
1056
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001057 if (do_nhm_cstates && !do_knl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001058 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1059 return -7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001060 } else if (do_knl_cstates) {
1061 if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1062 return -7;
Len Brownc98d5d92012-06-04 00:56:40 -04001063 }
1064
1065 if (do_snb_cstates)
1066 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1067 return -8;
1068
Len Brown889facb2012-11-08 00:48:57 -05001069 if (do_dts) {
1070 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1071 return -9;
1072 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1073 }
1074
1075
Len Brownc98d5d92012-06-04 00:56:40 -04001076 /* collect package counters only for 1st core in package */
1077 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1078 return 0;
1079
Len Brown0b2bb692015-03-26 00:50:30 -04001080 if (do_skl_residency) {
1081 if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
1082 return -10;
1083 if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
1084 return -11;
1085 if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
1086 return -12;
1087 if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
1088 return -13;
1089 }
Len Brownee7e38e2015-02-09 23:39:45 -05001090 if (do_pc3)
Len Brownc98d5d92012-06-04 00:56:40 -04001091 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1092 return -9;
Len Brownee7e38e2015-02-09 23:39:45 -05001093 if (do_pc6)
Len Brownc98d5d92012-06-04 00:56:40 -04001094 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1095 return -10;
Len Brownee7e38e2015-02-09 23:39:45 -05001096 if (do_pc2)
Len Brownc98d5d92012-06-04 00:56:40 -04001097 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1098 return -11;
Len Brownee7e38e2015-02-09 23:39:45 -05001099 if (do_pc7)
Len Brownc98d5d92012-06-04 00:56:40 -04001100 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1101 return -12;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001102 if (do_c8_c9_c10) {
1103 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1104 return -13;
1105 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1106 return -13;
1107 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1108 return -13;
1109 }
Len Brown889facb2012-11-08 00:48:57 -05001110 if (do_rapl & RAPL_PKG) {
1111 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1112 return -13;
1113 p->energy_pkg = msr & 0xFFFFFFFF;
1114 }
1115 if (do_rapl & RAPL_CORES) {
1116 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1117 return -14;
1118 p->energy_cores = msr & 0xFFFFFFFF;
1119 }
1120 if (do_rapl & RAPL_DRAM) {
1121 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1122 return -15;
1123 p->energy_dram = msr & 0xFFFFFFFF;
1124 }
1125 if (do_rapl & RAPL_GFX) {
1126 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1127 return -16;
1128 p->energy_gfx = msr & 0xFFFFFFFF;
1129 }
1130 if (do_rapl & RAPL_PKG_PERF_STATUS) {
1131 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1132 return -16;
1133 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1134 }
1135 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1136 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1137 return -16;
1138 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1139 }
1140 if (do_ptm) {
1141 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1142 return -17;
1143 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1144 }
Len Brown103a8fe2010-10-22 23:53:03 -04001145 return 0;
1146}
1147
Len Brownee7e38e2015-02-09 23:39:45 -05001148/*
1149 * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1150 * If you change the values, note they are used both in comparisons
1151 * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1152 */
1153
1154#define PCLUKN 0 /* Unknown */
1155#define PCLRSV 1 /* Reserved */
1156#define PCL__0 2 /* PC0 */
1157#define PCL__1 3 /* PC1 */
1158#define PCL__2 4 /* PC2 */
1159#define PCL__3 5 /* PC3 */
1160#define PCL__4 6 /* PC4 */
1161#define PCL__6 7 /* PC6 */
1162#define PCL_6N 8 /* PC6 No Retention */
1163#define PCL_6R 9 /* PC6 Retention */
1164#define PCL__7 10 /* PC7 */
1165#define PCL_7S 11 /* PC7 Shrink */
Len Brown0b2bb692015-03-26 00:50:30 -04001166#define PCL__8 12 /* PC8 */
1167#define PCL__9 13 /* PC9 */
1168#define PCLUNL 14 /* Unlimited */
Len Brownee7e38e2015-02-09 23:39:45 -05001169
1170int pkg_cstate_limit = PCLUKN;
1171char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
Len Brown0b2bb692015-03-26 00:50:30 -04001172 "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"};
Len Brownee7e38e2015-02-09 23:39:45 -05001173
Len Browne9257f52015-04-01 21:02:57 -04001174int 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};
1175int 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};
1176int 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};
1177int 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};
1178int 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};
1179int 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 Brownee7e38e2015-02-09 23:39:45 -05001180
Len Browna2b7b742015-09-26 00:12:38 -04001181
1182static void
1183calculate_tsc_tweak()
1184{
Len Browna2b7b742015-09-26 00:12:38 -04001185 tsc_tweak = base_hz / tsc_hz;
1186}
1187
Len Brownfcd17212015-03-23 20:29:09 -04001188static void
1189dump_nhm_platform_info(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001190{
1191 unsigned long long msr;
1192 unsigned int ratio;
1193
Len Brownec0adc52015-11-12 02:42:31 -05001194 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001195
Len Brownb7d8c142016-02-13 23:36:17 -05001196 fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001197
Len Brown103a8fe2010-10-22 23:53:03 -04001198 ratio = (msr >> 40) & 0xFF;
Len Brownb7d8c142016-02-13 23:36:17 -05001199 fprintf(outf, "%d * %.0f = %.0f MHz max efficiency frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001200 ratio, bclk, ratio * bclk);
1201
1202 ratio = (msr >> 8) & 0xFF;
Len Brownb7d8c142016-02-13 23:36:17 -05001203 fprintf(outf, "%d * %.0f = %.0f MHz base frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001204 ratio, bclk, ratio * bclk);
1205
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001206 get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001207 fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
Len Brownbfae2052015-06-17 12:27:21 -04001208 base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
Len Brown67920412013-01-31 15:22:15 -05001209
Len Brownfcd17212015-03-23 20:29:09 -04001210 return;
1211}
1212
1213static void
1214dump_hsw_turbo_ratio_limits(void)
1215{
1216 unsigned long long msr;
1217 unsigned int ratio;
1218
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001219 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04001220
Len Brownb7d8c142016-02-13 23:36:17 -05001221 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04001222
1223 ratio = (msr >> 8) & 0xFF;
1224 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001225 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 18 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04001226 ratio, bclk, ratio * bclk);
1227
1228 ratio = (msr >> 0) & 0xFF;
1229 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001230 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 17 active cores\n",
Len Brownfcd17212015-03-23 20:29:09 -04001231 ratio, bclk, ratio * bclk);
1232 return;
1233}
1234
1235static void
1236dump_ivt_turbo_ratio_limits(void)
1237{
1238 unsigned long long msr;
1239 unsigned int ratio;
Len Brown6574a5d2012-09-21 00:01:31 -04001240
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001241 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001242
Len Brownb7d8c142016-02-13 23:36:17 -05001243 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001244
1245 ratio = (msr >> 56) & 0xFF;
1246 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001247 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 16 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001248 ratio, bclk, ratio * bclk);
1249
1250 ratio = (msr >> 48) & 0xFF;
1251 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001252 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 15 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001253 ratio, bclk, ratio * bclk);
1254
1255 ratio = (msr >> 40) & 0xFF;
1256 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001257 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 14 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001258 ratio, bclk, ratio * bclk);
1259
1260 ratio = (msr >> 32) & 0xFF;
1261 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001262 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 13 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001263 ratio, bclk, ratio * bclk);
1264
1265 ratio = (msr >> 24) & 0xFF;
1266 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001267 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 12 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001268 ratio, bclk, ratio * bclk);
1269
1270 ratio = (msr >> 16) & 0xFF;
1271 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001272 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 11 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001273 ratio, bclk, ratio * bclk);
1274
1275 ratio = (msr >> 8) & 0xFF;
1276 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001277 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 10 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001278 ratio, bclk, ratio * bclk);
1279
1280 ratio = (msr >> 0) & 0xFF;
1281 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001282 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 9 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001283 ratio, bclk, ratio * bclk);
Len Brownfcd17212015-03-23 20:29:09 -04001284 return;
1285}
Len Brown6574a5d2012-09-21 00:01:31 -04001286
Len Brownfcd17212015-03-23 20:29:09 -04001287static void
1288dump_nhm_turbo_ratio_limits(void)
1289{
1290 unsigned long long msr;
1291 unsigned int ratio;
Len Brown103a8fe2010-10-22 23:53:03 -04001292
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001293 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001294
Len Brownb7d8c142016-02-13 23:36:17 -05001295 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001296
1297 ratio = (msr >> 56) & 0xFF;
1298 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001299 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 8 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001300 ratio, bclk, ratio * bclk);
1301
1302 ratio = (msr >> 48) & 0xFF;
1303 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001304 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 7 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001305 ratio, bclk, ratio * bclk);
1306
1307 ratio = (msr >> 40) & 0xFF;
1308 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001309 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 6 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001310 ratio, bclk, ratio * bclk);
1311
1312 ratio = (msr >> 32) & 0xFF;
1313 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001314 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 5 active cores\n",
Len Brown6574a5d2012-09-21 00:01:31 -04001315 ratio, bclk, ratio * bclk);
1316
Len Brown103a8fe2010-10-22 23:53:03 -04001317 ratio = (msr >> 24) & 0xFF;
1318 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001319 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001320 ratio, bclk, ratio * bclk);
1321
1322 ratio = (msr >> 16) & 0xFF;
1323 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001324 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001325 ratio, bclk, ratio * bclk);
1326
1327 ratio = (msr >> 8) & 0xFF;
1328 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001329 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001330 ratio, bclk, ratio * bclk);
1331
1332 ratio = (msr >> 0) & 0xFF;
1333 if (ratio)
Len Brownb7d8c142016-02-13 23:36:17 -05001334 fprintf(outf, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001335 ratio, bclk, ratio * bclk);
Len Brownfcd17212015-03-23 20:29:09 -04001336 return;
1337}
Len Brown3a9a9412014-08-15 02:39:52 -04001338
Len Brownfcd17212015-03-23 20:29:09 -04001339static void
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001340dump_knl_turbo_ratio_limits(void)
1341{
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001342 const unsigned int buckets_no = 7;
1343
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001344 unsigned long long msr;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001345 int delta_cores, delta_ratio;
1346 int i, b_nr;
1347 unsigned int cores[buckets_no];
1348 unsigned int ratio[buckets_no];
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001349
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001350 get_msr(base_cpu, MSR_NHM_TURBO_RATIO_LIMIT, &msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001351
Len Brownb7d8c142016-02-13 23:36:17 -05001352 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n",
Len Brownbfae2052015-06-17 12:27:21 -04001353 base_cpu, msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001354
1355 /**
1356 * Turbo encoding in KNL is as follows:
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001357 * [0] -- Reserved
1358 * [7:1] -- Base value of number of active cores of bucket 1.
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001359 * [15:8] -- Base value of freq ratio of bucket 1.
1360 * [20:16] -- +ve delta of number of active cores of bucket 2.
1361 * i.e. active cores of bucket 2 =
1362 * active cores of bucket 1 + delta
1363 * [23:21] -- Negative delta of freq ratio of bucket 2.
1364 * i.e. freq ratio of bucket 2 =
1365 * freq ratio of bucket 1 - delta
1366 * [28:24]-- +ve delta of number of active cores of bucket 3.
1367 * [31:29]-- -ve delta of freq ratio of bucket 3.
1368 * [36:32]-- +ve delta of number of active cores of bucket 4.
1369 * [39:37]-- -ve delta of freq ratio of bucket 4.
1370 * [44:40]-- +ve delta of number of active cores of bucket 5.
1371 * [47:45]-- -ve delta of freq ratio of bucket 5.
1372 * [52:48]-- +ve delta of number of active cores of bucket 6.
1373 * [55:53]-- -ve delta of freq ratio of bucket 6.
1374 * [60:56]-- +ve delta of number of active cores of bucket 7.
1375 * [63:61]-- -ve delta of freq ratio of bucket 7.
1376 */
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001377
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001378 b_nr = 0;
1379 cores[b_nr] = (msr & 0xFF) >> 1;
1380 ratio[b_nr] = (msr >> 8) & 0xFF;
1381
1382 for (i = 16; i < 64; i += 8) {
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001383 delta_cores = (msr >> i) & 0x1F;
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001384 delta_ratio = (msr >> (i + 5)) & 0x7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001385
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001386 cores[b_nr + 1] = cores[b_nr] + delta_cores;
1387 ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
1388 b_nr++;
1389 }
1390
1391 for (i = buckets_no - 1; i >= 0; i--)
1392 if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
Len Brownb7d8c142016-02-13 23:36:17 -05001393 fprintf(outf,
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001394 "%d * %.0f = %.0f MHz max turbo %d active cores\n",
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001395 ratio[i], bclk, ratio[i] * bclk, cores[i]);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001396}
1397
1398static void
Len Brownfcd17212015-03-23 20:29:09 -04001399dump_nhm_cst_cfg(void)
1400{
1401 unsigned long long msr;
1402
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001403 get_msr(base_cpu, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04001404
1405#define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
1406#define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
1407
Len Brownb7d8c142016-02-13 23:36:17 -05001408 fprintf(outf, "cpu%d: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04001409
Len Brownb7d8c142016-02-13 23:36:17 -05001410 fprintf(outf, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: %s)\n",
Len Brownfcd17212015-03-23 20:29:09 -04001411 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
1412 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
1413 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
1414 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
1415 (msr & (1 << 15)) ? "" : "UN",
1416 (unsigned int)msr & 7,
1417 pkg_cstate_limit_strings[pkg_cstate_limit]);
1418 return;
Len Brown103a8fe2010-10-22 23:53:03 -04001419}
1420
Len Brown6fb31432015-06-17 16:23:45 -04001421static void
1422dump_config_tdp(void)
1423{
1424 unsigned long long msr;
1425
1426 get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001427 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
1428 fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xEF);
Len Brown6fb31432015-06-17 16:23:45 -04001429
1430 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001431 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04001432 if (msr) {
Len Brownb7d8c142016-02-13 23:36:17 -05001433 fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0xEFFF);
1434 fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0xEFFF);
1435 fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xEF);
1436 fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0xEFFF);
Len Brown6fb31432015-06-17 16:23:45 -04001437 }
Len Brownb7d8c142016-02-13 23:36:17 -05001438 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04001439
1440 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001441 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04001442 if (msr) {
Len Brownb7d8c142016-02-13 23:36:17 -05001443 fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0xEFFF);
1444 fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0xEFFF);
1445 fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xEF);
1446 fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0xEFFF);
Len Brown6fb31432015-06-17 16:23:45 -04001447 }
Len Brownb7d8c142016-02-13 23:36:17 -05001448 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04001449
1450 get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001451 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
Len Brown6fb31432015-06-17 16:23:45 -04001452 if ((msr) & 0x3)
Len Brownb7d8c142016-02-13 23:36:17 -05001453 fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
1454 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
1455 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04001456
1457 get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05001458 fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
1459 fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0x7F);
1460 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
1461 fprintf(outf, ")\n");
Len Brown6fb31432015-06-17 16:23:45 -04001462}
1463
Len Brownc98d5d92012-06-04 00:56:40 -04001464void free_all_buffers(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001465{
Len Brownc98d5d92012-06-04 00:56:40 -04001466 CPU_FREE(cpu_present_set);
1467 cpu_present_set = NULL;
1468 cpu_present_set = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001469
Len Brownc98d5d92012-06-04 00:56:40 -04001470 CPU_FREE(cpu_affinity_set);
1471 cpu_affinity_set = NULL;
1472 cpu_affinity_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001473
Len Brownc98d5d92012-06-04 00:56:40 -04001474 free(thread_even);
1475 free(core_even);
1476 free(package_even);
1477
1478 thread_even = NULL;
1479 core_even = NULL;
1480 package_even = NULL;
1481
1482 free(thread_odd);
1483 free(core_odd);
1484 free(package_odd);
1485
1486 thread_odd = NULL;
1487 core_odd = NULL;
1488 package_odd = NULL;
1489
1490 free(output_buffer);
1491 output_buffer = NULL;
1492 outp = NULL;
Len Brown103a8fe2010-10-22 23:53:03 -04001493}
1494
Len Brownc98d5d92012-06-04 00:56:40 -04001495/*
Josh Triplett57a42a32013-08-20 17:20:17 -07001496 * Open a file, and exit on failure
1497 */
1498FILE *fopen_or_die(const char *path, const char *mode)
1499{
Len Brownb7d8c142016-02-13 23:36:17 -05001500 FILE *filep = fopen(path, mode);
Josh Triplettb2c95d92013-08-20 17:20:18 -07001501 if (!filep)
1502 err(1, "%s: open failed", path);
Josh Triplett57a42a32013-08-20 17:20:17 -07001503 return filep;
1504}
1505
1506/*
Josh Triplett95aebc42013-08-20 17:20:16 -07001507 * Parse a file containing a single int.
1508 */
1509int parse_int_file(const char *fmt, ...)
1510{
1511 va_list args;
1512 char path[PATH_MAX];
1513 FILE *filep;
1514 int value;
1515
1516 va_start(args, fmt);
1517 vsnprintf(path, sizeof(path), fmt, args);
1518 va_end(args);
Josh Triplett57a42a32013-08-20 17:20:17 -07001519 filep = fopen_or_die(path, "r");
Josh Triplettb2c95d92013-08-20 17:20:18 -07001520 if (fscanf(filep, "%d", &value) != 1)
1521 err(1, "%s: failed to parse number from file", path);
Josh Triplett95aebc42013-08-20 17:20:16 -07001522 fclose(filep);
1523 return value;
1524}
1525
1526/*
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001527 * get_cpu_position_in_core(cpu)
1528 * return the position of the CPU among its HT siblings in the core
1529 * return -1 if the sibling is not in list
Len Brownc98d5d92012-06-04 00:56:40 -04001530 */
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001531int get_cpu_position_in_core(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001532{
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001533 char path[64];
1534 FILE *filep;
1535 int this_cpu;
1536 char character;
1537 int i;
1538
1539 sprintf(path,
1540 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list",
1541 cpu);
1542 filep = fopen(path, "r");
1543 if (filep == NULL) {
1544 perror(path);
1545 exit(1);
1546 }
1547
1548 for (i = 0; i < topo.num_threads_per_core; i++) {
1549 fscanf(filep, "%d", &this_cpu);
1550 if (this_cpu == cpu) {
1551 fclose(filep);
1552 return i;
1553 }
1554
1555 /* Account for no separator after last thread*/
1556 if (i != (topo.num_threads_per_core - 1))
1557 fscanf(filep, "%c", &character);
1558 }
1559
1560 fclose(filep);
1561 return -1;
Len Brown103a8fe2010-10-22 23:53:03 -04001562}
1563
Len Brownc98d5d92012-06-04 00:56:40 -04001564/*
1565 * cpu_is_first_core_in_package(cpu)
1566 * return 1 if given CPU is 1st core in package
1567 */
1568int cpu_is_first_core_in_package(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001569{
Josh Triplett95aebc42013-08-20 17:20:16 -07001570 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001571}
1572
1573int get_physical_package_id(int cpu)
1574{
Josh Triplett95aebc42013-08-20 17:20:16 -07001575 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001576}
1577
1578int get_core_id(int cpu)
1579{
Josh Triplett95aebc42013-08-20 17:20:16 -07001580 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001581}
1582
Len Brownc98d5d92012-06-04 00:56:40 -04001583int get_num_ht_siblings(int cpu)
1584{
1585 char path[80];
1586 FILE *filep;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001587 int sib1;
1588 int matches = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001589 char character;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001590 char str[100];
1591 char *ch;
Len Brownc98d5d92012-06-04 00:56:40 -04001592
1593 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
Josh Triplett57a42a32013-08-20 17:20:17 -07001594 filep = fopen_or_die(path, "r");
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001595
Len Brownc98d5d92012-06-04 00:56:40 -04001596 /*
1597 * file format:
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001598 * A ',' separated or '-' separated set of numbers
1599 * (eg 1-2 or 1,3,4,5)
Len Brownc98d5d92012-06-04 00:56:40 -04001600 */
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001601 fscanf(filep, "%d%c\n", &sib1, &character);
1602 fseek(filep, 0, SEEK_SET);
1603 fgets(str, 100, filep);
1604 ch = strchr(str, character);
1605 while (ch != NULL) {
1606 matches++;
1607 ch = strchr(ch+1, character);
1608 }
Len Brownc98d5d92012-06-04 00:56:40 -04001609
1610 fclose(filep);
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001611 return matches+1;
Len Brownc98d5d92012-06-04 00:56:40 -04001612}
1613
Len Brown103a8fe2010-10-22 23:53:03 -04001614/*
Len Brownc98d5d92012-06-04 00:56:40 -04001615 * run func(thread, core, package) in topology order
1616 * skip non-present cpus
Len Brown103a8fe2010-10-22 23:53:03 -04001617 */
1618
Len Brownc98d5d92012-06-04 00:56:40 -04001619int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
1620 struct pkg_data *, struct thread_data *, struct core_data *,
1621 struct pkg_data *), struct thread_data *thread_base,
1622 struct core_data *core_base, struct pkg_data *pkg_base,
1623 struct thread_data *thread_base2, struct core_data *core_base2,
1624 struct pkg_data *pkg_base2)
1625{
1626 int retval, pkg_no, core_no, thread_no;
1627
1628 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
1629 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
1630 for (thread_no = 0; thread_no <
1631 topo.num_threads_per_core; ++thread_no) {
1632 struct thread_data *t, *t2;
1633 struct core_data *c, *c2;
1634 struct pkg_data *p, *p2;
1635
1636 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
1637
1638 if (cpu_is_not_present(t->cpu_id))
1639 continue;
1640
1641 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
1642
1643 c = GET_CORE(core_base, core_no, pkg_no);
1644 c2 = GET_CORE(core_base2, core_no, pkg_no);
1645
1646 p = GET_PKG(pkg_base, pkg_no);
1647 p2 = GET_PKG(pkg_base2, pkg_no);
1648
1649 retval = func(t, c, p, t2, c2, p2);
1650 if (retval)
1651 return retval;
1652 }
1653 }
1654 }
1655 return 0;
1656}
1657
1658/*
1659 * run func(cpu) on every cpu in /proc/stat
1660 * return max_cpu number
1661 */
1662int for_all_proc_cpus(int (func)(int))
Len Brown103a8fe2010-10-22 23:53:03 -04001663{
1664 FILE *fp;
Len Brownc98d5d92012-06-04 00:56:40 -04001665 int cpu_num;
Len Brown103a8fe2010-10-22 23:53:03 -04001666 int retval;
1667
Josh Triplett57a42a32013-08-20 17:20:17 -07001668 fp = fopen_or_die(proc_stat, "r");
Len Brown103a8fe2010-10-22 23:53:03 -04001669
1670 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
Josh Triplettb2c95d92013-08-20 17:20:18 -07001671 if (retval != 0)
1672 err(1, "%s: failed to parse format", proc_stat);
Len Brown103a8fe2010-10-22 23:53:03 -04001673
Len Brownc98d5d92012-06-04 00:56:40 -04001674 while (1) {
1675 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 -04001676 if (retval != 1)
1677 break;
1678
Len Brownc98d5d92012-06-04 00:56:40 -04001679 retval = func(cpu_num);
1680 if (retval) {
1681 fclose(fp);
1682 return(retval);
1683 }
Len Brown103a8fe2010-10-22 23:53:03 -04001684 }
1685 fclose(fp);
Len Brownc98d5d92012-06-04 00:56:40 -04001686 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001687}
1688
1689void re_initialize(void)
1690{
Len Brownc98d5d92012-06-04 00:56:40 -04001691 free_all_buffers();
1692 setup_all_buffers();
1693 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04001694}
1695
Len Brownc98d5d92012-06-04 00:56:40 -04001696
Len Brown103a8fe2010-10-22 23:53:03 -04001697/*
Len Brownc98d5d92012-06-04 00:56:40 -04001698 * count_cpus()
1699 * remember the last one seen, it will be the max
Len Brown103a8fe2010-10-22 23:53:03 -04001700 */
Len Brownc98d5d92012-06-04 00:56:40 -04001701int count_cpus(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001702{
Len Brownc98d5d92012-06-04 00:56:40 -04001703 if (topo.max_cpu_num < cpu)
1704 topo.max_cpu_num = cpu;
Len Brown103a8fe2010-10-22 23:53:03 -04001705
Len Brownc98d5d92012-06-04 00:56:40 -04001706 topo.num_cpus += 1;
1707 return 0;
1708}
1709int mark_cpu_present(int cpu)
1710{
1711 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brown15aaa342012-03-29 22:19:58 -04001712 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001713}
1714
1715void turbostat_loop()
1716{
Len Brownc98d5d92012-06-04 00:56:40 -04001717 int retval;
Len Browne52966c2012-11-08 22:38:05 -05001718 int restarted = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001719
Len Brown103a8fe2010-10-22 23:53:03 -04001720restart:
Len Browne52966c2012-11-08 22:38:05 -05001721 restarted++;
1722
Len Brownc98d5d92012-06-04 00:56:40 -04001723 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001724 if (retval < -1) {
1725 exit(retval);
1726 } else if (retval == -1) {
Len Browne52966c2012-11-08 22:38:05 -05001727 if (restarted > 1) {
1728 exit(retval);
1729 }
Len Brownc98d5d92012-06-04 00:56:40 -04001730 re_initialize();
1731 goto restart;
1732 }
Len Browne52966c2012-11-08 22:38:05 -05001733 restarted = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001734 gettimeofday(&tv_even, (struct timezone *)NULL);
1735
1736 while (1) {
Len Brownc98d5d92012-06-04 00:56:40 -04001737 if (for_all_proc_cpus(cpu_is_not_present)) {
Len Brown103a8fe2010-10-22 23:53:03 -04001738 re_initialize();
1739 goto restart;
1740 }
Len Brown2a0609c2016-02-12 22:44:48 -05001741 nanosleep(&interval_ts, NULL);
Len Brownc98d5d92012-06-04 00:56:40 -04001742 retval = for_all_cpus(get_counters, ODD_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001743 if (retval < -1) {
1744 exit(retval);
1745 } else if (retval == -1) {
Len Brown15aaa342012-03-29 22:19:58 -04001746 re_initialize();
1747 goto restart;
1748 }
Len Brown103a8fe2010-10-22 23:53:03 -04001749 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04001750 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04001751 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
1752 compute_average(EVEN_COUNTERS);
1753 format_all_counters(EVEN_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05001754 flush_output_stdout();
Len Brown2a0609c2016-02-12 22:44:48 -05001755 nanosleep(&interval_ts, NULL);
Len Brownc98d5d92012-06-04 00:56:40 -04001756 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001757 if (retval < -1) {
1758 exit(retval);
1759 } else if (retval == -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04001760 re_initialize();
1761 goto restart;
1762 }
Len Brown103a8fe2010-10-22 23:53:03 -04001763 gettimeofday(&tv_even, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04001764 timersub(&tv_even, &tv_odd, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04001765 for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS);
1766 compute_average(ODD_COUNTERS);
1767 format_all_counters(ODD_COUNTERS);
Len Brownb7d8c142016-02-13 23:36:17 -05001768 flush_output_stdout();
Len Brown103a8fe2010-10-22 23:53:03 -04001769 }
1770}
1771
1772void check_dev_msr()
1773{
1774 struct stat sb;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001775 char pathname[32];
Len Brown103a8fe2010-10-22 23:53:03 -04001776
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001777 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
1778 if (stat(pathname, &sb))
Len Browna21d38c2015-03-24 16:37:35 -04001779 if (system("/sbin/modprobe msr > /dev/null 2>&1"))
1780 err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
Len Brown103a8fe2010-10-22 23:53:03 -04001781}
1782
Len Brown98481e72014-08-15 00:36:50 -04001783void check_permissions()
Len Brown103a8fe2010-10-22 23:53:03 -04001784{
Len Brown98481e72014-08-15 00:36:50 -04001785 struct __user_cap_header_struct cap_header_data;
1786 cap_user_header_t cap_header = &cap_header_data;
1787 struct __user_cap_data_struct cap_data_data;
1788 cap_user_data_t cap_data = &cap_data_data;
1789 extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
1790 int do_exit = 0;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001791 char pathname[32];
Len Brown98481e72014-08-15 00:36:50 -04001792
1793 /* check for CAP_SYS_RAWIO */
1794 cap_header->pid = getpid();
1795 cap_header->version = _LINUX_CAPABILITY_VERSION;
1796 if (capget(cap_header, cap_data) < 0)
1797 err(-6, "capget(2) failed");
1798
1799 if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
1800 do_exit++;
1801 warnx("capget(CAP_SYS_RAWIO) failed,"
1802 " try \"# setcap cap_sys_rawio=ep %s\"", progname);
1803 }
1804
1805 /* test file permissions */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001806 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
1807 if (euidaccess(pathname, R_OK)) {
Len Brown98481e72014-08-15 00:36:50 -04001808 do_exit++;
1809 warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
1810 }
1811
1812 /* if all else fails, thell them to be root */
1813 if (do_exit)
1814 if (getuid() != 0)
Len Brownd7899442015-01-23 00:12:33 -05001815 warnx("... or simply run as root");
Len Brown98481e72014-08-15 00:36:50 -04001816
1817 if (do_exit)
1818 exit(-6);
Len Brown103a8fe2010-10-22 23:53:03 -04001819}
1820
Len Brownd7899442015-01-23 00:12:33 -05001821/*
1822 * NHM adds support for additional MSRs:
1823 *
1824 * MSR_SMI_COUNT 0x00000034
1825 *
Len Brownec0adc52015-11-12 02:42:31 -05001826 * MSR_PLATFORM_INFO 0x000000ce
Len Brownd7899442015-01-23 00:12:33 -05001827 * MSR_NHM_SNB_PKG_CST_CFG_CTL 0x000000e2
1828 *
1829 * MSR_PKG_C3_RESIDENCY 0x000003f8
1830 * MSR_PKG_C6_RESIDENCY 0x000003f9
1831 * MSR_CORE_C3_RESIDENCY 0x000003fc
1832 * MSR_CORE_C6_RESIDENCY 0x000003fd
1833 *
Len Brownee7e38e2015-02-09 23:39:45 -05001834 * Side effect:
1835 * sets global pkg_cstate_limit to decode MSR_NHM_SNB_PKG_CST_CFG_CTL
Len Brownd7899442015-01-23 00:12:33 -05001836 */
Len Brownee7e38e2015-02-09 23:39:45 -05001837int probe_nhm_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04001838{
Len Brownee7e38e2015-02-09 23:39:45 -05001839 unsigned long long msr;
Len Brown21ed5572015-10-19 22:37:40 -04001840 unsigned int base_ratio;
Len Brownee7e38e2015-02-09 23:39:45 -05001841 int *pkg_cstate_limits;
1842
Len Brown103a8fe2010-10-22 23:53:03 -04001843 if (!genuine_intel)
1844 return 0;
1845
1846 if (family != 6)
1847 return 0;
1848
Len Brown21ed5572015-10-19 22:37:40 -04001849 bclk = discover_bclk(family, model);
1850
Len Brown103a8fe2010-10-22 23:53:03 -04001851 switch (model) {
1852 case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
1853 case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
1854 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
1855 case 0x25: /* Westmere Client - Clarkdale, Arrandale */
1856 case 0x2C: /* Westmere EP - Gulftown */
Len Brownee7e38e2015-02-09 23:39:45 -05001857 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1858 case 0x2F: /* Westmere-EX Xeon - Eagleton */
1859 pkg_cstate_limits = nhm_pkg_cstate_limits;
1860 break;
Len Brown103a8fe2010-10-22 23:53:03 -04001861 case 0x2A: /* SNB */
1862 case 0x2D: /* SNB Xeon */
Len Brown553575f2011-11-18 03:32:01 -05001863 case 0x3A: /* IVB */
Len Brown13006512012-09-26 18:11:31 -04001864 case 0x3E: /* IVB Xeon */
Len Brownee7e38e2015-02-09 23:39:45 -05001865 pkg_cstate_limits = snb_pkg_cstate_limits;
1866 break;
Len Brown70b43402013-01-08 01:26:07 -05001867 case 0x3C: /* HSW */
Len Browne6f9bb32013-12-03 02:19:19 -05001868 case 0x3F: /* HSX */
Len Brown70b43402013-01-08 01:26:07 -05001869 case 0x45: /* HSW */
Len Brown149c2312013-03-15 10:58:02 -04001870 case 0x46: /* HSW */
Len Brown4e8e863f2014-02-27 23:28:53 -05001871 case 0x3D: /* BDW */
Len Brown48a06312015-02-10 15:38:04 -05001872 case 0x47: /* BDW */
Len Brown4e8e863f2014-02-27 23:28:53 -05001873 case 0x4F: /* BDX */
1874 case 0x56: /* BDX-DE */
Len Brown0b2bb692015-03-26 00:50:30 -04001875 case 0x4E: /* SKL */
1876 case 0x5E: /* SKL */
Len Brownee7e38e2015-02-09 23:39:45 -05001877 pkg_cstate_limits = hsw_pkg_cstate_limits;
1878 break;
1879 case 0x37: /* BYT */
1880 case 0x4D: /* AVN */
1881 pkg_cstate_limits = slv_pkg_cstate_limits;
1882 break;
1883 case 0x4C: /* AMT */
1884 pkg_cstate_limits = amt_pkg_cstate_limits;
1885 break;
1886 case 0x57: /* PHI */
1887 pkg_cstate_limits = phi_pkg_cstate_limits;
1888 break;
Len Brown103a8fe2010-10-22 23:53:03 -04001889 default:
1890 return 0;
1891 }
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001892 get_msr(base_cpu, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
Len Browne9257f52015-04-01 21:02:57 -04001893 pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
Len Brownee7e38e2015-02-09 23:39:45 -05001894
Len Brownec0adc52015-11-12 02:42:31 -05001895 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown21ed5572015-10-19 22:37:40 -04001896 base_ratio = (msr >> 8) & 0xFF;
1897
1898 base_hz = base_ratio * bclk * 1000000;
1899 has_base_hz = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05001900 return 1;
Len Brown103a8fe2010-10-22 23:53:03 -04001901}
Len Brownd7899442015-01-23 00:12:33 -05001902int has_nhm_turbo_ratio_limit(unsigned int family, unsigned int model)
1903{
Len Brownd7899442015-01-23 00:12:33 -05001904 switch (model) {
1905 /* Nehalem compatible, but do not include turbo-ratio limit support */
1906 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1907 case 0x2F: /* Westmere-EX Xeon - Eagleton */
Hubert Chrzaniukcbf97ab2016-02-10 14:55:22 +01001908 case 0x57: /* PHI - Knights Landing (different MSR definition) */
Len Brownd7899442015-01-23 00:12:33 -05001909 return 0;
1910 default:
1911 return 1;
1912 }
1913}
Len Brown6574a5d2012-09-21 00:01:31 -04001914int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
1915{
1916 if (!genuine_intel)
1917 return 0;
1918
1919 if (family != 6)
1920 return 0;
1921
1922 switch (model) {
1923 case 0x3E: /* IVB Xeon */
Len Brownfcd17212015-03-23 20:29:09 -04001924 case 0x3F: /* HSW Xeon */
Len Brown6574a5d2012-09-21 00:01:31 -04001925 return 1;
1926 default:
1927 return 0;
1928 }
1929}
Len Brownfcd17212015-03-23 20:29:09 -04001930int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
1931{
1932 if (!genuine_intel)
1933 return 0;
1934
1935 if (family != 6)
1936 return 0;
1937
1938 switch (model) {
1939 case 0x3F: /* HSW Xeon */
1940 return 1;
1941 default:
1942 return 0;
1943 }
1944}
1945
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001946int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
1947{
1948 if (!genuine_intel)
1949 return 0;
1950
1951 if (family != 6)
1952 return 0;
1953
1954 switch (model) {
1955 case 0x57: /* Knights Landing */
1956 return 1;
1957 default:
1958 return 0;
1959 }
1960}
Len Brown6fb31432015-06-17 16:23:45 -04001961int has_config_tdp(unsigned int family, unsigned int model)
1962{
1963 if (!genuine_intel)
1964 return 0;
1965
1966 if (family != 6)
1967 return 0;
1968
1969 switch (model) {
1970 case 0x3A: /* IVB */
Len Brown6fb31432015-06-17 16:23:45 -04001971 case 0x3C: /* HSW */
1972 case 0x3F: /* HSX */
1973 case 0x45: /* HSW */
1974 case 0x46: /* HSW */
1975 case 0x3D: /* BDW */
1976 case 0x47: /* BDW */
1977 case 0x4F: /* BDX */
1978 case 0x56: /* BDX-DE */
1979 case 0x4E: /* SKL */
1980 case 0x5E: /* SKL */
1981
1982 case 0x57: /* Knights Landing */
1983 return 1;
1984 default:
1985 return 0;
1986 }
1987}
1988
Len Brownfcd17212015-03-23 20:29:09 -04001989static void
1990dump_cstate_pstate_config_info(family, model)
1991{
1992 if (!do_nhm_platform_info)
1993 return;
1994
1995 dump_nhm_platform_info();
1996
1997 if (has_hsw_turbo_ratio_limit(family, model))
1998 dump_hsw_turbo_ratio_limits();
1999
2000 if (has_ivt_turbo_ratio_limit(family, model))
2001 dump_ivt_turbo_ratio_limits();
2002
2003 if (has_nhm_turbo_ratio_limit(family, model))
2004 dump_nhm_turbo_ratio_limits();
2005
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002006 if (has_knl_turbo_ratio_limit(family, model))
2007 dump_knl_turbo_ratio_limits();
2008
Len Brown6fb31432015-06-17 16:23:45 -04002009 if (has_config_tdp(family, model))
2010 dump_config_tdp();
2011
Len Brownfcd17212015-03-23 20:29:09 -04002012 dump_nhm_cst_cfg();
2013}
2014
Len Brown6574a5d2012-09-21 00:01:31 -04002015
Len Brown889facb2012-11-08 00:48:57 -05002016/*
2017 * print_epb()
2018 * Decode the ENERGY_PERF_BIAS MSR
2019 */
2020int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2021{
2022 unsigned long long msr;
2023 char *epb_string;
2024 int cpu;
2025
2026 if (!has_epb)
2027 return 0;
2028
2029 cpu = t->cpu_id;
2030
2031 /* EPB is per-package */
2032 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2033 return 0;
2034
2035 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05002036 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05002037 return -1;
2038 }
2039
2040 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
2041 return 0;
2042
Len Browne9be7dd2015-05-26 12:19:37 -04002043 switch (msr & 0xF) {
Len Brown889facb2012-11-08 00:48:57 -05002044 case ENERGY_PERF_BIAS_PERFORMANCE:
2045 epb_string = "performance";
2046 break;
2047 case ENERGY_PERF_BIAS_NORMAL:
2048 epb_string = "balanced";
2049 break;
2050 case ENERGY_PERF_BIAS_POWERSAVE:
2051 epb_string = "powersave";
2052 break;
2053 default:
2054 epb_string = "custom";
2055 break;
2056 }
Len Brownb7d8c142016-02-13 23:36:17 -05002057 fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
Len Brown889facb2012-11-08 00:48:57 -05002058
2059 return 0;
2060}
Len Brown7f5c2582015-12-01 01:36:39 -05002061/*
2062 * print_hwp()
2063 * Decode the MSR_HWP_CAPABILITIES
2064 */
2065int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2066{
2067 unsigned long long msr;
2068 int cpu;
2069
2070 if (!has_hwp)
2071 return 0;
2072
2073 cpu = t->cpu_id;
2074
2075 /* MSR_HWP_CAPABILITIES is per-package */
2076 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2077 return 0;
2078
2079 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05002080 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown7f5c2582015-12-01 01:36:39 -05002081 return -1;
2082 }
2083
2084 if (get_msr(cpu, MSR_PM_ENABLE, &msr))
2085 return 0;
2086
Len Brownb7d8c142016-02-13 23:36:17 -05002087 fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n",
Len Brown7f5c2582015-12-01 01:36:39 -05002088 cpu, msr, (msr & (1 << 0)) ? "" : "No-");
2089
2090 /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
2091 if ((msr & (1 << 0)) == 0)
2092 return 0;
2093
2094 if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
2095 return 0;
2096
Len Brownb7d8c142016-02-13 23:36:17 -05002097 fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002098 "(high 0x%x guar 0x%x eff 0x%x low 0x%x)\n",
2099 cpu, msr,
2100 (unsigned int)HWP_HIGHEST_PERF(msr),
2101 (unsigned int)HWP_GUARANTEED_PERF(msr),
2102 (unsigned int)HWP_MOSTEFFICIENT_PERF(msr),
2103 (unsigned int)HWP_LOWEST_PERF(msr));
2104
2105 if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
2106 return 0;
2107
Len Brownb7d8c142016-02-13 23:36:17 -05002108 fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002109 "(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x pkg 0x%x)\n",
2110 cpu, msr,
2111 (unsigned int)(((msr) >> 0) & 0xff),
2112 (unsigned int)(((msr) >> 8) & 0xff),
2113 (unsigned int)(((msr) >> 16) & 0xff),
2114 (unsigned int)(((msr) >> 24) & 0xff),
2115 (unsigned int)(((msr) >> 32) & 0xff3),
2116 (unsigned int)(((msr) >> 42) & 0x1));
2117
2118 if (has_hwp_pkg) {
2119 if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
2120 return 0;
2121
Len Brownb7d8c142016-02-13 23:36:17 -05002122 fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002123 "(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x)\n",
2124 cpu, msr,
2125 (unsigned int)(((msr) >> 0) & 0xff),
2126 (unsigned int)(((msr) >> 8) & 0xff),
2127 (unsigned int)(((msr) >> 16) & 0xff),
2128 (unsigned int)(((msr) >> 24) & 0xff),
2129 (unsigned int)(((msr) >> 32) & 0xff3));
2130 }
2131 if (has_hwp_notify) {
2132 if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
2133 return 0;
2134
Len Brownb7d8c142016-02-13 23:36:17 -05002135 fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002136 "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
2137 cpu, msr,
2138 ((msr) & 0x1) ? "EN" : "Dis",
2139 ((msr) & 0x2) ? "EN" : "Dis");
2140 }
2141 if (get_msr(cpu, MSR_HWP_STATUS, &msr))
2142 return 0;
2143
Len Brownb7d8c142016-02-13 23:36:17 -05002144 fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
Len Brown7f5c2582015-12-01 01:36:39 -05002145 "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
2146 cpu, msr,
2147 ((msr) & 0x1) ? "" : "No-",
2148 ((msr) & 0x2) ? "" : "No-");
2149
2150 return 0;
2151}
Len Brown889facb2012-11-08 00:48:57 -05002152
Len Brown3a9a9412014-08-15 02:39:52 -04002153/*
2154 * print_perf_limit()
2155 */
2156int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2157{
2158 unsigned long long msr;
2159 int cpu;
2160
2161 cpu = t->cpu_id;
2162
2163 /* per-package */
2164 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2165 return 0;
2166
2167 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05002168 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown3a9a9412014-08-15 02:39:52 -04002169 return -1;
2170 }
2171
2172 if (do_core_perf_limit_reasons) {
2173 get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002174 fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2175 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
Len Browne33cbe82015-03-13 16:30:57 -04002176 (msr & 1 << 15) ? "bit15, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04002177 (msr & 1 << 14) ? "bit14, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04002178 (msr & 1 << 13) ? "Transitions, " : "",
2179 (msr & 1 << 12) ? "MultiCoreTurbo, " : "",
2180 (msr & 1 << 11) ? "PkgPwrL2, " : "",
2181 (msr & 1 << 10) ? "PkgPwrL1, " : "",
2182 (msr & 1 << 9) ? "CorePwr, " : "",
2183 (msr & 1 << 8) ? "Amps, " : "",
2184 (msr & 1 << 6) ? "VR-Therm, " : "",
2185 (msr & 1 << 5) ? "Auto-HWP, " : "",
2186 (msr & 1 << 4) ? "Graphics, " : "",
2187 (msr & 1 << 2) ? "bit2, " : "",
2188 (msr & 1 << 1) ? "ThermStatus, " : "",
2189 (msr & 1 << 0) ? "PROCHOT, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05002190 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 -04002191 (msr & 1 << 31) ? "bit31, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04002192 (msr & 1 << 30) ? "bit30, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04002193 (msr & 1 << 29) ? "Transitions, " : "",
2194 (msr & 1 << 28) ? "MultiCoreTurbo, " : "",
2195 (msr & 1 << 27) ? "PkgPwrL2, " : "",
2196 (msr & 1 << 26) ? "PkgPwrL1, " : "",
2197 (msr & 1 << 25) ? "CorePwr, " : "",
2198 (msr & 1 << 24) ? "Amps, " : "",
2199 (msr & 1 << 22) ? "VR-Therm, " : "",
2200 (msr & 1 << 21) ? "Auto-HWP, " : "",
2201 (msr & 1 << 20) ? "Graphics, " : "",
2202 (msr & 1 << 18) ? "bit18, " : "",
2203 (msr & 1 << 17) ? "ThermStatus, " : "",
2204 (msr & 1 << 16) ? "PROCHOT, " : "");
Len Brown3a9a9412014-08-15 02:39:52 -04002205
2206 }
2207 if (do_gfx_perf_limit_reasons) {
2208 get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002209 fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2210 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
Len Brown3a9a9412014-08-15 02:39:52 -04002211 (msr & 1 << 0) ? "PROCHOT, " : "",
2212 (msr & 1 << 1) ? "ThermStatus, " : "",
2213 (msr & 1 << 4) ? "Graphics, " : "",
2214 (msr & 1 << 6) ? "VR-Therm, " : "",
2215 (msr & 1 << 8) ? "Amps, " : "",
2216 (msr & 1 << 9) ? "GFXPwr, " : "",
2217 (msr & 1 << 10) ? "PkgPwrL1, " : "",
2218 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05002219 fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04002220 (msr & 1 << 16) ? "PROCHOT, " : "",
2221 (msr & 1 << 17) ? "ThermStatus, " : "",
2222 (msr & 1 << 20) ? "Graphics, " : "",
2223 (msr & 1 << 22) ? "VR-Therm, " : "",
2224 (msr & 1 << 24) ? "Amps, " : "",
2225 (msr & 1 << 25) ? "GFXPwr, " : "",
2226 (msr & 1 << 26) ? "PkgPwrL1, " : "",
2227 (msr & 1 << 27) ? "PkgPwrL2, " : "");
2228 }
2229 if (do_ring_perf_limit_reasons) {
2230 get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
Len Brownb7d8c142016-02-13 23:36:17 -05002231 fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2232 fprintf(outf, " (Active: %s%s%s%s%s%s)",
Len Brown3a9a9412014-08-15 02:39:52 -04002233 (msr & 1 << 0) ? "PROCHOT, " : "",
2234 (msr & 1 << 1) ? "ThermStatus, " : "",
2235 (msr & 1 << 6) ? "VR-Therm, " : "",
2236 (msr & 1 << 8) ? "Amps, " : "",
2237 (msr & 1 << 10) ? "PkgPwrL1, " : "",
2238 (msr & 1 << 11) ? "PkgPwrL2, " : "");
Len Brownb7d8c142016-02-13 23:36:17 -05002239 fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
Len Brown3a9a9412014-08-15 02:39:52 -04002240 (msr & 1 << 16) ? "PROCHOT, " : "",
2241 (msr & 1 << 17) ? "ThermStatus, " : "",
2242 (msr & 1 << 22) ? "VR-Therm, " : "",
2243 (msr & 1 << 24) ? "Amps, " : "",
2244 (msr & 1 << 26) ? "PkgPwrL1, " : "",
2245 (msr & 1 << 27) ? "PkgPwrL2, " : "");
2246 }
2247 return 0;
2248}
2249
Len Brown889facb2012-11-08 00:48:57 -05002250#define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
2251#define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
2252
Len Brown144b44b2013-11-09 00:30:16 -05002253double get_tdp(model)
2254{
2255 unsigned long long msr;
2256
2257 if (do_rapl & RAPL_PKG_POWER_INFO)
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002258 if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
Len Brown144b44b2013-11-09 00:30:16 -05002259 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
2260
2261 switch (model) {
2262 case 0x37:
2263 case 0x4D:
2264 return 30.0;
2265 default:
2266 return 135.0;
2267 }
2268}
2269
Andrey Semin40ee8e32014-12-05 00:07:00 -05002270/*
2271 * rapl_dram_energy_units_probe()
2272 * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
2273 */
2274static double
2275rapl_dram_energy_units_probe(int model, double rapl_energy_units)
2276{
2277 /* only called for genuine_intel, family 6 */
2278
2279 switch (model) {
2280 case 0x3F: /* HSX */
2281 case 0x4F: /* BDX */
2282 case 0x56: /* BDX-DE */
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002283 case 0x57: /* KNL */
Andrey Semin40ee8e32014-12-05 00:07:00 -05002284 return (rapl_dram_energy_units = 15.3 / 1000000);
2285 default:
2286 return (rapl_energy_units);
2287 }
2288}
2289
Len Brown144b44b2013-11-09 00:30:16 -05002290
Len Brown889facb2012-11-08 00:48:57 -05002291/*
2292 * rapl_probe()
2293 *
Len Brown144b44b2013-11-09 00:30:16 -05002294 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
Len Brown889facb2012-11-08 00:48:57 -05002295 */
2296void rapl_probe(unsigned int family, unsigned int model)
2297{
2298 unsigned long long msr;
Len Brown144b44b2013-11-09 00:30:16 -05002299 unsigned int time_unit;
Len Brown889facb2012-11-08 00:48:57 -05002300 double tdp;
2301
2302 if (!genuine_intel)
2303 return;
2304
2305 if (family != 6)
2306 return;
2307
2308 switch (model) {
2309 case 0x2A:
2310 case 0x3A:
Len Brown70b43402013-01-08 01:26:07 -05002311 case 0x3C: /* HSW */
Len Brown70b43402013-01-08 01:26:07 -05002312 case 0x45: /* HSW */
Len Brown149c2312013-03-15 10:58:02 -04002313 case 0x46: /* HSW */
Len Brown4e8e863f2014-02-27 23:28:53 -05002314 case 0x3D: /* BDW */
Len Brown48a06312015-02-10 15:38:04 -05002315 case 0x47: /* BDW */
Len Brown144b44b2013-11-09 00:30:16 -05002316 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
Len Brown889facb2012-11-08 00:48:57 -05002317 break;
Len Brown0b2bb692015-03-26 00:50:30 -04002318 case 0x4E: /* SKL */
2319 case 0x5E: /* SKL */
2320 do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
2321 break;
Len Browne6f9bb32013-12-03 02:19:19 -05002322 case 0x3F: /* HSX */
Len Brown4e8e863f2014-02-27 23:28:53 -05002323 case 0x4F: /* BDX */
2324 case 0x56: /* BDX-DE */
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002325 case 0x57: /* KNL */
Len Brown0b2bb692015-03-26 00:50:30 -04002326 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 -05002327 break;
Len Brown889facb2012-11-08 00:48:57 -05002328 case 0x2D:
2329 case 0x3E:
Len Brown0b2bb692015-03-26 00:50:30 -04002330 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 -05002331 break;
2332 case 0x37: /* BYT */
2333 case 0x4D: /* AVN */
2334 do_rapl = RAPL_PKG | RAPL_CORES ;
Len Brown889facb2012-11-08 00:48:57 -05002335 break;
2336 default:
2337 return;
2338 }
2339
2340 /* units on package 0, verify later other packages match */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002341 if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
Len Brown889facb2012-11-08 00:48:57 -05002342 return;
2343
2344 rapl_power_units = 1.0 / (1 << (msr & 0xF));
Len Brown144b44b2013-11-09 00:30:16 -05002345 if (model == 0x37)
2346 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
2347 else
2348 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
Len Brown889facb2012-11-08 00:48:57 -05002349
Andrey Semin40ee8e32014-12-05 00:07:00 -05002350 rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
2351
Len Brown144b44b2013-11-09 00:30:16 -05002352 time_unit = msr >> 16 & 0xF;
2353 if (time_unit == 0)
2354 time_unit = 0xA;
Len Brown889facb2012-11-08 00:48:57 -05002355
Len Brown144b44b2013-11-09 00:30:16 -05002356 rapl_time_units = 1.0 / (1 << (time_unit));
2357
2358 tdp = get_tdp(model);
Len Brown889facb2012-11-08 00:48:57 -05002359
2360 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
Len Brownd8af6f52015-02-10 01:56:38 -05002361 if (debug)
Len Brownb7d8c142016-02-13 23:36:17 -05002362 fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
Len Brown889facb2012-11-08 00:48:57 -05002363
2364 return;
2365}
2366
Len Brown3a9a9412014-08-15 02:39:52 -04002367void perf_limit_reasons_probe(family, model)
2368{
2369 if (!genuine_intel)
2370 return;
2371
2372 if (family != 6)
2373 return;
2374
2375 switch (model) {
2376 case 0x3C: /* HSW */
2377 case 0x45: /* HSW */
2378 case 0x46: /* HSW */
2379 do_gfx_perf_limit_reasons = 1;
2380 case 0x3F: /* HSX */
2381 do_core_perf_limit_reasons = 1;
2382 do_ring_perf_limit_reasons = 1;
2383 default:
2384 return;
2385 }
2386}
2387
Len Brown889facb2012-11-08 00:48:57 -05002388int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2389{
2390 unsigned long long msr;
2391 unsigned int dts;
2392 int cpu;
2393
2394 if (!(do_dts || do_ptm))
2395 return 0;
2396
2397 cpu = t->cpu_id;
2398
2399 /* DTS is per-core, no need to print for each thread */
2400 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
2401 return 0;
2402
2403 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05002404 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05002405 return -1;
2406 }
2407
2408 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
2409 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
2410 return 0;
2411
2412 dts = (msr >> 16) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05002413 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05002414 cpu, msr, tcc_activation_temp - dts);
2415
2416#ifdef THERM_DEBUG
2417 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
2418 return 0;
2419
2420 dts = (msr >> 16) & 0x7F;
2421 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05002422 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05002423 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
2424#endif
2425 }
2426
2427
2428 if (do_dts) {
2429 unsigned int resolution;
2430
2431 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
2432 return 0;
2433
2434 dts = (msr >> 16) & 0x7F;
2435 resolution = (msr >> 27) & 0xF;
Len Brownb7d8c142016-02-13 23:36:17 -05002436 fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
Len Brown889facb2012-11-08 00:48:57 -05002437 cpu, msr, tcc_activation_temp - dts, resolution);
2438
2439#ifdef THERM_DEBUG
2440 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
2441 return 0;
2442
2443 dts = (msr >> 16) & 0x7F;
2444 dts2 = (msr >> 8) & 0x7F;
Len Brownb7d8c142016-02-13 23:36:17 -05002445 fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05002446 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
2447#endif
2448 }
2449
2450 return 0;
2451}
2452
2453void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
2454{
Len Brownb7d8c142016-02-13 23:36:17 -05002455 fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05002456 cpu, label,
2457 ((msr >> 15) & 1) ? "EN" : "DIS",
2458 ((msr >> 0) & 0x7FFF) * rapl_power_units,
2459 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
2460 (((msr >> 16) & 1) ? "EN" : "DIS"));
2461
2462 return;
2463}
2464
2465int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2466{
2467 unsigned long long msr;
2468 int cpu;
Len Brown889facb2012-11-08 00:48:57 -05002469
2470 if (!do_rapl)
2471 return 0;
2472
2473 /* RAPL counters are per package, so print only for 1st thread/package */
2474 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2475 return 0;
2476
2477 cpu = t->cpu_id;
2478 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05002479 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05002480 return -1;
2481 }
2482
2483 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
2484 return -1;
2485
Len Brownd8af6f52015-02-10 01:56:38 -05002486 if (debug) {
Len Brownb7d8c142016-02-13 23:36:17 -05002487 fprintf(outf, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx "
Len Brown889facb2012-11-08 00:48:57 -05002488 "(%f Watts, %f Joules, %f sec.)\n", cpu, msr,
Len Brown144b44b2013-11-09 00:30:16 -05002489 rapl_power_units, rapl_energy_units, rapl_time_units);
Len Brown889facb2012-11-08 00:48:57 -05002490 }
Len Brown144b44b2013-11-09 00:30:16 -05002491 if (do_rapl & RAPL_PKG_POWER_INFO) {
2492
Len Brown889facb2012-11-08 00:48:57 -05002493 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
2494 return -5;
2495
2496
Len Brownb7d8c142016-02-13 23:36:17 -05002497 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 -05002498 cpu, msr,
2499 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2500 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2501 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2502 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
2503
Len Brown144b44b2013-11-09 00:30:16 -05002504 }
2505 if (do_rapl & RAPL_PKG) {
2506
Len Brown889facb2012-11-08 00:48:57 -05002507 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
2508 return -9;
2509
Len Brownb7d8c142016-02-13 23:36:17 -05002510 fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown889facb2012-11-08 00:48:57 -05002511 cpu, msr, (msr >> 63) & 1 ? "": "UN");
2512
2513 print_power_limit_msr(cpu, msr, "PKG Limit #1");
Len Brownb7d8c142016-02-13 23:36:17 -05002514 fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
Len Brown889facb2012-11-08 00:48:57 -05002515 cpu,
2516 ((msr >> 47) & 1) ? "EN" : "DIS",
2517 ((msr >> 32) & 0x7FFF) * rapl_power_units,
2518 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
2519 ((msr >> 48) & 1) ? "EN" : "DIS");
2520 }
2521
Len Brown0b2bb692015-03-26 00:50:30 -04002522 if (do_rapl & RAPL_DRAM_POWER_INFO) {
Len Brown889facb2012-11-08 00:48:57 -05002523 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
2524 return -6;
2525
Len Brownb7d8c142016-02-13 23:36:17 -05002526 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 -05002527 cpu, msr,
2528 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2529 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2530 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2531 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
Len Brown0b2bb692015-03-26 00:50:30 -04002532 }
2533 if (do_rapl & RAPL_DRAM) {
Len Brown889facb2012-11-08 00:48:57 -05002534 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
2535 return -9;
Len Brownb7d8c142016-02-13 23:36:17 -05002536 fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown889facb2012-11-08 00:48:57 -05002537 cpu, msr, (msr >> 31) & 1 ? "": "UN");
2538
2539 print_power_limit_msr(cpu, msr, "DRAM Limit");
2540 }
Len Brown144b44b2013-11-09 00:30:16 -05002541 if (do_rapl & RAPL_CORE_POLICY) {
Len Brownd8af6f52015-02-10 01:56:38 -05002542 if (debug) {
Len Brown889facb2012-11-08 00:48:57 -05002543 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
2544 return -7;
2545
Len Brownb7d8c142016-02-13 23:36:17 -05002546 fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown144b44b2013-11-09 00:30:16 -05002547 }
2548 }
2549 if (do_rapl & RAPL_CORES) {
Len Brownd8af6f52015-02-10 01:56:38 -05002550 if (debug) {
Len Brown889facb2012-11-08 00:48:57 -05002551
2552 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
2553 return -9;
Len Brownb7d8c142016-02-13 23:36:17 -05002554 fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown889facb2012-11-08 00:48:57 -05002555 cpu, msr, (msr >> 31) & 1 ? "": "UN");
2556 print_power_limit_msr(cpu, msr, "Cores Limit");
2557 }
2558 }
2559 if (do_rapl & RAPL_GFX) {
Len Brownd8af6f52015-02-10 01:56:38 -05002560 if (debug) {
Len Brown889facb2012-11-08 00:48:57 -05002561 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
2562 return -8;
2563
Len Brownb7d8c142016-02-13 23:36:17 -05002564 fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown889facb2012-11-08 00:48:57 -05002565
2566 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
2567 return -9;
Len Brownb7d8c142016-02-13 23:36:17 -05002568 fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
Len Brown889facb2012-11-08 00:48:57 -05002569 cpu, msr, (msr >> 31) & 1 ? "": "UN");
2570 print_power_limit_msr(cpu, msr, "GFX Limit");
2571 }
2572 }
2573 return 0;
2574}
2575
Len Brownd7899442015-01-23 00:12:33 -05002576/*
2577 * SNB adds support for additional MSRs:
2578 *
2579 * MSR_PKG_C7_RESIDENCY 0x000003fa
2580 * MSR_CORE_C7_RESIDENCY 0x000003fe
2581 * MSR_PKG_C2_RESIDENCY 0x0000060d
2582 */
Len Brown103a8fe2010-10-22 23:53:03 -04002583
Len Brownd7899442015-01-23 00:12:33 -05002584int has_snb_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04002585{
2586 if (!genuine_intel)
2587 return 0;
2588
2589 switch (model) {
2590 case 0x2A:
2591 case 0x2D:
Len Brown650a37f2012-06-03 23:34:44 -04002592 case 0x3A: /* IVB */
Len Brown13006512012-09-26 18:11:31 -04002593 case 0x3E: /* IVB Xeon */
Len Brown70b43402013-01-08 01:26:07 -05002594 case 0x3C: /* HSW */
2595 case 0x3F: /* HSW */
2596 case 0x45: /* HSW */
Len Brown149c2312013-03-15 10:58:02 -04002597 case 0x46: /* HSW */
Len Brown4e8e863f2014-02-27 23:28:53 -05002598 case 0x3D: /* BDW */
Len Brown48a06312015-02-10 15:38:04 -05002599 case 0x47: /* BDW */
Len Brown4e8e863f2014-02-27 23:28:53 -05002600 case 0x4F: /* BDX */
2601 case 0x56: /* BDX-DE */
Len Brown0b2bb692015-03-26 00:50:30 -04002602 case 0x4E: /* SKL */
2603 case 0x5E: /* SKL */
Len Brown103a8fe2010-10-22 23:53:03 -04002604 return 1;
2605 }
2606 return 0;
2607}
2608
Len Brownd7899442015-01-23 00:12:33 -05002609/*
2610 * HSW adds support for additional MSRs:
2611 *
2612 * MSR_PKG_C8_RESIDENCY 0x00000630
2613 * MSR_PKG_C9_RESIDENCY 0x00000631
2614 * MSR_PKG_C10_RESIDENCY 0x00000632
2615 */
2616int has_hsw_msrs(unsigned int family, unsigned int model)
Kristen Carlson Accardica587102012-11-21 05:22:43 -08002617{
2618 if (!genuine_intel)
2619 return 0;
2620
2621 switch (model) {
Len Brown4e8e863f2014-02-27 23:28:53 -05002622 case 0x45: /* HSW */
2623 case 0x3D: /* BDW */
Len Brown0b2bb692015-03-26 00:50:30 -04002624 case 0x4E: /* SKL */
2625 case 0x5E: /* SKL */
Kristen Carlson Accardica587102012-11-21 05:22:43 -08002626 return 1;
2627 }
2628 return 0;
2629}
2630
Len Brown0b2bb692015-03-26 00:50:30 -04002631/*
2632 * SKL adds support for additional MSRS:
2633 *
2634 * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658
2635 * MSR_PKG_ANY_CORE_C0_RES 0x00000659
2636 * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A
2637 * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B
2638 */
2639int has_skl_msrs(unsigned int family, unsigned int model)
2640{
2641 if (!genuine_intel)
2642 return 0;
2643
2644 switch (model) {
2645 case 0x4E: /* SKL */
2646 case 0x5E: /* SKL */
2647 return 1;
2648 }
2649 return 0;
2650}
2651
2652
Kristen Carlson Accardica587102012-11-21 05:22:43 -08002653
Len Brown144b44b2013-11-09 00:30:16 -05002654int is_slm(unsigned int family, unsigned int model)
2655{
2656 if (!genuine_intel)
2657 return 0;
2658 switch (model) {
2659 case 0x37: /* BYT */
2660 case 0x4D: /* AVN */
2661 return 1;
2662 }
2663 return 0;
2664}
2665
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002666int is_knl(unsigned int family, unsigned int model)
2667{
2668 if (!genuine_intel)
2669 return 0;
2670 switch (model) {
2671 case 0x57: /* KNL */
2672 return 1;
2673 }
2674 return 0;
2675}
2676
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02002677unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
2678{
2679 if (is_knl(family, model))
2680 return 1024;
2681 return 1;
2682}
2683
Len Brown144b44b2013-11-09 00:30:16 -05002684#define SLM_BCLK_FREQS 5
2685double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
2686
2687double slm_bclk(void)
2688{
2689 unsigned long long msr = 3;
2690 unsigned int i;
2691 double freq;
2692
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002693 if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
Len Brownb7d8c142016-02-13 23:36:17 -05002694 fprintf(outf, "SLM BCLK: unknown\n");
Len Brown144b44b2013-11-09 00:30:16 -05002695
2696 i = msr & 0xf;
2697 if (i >= SLM_BCLK_FREQS) {
Len Brownb7d8c142016-02-13 23:36:17 -05002698 fprintf(outf, "SLM BCLK[%d] invalid\n", i);
Len Brown144b44b2013-11-09 00:30:16 -05002699 msr = 3;
2700 }
2701 freq = slm_freq_table[i];
2702
Len Brownb7d8c142016-02-13 23:36:17 -05002703 fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
Len Brown144b44b2013-11-09 00:30:16 -05002704
2705 return freq;
2706}
2707
Len Brown103a8fe2010-10-22 23:53:03 -04002708double discover_bclk(unsigned int family, unsigned int model)
2709{
Chrzaniuk, Hubert121b48b2016-02-10 16:35:17 +01002710 if (has_snb_msrs(family, model) || is_knl(family, model))
Len Brown103a8fe2010-10-22 23:53:03 -04002711 return 100.00;
Len Brown144b44b2013-11-09 00:30:16 -05002712 else if (is_slm(family, model))
2713 return slm_bclk();
Len Brown103a8fe2010-10-22 23:53:03 -04002714 else
2715 return 133.33;
2716}
2717
Len Brown889facb2012-11-08 00:48:57 -05002718/*
2719 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
2720 * the Thermal Control Circuit (TCC) activates.
2721 * This is usually equal to tjMax.
2722 *
2723 * Older processors do not have this MSR, so there we guess,
2724 * but also allow cmdline over-ride with -T.
2725 *
2726 * Several MSR temperature values are in units of degrees-C
2727 * below this value, including the Digital Thermal Sensor (DTS),
2728 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
2729 */
2730int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2731{
2732 unsigned long long msr;
2733 unsigned int target_c_local;
2734 int cpu;
2735
2736 /* tcc_activation_temp is used only for dts or ptm */
2737 if (!(do_dts || do_ptm))
2738 return 0;
2739
2740 /* this is a per-package concept */
2741 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2742 return 0;
2743
2744 cpu = t->cpu_id;
2745 if (cpu_migrate(cpu)) {
Len Brownb7d8c142016-02-13 23:36:17 -05002746 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
Len Brown889facb2012-11-08 00:48:57 -05002747 return -1;
2748 }
2749
2750 if (tcc_activation_temp_override != 0) {
2751 tcc_activation_temp = tcc_activation_temp_override;
Len Brownb7d8c142016-02-13 23:36:17 -05002752 fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05002753 cpu, tcc_activation_temp);
2754 return 0;
2755 }
2756
2757 /* Temperature Target MSR is Nehalem and newer only */
Len Brownd7899442015-01-23 00:12:33 -05002758 if (!do_nhm_platform_info)
Len Brown889facb2012-11-08 00:48:57 -05002759 goto guess;
2760
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002761 if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
Len Brown889facb2012-11-08 00:48:57 -05002762 goto guess;
2763
Jean Delvare34821242014-05-01 11:40:19 +02002764 target_c_local = (msr >> 16) & 0xFF;
Len Brown889facb2012-11-08 00:48:57 -05002765
Len Brownd8af6f52015-02-10 01:56:38 -05002766 if (debug)
Len Brownb7d8c142016-02-13 23:36:17 -05002767 fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
Len Brown889facb2012-11-08 00:48:57 -05002768 cpu, msr, target_c_local);
2769
Jean Delvare34821242014-05-01 11:40:19 +02002770 if (!target_c_local)
Len Brown889facb2012-11-08 00:48:57 -05002771 goto guess;
2772
2773 tcc_activation_temp = target_c_local;
2774
2775 return 0;
2776
2777guess:
2778 tcc_activation_temp = TJMAX_DEFAULT;
Len Brownb7d8c142016-02-13 23:36:17 -05002779 fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
Len Brown889facb2012-11-08 00:48:57 -05002780 cpu, tcc_activation_temp);
2781
2782 return 0;
2783}
Len Brown69807a62015-11-21 12:22:47 -05002784
2785void decode_misc_enable_msr(void)
2786{
2787 unsigned long long msr;
2788
2789 if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
Len Brownb7d8c142016-02-13 23:36:17 -05002790 fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%s %s %s)\n",
Len Brown69807a62015-11-21 12:22:47 -05002791 base_cpu, msr,
2792 msr & (1 << 3) ? "TCC" : "",
2793 msr & (1 << 16) ? "EIST" : "",
2794 msr & (1 << 18) ? "MONITOR" : "");
2795}
2796
Len Brownf0057312015-12-03 01:35:36 -05002797/*
2798 * Decode MSR_MISC_PWR_MGMT
2799 *
2800 * Decode the bits according to the Nehalem documentation
2801 * bit[0] seems to continue to have same meaning going forward
2802 * bit[1] less so...
2803 */
2804void decode_misc_pwr_mgmt_msr(void)
2805{
2806 unsigned long long msr;
2807
2808 if (!do_nhm_platform_info)
2809 return;
2810
2811 if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
Len Brownb7d8c142016-02-13 23:36:17 -05002812 fprintf(outf, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB)\n",
Len Brownf0057312015-12-03 01:35:36 -05002813 base_cpu, msr,
2814 msr & (1 << 0) ? "DIS" : "EN",
2815 msr & (1 << 1) ? "EN" : "DIS");
2816}
Len Brown7f5c2582015-12-01 01:36:39 -05002817
Len Brownfcd17212015-03-23 20:29:09 -04002818void process_cpuid()
Len Brown103a8fe2010-10-22 23:53:03 -04002819{
Len Brown61a87ba2015-11-23 02:30:51 -05002820 unsigned int eax, ebx, ecx, edx, max_level, max_extended_level;
Len Brown103a8fe2010-10-22 23:53:03 -04002821 unsigned int fms, family, model, stepping;
2822
2823 eax = ebx = ecx = edx = 0;
2824
Josh Triplett2b928652013-08-20 17:20:14 -07002825 __get_cpuid(0, &max_level, &ebx, &ecx, &edx);
Len Brown103a8fe2010-10-22 23:53:03 -04002826
2827 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
2828 genuine_intel = 1;
2829
Len Brownd8af6f52015-02-10 01:56:38 -05002830 if (debug)
Len Brownb7d8c142016-02-13 23:36:17 -05002831 fprintf(outf, "CPUID(0): %.4s%.4s%.4s ",
Len Brown103a8fe2010-10-22 23:53:03 -04002832 (char *)&ebx, (char *)&edx, (char *)&ecx);
2833
Josh Triplett2b928652013-08-20 17:20:14 -07002834 __get_cpuid(1, &fms, &ebx, &ecx, &edx);
Len Brown103a8fe2010-10-22 23:53:03 -04002835 family = (fms >> 8) & 0xf;
2836 model = (fms >> 4) & 0xf;
2837 stepping = fms & 0xf;
2838 if (family == 6 || family == 0xf)
2839 model += ((fms >> 16) & 0xf) << 4;
2840
Len Brown69807a62015-11-21 12:22:47 -05002841 if (debug) {
Len Brownb7d8c142016-02-13 23:36:17 -05002842 fprintf(outf, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
Len Brown103a8fe2010-10-22 23:53:03 -04002843 max_level, family, model, stepping, family, model, stepping);
Len Brownb7d8c142016-02-13 23:36:17 -05002844 fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s\n",
Len Brown69807a62015-11-21 12:22:47 -05002845 ecx & (1 << 0) ? "SSE3" : "-",
2846 ecx & (1 << 3) ? "MONITOR" : "-",
2847 ecx & (1 << 7) ? "EIST" : "-",
2848 ecx & (1 << 8) ? "TM2" : "-",
2849 edx & (1 << 4) ? "TSC" : "-",
2850 edx & (1 << 5) ? "MSR" : "-",
2851 edx & (1 << 22) ? "ACPI-TM" : "-",
2852 edx & (1 << 29) ? "TM" : "-");
2853 }
Len Brown103a8fe2010-10-22 23:53:03 -04002854
Josh Triplettb2c95d92013-08-20 17:20:18 -07002855 if (!(edx & (1 << 5)))
2856 errx(1, "CPUID: no MSR");
Len Brown103a8fe2010-10-22 23:53:03 -04002857
2858 /*
2859 * check max extended function levels of CPUID.
2860 * This is needed to check for invariant TSC.
2861 * This check is valid for both Intel and AMD.
2862 */
2863 ebx = ecx = edx = 0;
Len Brown61a87ba2015-11-23 02:30:51 -05002864 __get_cpuid(0x80000000, &max_extended_level, &ebx, &ecx, &edx);
Len Brown103a8fe2010-10-22 23:53:03 -04002865
Len Brown61a87ba2015-11-23 02:30:51 -05002866 if (max_extended_level >= 0x80000007) {
Len Brown103a8fe2010-10-22 23:53:03 -04002867
Len Brownd7899442015-01-23 00:12:33 -05002868 /*
2869 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
2870 * this check is valid for both Intel and AMD
2871 */
2872 __get_cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
2873 has_invariant_tsc = edx & (1 << 8);
2874 }
Len Brown103a8fe2010-10-22 23:53:03 -04002875
2876 /*
2877 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
2878 * this check is valid for both Intel and AMD
2879 */
2880
Josh Triplett2b928652013-08-20 17:20:14 -07002881 __get_cpuid(0x6, &eax, &ebx, &ecx, &edx);
Thomas Renninger8209e052011-01-21 15:11:19 +01002882 has_aperf = ecx & (1 << 0);
Len Brown889facb2012-11-08 00:48:57 -05002883 do_dts = eax & (1 << 0);
2884 do_ptm = eax & (1 << 6);
Len Brown7f5c2582015-12-01 01:36:39 -05002885 has_hwp = eax & (1 << 7);
2886 has_hwp_notify = eax & (1 << 8);
2887 has_hwp_activity_window = eax & (1 << 9);
2888 has_hwp_epp = eax & (1 << 10);
2889 has_hwp_pkg = eax & (1 << 11);
Len Brown889facb2012-11-08 00:48:57 -05002890 has_epb = ecx & (1 << 3);
2891
Len Brownd8af6f52015-02-10 01:56:38 -05002892 if (debug)
Len Brownb7d8c142016-02-13 23:36:17 -05002893 fprintf(outf, "CPUID(6): %sAPERF, %sDTS, %sPTM, %sHWP, "
Len Brown7f5c2582015-12-01 01:36:39 -05002894 "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
2895 has_aperf ? "" : "No-",
2896 do_dts ? "" : "No-",
2897 do_ptm ? "" : "No-",
2898 has_hwp ? "" : "No-",
2899 has_hwp_notify ? "" : "No-",
2900 has_hwp_activity_window ? "" : "No-",
2901 has_hwp_epp ? "" : "No-",
2902 has_hwp_pkg ? "" : "No-",
2903 has_epb ? "" : "No-");
Len Brown103a8fe2010-10-22 23:53:03 -04002904
Len Brown69807a62015-11-21 12:22:47 -05002905 if (debug)
2906 decode_misc_enable_msr();
2907
Len Brown61a87ba2015-11-23 02:30:51 -05002908 if (max_level >= 0x15) {
Len Brown8a5bdf42015-04-01 21:02:57 -04002909 unsigned int eax_crystal;
2910 unsigned int ebx_tsc;
2911
2912 /*
2913 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
2914 */
2915 eax_crystal = ebx_tsc = crystal_hz = edx = 0;
2916 __get_cpuid(0x15, &eax_crystal, &ebx_tsc, &crystal_hz, &edx);
2917
2918 if (ebx_tsc != 0) {
2919
2920 if (debug && (ebx != 0))
Len Brownb7d8c142016-02-13 23:36:17 -05002921 fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04002922 eax_crystal, ebx_tsc, crystal_hz);
2923
2924 if (crystal_hz == 0)
2925 switch(model) {
2926 case 0x4E: /* SKL */
2927 case 0x5E: /* SKL */
2928 crystal_hz = 24000000; /* 24 MHz */
2929 break;
2930 default:
2931 crystal_hz = 0;
2932 }
2933
2934 if (crystal_hz) {
2935 tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
2936 if (debug)
Len Brownb7d8c142016-02-13 23:36:17 -05002937 fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
Len Brown8a5bdf42015-04-01 21:02:57 -04002938 tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal);
2939 }
2940 }
2941 }
Len Brown61a87ba2015-11-23 02:30:51 -05002942 if (max_level >= 0x16) {
2943 unsigned int base_mhz, max_mhz, bus_mhz, edx;
2944
2945 /*
2946 * CPUID 16H Base MHz, Max MHz, Bus MHz
2947 */
2948 base_mhz = max_mhz = bus_mhz = edx = 0;
2949
2950 __get_cpuid(0x16, &base_mhz, &max_mhz, &bus_mhz, &edx);
2951 if (debug)
Len Brownb7d8c142016-02-13 23:36:17 -05002952 fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
Len Brown61a87ba2015-11-23 02:30:51 -05002953 base_mhz, max_mhz, bus_mhz);
2954 }
Len Brown8a5bdf42015-04-01 21:02:57 -04002955
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02002956 if (has_aperf)
2957 aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
2958
Len Brownee7e38e2015-02-09 23:39:45 -05002959 do_nhm_platform_info = do_nhm_cstates = do_smi = probe_nhm_msrs(family, model);
Len Brownd7899442015-01-23 00:12:33 -05002960 do_snb_cstates = has_snb_msrs(family, model);
Len Brownee7e38e2015-02-09 23:39:45 -05002961 do_pc2 = do_snb_cstates && (pkg_cstate_limit >= PCL__2);
2962 do_pc3 = (pkg_cstate_limit >= PCL__3);
2963 do_pc6 = (pkg_cstate_limit >= PCL__6);
2964 do_pc7 = do_snb_cstates && (pkg_cstate_limit >= PCL__7);
Len Brownd7899442015-01-23 00:12:33 -05002965 do_c8_c9_c10 = has_hsw_msrs(family, model);
Len Brown0b2bb692015-03-26 00:50:30 -04002966 do_skl_residency = has_skl_msrs(family, model);
Len Brown144b44b2013-11-09 00:30:16 -05002967 do_slm_cstates = is_slm(family, model);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002968 do_knl_cstates = is_knl(family, model);
Len Brown103a8fe2010-10-22 23:53:03 -04002969
Len Brownf0057312015-12-03 01:35:36 -05002970 if (debug)
2971 decode_misc_pwr_mgmt_msr();
2972
Len Brown889facb2012-11-08 00:48:57 -05002973 rapl_probe(family, model);
Len Brown3a9a9412014-08-15 02:39:52 -04002974 perf_limit_reasons_probe(family, model);
Len Brown889facb2012-11-08 00:48:57 -05002975
Len Brownfcd17212015-03-23 20:29:09 -04002976 if (debug)
2977 dump_cstate_pstate_config_info();
2978
Len Browna2b7b742015-09-26 00:12:38 -04002979 if (has_skl_msrs(family, model))
2980 calculate_tsc_tweak();
2981
Len Brown889facb2012-11-08 00:48:57 -05002982 return;
Len Brown103a8fe2010-10-22 23:53:03 -04002983}
2984
Len Brownd8af6f52015-02-10 01:56:38 -05002985void help()
Len Brown103a8fe2010-10-22 23:53:03 -04002986{
Len Brownb7d8c142016-02-13 23:36:17 -05002987 fprintf(outf,
Len Brownd8af6f52015-02-10 01:56:38 -05002988 "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
2989 "\n"
2990 "Turbostat forks the specified COMMAND and prints statistics\n"
2991 "when COMMAND completes.\n"
2992 "If no COMMAND is specified, turbostat wakes every 5-seconds\n"
2993 "to print statistics, until interrupted.\n"
2994 "--debug run in \"debug\" mode\n"
2995 "--interval sec Override default 5-second measurement interval\n"
2996 "--help print this help message\n"
2997 "--counter msr print 32-bit counter at address \"msr\"\n"
2998 "--Counter msr print 64-bit Counter at address \"msr\"\n"
Len Brownb7d8c142016-02-13 23:36:17 -05002999 "--out file create or truncate \"file\" for all output\n"
Len Brownd8af6f52015-02-10 01:56:38 -05003000 "--msr msr print 32-bit value at address \"msr\"\n"
3001 "--MSR msr print 64-bit Value at address \"msr\"\n"
3002 "--version print version information\n"
3003 "\n"
3004 "For more help, run \"man turbostat\"\n");
Len Brown103a8fe2010-10-22 23:53:03 -04003005}
3006
3007
3008/*
3009 * in /dev/cpu/ return success for names that are numbers
3010 * ie. filter out ".", "..", "microcode".
3011 */
3012int dir_filter(const struct dirent *dirp)
3013{
3014 if (isdigit(dirp->d_name[0]))
3015 return 1;
3016 else
3017 return 0;
3018}
3019
3020int open_dev_cpu_msr(int dummy1)
3021{
3022 return 0;
3023}
3024
Len Brownc98d5d92012-06-04 00:56:40 -04003025void topology_probe()
3026{
3027 int i;
3028 int max_core_id = 0;
3029 int max_package_id = 0;
3030 int max_siblings = 0;
3031 struct cpu_topology {
3032 int core_id;
3033 int physical_package_id;
3034 } *cpus;
3035
3036 /* Initialize num_cpus, max_cpu_num */
3037 topo.num_cpus = 0;
3038 topo.max_cpu_num = 0;
3039 for_all_proc_cpus(count_cpus);
3040 if (!summary_only && topo.num_cpus > 1)
3041 show_cpu = 1;
3042
Len Brownd8af6f52015-02-10 01:56:38 -05003043 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05003044 fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
Len Brownc98d5d92012-06-04 00:56:40 -04003045
3046 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
Josh Triplettb2c95d92013-08-20 17:20:18 -07003047 if (cpus == NULL)
3048 err(1, "calloc cpus");
Len Brownc98d5d92012-06-04 00:56:40 -04003049
3050 /*
3051 * Allocate and initialize cpu_present_set
3052 */
3053 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07003054 if (cpu_present_set == NULL)
3055 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04003056 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
3057 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
3058 for_all_proc_cpus(mark_cpu_present);
3059
3060 /*
3061 * Allocate and initialize cpu_affinity_set
3062 */
3063 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07003064 if (cpu_affinity_set == NULL)
3065 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04003066 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
3067 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
3068
3069
3070 /*
3071 * For online cpus
3072 * find max_core_id, max_package_id
3073 */
3074 for (i = 0; i <= topo.max_cpu_num; ++i) {
3075 int siblings;
3076
3077 if (cpu_is_not_present(i)) {
Len Brownd8af6f52015-02-10 01:56:38 -05003078 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05003079 fprintf(outf, "cpu%d NOT PRESENT\n", i);
Len Brownc98d5d92012-06-04 00:56:40 -04003080 continue;
3081 }
3082 cpus[i].core_id = get_core_id(i);
3083 if (cpus[i].core_id > max_core_id)
3084 max_core_id = cpus[i].core_id;
3085
3086 cpus[i].physical_package_id = get_physical_package_id(i);
3087 if (cpus[i].physical_package_id > max_package_id)
3088 max_package_id = cpus[i].physical_package_id;
3089
3090 siblings = get_num_ht_siblings(i);
3091 if (siblings > max_siblings)
3092 max_siblings = siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05003093 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05003094 fprintf(outf, "cpu %d pkg %d core %d\n",
Len Brownc98d5d92012-06-04 00:56:40 -04003095 i, cpus[i].physical_package_id, cpus[i].core_id);
3096 }
3097 topo.num_cores_per_pkg = max_core_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05003098 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05003099 fprintf(outf, "max_core_id %d, sizing for %d cores per package\n",
Len Brownc98d5d92012-06-04 00:56:40 -04003100 max_core_id, topo.num_cores_per_pkg);
Len Brown1cc21f72015-02-23 00:34:57 -05003101 if (debug && !summary_only && topo.num_cores_per_pkg > 1)
Len Brownc98d5d92012-06-04 00:56:40 -04003102 show_core = 1;
3103
3104 topo.num_packages = max_package_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05003105 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05003106 fprintf(outf, "max_package_id %d, sizing for %d packages\n",
Len Brownc98d5d92012-06-04 00:56:40 -04003107 max_package_id, topo.num_packages);
Len Brown1cc21f72015-02-23 00:34:57 -05003108 if (debug && !summary_only && topo.num_packages > 1)
Len Brownc98d5d92012-06-04 00:56:40 -04003109 show_pkg = 1;
3110
3111 topo.num_threads_per_core = max_siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05003112 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05003113 fprintf(outf, "max_siblings %d\n", max_siblings);
Len Brownc98d5d92012-06-04 00:56:40 -04003114
3115 free(cpus);
3116}
3117
3118void
3119allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
3120{
3121 int i;
3122
3123 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
3124 topo.num_packages, sizeof(struct thread_data));
3125 if (*t == NULL)
3126 goto error;
3127
3128 for (i = 0; i < topo.num_threads_per_core *
3129 topo.num_cores_per_pkg * topo.num_packages; i++)
3130 (*t)[i].cpu_id = -1;
3131
3132 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
3133 sizeof(struct core_data));
3134 if (*c == NULL)
3135 goto error;
3136
3137 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
3138 (*c)[i].core_id = -1;
3139
3140 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
3141 if (*p == NULL)
3142 goto error;
3143
3144 for (i = 0; i < topo.num_packages; i++)
3145 (*p)[i].package_id = i;
3146
3147 return;
3148error:
Josh Triplettb2c95d92013-08-20 17:20:18 -07003149 err(1, "calloc counters");
Len Brownc98d5d92012-06-04 00:56:40 -04003150}
3151/*
3152 * init_counter()
3153 *
3154 * set cpu_id, core_num, pkg_num
3155 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
3156 *
3157 * increment topo.num_cores when 1st core in pkg seen
3158 */
3159void init_counter(struct thread_data *thread_base, struct core_data *core_base,
3160 struct pkg_data *pkg_base, int thread_num, int core_num,
3161 int pkg_num, int cpu_id)
3162{
3163 struct thread_data *t;
3164 struct core_data *c;
3165 struct pkg_data *p;
3166
3167 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
3168 c = GET_CORE(core_base, core_num, pkg_num);
3169 p = GET_PKG(pkg_base, pkg_num);
3170
3171 t->cpu_id = cpu_id;
3172 if (thread_num == 0) {
3173 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
3174 if (cpu_is_first_core_in_package(cpu_id))
3175 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
3176 }
3177
3178 c->core_id = core_num;
3179 p->package_id = pkg_num;
3180}
3181
3182
3183int initialize_counters(int cpu_id)
3184{
3185 int my_thread_id, my_core_id, my_package_id;
3186
3187 my_package_id = get_physical_package_id(cpu_id);
3188 my_core_id = get_core_id(cpu_id);
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07003189 my_thread_id = get_cpu_position_in_core(cpu_id);
3190 if (!my_thread_id)
Len Brownc98d5d92012-06-04 00:56:40 -04003191 topo.num_cores++;
Len Brownc98d5d92012-06-04 00:56:40 -04003192
3193 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
3194 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
3195 return 0;
3196}
3197
3198void allocate_output_buffer()
3199{
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02003200 output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
Len Brownc98d5d92012-06-04 00:56:40 -04003201 outp = output_buffer;
Josh Triplettb2c95d92013-08-20 17:20:18 -07003202 if (outp == NULL)
3203 err(-1, "calloc output buffer");
Len Brownc98d5d92012-06-04 00:56:40 -04003204}
3205
3206void setup_all_buffers(void)
3207{
3208 topology_probe();
3209 allocate_counters(&thread_even, &core_even, &package_even);
3210 allocate_counters(&thread_odd, &core_odd, &package_odd);
3211 allocate_output_buffer();
3212 for_all_proc_cpus(initialize_counters);
3213}
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02003214
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003215void set_base_cpu(void)
3216{
3217 base_cpu = sched_getcpu();
3218 if (base_cpu < 0)
3219 err(-ENODEV, "No valid cpus found");
3220
3221 if (debug > 1)
Len Brownb7d8c142016-02-13 23:36:17 -05003222 fprintf(outf, "base_cpu = %d\n", base_cpu);
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003223}
3224
Len Brown103a8fe2010-10-22 23:53:03 -04003225void turbostat_init()
3226{
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003227 setup_all_buffers();
3228 set_base_cpu();
Len Brown103a8fe2010-10-22 23:53:03 -04003229 check_dev_msr();
Len Brown98481e72014-08-15 00:36:50 -04003230 check_permissions();
Len Brownfcd17212015-03-23 20:29:09 -04003231 process_cpuid();
Len Brown103a8fe2010-10-22 23:53:03 -04003232
Len Brown103a8fe2010-10-22 23:53:03 -04003233
Len Brownd8af6f52015-02-10 01:56:38 -05003234 if (debug)
Len Brown7f5c2582015-12-01 01:36:39 -05003235 for_all_cpus(print_hwp, ODD_COUNTERS);
3236
3237 if (debug)
Len Brown889facb2012-11-08 00:48:57 -05003238 for_all_cpus(print_epb, ODD_COUNTERS);
3239
Len Brownd8af6f52015-02-10 01:56:38 -05003240 if (debug)
Len Brown3a9a9412014-08-15 02:39:52 -04003241 for_all_cpus(print_perf_limit, ODD_COUNTERS);
3242
Len Brownd8af6f52015-02-10 01:56:38 -05003243 if (debug)
Len Brown889facb2012-11-08 00:48:57 -05003244 for_all_cpus(print_rapl, ODD_COUNTERS);
3245
3246 for_all_cpus(set_temperature_target, ODD_COUNTERS);
3247
Len Brownd8af6f52015-02-10 01:56:38 -05003248 if (debug)
Len Brown889facb2012-11-08 00:48:57 -05003249 for_all_cpus(print_thermal, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04003250}
3251
3252int fork_it(char **argv)
3253{
Len Brown103a8fe2010-10-22 23:53:03 -04003254 pid_t child_pid;
Len Brownd91bb172012-11-01 00:08:19 -04003255 int status;
Len Brownd15cf7c2012-06-03 23:24:00 -04003256
Len Brownd91bb172012-11-01 00:08:19 -04003257 status = for_all_cpus(get_counters, EVEN_COUNTERS);
3258 if (status)
3259 exit(status);
Len Brownc98d5d92012-06-04 00:56:40 -04003260 /* clear affinity side-effect of get_counters() */
3261 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
Len Brown103a8fe2010-10-22 23:53:03 -04003262 gettimeofday(&tv_even, (struct timezone *)NULL);
3263
3264 child_pid = fork();
3265 if (!child_pid) {
3266 /* child */
3267 execvp(argv[0], argv);
3268 } else {
Len Brown103a8fe2010-10-22 23:53:03 -04003269
3270 /* parent */
Josh Triplettb2c95d92013-08-20 17:20:18 -07003271 if (child_pid == -1)
3272 err(1, "fork");
Len Brown103a8fe2010-10-22 23:53:03 -04003273
3274 signal(SIGINT, SIG_IGN);
3275 signal(SIGQUIT, SIG_IGN);
Josh Triplettb2c95d92013-08-20 17:20:18 -07003276 if (waitpid(child_pid, &status, 0) == -1)
3277 err(status, "waitpid");
Len Brown103a8fe2010-10-22 23:53:03 -04003278 }
Len Brownc98d5d92012-06-04 00:56:40 -04003279 /*
3280 * n.b. fork_it() does not check for errors from for_all_cpus()
3281 * because re-starting is problematic when forking
3282 */
3283 for_all_cpus(get_counters, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04003284 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04003285 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04003286 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
3287 compute_average(EVEN_COUNTERS);
3288 format_all_counters(EVEN_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04003289
Len Brownb7d8c142016-02-13 23:36:17 -05003290 fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
3291
3292 flush_output_stderr();
Len Brown103a8fe2010-10-22 23:53:03 -04003293
Len Brownd91bb172012-11-01 00:08:19 -04003294 return status;
Len Brown103a8fe2010-10-22 23:53:03 -04003295}
3296
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02003297int get_and_dump_counters(void)
3298{
3299 int status;
3300
3301 status = for_all_cpus(get_counters, ODD_COUNTERS);
3302 if (status)
3303 return status;
3304
3305 status = for_all_cpus(dump_counters, ODD_COUNTERS);
3306 if (status)
3307 return status;
3308
Len Brownb7d8c142016-02-13 23:36:17 -05003309 flush_output_stdout();
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02003310
3311 return status;
3312}
3313
Len Brownd8af6f52015-02-10 01:56:38 -05003314void print_version() {
Len Brownb7d8c142016-02-13 23:36:17 -05003315 fprintf(outf, "turbostat version 4.10 10 Dec, 2015"
Len Brownd8af6f52015-02-10 01:56:38 -05003316 " - Len Brown <lenb@kernel.org>\n");
3317}
3318
Len Brown103a8fe2010-10-22 23:53:03 -04003319void cmdline(int argc, char **argv)
3320{
3321 int opt;
Len Brownd8af6f52015-02-10 01:56:38 -05003322 int option_index = 0;
3323 static struct option long_options[] = {
3324 {"Counter", required_argument, 0, 'C'},
3325 {"counter", required_argument, 0, 'c'},
3326 {"Dump", no_argument, 0, 'D'},
3327 {"debug", no_argument, 0, 'd'},
3328 {"interval", required_argument, 0, 'i'},
3329 {"help", no_argument, 0, 'h'},
3330 {"Joules", no_argument, 0, 'J'},
3331 {"MSR", required_argument, 0, 'M'},
3332 {"msr", required_argument, 0, 'm'},
Len Brownb7d8c142016-02-13 23:36:17 -05003333 {"out", required_argument, 0, 'o'},
Len Brownd8af6f52015-02-10 01:56:38 -05003334 {"Package", no_argument, 0, 'p'},
3335 {"processor", no_argument, 0, 'p'},
3336 {"Summary", no_argument, 0, 'S'},
3337 {"TCC", required_argument, 0, 'T'},
3338 {"version", no_argument, 0, 'v' },
3339 {0, 0, 0, 0 }
3340 };
Len Brown103a8fe2010-10-22 23:53:03 -04003341
3342 progname = argv[0];
3343
Len Brownb7d8c142016-02-13 23:36:17 -05003344 while ((opt = getopt_long_only(argc, argv, "+C:c:Ddhi:JM:m:o:PpST:v",
Len Brownd8af6f52015-02-10 01:56:38 -05003345 long_options, &option_index)) != -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04003346 switch (opt) {
Len Brownd8af6f52015-02-10 01:56:38 -05003347 case 'C':
3348 sscanf(optarg, "%x", &extra_delta_offset64);
Len Brown103a8fe2010-10-22 23:53:03 -04003349 break;
Len Brownf9240812012-10-06 15:26:31 -04003350 case 'c':
Len Brown8e180f32012-09-22 01:25:08 -04003351 sscanf(optarg, "%x", &extra_delta_offset32);
3352 break;
Len Brownd8af6f52015-02-10 01:56:38 -05003353 case 'D':
3354 dump_only++;
Len Brown8e180f32012-09-22 01:25:08 -04003355 break;
Len Brownd8af6f52015-02-10 01:56:38 -05003356 case 'd':
3357 debug++;
Len Brown2f32edf2012-09-21 23:45:46 -04003358 break;
Len Brownd8af6f52015-02-10 01:56:38 -05003359 case 'h':
3360 default:
3361 help();
3362 exit(1);
3363 case 'i':
Len Brown2a0609c2016-02-12 22:44:48 -05003364 {
3365 double interval = strtod(optarg, NULL);
3366
3367 if (interval < 0.001) {
Len Brownb7d8c142016-02-13 23:36:17 -05003368 fprintf(outf, "interval %f seconds is too small\n",
Len Brown2a0609c2016-02-12 22:44:48 -05003369 interval);
3370 exit(2);
3371 }
3372
3373 interval_ts.tv_sec = interval;
3374 interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
3375 }
Len Brown889facb2012-11-08 00:48:57 -05003376 break;
Dirk Brandewie5c56be92013-12-16 10:23:41 -08003377 case 'J':
3378 rapl_joules++;
3379 break;
Len Brownd8af6f52015-02-10 01:56:38 -05003380 case 'M':
3381 sscanf(optarg, "%x", &extra_msr_offset64);
3382 break;
3383 case 'm':
3384 sscanf(optarg, "%x", &extra_msr_offset32);
3385 break;
Len Brownb7d8c142016-02-13 23:36:17 -05003386 case 'o':
3387 outf = fopen_or_die(optarg, "w");
3388 break;
Len Brownd8af6f52015-02-10 01:56:38 -05003389 case 'P':
3390 show_pkg_only++;
3391 break;
3392 case 'p':
3393 show_core_only++;
3394 break;
3395 case 'S':
3396 summary_only++;
3397 break;
3398 case 'T':
3399 tcc_activation_temp_override = atoi(optarg);
3400 break;
3401 case 'v':
3402 print_version();
3403 exit(0);
3404 break;
Len Brown103a8fe2010-10-22 23:53:03 -04003405 }
3406 }
3407}
3408
3409int main(int argc, char **argv)
3410{
Len Brownb7d8c142016-02-13 23:36:17 -05003411 outf = stderr;
3412
Len Brown103a8fe2010-10-22 23:53:03 -04003413 cmdline(argc, argv);
3414
Len Brownd8af6f52015-02-10 01:56:38 -05003415 if (debug)
3416 print_version();
Len Brown103a8fe2010-10-22 23:53:03 -04003417
3418 turbostat_init();
3419
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02003420 /* dump counters and exit */
3421 if (dump_only)
3422 return get_and_dump_counters();
3423
Len Brown103a8fe2010-10-22 23:53:03 -04003424 /*
3425 * if any params left, it must be a command to fork
3426 */
3427 if (argc - optind)
3428 return fork_it(argv + optind);
3429 else
3430 turbostat_loop();
3431
3432 return 0;
3433}