blob: af3b955aad1d897a0eeb849dceb8de310b622f47 [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>
Josh Triplett2b928652013-08-20 17:20:14 -070041#include <cpuid.h>
Len Brown98481e72014-08-15 00:36:50 -040042#include <linux/capability.h>
43#include <errno.h>
Len Brown103a8fe2010-10-22 23:53:03 -040044
Len Brown103a8fe2010-10-22 23:53:03 -040045char *proc_stat = "/proc/stat";
Len Brownd8af6f52015-02-10 01:56:38 -050046unsigned int interval_sec = 5;
47unsigned int debug;
48unsigned int rapl_joules;
49unsigned int summary_only;
50unsigned int dump_only;
Len Brown103a8fe2010-10-22 23:53:03 -040051unsigned int skip_c0;
52unsigned int skip_c1;
53unsigned int do_nhm_cstates;
54unsigned int do_snb_cstates;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -070055unsigned int do_knl_cstates;
Len Brownee7e38e2015-02-09 23:39:45 -050056unsigned int do_pc2;
57unsigned int do_pc3;
58unsigned int do_pc6;
59unsigned int do_pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -080060unsigned int do_c8_c9_c10;
Len Brown0b2bb692015-03-26 00:50:30 -040061unsigned int do_skl_residency;
Len Brown144b44b2013-11-09 00:30:16 -050062unsigned int do_slm_cstates;
63unsigned int use_c1_residency_msr;
Len Brown103a8fe2010-10-22 23:53:03 -040064unsigned int has_aperf;
Len Brown889facb2012-11-08 00:48:57 -050065unsigned int has_epb;
Len Brownfc04cc62014-02-06 00:55:19 -050066unsigned int units = 1000000; /* MHz etc */
Len Brown103a8fe2010-10-22 23:53:03 -040067unsigned int genuine_intel;
68unsigned int has_invariant_tsc;
Len Brownd7899442015-01-23 00:12:33 -050069unsigned int do_nhm_platform_info;
Len Brown2f32edf2012-09-21 23:45:46 -040070unsigned int extra_msr_offset32;
71unsigned int extra_msr_offset64;
Len Brown8e180f32012-09-22 01:25:08 -040072unsigned int extra_delta_offset32;
73unsigned int extra_delta_offset64;
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +020074unsigned int aperf_mperf_multiplier = 1;
Len Brown1ed51012013-02-10 17:19:24 -050075int do_smi;
Len Brown103a8fe2010-10-22 23:53:03 -040076double bclk;
Len Browna2b7b742015-09-26 00:12:38 -040077double base_hz;
Len Brown21ed5572015-10-19 22:37:40 -040078unsigned int has_base_hz;
Len Browna2b7b742015-09-26 00:12:38 -040079double tsc_tweak = 1.0;
Len Brown103a8fe2010-10-22 23:53:03 -040080unsigned int show_pkg;
81unsigned int show_core;
82unsigned int show_cpu;
Len Brownc98d5d92012-06-04 00:56:40 -040083unsigned int show_pkg_only;
84unsigned int show_core_only;
85char *output_buffer, *outp;
Len Brown889facb2012-11-08 00:48:57 -050086unsigned int do_rapl;
87unsigned int do_dts;
88unsigned int do_ptm;
89unsigned int tcc_activation_temp;
90unsigned int tcc_activation_temp_override;
Andrey Semin40ee8e32014-12-05 00:07:00 -050091double rapl_power_units, rapl_time_units;
92double rapl_dram_energy_units, rapl_energy_units;
Len Brown889facb2012-11-08 00:48:57 -050093double rapl_joule_counter_range;
Len Brown3a9a9412014-08-15 02:39:52 -040094unsigned int do_core_perf_limit_reasons;
95unsigned int do_gfx_perf_limit_reasons;
96unsigned int do_ring_perf_limit_reasons;
Len Brown8a5bdf42015-04-01 21:02:57 -040097unsigned int crystal_hz;
98unsigned long long tsc_hz;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -040099int base_cpu;
Len Brown21ed5572015-10-19 22:37:40 -0400100double discover_bclk(unsigned int family, unsigned int model);
Len Brown7f5c2582015-12-01 01:36:39 -0500101unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */
102 /* IA32_HWP_REQUEST, IA32_HWP_STATUS */
103unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */
104unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */
105unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */
106unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */
Len Brown889facb2012-11-08 00:48:57 -0500107
Len Browne6f9bb32013-12-03 02:19:19 -0500108#define RAPL_PKG (1 << 0)
109 /* 0x610 MSR_PKG_POWER_LIMIT */
110 /* 0x611 MSR_PKG_ENERGY_STATUS */
111#define RAPL_PKG_PERF_STATUS (1 << 1)
112 /* 0x613 MSR_PKG_PERF_STATUS */
113#define RAPL_PKG_POWER_INFO (1 << 2)
114 /* 0x614 MSR_PKG_POWER_INFO */
115
116#define RAPL_DRAM (1 << 3)
117 /* 0x618 MSR_DRAM_POWER_LIMIT */
118 /* 0x619 MSR_DRAM_ENERGY_STATUS */
Len Browne6f9bb32013-12-03 02:19:19 -0500119#define RAPL_DRAM_PERF_STATUS (1 << 4)
120 /* 0x61b MSR_DRAM_PERF_STATUS */
Len Brown0b2bb692015-03-26 00:50:30 -0400121#define RAPL_DRAM_POWER_INFO (1 << 5)
122 /* 0x61c MSR_DRAM_POWER_INFO */
Len Browne6f9bb32013-12-03 02:19:19 -0500123
Len Brown0b2bb692015-03-26 00:50:30 -0400124#define RAPL_CORES (1 << 6)
Len Browne6f9bb32013-12-03 02:19:19 -0500125 /* 0x638 MSR_PP0_POWER_LIMIT */
126 /* 0x639 MSR_PP0_ENERGY_STATUS */
Len Brown0b2bb692015-03-26 00:50:30 -0400127#define RAPL_CORE_POLICY (1 << 7)
Len Browne6f9bb32013-12-03 02:19:19 -0500128 /* 0x63a MSR_PP0_POLICY */
129
Len Brown0b2bb692015-03-26 00:50:30 -0400130#define RAPL_GFX (1 << 8)
Len Browne6f9bb32013-12-03 02:19:19 -0500131 /* 0x640 MSR_PP1_POWER_LIMIT */
132 /* 0x641 MSR_PP1_ENERGY_STATUS */
133 /* 0x642 MSR_PP1_POLICY */
Len Brown889facb2012-11-08 00:48:57 -0500134#define TJMAX_DEFAULT 100
135
136#define MAX(a, b) ((a) > (b) ? (a) : (b))
Len Brown103a8fe2010-10-22 23:53:03 -0400137
138int aperf_mperf_unstable;
139int backwards_count;
140char *progname;
Len Brown103a8fe2010-10-22 23:53:03 -0400141
Len Brownc98d5d92012-06-04 00:56:40 -0400142cpu_set_t *cpu_present_set, *cpu_affinity_set;
143size_t cpu_present_setsize, cpu_affinity_setsize;
Len Brown103a8fe2010-10-22 23:53:03 -0400144
Len Brownc98d5d92012-06-04 00:56:40 -0400145struct thread_data {
146 unsigned long long tsc;
147 unsigned long long aperf;
148 unsigned long long mperf;
Len Brown144b44b2013-11-09 00:30:16 -0500149 unsigned long long c1;
Len Brown2f32edf2012-09-21 23:45:46 -0400150 unsigned long long extra_msr64;
Len Brown8e180f32012-09-22 01:25:08 -0400151 unsigned long long extra_delta64;
152 unsigned long long extra_msr32;
153 unsigned long long extra_delta32;
Len Brown1ed51012013-02-10 17:19:24 -0500154 unsigned int smi_count;
Len Brownc98d5d92012-06-04 00:56:40 -0400155 unsigned int cpu_id;
156 unsigned int flags;
157#define CPU_IS_FIRST_THREAD_IN_CORE 0x2
158#define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
159} *thread_even, *thread_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400160
Len Brownc98d5d92012-06-04 00:56:40 -0400161struct core_data {
162 unsigned long long c3;
163 unsigned long long c6;
164 unsigned long long c7;
Len Brown889facb2012-11-08 00:48:57 -0500165 unsigned int core_temp_c;
Len Brownc98d5d92012-06-04 00:56:40 -0400166 unsigned int core_id;
167} *core_even, *core_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400168
Len Brownc98d5d92012-06-04 00:56:40 -0400169struct pkg_data {
170 unsigned long long pc2;
171 unsigned long long pc3;
172 unsigned long long pc6;
173 unsigned long long pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800174 unsigned long long pc8;
175 unsigned long long pc9;
176 unsigned long long pc10;
Len Brown0b2bb692015-03-26 00:50:30 -0400177 unsigned long long pkg_wtd_core_c0;
178 unsigned long long pkg_any_core_c0;
179 unsigned long long pkg_any_gfxe_c0;
180 unsigned long long pkg_both_core_gfxe_c0;
Len Brownc98d5d92012-06-04 00:56:40 -0400181 unsigned int package_id;
Len Brown889facb2012-11-08 00:48:57 -0500182 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
183 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
184 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
185 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
186 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
187 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
188 unsigned int pkg_temp_c;
189
Len Brownc98d5d92012-06-04 00:56:40 -0400190} *package_even, *package_odd;
191
192#define ODD_COUNTERS thread_odd, core_odd, package_odd
193#define EVEN_COUNTERS thread_even, core_even, package_even
194
195#define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
196 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
197 topo.num_threads_per_core + \
198 (core_no) * topo.num_threads_per_core + (thread_no))
199#define GET_CORE(core_base, core_no, pkg_no) \
200 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
201#define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
202
203struct system_summary {
204 struct thread_data threads;
205 struct core_data cores;
206 struct pkg_data packages;
207} sum, average;
208
209
210struct topo_params {
211 int num_packages;
212 int num_cpus;
213 int num_cores;
214 int max_cpu_num;
215 int num_cores_per_pkg;
216 int num_threads_per_core;
217} topo;
218
219struct timeval tv_even, tv_odd, tv_delta;
220
221void setup_all_buffers(void);
222
223int cpu_is_not_present(int cpu)
Len Brownd15cf7c2012-06-03 23:24:00 -0400224{
Len Brownc98d5d92012-06-04 00:56:40 -0400225 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brownd15cf7c2012-06-03 23:24:00 -0400226}
Len Brown88c32812012-03-29 21:44:40 -0400227/*
Len Brownc98d5d92012-06-04 00:56:40 -0400228 * run func(thread, core, package) in topology order
229 * skip non-present cpus
Len Brown88c32812012-03-29 21:44:40 -0400230 */
Len Brownd15cf7c2012-06-03 23:24:00 -0400231
Len Brownc98d5d92012-06-04 00:56:40 -0400232int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
233 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
Len Brown88c32812012-03-29 21:44:40 -0400234{
Len Brownc98d5d92012-06-04 00:56:40 -0400235 int retval, pkg_no, core_no, thread_no;
236
237 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
238 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
239 for (thread_no = 0; thread_no <
240 topo.num_threads_per_core; ++thread_no) {
241 struct thread_data *t;
242 struct core_data *c;
243 struct pkg_data *p;
244
245 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
246
247 if (cpu_is_not_present(t->cpu_id))
248 continue;
249
250 c = GET_CORE(core_base, core_no, pkg_no);
251 p = GET_PKG(pkg_base, pkg_no);
252
253 retval = func(t, c, p);
254 if (retval)
255 return retval;
256 }
257 }
258 }
259 return 0;
Len Brown88c32812012-03-29 21:44:40 -0400260}
261
262int cpu_migrate(int cpu)
263{
Len Brownc98d5d92012-06-04 00:56:40 -0400264 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
265 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
266 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
Len Brown88c32812012-03-29 21:44:40 -0400267 return -1;
268 else
269 return 0;
270}
271
Len Brown15aaa342012-03-29 22:19:58 -0400272int get_msr(int cpu, off_t offset, unsigned long long *msr)
Len Brown103a8fe2010-10-22 23:53:03 -0400273{
274 ssize_t retval;
Len Brown103a8fe2010-10-22 23:53:03 -0400275 char pathname[32];
276 int fd;
277
278 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
279 fd = open(pathname, O_RDONLY);
Len Brown15aaa342012-03-29 22:19:58 -0400280 if (fd < 0)
Len Brown98481e72014-08-15 00:36:50 -0400281 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 -0400282
Len Brown15aaa342012-03-29 22:19:58 -0400283 retval = pread(fd, msr, sizeof *msr, offset);
Len Brown103a8fe2010-10-22 23:53:03 -0400284 close(fd);
Len Brown15aaa342012-03-29 22:19:58 -0400285
Len Brown98481e72014-08-15 00:36:50 -0400286 if (retval != sizeof *msr)
287 err(-1, "%s offset 0x%llx read failed", pathname, (unsigned long long)offset);
Len Brown15aaa342012-03-29 22:19:58 -0400288
289 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400290}
291
Len Brownfc04cc62014-02-06 00:55:19 -0500292/*
293 * Example Format w/ field column widths:
294 *
Len Browne7c95ff2014-08-14 21:22:13 -0400295 * 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
296 * 123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678
Len Brownfc04cc62014-02-06 00:55:19 -0500297 */
298
Len Browna829eb42011-02-10 23:36:34 -0500299void print_header(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400300{
301 if (show_pkg)
Len Browne7c95ff2014-08-14 21:22:13 -0400302 outp += sprintf(outp, " Package");
Len Brown103a8fe2010-10-22 23:53:03 -0400303 if (show_core)
Len Browne7c95ff2014-08-14 21:22:13 -0400304 outp += sprintf(outp, " Core");
Len Brown103a8fe2010-10-22 23:53:03 -0400305 if (show_cpu)
Len Browne7c95ff2014-08-14 21:22:13 -0400306 outp += sprintf(outp, " CPU");
Len Brown103a8fe2010-10-22 23:53:03 -0400307 if (has_aperf)
Len Browne7c95ff2014-08-14 21:22:13 -0400308 outp += sprintf(outp, " Avg_MHz");
Len Brownd7899442015-01-23 00:12:33 -0500309 if (has_aperf)
Len Browne7c95ff2014-08-14 21:22:13 -0400310 outp += sprintf(outp, " %%Busy");
Len Brownfc04cc62014-02-06 00:55:19 -0500311 if (has_aperf)
Len Browne7c95ff2014-08-14 21:22:13 -0400312 outp += sprintf(outp, " Bzy_MHz");
313 outp += sprintf(outp, " TSC_MHz");
Len Brown1cc21f72015-02-23 00:34:57 -0500314
Len Brown8e180f32012-09-22 01:25:08 -0400315 if (extra_delta_offset32)
Len Browne7c95ff2014-08-14 21:22:13 -0400316 outp += sprintf(outp, " count 0x%03X", extra_delta_offset32);
Len Brown8e180f32012-09-22 01:25:08 -0400317 if (extra_delta_offset64)
Len Browne7c95ff2014-08-14 21:22:13 -0400318 outp += sprintf(outp, " COUNT 0x%03X", extra_delta_offset64);
Len Brown2f32edf2012-09-21 23:45:46 -0400319 if (extra_msr_offset32)
Len Browne7c95ff2014-08-14 21:22:13 -0400320 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset32);
Len Brown2f32edf2012-09-21 23:45:46 -0400321 if (extra_msr_offset64)
Len Browne7c95ff2014-08-14 21:22:13 -0400322 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset64);
Len Brown1cc21f72015-02-23 00:34:57 -0500323
324 if (!debug)
325 goto done;
326
327 if (do_smi)
328 outp += sprintf(outp, " SMI");
329
Len Brown103a8fe2010-10-22 23:53:03 -0400330 if (do_nhm_cstates)
Len Browne7c95ff2014-08-14 21:22:13 -0400331 outp += sprintf(outp, " CPU%%c1");
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -0700332 if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates)
Len Browne7c95ff2014-08-14 21:22:13 -0400333 outp += sprintf(outp, " CPU%%c3");
Len Brown103a8fe2010-10-22 23:53:03 -0400334 if (do_nhm_cstates)
Len Browne7c95ff2014-08-14 21:22:13 -0400335 outp += sprintf(outp, " CPU%%c6");
Len Brown103a8fe2010-10-22 23:53:03 -0400336 if (do_snb_cstates)
Len Browne7c95ff2014-08-14 21:22:13 -0400337 outp += sprintf(outp, " CPU%%c7");
Len Brown889facb2012-11-08 00:48:57 -0500338
339 if (do_dts)
Len Browne7c95ff2014-08-14 21:22:13 -0400340 outp += sprintf(outp, " CoreTmp");
Len Brown889facb2012-11-08 00:48:57 -0500341 if (do_ptm)
Len Browne7c95ff2014-08-14 21:22:13 -0400342 outp += sprintf(outp, " PkgTmp");
Len Brown889facb2012-11-08 00:48:57 -0500343
Len Brown0b2bb692015-03-26 00:50:30 -0400344 if (do_skl_residency) {
345 outp += sprintf(outp, " Totl%%C0");
346 outp += sprintf(outp, " Any%%C0");
347 outp += sprintf(outp, " GFX%%C0");
348 outp += sprintf(outp, " CPUGFX%%");
349 }
350
Len Brownee7e38e2015-02-09 23:39:45 -0500351 if (do_pc2)
Len Browne7c95ff2014-08-14 21:22:13 -0400352 outp += sprintf(outp, " Pkg%%pc2");
Len Brownee7e38e2015-02-09 23:39:45 -0500353 if (do_pc3)
Len Browne7c95ff2014-08-14 21:22:13 -0400354 outp += sprintf(outp, " Pkg%%pc3");
Len Brownee7e38e2015-02-09 23:39:45 -0500355 if (do_pc6)
Len Browne7c95ff2014-08-14 21:22:13 -0400356 outp += sprintf(outp, " Pkg%%pc6");
Len Brownee7e38e2015-02-09 23:39:45 -0500357 if (do_pc7)
Len Browne7c95ff2014-08-14 21:22:13 -0400358 outp += sprintf(outp, " Pkg%%pc7");
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800359 if (do_c8_c9_c10) {
Len Browne7c95ff2014-08-14 21:22:13 -0400360 outp += sprintf(outp, " Pkg%%pc8");
361 outp += sprintf(outp, " Pkg%%pc9");
362 outp += sprintf(outp, " Pk%%pc10");
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800363 }
Len Brown103a8fe2010-10-22 23:53:03 -0400364
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800365 if (do_rapl && !rapl_joules) {
366 if (do_rapl & RAPL_PKG)
Len Browne7c95ff2014-08-14 21:22:13 -0400367 outp += sprintf(outp, " PkgWatt");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800368 if (do_rapl & RAPL_CORES)
Len Browne7c95ff2014-08-14 21:22:13 -0400369 outp += sprintf(outp, " CorWatt");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800370 if (do_rapl & RAPL_GFX)
Len Browne7c95ff2014-08-14 21:22:13 -0400371 outp += sprintf(outp, " GFXWatt");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800372 if (do_rapl & RAPL_DRAM)
Len Browne7c95ff2014-08-14 21:22:13 -0400373 outp += sprintf(outp, " RAMWatt");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800374 if (do_rapl & RAPL_PKG_PERF_STATUS)
Len Browne7c95ff2014-08-14 21:22:13 -0400375 outp += sprintf(outp, " PKG_%%");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800376 if (do_rapl & RAPL_DRAM_PERF_STATUS)
Len Browne7c95ff2014-08-14 21:22:13 -0400377 outp += sprintf(outp, " RAM_%%");
Len Brownd7899442015-01-23 00:12:33 -0500378 } else if (do_rapl && rapl_joules) {
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800379 if (do_rapl & RAPL_PKG)
Len Browne7c95ff2014-08-14 21:22:13 -0400380 outp += sprintf(outp, " Pkg_J");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800381 if (do_rapl & RAPL_CORES)
Len Browne7c95ff2014-08-14 21:22:13 -0400382 outp += sprintf(outp, " Cor_J");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800383 if (do_rapl & RAPL_GFX)
Len Browne7c95ff2014-08-14 21:22:13 -0400384 outp += sprintf(outp, " GFX_J");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800385 if (do_rapl & RAPL_DRAM)
Len Brownbd6906e2015-07-24 10:35:23 -0400386 outp += sprintf(outp, " RAM_J");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800387 if (do_rapl & RAPL_PKG_PERF_STATUS)
Len Browne7c95ff2014-08-14 21:22:13 -0400388 outp += sprintf(outp, " PKG_%%");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800389 if (do_rapl & RAPL_DRAM_PERF_STATUS)
Len Browne7c95ff2014-08-14 21:22:13 -0400390 outp += sprintf(outp, " RAM_%%");
391 outp += sprintf(outp, " time");
Len Brown889facb2012-11-08 00:48:57 -0500392
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800393 }
Len Brown1cc21f72015-02-23 00:34:57 -0500394 done:
Len Brownc98d5d92012-06-04 00:56:40 -0400395 outp += sprintf(outp, "\n");
Len Brown103a8fe2010-10-22 23:53:03 -0400396}
397
Len Brownc98d5d92012-06-04 00:56:40 -0400398int dump_counters(struct thread_data *t, struct core_data *c,
399 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400400{
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200401 outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400402
Len Brownc98d5d92012-06-04 00:56:40 -0400403 if (t) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200404 outp += sprintf(outp, "CPU: %d flags 0x%x\n",
405 t->cpu_id, t->flags);
406 outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
407 outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
408 outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
409 outp += sprintf(outp, "c1: %016llX\n", t->c1);
410 outp += sprintf(outp, "msr0x%x: %08llX\n",
Len Brown8e180f32012-09-22 01:25:08 -0400411 extra_delta_offset32, t->extra_delta32);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200412 outp += sprintf(outp, "msr0x%x: %016llX\n",
Len Brown8e180f32012-09-22 01:25:08 -0400413 extra_delta_offset64, t->extra_delta64);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200414 outp += sprintf(outp, "msr0x%x: %08llX\n",
Len Brown2f32edf2012-09-21 23:45:46 -0400415 extra_msr_offset32, t->extra_msr32);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200416 outp += sprintf(outp, "msr0x%x: %016llX\n",
Len Brown2f32edf2012-09-21 23:45:46 -0400417 extra_msr_offset64, t->extra_msr64);
Len Brown1ed51012013-02-10 17:19:24 -0500418 if (do_smi)
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200419 outp += sprintf(outp, "SMI: %08X\n", t->smi_count);
Len Brownc98d5d92012-06-04 00:56:40 -0400420 }
Len Brown103a8fe2010-10-22 23:53:03 -0400421
Len Brownc98d5d92012-06-04 00:56:40 -0400422 if (c) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200423 outp += sprintf(outp, "core: %d\n", c->core_id);
424 outp += sprintf(outp, "c3: %016llX\n", c->c3);
425 outp += sprintf(outp, "c6: %016llX\n", c->c6);
426 outp += sprintf(outp, "c7: %016llX\n", c->c7);
427 outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
Len Brownc98d5d92012-06-04 00:56:40 -0400428 }
429
430 if (p) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200431 outp += sprintf(outp, "package: %d\n", p->package_id);
Len Brown0b2bb692015-03-26 00:50:30 -0400432
433 outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
434 outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
435 outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
436 outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
437
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200438 outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
Len Brownee7e38e2015-02-09 23:39:45 -0500439 if (do_pc3)
440 outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
441 if (do_pc6)
442 outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
443 if (do_pc7)
444 outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200445 outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
446 outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
447 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
448 outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
449 outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
450 outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
451 outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
452 outp += sprintf(outp, "Throttle PKG: %0X\n",
453 p->rapl_pkg_perf_status);
454 outp += sprintf(outp, "Throttle RAM: %0X\n",
455 p->rapl_dram_perf_status);
456 outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
Len Brownc98d5d92012-06-04 00:56:40 -0400457 }
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200458
459 outp += sprintf(outp, "\n");
460
Len Brownc98d5d92012-06-04 00:56:40 -0400461 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400462}
463
Len Browne23da032012-02-06 18:37:16 -0500464/*
465 * column formatting convention & formats
Len Browne23da032012-02-06 18:37:16 -0500466 */
Len Brownc98d5d92012-06-04 00:56:40 -0400467int format_counters(struct thread_data *t, struct core_data *c,
468 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400469{
470 double interval_float;
Len Brownfc04cc62014-02-06 00:55:19 -0500471 char *fmt8;
Len Brown103a8fe2010-10-22 23:53:03 -0400472
Len Brownc98d5d92012-06-04 00:56:40 -0400473 /* if showing only 1st thread in core and this isn't one, bail out */
474 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
475 return 0;
476
477 /* if showing only 1st thread in pkg and this isn't one, bail out */
478 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
479 return 0;
480
Len Brown103a8fe2010-10-22 23:53:03 -0400481 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
482
Len Brownc98d5d92012-06-04 00:56:40 -0400483 /* topo columns, print blanks on 1st (average) line */
484 if (t == &average.threads) {
Len Brown103a8fe2010-10-22 23:53:03 -0400485 if (show_pkg)
Len Brownfc04cc62014-02-06 00:55:19 -0500486 outp += sprintf(outp, " -");
Len Brown103a8fe2010-10-22 23:53:03 -0400487 if (show_core)
Len Brownfc04cc62014-02-06 00:55:19 -0500488 outp += sprintf(outp, " -");
Len Brown103a8fe2010-10-22 23:53:03 -0400489 if (show_cpu)
Len Brownfc04cc62014-02-06 00:55:19 -0500490 outp += sprintf(outp, " -");
Len Brown103a8fe2010-10-22 23:53:03 -0400491 } else {
Len Brownc98d5d92012-06-04 00:56:40 -0400492 if (show_pkg) {
493 if (p)
Len Brownfc04cc62014-02-06 00:55:19 -0500494 outp += sprintf(outp, "%8d", p->package_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400495 else
Len Brownfc04cc62014-02-06 00:55:19 -0500496 outp += sprintf(outp, " -");
Len Brownc98d5d92012-06-04 00:56:40 -0400497 }
Len Brownc98d5d92012-06-04 00:56:40 -0400498 if (show_core) {
499 if (c)
Len Brownfc04cc62014-02-06 00:55:19 -0500500 outp += sprintf(outp, "%8d", c->core_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400501 else
Len Brownfc04cc62014-02-06 00:55:19 -0500502 outp += sprintf(outp, " -");
Len Brownc98d5d92012-06-04 00:56:40 -0400503 }
Len Brown103a8fe2010-10-22 23:53:03 -0400504 if (show_cpu)
Len Brownfc04cc62014-02-06 00:55:19 -0500505 outp += sprintf(outp, "%8d", t->cpu_id);
Len Brown103a8fe2010-10-22 23:53:03 -0400506 }
Len Brownfc04cc62014-02-06 00:55:19 -0500507
Len Brownd7899442015-01-23 00:12:33 -0500508 /* Avg_MHz */
Len Brownfc04cc62014-02-06 00:55:19 -0500509 if (has_aperf)
510 outp += sprintf(outp, "%8.0f",
511 1.0 / units * t->aperf / interval_float);
512
Len Brownd7899442015-01-23 00:12:33 -0500513 /* %Busy */
514 if (has_aperf) {
Len Brown103a8fe2010-10-22 23:53:03 -0400515 if (!skip_c0)
Len Browna2b7b742015-09-26 00:12:38 -0400516 outp += sprintf(outp, "%8.2f", 100.0 * t->mperf/t->tsc/tsc_tweak);
Len Brown103a8fe2010-10-22 23:53:03 -0400517 else
Len Brownfc04cc62014-02-06 00:55:19 -0500518 outp += sprintf(outp, "********");
Len Brown103a8fe2010-10-22 23:53:03 -0400519 }
520
Len Brownd7899442015-01-23 00:12:33 -0500521 /* Bzy_MHz */
Len Brown21ed5572015-10-19 22:37:40 -0400522 if (has_aperf) {
523 if (has_base_hz)
524 outp += sprintf(outp, "%8.0f", base_hz / units * t->aperf / t->mperf);
525 else
526 outp += sprintf(outp, "%8.0f",
527 1.0 * t->tsc / units * t->aperf / t->mperf / interval_float);
528 }
Len Brown103a8fe2010-10-22 23:53:03 -0400529
Len Brownd7899442015-01-23 00:12:33 -0500530 /* TSC_MHz */
Len Brownfc04cc62014-02-06 00:55:19 -0500531 outp += sprintf(outp, "%8.0f", 1.0 * t->tsc/units/interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400532
Len Brown8e180f32012-09-22 01:25:08 -0400533 /* delta */
534 if (extra_delta_offset32)
535 outp += sprintf(outp, " %11llu", t->extra_delta32);
536
537 /* DELTA */
538 if (extra_delta_offset64)
539 outp += sprintf(outp, " %11llu", t->extra_delta64);
Len Brown2f32edf2012-09-21 23:45:46 -0400540 /* msr */
541 if (extra_msr_offset32)
Len Brown8e180f32012-09-22 01:25:08 -0400542 outp += sprintf(outp, " 0x%08llx", t->extra_msr32);
Len Brown2f32edf2012-09-21 23:45:46 -0400543
Len Brown130ff302012-09-21 22:56:06 -0400544 /* MSR */
Len Brown2f32edf2012-09-21 23:45:46 -0400545 if (extra_msr_offset64)
546 outp += sprintf(outp, " 0x%016llx", t->extra_msr64);
Len Brown130ff302012-09-21 22:56:06 -0400547
Len Brown1cc21f72015-02-23 00:34:57 -0500548 if (!debug)
549 goto done;
550
551 /* SMI */
552 if (do_smi)
553 outp += sprintf(outp, "%8d", t->smi_count);
554
Len Brown103a8fe2010-10-22 23:53:03 -0400555 if (do_nhm_cstates) {
556 if (!skip_c1)
Len Brownfc04cc62014-02-06 00:55:19 -0500557 outp += sprintf(outp, "%8.2f", 100.0 * t->c1/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400558 else
Len Brownfc04cc62014-02-06 00:55:19 -0500559 outp += sprintf(outp, "********");
Len Brown103a8fe2010-10-22 23:53:03 -0400560 }
Len Brownc98d5d92012-06-04 00:56:40 -0400561
562 /* print per-core data only for 1st thread in core */
563 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
564 goto done;
565
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -0700566 if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates)
Len Brownfc04cc62014-02-06 00:55:19 -0500567 outp += sprintf(outp, "%8.2f", 100.0 * c->c3/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400568 if (do_nhm_cstates)
Len Brownfc04cc62014-02-06 00:55:19 -0500569 outp += sprintf(outp, "%8.2f", 100.0 * c->c6/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400570 if (do_snb_cstates)
Len Brownfc04cc62014-02-06 00:55:19 -0500571 outp += sprintf(outp, "%8.2f", 100.0 * c->c7/t->tsc);
Len Brownc98d5d92012-06-04 00:56:40 -0400572
Len Brown889facb2012-11-08 00:48:57 -0500573 if (do_dts)
Len Brownfc04cc62014-02-06 00:55:19 -0500574 outp += sprintf(outp, "%8d", c->core_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500575
Len Brownc98d5d92012-06-04 00:56:40 -0400576 /* print per-package data only for 1st core in package */
577 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
578 goto done;
579
Len Brown0b2bb692015-03-26 00:50:30 -0400580 /* PkgTmp */
Len Brown889facb2012-11-08 00:48:57 -0500581 if (do_ptm)
Len Brownfc04cc62014-02-06 00:55:19 -0500582 outp += sprintf(outp, "%8d", p->pkg_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500583
Len Brown0b2bb692015-03-26 00:50:30 -0400584 /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
585 if (do_skl_residency) {
586 outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_wtd_core_c0/t->tsc);
587 outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_any_core_c0/t->tsc);
588 outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_any_gfxe_c0/t->tsc);
589 outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_both_core_gfxe_c0/t->tsc);
590 }
591
Len Brownee7e38e2015-02-09 23:39:45 -0500592 if (do_pc2)
Len Brownfc04cc62014-02-06 00:55:19 -0500593 outp += sprintf(outp, "%8.2f", 100.0 * p->pc2/t->tsc);
Len Brownee7e38e2015-02-09 23:39:45 -0500594 if (do_pc3)
Len Brownfc04cc62014-02-06 00:55:19 -0500595 outp += sprintf(outp, "%8.2f", 100.0 * p->pc3/t->tsc);
Len Brownee7e38e2015-02-09 23:39:45 -0500596 if (do_pc6)
Len Brownfc04cc62014-02-06 00:55:19 -0500597 outp += sprintf(outp, "%8.2f", 100.0 * p->pc6/t->tsc);
Len Brownee7e38e2015-02-09 23:39:45 -0500598 if (do_pc7)
Len Brownfc04cc62014-02-06 00:55:19 -0500599 outp += sprintf(outp, "%8.2f", 100.0 * p->pc7/t->tsc);
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800600 if (do_c8_c9_c10) {
Len Brownfc04cc62014-02-06 00:55:19 -0500601 outp += sprintf(outp, "%8.2f", 100.0 * p->pc8/t->tsc);
602 outp += sprintf(outp, "%8.2f", 100.0 * p->pc9/t->tsc);
603 outp += sprintf(outp, "%8.2f", 100.0 * p->pc10/t->tsc);
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800604 }
Len Brown889facb2012-11-08 00:48:57 -0500605
606 /*
607 * If measurement interval exceeds minimum RAPL Joule Counter range,
608 * indicate that results are suspect by printing "**" in fraction place.
609 */
Len Brownfc04cc62014-02-06 00:55:19 -0500610 if (interval_float < rapl_joule_counter_range)
611 fmt8 = "%8.2f";
612 else
613 fmt8 = " %6.0f**";
Len Brown889facb2012-11-08 00:48:57 -0500614
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800615 if (do_rapl && !rapl_joules) {
616 if (do_rapl & RAPL_PKG)
Len Brownfc04cc62014-02-06 00:55:19 -0500617 outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800618 if (do_rapl & RAPL_CORES)
Len Brownfc04cc62014-02-06 00:55:19 -0500619 outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800620 if (do_rapl & RAPL_GFX)
Len Brownfc04cc62014-02-06 00:55:19 -0500621 outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800622 if (do_rapl & RAPL_DRAM)
Andrey Semin40ee8e32014-12-05 00:07:00 -0500623 outp += sprintf(outp, fmt8, p->energy_dram * rapl_dram_energy_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800624 if (do_rapl & RAPL_PKG_PERF_STATUS)
Len Brownfc04cc62014-02-06 00:55:19 -0500625 outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800626 if (do_rapl & RAPL_DRAM_PERF_STATUS)
Len Brownfc04cc62014-02-06 00:55:19 -0500627 outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
Len Brownd7899442015-01-23 00:12:33 -0500628 } else if (do_rapl && rapl_joules) {
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800629 if (do_rapl & RAPL_PKG)
Len Brownfc04cc62014-02-06 00:55:19 -0500630 outp += sprintf(outp, fmt8,
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800631 p->energy_pkg * rapl_energy_units);
632 if (do_rapl & RAPL_CORES)
Len Brownfc04cc62014-02-06 00:55:19 -0500633 outp += sprintf(outp, fmt8,
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800634 p->energy_cores * rapl_energy_units);
635 if (do_rapl & RAPL_GFX)
Len Brownfc04cc62014-02-06 00:55:19 -0500636 outp += sprintf(outp, fmt8,
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800637 p->energy_gfx * rapl_energy_units);
638 if (do_rapl & RAPL_DRAM)
Len Brownfc04cc62014-02-06 00:55:19 -0500639 outp += sprintf(outp, fmt8,
Andrey Semin40ee8e32014-12-05 00:07:00 -0500640 p->energy_dram * rapl_dram_energy_units);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800641 if (do_rapl & RAPL_PKG_PERF_STATUS)
Len Brownfc04cc62014-02-06 00:55:19 -0500642 outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800643 if (do_rapl & RAPL_DRAM_PERF_STATUS)
Len Brownfc04cc62014-02-06 00:55:19 -0500644 outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
Len Brown889facb2012-11-08 00:48:57 -0500645
Len Brownd7899442015-01-23 00:12:33 -0500646 outp += sprintf(outp, fmt8, interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800647 }
Len Brownc98d5d92012-06-04 00:56:40 -0400648done:
Len Brownc98d5d92012-06-04 00:56:40 -0400649 outp += sprintf(outp, "\n");
650
651 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400652}
653
Len Brownc98d5d92012-06-04 00:56:40 -0400654void flush_stdout()
Len Brown103a8fe2010-10-22 23:53:03 -0400655{
Len Brownc98d5d92012-06-04 00:56:40 -0400656 fputs(output_buffer, stdout);
Len Brownddac0d62012-11-30 01:01:40 -0500657 fflush(stdout);
Len Brownc98d5d92012-06-04 00:56:40 -0400658 outp = output_buffer;
659}
660void flush_stderr()
661{
662 fputs(output_buffer, stderr);
663 outp = output_buffer;
664}
665void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
666{
Len Browne23da032012-02-06 18:37:16 -0500667 static int printed;
Len Brown103a8fe2010-10-22 23:53:03 -0400668
Len Browne23da032012-02-06 18:37:16 -0500669 if (!printed || !summary_only)
670 print_header();
Len Brown103a8fe2010-10-22 23:53:03 -0400671
Len Brownc98d5d92012-06-04 00:56:40 -0400672 if (topo.num_cpus > 1)
673 format_counters(&average.threads, &average.cores,
674 &average.packages);
Len Brown103a8fe2010-10-22 23:53:03 -0400675
Len Browne23da032012-02-06 18:37:16 -0500676 printed = 1;
677
678 if (summary_only)
679 return;
680
Len Brownc98d5d92012-06-04 00:56:40 -0400681 for_all_cpus(format_counters, t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400682}
683
Len Brown889facb2012-11-08 00:48:57 -0500684#define DELTA_WRAP32(new, old) \
685 if (new > old) { \
686 old = new - old; \
687 } else { \
688 old = 0x100000000 + new - old; \
689 }
690
Len Brownc98d5d92012-06-04 00:56:40 -0400691void
692delta_package(struct pkg_data *new, struct pkg_data *old)
Len Brown103a8fe2010-10-22 23:53:03 -0400693{
Len Brown0b2bb692015-03-26 00:50:30 -0400694
695 if (do_skl_residency) {
696 old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
697 old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
698 old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
699 old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0;
700 }
Len Brownc98d5d92012-06-04 00:56:40 -0400701 old->pc2 = new->pc2 - old->pc2;
Len Brownee7e38e2015-02-09 23:39:45 -0500702 if (do_pc3)
703 old->pc3 = new->pc3 - old->pc3;
704 if (do_pc6)
705 old->pc6 = new->pc6 - old->pc6;
706 if (do_pc7)
707 old->pc7 = new->pc7 - old->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800708 old->pc8 = new->pc8 - old->pc8;
709 old->pc9 = new->pc9 - old->pc9;
710 old->pc10 = new->pc10 - old->pc10;
Len Brown889facb2012-11-08 00:48:57 -0500711 old->pkg_temp_c = new->pkg_temp_c;
712
713 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
714 DELTA_WRAP32(new->energy_cores, old->energy_cores);
715 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
716 DELTA_WRAP32(new->energy_dram, old->energy_dram);
717 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
718 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
Len Brownc98d5d92012-06-04 00:56:40 -0400719}
Len Brown103a8fe2010-10-22 23:53:03 -0400720
Len Brownc98d5d92012-06-04 00:56:40 -0400721void
722delta_core(struct core_data *new, struct core_data *old)
723{
724 old->c3 = new->c3 - old->c3;
725 old->c6 = new->c6 - old->c6;
726 old->c7 = new->c7 - old->c7;
Len Brown889facb2012-11-08 00:48:57 -0500727 old->core_temp_c = new->core_temp_c;
Len Brownc98d5d92012-06-04 00:56:40 -0400728}
Len Brown103a8fe2010-10-22 23:53:03 -0400729
Len Brownc3ae3312012-06-13 21:31:46 -0400730/*
731 * old = new - old
732 */
Len Brownc98d5d92012-06-04 00:56:40 -0400733void
734delta_thread(struct thread_data *new, struct thread_data *old,
735 struct core_data *core_delta)
736{
737 old->tsc = new->tsc - old->tsc;
Len Brown103a8fe2010-10-22 23:53:03 -0400738
Len Brownc98d5d92012-06-04 00:56:40 -0400739 /* check for TSC < 1 Mcycles over interval */
Josh Triplettb2c95d92013-08-20 17:20:18 -0700740 if (old->tsc < (1000 * 1000))
741 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
742 "You can disable all c-states by booting with \"idle=poll\"\n"
743 "or just the deep ones with \"processor.max_cstate=1\"");
Len Brown103a8fe2010-10-22 23:53:03 -0400744
Len Brownc98d5d92012-06-04 00:56:40 -0400745 old->c1 = new->c1 - old->c1;
Len Brown103a8fe2010-10-22 23:53:03 -0400746
Len Browna7296172015-01-23 01:33:58 -0500747 if (has_aperf) {
748 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
749 old->aperf = new->aperf - old->aperf;
750 old->mperf = new->mperf - old->mperf;
751 } else {
Len Brown103a8fe2010-10-22 23:53:03 -0400752
Len Browna7296172015-01-23 01:33:58 -0500753 if (!aperf_mperf_unstable) {
754 fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname);
755 fprintf(stderr, "* Frequency results do not cover entire interval *\n");
756 fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n");
Len Brownc98d5d92012-06-04 00:56:40 -0400757
Len Browna7296172015-01-23 01:33:58 -0500758 aperf_mperf_unstable = 1;
759 }
760 /*
761 * mperf delta is likely a huge "positive" number
762 * can not use it for calculating c0 time
763 */
764 skip_c0 = 1;
765 skip_c1 = 1;
Len Brownc98d5d92012-06-04 00:56:40 -0400766 }
Len Brownc98d5d92012-06-04 00:56:40 -0400767 }
Len Brown103a8fe2010-10-22 23:53:03 -0400768
Len Brown103a8fe2010-10-22 23:53:03 -0400769
Len Brown144b44b2013-11-09 00:30:16 -0500770 if (use_c1_residency_msr) {
771 /*
772 * Some models have a dedicated C1 residency MSR,
773 * which should be more accurate than the derivation below.
774 */
775 } else {
776 /*
777 * As counter collection is not atomic,
778 * it is possible for mperf's non-halted cycles + idle states
779 * to exceed TSC's all cycles: show c1 = 0% in that case.
780 */
781 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
782 old->c1 = 0;
783 else {
784 /* normal case, derive c1 */
785 old->c1 = old->tsc - old->mperf - core_delta->c3
Len Brownc98d5d92012-06-04 00:56:40 -0400786 - core_delta->c6 - core_delta->c7;
Len Brown144b44b2013-11-09 00:30:16 -0500787 }
Len Brownc98d5d92012-06-04 00:56:40 -0400788 }
Len Brownc3ae3312012-06-13 21:31:46 -0400789
Len Brownc98d5d92012-06-04 00:56:40 -0400790 if (old->mperf == 0) {
Len Brownd8af6f52015-02-10 01:56:38 -0500791 if (debug > 1) fprintf(stderr, "cpu%d MPERF 0!\n", old->cpu_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400792 old->mperf = 1; /* divide by 0 protection */
793 }
794
Len Brown8e180f32012-09-22 01:25:08 -0400795 old->extra_delta32 = new->extra_delta32 - old->extra_delta32;
796 old->extra_delta32 &= 0xFFFFFFFF;
797
798 old->extra_delta64 = new->extra_delta64 - old->extra_delta64;
799
Len Brownc98d5d92012-06-04 00:56:40 -0400800 /*
Len Brown8e180f32012-09-22 01:25:08 -0400801 * Extra MSR is just a snapshot, simply copy latest w/o subtracting
Len Brownc98d5d92012-06-04 00:56:40 -0400802 */
Len Brown2f32edf2012-09-21 23:45:46 -0400803 old->extra_msr32 = new->extra_msr32;
804 old->extra_msr64 = new->extra_msr64;
Len Brown1ed51012013-02-10 17:19:24 -0500805
806 if (do_smi)
807 old->smi_count = new->smi_count - old->smi_count;
Len Brownc98d5d92012-06-04 00:56:40 -0400808}
809
810int delta_cpu(struct thread_data *t, struct core_data *c,
811 struct pkg_data *p, struct thread_data *t2,
812 struct core_data *c2, struct pkg_data *p2)
813{
814 /* calculate core delta only for 1st thread in core */
815 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
816 delta_core(c, c2);
817
818 /* always calculate thread delta */
819 delta_thread(t, t2, c2); /* c2 is core delta */
820
821 /* calculate package delta only for 1st core in package */
822 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
823 delta_package(p, p2);
824
825 return 0;
826}
827
828void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
829{
830 t->tsc = 0;
831 t->aperf = 0;
832 t->mperf = 0;
833 t->c1 = 0;
834
Len Brown1ed51012013-02-10 17:19:24 -0500835 t->smi_count = 0;
Len Brown8e180f32012-09-22 01:25:08 -0400836 t->extra_delta32 = 0;
837 t->extra_delta64 = 0;
838
Len Brownc98d5d92012-06-04 00:56:40 -0400839 /* tells format_counters to dump all fields from this set */
840 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
841
842 c->c3 = 0;
843 c->c6 = 0;
844 c->c7 = 0;
Len Brown889facb2012-11-08 00:48:57 -0500845 c->core_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -0400846
Len Brown0b2bb692015-03-26 00:50:30 -0400847 p->pkg_wtd_core_c0 = 0;
848 p->pkg_any_core_c0 = 0;
849 p->pkg_any_gfxe_c0 = 0;
850 p->pkg_both_core_gfxe_c0 = 0;
851
Len Brownc98d5d92012-06-04 00:56:40 -0400852 p->pc2 = 0;
Len Brownee7e38e2015-02-09 23:39:45 -0500853 if (do_pc3)
854 p->pc3 = 0;
855 if (do_pc6)
856 p->pc6 = 0;
857 if (do_pc7)
858 p->pc7 = 0;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800859 p->pc8 = 0;
860 p->pc9 = 0;
861 p->pc10 = 0;
Len Brown889facb2012-11-08 00:48:57 -0500862
863 p->energy_pkg = 0;
864 p->energy_dram = 0;
865 p->energy_cores = 0;
866 p->energy_gfx = 0;
867 p->rapl_pkg_perf_status = 0;
868 p->rapl_dram_perf_status = 0;
869 p->pkg_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -0400870}
871int sum_counters(struct thread_data *t, struct core_data *c,
872 struct pkg_data *p)
873{
874 average.threads.tsc += t->tsc;
875 average.threads.aperf += t->aperf;
876 average.threads.mperf += t->mperf;
877 average.threads.c1 += t->c1;
878
Len Brown8e180f32012-09-22 01:25:08 -0400879 average.threads.extra_delta32 += t->extra_delta32;
880 average.threads.extra_delta64 += t->extra_delta64;
881
Len Brownc98d5d92012-06-04 00:56:40 -0400882 /* sum per-core values only for 1st thread in core */
883 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
884 return 0;
885
886 average.cores.c3 += c->c3;
887 average.cores.c6 += c->c6;
888 average.cores.c7 += c->c7;
889
Len Brown889facb2012-11-08 00:48:57 -0500890 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
891
Len Brownc98d5d92012-06-04 00:56:40 -0400892 /* sum per-pkg values only for 1st core in pkg */
893 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
894 return 0;
895
Len Brown0b2bb692015-03-26 00:50:30 -0400896 if (do_skl_residency) {
897 average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
898 average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
899 average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
900 average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
901 }
902
Len Brownc98d5d92012-06-04 00:56:40 -0400903 average.packages.pc2 += p->pc2;
Len Brownee7e38e2015-02-09 23:39:45 -0500904 if (do_pc3)
905 average.packages.pc3 += p->pc3;
906 if (do_pc6)
907 average.packages.pc6 += p->pc6;
908 if (do_pc7)
909 average.packages.pc7 += p->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800910 average.packages.pc8 += p->pc8;
911 average.packages.pc9 += p->pc9;
912 average.packages.pc10 += p->pc10;
Len Brownc98d5d92012-06-04 00:56:40 -0400913
Len Brown889facb2012-11-08 00:48:57 -0500914 average.packages.energy_pkg += p->energy_pkg;
915 average.packages.energy_dram += p->energy_dram;
916 average.packages.energy_cores += p->energy_cores;
917 average.packages.energy_gfx += p->energy_gfx;
918
919 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
920
921 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
922 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
Len Brownc98d5d92012-06-04 00:56:40 -0400923 return 0;
924}
925/*
926 * sum the counters for all cpus in the system
927 * compute the weighted average
928 */
929void compute_average(struct thread_data *t, struct core_data *c,
930 struct pkg_data *p)
931{
932 clear_counters(&average.threads, &average.cores, &average.packages);
933
934 for_all_cpus(sum_counters, t, c, p);
935
936 average.threads.tsc /= topo.num_cpus;
937 average.threads.aperf /= topo.num_cpus;
938 average.threads.mperf /= topo.num_cpus;
939 average.threads.c1 /= topo.num_cpus;
940
Len Brown8e180f32012-09-22 01:25:08 -0400941 average.threads.extra_delta32 /= topo.num_cpus;
942 average.threads.extra_delta32 &= 0xFFFFFFFF;
943
944 average.threads.extra_delta64 /= topo.num_cpus;
945
Len Brownc98d5d92012-06-04 00:56:40 -0400946 average.cores.c3 /= topo.num_cores;
947 average.cores.c6 /= topo.num_cores;
948 average.cores.c7 /= topo.num_cores;
949
Len Brown0b2bb692015-03-26 00:50:30 -0400950 if (do_skl_residency) {
951 average.packages.pkg_wtd_core_c0 /= topo.num_packages;
952 average.packages.pkg_any_core_c0 /= topo.num_packages;
953 average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
954 average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
955 }
956
Len Brownc98d5d92012-06-04 00:56:40 -0400957 average.packages.pc2 /= topo.num_packages;
Len Brownee7e38e2015-02-09 23:39:45 -0500958 if (do_pc3)
959 average.packages.pc3 /= topo.num_packages;
960 if (do_pc6)
961 average.packages.pc6 /= topo.num_packages;
962 if (do_pc7)
963 average.packages.pc7 /= topo.num_packages;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800964
965 average.packages.pc8 /= topo.num_packages;
966 average.packages.pc9 /= topo.num_packages;
967 average.packages.pc10 /= topo.num_packages;
Len Brownc98d5d92012-06-04 00:56:40 -0400968}
969
970static unsigned long long rdtsc(void)
971{
972 unsigned int low, high;
973
974 asm volatile("rdtsc" : "=a" (low), "=d" (high));
975
976 return low | ((unsigned long long)high) << 32;
977}
978
979
980/*
981 * get_counters(...)
982 * migrate to cpu
983 * acquire and record local counters for that cpu
984 */
985int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
986{
987 int cpu = t->cpu_id;
Len Brown889facb2012-11-08 00:48:57 -0500988 unsigned long long msr;
Len Brownc98d5d92012-06-04 00:56:40 -0400989
Len Browne52966c2012-11-08 22:38:05 -0500990 if (cpu_migrate(cpu)) {
991 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
Len Brownc98d5d92012-06-04 00:56:40 -0400992 return -1;
Len Browne52966c2012-11-08 22:38:05 -0500993 }
Len Brownc98d5d92012-06-04 00:56:40 -0400994
995 t->tsc = rdtsc(); /* we are running on local CPU of interest */
996
997 if (has_aperf) {
Len Brown9c63a652012-10-31 01:29:52 -0400998 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
Len Brownc98d5d92012-06-04 00:56:40 -0400999 return -3;
Len Brown9c63a652012-10-31 01:29:52 -04001000 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001001 return -4;
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02001002 t->aperf = t->aperf * aperf_mperf_multiplier;
1003 t->mperf = t->mperf * aperf_mperf_multiplier;
Len Brownc98d5d92012-06-04 00:56:40 -04001004 }
1005
Len Brown1ed51012013-02-10 17:19:24 -05001006 if (do_smi) {
1007 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
1008 return -5;
1009 t->smi_count = msr & 0xFFFFFFFF;
1010 }
Len Brown8e180f32012-09-22 01:25:08 -04001011 if (extra_delta_offset32) {
Len Brown889facb2012-11-08 00:48:57 -05001012 if (get_msr(cpu, extra_delta_offset32, &msr))
Len Brown2f32edf2012-09-21 23:45:46 -04001013 return -5;
Len Brown889facb2012-11-08 00:48:57 -05001014 t->extra_delta32 = msr & 0xFFFFFFFF;
Len Brown8e180f32012-09-22 01:25:08 -04001015 }
1016
1017 if (extra_delta_offset64)
1018 if (get_msr(cpu, extra_delta_offset64, &t->extra_delta64))
1019 return -5;
1020
1021 if (extra_msr_offset32) {
Len Brown889facb2012-11-08 00:48:57 -05001022 if (get_msr(cpu, extra_msr_offset32, &msr))
Len Brown8e180f32012-09-22 01:25:08 -04001023 return -5;
Len Brown889facb2012-11-08 00:48:57 -05001024 t->extra_msr32 = msr & 0xFFFFFFFF;
Len Brown8e180f32012-09-22 01:25:08 -04001025 }
Len Brown2f32edf2012-09-21 23:45:46 -04001026
1027 if (extra_msr_offset64)
1028 if (get_msr(cpu, extra_msr_offset64, &t->extra_msr64))
Len Brownc98d5d92012-06-04 00:56:40 -04001029 return -5;
1030
Len Brown144b44b2013-11-09 00:30:16 -05001031 if (use_c1_residency_msr) {
1032 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1033 return -6;
1034 }
1035
Len Brownc98d5d92012-06-04 00:56:40 -04001036 /* collect core counters only for 1st thread in core */
1037 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1038 return 0;
1039
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001040 if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001041 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1042 return -6;
Len Brown144b44b2013-11-09 00:30:16 -05001043 }
1044
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001045 if (do_nhm_cstates && !do_knl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001046 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1047 return -7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001048 } else if (do_knl_cstates) {
1049 if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1050 return -7;
Len Brownc98d5d92012-06-04 00:56:40 -04001051 }
1052
1053 if (do_snb_cstates)
1054 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1055 return -8;
1056
Len Brown889facb2012-11-08 00:48:57 -05001057 if (do_dts) {
1058 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1059 return -9;
1060 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1061 }
1062
1063
Len Brownc98d5d92012-06-04 00:56:40 -04001064 /* collect package counters only for 1st core in package */
1065 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1066 return 0;
1067
Len Brown0b2bb692015-03-26 00:50:30 -04001068 if (do_skl_residency) {
1069 if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
1070 return -10;
1071 if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
1072 return -11;
1073 if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
1074 return -12;
1075 if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
1076 return -13;
1077 }
Len Brownee7e38e2015-02-09 23:39:45 -05001078 if (do_pc3)
Len Brownc98d5d92012-06-04 00:56:40 -04001079 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1080 return -9;
Len Brownee7e38e2015-02-09 23:39:45 -05001081 if (do_pc6)
Len Brownc98d5d92012-06-04 00:56:40 -04001082 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1083 return -10;
Len Brownee7e38e2015-02-09 23:39:45 -05001084 if (do_pc2)
Len Brownc98d5d92012-06-04 00:56:40 -04001085 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1086 return -11;
Len Brownee7e38e2015-02-09 23:39:45 -05001087 if (do_pc7)
Len Brownc98d5d92012-06-04 00:56:40 -04001088 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1089 return -12;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001090 if (do_c8_c9_c10) {
1091 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1092 return -13;
1093 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1094 return -13;
1095 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1096 return -13;
1097 }
Len Brown889facb2012-11-08 00:48:57 -05001098 if (do_rapl & RAPL_PKG) {
1099 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1100 return -13;
1101 p->energy_pkg = msr & 0xFFFFFFFF;
1102 }
1103 if (do_rapl & RAPL_CORES) {
1104 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1105 return -14;
1106 p->energy_cores = msr & 0xFFFFFFFF;
1107 }
1108 if (do_rapl & RAPL_DRAM) {
1109 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1110 return -15;
1111 p->energy_dram = msr & 0xFFFFFFFF;
1112 }
1113 if (do_rapl & RAPL_GFX) {
1114 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1115 return -16;
1116 p->energy_gfx = msr & 0xFFFFFFFF;
1117 }
1118 if (do_rapl & RAPL_PKG_PERF_STATUS) {
1119 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1120 return -16;
1121 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1122 }
1123 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1124 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1125 return -16;
1126 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1127 }
1128 if (do_ptm) {
1129 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1130 return -17;
1131 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1132 }
Len Brown103a8fe2010-10-22 23:53:03 -04001133 return 0;
1134}
1135
Len Brownee7e38e2015-02-09 23:39:45 -05001136/*
1137 * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1138 * If you change the values, note they are used both in comparisons
1139 * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1140 */
1141
1142#define PCLUKN 0 /* Unknown */
1143#define PCLRSV 1 /* Reserved */
1144#define PCL__0 2 /* PC0 */
1145#define PCL__1 3 /* PC1 */
1146#define PCL__2 4 /* PC2 */
1147#define PCL__3 5 /* PC3 */
1148#define PCL__4 6 /* PC4 */
1149#define PCL__6 7 /* PC6 */
1150#define PCL_6N 8 /* PC6 No Retention */
1151#define PCL_6R 9 /* PC6 Retention */
1152#define PCL__7 10 /* PC7 */
1153#define PCL_7S 11 /* PC7 Shrink */
Len Brown0b2bb692015-03-26 00:50:30 -04001154#define PCL__8 12 /* PC8 */
1155#define PCL__9 13 /* PC9 */
1156#define PCLUNL 14 /* Unlimited */
Len Brownee7e38e2015-02-09 23:39:45 -05001157
1158int pkg_cstate_limit = PCLUKN;
1159char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
Len Brown0b2bb692015-03-26 00:50:30 -04001160 "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"};
Len Brownee7e38e2015-02-09 23:39:45 -05001161
Len Browne9257f52015-04-01 21:02:57 -04001162int 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};
1163int 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};
1164int 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};
1165int 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};
1166int 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};
1167int 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 -05001168
Len Browna2b7b742015-09-26 00:12:38 -04001169
1170static void
1171calculate_tsc_tweak()
1172{
Len Browna2b7b742015-09-26 00:12:38 -04001173 tsc_tweak = base_hz / tsc_hz;
1174}
1175
Len Brownfcd17212015-03-23 20:29:09 -04001176static void
1177dump_nhm_platform_info(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001178{
1179 unsigned long long msr;
1180 unsigned int ratio;
1181
Len Brownec0adc52015-11-12 02:42:31 -05001182 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001183
Len Brownec0adc52015-11-12 02:42:31 -05001184 fprintf(stderr, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001185
Len Brown103a8fe2010-10-22 23:53:03 -04001186 ratio = (msr >> 40) & 0xFF;
Len Brown8f61f352015-03-23 18:36:57 -04001187 fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001188 ratio, bclk, ratio * bclk);
1189
1190 ratio = (msr >> 8) & 0xFF;
Len Brown8f61f352015-03-23 18:36:57 -04001191 fprintf(stderr, "%d * %.0f = %.0f MHz base frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001192 ratio, bclk, ratio * bclk);
1193
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001194 get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
Len Brownbfae2052015-06-17 12:27:21 -04001195 fprintf(stderr, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
1196 base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
Len Brown67920412013-01-31 15:22:15 -05001197
Len Brownfcd17212015-03-23 20:29:09 -04001198 return;
1199}
1200
1201static void
1202dump_hsw_turbo_ratio_limits(void)
1203{
1204 unsigned long long msr;
1205 unsigned int ratio;
1206
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001207 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04001208
Len Brownbfae2052015-06-17 12:27:21 -04001209 fprintf(stderr, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04001210
1211 ratio = (msr >> 8) & 0xFF;
1212 if (ratio)
1213 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 18 active cores\n",
1214 ratio, bclk, ratio * bclk);
1215
1216 ratio = (msr >> 0) & 0xFF;
1217 if (ratio)
1218 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 17 active cores\n",
1219 ratio, bclk, ratio * bclk);
1220 return;
1221}
1222
1223static void
1224dump_ivt_turbo_ratio_limits(void)
1225{
1226 unsigned long long msr;
1227 unsigned int ratio;
Len Brown6574a5d2012-09-21 00:01:31 -04001228
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001229 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001230
Len Brownbfae2052015-06-17 12:27:21 -04001231 fprintf(stderr, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001232
1233 ratio = (msr >> 56) & 0xFF;
1234 if (ratio)
1235 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 16 active cores\n",
1236 ratio, bclk, ratio * bclk);
1237
1238 ratio = (msr >> 48) & 0xFF;
1239 if (ratio)
1240 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 15 active cores\n",
1241 ratio, bclk, ratio * bclk);
1242
1243 ratio = (msr >> 40) & 0xFF;
1244 if (ratio)
1245 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 14 active cores\n",
1246 ratio, bclk, ratio * bclk);
1247
1248 ratio = (msr >> 32) & 0xFF;
1249 if (ratio)
1250 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 13 active cores\n",
1251 ratio, bclk, ratio * bclk);
1252
1253 ratio = (msr >> 24) & 0xFF;
1254 if (ratio)
1255 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 12 active cores\n",
1256 ratio, bclk, ratio * bclk);
1257
1258 ratio = (msr >> 16) & 0xFF;
1259 if (ratio)
1260 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 11 active cores\n",
1261 ratio, bclk, ratio * bclk);
1262
1263 ratio = (msr >> 8) & 0xFF;
1264 if (ratio)
1265 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 10 active cores\n",
1266 ratio, bclk, ratio * bclk);
1267
1268 ratio = (msr >> 0) & 0xFF;
1269 if (ratio)
1270 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 9 active cores\n",
1271 ratio, bclk, ratio * bclk);
Len Brownfcd17212015-03-23 20:29:09 -04001272 return;
1273}
Len Brown6574a5d2012-09-21 00:01:31 -04001274
Len Brownfcd17212015-03-23 20:29:09 -04001275static void
1276dump_nhm_turbo_ratio_limits(void)
1277{
1278 unsigned long long msr;
1279 unsigned int ratio;
Len Brown103a8fe2010-10-22 23:53:03 -04001280
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001281 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001282
Len Brownbfae2052015-06-17 12:27:21 -04001283 fprintf(stderr, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001284
1285 ratio = (msr >> 56) & 0xFF;
1286 if (ratio)
1287 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 8 active cores\n",
1288 ratio, bclk, ratio * bclk);
1289
1290 ratio = (msr >> 48) & 0xFF;
1291 if (ratio)
1292 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 7 active cores\n",
1293 ratio, bclk, ratio * bclk);
1294
1295 ratio = (msr >> 40) & 0xFF;
1296 if (ratio)
1297 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 6 active cores\n",
1298 ratio, bclk, ratio * bclk);
1299
1300 ratio = (msr >> 32) & 0xFF;
1301 if (ratio)
1302 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 5 active cores\n",
1303 ratio, bclk, ratio * bclk);
1304
Len Brown103a8fe2010-10-22 23:53:03 -04001305 ratio = (msr >> 24) & 0xFF;
1306 if (ratio)
1307 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
1308 ratio, bclk, ratio * bclk);
1309
1310 ratio = (msr >> 16) & 0xFF;
1311 if (ratio)
1312 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
1313 ratio, bclk, ratio * bclk);
1314
1315 ratio = (msr >> 8) & 0xFF;
1316 if (ratio)
1317 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
1318 ratio, bclk, ratio * bclk);
1319
1320 ratio = (msr >> 0) & 0xFF;
1321 if (ratio)
1322 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
1323 ratio, bclk, ratio * bclk);
Len Brownfcd17212015-03-23 20:29:09 -04001324 return;
1325}
Len Brown3a9a9412014-08-15 02:39:52 -04001326
Len Brownfcd17212015-03-23 20:29:09 -04001327static void
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001328dump_knl_turbo_ratio_limits(void)
1329{
1330 int cores;
1331 unsigned int ratio;
1332 unsigned long long msr;
1333 int delta_cores;
1334 int delta_ratio;
1335 int i;
1336
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001337 get_msr(base_cpu, MSR_NHM_TURBO_RATIO_LIMIT, &msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001338
Len Brownbfae2052015-06-17 12:27:21 -04001339 fprintf(stderr, "cpu%d: MSR_NHM_TURBO_RATIO_LIMIT: 0x%08llx\n",
1340 base_cpu, msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001341
1342 /**
1343 * Turbo encoding in KNL is as follows:
1344 * [7:0] -- Base value of number of active cores of bucket 1.
1345 * [15:8] -- Base value of freq ratio of bucket 1.
1346 * [20:16] -- +ve delta of number of active cores of bucket 2.
1347 * i.e. active cores of bucket 2 =
1348 * active cores of bucket 1 + delta
1349 * [23:21] -- Negative delta of freq ratio of bucket 2.
1350 * i.e. freq ratio of bucket 2 =
1351 * freq ratio of bucket 1 - delta
1352 * [28:24]-- +ve delta of number of active cores of bucket 3.
1353 * [31:29]-- -ve delta of freq ratio of bucket 3.
1354 * [36:32]-- +ve delta of number of active cores of bucket 4.
1355 * [39:37]-- -ve delta of freq ratio of bucket 4.
1356 * [44:40]-- +ve delta of number of active cores of bucket 5.
1357 * [47:45]-- -ve delta of freq ratio of bucket 5.
1358 * [52:48]-- +ve delta of number of active cores of bucket 6.
1359 * [55:53]-- -ve delta of freq ratio of bucket 6.
1360 * [60:56]-- +ve delta of number of active cores of bucket 7.
1361 * [63:61]-- -ve delta of freq ratio of bucket 7.
1362 */
1363 cores = msr & 0xFF;
1364 ratio = (msr >> 8) && 0xFF;
1365 if (ratio > 0)
1366 fprintf(stderr,
1367 "%d * %.0f = %.0f MHz max turbo %d active cores\n",
1368 ratio, bclk, ratio * bclk, cores);
1369
1370 for (i = 16; i < 64; i = i + 8) {
1371 delta_cores = (msr >> i) & 0x1F;
1372 delta_ratio = (msr >> (i + 5)) && 0x7;
1373 if (!delta_cores || !delta_ratio)
1374 return;
1375 cores = cores + delta_cores;
1376 ratio = ratio - delta_ratio;
1377
1378 /** -ve ratios will make successive ratio calculations
1379 * negative. Hence return instead of carrying on.
1380 */
1381 if (ratio > 0)
1382 fprintf(stderr,
1383 "%d * %.0f = %.0f MHz max turbo %d active cores\n",
1384 ratio, bclk, ratio * bclk, cores);
1385 }
1386}
1387
1388static void
Len Brownfcd17212015-03-23 20:29:09 -04001389dump_nhm_cst_cfg(void)
1390{
1391 unsigned long long msr;
1392
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001393 get_msr(base_cpu, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04001394
1395#define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
1396#define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
1397
Len Brownbfae2052015-06-17 12:27:21 -04001398 fprintf(stderr, "cpu%d: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04001399
1400 fprintf(stderr, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: %s)\n",
1401 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
1402 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
1403 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
1404 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
1405 (msr & (1 << 15)) ? "" : "UN",
1406 (unsigned int)msr & 7,
1407 pkg_cstate_limit_strings[pkg_cstate_limit]);
1408 return;
Len Brown103a8fe2010-10-22 23:53:03 -04001409}
1410
Len Brown6fb31432015-06-17 16:23:45 -04001411static void
1412dump_config_tdp(void)
1413{
1414 unsigned long long msr;
1415
1416 get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
1417 fprintf(stderr, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
1418 fprintf(stderr, " (base_ratio=%d)\n", (unsigned int)msr & 0xEF);
1419
1420 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
1421 fprintf(stderr, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
1422 if (msr) {
1423 fprintf(stderr, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0xEFFF);
1424 fprintf(stderr, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0xEFFF);
1425 fprintf(stderr, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xEF);
1426 fprintf(stderr, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0xEFFF);
1427 }
1428 fprintf(stderr, ")\n");
1429
1430 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
1431 fprintf(stderr, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
1432 if (msr) {
1433 fprintf(stderr, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0xEFFF);
1434 fprintf(stderr, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0xEFFF);
1435 fprintf(stderr, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xEF);
1436 fprintf(stderr, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0xEFFF);
1437 }
1438 fprintf(stderr, ")\n");
1439
1440 get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
1441 fprintf(stderr, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
1442 if ((msr) & 0x3)
1443 fprintf(stderr, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
1444 fprintf(stderr, " lock=%d", (unsigned int)(msr >> 31) & 1);
1445 fprintf(stderr, ")\n");
1446
1447 get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
1448 fprintf(stderr, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
Len Brown759d2a92015-10-22 02:42:12 -04001449 fprintf(stderr, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0x7F);
Len Brown6fb31432015-06-17 16:23:45 -04001450 fprintf(stderr, " lock=%d", (unsigned int)(msr >> 31) & 1);
1451 fprintf(stderr, ")\n");
1452}
1453
Len Brownc98d5d92012-06-04 00:56:40 -04001454void free_all_buffers(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001455{
Len Brownc98d5d92012-06-04 00:56:40 -04001456 CPU_FREE(cpu_present_set);
1457 cpu_present_set = NULL;
1458 cpu_present_set = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001459
Len Brownc98d5d92012-06-04 00:56:40 -04001460 CPU_FREE(cpu_affinity_set);
1461 cpu_affinity_set = NULL;
1462 cpu_affinity_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001463
Len Brownc98d5d92012-06-04 00:56:40 -04001464 free(thread_even);
1465 free(core_even);
1466 free(package_even);
1467
1468 thread_even = NULL;
1469 core_even = NULL;
1470 package_even = NULL;
1471
1472 free(thread_odd);
1473 free(core_odd);
1474 free(package_odd);
1475
1476 thread_odd = NULL;
1477 core_odd = NULL;
1478 package_odd = NULL;
1479
1480 free(output_buffer);
1481 output_buffer = NULL;
1482 outp = NULL;
Len Brown103a8fe2010-10-22 23:53:03 -04001483}
1484
Len Brownc98d5d92012-06-04 00:56:40 -04001485/*
Josh Triplett57a42a32013-08-20 17:20:17 -07001486 * Open a file, and exit on failure
1487 */
1488FILE *fopen_or_die(const char *path, const char *mode)
1489{
1490 FILE *filep = fopen(path, "r");
Josh Triplettb2c95d92013-08-20 17:20:18 -07001491 if (!filep)
1492 err(1, "%s: open failed", path);
Josh Triplett57a42a32013-08-20 17:20:17 -07001493 return filep;
1494}
1495
1496/*
Josh Triplett95aebc42013-08-20 17:20:16 -07001497 * Parse a file containing a single int.
1498 */
1499int parse_int_file(const char *fmt, ...)
1500{
1501 va_list args;
1502 char path[PATH_MAX];
1503 FILE *filep;
1504 int value;
1505
1506 va_start(args, fmt);
1507 vsnprintf(path, sizeof(path), fmt, args);
1508 va_end(args);
Josh Triplett57a42a32013-08-20 17:20:17 -07001509 filep = fopen_or_die(path, "r");
Josh Triplettb2c95d92013-08-20 17:20:18 -07001510 if (fscanf(filep, "%d", &value) != 1)
1511 err(1, "%s: failed to parse number from file", path);
Josh Triplett95aebc42013-08-20 17:20:16 -07001512 fclose(filep);
1513 return value;
1514}
1515
1516/*
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001517 * get_cpu_position_in_core(cpu)
1518 * return the position of the CPU among its HT siblings in the core
1519 * return -1 if the sibling is not in list
Len Brownc98d5d92012-06-04 00:56:40 -04001520 */
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001521int get_cpu_position_in_core(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001522{
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001523 char path[64];
1524 FILE *filep;
1525 int this_cpu;
1526 char character;
1527 int i;
1528
1529 sprintf(path,
1530 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list",
1531 cpu);
1532 filep = fopen(path, "r");
1533 if (filep == NULL) {
1534 perror(path);
1535 exit(1);
1536 }
1537
1538 for (i = 0; i < topo.num_threads_per_core; i++) {
1539 fscanf(filep, "%d", &this_cpu);
1540 if (this_cpu == cpu) {
1541 fclose(filep);
1542 return i;
1543 }
1544
1545 /* Account for no separator after last thread*/
1546 if (i != (topo.num_threads_per_core - 1))
1547 fscanf(filep, "%c", &character);
1548 }
1549
1550 fclose(filep);
1551 return -1;
Len Brown103a8fe2010-10-22 23:53:03 -04001552}
1553
Len Brownc98d5d92012-06-04 00:56:40 -04001554/*
1555 * cpu_is_first_core_in_package(cpu)
1556 * return 1 if given CPU is 1st core in package
1557 */
1558int cpu_is_first_core_in_package(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001559{
Josh Triplett95aebc42013-08-20 17:20:16 -07001560 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001561}
1562
1563int get_physical_package_id(int cpu)
1564{
Josh Triplett95aebc42013-08-20 17:20:16 -07001565 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001566}
1567
1568int get_core_id(int cpu)
1569{
Josh Triplett95aebc42013-08-20 17:20:16 -07001570 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001571}
1572
Len Brownc98d5d92012-06-04 00:56:40 -04001573int get_num_ht_siblings(int cpu)
1574{
1575 char path[80];
1576 FILE *filep;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001577 int sib1;
1578 int matches = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001579 char character;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001580 char str[100];
1581 char *ch;
Len Brownc98d5d92012-06-04 00:56:40 -04001582
1583 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
Josh Triplett57a42a32013-08-20 17:20:17 -07001584 filep = fopen_or_die(path, "r");
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001585
Len Brownc98d5d92012-06-04 00:56:40 -04001586 /*
1587 * file format:
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001588 * A ',' separated or '-' separated set of numbers
1589 * (eg 1-2 or 1,3,4,5)
Len Brownc98d5d92012-06-04 00:56:40 -04001590 */
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001591 fscanf(filep, "%d%c\n", &sib1, &character);
1592 fseek(filep, 0, SEEK_SET);
1593 fgets(str, 100, filep);
1594 ch = strchr(str, character);
1595 while (ch != NULL) {
1596 matches++;
1597 ch = strchr(ch+1, character);
1598 }
Len Brownc98d5d92012-06-04 00:56:40 -04001599
1600 fclose(filep);
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001601 return matches+1;
Len Brownc98d5d92012-06-04 00:56:40 -04001602}
1603
Len Brown103a8fe2010-10-22 23:53:03 -04001604/*
Len Brownc98d5d92012-06-04 00:56:40 -04001605 * run func(thread, core, package) in topology order
1606 * skip non-present cpus
Len Brown103a8fe2010-10-22 23:53:03 -04001607 */
1608
Len Brownc98d5d92012-06-04 00:56:40 -04001609int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
1610 struct pkg_data *, struct thread_data *, struct core_data *,
1611 struct pkg_data *), struct thread_data *thread_base,
1612 struct core_data *core_base, struct pkg_data *pkg_base,
1613 struct thread_data *thread_base2, struct core_data *core_base2,
1614 struct pkg_data *pkg_base2)
1615{
1616 int retval, pkg_no, core_no, thread_no;
1617
1618 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
1619 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
1620 for (thread_no = 0; thread_no <
1621 topo.num_threads_per_core; ++thread_no) {
1622 struct thread_data *t, *t2;
1623 struct core_data *c, *c2;
1624 struct pkg_data *p, *p2;
1625
1626 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
1627
1628 if (cpu_is_not_present(t->cpu_id))
1629 continue;
1630
1631 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
1632
1633 c = GET_CORE(core_base, core_no, pkg_no);
1634 c2 = GET_CORE(core_base2, core_no, pkg_no);
1635
1636 p = GET_PKG(pkg_base, pkg_no);
1637 p2 = GET_PKG(pkg_base2, pkg_no);
1638
1639 retval = func(t, c, p, t2, c2, p2);
1640 if (retval)
1641 return retval;
1642 }
1643 }
1644 }
1645 return 0;
1646}
1647
1648/*
1649 * run func(cpu) on every cpu in /proc/stat
1650 * return max_cpu number
1651 */
1652int for_all_proc_cpus(int (func)(int))
Len Brown103a8fe2010-10-22 23:53:03 -04001653{
1654 FILE *fp;
Len Brownc98d5d92012-06-04 00:56:40 -04001655 int cpu_num;
Len Brown103a8fe2010-10-22 23:53:03 -04001656 int retval;
1657
Josh Triplett57a42a32013-08-20 17:20:17 -07001658 fp = fopen_or_die(proc_stat, "r");
Len Brown103a8fe2010-10-22 23:53:03 -04001659
1660 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
Josh Triplettb2c95d92013-08-20 17:20:18 -07001661 if (retval != 0)
1662 err(1, "%s: failed to parse format", proc_stat);
Len Brown103a8fe2010-10-22 23:53:03 -04001663
Len Brownc98d5d92012-06-04 00:56:40 -04001664 while (1) {
1665 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 -04001666 if (retval != 1)
1667 break;
1668
Len Brownc98d5d92012-06-04 00:56:40 -04001669 retval = func(cpu_num);
1670 if (retval) {
1671 fclose(fp);
1672 return(retval);
1673 }
Len Brown103a8fe2010-10-22 23:53:03 -04001674 }
1675 fclose(fp);
Len Brownc98d5d92012-06-04 00:56:40 -04001676 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001677}
1678
1679void re_initialize(void)
1680{
Len Brownc98d5d92012-06-04 00:56:40 -04001681 free_all_buffers();
1682 setup_all_buffers();
1683 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04001684}
1685
Len Brownc98d5d92012-06-04 00:56:40 -04001686
Len Brown103a8fe2010-10-22 23:53:03 -04001687/*
Len Brownc98d5d92012-06-04 00:56:40 -04001688 * count_cpus()
1689 * remember the last one seen, it will be the max
Len Brown103a8fe2010-10-22 23:53:03 -04001690 */
Len Brownc98d5d92012-06-04 00:56:40 -04001691int count_cpus(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001692{
Len Brownc98d5d92012-06-04 00:56:40 -04001693 if (topo.max_cpu_num < cpu)
1694 topo.max_cpu_num = cpu;
Len Brown103a8fe2010-10-22 23:53:03 -04001695
Len Brownc98d5d92012-06-04 00:56:40 -04001696 topo.num_cpus += 1;
1697 return 0;
1698}
1699int mark_cpu_present(int cpu)
1700{
1701 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brown15aaa342012-03-29 22:19:58 -04001702 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001703}
1704
1705void turbostat_loop()
1706{
Len Brownc98d5d92012-06-04 00:56:40 -04001707 int retval;
Len Browne52966c2012-11-08 22:38:05 -05001708 int restarted = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001709
Len Brown103a8fe2010-10-22 23:53:03 -04001710restart:
Len Browne52966c2012-11-08 22:38:05 -05001711 restarted++;
1712
Len Brownc98d5d92012-06-04 00:56:40 -04001713 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001714 if (retval < -1) {
1715 exit(retval);
1716 } else if (retval == -1) {
Len Browne52966c2012-11-08 22:38:05 -05001717 if (restarted > 1) {
1718 exit(retval);
1719 }
Len Brownc98d5d92012-06-04 00:56:40 -04001720 re_initialize();
1721 goto restart;
1722 }
Len Browne52966c2012-11-08 22:38:05 -05001723 restarted = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001724 gettimeofday(&tv_even, (struct timezone *)NULL);
1725
1726 while (1) {
Len Brownc98d5d92012-06-04 00:56:40 -04001727 if (for_all_proc_cpus(cpu_is_not_present)) {
Len Brown103a8fe2010-10-22 23:53:03 -04001728 re_initialize();
1729 goto restart;
1730 }
1731 sleep(interval_sec);
Len Brownc98d5d92012-06-04 00:56:40 -04001732 retval = for_all_cpus(get_counters, ODD_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001733 if (retval < -1) {
1734 exit(retval);
1735 } else if (retval == -1) {
Len Brown15aaa342012-03-29 22:19:58 -04001736 re_initialize();
1737 goto restart;
1738 }
Len Brown103a8fe2010-10-22 23:53:03 -04001739 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04001740 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04001741 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
1742 compute_average(EVEN_COUNTERS);
1743 format_all_counters(EVEN_COUNTERS);
1744 flush_stdout();
Len Brown15aaa342012-03-29 22:19:58 -04001745 sleep(interval_sec);
Len Brownc98d5d92012-06-04 00:56:40 -04001746 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001747 if (retval < -1) {
1748 exit(retval);
1749 } else if (retval == -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04001750 re_initialize();
1751 goto restart;
1752 }
Len Brown103a8fe2010-10-22 23:53:03 -04001753 gettimeofday(&tv_even, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04001754 timersub(&tv_even, &tv_odd, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04001755 for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS);
1756 compute_average(ODD_COUNTERS);
1757 format_all_counters(ODD_COUNTERS);
1758 flush_stdout();
Len Brown103a8fe2010-10-22 23:53:03 -04001759 }
1760}
1761
1762void check_dev_msr()
1763{
1764 struct stat sb;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001765 char pathname[32];
Len Brown103a8fe2010-10-22 23:53:03 -04001766
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001767 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
1768 if (stat(pathname, &sb))
Len Browna21d38c2015-03-24 16:37:35 -04001769 if (system("/sbin/modprobe msr > /dev/null 2>&1"))
1770 err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
Len Brown103a8fe2010-10-22 23:53:03 -04001771}
1772
Len Brown98481e72014-08-15 00:36:50 -04001773void check_permissions()
Len Brown103a8fe2010-10-22 23:53:03 -04001774{
Len Brown98481e72014-08-15 00:36:50 -04001775 struct __user_cap_header_struct cap_header_data;
1776 cap_user_header_t cap_header = &cap_header_data;
1777 struct __user_cap_data_struct cap_data_data;
1778 cap_user_data_t cap_data = &cap_data_data;
1779 extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
1780 int do_exit = 0;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001781 char pathname[32];
Len Brown98481e72014-08-15 00:36:50 -04001782
1783 /* check for CAP_SYS_RAWIO */
1784 cap_header->pid = getpid();
1785 cap_header->version = _LINUX_CAPABILITY_VERSION;
1786 if (capget(cap_header, cap_data) < 0)
1787 err(-6, "capget(2) failed");
1788
1789 if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
1790 do_exit++;
1791 warnx("capget(CAP_SYS_RAWIO) failed,"
1792 " try \"# setcap cap_sys_rawio=ep %s\"", progname);
1793 }
1794
1795 /* test file permissions */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001796 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
1797 if (euidaccess(pathname, R_OK)) {
Len Brown98481e72014-08-15 00:36:50 -04001798 do_exit++;
1799 warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
1800 }
1801
1802 /* if all else fails, thell them to be root */
1803 if (do_exit)
1804 if (getuid() != 0)
Len Brownd7899442015-01-23 00:12:33 -05001805 warnx("... or simply run as root");
Len Brown98481e72014-08-15 00:36:50 -04001806
1807 if (do_exit)
1808 exit(-6);
Len Brown103a8fe2010-10-22 23:53:03 -04001809}
1810
Len Brownd7899442015-01-23 00:12:33 -05001811/*
1812 * NHM adds support for additional MSRs:
1813 *
1814 * MSR_SMI_COUNT 0x00000034
1815 *
Len Brownec0adc52015-11-12 02:42:31 -05001816 * MSR_PLATFORM_INFO 0x000000ce
Len Brownd7899442015-01-23 00:12:33 -05001817 * MSR_NHM_SNB_PKG_CST_CFG_CTL 0x000000e2
1818 *
1819 * MSR_PKG_C3_RESIDENCY 0x000003f8
1820 * MSR_PKG_C6_RESIDENCY 0x000003f9
1821 * MSR_CORE_C3_RESIDENCY 0x000003fc
1822 * MSR_CORE_C6_RESIDENCY 0x000003fd
1823 *
Len Brownee7e38e2015-02-09 23:39:45 -05001824 * Side effect:
1825 * sets global pkg_cstate_limit to decode MSR_NHM_SNB_PKG_CST_CFG_CTL
Len Brownd7899442015-01-23 00:12:33 -05001826 */
Len Brownee7e38e2015-02-09 23:39:45 -05001827int probe_nhm_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04001828{
Len Brownee7e38e2015-02-09 23:39:45 -05001829 unsigned long long msr;
Len Brown21ed5572015-10-19 22:37:40 -04001830 unsigned int base_ratio;
Len Brownee7e38e2015-02-09 23:39:45 -05001831 int *pkg_cstate_limits;
1832
Len Brown103a8fe2010-10-22 23:53:03 -04001833 if (!genuine_intel)
1834 return 0;
1835
1836 if (family != 6)
1837 return 0;
1838
Len Brown21ed5572015-10-19 22:37:40 -04001839 bclk = discover_bclk(family, model);
1840
Len Brown103a8fe2010-10-22 23:53:03 -04001841 switch (model) {
1842 case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
1843 case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
1844 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
1845 case 0x25: /* Westmere Client - Clarkdale, Arrandale */
1846 case 0x2C: /* Westmere EP - Gulftown */
Len Brownee7e38e2015-02-09 23:39:45 -05001847 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1848 case 0x2F: /* Westmere-EX Xeon - Eagleton */
1849 pkg_cstate_limits = nhm_pkg_cstate_limits;
1850 break;
Len Brown103a8fe2010-10-22 23:53:03 -04001851 case 0x2A: /* SNB */
1852 case 0x2D: /* SNB Xeon */
Len Brown553575f2011-11-18 03:32:01 -05001853 case 0x3A: /* IVB */
Len Brown13006512012-09-26 18:11:31 -04001854 case 0x3E: /* IVB Xeon */
Len Brownee7e38e2015-02-09 23:39:45 -05001855 pkg_cstate_limits = snb_pkg_cstate_limits;
1856 break;
Len Brown70b43402013-01-08 01:26:07 -05001857 case 0x3C: /* HSW */
Len Browne6f9bb32013-12-03 02:19:19 -05001858 case 0x3F: /* HSX */
Len Brown70b43402013-01-08 01:26:07 -05001859 case 0x45: /* HSW */
Len Brown149c2312013-03-15 10:58:02 -04001860 case 0x46: /* HSW */
Len Brown4e8e863f2014-02-27 23:28:53 -05001861 case 0x3D: /* BDW */
Len Brown48a06312015-02-10 15:38:04 -05001862 case 0x47: /* BDW */
Len Brown4e8e863f2014-02-27 23:28:53 -05001863 case 0x4F: /* BDX */
1864 case 0x56: /* BDX-DE */
Len Brown0b2bb692015-03-26 00:50:30 -04001865 case 0x4E: /* SKL */
1866 case 0x5E: /* SKL */
Len Brownee7e38e2015-02-09 23:39:45 -05001867 pkg_cstate_limits = hsw_pkg_cstate_limits;
1868 break;
1869 case 0x37: /* BYT */
1870 case 0x4D: /* AVN */
1871 pkg_cstate_limits = slv_pkg_cstate_limits;
1872 break;
1873 case 0x4C: /* AMT */
1874 pkg_cstate_limits = amt_pkg_cstate_limits;
1875 break;
1876 case 0x57: /* PHI */
1877 pkg_cstate_limits = phi_pkg_cstate_limits;
1878 break;
Len Brown103a8fe2010-10-22 23:53:03 -04001879 default:
1880 return 0;
1881 }
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001882 get_msr(base_cpu, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
Len Browne9257f52015-04-01 21:02:57 -04001883 pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
Len Brownee7e38e2015-02-09 23:39:45 -05001884
Len Brownec0adc52015-11-12 02:42:31 -05001885 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown21ed5572015-10-19 22:37:40 -04001886 base_ratio = (msr >> 8) & 0xFF;
1887
1888 base_hz = base_ratio * bclk * 1000000;
1889 has_base_hz = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05001890 return 1;
Len Brown103a8fe2010-10-22 23:53:03 -04001891}
Len Brownd7899442015-01-23 00:12:33 -05001892int has_nhm_turbo_ratio_limit(unsigned int family, unsigned int model)
1893{
Len Brownd7899442015-01-23 00:12:33 -05001894 switch (model) {
1895 /* Nehalem compatible, but do not include turbo-ratio limit support */
1896 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1897 case 0x2F: /* Westmere-EX Xeon - Eagleton */
1898 return 0;
1899 default:
1900 return 1;
1901 }
1902}
Len Brown6574a5d2012-09-21 00:01:31 -04001903int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
1904{
1905 if (!genuine_intel)
1906 return 0;
1907
1908 if (family != 6)
1909 return 0;
1910
1911 switch (model) {
1912 case 0x3E: /* IVB Xeon */
Len Brownfcd17212015-03-23 20:29:09 -04001913 case 0x3F: /* HSW Xeon */
Len Brown6574a5d2012-09-21 00:01:31 -04001914 return 1;
1915 default:
1916 return 0;
1917 }
1918}
Len Brownfcd17212015-03-23 20:29:09 -04001919int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
1920{
1921 if (!genuine_intel)
1922 return 0;
1923
1924 if (family != 6)
1925 return 0;
1926
1927 switch (model) {
1928 case 0x3F: /* HSW Xeon */
1929 return 1;
1930 default:
1931 return 0;
1932 }
1933}
1934
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001935int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
1936{
1937 if (!genuine_intel)
1938 return 0;
1939
1940 if (family != 6)
1941 return 0;
1942
1943 switch (model) {
1944 case 0x57: /* Knights Landing */
1945 return 1;
1946 default:
1947 return 0;
1948 }
1949}
Len Brown6fb31432015-06-17 16:23:45 -04001950int has_config_tdp(unsigned int family, unsigned int model)
1951{
1952 if (!genuine_intel)
1953 return 0;
1954
1955 if (family != 6)
1956 return 0;
1957
1958 switch (model) {
1959 case 0x3A: /* IVB */
Len Brown6fb31432015-06-17 16:23:45 -04001960 case 0x3C: /* HSW */
1961 case 0x3F: /* HSX */
1962 case 0x45: /* HSW */
1963 case 0x46: /* HSW */
1964 case 0x3D: /* BDW */
1965 case 0x47: /* BDW */
1966 case 0x4F: /* BDX */
1967 case 0x56: /* BDX-DE */
1968 case 0x4E: /* SKL */
1969 case 0x5E: /* SKL */
1970
1971 case 0x57: /* Knights Landing */
1972 return 1;
1973 default:
1974 return 0;
1975 }
1976}
1977
Len Brownfcd17212015-03-23 20:29:09 -04001978static void
1979dump_cstate_pstate_config_info(family, model)
1980{
1981 if (!do_nhm_platform_info)
1982 return;
1983
1984 dump_nhm_platform_info();
1985
1986 if (has_hsw_turbo_ratio_limit(family, model))
1987 dump_hsw_turbo_ratio_limits();
1988
1989 if (has_ivt_turbo_ratio_limit(family, model))
1990 dump_ivt_turbo_ratio_limits();
1991
1992 if (has_nhm_turbo_ratio_limit(family, model))
1993 dump_nhm_turbo_ratio_limits();
1994
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001995 if (has_knl_turbo_ratio_limit(family, model))
1996 dump_knl_turbo_ratio_limits();
1997
Len Brown6fb31432015-06-17 16:23:45 -04001998 if (has_config_tdp(family, model))
1999 dump_config_tdp();
2000
Len Brownfcd17212015-03-23 20:29:09 -04002001 dump_nhm_cst_cfg();
2002}
2003
Len Brown6574a5d2012-09-21 00:01:31 -04002004
Len Brown889facb2012-11-08 00:48:57 -05002005/*
2006 * print_epb()
2007 * Decode the ENERGY_PERF_BIAS MSR
2008 */
2009int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2010{
2011 unsigned long long msr;
2012 char *epb_string;
2013 int cpu;
2014
2015 if (!has_epb)
2016 return 0;
2017
2018 cpu = t->cpu_id;
2019
2020 /* EPB is per-package */
2021 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2022 return 0;
2023
2024 if (cpu_migrate(cpu)) {
2025 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
2026 return -1;
2027 }
2028
2029 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
2030 return 0;
2031
Len Browne9be7dd2015-05-26 12:19:37 -04002032 switch (msr & 0xF) {
Len Brown889facb2012-11-08 00:48:57 -05002033 case ENERGY_PERF_BIAS_PERFORMANCE:
2034 epb_string = "performance";
2035 break;
2036 case ENERGY_PERF_BIAS_NORMAL:
2037 epb_string = "balanced";
2038 break;
2039 case ENERGY_PERF_BIAS_POWERSAVE:
2040 epb_string = "powersave";
2041 break;
2042 default:
2043 epb_string = "custom";
2044 break;
2045 }
2046 fprintf(stderr, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
2047
2048 return 0;
2049}
Len Brown7f5c2582015-12-01 01:36:39 -05002050/*
2051 * print_hwp()
2052 * Decode the MSR_HWP_CAPABILITIES
2053 */
2054int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2055{
2056 unsigned long long msr;
2057 int cpu;
2058
2059 if (!has_hwp)
2060 return 0;
2061
2062 cpu = t->cpu_id;
2063
2064 /* MSR_HWP_CAPABILITIES is per-package */
2065 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2066 return 0;
2067
2068 if (cpu_migrate(cpu)) {
2069 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
2070 return -1;
2071 }
2072
2073 if (get_msr(cpu, MSR_PM_ENABLE, &msr))
2074 return 0;
2075
2076 fprintf(stderr, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n",
2077 cpu, msr, (msr & (1 << 0)) ? "" : "No-");
2078
2079 /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
2080 if ((msr & (1 << 0)) == 0)
2081 return 0;
2082
2083 if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
2084 return 0;
2085
2086 fprintf(stderr, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
2087 "(high 0x%x guar 0x%x eff 0x%x low 0x%x)\n",
2088 cpu, msr,
2089 (unsigned int)HWP_HIGHEST_PERF(msr),
2090 (unsigned int)HWP_GUARANTEED_PERF(msr),
2091 (unsigned int)HWP_MOSTEFFICIENT_PERF(msr),
2092 (unsigned int)HWP_LOWEST_PERF(msr));
2093
2094 if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
2095 return 0;
2096
2097 fprintf(stderr, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
2098 "(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x pkg 0x%x)\n",
2099 cpu, msr,
2100 (unsigned int)(((msr) >> 0) & 0xff),
2101 (unsigned int)(((msr) >> 8) & 0xff),
2102 (unsigned int)(((msr) >> 16) & 0xff),
2103 (unsigned int)(((msr) >> 24) & 0xff),
2104 (unsigned int)(((msr) >> 32) & 0xff3),
2105 (unsigned int)(((msr) >> 42) & 0x1));
2106
2107 if (has_hwp_pkg) {
2108 if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
2109 return 0;
2110
2111 fprintf(stderr, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
2112 "(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x)\n",
2113 cpu, msr,
2114 (unsigned int)(((msr) >> 0) & 0xff),
2115 (unsigned int)(((msr) >> 8) & 0xff),
2116 (unsigned int)(((msr) >> 16) & 0xff),
2117 (unsigned int)(((msr) >> 24) & 0xff),
2118 (unsigned int)(((msr) >> 32) & 0xff3));
2119 }
2120 if (has_hwp_notify) {
2121 if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
2122 return 0;
2123
2124 fprintf(stderr, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
2125 "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
2126 cpu, msr,
2127 ((msr) & 0x1) ? "EN" : "Dis",
2128 ((msr) & 0x2) ? "EN" : "Dis");
2129 }
2130 if (get_msr(cpu, MSR_HWP_STATUS, &msr))
2131 return 0;
2132
2133 fprintf(stderr, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
2134 "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
2135 cpu, msr,
2136 ((msr) & 0x1) ? "" : "No-",
2137 ((msr) & 0x2) ? "" : "No-");
2138
2139 return 0;
2140}
Len Brown889facb2012-11-08 00:48:57 -05002141
Len Brown3a9a9412014-08-15 02:39:52 -04002142/*
2143 * print_perf_limit()
2144 */
2145int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2146{
2147 unsigned long long msr;
2148 int cpu;
2149
2150 cpu = t->cpu_id;
2151
2152 /* per-package */
2153 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2154 return 0;
2155
2156 if (cpu_migrate(cpu)) {
2157 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
2158 return -1;
2159 }
2160
2161 if (do_core_perf_limit_reasons) {
2162 get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
2163 fprintf(stderr, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2164 fprintf(stderr, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
Len Browne33cbe82015-03-13 16:30:57 -04002165 (msr & 1 << 15) ? "bit15, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04002166 (msr & 1 << 14) ? "bit14, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04002167 (msr & 1 << 13) ? "Transitions, " : "",
2168 (msr & 1 << 12) ? "MultiCoreTurbo, " : "",
2169 (msr & 1 << 11) ? "PkgPwrL2, " : "",
2170 (msr & 1 << 10) ? "PkgPwrL1, " : "",
2171 (msr & 1 << 9) ? "CorePwr, " : "",
2172 (msr & 1 << 8) ? "Amps, " : "",
2173 (msr & 1 << 6) ? "VR-Therm, " : "",
2174 (msr & 1 << 5) ? "Auto-HWP, " : "",
2175 (msr & 1 << 4) ? "Graphics, " : "",
2176 (msr & 1 << 2) ? "bit2, " : "",
2177 (msr & 1 << 1) ? "ThermStatus, " : "",
2178 (msr & 1 << 0) ? "PROCHOT, " : "");
Len Brown3a9a9412014-08-15 02:39:52 -04002179 fprintf(stderr, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n",
Len Browne33cbe82015-03-13 16:30:57 -04002180 (msr & 1 << 31) ? "bit31, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04002181 (msr & 1 << 30) ? "bit30, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04002182 (msr & 1 << 29) ? "Transitions, " : "",
2183 (msr & 1 << 28) ? "MultiCoreTurbo, " : "",
2184 (msr & 1 << 27) ? "PkgPwrL2, " : "",
2185 (msr & 1 << 26) ? "PkgPwrL1, " : "",
2186 (msr & 1 << 25) ? "CorePwr, " : "",
2187 (msr & 1 << 24) ? "Amps, " : "",
2188 (msr & 1 << 22) ? "VR-Therm, " : "",
2189 (msr & 1 << 21) ? "Auto-HWP, " : "",
2190 (msr & 1 << 20) ? "Graphics, " : "",
2191 (msr & 1 << 18) ? "bit18, " : "",
2192 (msr & 1 << 17) ? "ThermStatus, " : "",
2193 (msr & 1 << 16) ? "PROCHOT, " : "");
Len Brown3a9a9412014-08-15 02:39:52 -04002194
2195 }
2196 if (do_gfx_perf_limit_reasons) {
2197 get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
2198 fprintf(stderr, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2199 fprintf(stderr, " (Active: %s%s%s%s%s%s%s%s)",
2200 (msr & 1 << 0) ? "PROCHOT, " : "",
2201 (msr & 1 << 1) ? "ThermStatus, " : "",
2202 (msr & 1 << 4) ? "Graphics, " : "",
2203 (msr & 1 << 6) ? "VR-Therm, " : "",
2204 (msr & 1 << 8) ? "Amps, " : "",
2205 (msr & 1 << 9) ? "GFXPwr, " : "",
2206 (msr & 1 << 10) ? "PkgPwrL1, " : "",
2207 (msr & 1 << 11) ? "PkgPwrL2, " : "");
2208 fprintf(stderr, " (Logged: %s%s%s%s%s%s%s%s)\n",
2209 (msr & 1 << 16) ? "PROCHOT, " : "",
2210 (msr & 1 << 17) ? "ThermStatus, " : "",
2211 (msr & 1 << 20) ? "Graphics, " : "",
2212 (msr & 1 << 22) ? "VR-Therm, " : "",
2213 (msr & 1 << 24) ? "Amps, " : "",
2214 (msr & 1 << 25) ? "GFXPwr, " : "",
2215 (msr & 1 << 26) ? "PkgPwrL1, " : "",
2216 (msr & 1 << 27) ? "PkgPwrL2, " : "");
2217 }
2218 if (do_ring_perf_limit_reasons) {
2219 get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
2220 fprintf(stderr, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2221 fprintf(stderr, " (Active: %s%s%s%s%s%s)",
2222 (msr & 1 << 0) ? "PROCHOT, " : "",
2223 (msr & 1 << 1) ? "ThermStatus, " : "",
2224 (msr & 1 << 6) ? "VR-Therm, " : "",
2225 (msr & 1 << 8) ? "Amps, " : "",
2226 (msr & 1 << 10) ? "PkgPwrL1, " : "",
2227 (msr & 1 << 11) ? "PkgPwrL2, " : "");
2228 fprintf(stderr, " (Logged: %s%s%s%s%s%s)\n",
2229 (msr & 1 << 16) ? "PROCHOT, " : "",
2230 (msr & 1 << 17) ? "ThermStatus, " : "",
2231 (msr & 1 << 22) ? "VR-Therm, " : "",
2232 (msr & 1 << 24) ? "Amps, " : "",
2233 (msr & 1 << 26) ? "PkgPwrL1, " : "",
2234 (msr & 1 << 27) ? "PkgPwrL2, " : "");
2235 }
2236 return 0;
2237}
2238
Len Brown889facb2012-11-08 00:48:57 -05002239#define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
2240#define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
2241
Len Brown144b44b2013-11-09 00:30:16 -05002242double get_tdp(model)
2243{
2244 unsigned long long msr;
2245
2246 if (do_rapl & RAPL_PKG_POWER_INFO)
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002247 if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
Len Brown144b44b2013-11-09 00:30:16 -05002248 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
2249
2250 switch (model) {
2251 case 0x37:
2252 case 0x4D:
2253 return 30.0;
2254 default:
2255 return 135.0;
2256 }
2257}
2258
Andrey Semin40ee8e32014-12-05 00:07:00 -05002259/*
2260 * rapl_dram_energy_units_probe()
2261 * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
2262 */
2263static double
2264rapl_dram_energy_units_probe(int model, double rapl_energy_units)
2265{
2266 /* only called for genuine_intel, family 6 */
2267
2268 switch (model) {
2269 case 0x3F: /* HSX */
2270 case 0x4F: /* BDX */
2271 case 0x56: /* BDX-DE */
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002272 case 0x57: /* KNL */
Andrey Semin40ee8e32014-12-05 00:07:00 -05002273 return (rapl_dram_energy_units = 15.3 / 1000000);
2274 default:
2275 return (rapl_energy_units);
2276 }
2277}
2278
Len Brown144b44b2013-11-09 00:30:16 -05002279
Len Brown889facb2012-11-08 00:48:57 -05002280/*
2281 * rapl_probe()
2282 *
Len Brown144b44b2013-11-09 00:30:16 -05002283 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
Len Brown889facb2012-11-08 00:48:57 -05002284 */
2285void rapl_probe(unsigned int family, unsigned int model)
2286{
2287 unsigned long long msr;
Len Brown144b44b2013-11-09 00:30:16 -05002288 unsigned int time_unit;
Len Brown889facb2012-11-08 00:48:57 -05002289 double tdp;
2290
2291 if (!genuine_intel)
2292 return;
2293
2294 if (family != 6)
2295 return;
2296
2297 switch (model) {
2298 case 0x2A:
2299 case 0x3A:
Len Brown70b43402013-01-08 01:26:07 -05002300 case 0x3C: /* HSW */
Len Brown70b43402013-01-08 01:26:07 -05002301 case 0x45: /* HSW */
Len Brown149c2312013-03-15 10:58:02 -04002302 case 0x46: /* HSW */
Len Brown4e8e863f2014-02-27 23:28:53 -05002303 case 0x3D: /* BDW */
Len Brown48a06312015-02-10 15:38:04 -05002304 case 0x47: /* BDW */
Len Brown144b44b2013-11-09 00:30:16 -05002305 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
Len Brown889facb2012-11-08 00:48:57 -05002306 break;
Len Brown0b2bb692015-03-26 00:50:30 -04002307 case 0x4E: /* SKL */
2308 case 0x5E: /* SKL */
2309 do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
2310 break;
Len Browne6f9bb32013-12-03 02:19:19 -05002311 case 0x3F: /* HSX */
Len Brown4e8e863f2014-02-27 23:28:53 -05002312 case 0x4F: /* BDX */
2313 case 0x56: /* BDX-DE */
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002314 case 0x57: /* KNL */
Len Brown0b2bb692015-03-26 00:50:30 -04002315 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 -05002316 break;
Len Brown889facb2012-11-08 00:48:57 -05002317 case 0x2D:
2318 case 0x3E:
Len Brown0b2bb692015-03-26 00:50:30 -04002319 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 -05002320 break;
2321 case 0x37: /* BYT */
2322 case 0x4D: /* AVN */
2323 do_rapl = RAPL_PKG | RAPL_CORES ;
Len Brown889facb2012-11-08 00:48:57 -05002324 break;
2325 default:
2326 return;
2327 }
2328
2329 /* units on package 0, verify later other packages match */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002330 if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
Len Brown889facb2012-11-08 00:48:57 -05002331 return;
2332
2333 rapl_power_units = 1.0 / (1 << (msr & 0xF));
Len Brown144b44b2013-11-09 00:30:16 -05002334 if (model == 0x37)
2335 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
2336 else
2337 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
Len Brown889facb2012-11-08 00:48:57 -05002338
Andrey Semin40ee8e32014-12-05 00:07:00 -05002339 rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
2340
Len Brown144b44b2013-11-09 00:30:16 -05002341 time_unit = msr >> 16 & 0xF;
2342 if (time_unit == 0)
2343 time_unit = 0xA;
Len Brown889facb2012-11-08 00:48:57 -05002344
Len Brown144b44b2013-11-09 00:30:16 -05002345 rapl_time_units = 1.0 / (1 << (time_unit));
2346
2347 tdp = get_tdp(model);
Len Brown889facb2012-11-08 00:48:57 -05002348
2349 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
Len Brownd8af6f52015-02-10 01:56:38 -05002350 if (debug)
Len Brown144b44b2013-11-09 00:30:16 -05002351 fprintf(stderr, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
Len Brown889facb2012-11-08 00:48:57 -05002352
2353 return;
2354}
2355
Len Brown3a9a9412014-08-15 02:39:52 -04002356void perf_limit_reasons_probe(family, model)
2357{
2358 if (!genuine_intel)
2359 return;
2360
2361 if (family != 6)
2362 return;
2363
2364 switch (model) {
2365 case 0x3C: /* HSW */
2366 case 0x45: /* HSW */
2367 case 0x46: /* HSW */
2368 do_gfx_perf_limit_reasons = 1;
2369 case 0x3F: /* HSX */
2370 do_core_perf_limit_reasons = 1;
2371 do_ring_perf_limit_reasons = 1;
2372 default:
2373 return;
2374 }
2375}
2376
Len Brown889facb2012-11-08 00:48:57 -05002377int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2378{
2379 unsigned long long msr;
2380 unsigned int dts;
2381 int cpu;
2382
2383 if (!(do_dts || do_ptm))
2384 return 0;
2385
2386 cpu = t->cpu_id;
2387
2388 /* DTS is per-core, no need to print for each thread */
2389 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
2390 return 0;
2391
2392 if (cpu_migrate(cpu)) {
2393 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
2394 return -1;
2395 }
2396
2397 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
2398 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
2399 return 0;
2400
2401 dts = (msr >> 16) & 0x7F;
2402 fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
2403 cpu, msr, tcc_activation_temp - dts);
2404
2405#ifdef THERM_DEBUG
2406 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
2407 return 0;
2408
2409 dts = (msr >> 16) & 0x7F;
2410 dts2 = (msr >> 8) & 0x7F;
2411 fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
2412 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
2413#endif
2414 }
2415
2416
2417 if (do_dts) {
2418 unsigned int resolution;
2419
2420 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
2421 return 0;
2422
2423 dts = (msr >> 16) & 0x7F;
2424 resolution = (msr >> 27) & 0xF;
2425 fprintf(stderr, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
2426 cpu, msr, tcc_activation_temp - dts, resolution);
2427
2428#ifdef THERM_DEBUG
2429 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
2430 return 0;
2431
2432 dts = (msr >> 16) & 0x7F;
2433 dts2 = (msr >> 8) & 0x7F;
2434 fprintf(stderr, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
2435 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
2436#endif
2437 }
2438
2439 return 0;
2440}
2441
2442void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
2443{
2444 fprintf(stderr, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
2445 cpu, label,
2446 ((msr >> 15) & 1) ? "EN" : "DIS",
2447 ((msr >> 0) & 0x7FFF) * rapl_power_units,
2448 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
2449 (((msr >> 16) & 1) ? "EN" : "DIS"));
2450
2451 return;
2452}
2453
2454int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2455{
2456 unsigned long long msr;
2457 int cpu;
Len Brown889facb2012-11-08 00:48:57 -05002458
2459 if (!do_rapl)
2460 return 0;
2461
2462 /* RAPL counters are per package, so print only for 1st thread/package */
2463 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2464 return 0;
2465
2466 cpu = t->cpu_id;
2467 if (cpu_migrate(cpu)) {
2468 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
2469 return -1;
2470 }
2471
2472 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
2473 return -1;
2474
Len Brownd8af6f52015-02-10 01:56:38 -05002475 if (debug) {
Len Brown889facb2012-11-08 00:48:57 -05002476 fprintf(stderr, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx "
2477 "(%f Watts, %f Joules, %f sec.)\n", cpu, msr,
Len Brown144b44b2013-11-09 00:30:16 -05002478 rapl_power_units, rapl_energy_units, rapl_time_units);
Len Brown889facb2012-11-08 00:48:57 -05002479 }
Len Brown144b44b2013-11-09 00:30:16 -05002480 if (do_rapl & RAPL_PKG_POWER_INFO) {
2481
Len Brown889facb2012-11-08 00:48:57 -05002482 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
2483 return -5;
2484
2485
2486 fprintf(stderr, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
2487 cpu, msr,
2488 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2489 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2490 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2491 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
2492
Len Brown144b44b2013-11-09 00:30:16 -05002493 }
2494 if (do_rapl & RAPL_PKG) {
2495
Len Brown889facb2012-11-08 00:48:57 -05002496 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
2497 return -9;
2498
2499 fprintf(stderr, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
2500 cpu, msr, (msr >> 63) & 1 ? "": "UN");
2501
2502 print_power_limit_msr(cpu, msr, "PKG Limit #1");
2503 fprintf(stderr, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
2504 cpu,
2505 ((msr >> 47) & 1) ? "EN" : "DIS",
2506 ((msr >> 32) & 0x7FFF) * rapl_power_units,
2507 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
2508 ((msr >> 48) & 1) ? "EN" : "DIS");
2509 }
2510
Len Brown0b2bb692015-03-26 00:50:30 -04002511 if (do_rapl & RAPL_DRAM_POWER_INFO) {
Len Brown889facb2012-11-08 00:48:57 -05002512 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
2513 return -6;
2514
Len Brown889facb2012-11-08 00:48:57 -05002515 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
2516 cpu, msr,
2517 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2518 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2519 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2520 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
Len Brown0b2bb692015-03-26 00:50:30 -04002521 }
2522 if (do_rapl & RAPL_DRAM) {
Len Brown889facb2012-11-08 00:48:57 -05002523 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
2524 return -9;
2525 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
2526 cpu, msr, (msr >> 31) & 1 ? "": "UN");
2527
2528 print_power_limit_msr(cpu, msr, "DRAM Limit");
2529 }
Len Brown144b44b2013-11-09 00:30:16 -05002530 if (do_rapl & RAPL_CORE_POLICY) {
Len Brownd8af6f52015-02-10 01:56:38 -05002531 if (debug) {
Len Brown889facb2012-11-08 00:48:57 -05002532 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
2533 return -7;
2534
2535 fprintf(stderr, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown144b44b2013-11-09 00:30:16 -05002536 }
2537 }
2538 if (do_rapl & RAPL_CORES) {
Len Brownd8af6f52015-02-10 01:56:38 -05002539 if (debug) {
Len Brown889facb2012-11-08 00:48:57 -05002540
2541 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
2542 return -9;
2543 fprintf(stderr, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
2544 cpu, msr, (msr >> 31) & 1 ? "": "UN");
2545 print_power_limit_msr(cpu, msr, "Cores Limit");
2546 }
2547 }
2548 if (do_rapl & RAPL_GFX) {
Len Brownd8af6f52015-02-10 01:56:38 -05002549 if (debug) {
Len Brown889facb2012-11-08 00:48:57 -05002550 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
2551 return -8;
2552
2553 fprintf(stderr, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
2554
2555 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
2556 return -9;
2557 fprintf(stderr, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
2558 cpu, msr, (msr >> 31) & 1 ? "": "UN");
2559 print_power_limit_msr(cpu, msr, "GFX Limit");
2560 }
2561 }
2562 return 0;
2563}
2564
Len Brownd7899442015-01-23 00:12:33 -05002565/*
2566 * SNB adds support for additional MSRs:
2567 *
2568 * MSR_PKG_C7_RESIDENCY 0x000003fa
2569 * MSR_CORE_C7_RESIDENCY 0x000003fe
2570 * MSR_PKG_C2_RESIDENCY 0x0000060d
2571 */
Len Brown103a8fe2010-10-22 23:53:03 -04002572
Len Brownd7899442015-01-23 00:12:33 -05002573int has_snb_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04002574{
2575 if (!genuine_intel)
2576 return 0;
2577
2578 switch (model) {
2579 case 0x2A:
2580 case 0x2D:
Len Brown650a37f2012-06-03 23:34:44 -04002581 case 0x3A: /* IVB */
Len Brown13006512012-09-26 18:11:31 -04002582 case 0x3E: /* IVB Xeon */
Len Brown70b43402013-01-08 01:26:07 -05002583 case 0x3C: /* HSW */
2584 case 0x3F: /* HSW */
2585 case 0x45: /* HSW */
Len Brown149c2312013-03-15 10:58:02 -04002586 case 0x46: /* HSW */
Len Brown4e8e863f2014-02-27 23:28:53 -05002587 case 0x3D: /* BDW */
Len Brown48a06312015-02-10 15:38:04 -05002588 case 0x47: /* BDW */
Len Brown4e8e863f2014-02-27 23:28:53 -05002589 case 0x4F: /* BDX */
2590 case 0x56: /* BDX-DE */
Len Brown0b2bb692015-03-26 00:50:30 -04002591 case 0x4E: /* SKL */
2592 case 0x5E: /* SKL */
Len Brown103a8fe2010-10-22 23:53:03 -04002593 return 1;
2594 }
2595 return 0;
2596}
2597
Len Brownd7899442015-01-23 00:12:33 -05002598/*
2599 * HSW adds support for additional MSRs:
2600 *
2601 * MSR_PKG_C8_RESIDENCY 0x00000630
2602 * MSR_PKG_C9_RESIDENCY 0x00000631
2603 * MSR_PKG_C10_RESIDENCY 0x00000632
2604 */
2605int has_hsw_msrs(unsigned int family, unsigned int model)
Kristen Carlson Accardica587102012-11-21 05:22:43 -08002606{
2607 if (!genuine_intel)
2608 return 0;
2609
2610 switch (model) {
Len Brown4e8e863f2014-02-27 23:28:53 -05002611 case 0x45: /* HSW */
2612 case 0x3D: /* BDW */
Len Brown0b2bb692015-03-26 00:50:30 -04002613 case 0x4E: /* SKL */
2614 case 0x5E: /* SKL */
Kristen Carlson Accardica587102012-11-21 05:22:43 -08002615 return 1;
2616 }
2617 return 0;
2618}
2619
Len Brown0b2bb692015-03-26 00:50:30 -04002620/*
2621 * SKL adds support for additional MSRS:
2622 *
2623 * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658
2624 * MSR_PKG_ANY_CORE_C0_RES 0x00000659
2625 * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A
2626 * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B
2627 */
2628int has_skl_msrs(unsigned int family, unsigned int model)
2629{
2630 if (!genuine_intel)
2631 return 0;
2632
2633 switch (model) {
2634 case 0x4E: /* SKL */
2635 case 0x5E: /* SKL */
2636 return 1;
2637 }
2638 return 0;
2639}
2640
2641
Kristen Carlson Accardica587102012-11-21 05:22:43 -08002642
Len Brown144b44b2013-11-09 00:30:16 -05002643int is_slm(unsigned int family, unsigned int model)
2644{
2645 if (!genuine_intel)
2646 return 0;
2647 switch (model) {
2648 case 0x37: /* BYT */
2649 case 0x4D: /* AVN */
2650 return 1;
2651 }
2652 return 0;
2653}
2654
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002655int is_knl(unsigned int family, unsigned int model)
2656{
2657 if (!genuine_intel)
2658 return 0;
2659 switch (model) {
2660 case 0x57: /* KNL */
2661 return 1;
2662 }
2663 return 0;
2664}
2665
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02002666unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
2667{
2668 if (is_knl(family, model))
2669 return 1024;
2670 return 1;
2671}
2672
Len Brown144b44b2013-11-09 00:30:16 -05002673#define SLM_BCLK_FREQS 5
2674double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
2675
2676double slm_bclk(void)
2677{
2678 unsigned long long msr = 3;
2679 unsigned int i;
2680 double freq;
2681
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002682 if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
Len Brown144b44b2013-11-09 00:30:16 -05002683 fprintf(stderr, "SLM BCLK: unknown\n");
2684
2685 i = msr & 0xf;
2686 if (i >= SLM_BCLK_FREQS) {
2687 fprintf(stderr, "SLM BCLK[%d] invalid\n", i);
2688 msr = 3;
2689 }
2690 freq = slm_freq_table[i];
2691
2692 fprintf(stderr, "SLM BCLK: %.1f Mhz\n", freq);
2693
2694 return freq;
2695}
2696
Len Brown103a8fe2010-10-22 23:53:03 -04002697double discover_bclk(unsigned int family, unsigned int model)
2698{
Len Brownd7899442015-01-23 00:12:33 -05002699 if (has_snb_msrs(family, model))
Len Brown103a8fe2010-10-22 23:53:03 -04002700 return 100.00;
Len Brown144b44b2013-11-09 00:30:16 -05002701 else if (is_slm(family, model))
2702 return slm_bclk();
Len Brown103a8fe2010-10-22 23:53:03 -04002703 else
2704 return 133.33;
2705}
2706
Len Brown889facb2012-11-08 00:48:57 -05002707/*
2708 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
2709 * the Thermal Control Circuit (TCC) activates.
2710 * This is usually equal to tjMax.
2711 *
2712 * Older processors do not have this MSR, so there we guess,
2713 * but also allow cmdline over-ride with -T.
2714 *
2715 * Several MSR temperature values are in units of degrees-C
2716 * below this value, including the Digital Thermal Sensor (DTS),
2717 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
2718 */
2719int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2720{
2721 unsigned long long msr;
2722 unsigned int target_c_local;
2723 int cpu;
2724
2725 /* tcc_activation_temp is used only for dts or ptm */
2726 if (!(do_dts || do_ptm))
2727 return 0;
2728
2729 /* this is a per-package concept */
2730 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2731 return 0;
2732
2733 cpu = t->cpu_id;
2734 if (cpu_migrate(cpu)) {
2735 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
2736 return -1;
2737 }
2738
2739 if (tcc_activation_temp_override != 0) {
2740 tcc_activation_temp = tcc_activation_temp_override;
2741 fprintf(stderr, "cpu%d: Using cmdline TCC Target (%d C)\n",
2742 cpu, tcc_activation_temp);
2743 return 0;
2744 }
2745
2746 /* Temperature Target MSR is Nehalem and newer only */
Len Brownd7899442015-01-23 00:12:33 -05002747 if (!do_nhm_platform_info)
Len Brown889facb2012-11-08 00:48:57 -05002748 goto guess;
2749
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002750 if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
Len Brown889facb2012-11-08 00:48:57 -05002751 goto guess;
2752
Jean Delvare34821242014-05-01 11:40:19 +02002753 target_c_local = (msr >> 16) & 0xFF;
Len Brown889facb2012-11-08 00:48:57 -05002754
Len Brownd8af6f52015-02-10 01:56:38 -05002755 if (debug)
Len Brown889facb2012-11-08 00:48:57 -05002756 fprintf(stderr, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
2757 cpu, msr, target_c_local);
2758
Jean Delvare34821242014-05-01 11:40:19 +02002759 if (!target_c_local)
Len Brown889facb2012-11-08 00:48:57 -05002760 goto guess;
2761
2762 tcc_activation_temp = target_c_local;
2763
2764 return 0;
2765
2766guess:
2767 tcc_activation_temp = TJMAX_DEFAULT;
2768 fprintf(stderr, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
2769 cpu, tcc_activation_temp);
2770
2771 return 0;
2772}
Len Brown69807a62015-11-21 12:22:47 -05002773
2774void decode_misc_enable_msr(void)
2775{
2776 unsigned long long msr;
2777
2778 if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
2779 fprintf(stderr, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%s %s %s)\n",
2780 base_cpu, msr,
2781 msr & (1 << 3) ? "TCC" : "",
2782 msr & (1 << 16) ? "EIST" : "",
2783 msr & (1 << 18) ? "MONITOR" : "");
2784}
2785
Len Brown7f5c2582015-12-01 01:36:39 -05002786
Len Brownfcd17212015-03-23 20:29:09 -04002787void process_cpuid()
Len Brown103a8fe2010-10-22 23:53:03 -04002788{
Len Brown61a87ba2015-11-23 02:30:51 -05002789 unsigned int eax, ebx, ecx, edx, max_level, max_extended_level;
Len Brown103a8fe2010-10-22 23:53:03 -04002790 unsigned int fms, family, model, stepping;
2791
2792 eax = ebx = ecx = edx = 0;
2793
Josh Triplett2b928652013-08-20 17:20:14 -07002794 __get_cpuid(0, &max_level, &ebx, &ecx, &edx);
Len Brown103a8fe2010-10-22 23:53:03 -04002795
2796 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
2797 genuine_intel = 1;
2798
Len Brownd8af6f52015-02-10 01:56:38 -05002799 if (debug)
Len Brown889facb2012-11-08 00:48:57 -05002800 fprintf(stderr, "CPUID(0): %.4s%.4s%.4s ",
Len Brown103a8fe2010-10-22 23:53:03 -04002801 (char *)&ebx, (char *)&edx, (char *)&ecx);
2802
Josh Triplett2b928652013-08-20 17:20:14 -07002803 __get_cpuid(1, &fms, &ebx, &ecx, &edx);
Len Brown103a8fe2010-10-22 23:53:03 -04002804 family = (fms >> 8) & 0xf;
2805 model = (fms >> 4) & 0xf;
2806 stepping = fms & 0xf;
2807 if (family == 6 || family == 0xf)
2808 model += ((fms >> 16) & 0xf) << 4;
2809
Len Brown69807a62015-11-21 12:22:47 -05002810 if (debug) {
Len Brown103a8fe2010-10-22 23:53:03 -04002811 fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
2812 max_level, family, model, stepping, family, model, stepping);
Len Brown69807a62015-11-21 12:22:47 -05002813 fprintf(stderr, "CPUID(1): %s %s %s %s %s %s %s %s\n",
2814 ecx & (1 << 0) ? "SSE3" : "-",
2815 ecx & (1 << 3) ? "MONITOR" : "-",
2816 ecx & (1 << 7) ? "EIST" : "-",
2817 ecx & (1 << 8) ? "TM2" : "-",
2818 edx & (1 << 4) ? "TSC" : "-",
2819 edx & (1 << 5) ? "MSR" : "-",
2820 edx & (1 << 22) ? "ACPI-TM" : "-",
2821 edx & (1 << 29) ? "TM" : "-");
2822 }
Len Brown103a8fe2010-10-22 23:53:03 -04002823
Josh Triplettb2c95d92013-08-20 17:20:18 -07002824 if (!(edx & (1 << 5)))
2825 errx(1, "CPUID: no MSR");
Len Brown103a8fe2010-10-22 23:53:03 -04002826
2827 /*
2828 * check max extended function levels of CPUID.
2829 * This is needed to check for invariant TSC.
2830 * This check is valid for both Intel and AMD.
2831 */
2832 ebx = ecx = edx = 0;
Len Brown61a87ba2015-11-23 02:30:51 -05002833 __get_cpuid(0x80000000, &max_extended_level, &ebx, &ecx, &edx);
Len Brown103a8fe2010-10-22 23:53:03 -04002834
Len Brown61a87ba2015-11-23 02:30:51 -05002835 if (max_extended_level >= 0x80000007) {
Len Brown103a8fe2010-10-22 23:53:03 -04002836
Len Brownd7899442015-01-23 00:12:33 -05002837 /*
2838 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
2839 * this check is valid for both Intel and AMD
2840 */
2841 __get_cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
2842 has_invariant_tsc = edx & (1 << 8);
2843 }
Len Brown103a8fe2010-10-22 23:53:03 -04002844
2845 /*
2846 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
2847 * this check is valid for both Intel and AMD
2848 */
2849
Josh Triplett2b928652013-08-20 17:20:14 -07002850 __get_cpuid(0x6, &eax, &ebx, &ecx, &edx);
Thomas Renninger8209e052011-01-21 15:11:19 +01002851 has_aperf = ecx & (1 << 0);
Len Brown889facb2012-11-08 00:48:57 -05002852 do_dts = eax & (1 << 0);
2853 do_ptm = eax & (1 << 6);
Len Brown7f5c2582015-12-01 01:36:39 -05002854 has_hwp = eax & (1 << 7);
2855 has_hwp_notify = eax & (1 << 8);
2856 has_hwp_activity_window = eax & (1 << 9);
2857 has_hwp_epp = eax & (1 << 10);
2858 has_hwp_pkg = eax & (1 << 11);
Len Brown889facb2012-11-08 00:48:57 -05002859 has_epb = ecx & (1 << 3);
2860
Len Brownd8af6f52015-02-10 01:56:38 -05002861 if (debug)
Len Brown7f5c2582015-12-01 01:36:39 -05002862 fprintf(stderr, "CPUID(6): %sAPERF, %sDTS, %sPTM, %sHWP, "
2863 "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
2864 has_aperf ? "" : "No-",
2865 do_dts ? "" : "No-",
2866 do_ptm ? "" : "No-",
2867 has_hwp ? "" : "No-",
2868 has_hwp_notify ? "" : "No-",
2869 has_hwp_activity_window ? "" : "No-",
2870 has_hwp_epp ? "" : "No-",
2871 has_hwp_pkg ? "" : "No-",
2872 has_epb ? "" : "No-");
Len Brown103a8fe2010-10-22 23:53:03 -04002873
Len Brown69807a62015-11-21 12:22:47 -05002874 if (debug)
2875 decode_misc_enable_msr();
2876
Len Brown61a87ba2015-11-23 02:30:51 -05002877 if (max_level >= 0x15) {
Len Brown8a5bdf42015-04-01 21:02:57 -04002878 unsigned int eax_crystal;
2879 unsigned int ebx_tsc;
2880
2881 /*
2882 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
2883 */
2884 eax_crystal = ebx_tsc = crystal_hz = edx = 0;
2885 __get_cpuid(0x15, &eax_crystal, &ebx_tsc, &crystal_hz, &edx);
2886
2887 if (ebx_tsc != 0) {
2888
2889 if (debug && (ebx != 0))
2890 fprintf(stderr, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
2891 eax_crystal, ebx_tsc, crystal_hz);
2892
2893 if (crystal_hz == 0)
2894 switch(model) {
2895 case 0x4E: /* SKL */
2896 case 0x5E: /* SKL */
2897 crystal_hz = 24000000; /* 24 MHz */
2898 break;
2899 default:
2900 crystal_hz = 0;
2901 }
2902
2903 if (crystal_hz) {
2904 tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
2905 if (debug)
2906 fprintf(stderr, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
2907 tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal);
2908 }
2909 }
2910 }
Len Brown61a87ba2015-11-23 02:30:51 -05002911 if (max_level >= 0x16) {
2912 unsigned int base_mhz, max_mhz, bus_mhz, edx;
2913
2914 /*
2915 * CPUID 16H Base MHz, Max MHz, Bus MHz
2916 */
2917 base_mhz = max_mhz = bus_mhz = edx = 0;
2918
2919 __get_cpuid(0x16, &base_mhz, &max_mhz, &bus_mhz, &edx);
2920 if (debug)
2921 fprintf(stderr, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
2922 base_mhz, max_mhz, bus_mhz);
2923 }
Len Brown8a5bdf42015-04-01 21:02:57 -04002924
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02002925 if (has_aperf)
2926 aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
2927
Len Brownee7e38e2015-02-09 23:39:45 -05002928 do_nhm_platform_info = do_nhm_cstates = do_smi = probe_nhm_msrs(family, model);
Len Brownd7899442015-01-23 00:12:33 -05002929 do_snb_cstates = has_snb_msrs(family, model);
Len Brownee7e38e2015-02-09 23:39:45 -05002930 do_pc2 = do_snb_cstates && (pkg_cstate_limit >= PCL__2);
2931 do_pc3 = (pkg_cstate_limit >= PCL__3);
2932 do_pc6 = (pkg_cstate_limit >= PCL__6);
2933 do_pc7 = do_snb_cstates && (pkg_cstate_limit >= PCL__7);
Len Brownd7899442015-01-23 00:12:33 -05002934 do_c8_c9_c10 = has_hsw_msrs(family, model);
Len Brown0b2bb692015-03-26 00:50:30 -04002935 do_skl_residency = has_skl_msrs(family, model);
Len Brown144b44b2013-11-09 00:30:16 -05002936 do_slm_cstates = is_slm(family, model);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002937 do_knl_cstates = is_knl(family, model);
Len Brown103a8fe2010-10-22 23:53:03 -04002938
Len Brown889facb2012-11-08 00:48:57 -05002939 rapl_probe(family, model);
Len Brown3a9a9412014-08-15 02:39:52 -04002940 perf_limit_reasons_probe(family, model);
Len Brown889facb2012-11-08 00:48:57 -05002941
Len Brownfcd17212015-03-23 20:29:09 -04002942 if (debug)
2943 dump_cstate_pstate_config_info();
2944
Len Browna2b7b742015-09-26 00:12:38 -04002945 if (has_skl_msrs(family, model))
2946 calculate_tsc_tweak();
2947
Len Brown889facb2012-11-08 00:48:57 -05002948 return;
Len Brown103a8fe2010-10-22 23:53:03 -04002949}
2950
Len Brownd8af6f52015-02-10 01:56:38 -05002951void help()
Len Brown103a8fe2010-10-22 23:53:03 -04002952{
Len Brownd8af6f52015-02-10 01:56:38 -05002953 fprintf(stderr,
2954 "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
2955 "\n"
2956 "Turbostat forks the specified COMMAND and prints statistics\n"
2957 "when COMMAND completes.\n"
2958 "If no COMMAND is specified, turbostat wakes every 5-seconds\n"
2959 "to print statistics, until interrupted.\n"
2960 "--debug run in \"debug\" mode\n"
2961 "--interval sec Override default 5-second measurement interval\n"
2962 "--help print this help message\n"
2963 "--counter msr print 32-bit counter at address \"msr\"\n"
2964 "--Counter msr print 64-bit Counter at address \"msr\"\n"
2965 "--msr msr print 32-bit value at address \"msr\"\n"
2966 "--MSR msr print 64-bit Value at address \"msr\"\n"
2967 "--version print version information\n"
2968 "\n"
2969 "For more help, run \"man turbostat\"\n");
Len Brown103a8fe2010-10-22 23:53:03 -04002970}
2971
2972
2973/*
2974 * in /dev/cpu/ return success for names that are numbers
2975 * ie. filter out ".", "..", "microcode".
2976 */
2977int dir_filter(const struct dirent *dirp)
2978{
2979 if (isdigit(dirp->d_name[0]))
2980 return 1;
2981 else
2982 return 0;
2983}
2984
2985int open_dev_cpu_msr(int dummy1)
2986{
2987 return 0;
2988}
2989
Len Brownc98d5d92012-06-04 00:56:40 -04002990void topology_probe()
2991{
2992 int i;
2993 int max_core_id = 0;
2994 int max_package_id = 0;
2995 int max_siblings = 0;
2996 struct cpu_topology {
2997 int core_id;
2998 int physical_package_id;
2999 } *cpus;
3000
3001 /* Initialize num_cpus, max_cpu_num */
3002 topo.num_cpus = 0;
3003 topo.max_cpu_num = 0;
3004 for_all_proc_cpus(count_cpus);
3005 if (!summary_only && topo.num_cpus > 1)
3006 show_cpu = 1;
3007
Len Brownd8af6f52015-02-10 01:56:38 -05003008 if (debug > 1)
Len Brownc98d5d92012-06-04 00:56:40 -04003009 fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
3010
3011 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
Josh Triplettb2c95d92013-08-20 17:20:18 -07003012 if (cpus == NULL)
3013 err(1, "calloc cpus");
Len Brownc98d5d92012-06-04 00:56:40 -04003014
3015 /*
3016 * Allocate and initialize cpu_present_set
3017 */
3018 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07003019 if (cpu_present_set == NULL)
3020 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04003021 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
3022 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
3023 for_all_proc_cpus(mark_cpu_present);
3024
3025 /*
3026 * Allocate and initialize cpu_affinity_set
3027 */
3028 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07003029 if (cpu_affinity_set == NULL)
3030 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04003031 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
3032 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
3033
3034
3035 /*
3036 * For online cpus
3037 * find max_core_id, max_package_id
3038 */
3039 for (i = 0; i <= topo.max_cpu_num; ++i) {
3040 int siblings;
3041
3042 if (cpu_is_not_present(i)) {
Len Brownd8af6f52015-02-10 01:56:38 -05003043 if (debug > 1)
Len Brownc98d5d92012-06-04 00:56:40 -04003044 fprintf(stderr, "cpu%d NOT PRESENT\n", i);
3045 continue;
3046 }
3047 cpus[i].core_id = get_core_id(i);
3048 if (cpus[i].core_id > max_core_id)
3049 max_core_id = cpus[i].core_id;
3050
3051 cpus[i].physical_package_id = get_physical_package_id(i);
3052 if (cpus[i].physical_package_id > max_package_id)
3053 max_package_id = cpus[i].physical_package_id;
3054
3055 siblings = get_num_ht_siblings(i);
3056 if (siblings > max_siblings)
3057 max_siblings = siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05003058 if (debug > 1)
Len Brownc98d5d92012-06-04 00:56:40 -04003059 fprintf(stderr, "cpu %d pkg %d core %d\n",
3060 i, cpus[i].physical_package_id, cpus[i].core_id);
3061 }
3062 topo.num_cores_per_pkg = max_core_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05003063 if (debug > 1)
Len Brownc98d5d92012-06-04 00:56:40 -04003064 fprintf(stderr, "max_core_id %d, sizing for %d cores per package\n",
3065 max_core_id, topo.num_cores_per_pkg);
Len Brown1cc21f72015-02-23 00:34:57 -05003066 if (debug && !summary_only && topo.num_cores_per_pkg > 1)
Len Brownc98d5d92012-06-04 00:56:40 -04003067 show_core = 1;
3068
3069 topo.num_packages = max_package_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05003070 if (debug > 1)
Len Brownc98d5d92012-06-04 00:56:40 -04003071 fprintf(stderr, "max_package_id %d, sizing for %d packages\n",
3072 max_package_id, topo.num_packages);
Len Brown1cc21f72015-02-23 00:34:57 -05003073 if (debug && !summary_only && topo.num_packages > 1)
Len Brownc98d5d92012-06-04 00:56:40 -04003074 show_pkg = 1;
3075
3076 topo.num_threads_per_core = max_siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05003077 if (debug > 1)
Len Brownc98d5d92012-06-04 00:56:40 -04003078 fprintf(stderr, "max_siblings %d\n", max_siblings);
3079
3080 free(cpus);
3081}
3082
3083void
3084allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
3085{
3086 int i;
3087
3088 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
3089 topo.num_packages, sizeof(struct thread_data));
3090 if (*t == NULL)
3091 goto error;
3092
3093 for (i = 0; i < topo.num_threads_per_core *
3094 topo.num_cores_per_pkg * topo.num_packages; i++)
3095 (*t)[i].cpu_id = -1;
3096
3097 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
3098 sizeof(struct core_data));
3099 if (*c == NULL)
3100 goto error;
3101
3102 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
3103 (*c)[i].core_id = -1;
3104
3105 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
3106 if (*p == NULL)
3107 goto error;
3108
3109 for (i = 0; i < topo.num_packages; i++)
3110 (*p)[i].package_id = i;
3111
3112 return;
3113error:
Josh Triplettb2c95d92013-08-20 17:20:18 -07003114 err(1, "calloc counters");
Len Brownc98d5d92012-06-04 00:56:40 -04003115}
3116/*
3117 * init_counter()
3118 *
3119 * set cpu_id, core_num, pkg_num
3120 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
3121 *
3122 * increment topo.num_cores when 1st core in pkg seen
3123 */
3124void init_counter(struct thread_data *thread_base, struct core_data *core_base,
3125 struct pkg_data *pkg_base, int thread_num, int core_num,
3126 int pkg_num, int cpu_id)
3127{
3128 struct thread_data *t;
3129 struct core_data *c;
3130 struct pkg_data *p;
3131
3132 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
3133 c = GET_CORE(core_base, core_num, pkg_num);
3134 p = GET_PKG(pkg_base, pkg_num);
3135
3136 t->cpu_id = cpu_id;
3137 if (thread_num == 0) {
3138 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
3139 if (cpu_is_first_core_in_package(cpu_id))
3140 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
3141 }
3142
3143 c->core_id = core_num;
3144 p->package_id = pkg_num;
3145}
3146
3147
3148int initialize_counters(int cpu_id)
3149{
3150 int my_thread_id, my_core_id, my_package_id;
3151
3152 my_package_id = get_physical_package_id(cpu_id);
3153 my_core_id = get_core_id(cpu_id);
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07003154 my_thread_id = get_cpu_position_in_core(cpu_id);
3155 if (!my_thread_id)
Len Brownc98d5d92012-06-04 00:56:40 -04003156 topo.num_cores++;
Len Brownc98d5d92012-06-04 00:56:40 -04003157
3158 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
3159 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
3160 return 0;
3161}
3162
3163void allocate_output_buffer()
3164{
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02003165 output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
Len Brownc98d5d92012-06-04 00:56:40 -04003166 outp = output_buffer;
Josh Triplettb2c95d92013-08-20 17:20:18 -07003167 if (outp == NULL)
3168 err(-1, "calloc output buffer");
Len Brownc98d5d92012-06-04 00:56:40 -04003169}
3170
3171void setup_all_buffers(void)
3172{
3173 topology_probe();
3174 allocate_counters(&thread_even, &core_even, &package_even);
3175 allocate_counters(&thread_odd, &core_odd, &package_odd);
3176 allocate_output_buffer();
3177 for_all_proc_cpus(initialize_counters);
3178}
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02003179
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003180void set_base_cpu(void)
3181{
3182 base_cpu = sched_getcpu();
3183 if (base_cpu < 0)
3184 err(-ENODEV, "No valid cpus found");
3185
3186 if (debug > 1)
3187 fprintf(stderr, "base_cpu = %d\n", base_cpu);
3188}
3189
Len Brown103a8fe2010-10-22 23:53:03 -04003190void turbostat_init()
3191{
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003192 setup_all_buffers();
3193 set_base_cpu();
Len Brown103a8fe2010-10-22 23:53:03 -04003194 check_dev_msr();
Len Brown98481e72014-08-15 00:36:50 -04003195 check_permissions();
Len Brownfcd17212015-03-23 20:29:09 -04003196 process_cpuid();
Len Brown103a8fe2010-10-22 23:53:03 -04003197
Len Brown103a8fe2010-10-22 23:53:03 -04003198
Len Brownd8af6f52015-02-10 01:56:38 -05003199 if (debug)
Len Brown7f5c2582015-12-01 01:36:39 -05003200 for_all_cpus(print_hwp, ODD_COUNTERS);
3201
3202 if (debug)
Len Brown889facb2012-11-08 00:48:57 -05003203 for_all_cpus(print_epb, ODD_COUNTERS);
3204
Len Brownd8af6f52015-02-10 01:56:38 -05003205 if (debug)
Len Brown3a9a9412014-08-15 02:39:52 -04003206 for_all_cpus(print_perf_limit, ODD_COUNTERS);
3207
Len Brownd8af6f52015-02-10 01:56:38 -05003208 if (debug)
Len Brown889facb2012-11-08 00:48:57 -05003209 for_all_cpus(print_rapl, ODD_COUNTERS);
3210
3211 for_all_cpus(set_temperature_target, ODD_COUNTERS);
3212
Len Brownd8af6f52015-02-10 01:56:38 -05003213 if (debug)
Len Brown889facb2012-11-08 00:48:57 -05003214 for_all_cpus(print_thermal, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04003215}
3216
3217int fork_it(char **argv)
3218{
Len Brown103a8fe2010-10-22 23:53:03 -04003219 pid_t child_pid;
Len Brownd91bb172012-11-01 00:08:19 -04003220 int status;
Len Brownd15cf7c2012-06-03 23:24:00 -04003221
Len Brownd91bb172012-11-01 00:08:19 -04003222 status = for_all_cpus(get_counters, EVEN_COUNTERS);
3223 if (status)
3224 exit(status);
Len Brownc98d5d92012-06-04 00:56:40 -04003225 /* clear affinity side-effect of get_counters() */
3226 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
Len Brown103a8fe2010-10-22 23:53:03 -04003227 gettimeofday(&tv_even, (struct timezone *)NULL);
3228
3229 child_pid = fork();
3230 if (!child_pid) {
3231 /* child */
3232 execvp(argv[0], argv);
3233 } else {
Len Brown103a8fe2010-10-22 23:53:03 -04003234
3235 /* parent */
Josh Triplettb2c95d92013-08-20 17:20:18 -07003236 if (child_pid == -1)
3237 err(1, "fork");
Len Brown103a8fe2010-10-22 23:53:03 -04003238
3239 signal(SIGINT, SIG_IGN);
3240 signal(SIGQUIT, SIG_IGN);
Josh Triplettb2c95d92013-08-20 17:20:18 -07003241 if (waitpid(child_pid, &status, 0) == -1)
3242 err(status, "waitpid");
Len Brown103a8fe2010-10-22 23:53:03 -04003243 }
Len Brownc98d5d92012-06-04 00:56:40 -04003244 /*
3245 * n.b. fork_it() does not check for errors from for_all_cpus()
3246 * because re-starting is problematic when forking
3247 */
3248 for_all_cpus(get_counters, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04003249 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04003250 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04003251 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
3252 compute_average(EVEN_COUNTERS);
3253 format_all_counters(EVEN_COUNTERS);
3254 flush_stderr();
Len Brown103a8fe2010-10-22 23:53:03 -04003255
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07003256 fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
Len Brown103a8fe2010-10-22 23:53:03 -04003257
Len Brownd91bb172012-11-01 00:08:19 -04003258 return status;
Len Brown103a8fe2010-10-22 23:53:03 -04003259}
3260
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02003261int get_and_dump_counters(void)
3262{
3263 int status;
3264
3265 status = for_all_cpus(get_counters, ODD_COUNTERS);
3266 if (status)
3267 return status;
3268
3269 status = for_all_cpus(dump_counters, ODD_COUNTERS);
3270 if (status)
3271 return status;
3272
3273 flush_stdout();
3274
3275 return status;
3276}
3277
Len Brownd8af6f52015-02-10 01:56:38 -05003278void print_version() {
Len Brown7f5c2582015-12-01 01:36:39 -05003279 fprintf(stderr, "turbostat version 4.10 10 Dec, 2015"
Len Brownd8af6f52015-02-10 01:56:38 -05003280 " - Len Brown <lenb@kernel.org>\n");
3281}
3282
Len Brown103a8fe2010-10-22 23:53:03 -04003283void cmdline(int argc, char **argv)
3284{
3285 int opt;
Len Brownd8af6f52015-02-10 01:56:38 -05003286 int option_index = 0;
3287 static struct option long_options[] = {
3288 {"Counter", required_argument, 0, 'C'},
3289 {"counter", required_argument, 0, 'c'},
3290 {"Dump", no_argument, 0, 'D'},
3291 {"debug", no_argument, 0, 'd'},
3292 {"interval", required_argument, 0, 'i'},
3293 {"help", no_argument, 0, 'h'},
3294 {"Joules", no_argument, 0, 'J'},
3295 {"MSR", required_argument, 0, 'M'},
3296 {"msr", required_argument, 0, 'm'},
3297 {"Package", no_argument, 0, 'p'},
3298 {"processor", no_argument, 0, 'p'},
3299 {"Summary", no_argument, 0, 'S'},
3300 {"TCC", required_argument, 0, 'T'},
3301 {"version", no_argument, 0, 'v' },
3302 {0, 0, 0, 0 }
3303 };
Len Brown103a8fe2010-10-22 23:53:03 -04003304
3305 progname = argv[0];
3306
Len Browna01e72f2015-07-15 21:49:41 -04003307 while ((opt = getopt_long_only(argc, argv, "+C:c:Ddhi:JM:m:PpST:v",
Len Brownd8af6f52015-02-10 01:56:38 -05003308 long_options, &option_index)) != -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04003309 switch (opt) {
Len Brownd8af6f52015-02-10 01:56:38 -05003310 case 'C':
3311 sscanf(optarg, "%x", &extra_delta_offset64);
Len Brown103a8fe2010-10-22 23:53:03 -04003312 break;
Len Brownf9240812012-10-06 15:26:31 -04003313 case 'c':
Len Brown8e180f32012-09-22 01:25:08 -04003314 sscanf(optarg, "%x", &extra_delta_offset32);
3315 break;
Len Brownd8af6f52015-02-10 01:56:38 -05003316 case 'D':
3317 dump_only++;
Len Brown8e180f32012-09-22 01:25:08 -04003318 break;
Len Brownd8af6f52015-02-10 01:56:38 -05003319 case 'd':
3320 debug++;
Len Brown2f32edf2012-09-21 23:45:46 -04003321 break;
Len Brownd8af6f52015-02-10 01:56:38 -05003322 case 'h':
3323 default:
3324 help();
3325 exit(1);
3326 case 'i':
3327 interval_sec = atoi(optarg);
Len Brown889facb2012-11-08 00:48:57 -05003328 break;
Dirk Brandewie5c56be92013-12-16 10:23:41 -08003329 case 'J':
3330 rapl_joules++;
3331 break;
Len Brownd8af6f52015-02-10 01:56:38 -05003332 case 'M':
3333 sscanf(optarg, "%x", &extra_msr_offset64);
3334 break;
3335 case 'm':
3336 sscanf(optarg, "%x", &extra_msr_offset32);
3337 break;
3338 case 'P':
3339 show_pkg_only++;
3340 break;
3341 case 'p':
3342 show_core_only++;
3343 break;
3344 case 'S':
3345 summary_only++;
3346 break;
3347 case 'T':
3348 tcc_activation_temp_override = atoi(optarg);
3349 break;
3350 case 'v':
3351 print_version();
3352 exit(0);
3353 break;
Len Brown103a8fe2010-10-22 23:53:03 -04003354 }
3355 }
3356}
3357
3358int main(int argc, char **argv)
3359{
3360 cmdline(argc, argv);
3361
Len Brownd8af6f52015-02-10 01:56:38 -05003362 if (debug)
3363 print_version();
Len Brown103a8fe2010-10-22 23:53:03 -04003364
3365 turbostat_init();
3366
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02003367 /* dump counters and exit */
3368 if (dump_only)
3369 return get_and_dump_counters();
3370
Len Brown103a8fe2010-10-22 23:53:03 -04003371 /*
3372 * if any params left, it must be a command to fork
3373 */
3374 if (argc - optind)
3375 return fork_it(argv + optind);
3376 else
3377 turbostat_loop();
3378
3379 return 0;
3380}