blob: f92211e9e70c3a2acfe3ede39d0024c8fc820047 [file] [log] [blame]
Len Brown103a8fe2010-10-22 23:53:03 -04001/*
2 * turbostat -- show CPU frequency and C-state residency
3 * on modern Intel turbo-capable processors.
4 *
Len Brown144b44b2013-11-09 00:30:16 -05005 * Copyright (c) 2013 Intel Corporation.
Len Brown103a8fe2010-10-22 23:53:03 -04006 * Len Brown <len.brown@intel.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
Len Brown88c32812012-03-29 21:44:40 -040022#define _GNU_SOURCE
Josh Triplettb731f312013-08-20 17:20:12 -070023#include MSRHEADER
Josh Triplett95aebc42013-08-20 17:20:16 -070024#include <stdarg.h>
Len Brown103a8fe2010-10-22 23:53:03 -040025#include <stdio.h>
Josh Triplettb2c95d92013-08-20 17:20:18 -070026#include <err.h>
Len Brown103a8fe2010-10-22 23:53:03 -040027#include <unistd.h>
28#include <sys/types.h>
29#include <sys/wait.h>
30#include <sys/stat.h>
31#include <sys/resource.h>
32#include <fcntl.h>
33#include <signal.h>
34#include <sys/time.h>
35#include <stdlib.h>
Len Brownd8af6f52015-02-10 01:56:38 -050036#include <getopt.h>
Len Brown103a8fe2010-10-22 23:53:03 -040037#include <dirent.h>
38#include <string.h>
39#include <ctype.h>
Len Brown88c32812012-03-29 21:44:40 -040040#include <sched.h>
Josh Triplett2b928652013-08-20 17:20:14 -070041#include <cpuid.h>
Len Brown98481e72014-08-15 00:36:50 -040042#include <linux/capability.h>
43#include <errno.h>
Len Brown103a8fe2010-10-22 23:53:03 -040044
Len Brown103a8fe2010-10-22 23:53:03 -040045char *proc_stat = "/proc/stat";
Len Brownd8af6f52015-02-10 01:56:38 -050046unsigned int interval_sec = 5;
47unsigned int debug;
48unsigned int rapl_joules;
49unsigned int summary_only;
50unsigned int dump_only;
Len Brown103a8fe2010-10-22 23:53:03 -040051unsigned int skip_c0;
52unsigned int skip_c1;
53unsigned int do_nhm_cstates;
54unsigned int do_snb_cstates;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -070055unsigned int do_knl_cstates;
Len Brownee7e38e2015-02-09 23:39:45 -050056unsigned int do_pc2;
57unsigned int do_pc3;
58unsigned int do_pc6;
59unsigned int do_pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -080060unsigned int do_c8_c9_c10;
Len Brown0b2bb692015-03-26 00:50:30 -040061unsigned int do_skl_residency;
Len Brown144b44b2013-11-09 00:30:16 -050062unsigned int do_slm_cstates;
63unsigned int use_c1_residency_msr;
Len Brown103a8fe2010-10-22 23:53:03 -040064unsigned int has_aperf;
Len Brown889facb2012-11-08 00:48:57 -050065unsigned int has_epb;
Len Brownfc04cc62014-02-06 00:55:19 -050066unsigned int units = 1000000; /* MHz etc */
Len Brown103a8fe2010-10-22 23:53:03 -040067unsigned int genuine_intel;
68unsigned int has_invariant_tsc;
Len Brownd7899442015-01-23 00:12:33 -050069unsigned int do_nhm_platform_info;
Len Brown2f32edf2012-09-21 23:45:46 -040070unsigned int extra_msr_offset32;
71unsigned int extra_msr_offset64;
Len Brown8e180f32012-09-22 01:25:08 -040072unsigned int extra_delta_offset32;
73unsigned int extra_delta_offset64;
Len Brown1ed51012013-02-10 17:19:24 -050074int do_smi;
Len Brown103a8fe2010-10-22 23:53:03 -040075double bclk;
76unsigned int show_pkg;
77unsigned int show_core;
78unsigned int show_cpu;
Len Brownc98d5d92012-06-04 00:56:40 -040079unsigned int show_pkg_only;
80unsigned int show_core_only;
81char *output_buffer, *outp;
Len Brown889facb2012-11-08 00:48:57 -050082unsigned int do_rapl;
83unsigned int do_dts;
84unsigned int do_ptm;
85unsigned int tcc_activation_temp;
86unsigned int tcc_activation_temp_override;
Andrey Semin40ee8e32014-12-05 00:07:00 -050087double rapl_power_units, rapl_time_units;
88double rapl_dram_energy_units, rapl_energy_units;
Len Brown889facb2012-11-08 00:48:57 -050089double rapl_joule_counter_range;
Len Brown3a9a9412014-08-15 02:39:52 -040090unsigned int do_core_perf_limit_reasons;
91unsigned int do_gfx_perf_limit_reasons;
92unsigned int do_ring_perf_limit_reasons;
Len Brown8a5bdf42015-04-01 21:02:57 -040093unsigned int crystal_hz;
94unsigned long long tsc_hz;
Len Brown889facb2012-11-08 00:48:57 -050095
Len Browne6f9bb32013-12-03 02:19:19 -050096#define RAPL_PKG (1 << 0)
97 /* 0x610 MSR_PKG_POWER_LIMIT */
98 /* 0x611 MSR_PKG_ENERGY_STATUS */
99#define RAPL_PKG_PERF_STATUS (1 << 1)
100 /* 0x613 MSR_PKG_PERF_STATUS */
101#define RAPL_PKG_POWER_INFO (1 << 2)
102 /* 0x614 MSR_PKG_POWER_INFO */
103
104#define RAPL_DRAM (1 << 3)
105 /* 0x618 MSR_DRAM_POWER_LIMIT */
106 /* 0x619 MSR_DRAM_ENERGY_STATUS */
Len Browne6f9bb32013-12-03 02:19:19 -0500107#define RAPL_DRAM_PERF_STATUS (1 << 4)
108 /* 0x61b MSR_DRAM_PERF_STATUS */
Len Brown0b2bb692015-03-26 00:50:30 -0400109#define RAPL_DRAM_POWER_INFO (1 << 5)
110 /* 0x61c MSR_DRAM_POWER_INFO */
Len Browne6f9bb32013-12-03 02:19:19 -0500111
Len Brown0b2bb692015-03-26 00:50:30 -0400112#define RAPL_CORES (1 << 6)
Len Browne6f9bb32013-12-03 02:19:19 -0500113 /* 0x638 MSR_PP0_POWER_LIMIT */
114 /* 0x639 MSR_PP0_ENERGY_STATUS */
Len Brown0b2bb692015-03-26 00:50:30 -0400115#define RAPL_CORE_POLICY (1 << 7)
Len Browne6f9bb32013-12-03 02:19:19 -0500116 /* 0x63a MSR_PP0_POLICY */
117
Len Brown0b2bb692015-03-26 00:50:30 -0400118#define RAPL_GFX (1 << 8)
Len Browne6f9bb32013-12-03 02:19:19 -0500119 /* 0x640 MSR_PP1_POWER_LIMIT */
120 /* 0x641 MSR_PP1_ENERGY_STATUS */
121 /* 0x642 MSR_PP1_POLICY */
Len Brown889facb2012-11-08 00:48:57 -0500122#define TJMAX_DEFAULT 100
123
124#define MAX(a, b) ((a) > (b) ? (a) : (b))
Len Brown103a8fe2010-10-22 23:53:03 -0400125
126int aperf_mperf_unstable;
127int backwards_count;
128char *progname;
Len Brown103a8fe2010-10-22 23:53:03 -0400129
Len Brownc98d5d92012-06-04 00:56:40 -0400130cpu_set_t *cpu_present_set, *cpu_affinity_set;
131size_t cpu_present_setsize, cpu_affinity_setsize;
Len Brown103a8fe2010-10-22 23:53:03 -0400132
Len Brownc98d5d92012-06-04 00:56:40 -0400133struct thread_data {
134 unsigned long long tsc;
135 unsigned long long aperf;
136 unsigned long long mperf;
Len Brown144b44b2013-11-09 00:30:16 -0500137 unsigned long long c1;
Len Brown2f32edf2012-09-21 23:45:46 -0400138 unsigned long long extra_msr64;
Len Brown8e180f32012-09-22 01:25:08 -0400139 unsigned long long extra_delta64;
140 unsigned long long extra_msr32;
141 unsigned long long extra_delta32;
Len Brown1ed51012013-02-10 17:19:24 -0500142 unsigned int smi_count;
Len Brownc98d5d92012-06-04 00:56:40 -0400143 unsigned int cpu_id;
144 unsigned int flags;
145#define CPU_IS_FIRST_THREAD_IN_CORE 0x2
146#define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
147} *thread_even, *thread_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400148
Len Brownc98d5d92012-06-04 00:56:40 -0400149struct core_data {
150 unsigned long long c3;
151 unsigned long long c6;
152 unsigned long long c7;
Len Brown889facb2012-11-08 00:48:57 -0500153 unsigned int core_temp_c;
Len Brownc98d5d92012-06-04 00:56:40 -0400154 unsigned int core_id;
155} *core_even, *core_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400156
Len Brownc98d5d92012-06-04 00:56:40 -0400157struct pkg_data {
158 unsigned long long pc2;
159 unsigned long long pc3;
160 unsigned long long pc6;
161 unsigned long long pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800162 unsigned long long pc8;
163 unsigned long long pc9;
164 unsigned long long pc10;
Len Brown0b2bb692015-03-26 00:50:30 -0400165 unsigned long long pkg_wtd_core_c0;
166 unsigned long long pkg_any_core_c0;
167 unsigned long long pkg_any_gfxe_c0;
168 unsigned long long pkg_both_core_gfxe_c0;
Len Brownc98d5d92012-06-04 00:56:40 -0400169 unsigned int package_id;
Len Brown889facb2012-11-08 00:48:57 -0500170 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
171 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
172 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
173 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
174 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
175 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
176 unsigned int pkg_temp_c;
177
Len Brownc98d5d92012-06-04 00:56:40 -0400178} *package_even, *package_odd;
179
180#define ODD_COUNTERS thread_odd, core_odd, package_odd
181#define EVEN_COUNTERS thread_even, core_even, package_even
182
183#define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
184 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
185 topo.num_threads_per_core + \
186 (core_no) * topo.num_threads_per_core + (thread_no))
187#define GET_CORE(core_base, core_no, pkg_no) \
188 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
189#define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
190
191struct system_summary {
192 struct thread_data threads;
193 struct core_data cores;
194 struct pkg_data packages;
195} sum, average;
196
197
198struct topo_params {
199 int num_packages;
200 int num_cpus;
201 int num_cores;
202 int max_cpu_num;
203 int num_cores_per_pkg;
204 int num_threads_per_core;
205} topo;
206
207struct timeval tv_even, tv_odd, tv_delta;
208
209void setup_all_buffers(void);
210
211int cpu_is_not_present(int cpu)
Len Brownd15cf7c2012-06-03 23:24:00 -0400212{
Len Brownc98d5d92012-06-04 00:56:40 -0400213 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brownd15cf7c2012-06-03 23:24:00 -0400214}
Len Brown88c32812012-03-29 21:44:40 -0400215/*
Len Brownc98d5d92012-06-04 00:56:40 -0400216 * run func(thread, core, package) in topology order
217 * skip non-present cpus
Len Brown88c32812012-03-29 21:44:40 -0400218 */
Len Brownd15cf7c2012-06-03 23:24:00 -0400219
Len Brownc98d5d92012-06-04 00:56:40 -0400220int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
221 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
Len Brown88c32812012-03-29 21:44:40 -0400222{
Len Brownc98d5d92012-06-04 00:56:40 -0400223 int retval, pkg_no, core_no, thread_no;
224
225 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
226 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
227 for (thread_no = 0; thread_no <
228 topo.num_threads_per_core; ++thread_no) {
229 struct thread_data *t;
230 struct core_data *c;
231 struct pkg_data *p;
232
233 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
234
235 if (cpu_is_not_present(t->cpu_id))
236 continue;
237
238 c = GET_CORE(core_base, core_no, pkg_no);
239 p = GET_PKG(pkg_base, pkg_no);
240
241 retval = func(t, c, p);
242 if (retval)
243 return retval;
244 }
245 }
246 }
247 return 0;
Len Brown88c32812012-03-29 21:44:40 -0400248}
249
250int cpu_migrate(int cpu)
251{
Len Brownc98d5d92012-06-04 00:56:40 -0400252 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
253 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
254 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
Len Brown88c32812012-03-29 21:44:40 -0400255 return -1;
256 else
257 return 0;
258}
259
Len Brown15aaa342012-03-29 22:19:58 -0400260int get_msr(int cpu, off_t offset, unsigned long long *msr)
Len Brown103a8fe2010-10-22 23:53:03 -0400261{
262 ssize_t retval;
Len Brown103a8fe2010-10-22 23:53:03 -0400263 char pathname[32];
264 int fd;
265
266 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
267 fd = open(pathname, O_RDONLY);
Len Brown15aaa342012-03-29 22:19:58 -0400268 if (fd < 0)
Len Brown98481e72014-08-15 00:36:50 -0400269 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 -0400270
Len Brown15aaa342012-03-29 22:19:58 -0400271 retval = pread(fd, msr, sizeof *msr, offset);
Len Brown103a8fe2010-10-22 23:53:03 -0400272 close(fd);
Len Brown15aaa342012-03-29 22:19:58 -0400273
Len Brown98481e72014-08-15 00:36:50 -0400274 if (retval != sizeof *msr)
275 err(-1, "%s offset 0x%llx read failed", pathname, (unsigned long long)offset);
Len Brown15aaa342012-03-29 22:19:58 -0400276
277 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400278}
279
Len Brownfc04cc62014-02-06 00:55:19 -0500280/*
281 * Example Format w/ field column widths:
282 *
Len Browne7c95ff2014-08-14 21:22:13 -0400283 * 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
284 * 123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678
Len Brownfc04cc62014-02-06 00:55:19 -0500285 */
286
Len Browna829eb42011-02-10 23:36:34 -0500287void print_header(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400288{
289 if (show_pkg)
Len Browne7c95ff2014-08-14 21:22:13 -0400290 outp += sprintf(outp, " Package");
Len Brown103a8fe2010-10-22 23:53:03 -0400291 if (show_core)
Len Browne7c95ff2014-08-14 21:22:13 -0400292 outp += sprintf(outp, " Core");
Len Brown103a8fe2010-10-22 23:53:03 -0400293 if (show_cpu)
Len Browne7c95ff2014-08-14 21:22:13 -0400294 outp += sprintf(outp, " CPU");
Len Brown103a8fe2010-10-22 23:53:03 -0400295 if (has_aperf)
Len Browne7c95ff2014-08-14 21:22:13 -0400296 outp += sprintf(outp, " Avg_MHz");
Len Brownd7899442015-01-23 00:12:33 -0500297 if (has_aperf)
Len Browne7c95ff2014-08-14 21:22:13 -0400298 outp += sprintf(outp, " %%Busy");
Len Brownfc04cc62014-02-06 00:55:19 -0500299 if (has_aperf)
Len Browne7c95ff2014-08-14 21:22:13 -0400300 outp += sprintf(outp, " Bzy_MHz");
301 outp += sprintf(outp, " TSC_MHz");
Len Brown1cc21f72015-02-23 00:34:57 -0500302
Len Brown8e180f32012-09-22 01:25:08 -0400303 if (extra_delta_offset32)
Len Browne7c95ff2014-08-14 21:22:13 -0400304 outp += sprintf(outp, " count 0x%03X", extra_delta_offset32);
Len Brown8e180f32012-09-22 01:25:08 -0400305 if (extra_delta_offset64)
Len Browne7c95ff2014-08-14 21:22:13 -0400306 outp += sprintf(outp, " COUNT 0x%03X", extra_delta_offset64);
Len Brown2f32edf2012-09-21 23:45:46 -0400307 if (extra_msr_offset32)
Len Browne7c95ff2014-08-14 21:22:13 -0400308 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset32);
Len Brown2f32edf2012-09-21 23:45:46 -0400309 if (extra_msr_offset64)
Len Browne7c95ff2014-08-14 21:22:13 -0400310 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset64);
Len Brown1cc21f72015-02-23 00:34:57 -0500311
312 if (!debug)
313 goto done;
314
315 if (do_smi)
316 outp += sprintf(outp, " SMI");
317
Len Brown103a8fe2010-10-22 23:53:03 -0400318 if (do_nhm_cstates)
Len Browne7c95ff2014-08-14 21:22:13 -0400319 outp += sprintf(outp, " CPU%%c1");
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -0700320 if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates)
Len Browne7c95ff2014-08-14 21:22:13 -0400321 outp += sprintf(outp, " CPU%%c3");
Len Brown103a8fe2010-10-22 23:53:03 -0400322 if (do_nhm_cstates)
Len Browne7c95ff2014-08-14 21:22:13 -0400323 outp += sprintf(outp, " CPU%%c6");
Len Brown103a8fe2010-10-22 23:53:03 -0400324 if (do_snb_cstates)
Len Browne7c95ff2014-08-14 21:22:13 -0400325 outp += sprintf(outp, " CPU%%c7");
Len Brown889facb2012-11-08 00:48:57 -0500326
327 if (do_dts)
Len Browne7c95ff2014-08-14 21:22:13 -0400328 outp += sprintf(outp, " CoreTmp");
Len Brown889facb2012-11-08 00:48:57 -0500329 if (do_ptm)
Len Browne7c95ff2014-08-14 21:22:13 -0400330 outp += sprintf(outp, " PkgTmp");
Len Brown889facb2012-11-08 00:48:57 -0500331
Len Brown0b2bb692015-03-26 00:50:30 -0400332 if (do_skl_residency) {
333 outp += sprintf(outp, " Totl%%C0");
334 outp += sprintf(outp, " Any%%C0");
335 outp += sprintf(outp, " GFX%%C0");
336 outp += sprintf(outp, " CPUGFX%%");
337 }
338
Len Brownee7e38e2015-02-09 23:39:45 -0500339 if (do_pc2)
Len Browne7c95ff2014-08-14 21:22:13 -0400340 outp += sprintf(outp, " Pkg%%pc2");
Len Brownee7e38e2015-02-09 23:39:45 -0500341 if (do_pc3)
Len Browne7c95ff2014-08-14 21:22:13 -0400342 outp += sprintf(outp, " Pkg%%pc3");
Len Brownee7e38e2015-02-09 23:39:45 -0500343 if (do_pc6)
Len Browne7c95ff2014-08-14 21:22:13 -0400344 outp += sprintf(outp, " Pkg%%pc6");
Len Brownee7e38e2015-02-09 23:39:45 -0500345 if (do_pc7)
Len Browne7c95ff2014-08-14 21:22:13 -0400346 outp += sprintf(outp, " Pkg%%pc7");
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800347 if (do_c8_c9_c10) {
Len Browne7c95ff2014-08-14 21:22:13 -0400348 outp += sprintf(outp, " Pkg%%pc8");
349 outp += sprintf(outp, " Pkg%%pc9");
350 outp += sprintf(outp, " Pk%%pc10");
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800351 }
Len Brown103a8fe2010-10-22 23:53:03 -0400352
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800353 if (do_rapl && !rapl_joules) {
354 if (do_rapl & RAPL_PKG)
Len Browne7c95ff2014-08-14 21:22:13 -0400355 outp += sprintf(outp, " PkgWatt");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800356 if (do_rapl & RAPL_CORES)
Len Browne7c95ff2014-08-14 21:22:13 -0400357 outp += sprintf(outp, " CorWatt");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800358 if (do_rapl & RAPL_GFX)
Len Browne7c95ff2014-08-14 21:22:13 -0400359 outp += sprintf(outp, " GFXWatt");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800360 if (do_rapl & RAPL_DRAM)
Len Browne7c95ff2014-08-14 21:22:13 -0400361 outp += sprintf(outp, " RAMWatt");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800362 if (do_rapl & RAPL_PKG_PERF_STATUS)
Len Browne7c95ff2014-08-14 21:22:13 -0400363 outp += sprintf(outp, " PKG_%%");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800364 if (do_rapl & RAPL_DRAM_PERF_STATUS)
Len Browne7c95ff2014-08-14 21:22:13 -0400365 outp += sprintf(outp, " RAM_%%");
Len Brownd7899442015-01-23 00:12:33 -0500366 } else if (do_rapl && rapl_joules) {
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800367 if (do_rapl & RAPL_PKG)
Len Browne7c95ff2014-08-14 21:22:13 -0400368 outp += sprintf(outp, " Pkg_J");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800369 if (do_rapl & RAPL_CORES)
Len Browne7c95ff2014-08-14 21:22:13 -0400370 outp += sprintf(outp, " Cor_J");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800371 if (do_rapl & RAPL_GFX)
Len Browne7c95ff2014-08-14 21:22:13 -0400372 outp += sprintf(outp, " GFX_J");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800373 if (do_rapl & RAPL_DRAM)
Len Browne7c95ff2014-08-14 21:22:13 -0400374 outp += sprintf(outp, " RAM_W");
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_%%");
379 outp += sprintf(outp, " time");
Len Brown889facb2012-11-08 00:48:57 -0500380
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800381 }
Len Brown1cc21f72015-02-23 00:34:57 -0500382 done:
Len Brownc98d5d92012-06-04 00:56:40 -0400383 outp += sprintf(outp, "\n");
Len Brown103a8fe2010-10-22 23:53:03 -0400384}
385
Len Brownc98d5d92012-06-04 00:56:40 -0400386int dump_counters(struct thread_data *t, struct core_data *c,
387 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400388{
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200389 outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400390
Len Brownc98d5d92012-06-04 00:56:40 -0400391 if (t) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200392 outp += sprintf(outp, "CPU: %d flags 0x%x\n",
393 t->cpu_id, t->flags);
394 outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
395 outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
396 outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
397 outp += sprintf(outp, "c1: %016llX\n", t->c1);
398 outp += sprintf(outp, "msr0x%x: %08llX\n",
Len Brown8e180f32012-09-22 01:25:08 -0400399 extra_delta_offset32, t->extra_delta32);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200400 outp += sprintf(outp, "msr0x%x: %016llX\n",
Len Brown8e180f32012-09-22 01:25:08 -0400401 extra_delta_offset64, t->extra_delta64);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200402 outp += sprintf(outp, "msr0x%x: %08llX\n",
Len Brown2f32edf2012-09-21 23:45:46 -0400403 extra_msr_offset32, t->extra_msr32);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200404 outp += sprintf(outp, "msr0x%x: %016llX\n",
Len Brown2f32edf2012-09-21 23:45:46 -0400405 extra_msr_offset64, t->extra_msr64);
Len Brown1ed51012013-02-10 17:19:24 -0500406 if (do_smi)
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200407 outp += sprintf(outp, "SMI: %08X\n", t->smi_count);
Len Brownc98d5d92012-06-04 00:56:40 -0400408 }
Len Brown103a8fe2010-10-22 23:53:03 -0400409
Len Brownc98d5d92012-06-04 00:56:40 -0400410 if (c) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200411 outp += sprintf(outp, "core: %d\n", c->core_id);
412 outp += sprintf(outp, "c3: %016llX\n", c->c3);
413 outp += sprintf(outp, "c6: %016llX\n", c->c6);
414 outp += sprintf(outp, "c7: %016llX\n", c->c7);
415 outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
Len Brownc98d5d92012-06-04 00:56:40 -0400416 }
417
418 if (p) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200419 outp += sprintf(outp, "package: %d\n", p->package_id);
Len Brown0b2bb692015-03-26 00:50:30 -0400420
421 outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
422 outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
423 outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
424 outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
425
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200426 outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
Len Brownee7e38e2015-02-09 23:39:45 -0500427 if (do_pc3)
428 outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
429 if (do_pc6)
430 outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
431 if (do_pc7)
432 outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200433 outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
434 outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
435 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
436 outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
437 outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
438 outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
439 outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
440 outp += sprintf(outp, "Throttle PKG: %0X\n",
441 p->rapl_pkg_perf_status);
442 outp += sprintf(outp, "Throttle RAM: %0X\n",
443 p->rapl_dram_perf_status);
444 outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
Len Brownc98d5d92012-06-04 00:56:40 -0400445 }
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200446
447 outp += sprintf(outp, "\n");
448
Len Brownc98d5d92012-06-04 00:56:40 -0400449 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400450}
451
Len Browne23da032012-02-06 18:37:16 -0500452/*
453 * column formatting convention & formats
Len Browne23da032012-02-06 18:37:16 -0500454 */
Len Brownc98d5d92012-06-04 00:56:40 -0400455int format_counters(struct thread_data *t, struct core_data *c,
456 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400457{
458 double interval_float;
Len Brownfc04cc62014-02-06 00:55:19 -0500459 char *fmt8;
Len Brown103a8fe2010-10-22 23:53:03 -0400460
Len Brownc98d5d92012-06-04 00:56:40 -0400461 /* if showing only 1st thread in core and this isn't one, bail out */
462 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
463 return 0;
464
465 /* if showing only 1st thread in pkg and this isn't one, bail out */
466 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
467 return 0;
468
Len Brown103a8fe2010-10-22 23:53:03 -0400469 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
470
Len Brownc98d5d92012-06-04 00:56:40 -0400471 /* topo columns, print blanks on 1st (average) line */
472 if (t == &average.threads) {
Len Brown103a8fe2010-10-22 23:53:03 -0400473 if (show_pkg)
Len Brownfc04cc62014-02-06 00:55:19 -0500474 outp += sprintf(outp, " -");
Len Brown103a8fe2010-10-22 23:53:03 -0400475 if (show_core)
Len Brownfc04cc62014-02-06 00:55:19 -0500476 outp += sprintf(outp, " -");
Len Brown103a8fe2010-10-22 23:53:03 -0400477 if (show_cpu)
Len Brownfc04cc62014-02-06 00:55:19 -0500478 outp += sprintf(outp, " -");
Len Brown103a8fe2010-10-22 23:53:03 -0400479 } else {
Len Brownc98d5d92012-06-04 00:56:40 -0400480 if (show_pkg) {
481 if (p)
Len Brownfc04cc62014-02-06 00:55:19 -0500482 outp += sprintf(outp, "%8d", p->package_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400483 else
Len Brownfc04cc62014-02-06 00:55:19 -0500484 outp += sprintf(outp, " -");
Len Brownc98d5d92012-06-04 00:56:40 -0400485 }
Len Brownc98d5d92012-06-04 00:56:40 -0400486 if (show_core) {
487 if (c)
Len Brownfc04cc62014-02-06 00:55:19 -0500488 outp += sprintf(outp, "%8d", c->core_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400489 else
Len Brownfc04cc62014-02-06 00:55:19 -0500490 outp += sprintf(outp, " -");
Len Brownc98d5d92012-06-04 00:56:40 -0400491 }
Len Brown103a8fe2010-10-22 23:53:03 -0400492 if (show_cpu)
Len Brownfc04cc62014-02-06 00:55:19 -0500493 outp += sprintf(outp, "%8d", t->cpu_id);
Len Brown103a8fe2010-10-22 23:53:03 -0400494 }
Len Brownfc04cc62014-02-06 00:55:19 -0500495
Len Brownd7899442015-01-23 00:12:33 -0500496 /* Avg_MHz */
Len Brownfc04cc62014-02-06 00:55:19 -0500497 if (has_aperf)
498 outp += sprintf(outp, "%8.0f",
499 1.0 / units * t->aperf / interval_float);
500
Len Brownd7899442015-01-23 00:12:33 -0500501 /* %Busy */
502 if (has_aperf) {
Len Brown103a8fe2010-10-22 23:53:03 -0400503 if (!skip_c0)
Len Brownfc04cc62014-02-06 00:55:19 -0500504 outp += sprintf(outp, "%8.2f", 100.0 * t->mperf/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400505 else
Len Brownfc04cc62014-02-06 00:55:19 -0500506 outp += sprintf(outp, "********");
Len Brown103a8fe2010-10-22 23:53:03 -0400507 }
508
Len Brownd7899442015-01-23 00:12:33 -0500509 /* Bzy_MHz */
Len Brownfc04cc62014-02-06 00:55:19 -0500510 if (has_aperf)
511 outp += sprintf(outp, "%8.0f",
512 1.0 * t->tsc / units * t->aperf / t->mperf / interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400513
Len Brownd7899442015-01-23 00:12:33 -0500514 /* TSC_MHz */
Len Brownfc04cc62014-02-06 00:55:19 -0500515 outp += sprintf(outp, "%8.0f", 1.0 * t->tsc/units/interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400516
Len Brown8e180f32012-09-22 01:25:08 -0400517 /* delta */
518 if (extra_delta_offset32)
519 outp += sprintf(outp, " %11llu", t->extra_delta32);
520
521 /* DELTA */
522 if (extra_delta_offset64)
523 outp += sprintf(outp, " %11llu", t->extra_delta64);
Len Brown2f32edf2012-09-21 23:45:46 -0400524 /* msr */
525 if (extra_msr_offset32)
Len Brown8e180f32012-09-22 01:25:08 -0400526 outp += sprintf(outp, " 0x%08llx", t->extra_msr32);
Len Brown2f32edf2012-09-21 23:45:46 -0400527
Len Brown130ff302012-09-21 22:56:06 -0400528 /* MSR */
Len Brown2f32edf2012-09-21 23:45:46 -0400529 if (extra_msr_offset64)
530 outp += sprintf(outp, " 0x%016llx", t->extra_msr64);
Len Brown130ff302012-09-21 22:56:06 -0400531
Len Brown1cc21f72015-02-23 00:34:57 -0500532 if (!debug)
533 goto done;
534
535 /* SMI */
536 if (do_smi)
537 outp += sprintf(outp, "%8d", t->smi_count);
538
Len Brown103a8fe2010-10-22 23:53:03 -0400539 if (do_nhm_cstates) {
540 if (!skip_c1)
Len Brownfc04cc62014-02-06 00:55:19 -0500541 outp += sprintf(outp, "%8.2f", 100.0 * t->c1/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400542 else
Len Brownfc04cc62014-02-06 00:55:19 -0500543 outp += sprintf(outp, "********");
Len Brown103a8fe2010-10-22 23:53:03 -0400544 }
Len Brownc98d5d92012-06-04 00:56:40 -0400545
546 /* print per-core data only for 1st thread in core */
547 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
548 goto done;
549
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -0700550 if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates)
Len Brownfc04cc62014-02-06 00:55:19 -0500551 outp += sprintf(outp, "%8.2f", 100.0 * c->c3/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400552 if (do_nhm_cstates)
Len Brownfc04cc62014-02-06 00:55:19 -0500553 outp += sprintf(outp, "%8.2f", 100.0 * c->c6/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400554 if (do_snb_cstates)
Len Brownfc04cc62014-02-06 00:55:19 -0500555 outp += sprintf(outp, "%8.2f", 100.0 * c->c7/t->tsc);
Len Brownc98d5d92012-06-04 00:56:40 -0400556
Len Brown889facb2012-11-08 00:48:57 -0500557 if (do_dts)
Len Brownfc04cc62014-02-06 00:55:19 -0500558 outp += sprintf(outp, "%8d", c->core_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500559
Len Brownc98d5d92012-06-04 00:56:40 -0400560 /* print per-package data only for 1st core in package */
561 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
562 goto done;
563
Len Brown0b2bb692015-03-26 00:50:30 -0400564 /* PkgTmp */
Len Brown889facb2012-11-08 00:48:57 -0500565 if (do_ptm)
Len Brownfc04cc62014-02-06 00:55:19 -0500566 outp += sprintf(outp, "%8d", p->pkg_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500567
Len Brown0b2bb692015-03-26 00:50:30 -0400568 /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
569 if (do_skl_residency) {
570 outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_wtd_core_c0/t->tsc);
571 outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_any_core_c0/t->tsc);
572 outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_any_gfxe_c0/t->tsc);
573 outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_both_core_gfxe_c0/t->tsc);
574 }
575
Len Brownee7e38e2015-02-09 23:39:45 -0500576 if (do_pc2)
Len Brownfc04cc62014-02-06 00:55:19 -0500577 outp += sprintf(outp, "%8.2f", 100.0 * p->pc2/t->tsc);
Len Brownee7e38e2015-02-09 23:39:45 -0500578 if (do_pc3)
Len Brownfc04cc62014-02-06 00:55:19 -0500579 outp += sprintf(outp, "%8.2f", 100.0 * p->pc3/t->tsc);
Len Brownee7e38e2015-02-09 23:39:45 -0500580 if (do_pc6)
Len Brownfc04cc62014-02-06 00:55:19 -0500581 outp += sprintf(outp, "%8.2f", 100.0 * p->pc6/t->tsc);
Len Brownee7e38e2015-02-09 23:39:45 -0500582 if (do_pc7)
Len Brownfc04cc62014-02-06 00:55:19 -0500583 outp += sprintf(outp, "%8.2f", 100.0 * p->pc7/t->tsc);
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800584 if (do_c8_c9_c10) {
Len Brownfc04cc62014-02-06 00:55:19 -0500585 outp += sprintf(outp, "%8.2f", 100.0 * p->pc8/t->tsc);
586 outp += sprintf(outp, "%8.2f", 100.0 * p->pc9/t->tsc);
587 outp += sprintf(outp, "%8.2f", 100.0 * p->pc10/t->tsc);
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800588 }
Len Brown889facb2012-11-08 00:48:57 -0500589
590 /*
591 * If measurement interval exceeds minimum RAPL Joule Counter range,
592 * indicate that results are suspect by printing "**" in fraction place.
593 */
Len Brownfc04cc62014-02-06 00:55:19 -0500594 if (interval_float < rapl_joule_counter_range)
595 fmt8 = "%8.2f";
596 else
597 fmt8 = " %6.0f**";
Len Brown889facb2012-11-08 00:48:57 -0500598
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800599 if (do_rapl && !rapl_joules) {
600 if (do_rapl & RAPL_PKG)
Len Brownfc04cc62014-02-06 00:55:19 -0500601 outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800602 if (do_rapl & RAPL_CORES)
Len Brownfc04cc62014-02-06 00:55:19 -0500603 outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800604 if (do_rapl & RAPL_GFX)
Len Brownfc04cc62014-02-06 00:55:19 -0500605 outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800606 if (do_rapl & RAPL_DRAM)
Andrey Semin40ee8e32014-12-05 00:07:00 -0500607 outp += sprintf(outp, fmt8, p->energy_dram * rapl_dram_energy_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800608 if (do_rapl & RAPL_PKG_PERF_STATUS)
Len Brownfc04cc62014-02-06 00:55:19 -0500609 outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800610 if (do_rapl & RAPL_DRAM_PERF_STATUS)
Len Brownfc04cc62014-02-06 00:55:19 -0500611 outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
Len Brownd7899442015-01-23 00:12:33 -0500612 } else if (do_rapl && rapl_joules) {
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800613 if (do_rapl & RAPL_PKG)
Len Brownfc04cc62014-02-06 00:55:19 -0500614 outp += sprintf(outp, fmt8,
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800615 p->energy_pkg * rapl_energy_units);
616 if (do_rapl & RAPL_CORES)
Len Brownfc04cc62014-02-06 00:55:19 -0500617 outp += sprintf(outp, fmt8,
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800618 p->energy_cores * rapl_energy_units);
619 if (do_rapl & RAPL_GFX)
Len Brownfc04cc62014-02-06 00:55:19 -0500620 outp += sprintf(outp, fmt8,
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800621 p->energy_gfx * rapl_energy_units);
622 if (do_rapl & RAPL_DRAM)
Len Brownfc04cc62014-02-06 00:55:19 -0500623 outp += sprintf(outp, fmt8,
Andrey Semin40ee8e32014-12-05 00:07:00 -0500624 p->energy_dram * rapl_dram_energy_units);
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 Brown889facb2012-11-08 00:48:57 -0500629
Len Brownd7899442015-01-23 00:12:33 -0500630 outp += sprintf(outp, fmt8, interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800631 }
Len Brownc98d5d92012-06-04 00:56:40 -0400632done:
Len Brownc98d5d92012-06-04 00:56:40 -0400633 outp += sprintf(outp, "\n");
634
635 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400636}
637
Len Brownc98d5d92012-06-04 00:56:40 -0400638void flush_stdout()
Len Brown103a8fe2010-10-22 23:53:03 -0400639{
Len Brownc98d5d92012-06-04 00:56:40 -0400640 fputs(output_buffer, stdout);
Len Brownddac0d62012-11-30 01:01:40 -0500641 fflush(stdout);
Len Brownc98d5d92012-06-04 00:56:40 -0400642 outp = output_buffer;
643}
644void flush_stderr()
645{
646 fputs(output_buffer, stderr);
647 outp = output_buffer;
648}
649void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
650{
Len Browne23da032012-02-06 18:37:16 -0500651 static int printed;
Len Brown103a8fe2010-10-22 23:53:03 -0400652
Len Browne23da032012-02-06 18:37:16 -0500653 if (!printed || !summary_only)
654 print_header();
Len Brown103a8fe2010-10-22 23:53:03 -0400655
Len Brownc98d5d92012-06-04 00:56:40 -0400656 if (topo.num_cpus > 1)
657 format_counters(&average.threads, &average.cores,
658 &average.packages);
Len Brown103a8fe2010-10-22 23:53:03 -0400659
Len Browne23da032012-02-06 18:37:16 -0500660 printed = 1;
661
662 if (summary_only)
663 return;
664
Len Brownc98d5d92012-06-04 00:56:40 -0400665 for_all_cpus(format_counters, t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400666}
667
Len Brown889facb2012-11-08 00:48:57 -0500668#define DELTA_WRAP32(new, old) \
669 if (new > old) { \
670 old = new - old; \
671 } else { \
672 old = 0x100000000 + new - old; \
673 }
674
Len Brownc98d5d92012-06-04 00:56:40 -0400675void
676delta_package(struct pkg_data *new, struct pkg_data *old)
Len Brown103a8fe2010-10-22 23:53:03 -0400677{
Len Brown0b2bb692015-03-26 00:50:30 -0400678
679 if (do_skl_residency) {
680 old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
681 old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
682 old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
683 old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0;
684 }
Len Brownc98d5d92012-06-04 00:56:40 -0400685 old->pc2 = new->pc2 - old->pc2;
Len Brownee7e38e2015-02-09 23:39:45 -0500686 if (do_pc3)
687 old->pc3 = new->pc3 - old->pc3;
688 if (do_pc6)
689 old->pc6 = new->pc6 - old->pc6;
690 if (do_pc7)
691 old->pc7 = new->pc7 - old->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800692 old->pc8 = new->pc8 - old->pc8;
693 old->pc9 = new->pc9 - old->pc9;
694 old->pc10 = new->pc10 - old->pc10;
Len Brown889facb2012-11-08 00:48:57 -0500695 old->pkg_temp_c = new->pkg_temp_c;
696
697 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
698 DELTA_WRAP32(new->energy_cores, old->energy_cores);
699 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
700 DELTA_WRAP32(new->energy_dram, old->energy_dram);
701 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
702 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
Len Brownc98d5d92012-06-04 00:56:40 -0400703}
Len Brown103a8fe2010-10-22 23:53:03 -0400704
Len Brownc98d5d92012-06-04 00:56:40 -0400705void
706delta_core(struct core_data *new, struct core_data *old)
707{
708 old->c3 = new->c3 - old->c3;
709 old->c6 = new->c6 - old->c6;
710 old->c7 = new->c7 - old->c7;
Len Brown889facb2012-11-08 00:48:57 -0500711 old->core_temp_c = new->core_temp_c;
Len Brownc98d5d92012-06-04 00:56:40 -0400712}
Len Brown103a8fe2010-10-22 23:53:03 -0400713
Len Brownc3ae3312012-06-13 21:31:46 -0400714/*
715 * old = new - old
716 */
Len Brownc98d5d92012-06-04 00:56:40 -0400717void
718delta_thread(struct thread_data *new, struct thread_data *old,
719 struct core_data *core_delta)
720{
721 old->tsc = new->tsc - old->tsc;
Len Brown103a8fe2010-10-22 23:53:03 -0400722
Len Brownc98d5d92012-06-04 00:56:40 -0400723 /* check for TSC < 1 Mcycles over interval */
Josh Triplettb2c95d92013-08-20 17:20:18 -0700724 if (old->tsc < (1000 * 1000))
725 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
726 "You can disable all c-states by booting with \"idle=poll\"\n"
727 "or just the deep ones with \"processor.max_cstate=1\"");
Len Brown103a8fe2010-10-22 23:53:03 -0400728
Len Brownc98d5d92012-06-04 00:56:40 -0400729 old->c1 = new->c1 - old->c1;
Len Brown103a8fe2010-10-22 23:53:03 -0400730
Len Browna7296172015-01-23 01:33:58 -0500731 if (has_aperf) {
732 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
733 old->aperf = new->aperf - old->aperf;
734 old->mperf = new->mperf - old->mperf;
735 } else {
Len Brown103a8fe2010-10-22 23:53:03 -0400736
Len Browna7296172015-01-23 01:33:58 -0500737 if (!aperf_mperf_unstable) {
738 fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname);
739 fprintf(stderr, "* Frequency results do not cover entire interval *\n");
740 fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n");
Len Brownc98d5d92012-06-04 00:56:40 -0400741
Len Browna7296172015-01-23 01:33:58 -0500742 aperf_mperf_unstable = 1;
743 }
744 /*
745 * mperf delta is likely a huge "positive" number
746 * can not use it for calculating c0 time
747 */
748 skip_c0 = 1;
749 skip_c1 = 1;
Len Brownc98d5d92012-06-04 00:56:40 -0400750 }
Len Brownc98d5d92012-06-04 00:56:40 -0400751 }
Len Brown103a8fe2010-10-22 23:53:03 -0400752
Len Brown103a8fe2010-10-22 23:53:03 -0400753
Len Brown144b44b2013-11-09 00:30:16 -0500754 if (use_c1_residency_msr) {
755 /*
756 * Some models have a dedicated C1 residency MSR,
757 * which should be more accurate than the derivation below.
758 */
759 } else {
760 /*
761 * As counter collection is not atomic,
762 * it is possible for mperf's non-halted cycles + idle states
763 * to exceed TSC's all cycles: show c1 = 0% in that case.
764 */
765 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
766 old->c1 = 0;
767 else {
768 /* normal case, derive c1 */
769 old->c1 = old->tsc - old->mperf - core_delta->c3
Len Brownc98d5d92012-06-04 00:56:40 -0400770 - core_delta->c6 - core_delta->c7;
Len Brown144b44b2013-11-09 00:30:16 -0500771 }
Len Brownc98d5d92012-06-04 00:56:40 -0400772 }
Len Brownc3ae3312012-06-13 21:31:46 -0400773
Len Brownc98d5d92012-06-04 00:56:40 -0400774 if (old->mperf == 0) {
Len Brownd8af6f52015-02-10 01:56:38 -0500775 if (debug > 1) fprintf(stderr, "cpu%d MPERF 0!\n", old->cpu_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400776 old->mperf = 1; /* divide by 0 protection */
777 }
778
Len Brown8e180f32012-09-22 01:25:08 -0400779 old->extra_delta32 = new->extra_delta32 - old->extra_delta32;
780 old->extra_delta32 &= 0xFFFFFFFF;
781
782 old->extra_delta64 = new->extra_delta64 - old->extra_delta64;
783
Len Brownc98d5d92012-06-04 00:56:40 -0400784 /*
Len Brown8e180f32012-09-22 01:25:08 -0400785 * Extra MSR is just a snapshot, simply copy latest w/o subtracting
Len Brownc98d5d92012-06-04 00:56:40 -0400786 */
Len Brown2f32edf2012-09-21 23:45:46 -0400787 old->extra_msr32 = new->extra_msr32;
788 old->extra_msr64 = new->extra_msr64;
Len Brown1ed51012013-02-10 17:19:24 -0500789
790 if (do_smi)
791 old->smi_count = new->smi_count - old->smi_count;
Len Brownc98d5d92012-06-04 00:56:40 -0400792}
793
794int delta_cpu(struct thread_data *t, struct core_data *c,
795 struct pkg_data *p, struct thread_data *t2,
796 struct core_data *c2, struct pkg_data *p2)
797{
798 /* calculate core delta only for 1st thread in core */
799 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
800 delta_core(c, c2);
801
802 /* always calculate thread delta */
803 delta_thread(t, t2, c2); /* c2 is core delta */
804
805 /* calculate package delta only for 1st core in package */
806 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
807 delta_package(p, p2);
808
809 return 0;
810}
811
812void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
813{
814 t->tsc = 0;
815 t->aperf = 0;
816 t->mperf = 0;
817 t->c1 = 0;
818
Len Brown1ed51012013-02-10 17:19:24 -0500819 t->smi_count = 0;
Len Brown8e180f32012-09-22 01:25:08 -0400820 t->extra_delta32 = 0;
821 t->extra_delta64 = 0;
822
Len Brownc98d5d92012-06-04 00:56:40 -0400823 /* tells format_counters to dump all fields from this set */
824 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
825
826 c->c3 = 0;
827 c->c6 = 0;
828 c->c7 = 0;
Len Brown889facb2012-11-08 00:48:57 -0500829 c->core_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -0400830
Len Brown0b2bb692015-03-26 00:50:30 -0400831 p->pkg_wtd_core_c0 = 0;
832 p->pkg_any_core_c0 = 0;
833 p->pkg_any_gfxe_c0 = 0;
834 p->pkg_both_core_gfxe_c0 = 0;
835
Len Brownc98d5d92012-06-04 00:56:40 -0400836 p->pc2 = 0;
Len Brownee7e38e2015-02-09 23:39:45 -0500837 if (do_pc3)
838 p->pc3 = 0;
839 if (do_pc6)
840 p->pc6 = 0;
841 if (do_pc7)
842 p->pc7 = 0;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800843 p->pc8 = 0;
844 p->pc9 = 0;
845 p->pc10 = 0;
Len Brown889facb2012-11-08 00:48:57 -0500846
847 p->energy_pkg = 0;
848 p->energy_dram = 0;
849 p->energy_cores = 0;
850 p->energy_gfx = 0;
851 p->rapl_pkg_perf_status = 0;
852 p->rapl_dram_perf_status = 0;
853 p->pkg_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -0400854}
855int sum_counters(struct thread_data *t, struct core_data *c,
856 struct pkg_data *p)
857{
858 average.threads.tsc += t->tsc;
859 average.threads.aperf += t->aperf;
860 average.threads.mperf += t->mperf;
861 average.threads.c1 += t->c1;
862
Len Brown8e180f32012-09-22 01:25:08 -0400863 average.threads.extra_delta32 += t->extra_delta32;
864 average.threads.extra_delta64 += t->extra_delta64;
865
Len Brownc98d5d92012-06-04 00:56:40 -0400866 /* sum per-core values only for 1st thread in core */
867 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
868 return 0;
869
870 average.cores.c3 += c->c3;
871 average.cores.c6 += c->c6;
872 average.cores.c7 += c->c7;
873
Len Brown889facb2012-11-08 00:48:57 -0500874 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
875
Len Brownc98d5d92012-06-04 00:56:40 -0400876 /* sum per-pkg values only for 1st core in pkg */
877 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
878 return 0;
879
Len Brown0b2bb692015-03-26 00:50:30 -0400880 if (do_skl_residency) {
881 average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
882 average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
883 average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
884 average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
885 }
886
Len Brownc98d5d92012-06-04 00:56:40 -0400887 average.packages.pc2 += p->pc2;
Len Brownee7e38e2015-02-09 23:39:45 -0500888 if (do_pc3)
889 average.packages.pc3 += p->pc3;
890 if (do_pc6)
891 average.packages.pc6 += p->pc6;
892 if (do_pc7)
893 average.packages.pc7 += p->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800894 average.packages.pc8 += p->pc8;
895 average.packages.pc9 += p->pc9;
896 average.packages.pc10 += p->pc10;
Len Brownc98d5d92012-06-04 00:56:40 -0400897
Len Brown889facb2012-11-08 00:48:57 -0500898 average.packages.energy_pkg += p->energy_pkg;
899 average.packages.energy_dram += p->energy_dram;
900 average.packages.energy_cores += p->energy_cores;
901 average.packages.energy_gfx += p->energy_gfx;
902
903 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
904
905 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
906 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
Len Brownc98d5d92012-06-04 00:56:40 -0400907 return 0;
908}
909/*
910 * sum the counters for all cpus in the system
911 * compute the weighted average
912 */
913void compute_average(struct thread_data *t, struct core_data *c,
914 struct pkg_data *p)
915{
916 clear_counters(&average.threads, &average.cores, &average.packages);
917
918 for_all_cpus(sum_counters, t, c, p);
919
920 average.threads.tsc /= topo.num_cpus;
921 average.threads.aperf /= topo.num_cpus;
922 average.threads.mperf /= topo.num_cpus;
923 average.threads.c1 /= topo.num_cpus;
924
Len Brown8e180f32012-09-22 01:25:08 -0400925 average.threads.extra_delta32 /= topo.num_cpus;
926 average.threads.extra_delta32 &= 0xFFFFFFFF;
927
928 average.threads.extra_delta64 /= topo.num_cpus;
929
Len Brownc98d5d92012-06-04 00:56:40 -0400930 average.cores.c3 /= topo.num_cores;
931 average.cores.c6 /= topo.num_cores;
932 average.cores.c7 /= topo.num_cores;
933
Len Brown0b2bb692015-03-26 00:50:30 -0400934 if (do_skl_residency) {
935 average.packages.pkg_wtd_core_c0 /= topo.num_packages;
936 average.packages.pkg_any_core_c0 /= topo.num_packages;
937 average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
938 average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
939 }
940
Len Brownc98d5d92012-06-04 00:56:40 -0400941 average.packages.pc2 /= topo.num_packages;
Len Brownee7e38e2015-02-09 23:39:45 -0500942 if (do_pc3)
943 average.packages.pc3 /= topo.num_packages;
944 if (do_pc6)
945 average.packages.pc6 /= topo.num_packages;
946 if (do_pc7)
947 average.packages.pc7 /= topo.num_packages;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800948
949 average.packages.pc8 /= topo.num_packages;
950 average.packages.pc9 /= topo.num_packages;
951 average.packages.pc10 /= topo.num_packages;
Len Brownc98d5d92012-06-04 00:56:40 -0400952}
953
954static unsigned long long rdtsc(void)
955{
956 unsigned int low, high;
957
958 asm volatile("rdtsc" : "=a" (low), "=d" (high));
959
960 return low | ((unsigned long long)high) << 32;
961}
962
963
964/*
965 * get_counters(...)
966 * migrate to cpu
967 * acquire and record local counters for that cpu
968 */
969int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
970{
971 int cpu = t->cpu_id;
Len Brown889facb2012-11-08 00:48:57 -0500972 unsigned long long msr;
Len Brownc98d5d92012-06-04 00:56:40 -0400973
Len Browne52966c2012-11-08 22:38:05 -0500974 if (cpu_migrate(cpu)) {
975 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
Len Brownc98d5d92012-06-04 00:56:40 -0400976 return -1;
Len Browne52966c2012-11-08 22:38:05 -0500977 }
Len Brownc98d5d92012-06-04 00:56:40 -0400978
979 t->tsc = rdtsc(); /* we are running on local CPU of interest */
980
981 if (has_aperf) {
Len Brown9c63a652012-10-31 01:29:52 -0400982 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
Len Brownc98d5d92012-06-04 00:56:40 -0400983 return -3;
Len Brown9c63a652012-10-31 01:29:52 -0400984 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
Len Brownc98d5d92012-06-04 00:56:40 -0400985 return -4;
986 }
987
Len Brown1ed51012013-02-10 17:19:24 -0500988 if (do_smi) {
989 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
990 return -5;
991 t->smi_count = msr & 0xFFFFFFFF;
992 }
Len Brown8e180f32012-09-22 01:25:08 -0400993 if (extra_delta_offset32) {
Len Brown889facb2012-11-08 00:48:57 -0500994 if (get_msr(cpu, extra_delta_offset32, &msr))
Len Brown2f32edf2012-09-21 23:45:46 -0400995 return -5;
Len Brown889facb2012-11-08 00:48:57 -0500996 t->extra_delta32 = msr & 0xFFFFFFFF;
Len Brown8e180f32012-09-22 01:25:08 -0400997 }
998
999 if (extra_delta_offset64)
1000 if (get_msr(cpu, extra_delta_offset64, &t->extra_delta64))
1001 return -5;
1002
1003 if (extra_msr_offset32) {
Len Brown889facb2012-11-08 00:48:57 -05001004 if (get_msr(cpu, extra_msr_offset32, &msr))
Len Brown8e180f32012-09-22 01:25:08 -04001005 return -5;
Len Brown889facb2012-11-08 00:48:57 -05001006 t->extra_msr32 = msr & 0xFFFFFFFF;
Len Brown8e180f32012-09-22 01:25:08 -04001007 }
Len Brown2f32edf2012-09-21 23:45:46 -04001008
1009 if (extra_msr_offset64)
1010 if (get_msr(cpu, extra_msr_offset64, &t->extra_msr64))
Len Brownc98d5d92012-06-04 00:56:40 -04001011 return -5;
1012
Len Brown144b44b2013-11-09 00:30:16 -05001013 if (use_c1_residency_msr) {
1014 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1015 return -6;
1016 }
1017
Len Brownc98d5d92012-06-04 00:56:40 -04001018 /* collect core counters only for 1st thread in core */
1019 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1020 return 0;
1021
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001022 if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001023 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1024 return -6;
Len Brown144b44b2013-11-09 00:30:16 -05001025 }
1026
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001027 if (do_nhm_cstates && !do_knl_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -04001028 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1029 return -7;
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001030 } else if (do_knl_cstates) {
1031 if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1032 return -7;
Len Brownc98d5d92012-06-04 00:56:40 -04001033 }
1034
1035 if (do_snb_cstates)
1036 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1037 return -8;
1038
Len Brown889facb2012-11-08 00:48:57 -05001039 if (do_dts) {
1040 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1041 return -9;
1042 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1043 }
1044
1045
Len Brownc98d5d92012-06-04 00:56:40 -04001046 /* collect package counters only for 1st core in package */
1047 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1048 return 0;
1049
Len Brown0b2bb692015-03-26 00:50:30 -04001050 if (do_skl_residency) {
1051 if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
1052 return -10;
1053 if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
1054 return -11;
1055 if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
1056 return -12;
1057 if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
1058 return -13;
1059 }
Len Brownee7e38e2015-02-09 23:39:45 -05001060 if (do_pc3)
Len Brownc98d5d92012-06-04 00:56:40 -04001061 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1062 return -9;
Len Brownee7e38e2015-02-09 23:39:45 -05001063 if (do_pc6)
Len Brownc98d5d92012-06-04 00:56:40 -04001064 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1065 return -10;
Len Brownee7e38e2015-02-09 23:39:45 -05001066 if (do_pc2)
Len Brownc98d5d92012-06-04 00:56:40 -04001067 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1068 return -11;
Len Brownee7e38e2015-02-09 23:39:45 -05001069 if (do_pc7)
Len Brownc98d5d92012-06-04 00:56:40 -04001070 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1071 return -12;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001072 if (do_c8_c9_c10) {
1073 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1074 return -13;
1075 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1076 return -13;
1077 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1078 return -13;
1079 }
Len Brown889facb2012-11-08 00:48:57 -05001080 if (do_rapl & RAPL_PKG) {
1081 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1082 return -13;
1083 p->energy_pkg = msr & 0xFFFFFFFF;
1084 }
1085 if (do_rapl & RAPL_CORES) {
1086 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1087 return -14;
1088 p->energy_cores = msr & 0xFFFFFFFF;
1089 }
1090 if (do_rapl & RAPL_DRAM) {
1091 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1092 return -15;
1093 p->energy_dram = msr & 0xFFFFFFFF;
1094 }
1095 if (do_rapl & RAPL_GFX) {
1096 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1097 return -16;
1098 p->energy_gfx = msr & 0xFFFFFFFF;
1099 }
1100 if (do_rapl & RAPL_PKG_PERF_STATUS) {
1101 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1102 return -16;
1103 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1104 }
1105 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1106 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1107 return -16;
1108 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1109 }
1110 if (do_ptm) {
1111 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1112 return -17;
1113 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1114 }
Len Brown103a8fe2010-10-22 23:53:03 -04001115 return 0;
1116}
1117
Len Brownee7e38e2015-02-09 23:39:45 -05001118/*
1119 * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1120 * If you change the values, note they are used both in comparisons
1121 * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1122 */
1123
1124#define PCLUKN 0 /* Unknown */
1125#define PCLRSV 1 /* Reserved */
1126#define PCL__0 2 /* PC0 */
1127#define PCL__1 3 /* PC1 */
1128#define PCL__2 4 /* PC2 */
1129#define PCL__3 5 /* PC3 */
1130#define PCL__4 6 /* PC4 */
1131#define PCL__6 7 /* PC6 */
1132#define PCL_6N 8 /* PC6 No Retention */
1133#define PCL_6R 9 /* PC6 Retention */
1134#define PCL__7 10 /* PC7 */
1135#define PCL_7S 11 /* PC7 Shrink */
Len Brown0b2bb692015-03-26 00:50:30 -04001136#define PCL__8 12 /* PC8 */
1137#define PCL__9 13 /* PC9 */
1138#define PCLUNL 14 /* Unlimited */
Len Brownee7e38e2015-02-09 23:39:45 -05001139
1140int pkg_cstate_limit = PCLUKN;
1141char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
Len Brown0b2bb692015-03-26 00:50:30 -04001142 "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"};
Len Brownee7e38e2015-02-09 23:39:45 -05001143
Len Browne9257f52015-04-01 21:02:57 -04001144int 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};
1145int 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};
1146int 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};
1147int 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};
1148int 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};
1149int 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 -05001150
Len Brownfcd17212015-03-23 20:29:09 -04001151static void
1152dump_nhm_platform_info(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001153{
1154 unsigned long long msr;
1155 unsigned int ratio;
1156
Len Brown9c63a652012-10-31 01:29:52 -04001157 get_msr(0, MSR_NHM_PLATFORM_INFO, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001158
Len Brown67920412013-01-31 15:22:15 -05001159 fprintf(stderr, "cpu0: MSR_NHM_PLATFORM_INFO: 0x%08llx\n", msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001160
Len Brown103a8fe2010-10-22 23:53:03 -04001161 ratio = (msr >> 40) & 0xFF;
Len Brown8f61f352015-03-23 18:36:57 -04001162 fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001163 ratio, bclk, ratio * bclk);
1164
1165 ratio = (msr >> 8) & 0xFF;
Len Brown8f61f352015-03-23 18:36:57 -04001166 fprintf(stderr, "%d * %.0f = %.0f MHz base frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001167 ratio, bclk, ratio * bclk);
1168
Len Brown67920412013-01-31 15:22:15 -05001169 get_msr(0, MSR_IA32_POWER_CTL, &msr);
Len Brown144b44b2013-11-09 00:30:16 -05001170 fprintf(stderr, "cpu0: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
Len Brown67920412013-01-31 15:22:15 -05001171 msr, msr & 0x2 ? "EN" : "DIS");
1172
Len Brownfcd17212015-03-23 20:29:09 -04001173 return;
1174}
1175
1176static void
1177dump_hsw_turbo_ratio_limits(void)
1178{
1179 unsigned long long msr;
1180 unsigned int ratio;
1181
1182 get_msr(0, MSR_TURBO_RATIO_LIMIT2, &msr);
1183
1184 fprintf(stderr, "cpu0: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", msr);
1185
1186 ratio = (msr >> 8) & 0xFF;
1187 if (ratio)
1188 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 18 active cores\n",
1189 ratio, bclk, ratio * bclk);
1190
1191 ratio = (msr >> 0) & 0xFF;
1192 if (ratio)
1193 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 17 active cores\n",
1194 ratio, bclk, ratio * bclk);
1195 return;
1196}
1197
1198static void
1199dump_ivt_turbo_ratio_limits(void)
1200{
1201 unsigned long long msr;
1202 unsigned int ratio;
Len Brown6574a5d2012-09-21 00:01:31 -04001203
Len Brown12bb43c2015-04-13 16:08:18 -04001204 get_msr(0, MSR_TURBO_RATIO_LIMIT1, &msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001205
Len Brown12bb43c2015-04-13 16:08:18 -04001206 fprintf(stderr, "cpu0: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001207
1208 ratio = (msr >> 56) & 0xFF;
1209 if (ratio)
1210 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 16 active cores\n",
1211 ratio, bclk, ratio * bclk);
1212
1213 ratio = (msr >> 48) & 0xFF;
1214 if (ratio)
1215 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 15 active cores\n",
1216 ratio, bclk, ratio * bclk);
1217
1218 ratio = (msr >> 40) & 0xFF;
1219 if (ratio)
1220 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 14 active cores\n",
1221 ratio, bclk, ratio * bclk);
1222
1223 ratio = (msr >> 32) & 0xFF;
1224 if (ratio)
1225 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 13 active cores\n",
1226 ratio, bclk, ratio * bclk);
1227
1228 ratio = (msr >> 24) & 0xFF;
1229 if (ratio)
1230 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 12 active cores\n",
1231 ratio, bclk, ratio * bclk);
1232
1233 ratio = (msr >> 16) & 0xFF;
1234 if (ratio)
1235 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 11 active cores\n",
1236 ratio, bclk, ratio * bclk);
1237
1238 ratio = (msr >> 8) & 0xFF;
1239 if (ratio)
1240 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 10 active cores\n",
1241 ratio, bclk, ratio * bclk);
1242
1243 ratio = (msr >> 0) & 0xFF;
1244 if (ratio)
1245 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 9 active cores\n",
1246 ratio, bclk, ratio * bclk);
Len Brownfcd17212015-03-23 20:29:09 -04001247 return;
1248}
Len Brown6574a5d2012-09-21 00:01:31 -04001249
Len Brownfcd17212015-03-23 20:29:09 -04001250static void
1251dump_nhm_turbo_ratio_limits(void)
1252{
1253 unsigned long long msr;
1254 unsigned int ratio;
Len Brown103a8fe2010-10-22 23:53:03 -04001255
Len Brown12bb43c2015-04-13 16:08:18 -04001256 get_msr(0, MSR_TURBO_RATIO_LIMIT, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001257
Len Brown12bb43c2015-04-13 16:08:18 -04001258 fprintf(stderr, "cpu0: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001259
1260 ratio = (msr >> 56) & 0xFF;
1261 if (ratio)
1262 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 8 active cores\n",
1263 ratio, bclk, ratio * bclk);
1264
1265 ratio = (msr >> 48) & 0xFF;
1266 if (ratio)
1267 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 7 active cores\n",
1268 ratio, bclk, ratio * bclk);
1269
1270 ratio = (msr >> 40) & 0xFF;
1271 if (ratio)
1272 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 6 active cores\n",
1273 ratio, bclk, ratio * bclk);
1274
1275 ratio = (msr >> 32) & 0xFF;
1276 if (ratio)
1277 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 5 active cores\n",
1278 ratio, bclk, ratio * bclk);
1279
Len Brown103a8fe2010-10-22 23:53:03 -04001280 ratio = (msr >> 24) & 0xFF;
1281 if (ratio)
1282 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
1283 ratio, bclk, ratio * bclk);
1284
1285 ratio = (msr >> 16) & 0xFF;
1286 if (ratio)
1287 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
1288 ratio, bclk, ratio * bclk);
1289
1290 ratio = (msr >> 8) & 0xFF;
1291 if (ratio)
1292 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
1293 ratio, bclk, ratio * bclk);
1294
1295 ratio = (msr >> 0) & 0xFF;
1296 if (ratio)
1297 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
1298 ratio, bclk, ratio * bclk);
Len Brownfcd17212015-03-23 20:29:09 -04001299 return;
1300}
Len Brown3a9a9412014-08-15 02:39:52 -04001301
Len Brownfcd17212015-03-23 20:29:09 -04001302static void
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001303dump_knl_turbo_ratio_limits(void)
1304{
1305 int cores;
1306 unsigned int ratio;
1307 unsigned long long msr;
1308 int delta_cores;
1309 int delta_ratio;
1310 int i;
1311
1312 get_msr(0, MSR_NHM_TURBO_RATIO_LIMIT, &msr);
1313
1314 fprintf(stderr, "cpu0: MSR_NHM_TURBO_RATIO_LIMIT: 0x%08llx\n",
1315 msr);
1316
1317 /**
1318 * Turbo encoding in KNL is as follows:
1319 * [7:0] -- Base value of number of active cores of bucket 1.
1320 * [15:8] -- Base value of freq ratio of bucket 1.
1321 * [20:16] -- +ve delta of number of active cores of bucket 2.
1322 * i.e. active cores of bucket 2 =
1323 * active cores of bucket 1 + delta
1324 * [23:21] -- Negative delta of freq ratio of bucket 2.
1325 * i.e. freq ratio of bucket 2 =
1326 * freq ratio of bucket 1 - delta
1327 * [28:24]-- +ve delta of number of active cores of bucket 3.
1328 * [31:29]-- -ve delta of freq ratio of bucket 3.
1329 * [36:32]-- +ve delta of number of active cores of bucket 4.
1330 * [39:37]-- -ve delta of freq ratio of bucket 4.
1331 * [44:40]-- +ve delta of number of active cores of bucket 5.
1332 * [47:45]-- -ve delta of freq ratio of bucket 5.
1333 * [52:48]-- +ve delta of number of active cores of bucket 6.
1334 * [55:53]-- -ve delta of freq ratio of bucket 6.
1335 * [60:56]-- +ve delta of number of active cores of bucket 7.
1336 * [63:61]-- -ve delta of freq ratio of bucket 7.
1337 */
1338 cores = msr & 0xFF;
1339 ratio = (msr >> 8) && 0xFF;
1340 if (ratio > 0)
1341 fprintf(stderr,
1342 "%d * %.0f = %.0f MHz max turbo %d active cores\n",
1343 ratio, bclk, ratio * bclk, cores);
1344
1345 for (i = 16; i < 64; i = i + 8) {
1346 delta_cores = (msr >> i) & 0x1F;
1347 delta_ratio = (msr >> (i + 5)) && 0x7;
1348 if (!delta_cores || !delta_ratio)
1349 return;
1350 cores = cores + delta_cores;
1351 ratio = ratio - delta_ratio;
1352
1353 /** -ve ratios will make successive ratio calculations
1354 * negative. Hence return instead of carrying on.
1355 */
1356 if (ratio > 0)
1357 fprintf(stderr,
1358 "%d * %.0f = %.0f MHz max turbo %d active cores\n",
1359 ratio, bclk, ratio * bclk, cores);
1360 }
1361}
1362
1363static void
Len Brownfcd17212015-03-23 20:29:09 -04001364dump_nhm_cst_cfg(void)
1365{
1366 unsigned long long msr;
1367
1368 get_msr(0, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
1369
1370#define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
1371#define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
1372
1373 fprintf(stderr, "cpu0: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", msr);
1374
1375 fprintf(stderr, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: %s)\n",
1376 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
1377 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
1378 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
1379 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
1380 (msr & (1 << 15)) ? "" : "UN",
1381 (unsigned int)msr & 7,
1382 pkg_cstate_limit_strings[pkg_cstate_limit]);
1383 return;
Len Brown103a8fe2010-10-22 23:53:03 -04001384}
1385
Len Brownc98d5d92012-06-04 00:56:40 -04001386void free_all_buffers(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001387{
Len Brownc98d5d92012-06-04 00:56:40 -04001388 CPU_FREE(cpu_present_set);
1389 cpu_present_set = NULL;
1390 cpu_present_set = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001391
Len Brownc98d5d92012-06-04 00:56:40 -04001392 CPU_FREE(cpu_affinity_set);
1393 cpu_affinity_set = NULL;
1394 cpu_affinity_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001395
Len Brownc98d5d92012-06-04 00:56:40 -04001396 free(thread_even);
1397 free(core_even);
1398 free(package_even);
1399
1400 thread_even = NULL;
1401 core_even = NULL;
1402 package_even = NULL;
1403
1404 free(thread_odd);
1405 free(core_odd);
1406 free(package_odd);
1407
1408 thread_odd = NULL;
1409 core_odd = NULL;
1410 package_odd = NULL;
1411
1412 free(output_buffer);
1413 output_buffer = NULL;
1414 outp = NULL;
Len Brown103a8fe2010-10-22 23:53:03 -04001415}
1416
Len Brownc98d5d92012-06-04 00:56:40 -04001417/*
Josh Triplett57a42a32013-08-20 17:20:17 -07001418 * Open a file, and exit on failure
1419 */
1420FILE *fopen_or_die(const char *path, const char *mode)
1421{
1422 FILE *filep = fopen(path, "r");
Josh Triplettb2c95d92013-08-20 17:20:18 -07001423 if (!filep)
1424 err(1, "%s: open failed", path);
Josh Triplett57a42a32013-08-20 17:20:17 -07001425 return filep;
1426}
1427
1428/*
Josh Triplett95aebc42013-08-20 17:20:16 -07001429 * Parse a file containing a single int.
1430 */
1431int parse_int_file(const char *fmt, ...)
1432{
1433 va_list args;
1434 char path[PATH_MAX];
1435 FILE *filep;
1436 int value;
1437
1438 va_start(args, fmt);
1439 vsnprintf(path, sizeof(path), fmt, args);
1440 va_end(args);
Josh Triplett57a42a32013-08-20 17:20:17 -07001441 filep = fopen_or_die(path, "r");
Josh Triplettb2c95d92013-08-20 17:20:18 -07001442 if (fscanf(filep, "%d", &value) != 1)
1443 err(1, "%s: failed to parse number from file", path);
Josh Triplett95aebc42013-08-20 17:20:16 -07001444 fclose(filep);
1445 return value;
1446}
1447
1448/*
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001449 * get_cpu_position_in_core(cpu)
1450 * return the position of the CPU among its HT siblings in the core
1451 * return -1 if the sibling is not in list
Len Brownc98d5d92012-06-04 00:56:40 -04001452 */
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001453int get_cpu_position_in_core(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001454{
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001455 char path[64];
1456 FILE *filep;
1457 int this_cpu;
1458 char character;
1459 int i;
1460
1461 sprintf(path,
1462 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list",
1463 cpu);
1464 filep = fopen(path, "r");
1465 if (filep == NULL) {
1466 perror(path);
1467 exit(1);
1468 }
1469
1470 for (i = 0; i < topo.num_threads_per_core; i++) {
1471 fscanf(filep, "%d", &this_cpu);
1472 if (this_cpu == cpu) {
1473 fclose(filep);
1474 return i;
1475 }
1476
1477 /* Account for no separator after last thread*/
1478 if (i != (topo.num_threads_per_core - 1))
1479 fscanf(filep, "%c", &character);
1480 }
1481
1482 fclose(filep);
1483 return -1;
Len Brown103a8fe2010-10-22 23:53:03 -04001484}
1485
Len Brownc98d5d92012-06-04 00:56:40 -04001486/*
1487 * cpu_is_first_core_in_package(cpu)
1488 * return 1 if given CPU is 1st core in package
1489 */
1490int cpu_is_first_core_in_package(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001491{
Josh Triplett95aebc42013-08-20 17:20:16 -07001492 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001493}
1494
1495int get_physical_package_id(int cpu)
1496{
Josh Triplett95aebc42013-08-20 17:20:16 -07001497 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001498}
1499
1500int get_core_id(int cpu)
1501{
Josh Triplett95aebc42013-08-20 17:20:16 -07001502 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001503}
1504
Len Brownc98d5d92012-06-04 00:56:40 -04001505int get_num_ht_siblings(int cpu)
1506{
1507 char path[80];
1508 FILE *filep;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001509 int sib1;
1510 int matches = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001511 char character;
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001512 char str[100];
1513 char *ch;
Len Brownc98d5d92012-06-04 00:56:40 -04001514
1515 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
Josh Triplett57a42a32013-08-20 17:20:17 -07001516 filep = fopen_or_die(path, "r");
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001517
Len Brownc98d5d92012-06-04 00:56:40 -04001518 /*
1519 * file format:
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001520 * A ',' separated or '-' separated set of numbers
1521 * (eg 1-2 or 1,3,4,5)
Len Brownc98d5d92012-06-04 00:56:40 -04001522 */
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001523 fscanf(filep, "%d%c\n", &sib1, &character);
1524 fseek(filep, 0, SEEK_SET);
1525 fgets(str, 100, filep);
1526 ch = strchr(str, character);
1527 while (ch != NULL) {
1528 matches++;
1529 ch = strchr(ch+1, character);
1530 }
Len Brownc98d5d92012-06-04 00:56:40 -04001531
1532 fclose(filep);
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07001533 return matches+1;
Len Brownc98d5d92012-06-04 00:56:40 -04001534}
1535
Len Brown103a8fe2010-10-22 23:53:03 -04001536/*
Len Brownc98d5d92012-06-04 00:56:40 -04001537 * run func(thread, core, package) in topology order
1538 * skip non-present cpus
Len Brown103a8fe2010-10-22 23:53:03 -04001539 */
1540
Len Brownc98d5d92012-06-04 00:56:40 -04001541int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
1542 struct pkg_data *, struct thread_data *, struct core_data *,
1543 struct pkg_data *), struct thread_data *thread_base,
1544 struct core_data *core_base, struct pkg_data *pkg_base,
1545 struct thread_data *thread_base2, struct core_data *core_base2,
1546 struct pkg_data *pkg_base2)
1547{
1548 int retval, pkg_no, core_no, thread_no;
1549
1550 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
1551 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
1552 for (thread_no = 0; thread_no <
1553 topo.num_threads_per_core; ++thread_no) {
1554 struct thread_data *t, *t2;
1555 struct core_data *c, *c2;
1556 struct pkg_data *p, *p2;
1557
1558 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
1559
1560 if (cpu_is_not_present(t->cpu_id))
1561 continue;
1562
1563 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
1564
1565 c = GET_CORE(core_base, core_no, pkg_no);
1566 c2 = GET_CORE(core_base2, core_no, pkg_no);
1567
1568 p = GET_PKG(pkg_base, pkg_no);
1569 p2 = GET_PKG(pkg_base2, pkg_no);
1570
1571 retval = func(t, c, p, t2, c2, p2);
1572 if (retval)
1573 return retval;
1574 }
1575 }
1576 }
1577 return 0;
1578}
1579
1580/*
1581 * run func(cpu) on every cpu in /proc/stat
1582 * return max_cpu number
1583 */
1584int for_all_proc_cpus(int (func)(int))
Len Brown103a8fe2010-10-22 23:53:03 -04001585{
1586 FILE *fp;
Len Brownc98d5d92012-06-04 00:56:40 -04001587 int cpu_num;
Len Brown103a8fe2010-10-22 23:53:03 -04001588 int retval;
1589
Josh Triplett57a42a32013-08-20 17:20:17 -07001590 fp = fopen_or_die(proc_stat, "r");
Len Brown103a8fe2010-10-22 23:53:03 -04001591
1592 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
Josh Triplettb2c95d92013-08-20 17:20:18 -07001593 if (retval != 0)
1594 err(1, "%s: failed to parse format", proc_stat);
Len Brown103a8fe2010-10-22 23:53:03 -04001595
Len Brownc98d5d92012-06-04 00:56:40 -04001596 while (1) {
1597 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 -04001598 if (retval != 1)
1599 break;
1600
Len Brownc98d5d92012-06-04 00:56:40 -04001601 retval = func(cpu_num);
1602 if (retval) {
1603 fclose(fp);
1604 return(retval);
1605 }
Len Brown103a8fe2010-10-22 23:53:03 -04001606 }
1607 fclose(fp);
Len Brownc98d5d92012-06-04 00:56:40 -04001608 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001609}
1610
1611void re_initialize(void)
1612{
Len Brownc98d5d92012-06-04 00:56:40 -04001613 free_all_buffers();
1614 setup_all_buffers();
1615 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04001616}
1617
Len Brownc98d5d92012-06-04 00:56:40 -04001618
Len Brown103a8fe2010-10-22 23:53:03 -04001619/*
Len Brownc98d5d92012-06-04 00:56:40 -04001620 * count_cpus()
1621 * remember the last one seen, it will be the max
Len Brown103a8fe2010-10-22 23:53:03 -04001622 */
Len Brownc98d5d92012-06-04 00:56:40 -04001623int count_cpus(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001624{
Len Brownc98d5d92012-06-04 00:56:40 -04001625 if (topo.max_cpu_num < cpu)
1626 topo.max_cpu_num = cpu;
Len Brown103a8fe2010-10-22 23:53:03 -04001627
Len Brownc98d5d92012-06-04 00:56:40 -04001628 topo.num_cpus += 1;
1629 return 0;
1630}
1631int mark_cpu_present(int cpu)
1632{
1633 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brown15aaa342012-03-29 22:19:58 -04001634 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001635}
1636
1637void turbostat_loop()
1638{
Len Brownc98d5d92012-06-04 00:56:40 -04001639 int retval;
Len Browne52966c2012-11-08 22:38:05 -05001640 int restarted = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001641
Len Brown103a8fe2010-10-22 23:53:03 -04001642restart:
Len Browne52966c2012-11-08 22:38:05 -05001643 restarted++;
1644
Len Brownc98d5d92012-06-04 00:56:40 -04001645 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001646 if (retval < -1) {
1647 exit(retval);
1648 } else if (retval == -1) {
Len Browne52966c2012-11-08 22:38:05 -05001649 if (restarted > 1) {
1650 exit(retval);
1651 }
Len Brownc98d5d92012-06-04 00:56:40 -04001652 re_initialize();
1653 goto restart;
1654 }
Len Browne52966c2012-11-08 22:38:05 -05001655 restarted = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001656 gettimeofday(&tv_even, (struct timezone *)NULL);
1657
1658 while (1) {
Len Brownc98d5d92012-06-04 00:56:40 -04001659 if (for_all_proc_cpus(cpu_is_not_present)) {
Len Brown103a8fe2010-10-22 23:53:03 -04001660 re_initialize();
1661 goto restart;
1662 }
1663 sleep(interval_sec);
Len Brownc98d5d92012-06-04 00:56:40 -04001664 retval = for_all_cpus(get_counters, ODD_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001665 if (retval < -1) {
1666 exit(retval);
1667 } else if (retval == -1) {
Len Brown15aaa342012-03-29 22:19:58 -04001668 re_initialize();
1669 goto restart;
1670 }
Len Brown103a8fe2010-10-22 23:53:03 -04001671 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04001672 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04001673 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
1674 compute_average(EVEN_COUNTERS);
1675 format_all_counters(EVEN_COUNTERS);
1676 flush_stdout();
Len Brown15aaa342012-03-29 22:19:58 -04001677 sleep(interval_sec);
Len Brownc98d5d92012-06-04 00:56:40 -04001678 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001679 if (retval < -1) {
1680 exit(retval);
1681 } else if (retval == -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04001682 re_initialize();
1683 goto restart;
1684 }
Len Brown103a8fe2010-10-22 23:53:03 -04001685 gettimeofday(&tv_even, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04001686 timersub(&tv_even, &tv_odd, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04001687 for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS);
1688 compute_average(ODD_COUNTERS);
1689 format_all_counters(ODD_COUNTERS);
1690 flush_stdout();
Len Brown103a8fe2010-10-22 23:53:03 -04001691 }
1692}
1693
1694void check_dev_msr()
1695{
1696 struct stat sb;
1697
Josh Triplettb2c95d92013-08-20 17:20:18 -07001698 if (stat("/dev/cpu/0/msr", &sb))
Len Browna21d38c2015-03-24 16:37:35 -04001699 if (system("/sbin/modprobe msr > /dev/null 2>&1"))
1700 err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
Len Brown103a8fe2010-10-22 23:53:03 -04001701}
1702
Len Brown98481e72014-08-15 00:36:50 -04001703void check_permissions()
Len Brown103a8fe2010-10-22 23:53:03 -04001704{
Len Brown98481e72014-08-15 00:36:50 -04001705 struct __user_cap_header_struct cap_header_data;
1706 cap_user_header_t cap_header = &cap_header_data;
1707 struct __user_cap_data_struct cap_data_data;
1708 cap_user_data_t cap_data = &cap_data_data;
1709 extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
1710 int do_exit = 0;
1711
1712 /* check for CAP_SYS_RAWIO */
1713 cap_header->pid = getpid();
1714 cap_header->version = _LINUX_CAPABILITY_VERSION;
1715 if (capget(cap_header, cap_data) < 0)
1716 err(-6, "capget(2) failed");
1717
1718 if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
1719 do_exit++;
1720 warnx("capget(CAP_SYS_RAWIO) failed,"
1721 " try \"# setcap cap_sys_rawio=ep %s\"", progname);
1722 }
1723
1724 /* test file permissions */
1725 if (euidaccess("/dev/cpu/0/msr", R_OK)) {
1726 do_exit++;
1727 warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
1728 }
1729
1730 /* if all else fails, thell them to be root */
1731 if (do_exit)
1732 if (getuid() != 0)
Len Brownd7899442015-01-23 00:12:33 -05001733 warnx("... or simply run as root");
Len Brown98481e72014-08-15 00:36:50 -04001734
1735 if (do_exit)
1736 exit(-6);
Len Brown103a8fe2010-10-22 23:53:03 -04001737}
1738
Len Brownd7899442015-01-23 00:12:33 -05001739/*
1740 * NHM adds support for additional MSRs:
1741 *
1742 * MSR_SMI_COUNT 0x00000034
1743 *
1744 * MSR_NHM_PLATFORM_INFO 0x000000ce
1745 * MSR_NHM_SNB_PKG_CST_CFG_CTL 0x000000e2
1746 *
1747 * MSR_PKG_C3_RESIDENCY 0x000003f8
1748 * MSR_PKG_C6_RESIDENCY 0x000003f9
1749 * MSR_CORE_C3_RESIDENCY 0x000003fc
1750 * MSR_CORE_C6_RESIDENCY 0x000003fd
1751 *
Len Brownee7e38e2015-02-09 23:39:45 -05001752 * Side effect:
1753 * sets global pkg_cstate_limit to decode MSR_NHM_SNB_PKG_CST_CFG_CTL
Len Brownd7899442015-01-23 00:12:33 -05001754 */
Len Brownee7e38e2015-02-09 23:39:45 -05001755int probe_nhm_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04001756{
Len Brownee7e38e2015-02-09 23:39:45 -05001757 unsigned long long msr;
1758 int *pkg_cstate_limits;
1759
Len Brown103a8fe2010-10-22 23:53:03 -04001760 if (!genuine_intel)
1761 return 0;
1762
1763 if (family != 6)
1764 return 0;
1765
1766 switch (model) {
1767 case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
1768 case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
1769 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
1770 case 0x25: /* Westmere Client - Clarkdale, Arrandale */
1771 case 0x2C: /* Westmere EP - Gulftown */
Len Brownee7e38e2015-02-09 23:39:45 -05001772 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1773 case 0x2F: /* Westmere-EX Xeon - Eagleton */
1774 pkg_cstate_limits = nhm_pkg_cstate_limits;
1775 break;
Len Brown103a8fe2010-10-22 23:53:03 -04001776 case 0x2A: /* SNB */
1777 case 0x2D: /* SNB Xeon */
Len Brown553575f2011-11-18 03:32:01 -05001778 case 0x3A: /* IVB */
Len Brown13006512012-09-26 18:11:31 -04001779 case 0x3E: /* IVB Xeon */
Len Brownee7e38e2015-02-09 23:39:45 -05001780 pkg_cstate_limits = snb_pkg_cstate_limits;
1781 break;
Len Brown70b43402013-01-08 01:26:07 -05001782 case 0x3C: /* HSW */
Len Browne6f9bb32013-12-03 02:19:19 -05001783 case 0x3F: /* HSX */
Len Brown70b43402013-01-08 01:26:07 -05001784 case 0x45: /* HSW */
Len Brown149c2312013-03-15 10:58:02 -04001785 case 0x46: /* HSW */
Len Brown4e8e863f2014-02-27 23:28:53 -05001786 case 0x3D: /* BDW */
Len Brown48a06312015-02-10 15:38:04 -05001787 case 0x47: /* BDW */
Len Brown4e8e863f2014-02-27 23:28:53 -05001788 case 0x4F: /* BDX */
1789 case 0x56: /* BDX-DE */
Len Brown0b2bb692015-03-26 00:50:30 -04001790 case 0x4E: /* SKL */
1791 case 0x5E: /* SKL */
Len Brownee7e38e2015-02-09 23:39:45 -05001792 pkg_cstate_limits = hsw_pkg_cstate_limits;
1793 break;
1794 case 0x37: /* BYT */
1795 case 0x4D: /* AVN */
1796 pkg_cstate_limits = slv_pkg_cstate_limits;
1797 break;
1798 case 0x4C: /* AMT */
1799 pkg_cstate_limits = amt_pkg_cstate_limits;
1800 break;
1801 case 0x57: /* PHI */
1802 pkg_cstate_limits = phi_pkg_cstate_limits;
1803 break;
Len Brown103a8fe2010-10-22 23:53:03 -04001804 default:
1805 return 0;
1806 }
Len Brownee7e38e2015-02-09 23:39:45 -05001807 get_msr(0, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
1808
Len Browne9257f52015-04-01 21:02:57 -04001809 pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
Len Brownee7e38e2015-02-09 23:39:45 -05001810
1811 return 1;
Len Brown103a8fe2010-10-22 23:53:03 -04001812}
Len Brownd7899442015-01-23 00:12:33 -05001813int has_nhm_turbo_ratio_limit(unsigned int family, unsigned int model)
1814{
Len Brownd7899442015-01-23 00:12:33 -05001815 switch (model) {
1816 /* Nehalem compatible, but do not include turbo-ratio limit support */
1817 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1818 case 0x2F: /* Westmere-EX Xeon - Eagleton */
1819 return 0;
1820 default:
1821 return 1;
1822 }
1823}
Len Brown6574a5d2012-09-21 00:01:31 -04001824int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
1825{
1826 if (!genuine_intel)
1827 return 0;
1828
1829 if (family != 6)
1830 return 0;
1831
1832 switch (model) {
1833 case 0x3E: /* IVB Xeon */
Len Brownfcd17212015-03-23 20:29:09 -04001834 case 0x3F: /* HSW Xeon */
Len Brown6574a5d2012-09-21 00:01:31 -04001835 return 1;
1836 default:
1837 return 0;
1838 }
1839}
Len Brownfcd17212015-03-23 20:29:09 -04001840int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
1841{
1842 if (!genuine_intel)
1843 return 0;
1844
1845 if (family != 6)
1846 return 0;
1847
1848 switch (model) {
1849 case 0x3F: /* HSW Xeon */
1850 return 1;
1851 default:
1852 return 0;
1853 }
1854}
1855
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001856int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
1857{
1858 if (!genuine_intel)
1859 return 0;
1860
1861 if (family != 6)
1862 return 0;
1863
1864 switch (model) {
1865 case 0x57: /* Knights Landing */
1866 return 1;
1867 default:
1868 return 0;
1869 }
1870}
Len Brownfcd17212015-03-23 20:29:09 -04001871static void
1872dump_cstate_pstate_config_info(family, model)
1873{
1874 if (!do_nhm_platform_info)
1875 return;
1876
1877 dump_nhm_platform_info();
1878
1879 if (has_hsw_turbo_ratio_limit(family, model))
1880 dump_hsw_turbo_ratio_limits();
1881
1882 if (has_ivt_turbo_ratio_limit(family, model))
1883 dump_ivt_turbo_ratio_limits();
1884
1885 if (has_nhm_turbo_ratio_limit(family, model))
1886 dump_nhm_turbo_ratio_limits();
1887
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07001888 if (has_knl_turbo_ratio_limit(family, model))
1889 dump_knl_turbo_ratio_limits();
1890
Len Brownfcd17212015-03-23 20:29:09 -04001891 dump_nhm_cst_cfg();
1892}
1893
Len Brown6574a5d2012-09-21 00:01:31 -04001894
Len Brown889facb2012-11-08 00:48:57 -05001895/*
1896 * print_epb()
1897 * Decode the ENERGY_PERF_BIAS MSR
1898 */
1899int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1900{
1901 unsigned long long msr;
1902 char *epb_string;
1903 int cpu;
1904
1905 if (!has_epb)
1906 return 0;
1907
1908 cpu = t->cpu_id;
1909
1910 /* EPB is per-package */
1911 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1912 return 0;
1913
1914 if (cpu_migrate(cpu)) {
1915 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1916 return -1;
1917 }
1918
1919 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
1920 return 0;
1921
Len Browne9be7dd2015-05-26 12:19:37 -04001922 switch (msr & 0xF) {
Len Brown889facb2012-11-08 00:48:57 -05001923 case ENERGY_PERF_BIAS_PERFORMANCE:
1924 epb_string = "performance";
1925 break;
1926 case ENERGY_PERF_BIAS_NORMAL:
1927 epb_string = "balanced";
1928 break;
1929 case ENERGY_PERF_BIAS_POWERSAVE:
1930 epb_string = "powersave";
1931 break;
1932 default:
1933 epb_string = "custom";
1934 break;
1935 }
1936 fprintf(stderr, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
1937
1938 return 0;
1939}
1940
Len Brown3a9a9412014-08-15 02:39:52 -04001941/*
1942 * print_perf_limit()
1943 */
1944int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1945{
1946 unsigned long long msr;
1947 int cpu;
1948
1949 cpu = t->cpu_id;
1950
1951 /* per-package */
1952 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1953 return 0;
1954
1955 if (cpu_migrate(cpu)) {
1956 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1957 return -1;
1958 }
1959
1960 if (do_core_perf_limit_reasons) {
1961 get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
1962 fprintf(stderr, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
1963 fprintf(stderr, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
Len Browne33cbe82015-03-13 16:30:57 -04001964 (msr & 1 << 15) ? "bit15, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04001965 (msr & 1 << 14) ? "bit14, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04001966 (msr & 1 << 13) ? "Transitions, " : "",
1967 (msr & 1 << 12) ? "MultiCoreTurbo, " : "",
1968 (msr & 1 << 11) ? "PkgPwrL2, " : "",
1969 (msr & 1 << 10) ? "PkgPwrL1, " : "",
1970 (msr & 1 << 9) ? "CorePwr, " : "",
1971 (msr & 1 << 8) ? "Amps, " : "",
1972 (msr & 1 << 6) ? "VR-Therm, " : "",
1973 (msr & 1 << 5) ? "Auto-HWP, " : "",
1974 (msr & 1 << 4) ? "Graphics, " : "",
1975 (msr & 1 << 2) ? "bit2, " : "",
1976 (msr & 1 << 1) ? "ThermStatus, " : "",
1977 (msr & 1 << 0) ? "PROCHOT, " : "");
Len Brown3a9a9412014-08-15 02:39:52 -04001978 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 -04001979 (msr & 1 << 31) ? "bit31, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04001980 (msr & 1 << 30) ? "bit30, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04001981 (msr & 1 << 29) ? "Transitions, " : "",
1982 (msr & 1 << 28) ? "MultiCoreTurbo, " : "",
1983 (msr & 1 << 27) ? "PkgPwrL2, " : "",
1984 (msr & 1 << 26) ? "PkgPwrL1, " : "",
1985 (msr & 1 << 25) ? "CorePwr, " : "",
1986 (msr & 1 << 24) ? "Amps, " : "",
1987 (msr & 1 << 22) ? "VR-Therm, " : "",
1988 (msr & 1 << 21) ? "Auto-HWP, " : "",
1989 (msr & 1 << 20) ? "Graphics, " : "",
1990 (msr & 1 << 18) ? "bit18, " : "",
1991 (msr & 1 << 17) ? "ThermStatus, " : "",
1992 (msr & 1 << 16) ? "PROCHOT, " : "");
Len Brown3a9a9412014-08-15 02:39:52 -04001993
1994 }
1995 if (do_gfx_perf_limit_reasons) {
1996 get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
1997 fprintf(stderr, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
1998 fprintf(stderr, " (Active: %s%s%s%s%s%s%s%s)",
1999 (msr & 1 << 0) ? "PROCHOT, " : "",
2000 (msr & 1 << 1) ? "ThermStatus, " : "",
2001 (msr & 1 << 4) ? "Graphics, " : "",
2002 (msr & 1 << 6) ? "VR-Therm, " : "",
2003 (msr & 1 << 8) ? "Amps, " : "",
2004 (msr & 1 << 9) ? "GFXPwr, " : "",
2005 (msr & 1 << 10) ? "PkgPwrL1, " : "",
2006 (msr & 1 << 11) ? "PkgPwrL2, " : "");
2007 fprintf(stderr, " (Logged: %s%s%s%s%s%s%s%s)\n",
2008 (msr & 1 << 16) ? "PROCHOT, " : "",
2009 (msr & 1 << 17) ? "ThermStatus, " : "",
2010 (msr & 1 << 20) ? "Graphics, " : "",
2011 (msr & 1 << 22) ? "VR-Therm, " : "",
2012 (msr & 1 << 24) ? "Amps, " : "",
2013 (msr & 1 << 25) ? "GFXPwr, " : "",
2014 (msr & 1 << 26) ? "PkgPwrL1, " : "",
2015 (msr & 1 << 27) ? "PkgPwrL2, " : "");
2016 }
2017 if (do_ring_perf_limit_reasons) {
2018 get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
2019 fprintf(stderr, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2020 fprintf(stderr, " (Active: %s%s%s%s%s%s)",
2021 (msr & 1 << 0) ? "PROCHOT, " : "",
2022 (msr & 1 << 1) ? "ThermStatus, " : "",
2023 (msr & 1 << 6) ? "VR-Therm, " : "",
2024 (msr & 1 << 8) ? "Amps, " : "",
2025 (msr & 1 << 10) ? "PkgPwrL1, " : "",
2026 (msr & 1 << 11) ? "PkgPwrL2, " : "");
2027 fprintf(stderr, " (Logged: %s%s%s%s%s%s)\n",
2028 (msr & 1 << 16) ? "PROCHOT, " : "",
2029 (msr & 1 << 17) ? "ThermStatus, " : "",
2030 (msr & 1 << 22) ? "VR-Therm, " : "",
2031 (msr & 1 << 24) ? "Amps, " : "",
2032 (msr & 1 << 26) ? "PkgPwrL1, " : "",
2033 (msr & 1 << 27) ? "PkgPwrL2, " : "");
2034 }
2035 return 0;
2036}
2037
Len Brown889facb2012-11-08 00:48:57 -05002038#define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
2039#define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
2040
Len Brown144b44b2013-11-09 00:30:16 -05002041double get_tdp(model)
2042{
2043 unsigned long long msr;
2044
2045 if (do_rapl & RAPL_PKG_POWER_INFO)
2046 if (!get_msr(0, MSR_PKG_POWER_INFO, &msr))
2047 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
2048
2049 switch (model) {
2050 case 0x37:
2051 case 0x4D:
2052 return 30.0;
2053 default:
2054 return 135.0;
2055 }
2056}
2057
Andrey Semin40ee8e32014-12-05 00:07:00 -05002058/*
2059 * rapl_dram_energy_units_probe()
2060 * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
2061 */
2062static double
2063rapl_dram_energy_units_probe(int model, double rapl_energy_units)
2064{
2065 /* only called for genuine_intel, family 6 */
2066
2067 switch (model) {
2068 case 0x3F: /* HSX */
2069 case 0x4F: /* BDX */
2070 case 0x56: /* BDX-DE */
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002071 case 0x57: /* KNL */
Andrey Semin40ee8e32014-12-05 00:07:00 -05002072 return (rapl_dram_energy_units = 15.3 / 1000000);
2073 default:
2074 return (rapl_energy_units);
2075 }
2076}
2077
Len Brown144b44b2013-11-09 00:30:16 -05002078
Len Brown889facb2012-11-08 00:48:57 -05002079/*
2080 * rapl_probe()
2081 *
Len Brown144b44b2013-11-09 00:30:16 -05002082 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
Len Brown889facb2012-11-08 00:48:57 -05002083 */
2084void rapl_probe(unsigned int family, unsigned int model)
2085{
2086 unsigned long long msr;
Len Brown144b44b2013-11-09 00:30:16 -05002087 unsigned int time_unit;
Len Brown889facb2012-11-08 00:48:57 -05002088 double tdp;
2089
2090 if (!genuine_intel)
2091 return;
2092
2093 if (family != 6)
2094 return;
2095
2096 switch (model) {
2097 case 0x2A:
2098 case 0x3A:
Len Brown70b43402013-01-08 01:26:07 -05002099 case 0x3C: /* HSW */
Len Brown70b43402013-01-08 01:26:07 -05002100 case 0x45: /* HSW */
Len Brown149c2312013-03-15 10:58:02 -04002101 case 0x46: /* HSW */
Len Brown4e8e863f2014-02-27 23:28:53 -05002102 case 0x3D: /* BDW */
Len Brown48a06312015-02-10 15:38:04 -05002103 case 0x47: /* BDW */
Len Brown144b44b2013-11-09 00:30:16 -05002104 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
Len Brown889facb2012-11-08 00:48:57 -05002105 break;
Len Brown0b2bb692015-03-26 00:50:30 -04002106 case 0x4E: /* SKL */
2107 case 0x5E: /* SKL */
2108 do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
2109 break;
Len Browne6f9bb32013-12-03 02:19:19 -05002110 case 0x3F: /* HSX */
Len Brown4e8e863f2014-02-27 23:28:53 -05002111 case 0x4F: /* BDX */
2112 case 0x56: /* BDX-DE */
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002113 case 0x57: /* KNL */
Len Brown0b2bb692015-03-26 00:50:30 -04002114 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 -05002115 break;
Len Brown889facb2012-11-08 00:48:57 -05002116 case 0x2D:
2117 case 0x3E:
Len Brown0b2bb692015-03-26 00:50:30 -04002118 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 -05002119 break;
2120 case 0x37: /* BYT */
2121 case 0x4D: /* AVN */
2122 do_rapl = RAPL_PKG | RAPL_CORES ;
Len Brown889facb2012-11-08 00:48:57 -05002123 break;
2124 default:
2125 return;
2126 }
2127
2128 /* units on package 0, verify later other packages match */
2129 if (get_msr(0, MSR_RAPL_POWER_UNIT, &msr))
2130 return;
2131
2132 rapl_power_units = 1.0 / (1 << (msr & 0xF));
Len Brown144b44b2013-11-09 00:30:16 -05002133 if (model == 0x37)
2134 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
2135 else
2136 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
Len Brown889facb2012-11-08 00:48:57 -05002137
Andrey Semin40ee8e32014-12-05 00:07:00 -05002138 rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
2139
Len Brown144b44b2013-11-09 00:30:16 -05002140 time_unit = msr >> 16 & 0xF;
2141 if (time_unit == 0)
2142 time_unit = 0xA;
Len Brown889facb2012-11-08 00:48:57 -05002143
Len Brown144b44b2013-11-09 00:30:16 -05002144 rapl_time_units = 1.0 / (1 << (time_unit));
2145
2146 tdp = get_tdp(model);
Len Brown889facb2012-11-08 00:48:57 -05002147
2148 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
Len Brownd8af6f52015-02-10 01:56:38 -05002149 if (debug)
Len Brown144b44b2013-11-09 00:30:16 -05002150 fprintf(stderr, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
Len Brown889facb2012-11-08 00:48:57 -05002151
2152 return;
2153}
2154
Len Brown3a9a9412014-08-15 02:39:52 -04002155void perf_limit_reasons_probe(family, model)
2156{
2157 if (!genuine_intel)
2158 return;
2159
2160 if (family != 6)
2161 return;
2162
2163 switch (model) {
2164 case 0x3C: /* HSW */
2165 case 0x45: /* HSW */
2166 case 0x46: /* HSW */
2167 do_gfx_perf_limit_reasons = 1;
2168 case 0x3F: /* HSX */
2169 do_core_perf_limit_reasons = 1;
2170 do_ring_perf_limit_reasons = 1;
2171 default:
2172 return;
2173 }
2174}
2175
Len Brown889facb2012-11-08 00:48:57 -05002176int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2177{
2178 unsigned long long msr;
2179 unsigned int dts;
2180 int cpu;
2181
2182 if (!(do_dts || do_ptm))
2183 return 0;
2184
2185 cpu = t->cpu_id;
2186
2187 /* DTS is per-core, no need to print for each thread */
2188 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
2189 return 0;
2190
2191 if (cpu_migrate(cpu)) {
2192 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
2193 return -1;
2194 }
2195
2196 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
2197 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
2198 return 0;
2199
2200 dts = (msr >> 16) & 0x7F;
2201 fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
2202 cpu, msr, tcc_activation_temp - dts);
2203
2204#ifdef THERM_DEBUG
2205 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
2206 return 0;
2207
2208 dts = (msr >> 16) & 0x7F;
2209 dts2 = (msr >> 8) & 0x7F;
2210 fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
2211 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
2212#endif
2213 }
2214
2215
2216 if (do_dts) {
2217 unsigned int resolution;
2218
2219 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
2220 return 0;
2221
2222 dts = (msr >> 16) & 0x7F;
2223 resolution = (msr >> 27) & 0xF;
2224 fprintf(stderr, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
2225 cpu, msr, tcc_activation_temp - dts, resolution);
2226
2227#ifdef THERM_DEBUG
2228 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
2229 return 0;
2230
2231 dts = (msr >> 16) & 0x7F;
2232 dts2 = (msr >> 8) & 0x7F;
2233 fprintf(stderr, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
2234 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
2235#endif
2236 }
2237
2238 return 0;
2239}
2240
2241void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
2242{
2243 fprintf(stderr, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
2244 cpu, label,
2245 ((msr >> 15) & 1) ? "EN" : "DIS",
2246 ((msr >> 0) & 0x7FFF) * rapl_power_units,
2247 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
2248 (((msr >> 16) & 1) ? "EN" : "DIS"));
2249
2250 return;
2251}
2252
2253int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2254{
2255 unsigned long long msr;
2256 int cpu;
Len Brown889facb2012-11-08 00:48:57 -05002257
2258 if (!do_rapl)
2259 return 0;
2260
2261 /* RAPL counters are per package, so print only for 1st thread/package */
2262 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2263 return 0;
2264
2265 cpu = t->cpu_id;
2266 if (cpu_migrate(cpu)) {
2267 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
2268 return -1;
2269 }
2270
2271 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
2272 return -1;
2273
Len Brownd8af6f52015-02-10 01:56:38 -05002274 if (debug) {
Len Brown889facb2012-11-08 00:48:57 -05002275 fprintf(stderr, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx "
2276 "(%f Watts, %f Joules, %f sec.)\n", cpu, msr,
Len Brown144b44b2013-11-09 00:30:16 -05002277 rapl_power_units, rapl_energy_units, rapl_time_units);
Len Brown889facb2012-11-08 00:48:57 -05002278 }
Len Brown144b44b2013-11-09 00:30:16 -05002279 if (do_rapl & RAPL_PKG_POWER_INFO) {
2280
Len Brown889facb2012-11-08 00:48:57 -05002281 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
2282 return -5;
2283
2284
2285 fprintf(stderr, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
2286 cpu, msr,
2287 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2288 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2289 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2290 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
2291
Len Brown144b44b2013-11-09 00:30:16 -05002292 }
2293 if (do_rapl & RAPL_PKG) {
2294
Len Brown889facb2012-11-08 00:48:57 -05002295 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
2296 return -9;
2297
2298 fprintf(stderr, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
2299 cpu, msr, (msr >> 63) & 1 ? "": "UN");
2300
2301 print_power_limit_msr(cpu, msr, "PKG Limit #1");
2302 fprintf(stderr, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
2303 cpu,
2304 ((msr >> 47) & 1) ? "EN" : "DIS",
2305 ((msr >> 32) & 0x7FFF) * rapl_power_units,
2306 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
2307 ((msr >> 48) & 1) ? "EN" : "DIS");
2308 }
2309
Len Brown0b2bb692015-03-26 00:50:30 -04002310 if (do_rapl & RAPL_DRAM_POWER_INFO) {
Len Brown889facb2012-11-08 00:48:57 -05002311 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
2312 return -6;
2313
Len Brown889facb2012-11-08 00:48:57 -05002314 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
2315 cpu, msr,
2316 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2317 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2318 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2319 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
Len Brown0b2bb692015-03-26 00:50:30 -04002320 }
2321 if (do_rapl & RAPL_DRAM) {
Len Brown889facb2012-11-08 00:48:57 -05002322 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
2323 return -9;
2324 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
2325 cpu, msr, (msr >> 31) & 1 ? "": "UN");
2326
2327 print_power_limit_msr(cpu, msr, "DRAM Limit");
2328 }
Len Brown144b44b2013-11-09 00:30:16 -05002329 if (do_rapl & RAPL_CORE_POLICY) {
Len Brownd8af6f52015-02-10 01:56:38 -05002330 if (debug) {
Len Brown889facb2012-11-08 00:48:57 -05002331 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
2332 return -7;
2333
2334 fprintf(stderr, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown144b44b2013-11-09 00:30:16 -05002335 }
2336 }
2337 if (do_rapl & RAPL_CORES) {
Len Brownd8af6f52015-02-10 01:56:38 -05002338 if (debug) {
Len Brown889facb2012-11-08 00:48:57 -05002339
2340 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
2341 return -9;
2342 fprintf(stderr, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
2343 cpu, msr, (msr >> 31) & 1 ? "": "UN");
2344 print_power_limit_msr(cpu, msr, "Cores Limit");
2345 }
2346 }
2347 if (do_rapl & RAPL_GFX) {
Len Brownd8af6f52015-02-10 01:56:38 -05002348 if (debug) {
Len Brown889facb2012-11-08 00:48:57 -05002349 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
2350 return -8;
2351
2352 fprintf(stderr, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
2353
2354 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
2355 return -9;
2356 fprintf(stderr, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
2357 cpu, msr, (msr >> 31) & 1 ? "": "UN");
2358 print_power_limit_msr(cpu, msr, "GFX Limit");
2359 }
2360 }
2361 return 0;
2362}
2363
Len Brownd7899442015-01-23 00:12:33 -05002364/*
2365 * SNB adds support for additional MSRs:
2366 *
2367 * MSR_PKG_C7_RESIDENCY 0x000003fa
2368 * MSR_CORE_C7_RESIDENCY 0x000003fe
2369 * MSR_PKG_C2_RESIDENCY 0x0000060d
2370 */
Len Brown103a8fe2010-10-22 23:53:03 -04002371
Len Brownd7899442015-01-23 00:12:33 -05002372int has_snb_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04002373{
2374 if (!genuine_intel)
2375 return 0;
2376
2377 switch (model) {
2378 case 0x2A:
2379 case 0x2D:
Len Brown650a37f2012-06-03 23:34:44 -04002380 case 0x3A: /* IVB */
Len Brown13006512012-09-26 18:11:31 -04002381 case 0x3E: /* IVB Xeon */
Len Brown70b43402013-01-08 01:26:07 -05002382 case 0x3C: /* HSW */
2383 case 0x3F: /* HSW */
2384 case 0x45: /* HSW */
Len Brown149c2312013-03-15 10:58:02 -04002385 case 0x46: /* HSW */
Len Brown4e8e863f2014-02-27 23:28:53 -05002386 case 0x3D: /* BDW */
Len Brown48a06312015-02-10 15:38:04 -05002387 case 0x47: /* BDW */
Len Brown4e8e863f2014-02-27 23:28:53 -05002388 case 0x4F: /* BDX */
2389 case 0x56: /* BDX-DE */
Len Brown0b2bb692015-03-26 00:50:30 -04002390 case 0x4E: /* SKL */
2391 case 0x5E: /* SKL */
Len Brown103a8fe2010-10-22 23:53:03 -04002392 return 1;
2393 }
2394 return 0;
2395}
2396
Len Brownd7899442015-01-23 00:12:33 -05002397/*
2398 * HSW adds support for additional MSRs:
2399 *
2400 * MSR_PKG_C8_RESIDENCY 0x00000630
2401 * MSR_PKG_C9_RESIDENCY 0x00000631
2402 * MSR_PKG_C10_RESIDENCY 0x00000632
2403 */
2404int has_hsw_msrs(unsigned int family, unsigned int model)
Kristen Carlson Accardica587102012-11-21 05:22:43 -08002405{
2406 if (!genuine_intel)
2407 return 0;
2408
2409 switch (model) {
Len Brown4e8e863f2014-02-27 23:28:53 -05002410 case 0x45: /* HSW */
2411 case 0x3D: /* BDW */
Len Brown0b2bb692015-03-26 00:50:30 -04002412 case 0x4E: /* SKL */
2413 case 0x5E: /* SKL */
Kristen Carlson Accardica587102012-11-21 05:22:43 -08002414 return 1;
2415 }
2416 return 0;
2417}
2418
Len Brown0b2bb692015-03-26 00:50:30 -04002419/*
2420 * SKL adds support for additional MSRS:
2421 *
2422 * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658
2423 * MSR_PKG_ANY_CORE_C0_RES 0x00000659
2424 * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A
2425 * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B
2426 */
2427int has_skl_msrs(unsigned int family, unsigned int model)
2428{
2429 if (!genuine_intel)
2430 return 0;
2431
2432 switch (model) {
2433 case 0x4E: /* SKL */
2434 case 0x5E: /* SKL */
2435 return 1;
2436 }
2437 return 0;
2438}
2439
2440
Kristen Carlson Accardica587102012-11-21 05:22:43 -08002441
Len Brown144b44b2013-11-09 00:30:16 -05002442int is_slm(unsigned int family, unsigned int model)
2443{
2444 if (!genuine_intel)
2445 return 0;
2446 switch (model) {
2447 case 0x37: /* BYT */
2448 case 0x4D: /* AVN */
2449 return 1;
2450 }
2451 return 0;
2452}
2453
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002454int is_knl(unsigned int family, unsigned int model)
2455{
2456 if (!genuine_intel)
2457 return 0;
2458 switch (model) {
2459 case 0x57: /* KNL */
2460 return 1;
2461 }
2462 return 0;
2463}
2464
Len Brown144b44b2013-11-09 00:30:16 -05002465#define SLM_BCLK_FREQS 5
2466double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
2467
2468double slm_bclk(void)
2469{
2470 unsigned long long msr = 3;
2471 unsigned int i;
2472 double freq;
2473
2474 if (get_msr(0, MSR_FSB_FREQ, &msr))
2475 fprintf(stderr, "SLM BCLK: unknown\n");
2476
2477 i = msr & 0xf;
2478 if (i >= SLM_BCLK_FREQS) {
2479 fprintf(stderr, "SLM BCLK[%d] invalid\n", i);
2480 msr = 3;
2481 }
2482 freq = slm_freq_table[i];
2483
2484 fprintf(stderr, "SLM BCLK: %.1f Mhz\n", freq);
2485
2486 return freq;
2487}
2488
Len Brown103a8fe2010-10-22 23:53:03 -04002489double discover_bclk(unsigned int family, unsigned int model)
2490{
Len Brownd7899442015-01-23 00:12:33 -05002491 if (has_snb_msrs(family, model))
Len Brown103a8fe2010-10-22 23:53:03 -04002492 return 100.00;
Len Brown144b44b2013-11-09 00:30:16 -05002493 else if (is_slm(family, model))
2494 return slm_bclk();
Len Brown103a8fe2010-10-22 23:53:03 -04002495 else
2496 return 133.33;
2497}
2498
Len Brown889facb2012-11-08 00:48:57 -05002499/*
2500 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
2501 * the Thermal Control Circuit (TCC) activates.
2502 * This is usually equal to tjMax.
2503 *
2504 * Older processors do not have this MSR, so there we guess,
2505 * but also allow cmdline over-ride with -T.
2506 *
2507 * Several MSR temperature values are in units of degrees-C
2508 * below this value, including the Digital Thermal Sensor (DTS),
2509 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
2510 */
2511int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2512{
2513 unsigned long long msr;
2514 unsigned int target_c_local;
2515 int cpu;
2516
2517 /* tcc_activation_temp is used only for dts or ptm */
2518 if (!(do_dts || do_ptm))
2519 return 0;
2520
2521 /* this is a per-package concept */
2522 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2523 return 0;
2524
2525 cpu = t->cpu_id;
2526 if (cpu_migrate(cpu)) {
2527 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
2528 return -1;
2529 }
2530
2531 if (tcc_activation_temp_override != 0) {
2532 tcc_activation_temp = tcc_activation_temp_override;
2533 fprintf(stderr, "cpu%d: Using cmdline TCC Target (%d C)\n",
2534 cpu, tcc_activation_temp);
2535 return 0;
2536 }
2537
2538 /* Temperature Target MSR is Nehalem and newer only */
Len Brownd7899442015-01-23 00:12:33 -05002539 if (!do_nhm_platform_info)
Len Brown889facb2012-11-08 00:48:57 -05002540 goto guess;
2541
2542 if (get_msr(0, MSR_IA32_TEMPERATURE_TARGET, &msr))
2543 goto guess;
2544
Jean Delvare34821242014-05-01 11:40:19 +02002545 target_c_local = (msr >> 16) & 0xFF;
Len Brown889facb2012-11-08 00:48:57 -05002546
Len Brownd8af6f52015-02-10 01:56:38 -05002547 if (debug)
Len Brown889facb2012-11-08 00:48:57 -05002548 fprintf(stderr, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
2549 cpu, msr, target_c_local);
2550
Jean Delvare34821242014-05-01 11:40:19 +02002551 if (!target_c_local)
Len Brown889facb2012-11-08 00:48:57 -05002552 goto guess;
2553
2554 tcc_activation_temp = target_c_local;
2555
2556 return 0;
2557
2558guess:
2559 tcc_activation_temp = TJMAX_DEFAULT;
2560 fprintf(stderr, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
2561 cpu, tcc_activation_temp);
2562
2563 return 0;
2564}
Len Brownfcd17212015-03-23 20:29:09 -04002565void process_cpuid()
Len Brown103a8fe2010-10-22 23:53:03 -04002566{
2567 unsigned int eax, ebx, ecx, edx, max_level;
2568 unsigned int fms, family, model, stepping;
2569
2570 eax = ebx = ecx = edx = 0;
2571
Josh Triplett2b928652013-08-20 17:20:14 -07002572 __get_cpuid(0, &max_level, &ebx, &ecx, &edx);
Len Brown103a8fe2010-10-22 23:53:03 -04002573
2574 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
2575 genuine_intel = 1;
2576
Len Brownd8af6f52015-02-10 01:56:38 -05002577 if (debug)
Len Brown889facb2012-11-08 00:48:57 -05002578 fprintf(stderr, "CPUID(0): %.4s%.4s%.4s ",
Len Brown103a8fe2010-10-22 23:53:03 -04002579 (char *)&ebx, (char *)&edx, (char *)&ecx);
2580
Josh Triplett2b928652013-08-20 17:20:14 -07002581 __get_cpuid(1, &fms, &ebx, &ecx, &edx);
Len Brown103a8fe2010-10-22 23:53:03 -04002582 family = (fms >> 8) & 0xf;
2583 model = (fms >> 4) & 0xf;
2584 stepping = fms & 0xf;
2585 if (family == 6 || family == 0xf)
2586 model += ((fms >> 16) & 0xf) << 4;
2587
Len Brownd8af6f52015-02-10 01:56:38 -05002588 if (debug)
Len Brown103a8fe2010-10-22 23:53:03 -04002589 fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
2590 max_level, family, model, stepping, family, model, stepping);
2591
Josh Triplettb2c95d92013-08-20 17:20:18 -07002592 if (!(edx & (1 << 5)))
2593 errx(1, "CPUID: no MSR");
Len Brown103a8fe2010-10-22 23:53:03 -04002594
2595 /*
2596 * check max extended function levels of CPUID.
2597 * This is needed to check for invariant TSC.
2598 * This check is valid for both Intel and AMD.
2599 */
2600 ebx = ecx = edx = 0;
Josh Triplett2b928652013-08-20 17:20:14 -07002601 __get_cpuid(0x80000000, &max_level, &ebx, &ecx, &edx);
Len Brown103a8fe2010-10-22 23:53:03 -04002602
Len Brownd7899442015-01-23 00:12:33 -05002603 if (max_level >= 0x80000007) {
Len Brown103a8fe2010-10-22 23:53:03 -04002604
Len Brownd7899442015-01-23 00:12:33 -05002605 /*
2606 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
2607 * this check is valid for both Intel and AMD
2608 */
2609 __get_cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
2610 has_invariant_tsc = edx & (1 << 8);
2611 }
Len Brown103a8fe2010-10-22 23:53:03 -04002612
2613 /*
2614 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
2615 * this check is valid for both Intel and AMD
2616 */
2617
Josh Triplett2b928652013-08-20 17:20:14 -07002618 __get_cpuid(0x6, &eax, &ebx, &ecx, &edx);
Thomas Renninger8209e052011-01-21 15:11:19 +01002619 has_aperf = ecx & (1 << 0);
Len Brown889facb2012-11-08 00:48:57 -05002620 do_dts = eax & (1 << 0);
2621 do_ptm = eax & (1 << 6);
2622 has_epb = ecx & (1 << 3);
2623
Len Brownd8af6f52015-02-10 01:56:38 -05002624 if (debug)
Len Browna7296172015-01-23 01:33:58 -05002625 fprintf(stderr, "CPUID(6): %sAPERF, %sDTS, %sPTM, %sEPB\n",
2626 has_aperf ? "" : "No ",
2627 do_dts ? "" : "No ",
2628 do_ptm ? "" : "No ",
2629 has_epb ? "" : "No ");
Len Brown103a8fe2010-10-22 23:53:03 -04002630
Len Brown8a5bdf42015-04-01 21:02:57 -04002631 if (max_level > 0x15) {
2632 unsigned int eax_crystal;
2633 unsigned int ebx_tsc;
2634
2635 /*
2636 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
2637 */
2638 eax_crystal = ebx_tsc = crystal_hz = edx = 0;
2639 __get_cpuid(0x15, &eax_crystal, &ebx_tsc, &crystal_hz, &edx);
2640
2641 if (ebx_tsc != 0) {
2642
2643 if (debug && (ebx != 0))
2644 fprintf(stderr, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
2645 eax_crystal, ebx_tsc, crystal_hz);
2646
2647 if (crystal_hz == 0)
2648 switch(model) {
2649 case 0x4E: /* SKL */
2650 case 0x5E: /* SKL */
2651 crystal_hz = 24000000; /* 24 MHz */
2652 break;
2653 default:
2654 crystal_hz = 0;
2655 }
2656
2657 if (crystal_hz) {
2658 tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
2659 if (debug)
2660 fprintf(stderr, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
2661 tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal);
2662 }
2663 }
2664 }
2665
Len Brownee7e38e2015-02-09 23:39:45 -05002666 do_nhm_platform_info = do_nhm_cstates = do_smi = probe_nhm_msrs(family, model);
Len Brownd7899442015-01-23 00:12:33 -05002667 do_snb_cstates = has_snb_msrs(family, model);
Len Brownee7e38e2015-02-09 23:39:45 -05002668 do_pc2 = do_snb_cstates && (pkg_cstate_limit >= PCL__2);
2669 do_pc3 = (pkg_cstate_limit >= PCL__3);
2670 do_pc6 = (pkg_cstate_limit >= PCL__6);
2671 do_pc7 = do_snb_cstates && (pkg_cstate_limit >= PCL__7);
Len Brownd7899442015-01-23 00:12:33 -05002672 do_c8_c9_c10 = has_hsw_msrs(family, model);
Len Brown0b2bb692015-03-26 00:50:30 -04002673 do_skl_residency = has_skl_msrs(family, model);
Len Brown144b44b2013-11-09 00:30:16 -05002674 do_slm_cstates = is_slm(family, model);
Dasaratharaman Chandramoulifb5d4322015-05-20 09:49:34 -07002675 do_knl_cstates = is_knl(family, model);
Len Brown103a8fe2010-10-22 23:53:03 -04002676 bclk = discover_bclk(family, model);
2677
Len Brown889facb2012-11-08 00:48:57 -05002678 rapl_probe(family, model);
Len Brown3a9a9412014-08-15 02:39:52 -04002679 perf_limit_reasons_probe(family, model);
Len Brown889facb2012-11-08 00:48:57 -05002680
Len Brownfcd17212015-03-23 20:29:09 -04002681 if (debug)
2682 dump_cstate_pstate_config_info();
2683
Len Brown889facb2012-11-08 00:48:57 -05002684 return;
Len Brown103a8fe2010-10-22 23:53:03 -04002685}
2686
Len Brownd8af6f52015-02-10 01:56:38 -05002687void help()
Len Brown103a8fe2010-10-22 23:53:03 -04002688{
Len Brownd8af6f52015-02-10 01:56:38 -05002689 fprintf(stderr,
2690 "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
2691 "\n"
2692 "Turbostat forks the specified COMMAND and prints statistics\n"
2693 "when COMMAND completes.\n"
2694 "If no COMMAND is specified, turbostat wakes every 5-seconds\n"
2695 "to print statistics, until interrupted.\n"
2696 "--debug run in \"debug\" mode\n"
2697 "--interval sec Override default 5-second measurement interval\n"
2698 "--help print this help message\n"
2699 "--counter msr print 32-bit counter at address \"msr\"\n"
2700 "--Counter msr print 64-bit Counter at address \"msr\"\n"
2701 "--msr msr print 32-bit value at address \"msr\"\n"
2702 "--MSR msr print 64-bit Value at address \"msr\"\n"
2703 "--version print version information\n"
2704 "\n"
2705 "For more help, run \"man turbostat\"\n");
Len Brown103a8fe2010-10-22 23:53:03 -04002706}
2707
2708
2709/*
2710 * in /dev/cpu/ return success for names that are numbers
2711 * ie. filter out ".", "..", "microcode".
2712 */
2713int dir_filter(const struct dirent *dirp)
2714{
2715 if (isdigit(dirp->d_name[0]))
2716 return 1;
2717 else
2718 return 0;
2719}
2720
2721int open_dev_cpu_msr(int dummy1)
2722{
2723 return 0;
2724}
2725
Len Brownc98d5d92012-06-04 00:56:40 -04002726void topology_probe()
2727{
2728 int i;
2729 int max_core_id = 0;
2730 int max_package_id = 0;
2731 int max_siblings = 0;
2732 struct cpu_topology {
2733 int core_id;
2734 int physical_package_id;
2735 } *cpus;
2736
2737 /* Initialize num_cpus, max_cpu_num */
2738 topo.num_cpus = 0;
2739 topo.max_cpu_num = 0;
2740 for_all_proc_cpus(count_cpus);
2741 if (!summary_only && topo.num_cpus > 1)
2742 show_cpu = 1;
2743
Len Brownd8af6f52015-02-10 01:56:38 -05002744 if (debug > 1)
Len Brownc98d5d92012-06-04 00:56:40 -04002745 fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
2746
2747 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
Josh Triplettb2c95d92013-08-20 17:20:18 -07002748 if (cpus == NULL)
2749 err(1, "calloc cpus");
Len Brownc98d5d92012-06-04 00:56:40 -04002750
2751 /*
2752 * Allocate and initialize cpu_present_set
2753 */
2754 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07002755 if (cpu_present_set == NULL)
2756 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04002757 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2758 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
2759 for_all_proc_cpus(mark_cpu_present);
2760
2761 /*
2762 * Allocate and initialize cpu_affinity_set
2763 */
2764 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07002765 if (cpu_affinity_set == NULL)
2766 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04002767 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2768 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
2769
2770
2771 /*
2772 * For online cpus
2773 * find max_core_id, max_package_id
2774 */
2775 for (i = 0; i <= topo.max_cpu_num; ++i) {
2776 int siblings;
2777
2778 if (cpu_is_not_present(i)) {
Len Brownd8af6f52015-02-10 01:56:38 -05002779 if (debug > 1)
Len Brownc98d5d92012-06-04 00:56:40 -04002780 fprintf(stderr, "cpu%d NOT PRESENT\n", i);
2781 continue;
2782 }
2783 cpus[i].core_id = get_core_id(i);
2784 if (cpus[i].core_id > max_core_id)
2785 max_core_id = cpus[i].core_id;
2786
2787 cpus[i].physical_package_id = get_physical_package_id(i);
2788 if (cpus[i].physical_package_id > max_package_id)
2789 max_package_id = cpus[i].physical_package_id;
2790
2791 siblings = get_num_ht_siblings(i);
2792 if (siblings > max_siblings)
2793 max_siblings = siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05002794 if (debug > 1)
Len Brownc98d5d92012-06-04 00:56:40 -04002795 fprintf(stderr, "cpu %d pkg %d core %d\n",
2796 i, cpus[i].physical_package_id, cpus[i].core_id);
2797 }
2798 topo.num_cores_per_pkg = max_core_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05002799 if (debug > 1)
Len Brownc98d5d92012-06-04 00:56:40 -04002800 fprintf(stderr, "max_core_id %d, sizing for %d cores per package\n",
2801 max_core_id, topo.num_cores_per_pkg);
Len Brown1cc21f72015-02-23 00:34:57 -05002802 if (debug && !summary_only && topo.num_cores_per_pkg > 1)
Len Brownc98d5d92012-06-04 00:56:40 -04002803 show_core = 1;
2804
2805 topo.num_packages = max_package_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05002806 if (debug > 1)
Len Brownc98d5d92012-06-04 00:56:40 -04002807 fprintf(stderr, "max_package_id %d, sizing for %d packages\n",
2808 max_package_id, topo.num_packages);
Len Brown1cc21f72015-02-23 00:34:57 -05002809 if (debug && !summary_only && topo.num_packages > 1)
Len Brownc98d5d92012-06-04 00:56:40 -04002810 show_pkg = 1;
2811
2812 topo.num_threads_per_core = max_siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05002813 if (debug > 1)
Len Brownc98d5d92012-06-04 00:56:40 -04002814 fprintf(stderr, "max_siblings %d\n", max_siblings);
2815
2816 free(cpus);
2817}
2818
2819void
2820allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
2821{
2822 int i;
2823
2824 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
2825 topo.num_packages, sizeof(struct thread_data));
2826 if (*t == NULL)
2827 goto error;
2828
2829 for (i = 0; i < topo.num_threads_per_core *
2830 topo.num_cores_per_pkg * topo.num_packages; i++)
2831 (*t)[i].cpu_id = -1;
2832
2833 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
2834 sizeof(struct core_data));
2835 if (*c == NULL)
2836 goto error;
2837
2838 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
2839 (*c)[i].core_id = -1;
2840
2841 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
2842 if (*p == NULL)
2843 goto error;
2844
2845 for (i = 0; i < topo.num_packages; i++)
2846 (*p)[i].package_id = i;
2847
2848 return;
2849error:
Josh Triplettb2c95d92013-08-20 17:20:18 -07002850 err(1, "calloc counters");
Len Brownc98d5d92012-06-04 00:56:40 -04002851}
2852/*
2853 * init_counter()
2854 *
2855 * set cpu_id, core_num, pkg_num
2856 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
2857 *
2858 * increment topo.num_cores when 1st core in pkg seen
2859 */
2860void init_counter(struct thread_data *thread_base, struct core_data *core_base,
2861 struct pkg_data *pkg_base, int thread_num, int core_num,
2862 int pkg_num, int cpu_id)
2863{
2864 struct thread_data *t;
2865 struct core_data *c;
2866 struct pkg_data *p;
2867
2868 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
2869 c = GET_CORE(core_base, core_num, pkg_num);
2870 p = GET_PKG(pkg_base, pkg_num);
2871
2872 t->cpu_id = cpu_id;
2873 if (thread_num == 0) {
2874 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
2875 if (cpu_is_first_core_in_package(cpu_id))
2876 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
2877 }
2878
2879 c->core_id = core_num;
2880 p->package_id = pkg_num;
2881}
2882
2883
2884int initialize_counters(int cpu_id)
2885{
2886 int my_thread_id, my_core_id, my_package_id;
2887
2888 my_package_id = get_physical_package_id(cpu_id);
2889 my_core_id = get_core_id(cpu_id);
Dasaratharaman Chandramoulie275b382015-04-15 10:09:50 -07002890 my_thread_id = get_cpu_position_in_core(cpu_id);
2891 if (!my_thread_id)
Len Brownc98d5d92012-06-04 00:56:40 -04002892 topo.num_cores++;
Len Brownc98d5d92012-06-04 00:56:40 -04002893
2894 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2895 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2896 return 0;
2897}
2898
2899void allocate_output_buffer()
2900{
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02002901 output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
Len Brownc98d5d92012-06-04 00:56:40 -04002902 outp = output_buffer;
Josh Triplettb2c95d92013-08-20 17:20:18 -07002903 if (outp == NULL)
2904 err(-1, "calloc output buffer");
Len Brownc98d5d92012-06-04 00:56:40 -04002905}
2906
2907void setup_all_buffers(void)
2908{
2909 topology_probe();
2910 allocate_counters(&thread_even, &core_even, &package_even);
2911 allocate_counters(&thread_odd, &core_odd, &package_odd);
2912 allocate_output_buffer();
2913 for_all_proc_cpus(initialize_counters);
2914}
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02002915
Len Brown103a8fe2010-10-22 23:53:03 -04002916void turbostat_init()
2917{
Len Brown103a8fe2010-10-22 23:53:03 -04002918 check_dev_msr();
Len Brown98481e72014-08-15 00:36:50 -04002919 check_permissions();
Len Brownfcd17212015-03-23 20:29:09 -04002920 process_cpuid();
Len Brown103a8fe2010-10-22 23:53:03 -04002921
Len Brownc98d5d92012-06-04 00:56:40 -04002922 setup_all_buffers();
Len Brown103a8fe2010-10-22 23:53:03 -04002923
Len Brownd8af6f52015-02-10 01:56:38 -05002924 if (debug)
Len Brown889facb2012-11-08 00:48:57 -05002925 for_all_cpus(print_epb, ODD_COUNTERS);
2926
Len Brownd8af6f52015-02-10 01:56:38 -05002927 if (debug)
Len Brown3a9a9412014-08-15 02:39:52 -04002928 for_all_cpus(print_perf_limit, ODD_COUNTERS);
2929
Len Brownd8af6f52015-02-10 01:56:38 -05002930 if (debug)
Len Brown889facb2012-11-08 00:48:57 -05002931 for_all_cpus(print_rapl, ODD_COUNTERS);
2932
2933 for_all_cpus(set_temperature_target, ODD_COUNTERS);
2934
Len Brownd8af6f52015-02-10 01:56:38 -05002935 if (debug)
Len Brown889facb2012-11-08 00:48:57 -05002936 for_all_cpus(print_thermal, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04002937}
2938
2939int fork_it(char **argv)
2940{
Len Brown103a8fe2010-10-22 23:53:03 -04002941 pid_t child_pid;
Len Brownd91bb172012-11-01 00:08:19 -04002942 int status;
Len Brownd15cf7c2012-06-03 23:24:00 -04002943
Len Brownd91bb172012-11-01 00:08:19 -04002944 status = for_all_cpus(get_counters, EVEN_COUNTERS);
2945 if (status)
2946 exit(status);
Len Brownc98d5d92012-06-04 00:56:40 -04002947 /* clear affinity side-effect of get_counters() */
2948 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
Len Brown103a8fe2010-10-22 23:53:03 -04002949 gettimeofday(&tv_even, (struct timezone *)NULL);
2950
2951 child_pid = fork();
2952 if (!child_pid) {
2953 /* child */
2954 execvp(argv[0], argv);
2955 } else {
Len Brown103a8fe2010-10-22 23:53:03 -04002956
2957 /* parent */
Josh Triplettb2c95d92013-08-20 17:20:18 -07002958 if (child_pid == -1)
2959 err(1, "fork");
Len Brown103a8fe2010-10-22 23:53:03 -04002960
2961 signal(SIGINT, SIG_IGN);
2962 signal(SIGQUIT, SIG_IGN);
Josh Triplettb2c95d92013-08-20 17:20:18 -07002963 if (waitpid(child_pid, &status, 0) == -1)
2964 err(status, "waitpid");
Len Brown103a8fe2010-10-22 23:53:03 -04002965 }
Len Brownc98d5d92012-06-04 00:56:40 -04002966 /*
2967 * n.b. fork_it() does not check for errors from for_all_cpus()
2968 * because re-starting is problematic when forking
2969 */
2970 for_all_cpus(get_counters, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04002971 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002972 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04002973 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
2974 compute_average(EVEN_COUNTERS);
2975 format_all_counters(EVEN_COUNTERS);
2976 flush_stderr();
Len Brown103a8fe2010-10-22 23:53:03 -04002977
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07002978 fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
Len Brown103a8fe2010-10-22 23:53:03 -04002979
Len Brownd91bb172012-11-01 00:08:19 -04002980 return status;
Len Brown103a8fe2010-10-22 23:53:03 -04002981}
2982
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02002983int get_and_dump_counters(void)
2984{
2985 int status;
2986
2987 status = for_all_cpus(get_counters, ODD_COUNTERS);
2988 if (status)
2989 return status;
2990
2991 status = for_all_cpus(dump_counters, ODD_COUNTERS);
2992 if (status)
2993 return status;
2994
2995 flush_stdout();
2996
2997 return status;
2998}
2999
Len Brownd8af6f52015-02-10 01:56:38 -05003000void print_version() {
Len Browne9257f52015-04-01 21:02:57 -04003001 fprintf(stderr, "turbostat version 4.5 2 Apr, 2015"
Len Brownd8af6f52015-02-10 01:56:38 -05003002 " - Len Brown <lenb@kernel.org>\n");
3003}
3004
Len Brown103a8fe2010-10-22 23:53:03 -04003005void cmdline(int argc, char **argv)
3006{
3007 int opt;
Len Brownd8af6f52015-02-10 01:56:38 -05003008 int option_index = 0;
3009 static struct option long_options[] = {
3010 {"Counter", required_argument, 0, 'C'},
3011 {"counter", required_argument, 0, 'c'},
3012 {"Dump", no_argument, 0, 'D'},
3013 {"debug", no_argument, 0, 'd'},
3014 {"interval", required_argument, 0, 'i'},
3015 {"help", no_argument, 0, 'h'},
3016 {"Joules", no_argument, 0, 'J'},
3017 {"MSR", required_argument, 0, 'M'},
3018 {"msr", required_argument, 0, 'm'},
3019 {"Package", no_argument, 0, 'p'},
3020 {"processor", no_argument, 0, 'p'},
3021 {"Summary", no_argument, 0, 'S'},
3022 {"TCC", required_argument, 0, 'T'},
3023 {"version", no_argument, 0, 'v' },
3024 {0, 0, 0, 0 }
3025 };
Len Brown103a8fe2010-10-22 23:53:03 -04003026
3027 progname = argv[0];
3028
Len Brownd8af6f52015-02-10 01:56:38 -05003029 while ((opt = getopt_long_only(argc, argv, "C:c:Ddhi:JM:m:PpST:v",
3030 long_options, &option_index)) != -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04003031 switch (opt) {
Len Brownd8af6f52015-02-10 01:56:38 -05003032 case 'C':
3033 sscanf(optarg, "%x", &extra_delta_offset64);
Len Brown103a8fe2010-10-22 23:53:03 -04003034 break;
Len Brownf9240812012-10-06 15:26:31 -04003035 case 'c':
Len Brown8e180f32012-09-22 01:25:08 -04003036 sscanf(optarg, "%x", &extra_delta_offset32);
3037 break;
Len Brownd8af6f52015-02-10 01:56:38 -05003038 case 'D':
3039 dump_only++;
Len Brown8e180f32012-09-22 01:25:08 -04003040 break;
Len Brownd8af6f52015-02-10 01:56:38 -05003041 case 'd':
3042 debug++;
Len Brown2f32edf2012-09-21 23:45:46 -04003043 break;
Len Brownd8af6f52015-02-10 01:56:38 -05003044 case 'h':
3045 default:
3046 help();
3047 exit(1);
3048 case 'i':
3049 interval_sec = atoi(optarg);
Len Brown889facb2012-11-08 00:48:57 -05003050 break;
Dirk Brandewie5c56be92013-12-16 10:23:41 -08003051 case 'J':
3052 rapl_joules++;
3053 break;
Len Brownd8af6f52015-02-10 01:56:38 -05003054 case 'M':
3055 sscanf(optarg, "%x", &extra_msr_offset64);
3056 break;
3057 case 'm':
3058 sscanf(optarg, "%x", &extra_msr_offset32);
3059 break;
3060 case 'P':
3061 show_pkg_only++;
3062 break;
3063 case 'p':
3064 show_core_only++;
3065 break;
3066 case 'S':
3067 summary_only++;
3068 break;
3069 case 'T':
3070 tcc_activation_temp_override = atoi(optarg);
3071 break;
3072 case 'v':
3073 print_version();
3074 exit(0);
3075 break;
Len Brown103a8fe2010-10-22 23:53:03 -04003076 }
3077 }
3078}
3079
3080int main(int argc, char **argv)
3081{
3082 cmdline(argc, argv);
3083
Len Brownd8af6f52015-02-10 01:56:38 -05003084 if (debug)
3085 print_version();
Len Brown103a8fe2010-10-22 23:53:03 -04003086
3087 turbostat_init();
3088
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02003089 /* dump counters and exit */
3090 if (dump_only)
3091 return get_and_dump_counters();
3092
Len Brown103a8fe2010-10-22 23:53:03 -04003093 /*
3094 * if any params left, it must be a command to fork
3095 */
3096 if (argc - optind)
3097 return fork_it(argv + optind);
3098 else
3099 turbostat_loop();
3100
3101 return 0;
3102}