blob: 652d08df5d12d6ad6bfb931b3b541b0960934ec6 [file] [log] [blame]
Len Brown103a8fe2010-10-22 23:53:03 -04001/*
2 * turbostat -- show CPU frequency and C-state residency
3 * on modern Intel turbo-capable processors.
4 *
Len Brown144b44b2013-11-09 00:30:16 -05005 * Copyright (c) 2013 Intel Corporation.
Len Brown103a8fe2010-10-22 23:53:03 -04006 * Len Brown <len.brown@intel.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
Len Brown88c32812012-03-29 21:44:40 -040022#define _GNU_SOURCE
Josh Triplettb731f312013-08-20 17:20:12 -070023#include MSRHEADER
Josh Triplett95aebc42013-08-20 17:20:16 -070024#include <stdarg.h>
Len Brown103a8fe2010-10-22 23:53:03 -040025#include <stdio.h>
Josh Triplettb2c95d92013-08-20 17:20:18 -070026#include <err.h>
Len Brown103a8fe2010-10-22 23:53:03 -040027#include <unistd.h>
28#include <sys/types.h>
29#include <sys/wait.h>
30#include <sys/stat.h>
31#include <sys/resource.h>
32#include <fcntl.h>
33#include <signal.h>
34#include <sys/time.h>
35#include <stdlib.h>
Len Brownd8af6f52015-02-10 01:56:38 -050036#include <getopt.h>
Len Brown103a8fe2010-10-22 23:53:03 -040037#include <dirent.h>
38#include <string.h>
39#include <ctype.h>
Len Brown88c32812012-03-29 21:44:40 -040040#include <sched.h>
Len Brown2a0609c2016-02-12 22:44:48 -050041#include <time.h>
Josh Triplett2b928652013-08-20 17:20:14 -070042#include <cpuid.h>
Len Brown98481e72014-08-15 00:36:50 -040043#include <linux/capability.h>
44#include <errno.h>
Len Brown103a8fe2010-10-22 23:53:03 -040045
Len Brown103a8fe2010-10-22 23:53:03 -040046char *proc_stat = "/proc/stat";
Len Brown2a0609c2016-02-12 22:44:48 -050047struct timespec interval_ts = {5, 0};
Len Brownd8af6f52015-02-10 01:56:38 -050048unsigned int debug;
49unsigned int rapl_joules;
50unsigned int summary_only;
51unsigned int dump_only;
Len Brown103a8fe2010-10-22 23:53:03 -040052unsigned int skip_c0;
53unsigned int skip_c1;
54unsigned int do_nhm_cstates;
55unsigned int do_snb_cstates;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -070056unsigned int do_knl_cstates;
Len Brownee7e38e2015-02-09 23:39:45 -050057unsigned int do_pc2;
58unsigned int do_pc3;
59unsigned int do_pc6;
60unsigned int do_pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -080061unsigned int do_c8_c9_c10;
Len Brown0b2bb692015-03-26 00:50:30 -040062unsigned int do_skl_residency;
Len Brown144b44b2013-11-09 00:30:16 -050063unsigned int do_slm_cstates;
64unsigned int use_c1_residency_msr;
Len Brown103a8fe2010-10-22 23:53:03 -040065unsigned int has_aperf;
Len Brown889facb2012-11-08 00:48:57 -050066unsigned int has_epb;
Len Brownfc04cc62014-02-06 00:55:19 -050067unsigned int units = 1000000; /* MHz etc */
Len Brown103a8fe2010-10-22 23:53:03 -040068unsigned int genuine_intel;
69unsigned int has_invariant_tsc;
Len Brownd7899442015-01-23 00:12:33 -050070unsigned int do_nhm_platform_info;
Len Brown2f32edf2012-09-21 23:45:46 -040071unsigned int extra_msr_offset32;
72unsigned int extra_msr_offset64;
Len Brown8e180f32012-09-22 01:25:08 -040073unsigned int extra_delta_offset32;
74unsigned int extra_delta_offset64;
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +020075unsigned int aperf_mperf_multiplier = 1;
Len Brown1ed51012013-02-10 17:19:24 -050076int do_smi;
Len Brown103a8fe2010-10-22 23:53:03 -040077double bclk;
Len Browna2b7b742015-09-26 00:12:38 -040078double base_hz;
Len Brown21ed5572015-10-19 22:37:40 -040079unsigned int has_base_hz;
Len Browna2b7b742015-09-26 00:12:38 -040080double tsc_tweak = 1.0;
Len Brown103a8fe2010-10-22 23:53:03 -040081unsigned int show_pkg;
82unsigned int show_core;
83unsigned int show_cpu;
Len Brownc98d5d92012-06-04 00:56:40 -040084unsigned int show_pkg_only;
85unsigned int show_core_only;
86char *output_buffer, *outp;
Len Brown889facb2012-11-08 00:48:57 -050087unsigned int do_rapl;
88unsigned int do_dts;
89unsigned int do_ptm;
90unsigned int tcc_activation_temp;
91unsigned int tcc_activation_temp_override;
Andrey Semin40ee8e32014-12-05 00:07:00 -050092double rapl_power_units, rapl_time_units;
93double rapl_dram_energy_units, rapl_energy_units;
Len Brown889facb2012-11-08 00:48:57 -050094double rapl_joule_counter_range;
Len Brown3a9a9412014-08-15 02:39:52 -040095unsigned int do_core_perf_limit_reasons;
96unsigned int do_gfx_perf_limit_reasons;
97unsigned int do_ring_perf_limit_reasons;
Len Brown8a5bdf42015-04-01 21:02:57 -040098unsigned int crystal_hz;
99unsigned long long tsc_hz;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -0400100int base_cpu;
Len Brown21ed5572015-10-19 22:37:40 -0400101double discover_bclk(unsigned int family, unsigned int model);
Len Brown7f5c2582015-12-01 01:36:39 -0500102unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */
103 /* IA32_HWP_REQUEST, IA32_HWP_STATUS */
104unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */
105unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */
106unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */
107unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */
Len Brown889facb2012-11-08 00:48:57 -0500108
Len Browne6f9bb32013-12-03 02:19:19 -0500109#define RAPL_PKG (1 << 0)
110 /* 0x610 MSR_PKG_POWER_LIMIT */
111 /* 0x611 MSR_PKG_ENERGY_STATUS */
112#define RAPL_PKG_PERF_STATUS (1 << 1)
113 /* 0x613 MSR_PKG_PERF_STATUS */
114#define RAPL_PKG_POWER_INFO (1 << 2)
115 /* 0x614 MSR_PKG_POWER_INFO */
116
117#define RAPL_DRAM (1 << 3)
118 /* 0x618 MSR_DRAM_POWER_LIMIT */
119 /* 0x619 MSR_DRAM_ENERGY_STATUS */
Len Browne6f9bb32013-12-03 02:19:19 -0500120#define RAPL_DRAM_PERF_STATUS (1 << 4)
121 /* 0x61b MSR_DRAM_PERF_STATUS */
Len Brown0b2bb692015-03-26 00:50:30 -0400122#define RAPL_DRAM_POWER_INFO (1 << 5)
123 /* 0x61c MSR_DRAM_POWER_INFO */
Len Browne6f9bb32013-12-03 02:19:19 -0500124
Len Brown0b2bb692015-03-26 00:50:30 -0400125#define RAPL_CORES (1 << 6)
Len Browne6f9bb32013-12-03 02:19:19 -0500126 /* 0x638 MSR_PP0_POWER_LIMIT */
127 /* 0x639 MSR_PP0_ENERGY_STATUS */
Len Brown0b2bb692015-03-26 00:50:30 -0400128#define RAPL_CORE_POLICY (1 << 7)
Len Browne6f9bb32013-12-03 02:19:19 -0500129 /* 0x63a MSR_PP0_POLICY */
130
Len Brown0b2bb692015-03-26 00:50:30 -0400131#define RAPL_GFX (1 << 8)
Len Browne6f9bb32013-12-03 02:19:19 -0500132 /* 0x640 MSR_PP1_POWER_LIMIT */
133 /* 0x641 MSR_PP1_ENERGY_STATUS */
134 /* 0x642 MSR_PP1_POLICY */
Len Brown889facb2012-11-08 00:48:57 -0500135#define TJMAX_DEFAULT 100
136
137#define MAX(a, b) ((a) > (b) ? (a) : (b))
Len Brown103a8fe2010-10-22 23:53:03 -0400138
139int aperf_mperf_unstable;
140int backwards_count;
141char *progname;
Len Brown103a8fe2010-10-22 23:53:03 -0400142
Len Brownc98d5d92012-06-04 00:56:40 -0400143cpu_set_t *cpu_present_set, *cpu_affinity_set;
144size_t cpu_present_setsize, cpu_affinity_setsize;
Len Brown103a8fe2010-10-22 23:53:03 -0400145
Len Brownc98d5d92012-06-04 00:56:40 -0400146struct thread_data {
147 unsigned long long tsc;
148 unsigned long long aperf;
149 unsigned long long mperf;
Len Brown144b44b2013-11-09 00:30:16 -0500150 unsigned long long c1;
Len Brown2f32edf2012-09-21 23:45:46 -0400151 unsigned long long extra_msr64;
Len Brown8e180f32012-09-22 01:25:08 -0400152 unsigned long long extra_delta64;
153 unsigned long long extra_msr32;
154 unsigned long long extra_delta32;
Len Brown1ed51012013-02-10 17:19:24 -0500155 unsigned int smi_count;
Len Brownc98d5d92012-06-04 00:56:40 -0400156 unsigned int cpu_id;
157 unsigned int flags;
158#define CPU_IS_FIRST_THREAD_IN_CORE 0x2
159#define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
160} *thread_even, *thread_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400161
Len Brownc98d5d92012-06-04 00:56:40 -0400162struct core_data {
163 unsigned long long c3;
164 unsigned long long c6;
165 unsigned long long c7;
Len Brown889facb2012-11-08 00:48:57 -0500166 unsigned int core_temp_c;
Len Brownc98d5d92012-06-04 00:56:40 -0400167 unsigned int core_id;
168} *core_even, *core_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400169
Len Brownc98d5d92012-06-04 00:56:40 -0400170struct pkg_data {
171 unsigned long long pc2;
172 unsigned long long pc3;
173 unsigned long long pc6;
174 unsigned long long pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800175 unsigned long long pc8;
176 unsigned long long pc9;
177 unsigned long long pc10;
Len Brown0b2bb692015-03-26 00:50:30 -0400178 unsigned long long pkg_wtd_core_c0;
179 unsigned long long pkg_any_core_c0;
180 unsigned long long pkg_any_gfxe_c0;
181 unsigned long long pkg_both_core_gfxe_c0;
Len Brownc98d5d92012-06-04 00:56:40 -0400182 unsigned int package_id;
Len Brown889facb2012-11-08 00:48:57 -0500183 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
184 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
185 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
186 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
187 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
188 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
189 unsigned int pkg_temp_c;
190
Len Brownc98d5d92012-06-04 00:56:40 -0400191} *package_even, *package_odd;
192
193#define ODD_COUNTERS thread_odd, core_odd, package_odd
194#define EVEN_COUNTERS thread_even, core_even, package_even
195
196#define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
197 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
198 topo.num_threads_per_core + \
199 (core_no) * topo.num_threads_per_core + (thread_no))
200#define GET_CORE(core_base, core_no, pkg_no) \
201 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
202#define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
203
204struct system_summary {
205 struct thread_data threads;
206 struct core_data cores;
207 struct pkg_data packages;
208} sum, average;
209
210
211struct topo_params {
212 int num_packages;
213 int num_cpus;
214 int num_cores;
215 int max_cpu_num;
216 int num_cores_per_pkg;
217 int num_threads_per_core;
218} topo;
219
220struct timeval tv_even, tv_odd, tv_delta;
221
222void setup_all_buffers(void);
223
224int cpu_is_not_present(int cpu)
Len Brownd15cf7c2012-06-03 23:24:00 -0400225{
Len Brownc98d5d92012-06-04 00:56:40 -0400226 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brownd15cf7c2012-06-03 23:24:00 -0400227}
Len Brown88c32812012-03-29 21:44:40 -0400228/*
Len Brownc98d5d92012-06-04 00:56:40 -0400229 * run func(thread, core, package) in topology order
230 * skip non-present cpus
Len Brown88c32812012-03-29 21:44:40 -0400231 */
Len Brownd15cf7c2012-06-03 23:24:00 -0400232
Len Brownc98d5d92012-06-04 00:56:40 -0400233int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
234 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
Len Brown88c32812012-03-29 21:44:40 -0400235{
Len Brownc98d5d92012-06-04 00:56:40 -0400236 int retval, pkg_no, core_no, thread_no;
237
238 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
239 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
240 for (thread_no = 0; thread_no <
241 topo.num_threads_per_core; ++thread_no) {
242 struct thread_data *t;
243 struct core_data *c;
244 struct pkg_data *p;
245
246 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
247
248 if (cpu_is_not_present(t->cpu_id))
249 continue;
250
251 c = GET_CORE(core_base, core_no, pkg_no);
252 p = GET_PKG(pkg_base, pkg_no);
253
254 retval = func(t, c, p);
255 if (retval)
256 return retval;
257 }
258 }
259 }
260 return 0;
Len Brown88c32812012-03-29 21:44:40 -0400261}
262
263int cpu_migrate(int cpu)
264{
Len Brownc98d5d92012-06-04 00:56:40 -0400265 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
266 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
267 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
Len Brown88c32812012-03-29 21:44:40 -0400268 return -1;
269 else
270 return 0;
271}
272
Len Brown15aaa342012-03-29 22:19:58 -0400273int get_msr(int cpu, off_t offset, unsigned long long *msr)
Len Brown103a8fe2010-10-22 23:53:03 -0400274{
275 ssize_t retval;
Len Brown103a8fe2010-10-22 23:53:03 -0400276 char pathname[32];
277 int fd;
278
279 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
280 fd = open(pathname, O_RDONLY);
Len Brown15aaa342012-03-29 22:19:58 -0400281 if (fd < 0)
Len Brown98481e72014-08-15 00:36:50 -0400282 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 -0400283
Len Brown15aaa342012-03-29 22:19:58 -0400284 retval = pread(fd, msr, sizeof *msr, offset);
Len Brown103a8fe2010-10-22 23:53:03 -0400285 close(fd);
Len Brown15aaa342012-03-29 22:19:58 -0400286
Len Brown98481e72014-08-15 00:36:50 -0400287 if (retval != sizeof *msr)
288 err(-1, "%s offset 0x%llx read failed", pathname, (unsigned long long)offset);
Len Brown15aaa342012-03-29 22:19:58 -0400289
290 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400291}
292
Len Brownfc04cc62014-02-06 00:55:19 -0500293/*
294 * Example Format w/ field column widths:
295 *
Len Browne7c95ff2014-08-14 21:22:13 -0400296 * 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
297 * 123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678
Len Brownfc04cc62014-02-06 00:55:19 -0500298 */
299
Len Browna829eb42011-02-10 23:36:34 -0500300void print_header(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400301{
302 if (show_pkg)
Len Browne7c95ff2014-08-14 21:22:13 -0400303 outp += sprintf(outp, " Package");
Len Brown103a8fe2010-10-22 23:53:03 -0400304 if (show_core)
Len Browne7c95ff2014-08-14 21:22:13 -0400305 outp += sprintf(outp, " Core");
Len Brown103a8fe2010-10-22 23:53:03 -0400306 if (show_cpu)
Len Browne7c95ff2014-08-14 21:22:13 -0400307 outp += sprintf(outp, " CPU");
Len Brown103a8fe2010-10-22 23:53:03 -0400308 if (has_aperf)
Len Browne7c95ff2014-08-14 21:22:13 -0400309 outp += sprintf(outp, " Avg_MHz");
Len Brownd7899442015-01-23 00:12:33 -0500310 if (has_aperf)
Len Browne7c95ff2014-08-14 21:22:13 -0400311 outp += sprintf(outp, " %%Busy");
Len Brownfc04cc62014-02-06 00:55:19 -0500312 if (has_aperf)
Len Browne7c95ff2014-08-14 21:22:13 -0400313 outp += sprintf(outp, " Bzy_MHz");
314 outp += sprintf(outp, " TSC_MHz");
Len Brown1cc21f72015-02-23 00:34:57 -0500315
Len Brown8e180f32012-09-22 01:25:08 -0400316 if (extra_delta_offset32)
Len Browne7c95ff2014-08-14 21:22:13 -0400317 outp += sprintf(outp, " count 0x%03X", extra_delta_offset32);
Len Brown8e180f32012-09-22 01:25:08 -0400318 if (extra_delta_offset64)
Len Browne7c95ff2014-08-14 21:22:13 -0400319 outp += sprintf(outp, " COUNT 0x%03X", extra_delta_offset64);
Len Brown2f32edf2012-09-21 23:45:46 -0400320 if (extra_msr_offset32)
Len Browne7c95ff2014-08-14 21:22:13 -0400321 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset32);
Len Brown2f32edf2012-09-21 23:45:46 -0400322 if (extra_msr_offset64)
Len Browne7c95ff2014-08-14 21:22:13 -0400323 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset64);
Len Brown1cc21f72015-02-23 00:34:57 -0500324
325 if (!debug)
326 goto done;
327
328 if (do_smi)
329 outp += sprintf(outp, " SMI");
330
Len Brown103a8fe2010-10-22 23:53:03 -0400331 if (do_nhm_cstates)
Len Browne7c95ff2014-08-14 21:22:13 -0400332 outp += sprintf(outp, " CPU%%c1");
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -0700333 if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates)
Len Browne7c95ff2014-08-14 21:22:13 -0400334 outp += sprintf(outp, " CPU%%c3");
Len Brown103a8fe2010-10-22 23:53:03 -0400335 if (do_nhm_cstates)
Len Browne7c95ff2014-08-14 21:22:13 -0400336 outp += sprintf(outp, " CPU%%c6");
Len Brown103a8fe2010-10-22 23:53:03 -0400337 if (do_snb_cstates)
Len Browne7c95ff2014-08-14 21:22:13 -0400338 outp += sprintf(outp, " CPU%%c7");
Len Brown889facb2012-11-08 00:48:57 -0500339
340 if (do_dts)
Len Browne7c95ff2014-08-14 21:22:13 -0400341 outp += sprintf(outp, " CoreTmp");
Len Brown889facb2012-11-08 00:48:57 -0500342 if (do_ptm)
Len Browne7c95ff2014-08-14 21:22:13 -0400343 outp += sprintf(outp, " PkgTmp");
Len Brown889facb2012-11-08 00:48:57 -0500344
Len Brown0b2bb692015-03-26 00:50:30 -0400345 if (do_skl_residency) {
346 outp += sprintf(outp, " Totl%%C0");
347 outp += sprintf(outp, " Any%%C0");
348 outp += sprintf(outp, " GFX%%C0");
349 outp += sprintf(outp, " CPUGFX%%");
350 }
351
Len Brownee7e38e2015-02-09 23:39:45 -0500352 if (do_pc2)
Len Browne7c95ff2014-08-14 21:22:13 -0400353 outp += sprintf(outp, " Pkg%%pc2");
Len Brownee7e38e2015-02-09 23:39:45 -0500354 if (do_pc3)
Len Browne7c95ff2014-08-14 21:22:13 -0400355 outp += sprintf(outp, " Pkg%%pc3");
Len Brownee7e38e2015-02-09 23:39:45 -0500356 if (do_pc6)
Len Browne7c95ff2014-08-14 21:22:13 -0400357 outp += sprintf(outp, " Pkg%%pc6");
Len Brownee7e38e2015-02-09 23:39:45 -0500358 if (do_pc7)
Len Browne7c95ff2014-08-14 21:22:13 -0400359 outp += sprintf(outp, " Pkg%%pc7");
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800360 if (do_c8_c9_c10) {
Len Browne7c95ff2014-08-14 21:22:13 -0400361 outp += sprintf(outp, " Pkg%%pc8");
362 outp += sprintf(outp, " Pkg%%pc9");
363 outp += sprintf(outp, " Pk%%pc10");
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800364 }
Len Brown103a8fe2010-10-22 23:53:03 -0400365
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800366 if (do_rapl && !rapl_joules) {
367 if (do_rapl & RAPL_PKG)
Len Browne7c95ff2014-08-14 21:22:13 -0400368 outp += sprintf(outp, " PkgWatt");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800369 if (do_rapl & RAPL_CORES)
Len Browne7c95ff2014-08-14 21:22:13 -0400370 outp += sprintf(outp, " CorWatt");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800371 if (do_rapl & RAPL_GFX)
Len Browne7c95ff2014-08-14 21:22:13 -0400372 outp += sprintf(outp, " GFXWatt");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800373 if (do_rapl & RAPL_DRAM)
Len Browne7c95ff2014-08-14 21:22:13 -0400374 outp += sprintf(outp, " RAMWatt");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800375 if (do_rapl & RAPL_PKG_PERF_STATUS)
Len Browne7c95ff2014-08-14 21:22:13 -0400376 outp += sprintf(outp, " PKG_%%");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800377 if (do_rapl & RAPL_DRAM_PERF_STATUS)
Len Browne7c95ff2014-08-14 21:22:13 -0400378 outp += sprintf(outp, " RAM_%%");
Len Brownd7899442015-01-23 00:12:33 -0500379 } else if (do_rapl && rapl_joules) {
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800380 if (do_rapl & RAPL_PKG)
Len Browne7c95ff2014-08-14 21:22:13 -0400381 outp += sprintf(outp, " Pkg_J");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800382 if (do_rapl & RAPL_CORES)
Len Browne7c95ff2014-08-14 21:22:13 -0400383 outp += sprintf(outp, " Cor_J");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800384 if (do_rapl & RAPL_GFX)
Len Browne7c95ff2014-08-14 21:22:13 -0400385 outp += sprintf(outp, " GFX_J");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800386 if (do_rapl & RAPL_DRAM)
Len Brownbd6906e2015-07-24 10:35:23 -0400387 outp += sprintf(outp, " RAM_J");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800388 if (do_rapl & RAPL_PKG_PERF_STATUS)
Len Browne7c95ff2014-08-14 21:22:13 -0400389 outp += sprintf(outp, " PKG_%%");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800390 if (do_rapl & RAPL_DRAM_PERF_STATUS)
Len Browne7c95ff2014-08-14 21:22:13 -0400391 outp += sprintf(outp, " RAM_%%");
392 outp += sprintf(outp, " time");
Len Brown889facb2012-11-08 00:48:57 -0500393
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800394 }
Len Brown1cc21f72015-02-23 00:34:57 -0500395 done:
Len Brownc98d5d92012-06-04 00:56:40 -0400396 outp += sprintf(outp, "\n");
Len Brown103a8fe2010-10-22 23:53:03 -0400397}
398
Len Brownc98d5d92012-06-04 00:56:40 -0400399int dump_counters(struct thread_data *t, struct core_data *c,
400 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400401{
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200402 outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400403
Len Brownc98d5d92012-06-04 00:56:40 -0400404 if (t) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200405 outp += sprintf(outp, "CPU: %d flags 0x%x\n",
406 t->cpu_id, t->flags);
407 outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
408 outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
409 outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
410 outp += sprintf(outp, "c1: %016llX\n", t->c1);
411 outp += sprintf(outp, "msr0x%x: %08llX\n",
Len Brown8e180f32012-09-22 01:25:08 -0400412 extra_delta_offset32, t->extra_delta32);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200413 outp += sprintf(outp, "msr0x%x: %016llX\n",
Len Brown8e180f32012-09-22 01:25:08 -0400414 extra_delta_offset64, t->extra_delta64);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200415 outp += sprintf(outp, "msr0x%x: %08llX\n",
Len Brown2f32edf2012-09-21 23:45:46 -0400416 extra_msr_offset32, t->extra_msr32);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200417 outp += sprintf(outp, "msr0x%x: %016llX\n",
Len Brown2f32edf2012-09-21 23:45:46 -0400418 extra_msr_offset64, t->extra_msr64);
Len Brown1ed51012013-02-10 17:19:24 -0500419 if (do_smi)
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200420 outp += sprintf(outp, "SMI: %08X\n", t->smi_count);
Len Brownc98d5d92012-06-04 00:56:40 -0400421 }
Len Brown103a8fe2010-10-22 23:53:03 -0400422
Len Brownc98d5d92012-06-04 00:56:40 -0400423 if (c) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200424 outp += sprintf(outp, "core: %d\n", c->core_id);
425 outp += sprintf(outp, "c3: %016llX\n", c->c3);
426 outp += sprintf(outp, "c6: %016llX\n", c->c6);
427 outp += sprintf(outp, "c7: %016llX\n", c->c7);
428 outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
Len Brownc98d5d92012-06-04 00:56:40 -0400429 }
430
431 if (p) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200432 outp += sprintf(outp, "package: %d\n", p->package_id);
Len Brown0b2bb692015-03-26 00:50:30 -0400433
434 outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
435 outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
436 outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
437 outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
438
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200439 outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
Len Brownee7e38e2015-02-09 23:39:45 -0500440 if (do_pc3)
441 outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
442 if (do_pc6)
443 outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
444 if (do_pc7)
445 outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200446 outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
447 outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
448 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
449 outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
450 outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
451 outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
452 outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
453 outp += sprintf(outp, "Throttle PKG: %0X\n",
454 p->rapl_pkg_perf_status);
455 outp += sprintf(outp, "Throttle RAM: %0X\n",
456 p->rapl_dram_perf_status);
457 outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
Len Brownc98d5d92012-06-04 00:56:40 -0400458 }
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200459
460 outp += sprintf(outp, "\n");
461
Len Brownc98d5d92012-06-04 00:56:40 -0400462 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400463}
464
Len Browne23da032012-02-06 18:37:16 -0500465/*
466 * column formatting convention & formats
Len Browne23da032012-02-06 18:37:16 -0500467 */
Len Brownc98d5d92012-06-04 00:56:40 -0400468int format_counters(struct thread_data *t, struct core_data *c,
469 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400470{
471 double interval_float;
Len Brownfc04cc62014-02-06 00:55:19 -0500472 char *fmt8;
Len Brown103a8fe2010-10-22 23:53:03 -0400473
Len Brownc98d5d92012-06-04 00:56:40 -0400474 /* if showing only 1st thread in core and this isn't one, bail out */
475 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
476 return 0;
477
478 /* if showing only 1st thread in pkg and this isn't one, bail out */
479 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
480 return 0;
481
Len Brown103a8fe2010-10-22 23:53:03 -0400482 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
483
Len Brownc98d5d92012-06-04 00:56:40 -0400484 /* topo columns, print blanks on 1st (average) line */
485 if (t == &average.threads) {
Len Brown103a8fe2010-10-22 23:53:03 -0400486 if (show_pkg)
Len Brownfc04cc62014-02-06 00:55:19 -0500487 outp += sprintf(outp, " -");
Len Brown103a8fe2010-10-22 23:53:03 -0400488 if (show_core)
Len Brownfc04cc62014-02-06 00:55:19 -0500489 outp += sprintf(outp, " -");
Len Brown103a8fe2010-10-22 23:53:03 -0400490 if (show_cpu)
Len Brownfc04cc62014-02-06 00:55:19 -0500491 outp += sprintf(outp, " -");
Len Brown103a8fe2010-10-22 23:53:03 -0400492 } else {
Len Brownc98d5d92012-06-04 00:56:40 -0400493 if (show_pkg) {
494 if (p)
Len Brownfc04cc62014-02-06 00:55:19 -0500495 outp += sprintf(outp, "%8d", p->package_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400496 else
Len Brownfc04cc62014-02-06 00:55:19 -0500497 outp += sprintf(outp, " -");
Len Brownc98d5d92012-06-04 00:56:40 -0400498 }
Len Brownc98d5d92012-06-04 00:56:40 -0400499 if (show_core) {
500 if (c)
Len Brownfc04cc62014-02-06 00:55:19 -0500501 outp += sprintf(outp, "%8d", c->core_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400502 else
Len Brownfc04cc62014-02-06 00:55:19 -0500503 outp += sprintf(outp, " -");
Len Brownc98d5d92012-06-04 00:56:40 -0400504 }
Len Brown103a8fe2010-10-22 23:53:03 -0400505 if (show_cpu)
Len Brownfc04cc62014-02-06 00:55:19 -0500506 outp += sprintf(outp, "%8d", t->cpu_id);
Len Brown103a8fe2010-10-22 23:53:03 -0400507 }
Len Brownfc04cc62014-02-06 00:55:19 -0500508
Len Brownd7899442015-01-23 00:12:33 -0500509 /* Avg_MHz */
Len Brownfc04cc62014-02-06 00:55:19 -0500510 if (has_aperf)
511 outp += sprintf(outp, "%8.0f",
512 1.0 / units * t->aperf / interval_float);
513
Len Brownd7899442015-01-23 00:12:33 -0500514 /* %Busy */
515 if (has_aperf) {
Len Brown103a8fe2010-10-22 23:53:03 -0400516 if (!skip_c0)
Len Browna2b7b742015-09-26 00:12:38 -0400517 outp += sprintf(outp, "%8.2f", 100.0 * t->mperf/t->tsc/tsc_tweak);
Len Brown103a8fe2010-10-22 23:53:03 -0400518 else
Len Brownfc04cc62014-02-06 00:55:19 -0500519 outp += sprintf(outp, "********");
Len Brown103a8fe2010-10-22 23:53:03 -0400520 }
521
Len Brownd7899442015-01-23 00:12:33 -0500522 /* Bzy_MHz */
Len Brown21ed5572015-10-19 22:37:40 -0400523 if (has_aperf) {
524 if (has_base_hz)
525 outp += sprintf(outp, "%8.0f", base_hz / units * t->aperf / t->mperf);
526 else
527 outp += sprintf(outp, "%8.0f",
528 1.0 * t->tsc / units * t->aperf / t->mperf / interval_float);
529 }
Len Brown103a8fe2010-10-22 23:53:03 -0400530
Len Brownd7899442015-01-23 00:12:33 -0500531 /* TSC_MHz */
Len Brownfc04cc62014-02-06 00:55:19 -0500532 outp += sprintf(outp, "%8.0f", 1.0 * t->tsc/units/interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400533
Len Brown8e180f32012-09-22 01:25:08 -0400534 /* delta */
535 if (extra_delta_offset32)
536 outp += sprintf(outp, " %11llu", t->extra_delta32);
537
538 /* DELTA */
539 if (extra_delta_offset64)
540 outp += sprintf(outp, " %11llu", t->extra_delta64);
Len Brown2f32edf2012-09-21 23:45:46 -0400541 /* msr */
542 if (extra_msr_offset32)
Len Brown8e180f32012-09-22 01:25:08 -0400543 outp += sprintf(outp, " 0x%08llx", t->extra_msr32);
Len Brown2f32edf2012-09-21 23:45:46 -0400544
Len Brown130ff302012-09-21 22:56:06 -0400545 /* MSR */
Len Brown2f32edf2012-09-21 23:45:46 -0400546 if (extra_msr_offset64)
547 outp += sprintf(outp, " 0x%016llx", t->extra_msr64);
Len Brown130ff302012-09-21 22:56:06 -0400548
Len Brown1cc21f72015-02-23 00:34:57 -0500549 if (!debug)
550 goto done;
551
552 /* SMI */
553 if (do_smi)
554 outp += sprintf(outp, "%8d", t->smi_count);
555
Len Brown103a8fe2010-10-22 23:53:03 -0400556 if (do_nhm_cstates) {
557 if (!skip_c1)
Len Brownfc04cc62014-02-06 00:55:19 -0500558 outp += sprintf(outp, "%8.2f", 100.0 * t->c1/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400559 else
Len Brownfc04cc62014-02-06 00:55:19 -0500560 outp += sprintf(outp, "********");
Len Brown103a8fe2010-10-22 23:53:03 -0400561 }
Len Brownc98d5d92012-06-04 00:56:40 -0400562
563 /* print per-core data only for 1st thread in core */
564 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
565 goto done;
566
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -0700567 if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates)
Len Brownfc04cc62014-02-06 00:55:19 -0500568 outp += sprintf(outp, "%8.2f", 100.0 * c->c3/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400569 if (do_nhm_cstates)
Len Brownfc04cc62014-02-06 00:55:19 -0500570 outp += sprintf(outp, "%8.2f", 100.0 * c->c6/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400571 if (do_snb_cstates)
Len Brownfc04cc62014-02-06 00:55:19 -0500572 outp += sprintf(outp, "%8.2f", 100.0 * c->c7/t->tsc);
Len Brownc98d5d92012-06-04 00:56:40 -0400573
Len Brown889facb2012-11-08 00:48:57 -0500574 if (do_dts)
Len Brownfc04cc62014-02-06 00:55:19 -0500575 outp += sprintf(outp, "%8d", c->core_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500576
Len Brownc98d5d92012-06-04 00:56:40 -0400577 /* print per-package data only for 1st core in package */
578 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
579 goto done;
580
Len Brown0b2bb692015-03-26 00:50:30 -0400581 /* PkgTmp */
Len Brown889facb2012-11-08 00:48:57 -0500582 if (do_ptm)
Len Brownfc04cc62014-02-06 00:55:19 -0500583 outp += sprintf(outp, "%8d", p->pkg_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500584
Len Brown0b2bb692015-03-26 00:50:30 -0400585 /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
586 if (do_skl_residency) {
587 outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_wtd_core_c0/t->tsc);
588 outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_any_core_c0/t->tsc);
589 outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_any_gfxe_c0/t->tsc);
590 outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_both_core_gfxe_c0/t->tsc);
591 }
592
Len Brownee7e38e2015-02-09 23:39:45 -0500593 if (do_pc2)
Len Brownfc04cc62014-02-06 00:55:19 -0500594 outp += sprintf(outp, "%8.2f", 100.0 * p->pc2/t->tsc);
Len Brownee7e38e2015-02-09 23:39:45 -0500595 if (do_pc3)
Len Brownfc04cc62014-02-06 00:55:19 -0500596 outp += sprintf(outp, "%8.2f", 100.0 * p->pc3/t->tsc);
Len Brownee7e38e2015-02-09 23:39:45 -0500597 if (do_pc6)
Len Brownfc04cc62014-02-06 00:55:19 -0500598 outp += sprintf(outp, "%8.2f", 100.0 * p->pc6/t->tsc);
Len Brownee7e38e2015-02-09 23:39:45 -0500599 if (do_pc7)
Len Brownfc04cc62014-02-06 00:55:19 -0500600 outp += sprintf(outp, "%8.2f", 100.0 * p->pc7/t->tsc);
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800601 if (do_c8_c9_c10) {
Len Brownfc04cc62014-02-06 00:55:19 -0500602 outp += sprintf(outp, "%8.2f", 100.0 * p->pc8/t->tsc);
603 outp += sprintf(outp, "%8.2f", 100.0 * p->pc9/t->tsc);
604 outp += sprintf(outp, "%8.2f", 100.0 * p->pc10/t->tsc);
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800605 }
Len Brown889facb2012-11-08 00:48:57 -0500606
607 /*
608 * If measurement interval exceeds minimum RAPL Joule Counter range,
609 * indicate that results are suspect by printing "**" in fraction place.
610 */
Len Brownfc04cc62014-02-06 00:55:19 -0500611 if (interval_float < rapl_joule_counter_range)
612 fmt8 = "%8.2f";
613 else
614 fmt8 = " %6.0f**";
Len Brown889facb2012-11-08 00:48:57 -0500615
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800616 if (do_rapl && !rapl_joules) {
617 if (do_rapl & RAPL_PKG)
Len Brownfc04cc62014-02-06 00:55:19 -0500618 outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800619 if (do_rapl & RAPL_CORES)
Len Brownfc04cc62014-02-06 00:55:19 -0500620 outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800621 if (do_rapl & RAPL_GFX)
Len Brownfc04cc62014-02-06 00:55:19 -0500622 outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800623 if (do_rapl & RAPL_DRAM)
Andrey Semin40ee8e32014-12-05 00:07:00 -0500624 outp += sprintf(outp, fmt8, p->energy_dram * rapl_dram_energy_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800625 if (do_rapl & RAPL_PKG_PERF_STATUS)
Len Brownfc04cc62014-02-06 00:55:19 -0500626 outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800627 if (do_rapl & RAPL_DRAM_PERF_STATUS)
Len Brownfc04cc62014-02-06 00:55:19 -0500628 outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
Len Brownd7899442015-01-23 00:12:33 -0500629 } else if (do_rapl && rapl_joules) {
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800630 if (do_rapl & RAPL_PKG)
Len Brownfc04cc62014-02-06 00:55:19 -0500631 outp += sprintf(outp, fmt8,
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800632 p->energy_pkg * rapl_energy_units);
633 if (do_rapl & RAPL_CORES)
Len Brownfc04cc62014-02-06 00:55:19 -0500634 outp += sprintf(outp, fmt8,
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800635 p->energy_cores * rapl_energy_units);
636 if (do_rapl & RAPL_GFX)
Len Brownfc04cc62014-02-06 00:55:19 -0500637 outp += sprintf(outp, fmt8,
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800638 p->energy_gfx * rapl_energy_units);
639 if (do_rapl & RAPL_DRAM)
Len Brownfc04cc62014-02-06 00:55:19 -0500640 outp += sprintf(outp, fmt8,
Andrey Semin40ee8e32014-12-05 00:07:00 -0500641 p->energy_dram * rapl_dram_energy_units);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800642 if (do_rapl & RAPL_PKG_PERF_STATUS)
Len Brownfc04cc62014-02-06 00:55:19 -0500643 outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800644 if (do_rapl & RAPL_DRAM_PERF_STATUS)
Len Brownfc04cc62014-02-06 00:55:19 -0500645 outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
Len Brown889facb2012-11-08 00:48:57 -0500646
Len Brownd7899442015-01-23 00:12:33 -0500647 outp += sprintf(outp, fmt8, interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800648 }
Len Brownc98d5d92012-06-04 00:56:40 -0400649done:
Len Brownc98d5d92012-06-04 00:56:40 -0400650 outp += sprintf(outp, "\n");
651
652 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400653}
654
Len Brownc98d5d92012-06-04 00:56:40 -0400655void flush_stdout()
Len Brown103a8fe2010-10-22 23:53:03 -0400656{
Len Brownc98d5d92012-06-04 00:56:40 -0400657 fputs(output_buffer, stdout);
Len Brownddac0d62012-11-30 01:01:40 -0500658 fflush(stdout);
Len Brownc98d5d92012-06-04 00:56:40 -0400659 outp = output_buffer;
660}
661void flush_stderr()
662{
663 fputs(output_buffer, stderr);
664 outp = output_buffer;
665}
666void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
667{
Len Browne23da032012-02-06 18:37:16 -0500668 static int printed;
Len Brown103a8fe2010-10-22 23:53:03 -0400669
Len Browne23da032012-02-06 18:37:16 -0500670 if (!printed || !summary_only)
671 print_header();
Len Brown103a8fe2010-10-22 23:53:03 -0400672
Len Brownc98d5d92012-06-04 00:56:40 -0400673 if (topo.num_cpus > 1)
674 format_counters(&average.threads, &average.cores,
675 &average.packages);
Len Brown103a8fe2010-10-22 23:53:03 -0400676
Len Browne23da032012-02-06 18:37:16 -0500677 printed = 1;
678
679 if (summary_only)
680 return;
681
Len Brownc98d5d92012-06-04 00:56:40 -0400682 for_all_cpus(format_counters, t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400683}
684
Len Brown889facb2012-11-08 00:48:57 -0500685#define DELTA_WRAP32(new, old) \
686 if (new > old) { \
687 old = new - old; \
688 } else { \
689 old = 0x100000000 + new - old; \
690 }
691
Len Brownc98d5d92012-06-04 00:56:40 -0400692void
693delta_package(struct pkg_data *new, struct pkg_data *old)
Len Brown103a8fe2010-10-22 23:53:03 -0400694{
Len Brown0b2bb692015-03-26 00:50:30 -0400695
696 if (do_skl_residency) {
697 old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
698 old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
699 old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
700 old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0;
701 }
Len Brownc98d5d92012-06-04 00:56:40 -0400702 old->pc2 = new->pc2 - old->pc2;
Len Brownee7e38e2015-02-09 23:39:45 -0500703 if (do_pc3)
704 old->pc3 = new->pc3 - old->pc3;
705 if (do_pc6)
706 old->pc6 = new->pc6 - old->pc6;
707 if (do_pc7)
708 old->pc7 = new->pc7 - old->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800709 old->pc8 = new->pc8 - old->pc8;
710 old->pc9 = new->pc9 - old->pc9;
711 old->pc10 = new->pc10 - old->pc10;
Len Brown889facb2012-11-08 00:48:57 -0500712 old->pkg_temp_c = new->pkg_temp_c;
713
714 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
715 DELTA_WRAP32(new->energy_cores, old->energy_cores);
716 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
717 DELTA_WRAP32(new->energy_dram, old->energy_dram);
718 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
719 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
Len Brownc98d5d92012-06-04 00:56:40 -0400720}
Len Brown103a8fe2010-10-22 23:53:03 -0400721
Len Brownc98d5d92012-06-04 00:56:40 -0400722void
723delta_core(struct core_data *new, struct core_data *old)
724{
725 old->c3 = new->c3 - old->c3;
726 old->c6 = new->c6 - old->c6;
727 old->c7 = new->c7 - old->c7;
Len Brown889facb2012-11-08 00:48:57 -0500728 old->core_temp_c = new->core_temp_c;
Len Brownc98d5d92012-06-04 00:56:40 -0400729}
Len Brown103a8fe2010-10-22 23:53:03 -0400730
Len Brownc3ae3312012-06-13 21:31:46 -0400731/*
732 * old = new - old
733 */
Len Brownc98d5d92012-06-04 00:56:40 -0400734void
735delta_thread(struct thread_data *new, struct thread_data *old,
736 struct core_data *core_delta)
737{
738 old->tsc = new->tsc - old->tsc;
Len Brown103a8fe2010-10-22 23:53:03 -0400739
Len Brownc98d5d92012-06-04 00:56:40 -0400740 /* check for TSC < 1 Mcycles over interval */
Josh Triplettb2c95d92013-08-20 17:20:18 -0700741 if (old->tsc < (1000 * 1000))
742 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
743 "You can disable all c-states by booting with \"idle=poll\"\n"
744 "or just the deep ones with \"processor.max_cstate=1\"");
Len Brown103a8fe2010-10-22 23:53:03 -0400745
Len Brownc98d5d92012-06-04 00:56:40 -0400746 old->c1 = new->c1 - old->c1;
Len Brown103a8fe2010-10-22 23:53:03 -0400747
Len Browna7296172015-01-23 01:33:58 -0500748 if (has_aperf) {
749 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
750 old->aperf = new->aperf - old->aperf;
751 old->mperf = new->mperf - old->mperf;
752 } else {
Len Brown103a8fe2010-10-22 23:53:03 -0400753
Len Browna7296172015-01-23 01:33:58 -0500754 if (!aperf_mperf_unstable) {
755 fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname);
756 fprintf(stderr, "* Frequency results do not cover entire interval *\n");
757 fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n");
Len Brownc98d5d92012-06-04 00:56:40 -0400758
Len Browna7296172015-01-23 01:33:58 -0500759 aperf_mperf_unstable = 1;
760 }
761 /*
762 * mperf delta is likely a huge "positive" number
763 * can not use it for calculating c0 time
764 */
765 skip_c0 = 1;
766 skip_c1 = 1;
Len Brownc98d5d92012-06-04 00:56:40 -0400767 }
Len Brownc98d5d92012-06-04 00:56:40 -0400768 }
Len Brown103a8fe2010-10-22 23:53:03 -0400769
Len Brown103a8fe2010-10-22 23:53:03 -0400770
Len Brown144b44b2013-11-09 00:30:16 -0500771 if (use_c1_residency_msr) {
772 /*
773 * Some models have a dedicated C1 residency MSR,
774 * which should be more accurate than the derivation below.
775 */
776 } else {
777 /*
778 * As counter collection is not atomic,
779 * it is possible for mperf's non-halted cycles + idle states
780 * to exceed TSC's all cycles: show c1 = 0% in that case.
781 */
782 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
783 old->c1 = 0;
784 else {
785 /* normal case, derive c1 */
786 old->c1 = old->tsc - old->mperf - core_delta->c3
Len Brownc98d5d92012-06-04 00:56:40 -0400787 - core_delta->c6 - core_delta->c7;
Len Brown144b44b2013-11-09 00:30:16 -0500788 }
Len Brownc98d5d92012-06-04 00:56:40 -0400789 }
Len Brownc3ae3312012-06-13 21:31:46 -0400790
Len Brownc98d5d92012-06-04 00:56:40 -0400791 if (old->mperf == 0) {
Len Brownd8af6f52015-02-10 01:56:38 -0500792 if (debug > 1) fprintf(stderr, "cpu%d MPERF 0!\n", old->cpu_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400793 old->mperf = 1; /* divide by 0 protection */
794 }
795
Len Brown8e180f32012-09-22 01:25:08 -0400796 old->extra_delta32 = new->extra_delta32 - old->extra_delta32;
797 old->extra_delta32 &= 0xFFFFFFFF;
798
799 old->extra_delta64 = new->extra_delta64 - old->extra_delta64;
800
Len Brownc98d5d92012-06-04 00:56:40 -0400801 /*
Len Brown8e180f32012-09-22 01:25:08 -0400802 * Extra MSR is just a snapshot, simply copy latest w/o subtracting
Len Brownc98d5d92012-06-04 00:56:40 -0400803 */
Len Brown2f32edf2012-09-21 23:45:46 -0400804 old->extra_msr32 = new->extra_msr32;
805 old->extra_msr64 = new->extra_msr64;
Len Brown1ed51012013-02-10 17:19:24 -0500806
807 if (do_smi)
808 old->smi_count = new->smi_count - old->smi_count;
Len Brownc98d5d92012-06-04 00:56:40 -0400809}
810
811int delta_cpu(struct thread_data *t, struct core_data *c,
812 struct pkg_data *p, struct thread_data *t2,
813 struct core_data *c2, struct pkg_data *p2)
814{
815 /* calculate core delta only for 1st thread in core */
816 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
817 delta_core(c, c2);
818
819 /* always calculate thread delta */
820 delta_thread(t, t2, c2); /* c2 is core delta */
821
822 /* calculate package delta only for 1st core in package */
823 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
824 delta_package(p, p2);
825
826 return 0;
827}
828
829void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
830{
831 t->tsc = 0;
832 t->aperf = 0;
833 t->mperf = 0;
834 t->c1 = 0;
835
Len Brown1ed51012013-02-10 17:19:24 -0500836 t->smi_count = 0;
Len Brown8e180f32012-09-22 01:25:08 -0400837 t->extra_delta32 = 0;
838 t->extra_delta64 = 0;
839
Len Brownc98d5d92012-06-04 00:56:40 -0400840 /* tells format_counters to dump all fields from this set */
841 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
842
843 c->c3 = 0;
844 c->c6 = 0;
845 c->c7 = 0;
Len Brown889facb2012-11-08 00:48:57 -0500846 c->core_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -0400847
Len Brown0b2bb692015-03-26 00:50:30 -0400848 p->pkg_wtd_core_c0 = 0;
849 p->pkg_any_core_c0 = 0;
850 p->pkg_any_gfxe_c0 = 0;
851 p->pkg_both_core_gfxe_c0 = 0;
852
Len Brownc98d5d92012-06-04 00:56:40 -0400853 p->pc2 = 0;
Len Brownee7e38e2015-02-09 23:39:45 -0500854 if (do_pc3)
855 p->pc3 = 0;
856 if (do_pc6)
857 p->pc6 = 0;
858 if (do_pc7)
859 p->pc7 = 0;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800860 p->pc8 = 0;
861 p->pc9 = 0;
862 p->pc10 = 0;
Len Brown889facb2012-11-08 00:48:57 -0500863
864 p->energy_pkg = 0;
865 p->energy_dram = 0;
866 p->energy_cores = 0;
867 p->energy_gfx = 0;
868 p->rapl_pkg_perf_status = 0;
869 p->rapl_dram_perf_status = 0;
870 p->pkg_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -0400871}
872int sum_counters(struct thread_data *t, struct core_data *c,
873 struct pkg_data *p)
874{
875 average.threads.tsc += t->tsc;
876 average.threads.aperf += t->aperf;
877 average.threads.mperf += t->mperf;
878 average.threads.c1 += t->c1;
879
Len Brown8e180f32012-09-22 01:25:08 -0400880 average.threads.extra_delta32 += t->extra_delta32;
881 average.threads.extra_delta64 += t->extra_delta64;
882
Len Brownc98d5d92012-06-04 00:56:40 -0400883 /* sum per-core values only for 1st thread in core */
884 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
885 return 0;
886
887 average.cores.c3 += c->c3;
888 average.cores.c6 += c->c6;
889 average.cores.c7 += c->c7;
890
Len Brown889facb2012-11-08 00:48:57 -0500891 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
892
Len Brownc98d5d92012-06-04 00:56:40 -0400893 /* sum per-pkg values only for 1st core in pkg */
894 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
895 return 0;
896
Len Brown0b2bb692015-03-26 00:50:30 -0400897 if (do_skl_residency) {
898 average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
899 average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
900 average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
901 average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
902 }
903
Len Brownc98d5d92012-06-04 00:56:40 -0400904 average.packages.pc2 += p->pc2;
Len Brownee7e38e2015-02-09 23:39:45 -0500905 if (do_pc3)
906 average.packages.pc3 += p->pc3;
907 if (do_pc6)
908 average.packages.pc6 += p->pc6;
909 if (do_pc7)
910 average.packages.pc7 += p->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800911 average.packages.pc8 += p->pc8;
912 average.packages.pc9 += p->pc9;
913 average.packages.pc10 += p->pc10;
Len Brownc98d5d92012-06-04 00:56:40 -0400914
Len Brown889facb2012-11-08 00:48:57 -0500915 average.packages.energy_pkg += p->energy_pkg;
916 average.packages.energy_dram += p->energy_dram;
917 average.packages.energy_cores += p->energy_cores;
918 average.packages.energy_gfx += p->energy_gfx;
919
920 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
921
922 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
923 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
Len Brownc98d5d92012-06-04 00:56:40 -0400924 return 0;
925}
926/*
927 * sum the counters for all cpus in the system
928 * compute the weighted average
929 */
930void compute_average(struct thread_data *t, struct core_data *c,
931 struct pkg_data *p)
932{
933 clear_counters(&average.threads, &average.cores, &average.packages);
934
935 for_all_cpus(sum_counters, t, c, p);
936
937 average.threads.tsc /= topo.num_cpus;
938 average.threads.aperf /= topo.num_cpus;
939 average.threads.mperf /= topo.num_cpus;
940 average.threads.c1 /= topo.num_cpus;
941
Len Brown8e180f32012-09-22 01:25:08 -0400942 average.threads.extra_delta32 /= topo.num_cpus;
943 average.threads.extra_delta32 &= 0xFFFFFFFF;
944
945 average.threads.extra_delta64 /= topo.num_cpus;
946
Len Brownc98d5d92012-06-04 00:56:40 -0400947 average.cores.c3 /= topo.num_cores;
948 average.cores.c6 /= topo.num_cores;
949 average.cores.c7 /= topo.num_cores;
950
Len Brown0b2bb692015-03-26 00:50:30 -0400951 if (do_skl_residency) {
952 average.packages.pkg_wtd_core_c0 /= topo.num_packages;
953 average.packages.pkg_any_core_c0 /= topo.num_packages;
954 average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
955 average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
956 }
957
Len Brownc98d5d92012-06-04 00:56:40 -0400958 average.packages.pc2 /= topo.num_packages;
Len Brownee7e38e2015-02-09 23:39:45 -0500959 if (do_pc3)
960 average.packages.pc3 /= topo.num_packages;
961 if (do_pc6)
962 average.packages.pc6 /= topo.num_packages;
963 if (do_pc7)
964 average.packages.pc7 /= topo.num_packages;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800965
966 average.packages.pc8 /= topo.num_packages;
967 average.packages.pc9 /= topo.num_packages;
968 average.packages.pc10 /= topo.num_packages;
Len Brownc98d5d92012-06-04 00:56:40 -0400969}
970
971static unsigned long long rdtsc(void)
972{
973 unsigned int low, high;
974
975 asm volatile("rdtsc" : "=a" (low), "=d" (high));
976
977 return low | ((unsigned long long)high) << 32;
978}
979
980
981/*
982 * get_counters(...)
983 * migrate to cpu
984 * acquire and record local counters for that cpu
985 */
986int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
987{
988 int cpu = t->cpu_id;
Len Brown889facb2012-11-08 00:48:57 -0500989 unsigned long long msr;
Len Brownc98d5d92012-06-04 00:56:40 -0400990
Len Browne52966c2012-11-08 22:38:05 -0500991 if (cpu_migrate(cpu)) {
992 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
Len Brownc98d5d92012-06-04 00:56:40 -0400993 return -1;
Len Browne52966c2012-11-08 22:38:05 -0500994 }
Len Brownc98d5d92012-06-04 00:56:40 -0400995
996 t->tsc = rdtsc(); /* we are running on local CPU of interest */
997
998 if (has_aperf) {
Len Brown9c63a652012-10-31 01:29:52 -0400999 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001000 return -3;
Len Brown9c63a652012-10-31 01:29:52 -04001001 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
Len Brownc98d5d92012-06-04 00:56:40 -04001002 return -4;
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02001003 t->aperf = t->aperf * aperf_mperf_multiplier;
1004 t->mperf = t->mperf * aperf_mperf_multiplier;
Len Brownc98d5d92012-06-04 00:56:40 -04001005 }
1006
Len Brown1ed51012013-02-10 17:19:24 -05001007 if (do_smi) {
1008 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
1009 return -5;
1010 t->smi_count = msr & 0xFFFFFFFF;
1011 }
Len Brown8e180f32012-09-22 01:25:08 -04001012 if (extra_delta_offset32) {
Len Brown889facb2012-11-08 00:48:57 -05001013 if (get_msr(cpu, extra_delta_offset32, &msr))
Len Brown2f32edf2012-09-21 23:45:46 -04001014 return -5;
Len Brown889facb2012-11-08 00:48:57 -05001015 t->extra_delta32 = msr & 0xFFFFFFFF;
Len Brown8e180f32012-09-22 01:25:08 -04001016 }
1017
1018 if (extra_delta_offset64)
1019 if (get_msr(cpu, extra_delta_offset64, &t->extra_delta64))
1020 return -5;
1021
1022 if (extra_msr_offset32) {
Len Brown889facb2012-11-08 00:48:57 -05001023 if (get_msr(cpu, extra_msr_offset32, &msr))
Len Brown8e180f32012-09-22 01:25:08 -04001024 return -5;
Len Brown889facb2012-11-08 00:48:57 -05001025 t->extra_msr32 = msr & 0xFFFFFFFF;
Len Brown8e180f32012-09-22 01:25:08 -04001026 }
Len Brown2f32edf2012-09-21 23:45:46 -04001027
1028 if (extra_msr_offset64)
1029 if (get_msr(cpu, extra_msr_offset64, &t->extra_msr64))
Len Brownc98d5d92012-06-04 00:56:40 -04001030 return -5;
1031
Len Brown144b44b2013-11-09 00:30:16 -05001032 if (use_c1_residency_msr) {
1033 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1034 return -6;
1035 }
1036
Len Brownc98d5d92012-06-04 00:56:40 -04001037 /* collect core counters only for 1st thread in core */
1038 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1039 return 0;
1040
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001041 if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001042 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1043 return -6;
Len Brown144b44b2013-11-09 00:30:16 -05001044 }
1045
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001046 if (do_nhm_cstates && !do_knl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001047 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1048 return -7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001049 } else if (do_knl_cstates) {
1050 if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1051 return -7;
Len Brownc98d5d92012-06-04 00:56:40 -04001052 }
1053
1054 if (do_snb_cstates)
1055 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1056 return -8;
1057
Len Brown889facb2012-11-08 00:48:57 -05001058 if (do_dts) {
1059 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1060 return -9;
1061 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1062 }
1063
1064
Len Brownc98d5d92012-06-04 00:56:40 -04001065 /* collect package counters only for 1st core in package */
1066 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1067 return 0;
1068
Len Brown0b2bb692015-03-26 00:50:30 -04001069 if (do_skl_residency) {
1070 if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
1071 return -10;
1072 if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
1073 return -11;
1074 if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
1075 return -12;
1076 if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
1077 return -13;
1078 }
Len Brownee7e38e2015-02-09 23:39:45 -05001079 if (do_pc3)
Len Brownc98d5d92012-06-04 00:56:40 -04001080 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1081 return -9;
Len Brownee7e38e2015-02-09 23:39:45 -05001082 if (do_pc6)
Len Brownc98d5d92012-06-04 00:56:40 -04001083 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1084 return -10;
Len Brownee7e38e2015-02-09 23:39:45 -05001085 if (do_pc2)
Len Brownc98d5d92012-06-04 00:56:40 -04001086 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1087 return -11;
Len Brownee7e38e2015-02-09 23:39:45 -05001088 if (do_pc7)
Len Brownc98d5d92012-06-04 00:56:40 -04001089 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1090 return -12;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001091 if (do_c8_c9_c10) {
1092 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1093 return -13;
1094 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1095 return -13;
1096 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1097 return -13;
1098 }
Len Brown889facb2012-11-08 00:48:57 -05001099 if (do_rapl & RAPL_PKG) {
1100 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1101 return -13;
1102 p->energy_pkg = msr & 0xFFFFFFFF;
1103 }
1104 if (do_rapl & RAPL_CORES) {
1105 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1106 return -14;
1107 p->energy_cores = msr & 0xFFFFFFFF;
1108 }
1109 if (do_rapl & RAPL_DRAM) {
1110 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1111 return -15;
1112 p->energy_dram = msr & 0xFFFFFFFF;
1113 }
1114 if (do_rapl & RAPL_GFX) {
1115 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1116 return -16;
1117 p->energy_gfx = msr & 0xFFFFFFFF;
1118 }
1119 if (do_rapl & RAPL_PKG_PERF_STATUS) {
1120 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1121 return -16;
1122 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1123 }
1124 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1125 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1126 return -16;
1127 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1128 }
1129 if (do_ptm) {
1130 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1131 return -17;
1132 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1133 }
Len Brown103a8fe2010-10-22 23:53:03 -04001134 return 0;
1135}
1136
Len Brownee7e38e2015-02-09 23:39:45 -05001137/*
1138 * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1139 * If you change the values, note they are used both in comparisons
1140 * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1141 */
1142
1143#define PCLUKN 0 /* Unknown */
1144#define PCLRSV 1 /* Reserved */
1145#define PCL__0 2 /* PC0 */
1146#define PCL__1 3 /* PC1 */
1147#define PCL__2 4 /* PC2 */
1148#define PCL__3 5 /* PC3 */
1149#define PCL__4 6 /* PC4 */
1150#define PCL__6 7 /* PC6 */
1151#define PCL_6N 8 /* PC6 No Retention */
1152#define PCL_6R 9 /* PC6 Retention */
1153#define PCL__7 10 /* PC7 */
1154#define PCL_7S 11 /* PC7 Shrink */
Len Brown0b2bb692015-03-26 00:50:30 -04001155#define PCL__8 12 /* PC8 */
1156#define PCL__9 13 /* PC9 */
1157#define PCLUNL 14 /* Unlimited */
Len Brownee7e38e2015-02-09 23:39:45 -05001158
1159int pkg_cstate_limit = PCLUKN;
1160char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
Len Brown0b2bb692015-03-26 00:50:30 -04001161 "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"};
Len Brownee7e38e2015-02-09 23:39:45 -05001162
Len Browne9257f52015-04-01 21:02:57 -04001163int 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};
1164int 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};
1165int 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};
1166int 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};
1167int 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};
1168int 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 -05001169
Len Browna2b7b742015-09-26 00:12:38 -04001170
1171static void
1172calculate_tsc_tweak()
1173{
Len Browna2b7b742015-09-26 00:12:38 -04001174 tsc_tweak = base_hz / tsc_hz;
1175}
1176
Len Brownfcd17212015-03-23 20:29:09 -04001177static void
1178dump_nhm_platform_info(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001179{
1180 unsigned long long msr;
1181 unsigned int ratio;
1182
Len Brownec0adc52015-11-12 02:42:31 -05001183 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001184
Len Brownec0adc52015-11-12 02:42:31 -05001185 fprintf(stderr, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001186
Len Brown103a8fe2010-10-22 23:53:03 -04001187 ratio = (msr >> 40) & 0xFF;
Len Brown8f61f352015-03-23 18:36:57 -04001188 fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001189 ratio, bclk, ratio * bclk);
1190
1191 ratio = (msr >> 8) & 0xFF;
Len Brown8f61f352015-03-23 18:36:57 -04001192 fprintf(stderr, "%d * %.0f = %.0f MHz base frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001193 ratio, bclk, ratio * bclk);
1194
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001195 get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
Len Brownbfae2052015-06-17 12:27:21 -04001196 fprintf(stderr, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
1197 base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
Len Brown67920412013-01-31 15:22:15 -05001198
Len Brownfcd17212015-03-23 20:29:09 -04001199 return;
1200}
1201
1202static void
1203dump_hsw_turbo_ratio_limits(void)
1204{
1205 unsigned long long msr;
1206 unsigned int ratio;
1207
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001208 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04001209
Len Brownbfae2052015-06-17 12:27:21 -04001210 fprintf(stderr, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04001211
1212 ratio = (msr >> 8) & 0xFF;
1213 if (ratio)
1214 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 18 active cores\n",
1215 ratio, bclk, ratio * bclk);
1216
1217 ratio = (msr >> 0) & 0xFF;
1218 if (ratio)
1219 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 17 active cores\n",
1220 ratio, bclk, ratio * bclk);
1221 return;
1222}
1223
1224static void
1225dump_ivt_turbo_ratio_limits(void)
1226{
1227 unsigned long long msr;
1228 unsigned int ratio;
Len Brown6574a5d2012-09-21 00:01:31 -04001229
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001230 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001231
Len Brownbfae2052015-06-17 12:27:21 -04001232 fprintf(stderr, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001233
1234 ratio = (msr >> 56) & 0xFF;
1235 if (ratio)
1236 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 16 active cores\n",
1237 ratio, bclk, ratio * bclk);
1238
1239 ratio = (msr >> 48) & 0xFF;
1240 if (ratio)
1241 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 15 active cores\n",
1242 ratio, bclk, ratio * bclk);
1243
1244 ratio = (msr >> 40) & 0xFF;
1245 if (ratio)
1246 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 14 active cores\n",
1247 ratio, bclk, ratio * bclk);
1248
1249 ratio = (msr >> 32) & 0xFF;
1250 if (ratio)
1251 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 13 active cores\n",
1252 ratio, bclk, ratio * bclk);
1253
1254 ratio = (msr >> 24) & 0xFF;
1255 if (ratio)
1256 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 12 active cores\n",
1257 ratio, bclk, ratio * bclk);
1258
1259 ratio = (msr >> 16) & 0xFF;
1260 if (ratio)
1261 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 11 active cores\n",
1262 ratio, bclk, ratio * bclk);
1263
1264 ratio = (msr >> 8) & 0xFF;
1265 if (ratio)
1266 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 10 active cores\n",
1267 ratio, bclk, ratio * bclk);
1268
1269 ratio = (msr >> 0) & 0xFF;
1270 if (ratio)
1271 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 9 active cores\n",
1272 ratio, bclk, ratio * bclk);
Len Brownfcd17212015-03-23 20:29:09 -04001273 return;
1274}
Len Brown6574a5d2012-09-21 00:01:31 -04001275
Len Brownfcd17212015-03-23 20:29:09 -04001276static void
1277dump_nhm_turbo_ratio_limits(void)
1278{
1279 unsigned long long msr;
1280 unsigned int ratio;
Len Brown103a8fe2010-10-22 23:53:03 -04001281
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001282 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001283
Len Brownbfae2052015-06-17 12:27:21 -04001284 fprintf(stderr, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001285
1286 ratio = (msr >> 56) & 0xFF;
1287 if (ratio)
1288 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 8 active cores\n",
1289 ratio, bclk, ratio * bclk);
1290
1291 ratio = (msr >> 48) & 0xFF;
1292 if (ratio)
1293 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 7 active cores\n",
1294 ratio, bclk, ratio * bclk);
1295
1296 ratio = (msr >> 40) & 0xFF;
1297 if (ratio)
1298 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 6 active cores\n",
1299 ratio, bclk, ratio * bclk);
1300
1301 ratio = (msr >> 32) & 0xFF;
1302 if (ratio)
1303 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 5 active cores\n",
1304 ratio, bclk, ratio * bclk);
1305
Len Brown103a8fe2010-10-22 23:53:03 -04001306 ratio = (msr >> 24) & 0xFF;
1307 if (ratio)
1308 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
1309 ratio, bclk, ratio * bclk);
1310
1311 ratio = (msr >> 16) & 0xFF;
1312 if (ratio)
1313 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
1314 ratio, bclk, ratio * bclk);
1315
1316 ratio = (msr >> 8) & 0xFF;
1317 if (ratio)
1318 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
1319 ratio, bclk, ratio * bclk);
1320
1321 ratio = (msr >> 0) & 0xFF;
1322 if (ratio)
1323 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
1324 ratio, bclk, ratio * bclk);
Len Brownfcd17212015-03-23 20:29:09 -04001325 return;
1326}
Len Brown3a9a9412014-08-15 02:39:52 -04001327
Len Brownfcd17212015-03-23 20:29:09 -04001328static void
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001329dump_knl_turbo_ratio_limits(void)
1330{
1331 int cores;
1332 unsigned int ratio;
1333 unsigned long long msr;
1334 int delta_cores;
1335 int delta_ratio;
1336 int i;
1337
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001338 get_msr(base_cpu, MSR_NHM_TURBO_RATIO_LIMIT, &msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001339
Len Brownbfae2052015-06-17 12:27:21 -04001340 fprintf(stderr, "cpu%d: MSR_NHM_TURBO_RATIO_LIMIT: 0x%08llx\n",
1341 base_cpu, msr);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001342
1343 /**
1344 * Turbo encoding in KNL is as follows:
1345 * [7:0] -- Base value of number of active cores of bucket 1.
1346 * [15:8] -- Base value of freq ratio of bucket 1.
1347 * [20:16] -- +ve delta of number of active cores of bucket 2.
1348 * i.e. active cores of bucket 2 =
1349 * active cores of bucket 1 + delta
1350 * [23:21] -- Negative delta of freq ratio of bucket 2.
1351 * i.e. freq ratio of bucket 2 =
1352 * freq ratio of bucket 1 - delta
1353 * [28:24]-- +ve delta of number of active cores of bucket 3.
1354 * [31:29]-- -ve delta of freq ratio of bucket 3.
1355 * [36:32]-- +ve delta of number of active cores of bucket 4.
1356 * [39:37]-- -ve delta of freq ratio of bucket 4.
1357 * [44:40]-- +ve delta of number of active cores of bucket 5.
1358 * [47:45]-- -ve delta of freq ratio of bucket 5.
1359 * [52:48]-- +ve delta of number of active cores of bucket 6.
1360 * [55:53]-- -ve delta of freq ratio of bucket 6.
1361 * [60:56]-- +ve delta of number of active cores of bucket 7.
1362 * [63:61]-- -ve delta of freq ratio of bucket 7.
1363 */
1364 cores = msr & 0xFF;
1365 ratio = (msr >> 8) && 0xFF;
1366 if (ratio > 0)
1367 fprintf(stderr,
1368 "%d * %.0f = %.0f MHz max turbo %d active cores\n",
1369 ratio, bclk, ratio * bclk, cores);
1370
1371 for (i = 16; i < 64; i = i + 8) {
1372 delta_cores = (msr >> i) & 0x1F;
1373 delta_ratio = (msr >> (i + 5)) && 0x7;
1374 if (!delta_cores || !delta_ratio)
1375 return;
1376 cores = cores + delta_cores;
1377 ratio = ratio - delta_ratio;
1378
1379 /** -ve ratios will make successive ratio calculations
1380 * negative. Hence return instead of carrying on.
1381 */
1382 if (ratio > 0)
1383 fprintf(stderr,
1384 "%d * %.0f = %.0f MHz max turbo %d active cores\n",
1385 ratio, bclk, ratio * bclk, cores);
1386 }
1387}
1388
1389static void
Len Brownfcd17212015-03-23 20:29:09 -04001390dump_nhm_cst_cfg(void)
1391{
1392 unsigned long long msr;
1393
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001394 get_msr(base_cpu, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
Len Brownfcd17212015-03-23 20:29:09 -04001395
1396#define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
1397#define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
1398
Len Brownbfae2052015-06-17 12:27:21 -04001399 fprintf(stderr, "cpu%d: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", base_cpu, msr);
Len Brownfcd17212015-03-23 20:29:09 -04001400
1401 fprintf(stderr, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: %s)\n",
1402 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
1403 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
1404 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
1405 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
1406 (msr & (1 << 15)) ? "" : "UN",
1407 (unsigned int)msr & 7,
1408 pkg_cstate_limit_strings[pkg_cstate_limit]);
1409 return;
Len Brown103a8fe2010-10-22 23:53:03 -04001410}
1411
Len Brown6fb31432015-06-17 16:23:45 -04001412static void
1413dump_config_tdp(void)
1414{
1415 unsigned long long msr;
1416
1417 get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
1418 fprintf(stderr, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
1419 fprintf(stderr, " (base_ratio=%d)\n", (unsigned int)msr & 0xEF);
1420
1421 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
1422 fprintf(stderr, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
1423 if (msr) {
1424 fprintf(stderr, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0xEFFF);
1425 fprintf(stderr, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0xEFFF);
1426 fprintf(stderr, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xEF);
1427 fprintf(stderr, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0xEFFF);
1428 }
1429 fprintf(stderr, ")\n");
1430
1431 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
1432 fprintf(stderr, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
1433 if (msr) {
1434 fprintf(stderr, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0xEFFF);
1435 fprintf(stderr, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0xEFFF);
1436 fprintf(stderr, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xEF);
1437 fprintf(stderr, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0xEFFF);
1438 }
1439 fprintf(stderr, ")\n");
1440
1441 get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
1442 fprintf(stderr, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
1443 if ((msr) & 0x3)
1444 fprintf(stderr, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
1445 fprintf(stderr, " lock=%d", (unsigned int)(msr >> 31) & 1);
1446 fprintf(stderr, ")\n");
1447
1448 get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
1449 fprintf(stderr, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
Len Brown759d2a92015-10-22 02:42:12 -04001450 fprintf(stderr, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0x7F);
Len Brown6fb31432015-06-17 16:23:45 -04001451 fprintf(stderr, " lock=%d", (unsigned int)(msr >> 31) & 1);
1452 fprintf(stderr, ")\n");
1453}
1454
Len Brownc98d5d92012-06-04 00:56:40 -04001455void free_all_buffers(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001456{
Len Brownc98d5d92012-06-04 00:56:40 -04001457 CPU_FREE(cpu_present_set);
1458 cpu_present_set = NULL;
1459 cpu_present_set = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001460
Len Brownc98d5d92012-06-04 00:56:40 -04001461 CPU_FREE(cpu_affinity_set);
1462 cpu_affinity_set = NULL;
1463 cpu_affinity_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001464
Len Brownc98d5d92012-06-04 00:56:40 -04001465 free(thread_even);
1466 free(core_even);
1467 free(package_even);
1468
1469 thread_even = NULL;
1470 core_even = NULL;
1471 package_even = NULL;
1472
1473 free(thread_odd);
1474 free(core_odd);
1475 free(package_odd);
1476
1477 thread_odd = NULL;
1478 core_odd = NULL;
1479 package_odd = NULL;
1480
1481 free(output_buffer);
1482 output_buffer = NULL;
1483 outp = NULL;
Len Brown103a8fe2010-10-22 23:53:03 -04001484}
1485
Len Brownc98d5d92012-06-04 00:56:40 -04001486/*
Josh Triplett57a42a32013-08-20 17:20:17 -07001487 * Open a file, and exit on failure
1488 */
1489FILE *fopen_or_die(const char *path, const char *mode)
1490{
1491 FILE *filep = fopen(path, "r");
Josh Triplettb2c95d92013-08-20 17:20:18 -07001492 if (!filep)
1493 err(1, "%s: open failed", path);
Josh Triplett57a42a32013-08-20 17:20:17 -07001494 return filep;
1495}
1496
1497/*
Josh Triplett95aebc42013-08-20 17:20:16 -07001498 * Parse a file containing a single int.
1499 */
1500int parse_int_file(const char *fmt, ...)
1501{
1502 va_list args;
1503 char path[PATH_MAX];
1504 FILE *filep;
1505 int value;
1506
1507 va_start(args, fmt);
1508 vsnprintf(path, sizeof(path), fmt, args);
1509 va_end(args);
Josh Triplett57a42a32013-08-20 17:20:17 -07001510 filep = fopen_or_die(path, "r");
Josh Triplettb2c95d92013-08-20 17:20:18 -07001511 if (fscanf(filep, "%d", &value) != 1)
1512 err(1, "%s: failed to parse number from file", path);
Josh Triplett95aebc42013-08-20 17:20:16 -07001513 fclose(filep);
1514 return value;
1515}
1516
1517/*
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001518 * get_cpu_position_in_core(cpu)
1519 * return the position of the CPU among its HT siblings in the core
1520 * return -1 if the sibling is not in list
Len Brownc98d5d92012-06-04 00:56:40 -04001521 */
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001522int get_cpu_position_in_core(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001523{
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001524 char path[64];
1525 FILE *filep;
1526 int this_cpu;
1527 char character;
1528 int i;
1529
1530 sprintf(path,
1531 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list",
1532 cpu);
1533 filep = fopen(path, "r");
1534 if (filep == NULL) {
1535 perror(path);
1536 exit(1);
1537 }
1538
1539 for (i = 0; i < topo.num_threads_per_core; i++) {
1540 fscanf(filep, "%d", &this_cpu);
1541 if (this_cpu == cpu) {
1542 fclose(filep);
1543 return i;
1544 }
1545
1546 /* Account for no separator after last thread*/
1547 if (i != (topo.num_threads_per_core - 1))
1548 fscanf(filep, "%c", &character);
1549 }
1550
1551 fclose(filep);
1552 return -1;
Len Brown103a8fe2010-10-22 23:53:03 -04001553}
1554
Len Brownc98d5d92012-06-04 00:56:40 -04001555/*
1556 * cpu_is_first_core_in_package(cpu)
1557 * return 1 if given CPU is 1st core in package
1558 */
1559int cpu_is_first_core_in_package(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001560{
Josh Triplett95aebc42013-08-20 17:20:16 -07001561 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001562}
1563
1564int get_physical_package_id(int cpu)
1565{
Josh Triplett95aebc42013-08-20 17:20:16 -07001566 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001567}
1568
1569int get_core_id(int cpu)
1570{
Josh Triplett95aebc42013-08-20 17:20:16 -07001571 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001572}
1573
Len Brownc98d5d92012-06-04 00:56:40 -04001574int get_num_ht_siblings(int cpu)
1575{
1576 char path[80];
1577 FILE *filep;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001578 int sib1;
1579 int matches = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001580 char character;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001581 char str[100];
1582 char *ch;
Len Brownc98d5d92012-06-04 00:56:40 -04001583
1584 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
Josh Triplett57a42a32013-08-20 17:20:17 -07001585 filep = fopen_or_die(path, "r");
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001586
Len Brownc98d5d92012-06-04 00:56:40 -04001587 /*
1588 * file format:
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001589 * A ',' separated or '-' separated set of numbers
1590 * (eg 1-2 or 1,3,4,5)
Len Brownc98d5d92012-06-04 00:56:40 -04001591 */
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001592 fscanf(filep, "%d%c\n", &sib1, &character);
1593 fseek(filep, 0, SEEK_SET);
1594 fgets(str, 100, filep);
1595 ch = strchr(str, character);
1596 while (ch != NULL) {
1597 matches++;
1598 ch = strchr(ch+1, character);
1599 }
Len Brownc98d5d92012-06-04 00:56:40 -04001600
1601 fclose(filep);
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001602 return matches+1;
Len Brownc98d5d92012-06-04 00:56:40 -04001603}
1604
Len Brown103a8fe2010-10-22 23:53:03 -04001605/*
Len Brownc98d5d92012-06-04 00:56:40 -04001606 * run func(thread, core, package) in topology order
1607 * skip non-present cpus
Len Brown103a8fe2010-10-22 23:53:03 -04001608 */
1609
Len Brownc98d5d92012-06-04 00:56:40 -04001610int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
1611 struct pkg_data *, struct thread_data *, struct core_data *,
1612 struct pkg_data *), struct thread_data *thread_base,
1613 struct core_data *core_base, struct pkg_data *pkg_base,
1614 struct thread_data *thread_base2, struct core_data *core_base2,
1615 struct pkg_data *pkg_base2)
1616{
1617 int retval, pkg_no, core_no, thread_no;
1618
1619 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
1620 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
1621 for (thread_no = 0; thread_no <
1622 topo.num_threads_per_core; ++thread_no) {
1623 struct thread_data *t, *t2;
1624 struct core_data *c, *c2;
1625 struct pkg_data *p, *p2;
1626
1627 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
1628
1629 if (cpu_is_not_present(t->cpu_id))
1630 continue;
1631
1632 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
1633
1634 c = GET_CORE(core_base, core_no, pkg_no);
1635 c2 = GET_CORE(core_base2, core_no, pkg_no);
1636
1637 p = GET_PKG(pkg_base, pkg_no);
1638 p2 = GET_PKG(pkg_base2, pkg_no);
1639
1640 retval = func(t, c, p, t2, c2, p2);
1641 if (retval)
1642 return retval;
1643 }
1644 }
1645 }
1646 return 0;
1647}
1648
1649/*
1650 * run func(cpu) on every cpu in /proc/stat
1651 * return max_cpu number
1652 */
1653int for_all_proc_cpus(int (func)(int))
Len Brown103a8fe2010-10-22 23:53:03 -04001654{
1655 FILE *fp;
Len Brownc98d5d92012-06-04 00:56:40 -04001656 int cpu_num;
Len Brown103a8fe2010-10-22 23:53:03 -04001657 int retval;
1658
Josh Triplett57a42a32013-08-20 17:20:17 -07001659 fp = fopen_or_die(proc_stat, "r");
Len Brown103a8fe2010-10-22 23:53:03 -04001660
1661 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
Josh Triplettb2c95d92013-08-20 17:20:18 -07001662 if (retval != 0)
1663 err(1, "%s: failed to parse format", proc_stat);
Len Brown103a8fe2010-10-22 23:53:03 -04001664
Len Brownc98d5d92012-06-04 00:56:40 -04001665 while (1) {
1666 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 -04001667 if (retval != 1)
1668 break;
1669
Len Brownc98d5d92012-06-04 00:56:40 -04001670 retval = func(cpu_num);
1671 if (retval) {
1672 fclose(fp);
1673 return(retval);
1674 }
Len Brown103a8fe2010-10-22 23:53:03 -04001675 }
1676 fclose(fp);
Len Brownc98d5d92012-06-04 00:56:40 -04001677 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001678}
1679
1680void re_initialize(void)
1681{
Len Brownc98d5d92012-06-04 00:56:40 -04001682 free_all_buffers();
1683 setup_all_buffers();
1684 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04001685}
1686
Len Brownc98d5d92012-06-04 00:56:40 -04001687
Len Brown103a8fe2010-10-22 23:53:03 -04001688/*
Len Brownc98d5d92012-06-04 00:56:40 -04001689 * count_cpus()
1690 * remember the last one seen, it will be the max
Len Brown103a8fe2010-10-22 23:53:03 -04001691 */
Len Brownc98d5d92012-06-04 00:56:40 -04001692int count_cpus(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001693{
Len Brownc98d5d92012-06-04 00:56:40 -04001694 if (topo.max_cpu_num < cpu)
1695 topo.max_cpu_num = cpu;
Len Brown103a8fe2010-10-22 23:53:03 -04001696
Len Brownc98d5d92012-06-04 00:56:40 -04001697 topo.num_cpus += 1;
1698 return 0;
1699}
1700int mark_cpu_present(int cpu)
1701{
1702 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brown15aaa342012-03-29 22:19:58 -04001703 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001704}
1705
1706void turbostat_loop()
1707{
Len Brownc98d5d92012-06-04 00:56:40 -04001708 int retval;
Len Browne52966c2012-11-08 22:38:05 -05001709 int restarted = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001710
Len Brown103a8fe2010-10-22 23:53:03 -04001711restart:
Len Browne52966c2012-11-08 22:38:05 -05001712 restarted++;
1713
Len Brownc98d5d92012-06-04 00:56:40 -04001714 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001715 if (retval < -1) {
1716 exit(retval);
1717 } else if (retval == -1) {
Len Browne52966c2012-11-08 22:38:05 -05001718 if (restarted > 1) {
1719 exit(retval);
1720 }
Len Brownc98d5d92012-06-04 00:56:40 -04001721 re_initialize();
1722 goto restart;
1723 }
Len Browne52966c2012-11-08 22:38:05 -05001724 restarted = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001725 gettimeofday(&tv_even, (struct timezone *)NULL);
1726
1727 while (1) {
Len Brownc98d5d92012-06-04 00:56:40 -04001728 if (for_all_proc_cpus(cpu_is_not_present)) {
Len Brown103a8fe2010-10-22 23:53:03 -04001729 re_initialize();
1730 goto restart;
1731 }
Len Brown2a0609c2016-02-12 22:44:48 -05001732 nanosleep(&interval_ts, NULL);
Len Brownc98d5d92012-06-04 00:56:40 -04001733 retval = for_all_cpus(get_counters, ODD_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001734 if (retval < -1) {
1735 exit(retval);
1736 } else if (retval == -1) {
Len Brown15aaa342012-03-29 22:19:58 -04001737 re_initialize();
1738 goto restart;
1739 }
Len Brown103a8fe2010-10-22 23:53:03 -04001740 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04001741 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04001742 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
1743 compute_average(EVEN_COUNTERS);
1744 format_all_counters(EVEN_COUNTERS);
1745 flush_stdout();
Len Brown2a0609c2016-02-12 22:44:48 -05001746 nanosleep(&interval_ts, NULL);
Len Brownc98d5d92012-06-04 00:56:40 -04001747 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001748 if (retval < -1) {
1749 exit(retval);
1750 } else if (retval == -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04001751 re_initialize();
1752 goto restart;
1753 }
Len Brown103a8fe2010-10-22 23:53:03 -04001754 gettimeofday(&tv_even, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04001755 timersub(&tv_even, &tv_odd, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04001756 for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS);
1757 compute_average(ODD_COUNTERS);
1758 format_all_counters(ODD_COUNTERS);
1759 flush_stdout();
Len Brown103a8fe2010-10-22 23:53:03 -04001760 }
1761}
1762
1763void check_dev_msr()
1764{
1765 struct stat sb;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001766 char pathname[32];
Len Brown103a8fe2010-10-22 23:53:03 -04001767
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001768 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
1769 if (stat(pathname, &sb))
Len Browna21d38c2015-03-24 16:37:35 -04001770 if (system("/sbin/modprobe msr > /dev/null 2>&1"))
1771 err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
Len Brown103a8fe2010-10-22 23:53:03 -04001772}
1773
Len Brown98481e72014-08-15 00:36:50 -04001774void check_permissions()
Len Brown103a8fe2010-10-22 23:53:03 -04001775{
Len Brown98481e72014-08-15 00:36:50 -04001776 struct __user_cap_header_struct cap_header_data;
1777 cap_user_header_t cap_header = &cap_header_data;
1778 struct __user_cap_data_struct cap_data_data;
1779 cap_user_data_t cap_data = &cap_data_data;
1780 extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
1781 int do_exit = 0;
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001782 char pathname[32];
Len Brown98481e72014-08-15 00:36:50 -04001783
1784 /* check for CAP_SYS_RAWIO */
1785 cap_header->pid = getpid();
1786 cap_header->version = _LINUX_CAPABILITY_VERSION;
1787 if (capget(cap_header, cap_data) < 0)
1788 err(-6, "capget(2) failed");
1789
1790 if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
1791 do_exit++;
1792 warnx("capget(CAP_SYS_RAWIO) failed,"
1793 " try \"# setcap cap_sys_rawio=ep %s\"", progname);
1794 }
1795
1796 /* test file permissions */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001797 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
1798 if (euidaccess(pathname, R_OK)) {
Len Brown98481e72014-08-15 00:36:50 -04001799 do_exit++;
1800 warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
1801 }
1802
1803 /* if all else fails, thell them to be root */
1804 if (do_exit)
1805 if (getuid() != 0)
Len Brownd7899442015-01-23 00:12:33 -05001806 warnx("... or simply run as root");
Len Brown98481e72014-08-15 00:36:50 -04001807
1808 if (do_exit)
1809 exit(-6);
Len Brown103a8fe2010-10-22 23:53:03 -04001810}
1811
Len Brownd7899442015-01-23 00:12:33 -05001812/*
1813 * NHM adds support for additional MSRs:
1814 *
1815 * MSR_SMI_COUNT 0x00000034
1816 *
Len Brownec0adc52015-11-12 02:42:31 -05001817 * MSR_PLATFORM_INFO 0x000000ce
Len Brownd7899442015-01-23 00:12:33 -05001818 * MSR_NHM_SNB_PKG_CST_CFG_CTL 0x000000e2
1819 *
1820 * MSR_PKG_C3_RESIDENCY 0x000003f8
1821 * MSR_PKG_C6_RESIDENCY 0x000003f9
1822 * MSR_CORE_C3_RESIDENCY 0x000003fc
1823 * MSR_CORE_C6_RESIDENCY 0x000003fd
1824 *
Len Brownee7e38e2015-02-09 23:39:45 -05001825 * Side effect:
1826 * sets global pkg_cstate_limit to decode MSR_NHM_SNB_PKG_CST_CFG_CTL
Len Brownd7899442015-01-23 00:12:33 -05001827 */
Len Brownee7e38e2015-02-09 23:39:45 -05001828int probe_nhm_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04001829{
Len Brownee7e38e2015-02-09 23:39:45 -05001830 unsigned long long msr;
Len Brown21ed5572015-10-19 22:37:40 -04001831 unsigned int base_ratio;
Len Brownee7e38e2015-02-09 23:39:45 -05001832 int *pkg_cstate_limits;
1833
Len Brown103a8fe2010-10-22 23:53:03 -04001834 if (!genuine_intel)
1835 return 0;
1836
1837 if (family != 6)
1838 return 0;
1839
Len Brown21ed5572015-10-19 22:37:40 -04001840 bclk = discover_bclk(family, model);
1841
Len Brown103a8fe2010-10-22 23:53:03 -04001842 switch (model) {
1843 case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
1844 case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
1845 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
1846 case 0x25: /* Westmere Client - Clarkdale, Arrandale */
1847 case 0x2C: /* Westmere EP - Gulftown */
Len Brownee7e38e2015-02-09 23:39:45 -05001848 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1849 case 0x2F: /* Westmere-EX Xeon - Eagleton */
1850 pkg_cstate_limits = nhm_pkg_cstate_limits;
1851 break;
Len Brown103a8fe2010-10-22 23:53:03 -04001852 case 0x2A: /* SNB */
1853 case 0x2D: /* SNB Xeon */
Len Brown553575f2011-11-18 03:32:01 -05001854 case 0x3A: /* IVB */
Len Brown13006512012-09-26 18:11:31 -04001855 case 0x3E: /* IVB Xeon */
Len Brownee7e38e2015-02-09 23:39:45 -05001856 pkg_cstate_limits = snb_pkg_cstate_limits;
1857 break;
Len Brown70b43402013-01-08 01:26:07 -05001858 case 0x3C: /* HSW */
Len Browne6f9bb32013-12-03 02:19:19 -05001859 case 0x3F: /* HSX */
Len Brown70b43402013-01-08 01:26:07 -05001860 case 0x45: /* HSW */
Len Brown149c2312013-03-15 10:58:02 -04001861 case 0x46: /* HSW */
Len Brown4e8e863f2014-02-27 23:28:53 -05001862 case 0x3D: /* BDW */
Len Brown48a06312015-02-10 15:38:04 -05001863 case 0x47: /* BDW */
Len Brown4e8e863f2014-02-27 23:28:53 -05001864 case 0x4F: /* BDX */
1865 case 0x56: /* BDX-DE */
Len Brown0b2bb692015-03-26 00:50:30 -04001866 case 0x4E: /* SKL */
1867 case 0x5E: /* SKL */
Len Brownee7e38e2015-02-09 23:39:45 -05001868 pkg_cstate_limits = hsw_pkg_cstate_limits;
1869 break;
1870 case 0x37: /* BYT */
1871 case 0x4D: /* AVN */
1872 pkg_cstate_limits = slv_pkg_cstate_limits;
1873 break;
1874 case 0x4C: /* AMT */
1875 pkg_cstate_limits = amt_pkg_cstate_limits;
1876 break;
1877 case 0x57: /* PHI */
1878 pkg_cstate_limits = phi_pkg_cstate_limits;
1879 break;
Len Brown103a8fe2010-10-22 23:53:03 -04001880 default:
1881 return 0;
1882 }
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04001883 get_msr(base_cpu, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
Len Browne9257f52015-04-01 21:02:57 -04001884 pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
Len Brownee7e38e2015-02-09 23:39:45 -05001885
Len Brownec0adc52015-11-12 02:42:31 -05001886 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
Len Brown21ed5572015-10-19 22:37:40 -04001887 base_ratio = (msr >> 8) & 0xFF;
1888
1889 base_hz = base_ratio * bclk * 1000000;
1890 has_base_hz = 1;
Len Brownee7e38e2015-02-09 23:39:45 -05001891 return 1;
Len Brown103a8fe2010-10-22 23:53:03 -04001892}
Len Brownd7899442015-01-23 00:12:33 -05001893int has_nhm_turbo_ratio_limit(unsigned int family, unsigned int model)
1894{
Len Brownd7899442015-01-23 00:12:33 -05001895 switch (model) {
1896 /* Nehalem compatible, but do not include turbo-ratio limit support */
1897 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1898 case 0x2F: /* Westmere-EX Xeon - Eagleton */
1899 return 0;
1900 default:
1901 return 1;
1902 }
1903}
Len Brown6574a5d2012-09-21 00:01:31 -04001904int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
1905{
1906 if (!genuine_intel)
1907 return 0;
1908
1909 if (family != 6)
1910 return 0;
1911
1912 switch (model) {
1913 case 0x3E: /* IVB Xeon */
Len Brownfcd17212015-03-23 20:29:09 -04001914 case 0x3F: /* HSW Xeon */
Len Brown6574a5d2012-09-21 00:01:31 -04001915 return 1;
1916 default:
1917 return 0;
1918 }
1919}
Len Brownfcd17212015-03-23 20:29:09 -04001920int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
1921{
1922 if (!genuine_intel)
1923 return 0;
1924
1925 if (family != 6)
1926 return 0;
1927
1928 switch (model) {
1929 case 0x3F: /* HSW Xeon */
1930 return 1;
1931 default:
1932 return 0;
1933 }
1934}
1935
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001936int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
1937{
1938 if (!genuine_intel)
1939 return 0;
1940
1941 if (family != 6)
1942 return 0;
1943
1944 switch (model) {
1945 case 0x57: /* Knights Landing */
1946 return 1;
1947 default:
1948 return 0;
1949 }
1950}
Len Brown6fb31432015-06-17 16:23:45 -04001951int has_config_tdp(unsigned int family, unsigned int model)
1952{
1953 if (!genuine_intel)
1954 return 0;
1955
1956 if (family != 6)
1957 return 0;
1958
1959 switch (model) {
1960 case 0x3A: /* IVB */
Len Brown6fb31432015-06-17 16:23:45 -04001961 case 0x3C: /* HSW */
1962 case 0x3F: /* HSX */
1963 case 0x45: /* HSW */
1964 case 0x46: /* HSW */
1965 case 0x3D: /* BDW */
1966 case 0x47: /* BDW */
1967 case 0x4F: /* BDX */
1968 case 0x56: /* BDX-DE */
1969 case 0x4E: /* SKL */
1970 case 0x5E: /* SKL */
1971
1972 case 0x57: /* Knights Landing */
1973 return 1;
1974 default:
1975 return 0;
1976 }
1977}
1978
Len Brownfcd17212015-03-23 20:29:09 -04001979static void
1980dump_cstate_pstate_config_info(family, model)
1981{
1982 if (!do_nhm_platform_info)
1983 return;
1984
1985 dump_nhm_platform_info();
1986
1987 if (has_hsw_turbo_ratio_limit(family, model))
1988 dump_hsw_turbo_ratio_limits();
1989
1990 if (has_ivt_turbo_ratio_limit(family, model))
1991 dump_ivt_turbo_ratio_limits();
1992
1993 if (has_nhm_turbo_ratio_limit(family, model))
1994 dump_nhm_turbo_ratio_limits();
1995
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001996 if (has_knl_turbo_ratio_limit(family, model))
1997 dump_knl_turbo_ratio_limits();
1998
Len Brown6fb31432015-06-17 16:23:45 -04001999 if (has_config_tdp(family, model))
2000 dump_config_tdp();
2001
Len Brownfcd17212015-03-23 20:29:09 -04002002 dump_nhm_cst_cfg();
2003}
2004
Len Brown6574a5d2012-09-21 00:01:31 -04002005
Len Brown889facb2012-11-08 00:48:57 -05002006/*
2007 * print_epb()
2008 * Decode the ENERGY_PERF_BIAS MSR
2009 */
2010int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2011{
2012 unsigned long long msr;
2013 char *epb_string;
2014 int cpu;
2015
2016 if (!has_epb)
2017 return 0;
2018
2019 cpu = t->cpu_id;
2020
2021 /* EPB is per-package */
2022 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2023 return 0;
2024
2025 if (cpu_migrate(cpu)) {
2026 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
2027 return -1;
2028 }
2029
2030 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
2031 return 0;
2032
Len Browne9be7dd2015-05-26 12:19:37 -04002033 switch (msr & 0xF) {
Len Brown889facb2012-11-08 00:48:57 -05002034 case ENERGY_PERF_BIAS_PERFORMANCE:
2035 epb_string = "performance";
2036 break;
2037 case ENERGY_PERF_BIAS_NORMAL:
2038 epb_string = "balanced";
2039 break;
2040 case ENERGY_PERF_BIAS_POWERSAVE:
2041 epb_string = "powersave";
2042 break;
2043 default:
2044 epb_string = "custom";
2045 break;
2046 }
2047 fprintf(stderr, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
2048
2049 return 0;
2050}
Len Brown7f5c2582015-12-01 01:36:39 -05002051/*
2052 * print_hwp()
2053 * Decode the MSR_HWP_CAPABILITIES
2054 */
2055int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2056{
2057 unsigned long long msr;
2058 int cpu;
2059
2060 if (!has_hwp)
2061 return 0;
2062
2063 cpu = t->cpu_id;
2064
2065 /* MSR_HWP_CAPABILITIES is per-package */
2066 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2067 return 0;
2068
2069 if (cpu_migrate(cpu)) {
2070 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
2071 return -1;
2072 }
2073
2074 if (get_msr(cpu, MSR_PM_ENABLE, &msr))
2075 return 0;
2076
2077 fprintf(stderr, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n",
2078 cpu, msr, (msr & (1 << 0)) ? "" : "No-");
2079
2080 /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
2081 if ((msr & (1 << 0)) == 0)
2082 return 0;
2083
2084 if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
2085 return 0;
2086
2087 fprintf(stderr, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
2088 "(high 0x%x guar 0x%x eff 0x%x low 0x%x)\n",
2089 cpu, msr,
2090 (unsigned int)HWP_HIGHEST_PERF(msr),
2091 (unsigned int)HWP_GUARANTEED_PERF(msr),
2092 (unsigned int)HWP_MOSTEFFICIENT_PERF(msr),
2093 (unsigned int)HWP_LOWEST_PERF(msr));
2094
2095 if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
2096 return 0;
2097
2098 fprintf(stderr, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
2099 "(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x pkg 0x%x)\n",
2100 cpu, msr,
2101 (unsigned int)(((msr) >> 0) & 0xff),
2102 (unsigned int)(((msr) >> 8) & 0xff),
2103 (unsigned int)(((msr) >> 16) & 0xff),
2104 (unsigned int)(((msr) >> 24) & 0xff),
2105 (unsigned int)(((msr) >> 32) & 0xff3),
2106 (unsigned int)(((msr) >> 42) & 0x1));
2107
2108 if (has_hwp_pkg) {
2109 if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
2110 return 0;
2111
2112 fprintf(stderr, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
2113 "(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x)\n",
2114 cpu, msr,
2115 (unsigned int)(((msr) >> 0) & 0xff),
2116 (unsigned int)(((msr) >> 8) & 0xff),
2117 (unsigned int)(((msr) >> 16) & 0xff),
2118 (unsigned int)(((msr) >> 24) & 0xff),
2119 (unsigned int)(((msr) >> 32) & 0xff3));
2120 }
2121 if (has_hwp_notify) {
2122 if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
2123 return 0;
2124
2125 fprintf(stderr, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
2126 "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
2127 cpu, msr,
2128 ((msr) & 0x1) ? "EN" : "Dis",
2129 ((msr) & 0x2) ? "EN" : "Dis");
2130 }
2131 if (get_msr(cpu, MSR_HWP_STATUS, &msr))
2132 return 0;
2133
2134 fprintf(stderr, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
2135 "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
2136 cpu, msr,
2137 ((msr) & 0x1) ? "" : "No-",
2138 ((msr) & 0x2) ? "" : "No-");
2139
2140 return 0;
2141}
Len Brown889facb2012-11-08 00:48:57 -05002142
Len Brown3a9a9412014-08-15 02:39:52 -04002143/*
2144 * print_perf_limit()
2145 */
2146int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2147{
2148 unsigned long long msr;
2149 int cpu;
2150
2151 cpu = t->cpu_id;
2152
2153 /* per-package */
2154 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2155 return 0;
2156
2157 if (cpu_migrate(cpu)) {
2158 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
2159 return -1;
2160 }
2161
2162 if (do_core_perf_limit_reasons) {
2163 get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
2164 fprintf(stderr, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2165 fprintf(stderr, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
Len Browne33cbe82015-03-13 16:30:57 -04002166 (msr & 1 << 15) ? "bit15, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04002167 (msr & 1 << 14) ? "bit14, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04002168 (msr & 1 << 13) ? "Transitions, " : "",
2169 (msr & 1 << 12) ? "MultiCoreTurbo, " : "",
2170 (msr & 1 << 11) ? "PkgPwrL2, " : "",
2171 (msr & 1 << 10) ? "PkgPwrL1, " : "",
2172 (msr & 1 << 9) ? "CorePwr, " : "",
2173 (msr & 1 << 8) ? "Amps, " : "",
2174 (msr & 1 << 6) ? "VR-Therm, " : "",
2175 (msr & 1 << 5) ? "Auto-HWP, " : "",
2176 (msr & 1 << 4) ? "Graphics, " : "",
2177 (msr & 1 << 2) ? "bit2, " : "",
2178 (msr & 1 << 1) ? "ThermStatus, " : "",
2179 (msr & 1 << 0) ? "PROCHOT, " : "");
Len Brown3a9a9412014-08-15 02:39:52 -04002180 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 -04002181 (msr & 1 << 31) ? "bit31, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04002182 (msr & 1 << 30) ? "bit30, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04002183 (msr & 1 << 29) ? "Transitions, " : "",
2184 (msr & 1 << 28) ? "MultiCoreTurbo, " : "",
2185 (msr & 1 << 27) ? "PkgPwrL2, " : "",
2186 (msr & 1 << 26) ? "PkgPwrL1, " : "",
2187 (msr & 1 << 25) ? "CorePwr, " : "",
2188 (msr & 1 << 24) ? "Amps, " : "",
2189 (msr & 1 << 22) ? "VR-Therm, " : "",
2190 (msr & 1 << 21) ? "Auto-HWP, " : "",
2191 (msr & 1 << 20) ? "Graphics, " : "",
2192 (msr & 1 << 18) ? "bit18, " : "",
2193 (msr & 1 << 17) ? "ThermStatus, " : "",
2194 (msr & 1 << 16) ? "PROCHOT, " : "");
Len Brown3a9a9412014-08-15 02:39:52 -04002195
2196 }
2197 if (do_gfx_perf_limit_reasons) {
2198 get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
2199 fprintf(stderr, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2200 fprintf(stderr, " (Active: %s%s%s%s%s%s%s%s)",
2201 (msr & 1 << 0) ? "PROCHOT, " : "",
2202 (msr & 1 << 1) ? "ThermStatus, " : "",
2203 (msr & 1 << 4) ? "Graphics, " : "",
2204 (msr & 1 << 6) ? "VR-Therm, " : "",
2205 (msr & 1 << 8) ? "Amps, " : "",
2206 (msr & 1 << 9) ? "GFXPwr, " : "",
2207 (msr & 1 << 10) ? "PkgPwrL1, " : "",
2208 (msr & 1 << 11) ? "PkgPwrL2, " : "");
2209 fprintf(stderr, " (Logged: %s%s%s%s%s%s%s%s)\n",
2210 (msr & 1 << 16) ? "PROCHOT, " : "",
2211 (msr & 1 << 17) ? "ThermStatus, " : "",
2212 (msr & 1 << 20) ? "Graphics, " : "",
2213 (msr & 1 << 22) ? "VR-Therm, " : "",
2214 (msr & 1 << 24) ? "Amps, " : "",
2215 (msr & 1 << 25) ? "GFXPwr, " : "",
2216 (msr & 1 << 26) ? "PkgPwrL1, " : "",
2217 (msr & 1 << 27) ? "PkgPwrL2, " : "");
2218 }
2219 if (do_ring_perf_limit_reasons) {
2220 get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
2221 fprintf(stderr, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2222 fprintf(stderr, " (Active: %s%s%s%s%s%s)",
2223 (msr & 1 << 0) ? "PROCHOT, " : "",
2224 (msr & 1 << 1) ? "ThermStatus, " : "",
2225 (msr & 1 << 6) ? "VR-Therm, " : "",
2226 (msr & 1 << 8) ? "Amps, " : "",
2227 (msr & 1 << 10) ? "PkgPwrL1, " : "",
2228 (msr & 1 << 11) ? "PkgPwrL2, " : "");
2229 fprintf(stderr, " (Logged: %s%s%s%s%s%s)\n",
2230 (msr & 1 << 16) ? "PROCHOT, " : "",
2231 (msr & 1 << 17) ? "ThermStatus, " : "",
2232 (msr & 1 << 22) ? "VR-Therm, " : "",
2233 (msr & 1 << 24) ? "Amps, " : "",
2234 (msr & 1 << 26) ? "PkgPwrL1, " : "",
2235 (msr & 1 << 27) ? "PkgPwrL2, " : "");
2236 }
2237 return 0;
2238}
2239
Len Brown889facb2012-11-08 00:48:57 -05002240#define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
2241#define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
2242
Len Brown144b44b2013-11-09 00:30:16 -05002243double get_tdp(model)
2244{
2245 unsigned long long msr;
2246
2247 if (do_rapl & RAPL_PKG_POWER_INFO)
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002248 if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
Len Brown144b44b2013-11-09 00:30:16 -05002249 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
2250
2251 switch (model) {
2252 case 0x37:
2253 case 0x4D:
2254 return 30.0;
2255 default:
2256 return 135.0;
2257 }
2258}
2259
Andrey Semin40ee8e32014-12-05 00:07:00 -05002260/*
2261 * rapl_dram_energy_units_probe()
2262 * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
2263 */
2264static double
2265rapl_dram_energy_units_probe(int model, double rapl_energy_units)
2266{
2267 /* only called for genuine_intel, family 6 */
2268
2269 switch (model) {
2270 case 0x3F: /* HSX */
2271 case 0x4F: /* BDX */
2272 case 0x56: /* BDX-DE */
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002273 case 0x57: /* KNL */
Andrey Semin40ee8e32014-12-05 00:07:00 -05002274 return (rapl_dram_energy_units = 15.3 / 1000000);
2275 default:
2276 return (rapl_energy_units);
2277 }
2278}
2279
Len Brown144b44b2013-11-09 00:30:16 -05002280
Len Brown889facb2012-11-08 00:48:57 -05002281/*
2282 * rapl_probe()
2283 *
Len Brown144b44b2013-11-09 00:30:16 -05002284 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
Len Brown889facb2012-11-08 00:48:57 -05002285 */
2286void rapl_probe(unsigned int family, unsigned int model)
2287{
2288 unsigned long long msr;
Len Brown144b44b2013-11-09 00:30:16 -05002289 unsigned int time_unit;
Len Brown889facb2012-11-08 00:48:57 -05002290 double tdp;
2291
2292 if (!genuine_intel)
2293 return;
2294
2295 if (family != 6)
2296 return;
2297
2298 switch (model) {
2299 case 0x2A:
2300 case 0x3A:
Len Brown70b43402013-01-08 01:26:07 -05002301 case 0x3C: /* HSW */
Len Brown70b43402013-01-08 01:26:07 -05002302 case 0x45: /* HSW */
Len Brown149c2312013-03-15 10:58:02 -04002303 case 0x46: /* HSW */
Len Brown4e8e863f2014-02-27 23:28:53 -05002304 case 0x3D: /* BDW */
Len Brown48a06312015-02-10 15:38:04 -05002305 case 0x47: /* BDW */
Len Brown144b44b2013-11-09 00:30:16 -05002306 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
Len Brown889facb2012-11-08 00:48:57 -05002307 break;
Len Brown0b2bb692015-03-26 00:50:30 -04002308 case 0x4E: /* SKL */
2309 case 0x5E: /* SKL */
2310 do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
2311 break;
Len Browne6f9bb32013-12-03 02:19:19 -05002312 case 0x3F: /* HSX */
Len Brown4e8e863f2014-02-27 23:28:53 -05002313 case 0x4F: /* BDX */
2314 case 0x56: /* BDX-DE */
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002315 case 0x57: /* KNL */
Len Brown0b2bb692015-03-26 00:50:30 -04002316 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 -05002317 break;
Len Brown889facb2012-11-08 00:48:57 -05002318 case 0x2D:
2319 case 0x3E:
Len Brown0b2bb692015-03-26 00:50:30 -04002320 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 -05002321 break;
2322 case 0x37: /* BYT */
2323 case 0x4D: /* AVN */
2324 do_rapl = RAPL_PKG | RAPL_CORES ;
Len Brown889facb2012-11-08 00:48:57 -05002325 break;
2326 default:
2327 return;
2328 }
2329
2330 /* units on package 0, verify later other packages match */
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002331 if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
Len Brown889facb2012-11-08 00:48:57 -05002332 return;
2333
2334 rapl_power_units = 1.0 / (1 << (msr & 0xF));
Len Brown144b44b2013-11-09 00:30:16 -05002335 if (model == 0x37)
2336 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
2337 else
2338 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
Len Brown889facb2012-11-08 00:48:57 -05002339
Andrey Semin40ee8e32014-12-05 00:07:00 -05002340 rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
2341
Len Brown144b44b2013-11-09 00:30:16 -05002342 time_unit = msr >> 16 & 0xF;
2343 if (time_unit == 0)
2344 time_unit = 0xA;
Len Brown889facb2012-11-08 00:48:57 -05002345
Len Brown144b44b2013-11-09 00:30:16 -05002346 rapl_time_units = 1.0 / (1 << (time_unit));
2347
2348 tdp = get_tdp(model);
Len Brown889facb2012-11-08 00:48:57 -05002349
2350 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
Len Brownd8af6f52015-02-10 01:56:38 -05002351 if (debug)
Len Brown144b44b2013-11-09 00:30:16 -05002352 fprintf(stderr, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
Len Brown889facb2012-11-08 00:48:57 -05002353
2354 return;
2355}
2356
Len Brown3a9a9412014-08-15 02:39:52 -04002357void perf_limit_reasons_probe(family, model)
2358{
2359 if (!genuine_intel)
2360 return;
2361
2362 if (family != 6)
2363 return;
2364
2365 switch (model) {
2366 case 0x3C: /* HSW */
2367 case 0x45: /* HSW */
2368 case 0x46: /* HSW */
2369 do_gfx_perf_limit_reasons = 1;
2370 case 0x3F: /* HSX */
2371 do_core_perf_limit_reasons = 1;
2372 do_ring_perf_limit_reasons = 1;
2373 default:
2374 return;
2375 }
2376}
2377
Len Brown889facb2012-11-08 00:48:57 -05002378int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2379{
2380 unsigned long long msr;
2381 unsigned int dts;
2382 int cpu;
2383
2384 if (!(do_dts || do_ptm))
2385 return 0;
2386
2387 cpu = t->cpu_id;
2388
2389 /* DTS is per-core, no need to print for each thread */
2390 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
2391 return 0;
2392
2393 if (cpu_migrate(cpu)) {
2394 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
2395 return -1;
2396 }
2397
2398 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
2399 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
2400 return 0;
2401
2402 dts = (msr >> 16) & 0x7F;
2403 fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
2404 cpu, msr, tcc_activation_temp - dts);
2405
2406#ifdef THERM_DEBUG
2407 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
2408 return 0;
2409
2410 dts = (msr >> 16) & 0x7F;
2411 dts2 = (msr >> 8) & 0x7F;
2412 fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
2413 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
2414#endif
2415 }
2416
2417
2418 if (do_dts) {
2419 unsigned int resolution;
2420
2421 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
2422 return 0;
2423
2424 dts = (msr >> 16) & 0x7F;
2425 resolution = (msr >> 27) & 0xF;
2426 fprintf(stderr, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
2427 cpu, msr, tcc_activation_temp - dts, resolution);
2428
2429#ifdef THERM_DEBUG
2430 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
2431 return 0;
2432
2433 dts = (msr >> 16) & 0x7F;
2434 dts2 = (msr >> 8) & 0x7F;
2435 fprintf(stderr, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
2436 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
2437#endif
2438 }
2439
2440 return 0;
2441}
2442
2443void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
2444{
2445 fprintf(stderr, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
2446 cpu, label,
2447 ((msr >> 15) & 1) ? "EN" : "DIS",
2448 ((msr >> 0) & 0x7FFF) * rapl_power_units,
2449 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
2450 (((msr >> 16) & 1) ? "EN" : "DIS"));
2451
2452 return;
2453}
2454
2455int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2456{
2457 unsigned long long msr;
2458 int cpu;
Len Brown889facb2012-11-08 00:48:57 -05002459
2460 if (!do_rapl)
2461 return 0;
2462
2463 /* RAPL counters are per package, so print only for 1st thread/package */
2464 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2465 return 0;
2466
2467 cpu = t->cpu_id;
2468 if (cpu_migrate(cpu)) {
2469 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
2470 return -1;
2471 }
2472
2473 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
2474 return -1;
2475
Len Brownd8af6f52015-02-10 01:56:38 -05002476 if (debug) {
Len Brown889facb2012-11-08 00:48:57 -05002477 fprintf(stderr, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx "
2478 "(%f Watts, %f Joules, %f sec.)\n", cpu, msr,
Len Brown144b44b2013-11-09 00:30:16 -05002479 rapl_power_units, rapl_energy_units, rapl_time_units);
Len Brown889facb2012-11-08 00:48:57 -05002480 }
Len Brown144b44b2013-11-09 00:30:16 -05002481 if (do_rapl & RAPL_PKG_POWER_INFO) {
2482
Len Brown889facb2012-11-08 00:48:57 -05002483 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
2484 return -5;
2485
2486
2487 fprintf(stderr, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
2488 cpu, msr,
2489 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2490 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2491 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2492 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
2493
Len Brown144b44b2013-11-09 00:30:16 -05002494 }
2495 if (do_rapl & RAPL_PKG) {
2496
Len Brown889facb2012-11-08 00:48:57 -05002497 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
2498 return -9;
2499
2500 fprintf(stderr, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
2501 cpu, msr, (msr >> 63) & 1 ? "": "UN");
2502
2503 print_power_limit_msr(cpu, msr, "PKG Limit #1");
2504 fprintf(stderr, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
2505 cpu,
2506 ((msr >> 47) & 1) ? "EN" : "DIS",
2507 ((msr >> 32) & 0x7FFF) * rapl_power_units,
2508 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
2509 ((msr >> 48) & 1) ? "EN" : "DIS");
2510 }
2511
Len Brown0b2bb692015-03-26 00:50:30 -04002512 if (do_rapl & RAPL_DRAM_POWER_INFO) {
Len Brown889facb2012-11-08 00:48:57 -05002513 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
2514 return -6;
2515
Len Brown889facb2012-11-08 00:48:57 -05002516 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
2517 cpu, msr,
2518 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2519 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2520 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2521 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
Len Brown0b2bb692015-03-26 00:50:30 -04002522 }
2523 if (do_rapl & RAPL_DRAM) {
Len Brown889facb2012-11-08 00:48:57 -05002524 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
2525 return -9;
2526 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
2527 cpu, msr, (msr >> 31) & 1 ? "": "UN");
2528
2529 print_power_limit_msr(cpu, msr, "DRAM Limit");
2530 }
Len Brown144b44b2013-11-09 00:30:16 -05002531 if (do_rapl & RAPL_CORE_POLICY) {
Len Brownd8af6f52015-02-10 01:56:38 -05002532 if (debug) {
Len Brown889facb2012-11-08 00:48:57 -05002533 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
2534 return -7;
2535
2536 fprintf(stderr, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown144b44b2013-11-09 00:30:16 -05002537 }
2538 }
2539 if (do_rapl & RAPL_CORES) {
Len Brownd8af6f52015-02-10 01:56:38 -05002540 if (debug) {
Len Brown889facb2012-11-08 00:48:57 -05002541
2542 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
2543 return -9;
2544 fprintf(stderr, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
2545 cpu, msr, (msr >> 31) & 1 ? "": "UN");
2546 print_power_limit_msr(cpu, msr, "Cores Limit");
2547 }
2548 }
2549 if (do_rapl & RAPL_GFX) {
Len Brownd8af6f52015-02-10 01:56:38 -05002550 if (debug) {
Len Brown889facb2012-11-08 00:48:57 -05002551 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
2552 return -8;
2553
2554 fprintf(stderr, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
2555
2556 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
2557 return -9;
2558 fprintf(stderr, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
2559 cpu, msr, (msr >> 31) & 1 ? "": "UN");
2560 print_power_limit_msr(cpu, msr, "GFX Limit");
2561 }
2562 }
2563 return 0;
2564}
2565
Len Brownd7899442015-01-23 00:12:33 -05002566/*
2567 * SNB adds support for additional MSRs:
2568 *
2569 * MSR_PKG_C7_RESIDENCY 0x000003fa
2570 * MSR_CORE_C7_RESIDENCY 0x000003fe
2571 * MSR_PKG_C2_RESIDENCY 0x0000060d
2572 */
Len Brown103a8fe2010-10-22 23:53:03 -04002573
Len Brownd7899442015-01-23 00:12:33 -05002574int has_snb_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04002575{
2576 if (!genuine_intel)
2577 return 0;
2578
2579 switch (model) {
2580 case 0x2A:
2581 case 0x2D:
Len Brown650a37f2012-06-03 23:34:44 -04002582 case 0x3A: /* IVB */
Len Brown13006512012-09-26 18:11:31 -04002583 case 0x3E: /* IVB Xeon */
Len Brown70b43402013-01-08 01:26:07 -05002584 case 0x3C: /* HSW */
2585 case 0x3F: /* HSW */
2586 case 0x45: /* HSW */
Len Brown149c2312013-03-15 10:58:02 -04002587 case 0x46: /* HSW */
Len Brown4e8e863f2014-02-27 23:28:53 -05002588 case 0x3D: /* BDW */
Len Brown48a06312015-02-10 15:38:04 -05002589 case 0x47: /* BDW */
Len Brown4e8e863f2014-02-27 23:28:53 -05002590 case 0x4F: /* BDX */
2591 case 0x56: /* BDX-DE */
Len Brown0b2bb692015-03-26 00:50:30 -04002592 case 0x4E: /* SKL */
2593 case 0x5E: /* SKL */
Len Brown103a8fe2010-10-22 23:53:03 -04002594 return 1;
2595 }
2596 return 0;
2597}
2598
Len Brownd7899442015-01-23 00:12:33 -05002599/*
2600 * HSW adds support for additional MSRs:
2601 *
2602 * MSR_PKG_C8_RESIDENCY 0x00000630
2603 * MSR_PKG_C9_RESIDENCY 0x00000631
2604 * MSR_PKG_C10_RESIDENCY 0x00000632
2605 */
2606int has_hsw_msrs(unsigned int family, unsigned int model)
Kristen Carlson Accardica587102012-11-21 05:22:43 -08002607{
2608 if (!genuine_intel)
2609 return 0;
2610
2611 switch (model) {
Len Brown4e8e863f2014-02-27 23:28:53 -05002612 case 0x45: /* HSW */
2613 case 0x3D: /* BDW */
Len Brown0b2bb692015-03-26 00:50:30 -04002614 case 0x4E: /* SKL */
2615 case 0x5E: /* SKL */
Kristen Carlson Accardica587102012-11-21 05:22:43 -08002616 return 1;
2617 }
2618 return 0;
2619}
2620
Len Brown0b2bb692015-03-26 00:50:30 -04002621/*
2622 * SKL adds support for additional MSRS:
2623 *
2624 * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658
2625 * MSR_PKG_ANY_CORE_C0_RES 0x00000659
2626 * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A
2627 * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B
2628 */
2629int has_skl_msrs(unsigned int family, unsigned int model)
2630{
2631 if (!genuine_intel)
2632 return 0;
2633
2634 switch (model) {
2635 case 0x4E: /* SKL */
2636 case 0x5E: /* SKL */
2637 return 1;
2638 }
2639 return 0;
2640}
2641
2642
Kristen Carlson Accardica587102012-11-21 05:22:43 -08002643
Len Brown144b44b2013-11-09 00:30:16 -05002644int is_slm(unsigned int family, unsigned int model)
2645{
2646 if (!genuine_intel)
2647 return 0;
2648 switch (model) {
2649 case 0x37: /* BYT */
2650 case 0x4D: /* AVN */
2651 return 1;
2652 }
2653 return 0;
2654}
2655
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002656int is_knl(unsigned int family, unsigned int model)
2657{
2658 if (!genuine_intel)
2659 return 0;
2660 switch (model) {
2661 case 0x57: /* KNL */
2662 return 1;
2663 }
2664 return 0;
2665}
2666
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02002667unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
2668{
2669 if (is_knl(family, model))
2670 return 1024;
2671 return 1;
2672}
2673
Len Brown144b44b2013-11-09 00:30:16 -05002674#define SLM_BCLK_FREQS 5
2675double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
2676
2677double slm_bclk(void)
2678{
2679 unsigned long long msr = 3;
2680 unsigned int i;
2681 double freq;
2682
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002683 if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
Len Brown144b44b2013-11-09 00:30:16 -05002684 fprintf(stderr, "SLM BCLK: unknown\n");
2685
2686 i = msr & 0xf;
2687 if (i >= SLM_BCLK_FREQS) {
2688 fprintf(stderr, "SLM BCLK[%d] invalid\n", i);
2689 msr = 3;
2690 }
2691 freq = slm_freq_table[i];
2692
2693 fprintf(stderr, "SLM BCLK: %.1f Mhz\n", freq);
2694
2695 return freq;
2696}
2697
Len Brown103a8fe2010-10-22 23:53:03 -04002698double discover_bclk(unsigned int family, unsigned int model)
2699{
Chrzaniuk, Hubert121b48b2016-02-10 16:35:17 +01002700 if (has_snb_msrs(family, model) || is_knl(family, model))
Len Brown103a8fe2010-10-22 23:53:03 -04002701 return 100.00;
Len Brown144b44b2013-11-09 00:30:16 -05002702 else if (is_slm(family, model))
2703 return slm_bclk();
Len Brown103a8fe2010-10-22 23:53:03 -04002704 else
2705 return 133.33;
2706}
2707
Len Brown889facb2012-11-08 00:48:57 -05002708/*
2709 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
2710 * the Thermal Control Circuit (TCC) activates.
2711 * This is usually equal to tjMax.
2712 *
2713 * Older processors do not have this MSR, so there we guess,
2714 * but also allow cmdline over-ride with -T.
2715 *
2716 * Several MSR temperature values are in units of degrees-C
2717 * below this value, including the Digital Thermal Sensor (DTS),
2718 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
2719 */
2720int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2721{
2722 unsigned long long msr;
2723 unsigned int target_c_local;
2724 int cpu;
2725
2726 /* tcc_activation_temp is used only for dts or ptm */
2727 if (!(do_dts || do_ptm))
2728 return 0;
2729
2730 /* this is a per-package concept */
2731 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2732 return 0;
2733
2734 cpu = t->cpu_id;
2735 if (cpu_migrate(cpu)) {
2736 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
2737 return -1;
2738 }
2739
2740 if (tcc_activation_temp_override != 0) {
2741 tcc_activation_temp = tcc_activation_temp_override;
2742 fprintf(stderr, "cpu%d: Using cmdline TCC Target (%d C)\n",
2743 cpu, tcc_activation_temp);
2744 return 0;
2745 }
2746
2747 /* Temperature Target MSR is Nehalem and newer only */
Len Brownd7899442015-01-23 00:12:33 -05002748 if (!do_nhm_platform_info)
Len Brown889facb2012-11-08 00:48:57 -05002749 goto guess;
2750
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04002751 if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
Len Brown889facb2012-11-08 00:48:57 -05002752 goto guess;
2753
Jean Delvare34821242014-05-01 11:40:19 +02002754 target_c_local = (msr >> 16) & 0xFF;
Len Brown889facb2012-11-08 00:48:57 -05002755
Len Brownd8af6f52015-02-10 01:56:38 -05002756 if (debug)
Len Brown889facb2012-11-08 00:48:57 -05002757 fprintf(stderr, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
2758 cpu, msr, target_c_local);
2759
Jean Delvare34821242014-05-01 11:40:19 +02002760 if (!target_c_local)
Len Brown889facb2012-11-08 00:48:57 -05002761 goto guess;
2762
2763 tcc_activation_temp = target_c_local;
2764
2765 return 0;
2766
2767guess:
2768 tcc_activation_temp = TJMAX_DEFAULT;
2769 fprintf(stderr, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
2770 cpu, tcc_activation_temp);
2771
2772 return 0;
2773}
Len Brown69807a62015-11-21 12:22:47 -05002774
2775void decode_misc_enable_msr(void)
2776{
2777 unsigned long long msr;
2778
2779 if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
2780 fprintf(stderr, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%s %s %s)\n",
2781 base_cpu, msr,
2782 msr & (1 << 3) ? "TCC" : "",
2783 msr & (1 << 16) ? "EIST" : "",
2784 msr & (1 << 18) ? "MONITOR" : "");
2785}
2786
Len Brownf0057312015-12-03 01:35:36 -05002787/*
2788 * Decode MSR_MISC_PWR_MGMT
2789 *
2790 * Decode the bits according to the Nehalem documentation
2791 * bit[0] seems to continue to have same meaning going forward
2792 * bit[1] less so...
2793 */
2794void decode_misc_pwr_mgmt_msr(void)
2795{
2796 unsigned long long msr;
2797
2798 if (!do_nhm_platform_info)
2799 return;
2800
2801 if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
2802 fprintf(stderr, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB)\n",
2803 base_cpu, msr,
2804 msr & (1 << 0) ? "DIS" : "EN",
2805 msr & (1 << 1) ? "EN" : "DIS");
2806}
Len Brown7f5c2582015-12-01 01:36:39 -05002807
Len Brownfcd17212015-03-23 20:29:09 -04002808void process_cpuid()
Len Brown103a8fe2010-10-22 23:53:03 -04002809{
Len Brown61a87ba2015-11-23 02:30:51 -05002810 unsigned int eax, ebx, ecx, edx, max_level, max_extended_level;
Len Brown103a8fe2010-10-22 23:53:03 -04002811 unsigned int fms, family, model, stepping;
2812
2813 eax = ebx = ecx = edx = 0;
2814
Josh Triplett2b928652013-08-20 17:20:14 -07002815 __get_cpuid(0, &max_level, &ebx, &ecx, &edx);
Len Brown103a8fe2010-10-22 23:53:03 -04002816
2817 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
2818 genuine_intel = 1;
2819
Len Brownd8af6f52015-02-10 01:56:38 -05002820 if (debug)
Len Brown889facb2012-11-08 00:48:57 -05002821 fprintf(stderr, "CPUID(0): %.4s%.4s%.4s ",
Len Brown103a8fe2010-10-22 23:53:03 -04002822 (char *)&ebx, (char *)&edx, (char *)&ecx);
2823
Josh Triplett2b928652013-08-20 17:20:14 -07002824 __get_cpuid(1, &fms, &ebx, &ecx, &edx);
Len Brown103a8fe2010-10-22 23:53:03 -04002825 family = (fms >> 8) & 0xf;
2826 model = (fms >> 4) & 0xf;
2827 stepping = fms & 0xf;
2828 if (family == 6 || family == 0xf)
2829 model += ((fms >> 16) & 0xf) << 4;
2830
Len Brown69807a62015-11-21 12:22:47 -05002831 if (debug) {
Len Brown103a8fe2010-10-22 23:53:03 -04002832 fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
2833 max_level, family, model, stepping, family, model, stepping);
Len Brown69807a62015-11-21 12:22:47 -05002834 fprintf(stderr, "CPUID(1): %s %s %s %s %s %s %s %s\n",
2835 ecx & (1 << 0) ? "SSE3" : "-",
2836 ecx & (1 << 3) ? "MONITOR" : "-",
2837 ecx & (1 << 7) ? "EIST" : "-",
2838 ecx & (1 << 8) ? "TM2" : "-",
2839 edx & (1 << 4) ? "TSC" : "-",
2840 edx & (1 << 5) ? "MSR" : "-",
2841 edx & (1 << 22) ? "ACPI-TM" : "-",
2842 edx & (1 << 29) ? "TM" : "-");
2843 }
Len Brown103a8fe2010-10-22 23:53:03 -04002844
Josh Triplettb2c95d92013-08-20 17:20:18 -07002845 if (!(edx & (1 << 5)))
2846 errx(1, "CPUID: no MSR");
Len Brown103a8fe2010-10-22 23:53:03 -04002847
2848 /*
2849 * check max extended function levels of CPUID.
2850 * This is needed to check for invariant TSC.
2851 * This check is valid for both Intel and AMD.
2852 */
2853 ebx = ecx = edx = 0;
Len Brown61a87ba2015-11-23 02:30:51 -05002854 __get_cpuid(0x80000000, &max_extended_level, &ebx, &ecx, &edx);
Len Brown103a8fe2010-10-22 23:53:03 -04002855
Len Brown61a87ba2015-11-23 02:30:51 -05002856 if (max_extended_level >= 0x80000007) {
Len Brown103a8fe2010-10-22 23:53:03 -04002857
Len Brownd7899442015-01-23 00:12:33 -05002858 /*
2859 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
2860 * this check is valid for both Intel and AMD
2861 */
2862 __get_cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
2863 has_invariant_tsc = edx & (1 << 8);
2864 }
Len Brown103a8fe2010-10-22 23:53:03 -04002865
2866 /*
2867 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
2868 * this check is valid for both Intel and AMD
2869 */
2870
Josh Triplett2b928652013-08-20 17:20:14 -07002871 __get_cpuid(0x6, &eax, &ebx, &ecx, &edx);
Thomas Renninger8209e052011-01-21 15:11:19 +01002872 has_aperf = ecx & (1 << 0);
Len Brown889facb2012-11-08 00:48:57 -05002873 do_dts = eax & (1 << 0);
2874 do_ptm = eax & (1 << 6);
Len Brown7f5c2582015-12-01 01:36:39 -05002875 has_hwp = eax & (1 << 7);
2876 has_hwp_notify = eax & (1 << 8);
2877 has_hwp_activity_window = eax & (1 << 9);
2878 has_hwp_epp = eax & (1 << 10);
2879 has_hwp_pkg = eax & (1 << 11);
Len Brown889facb2012-11-08 00:48:57 -05002880 has_epb = ecx & (1 << 3);
2881
Len Brownd8af6f52015-02-10 01:56:38 -05002882 if (debug)
Len Brown7f5c2582015-12-01 01:36:39 -05002883 fprintf(stderr, "CPUID(6): %sAPERF, %sDTS, %sPTM, %sHWP, "
2884 "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
2885 has_aperf ? "" : "No-",
2886 do_dts ? "" : "No-",
2887 do_ptm ? "" : "No-",
2888 has_hwp ? "" : "No-",
2889 has_hwp_notify ? "" : "No-",
2890 has_hwp_activity_window ? "" : "No-",
2891 has_hwp_epp ? "" : "No-",
2892 has_hwp_pkg ? "" : "No-",
2893 has_epb ? "" : "No-");
Len Brown103a8fe2010-10-22 23:53:03 -04002894
Len Brown69807a62015-11-21 12:22:47 -05002895 if (debug)
2896 decode_misc_enable_msr();
2897
Len Brown61a87ba2015-11-23 02:30:51 -05002898 if (max_level >= 0x15) {
Len Brown8a5bdf42015-04-01 21:02:57 -04002899 unsigned int eax_crystal;
2900 unsigned int ebx_tsc;
2901
2902 /*
2903 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
2904 */
2905 eax_crystal = ebx_tsc = crystal_hz = edx = 0;
2906 __get_cpuid(0x15, &eax_crystal, &ebx_tsc, &crystal_hz, &edx);
2907
2908 if (ebx_tsc != 0) {
2909
2910 if (debug && (ebx != 0))
2911 fprintf(stderr, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
2912 eax_crystal, ebx_tsc, crystal_hz);
2913
2914 if (crystal_hz == 0)
2915 switch(model) {
2916 case 0x4E: /* SKL */
2917 case 0x5E: /* SKL */
2918 crystal_hz = 24000000; /* 24 MHz */
2919 break;
2920 default:
2921 crystal_hz = 0;
2922 }
2923
2924 if (crystal_hz) {
2925 tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
2926 if (debug)
2927 fprintf(stderr, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
2928 tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal);
2929 }
2930 }
2931 }
Len Brown61a87ba2015-11-23 02:30:51 -05002932 if (max_level >= 0x16) {
2933 unsigned int base_mhz, max_mhz, bus_mhz, edx;
2934
2935 /*
2936 * CPUID 16H Base MHz, Max MHz, Bus MHz
2937 */
2938 base_mhz = max_mhz = bus_mhz = edx = 0;
2939
2940 __get_cpuid(0x16, &base_mhz, &max_mhz, &bus_mhz, &edx);
2941 if (debug)
2942 fprintf(stderr, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
2943 base_mhz, max_mhz, bus_mhz);
2944 }
Len Brown8a5bdf42015-04-01 21:02:57 -04002945
Hubert Chrzaniukb2b34df2015-09-14 13:31:00 +02002946 if (has_aperf)
2947 aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
2948
Len Brownee7e38e2015-02-09 23:39:45 -05002949 do_nhm_platform_info = do_nhm_cstates = do_smi = probe_nhm_msrs(family, model);
Len Brownd7899442015-01-23 00:12:33 -05002950 do_snb_cstates = has_snb_msrs(family, model);
Len Brownee7e38e2015-02-09 23:39:45 -05002951 do_pc2 = do_snb_cstates && (pkg_cstate_limit >= PCL__2);
2952 do_pc3 = (pkg_cstate_limit >= PCL__3);
2953 do_pc6 = (pkg_cstate_limit >= PCL__6);
2954 do_pc7 = do_snb_cstates && (pkg_cstate_limit >= PCL__7);
Len Brownd7899442015-01-23 00:12:33 -05002955 do_c8_c9_c10 = has_hsw_msrs(family, model);
Len Brown0b2bb692015-03-26 00:50:30 -04002956 do_skl_residency = has_skl_msrs(family, model);
Len Brown144b44b2013-11-09 00:30:16 -05002957 do_slm_cstates = is_slm(family, model);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002958 do_knl_cstates = is_knl(family, model);
Len Brown103a8fe2010-10-22 23:53:03 -04002959
Len Brownf0057312015-12-03 01:35:36 -05002960 if (debug)
2961 decode_misc_pwr_mgmt_msr();
2962
Len Brown889facb2012-11-08 00:48:57 -05002963 rapl_probe(family, model);
Len Brown3a9a9412014-08-15 02:39:52 -04002964 perf_limit_reasons_probe(family, model);
Len Brown889facb2012-11-08 00:48:57 -05002965
Len Brownfcd17212015-03-23 20:29:09 -04002966 if (debug)
2967 dump_cstate_pstate_config_info();
2968
Len Browna2b7b742015-09-26 00:12:38 -04002969 if (has_skl_msrs(family, model))
2970 calculate_tsc_tweak();
2971
Len Brown889facb2012-11-08 00:48:57 -05002972 return;
Len Brown103a8fe2010-10-22 23:53:03 -04002973}
2974
Len Brownd8af6f52015-02-10 01:56:38 -05002975void help()
Len Brown103a8fe2010-10-22 23:53:03 -04002976{
Len Brownd8af6f52015-02-10 01:56:38 -05002977 fprintf(stderr,
2978 "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
2979 "\n"
2980 "Turbostat forks the specified COMMAND and prints statistics\n"
2981 "when COMMAND completes.\n"
2982 "If no COMMAND is specified, turbostat wakes every 5-seconds\n"
2983 "to print statistics, until interrupted.\n"
2984 "--debug run in \"debug\" mode\n"
2985 "--interval sec Override default 5-second measurement interval\n"
2986 "--help print this help message\n"
2987 "--counter msr print 32-bit counter at address \"msr\"\n"
2988 "--Counter msr print 64-bit Counter at address \"msr\"\n"
2989 "--msr msr print 32-bit value at address \"msr\"\n"
2990 "--MSR msr print 64-bit Value at address \"msr\"\n"
2991 "--version print version information\n"
2992 "\n"
2993 "For more help, run \"man turbostat\"\n");
Len Brown103a8fe2010-10-22 23:53:03 -04002994}
2995
2996
2997/*
2998 * in /dev/cpu/ return success for names that are numbers
2999 * ie. filter out ".", "..", "microcode".
3000 */
3001int dir_filter(const struct dirent *dirp)
3002{
3003 if (isdigit(dirp->d_name[0]))
3004 return 1;
3005 else
3006 return 0;
3007}
3008
3009int open_dev_cpu_msr(int dummy1)
3010{
3011 return 0;
3012}
3013
Len Brownc98d5d92012-06-04 00:56:40 -04003014void topology_probe()
3015{
3016 int i;
3017 int max_core_id = 0;
3018 int max_package_id = 0;
3019 int max_siblings = 0;
3020 struct cpu_topology {
3021 int core_id;
3022 int physical_package_id;
3023 } *cpus;
3024
3025 /* Initialize num_cpus, max_cpu_num */
3026 topo.num_cpus = 0;
3027 topo.max_cpu_num = 0;
3028 for_all_proc_cpus(count_cpus);
3029 if (!summary_only && topo.num_cpus > 1)
3030 show_cpu = 1;
3031
Len Brownd8af6f52015-02-10 01:56:38 -05003032 if (debug > 1)
Len Brownc98d5d92012-06-04 00:56:40 -04003033 fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
3034
3035 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
Josh Triplettb2c95d92013-08-20 17:20:18 -07003036 if (cpus == NULL)
3037 err(1, "calloc cpus");
Len Brownc98d5d92012-06-04 00:56:40 -04003038
3039 /*
3040 * Allocate and initialize cpu_present_set
3041 */
3042 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07003043 if (cpu_present_set == NULL)
3044 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04003045 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
3046 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
3047 for_all_proc_cpus(mark_cpu_present);
3048
3049 /*
3050 * Allocate and initialize cpu_affinity_set
3051 */
3052 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07003053 if (cpu_affinity_set == NULL)
3054 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04003055 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
3056 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
3057
3058
3059 /*
3060 * For online cpus
3061 * find max_core_id, max_package_id
3062 */
3063 for (i = 0; i <= topo.max_cpu_num; ++i) {
3064 int siblings;
3065
3066 if (cpu_is_not_present(i)) {
Len Brownd8af6f52015-02-10 01:56:38 -05003067 if (debug > 1)
Len Brownc98d5d92012-06-04 00:56:40 -04003068 fprintf(stderr, "cpu%d NOT PRESENT\n", i);
3069 continue;
3070 }
3071 cpus[i].core_id = get_core_id(i);
3072 if (cpus[i].core_id > max_core_id)
3073 max_core_id = cpus[i].core_id;
3074
3075 cpus[i].physical_package_id = get_physical_package_id(i);
3076 if (cpus[i].physical_package_id > max_package_id)
3077 max_package_id = cpus[i].physical_package_id;
3078
3079 siblings = get_num_ht_siblings(i);
3080 if (siblings > max_siblings)
3081 max_siblings = siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05003082 if (debug > 1)
Len Brownc98d5d92012-06-04 00:56:40 -04003083 fprintf(stderr, "cpu %d pkg %d core %d\n",
3084 i, cpus[i].physical_package_id, cpus[i].core_id);
3085 }
3086 topo.num_cores_per_pkg = max_core_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05003087 if (debug > 1)
Len Brownc98d5d92012-06-04 00:56:40 -04003088 fprintf(stderr, "max_core_id %d, sizing for %d cores per package\n",
3089 max_core_id, topo.num_cores_per_pkg);
Len Brown1cc21f72015-02-23 00:34:57 -05003090 if (debug && !summary_only && topo.num_cores_per_pkg > 1)
Len Brownc98d5d92012-06-04 00:56:40 -04003091 show_core = 1;
3092
3093 topo.num_packages = max_package_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05003094 if (debug > 1)
Len Brownc98d5d92012-06-04 00:56:40 -04003095 fprintf(stderr, "max_package_id %d, sizing for %d packages\n",
3096 max_package_id, topo.num_packages);
Len Brown1cc21f72015-02-23 00:34:57 -05003097 if (debug && !summary_only && topo.num_packages > 1)
Len Brownc98d5d92012-06-04 00:56:40 -04003098 show_pkg = 1;
3099
3100 topo.num_threads_per_core = max_siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05003101 if (debug > 1)
Len Brownc98d5d92012-06-04 00:56:40 -04003102 fprintf(stderr, "max_siblings %d\n", max_siblings);
3103
3104 free(cpus);
3105}
3106
3107void
3108allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
3109{
3110 int i;
3111
3112 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
3113 topo.num_packages, sizeof(struct thread_data));
3114 if (*t == NULL)
3115 goto error;
3116
3117 for (i = 0; i < topo.num_threads_per_core *
3118 topo.num_cores_per_pkg * topo.num_packages; i++)
3119 (*t)[i].cpu_id = -1;
3120
3121 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
3122 sizeof(struct core_data));
3123 if (*c == NULL)
3124 goto error;
3125
3126 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
3127 (*c)[i].core_id = -1;
3128
3129 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
3130 if (*p == NULL)
3131 goto error;
3132
3133 for (i = 0; i < topo.num_packages; i++)
3134 (*p)[i].package_id = i;
3135
3136 return;
3137error:
Josh Triplettb2c95d92013-08-20 17:20:18 -07003138 err(1, "calloc counters");
Len Brownc98d5d92012-06-04 00:56:40 -04003139}
3140/*
3141 * init_counter()
3142 *
3143 * set cpu_id, core_num, pkg_num
3144 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
3145 *
3146 * increment topo.num_cores when 1st core in pkg seen
3147 */
3148void init_counter(struct thread_data *thread_base, struct core_data *core_base,
3149 struct pkg_data *pkg_base, int thread_num, int core_num,
3150 int pkg_num, int cpu_id)
3151{
3152 struct thread_data *t;
3153 struct core_data *c;
3154 struct pkg_data *p;
3155
3156 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
3157 c = GET_CORE(core_base, core_num, pkg_num);
3158 p = GET_PKG(pkg_base, pkg_num);
3159
3160 t->cpu_id = cpu_id;
3161 if (thread_num == 0) {
3162 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
3163 if (cpu_is_first_core_in_package(cpu_id))
3164 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
3165 }
3166
3167 c->core_id = core_num;
3168 p->package_id = pkg_num;
3169}
3170
3171
3172int initialize_counters(int cpu_id)
3173{
3174 int my_thread_id, my_core_id, my_package_id;
3175
3176 my_package_id = get_physical_package_id(cpu_id);
3177 my_core_id = get_core_id(cpu_id);
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07003178 my_thread_id = get_cpu_position_in_core(cpu_id);
3179 if (!my_thread_id)
Len Brownc98d5d92012-06-04 00:56:40 -04003180 topo.num_cores++;
Len Brownc98d5d92012-06-04 00:56:40 -04003181
3182 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
3183 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
3184 return 0;
3185}
3186
3187void allocate_output_buffer()
3188{
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02003189 output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
Len Brownc98d5d92012-06-04 00:56:40 -04003190 outp = output_buffer;
Josh Triplettb2c95d92013-08-20 17:20:18 -07003191 if (outp == NULL)
3192 err(-1, "calloc output buffer");
Len Brownc98d5d92012-06-04 00:56:40 -04003193}
3194
3195void setup_all_buffers(void)
3196{
3197 topology_probe();
3198 allocate_counters(&thread_even, &core_even, &package_even);
3199 allocate_counters(&thread_odd, &core_odd, &package_odd);
3200 allocate_output_buffer();
3201 for_all_proc_cpus(initialize_counters);
3202}
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02003203
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003204void set_base_cpu(void)
3205{
3206 base_cpu = sched_getcpu();
3207 if (base_cpu < 0)
3208 err(-ENODEV, "No valid cpus found");
3209
3210 if (debug > 1)
3211 fprintf(stderr, "base_cpu = %d\n", base_cpu);
3212}
3213
Len Brown103a8fe2010-10-22 23:53:03 -04003214void turbostat_init()
3215{
Prarit Bhargava7ce7d5d2015-05-25 08:34:28 -04003216 setup_all_buffers();
3217 set_base_cpu();
Len Brown103a8fe2010-10-22 23:53:03 -04003218 check_dev_msr();
Len Brown98481e72014-08-15 00:36:50 -04003219 check_permissions();
Len Brownfcd17212015-03-23 20:29:09 -04003220 process_cpuid();
Len Brown103a8fe2010-10-22 23:53:03 -04003221
Len Brown103a8fe2010-10-22 23:53:03 -04003222
Len Brownd8af6f52015-02-10 01:56:38 -05003223 if (debug)
Len Brown7f5c2582015-12-01 01:36:39 -05003224 for_all_cpus(print_hwp, ODD_COUNTERS);
3225
3226 if (debug)
Len Brown889facb2012-11-08 00:48:57 -05003227 for_all_cpus(print_epb, ODD_COUNTERS);
3228
Len Brownd8af6f52015-02-10 01:56:38 -05003229 if (debug)
Len Brown3a9a9412014-08-15 02:39:52 -04003230 for_all_cpus(print_perf_limit, ODD_COUNTERS);
3231
Len Brownd8af6f52015-02-10 01:56:38 -05003232 if (debug)
Len Brown889facb2012-11-08 00:48:57 -05003233 for_all_cpus(print_rapl, ODD_COUNTERS);
3234
3235 for_all_cpus(set_temperature_target, ODD_COUNTERS);
3236
Len Brownd8af6f52015-02-10 01:56:38 -05003237 if (debug)
Len Brown889facb2012-11-08 00:48:57 -05003238 for_all_cpus(print_thermal, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04003239}
3240
3241int fork_it(char **argv)
3242{
Len Brown103a8fe2010-10-22 23:53:03 -04003243 pid_t child_pid;
Len Brownd91bb172012-11-01 00:08:19 -04003244 int status;
Len Brownd15cf7c2012-06-03 23:24:00 -04003245
Len Brownd91bb172012-11-01 00:08:19 -04003246 status = for_all_cpus(get_counters, EVEN_COUNTERS);
3247 if (status)
3248 exit(status);
Len Brownc98d5d92012-06-04 00:56:40 -04003249 /* clear affinity side-effect of get_counters() */
3250 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
Len Brown103a8fe2010-10-22 23:53:03 -04003251 gettimeofday(&tv_even, (struct timezone *)NULL);
3252
3253 child_pid = fork();
3254 if (!child_pid) {
3255 /* child */
3256 execvp(argv[0], argv);
3257 } else {
Len Brown103a8fe2010-10-22 23:53:03 -04003258
3259 /* parent */
Josh Triplettb2c95d92013-08-20 17:20:18 -07003260 if (child_pid == -1)
3261 err(1, "fork");
Len Brown103a8fe2010-10-22 23:53:03 -04003262
3263 signal(SIGINT, SIG_IGN);
3264 signal(SIGQUIT, SIG_IGN);
Josh Triplettb2c95d92013-08-20 17:20:18 -07003265 if (waitpid(child_pid, &status, 0) == -1)
3266 err(status, "waitpid");
Len Brown103a8fe2010-10-22 23:53:03 -04003267 }
Len Brownc98d5d92012-06-04 00:56:40 -04003268 /*
3269 * n.b. fork_it() does not check for errors from for_all_cpus()
3270 * because re-starting is problematic when forking
3271 */
3272 for_all_cpus(get_counters, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04003273 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04003274 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04003275 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
3276 compute_average(EVEN_COUNTERS);
3277 format_all_counters(EVEN_COUNTERS);
3278 flush_stderr();
Len Brown103a8fe2010-10-22 23:53:03 -04003279
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07003280 fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
Len Brown103a8fe2010-10-22 23:53:03 -04003281
Len Brownd91bb172012-11-01 00:08:19 -04003282 return status;
Len Brown103a8fe2010-10-22 23:53:03 -04003283}
3284
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02003285int get_and_dump_counters(void)
3286{
3287 int status;
3288
3289 status = for_all_cpus(get_counters, ODD_COUNTERS);
3290 if (status)
3291 return status;
3292
3293 status = for_all_cpus(dump_counters, ODD_COUNTERS);
3294 if (status)
3295 return status;
3296
3297 flush_stdout();
3298
3299 return status;
3300}
3301
Len Brownd8af6f52015-02-10 01:56:38 -05003302void print_version() {
Len Brown7f5c2582015-12-01 01:36:39 -05003303 fprintf(stderr, "turbostat version 4.10 10 Dec, 2015"
Len Brownd8af6f52015-02-10 01:56:38 -05003304 " - Len Brown <lenb@kernel.org>\n");
3305}
3306
Len Brown103a8fe2010-10-22 23:53:03 -04003307void cmdline(int argc, char **argv)
3308{
3309 int opt;
Len Brownd8af6f52015-02-10 01:56:38 -05003310 int option_index = 0;
3311 static struct option long_options[] = {
3312 {"Counter", required_argument, 0, 'C'},
3313 {"counter", required_argument, 0, 'c'},
3314 {"Dump", no_argument, 0, 'D'},
3315 {"debug", no_argument, 0, 'd'},
3316 {"interval", required_argument, 0, 'i'},
3317 {"help", no_argument, 0, 'h'},
3318 {"Joules", no_argument, 0, 'J'},
3319 {"MSR", required_argument, 0, 'M'},
3320 {"msr", required_argument, 0, 'm'},
3321 {"Package", no_argument, 0, 'p'},
3322 {"processor", no_argument, 0, 'p'},
3323 {"Summary", no_argument, 0, 'S'},
3324 {"TCC", required_argument, 0, 'T'},
3325 {"version", no_argument, 0, 'v' },
3326 {0, 0, 0, 0 }
3327 };
Len Brown103a8fe2010-10-22 23:53:03 -04003328
3329 progname = argv[0];
3330
Len Browna01e72f2015-07-15 21:49:41 -04003331 while ((opt = getopt_long_only(argc, argv, "+C:c:Ddhi:JM:m:PpST:v",
Len Brownd8af6f52015-02-10 01:56:38 -05003332 long_options, &option_index)) != -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04003333 switch (opt) {
Len Brownd8af6f52015-02-10 01:56:38 -05003334 case 'C':
3335 sscanf(optarg, "%x", &extra_delta_offset64);
Len Brown103a8fe2010-10-22 23:53:03 -04003336 break;
Len Brownf9240812012-10-06 15:26:31 -04003337 case 'c':
Len Brown8e180f32012-09-22 01:25:08 -04003338 sscanf(optarg, "%x", &extra_delta_offset32);
3339 break;
Len Brownd8af6f52015-02-10 01:56:38 -05003340 case 'D':
3341 dump_only++;
Len Brown8e180f32012-09-22 01:25:08 -04003342 break;
Len Brownd8af6f52015-02-10 01:56:38 -05003343 case 'd':
3344 debug++;
Len Brown2f32edf2012-09-21 23:45:46 -04003345 break;
Len Brownd8af6f52015-02-10 01:56:38 -05003346 case 'h':
3347 default:
3348 help();
3349 exit(1);
3350 case 'i':
Len Brown2a0609c2016-02-12 22:44:48 -05003351 {
3352 double interval = strtod(optarg, NULL);
3353
3354 if (interval < 0.001) {
3355 fprintf(stderr, "interval %f seconds is too small\n",
3356 interval);
3357 exit(2);
3358 }
3359
3360 interval_ts.tv_sec = interval;
3361 interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
3362 }
Len Brown889facb2012-11-08 00:48:57 -05003363 break;
Dirk Brandewie5c56be92013-12-16 10:23:41 -08003364 case 'J':
3365 rapl_joules++;
3366 break;
Len Brownd8af6f52015-02-10 01:56:38 -05003367 case 'M':
3368 sscanf(optarg, "%x", &extra_msr_offset64);
3369 break;
3370 case 'm':
3371 sscanf(optarg, "%x", &extra_msr_offset32);
3372 break;
3373 case 'P':
3374 show_pkg_only++;
3375 break;
3376 case 'p':
3377 show_core_only++;
3378 break;
3379 case 'S':
3380 summary_only++;
3381 break;
3382 case 'T':
3383 tcc_activation_temp_override = atoi(optarg);
3384 break;
3385 case 'v':
3386 print_version();
3387 exit(0);
3388 break;
Len Brown103a8fe2010-10-22 23:53:03 -04003389 }
3390 }
3391}
3392
3393int main(int argc, char **argv)
3394{
3395 cmdline(argc, argv);
3396
Len Brownd8af6f52015-02-10 01:56:38 -05003397 if (debug)
3398 print_version();
Len Brown103a8fe2010-10-22 23:53:03 -04003399
3400 turbostat_init();
3401
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02003402 /* dump counters and exit */
3403 if (dump_only)
3404 return get_and_dump_counters();
3405
Len Brown103a8fe2010-10-22 23:53:03 -04003406 /*
3407 * if any params left, it must be a command to fork
3408 */
3409 if (argc - optind)
3410 return fork_it(argv + optind);
3411 else
3412 turbostat_loop();
3413
3414 return 0;
3415}