blob: 6f29fc11fde6593fc968d31e010d6df1c2808037 [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>
36#include <dirent.h>
37#include <string.h>
38#include <ctype.h>
Len Brown88c32812012-03-29 21:44:40 -040039#include <sched.h>
Josh Triplett2b928652013-08-20 17:20:14 -070040#include <cpuid.h>
Len Brown98481e72014-08-15 00:36:50 -040041#include <linux/capability.h>
42#include <errno.h>
Len Brown103a8fe2010-10-22 23:53:03 -040043
Len Brown103a8fe2010-10-22 23:53:03 -040044char *proc_stat = "/proc/stat";
45unsigned int interval_sec = 5; /* set with -i interval_sec */
46unsigned int verbose; /* set with -v */
Len Brown889facb2012-11-08 00:48:57 -050047unsigned int rapl_verbose; /* set with -R */
Dirk Brandewie5c56be92013-12-16 10:23:41 -080048unsigned int rapl_joules; /* set with -J */
Len Brown889facb2012-11-08 00:48:57 -050049unsigned int thermal_verbose; /* set with -T */
Andy Shevchenkof591c382014-01-23 17:13:14 +020050unsigned int summary_only; /* set with -S */
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +020051unsigned int dump_only; /* set with -s */
Len Brown103a8fe2010-10-22 23:53:03 -040052unsigned int skip_c0;
53unsigned int skip_c1;
54unsigned int do_nhm_cstates;
55unsigned int do_snb_cstates;
Kristen Carlson Accardica587102012-11-21 05:22:43 -080056unsigned int do_c8_c9_c10;
Len Brown144b44b2013-11-09 00:30:16 -050057unsigned int do_slm_cstates;
58unsigned int use_c1_residency_msr;
Len Brown103a8fe2010-10-22 23:53:03 -040059unsigned int has_aperf;
Len Brown889facb2012-11-08 00:48:57 -050060unsigned int has_epb;
Len Brownfc04cc62014-02-06 00:55:19 -050061unsigned int units = 1000000; /* MHz etc */
Len Brown103a8fe2010-10-22 23:53:03 -040062unsigned int genuine_intel;
63unsigned int has_invariant_tsc;
64unsigned int do_nehalem_platform_info;
65unsigned int do_nehalem_turbo_ratio_limit;
Len Brown6574a5d2012-09-21 00:01:31 -040066unsigned int do_ivt_turbo_ratio_limit;
Len Brown2f32edf2012-09-21 23:45:46 -040067unsigned int extra_msr_offset32;
68unsigned int extra_msr_offset64;
Len Brown8e180f32012-09-22 01:25:08 -040069unsigned int extra_delta_offset32;
70unsigned int extra_delta_offset64;
Len Brown1ed51012013-02-10 17:19:24 -050071int do_smi;
Len Brown103a8fe2010-10-22 23:53:03 -040072double bclk;
73unsigned int show_pkg;
74unsigned int show_core;
75unsigned int show_cpu;
Len Brownc98d5d92012-06-04 00:56:40 -040076unsigned int show_pkg_only;
77unsigned int show_core_only;
78char *output_buffer, *outp;
Len Brown889facb2012-11-08 00:48:57 -050079unsigned int do_rapl;
80unsigned int do_dts;
81unsigned int do_ptm;
82unsigned int tcc_activation_temp;
83unsigned int tcc_activation_temp_override;
84double rapl_power_units, rapl_energy_units, rapl_time_units;
85double rapl_joule_counter_range;
86
Len Browne6f9bb32013-12-03 02:19:19 -050087#define RAPL_PKG (1 << 0)
88 /* 0x610 MSR_PKG_POWER_LIMIT */
89 /* 0x611 MSR_PKG_ENERGY_STATUS */
90#define RAPL_PKG_PERF_STATUS (1 << 1)
91 /* 0x613 MSR_PKG_PERF_STATUS */
92#define RAPL_PKG_POWER_INFO (1 << 2)
93 /* 0x614 MSR_PKG_POWER_INFO */
94
95#define RAPL_DRAM (1 << 3)
96 /* 0x618 MSR_DRAM_POWER_LIMIT */
97 /* 0x619 MSR_DRAM_ENERGY_STATUS */
98 /* 0x61c MSR_DRAM_POWER_INFO */
99#define RAPL_DRAM_PERF_STATUS (1 << 4)
100 /* 0x61b MSR_DRAM_PERF_STATUS */
101
102#define RAPL_CORES (1 << 5)
103 /* 0x638 MSR_PP0_POWER_LIMIT */
104 /* 0x639 MSR_PP0_ENERGY_STATUS */
105#define RAPL_CORE_POLICY (1 << 6)
106 /* 0x63a MSR_PP0_POLICY */
107
108
109#define RAPL_GFX (1 << 7)
110 /* 0x640 MSR_PP1_POWER_LIMIT */
111 /* 0x641 MSR_PP1_ENERGY_STATUS */
112 /* 0x642 MSR_PP1_POLICY */
Len Brown889facb2012-11-08 00:48:57 -0500113#define TJMAX_DEFAULT 100
114
115#define MAX(a, b) ((a) > (b) ? (a) : (b))
Len Brown103a8fe2010-10-22 23:53:03 -0400116
117int aperf_mperf_unstable;
118int backwards_count;
119char *progname;
Len Brown103a8fe2010-10-22 23:53:03 -0400120
Len Brownc98d5d92012-06-04 00:56:40 -0400121cpu_set_t *cpu_present_set, *cpu_affinity_set;
122size_t cpu_present_setsize, cpu_affinity_setsize;
Len Brown103a8fe2010-10-22 23:53:03 -0400123
Len Brownc98d5d92012-06-04 00:56:40 -0400124struct thread_data {
125 unsigned long long tsc;
126 unsigned long long aperf;
127 unsigned long long mperf;
Len Brown144b44b2013-11-09 00:30:16 -0500128 unsigned long long c1;
Len Brown2f32edf2012-09-21 23:45:46 -0400129 unsigned long long extra_msr64;
Len Brown8e180f32012-09-22 01:25:08 -0400130 unsigned long long extra_delta64;
131 unsigned long long extra_msr32;
132 unsigned long long extra_delta32;
Len Brown1ed51012013-02-10 17:19:24 -0500133 unsigned int smi_count;
Len Brownc98d5d92012-06-04 00:56:40 -0400134 unsigned int cpu_id;
135 unsigned int flags;
136#define CPU_IS_FIRST_THREAD_IN_CORE 0x2
137#define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
138} *thread_even, *thread_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400139
Len Brownc98d5d92012-06-04 00:56:40 -0400140struct core_data {
141 unsigned long long c3;
142 unsigned long long c6;
143 unsigned long long c7;
Len Brown889facb2012-11-08 00:48:57 -0500144 unsigned int core_temp_c;
Len Brownc98d5d92012-06-04 00:56:40 -0400145 unsigned int core_id;
146} *core_even, *core_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400147
Len Brownc98d5d92012-06-04 00:56:40 -0400148struct pkg_data {
149 unsigned long long pc2;
150 unsigned long long pc3;
151 unsigned long long pc6;
152 unsigned long long pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800153 unsigned long long pc8;
154 unsigned long long pc9;
155 unsigned long long pc10;
Len Brownc98d5d92012-06-04 00:56:40 -0400156 unsigned int package_id;
Len Brown889facb2012-11-08 00:48:57 -0500157 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
158 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
159 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
160 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
161 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
162 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
163 unsigned int pkg_temp_c;
164
Len Brownc98d5d92012-06-04 00:56:40 -0400165} *package_even, *package_odd;
166
167#define ODD_COUNTERS thread_odd, core_odd, package_odd
168#define EVEN_COUNTERS thread_even, core_even, package_even
169
170#define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
171 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
172 topo.num_threads_per_core + \
173 (core_no) * topo.num_threads_per_core + (thread_no))
174#define GET_CORE(core_base, core_no, pkg_no) \
175 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
176#define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
177
178struct system_summary {
179 struct thread_data threads;
180 struct core_data cores;
181 struct pkg_data packages;
182} sum, average;
183
184
185struct topo_params {
186 int num_packages;
187 int num_cpus;
188 int num_cores;
189 int max_cpu_num;
190 int num_cores_per_pkg;
191 int num_threads_per_core;
192} topo;
193
194struct timeval tv_even, tv_odd, tv_delta;
195
196void setup_all_buffers(void);
197
198int cpu_is_not_present(int cpu)
Len Brownd15cf7c2012-06-03 23:24:00 -0400199{
Len Brownc98d5d92012-06-04 00:56:40 -0400200 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brownd15cf7c2012-06-03 23:24:00 -0400201}
Len Brown88c32812012-03-29 21:44:40 -0400202/*
Len Brownc98d5d92012-06-04 00:56:40 -0400203 * run func(thread, core, package) in topology order
204 * skip non-present cpus
Len Brown88c32812012-03-29 21:44:40 -0400205 */
Len Brownd15cf7c2012-06-03 23:24:00 -0400206
Len Brownc98d5d92012-06-04 00:56:40 -0400207int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
208 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
Len Brown88c32812012-03-29 21:44:40 -0400209{
Len Brownc98d5d92012-06-04 00:56:40 -0400210 int retval, pkg_no, core_no, thread_no;
211
212 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
213 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
214 for (thread_no = 0; thread_no <
215 topo.num_threads_per_core; ++thread_no) {
216 struct thread_data *t;
217 struct core_data *c;
218 struct pkg_data *p;
219
220 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
221
222 if (cpu_is_not_present(t->cpu_id))
223 continue;
224
225 c = GET_CORE(core_base, core_no, pkg_no);
226 p = GET_PKG(pkg_base, pkg_no);
227
228 retval = func(t, c, p);
229 if (retval)
230 return retval;
231 }
232 }
233 }
234 return 0;
Len Brown88c32812012-03-29 21:44:40 -0400235}
236
237int cpu_migrate(int cpu)
238{
Len Brownc98d5d92012-06-04 00:56:40 -0400239 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
240 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
241 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
Len Brown88c32812012-03-29 21:44:40 -0400242 return -1;
243 else
244 return 0;
245}
246
Len Brown15aaa342012-03-29 22:19:58 -0400247int get_msr(int cpu, off_t offset, unsigned long long *msr)
Len Brown103a8fe2010-10-22 23:53:03 -0400248{
249 ssize_t retval;
Len Brown103a8fe2010-10-22 23:53:03 -0400250 char pathname[32];
251 int fd;
252
253 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
254 fd = open(pathname, O_RDONLY);
Len Brown15aaa342012-03-29 22:19:58 -0400255 if (fd < 0)
Len Brown98481e72014-08-15 00:36:50 -0400256 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 -0400257
Len Brown15aaa342012-03-29 22:19:58 -0400258 retval = pread(fd, msr, sizeof *msr, offset);
Len Brown103a8fe2010-10-22 23:53:03 -0400259 close(fd);
Len Brown15aaa342012-03-29 22:19:58 -0400260
Len Brown98481e72014-08-15 00:36:50 -0400261 if (retval != sizeof *msr)
262 err(-1, "%s offset 0x%llx read failed", pathname, (unsigned long long)offset);
Len Brown15aaa342012-03-29 22:19:58 -0400263
264 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400265}
266
Len Brownfc04cc62014-02-06 00:55:19 -0500267/*
268 * Example Format w/ field column widths:
269 *
Len Browne7c95ff2014-08-14 21:22:13 -0400270 * 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
271 * 123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678
Len Brownfc04cc62014-02-06 00:55:19 -0500272 */
273
Len Browna829eb42011-02-10 23:36:34 -0500274void print_header(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400275{
276 if (show_pkg)
Len Browne7c95ff2014-08-14 21:22:13 -0400277 outp += sprintf(outp, " Package");
Len Brown103a8fe2010-10-22 23:53:03 -0400278 if (show_core)
Len Browne7c95ff2014-08-14 21:22:13 -0400279 outp += sprintf(outp, " Core");
Len Brown103a8fe2010-10-22 23:53:03 -0400280 if (show_cpu)
Len Browne7c95ff2014-08-14 21:22:13 -0400281 outp += sprintf(outp, " CPU");
Len Brown103a8fe2010-10-22 23:53:03 -0400282 if (has_aperf)
Len Browne7c95ff2014-08-14 21:22:13 -0400283 outp += sprintf(outp, " Avg_MHz");
Len Brownfc04cc62014-02-06 00:55:19 -0500284 if (do_nhm_cstates)
Len Browne7c95ff2014-08-14 21:22:13 -0400285 outp += sprintf(outp, " %%Busy");
Len Brownfc04cc62014-02-06 00:55:19 -0500286 if (has_aperf)
Len Browne7c95ff2014-08-14 21:22:13 -0400287 outp += sprintf(outp, " Bzy_MHz");
288 outp += sprintf(outp, " TSC_MHz");
Len Brown1ed51012013-02-10 17:19:24 -0500289 if (do_smi)
Len Browne7c95ff2014-08-14 21:22:13 -0400290 outp += sprintf(outp, " SMI");
Len Brown8e180f32012-09-22 01:25:08 -0400291 if (extra_delta_offset32)
Len Browne7c95ff2014-08-14 21:22:13 -0400292 outp += sprintf(outp, " count 0x%03X", extra_delta_offset32);
Len Brown8e180f32012-09-22 01:25:08 -0400293 if (extra_delta_offset64)
Len Browne7c95ff2014-08-14 21:22:13 -0400294 outp += sprintf(outp, " COUNT 0x%03X", extra_delta_offset64);
Len Brown2f32edf2012-09-21 23:45:46 -0400295 if (extra_msr_offset32)
Len Browne7c95ff2014-08-14 21:22:13 -0400296 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset32);
Len Brown2f32edf2012-09-21 23:45:46 -0400297 if (extra_msr_offset64)
Len Browne7c95ff2014-08-14 21:22:13 -0400298 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset64);
Len Brown103a8fe2010-10-22 23:53:03 -0400299 if (do_nhm_cstates)
Len Browne7c95ff2014-08-14 21:22:13 -0400300 outp += sprintf(outp, " CPU%%c1");
Len Brown144b44b2013-11-09 00:30:16 -0500301 if (do_nhm_cstates && !do_slm_cstates)
Len Browne7c95ff2014-08-14 21:22:13 -0400302 outp += sprintf(outp, " CPU%%c3");
Len Brown103a8fe2010-10-22 23:53:03 -0400303 if (do_nhm_cstates)
Len Browne7c95ff2014-08-14 21:22:13 -0400304 outp += sprintf(outp, " CPU%%c6");
Len Brown103a8fe2010-10-22 23:53:03 -0400305 if (do_snb_cstates)
Len Browne7c95ff2014-08-14 21:22:13 -0400306 outp += sprintf(outp, " CPU%%c7");
Len Brown889facb2012-11-08 00:48:57 -0500307
308 if (do_dts)
Len Browne7c95ff2014-08-14 21:22:13 -0400309 outp += sprintf(outp, " CoreTmp");
Len Brown889facb2012-11-08 00:48:57 -0500310 if (do_ptm)
Len Browne7c95ff2014-08-14 21:22:13 -0400311 outp += sprintf(outp, " PkgTmp");
Len Brown889facb2012-11-08 00:48:57 -0500312
Len Brown103a8fe2010-10-22 23:53:03 -0400313 if (do_snb_cstates)
Len Browne7c95ff2014-08-14 21:22:13 -0400314 outp += sprintf(outp, " Pkg%%pc2");
Len Brown144b44b2013-11-09 00:30:16 -0500315 if (do_nhm_cstates && !do_slm_cstates)
Len Browne7c95ff2014-08-14 21:22:13 -0400316 outp += sprintf(outp, " Pkg%%pc3");
Len Brown144b44b2013-11-09 00:30:16 -0500317 if (do_nhm_cstates && !do_slm_cstates)
Len Browne7c95ff2014-08-14 21:22:13 -0400318 outp += sprintf(outp, " Pkg%%pc6");
Len Brown103a8fe2010-10-22 23:53:03 -0400319 if (do_snb_cstates)
Len Browne7c95ff2014-08-14 21:22:13 -0400320 outp += sprintf(outp, " Pkg%%pc7");
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800321 if (do_c8_c9_c10) {
Len Browne7c95ff2014-08-14 21:22:13 -0400322 outp += sprintf(outp, " Pkg%%pc8");
323 outp += sprintf(outp, " Pkg%%pc9");
324 outp += sprintf(outp, " Pk%%pc10");
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800325 }
Len Brown103a8fe2010-10-22 23:53:03 -0400326
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800327 if (do_rapl && !rapl_joules) {
328 if (do_rapl & RAPL_PKG)
Len Browne7c95ff2014-08-14 21:22:13 -0400329 outp += sprintf(outp, " PkgWatt");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800330 if (do_rapl & RAPL_CORES)
Len Browne7c95ff2014-08-14 21:22:13 -0400331 outp += sprintf(outp, " CorWatt");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800332 if (do_rapl & RAPL_GFX)
Len Browne7c95ff2014-08-14 21:22:13 -0400333 outp += sprintf(outp, " GFXWatt");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800334 if (do_rapl & RAPL_DRAM)
Len Browne7c95ff2014-08-14 21:22:13 -0400335 outp += sprintf(outp, " RAMWatt");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800336 if (do_rapl & RAPL_PKG_PERF_STATUS)
Len Browne7c95ff2014-08-14 21:22:13 -0400337 outp += sprintf(outp, " PKG_%%");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800338 if (do_rapl & RAPL_DRAM_PERF_STATUS)
Len Browne7c95ff2014-08-14 21:22:13 -0400339 outp += sprintf(outp, " RAM_%%");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800340 } else {
341 if (do_rapl & RAPL_PKG)
Len Browne7c95ff2014-08-14 21:22:13 -0400342 outp += sprintf(outp, " Pkg_J");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800343 if (do_rapl & RAPL_CORES)
Len Browne7c95ff2014-08-14 21:22:13 -0400344 outp += sprintf(outp, " Cor_J");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800345 if (do_rapl & RAPL_GFX)
Len Browne7c95ff2014-08-14 21:22:13 -0400346 outp += sprintf(outp, " GFX_J");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800347 if (do_rapl & RAPL_DRAM)
Len Browne7c95ff2014-08-14 21:22:13 -0400348 outp += sprintf(outp, " RAM_W");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800349 if (do_rapl & RAPL_PKG_PERF_STATUS)
Len Browne7c95ff2014-08-14 21:22:13 -0400350 outp += sprintf(outp, " PKG_%%");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800351 if (do_rapl & RAPL_DRAM_PERF_STATUS)
Len Browne7c95ff2014-08-14 21:22:13 -0400352 outp += sprintf(outp, " RAM_%%");
353 outp += sprintf(outp, " time");
Len Brown889facb2012-11-08 00:48:57 -0500354
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800355 }
Len Brownc98d5d92012-06-04 00:56:40 -0400356 outp += sprintf(outp, "\n");
Len Brown103a8fe2010-10-22 23:53:03 -0400357}
358
Len Brownc98d5d92012-06-04 00:56:40 -0400359int dump_counters(struct thread_data *t, struct core_data *c,
360 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400361{
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200362 outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400363
Len Brownc98d5d92012-06-04 00:56:40 -0400364 if (t) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200365 outp += sprintf(outp, "CPU: %d flags 0x%x\n",
366 t->cpu_id, t->flags);
367 outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
368 outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
369 outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
370 outp += sprintf(outp, "c1: %016llX\n", t->c1);
371 outp += sprintf(outp, "msr0x%x: %08llX\n",
Len Brown8e180f32012-09-22 01:25:08 -0400372 extra_delta_offset32, t->extra_delta32);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200373 outp += sprintf(outp, "msr0x%x: %016llX\n",
Len Brown8e180f32012-09-22 01:25:08 -0400374 extra_delta_offset64, t->extra_delta64);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200375 outp += sprintf(outp, "msr0x%x: %08llX\n",
Len Brown2f32edf2012-09-21 23:45:46 -0400376 extra_msr_offset32, t->extra_msr32);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200377 outp += sprintf(outp, "msr0x%x: %016llX\n",
Len Brown2f32edf2012-09-21 23:45:46 -0400378 extra_msr_offset64, t->extra_msr64);
Len Brown1ed51012013-02-10 17:19:24 -0500379 if (do_smi)
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200380 outp += sprintf(outp, "SMI: %08X\n", t->smi_count);
Len Brownc98d5d92012-06-04 00:56:40 -0400381 }
Len Brown103a8fe2010-10-22 23:53:03 -0400382
Len Brownc98d5d92012-06-04 00:56:40 -0400383 if (c) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200384 outp += sprintf(outp, "core: %d\n", c->core_id);
385 outp += sprintf(outp, "c3: %016llX\n", c->c3);
386 outp += sprintf(outp, "c6: %016llX\n", c->c6);
387 outp += sprintf(outp, "c7: %016llX\n", c->c7);
388 outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
Len Brownc98d5d92012-06-04 00:56:40 -0400389 }
390
391 if (p) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200392 outp += sprintf(outp, "package: %d\n", p->package_id);
393 outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
394 outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
395 outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
396 outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
397 outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
398 outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
399 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
400 outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
401 outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
402 outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
403 outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
404 outp += sprintf(outp, "Throttle PKG: %0X\n",
405 p->rapl_pkg_perf_status);
406 outp += sprintf(outp, "Throttle RAM: %0X\n",
407 p->rapl_dram_perf_status);
408 outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
Len Brownc98d5d92012-06-04 00:56:40 -0400409 }
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200410
411 outp += sprintf(outp, "\n");
412
Len Brownc98d5d92012-06-04 00:56:40 -0400413 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400414}
415
Len Browne23da032012-02-06 18:37:16 -0500416/*
417 * column formatting convention & formats
Len Browne23da032012-02-06 18:37:16 -0500418 */
Len Brownc98d5d92012-06-04 00:56:40 -0400419int format_counters(struct thread_data *t, struct core_data *c,
420 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400421{
422 double interval_float;
Len Brownfc04cc62014-02-06 00:55:19 -0500423 char *fmt8;
Len Brown103a8fe2010-10-22 23:53:03 -0400424
Len Brownc98d5d92012-06-04 00:56:40 -0400425 /* if showing only 1st thread in core and this isn't one, bail out */
426 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
427 return 0;
428
429 /* if showing only 1st thread in pkg and this isn't one, bail out */
430 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
431 return 0;
432
Len Brown103a8fe2010-10-22 23:53:03 -0400433 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
434
Len Brownc98d5d92012-06-04 00:56:40 -0400435 /* topo columns, print blanks on 1st (average) line */
436 if (t == &average.threads) {
Len Brown103a8fe2010-10-22 23:53:03 -0400437 if (show_pkg)
Len Brownfc04cc62014-02-06 00:55:19 -0500438 outp += sprintf(outp, " -");
Len Brown103a8fe2010-10-22 23:53:03 -0400439 if (show_core)
Len Brownfc04cc62014-02-06 00:55:19 -0500440 outp += sprintf(outp, " -");
Len Brown103a8fe2010-10-22 23:53:03 -0400441 if (show_cpu)
Len Brownfc04cc62014-02-06 00:55:19 -0500442 outp += sprintf(outp, " -");
Len Brown103a8fe2010-10-22 23:53:03 -0400443 } else {
Len Brownc98d5d92012-06-04 00:56:40 -0400444 if (show_pkg) {
445 if (p)
Len Brownfc04cc62014-02-06 00:55:19 -0500446 outp += sprintf(outp, "%8d", p->package_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400447 else
Len Brownfc04cc62014-02-06 00:55:19 -0500448 outp += sprintf(outp, " -");
Len Brownc98d5d92012-06-04 00:56:40 -0400449 }
Len Brownc98d5d92012-06-04 00:56:40 -0400450 if (show_core) {
451 if (c)
Len Brownfc04cc62014-02-06 00:55:19 -0500452 outp += sprintf(outp, "%8d", c->core_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400453 else
Len Brownfc04cc62014-02-06 00:55:19 -0500454 outp += sprintf(outp, " -");
Len Brownc98d5d92012-06-04 00:56:40 -0400455 }
Len Brown103a8fe2010-10-22 23:53:03 -0400456 if (show_cpu)
Len Brownfc04cc62014-02-06 00:55:19 -0500457 outp += sprintf(outp, "%8d", t->cpu_id);
Len Brown103a8fe2010-10-22 23:53:03 -0400458 }
Len Brownfc04cc62014-02-06 00:55:19 -0500459
460 /* AvgMHz */
461 if (has_aperf)
462 outp += sprintf(outp, "%8.0f",
463 1.0 / units * t->aperf / interval_float);
464
Len Brown103a8fe2010-10-22 23:53:03 -0400465 /* %c0 */
466 if (do_nhm_cstates) {
467 if (!skip_c0)
Len Brownfc04cc62014-02-06 00:55:19 -0500468 outp += sprintf(outp, "%8.2f", 100.0 * t->mperf/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400469 else
Len Brownfc04cc62014-02-06 00:55:19 -0500470 outp += sprintf(outp, "********");
Len Brown103a8fe2010-10-22 23:53:03 -0400471 }
472
Len Brownfc04cc62014-02-06 00:55:19 -0500473 /* BzyMHz */
474 if (has_aperf)
475 outp += sprintf(outp, "%8.0f",
476 1.0 * t->tsc / units * t->aperf / t->mperf / interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400477
478 /* TSC */
Len Brownfc04cc62014-02-06 00:55:19 -0500479 outp += sprintf(outp, "%8.0f", 1.0 * t->tsc/units/interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400480
Len Brown1ed51012013-02-10 17:19:24 -0500481 /* SMI */
482 if (do_smi)
Len Brownfc04cc62014-02-06 00:55:19 -0500483 outp += sprintf(outp, "%8d", t->smi_count);
Len Brown1ed51012013-02-10 17:19:24 -0500484
Len Brown8e180f32012-09-22 01:25:08 -0400485 /* delta */
486 if (extra_delta_offset32)
487 outp += sprintf(outp, " %11llu", t->extra_delta32);
488
489 /* DELTA */
490 if (extra_delta_offset64)
491 outp += sprintf(outp, " %11llu", t->extra_delta64);
Len Brown2f32edf2012-09-21 23:45:46 -0400492 /* msr */
493 if (extra_msr_offset32)
Len Brown8e180f32012-09-22 01:25:08 -0400494 outp += sprintf(outp, " 0x%08llx", t->extra_msr32);
Len Brown2f32edf2012-09-21 23:45:46 -0400495
Len Brown130ff302012-09-21 22:56:06 -0400496 /* MSR */
Len Brown2f32edf2012-09-21 23:45:46 -0400497 if (extra_msr_offset64)
498 outp += sprintf(outp, " 0x%016llx", t->extra_msr64);
Len Brown130ff302012-09-21 22:56:06 -0400499
Len Brown103a8fe2010-10-22 23:53:03 -0400500 if (do_nhm_cstates) {
501 if (!skip_c1)
Len Brownfc04cc62014-02-06 00:55:19 -0500502 outp += sprintf(outp, "%8.2f", 100.0 * t->c1/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400503 else
Len Brownfc04cc62014-02-06 00:55:19 -0500504 outp += sprintf(outp, "********");
Len Brown103a8fe2010-10-22 23:53:03 -0400505 }
Len Brownc98d5d92012-06-04 00:56:40 -0400506
507 /* print per-core data only for 1st thread in core */
508 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
509 goto done;
510
Len Brown144b44b2013-11-09 00:30:16 -0500511 if (do_nhm_cstates && !do_slm_cstates)
Len Brownfc04cc62014-02-06 00:55:19 -0500512 outp += sprintf(outp, "%8.2f", 100.0 * c->c3/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400513 if (do_nhm_cstates)
Len Brownfc04cc62014-02-06 00:55:19 -0500514 outp += sprintf(outp, "%8.2f", 100.0 * c->c6/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400515 if (do_snb_cstates)
Len Brownfc04cc62014-02-06 00:55:19 -0500516 outp += sprintf(outp, "%8.2f", 100.0 * c->c7/t->tsc);
Len Brownc98d5d92012-06-04 00:56:40 -0400517
Len Brown889facb2012-11-08 00:48:57 -0500518 if (do_dts)
Len Brownfc04cc62014-02-06 00:55:19 -0500519 outp += sprintf(outp, "%8d", c->core_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500520
Len Brownc98d5d92012-06-04 00:56:40 -0400521 /* print per-package data only for 1st core in package */
522 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
523 goto done;
524
Len Brown889facb2012-11-08 00:48:57 -0500525 if (do_ptm)
Len Brownfc04cc62014-02-06 00:55:19 -0500526 outp += sprintf(outp, "%8d", p->pkg_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500527
Len Brown103a8fe2010-10-22 23:53:03 -0400528 if (do_snb_cstates)
Len Brownfc04cc62014-02-06 00:55:19 -0500529 outp += sprintf(outp, "%8.2f", 100.0 * p->pc2/t->tsc);
Len Brown144b44b2013-11-09 00:30:16 -0500530 if (do_nhm_cstates && !do_slm_cstates)
Len Brownfc04cc62014-02-06 00:55:19 -0500531 outp += sprintf(outp, "%8.2f", 100.0 * p->pc3/t->tsc);
Len Brown144b44b2013-11-09 00:30:16 -0500532 if (do_nhm_cstates && !do_slm_cstates)
Len Brownfc04cc62014-02-06 00:55:19 -0500533 outp += sprintf(outp, "%8.2f", 100.0 * p->pc6/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400534 if (do_snb_cstates)
Len Brownfc04cc62014-02-06 00:55:19 -0500535 outp += sprintf(outp, "%8.2f", 100.0 * p->pc7/t->tsc);
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800536 if (do_c8_c9_c10) {
Len Brownfc04cc62014-02-06 00:55:19 -0500537 outp += sprintf(outp, "%8.2f", 100.0 * p->pc8/t->tsc);
538 outp += sprintf(outp, "%8.2f", 100.0 * p->pc9/t->tsc);
539 outp += sprintf(outp, "%8.2f", 100.0 * p->pc10/t->tsc);
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800540 }
Len Brown889facb2012-11-08 00:48:57 -0500541
542 /*
543 * If measurement interval exceeds minimum RAPL Joule Counter range,
544 * indicate that results are suspect by printing "**" in fraction place.
545 */
Len Brownfc04cc62014-02-06 00:55:19 -0500546 if (interval_float < rapl_joule_counter_range)
547 fmt8 = "%8.2f";
548 else
549 fmt8 = " %6.0f**";
Len Brown889facb2012-11-08 00:48:57 -0500550
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800551 if (do_rapl && !rapl_joules) {
552 if (do_rapl & RAPL_PKG)
Len Brownfc04cc62014-02-06 00:55:19 -0500553 outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800554 if (do_rapl & RAPL_CORES)
Len Brownfc04cc62014-02-06 00:55:19 -0500555 outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800556 if (do_rapl & RAPL_GFX)
Len Brownfc04cc62014-02-06 00:55:19 -0500557 outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800558 if (do_rapl & RAPL_DRAM)
Len Brownfc04cc62014-02-06 00:55:19 -0500559 outp += sprintf(outp, fmt8, p->energy_dram * rapl_energy_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800560 if (do_rapl & RAPL_PKG_PERF_STATUS)
Len Brownfc04cc62014-02-06 00:55:19 -0500561 outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800562 if (do_rapl & RAPL_DRAM_PERF_STATUS)
Len Brownfc04cc62014-02-06 00:55:19 -0500563 outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800564 } else {
565 if (do_rapl & RAPL_PKG)
Len Brownfc04cc62014-02-06 00:55:19 -0500566 outp += sprintf(outp, fmt8,
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800567 p->energy_pkg * rapl_energy_units);
568 if (do_rapl & RAPL_CORES)
Len Brownfc04cc62014-02-06 00:55:19 -0500569 outp += sprintf(outp, fmt8,
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800570 p->energy_cores * rapl_energy_units);
571 if (do_rapl & RAPL_GFX)
Len Brownfc04cc62014-02-06 00:55:19 -0500572 outp += sprintf(outp, fmt8,
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800573 p->energy_gfx * rapl_energy_units);
574 if (do_rapl & RAPL_DRAM)
Len Brownfc04cc62014-02-06 00:55:19 -0500575 outp += sprintf(outp, fmt8,
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800576 p->energy_dram * rapl_energy_units);
577 if (do_rapl & RAPL_PKG_PERF_STATUS)
Len Brownfc04cc62014-02-06 00:55:19 -0500578 outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800579 if (do_rapl & RAPL_DRAM_PERF_STATUS)
Len Brownfc04cc62014-02-06 00:55:19 -0500580 outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
581 outp += sprintf(outp, fmt8, interval_float);
Len Brown889facb2012-11-08 00:48:57 -0500582
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800583 }
Len Brownc98d5d92012-06-04 00:56:40 -0400584done:
Len Brownc98d5d92012-06-04 00:56:40 -0400585 outp += sprintf(outp, "\n");
586
587 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400588}
589
Len Brownc98d5d92012-06-04 00:56:40 -0400590void flush_stdout()
Len Brown103a8fe2010-10-22 23:53:03 -0400591{
Len Brownc98d5d92012-06-04 00:56:40 -0400592 fputs(output_buffer, stdout);
Len Brownddac0d62012-11-30 01:01:40 -0500593 fflush(stdout);
Len Brownc98d5d92012-06-04 00:56:40 -0400594 outp = output_buffer;
595}
596void flush_stderr()
597{
598 fputs(output_buffer, stderr);
599 outp = output_buffer;
600}
601void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
602{
Len Browne23da032012-02-06 18:37:16 -0500603 static int printed;
Len Brown103a8fe2010-10-22 23:53:03 -0400604
Len Browne23da032012-02-06 18:37:16 -0500605 if (!printed || !summary_only)
606 print_header();
Len Brown103a8fe2010-10-22 23:53:03 -0400607
Len Brownc98d5d92012-06-04 00:56:40 -0400608 if (topo.num_cpus > 1)
609 format_counters(&average.threads, &average.cores,
610 &average.packages);
Len Brown103a8fe2010-10-22 23:53:03 -0400611
Len Browne23da032012-02-06 18:37:16 -0500612 printed = 1;
613
614 if (summary_only)
615 return;
616
Len Brownc98d5d92012-06-04 00:56:40 -0400617 for_all_cpus(format_counters, t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400618}
619
Len Brown889facb2012-11-08 00:48:57 -0500620#define DELTA_WRAP32(new, old) \
621 if (new > old) { \
622 old = new - old; \
623 } else { \
624 old = 0x100000000 + new - old; \
625 }
626
Len Brownc98d5d92012-06-04 00:56:40 -0400627void
628delta_package(struct pkg_data *new, struct pkg_data *old)
Len Brown103a8fe2010-10-22 23:53:03 -0400629{
Len Brownc98d5d92012-06-04 00:56:40 -0400630 old->pc2 = new->pc2 - old->pc2;
631 old->pc3 = new->pc3 - old->pc3;
632 old->pc6 = new->pc6 - old->pc6;
633 old->pc7 = new->pc7 - old->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800634 old->pc8 = new->pc8 - old->pc8;
635 old->pc9 = new->pc9 - old->pc9;
636 old->pc10 = new->pc10 - old->pc10;
Len Brown889facb2012-11-08 00:48:57 -0500637 old->pkg_temp_c = new->pkg_temp_c;
638
639 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
640 DELTA_WRAP32(new->energy_cores, old->energy_cores);
641 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
642 DELTA_WRAP32(new->energy_dram, old->energy_dram);
643 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
644 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
Len Brownc98d5d92012-06-04 00:56:40 -0400645}
Len Brown103a8fe2010-10-22 23:53:03 -0400646
Len Brownc98d5d92012-06-04 00:56:40 -0400647void
648delta_core(struct core_data *new, struct core_data *old)
649{
650 old->c3 = new->c3 - old->c3;
651 old->c6 = new->c6 - old->c6;
652 old->c7 = new->c7 - old->c7;
Len Brown889facb2012-11-08 00:48:57 -0500653 old->core_temp_c = new->core_temp_c;
Len Brownc98d5d92012-06-04 00:56:40 -0400654}
Len Brown103a8fe2010-10-22 23:53:03 -0400655
Len Brownc3ae3312012-06-13 21:31:46 -0400656/*
657 * old = new - old
658 */
Len Brownc98d5d92012-06-04 00:56:40 -0400659void
660delta_thread(struct thread_data *new, struct thread_data *old,
661 struct core_data *core_delta)
662{
663 old->tsc = new->tsc - old->tsc;
Len Brown103a8fe2010-10-22 23:53:03 -0400664
Len Brownc98d5d92012-06-04 00:56:40 -0400665 /* check for TSC < 1 Mcycles over interval */
Josh Triplettb2c95d92013-08-20 17:20:18 -0700666 if (old->tsc < (1000 * 1000))
667 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
668 "You can disable all c-states by booting with \"idle=poll\"\n"
669 "or just the deep ones with \"processor.max_cstate=1\"");
Len Brown103a8fe2010-10-22 23:53:03 -0400670
Len Brownc98d5d92012-06-04 00:56:40 -0400671 old->c1 = new->c1 - old->c1;
Len Brown103a8fe2010-10-22 23:53:03 -0400672
Len Brownc98d5d92012-06-04 00:56:40 -0400673 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
674 old->aperf = new->aperf - old->aperf;
675 old->mperf = new->mperf - old->mperf;
676 } else {
Len Brown103a8fe2010-10-22 23:53:03 -0400677
Len Brownc98d5d92012-06-04 00:56:40 -0400678 if (!aperf_mperf_unstable) {
679 fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname);
680 fprintf(stderr, "* Frequency results do not cover entire interval *\n");
681 fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n");
682
683 aperf_mperf_unstable = 1;
684 }
Len Brown103a8fe2010-10-22 23:53:03 -0400685 /*
Len Brownc98d5d92012-06-04 00:56:40 -0400686 * mperf delta is likely a huge "positive" number
687 * can not use it for calculating c0 time
Len Brown103a8fe2010-10-22 23:53:03 -0400688 */
Len Brownc98d5d92012-06-04 00:56:40 -0400689 skip_c0 = 1;
690 skip_c1 = 1;
691 }
Len Brown103a8fe2010-10-22 23:53:03 -0400692
Len Brown103a8fe2010-10-22 23:53:03 -0400693
Len Brown144b44b2013-11-09 00:30:16 -0500694 if (use_c1_residency_msr) {
695 /*
696 * Some models have a dedicated C1 residency MSR,
697 * which should be more accurate than the derivation below.
698 */
699 } else {
700 /*
701 * As counter collection is not atomic,
702 * it is possible for mperf's non-halted cycles + idle states
703 * to exceed TSC's all cycles: show c1 = 0% in that case.
704 */
705 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
706 old->c1 = 0;
707 else {
708 /* normal case, derive c1 */
709 old->c1 = old->tsc - old->mperf - core_delta->c3
Len Brownc98d5d92012-06-04 00:56:40 -0400710 - core_delta->c6 - core_delta->c7;
Len Brown144b44b2013-11-09 00:30:16 -0500711 }
Len Brownc98d5d92012-06-04 00:56:40 -0400712 }
Len Brownc3ae3312012-06-13 21:31:46 -0400713
Len Brownc98d5d92012-06-04 00:56:40 -0400714 if (old->mperf == 0) {
Len Brownc3ae3312012-06-13 21:31:46 -0400715 if (verbose > 1) fprintf(stderr, "cpu%d MPERF 0!\n", old->cpu_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400716 old->mperf = 1; /* divide by 0 protection */
717 }
718
Len Brown8e180f32012-09-22 01:25:08 -0400719 old->extra_delta32 = new->extra_delta32 - old->extra_delta32;
720 old->extra_delta32 &= 0xFFFFFFFF;
721
722 old->extra_delta64 = new->extra_delta64 - old->extra_delta64;
723
Len Brownc98d5d92012-06-04 00:56:40 -0400724 /*
Len Brown8e180f32012-09-22 01:25:08 -0400725 * Extra MSR is just a snapshot, simply copy latest w/o subtracting
Len Brownc98d5d92012-06-04 00:56:40 -0400726 */
Len Brown2f32edf2012-09-21 23:45:46 -0400727 old->extra_msr32 = new->extra_msr32;
728 old->extra_msr64 = new->extra_msr64;
Len Brown1ed51012013-02-10 17:19:24 -0500729
730 if (do_smi)
731 old->smi_count = new->smi_count - old->smi_count;
Len Brownc98d5d92012-06-04 00:56:40 -0400732}
733
734int delta_cpu(struct thread_data *t, struct core_data *c,
735 struct pkg_data *p, struct thread_data *t2,
736 struct core_data *c2, struct pkg_data *p2)
737{
738 /* calculate core delta only for 1st thread in core */
739 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
740 delta_core(c, c2);
741
742 /* always calculate thread delta */
743 delta_thread(t, t2, c2); /* c2 is core delta */
744
745 /* calculate package delta only for 1st core in package */
746 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
747 delta_package(p, p2);
748
749 return 0;
750}
751
752void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
753{
754 t->tsc = 0;
755 t->aperf = 0;
756 t->mperf = 0;
757 t->c1 = 0;
758
Len Brown1ed51012013-02-10 17:19:24 -0500759 t->smi_count = 0;
Len Brown8e180f32012-09-22 01:25:08 -0400760 t->extra_delta32 = 0;
761 t->extra_delta64 = 0;
762
Len Brownc98d5d92012-06-04 00:56:40 -0400763 /* tells format_counters to dump all fields from this set */
764 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
765
766 c->c3 = 0;
767 c->c6 = 0;
768 c->c7 = 0;
Len Brown889facb2012-11-08 00:48:57 -0500769 c->core_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -0400770
771 p->pc2 = 0;
772 p->pc3 = 0;
773 p->pc6 = 0;
774 p->pc7 = 0;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800775 p->pc8 = 0;
776 p->pc9 = 0;
777 p->pc10 = 0;
Len Brown889facb2012-11-08 00:48:57 -0500778
779 p->energy_pkg = 0;
780 p->energy_dram = 0;
781 p->energy_cores = 0;
782 p->energy_gfx = 0;
783 p->rapl_pkg_perf_status = 0;
784 p->rapl_dram_perf_status = 0;
785 p->pkg_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -0400786}
787int sum_counters(struct thread_data *t, struct core_data *c,
788 struct pkg_data *p)
789{
790 average.threads.tsc += t->tsc;
791 average.threads.aperf += t->aperf;
792 average.threads.mperf += t->mperf;
793 average.threads.c1 += t->c1;
794
Len Brown8e180f32012-09-22 01:25:08 -0400795 average.threads.extra_delta32 += t->extra_delta32;
796 average.threads.extra_delta64 += t->extra_delta64;
797
Len Brownc98d5d92012-06-04 00:56:40 -0400798 /* sum per-core values only for 1st thread in core */
799 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
800 return 0;
801
802 average.cores.c3 += c->c3;
803 average.cores.c6 += c->c6;
804 average.cores.c7 += c->c7;
805
Len Brown889facb2012-11-08 00:48:57 -0500806 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
807
Len Brownc98d5d92012-06-04 00:56:40 -0400808 /* sum per-pkg values only for 1st core in pkg */
809 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
810 return 0;
811
812 average.packages.pc2 += p->pc2;
813 average.packages.pc3 += p->pc3;
814 average.packages.pc6 += p->pc6;
815 average.packages.pc7 += p->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800816 average.packages.pc8 += p->pc8;
817 average.packages.pc9 += p->pc9;
818 average.packages.pc10 += p->pc10;
Len Brownc98d5d92012-06-04 00:56:40 -0400819
Len Brown889facb2012-11-08 00:48:57 -0500820 average.packages.energy_pkg += p->energy_pkg;
821 average.packages.energy_dram += p->energy_dram;
822 average.packages.energy_cores += p->energy_cores;
823 average.packages.energy_gfx += p->energy_gfx;
824
825 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
826
827 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
828 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
Len Brownc98d5d92012-06-04 00:56:40 -0400829 return 0;
830}
831/*
832 * sum the counters for all cpus in the system
833 * compute the weighted average
834 */
835void compute_average(struct thread_data *t, struct core_data *c,
836 struct pkg_data *p)
837{
838 clear_counters(&average.threads, &average.cores, &average.packages);
839
840 for_all_cpus(sum_counters, t, c, p);
841
842 average.threads.tsc /= topo.num_cpus;
843 average.threads.aperf /= topo.num_cpus;
844 average.threads.mperf /= topo.num_cpus;
845 average.threads.c1 /= topo.num_cpus;
846
Len Brown8e180f32012-09-22 01:25:08 -0400847 average.threads.extra_delta32 /= topo.num_cpus;
848 average.threads.extra_delta32 &= 0xFFFFFFFF;
849
850 average.threads.extra_delta64 /= topo.num_cpus;
851
Len Brownc98d5d92012-06-04 00:56:40 -0400852 average.cores.c3 /= topo.num_cores;
853 average.cores.c6 /= topo.num_cores;
854 average.cores.c7 /= topo.num_cores;
855
856 average.packages.pc2 /= topo.num_packages;
857 average.packages.pc3 /= topo.num_packages;
858 average.packages.pc6 /= topo.num_packages;
859 average.packages.pc7 /= topo.num_packages;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800860
861 average.packages.pc8 /= topo.num_packages;
862 average.packages.pc9 /= topo.num_packages;
863 average.packages.pc10 /= topo.num_packages;
Len Brownc98d5d92012-06-04 00:56:40 -0400864}
865
866static unsigned long long rdtsc(void)
867{
868 unsigned int low, high;
869
870 asm volatile("rdtsc" : "=a" (low), "=d" (high));
871
872 return low | ((unsigned long long)high) << 32;
873}
874
875
876/*
877 * get_counters(...)
878 * migrate to cpu
879 * acquire and record local counters for that cpu
880 */
881int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
882{
883 int cpu = t->cpu_id;
Len Brown889facb2012-11-08 00:48:57 -0500884 unsigned long long msr;
Len Brownc98d5d92012-06-04 00:56:40 -0400885
Len Browne52966c2012-11-08 22:38:05 -0500886 if (cpu_migrate(cpu)) {
887 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
Len Brownc98d5d92012-06-04 00:56:40 -0400888 return -1;
Len Browne52966c2012-11-08 22:38:05 -0500889 }
Len Brownc98d5d92012-06-04 00:56:40 -0400890
891 t->tsc = rdtsc(); /* we are running on local CPU of interest */
892
893 if (has_aperf) {
Len Brown9c63a652012-10-31 01:29:52 -0400894 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
Len Brownc98d5d92012-06-04 00:56:40 -0400895 return -3;
Len Brown9c63a652012-10-31 01:29:52 -0400896 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
Len Brownc98d5d92012-06-04 00:56:40 -0400897 return -4;
898 }
899
Len Brown1ed51012013-02-10 17:19:24 -0500900 if (do_smi) {
901 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
902 return -5;
903 t->smi_count = msr & 0xFFFFFFFF;
904 }
Len Brown8e180f32012-09-22 01:25:08 -0400905 if (extra_delta_offset32) {
Len Brown889facb2012-11-08 00:48:57 -0500906 if (get_msr(cpu, extra_delta_offset32, &msr))
Len Brown2f32edf2012-09-21 23:45:46 -0400907 return -5;
Len Brown889facb2012-11-08 00:48:57 -0500908 t->extra_delta32 = msr & 0xFFFFFFFF;
Len Brown8e180f32012-09-22 01:25:08 -0400909 }
910
911 if (extra_delta_offset64)
912 if (get_msr(cpu, extra_delta_offset64, &t->extra_delta64))
913 return -5;
914
915 if (extra_msr_offset32) {
Len Brown889facb2012-11-08 00:48:57 -0500916 if (get_msr(cpu, extra_msr_offset32, &msr))
Len Brown8e180f32012-09-22 01:25:08 -0400917 return -5;
Len Brown889facb2012-11-08 00:48:57 -0500918 t->extra_msr32 = msr & 0xFFFFFFFF;
Len Brown8e180f32012-09-22 01:25:08 -0400919 }
Len Brown2f32edf2012-09-21 23:45:46 -0400920
921 if (extra_msr_offset64)
922 if (get_msr(cpu, extra_msr_offset64, &t->extra_msr64))
Len Brownc98d5d92012-06-04 00:56:40 -0400923 return -5;
924
Len Brown144b44b2013-11-09 00:30:16 -0500925 if (use_c1_residency_msr) {
926 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
927 return -6;
928 }
929
Len Brownc98d5d92012-06-04 00:56:40 -0400930 /* collect core counters only for 1st thread in core */
931 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
932 return 0;
933
Len Brown144b44b2013-11-09 00:30:16 -0500934 if (do_nhm_cstates && !do_slm_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -0400935 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
936 return -6;
Len Brown144b44b2013-11-09 00:30:16 -0500937 }
938
939 if (do_nhm_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -0400940 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
941 return -7;
942 }
943
944 if (do_snb_cstates)
945 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
946 return -8;
947
Len Brown889facb2012-11-08 00:48:57 -0500948 if (do_dts) {
949 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
950 return -9;
951 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
952 }
953
954
Len Brownc98d5d92012-06-04 00:56:40 -0400955 /* collect package counters only for 1st core in package */
956 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
957 return 0;
958
Len Brown144b44b2013-11-09 00:30:16 -0500959 if (do_nhm_cstates && !do_slm_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -0400960 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
961 return -9;
962 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
963 return -10;
964 }
965 if (do_snb_cstates) {
966 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
967 return -11;
968 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
969 return -12;
Len Brown103a8fe2010-10-22 23:53:03 -0400970 }
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800971 if (do_c8_c9_c10) {
972 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
973 return -13;
974 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
975 return -13;
976 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
977 return -13;
978 }
Len Brown889facb2012-11-08 00:48:57 -0500979 if (do_rapl & RAPL_PKG) {
980 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
981 return -13;
982 p->energy_pkg = msr & 0xFFFFFFFF;
983 }
984 if (do_rapl & RAPL_CORES) {
985 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
986 return -14;
987 p->energy_cores = msr & 0xFFFFFFFF;
988 }
989 if (do_rapl & RAPL_DRAM) {
990 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
991 return -15;
992 p->energy_dram = msr & 0xFFFFFFFF;
993 }
994 if (do_rapl & RAPL_GFX) {
995 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
996 return -16;
997 p->energy_gfx = msr & 0xFFFFFFFF;
998 }
999 if (do_rapl & RAPL_PKG_PERF_STATUS) {
1000 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1001 return -16;
1002 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1003 }
1004 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1005 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1006 return -16;
1007 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1008 }
1009 if (do_ptm) {
1010 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1011 return -17;
1012 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1013 }
Len Brown103a8fe2010-10-22 23:53:03 -04001014 return 0;
1015}
1016
Len Brownc98d5d92012-06-04 00:56:40 -04001017void print_verbose_header(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001018{
1019 unsigned long long msr;
1020 unsigned int ratio;
1021
1022 if (!do_nehalem_platform_info)
1023 return;
1024
Len Brown9c63a652012-10-31 01:29:52 -04001025 get_msr(0, MSR_NHM_PLATFORM_INFO, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001026
Len Brown67920412013-01-31 15:22:15 -05001027 fprintf(stderr, "cpu0: MSR_NHM_PLATFORM_INFO: 0x%08llx\n", msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001028
Len Brown103a8fe2010-10-22 23:53:03 -04001029 ratio = (msr >> 40) & 0xFF;
1030 fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency\n",
1031 ratio, bclk, ratio * bclk);
1032
1033 ratio = (msr >> 8) & 0xFF;
1034 fprintf(stderr, "%d * %.0f = %.0f MHz TSC frequency\n",
1035 ratio, bclk, ratio * bclk);
1036
Len Brown67920412013-01-31 15:22:15 -05001037 get_msr(0, MSR_IA32_POWER_CTL, &msr);
Len Brown144b44b2013-11-09 00:30:16 -05001038 fprintf(stderr, "cpu0: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
Len Brown67920412013-01-31 15:22:15 -05001039 msr, msr & 0x2 ? "EN" : "DIS");
1040
Len Brown6574a5d2012-09-21 00:01:31 -04001041 if (!do_ivt_turbo_ratio_limit)
1042 goto print_nhm_turbo_ratio_limits;
1043
1044 get_msr(0, MSR_IVT_TURBO_RATIO_LIMIT, &msr);
1045
Len Brown67920412013-01-31 15:22:15 -05001046 fprintf(stderr, "cpu0: MSR_IVT_TURBO_RATIO_LIMIT: 0x%08llx\n", msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001047
1048 ratio = (msr >> 56) & 0xFF;
1049 if (ratio)
1050 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 16 active cores\n",
1051 ratio, bclk, ratio * bclk);
1052
1053 ratio = (msr >> 48) & 0xFF;
1054 if (ratio)
1055 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 15 active cores\n",
1056 ratio, bclk, ratio * bclk);
1057
1058 ratio = (msr >> 40) & 0xFF;
1059 if (ratio)
1060 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 14 active cores\n",
1061 ratio, bclk, ratio * bclk);
1062
1063 ratio = (msr >> 32) & 0xFF;
1064 if (ratio)
1065 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 13 active cores\n",
1066 ratio, bclk, ratio * bclk);
1067
1068 ratio = (msr >> 24) & 0xFF;
1069 if (ratio)
1070 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 12 active cores\n",
1071 ratio, bclk, ratio * bclk);
1072
1073 ratio = (msr >> 16) & 0xFF;
1074 if (ratio)
1075 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 11 active cores\n",
1076 ratio, bclk, ratio * bclk);
1077
1078 ratio = (msr >> 8) & 0xFF;
1079 if (ratio)
1080 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 10 active cores\n",
1081 ratio, bclk, ratio * bclk);
1082
1083 ratio = (msr >> 0) & 0xFF;
1084 if (ratio)
1085 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 9 active cores\n",
1086 ratio, bclk, ratio * bclk);
1087
1088print_nhm_turbo_ratio_limits:
Len Brown889facb2012-11-08 00:48:57 -05001089 get_msr(0, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
1090
1091#define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
1092#define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
1093
1094 fprintf(stderr, "cpu0: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", msr);
1095
1096 fprintf(stderr, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: ",
1097 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
1098 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
1099 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
1100 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
1101 (msr & (1 << 15)) ? "" : "UN",
1102 (unsigned int)msr & 7);
1103
1104
1105 switch(msr & 0x7) {
1106 case 0:
Len Brown144b44b2013-11-09 00:30:16 -05001107 fprintf(stderr, do_slm_cstates ? "no pkg states" : "pc0");
Len Brown889facb2012-11-08 00:48:57 -05001108 break;
1109 case 1:
Len Brown144b44b2013-11-09 00:30:16 -05001110 fprintf(stderr, do_slm_cstates ? "no pkg states" : do_snb_cstates ? "pc2" : "pc0");
Len Brown889facb2012-11-08 00:48:57 -05001111 break;
1112 case 2:
Len Brown144b44b2013-11-09 00:30:16 -05001113 fprintf(stderr, do_slm_cstates ? "invalid" : do_snb_cstates ? "pc6-noret" : "pc3");
Len Brown889facb2012-11-08 00:48:57 -05001114 break;
1115 case 3:
Len Brown144b44b2013-11-09 00:30:16 -05001116 fprintf(stderr, do_slm_cstates ? "invalid" : "pc6");
Len Brown889facb2012-11-08 00:48:57 -05001117 break;
1118 case 4:
Len Brown144b44b2013-11-09 00:30:16 -05001119 fprintf(stderr, do_slm_cstates ? "pc4" : "pc7");
Len Brown889facb2012-11-08 00:48:57 -05001120 break;
1121 case 5:
Len Brown144b44b2013-11-09 00:30:16 -05001122 fprintf(stderr, do_slm_cstates ? "invalid" : do_snb_cstates ? "pc7s" : "invalid");
1123 break;
1124 case 6:
1125 fprintf(stderr, do_slm_cstates ? "pc6" : "invalid");
Len Brown889facb2012-11-08 00:48:57 -05001126 break;
1127 case 7:
Len Brown144b44b2013-11-09 00:30:16 -05001128 fprintf(stderr, do_slm_cstates ? "pc7" : "unlimited");
Len Brown889facb2012-11-08 00:48:57 -05001129 break;
1130 default:
1131 fprintf(stderr, "invalid");
1132 }
1133 fprintf(stderr, ")\n");
Len Brown103a8fe2010-10-22 23:53:03 -04001134
1135 if (!do_nehalem_turbo_ratio_limit)
1136 return;
1137
Len Brown9c63a652012-10-31 01:29:52 -04001138 get_msr(0, MSR_NHM_TURBO_RATIO_LIMIT, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001139
Len Brown67920412013-01-31 15:22:15 -05001140 fprintf(stderr, "cpu0: MSR_NHM_TURBO_RATIO_LIMIT: 0x%08llx\n", msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001141
1142 ratio = (msr >> 56) & 0xFF;
1143 if (ratio)
1144 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 8 active cores\n",
1145 ratio, bclk, ratio * bclk);
1146
1147 ratio = (msr >> 48) & 0xFF;
1148 if (ratio)
1149 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 7 active cores\n",
1150 ratio, bclk, ratio * bclk);
1151
1152 ratio = (msr >> 40) & 0xFF;
1153 if (ratio)
1154 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 6 active cores\n",
1155 ratio, bclk, ratio * bclk);
1156
1157 ratio = (msr >> 32) & 0xFF;
1158 if (ratio)
1159 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 5 active cores\n",
1160 ratio, bclk, ratio * bclk);
1161
Len Brown103a8fe2010-10-22 23:53:03 -04001162 ratio = (msr >> 24) & 0xFF;
1163 if (ratio)
1164 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
1165 ratio, bclk, ratio * bclk);
1166
1167 ratio = (msr >> 16) & 0xFF;
1168 if (ratio)
1169 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
1170 ratio, bclk, ratio * bclk);
1171
1172 ratio = (msr >> 8) & 0xFF;
1173 if (ratio)
1174 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
1175 ratio, bclk, ratio * bclk);
1176
1177 ratio = (msr >> 0) & 0xFF;
1178 if (ratio)
1179 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
1180 ratio, bclk, ratio * bclk);
Len Brown103a8fe2010-10-22 23:53:03 -04001181}
1182
Len Brownc98d5d92012-06-04 00:56:40 -04001183void free_all_buffers(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001184{
Len Brownc98d5d92012-06-04 00:56:40 -04001185 CPU_FREE(cpu_present_set);
1186 cpu_present_set = NULL;
1187 cpu_present_set = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001188
Len Brownc98d5d92012-06-04 00:56:40 -04001189 CPU_FREE(cpu_affinity_set);
1190 cpu_affinity_set = NULL;
1191 cpu_affinity_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001192
Len Brownc98d5d92012-06-04 00:56:40 -04001193 free(thread_even);
1194 free(core_even);
1195 free(package_even);
1196
1197 thread_even = NULL;
1198 core_even = NULL;
1199 package_even = NULL;
1200
1201 free(thread_odd);
1202 free(core_odd);
1203 free(package_odd);
1204
1205 thread_odd = NULL;
1206 core_odd = NULL;
1207 package_odd = NULL;
1208
1209 free(output_buffer);
1210 output_buffer = NULL;
1211 outp = NULL;
Len Brown103a8fe2010-10-22 23:53:03 -04001212}
1213
Len Brownc98d5d92012-06-04 00:56:40 -04001214/*
Josh Triplett57a42a32013-08-20 17:20:17 -07001215 * Open a file, and exit on failure
1216 */
1217FILE *fopen_or_die(const char *path, const char *mode)
1218{
1219 FILE *filep = fopen(path, "r");
Josh Triplettb2c95d92013-08-20 17:20:18 -07001220 if (!filep)
1221 err(1, "%s: open failed", path);
Josh Triplett57a42a32013-08-20 17:20:17 -07001222 return filep;
1223}
1224
1225/*
Josh Triplett95aebc42013-08-20 17:20:16 -07001226 * Parse a file containing a single int.
1227 */
1228int parse_int_file(const char *fmt, ...)
1229{
1230 va_list args;
1231 char path[PATH_MAX];
1232 FILE *filep;
1233 int value;
1234
1235 va_start(args, fmt);
1236 vsnprintf(path, sizeof(path), fmt, args);
1237 va_end(args);
Josh Triplett57a42a32013-08-20 17:20:17 -07001238 filep = fopen_or_die(path, "r");
Josh Triplettb2c95d92013-08-20 17:20:18 -07001239 if (fscanf(filep, "%d", &value) != 1)
1240 err(1, "%s: failed to parse number from file", path);
Josh Triplett95aebc42013-08-20 17:20:16 -07001241 fclose(filep);
1242 return value;
1243}
1244
1245/*
Len Brownc98d5d92012-06-04 00:56:40 -04001246 * cpu_is_first_sibling_in_core(cpu)
1247 * return 1 if given CPU is 1st HT sibling in the core
1248 */
1249int cpu_is_first_sibling_in_core(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001250{
Josh Triplett95aebc42013-08-20 17:20:16 -07001251 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001252}
1253
Len Brownc98d5d92012-06-04 00:56:40 -04001254/*
1255 * cpu_is_first_core_in_package(cpu)
1256 * return 1 if given CPU is 1st core in package
1257 */
1258int cpu_is_first_core_in_package(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001259{
Josh Triplett95aebc42013-08-20 17:20:16 -07001260 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001261}
1262
1263int get_physical_package_id(int cpu)
1264{
Josh Triplett95aebc42013-08-20 17:20:16 -07001265 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001266}
1267
1268int get_core_id(int cpu)
1269{
Josh Triplett95aebc42013-08-20 17:20:16 -07001270 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001271}
1272
Len Brownc98d5d92012-06-04 00:56:40 -04001273int get_num_ht_siblings(int cpu)
1274{
1275 char path[80];
1276 FILE *filep;
1277 int sib1, sib2;
1278 int matches;
1279 char character;
1280
1281 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
Josh Triplett57a42a32013-08-20 17:20:17 -07001282 filep = fopen_or_die(path, "r");
Len Brownc98d5d92012-06-04 00:56:40 -04001283 /*
1284 * file format:
1285 * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4)
1286 * otherwinse 1 sibling (self).
1287 */
1288 matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2);
1289
1290 fclose(filep);
1291
1292 if (matches == 3)
1293 return 2;
1294 else
1295 return 1;
1296}
1297
Len Brown103a8fe2010-10-22 23:53:03 -04001298/*
Len Brownc98d5d92012-06-04 00:56:40 -04001299 * run func(thread, core, package) in topology order
1300 * skip non-present cpus
Len Brown103a8fe2010-10-22 23:53:03 -04001301 */
1302
Len Brownc98d5d92012-06-04 00:56:40 -04001303int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
1304 struct pkg_data *, struct thread_data *, struct core_data *,
1305 struct pkg_data *), struct thread_data *thread_base,
1306 struct core_data *core_base, struct pkg_data *pkg_base,
1307 struct thread_data *thread_base2, struct core_data *core_base2,
1308 struct pkg_data *pkg_base2)
1309{
1310 int retval, pkg_no, core_no, thread_no;
1311
1312 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
1313 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
1314 for (thread_no = 0; thread_no <
1315 topo.num_threads_per_core; ++thread_no) {
1316 struct thread_data *t, *t2;
1317 struct core_data *c, *c2;
1318 struct pkg_data *p, *p2;
1319
1320 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
1321
1322 if (cpu_is_not_present(t->cpu_id))
1323 continue;
1324
1325 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
1326
1327 c = GET_CORE(core_base, core_no, pkg_no);
1328 c2 = GET_CORE(core_base2, core_no, pkg_no);
1329
1330 p = GET_PKG(pkg_base, pkg_no);
1331 p2 = GET_PKG(pkg_base2, pkg_no);
1332
1333 retval = func(t, c, p, t2, c2, p2);
1334 if (retval)
1335 return retval;
1336 }
1337 }
1338 }
1339 return 0;
1340}
1341
1342/*
1343 * run func(cpu) on every cpu in /proc/stat
1344 * return max_cpu number
1345 */
1346int for_all_proc_cpus(int (func)(int))
Len Brown103a8fe2010-10-22 23:53:03 -04001347{
1348 FILE *fp;
Len Brownc98d5d92012-06-04 00:56:40 -04001349 int cpu_num;
Len Brown103a8fe2010-10-22 23:53:03 -04001350 int retval;
1351
Josh Triplett57a42a32013-08-20 17:20:17 -07001352 fp = fopen_or_die(proc_stat, "r");
Len Brown103a8fe2010-10-22 23:53:03 -04001353
1354 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
Josh Triplettb2c95d92013-08-20 17:20:18 -07001355 if (retval != 0)
1356 err(1, "%s: failed to parse format", proc_stat);
Len Brown103a8fe2010-10-22 23:53:03 -04001357
Len Brownc98d5d92012-06-04 00:56:40 -04001358 while (1) {
1359 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 -04001360 if (retval != 1)
1361 break;
1362
Len Brownc98d5d92012-06-04 00:56:40 -04001363 retval = func(cpu_num);
1364 if (retval) {
1365 fclose(fp);
1366 return(retval);
1367 }
Len Brown103a8fe2010-10-22 23:53:03 -04001368 }
1369 fclose(fp);
Len Brownc98d5d92012-06-04 00:56:40 -04001370 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001371}
1372
1373void re_initialize(void)
1374{
Len Brownc98d5d92012-06-04 00:56:40 -04001375 free_all_buffers();
1376 setup_all_buffers();
1377 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04001378}
1379
Len Brownc98d5d92012-06-04 00:56:40 -04001380
Len Brown103a8fe2010-10-22 23:53:03 -04001381/*
Len Brownc98d5d92012-06-04 00:56:40 -04001382 * count_cpus()
1383 * remember the last one seen, it will be the max
Len Brown103a8fe2010-10-22 23:53:03 -04001384 */
Len Brownc98d5d92012-06-04 00:56:40 -04001385int count_cpus(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001386{
Len Brownc98d5d92012-06-04 00:56:40 -04001387 if (topo.max_cpu_num < cpu)
1388 topo.max_cpu_num = cpu;
Len Brown103a8fe2010-10-22 23:53:03 -04001389
Len Brownc98d5d92012-06-04 00:56:40 -04001390 topo.num_cpus += 1;
1391 return 0;
1392}
1393int mark_cpu_present(int cpu)
1394{
1395 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brown15aaa342012-03-29 22:19:58 -04001396 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001397}
1398
1399void turbostat_loop()
1400{
Len Brownc98d5d92012-06-04 00:56:40 -04001401 int retval;
Len Browne52966c2012-11-08 22:38:05 -05001402 int restarted = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001403
Len Brown103a8fe2010-10-22 23:53:03 -04001404restart:
Len Browne52966c2012-11-08 22:38:05 -05001405 restarted++;
1406
Len Brownc98d5d92012-06-04 00:56:40 -04001407 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001408 if (retval < -1) {
1409 exit(retval);
1410 } else if (retval == -1) {
Len Browne52966c2012-11-08 22:38:05 -05001411 if (restarted > 1) {
1412 exit(retval);
1413 }
Len Brownc98d5d92012-06-04 00:56:40 -04001414 re_initialize();
1415 goto restart;
1416 }
Len Browne52966c2012-11-08 22:38:05 -05001417 restarted = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001418 gettimeofday(&tv_even, (struct timezone *)NULL);
1419
1420 while (1) {
Len Brownc98d5d92012-06-04 00:56:40 -04001421 if (for_all_proc_cpus(cpu_is_not_present)) {
Len Brown103a8fe2010-10-22 23:53:03 -04001422 re_initialize();
1423 goto restart;
1424 }
1425 sleep(interval_sec);
Len Brownc98d5d92012-06-04 00:56:40 -04001426 retval = for_all_cpus(get_counters, ODD_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001427 if (retval < -1) {
1428 exit(retval);
1429 } else if (retval == -1) {
Len Brown15aaa342012-03-29 22:19:58 -04001430 re_initialize();
1431 goto restart;
1432 }
Len Brown103a8fe2010-10-22 23:53:03 -04001433 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04001434 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04001435 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
1436 compute_average(EVEN_COUNTERS);
1437 format_all_counters(EVEN_COUNTERS);
1438 flush_stdout();
Len Brown15aaa342012-03-29 22:19:58 -04001439 sleep(interval_sec);
Len Brownc98d5d92012-06-04 00:56:40 -04001440 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001441 if (retval < -1) {
1442 exit(retval);
1443 } else if (retval == -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04001444 re_initialize();
1445 goto restart;
1446 }
Len Brown103a8fe2010-10-22 23:53:03 -04001447 gettimeofday(&tv_even, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04001448 timersub(&tv_even, &tv_odd, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04001449 for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS);
1450 compute_average(ODD_COUNTERS);
1451 format_all_counters(ODD_COUNTERS);
1452 flush_stdout();
Len Brown103a8fe2010-10-22 23:53:03 -04001453 }
1454}
1455
1456void check_dev_msr()
1457{
1458 struct stat sb;
1459
Josh Triplettb2c95d92013-08-20 17:20:18 -07001460 if (stat("/dev/cpu/0/msr", &sb))
1461 err(-5, "no /dev/cpu/0/msr\n"
1462 "Try \"# modprobe msr\"");
Len Brown103a8fe2010-10-22 23:53:03 -04001463}
1464
Len Brown98481e72014-08-15 00:36:50 -04001465void check_permissions()
Len Brown103a8fe2010-10-22 23:53:03 -04001466{
Len Brown98481e72014-08-15 00:36:50 -04001467 struct __user_cap_header_struct cap_header_data;
1468 cap_user_header_t cap_header = &cap_header_data;
1469 struct __user_cap_data_struct cap_data_data;
1470 cap_user_data_t cap_data = &cap_data_data;
1471 extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
1472 int do_exit = 0;
1473
1474 /* check for CAP_SYS_RAWIO */
1475 cap_header->pid = getpid();
1476 cap_header->version = _LINUX_CAPABILITY_VERSION;
1477 if (capget(cap_header, cap_data) < 0)
1478 err(-6, "capget(2) failed");
1479
1480 if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
1481 do_exit++;
1482 warnx("capget(CAP_SYS_RAWIO) failed,"
1483 " try \"# setcap cap_sys_rawio=ep %s\"", progname);
1484 }
1485
1486 /* test file permissions */
1487 if (euidaccess("/dev/cpu/0/msr", R_OK)) {
1488 do_exit++;
1489 warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
1490 }
1491
1492 /* if all else fails, thell them to be root */
1493 if (do_exit)
1494 if (getuid() != 0)
1495 warnx("Or simply run as root");
1496
1497 if (do_exit)
1498 exit(-6);
Len Brown103a8fe2010-10-22 23:53:03 -04001499}
1500
1501int has_nehalem_turbo_ratio_limit(unsigned int family, unsigned int model)
1502{
1503 if (!genuine_intel)
1504 return 0;
1505
1506 if (family != 6)
1507 return 0;
1508
1509 switch (model) {
1510 case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
1511 case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
1512 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
1513 case 0x25: /* Westmere Client - Clarkdale, Arrandale */
1514 case 0x2C: /* Westmere EP - Gulftown */
1515 case 0x2A: /* SNB */
1516 case 0x2D: /* SNB Xeon */
Len Brown553575f2011-11-18 03:32:01 -05001517 case 0x3A: /* IVB */
Len Brown13006512012-09-26 18:11:31 -04001518 case 0x3E: /* IVB Xeon */
Len Brown70b43402013-01-08 01:26:07 -05001519 case 0x3C: /* HSW */
Len Browne6f9bb32013-12-03 02:19:19 -05001520 case 0x3F: /* HSX */
Len Brown70b43402013-01-08 01:26:07 -05001521 case 0x45: /* HSW */
Len Brown149c2312013-03-15 10:58:02 -04001522 case 0x46: /* HSW */
Len Brown144b44b2013-11-09 00:30:16 -05001523 case 0x37: /* BYT */
1524 case 0x4D: /* AVN */
Len Brown4e8e863f2014-02-27 23:28:53 -05001525 case 0x3D: /* BDW */
1526 case 0x4F: /* BDX */
1527 case 0x56: /* BDX-DE */
Len Brown103a8fe2010-10-22 23:53:03 -04001528 return 1;
1529 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1530 case 0x2F: /* Westmere-EX Xeon - Eagleton */
1531 default:
1532 return 0;
1533 }
1534}
Len Brown6574a5d2012-09-21 00:01:31 -04001535int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
1536{
1537 if (!genuine_intel)
1538 return 0;
1539
1540 if (family != 6)
1541 return 0;
1542
1543 switch (model) {
1544 case 0x3E: /* IVB Xeon */
1545 return 1;
1546 default:
1547 return 0;
1548 }
1549}
1550
Len Brown889facb2012-11-08 00:48:57 -05001551/*
1552 * print_epb()
1553 * Decode the ENERGY_PERF_BIAS MSR
1554 */
1555int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1556{
1557 unsigned long long msr;
1558 char *epb_string;
1559 int cpu;
1560
1561 if (!has_epb)
1562 return 0;
1563
1564 cpu = t->cpu_id;
1565
1566 /* EPB is per-package */
1567 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1568 return 0;
1569
1570 if (cpu_migrate(cpu)) {
1571 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1572 return -1;
1573 }
1574
1575 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
1576 return 0;
1577
1578 switch (msr & 0x7) {
1579 case ENERGY_PERF_BIAS_PERFORMANCE:
1580 epb_string = "performance";
1581 break;
1582 case ENERGY_PERF_BIAS_NORMAL:
1583 epb_string = "balanced";
1584 break;
1585 case ENERGY_PERF_BIAS_POWERSAVE:
1586 epb_string = "powersave";
1587 break;
1588 default:
1589 epb_string = "custom";
1590 break;
1591 }
1592 fprintf(stderr, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
1593
1594 return 0;
1595}
1596
1597#define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
1598#define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
1599
Len Brown144b44b2013-11-09 00:30:16 -05001600double get_tdp(model)
1601{
1602 unsigned long long msr;
1603
1604 if (do_rapl & RAPL_PKG_POWER_INFO)
1605 if (!get_msr(0, MSR_PKG_POWER_INFO, &msr))
1606 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
1607
1608 switch (model) {
1609 case 0x37:
1610 case 0x4D:
1611 return 30.0;
1612 default:
1613 return 135.0;
1614 }
1615}
1616
1617
Len Brown889facb2012-11-08 00:48:57 -05001618/*
1619 * rapl_probe()
1620 *
Len Brown144b44b2013-11-09 00:30:16 -05001621 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
Len Brown889facb2012-11-08 00:48:57 -05001622 */
1623void rapl_probe(unsigned int family, unsigned int model)
1624{
1625 unsigned long long msr;
Len Brown144b44b2013-11-09 00:30:16 -05001626 unsigned int time_unit;
Len Brown889facb2012-11-08 00:48:57 -05001627 double tdp;
1628
1629 if (!genuine_intel)
1630 return;
1631
1632 if (family != 6)
1633 return;
1634
1635 switch (model) {
1636 case 0x2A:
1637 case 0x3A:
Len Brown70b43402013-01-08 01:26:07 -05001638 case 0x3C: /* HSW */
Len Brown70b43402013-01-08 01:26:07 -05001639 case 0x45: /* HSW */
Len Brown149c2312013-03-15 10:58:02 -04001640 case 0x46: /* HSW */
Len Brown4e8e863f2014-02-27 23:28:53 -05001641 case 0x3D: /* BDW */
Len Brown144b44b2013-11-09 00:30:16 -05001642 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
Len Brown889facb2012-11-08 00:48:57 -05001643 break;
Len Browne6f9bb32013-12-03 02:19:19 -05001644 case 0x3F: /* HSX */
Len Brown4e8e863f2014-02-27 23:28:53 -05001645 case 0x4F: /* BDX */
1646 case 0x56: /* BDX-DE */
Len Browne6f9bb32013-12-03 02:19:19 -05001647 do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
1648 break;
Len Brown889facb2012-11-08 00:48:57 -05001649 case 0x2D:
1650 case 0x3E:
Len Brown144b44b2013-11-09 00:30:16 -05001651 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_PKG_PERF_STATUS | RAPL_DRAM_PERF_STATUS | RAPL_PKG_POWER_INFO;
1652 break;
1653 case 0x37: /* BYT */
1654 case 0x4D: /* AVN */
1655 do_rapl = RAPL_PKG | RAPL_CORES ;
Len Brown889facb2012-11-08 00:48:57 -05001656 break;
1657 default:
1658 return;
1659 }
1660
1661 /* units on package 0, verify later other packages match */
1662 if (get_msr(0, MSR_RAPL_POWER_UNIT, &msr))
1663 return;
1664
1665 rapl_power_units = 1.0 / (1 << (msr & 0xF));
Len Brown144b44b2013-11-09 00:30:16 -05001666 if (model == 0x37)
1667 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
1668 else
1669 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
Len Brown889facb2012-11-08 00:48:57 -05001670
Len Brown144b44b2013-11-09 00:30:16 -05001671 time_unit = msr >> 16 & 0xF;
1672 if (time_unit == 0)
1673 time_unit = 0xA;
Len Brown889facb2012-11-08 00:48:57 -05001674
Len Brown144b44b2013-11-09 00:30:16 -05001675 rapl_time_units = 1.0 / (1 << (time_unit));
1676
1677 tdp = get_tdp(model);
Len Brown889facb2012-11-08 00:48:57 -05001678
1679 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
Len Brown889facb2012-11-08 00:48:57 -05001680 if (verbose)
Len Brown144b44b2013-11-09 00:30:16 -05001681 fprintf(stderr, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
Len Brown889facb2012-11-08 00:48:57 -05001682
1683 return;
1684}
1685
1686int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1687{
1688 unsigned long long msr;
1689 unsigned int dts;
1690 int cpu;
1691
1692 if (!(do_dts || do_ptm))
1693 return 0;
1694
1695 cpu = t->cpu_id;
1696
1697 /* DTS is per-core, no need to print for each thread */
1698 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1699 return 0;
1700
1701 if (cpu_migrate(cpu)) {
1702 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1703 return -1;
1704 }
1705
1706 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
1707 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1708 return 0;
1709
1710 dts = (msr >> 16) & 0x7F;
1711 fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
1712 cpu, msr, tcc_activation_temp - dts);
1713
1714#ifdef THERM_DEBUG
1715 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
1716 return 0;
1717
1718 dts = (msr >> 16) & 0x7F;
1719 dts2 = (msr >> 8) & 0x7F;
1720 fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
1721 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
1722#endif
1723 }
1724
1725
1726 if (do_dts) {
1727 unsigned int resolution;
1728
1729 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1730 return 0;
1731
1732 dts = (msr >> 16) & 0x7F;
1733 resolution = (msr >> 27) & 0xF;
1734 fprintf(stderr, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
1735 cpu, msr, tcc_activation_temp - dts, resolution);
1736
1737#ifdef THERM_DEBUG
1738 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
1739 return 0;
1740
1741 dts = (msr >> 16) & 0x7F;
1742 dts2 = (msr >> 8) & 0x7F;
1743 fprintf(stderr, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
1744 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
1745#endif
1746 }
1747
1748 return 0;
1749}
1750
1751void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
1752{
1753 fprintf(stderr, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
1754 cpu, label,
1755 ((msr >> 15) & 1) ? "EN" : "DIS",
1756 ((msr >> 0) & 0x7FFF) * rapl_power_units,
1757 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
1758 (((msr >> 16) & 1) ? "EN" : "DIS"));
1759
1760 return;
1761}
1762
1763int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1764{
1765 unsigned long long msr;
1766 int cpu;
Len Brown889facb2012-11-08 00:48:57 -05001767
1768 if (!do_rapl)
1769 return 0;
1770
1771 /* RAPL counters are per package, so print only for 1st thread/package */
1772 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1773 return 0;
1774
1775 cpu = t->cpu_id;
1776 if (cpu_migrate(cpu)) {
1777 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1778 return -1;
1779 }
1780
1781 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
1782 return -1;
1783
Len Brown889facb2012-11-08 00:48:57 -05001784 if (verbose) {
1785 fprintf(stderr, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx "
1786 "(%f Watts, %f Joules, %f sec.)\n", cpu, msr,
Len Brown144b44b2013-11-09 00:30:16 -05001787 rapl_power_units, rapl_energy_units, rapl_time_units);
Len Brown889facb2012-11-08 00:48:57 -05001788 }
Len Brown144b44b2013-11-09 00:30:16 -05001789 if (do_rapl & RAPL_PKG_POWER_INFO) {
1790
Len Brown889facb2012-11-08 00:48:57 -05001791 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
1792 return -5;
1793
1794
1795 fprintf(stderr, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
1796 cpu, msr,
1797 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1798 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1799 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1800 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
1801
Len Brown144b44b2013-11-09 00:30:16 -05001802 }
1803 if (do_rapl & RAPL_PKG) {
1804
Len Brown889facb2012-11-08 00:48:57 -05001805 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
1806 return -9;
1807
1808 fprintf(stderr, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
1809 cpu, msr, (msr >> 63) & 1 ? "": "UN");
1810
1811 print_power_limit_msr(cpu, msr, "PKG Limit #1");
1812 fprintf(stderr, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
1813 cpu,
1814 ((msr >> 47) & 1) ? "EN" : "DIS",
1815 ((msr >> 32) & 0x7FFF) * rapl_power_units,
1816 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
1817 ((msr >> 48) & 1) ? "EN" : "DIS");
1818 }
1819
1820 if (do_rapl & RAPL_DRAM) {
1821 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
1822 return -6;
1823
1824
1825 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
1826 cpu, msr,
1827 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1828 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1829 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1830 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
1831
1832
1833 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
1834 return -9;
1835 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
1836 cpu, msr, (msr >> 31) & 1 ? "": "UN");
1837
1838 print_power_limit_msr(cpu, msr, "DRAM Limit");
1839 }
Len Brown144b44b2013-11-09 00:30:16 -05001840 if (do_rapl & RAPL_CORE_POLICY) {
Len Brown889facb2012-11-08 00:48:57 -05001841 if (verbose) {
1842 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
1843 return -7;
1844
1845 fprintf(stderr, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown144b44b2013-11-09 00:30:16 -05001846 }
1847 }
1848 if (do_rapl & RAPL_CORES) {
1849 if (verbose) {
Len Brown889facb2012-11-08 00:48:57 -05001850
1851 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
1852 return -9;
1853 fprintf(stderr, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
1854 cpu, msr, (msr >> 31) & 1 ? "": "UN");
1855 print_power_limit_msr(cpu, msr, "Cores Limit");
1856 }
1857 }
1858 if (do_rapl & RAPL_GFX) {
1859 if (verbose) {
1860 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
1861 return -8;
1862
1863 fprintf(stderr, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
1864
1865 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
1866 return -9;
1867 fprintf(stderr, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
1868 cpu, msr, (msr >> 31) & 1 ? "": "UN");
1869 print_power_limit_msr(cpu, msr, "GFX Limit");
1870 }
1871 }
1872 return 0;
1873}
1874
Len Brown103a8fe2010-10-22 23:53:03 -04001875
1876int is_snb(unsigned int family, unsigned int model)
1877{
1878 if (!genuine_intel)
1879 return 0;
1880
1881 switch (model) {
1882 case 0x2A:
1883 case 0x2D:
Len Brown650a37f2012-06-03 23:34:44 -04001884 case 0x3A: /* IVB */
Len Brown13006512012-09-26 18:11:31 -04001885 case 0x3E: /* IVB Xeon */
Len Brown70b43402013-01-08 01:26:07 -05001886 case 0x3C: /* HSW */
1887 case 0x3F: /* HSW */
1888 case 0x45: /* HSW */
Len Brown149c2312013-03-15 10:58:02 -04001889 case 0x46: /* HSW */
Len Brown4e8e863f2014-02-27 23:28:53 -05001890 case 0x3D: /* BDW */
1891 case 0x4F: /* BDX */
1892 case 0x56: /* BDX-DE */
Len Brown103a8fe2010-10-22 23:53:03 -04001893 return 1;
1894 }
1895 return 0;
1896}
1897
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001898int has_c8_c9_c10(unsigned int family, unsigned int model)
1899{
1900 if (!genuine_intel)
1901 return 0;
1902
1903 switch (model) {
Len Brown4e8e863f2014-02-27 23:28:53 -05001904 case 0x45: /* HSW */
1905 case 0x3D: /* BDW */
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001906 return 1;
1907 }
1908 return 0;
1909}
1910
1911
Len Brown144b44b2013-11-09 00:30:16 -05001912int is_slm(unsigned int family, unsigned int model)
1913{
1914 if (!genuine_intel)
1915 return 0;
1916 switch (model) {
1917 case 0x37: /* BYT */
1918 case 0x4D: /* AVN */
1919 return 1;
1920 }
1921 return 0;
1922}
1923
1924#define SLM_BCLK_FREQS 5
1925double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
1926
1927double slm_bclk(void)
1928{
1929 unsigned long long msr = 3;
1930 unsigned int i;
1931 double freq;
1932
1933 if (get_msr(0, MSR_FSB_FREQ, &msr))
1934 fprintf(stderr, "SLM BCLK: unknown\n");
1935
1936 i = msr & 0xf;
1937 if (i >= SLM_BCLK_FREQS) {
1938 fprintf(stderr, "SLM BCLK[%d] invalid\n", i);
1939 msr = 3;
1940 }
1941 freq = slm_freq_table[i];
1942
1943 fprintf(stderr, "SLM BCLK: %.1f Mhz\n", freq);
1944
1945 return freq;
1946}
1947
Len Brown103a8fe2010-10-22 23:53:03 -04001948double discover_bclk(unsigned int family, unsigned int model)
1949{
1950 if (is_snb(family, model))
1951 return 100.00;
Len Brown144b44b2013-11-09 00:30:16 -05001952 else if (is_slm(family, model))
1953 return slm_bclk();
Len Brown103a8fe2010-10-22 23:53:03 -04001954 else
1955 return 133.33;
1956}
1957
Len Brown889facb2012-11-08 00:48:57 -05001958/*
1959 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
1960 * the Thermal Control Circuit (TCC) activates.
1961 * This is usually equal to tjMax.
1962 *
1963 * Older processors do not have this MSR, so there we guess,
1964 * but also allow cmdline over-ride with -T.
1965 *
1966 * Several MSR temperature values are in units of degrees-C
1967 * below this value, including the Digital Thermal Sensor (DTS),
1968 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
1969 */
1970int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1971{
1972 unsigned long long msr;
1973 unsigned int target_c_local;
1974 int cpu;
1975
1976 /* tcc_activation_temp is used only for dts or ptm */
1977 if (!(do_dts || do_ptm))
1978 return 0;
1979
1980 /* this is a per-package concept */
1981 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1982 return 0;
1983
1984 cpu = t->cpu_id;
1985 if (cpu_migrate(cpu)) {
1986 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1987 return -1;
1988 }
1989
1990 if (tcc_activation_temp_override != 0) {
1991 tcc_activation_temp = tcc_activation_temp_override;
1992 fprintf(stderr, "cpu%d: Using cmdline TCC Target (%d C)\n",
1993 cpu, tcc_activation_temp);
1994 return 0;
1995 }
1996
1997 /* Temperature Target MSR is Nehalem and newer only */
1998 if (!do_nehalem_platform_info)
1999 goto guess;
2000
2001 if (get_msr(0, MSR_IA32_TEMPERATURE_TARGET, &msr))
2002 goto guess;
2003
Jean Delvare34821242014-05-01 11:40:19 +02002004 target_c_local = (msr >> 16) & 0xFF;
Len Brown889facb2012-11-08 00:48:57 -05002005
2006 if (verbose)
2007 fprintf(stderr, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
2008 cpu, msr, target_c_local);
2009
Jean Delvare34821242014-05-01 11:40:19 +02002010 if (!target_c_local)
Len Brown889facb2012-11-08 00:48:57 -05002011 goto guess;
2012
2013 tcc_activation_temp = target_c_local;
2014
2015 return 0;
2016
2017guess:
2018 tcc_activation_temp = TJMAX_DEFAULT;
2019 fprintf(stderr, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
2020 cpu, tcc_activation_temp);
2021
2022 return 0;
2023}
Len Brown103a8fe2010-10-22 23:53:03 -04002024void check_cpuid()
2025{
2026 unsigned int eax, ebx, ecx, edx, max_level;
2027 unsigned int fms, family, model, stepping;
2028
2029 eax = ebx = ecx = edx = 0;
2030
Josh Triplett2b928652013-08-20 17:20:14 -07002031 __get_cpuid(0, &max_level, &ebx, &ecx, &edx);
Len Brown103a8fe2010-10-22 23:53:03 -04002032
2033 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
2034 genuine_intel = 1;
2035
2036 if (verbose)
Len Brown889facb2012-11-08 00:48:57 -05002037 fprintf(stderr, "CPUID(0): %.4s%.4s%.4s ",
Len Brown103a8fe2010-10-22 23:53:03 -04002038 (char *)&ebx, (char *)&edx, (char *)&ecx);
2039
Josh Triplett2b928652013-08-20 17:20:14 -07002040 __get_cpuid(1, &fms, &ebx, &ecx, &edx);
Len Brown103a8fe2010-10-22 23:53:03 -04002041 family = (fms >> 8) & 0xf;
2042 model = (fms >> 4) & 0xf;
2043 stepping = fms & 0xf;
2044 if (family == 6 || family == 0xf)
2045 model += ((fms >> 16) & 0xf) << 4;
2046
2047 if (verbose)
2048 fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
2049 max_level, family, model, stepping, family, model, stepping);
2050
Josh Triplettb2c95d92013-08-20 17:20:18 -07002051 if (!(edx & (1 << 5)))
2052 errx(1, "CPUID: no MSR");
Len Brown103a8fe2010-10-22 23:53:03 -04002053
2054 /*
2055 * check max extended function levels of CPUID.
2056 * This is needed to check for invariant TSC.
2057 * This check is valid for both Intel and AMD.
2058 */
2059 ebx = ecx = edx = 0;
Josh Triplett2b928652013-08-20 17:20:14 -07002060 __get_cpuid(0x80000000, &max_level, &ebx, &ecx, &edx);
Len Brown103a8fe2010-10-22 23:53:03 -04002061
Josh Triplettb2c95d92013-08-20 17:20:18 -07002062 if (max_level < 0x80000007)
2063 errx(1, "CPUID: no invariant TSC (max_level 0x%x)", max_level);
Len Brown103a8fe2010-10-22 23:53:03 -04002064
2065 /*
2066 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
2067 * this check is valid for both Intel and AMD
2068 */
Josh Triplett2b928652013-08-20 17:20:14 -07002069 __get_cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
Thomas Renninger8209e052011-01-21 15:11:19 +01002070 has_invariant_tsc = edx & (1 << 8);
Len Brown103a8fe2010-10-22 23:53:03 -04002071
Josh Triplettb2c95d92013-08-20 17:20:18 -07002072 if (!has_invariant_tsc)
2073 errx(1, "No invariant TSC");
Len Brown103a8fe2010-10-22 23:53:03 -04002074
2075 /*
2076 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
2077 * this check is valid for both Intel and AMD
2078 */
2079
Josh Triplett2b928652013-08-20 17:20:14 -07002080 __get_cpuid(0x6, &eax, &ebx, &ecx, &edx);
Thomas Renninger8209e052011-01-21 15:11:19 +01002081 has_aperf = ecx & (1 << 0);
Len Brown889facb2012-11-08 00:48:57 -05002082 do_dts = eax & (1 << 0);
2083 do_ptm = eax & (1 << 6);
2084 has_epb = ecx & (1 << 3);
2085
2086 if (verbose)
2087 fprintf(stderr, "CPUID(6): %s%s%s%s\n",
2088 has_aperf ? "APERF" : "No APERF!",
2089 do_dts ? ", DTS" : "",
2090 do_ptm ? ", PTM": "",
2091 has_epb ? ", EPB": "");
2092
2093 if (!has_aperf)
Josh Triplettb2c95d92013-08-20 17:20:18 -07002094 errx(-1, "No APERF");
Len Brown103a8fe2010-10-22 23:53:03 -04002095
2096 do_nehalem_platform_info = genuine_intel && has_invariant_tsc;
2097 do_nhm_cstates = genuine_intel; /* all Intel w/ non-stop TSC have NHM counters */
Len Brown1ed51012013-02-10 17:19:24 -05002098 do_smi = do_nhm_cstates;
Len Brown103a8fe2010-10-22 23:53:03 -04002099 do_snb_cstates = is_snb(family, model);
Kristen Carlson Accardica587102012-11-21 05:22:43 -08002100 do_c8_c9_c10 = has_c8_c9_c10(family, model);
Len Brown144b44b2013-11-09 00:30:16 -05002101 do_slm_cstates = is_slm(family, model);
Len Brown103a8fe2010-10-22 23:53:03 -04002102 bclk = discover_bclk(family, model);
2103
2104 do_nehalem_turbo_ratio_limit = has_nehalem_turbo_ratio_limit(family, model);
Len Brown6574a5d2012-09-21 00:01:31 -04002105 do_ivt_turbo_ratio_limit = has_ivt_turbo_ratio_limit(family, model);
Len Brown889facb2012-11-08 00:48:57 -05002106 rapl_probe(family, model);
2107
2108 return;
Len Brown103a8fe2010-10-22 23:53:03 -04002109}
2110
2111
2112void usage()
2113{
Andy Shevchenkof591c382014-01-23 17:13:14 +02002114 errx(1, "%s: [-v][-R][-T][-p|-P|-S][-c MSR#][-C MSR#][-m MSR#][-M MSR#][-i interval_sec | command ...]\n",
Josh Triplettb2c95d92013-08-20 17:20:18 -07002115 progname);
Len Brown103a8fe2010-10-22 23:53:03 -04002116}
2117
2118
2119/*
2120 * in /dev/cpu/ return success for names that are numbers
2121 * ie. filter out ".", "..", "microcode".
2122 */
2123int dir_filter(const struct dirent *dirp)
2124{
2125 if (isdigit(dirp->d_name[0]))
2126 return 1;
2127 else
2128 return 0;
2129}
2130
2131int open_dev_cpu_msr(int dummy1)
2132{
2133 return 0;
2134}
2135
Len Brownc98d5d92012-06-04 00:56:40 -04002136void topology_probe()
2137{
2138 int i;
2139 int max_core_id = 0;
2140 int max_package_id = 0;
2141 int max_siblings = 0;
2142 struct cpu_topology {
2143 int core_id;
2144 int physical_package_id;
2145 } *cpus;
2146
2147 /* Initialize num_cpus, max_cpu_num */
2148 topo.num_cpus = 0;
2149 topo.max_cpu_num = 0;
2150 for_all_proc_cpus(count_cpus);
2151 if (!summary_only && topo.num_cpus > 1)
2152 show_cpu = 1;
2153
2154 if (verbose > 1)
2155 fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
2156
2157 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
Josh Triplettb2c95d92013-08-20 17:20:18 -07002158 if (cpus == NULL)
2159 err(1, "calloc cpus");
Len Brownc98d5d92012-06-04 00:56:40 -04002160
2161 /*
2162 * Allocate and initialize cpu_present_set
2163 */
2164 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07002165 if (cpu_present_set == NULL)
2166 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04002167 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2168 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
2169 for_all_proc_cpus(mark_cpu_present);
2170
2171 /*
2172 * Allocate and initialize cpu_affinity_set
2173 */
2174 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07002175 if (cpu_affinity_set == NULL)
2176 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04002177 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2178 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
2179
2180
2181 /*
2182 * For online cpus
2183 * find max_core_id, max_package_id
2184 */
2185 for (i = 0; i <= topo.max_cpu_num; ++i) {
2186 int siblings;
2187
2188 if (cpu_is_not_present(i)) {
2189 if (verbose > 1)
2190 fprintf(stderr, "cpu%d NOT PRESENT\n", i);
2191 continue;
2192 }
2193 cpus[i].core_id = get_core_id(i);
2194 if (cpus[i].core_id > max_core_id)
2195 max_core_id = cpus[i].core_id;
2196
2197 cpus[i].physical_package_id = get_physical_package_id(i);
2198 if (cpus[i].physical_package_id > max_package_id)
2199 max_package_id = cpus[i].physical_package_id;
2200
2201 siblings = get_num_ht_siblings(i);
2202 if (siblings > max_siblings)
2203 max_siblings = siblings;
2204 if (verbose > 1)
2205 fprintf(stderr, "cpu %d pkg %d core %d\n",
2206 i, cpus[i].physical_package_id, cpus[i].core_id);
2207 }
2208 topo.num_cores_per_pkg = max_core_id + 1;
2209 if (verbose > 1)
2210 fprintf(stderr, "max_core_id %d, sizing for %d cores per package\n",
2211 max_core_id, topo.num_cores_per_pkg);
2212 if (!summary_only && topo.num_cores_per_pkg > 1)
2213 show_core = 1;
2214
2215 topo.num_packages = max_package_id + 1;
2216 if (verbose > 1)
2217 fprintf(stderr, "max_package_id %d, sizing for %d packages\n",
2218 max_package_id, topo.num_packages);
2219 if (!summary_only && topo.num_packages > 1)
2220 show_pkg = 1;
2221
2222 topo.num_threads_per_core = max_siblings;
2223 if (verbose > 1)
2224 fprintf(stderr, "max_siblings %d\n", max_siblings);
2225
2226 free(cpus);
2227}
2228
2229void
2230allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
2231{
2232 int i;
2233
2234 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
2235 topo.num_packages, sizeof(struct thread_data));
2236 if (*t == NULL)
2237 goto error;
2238
2239 for (i = 0; i < topo.num_threads_per_core *
2240 topo.num_cores_per_pkg * topo.num_packages; i++)
2241 (*t)[i].cpu_id = -1;
2242
2243 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
2244 sizeof(struct core_data));
2245 if (*c == NULL)
2246 goto error;
2247
2248 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
2249 (*c)[i].core_id = -1;
2250
2251 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
2252 if (*p == NULL)
2253 goto error;
2254
2255 for (i = 0; i < topo.num_packages; i++)
2256 (*p)[i].package_id = i;
2257
2258 return;
2259error:
Josh Triplettb2c95d92013-08-20 17:20:18 -07002260 err(1, "calloc counters");
Len Brownc98d5d92012-06-04 00:56:40 -04002261}
2262/*
2263 * init_counter()
2264 *
2265 * set cpu_id, core_num, pkg_num
2266 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
2267 *
2268 * increment topo.num_cores when 1st core in pkg seen
2269 */
2270void init_counter(struct thread_data *thread_base, struct core_data *core_base,
2271 struct pkg_data *pkg_base, int thread_num, int core_num,
2272 int pkg_num, int cpu_id)
2273{
2274 struct thread_data *t;
2275 struct core_data *c;
2276 struct pkg_data *p;
2277
2278 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
2279 c = GET_CORE(core_base, core_num, pkg_num);
2280 p = GET_PKG(pkg_base, pkg_num);
2281
2282 t->cpu_id = cpu_id;
2283 if (thread_num == 0) {
2284 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
2285 if (cpu_is_first_core_in_package(cpu_id))
2286 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
2287 }
2288
2289 c->core_id = core_num;
2290 p->package_id = pkg_num;
2291}
2292
2293
2294int initialize_counters(int cpu_id)
2295{
2296 int my_thread_id, my_core_id, my_package_id;
2297
2298 my_package_id = get_physical_package_id(cpu_id);
2299 my_core_id = get_core_id(cpu_id);
2300
2301 if (cpu_is_first_sibling_in_core(cpu_id)) {
2302 my_thread_id = 0;
2303 topo.num_cores++;
2304 } else {
2305 my_thread_id = 1;
2306 }
2307
2308 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2309 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2310 return 0;
2311}
2312
2313void allocate_output_buffer()
2314{
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02002315 output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
Len Brownc98d5d92012-06-04 00:56:40 -04002316 outp = output_buffer;
Josh Triplettb2c95d92013-08-20 17:20:18 -07002317 if (outp == NULL)
2318 err(-1, "calloc output buffer");
Len Brownc98d5d92012-06-04 00:56:40 -04002319}
2320
2321void setup_all_buffers(void)
2322{
2323 topology_probe();
2324 allocate_counters(&thread_even, &core_even, &package_even);
2325 allocate_counters(&thread_odd, &core_odd, &package_odd);
2326 allocate_output_buffer();
2327 for_all_proc_cpus(initialize_counters);
2328}
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02002329
Len Brown103a8fe2010-10-22 23:53:03 -04002330void turbostat_init()
2331{
Len Brown103a8fe2010-10-22 23:53:03 -04002332 check_dev_msr();
Len Brown98481e72014-08-15 00:36:50 -04002333 check_permissions();
2334 check_cpuid();
Len Brown103a8fe2010-10-22 23:53:03 -04002335
Len Brownc98d5d92012-06-04 00:56:40 -04002336 setup_all_buffers();
Len Brown103a8fe2010-10-22 23:53:03 -04002337
2338 if (verbose)
Len Brownc98d5d92012-06-04 00:56:40 -04002339 print_verbose_header();
Len Brown889facb2012-11-08 00:48:57 -05002340
2341 if (verbose)
2342 for_all_cpus(print_epb, ODD_COUNTERS);
2343
2344 if (verbose)
2345 for_all_cpus(print_rapl, ODD_COUNTERS);
2346
2347 for_all_cpus(set_temperature_target, ODD_COUNTERS);
2348
2349 if (verbose)
2350 for_all_cpus(print_thermal, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04002351}
2352
2353int fork_it(char **argv)
2354{
Len Brown103a8fe2010-10-22 23:53:03 -04002355 pid_t child_pid;
Len Brownd91bb172012-11-01 00:08:19 -04002356 int status;
Len Brownd15cf7c2012-06-03 23:24:00 -04002357
Len Brownd91bb172012-11-01 00:08:19 -04002358 status = for_all_cpus(get_counters, EVEN_COUNTERS);
2359 if (status)
2360 exit(status);
Len Brownc98d5d92012-06-04 00:56:40 -04002361 /* clear affinity side-effect of get_counters() */
2362 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
Len Brown103a8fe2010-10-22 23:53:03 -04002363 gettimeofday(&tv_even, (struct timezone *)NULL);
2364
2365 child_pid = fork();
2366 if (!child_pid) {
2367 /* child */
2368 execvp(argv[0], argv);
2369 } else {
Len Brown103a8fe2010-10-22 23:53:03 -04002370
2371 /* parent */
Josh Triplettb2c95d92013-08-20 17:20:18 -07002372 if (child_pid == -1)
2373 err(1, "fork");
Len Brown103a8fe2010-10-22 23:53:03 -04002374
2375 signal(SIGINT, SIG_IGN);
2376 signal(SIGQUIT, SIG_IGN);
Josh Triplettb2c95d92013-08-20 17:20:18 -07002377 if (waitpid(child_pid, &status, 0) == -1)
2378 err(status, "waitpid");
Len Brown103a8fe2010-10-22 23:53:03 -04002379 }
Len Brownc98d5d92012-06-04 00:56:40 -04002380 /*
2381 * n.b. fork_it() does not check for errors from for_all_cpus()
2382 * because re-starting is problematic when forking
2383 */
2384 for_all_cpus(get_counters, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04002385 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002386 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04002387 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
2388 compute_average(EVEN_COUNTERS);
2389 format_all_counters(EVEN_COUNTERS);
2390 flush_stderr();
Len Brown103a8fe2010-10-22 23:53:03 -04002391
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07002392 fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
Len Brown103a8fe2010-10-22 23:53:03 -04002393
Len Brownd91bb172012-11-01 00:08:19 -04002394 return status;
Len Brown103a8fe2010-10-22 23:53:03 -04002395}
2396
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02002397int get_and_dump_counters(void)
2398{
2399 int status;
2400
2401 status = for_all_cpus(get_counters, ODD_COUNTERS);
2402 if (status)
2403 return status;
2404
2405 status = for_all_cpus(dump_counters, ODD_COUNTERS);
2406 if (status)
2407 return status;
2408
2409 flush_stdout();
2410
2411 return status;
2412}
2413
Len Brown103a8fe2010-10-22 23:53:03 -04002414void cmdline(int argc, char **argv)
2415{
2416 int opt;
2417
2418 progname = argv[0];
2419
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02002420 while ((opt = getopt(argc, argv, "+pPsSvi:c:C:m:M:RJT:")) != -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04002421 switch (opt) {
Len Brownf9240812012-10-06 15:26:31 -04002422 case 'p':
Len Brownc98d5d92012-06-04 00:56:40 -04002423 show_core_only++;
2424 break;
Len Brownf9240812012-10-06 15:26:31 -04002425 case 'P':
Len Brownc98d5d92012-06-04 00:56:40 -04002426 show_pkg_only++;
2427 break;
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02002428 case 's':
2429 dump_only++;
2430 break;
Len Brownf9240812012-10-06 15:26:31 -04002431 case 'S':
Len Browne23da032012-02-06 18:37:16 -05002432 summary_only++;
2433 break;
Len Brown103a8fe2010-10-22 23:53:03 -04002434 case 'v':
2435 verbose++;
2436 break;
2437 case 'i':
2438 interval_sec = atoi(optarg);
2439 break;
Len Brownf9240812012-10-06 15:26:31 -04002440 case 'c':
Len Brown8e180f32012-09-22 01:25:08 -04002441 sscanf(optarg, "%x", &extra_delta_offset32);
2442 break;
Len Brownf9240812012-10-06 15:26:31 -04002443 case 'C':
Len Brown8e180f32012-09-22 01:25:08 -04002444 sscanf(optarg, "%x", &extra_delta_offset64);
2445 break;
Len Brown2f32edf2012-09-21 23:45:46 -04002446 case 'm':
2447 sscanf(optarg, "%x", &extra_msr_offset32);
Len Brown2f32edf2012-09-21 23:45:46 -04002448 break;
2449 case 'M':
2450 sscanf(optarg, "%x", &extra_msr_offset64);
Len Brown103a8fe2010-10-22 23:53:03 -04002451 break;
Len Brown889facb2012-11-08 00:48:57 -05002452 case 'R':
2453 rapl_verbose++;
2454 break;
2455 case 'T':
2456 tcc_activation_temp_override = atoi(optarg);
2457 break;
Dirk Brandewie5c56be92013-12-16 10:23:41 -08002458 case 'J':
2459 rapl_joules++;
2460 break;
2461
Len Brown103a8fe2010-10-22 23:53:03 -04002462 default:
2463 usage();
2464 }
2465 }
2466}
2467
2468int main(int argc, char **argv)
2469{
2470 cmdline(argc, argv);
2471
Len Brown889facb2012-11-08 00:48:57 -05002472 if (verbose)
Len Brown98481e72014-08-15 00:36:50 -04002473 fprintf(stderr, "turbostat v3.8 14-Aug 2014"
Len Brown103a8fe2010-10-22 23:53:03 -04002474 " - Len Brown <lenb@kernel.org>\n");
Len Brown103a8fe2010-10-22 23:53:03 -04002475
2476 turbostat_init();
2477
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02002478 /* dump counters and exit */
2479 if (dump_only)
2480 return get_and_dump_counters();
2481
Len Brown103a8fe2010-10-22 23:53:03 -04002482 /*
2483 * if any params left, it must be a command to fork
2484 */
2485 if (argc - optind)
2486 return fork_it(argv + optind);
2487 else
2488 turbostat_loop();
2489
2490 return 0;
2491}