blob: 3f5a9af34f471bced94ecdbce0c13260fa0217db [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;
Len Brownee7e38e2015-02-09 23:39:45 -050055unsigned int do_pc2;
56unsigned int do_pc3;
57unsigned int do_pc6;
58unsigned int do_pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -080059unsigned int do_c8_c9_c10;
Len Brown144b44b2013-11-09 00:30:16 -050060unsigned int do_slm_cstates;
61unsigned int use_c1_residency_msr;
Len Brown103a8fe2010-10-22 23:53:03 -040062unsigned int has_aperf;
Len Brown889facb2012-11-08 00:48:57 -050063unsigned int has_epb;
Len Brownfc04cc62014-02-06 00:55:19 -050064unsigned int units = 1000000; /* MHz etc */
Len Brown103a8fe2010-10-22 23:53:03 -040065unsigned int genuine_intel;
66unsigned int has_invariant_tsc;
Len Brownd7899442015-01-23 00:12:33 -050067unsigned int do_nhm_platform_info;
68unsigned int do_nhm_turbo_ratio_limit;
Len Brown6574a5d2012-09-21 00:01:31 -040069unsigned int do_ivt_turbo_ratio_limit;
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;
87double rapl_power_units, rapl_energy_units, rapl_time_units;
88double rapl_joule_counter_range;
Len Brown3a9a9412014-08-15 02:39:52 -040089unsigned int do_core_perf_limit_reasons;
90unsigned int do_gfx_perf_limit_reasons;
91unsigned int do_ring_perf_limit_reasons;
Len Brown889facb2012-11-08 00:48:57 -050092
Len Browne6f9bb32013-12-03 02:19:19 -050093#define RAPL_PKG (1 << 0)
94 /* 0x610 MSR_PKG_POWER_LIMIT */
95 /* 0x611 MSR_PKG_ENERGY_STATUS */
96#define RAPL_PKG_PERF_STATUS (1 << 1)
97 /* 0x613 MSR_PKG_PERF_STATUS */
98#define RAPL_PKG_POWER_INFO (1 << 2)
99 /* 0x614 MSR_PKG_POWER_INFO */
100
101#define RAPL_DRAM (1 << 3)
102 /* 0x618 MSR_DRAM_POWER_LIMIT */
103 /* 0x619 MSR_DRAM_ENERGY_STATUS */
104 /* 0x61c MSR_DRAM_POWER_INFO */
105#define RAPL_DRAM_PERF_STATUS (1 << 4)
106 /* 0x61b MSR_DRAM_PERF_STATUS */
107
108#define RAPL_CORES (1 << 5)
109 /* 0x638 MSR_PP0_POWER_LIMIT */
110 /* 0x639 MSR_PP0_ENERGY_STATUS */
111#define RAPL_CORE_POLICY (1 << 6)
112 /* 0x63a MSR_PP0_POLICY */
113
114
115#define RAPL_GFX (1 << 7)
116 /* 0x640 MSR_PP1_POWER_LIMIT */
117 /* 0x641 MSR_PP1_ENERGY_STATUS */
118 /* 0x642 MSR_PP1_POLICY */
Len Brown889facb2012-11-08 00:48:57 -0500119#define TJMAX_DEFAULT 100
120
121#define MAX(a, b) ((a) > (b) ? (a) : (b))
Len Brown103a8fe2010-10-22 23:53:03 -0400122
123int aperf_mperf_unstable;
124int backwards_count;
125char *progname;
Len Brown103a8fe2010-10-22 23:53:03 -0400126
Len Brownc98d5d92012-06-04 00:56:40 -0400127cpu_set_t *cpu_present_set, *cpu_affinity_set;
128size_t cpu_present_setsize, cpu_affinity_setsize;
Len Brown103a8fe2010-10-22 23:53:03 -0400129
Len Brownc98d5d92012-06-04 00:56:40 -0400130struct thread_data {
131 unsigned long long tsc;
132 unsigned long long aperf;
133 unsigned long long mperf;
Len Brown144b44b2013-11-09 00:30:16 -0500134 unsigned long long c1;
Len Brown2f32edf2012-09-21 23:45:46 -0400135 unsigned long long extra_msr64;
Len Brown8e180f32012-09-22 01:25:08 -0400136 unsigned long long extra_delta64;
137 unsigned long long extra_msr32;
138 unsigned long long extra_delta32;
Len Brown1ed51012013-02-10 17:19:24 -0500139 unsigned int smi_count;
Len Brownc98d5d92012-06-04 00:56:40 -0400140 unsigned int cpu_id;
141 unsigned int flags;
142#define CPU_IS_FIRST_THREAD_IN_CORE 0x2
143#define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
144} *thread_even, *thread_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400145
Len Brownc98d5d92012-06-04 00:56:40 -0400146struct core_data {
147 unsigned long long c3;
148 unsigned long long c6;
149 unsigned long long c7;
Len Brown889facb2012-11-08 00:48:57 -0500150 unsigned int core_temp_c;
Len Brownc98d5d92012-06-04 00:56:40 -0400151 unsigned int core_id;
152} *core_even, *core_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400153
Len Brownc98d5d92012-06-04 00:56:40 -0400154struct pkg_data {
155 unsigned long long pc2;
156 unsigned long long pc3;
157 unsigned long long pc6;
158 unsigned long long pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800159 unsigned long long pc8;
160 unsigned long long pc9;
161 unsigned long long pc10;
Len Brownc98d5d92012-06-04 00:56:40 -0400162 unsigned int package_id;
Len Brown889facb2012-11-08 00:48:57 -0500163 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
164 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
165 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
166 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
167 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
168 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
169 unsigned int pkg_temp_c;
170
Len Brownc98d5d92012-06-04 00:56:40 -0400171} *package_even, *package_odd;
172
173#define ODD_COUNTERS thread_odd, core_odd, package_odd
174#define EVEN_COUNTERS thread_even, core_even, package_even
175
176#define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
177 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
178 topo.num_threads_per_core + \
179 (core_no) * topo.num_threads_per_core + (thread_no))
180#define GET_CORE(core_base, core_no, pkg_no) \
181 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
182#define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
183
184struct system_summary {
185 struct thread_data threads;
186 struct core_data cores;
187 struct pkg_data packages;
188} sum, average;
189
190
191struct topo_params {
192 int num_packages;
193 int num_cpus;
194 int num_cores;
195 int max_cpu_num;
196 int num_cores_per_pkg;
197 int num_threads_per_core;
198} topo;
199
200struct timeval tv_even, tv_odd, tv_delta;
201
202void setup_all_buffers(void);
203
204int cpu_is_not_present(int cpu)
Len Brownd15cf7c2012-06-03 23:24:00 -0400205{
Len Brownc98d5d92012-06-04 00:56:40 -0400206 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brownd15cf7c2012-06-03 23:24:00 -0400207}
Len Brown88c32812012-03-29 21:44:40 -0400208/*
Len Brownc98d5d92012-06-04 00:56:40 -0400209 * run func(thread, core, package) in topology order
210 * skip non-present cpus
Len Brown88c32812012-03-29 21:44:40 -0400211 */
Len Brownd15cf7c2012-06-03 23:24:00 -0400212
Len Brownc98d5d92012-06-04 00:56:40 -0400213int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
214 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
Len Brown88c32812012-03-29 21:44:40 -0400215{
Len Brownc98d5d92012-06-04 00:56:40 -0400216 int retval, pkg_no, core_no, thread_no;
217
218 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
219 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
220 for (thread_no = 0; thread_no <
221 topo.num_threads_per_core; ++thread_no) {
222 struct thread_data *t;
223 struct core_data *c;
224 struct pkg_data *p;
225
226 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
227
228 if (cpu_is_not_present(t->cpu_id))
229 continue;
230
231 c = GET_CORE(core_base, core_no, pkg_no);
232 p = GET_PKG(pkg_base, pkg_no);
233
234 retval = func(t, c, p);
235 if (retval)
236 return retval;
237 }
238 }
239 }
240 return 0;
Len Brown88c32812012-03-29 21:44:40 -0400241}
242
243int cpu_migrate(int cpu)
244{
Len Brownc98d5d92012-06-04 00:56:40 -0400245 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
246 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
247 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
Len Brown88c32812012-03-29 21:44:40 -0400248 return -1;
249 else
250 return 0;
251}
252
Len Brown15aaa342012-03-29 22:19:58 -0400253int get_msr(int cpu, off_t offset, unsigned long long *msr)
Len Brown103a8fe2010-10-22 23:53:03 -0400254{
255 ssize_t retval;
Len Brown103a8fe2010-10-22 23:53:03 -0400256 char pathname[32];
257 int fd;
258
259 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
260 fd = open(pathname, O_RDONLY);
Len Brown15aaa342012-03-29 22:19:58 -0400261 if (fd < 0)
Len Brown98481e72014-08-15 00:36:50 -0400262 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 -0400263
Len Brown15aaa342012-03-29 22:19:58 -0400264 retval = pread(fd, msr, sizeof *msr, offset);
Len Brown103a8fe2010-10-22 23:53:03 -0400265 close(fd);
Len Brown15aaa342012-03-29 22:19:58 -0400266
Len Brown98481e72014-08-15 00:36:50 -0400267 if (retval != sizeof *msr)
268 err(-1, "%s offset 0x%llx read failed", pathname, (unsigned long long)offset);
Len Brown15aaa342012-03-29 22:19:58 -0400269
270 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400271}
272
Len Brownfc04cc62014-02-06 00:55:19 -0500273/*
274 * Example Format w/ field column widths:
275 *
Len Browne7c95ff2014-08-14 21:22:13 -0400276 * 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
277 * 123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678
Len Brownfc04cc62014-02-06 00:55:19 -0500278 */
279
Len Browna829eb42011-02-10 23:36:34 -0500280void print_header(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400281{
282 if (show_pkg)
Len Browne7c95ff2014-08-14 21:22:13 -0400283 outp += sprintf(outp, " Package");
Len Brown103a8fe2010-10-22 23:53:03 -0400284 if (show_core)
Len Browne7c95ff2014-08-14 21:22:13 -0400285 outp += sprintf(outp, " Core");
Len Brown103a8fe2010-10-22 23:53:03 -0400286 if (show_cpu)
Len Browne7c95ff2014-08-14 21:22:13 -0400287 outp += sprintf(outp, " CPU");
Len Brown103a8fe2010-10-22 23:53:03 -0400288 if (has_aperf)
Len Browne7c95ff2014-08-14 21:22:13 -0400289 outp += sprintf(outp, " Avg_MHz");
Len Brownd7899442015-01-23 00:12:33 -0500290 if (has_aperf)
Len Browne7c95ff2014-08-14 21:22:13 -0400291 outp += sprintf(outp, " %%Busy");
Len Brownfc04cc62014-02-06 00:55:19 -0500292 if (has_aperf)
Len Browne7c95ff2014-08-14 21:22:13 -0400293 outp += sprintf(outp, " Bzy_MHz");
294 outp += sprintf(outp, " TSC_MHz");
Len Brown1ed51012013-02-10 17:19:24 -0500295 if (do_smi)
Len Browne7c95ff2014-08-14 21:22:13 -0400296 outp += sprintf(outp, " SMI");
Len Brown8e180f32012-09-22 01:25:08 -0400297 if (extra_delta_offset32)
Len Browne7c95ff2014-08-14 21:22:13 -0400298 outp += sprintf(outp, " count 0x%03X", extra_delta_offset32);
Len Brown8e180f32012-09-22 01:25:08 -0400299 if (extra_delta_offset64)
Len Browne7c95ff2014-08-14 21:22:13 -0400300 outp += sprintf(outp, " COUNT 0x%03X", extra_delta_offset64);
Len Brown2f32edf2012-09-21 23:45:46 -0400301 if (extra_msr_offset32)
Len Browne7c95ff2014-08-14 21:22:13 -0400302 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset32);
Len Brown2f32edf2012-09-21 23:45:46 -0400303 if (extra_msr_offset64)
Len Browne7c95ff2014-08-14 21:22:13 -0400304 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset64);
Len Brown103a8fe2010-10-22 23:53:03 -0400305 if (do_nhm_cstates)
Len Browne7c95ff2014-08-14 21:22:13 -0400306 outp += sprintf(outp, " CPU%%c1");
Len Brown144b44b2013-11-09 00:30:16 -0500307 if (do_nhm_cstates && !do_slm_cstates)
Len Browne7c95ff2014-08-14 21:22:13 -0400308 outp += sprintf(outp, " CPU%%c3");
Len Brown103a8fe2010-10-22 23:53:03 -0400309 if (do_nhm_cstates)
Len Browne7c95ff2014-08-14 21:22:13 -0400310 outp += sprintf(outp, " CPU%%c6");
Len Brown103a8fe2010-10-22 23:53:03 -0400311 if (do_snb_cstates)
Len Browne7c95ff2014-08-14 21:22:13 -0400312 outp += sprintf(outp, " CPU%%c7");
Len Brown889facb2012-11-08 00:48:57 -0500313
314 if (do_dts)
Len Browne7c95ff2014-08-14 21:22:13 -0400315 outp += sprintf(outp, " CoreTmp");
Len Brown889facb2012-11-08 00:48:57 -0500316 if (do_ptm)
Len Browne7c95ff2014-08-14 21:22:13 -0400317 outp += sprintf(outp, " PkgTmp");
Len Brown889facb2012-11-08 00:48:57 -0500318
Len Brownee7e38e2015-02-09 23:39:45 -0500319 if (do_pc2)
Len Browne7c95ff2014-08-14 21:22:13 -0400320 outp += sprintf(outp, " Pkg%%pc2");
Len Brownee7e38e2015-02-09 23:39:45 -0500321 if (do_pc3)
Len Browne7c95ff2014-08-14 21:22:13 -0400322 outp += sprintf(outp, " Pkg%%pc3");
Len Brownee7e38e2015-02-09 23:39:45 -0500323 if (do_pc6)
Len Browne7c95ff2014-08-14 21:22:13 -0400324 outp += sprintf(outp, " Pkg%%pc6");
Len Brownee7e38e2015-02-09 23:39:45 -0500325 if (do_pc7)
Len Browne7c95ff2014-08-14 21:22:13 -0400326 outp += sprintf(outp, " Pkg%%pc7");
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800327 if (do_c8_c9_c10) {
Len Browne7c95ff2014-08-14 21:22:13 -0400328 outp += sprintf(outp, " Pkg%%pc8");
329 outp += sprintf(outp, " Pkg%%pc9");
330 outp += sprintf(outp, " Pk%%pc10");
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800331 }
Len Brown103a8fe2010-10-22 23:53:03 -0400332
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800333 if (do_rapl && !rapl_joules) {
334 if (do_rapl & RAPL_PKG)
Len Browne7c95ff2014-08-14 21:22:13 -0400335 outp += sprintf(outp, " PkgWatt");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800336 if (do_rapl & RAPL_CORES)
Len Browne7c95ff2014-08-14 21:22:13 -0400337 outp += sprintf(outp, " CorWatt");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800338 if (do_rapl & RAPL_GFX)
Len Browne7c95ff2014-08-14 21:22:13 -0400339 outp += sprintf(outp, " GFXWatt");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800340 if (do_rapl & RAPL_DRAM)
Len Browne7c95ff2014-08-14 21:22:13 -0400341 outp += sprintf(outp, " RAMWatt");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800342 if (do_rapl & RAPL_PKG_PERF_STATUS)
Len Browne7c95ff2014-08-14 21:22:13 -0400343 outp += sprintf(outp, " PKG_%%");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800344 if (do_rapl & RAPL_DRAM_PERF_STATUS)
Len Browne7c95ff2014-08-14 21:22:13 -0400345 outp += sprintf(outp, " RAM_%%");
Len Brownd7899442015-01-23 00:12:33 -0500346 } else if (do_rapl && rapl_joules) {
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800347 if (do_rapl & RAPL_PKG)
Len Browne7c95ff2014-08-14 21:22:13 -0400348 outp += sprintf(outp, " Pkg_J");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800349 if (do_rapl & RAPL_CORES)
Len Browne7c95ff2014-08-14 21:22:13 -0400350 outp += sprintf(outp, " Cor_J");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800351 if (do_rapl & RAPL_GFX)
Len Browne7c95ff2014-08-14 21:22:13 -0400352 outp += sprintf(outp, " GFX_J");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800353 if (do_rapl & RAPL_DRAM)
Len Browne7c95ff2014-08-14 21:22:13 -0400354 outp += sprintf(outp, " RAM_W");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800355 if (do_rapl & RAPL_PKG_PERF_STATUS)
Len Browne7c95ff2014-08-14 21:22:13 -0400356 outp += sprintf(outp, " PKG_%%");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800357 if (do_rapl & RAPL_DRAM_PERF_STATUS)
Len Browne7c95ff2014-08-14 21:22:13 -0400358 outp += sprintf(outp, " RAM_%%");
359 outp += sprintf(outp, " time");
Len Brown889facb2012-11-08 00:48:57 -0500360
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800361 }
Len Brownc98d5d92012-06-04 00:56:40 -0400362 outp += sprintf(outp, "\n");
Len Brown103a8fe2010-10-22 23:53:03 -0400363}
364
Len Brownc98d5d92012-06-04 00:56:40 -0400365int dump_counters(struct thread_data *t, struct core_data *c,
366 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400367{
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200368 outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400369
Len Brownc98d5d92012-06-04 00:56:40 -0400370 if (t) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200371 outp += sprintf(outp, "CPU: %d flags 0x%x\n",
372 t->cpu_id, t->flags);
373 outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
374 outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
375 outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
376 outp += sprintf(outp, "c1: %016llX\n", t->c1);
377 outp += sprintf(outp, "msr0x%x: %08llX\n",
Len Brown8e180f32012-09-22 01:25:08 -0400378 extra_delta_offset32, t->extra_delta32);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200379 outp += sprintf(outp, "msr0x%x: %016llX\n",
Len Brown8e180f32012-09-22 01:25:08 -0400380 extra_delta_offset64, t->extra_delta64);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200381 outp += sprintf(outp, "msr0x%x: %08llX\n",
Len Brown2f32edf2012-09-21 23:45:46 -0400382 extra_msr_offset32, t->extra_msr32);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200383 outp += sprintf(outp, "msr0x%x: %016llX\n",
Len Brown2f32edf2012-09-21 23:45:46 -0400384 extra_msr_offset64, t->extra_msr64);
Len Brown1ed51012013-02-10 17:19:24 -0500385 if (do_smi)
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200386 outp += sprintf(outp, "SMI: %08X\n", t->smi_count);
Len Brownc98d5d92012-06-04 00:56:40 -0400387 }
Len Brown103a8fe2010-10-22 23:53:03 -0400388
Len Brownc98d5d92012-06-04 00:56:40 -0400389 if (c) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200390 outp += sprintf(outp, "core: %d\n", c->core_id);
391 outp += sprintf(outp, "c3: %016llX\n", c->c3);
392 outp += sprintf(outp, "c6: %016llX\n", c->c6);
393 outp += sprintf(outp, "c7: %016llX\n", c->c7);
394 outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
Len Brownc98d5d92012-06-04 00:56:40 -0400395 }
396
397 if (p) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200398 outp += sprintf(outp, "package: %d\n", p->package_id);
399 outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
Len Brownee7e38e2015-02-09 23:39:45 -0500400 if (do_pc3)
401 outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
402 if (do_pc6)
403 outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
404 if (do_pc7)
405 outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200406 outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
407 outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
408 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
409 outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
410 outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
411 outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
412 outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
413 outp += sprintf(outp, "Throttle PKG: %0X\n",
414 p->rapl_pkg_perf_status);
415 outp += sprintf(outp, "Throttle RAM: %0X\n",
416 p->rapl_dram_perf_status);
417 outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
Len Brownc98d5d92012-06-04 00:56:40 -0400418 }
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200419
420 outp += sprintf(outp, "\n");
421
Len Brownc98d5d92012-06-04 00:56:40 -0400422 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400423}
424
Len Browne23da032012-02-06 18:37:16 -0500425/*
426 * column formatting convention & formats
Len Browne23da032012-02-06 18:37:16 -0500427 */
Len Brownc98d5d92012-06-04 00:56:40 -0400428int format_counters(struct thread_data *t, struct core_data *c,
429 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400430{
431 double interval_float;
Len Brownfc04cc62014-02-06 00:55:19 -0500432 char *fmt8;
Len Brown103a8fe2010-10-22 23:53:03 -0400433
Len Brownc98d5d92012-06-04 00:56:40 -0400434 /* if showing only 1st thread in core and this isn't one, bail out */
435 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
436 return 0;
437
438 /* if showing only 1st thread in pkg and this isn't one, bail out */
439 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
440 return 0;
441
Len Brown103a8fe2010-10-22 23:53:03 -0400442 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
443
Len Brownc98d5d92012-06-04 00:56:40 -0400444 /* topo columns, print blanks on 1st (average) line */
445 if (t == &average.threads) {
Len Brown103a8fe2010-10-22 23:53:03 -0400446 if (show_pkg)
Len Brownfc04cc62014-02-06 00:55:19 -0500447 outp += sprintf(outp, " -");
Len Brown103a8fe2010-10-22 23:53:03 -0400448 if (show_core)
Len Brownfc04cc62014-02-06 00:55:19 -0500449 outp += sprintf(outp, " -");
Len Brown103a8fe2010-10-22 23:53:03 -0400450 if (show_cpu)
Len Brownfc04cc62014-02-06 00:55:19 -0500451 outp += sprintf(outp, " -");
Len Brown103a8fe2010-10-22 23:53:03 -0400452 } else {
Len Brownc98d5d92012-06-04 00:56:40 -0400453 if (show_pkg) {
454 if (p)
Len Brownfc04cc62014-02-06 00:55:19 -0500455 outp += sprintf(outp, "%8d", p->package_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400456 else
Len Brownfc04cc62014-02-06 00:55:19 -0500457 outp += sprintf(outp, " -");
Len Brownc98d5d92012-06-04 00:56:40 -0400458 }
Len Brownc98d5d92012-06-04 00:56:40 -0400459 if (show_core) {
460 if (c)
Len Brownfc04cc62014-02-06 00:55:19 -0500461 outp += sprintf(outp, "%8d", c->core_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400462 else
Len Brownfc04cc62014-02-06 00:55:19 -0500463 outp += sprintf(outp, " -");
Len Brownc98d5d92012-06-04 00:56:40 -0400464 }
Len Brown103a8fe2010-10-22 23:53:03 -0400465 if (show_cpu)
Len Brownfc04cc62014-02-06 00:55:19 -0500466 outp += sprintf(outp, "%8d", t->cpu_id);
Len Brown103a8fe2010-10-22 23:53:03 -0400467 }
Len Brownfc04cc62014-02-06 00:55:19 -0500468
Len Brownd7899442015-01-23 00:12:33 -0500469 /* Avg_MHz */
Len Brownfc04cc62014-02-06 00:55:19 -0500470 if (has_aperf)
471 outp += sprintf(outp, "%8.0f",
472 1.0 / units * t->aperf / interval_float);
473
Len Brownd7899442015-01-23 00:12:33 -0500474 /* %Busy */
475 if (has_aperf) {
Len Brown103a8fe2010-10-22 23:53:03 -0400476 if (!skip_c0)
Len Brownfc04cc62014-02-06 00:55:19 -0500477 outp += sprintf(outp, "%8.2f", 100.0 * t->mperf/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400478 else
Len Brownfc04cc62014-02-06 00:55:19 -0500479 outp += sprintf(outp, "********");
Len Brown103a8fe2010-10-22 23:53:03 -0400480 }
481
Len Brownd7899442015-01-23 00:12:33 -0500482 /* Bzy_MHz */
Len Brownfc04cc62014-02-06 00:55:19 -0500483 if (has_aperf)
484 outp += sprintf(outp, "%8.0f",
485 1.0 * t->tsc / units * t->aperf / t->mperf / interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400486
Len Brownd7899442015-01-23 00:12:33 -0500487 /* TSC_MHz */
Len Brownfc04cc62014-02-06 00:55:19 -0500488 outp += sprintf(outp, "%8.0f", 1.0 * t->tsc/units/interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400489
Len Brown1ed51012013-02-10 17:19:24 -0500490 /* SMI */
491 if (do_smi)
Len Brownfc04cc62014-02-06 00:55:19 -0500492 outp += sprintf(outp, "%8d", t->smi_count);
Len Brown1ed51012013-02-10 17:19:24 -0500493
Len Brown8e180f32012-09-22 01:25:08 -0400494 /* delta */
495 if (extra_delta_offset32)
496 outp += sprintf(outp, " %11llu", t->extra_delta32);
497
498 /* DELTA */
499 if (extra_delta_offset64)
500 outp += sprintf(outp, " %11llu", t->extra_delta64);
Len Brown2f32edf2012-09-21 23:45:46 -0400501 /* msr */
502 if (extra_msr_offset32)
Len Brown8e180f32012-09-22 01:25:08 -0400503 outp += sprintf(outp, " 0x%08llx", t->extra_msr32);
Len Brown2f32edf2012-09-21 23:45:46 -0400504
Len Brown130ff302012-09-21 22:56:06 -0400505 /* MSR */
Len Brown2f32edf2012-09-21 23:45:46 -0400506 if (extra_msr_offset64)
507 outp += sprintf(outp, " 0x%016llx", t->extra_msr64);
Len Brown130ff302012-09-21 22:56:06 -0400508
Len Brown103a8fe2010-10-22 23:53:03 -0400509 if (do_nhm_cstates) {
510 if (!skip_c1)
Len Brownfc04cc62014-02-06 00:55:19 -0500511 outp += sprintf(outp, "%8.2f", 100.0 * t->c1/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400512 else
Len Brownfc04cc62014-02-06 00:55:19 -0500513 outp += sprintf(outp, "********");
Len Brown103a8fe2010-10-22 23:53:03 -0400514 }
Len Brownc98d5d92012-06-04 00:56:40 -0400515
516 /* print per-core data only for 1st thread in core */
517 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
518 goto done;
519
Len Brown144b44b2013-11-09 00:30:16 -0500520 if (do_nhm_cstates && !do_slm_cstates)
Len Brownfc04cc62014-02-06 00:55:19 -0500521 outp += sprintf(outp, "%8.2f", 100.0 * c->c3/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400522 if (do_nhm_cstates)
Len Brownfc04cc62014-02-06 00:55:19 -0500523 outp += sprintf(outp, "%8.2f", 100.0 * c->c6/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400524 if (do_snb_cstates)
Len Brownfc04cc62014-02-06 00:55:19 -0500525 outp += sprintf(outp, "%8.2f", 100.0 * c->c7/t->tsc);
Len Brownc98d5d92012-06-04 00:56:40 -0400526
Len Brown889facb2012-11-08 00:48:57 -0500527 if (do_dts)
Len Brownfc04cc62014-02-06 00:55:19 -0500528 outp += sprintf(outp, "%8d", c->core_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500529
Len Brownc98d5d92012-06-04 00:56:40 -0400530 /* print per-package data only for 1st core in package */
531 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
532 goto done;
533
Len Brown889facb2012-11-08 00:48:57 -0500534 if (do_ptm)
Len Brownfc04cc62014-02-06 00:55:19 -0500535 outp += sprintf(outp, "%8d", p->pkg_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500536
Len Brownee7e38e2015-02-09 23:39:45 -0500537 if (do_pc2)
Len Brownfc04cc62014-02-06 00:55:19 -0500538 outp += sprintf(outp, "%8.2f", 100.0 * p->pc2/t->tsc);
Len Brownee7e38e2015-02-09 23:39:45 -0500539 if (do_pc3)
Len Brownfc04cc62014-02-06 00:55:19 -0500540 outp += sprintf(outp, "%8.2f", 100.0 * p->pc3/t->tsc);
Len Brownee7e38e2015-02-09 23:39:45 -0500541 if (do_pc6)
Len Brownfc04cc62014-02-06 00:55:19 -0500542 outp += sprintf(outp, "%8.2f", 100.0 * p->pc6/t->tsc);
Len Brownee7e38e2015-02-09 23:39:45 -0500543 if (do_pc7)
Len Brownfc04cc62014-02-06 00:55:19 -0500544 outp += sprintf(outp, "%8.2f", 100.0 * p->pc7/t->tsc);
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800545 if (do_c8_c9_c10) {
Len Brownfc04cc62014-02-06 00:55:19 -0500546 outp += sprintf(outp, "%8.2f", 100.0 * p->pc8/t->tsc);
547 outp += sprintf(outp, "%8.2f", 100.0 * p->pc9/t->tsc);
548 outp += sprintf(outp, "%8.2f", 100.0 * p->pc10/t->tsc);
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800549 }
Len Brown889facb2012-11-08 00:48:57 -0500550
551 /*
552 * If measurement interval exceeds minimum RAPL Joule Counter range,
553 * indicate that results are suspect by printing "**" in fraction place.
554 */
Len Brownfc04cc62014-02-06 00:55:19 -0500555 if (interval_float < rapl_joule_counter_range)
556 fmt8 = "%8.2f";
557 else
558 fmt8 = " %6.0f**";
Len Brown889facb2012-11-08 00:48:57 -0500559
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800560 if (do_rapl && !rapl_joules) {
561 if (do_rapl & RAPL_PKG)
Len Brownfc04cc62014-02-06 00:55:19 -0500562 outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800563 if (do_rapl & RAPL_CORES)
Len Brownfc04cc62014-02-06 00:55:19 -0500564 outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800565 if (do_rapl & RAPL_GFX)
Len Brownfc04cc62014-02-06 00:55:19 -0500566 outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800567 if (do_rapl & RAPL_DRAM)
Len Brownfc04cc62014-02-06 00:55:19 -0500568 outp += sprintf(outp, fmt8, p->energy_dram * rapl_energy_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800569 if (do_rapl & RAPL_PKG_PERF_STATUS)
Len Brownfc04cc62014-02-06 00:55:19 -0500570 outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800571 if (do_rapl & RAPL_DRAM_PERF_STATUS)
Len Brownfc04cc62014-02-06 00:55:19 -0500572 outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
Len Brownd7899442015-01-23 00:12:33 -0500573 } else if (do_rapl && rapl_joules) {
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800574 if (do_rapl & RAPL_PKG)
Len Brownfc04cc62014-02-06 00:55:19 -0500575 outp += sprintf(outp, fmt8,
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800576 p->energy_pkg * rapl_energy_units);
577 if (do_rapl & RAPL_CORES)
Len Brownfc04cc62014-02-06 00:55:19 -0500578 outp += sprintf(outp, fmt8,
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800579 p->energy_cores * rapl_energy_units);
580 if (do_rapl & RAPL_GFX)
Len Brownfc04cc62014-02-06 00:55:19 -0500581 outp += sprintf(outp, fmt8,
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800582 p->energy_gfx * rapl_energy_units);
583 if (do_rapl & RAPL_DRAM)
Len Brownfc04cc62014-02-06 00:55:19 -0500584 outp += sprintf(outp, fmt8,
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800585 p->energy_dram * rapl_energy_units);
586 if (do_rapl & RAPL_PKG_PERF_STATUS)
Len Brownfc04cc62014-02-06 00:55:19 -0500587 outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800588 if (do_rapl & RAPL_DRAM_PERF_STATUS)
Len Brownfc04cc62014-02-06 00:55:19 -0500589 outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
Len Brown889facb2012-11-08 00:48:57 -0500590
Len Brownd7899442015-01-23 00:12:33 -0500591 outp += sprintf(outp, fmt8, interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800592 }
Len Brownc98d5d92012-06-04 00:56:40 -0400593done:
Len Brownc98d5d92012-06-04 00:56:40 -0400594 outp += sprintf(outp, "\n");
595
596 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400597}
598
Len Brownc98d5d92012-06-04 00:56:40 -0400599void flush_stdout()
Len Brown103a8fe2010-10-22 23:53:03 -0400600{
Len Brownc98d5d92012-06-04 00:56:40 -0400601 fputs(output_buffer, stdout);
Len Brownddac0d62012-11-30 01:01:40 -0500602 fflush(stdout);
Len Brownc98d5d92012-06-04 00:56:40 -0400603 outp = output_buffer;
604}
605void flush_stderr()
606{
607 fputs(output_buffer, stderr);
608 outp = output_buffer;
609}
610void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
611{
Len Browne23da032012-02-06 18:37:16 -0500612 static int printed;
Len Brown103a8fe2010-10-22 23:53:03 -0400613
Len Browne23da032012-02-06 18:37:16 -0500614 if (!printed || !summary_only)
615 print_header();
Len Brown103a8fe2010-10-22 23:53:03 -0400616
Len Brownc98d5d92012-06-04 00:56:40 -0400617 if (topo.num_cpus > 1)
618 format_counters(&average.threads, &average.cores,
619 &average.packages);
Len Brown103a8fe2010-10-22 23:53:03 -0400620
Len Browne23da032012-02-06 18:37:16 -0500621 printed = 1;
622
623 if (summary_only)
624 return;
625
Len Brownc98d5d92012-06-04 00:56:40 -0400626 for_all_cpus(format_counters, t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400627}
628
Len Brown889facb2012-11-08 00:48:57 -0500629#define DELTA_WRAP32(new, old) \
630 if (new > old) { \
631 old = new - old; \
632 } else { \
633 old = 0x100000000 + new - old; \
634 }
635
Len Brownc98d5d92012-06-04 00:56:40 -0400636void
637delta_package(struct pkg_data *new, struct pkg_data *old)
Len Brown103a8fe2010-10-22 23:53:03 -0400638{
Len Brownc98d5d92012-06-04 00:56:40 -0400639 old->pc2 = new->pc2 - old->pc2;
Len Brownee7e38e2015-02-09 23:39:45 -0500640 if (do_pc3)
641 old->pc3 = new->pc3 - old->pc3;
642 if (do_pc6)
643 old->pc6 = new->pc6 - old->pc6;
644 if (do_pc7)
645 old->pc7 = new->pc7 - old->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800646 old->pc8 = new->pc8 - old->pc8;
647 old->pc9 = new->pc9 - old->pc9;
648 old->pc10 = new->pc10 - old->pc10;
Len Brown889facb2012-11-08 00:48:57 -0500649 old->pkg_temp_c = new->pkg_temp_c;
650
651 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
652 DELTA_WRAP32(new->energy_cores, old->energy_cores);
653 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
654 DELTA_WRAP32(new->energy_dram, old->energy_dram);
655 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
656 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
Len Brownc98d5d92012-06-04 00:56:40 -0400657}
Len Brown103a8fe2010-10-22 23:53:03 -0400658
Len Brownc98d5d92012-06-04 00:56:40 -0400659void
660delta_core(struct core_data *new, struct core_data *old)
661{
662 old->c3 = new->c3 - old->c3;
663 old->c6 = new->c6 - old->c6;
664 old->c7 = new->c7 - old->c7;
Len Brown889facb2012-11-08 00:48:57 -0500665 old->core_temp_c = new->core_temp_c;
Len Brownc98d5d92012-06-04 00:56:40 -0400666}
Len Brown103a8fe2010-10-22 23:53:03 -0400667
Len Brownc3ae3312012-06-13 21:31:46 -0400668/*
669 * old = new - old
670 */
Len Brownc98d5d92012-06-04 00:56:40 -0400671void
672delta_thread(struct thread_data *new, struct thread_data *old,
673 struct core_data *core_delta)
674{
675 old->tsc = new->tsc - old->tsc;
Len Brown103a8fe2010-10-22 23:53:03 -0400676
Len Brownc98d5d92012-06-04 00:56:40 -0400677 /* check for TSC < 1 Mcycles over interval */
Josh Triplettb2c95d92013-08-20 17:20:18 -0700678 if (old->tsc < (1000 * 1000))
679 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
680 "You can disable all c-states by booting with \"idle=poll\"\n"
681 "or just the deep ones with \"processor.max_cstate=1\"");
Len Brown103a8fe2010-10-22 23:53:03 -0400682
Len Brownc98d5d92012-06-04 00:56:40 -0400683 old->c1 = new->c1 - old->c1;
Len Brown103a8fe2010-10-22 23:53:03 -0400684
Len Browna7296172015-01-23 01:33:58 -0500685 if (has_aperf) {
686 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
687 old->aperf = new->aperf - old->aperf;
688 old->mperf = new->mperf - old->mperf;
689 } else {
Len Brown103a8fe2010-10-22 23:53:03 -0400690
Len Browna7296172015-01-23 01:33:58 -0500691 if (!aperf_mperf_unstable) {
692 fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname);
693 fprintf(stderr, "* Frequency results do not cover entire interval *\n");
694 fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n");
Len Brownc98d5d92012-06-04 00:56:40 -0400695
Len Browna7296172015-01-23 01:33:58 -0500696 aperf_mperf_unstable = 1;
697 }
698 /*
699 * mperf delta is likely a huge "positive" number
700 * can not use it for calculating c0 time
701 */
702 skip_c0 = 1;
703 skip_c1 = 1;
Len Brownc98d5d92012-06-04 00:56:40 -0400704 }
Len Brownc98d5d92012-06-04 00:56:40 -0400705 }
Len Brown103a8fe2010-10-22 23:53:03 -0400706
Len Brown103a8fe2010-10-22 23:53:03 -0400707
Len Brown144b44b2013-11-09 00:30:16 -0500708 if (use_c1_residency_msr) {
709 /*
710 * Some models have a dedicated C1 residency MSR,
711 * which should be more accurate than the derivation below.
712 */
713 } else {
714 /*
715 * As counter collection is not atomic,
716 * it is possible for mperf's non-halted cycles + idle states
717 * to exceed TSC's all cycles: show c1 = 0% in that case.
718 */
719 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
720 old->c1 = 0;
721 else {
722 /* normal case, derive c1 */
723 old->c1 = old->tsc - old->mperf - core_delta->c3
Len Brownc98d5d92012-06-04 00:56:40 -0400724 - core_delta->c6 - core_delta->c7;
Len Brown144b44b2013-11-09 00:30:16 -0500725 }
Len Brownc98d5d92012-06-04 00:56:40 -0400726 }
Len Brownc3ae3312012-06-13 21:31:46 -0400727
Len Brownc98d5d92012-06-04 00:56:40 -0400728 if (old->mperf == 0) {
Len Brownd8af6f52015-02-10 01:56:38 -0500729 if (debug > 1) fprintf(stderr, "cpu%d MPERF 0!\n", old->cpu_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400730 old->mperf = 1; /* divide by 0 protection */
731 }
732
Len Brown8e180f32012-09-22 01:25:08 -0400733 old->extra_delta32 = new->extra_delta32 - old->extra_delta32;
734 old->extra_delta32 &= 0xFFFFFFFF;
735
736 old->extra_delta64 = new->extra_delta64 - old->extra_delta64;
737
Len Brownc98d5d92012-06-04 00:56:40 -0400738 /*
Len Brown8e180f32012-09-22 01:25:08 -0400739 * Extra MSR is just a snapshot, simply copy latest w/o subtracting
Len Brownc98d5d92012-06-04 00:56:40 -0400740 */
Len Brown2f32edf2012-09-21 23:45:46 -0400741 old->extra_msr32 = new->extra_msr32;
742 old->extra_msr64 = new->extra_msr64;
Len Brown1ed51012013-02-10 17:19:24 -0500743
744 if (do_smi)
745 old->smi_count = new->smi_count - old->smi_count;
Len Brownc98d5d92012-06-04 00:56:40 -0400746}
747
748int delta_cpu(struct thread_data *t, struct core_data *c,
749 struct pkg_data *p, struct thread_data *t2,
750 struct core_data *c2, struct pkg_data *p2)
751{
752 /* calculate core delta only for 1st thread in core */
753 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
754 delta_core(c, c2);
755
756 /* always calculate thread delta */
757 delta_thread(t, t2, c2); /* c2 is core delta */
758
759 /* calculate package delta only for 1st core in package */
760 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
761 delta_package(p, p2);
762
763 return 0;
764}
765
766void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
767{
768 t->tsc = 0;
769 t->aperf = 0;
770 t->mperf = 0;
771 t->c1 = 0;
772
Len Brown1ed51012013-02-10 17:19:24 -0500773 t->smi_count = 0;
Len Brown8e180f32012-09-22 01:25:08 -0400774 t->extra_delta32 = 0;
775 t->extra_delta64 = 0;
776
Len Brownc98d5d92012-06-04 00:56:40 -0400777 /* tells format_counters to dump all fields from this set */
778 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
779
780 c->c3 = 0;
781 c->c6 = 0;
782 c->c7 = 0;
Len Brown889facb2012-11-08 00:48:57 -0500783 c->core_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -0400784
785 p->pc2 = 0;
Len Brownee7e38e2015-02-09 23:39:45 -0500786 if (do_pc3)
787 p->pc3 = 0;
788 if (do_pc6)
789 p->pc6 = 0;
790 if (do_pc7)
791 p->pc7 = 0;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800792 p->pc8 = 0;
793 p->pc9 = 0;
794 p->pc10 = 0;
Len Brown889facb2012-11-08 00:48:57 -0500795
796 p->energy_pkg = 0;
797 p->energy_dram = 0;
798 p->energy_cores = 0;
799 p->energy_gfx = 0;
800 p->rapl_pkg_perf_status = 0;
801 p->rapl_dram_perf_status = 0;
802 p->pkg_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -0400803}
804int sum_counters(struct thread_data *t, struct core_data *c,
805 struct pkg_data *p)
806{
807 average.threads.tsc += t->tsc;
808 average.threads.aperf += t->aperf;
809 average.threads.mperf += t->mperf;
810 average.threads.c1 += t->c1;
811
Len Brown8e180f32012-09-22 01:25:08 -0400812 average.threads.extra_delta32 += t->extra_delta32;
813 average.threads.extra_delta64 += t->extra_delta64;
814
Len Brownc98d5d92012-06-04 00:56:40 -0400815 /* sum per-core values only for 1st thread in core */
816 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
817 return 0;
818
819 average.cores.c3 += c->c3;
820 average.cores.c6 += c->c6;
821 average.cores.c7 += c->c7;
822
Len Brown889facb2012-11-08 00:48:57 -0500823 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
824
Len Brownc98d5d92012-06-04 00:56:40 -0400825 /* sum per-pkg values only for 1st core in pkg */
826 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
827 return 0;
828
829 average.packages.pc2 += p->pc2;
Len Brownee7e38e2015-02-09 23:39:45 -0500830 if (do_pc3)
831 average.packages.pc3 += p->pc3;
832 if (do_pc6)
833 average.packages.pc6 += p->pc6;
834 if (do_pc7)
835 average.packages.pc7 += p->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800836 average.packages.pc8 += p->pc8;
837 average.packages.pc9 += p->pc9;
838 average.packages.pc10 += p->pc10;
Len Brownc98d5d92012-06-04 00:56:40 -0400839
Len Brown889facb2012-11-08 00:48:57 -0500840 average.packages.energy_pkg += p->energy_pkg;
841 average.packages.energy_dram += p->energy_dram;
842 average.packages.energy_cores += p->energy_cores;
843 average.packages.energy_gfx += p->energy_gfx;
844
845 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
846
847 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
848 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
Len Brownc98d5d92012-06-04 00:56:40 -0400849 return 0;
850}
851/*
852 * sum the counters for all cpus in the system
853 * compute the weighted average
854 */
855void compute_average(struct thread_data *t, struct core_data *c,
856 struct pkg_data *p)
857{
858 clear_counters(&average.threads, &average.cores, &average.packages);
859
860 for_all_cpus(sum_counters, t, c, p);
861
862 average.threads.tsc /= topo.num_cpus;
863 average.threads.aperf /= topo.num_cpus;
864 average.threads.mperf /= topo.num_cpus;
865 average.threads.c1 /= topo.num_cpus;
866
Len Brown8e180f32012-09-22 01:25:08 -0400867 average.threads.extra_delta32 /= topo.num_cpus;
868 average.threads.extra_delta32 &= 0xFFFFFFFF;
869
870 average.threads.extra_delta64 /= topo.num_cpus;
871
Len Brownc98d5d92012-06-04 00:56:40 -0400872 average.cores.c3 /= topo.num_cores;
873 average.cores.c6 /= topo.num_cores;
874 average.cores.c7 /= topo.num_cores;
875
876 average.packages.pc2 /= topo.num_packages;
Len Brownee7e38e2015-02-09 23:39:45 -0500877 if (do_pc3)
878 average.packages.pc3 /= topo.num_packages;
879 if (do_pc6)
880 average.packages.pc6 /= topo.num_packages;
881 if (do_pc7)
882 average.packages.pc7 /= topo.num_packages;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800883
884 average.packages.pc8 /= topo.num_packages;
885 average.packages.pc9 /= topo.num_packages;
886 average.packages.pc10 /= topo.num_packages;
Len Brownc98d5d92012-06-04 00:56:40 -0400887}
888
889static unsigned long long rdtsc(void)
890{
891 unsigned int low, high;
892
893 asm volatile("rdtsc" : "=a" (low), "=d" (high));
894
895 return low | ((unsigned long long)high) << 32;
896}
897
898
899/*
900 * get_counters(...)
901 * migrate to cpu
902 * acquire and record local counters for that cpu
903 */
904int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
905{
906 int cpu = t->cpu_id;
Len Brown889facb2012-11-08 00:48:57 -0500907 unsigned long long msr;
Len Brownc98d5d92012-06-04 00:56:40 -0400908
Len Browne52966c2012-11-08 22:38:05 -0500909 if (cpu_migrate(cpu)) {
910 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
Len Brownc98d5d92012-06-04 00:56:40 -0400911 return -1;
Len Browne52966c2012-11-08 22:38:05 -0500912 }
Len Brownc98d5d92012-06-04 00:56:40 -0400913
914 t->tsc = rdtsc(); /* we are running on local CPU of interest */
915
916 if (has_aperf) {
Len Brown9c63a652012-10-31 01:29:52 -0400917 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
Len Brownc98d5d92012-06-04 00:56:40 -0400918 return -3;
Len Brown9c63a652012-10-31 01:29:52 -0400919 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
Len Brownc98d5d92012-06-04 00:56:40 -0400920 return -4;
921 }
922
Len Brown1ed51012013-02-10 17:19:24 -0500923 if (do_smi) {
924 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
925 return -5;
926 t->smi_count = msr & 0xFFFFFFFF;
927 }
Len Brown8e180f32012-09-22 01:25:08 -0400928 if (extra_delta_offset32) {
Len Brown889facb2012-11-08 00:48:57 -0500929 if (get_msr(cpu, extra_delta_offset32, &msr))
Len Brown2f32edf2012-09-21 23:45:46 -0400930 return -5;
Len Brown889facb2012-11-08 00:48:57 -0500931 t->extra_delta32 = msr & 0xFFFFFFFF;
Len Brown8e180f32012-09-22 01:25:08 -0400932 }
933
934 if (extra_delta_offset64)
935 if (get_msr(cpu, extra_delta_offset64, &t->extra_delta64))
936 return -5;
937
938 if (extra_msr_offset32) {
Len Brown889facb2012-11-08 00:48:57 -0500939 if (get_msr(cpu, extra_msr_offset32, &msr))
Len Brown8e180f32012-09-22 01:25:08 -0400940 return -5;
Len Brown889facb2012-11-08 00:48:57 -0500941 t->extra_msr32 = msr & 0xFFFFFFFF;
Len Brown8e180f32012-09-22 01:25:08 -0400942 }
Len Brown2f32edf2012-09-21 23:45:46 -0400943
944 if (extra_msr_offset64)
945 if (get_msr(cpu, extra_msr_offset64, &t->extra_msr64))
Len Brownc98d5d92012-06-04 00:56:40 -0400946 return -5;
947
Len Brown144b44b2013-11-09 00:30:16 -0500948 if (use_c1_residency_msr) {
949 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
950 return -6;
951 }
952
Len Brownc98d5d92012-06-04 00:56:40 -0400953 /* collect core counters only for 1st thread in core */
954 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
955 return 0;
956
Len Brown144b44b2013-11-09 00:30:16 -0500957 if (do_nhm_cstates && !do_slm_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -0400958 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
959 return -6;
Len Brown144b44b2013-11-09 00:30:16 -0500960 }
961
962 if (do_nhm_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -0400963 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
964 return -7;
965 }
966
967 if (do_snb_cstates)
968 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
969 return -8;
970
Len Brown889facb2012-11-08 00:48:57 -0500971 if (do_dts) {
972 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
973 return -9;
974 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
975 }
976
977
Len Brownc98d5d92012-06-04 00:56:40 -0400978 /* collect package counters only for 1st core in package */
979 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
980 return 0;
981
Len Brownee7e38e2015-02-09 23:39:45 -0500982 if (do_pc3)
Len Brownc98d5d92012-06-04 00:56:40 -0400983 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
984 return -9;
Len Brownee7e38e2015-02-09 23:39:45 -0500985 if (do_pc6)
Len Brownc98d5d92012-06-04 00:56:40 -0400986 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
987 return -10;
Len Brownee7e38e2015-02-09 23:39:45 -0500988 if (do_pc2)
Len Brownc98d5d92012-06-04 00:56:40 -0400989 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
990 return -11;
Len Brownee7e38e2015-02-09 23:39:45 -0500991 if (do_pc7)
Len Brownc98d5d92012-06-04 00:56:40 -0400992 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
993 return -12;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800994 if (do_c8_c9_c10) {
995 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
996 return -13;
997 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
998 return -13;
999 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1000 return -13;
1001 }
Len Brown889facb2012-11-08 00:48:57 -05001002 if (do_rapl & RAPL_PKG) {
1003 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1004 return -13;
1005 p->energy_pkg = msr & 0xFFFFFFFF;
1006 }
1007 if (do_rapl & RAPL_CORES) {
1008 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1009 return -14;
1010 p->energy_cores = msr & 0xFFFFFFFF;
1011 }
1012 if (do_rapl & RAPL_DRAM) {
1013 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1014 return -15;
1015 p->energy_dram = msr & 0xFFFFFFFF;
1016 }
1017 if (do_rapl & RAPL_GFX) {
1018 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1019 return -16;
1020 p->energy_gfx = msr & 0xFFFFFFFF;
1021 }
1022 if (do_rapl & RAPL_PKG_PERF_STATUS) {
1023 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1024 return -16;
1025 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1026 }
1027 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1028 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1029 return -16;
1030 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1031 }
1032 if (do_ptm) {
1033 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1034 return -17;
1035 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1036 }
Len Brown103a8fe2010-10-22 23:53:03 -04001037 return 0;
1038}
1039
Len Brownee7e38e2015-02-09 23:39:45 -05001040/*
1041 * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1042 * If you change the values, note they are used both in comparisons
1043 * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1044 */
1045
1046#define PCLUKN 0 /* Unknown */
1047#define PCLRSV 1 /* Reserved */
1048#define PCL__0 2 /* PC0 */
1049#define PCL__1 3 /* PC1 */
1050#define PCL__2 4 /* PC2 */
1051#define PCL__3 5 /* PC3 */
1052#define PCL__4 6 /* PC4 */
1053#define PCL__6 7 /* PC6 */
1054#define PCL_6N 8 /* PC6 No Retention */
1055#define PCL_6R 9 /* PC6 Retention */
1056#define PCL__7 10 /* PC7 */
1057#define PCL_7S 11 /* PC7 Shrink */
1058#define PCLUNL 12 /* Unlimited */
1059
1060int pkg_cstate_limit = PCLUKN;
1061char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
1062 "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "unlimited"};
1063
1064int nhm_pkg_cstate_limits[8] = {PCL__0, PCL__1, PCL__3, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLUNL};
1065int snb_pkg_cstate_limits[8] = {PCL__0, PCL__2, PCL_6N, PCL_6R, PCL__7, PCL_7S, PCLRSV, PCLUNL};
1066int hsw_pkg_cstate_limits[8] = {PCL__0, PCL__2, PCL__3, PCL__6, PCL__7, PCL_7S, PCLRSV, PCLUNL};
1067int slv_pkg_cstate_limits[8] = {PCL__0, PCL__1, PCLRSV, PCLRSV, PCL__4, PCLRSV, PCL__6, PCL__7};
1068int amt_pkg_cstate_limits[8] = {PCL__0, PCL__1, PCL__2, PCLRSV, PCLRSV, PCLRSV, PCL__6, PCL__7};
1069int phi_pkg_cstate_limits[8] = {PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLUNL};
1070
Len Brownc98d5d92012-06-04 00:56:40 -04001071void print_verbose_header(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001072{
1073 unsigned long long msr;
1074 unsigned int ratio;
1075
Len Brownd7899442015-01-23 00:12:33 -05001076 if (!do_nhm_platform_info)
Len Brown103a8fe2010-10-22 23:53:03 -04001077 return;
1078
Len Brown9c63a652012-10-31 01:29:52 -04001079 get_msr(0, MSR_NHM_PLATFORM_INFO, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001080
Len Brown67920412013-01-31 15:22:15 -05001081 fprintf(stderr, "cpu0: MSR_NHM_PLATFORM_INFO: 0x%08llx\n", msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001082
Len Brown103a8fe2010-10-22 23:53:03 -04001083 ratio = (msr >> 40) & 0xFF;
1084 fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency\n",
1085 ratio, bclk, ratio * bclk);
1086
1087 ratio = (msr >> 8) & 0xFF;
1088 fprintf(stderr, "%d * %.0f = %.0f MHz TSC frequency\n",
1089 ratio, bclk, ratio * bclk);
1090
Len Brown67920412013-01-31 15:22:15 -05001091 get_msr(0, MSR_IA32_POWER_CTL, &msr);
Len Brown144b44b2013-11-09 00:30:16 -05001092 fprintf(stderr, "cpu0: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
Len Brown67920412013-01-31 15:22:15 -05001093 msr, msr & 0x2 ? "EN" : "DIS");
1094
Len Brown6574a5d2012-09-21 00:01:31 -04001095 if (!do_ivt_turbo_ratio_limit)
1096 goto print_nhm_turbo_ratio_limits;
1097
1098 get_msr(0, MSR_IVT_TURBO_RATIO_LIMIT, &msr);
1099
Len Brown67920412013-01-31 15:22:15 -05001100 fprintf(stderr, "cpu0: MSR_IVT_TURBO_RATIO_LIMIT: 0x%08llx\n", msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001101
1102 ratio = (msr >> 56) & 0xFF;
1103 if (ratio)
1104 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 16 active cores\n",
1105 ratio, bclk, ratio * bclk);
1106
1107 ratio = (msr >> 48) & 0xFF;
1108 if (ratio)
1109 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 15 active cores\n",
1110 ratio, bclk, ratio * bclk);
1111
1112 ratio = (msr >> 40) & 0xFF;
1113 if (ratio)
1114 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 14 active cores\n",
1115 ratio, bclk, ratio * bclk);
1116
1117 ratio = (msr >> 32) & 0xFF;
1118 if (ratio)
1119 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 13 active cores\n",
1120 ratio, bclk, ratio * bclk);
1121
1122 ratio = (msr >> 24) & 0xFF;
1123 if (ratio)
1124 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 12 active cores\n",
1125 ratio, bclk, ratio * bclk);
1126
1127 ratio = (msr >> 16) & 0xFF;
1128 if (ratio)
1129 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 11 active cores\n",
1130 ratio, bclk, ratio * bclk);
1131
1132 ratio = (msr >> 8) & 0xFF;
1133 if (ratio)
1134 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 10 active cores\n",
1135 ratio, bclk, ratio * bclk);
1136
1137 ratio = (msr >> 0) & 0xFF;
1138 if (ratio)
1139 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 9 active cores\n",
1140 ratio, bclk, ratio * bclk);
1141
1142print_nhm_turbo_ratio_limits:
Len Brown889facb2012-11-08 00:48:57 -05001143 get_msr(0, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
1144
1145#define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
1146#define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
1147
1148 fprintf(stderr, "cpu0: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", msr);
1149
Len Brownee7e38e2015-02-09 23:39:45 -05001150 fprintf(stderr, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: %s)\n",
Len Brown889facb2012-11-08 00:48:57 -05001151 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
1152 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
1153 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
1154 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
1155 (msr & (1 << 15)) ? "" : "UN",
Len Brownee7e38e2015-02-09 23:39:45 -05001156 (unsigned int)msr & 7,
1157 pkg_cstate_limit_strings[pkg_cstate_limit]);
Len Brown103a8fe2010-10-22 23:53:03 -04001158
Len Brownd7899442015-01-23 00:12:33 -05001159 if (!do_nhm_turbo_ratio_limit)
Len Brown103a8fe2010-10-22 23:53:03 -04001160 return;
1161
Len Brown9c63a652012-10-31 01:29:52 -04001162 get_msr(0, MSR_NHM_TURBO_RATIO_LIMIT, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001163
Len Brown67920412013-01-31 15:22:15 -05001164 fprintf(stderr, "cpu0: MSR_NHM_TURBO_RATIO_LIMIT: 0x%08llx\n", msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001165
1166 ratio = (msr >> 56) & 0xFF;
1167 if (ratio)
1168 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 8 active cores\n",
1169 ratio, bclk, ratio * bclk);
1170
1171 ratio = (msr >> 48) & 0xFF;
1172 if (ratio)
1173 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 7 active cores\n",
1174 ratio, bclk, ratio * bclk);
1175
1176 ratio = (msr >> 40) & 0xFF;
1177 if (ratio)
1178 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 6 active cores\n",
1179 ratio, bclk, ratio * bclk);
1180
1181 ratio = (msr >> 32) & 0xFF;
1182 if (ratio)
1183 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 5 active cores\n",
1184 ratio, bclk, ratio * bclk);
1185
Len Brown103a8fe2010-10-22 23:53:03 -04001186 ratio = (msr >> 24) & 0xFF;
1187 if (ratio)
1188 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
1189 ratio, bclk, ratio * bclk);
1190
1191 ratio = (msr >> 16) & 0xFF;
1192 if (ratio)
1193 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
1194 ratio, bclk, ratio * bclk);
1195
1196 ratio = (msr >> 8) & 0xFF;
1197 if (ratio)
1198 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
1199 ratio, bclk, ratio * bclk);
1200
1201 ratio = (msr >> 0) & 0xFF;
1202 if (ratio)
1203 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
1204 ratio, bclk, ratio * bclk);
Len Brown3a9a9412014-08-15 02:39:52 -04001205
Len Brown103a8fe2010-10-22 23:53:03 -04001206}
1207
Len Brownc98d5d92012-06-04 00:56:40 -04001208void free_all_buffers(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001209{
Len Brownc98d5d92012-06-04 00:56:40 -04001210 CPU_FREE(cpu_present_set);
1211 cpu_present_set = NULL;
1212 cpu_present_set = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001213
Len Brownc98d5d92012-06-04 00:56:40 -04001214 CPU_FREE(cpu_affinity_set);
1215 cpu_affinity_set = NULL;
1216 cpu_affinity_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001217
Len Brownc98d5d92012-06-04 00:56:40 -04001218 free(thread_even);
1219 free(core_even);
1220 free(package_even);
1221
1222 thread_even = NULL;
1223 core_even = NULL;
1224 package_even = NULL;
1225
1226 free(thread_odd);
1227 free(core_odd);
1228 free(package_odd);
1229
1230 thread_odd = NULL;
1231 core_odd = NULL;
1232 package_odd = NULL;
1233
1234 free(output_buffer);
1235 output_buffer = NULL;
1236 outp = NULL;
Len Brown103a8fe2010-10-22 23:53:03 -04001237}
1238
Len Brownc98d5d92012-06-04 00:56:40 -04001239/*
Josh Triplett57a42a32013-08-20 17:20:17 -07001240 * Open a file, and exit on failure
1241 */
1242FILE *fopen_or_die(const char *path, const char *mode)
1243{
1244 FILE *filep = fopen(path, "r");
Josh Triplettb2c95d92013-08-20 17:20:18 -07001245 if (!filep)
1246 err(1, "%s: open failed", path);
Josh Triplett57a42a32013-08-20 17:20:17 -07001247 return filep;
1248}
1249
1250/*
Josh Triplett95aebc42013-08-20 17:20:16 -07001251 * Parse a file containing a single int.
1252 */
1253int parse_int_file(const char *fmt, ...)
1254{
1255 va_list args;
1256 char path[PATH_MAX];
1257 FILE *filep;
1258 int value;
1259
1260 va_start(args, fmt);
1261 vsnprintf(path, sizeof(path), fmt, args);
1262 va_end(args);
Josh Triplett57a42a32013-08-20 17:20:17 -07001263 filep = fopen_or_die(path, "r");
Josh Triplettb2c95d92013-08-20 17:20:18 -07001264 if (fscanf(filep, "%d", &value) != 1)
1265 err(1, "%s: failed to parse number from file", path);
Josh Triplett95aebc42013-08-20 17:20:16 -07001266 fclose(filep);
1267 return value;
1268}
1269
1270/*
Len Brownc98d5d92012-06-04 00:56:40 -04001271 * cpu_is_first_sibling_in_core(cpu)
1272 * return 1 if given CPU is 1st HT sibling in the core
1273 */
1274int cpu_is_first_sibling_in_core(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001275{
Josh Triplett95aebc42013-08-20 17:20:16 -07001276 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001277}
1278
Len Brownc98d5d92012-06-04 00:56:40 -04001279/*
1280 * cpu_is_first_core_in_package(cpu)
1281 * return 1 if given CPU is 1st core in package
1282 */
1283int cpu_is_first_core_in_package(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001284{
Josh Triplett95aebc42013-08-20 17:20:16 -07001285 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001286}
1287
1288int get_physical_package_id(int cpu)
1289{
Josh Triplett95aebc42013-08-20 17:20:16 -07001290 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001291}
1292
1293int get_core_id(int cpu)
1294{
Josh Triplett95aebc42013-08-20 17:20:16 -07001295 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001296}
1297
Len Brownc98d5d92012-06-04 00:56:40 -04001298int get_num_ht_siblings(int cpu)
1299{
1300 char path[80];
1301 FILE *filep;
1302 int sib1, sib2;
1303 int matches;
1304 char character;
1305
1306 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
Josh Triplett57a42a32013-08-20 17:20:17 -07001307 filep = fopen_or_die(path, "r");
Len Brownc98d5d92012-06-04 00:56:40 -04001308 /*
1309 * file format:
1310 * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4)
1311 * otherwinse 1 sibling (self).
1312 */
1313 matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2);
1314
1315 fclose(filep);
1316
1317 if (matches == 3)
1318 return 2;
1319 else
1320 return 1;
1321}
1322
Len Brown103a8fe2010-10-22 23:53:03 -04001323/*
Len Brownc98d5d92012-06-04 00:56:40 -04001324 * run func(thread, core, package) in topology order
1325 * skip non-present cpus
Len Brown103a8fe2010-10-22 23:53:03 -04001326 */
1327
Len Brownc98d5d92012-06-04 00:56:40 -04001328int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
1329 struct pkg_data *, struct thread_data *, struct core_data *,
1330 struct pkg_data *), struct thread_data *thread_base,
1331 struct core_data *core_base, struct pkg_data *pkg_base,
1332 struct thread_data *thread_base2, struct core_data *core_base2,
1333 struct pkg_data *pkg_base2)
1334{
1335 int retval, pkg_no, core_no, thread_no;
1336
1337 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
1338 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
1339 for (thread_no = 0; thread_no <
1340 topo.num_threads_per_core; ++thread_no) {
1341 struct thread_data *t, *t2;
1342 struct core_data *c, *c2;
1343 struct pkg_data *p, *p2;
1344
1345 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
1346
1347 if (cpu_is_not_present(t->cpu_id))
1348 continue;
1349
1350 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
1351
1352 c = GET_CORE(core_base, core_no, pkg_no);
1353 c2 = GET_CORE(core_base2, core_no, pkg_no);
1354
1355 p = GET_PKG(pkg_base, pkg_no);
1356 p2 = GET_PKG(pkg_base2, pkg_no);
1357
1358 retval = func(t, c, p, t2, c2, p2);
1359 if (retval)
1360 return retval;
1361 }
1362 }
1363 }
1364 return 0;
1365}
1366
1367/*
1368 * run func(cpu) on every cpu in /proc/stat
1369 * return max_cpu number
1370 */
1371int for_all_proc_cpus(int (func)(int))
Len Brown103a8fe2010-10-22 23:53:03 -04001372{
1373 FILE *fp;
Len Brownc98d5d92012-06-04 00:56:40 -04001374 int cpu_num;
Len Brown103a8fe2010-10-22 23:53:03 -04001375 int retval;
1376
Josh Triplett57a42a32013-08-20 17:20:17 -07001377 fp = fopen_or_die(proc_stat, "r");
Len Brown103a8fe2010-10-22 23:53:03 -04001378
1379 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
Josh Triplettb2c95d92013-08-20 17:20:18 -07001380 if (retval != 0)
1381 err(1, "%s: failed to parse format", proc_stat);
Len Brown103a8fe2010-10-22 23:53:03 -04001382
Len Brownc98d5d92012-06-04 00:56:40 -04001383 while (1) {
1384 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 -04001385 if (retval != 1)
1386 break;
1387
Len Brownc98d5d92012-06-04 00:56:40 -04001388 retval = func(cpu_num);
1389 if (retval) {
1390 fclose(fp);
1391 return(retval);
1392 }
Len Brown103a8fe2010-10-22 23:53:03 -04001393 }
1394 fclose(fp);
Len Brownc98d5d92012-06-04 00:56:40 -04001395 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001396}
1397
1398void re_initialize(void)
1399{
Len Brownc98d5d92012-06-04 00:56:40 -04001400 free_all_buffers();
1401 setup_all_buffers();
1402 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04001403}
1404
Len Brownc98d5d92012-06-04 00:56:40 -04001405
Len Brown103a8fe2010-10-22 23:53:03 -04001406/*
Len Brownc98d5d92012-06-04 00:56:40 -04001407 * count_cpus()
1408 * remember the last one seen, it will be the max
Len Brown103a8fe2010-10-22 23:53:03 -04001409 */
Len Brownc98d5d92012-06-04 00:56:40 -04001410int count_cpus(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001411{
Len Brownc98d5d92012-06-04 00:56:40 -04001412 if (topo.max_cpu_num < cpu)
1413 topo.max_cpu_num = cpu;
Len Brown103a8fe2010-10-22 23:53:03 -04001414
Len Brownc98d5d92012-06-04 00:56:40 -04001415 topo.num_cpus += 1;
1416 return 0;
1417}
1418int mark_cpu_present(int cpu)
1419{
1420 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brown15aaa342012-03-29 22:19:58 -04001421 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001422}
1423
1424void turbostat_loop()
1425{
Len Brownc98d5d92012-06-04 00:56:40 -04001426 int retval;
Len Browne52966c2012-11-08 22:38:05 -05001427 int restarted = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001428
Len Brown103a8fe2010-10-22 23:53:03 -04001429restart:
Len Browne52966c2012-11-08 22:38:05 -05001430 restarted++;
1431
Len Brownc98d5d92012-06-04 00:56:40 -04001432 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001433 if (retval < -1) {
1434 exit(retval);
1435 } else if (retval == -1) {
Len Browne52966c2012-11-08 22:38:05 -05001436 if (restarted > 1) {
1437 exit(retval);
1438 }
Len Brownc98d5d92012-06-04 00:56:40 -04001439 re_initialize();
1440 goto restart;
1441 }
Len Browne52966c2012-11-08 22:38:05 -05001442 restarted = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001443 gettimeofday(&tv_even, (struct timezone *)NULL);
1444
1445 while (1) {
Len Brownc98d5d92012-06-04 00:56:40 -04001446 if (for_all_proc_cpus(cpu_is_not_present)) {
Len Brown103a8fe2010-10-22 23:53:03 -04001447 re_initialize();
1448 goto restart;
1449 }
1450 sleep(interval_sec);
Len Brownc98d5d92012-06-04 00:56:40 -04001451 retval = for_all_cpus(get_counters, ODD_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001452 if (retval < -1) {
1453 exit(retval);
1454 } else if (retval == -1) {
Len Brown15aaa342012-03-29 22:19:58 -04001455 re_initialize();
1456 goto restart;
1457 }
Len Brown103a8fe2010-10-22 23:53:03 -04001458 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04001459 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04001460 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
1461 compute_average(EVEN_COUNTERS);
1462 format_all_counters(EVEN_COUNTERS);
1463 flush_stdout();
Len Brown15aaa342012-03-29 22:19:58 -04001464 sleep(interval_sec);
Len Brownc98d5d92012-06-04 00:56:40 -04001465 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001466 if (retval < -1) {
1467 exit(retval);
1468 } else if (retval == -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04001469 re_initialize();
1470 goto restart;
1471 }
Len Brown103a8fe2010-10-22 23:53:03 -04001472 gettimeofday(&tv_even, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04001473 timersub(&tv_even, &tv_odd, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04001474 for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS);
1475 compute_average(ODD_COUNTERS);
1476 format_all_counters(ODD_COUNTERS);
1477 flush_stdout();
Len Brown103a8fe2010-10-22 23:53:03 -04001478 }
1479}
1480
1481void check_dev_msr()
1482{
1483 struct stat sb;
1484
Josh Triplettb2c95d92013-08-20 17:20:18 -07001485 if (stat("/dev/cpu/0/msr", &sb))
Len Brownd7899442015-01-23 00:12:33 -05001486 err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
Len Brown103a8fe2010-10-22 23:53:03 -04001487}
1488
Len Brown98481e72014-08-15 00:36:50 -04001489void check_permissions()
Len Brown103a8fe2010-10-22 23:53:03 -04001490{
Len Brown98481e72014-08-15 00:36:50 -04001491 struct __user_cap_header_struct cap_header_data;
1492 cap_user_header_t cap_header = &cap_header_data;
1493 struct __user_cap_data_struct cap_data_data;
1494 cap_user_data_t cap_data = &cap_data_data;
1495 extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
1496 int do_exit = 0;
1497
1498 /* check for CAP_SYS_RAWIO */
1499 cap_header->pid = getpid();
1500 cap_header->version = _LINUX_CAPABILITY_VERSION;
1501 if (capget(cap_header, cap_data) < 0)
1502 err(-6, "capget(2) failed");
1503
1504 if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
1505 do_exit++;
1506 warnx("capget(CAP_SYS_RAWIO) failed,"
1507 " try \"# setcap cap_sys_rawio=ep %s\"", progname);
1508 }
1509
1510 /* test file permissions */
1511 if (euidaccess("/dev/cpu/0/msr", R_OK)) {
1512 do_exit++;
1513 warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
1514 }
1515
1516 /* if all else fails, thell them to be root */
1517 if (do_exit)
1518 if (getuid() != 0)
Len Brownd7899442015-01-23 00:12:33 -05001519 warnx("... or simply run as root");
Len Brown98481e72014-08-15 00:36:50 -04001520
1521 if (do_exit)
1522 exit(-6);
Len Brown103a8fe2010-10-22 23:53:03 -04001523}
1524
Len Brownd7899442015-01-23 00:12:33 -05001525/*
1526 * NHM adds support for additional MSRs:
1527 *
1528 * MSR_SMI_COUNT 0x00000034
1529 *
1530 * MSR_NHM_PLATFORM_INFO 0x000000ce
1531 * MSR_NHM_SNB_PKG_CST_CFG_CTL 0x000000e2
1532 *
1533 * MSR_PKG_C3_RESIDENCY 0x000003f8
1534 * MSR_PKG_C6_RESIDENCY 0x000003f9
1535 * MSR_CORE_C3_RESIDENCY 0x000003fc
1536 * MSR_CORE_C6_RESIDENCY 0x000003fd
1537 *
Len Brownee7e38e2015-02-09 23:39:45 -05001538 * Side effect:
1539 * sets global pkg_cstate_limit to decode MSR_NHM_SNB_PKG_CST_CFG_CTL
Len Brownd7899442015-01-23 00:12:33 -05001540 */
Len Brownee7e38e2015-02-09 23:39:45 -05001541int probe_nhm_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04001542{
Len Brownee7e38e2015-02-09 23:39:45 -05001543 unsigned long long msr;
1544 int *pkg_cstate_limits;
1545
Len Brown103a8fe2010-10-22 23:53:03 -04001546 if (!genuine_intel)
1547 return 0;
1548
1549 if (family != 6)
1550 return 0;
1551
1552 switch (model) {
1553 case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
1554 case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
1555 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
1556 case 0x25: /* Westmere Client - Clarkdale, Arrandale */
1557 case 0x2C: /* Westmere EP - Gulftown */
Len Brownee7e38e2015-02-09 23:39:45 -05001558 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1559 case 0x2F: /* Westmere-EX Xeon - Eagleton */
1560 pkg_cstate_limits = nhm_pkg_cstate_limits;
1561 break;
Len Brown103a8fe2010-10-22 23:53:03 -04001562 case 0x2A: /* SNB */
1563 case 0x2D: /* SNB Xeon */
Len Brown553575f2011-11-18 03:32:01 -05001564 case 0x3A: /* IVB */
Len Brown13006512012-09-26 18:11:31 -04001565 case 0x3E: /* IVB Xeon */
Len Brownee7e38e2015-02-09 23:39:45 -05001566 pkg_cstate_limits = snb_pkg_cstate_limits;
1567 break;
Len Brown70b43402013-01-08 01:26:07 -05001568 case 0x3C: /* HSW */
Len Browne6f9bb32013-12-03 02:19:19 -05001569 case 0x3F: /* HSX */
Len Brown70b43402013-01-08 01:26:07 -05001570 case 0x45: /* HSW */
Len Brown149c2312013-03-15 10:58:02 -04001571 case 0x46: /* HSW */
Len Brown4e8e863f2014-02-27 23:28:53 -05001572 case 0x3D: /* BDW */
1573 case 0x4F: /* BDX */
1574 case 0x56: /* BDX-DE */
Len Brownee7e38e2015-02-09 23:39:45 -05001575 pkg_cstate_limits = hsw_pkg_cstate_limits;
1576 break;
1577 case 0x37: /* BYT */
1578 case 0x4D: /* AVN */
1579 pkg_cstate_limits = slv_pkg_cstate_limits;
1580 break;
1581 case 0x4C: /* AMT */
1582 pkg_cstate_limits = amt_pkg_cstate_limits;
1583 break;
1584 case 0x57: /* PHI */
1585 pkg_cstate_limits = phi_pkg_cstate_limits;
1586 break;
Len Brown103a8fe2010-10-22 23:53:03 -04001587 default:
1588 return 0;
1589 }
Len Brownee7e38e2015-02-09 23:39:45 -05001590 get_msr(0, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
1591
1592 pkg_cstate_limit = pkg_cstate_limits[msr & 0x7];
1593
1594 return 1;
Len Brown103a8fe2010-10-22 23:53:03 -04001595}
Len Brownd7899442015-01-23 00:12:33 -05001596int has_nhm_turbo_ratio_limit(unsigned int family, unsigned int model)
1597{
Len Brownd7899442015-01-23 00:12:33 -05001598 switch (model) {
1599 /* Nehalem compatible, but do not include turbo-ratio limit support */
1600 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1601 case 0x2F: /* Westmere-EX Xeon - Eagleton */
1602 return 0;
1603 default:
1604 return 1;
1605 }
1606}
Len Brown6574a5d2012-09-21 00:01:31 -04001607int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
1608{
1609 if (!genuine_intel)
1610 return 0;
1611
1612 if (family != 6)
1613 return 0;
1614
1615 switch (model) {
1616 case 0x3E: /* IVB Xeon */
1617 return 1;
1618 default:
1619 return 0;
1620 }
1621}
1622
Len Brown889facb2012-11-08 00:48:57 -05001623/*
1624 * print_epb()
1625 * Decode the ENERGY_PERF_BIAS MSR
1626 */
1627int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1628{
1629 unsigned long long msr;
1630 char *epb_string;
1631 int cpu;
1632
1633 if (!has_epb)
1634 return 0;
1635
1636 cpu = t->cpu_id;
1637
1638 /* EPB is per-package */
1639 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1640 return 0;
1641
1642 if (cpu_migrate(cpu)) {
1643 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1644 return -1;
1645 }
1646
1647 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
1648 return 0;
1649
1650 switch (msr & 0x7) {
1651 case ENERGY_PERF_BIAS_PERFORMANCE:
1652 epb_string = "performance";
1653 break;
1654 case ENERGY_PERF_BIAS_NORMAL:
1655 epb_string = "balanced";
1656 break;
1657 case ENERGY_PERF_BIAS_POWERSAVE:
1658 epb_string = "powersave";
1659 break;
1660 default:
1661 epb_string = "custom";
1662 break;
1663 }
1664 fprintf(stderr, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
1665
1666 return 0;
1667}
1668
Len Brown3a9a9412014-08-15 02:39:52 -04001669/*
1670 * print_perf_limit()
1671 */
1672int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1673{
1674 unsigned long long msr;
1675 int cpu;
1676
1677 cpu = t->cpu_id;
1678
1679 /* per-package */
1680 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1681 return 0;
1682
1683 if (cpu_migrate(cpu)) {
1684 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1685 return -1;
1686 }
1687
1688 if (do_core_perf_limit_reasons) {
1689 get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
1690 fprintf(stderr, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
1691 fprintf(stderr, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
1692 (msr & 1 << 0) ? "PROCHOT, " : "",
1693 (msr & 1 << 1) ? "ThermStatus, " : "",
1694 (msr & 1 << 2) ? "bit2, " : "",
1695 (msr & 1 << 4) ? "Graphics, " : "",
1696 (msr & 1 << 5) ? "Auto-HWP, " : "",
1697 (msr & 1 << 6) ? "VR-Therm, " : "",
1698 (msr & 1 << 8) ? "Amps, " : "",
1699 (msr & 1 << 9) ? "CorePwr, " : "",
1700 (msr & 1 << 10) ? "PkgPwrL1, " : "",
1701 (msr & 1 << 11) ? "PkgPwrL2, " : "",
1702 (msr & 1 << 12) ? "MultiCoreTurbo, " : "",
1703 (msr & 1 << 13) ? "Transitions, " : "",
1704 (msr & 1 << 14) ? "bit14, " : "",
1705 (msr & 1 << 15) ? "bit15, " : "");
1706 fprintf(stderr, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n",
1707 (msr & 1 << 16) ? "PROCHOT, " : "",
1708 (msr & 1 << 17) ? "ThermStatus, " : "",
1709 (msr & 1 << 18) ? "bit18, " : "",
1710 (msr & 1 << 20) ? "Graphics, " : "",
1711 (msr & 1 << 21) ? "Auto-HWP, " : "",
1712 (msr & 1 << 22) ? "VR-Therm, " : "",
1713 (msr & 1 << 24) ? "Amps, " : "",
1714 (msr & 1 << 25) ? "CorePwr, " : "",
1715 (msr & 1 << 26) ? "PkgPwrL1, " : "",
1716 (msr & 1 << 27) ? "PkgPwrL2, " : "",
1717 (msr & 1 << 28) ? "MultiCoreTurbo, " : "",
1718 (msr & 1 << 29) ? "Transitions, " : "",
1719 (msr & 1 << 30) ? "bit30, " : "",
1720 (msr & 1 << 31) ? "bit31, " : "");
1721
1722 }
1723 if (do_gfx_perf_limit_reasons) {
1724 get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
1725 fprintf(stderr, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
1726 fprintf(stderr, " (Active: %s%s%s%s%s%s%s%s)",
1727 (msr & 1 << 0) ? "PROCHOT, " : "",
1728 (msr & 1 << 1) ? "ThermStatus, " : "",
1729 (msr & 1 << 4) ? "Graphics, " : "",
1730 (msr & 1 << 6) ? "VR-Therm, " : "",
1731 (msr & 1 << 8) ? "Amps, " : "",
1732 (msr & 1 << 9) ? "GFXPwr, " : "",
1733 (msr & 1 << 10) ? "PkgPwrL1, " : "",
1734 (msr & 1 << 11) ? "PkgPwrL2, " : "");
1735 fprintf(stderr, " (Logged: %s%s%s%s%s%s%s%s)\n",
1736 (msr & 1 << 16) ? "PROCHOT, " : "",
1737 (msr & 1 << 17) ? "ThermStatus, " : "",
1738 (msr & 1 << 20) ? "Graphics, " : "",
1739 (msr & 1 << 22) ? "VR-Therm, " : "",
1740 (msr & 1 << 24) ? "Amps, " : "",
1741 (msr & 1 << 25) ? "GFXPwr, " : "",
1742 (msr & 1 << 26) ? "PkgPwrL1, " : "",
1743 (msr & 1 << 27) ? "PkgPwrL2, " : "");
1744 }
1745 if (do_ring_perf_limit_reasons) {
1746 get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
1747 fprintf(stderr, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
1748 fprintf(stderr, " (Active: %s%s%s%s%s%s)",
1749 (msr & 1 << 0) ? "PROCHOT, " : "",
1750 (msr & 1 << 1) ? "ThermStatus, " : "",
1751 (msr & 1 << 6) ? "VR-Therm, " : "",
1752 (msr & 1 << 8) ? "Amps, " : "",
1753 (msr & 1 << 10) ? "PkgPwrL1, " : "",
1754 (msr & 1 << 11) ? "PkgPwrL2, " : "");
1755 fprintf(stderr, " (Logged: %s%s%s%s%s%s)\n",
1756 (msr & 1 << 16) ? "PROCHOT, " : "",
1757 (msr & 1 << 17) ? "ThermStatus, " : "",
1758 (msr & 1 << 22) ? "VR-Therm, " : "",
1759 (msr & 1 << 24) ? "Amps, " : "",
1760 (msr & 1 << 26) ? "PkgPwrL1, " : "",
1761 (msr & 1 << 27) ? "PkgPwrL2, " : "");
1762 }
1763 return 0;
1764}
1765
Len Brown889facb2012-11-08 00:48:57 -05001766#define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
1767#define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
1768
Len Brown144b44b2013-11-09 00:30:16 -05001769double get_tdp(model)
1770{
1771 unsigned long long msr;
1772
1773 if (do_rapl & RAPL_PKG_POWER_INFO)
1774 if (!get_msr(0, MSR_PKG_POWER_INFO, &msr))
1775 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
1776
1777 switch (model) {
1778 case 0x37:
1779 case 0x4D:
1780 return 30.0;
1781 default:
1782 return 135.0;
1783 }
1784}
1785
1786
Len Brown889facb2012-11-08 00:48:57 -05001787/*
1788 * rapl_probe()
1789 *
Len Brown144b44b2013-11-09 00:30:16 -05001790 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
Len Brown889facb2012-11-08 00:48:57 -05001791 */
1792void rapl_probe(unsigned int family, unsigned int model)
1793{
1794 unsigned long long msr;
Len Brown144b44b2013-11-09 00:30:16 -05001795 unsigned int time_unit;
Len Brown889facb2012-11-08 00:48:57 -05001796 double tdp;
1797
1798 if (!genuine_intel)
1799 return;
1800
1801 if (family != 6)
1802 return;
1803
1804 switch (model) {
1805 case 0x2A:
1806 case 0x3A:
Len Brown70b43402013-01-08 01:26:07 -05001807 case 0x3C: /* HSW */
Len Brown70b43402013-01-08 01:26:07 -05001808 case 0x45: /* HSW */
Len Brown149c2312013-03-15 10:58:02 -04001809 case 0x46: /* HSW */
Len Brown4e8e863f2014-02-27 23:28:53 -05001810 case 0x3D: /* BDW */
Len Brown144b44b2013-11-09 00:30:16 -05001811 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
Len Brown889facb2012-11-08 00:48:57 -05001812 break;
Len Browne6f9bb32013-12-03 02:19:19 -05001813 case 0x3F: /* HSX */
Len Brown4e8e863f2014-02-27 23:28:53 -05001814 case 0x4F: /* BDX */
1815 case 0x56: /* BDX-DE */
Len Browne6f9bb32013-12-03 02:19:19 -05001816 do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
1817 break;
Len Brown889facb2012-11-08 00:48:57 -05001818 case 0x2D:
1819 case 0x3E:
Len Brown144b44b2013-11-09 00:30:16 -05001820 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_PKG_PERF_STATUS | RAPL_DRAM_PERF_STATUS | RAPL_PKG_POWER_INFO;
1821 break;
1822 case 0x37: /* BYT */
1823 case 0x4D: /* AVN */
1824 do_rapl = RAPL_PKG | RAPL_CORES ;
Len Brown889facb2012-11-08 00:48:57 -05001825 break;
1826 default:
1827 return;
1828 }
1829
1830 /* units on package 0, verify later other packages match */
1831 if (get_msr(0, MSR_RAPL_POWER_UNIT, &msr))
1832 return;
1833
1834 rapl_power_units = 1.0 / (1 << (msr & 0xF));
Len Brown144b44b2013-11-09 00:30:16 -05001835 if (model == 0x37)
1836 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
1837 else
1838 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
Len Brown889facb2012-11-08 00:48:57 -05001839
Len Brown144b44b2013-11-09 00:30:16 -05001840 time_unit = msr >> 16 & 0xF;
1841 if (time_unit == 0)
1842 time_unit = 0xA;
Len Brown889facb2012-11-08 00:48:57 -05001843
Len Brown144b44b2013-11-09 00:30:16 -05001844 rapl_time_units = 1.0 / (1 << (time_unit));
1845
1846 tdp = get_tdp(model);
Len Brown889facb2012-11-08 00:48:57 -05001847
1848 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
Len Brownd8af6f52015-02-10 01:56:38 -05001849 if (debug)
Len Brown144b44b2013-11-09 00:30:16 -05001850 fprintf(stderr, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
Len Brown889facb2012-11-08 00:48:57 -05001851
1852 return;
1853}
1854
Len Brown3a9a9412014-08-15 02:39:52 -04001855void perf_limit_reasons_probe(family, model)
1856{
1857 if (!genuine_intel)
1858 return;
1859
1860 if (family != 6)
1861 return;
1862
1863 switch (model) {
1864 case 0x3C: /* HSW */
1865 case 0x45: /* HSW */
1866 case 0x46: /* HSW */
1867 do_gfx_perf_limit_reasons = 1;
1868 case 0x3F: /* HSX */
1869 do_core_perf_limit_reasons = 1;
1870 do_ring_perf_limit_reasons = 1;
1871 default:
1872 return;
1873 }
1874}
1875
Len Brown889facb2012-11-08 00:48:57 -05001876int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1877{
1878 unsigned long long msr;
1879 unsigned int dts;
1880 int cpu;
1881
1882 if (!(do_dts || do_ptm))
1883 return 0;
1884
1885 cpu = t->cpu_id;
1886
1887 /* DTS is per-core, no need to print for each thread */
1888 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1889 return 0;
1890
1891 if (cpu_migrate(cpu)) {
1892 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1893 return -1;
1894 }
1895
1896 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
1897 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1898 return 0;
1899
1900 dts = (msr >> 16) & 0x7F;
1901 fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
1902 cpu, msr, tcc_activation_temp - dts);
1903
1904#ifdef THERM_DEBUG
1905 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
1906 return 0;
1907
1908 dts = (msr >> 16) & 0x7F;
1909 dts2 = (msr >> 8) & 0x7F;
1910 fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
1911 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
1912#endif
1913 }
1914
1915
1916 if (do_dts) {
1917 unsigned int resolution;
1918
1919 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1920 return 0;
1921
1922 dts = (msr >> 16) & 0x7F;
1923 resolution = (msr >> 27) & 0xF;
1924 fprintf(stderr, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
1925 cpu, msr, tcc_activation_temp - dts, resolution);
1926
1927#ifdef THERM_DEBUG
1928 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
1929 return 0;
1930
1931 dts = (msr >> 16) & 0x7F;
1932 dts2 = (msr >> 8) & 0x7F;
1933 fprintf(stderr, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
1934 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
1935#endif
1936 }
1937
1938 return 0;
1939}
1940
1941void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
1942{
1943 fprintf(stderr, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
1944 cpu, label,
1945 ((msr >> 15) & 1) ? "EN" : "DIS",
1946 ((msr >> 0) & 0x7FFF) * rapl_power_units,
1947 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
1948 (((msr >> 16) & 1) ? "EN" : "DIS"));
1949
1950 return;
1951}
1952
1953int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1954{
1955 unsigned long long msr;
1956 int cpu;
Len Brown889facb2012-11-08 00:48:57 -05001957
1958 if (!do_rapl)
1959 return 0;
1960
1961 /* RAPL counters are per package, so print only for 1st thread/package */
1962 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1963 return 0;
1964
1965 cpu = t->cpu_id;
1966 if (cpu_migrate(cpu)) {
1967 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1968 return -1;
1969 }
1970
1971 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
1972 return -1;
1973
Len Brownd8af6f52015-02-10 01:56:38 -05001974 if (debug) {
Len Brown889facb2012-11-08 00:48:57 -05001975 fprintf(stderr, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx "
1976 "(%f Watts, %f Joules, %f sec.)\n", cpu, msr,
Len Brown144b44b2013-11-09 00:30:16 -05001977 rapl_power_units, rapl_energy_units, rapl_time_units);
Len Brown889facb2012-11-08 00:48:57 -05001978 }
Len Brown144b44b2013-11-09 00:30:16 -05001979 if (do_rapl & RAPL_PKG_POWER_INFO) {
1980
Len Brown889facb2012-11-08 00:48:57 -05001981 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
1982 return -5;
1983
1984
1985 fprintf(stderr, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
1986 cpu, msr,
1987 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1988 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1989 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1990 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
1991
Len Brown144b44b2013-11-09 00:30:16 -05001992 }
1993 if (do_rapl & RAPL_PKG) {
1994
Len Brown889facb2012-11-08 00:48:57 -05001995 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
1996 return -9;
1997
1998 fprintf(stderr, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
1999 cpu, msr, (msr >> 63) & 1 ? "": "UN");
2000
2001 print_power_limit_msr(cpu, msr, "PKG Limit #1");
2002 fprintf(stderr, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
2003 cpu,
2004 ((msr >> 47) & 1) ? "EN" : "DIS",
2005 ((msr >> 32) & 0x7FFF) * rapl_power_units,
2006 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
2007 ((msr >> 48) & 1) ? "EN" : "DIS");
2008 }
2009
2010 if (do_rapl & RAPL_DRAM) {
2011 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
2012 return -6;
2013
2014
2015 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
2016 cpu, msr,
2017 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2018 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2019 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2020 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
2021
2022
2023 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
2024 return -9;
2025 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
2026 cpu, msr, (msr >> 31) & 1 ? "": "UN");
2027
2028 print_power_limit_msr(cpu, msr, "DRAM Limit");
2029 }
Len Brown144b44b2013-11-09 00:30:16 -05002030 if (do_rapl & RAPL_CORE_POLICY) {
Len Brownd8af6f52015-02-10 01:56:38 -05002031 if (debug) {
Len Brown889facb2012-11-08 00:48:57 -05002032 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
2033 return -7;
2034
2035 fprintf(stderr, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown144b44b2013-11-09 00:30:16 -05002036 }
2037 }
2038 if (do_rapl & RAPL_CORES) {
Len Brownd8af6f52015-02-10 01:56:38 -05002039 if (debug) {
Len Brown889facb2012-11-08 00:48:57 -05002040
2041 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
2042 return -9;
2043 fprintf(stderr, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
2044 cpu, msr, (msr >> 31) & 1 ? "": "UN");
2045 print_power_limit_msr(cpu, msr, "Cores Limit");
2046 }
2047 }
2048 if (do_rapl & RAPL_GFX) {
Len Brownd8af6f52015-02-10 01:56:38 -05002049 if (debug) {
Len Brown889facb2012-11-08 00:48:57 -05002050 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
2051 return -8;
2052
2053 fprintf(stderr, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
2054
2055 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
2056 return -9;
2057 fprintf(stderr, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
2058 cpu, msr, (msr >> 31) & 1 ? "": "UN");
2059 print_power_limit_msr(cpu, msr, "GFX Limit");
2060 }
2061 }
2062 return 0;
2063}
2064
Len Brownd7899442015-01-23 00:12:33 -05002065/*
2066 * SNB adds support for additional MSRs:
2067 *
2068 * MSR_PKG_C7_RESIDENCY 0x000003fa
2069 * MSR_CORE_C7_RESIDENCY 0x000003fe
2070 * MSR_PKG_C2_RESIDENCY 0x0000060d
2071 */
Len Brown103a8fe2010-10-22 23:53:03 -04002072
Len Brownd7899442015-01-23 00:12:33 -05002073int has_snb_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04002074{
2075 if (!genuine_intel)
2076 return 0;
2077
2078 switch (model) {
2079 case 0x2A:
2080 case 0x2D:
Len Brown650a37f2012-06-03 23:34:44 -04002081 case 0x3A: /* IVB */
Len Brown13006512012-09-26 18:11:31 -04002082 case 0x3E: /* IVB Xeon */
Len Brown70b43402013-01-08 01:26:07 -05002083 case 0x3C: /* HSW */
2084 case 0x3F: /* HSW */
2085 case 0x45: /* HSW */
Len Brown149c2312013-03-15 10:58:02 -04002086 case 0x46: /* HSW */
Len Brown4e8e863f2014-02-27 23:28:53 -05002087 case 0x3D: /* BDW */
2088 case 0x4F: /* BDX */
2089 case 0x56: /* BDX-DE */
Len Brown103a8fe2010-10-22 23:53:03 -04002090 return 1;
2091 }
2092 return 0;
2093}
2094
Len Brownd7899442015-01-23 00:12:33 -05002095/*
2096 * HSW adds support for additional MSRs:
2097 *
2098 * MSR_PKG_C8_RESIDENCY 0x00000630
2099 * MSR_PKG_C9_RESIDENCY 0x00000631
2100 * MSR_PKG_C10_RESIDENCY 0x00000632
2101 */
2102int has_hsw_msrs(unsigned int family, unsigned int model)
Kristen Carlson Accardica587102012-11-21 05:22:43 -08002103{
2104 if (!genuine_intel)
2105 return 0;
2106
2107 switch (model) {
Len Brown4e8e863f2014-02-27 23:28:53 -05002108 case 0x45: /* HSW */
2109 case 0x3D: /* BDW */
Kristen Carlson Accardica587102012-11-21 05:22:43 -08002110 return 1;
2111 }
2112 return 0;
2113}
2114
2115
Len Brown144b44b2013-11-09 00:30:16 -05002116int is_slm(unsigned int family, unsigned int model)
2117{
2118 if (!genuine_intel)
2119 return 0;
2120 switch (model) {
2121 case 0x37: /* BYT */
2122 case 0x4D: /* AVN */
2123 return 1;
2124 }
2125 return 0;
2126}
2127
2128#define SLM_BCLK_FREQS 5
2129double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
2130
2131double slm_bclk(void)
2132{
2133 unsigned long long msr = 3;
2134 unsigned int i;
2135 double freq;
2136
2137 if (get_msr(0, MSR_FSB_FREQ, &msr))
2138 fprintf(stderr, "SLM BCLK: unknown\n");
2139
2140 i = msr & 0xf;
2141 if (i >= SLM_BCLK_FREQS) {
2142 fprintf(stderr, "SLM BCLK[%d] invalid\n", i);
2143 msr = 3;
2144 }
2145 freq = slm_freq_table[i];
2146
2147 fprintf(stderr, "SLM BCLK: %.1f Mhz\n", freq);
2148
2149 return freq;
2150}
2151
Len Brown103a8fe2010-10-22 23:53:03 -04002152double discover_bclk(unsigned int family, unsigned int model)
2153{
Len Brownd7899442015-01-23 00:12:33 -05002154 if (has_snb_msrs(family, model))
Len Brown103a8fe2010-10-22 23:53:03 -04002155 return 100.00;
Len Brown144b44b2013-11-09 00:30:16 -05002156 else if (is_slm(family, model))
2157 return slm_bclk();
Len Brown103a8fe2010-10-22 23:53:03 -04002158 else
2159 return 133.33;
2160}
2161
Len Brown889facb2012-11-08 00:48:57 -05002162/*
2163 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
2164 * the Thermal Control Circuit (TCC) activates.
2165 * This is usually equal to tjMax.
2166 *
2167 * Older processors do not have this MSR, so there we guess,
2168 * but also allow cmdline over-ride with -T.
2169 *
2170 * Several MSR temperature values are in units of degrees-C
2171 * below this value, including the Digital Thermal Sensor (DTS),
2172 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
2173 */
2174int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2175{
2176 unsigned long long msr;
2177 unsigned int target_c_local;
2178 int cpu;
2179
2180 /* tcc_activation_temp is used only for dts or ptm */
2181 if (!(do_dts || do_ptm))
2182 return 0;
2183
2184 /* this is a per-package concept */
2185 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2186 return 0;
2187
2188 cpu = t->cpu_id;
2189 if (cpu_migrate(cpu)) {
2190 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
2191 return -1;
2192 }
2193
2194 if (tcc_activation_temp_override != 0) {
2195 tcc_activation_temp = tcc_activation_temp_override;
2196 fprintf(stderr, "cpu%d: Using cmdline TCC Target (%d C)\n",
2197 cpu, tcc_activation_temp);
2198 return 0;
2199 }
2200
2201 /* Temperature Target MSR is Nehalem and newer only */
Len Brownd7899442015-01-23 00:12:33 -05002202 if (!do_nhm_platform_info)
Len Brown889facb2012-11-08 00:48:57 -05002203 goto guess;
2204
2205 if (get_msr(0, MSR_IA32_TEMPERATURE_TARGET, &msr))
2206 goto guess;
2207
Jean Delvare34821242014-05-01 11:40:19 +02002208 target_c_local = (msr >> 16) & 0xFF;
Len Brown889facb2012-11-08 00:48:57 -05002209
Len Brownd8af6f52015-02-10 01:56:38 -05002210 if (debug)
Len Brown889facb2012-11-08 00:48:57 -05002211 fprintf(stderr, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
2212 cpu, msr, target_c_local);
2213
Jean Delvare34821242014-05-01 11:40:19 +02002214 if (!target_c_local)
Len Brown889facb2012-11-08 00:48:57 -05002215 goto guess;
2216
2217 tcc_activation_temp = target_c_local;
2218
2219 return 0;
2220
2221guess:
2222 tcc_activation_temp = TJMAX_DEFAULT;
2223 fprintf(stderr, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
2224 cpu, tcc_activation_temp);
2225
2226 return 0;
2227}
Len Brown103a8fe2010-10-22 23:53:03 -04002228void check_cpuid()
2229{
2230 unsigned int eax, ebx, ecx, edx, max_level;
2231 unsigned int fms, family, model, stepping;
2232
2233 eax = ebx = ecx = edx = 0;
2234
Josh Triplett2b928652013-08-20 17:20:14 -07002235 __get_cpuid(0, &max_level, &ebx, &ecx, &edx);
Len Brown103a8fe2010-10-22 23:53:03 -04002236
2237 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
2238 genuine_intel = 1;
2239
Len Brownd8af6f52015-02-10 01:56:38 -05002240 if (debug)
Len Brown889facb2012-11-08 00:48:57 -05002241 fprintf(stderr, "CPUID(0): %.4s%.4s%.4s ",
Len Brown103a8fe2010-10-22 23:53:03 -04002242 (char *)&ebx, (char *)&edx, (char *)&ecx);
2243
Josh Triplett2b928652013-08-20 17:20:14 -07002244 __get_cpuid(1, &fms, &ebx, &ecx, &edx);
Len Brown103a8fe2010-10-22 23:53:03 -04002245 family = (fms >> 8) & 0xf;
2246 model = (fms >> 4) & 0xf;
2247 stepping = fms & 0xf;
2248 if (family == 6 || family == 0xf)
2249 model += ((fms >> 16) & 0xf) << 4;
2250
Len Brownd8af6f52015-02-10 01:56:38 -05002251 if (debug)
Len Brown103a8fe2010-10-22 23:53:03 -04002252 fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
2253 max_level, family, model, stepping, family, model, stepping);
2254
Josh Triplettb2c95d92013-08-20 17:20:18 -07002255 if (!(edx & (1 << 5)))
2256 errx(1, "CPUID: no MSR");
Len Brown103a8fe2010-10-22 23:53:03 -04002257
2258 /*
2259 * check max extended function levels of CPUID.
2260 * This is needed to check for invariant TSC.
2261 * This check is valid for both Intel and AMD.
2262 */
2263 ebx = ecx = edx = 0;
Josh Triplett2b928652013-08-20 17:20:14 -07002264 __get_cpuid(0x80000000, &max_level, &ebx, &ecx, &edx);
Len Brown103a8fe2010-10-22 23:53:03 -04002265
Len Brownd7899442015-01-23 00:12:33 -05002266 if (max_level >= 0x80000007) {
Len Brown103a8fe2010-10-22 23:53:03 -04002267
Len Brownd7899442015-01-23 00:12:33 -05002268 /*
2269 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
2270 * this check is valid for both Intel and AMD
2271 */
2272 __get_cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
2273 has_invariant_tsc = edx & (1 << 8);
2274 }
Len Brown103a8fe2010-10-22 23:53:03 -04002275
2276 /*
2277 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
2278 * this check is valid for both Intel and AMD
2279 */
2280
Josh Triplett2b928652013-08-20 17:20:14 -07002281 __get_cpuid(0x6, &eax, &ebx, &ecx, &edx);
Thomas Renninger8209e052011-01-21 15:11:19 +01002282 has_aperf = ecx & (1 << 0);
Len Brown889facb2012-11-08 00:48:57 -05002283 do_dts = eax & (1 << 0);
2284 do_ptm = eax & (1 << 6);
2285 has_epb = ecx & (1 << 3);
2286
Len Brownd8af6f52015-02-10 01:56:38 -05002287 if (debug)
Len Browna7296172015-01-23 01:33:58 -05002288 fprintf(stderr, "CPUID(6): %sAPERF, %sDTS, %sPTM, %sEPB\n",
2289 has_aperf ? "" : "No ",
2290 do_dts ? "" : "No ",
2291 do_ptm ? "" : "No ",
2292 has_epb ? "" : "No ");
Len Brown103a8fe2010-10-22 23:53:03 -04002293
Len Brownee7e38e2015-02-09 23:39:45 -05002294 do_nhm_platform_info = do_nhm_cstates = do_smi = probe_nhm_msrs(family, model);
Len Brownd7899442015-01-23 00:12:33 -05002295 do_snb_cstates = has_snb_msrs(family, model);
Len Brownee7e38e2015-02-09 23:39:45 -05002296 do_pc2 = do_snb_cstates && (pkg_cstate_limit >= PCL__2);
2297 do_pc3 = (pkg_cstate_limit >= PCL__3);
2298 do_pc6 = (pkg_cstate_limit >= PCL__6);
2299 do_pc7 = do_snb_cstates && (pkg_cstate_limit >= PCL__7);
Len Brownd7899442015-01-23 00:12:33 -05002300 do_c8_c9_c10 = has_hsw_msrs(family, model);
Len Brown144b44b2013-11-09 00:30:16 -05002301 do_slm_cstates = is_slm(family, model);
Len Brown103a8fe2010-10-22 23:53:03 -04002302 bclk = discover_bclk(family, model);
2303
Len Brownee7e38e2015-02-09 23:39:45 -05002304 do_nhm_turbo_ratio_limit = do_nhm_platform_info && has_nhm_turbo_ratio_limit(family, model);
Len Brown6574a5d2012-09-21 00:01:31 -04002305 do_ivt_turbo_ratio_limit = has_ivt_turbo_ratio_limit(family, model);
Len Brown889facb2012-11-08 00:48:57 -05002306 rapl_probe(family, model);
Len Brown3a9a9412014-08-15 02:39:52 -04002307 perf_limit_reasons_probe(family, model);
Len Brown889facb2012-11-08 00:48:57 -05002308
2309 return;
Len Brown103a8fe2010-10-22 23:53:03 -04002310}
2311
2312
Len Brownd8af6f52015-02-10 01:56:38 -05002313void help()
Len Brown103a8fe2010-10-22 23:53:03 -04002314{
Len Brownd8af6f52015-02-10 01:56:38 -05002315 fprintf(stderr,
2316 "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
2317 "\n"
2318 "Turbostat forks the specified COMMAND and prints statistics\n"
2319 "when COMMAND completes.\n"
2320 "If no COMMAND is specified, turbostat wakes every 5-seconds\n"
2321 "to print statistics, until interrupted.\n"
2322 "--debug run in \"debug\" mode\n"
2323 "--interval sec Override default 5-second measurement interval\n"
2324 "--help print this help message\n"
2325 "--counter msr print 32-bit counter at address \"msr\"\n"
2326 "--Counter msr print 64-bit Counter at address \"msr\"\n"
2327 "--msr msr print 32-bit value at address \"msr\"\n"
2328 "--MSR msr print 64-bit Value at address \"msr\"\n"
2329 "--version print version information\n"
2330 "\n"
2331 "For more help, run \"man turbostat\"\n");
Len Brown103a8fe2010-10-22 23:53:03 -04002332}
2333
2334
2335/*
2336 * in /dev/cpu/ return success for names that are numbers
2337 * ie. filter out ".", "..", "microcode".
2338 */
2339int dir_filter(const struct dirent *dirp)
2340{
2341 if (isdigit(dirp->d_name[0]))
2342 return 1;
2343 else
2344 return 0;
2345}
2346
2347int open_dev_cpu_msr(int dummy1)
2348{
2349 return 0;
2350}
2351
Len Brownc98d5d92012-06-04 00:56:40 -04002352void topology_probe()
2353{
2354 int i;
2355 int max_core_id = 0;
2356 int max_package_id = 0;
2357 int max_siblings = 0;
2358 struct cpu_topology {
2359 int core_id;
2360 int physical_package_id;
2361 } *cpus;
2362
2363 /* Initialize num_cpus, max_cpu_num */
2364 topo.num_cpus = 0;
2365 topo.max_cpu_num = 0;
2366 for_all_proc_cpus(count_cpus);
2367 if (!summary_only && topo.num_cpus > 1)
2368 show_cpu = 1;
2369
Len Brownd8af6f52015-02-10 01:56:38 -05002370 if (debug > 1)
Len Brownc98d5d92012-06-04 00:56:40 -04002371 fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
2372
2373 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
Josh Triplettb2c95d92013-08-20 17:20:18 -07002374 if (cpus == NULL)
2375 err(1, "calloc cpus");
Len Brownc98d5d92012-06-04 00:56:40 -04002376
2377 /*
2378 * Allocate and initialize cpu_present_set
2379 */
2380 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07002381 if (cpu_present_set == NULL)
2382 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04002383 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2384 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
2385 for_all_proc_cpus(mark_cpu_present);
2386
2387 /*
2388 * Allocate and initialize cpu_affinity_set
2389 */
2390 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07002391 if (cpu_affinity_set == NULL)
2392 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04002393 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2394 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
2395
2396
2397 /*
2398 * For online cpus
2399 * find max_core_id, max_package_id
2400 */
2401 for (i = 0; i <= topo.max_cpu_num; ++i) {
2402 int siblings;
2403
2404 if (cpu_is_not_present(i)) {
Len Brownd8af6f52015-02-10 01:56:38 -05002405 if (debug > 1)
Len Brownc98d5d92012-06-04 00:56:40 -04002406 fprintf(stderr, "cpu%d NOT PRESENT\n", i);
2407 continue;
2408 }
2409 cpus[i].core_id = get_core_id(i);
2410 if (cpus[i].core_id > max_core_id)
2411 max_core_id = cpus[i].core_id;
2412
2413 cpus[i].physical_package_id = get_physical_package_id(i);
2414 if (cpus[i].physical_package_id > max_package_id)
2415 max_package_id = cpus[i].physical_package_id;
2416
2417 siblings = get_num_ht_siblings(i);
2418 if (siblings > max_siblings)
2419 max_siblings = siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05002420 if (debug > 1)
Len Brownc98d5d92012-06-04 00:56:40 -04002421 fprintf(stderr, "cpu %d pkg %d core %d\n",
2422 i, cpus[i].physical_package_id, cpus[i].core_id);
2423 }
2424 topo.num_cores_per_pkg = max_core_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05002425 if (debug > 1)
Len Brownc98d5d92012-06-04 00:56:40 -04002426 fprintf(stderr, "max_core_id %d, sizing for %d cores per package\n",
2427 max_core_id, topo.num_cores_per_pkg);
2428 if (!summary_only && topo.num_cores_per_pkg > 1)
2429 show_core = 1;
2430
2431 topo.num_packages = max_package_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05002432 if (debug > 1)
Len Brownc98d5d92012-06-04 00:56:40 -04002433 fprintf(stderr, "max_package_id %d, sizing for %d packages\n",
2434 max_package_id, topo.num_packages);
2435 if (!summary_only && topo.num_packages > 1)
2436 show_pkg = 1;
2437
2438 topo.num_threads_per_core = max_siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05002439 if (debug > 1)
Len Brownc98d5d92012-06-04 00:56:40 -04002440 fprintf(stderr, "max_siblings %d\n", max_siblings);
2441
2442 free(cpus);
2443}
2444
2445void
2446allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
2447{
2448 int i;
2449
2450 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
2451 topo.num_packages, sizeof(struct thread_data));
2452 if (*t == NULL)
2453 goto error;
2454
2455 for (i = 0; i < topo.num_threads_per_core *
2456 topo.num_cores_per_pkg * topo.num_packages; i++)
2457 (*t)[i].cpu_id = -1;
2458
2459 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
2460 sizeof(struct core_data));
2461 if (*c == NULL)
2462 goto error;
2463
2464 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
2465 (*c)[i].core_id = -1;
2466
2467 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
2468 if (*p == NULL)
2469 goto error;
2470
2471 for (i = 0; i < topo.num_packages; i++)
2472 (*p)[i].package_id = i;
2473
2474 return;
2475error:
Josh Triplettb2c95d92013-08-20 17:20:18 -07002476 err(1, "calloc counters");
Len Brownc98d5d92012-06-04 00:56:40 -04002477}
2478/*
2479 * init_counter()
2480 *
2481 * set cpu_id, core_num, pkg_num
2482 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
2483 *
2484 * increment topo.num_cores when 1st core in pkg seen
2485 */
2486void init_counter(struct thread_data *thread_base, struct core_data *core_base,
2487 struct pkg_data *pkg_base, int thread_num, int core_num,
2488 int pkg_num, int cpu_id)
2489{
2490 struct thread_data *t;
2491 struct core_data *c;
2492 struct pkg_data *p;
2493
2494 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
2495 c = GET_CORE(core_base, core_num, pkg_num);
2496 p = GET_PKG(pkg_base, pkg_num);
2497
2498 t->cpu_id = cpu_id;
2499 if (thread_num == 0) {
2500 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
2501 if (cpu_is_first_core_in_package(cpu_id))
2502 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
2503 }
2504
2505 c->core_id = core_num;
2506 p->package_id = pkg_num;
2507}
2508
2509
2510int initialize_counters(int cpu_id)
2511{
2512 int my_thread_id, my_core_id, my_package_id;
2513
2514 my_package_id = get_physical_package_id(cpu_id);
2515 my_core_id = get_core_id(cpu_id);
2516
2517 if (cpu_is_first_sibling_in_core(cpu_id)) {
2518 my_thread_id = 0;
2519 topo.num_cores++;
2520 } else {
2521 my_thread_id = 1;
2522 }
2523
2524 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2525 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2526 return 0;
2527}
2528
2529void allocate_output_buffer()
2530{
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02002531 output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
Len Brownc98d5d92012-06-04 00:56:40 -04002532 outp = output_buffer;
Josh Triplettb2c95d92013-08-20 17:20:18 -07002533 if (outp == NULL)
2534 err(-1, "calloc output buffer");
Len Brownc98d5d92012-06-04 00:56:40 -04002535}
2536
2537void setup_all_buffers(void)
2538{
2539 topology_probe();
2540 allocate_counters(&thread_even, &core_even, &package_even);
2541 allocate_counters(&thread_odd, &core_odd, &package_odd);
2542 allocate_output_buffer();
2543 for_all_proc_cpus(initialize_counters);
2544}
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02002545
Len Brown103a8fe2010-10-22 23:53:03 -04002546void turbostat_init()
2547{
Len Brown103a8fe2010-10-22 23:53:03 -04002548 check_dev_msr();
Len Brown98481e72014-08-15 00:36:50 -04002549 check_permissions();
2550 check_cpuid();
Len Brown103a8fe2010-10-22 23:53:03 -04002551
Len Brownc98d5d92012-06-04 00:56:40 -04002552 setup_all_buffers();
Len Brown103a8fe2010-10-22 23:53:03 -04002553
Len Brownd8af6f52015-02-10 01:56:38 -05002554 if (debug)
Len Brownc98d5d92012-06-04 00:56:40 -04002555 print_verbose_header();
Len Brown889facb2012-11-08 00:48:57 -05002556
Len Brownd8af6f52015-02-10 01:56:38 -05002557 if (debug)
Len Brown889facb2012-11-08 00:48:57 -05002558 for_all_cpus(print_epb, ODD_COUNTERS);
2559
Len Brownd8af6f52015-02-10 01:56:38 -05002560 if (debug)
Len Brown3a9a9412014-08-15 02:39:52 -04002561 for_all_cpus(print_perf_limit, ODD_COUNTERS);
2562
Len Brownd8af6f52015-02-10 01:56:38 -05002563 if (debug)
Len Brown889facb2012-11-08 00:48:57 -05002564 for_all_cpus(print_rapl, ODD_COUNTERS);
2565
2566 for_all_cpus(set_temperature_target, ODD_COUNTERS);
2567
Len Brownd8af6f52015-02-10 01:56:38 -05002568 if (debug)
Len Brown889facb2012-11-08 00:48:57 -05002569 for_all_cpus(print_thermal, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04002570}
2571
2572int fork_it(char **argv)
2573{
Len Brown103a8fe2010-10-22 23:53:03 -04002574 pid_t child_pid;
Len Brownd91bb172012-11-01 00:08:19 -04002575 int status;
Len Brownd15cf7c2012-06-03 23:24:00 -04002576
Len Brownd91bb172012-11-01 00:08:19 -04002577 status = for_all_cpus(get_counters, EVEN_COUNTERS);
2578 if (status)
2579 exit(status);
Len Brownc98d5d92012-06-04 00:56:40 -04002580 /* clear affinity side-effect of get_counters() */
2581 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
Len Brown103a8fe2010-10-22 23:53:03 -04002582 gettimeofday(&tv_even, (struct timezone *)NULL);
2583
2584 child_pid = fork();
2585 if (!child_pid) {
2586 /* child */
2587 execvp(argv[0], argv);
2588 } else {
Len Brown103a8fe2010-10-22 23:53:03 -04002589
2590 /* parent */
Josh Triplettb2c95d92013-08-20 17:20:18 -07002591 if (child_pid == -1)
2592 err(1, "fork");
Len Brown103a8fe2010-10-22 23:53:03 -04002593
2594 signal(SIGINT, SIG_IGN);
2595 signal(SIGQUIT, SIG_IGN);
Josh Triplettb2c95d92013-08-20 17:20:18 -07002596 if (waitpid(child_pid, &status, 0) == -1)
2597 err(status, "waitpid");
Len Brown103a8fe2010-10-22 23:53:03 -04002598 }
Len Brownc98d5d92012-06-04 00:56:40 -04002599 /*
2600 * n.b. fork_it() does not check for errors from for_all_cpus()
2601 * because re-starting is problematic when forking
2602 */
2603 for_all_cpus(get_counters, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04002604 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002605 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04002606 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
2607 compute_average(EVEN_COUNTERS);
2608 format_all_counters(EVEN_COUNTERS);
2609 flush_stderr();
Len Brown103a8fe2010-10-22 23:53:03 -04002610
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07002611 fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
Len Brown103a8fe2010-10-22 23:53:03 -04002612
Len Brownd91bb172012-11-01 00:08:19 -04002613 return status;
Len Brown103a8fe2010-10-22 23:53:03 -04002614}
2615
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02002616int get_and_dump_counters(void)
2617{
2618 int status;
2619
2620 status = for_all_cpus(get_counters, ODD_COUNTERS);
2621 if (status)
2622 return status;
2623
2624 status = for_all_cpus(dump_counters, ODD_COUNTERS);
2625 if (status)
2626 return status;
2627
2628 flush_stdout();
2629
2630 return status;
2631}
2632
Len Brownd8af6f52015-02-10 01:56:38 -05002633void print_version() {
2634 fprintf(stderr, "turbostat version 4.0 10-Feb, 2015"
2635 " - Len Brown <lenb@kernel.org>\n");
2636}
2637
Len Brown103a8fe2010-10-22 23:53:03 -04002638void cmdline(int argc, char **argv)
2639{
2640 int opt;
Len Brownd8af6f52015-02-10 01:56:38 -05002641 int option_index = 0;
2642 static struct option long_options[] = {
2643 {"Counter", required_argument, 0, 'C'},
2644 {"counter", required_argument, 0, 'c'},
2645 {"Dump", no_argument, 0, 'D'},
2646 {"debug", no_argument, 0, 'd'},
2647 {"interval", required_argument, 0, 'i'},
2648 {"help", no_argument, 0, 'h'},
2649 {"Joules", no_argument, 0, 'J'},
2650 {"MSR", required_argument, 0, 'M'},
2651 {"msr", required_argument, 0, 'm'},
2652 {"Package", no_argument, 0, 'p'},
2653 {"processor", no_argument, 0, 'p'},
2654 {"Summary", no_argument, 0, 'S'},
2655 {"TCC", required_argument, 0, 'T'},
2656 {"version", no_argument, 0, 'v' },
2657 {0, 0, 0, 0 }
2658 };
Len Brown103a8fe2010-10-22 23:53:03 -04002659
2660 progname = argv[0];
2661
Len Brownd8af6f52015-02-10 01:56:38 -05002662 while ((opt = getopt_long_only(argc, argv, "C:c:Ddhi:JM:m:PpST:v",
2663 long_options, &option_index)) != -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04002664 switch (opt) {
Len Brownd8af6f52015-02-10 01:56:38 -05002665 case 'C':
2666 sscanf(optarg, "%x", &extra_delta_offset64);
Len Brown103a8fe2010-10-22 23:53:03 -04002667 break;
Len Brownf9240812012-10-06 15:26:31 -04002668 case 'c':
Len Brown8e180f32012-09-22 01:25:08 -04002669 sscanf(optarg, "%x", &extra_delta_offset32);
2670 break;
Len Brownd8af6f52015-02-10 01:56:38 -05002671 case 'D':
2672 dump_only++;
Len Brown8e180f32012-09-22 01:25:08 -04002673 break;
Len Brownd8af6f52015-02-10 01:56:38 -05002674 case 'd':
2675 debug++;
Len Brown2f32edf2012-09-21 23:45:46 -04002676 break;
Len Brownd8af6f52015-02-10 01:56:38 -05002677 case 'h':
2678 default:
2679 help();
2680 exit(1);
2681 case 'i':
2682 interval_sec = atoi(optarg);
Len Brown889facb2012-11-08 00:48:57 -05002683 break;
Dirk Brandewie5c56be92013-12-16 10:23:41 -08002684 case 'J':
2685 rapl_joules++;
2686 break;
Len Brownd8af6f52015-02-10 01:56:38 -05002687 case 'M':
2688 sscanf(optarg, "%x", &extra_msr_offset64);
2689 break;
2690 case 'm':
2691 sscanf(optarg, "%x", &extra_msr_offset32);
2692 break;
2693 case 'P':
2694 show_pkg_only++;
2695 break;
2696 case 'p':
2697 show_core_only++;
2698 break;
2699 case 'S':
2700 summary_only++;
2701 break;
2702 case 'T':
2703 tcc_activation_temp_override = atoi(optarg);
2704 break;
2705 case 'v':
2706 print_version();
2707 exit(0);
2708 break;
Len Brown103a8fe2010-10-22 23:53:03 -04002709 }
2710 }
2711}
2712
2713int main(int argc, char **argv)
2714{
2715 cmdline(argc, argv);
2716
Len Brownd8af6f52015-02-10 01:56:38 -05002717 if (debug)
2718 print_version();
Len Brown103a8fe2010-10-22 23:53:03 -04002719
2720 turbostat_init();
2721
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02002722 /* dump counters and exit */
2723 if (dump_only)
2724 return get_and_dump_counters();
2725
Len Brown103a8fe2010-10-22 23:53:03 -04002726 /*
2727 * if any params left, it must be a command to fork
2728 */
2729 if (argc - optind)
2730 return fork_it(argv + optind);
2731 else
2732 turbostat_loop();
2733
2734 return 0;
2735}