blob: a02c02f25e88a273ac92ed7b1b1a2d1bf801a17c [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;
Len Brownd7899442015-01-23 00:12:33 -050064unsigned int do_nhm_platform_info;
65unsigned int do_nhm_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;
Len Brown3a9a9412014-08-15 02:39:52 -040086unsigned int do_core_perf_limit_reasons;
87unsigned int do_gfx_perf_limit_reasons;
88unsigned int do_ring_perf_limit_reasons;
Len Brown889facb2012-11-08 00:48:57 -050089
Len Browne6f9bb32013-12-03 02:19:19 -050090#define RAPL_PKG (1 << 0)
91 /* 0x610 MSR_PKG_POWER_LIMIT */
92 /* 0x611 MSR_PKG_ENERGY_STATUS */
93#define RAPL_PKG_PERF_STATUS (1 << 1)
94 /* 0x613 MSR_PKG_PERF_STATUS */
95#define RAPL_PKG_POWER_INFO (1 << 2)
96 /* 0x614 MSR_PKG_POWER_INFO */
97
98#define RAPL_DRAM (1 << 3)
99 /* 0x618 MSR_DRAM_POWER_LIMIT */
100 /* 0x619 MSR_DRAM_ENERGY_STATUS */
101 /* 0x61c MSR_DRAM_POWER_INFO */
102#define RAPL_DRAM_PERF_STATUS (1 << 4)
103 /* 0x61b MSR_DRAM_PERF_STATUS */
104
105#define RAPL_CORES (1 << 5)
106 /* 0x638 MSR_PP0_POWER_LIMIT */
107 /* 0x639 MSR_PP0_ENERGY_STATUS */
108#define RAPL_CORE_POLICY (1 << 6)
109 /* 0x63a MSR_PP0_POLICY */
110
111
112#define RAPL_GFX (1 << 7)
113 /* 0x640 MSR_PP1_POWER_LIMIT */
114 /* 0x641 MSR_PP1_ENERGY_STATUS */
115 /* 0x642 MSR_PP1_POLICY */
Len Brown889facb2012-11-08 00:48:57 -0500116#define TJMAX_DEFAULT 100
117
118#define MAX(a, b) ((a) > (b) ? (a) : (b))
Len Brown103a8fe2010-10-22 23:53:03 -0400119
120int aperf_mperf_unstable;
121int backwards_count;
122char *progname;
Len Brown103a8fe2010-10-22 23:53:03 -0400123
Len Brownc98d5d92012-06-04 00:56:40 -0400124cpu_set_t *cpu_present_set, *cpu_affinity_set;
125size_t cpu_present_setsize, cpu_affinity_setsize;
Len Brown103a8fe2010-10-22 23:53:03 -0400126
Len Brownc98d5d92012-06-04 00:56:40 -0400127struct thread_data {
128 unsigned long long tsc;
129 unsigned long long aperf;
130 unsigned long long mperf;
Len Brown144b44b2013-11-09 00:30:16 -0500131 unsigned long long c1;
Len Brown2f32edf2012-09-21 23:45:46 -0400132 unsigned long long extra_msr64;
Len Brown8e180f32012-09-22 01:25:08 -0400133 unsigned long long extra_delta64;
134 unsigned long long extra_msr32;
135 unsigned long long extra_delta32;
Len Brown1ed51012013-02-10 17:19:24 -0500136 unsigned int smi_count;
Len Brownc98d5d92012-06-04 00:56:40 -0400137 unsigned int cpu_id;
138 unsigned int flags;
139#define CPU_IS_FIRST_THREAD_IN_CORE 0x2
140#define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
141} *thread_even, *thread_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400142
Len Brownc98d5d92012-06-04 00:56:40 -0400143struct core_data {
144 unsigned long long c3;
145 unsigned long long c6;
146 unsigned long long c7;
Len Brown889facb2012-11-08 00:48:57 -0500147 unsigned int core_temp_c;
Len Brownc98d5d92012-06-04 00:56:40 -0400148 unsigned int core_id;
149} *core_even, *core_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400150
Len Brownc98d5d92012-06-04 00:56:40 -0400151struct pkg_data {
152 unsigned long long pc2;
153 unsigned long long pc3;
154 unsigned long long pc6;
155 unsigned long long pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800156 unsigned long long pc8;
157 unsigned long long pc9;
158 unsigned long long pc10;
Len Brownc98d5d92012-06-04 00:56:40 -0400159 unsigned int package_id;
Len Brown889facb2012-11-08 00:48:57 -0500160 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
161 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
162 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
163 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
164 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
165 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
166 unsigned int pkg_temp_c;
167
Len Brownc98d5d92012-06-04 00:56:40 -0400168} *package_even, *package_odd;
169
170#define ODD_COUNTERS thread_odd, core_odd, package_odd
171#define EVEN_COUNTERS thread_even, core_even, package_even
172
173#define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
174 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
175 topo.num_threads_per_core + \
176 (core_no) * topo.num_threads_per_core + (thread_no))
177#define GET_CORE(core_base, core_no, pkg_no) \
178 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
179#define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
180
181struct system_summary {
182 struct thread_data threads;
183 struct core_data cores;
184 struct pkg_data packages;
185} sum, average;
186
187
188struct topo_params {
189 int num_packages;
190 int num_cpus;
191 int num_cores;
192 int max_cpu_num;
193 int num_cores_per_pkg;
194 int num_threads_per_core;
195} topo;
196
197struct timeval tv_even, tv_odd, tv_delta;
198
199void setup_all_buffers(void);
200
201int cpu_is_not_present(int cpu)
Len Brownd15cf7c2012-06-03 23:24:00 -0400202{
Len Brownc98d5d92012-06-04 00:56:40 -0400203 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brownd15cf7c2012-06-03 23:24:00 -0400204}
Len Brown88c32812012-03-29 21:44:40 -0400205/*
Len Brownc98d5d92012-06-04 00:56:40 -0400206 * run func(thread, core, package) in topology order
207 * skip non-present cpus
Len Brown88c32812012-03-29 21:44:40 -0400208 */
Len Brownd15cf7c2012-06-03 23:24:00 -0400209
Len Brownc98d5d92012-06-04 00:56:40 -0400210int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
211 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
Len Brown88c32812012-03-29 21:44:40 -0400212{
Len Brownc98d5d92012-06-04 00:56:40 -0400213 int retval, pkg_no, core_no, thread_no;
214
215 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
216 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
217 for (thread_no = 0; thread_no <
218 topo.num_threads_per_core; ++thread_no) {
219 struct thread_data *t;
220 struct core_data *c;
221 struct pkg_data *p;
222
223 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
224
225 if (cpu_is_not_present(t->cpu_id))
226 continue;
227
228 c = GET_CORE(core_base, core_no, pkg_no);
229 p = GET_PKG(pkg_base, pkg_no);
230
231 retval = func(t, c, p);
232 if (retval)
233 return retval;
234 }
235 }
236 }
237 return 0;
Len Brown88c32812012-03-29 21:44:40 -0400238}
239
240int cpu_migrate(int cpu)
241{
Len Brownc98d5d92012-06-04 00:56:40 -0400242 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
243 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
244 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
Len Brown88c32812012-03-29 21:44:40 -0400245 return -1;
246 else
247 return 0;
248}
249
Len Brown15aaa342012-03-29 22:19:58 -0400250int get_msr(int cpu, off_t offset, unsigned long long *msr)
Len Brown103a8fe2010-10-22 23:53:03 -0400251{
252 ssize_t retval;
Len Brown103a8fe2010-10-22 23:53:03 -0400253 char pathname[32];
254 int fd;
255
256 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
257 fd = open(pathname, O_RDONLY);
Len Brown15aaa342012-03-29 22:19:58 -0400258 if (fd < 0)
Len Brown98481e72014-08-15 00:36:50 -0400259 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 -0400260
Len Brown15aaa342012-03-29 22:19:58 -0400261 retval = pread(fd, msr, sizeof *msr, offset);
Len Brown103a8fe2010-10-22 23:53:03 -0400262 close(fd);
Len Brown15aaa342012-03-29 22:19:58 -0400263
Len Brown98481e72014-08-15 00:36:50 -0400264 if (retval != sizeof *msr)
265 err(-1, "%s offset 0x%llx read failed", pathname, (unsigned long long)offset);
Len Brown15aaa342012-03-29 22:19:58 -0400266
267 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400268}
269
Len Brownfc04cc62014-02-06 00:55:19 -0500270/*
271 * Example Format w/ field column widths:
272 *
Len Browne7c95ff2014-08-14 21:22:13 -0400273 * 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
274 * 123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678
Len Brownfc04cc62014-02-06 00:55:19 -0500275 */
276
Len Browna829eb42011-02-10 23:36:34 -0500277void print_header(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400278{
279 if (show_pkg)
Len Browne7c95ff2014-08-14 21:22:13 -0400280 outp += sprintf(outp, " Package");
Len Brown103a8fe2010-10-22 23:53:03 -0400281 if (show_core)
Len Browne7c95ff2014-08-14 21:22:13 -0400282 outp += sprintf(outp, " Core");
Len Brown103a8fe2010-10-22 23:53:03 -0400283 if (show_cpu)
Len Browne7c95ff2014-08-14 21:22:13 -0400284 outp += sprintf(outp, " CPU");
Len Brown103a8fe2010-10-22 23:53:03 -0400285 if (has_aperf)
Len Browne7c95ff2014-08-14 21:22:13 -0400286 outp += sprintf(outp, " Avg_MHz");
Len Brownd7899442015-01-23 00:12:33 -0500287 if (has_aperf)
Len Browne7c95ff2014-08-14 21:22:13 -0400288 outp += sprintf(outp, " %%Busy");
Len Brownfc04cc62014-02-06 00:55:19 -0500289 if (has_aperf)
Len Browne7c95ff2014-08-14 21:22:13 -0400290 outp += sprintf(outp, " Bzy_MHz");
291 outp += sprintf(outp, " TSC_MHz");
Len Brown1ed51012013-02-10 17:19:24 -0500292 if (do_smi)
Len Browne7c95ff2014-08-14 21:22:13 -0400293 outp += sprintf(outp, " SMI");
Len Brown8e180f32012-09-22 01:25:08 -0400294 if (extra_delta_offset32)
Len Browne7c95ff2014-08-14 21:22:13 -0400295 outp += sprintf(outp, " count 0x%03X", extra_delta_offset32);
Len Brown8e180f32012-09-22 01:25:08 -0400296 if (extra_delta_offset64)
Len Browne7c95ff2014-08-14 21:22:13 -0400297 outp += sprintf(outp, " COUNT 0x%03X", extra_delta_offset64);
Len Brown2f32edf2012-09-21 23:45:46 -0400298 if (extra_msr_offset32)
Len Browne7c95ff2014-08-14 21:22:13 -0400299 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset32);
Len Brown2f32edf2012-09-21 23:45:46 -0400300 if (extra_msr_offset64)
Len Browne7c95ff2014-08-14 21:22:13 -0400301 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset64);
Len Brown103a8fe2010-10-22 23:53:03 -0400302 if (do_nhm_cstates)
Len Browne7c95ff2014-08-14 21:22:13 -0400303 outp += sprintf(outp, " CPU%%c1");
Len Brown144b44b2013-11-09 00:30:16 -0500304 if (do_nhm_cstates && !do_slm_cstates)
Len Browne7c95ff2014-08-14 21:22:13 -0400305 outp += sprintf(outp, " CPU%%c3");
Len Brown103a8fe2010-10-22 23:53:03 -0400306 if (do_nhm_cstates)
Len Browne7c95ff2014-08-14 21:22:13 -0400307 outp += sprintf(outp, " CPU%%c6");
Len Brown103a8fe2010-10-22 23:53:03 -0400308 if (do_snb_cstates)
Len Browne7c95ff2014-08-14 21:22:13 -0400309 outp += sprintf(outp, " CPU%%c7");
Len Brown889facb2012-11-08 00:48:57 -0500310
311 if (do_dts)
Len Browne7c95ff2014-08-14 21:22:13 -0400312 outp += sprintf(outp, " CoreTmp");
Len Brown889facb2012-11-08 00:48:57 -0500313 if (do_ptm)
Len Browne7c95ff2014-08-14 21:22:13 -0400314 outp += sprintf(outp, " PkgTmp");
Len Brown889facb2012-11-08 00:48:57 -0500315
Len Brown103a8fe2010-10-22 23:53:03 -0400316 if (do_snb_cstates)
Len Browne7c95ff2014-08-14 21:22:13 -0400317 outp += sprintf(outp, " Pkg%%pc2");
Len Brown144b44b2013-11-09 00:30:16 -0500318 if (do_nhm_cstates && !do_slm_cstates)
Len Browne7c95ff2014-08-14 21:22:13 -0400319 outp += sprintf(outp, " Pkg%%pc3");
Len Brown144b44b2013-11-09 00:30:16 -0500320 if (do_nhm_cstates && !do_slm_cstates)
Len Browne7c95ff2014-08-14 21:22:13 -0400321 outp += sprintf(outp, " Pkg%%pc6");
Len Brown103a8fe2010-10-22 23:53:03 -0400322 if (do_snb_cstates)
Len Browne7c95ff2014-08-14 21:22:13 -0400323 outp += sprintf(outp, " Pkg%%pc7");
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800324 if (do_c8_c9_c10) {
Len Browne7c95ff2014-08-14 21:22:13 -0400325 outp += sprintf(outp, " Pkg%%pc8");
326 outp += sprintf(outp, " Pkg%%pc9");
327 outp += sprintf(outp, " Pk%%pc10");
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800328 }
Len Brown103a8fe2010-10-22 23:53:03 -0400329
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800330 if (do_rapl && !rapl_joules) {
331 if (do_rapl & RAPL_PKG)
Len Browne7c95ff2014-08-14 21:22:13 -0400332 outp += sprintf(outp, " PkgWatt");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800333 if (do_rapl & RAPL_CORES)
Len Browne7c95ff2014-08-14 21:22:13 -0400334 outp += sprintf(outp, " CorWatt");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800335 if (do_rapl & RAPL_GFX)
Len Browne7c95ff2014-08-14 21:22:13 -0400336 outp += sprintf(outp, " GFXWatt");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800337 if (do_rapl & RAPL_DRAM)
Len Browne7c95ff2014-08-14 21:22:13 -0400338 outp += sprintf(outp, " RAMWatt");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800339 if (do_rapl & RAPL_PKG_PERF_STATUS)
Len Browne7c95ff2014-08-14 21:22:13 -0400340 outp += sprintf(outp, " PKG_%%");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800341 if (do_rapl & RAPL_DRAM_PERF_STATUS)
Len Browne7c95ff2014-08-14 21:22:13 -0400342 outp += sprintf(outp, " RAM_%%");
Len Brownd7899442015-01-23 00:12:33 -0500343 } else if (do_rapl && rapl_joules) {
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800344 if (do_rapl & RAPL_PKG)
Len Browne7c95ff2014-08-14 21:22:13 -0400345 outp += sprintf(outp, " Pkg_J");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800346 if (do_rapl & RAPL_CORES)
Len Browne7c95ff2014-08-14 21:22:13 -0400347 outp += sprintf(outp, " Cor_J");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800348 if (do_rapl & RAPL_GFX)
Len Browne7c95ff2014-08-14 21:22:13 -0400349 outp += sprintf(outp, " GFX_J");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800350 if (do_rapl & RAPL_DRAM)
Len Browne7c95ff2014-08-14 21:22:13 -0400351 outp += sprintf(outp, " RAM_W");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800352 if (do_rapl & RAPL_PKG_PERF_STATUS)
Len Browne7c95ff2014-08-14 21:22:13 -0400353 outp += sprintf(outp, " PKG_%%");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800354 if (do_rapl & RAPL_DRAM_PERF_STATUS)
Len Browne7c95ff2014-08-14 21:22:13 -0400355 outp += sprintf(outp, " RAM_%%");
356 outp += sprintf(outp, " time");
Len Brown889facb2012-11-08 00:48:57 -0500357
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800358 }
Len Brownc98d5d92012-06-04 00:56:40 -0400359 outp += sprintf(outp, "\n");
Len Brown103a8fe2010-10-22 23:53:03 -0400360}
361
Len Brownc98d5d92012-06-04 00:56:40 -0400362int dump_counters(struct thread_data *t, struct core_data *c,
363 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400364{
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200365 outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400366
Len Brownc98d5d92012-06-04 00:56:40 -0400367 if (t) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200368 outp += sprintf(outp, "CPU: %d flags 0x%x\n",
369 t->cpu_id, t->flags);
370 outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
371 outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
372 outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
373 outp += sprintf(outp, "c1: %016llX\n", t->c1);
374 outp += sprintf(outp, "msr0x%x: %08llX\n",
Len Brown8e180f32012-09-22 01:25:08 -0400375 extra_delta_offset32, t->extra_delta32);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200376 outp += sprintf(outp, "msr0x%x: %016llX\n",
Len Brown8e180f32012-09-22 01:25:08 -0400377 extra_delta_offset64, t->extra_delta64);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200378 outp += sprintf(outp, "msr0x%x: %08llX\n",
Len Brown2f32edf2012-09-21 23:45:46 -0400379 extra_msr_offset32, t->extra_msr32);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200380 outp += sprintf(outp, "msr0x%x: %016llX\n",
Len Brown2f32edf2012-09-21 23:45:46 -0400381 extra_msr_offset64, t->extra_msr64);
Len Brown1ed51012013-02-10 17:19:24 -0500382 if (do_smi)
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200383 outp += sprintf(outp, "SMI: %08X\n", t->smi_count);
Len Brownc98d5d92012-06-04 00:56:40 -0400384 }
Len Brown103a8fe2010-10-22 23:53:03 -0400385
Len Brownc98d5d92012-06-04 00:56:40 -0400386 if (c) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200387 outp += sprintf(outp, "core: %d\n", c->core_id);
388 outp += sprintf(outp, "c3: %016llX\n", c->c3);
389 outp += sprintf(outp, "c6: %016llX\n", c->c6);
390 outp += sprintf(outp, "c7: %016llX\n", c->c7);
391 outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
Len Brownc98d5d92012-06-04 00:56:40 -0400392 }
393
394 if (p) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200395 outp += sprintf(outp, "package: %d\n", p->package_id);
396 outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
397 outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
398 outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
399 outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
400 outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
401 outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
402 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
403 outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
404 outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
405 outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
406 outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
407 outp += sprintf(outp, "Throttle PKG: %0X\n",
408 p->rapl_pkg_perf_status);
409 outp += sprintf(outp, "Throttle RAM: %0X\n",
410 p->rapl_dram_perf_status);
411 outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
Len Brownc98d5d92012-06-04 00:56:40 -0400412 }
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200413
414 outp += sprintf(outp, "\n");
415
Len Brownc98d5d92012-06-04 00:56:40 -0400416 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400417}
418
Len Browne23da032012-02-06 18:37:16 -0500419/*
420 * column formatting convention & formats
Len Browne23da032012-02-06 18:37:16 -0500421 */
Len Brownc98d5d92012-06-04 00:56:40 -0400422int format_counters(struct thread_data *t, struct core_data *c,
423 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400424{
425 double interval_float;
Len Brownfc04cc62014-02-06 00:55:19 -0500426 char *fmt8;
Len Brown103a8fe2010-10-22 23:53:03 -0400427
Len Brownc98d5d92012-06-04 00:56:40 -0400428 /* if showing only 1st thread in core and this isn't one, bail out */
429 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
430 return 0;
431
432 /* if showing only 1st thread in pkg and this isn't one, bail out */
433 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
434 return 0;
435
Len Brown103a8fe2010-10-22 23:53:03 -0400436 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
437
Len Brownc98d5d92012-06-04 00:56:40 -0400438 /* topo columns, print blanks on 1st (average) line */
439 if (t == &average.threads) {
Len Brown103a8fe2010-10-22 23:53:03 -0400440 if (show_pkg)
Len Brownfc04cc62014-02-06 00:55:19 -0500441 outp += sprintf(outp, " -");
Len Brown103a8fe2010-10-22 23:53:03 -0400442 if (show_core)
Len Brownfc04cc62014-02-06 00:55:19 -0500443 outp += sprintf(outp, " -");
Len Brown103a8fe2010-10-22 23:53:03 -0400444 if (show_cpu)
Len Brownfc04cc62014-02-06 00:55:19 -0500445 outp += sprintf(outp, " -");
Len Brown103a8fe2010-10-22 23:53:03 -0400446 } else {
Len Brownc98d5d92012-06-04 00:56:40 -0400447 if (show_pkg) {
448 if (p)
Len Brownfc04cc62014-02-06 00:55:19 -0500449 outp += sprintf(outp, "%8d", p->package_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400450 else
Len Brownfc04cc62014-02-06 00:55:19 -0500451 outp += sprintf(outp, " -");
Len Brownc98d5d92012-06-04 00:56:40 -0400452 }
Len Brownc98d5d92012-06-04 00:56:40 -0400453 if (show_core) {
454 if (c)
Len Brownfc04cc62014-02-06 00:55:19 -0500455 outp += sprintf(outp, "%8d", c->core_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400456 else
Len Brownfc04cc62014-02-06 00:55:19 -0500457 outp += sprintf(outp, " -");
Len Brownc98d5d92012-06-04 00:56:40 -0400458 }
Len Brown103a8fe2010-10-22 23:53:03 -0400459 if (show_cpu)
Len Brownfc04cc62014-02-06 00:55:19 -0500460 outp += sprintf(outp, "%8d", t->cpu_id);
Len Brown103a8fe2010-10-22 23:53:03 -0400461 }
Len Brownfc04cc62014-02-06 00:55:19 -0500462
Len Brownd7899442015-01-23 00:12:33 -0500463 /* Avg_MHz */
Len Brownfc04cc62014-02-06 00:55:19 -0500464 if (has_aperf)
465 outp += sprintf(outp, "%8.0f",
466 1.0 / units * t->aperf / interval_float);
467
Len Brownd7899442015-01-23 00:12:33 -0500468 /* %Busy */
469 if (has_aperf) {
Len Brown103a8fe2010-10-22 23:53:03 -0400470 if (!skip_c0)
Len Brownfc04cc62014-02-06 00:55:19 -0500471 outp += sprintf(outp, "%8.2f", 100.0 * t->mperf/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400472 else
Len Brownfc04cc62014-02-06 00:55:19 -0500473 outp += sprintf(outp, "********");
Len Brown103a8fe2010-10-22 23:53:03 -0400474 }
475
Len Brownd7899442015-01-23 00:12:33 -0500476 /* Bzy_MHz */
Len Brownfc04cc62014-02-06 00:55:19 -0500477 if (has_aperf)
478 outp += sprintf(outp, "%8.0f",
479 1.0 * t->tsc / units * t->aperf / t->mperf / interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400480
Len Brownd7899442015-01-23 00:12:33 -0500481 /* TSC_MHz */
Len Brownfc04cc62014-02-06 00:55:19 -0500482 outp += sprintf(outp, "%8.0f", 1.0 * t->tsc/units/interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400483
Len Brown1ed51012013-02-10 17:19:24 -0500484 /* SMI */
485 if (do_smi)
Len Brownfc04cc62014-02-06 00:55:19 -0500486 outp += sprintf(outp, "%8d", t->smi_count);
Len Brown1ed51012013-02-10 17:19:24 -0500487
Len Brown8e180f32012-09-22 01:25:08 -0400488 /* delta */
489 if (extra_delta_offset32)
490 outp += sprintf(outp, " %11llu", t->extra_delta32);
491
492 /* DELTA */
493 if (extra_delta_offset64)
494 outp += sprintf(outp, " %11llu", t->extra_delta64);
Len Brown2f32edf2012-09-21 23:45:46 -0400495 /* msr */
496 if (extra_msr_offset32)
Len Brown8e180f32012-09-22 01:25:08 -0400497 outp += sprintf(outp, " 0x%08llx", t->extra_msr32);
Len Brown2f32edf2012-09-21 23:45:46 -0400498
Len Brown130ff302012-09-21 22:56:06 -0400499 /* MSR */
Len Brown2f32edf2012-09-21 23:45:46 -0400500 if (extra_msr_offset64)
501 outp += sprintf(outp, " 0x%016llx", t->extra_msr64);
Len Brown130ff302012-09-21 22:56:06 -0400502
Len Brown103a8fe2010-10-22 23:53:03 -0400503 if (do_nhm_cstates) {
504 if (!skip_c1)
Len Brownfc04cc62014-02-06 00:55:19 -0500505 outp += sprintf(outp, "%8.2f", 100.0 * t->c1/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400506 else
Len Brownfc04cc62014-02-06 00:55:19 -0500507 outp += sprintf(outp, "********");
Len Brown103a8fe2010-10-22 23:53:03 -0400508 }
Len Brownc98d5d92012-06-04 00:56:40 -0400509
510 /* print per-core data only for 1st thread in core */
511 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
512 goto done;
513
Len Brown144b44b2013-11-09 00:30:16 -0500514 if (do_nhm_cstates && !do_slm_cstates)
Len Brownfc04cc62014-02-06 00:55:19 -0500515 outp += sprintf(outp, "%8.2f", 100.0 * c->c3/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400516 if (do_nhm_cstates)
Len Brownfc04cc62014-02-06 00:55:19 -0500517 outp += sprintf(outp, "%8.2f", 100.0 * c->c6/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400518 if (do_snb_cstates)
Len Brownfc04cc62014-02-06 00:55:19 -0500519 outp += sprintf(outp, "%8.2f", 100.0 * c->c7/t->tsc);
Len Brownc98d5d92012-06-04 00:56:40 -0400520
Len Brown889facb2012-11-08 00:48:57 -0500521 if (do_dts)
Len Brownfc04cc62014-02-06 00:55:19 -0500522 outp += sprintf(outp, "%8d", c->core_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500523
Len Brownc98d5d92012-06-04 00:56:40 -0400524 /* print per-package data only for 1st core in package */
525 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
526 goto done;
527
Len Brown889facb2012-11-08 00:48:57 -0500528 if (do_ptm)
Len Brownfc04cc62014-02-06 00:55:19 -0500529 outp += sprintf(outp, "%8d", p->pkg_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500530
Len Brown103a8fe2010-10-22 23:53:03 -0400531 if (do_snb_cstates)
Len Brownfc04cc62014-02-06 00:55:19 -0500532 outp += sprintf(outp, "%8.2f", 100.0 * p->pc2/t->tsc);
Len Brown144b44b2013-11-09 00:30:16 -0500533 if (do_nhm_cstates && !do_slm_cstates)
Len Brownfc04cc62014-02-06 00:55:19 -0500534 outp += sprintf(outp, "%8.2f", 100.0 * p->pc3/t->tsc);
Len Brown144b44b2013-11-09 00:30:16 -0500535 if (do_nhm_cstates && !do_slm_cstates)
Len Brownfc04cc62014-02-06 00:55:19 -0500536 outp += sprintf(outp, "%8.2f", 100.0 * p->pc6/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400537 if (do_snb_cstates)
Len Brownfc04cc62014-02-06 00:55:19 -0500538 outp += sprintf(outp, "%8.2f", 100.0 * p->pc7/t->tsc);
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800539 if (do_c8_c9_c10) {
Len Brownfc04cc62014-02-06 00:55:19 -0500540 outp += sprintf(outp, "%8.2f", 100.0 * p->pc8/t->tsc);
541 outp += sprintf(outp, "%8.2f", 100.0 * p->pc9/t->tsc);
542 outp += sprintf(outp, "%8.2f", 100.0 * p->pc10/t->tsc);
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800543 }
Len Brown889facb2012-11-08 00:48:57 -0500544
545 /*
546 * If measurement interval exceeds minimum RAPL Joule Counter range,
547 * indicate that results are suspect by printing "**" in fraction place.
548 */
Len Brownfc04cc62014-02-06 00:55:19 -0500549 if (interval_float < rapl_joule_counter_range)
550 fmt8 = "%8.2f";
551 else
552 fmt8 = " %6.0f**";
Len Brown889facb2012-11-08 00:48:57 -0500553
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800554 if (do_rapl && !rapl_joules) {
555 if (do_rapl & RAPL_PKG)
Len Brownfc04cc62014-02-06 00:55:19 -0500556 outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800557 if (do_rapl & RAPL_CORES)
Len Brownfc04cc62014-02-06 00:55:19 -0500558 outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800559 if (do_rapl & RAPL_GFX)
Len Brownfc04cc62014-02-06 00:55:19 -0500560 outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800561 if (do_rapl & RAPL_DRAM)
Len Brownfc04cc62014-02-06 00:55:19 -0500562 outp += sprintf(outp, fmt8, p->energy_dram * rapl_energy_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800563 if (do_rapl & RAPL_PKG_PERF_STATUS)
Len Brownfc04cc62014-02-06 00:55:19 -0500564 outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800565 if (do_rapl & RAPL_DRAM_PERF_STATUS)
Len Brownfc04cc62014-02-06 00:55:19 -0500566 outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
Len Brownd7899442015-01-23 00:12:33 -0500567 } else if (do_rapl && rapl_joules) {
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800568 if (do_rapl & RAPL_PKG)
Len Brownfc04cc62014-02-06 00:55:19 -0500569 outp += sprintf(outp, fmt8,
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800570 p->energy_pkg * rapl_energy_units);
571 if (do_rapl & RAPL_CORES)
Len Brownfc04cc62014-02-06 00:55:19 -0500572 outp += sprintf(outp, fmt8,
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800573 p->energy_cores * rapl_energy_units);
574 if (do_rapl & RAPL_GFX)
Len Brownfc04cc62014-02-06 00:55:19 -0500575 outp += sprintf(outp, fmt8,
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800576 p->energy_gfx * rapl_energy_units);
577 if (do_rapl & RAPL_DRAM)
Len Brownfc04cc62014-02-06 00:55:19 -0500578 outp += sprintf(outp, fmt8,
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800579 p->energy_dram * rapl_energy_units);
580 if (do_rapl & RAPL_PKG_PERF_STATUS)
Len Brownfc04cc62014-02-06 00:55:19 -0500581 outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800582 if (do_rapl & RAPL_DRAM_PERF_STATUS)
Len Brownfc04cc62014-02-06 00:55:19 -0500583 outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
Len Brown889facb2012-11-08 00:48:57 -0500584
Len Brownd7899442015-01-23 00:12:33 -0500585 outp += sprintf(outp, fmt8, interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800586 }
Len Brownc98d5d92012-06-04 00:56:40 -0400587done:
Len Brownc98d5d92012-06-04 00:56:40 -0400588 outp += sprintf(outp, "\n");
589
590 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400591}
592
Len Brownc98d5d92012-06-04 00:56:40 -0400593void flush_stdout()
Len Brown103a8fe2010-10-22 23:53:03 -0400594{
Len Brownc98d5d92012-06-04 00:56:40 -0400595 fputs(output_buffer, stdout);
Len Brownddac0d62012-11-30 01:01:40 -0500596 fflush(stdout);
Len Brownc98d5d92012-06-04 00:56:40 -0400597 outp = output_buffer;
598}
599void flush_stderr()
600{
601 fputs(output_buffer, stderr);
602 outp = output_buffer;
603}
604void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
605{
Len Browne23da032012-02-06 18:37:16 -0500606 static int printed;
Len Brown103a8fe2010-10-22 23:53:03 -0400607
Len Browne23da032012-02-06 18:37:16 -0500608 if (!printed || !summary_only)
609 print_header();
Len Brown103a8fe2010-10-22 23:53:03 -0400610
Len Brownc98d5d92012-06-04 00:56:40 -0400611 if (topo.num_cpus > 1)
612 format_counters(&average.threads, &average.cores,
613 &average.packages);
Len Brown103a8fe2010-10-22 23:53:03 -0400614
Len Browne23da032012-02-06 18:37:16 -0500615 printed = 1;
616
617 if (summary_only)
618 return;
619
Len Brownc98d5d92012-06-04 00:56:40 -0400620 for_all_cpus(format_counters, t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400621}
622
Len Brown889facb2012-11-08 00:48:57 -0500623#define DELTA_WRAP32(new, old) \
624 if (new > old) { \
625 old = new - old; \
626 } else { \
627 old = 0x100000000 + new - old; \
628 }
629
Len Brownc98d5d92012-06-04 00:56:40 -0400630void
631delta_package(struct pkg_data *new, struct pkg_data *old)
Len Brown103a8fe2010-10-22 23:53:03 -0400632{
Len Brownc98d5d92012-06-04 00:56:40 -0400633 old->pc2 = new->pc2 - old->pc2;
634 old->pc3 = new->pc3 - old->pc3;
635 old->pc6 = new->pc6 - old->pc6;
636 old->pc7 = new->pc7 - old->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800637 old->pc8 = new->pc8 - old->pc8;
638 old->pc9 = new->pc9 - old->pc9;
639 old->pc10 = new->pc10 - old->pc10;
Len Brown889facb2012-11-08 00:48:57 -0500640 old->pkg_temp_c = new->pkg_temp_c;
641
642 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
643 DELTA_WRAP32(new->energy_cores, old->energy_cores);
644 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
645 DELTA_WRAP32(new->energy_dram, old->energy_dram);
646 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
647 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
Len Brownc98d5d92012-06-04 00:56:40 -0400648}
Len Brown103a8fe2010-10-22 23:53:03 -0400649
Len Brownc98d5d92012-06-04 00:56:40 -0400650void
651delta_core(struct core_data *new, struct core_data *old)
652{
653 old->c3 = new->c3 - old->c3;
654 old->c6 = new->c6 - old->c6;
655 old->c7 = new->c7 - old->c7;
Len Brown889facb2012-11-08 00:48:57 -0500656 old->core_temp_c = new->core_temp_c;
Len Brownc98d5d92012-06-04 00:56:40 -0400657}
Len Brown103a8fe2010-10-22 23:53:03 -0400658
Len Brownc3ae3312012-06-13 21:31:46 -0400659/*
660 * old = new - old
661 */
Len Brownc98d5d92012-06-04 00:56:40 -0400662void
663delta_thread(struct thread_data *new, struct thread_data *old,
664 struct core_data *core_delta)
665{
666 old->tsc = new->tsc - old->tsc;
Len Brown103a8fe2010-10-22 23:53:03 -0400667
Len Brownc98d5d92012-06-04 00:56:40 -0400668 /* check for TSC < 1 Mcycles over interval */
Josh Triplettb2c95d92013-08-20 17:20:18 -0700669 if (old->tsc < (1000 * 1000))
670 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
671 "You can disable all c-states by booting with \"idle=poll\"\n"
672 "or just the deep ones with \"processor.max_cstate=1\"");
Len Brown103a8fe2010-10-22 23:53:03 -0400673
Len Brownc98d5d92012-06-04 00:56:40 -0400674 old->c1 = new->c1 - old->c1;
Len Brown103a8fe2010-10-22 23:53:03 -0400675
Len Browna7296172015-01-23 01:33:58 -0500676 if (has_aperf) {
677 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
678 old->aperf = new->aperf - old->aperf;
679 old->mperf = new->mperf - old->mperf;
680 } else {
Len Brown103a8fe2010-10-22 23:53:03 -0400681
Len Browna7296172015-01-23 01:33:58 -0500682 if (!aperf_mperf_unstable) {
683 fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname);
684 fprintf(stderr, "* Frequency results do not cover entire interval *\n");
685 fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n");
Len Brownc98d5d92012-06-04 00:56:40 -0400686
Len Browna7296172015-01-23 01:33:58 -0500687 aperf_mperf_unstable = 1;
688 }
689 /*
690 * mperf delta is likely a huge "positive" number
691 * can not use it for calculating c0 time
692 */
693 skip_c0 = 1;
694 skip_c1 = 1;
Len Brownc98d5d92012-06-04 00:56:40 -0400695 }
Len Brownc98d5d92012-06-04 00:56:40 -0400696 }
Len Brown103a8fe2010-10-22 23:53:03 -0400697
Len Brown103a8fe2010-10-22 23:53:03 -0400698
Len Brown144b44b2013-11-09 00:30:16 -0500699 if (use_c1_residency_msr) {
700 /*
701 * Some models have a dedicated C1 residency MSR,
702 * which should be more accurate than the derivation below.
703 */
704 } else {
705 /*
706 * As counter collection is not atomic,
707 * it is possible for mperf's non-halted cycles + idle states
708 * to exceed TSC's all cycles: show c1 = 0% in that case.
709 */
710 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
711 old->c1 = 0;
712 else {
713 /* normal case, derive c1 */
714 old->c1 = old->tsc - old->mperf - core_delta->c3
Len Brownc98d5d92012-06-04 00:56:40 -0400715 - core_delta->c6 - core_delta->c7;
Len Brown144b44b2013-11-09 00:30:16 -0500716 }
Len Brownc98d5d92012-06-04 00:56:40 -0400717 }
Len Brownc3ae3312012-06-13 21:31:46 -0400718
Len Brownc98d5d92012-06-04 00:56:40 -0400719 if (old->mperf == 0) {
Len Brownc3ae3312012-06-13 21:31:46 -0400720 if (verbose > 1) fprintf(stderr, "cpu%d MPERF 0!\n", old->cpu_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400721 old->mperf = 1; /* divide by 0 protection */
722 }
723
Len Brown8e180f32012-09-22 01:25:08 -0400724 old->extra_delta32 = new->extra_delta32 - old->extra_delta32;
725 old->extra_delta32 &= 0xFFFFFFFF;
726
727 old->extra_delta64 = new->extra_delta64 - old->extra_delta64;
728
Len Brownc98d5d92012-06-04 00:56:40 -0400729 /*
Len Brown8e180f32012-09-22 01:25:08 -0400730 * Extra MSR is just a snapshot, simply copy latest w/o subtracting
Len Brownc98d5d92012-06-04 00:56:40 -0400731 */
Len Brown2f32edf2012-09-21 23:45:46 -0400732 old->extra_msr32 = new->extra_msr32;
733 old->extra_msr64 = new->extra_msr64;
Len Brown1ed51012013-02-10 17:19:24 -0500734
735 if (do_smi)
736 old->smi_count = new->smi_count - old->smi_count;
Len Brownc98d5d92012-06-04 00:56:40 -0400737}
738
739int delta_cpu(struct thread_data *t, struct core_data *c,
740 struct pkg_data *p, struct thread_data *t2,
741 struct core_data *c2, struct pkg_data *p2)
742{
743 /* calculate core delta only for 1st thread in core */
744 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
745 delta_core(c, c2);
746
747 /* always calculate thread delta */
748 delta_thread(t, t2, c2); /* c2 is core delta */
749
750 /* calculate package delta only for 1st core in package */
751 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
752 delta_package(p, p2);
753
754 return 0;
755}
756
757void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
758{
759 t->tsc = 0;
760 t->aperf = 0;
761 t->mperf = 0;
762 t->c1 = 0;
763
Len Brown1ed51012013-02-10 17:19:24 -0500764 t->smi_count = 0;
Len Brown8e180f32012-09-22 01:25:08 -0400765 t->extra_delta32 = 0;
766 t->extra_delta64 = 0;
767
Len Brownc98d5d92012-06-04 00:56:40 -0400768 /* tells format_counters to dump all fields from this set */
769 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
770
771 c->c3 = 0;
772 c->c6 = 0;
773 c->c7 = 0;
Len Brown889facb2012-11-08 00:48:57 -0500774 c->core_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -0400775
776 p->pc2 = 0;
777 p->pc3 = 0;
778 p->pc6 = 0;
779 p->pc7 = 0;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800780 p->pc8 = 0;
781 p->pc9 = 0;
782 p->pc10 = 0;
Len Brown889facb2012-11-08 00:48:57 -0500783
784 p->energy_pkg = 0;
785 p->energy_dram = 0;
786 p->energy_cores = 0;
787 p->energy_gfx = 0;
788 p->rapl_pkg_perf_status = 0;
789 p->rapl_dram_perf_status = 0;
790 p->pkg_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -0400791}
792int sum_counters(struct thread_data *t, struct core_data *c,
793 struct pkg_data *p)
794{
795 average.threads.tsc += t->tsc;
796 average.threads.aperf += t->aperf;
797 average.threads.mperf += t->mperf;
798 average.threads.c1 += t->c1;
799
Len Brown8e180f32012-09-22 01:25:08 -0400800 average.threads.extra_delta32 += t->extra_delta32;
801 average.threads.extra_delta64 += t->extra_delta64;
802
Len Brownc98d5d92012-06-04 00:56:40 -0400803 /* sum per-core values only for 1st thread in core */
804 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
805 return 0;
806
807 average.cores.c3 += c->c3;
808 average.cores.c6 += c->c6;
809 average.cores.c7 += c->c7;
810
Len Brown889facb2012-11-08 00:48:57 -0500811 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
812
Len Brownc98d5d92012-06-04 00:56:40 -0400813 /* sum per-pkg values only for 1st core in pkg */
814 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
815 return 0;
816
817 average.packages.pc2 += p->pc2;
818 average.packages.pc3 += p->pc3;
819 average.packages.pc6 += p->pc6;
820 average.packages.pc7 += p->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800821 average.packages.pc8 += p->pc8;
822 average.packages.pc9 += p->pc9;
823 average.packages.pc10 += p->pc10;
Len Brownc98d5d92012-06-04 00:56:40 -0400824
Len Brown889facb2012-11-08 00:48:57 -0500825 average.packages.energy_pkg += p->energy_pkg;
826 average.packages.energy_dram += p->energy_dram;
827 average.packages.energy_cores += p->energy_cores;
828 average.packages.energy_gfx += p->energy_gfx;
829
830 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
831
832 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
833 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
Len Brownc98d5d92012-06-04 00:56:40 -0400834 return 0;
835}
836/*
837 * sum the counters for all cpus in the system
838 * compute the weighted average
839 */
840void compute_average(struct thread_data *t, struct core_data *c,
841 struct pkg_data *p)
842{
843 clear_counters(&average.threads, &average.cores, &average.packages);
844
845 for_all_cpus(sum_counters, t, c, p);
846
847 average.threads.tsc /= topo.num_cpus;
848 average.threads.aperf /= topo.num_cpus;
849 average.threads.mperf /= topo.num_cpus;
850 average.threads.c1 /= topo.num_cpus;
851
Len Brown8e180f32012-09-22 01:25:08 -0400852 average.threads.extra_delta32 /= topo.num_cpus;
853 average.threads.extra_delta32 &= 0xFFFFFFFF;
854
855 average.threads.extra_delta64 /= topo.num_cpus;
856
Len Brownc98d5d92012-06-04 00:56:40 -0400857 average.cores.c3 /= topo.num_cores;
858 average.cores.c6 /= topo.num_cores;
859 average.cores.c7 /= topo.num_cores;
860
861 average.packages.pc2 /= topo.num_packages;
862 average.packages.pc3 /= topo.num_packages;
863 average.packages.pc6 /= topo.num_packages;
864 average.packages.pc7 /= topo.num_packages;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800865
866 average.packages.pc8 /= topo.num_packages;
867 average.packages.pc9 /= topo.num_packages;
868 average.packages.pc10 /= topo.num_packages;
Len Brownc98d5d92012-06-04 00:56:40 -0400869}
870
871static unsigned long long rdtsc(void)
872{
873 unsigned int low, high;
874
875 asm volatile("rdtsc" : "=a" (low), "=d" (high));
876
877 return low | ((unsigned long long)high) << 32;
878}
879
880
881/*
882 * get_counters(...)
883 * migrate to cpu
884 * acquire and record local counters for that cpu
885 */
886int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
887{
888 int cpu = t->cpu_id;
Len Brown889facb2012-11-08 00:48:57 -0500889 unsigned long long msr;
Len Brownc98d5d92012-06-04 00:56:40 -0400890
Len Browne52966c2012-11-08 22:38:05 -0500891 if (cpu_migrate(cpu)) {
892 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
Len Brownc98d5d92012-06-04 00:56:40 -0400893 return -1;
Len Browne52966c2012-11-08 22:38:05 -0500894 }
Len Brownc98d5d92012-06-04 00:56:40 -0400895
896 t->tsc = rdtsc(); /* we are running on local CPU of interest */
897
898 if (has_aperf) {
Len Brown9c63a652012-10-31 01:29:52 -0400899 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
Len Brownc98d5d92012-06-04 00:56:40 -0400900 return -3;
Len Brown9c63a652012-10-31 01:29:52 -0400901 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
Len Brownc98d5d92012-06-04 00:56:40 -0400902 return -4;
903 }
904
Len Brown1ed51012013-02-10 17:19:24 -0500905 if (do_smi) {
906 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
907 return -5;
908 t->smi_count = msr & 0xFFFFFFFF;
909 }
Len Brown8e180f32012-09-22 01:25:08 -0400910 if (extra_delta_offset32) {
Len Brown889facb2012-11-08 00:48:57 -0500911 if (get_msr(cpu, extra_delta_offset32, &msr))
Len Brown2f32edf2012-09-21 23:45:46 -0400912 return -5;
Len Brown889facb2012-11-08 00:48:57 -0500913 t->extra_delta32 = msr & 0xFFFFFFFF;
Len Brown8e180f32012-09-22 01:25:08 -0400914 }
915
916 if (extra_delta_offset64)
917 if (get_msr(cpu, extra_delta_offset64, &t->extra_delta64))
918 return -5;
919
920 if (extra_msr_offset32) {
Len Brown889facb2012-11-08 00:48:57 -0500921 if (get_msr(cpu, extra_msr_offset32, &msr))
Len Brown8e180f32012-09-22 01:25:08 -0400922 return -5;
Len Brown889facb2012-11-08 00:48:57 -0500923 t->extra_msr32 = msr & 0xFFFFFFFF;
Len Brown8e180f32012-09-22 01:25:08 -0400924 }
Len Brown2f32edf2012-09-21 23:45:46 -0400925
926 if (extra_msr_offset64)
927 if (get_msr(cpu, extra_msr_offset64, &t->extra_msr64))
Len Brownc98d5d92012-06-04 00:56:40 -0400928 return -5;
929
Len Brown144b44b2013-11-09 00:30:16 -0500930 if (use_c1_residency_msr) {
931 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
932 return -6;
933 }
934
Len Brownc98d5d92012-06-04 00:56:40 -0400935 /* collect core counters only for 1st thread in core */
936 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
937 return 0;
938
Len Brown144b44b2013-11-09 00:30:16 -0500939 if (do_nhm_cstates && !do_slm_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -0400940 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
941 return -6;
Len Brown144b44b2013-11-09 00:30:16 -0500942 }
943
944 if (do_nhm_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -0400945 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
946 return -7;
947 }
948
949 if (do_snb_cstates)
950 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
951 return -8;
952
Len Brown889facb2012-11-08 00:48:57 -0500953 if (do_dts) {
954 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
955 return -9;
956 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
957 }
958
959
Len Brownc98d5d92012-06-04 00:56:40 -0400960 /* collect package counters only for 1st core in package */
961 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
962 return 0;
963
Len Brown144b44b2013-11-09 00:30:16 -0500964 if (do_nhm_cstates && !do_slm_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -0400965 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
966 return -9;
967 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
968 return -10;
969 }
970 if (do_snb_cstates) {
971 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
972 return -11;
973 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
974 return -12;
Len Brown103a8fe2010-10-22 23:53:03 -0400975 }
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800976 if (do_c8_c9_c10) {
977 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
978 return -13;
979 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
980 return -13;
981 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
982 return -13;
983 }
Len Brown889facb2012-11-08 00:48:57 -0500984 if (do_rapl & RAPL_PKG) {
985 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
986 return -13;
987 p->energy_pkg = msr & 0xFFFFFFFF;
988 }
989 if (do_rapl & RAPL_CORES) {
990 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
991 return -14;
992 p->energy_cores = msr & 0xFFFFFFFF;
993 }
994 if (do_rapl & RAPL_DRAM) {
995 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
996 return -15;
997 p->energy_dram = msr & 0xFFFFFFFF;
998 }
999 if (do_rapl & RAPL_GFX) {
1000 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1001 return -16;
1002 p->energy_gfx = msr & 0xFFFFFFFF;
1003 }
1004 if (do_rapl & RAPL_PKG_PERF_STATUS) {
1005 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1006 return -16;
1007 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1008 }
1009 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1010 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1011 return -16;
1012 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1013 }
1014 if (do_ptm) {
1015 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1016 return -17;
1017 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1018 }
Len Brown103a8fe2010-10-22 23:53:03 -04001019 return 0;
1020}
1021
Len Brownc98d5d92012-06-04 00:56:40 -04001022void print_verbose_header(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001023{
1024 unsigned long long msr;
1025 unsigned int ratio;
1026
Len Brownd7899442015-01-23 00:12:33 -05001027 if (!do_nhm_platform_info)
Len Brown103a8fe2010-10-22 23:53:03 -04001028 return;
1029
Len Brown9c63a652012-10-31 01:29:52 -04001030 get_msr(0, MSR_NHM_PLATFORM_INFO, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001031
Len Brown67920412013-01-31 15:22:15 -05001032 fprintf(stderr, "cpu0: MSR_NHM_PLATFORM_INFO: 0x%08llx\n", msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001033
Len Brown103a8fe2010-10-22 23:53:03 -04001034 ratio = (msr >> 40) & 0xFF;
1035 fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency\n",
1036 ratio, bclk, ratio * bclk);
1037
1038 ratio = (msr >> 8) & 0xFF;
1039 fprintf(stderr, "%d * %.0f = %.0f MHz TSC frequency\n",
1040 ratio, bclk, ratio * bclk);
1041
Len Brown67920412013-01-31 15:22:15 -05001042 get_msr(0, MSR_IA32_POWER_CTL, &msr);
Len Brown144b44b2013-11-09 00:30:16 -05001043 fprintf(stderr, "cpu0: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
Len Brown67920412013-01-31 15:22:15 -05001044 msr, msr & 0x2 ? "EN" : "DIS");
1045
Len Brown6574a5d2012-09-21 00:01:31 -04001046 if (!do_ivt_turbo_ratio_limit)
1047 goto print_nhm_turbo_ratio_limits;
1048
1049 get_msr(0, MSR_IVT_TURBO_RATIO_LIMIT, &msr);
1050
Len Brown67920412013-01-31 15:22:15 -05001051 fprintf(stderr, "cpu0: MSR_IVT_TURBO_RATIO_LIMIT: 0x%08llx\n", msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001052
1053 ratio = (msr >> 56) & 0xFF;
1054 if (ratio)
1055 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 16 active cores\n",
1056 ratio, bclk, ratio * bclk);
1057
1058 ratio = (msr >> 48) & 0xFF;
1059 if (ratio)
1060 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 15 active cores\n",
1061 ratio, bclk, ratio * bclk);
1062
1063 ratio = (msr >> 40) & 0xFF;
1064 if (ratio)
1065 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 14 active cores\n",
1066 ratio, bclk, ratio * bclk);
1067
1068 ratio = (msr >> 32) & 0xFF;
1069 if (ratio)
1070 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 13 active cores\n",
1071 ratio, bclk, ratio * bclk);
1072
1073 ratio = (msr >> 24) & 0xFF;
1074 if (ratio)
1075 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 12 active cores\n",
1076 ratio, bclk, ratio * bclk);
1077
1078 ratio = (msr >> 16) & 0xFF;
1079 if (ratio)
1080 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 11 active cores\n",
1081 ratio, bclk, ratio * bclk);
1082
1083 ratio = (msr >> 8) & 0xFF;
1084 if (ratio)
1085 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 10 active cores\n",
1086 ratio, bclk, ratio * bclk);
1087
1088 ratio = (msr >> 0) & 0xFF;
1089 if (ratio)
1090 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 9 active cores\n",
1091 ratio, bclk, ratio * bclk);
1092
1093print_nhm_turbo_ratio_limits:
Len Brown889facb2012-11-08 00:48:57 -05001094 get_msr(0, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
1095
1096#define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
1097#define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
1098
1099 fprintf(stderr, "cpu0: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", msr);
1100
1101 fprintf(stderr, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: ",
1102 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
1103 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
1104 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
1105 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
1106 (msr & (1 << 15)) ? "" : "UN",
1107 (unsigned int)msr & 7);
1108
1109
1110 switch(msr & 0x7) {
1111 case 0:
Len Brown144b44b2013-11-09 00:30:16 -05001112 fprintf(stderr, do_slm_cstates ? "no pkg states" : "pc0");
Len Brown889facb2012-11-08 00:48:57 -05001113 break;
1114 case 1:
Len Brown144b44b2013-11-09 00:30:16 -05001115 fprintf(stderr, do_slm_cstates ? "no pkg states" : do_snb_cstates ? "pc2" : "pc0");
Len Brown889facb2012-11-08 00:48:57 -05001116 break;
1117 case 2:
Len Brown144b44b2013-11-09 00:30:16 -05001118 fprintf(stderr, do_slm_cstates ? "invalid" : do_snb_cstates ? "pc6-noret" : "pc3");
Len Brown889facb2012-11-08 00:48:57 -05001119 break;
1120 case 3:
Len Brown144b44b2013-11-09 00:30:16 -05001121 fprintf(stderr, do_slm_cstates ? "invalid" : "pc6");
Len Brown889facb2012-11-08 00:48:57 -05001122 break;
1123 case 4:
Len Brown144b44b2013-11-09 00:30:16 -05001124 fprintf(stderr, do_slm_cstates ? "pc4" : "pc7");
Len Brown889facb2012-11-08 00:48:57 -05001125 break;
1126 case 5:
Len Brown144b44b2013-11-09 00:30:16 -05001127 fprintf(stderr, do_slm_cstates ? "invalid" : do_snb_cstates ? "pc7s" : "invalid");
1128 break;
1129 case 6:
1130 fprintf(stderr, do_slm_cstates ? "pc6" : "invalid");
Len Brown889facb2012-11-08 00:48:57 -05001131 break;
1132 case 7:
Len Brown144b44b2013-11-09 00:30:16 -05001133 fprintf(stderr, do_slm_cstates ? "pc7" : "unlimited");
Len Brown889facb2012-11-08 00:48:57 -05001134 break;
1135 default:
1136 fprintf(stderr, "invalid");
1137 }
1138 fprintf(stderr, ")\n");
Len Brown103a8fe2010-10-22 23:53:03 -04001139
Len Brownd7899442015-01-23 00:12:33 -05001140 if (!do_nhm_turbo_ratio_limit)
Len Brown103a8fe2010-10-22 23:53:03 -04001141 return;
1142
Len Brown9c63a652012-10-31 01:29:52 -04001143 get_msr(0, MSR_NHM_TURBO_RATIO_LIMIT, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001144
Len Brown67920412013-01-31 15:22:15 -05001145 fprintf(stderr, "cpu0: MSR_NHM_TURBO_RATIO_LIMIT: 0x%08llx\n", msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001146
1147 ratio = (msr >> 56) & 0xFF;
1148 if (ratio)
1149 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 8 active cores\n",
1150 ratio, bclk, ratio * bclk);
1151
1152 ratio = (msr >> 48) & 0xFF;
1153 if (ratio)
1154 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 7 active cores\n",
1155 ratio, bclk, ratio * bclk);
1156
1157 ratio = (msr >> 40) & 0xFF;
1158 if (ratio)
1159 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 6 active cores\n",
1160 ratio, bclk, ratio * bclk);
1161
1162 ratio = (msr >> 32) & 0xFF;
1163 if (ratio)
1164 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 5 active cores\n",
1165 ratio, bclk, ratio * bclk);
1166
Len Brown103a8fe2010-10-22 23:53:03 -04001167 ratio = (msr >> 24) & 0xFF;
1168 if (ratio)
1169 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
1170 ratio, bclk, ratio * bclk);
1171
1172 ratio = (msr >> 16) & 0xFF;
1173 if (ratio)
1174 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
1175 ratio, bclk, ratio * bclk);
1176
1177 ratio = (msr >> 8) & 0xFF;
1178 if (ratio)
1179 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
1180 ratio, bclk, ratio * bclk);
1181
1182 ratio = (msr >> 0) & 0xFF;
1183 if (ratio)
1184 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
1185 ratio, bclk, ratio * bclk);
Len Brown3a9a9412014-08-15 02:39:52 -04001186
Len Brown103a8fe2010-10-22 23:53:03 -04001187}
1188
Len Brownc98d5d92012-06-04 00:56:40 -04001189void free_all_buffers(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001190{
Len Brownc98d5d92012-06-04 00:56:40 -04001191 CPU_FREE(cpu_present_set);
1192 cpu_present_set = NULL;
1193 cpu_present_set = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001194
Len Brownc98d5d92012-06-04 00:56:40 -04001195 CPU_FREE(cpu_affinity_set);
1196 cpu_affinity_set = NULL;
1197 cpu_affinity_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001198
Len Brownc98d5d92012-06-04 00:56:40 -04001199 free(thread_even);
1200 free(core_even);
1201 free(package_even);
1202
1203 thread_even = NULL;
1204 core_even = NULL;
1205 package_even = NULL;
1206
1207 free(thread_odd);
1208 free(core_odd);
1209 free(package_odd);
1210
1211 thread_odd = NULL;
1212 core_odd = NULL;
1213 package_odd = NULL;
1214
1215 free(output_buffer);
1216 output_buffer = NULL;
1217 outp = NULL;
Len Brown103a8fe2010-10-22 23:53:03 -04001218}
1219
Len Brownc98d5d92012-06-04 00:56:40 -04001220/*
Josh Triplett57a42a32013-08-20 17:20:17 -07001221 * Open a file, and exit on failure
1222 */
1223FILE *fopen_or_die(const char *path, const char *mode)
1224{
1225 FILE *filep = fopen(path, "r");
Josh Triplettb2c95d92013-08-20 17:20:18 -07001226 if (!filep)
1227 err(1, "%s: open failed", path);
Josh Triplett57a42a32013-08-20 17:20:17 -07001228 return filep;
1229}
1230
1231/*
Josh Triplett95aebc42013-08-20 17:20:16 -07001232 * Parse a file containing a single int.
1233 */
1234int parse_int_file(const char *fmt, ...)
1235{
1236 va_list args;
1237 char path[PATH_MAX];
1238 FILE *filep;
1239 int value;
1240
1241 va_start(args, fmt);
1242 vsnprintf(path, sizeof(path), fmt, args);
1243 va_end(args);
Josh Triplett57a42a32013-08-20 17:20:17 -07001244 filep = fopen_or_die(path, "r");
Josh Triplettb2c95d92013-08-20 17:20:18 -07001245 if (fscanf(filep, "%d", &value) != 1)
1246 err(1, "%s: failed to parse number from file", path);
Josh Triplett95aebc42013-08-20 17:20:16 -07001247 fclose(filep);
1248 return value;
1249}
1250
1251/*
Len Brownc98d5d92012-06-04 00:56:40 -04001252 * cpu_is_first_sibling_in_core(cpu)
1253 * return 1 if given CPU is 1st HT sibling in the core
1254 */
1255int cpu_is_first_sibling_in_core(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001256{
Josh Triplett95aebc42013-08-20 17:20:16 -07001257 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001258}
1259
Len Brownc98d5d92012-06-04 00:56:40 -04001260/*
1261 * cpu_is_first_core_in_package(cpu)
1262 * return 1 if given CPU is 1st core in package
1263 */
1264int cpu_is_first_core_in_package(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001265{
Josh Triplett95aebc42013-08-20 17:20:16 -07001266 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001267}
1268
1269int get_physical_package_id(int cpu)
1270{
Josh Triplett95aebc42013-08-20 17:20:16 -07001271 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001272}
1273
1274int get_core_id(int cpu)
1275{
Josh Triplett95aebc42013-08-20 17:20:16 -07001276 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001277}
1278
Len Brownc98d5d92012-06-04 00:56:40 -04001279int get_num_ht_siblings(int cpu)
1280{
1281 char path[80];
1282 FILE *filep;
1283 int sib1, sib2;
1284 int matches;
1285 char character;
1286
1287 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
Josh Triplett57a42a32013-08-20 17:20:17 -07001288 filep = fopen_or_die(path, "r");
Len Brownc98d5d92012-06-04 00:56:40 -04001289 /*
1290 * file format:
1291 * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4)
1292 * otherwinse 1 sibling (self).
1293 */
1294 matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2);
1295
1296 fclose(filep);
1297
1298 if (matches == 3)
1299 return 2;
1300 else
1301 return 1;
1302}
1303
Len Brown103a8fe2010-10-22 23:53:03 -04001304/*
Len Brownc98d5d92012-06-04 00:56:40 -04001305 * run func(thread, core, package) in topology order
1306 * skip non-present cpus
Len Brown103a8fe2010-10-22 23:53:03 -04001307 */
1308
Len Brownc98d5d92012-06-04 00:56:40 -04001309int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
1310 struct pkg_data *, struct thread_data *, struct core_data *,
1311 struct pkg_data *), struct thread_data *thread_base,
1312 struct core_data *core_base, struct pkg_data *pkg_base,
1313 struct thread_data *thread_base2, struct core_data *core_base2,
1314 struct pkg_data *pkg_base2)
1315{
1316 int retval, pkg_no, core_no, thread_no;
1317
1318 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
1319 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
1320 for (thread_no = 0; thread_no <
1321 topo.num_threads_per_core; ++thread_no) {
1322 struct thread_data *t, *t2;
1323 struct core_data *c, *c2;
1324 struct pkg_data *p, *p2;
1325
1326 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
1327
1328 if (cpu_is_not_present(t->cpu_id))
1329 continue;
1330
1331 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
1332
1333 c = GET_CORE(core_base, core_no, pkg_no);
1334 c2 = GET_CORE(core_base2, core_no, pkg_no);
1335
1336 p = GET_PKG(pkg_base, pkg_no);
1337 p2 = GET_PKG(pkg_base2, pkg_no);
1338
1339 retval = func(t, c, p, t2, c2, p2);
1340 if (retval)
1341 return retval;
1342 }
1343 }
1344 }
1345 return 0;
1346}
1347
1348/*
1349 * run func(cpu) on every cpu in /proc/stat
1350 * return max_cpu number
1351 */
1352int for_all_proc_cpus(int (func)(int))
Len Brown103a8fe2010-10-22 23:53:03 -04001353{
1354 FILE *fp;
Len Brownc98d5d92012-06-04 00:56:40 -04001355 int cpu_num;
Len Brown103a8fe2010-10-22 23:53:03 -04001356 int retval;
1357
Josh Triplett57a42a32013-08-20 17:20:17 -07001358 fp = fopen_or_die(proc_stat, "r");
Len Brown103a8fe2010-10-22 23:53:03 -04001359
1360 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
Josh Triplettb2c95d92013-08-20 17:20:18 -07001361 if (retval != 0)
1362 err(1, "%s: failed to parse format", proc_stat);
Len Brown103a8fe2010-10-22 23:53:03 -04001363
Len Brownc98d5d92012-06-04 00:56:40 -04001364 while (1) {
1365 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 -04001366 if (retval != 1)
1367 break;
1368
Len Brownc98d5d92012-06-04 00:56:40 -04001369 retval = func(cpu_num);
1370 if (retval) {
1371 fclose(fp);
1372 return(retval);
1373 }
Len Brown103a8fe2010-10-22 23:53:03 -04001374 }
1375 fclose(fp);
Len Brownc98d5d92012-06-04 00:56:40 -04001376 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001377}
1378
1379void re_initialize(void)
1380{
Len Brownc98d5d92012-06-04 00:56:40 -04001381 free_all_buffers();
1382 setup_all_buffers();
1383 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04001384}
1385
Len Brownc98d5d92012-06-04 00:56:40 -04001386
Len Brown103a8fe2010-10-22 23:53:03 -04001387/*
Len Brownc98d5d92012-06-04 00:56:40 -04001388 * count_cpus()
1389 * remember the last one seen, it will be the max
Len Brown103a8fe2010-10-22 23:53:03 -04001390 */
Len Brownc98d5d92012-06-04 00:56:40 -04001391int count_cpus(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001392{
Len Brownc98d5d92012-06-04 00:56:40 -04001393 if (topo.max_cpu_num < cpu)
1394 topo.max_cpu_num = cpu;
Len Brown103a8fe2010-10-22 23:53:03 -04001395
Len Brownc98d5d92012-06-04 00:56:40 -04001396 topo.num_cpus += 1;
1397 return 0;
1398}
1399int mark_cpu_present(int cpu)
1400{
1401 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brown15aaa342012-03-29 22:19:58 -04001402 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001403}
1404
1405void turbostat_loop()
1406{
Len Brownc98d5d92012-06-04 00:56:40 -04001407 int retval;
Len Browne52966c2012-11-08 22:38:05 -05001408 int restarted = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001409
Len Brown103a8fe2010-10-22 23:53:03 -04001410restart:
Len Browne52966c2012-11-08 22:38:05 -05001411 restarted++;
1412
Len Brownc98d5d92012-06-04 00:56:40 -04001413 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001414 if (retval < -1) {
1415 exit(retval);
1416 } else if (retval == -1) {
Len Browne52966c2012-11-08 22:38:05 -05001417 if (restarted > 1) {
1418 exit(retval);
1419 }
Len Brownc98d5d92012-06-04 00:56:40 -04001420 re_initialize();
1421 goto restart;
1422 }
Len Browne52966c2012-11-08 22:38:05 -05001423 restarted = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001424 gettimeofday(&tv_even, (struct timezone *)NULL);
1425
1426 while (1) {
Len Brownc98d5d92012-06-04 00:56:40 -04001427 if (for_all_proc_cpus(cpu_is_not_present)) {
Len Brown103a8fe2010-10-22 23:53:03 -04001428 re_initialize();
1429 goto restart;
1430 }
1431 sleep(interval_sec);
Len Brownc98d5d92012-06-04 00:56:40 -04001432 retval = for_all_cpus(get_counters, ODD_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001433 if (retval < -1) {
1434 exit(retval);
1435 } else if (retval == -1) {
Len Brown15aaa342012-03-29 22:19:58 -04001436 re_initialize();
1437 goto restart;
1438 }
Len Brown103a8fe2010-10-22 23:53:03 -04001439 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04001440 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04001441 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
1442 compute_average(EVEN_COUNTERS);
1443 format_all_counters(EVEN_COUNTERS);
1444 flush_stdout();
Len Brown15aaa342012-03-29 22:19:58 -04001445 sleep(interval_sec);
Len Brownc98d5d92012-06-04 00:56:40 -04001446 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001447 if (retval < -1) {
1448 exit(retval);
1449 } else if (retval == -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04001450 re_initialize();
1451 goto restart;
1452 }
Len Brown103a8fe2010-10-22 23:53:03 -04001453 gettimeofday(&tv_even, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04001454 timersub(&tv_even, &tv_odd, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04001455 for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS);
1456 compute_average(ODD_COUNTERS);
1457 format_all_counters(ODD_COUNTERS);
1458 flush_stdout();
Len Brown103a8fe2010-10-22 23:53:03 -04001459 }
1460}
1461
1462void check_dev_msr()
1463{
1464 struct stat sb;
1465
Josh Triplettb2c95d92013-08-20 17:20:18 -07001466 if (stat("/dev/cpu/0/msr", &sb))
Len Brownd7899442015-01-23 00:12:33 -05001467 err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
Len Brown103a8fe2010-10-22 23:53:03 -04001468}
1469
Len Brown98481e72014-08-15 00:36:50 -04001470void check_permissions()
Len Brown103a8fe2010-10-22 23:53:03 -04001471{
Len Brown98481e72014-08-15 00:36:50 -04001472 struct __user_cap_header_struct cap_header_data;
1473 cap_user_header_t cap_header = &cap_header_data;
1474 struct __user_cap_data_struct cap_data_data;
1475 cap_user_data_t cap_data = &cap_data_data;
1476 extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
1477 int do_exit = 0;
1478
1479 /* check for CAP_SYS_RAWIO */
1480 cap_header->pid = getpid();
1481 cap_header->version = _LINUX_CAPABILITY_VERSION;
1482 if (capget(cap_header, cap_data) < 0)
1483 err(-6, "capget(2) failed");
1484
1485 if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
1486 do_exit++;
1487 warnx("capget(CAP_SYS_RAWIO) failed,"
1488 " try \"# setcap cap_sys_rawio=ep %s\"", progname);
1489 }
1490
1491 /* test file permissions */
1492 if (euidaccess("/dev/cpu/0/msr", R_OK)) {
1493 do_exit++;
1494 warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
1495 }
1496
1497 /* if all else fails, thell them to be root */
1498 if (do_exit)
1499 if (getuid() != 0)
Len Brownd7899442015-01-23 00:12:33 -05001500 warnx("... or simply run as root");
Len Brown98481e72014-08-15 00:36:50 -04001501
1502 if (do_exit)
1503 exit(-6);
Len Brown103a8fe2010-10-22 23:53:03 -04001504}
1505
Len Brownd7899442015-01-23 00:12:33 -05001506/*
1507 * NHM adds support for additional MSRs:
1508 *
1509 * MSR_SMI_COUNT 0x00000034
1510 *
1511 * MSR_NHM_PLATFORM_INFO 0x000000ce
1512 * MSR_NHM_SNB_PKG_CST_CFG_CTL 0x000000e2
1513 *
1514 * MSR_PKG_C3_RESIDENCY 0x000003f8
1515 * MSR_PKG_C6_RESIDENCY 0x000003f9
1516 * MSR_CORE_C3_RESIDENCY 0x000003fc
1517 * MSR_CORE_C6_RESIDENCY 0x000003fd
1518 *
1519 */
1520int has_nhm_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04001521{
1522 if (!genuine_intel)
1523 return 0;
1524
1525 if (family != 6)
1526 return 0;
1527
1528 switch (model) {
1529 case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
1530 case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
1531 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
1532 case 0x25: /* Westmere Client - Clarkdale, Arrandale */
1533 case 0x2C: /* Westmere EP - Gulftown */
1534 case 0x2A: /* SNB */
1535 case 0x2D: /* SNB Xeon */
Len Brown553575f2011-11-18 03:32:01 -05001536 case 0x3A: /* IVB */
Len Brown13006512012-09-26 18:11:31 -04001537 case 0x3E: /* IVB Xeon */
Len Brown70b43402013-01-08 01:26:07 -05001538 case 0x3C: /* HSW */
Len Browne6f9bb32013-12-03 02:19:19 -05001539 case 0x3F: /* HSX */
Len Brown70b43402013-01-08 01:26:07 -05001540 case 0x45: /* HSW */
Len Brown149c2312013-03-15 10:58:02 -04001541 case 0x46: /* HSW */
Len Brown144b44b2013-11-09 00:30:16 -05001542 case 0x37: /* BYT */
1543 case 0x4D: /* AVN */
Len Brown4e8e863f2014-02-27 23:28:53 -05001544 case 0x3D: /* BDW */
1545 case 0x4F: /* BDX */
1546 case 0x56: /* BDX-DE */
Len Brown103a8fe2010-10-22 23:53:03 -04001547 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1548 case 0x2F: /* Westmere-EX Xeon - Eagleton */
Len Brownd7899442015-01-23 00:12:33 -05001549 return 1;
Len Brown103a8fe2010-10-22 23:53:03 -04001550 default:
1551 return 0;
1552 }
1553}
Len Brownd7899442015-01-23 00:12:33 -05001554int has_nhm_turbo_ratio_limit(unsigned int family, unsigned int model)
1555{
1556 if (!has_nhm_msrs(family, model))
1557 return 0;
1558
1559 switch (model) {
1560 /* Nehalem compatible, but do not include turbo-ratio limit support */
1561 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1562 case 0x2F: /* Westmere-EX Xeon - Eagleton */
1563 return 0;
1564 default:
1565 return 1;
1566 }
1567}
Len Brown6574a5d2012-09-21 00:01:31 -04001568int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
1569{
1570 if (!genuine_intel)
1571 return 0;
1572
1573 if (family != 6)
1574 return 0;
1575
1576 switch (model) {
1577 case 0x3E: /* IVB Xeon */
1578 return 1;
1579 default:
1580 return 0;
1581 }
1582}
1583
Len Brown889facb2012-11-08 00:48:57 -05001584/*
1585 * print_epb()
1586 * Decode the ENERGY_PERF_BIAS MSR
1587 */
1588int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1589{
1590 unsigned long long msr;
1591 char *epb_string;
1592 int cpu;
1593
1594 if (!has_epb)
1595 return 0;
1596
1597 cpu = t->cpu_id;
1598
1599 /* EPB is per-package */
1600 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1601 return 0;
1602
1603 if (cpu_migrate(cpu)) {
1604 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1605 return -1;
1606 }
1607
1608 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
1609 return 0;
1610
1611 switch (msr & 0x7) {
1612 case ENERGY_PERF_BIAS_PERFORMANCE:
1613 epb_string = "performance";
1614 break;
1615 case ENERGY_PERF_BIAS_NORMAL:
1616 epb_string = "balanced";
1617 break;
1618 case ENERGY_PERF_BIAS_POWERSAVE:
1619 epb_string = "powersave";
1620 break;
1621 default:
1622 epb_string = "custom";
1623 break;
1624 }
1625 fprintf(stderr, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
1626
1627 return 0;
1628}
1629
Len Brown3a9a9412014-08-15 02:39:52 -04001630/*
1631 * print_perf_limit()
1632 */
1633int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1634{
1635 unsigned long long msr;
1636 int cpu;
1637
1638 cpu = t->cpu_id;
1639
1640 /* per-package */
1641 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1642 return 0;
1643
1644 if (cpu_migrate(cpu)) {
1645 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1646 return -1;
1647 }
1648
1649 if (do_core_perf_limit_reasons) {
1650 get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
1651 fprintf(stderr, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
1652 fprintf(stderr, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
1653 (msr & 1 << 0) ? "PROCHOT, " : "",
1654 (msr & 1 << 1) ? "ThermStatus, " : "",
1655 (msr & 1 << 2) ? "bit2, " : "",
1656 (msr & 1 << 4) ? "Graphics, " : "",
1657 (msr & 1 << 5) ? "Auto-HWP, " : "",
1658 (msr & 1 << 6) ? "VR-Therm, " : "",
1659 (msr & 1 << 8) ? "Amps, " : "",
1660 (msr & 1 << 9) ? "CorePwr, " : "",
1661 (msr & 1 << 10) ? "PkgPwrL1, " : "",
1662 (msr & 1 << 11) ? "PkgPwrL2, " : "",
1663 (msr & 1 << 12) ? "MultiCoreTurbo, " : "",
1664 (msr & 1 << 13) ? "Transitions, " : "",
1665 (msr & 1 << 14) ? "bit14, " : "",
1666 (msr & 1 << 15) ? "bit15, " : "");
1667 fprintf(stderr, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n",
1668 (msr & 1 << 16) ? "PROCHOT, " : "",
1669 (msr & 1 << 17) ? "ThermStatus, " : "",
1670 (msr & 1 << 18) ? "bit18, " : "",
1671 (msr & 1 << 20) ? "Graphics, " : "",
1672 (msr & 1 << 21) ? "Auto-HWP, " : "",
1673 (msr & 1 << 22) ? "VR-Therm, " : "",
1674 (msr & 1 << 24) ? "Amps, " : "",
1675 (msr & 1 << 25) ? "CorePwr, " : "",
1676 (msr & 1 << 26) ? "PkgPwrL1, " : "",
1677 (msr & 1 << 27) ? "PkgPwrL2, " : "",
1678 (msr & 1 << 28) ? "MultiCoreTurbo, " : "",
1679 (msr & 1 << 29) ? "Transitions, " : "",
1680 (msr & 1 << 30) ? "bit30, " : "",
1681 (msr & 1 << 31) ? "bit31, " : "");
1682
1683 }
1684 if (do_gfx_perf_limit_reasons) {
1685 get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
1686 fprintf(stderr, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
1687 fprintf(stderr, " (Active: %s%s%s%s%s%s%s%s)",
1688 (msr & 1 << 0) ? "PROCHOT, " : "",
1689 (msr & 1 << 1) ? "ThermStatus, " : "",
1690 (msr & 1 << 4) ? "Graphics, " : "",
1691 (msr & 1 << 6) ? "VR-Therm, " : "",
1692 (msr & 1 << 8) ? "Amps, " : "",
1693 (msr & 1 << 9) ? "GFXPwr, " : "",
1694 (msr & 1 << 10) ? "PkgPwrL1, " : "",
1695 (msr & 1 << 11) ? "PkgPwrL2, " : "");
1696 fprintf(stderr, " (Logged: %s%s%s%s%s%s%s%s)\n",
1697 (msr & 1 << 16) ? "PROCHOT, " : "",
1698 (msr & 1 << 17) ? "ThermStatus, " : "",
1699 (msr & 1 << 20) ? "Graphics, " : "",
1700 (msr & 1 << 22) ? "VR-Therm, " : "",
1701 (msr & 1 << 24) ? "Amps, " : "",
1702 (msr & 1 << 25) ? "GFXPwr, " : "",
1703 (msr & 1 << 26) ? "PkgPwrL1, " : "",
1704 (msr & 1 << 27) ? "PkgPwrL2, " : "");
1705 }
1706 if (do_ring_perf_limit_reasons) {
1707 get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
1708 fprintf(stderr, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
1709 fprintf(stderr, " (Active: %s%s%s%s%s%s)",
1710 (msr & 1 << 0) ? "PROCHOT, " : "",
1711 (msr & 1 << 1) ? "ThermStatus, " : "",
1712 (msr & 1 << 6) ? "VR-Therm, " : "",
1713 (msr & 1 << 8) ? "Amps, " : "",
1714 (msr & 1 << 10) ? "PkgPwrL1, " : "",
1715 (msr & 1 << 11) ? "PkgPwrL2, " : "");
1716 fprintf(stderr, " (Logged: %s%s%s%s%s%s)\n",
1717 (msr & 1 << 16) ? "PROCHOT, " : "",
1718 (msr & 1 << 17) ? "ThermStatus, " : "",
1719 (msr & 1 << 22) ? "VR-Therm, " : "",
1720 (msr & 1 << 24) ? "Amps, " : "",
1721 (msr & 1 << 26) ? "PkgPwrL1, " : "",
1722 (msr & 1 << 27) ? "PkgPwrL2, " : "");
1723 }
1724 return 0;
1725}
1726
Len Brown889facb2012-11-08 00:48:57 -05001727#define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
1728#define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
1729
Len Brown144b44b2013-11-09 00:30:16 -05001730double get_tdp(model)
1731{
1732 unsigned long long msr;
1733
1734 if (do_rapl & RAPL_PKG_POWER_INFO)
1735 if (!get_msr(0, MSR_PKG_POWER_INFO, &msr))
1736 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
1737
1738 switch (model) {
1739 case 0x37:
1740 case 0x4D:
1741 return 30.0;
1742 default:
1743 return 135.0;
1744 }
1745}
1746
1747
Len Brown889facb2012-11-08 00:48:57 -05001748/*
1749 * rapl_probe()
1750 *
Len Brown144b44b2013-11-09 00:30:16 -05001751 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
Len Brown889facb2012-11-08 00:48:57 -05001752 */
1753void rapl_probe(unsigned int family, unsigned int model)
1754{
1755 unsigned long long msr;
Len Brown144b44b2013-11-09 00:30:16 -05001756 unsigned int time_unit;
Len Brown889facb2012-11-08 00:48:57 -05001757 double tdp;
1758
1759 if (!genuine_intel)
1760 return;
1761
1762 if (family != 6)
1763 return;
1764
1765 switch (model) {
1766 case 0x2A:
1767 case 0x3A:
Len Brown70b43402013-01-08 01:26:07 -05001768 case 0x3C: /* HSW */
Len Brown70b43402013-01-08 01:26:07 -05001769 case 0x45: /* HSW */
Len Brown149c2312013-03-15 10:58:02 -04001770 case 0x46: /* HSW */
Len Brown4e8e863f2014-02-27 23:28:53 -05001771 case 0x3D: /* BDW */
Len Brown144b44b2013-11-09 00:30:16 -05001772 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
Len Brown889facb2012-11-08 00:48:57 -05001773 break;
Len Browne6f9bb32013-12-03 02:19:19 -05001774 case 0x3F: /* HSX */
Len Brown4e8e863f2014-02-27 23:28:53 -05001775 case 0x4F: /* BDX */
1776 case 0x56: /* BDX-DE */
Len Browne6f9bb32013-12-03 02:19:19 -05001777 do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
1778 break;
Len Brown889facb2012-11-08 00:48:57 -05001779 case 0x2D:
1780 case 0x3E:
Len Brown144b44b2013-11-09 00:30:16 -05001781 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_PKG_PERF_STATUS | RAPL_DRAM_PERF_STATUS | RAPL_PKG_POWER_INFO;
1782 break;
1783 case 0x37: /* BYT */
1784 case 0x4D: /* AVN */
1785 do_rapl = RAPL_PKG | RAPL_CORES ;
Len Brown889facb2012-11-08 00:48:57 -05001786 break;
1787 default:
1788 return;
1789 }
1790
1791 /* units on package 0, verify later other packages match */
1792 if (get_msr(0, MSR_RAPL_POWER_UNIT, &msr))
1793 return;
1794
1795 rapl_power_units = 1.0 / (1 << (msr & 0xF));
Len Brown144b44b2013-11-09 00:30:16 -05001796 if (model == 0x37)
1797 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
1798 else
1799 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
Len Brown889facb2012-11-08 00:48:57 -05001800
Len Brown144b44b2013-11-09 00:30:16 -05001801 time_unit = msr >> 16 & 0xF;
1802 if (time_unit == 0)
1803 time_unit = 0xA;
Len Brown889facb2012-11-08 00:48:57 -05001804
Len Brown144b44b2013-11-09 00:30:16 -05001805 rapl_time_units = 1.0 / (1 << (time_unit));
1806
1807 tdp = get_tdp(model);
Len Brown889facb2012-11-08 00:48:57 -05001808
1809 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
Len Brown889facb2012-11-08 00:48:57 -05001810 if (verbose)
Len Brown144b44b2013-11-09 00:30:16 -05001811 fprintf(stderr, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
Len Brown889facb2012-11-08 00:48:57 -05001812
1813 return;
1814}
1815
Len Brown3a9a9412014-08-15 02:39:52 -04001816void perf_limit_reasons_probe(family, model)
1817{
1818 if (!genuine_intel)
1819 return;
1820
1821 if (family != 6)
1822 return;
1823
1824 switch (model) {
1825 case 0x3C: /* HSW */
1826 case 0x45: /* HSW */
1827 case 0x46: /* HSW */
1828 do_gfx_perf_limit_reasons = 1;
1829 case 0x3F: /* HSX */
1830 do_core_perf_limit_reasons = 1;
1831 do_ring_perf_limit_reasons = 1;
1832 default:
1833 return;
1834 }
1835}
1836
Len Brown889facb2012-11-08 00:48:57 -05001837int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1838{
1839 unsigned long long msr;
1840 unsigned int dts;
1841 int cpu;
1842
1843 if (!(do_dts || do_ptm))
1844 return 0;
1845
1846 cpu = t->cpu_id;
1847
1848 /* DTS is per-core, no need to print for each thread */
1849 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1850 return 0;
1851
1852 if (cpu_migrate(cpu)) {
1853 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1854 return -1;
1855 }
1856
1857 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
1858 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1859 return 0;
1860
1861 dts = (msr >> 16) & 0x7F;
1862 fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
1863 cpu, msr, tcc_activation_temp - dts);
1864
1865#ifdef THERM_DEBUG
1866 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
1867 return 0;
1868
1869 dts = (msr >> 16) & 0x7F;
1870 dts2 = (msr >> 8) & 0x7F;
1871 fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
1872 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
1873#endif
1874 }
1875
1876
1877 if (do_dts) {
1878 unsigned int resolution;
1879
1880 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1881 return 0;
1882
1883 dts = (msr >> 16) & 0x7F;
1884 resolution = (msr >> 27) & 0xF;
1885 fprintf(stderr, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
1886 cpu, msr, tcc_activation_temp - dts, resolution);
1887
1888#ifdef THERM_DEBUG
1889 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
1890 return 0;
1891
1892 dts = (msr >> 16) & 0x7F;
1893 dts2 = (msr >> 8) & 0x7F;
1894 fprintf(stderr, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
1895 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
1896#endif
1897 }
1898
1899 return 0;
1900}
1901
1902void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
1903{
1904 fprintf(stderr, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
1905 cpu, label,
1906 ((msr >> 15) & 1) ? "EN" : "DIS",
1907 ((msr >> 0) & 0x7FFF) * rapl_power_units,
1908 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
1909 (((msr >> 16) & 1) ? "EN" : "DIS"));
1910
1911 return;
1912}
1913
1914int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1915{
1916 unsigned long long msr;
1917 int cpu;
Len Brown889facb2012-11-08 00:48:57 -05001918
1919 if (!do_rapl)
1920 return 0;
1921
1922 /* RAPL counters are per package, so print only for 1st thread/package */
1923 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1924 return 0;
1925
1926 cpu = t->cpu_id;
1927 if (cpu_migrate(cpu)) {
1928 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1929 return -1;
1930 }
1931
1932 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
1933 return -1;
1934
Len Brown889facb2012-11-08 00:48:57 -05001935 if (verbose) {
1936 fprintf(stderr, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx "
1937 "(%f Watts, %f Joules, %f sec.)\n", cpu, msr,
Len Brown144b44b2013-11-09 00:30:16 -05001938 rapl_power_units, rapl_energy_units, rapl_time_units);
Len Brown889facb2012-11-08 00:48:57 -05001939 }
Len Brown144b44b2013-11-09 00:30:16 -05001940 if (do_rapl & RAPL_PKG_POWER_INFO) {
1941
Len Brown889facb2012-11-08 00:48:57 -05001942 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
1943 return -5;
1944
1945
1946 fprintf(stderr, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
1947 cpu, msr,
1948 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1949 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1950 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1951 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
1952
Len Brown144b44b2013-11-09 00:30:16 -05001953 }
1954 if (do_rapl & RAPL_PKG) {
1955
Len Brown889facb2012-11-08 00:48:57 -05001956 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
1957 return -9;
1958
1959 fprintf(stderr, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
1960 cpu, msr, (msr >> 63) & 1 ? "": "UN");
1961
1962 print_power_limit_msr(cpu, msr, "PKG Limit #1");
1963 fprintf(stderr, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
1964 cpu,
1965 ((msr >> 47) & 1) ? "EN" : "DIS",
1966 ((msr >> 32) & 0x7FFF) * rapl_power_units,
1967 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
1968 ((msr >> 48) & 1) ? "EN" : "DIS");
1969 }
1970
1971 if (do_rapl & RAPL_DRAM) {
1972 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
1973 return -6;
1974
1975
1976 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
1977 cpu, msr,
1978 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1979 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1980 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1981 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
1982
1983
1984 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
1985 return -9;
1986 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
1987 cpu, msr, (msr >> 31) & 1 ? "": "UN");
1988
1989 print_power_limit_msr(cpu, msr, "DRAM Limit");
1990 }
Len Brown144b44b2013-11-09 00:30:16 -05001991 if (do_rapl & RAPL_CORE_POLICY) {
Len Brown889facb2012-11-08 00:48:57 -05001992 if (verbose) {
1993 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
1994 return -7;
1995
1996 fprintf(stderr, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown144b44b2013-11-09 00:30:16 -05001997 }
1998 }
1999 if (do_rapl & RAPL_CORES) {
2000 if (verbose) {
Len Brown889facb2012-11-08 00:48:57 -05002001
2002 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
2003 return -9;
2004 fprintf(stderr, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
2005 cpu, msr, (msr >> 31) & 1 ? "": "UN");
2006 print_power_limit_msr(cpu, msr, "Cores Limit");
2007 }
2008 }
2009 if (do_rapl & RAPL_GFX) {
2010 if (verbose) {
2011 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
2012 return -8;
2013
2014 fprintf(stderr, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
2015
2016 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
2017 return -9;
2018 fprintf(stderr, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
2019 cpu, msr, (msr >> 31) & 1 ? "": "UN");
2020 print_power_limit_msr(cpu, msr, "GFX Limit");
2021 }
2022 }
2023 return 0;
2024}
2025
Len Brownd7899442015-01-23 00:12:33 -05002026/*
2027 * SNB adds support for additional MSRs:
2028 *
2029 * MSR_PKG_C7_RESIDENCY 0x000003fa
2030 * MSR_CORE_C7_RESIDENCY 0x000003fe
2031 * MSR_PKG_C2_RESIDENCY 0x0000060d
2032 */
Len Brown103a8fe2010-10-22 23:53:03 -04002033
Len Brownd7899442015-01-23 00:12:33 -05002034int has_snb_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04002035{
2036 if (!genuine_intel)
2037 return 0;
2038
2039 switch (model) {
2040 case 0x2A:
2041 case 0x2D:
Len Brown650a37f2012-06-03 23:34:44 -04002042 case 0x3A: /* IVB */
Len Brown13006512012-09-26 18:11:31 -04002043 case 0x3E: /* IVB Xeon */
Len Brown70b43402013-01-08 01:26:07 -05002044 case 0x3C: /* HSW */
2045 case 0x3F: /* HSW */
2046 case 0x45: /* HSW */
Len Brown149c2312013-03-15 10:58:02 -04002047 case 0x46: /* HSW */
Len Brown4e8e863f2014-02-27 23:28:53 -05002048 case 0x3D: /* BDW */
2049 case 0x4F: /* BDX */
2050 case 0x56: /* BDX-DE */
Len Brown103a8fe2010-10-22 23:53:03 -04002051 return 1;
2052 }
2053 return 0;
2054}
2055
Len Brownd7899442015-01-23 00:12:33 -05002056/*
2057 * HSW adds support for additional MSRs:
2058 *
2059 * MSR_PKG_C8_RESIDENCY 0x00000630
2060 * MSR_PKG_C9_RESIDENCY 0x00000631
2061 * MSR_PKG_C10_RESIDENCY 0x00000632
2062 */
2063int has_hsw_msrs(unsigned int family, unsigned int model)
Kristen Carlson Accardica587102012-11-21 05:22:43 -08002064{
2065 if (!genuine_intel)
2066 return 0;
2067
2068 switch (model) {
Len Brown4e8e863f2014-02-27 23:28:53 -05002069 case 0x45: /* HSW */
2070 case 0x3D: /* BDW */
Kristen Carlson Accardica587102012-11-21 05:22:43 -08002071 return 1;
2072 }
2073 return 0;
2074}
2075
2076
Len Brown144b44b2013-11-09 00:30:16 -05002077int is_slm(unsigned int family, unsigned int model)
2078{
2079 if (!genuine_intel)
2080 return 0;
2081 switch (model) {
2082 case 0x37: /* BYT */
2083 case 0x4D: /* AVN */
2084 return 1;
2085 }
2086 return 0;
2087}
2088
2089#define SLM_BCLK_FREQS 5
2090double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
2091
2092double slm_bclk(void)
2093{
2094 unsigned long long msr = 3;
2095 unsigned int i;
2096 double freq;
2097
2098 if (get_msr(0, MSR_FSB_FREQ, &msr))
2099 fprintf(stderr, "SLM BCLK: unknown\n");
2100
2101 i = msr & 0xf;
2102 if (i >= SLM_BCLK_FREQS) {
2103 fprintf(stderr, "SLM BCLK[%d] invalid\n", i);
2104 msr = 3;
2105 }
2106 freq = slm_freq_table[i];
2107
2108 fprintf(stderr, "SLM BCLK: %.1f Mhz\n", freq);
2109
2110 return freq;
2111}
2112
Len Brown103a8fe2010-10-22 23:53:03 -04002113double discover_bclk(unsigned int family, unsigned int model)
2114{
Len Brownd7899442015-01-23 00:12:33 -05002115 if (has_snb_msrs(family, model))
Len Brown103a8fe2010-10-22 23:53:03 -04002116 return 100.00;
Len Brown144b44b2013-11-09 00:30:16 -05002117 else if (is_slm(family, model))
2118 return slm_bclk();
Len Brown103a8fe2010-10-22 23:53:03 -04002119 else
2120 return 133.33;
2121}
2122
Len Brown889facb2012-11-08 00:48:57 -05002123/*
2124 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
2125 * the Thermal Control Circuit (TCC) activates.
2126 * This is usually equal to tjMax.
2127 *
2128 * Older processors do not have this MSR, so there we guess,
2129 * but also allow cmdline over-ride with -T.
2130 *
2131 * Several MSR temperature values are in units of degrees-C
2132 * below this value, including the Digital Thermal Sensor (DTS),
2133 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
2134 */
2135int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2136{
2137 unsigned long long msr;
2138 unsigned int target_c_local;
2139 int cpu;
2140
2141 /* tcc_activation_temp is used only for dts or ptm */
2142 if (!(do_dts || do_ptm))
2143 return 0;
2144
2145 /* this is a per-package concept */
2146 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2147 return 0;
2148
2149 cpu = t->cpu_id;
2150 if (cpu_migrate(cpu)) {
2151 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
2152 return -1;
2153 }
2154
2155 if (tcc_activation_temp_override != 0) {
2156 tcc_activation_temp = tcc_activation_temp_override;
2157 fprintf(stderr, "cpu%d: Using cmdline TCC Target (%d C)\n",
2158 cpu, tcc_activation_temp);
2159 return 0;
2160 }
2161
2162 /* Temperature Target MSR is Nehalem and newer only */
Len Brownd7899442015-01-23 00:12:33 -05002163 if (!do_nhm_platform_info)
Len Brown889facb2012-11-08 00:48:57 -05002164 goto guess;
2165
2166 if (get_msr(0, MSR_IA32_TEMPERATURE_TARGET, &msr))
2167 goto guess;
2168
Jean Delvare34821242014-05-01 11:40:19 +02002169 target_c_local = (msr >> 16) & 0xFF;
Len Brown889facb2012-11-08 00:48:57 -05002170
2171 if (verbose)
2172 fprintf(stderr, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
2173 cpu, msr, target_c_local);
2174
Jean Delvare34821242014-05-01 11:40:19 +02002175 if (!target_c_local)
Len Brown889facb2012-11-08 00:48:57 -05002176 goto guess;
2177
2178 tcc_activation_temp = target_c_local;
2179
2180 return 0;
2181
2182guess:
2183 tcc_activation_temp = TJMAX_DEFAULT;
2184 fprintf(stderr, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
2185 cpu, tcc_activation_temp);
2186
2187 return 0;
2188}
Len Brown103a8fe2010-10-22 23:53:03 -04002189void check_cpuid()
2190{
2191 unsigned int eax, ebx, ecx, edx, max_level;
2192 unsigned int fms, family, model, stepping;
2193
2194 eax = ebx = ecx = edx = 0;
2195
Josh Triplett2b928652013-08-20 17:20:14 -07002196 __get_cpuid(0, &max_level, &ebx, &ecx, &edx);
Len Brown103a8fe2010-10-22 23:53:03 -04002197
2198 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
2199 genuine_intel = 1;
2200
2201 if (verbose)
Len Brown889facb2012-11-08 00:48:57 -05002202 fprintf(stderr, "CPUID(0): %.4s%.4s%.4s ",
Len Brown103a8fe2010-10-22 23:53:03 -04002203 (char *)&ebx, (char *)&edx, (char *)&ecx);
2204
Josh Triplett2b928652013-08-20 17:20:14 -07002205 __get_cpuid(1, &fms, &ebx, &ecx, &edx);
Len Brown103a8fe2010-10-22 23:53:03 -04002206 family = (fms >> 8) & 0xf;
2207 model = (fms >> 4) & 0xf;
2208 stepping = fms & 0xf;
2209 if (family == 6 || family == 0xf)
2210 model += ((fms >> 16) & 0xf) << 4;
2211
2212 if (verbose)
2213 fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
2214 max_level, family, model, stepping, family, model, stepping);
2215
Josh Triplettb2c95d92013-08-20 17:20:18 -07002216 if (!(edx & (1 << 5)))
2217 errx(1, "CPUID: no MSR");
Len Brown103a8fe2010-10-22 23:53:03 -04002218
2219 /*
2220 * check max extended function levels of CPUID.
2221 * This is needed to check for invariant TSC.
2222 * This check is valid for both Intel and AMD.
2223 */
2224 ebx = ecx = edx = 0;
Josh Triplett2b928652013-08-20 17:20:14 -07002225 __get_cpuid(0x80000000, &max_level, &ebx, &ecx, &edx);
Len Brown103a8fe2010-10-22 23:53:03 -04002226
Len Brownd7899442015-01-23 00:12:33 -05002227 if (max_level >= 0x80000007) {
Len Brown103a8fe2010-10-22 23:53:03 -04002228
Len Brownd7899442015-01-23 00:12:33 -05002229 /*
2230 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
2231 * this check is valid for both Intel and AMD
2232 */
2233 __get_cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
2234 has_invariant_tsc = edx & (1 << 8);
2235 }
Len Brown103a8fe2010-10-22 23:53:03 -04002236
2237 /*
2238 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
2239 * this check is valid for both Intel and AMD
2240 */
2241
Josh Triplett2b928652013-08-20 17:20:14 -07002242 __get_cpuid(0x6, &eax, &ebx, &ecx, &edx);
Thomas Renninger8209e052011-01-21 15:11:19 +01002243 has_aperf = ecx & (1 << 0);
Len Brown889facb2012-11-08 00:48:57 -05002244 do_dts = eax & (1 << 0);
2245 do_ptm = eax & (1 << 6);
2246 has_epb = ecx & (1 << 3);
2247
2248 if (verbose)
Len Browna7296172015-01-23 01:33:58 -05002249 fprintf(stderr, "CPUID(6): %sAPERF, %sDTS, %sPTM, %sEPB\n",
2250 has_aperf ? "" : "No ",
2251 do_dts ? "" : "No ",
2252 do_ptm ? "" : "No ",
2253 has_epb ? "" : "No ");
Len Brown103a8fe2010-10-22 23:53:03 -04002254
Len Brownd7899442015-01-23 00:12:33 -05002255 do_nhm_platform_info = do_nhm_cstates = do_smi = has_nhm_msrs(family, model);
2256 do_snb_cstates = has_snb_msrs(family, model);
2257 do_c8_c9_c10 = has_hsw_msrs(family, model);
Len Brown144b44b2013-11-09 00:30:16 -05002258 do_slm_cstates = is_slm(family, model);
Len Brown103a8fe2010-10-22 23:53:03 -04002259 bclk = discover_bclk(family, model);
2260
Len Brownd7899442015-01-23 00:12:33 -05002261 do_nhm_turbo_ratio_limit = has_nhm_turbo_ratio_limit(family, model);
Len Brown6574a5d2012-09-21 00:01:31 -04002262 do_ivt_turbo_ratio_limit = has_ivt_turbo_ratio_limit(family, model);
Len Brown889facb2012-11-08 00:48:57 -05002263 rapl_probe(family, model);
Len Brown3a9a9412014-08-15 02:39:52 -04002264 perf_limit_reasons_probe(family, model);
Len Brown889facb2012-11-08 00:48:57 -05002265
2266 return;
Len Brown103a8fe2010-10-22 23:53:03 -04002267}
2268
2269
2270void usage()
2271{
Andy Shevchenkof591c382014-01-23 17:13:14 +02002272 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 -07002273 progname);
Len Brown103a8fe2010-10-22 23:53:03 -04002274}
2275
2276
2277/*
2278 * in /dev/cpu/ return success for names that are numbers
2279 * ie. filter out ".", "..", "microcode".
2280 */
2281int dir_filter(const struct dirent *dirp)
2282{
2283 if (isdigit(dirp->d_name[0]))
2284 return 1;
2285 else
2286 return 0;
2287}
2288
2289int open_dev_cpu_msr(int dummy1)
2290{
2291 return 0;
2292}
2293
Len Brownc98d5d92012-06-04 00:56:40 -04002294void topology_probe()
2295{
2296 int i;
2297 int max_core_id = 0;
2298 int max_package_id = 0;
2299 int max_siblings = 0;
2300 struct cpu_topology {
2301 int core_id;
2302 int physical_package_id;
2303 } *cpus;
2304
2305 /* Initialize num_cpus, max_cpu_num */
2306 topo.num_cpus = 0;
2307 topo.max_cpu_num = 0;
2308 for_all_proc_cpus(count_cpus);
2309 if (!summary_only && topo.num_cpus > 1)
2310 show_cpu = 1;
2311
2312 if (verbose > 1)
2313 fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
2314
2315 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
Josh Triplettb2c95d92013-08-20 17:20:18 -07002316 if (cpus == NULL)
2317 err(1, "calloc cpus");
Len Brownc98d5d92012-06-04 00:56:40 -04002318
2319 /*
2320 * Allocate and initialize cpu_present_set
2321 */
2322 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07002323 if (cpu_present_set == NULL)
2324 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04002325 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2326 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
2327 for_all_proc_cpus(mark_cpu_present);
2328
2329 /*
2330 * Allocate and initialize cpu_affinity_set
2331 */
2332 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07002333 if (cpu_affinity_set == NULL)
2334 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04002335 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2336 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
2337
2338
2339 /*
2340 * For online cpus
2341 * find max_core_id, max_package_id
2342 */
2343 for (i = 0; i <= topo.max_cpu_num; ++i) {
2344 int siblings;
2345
2346 if (cpu_is_not_present(i)) {
2347 if (verbose > 1)
2348 fprintf(stderr, "cpu%d NOT PRESENT\n", i);
2349 continue;
2350 }
2351 cpus[i].core_id = get_core_id(i);
2352 if (cpus[i].core_id > max_core_id)
2353 max_core_id = cpus[i].core_id;
2354
2355 cpus[i].physical_package_id = get_physical_package_id(i);
2356 if (cpus[i].physical_package_id > max_package_id)
2357 max_package_id = cpus[i].physical_package_id;
2358
2359 siblings = get_num_ht_siblings(i);
2360 if (siblings > max_siblings)
2361 max_siblings = siblings;
2362 if (verbose > 1)
2363 fprintf(stderr, "cpu %d pkg %d core %d\n",
2364 i, cpus[i].physical_package_id, cpus[i].core_id);
2365 }
2366 topo.num_cores_per_pkg = max_core_id + 1;
2367 if (verbose > 1)
2368 fprintf(stderr, "max_core_id %d, sizing for %d cores per package\n",
2369 max_core_id, topo.num_cores_per_pkg);
2370 if (!summary_only && topo.num_cores_per_pkg > 1)
2371 show_core = 1;
2372
2373 topo.num_packages = max_package_id + 1;
2374 if (verbose > 1)
2375 fprintf(stderr, "max_package_id %d, sizing for %d packages\n",
2376 max_package_id, topo.num_packages);
2377 if (!summary_only && topo.num_packages > 1)
2378 show_pkg = 1;
2379
2380 topo.num_threads_per_core = max_siblings;
2381 if (verbose > 1)
2382 fprintf(stderr, "max_siblings %d\n", max_siblings);
2383
2384 free(cpus);
2385}
2386
2387void
2388allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
2389{
2390 int i;
2391
2392 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
2393 topo.num_packages, sizeof(struct thread_data));
2394 if (*t == NULL)
2395 goto error;
2396
2397 for (i = 0; i < topo.num_threads_per_core *
2398 topo.num_cores_per_pkg * topo.num_packages; i++)
2399 (*t)[i].cpu_id = -1;
2400
2401 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
2402 sizeof(struct core_data));
2403 if (*c == NULL)
2404 goto error;
2405
2406 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
2407 (*c)[i].core_id = -1;
2408
2409 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
2410 if (*p == NULL)
2411 goto error;
2412
2413 for (i = 0; i < topo.num_packages; i++)
2414 (*p)[i].package_id = i;
2415
2416 return;
2417error:
Josh Triplettb2c95d92013-08-20 17:20:18 -07002418 err(1, "calloc counters");
Len Brownc98d5d92012-06-04 00:56:40 -04002419}
2420/*
2421 * init_counter()
2422 *
2423 * set cpu_id, core_num, pkg_num
2424 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
2425 *
2426 * increment topo.num_cores when 1st core in pkg seen
2427 */
2428void init_counter(struct thread_data *thread_base, struct core_data *core_base,
2429 struct pkg_data *pkg_base, int thread_num, int core_num,
2430 int pkg_num, int cpu_id)
2431{
2432 struct thread_data *t;
2433 struct core_data *c;
2434 struct pkg_data *p;
2435
2436 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
2437 c = GET_CORE(core_base, core_num, pkg_num);
2438 p = GET_PKG(pkg_base, pkg_num);
2439
2440 t->cpu_id = cpu_id;
2441 if (thread_num == 0) {
2442 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
2443 if (cpu_is_first_core_in_package(cpu_id))
2444 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
2445 }
2446
2447 c->core_id = core_num;
2448 p->package_id = pkg_num;
2449}
2450
2451
2452int initialize_counters(int cpu_id)
2453{
2454 int my_thread_id, my_core_id, my_package_id;
2455
2456 my_package_id = get_physical_package_id(cpu_id);
2457 my_core_id = get_core_id(cpu_id);
2458
2459 if (cpu_is_first_sibling_in_core(cpu_id)) {
2460 my_thread_id = 0;
2461 topo.num_cores++;
2462 } else {
2463 my_thread_id = 1;
2464 }
2465
2466 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2467 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2468 return 0;
2469}
2470
2471void allocate_output_buffer()
2472{
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02002473 output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
Len Brownc98d5d92012-06-04 00:56:40 -04002474 outp = output_buffer;
Josh Triplettb2c95d92013-08-20 17:20:18 -07002475 if (outp == NULL)
2476 err(-1, "calloc output buffer");
Len Brownc98d5d92012-06-04 00:56:40 -04002477}
2478
2479void setup_all_buffers(void)
2480{
2481 topology_probe();
2482 allocate_counters(&thread_even, &core_even, &package_even);
2483 allocate_counters(&thread_odd, &core_odd, &package_odd);
2484 allocate_output_buffer();
2485 for_all_proc_cpus(initialize_counters);
2486}
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02002487
Len Brown103a8fe2010-10-22 23:53:03 -04002488void turbostat_init()
2489{
Len Brown103a8fe2010-10-22 23:53:03 -04002490 check_dev_msr();
Len Brown98481e72014-08-15 00:36:50 -04002491 check_permissions();
2492 check_cpuid();
Len Brown103a8fe2010-10-22 23:53:03 -04002493
Len Brownc98d5d92012-06-04 00:56:40 -04002494 setup_all_buffers();
Len Brown103a8fe2010-10-22 23:53:03 -04002495
2496 if (verbose)
Len Brownc98d5d92012-06-04 00:56:40 -04002497 print_verbose_header();
Len Brown889facb2012-11-08 00:48:57 -05002498
2499 if (verbose)
2500 for_all_cpus(print_epb, ODD_COUNTERS);
2501
2502 if (verbose)
Len Brown3a9a9412014-08-15 02:39:52 -04002503 for_all_cpus(print_perf_limit, ODD_COUNTERS);
2504
2505 if (verbose)
Len Brown889facb2012-11-08 00:48:57 -05002506 for_all_cpus(print_rapl, ODD_COUNTERS);
2507
2508 for_all_cpus(set_temperature_target, ODD_COUNTERS);
2509
2510 if (verbose)
2511 for_all_cpus(print_thermal, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04002512}
2513
2514int fork_it(char **argv)
2515{
Len Brown103a8fe2010-10-22 23:53:03 -04002516 pid_t child_pid;
Len Brownd91bb172012-11-01 00:08:19 -04002517 int status;
Len Brownd15cf7c2012-06-03 23:24:00 -04002518
Len Brownd91bb172012-11-01 00:08:19 -04002519 status = for_all_cpus(get_counters, EVEN_COUNTERS);
2520 if (status)
2521 exit(status);
Len Brownc98d5d92012-06-04 00:56:40 -04002522 /* clear affinity side-effect of get_counters() */
2523 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
Len Brown103a8fe2010-10-22 23:53:03 -04002524 gettimeofday(&tv_even, (struct timezone *)NULL);
2525
2526 child_pid = fork();
2527 if (!child_pid) {
2528 /* child */
2529 execvp(argv[0], argv);
2530 } else {
Len Brown103a8fe2010-10-22 23:53:03 -04002531
2532 /* parent */
Josh Triplettb2c95d92013-08-20 17:20:18 -07002533 if (child_pid == -1)
2534 err(1, "fork");
Len Brown103a8fe2010-10-22 23:53:03 -04002535
2536 signal(SIGINT, SIG_IGN);
2537 signal(SIGQUIT, SIG_IGN);
Josh Triplettb2c95d92013-08-20 17:20:18 -07002538 if (waitpid(child_pid, &status, 0) == -1)
2539 err(status, "waitpid");
Len Brown103a8fe2010-10-22 23:53:03 -04002540 }
Len Brownc98d5d92012-06-04 00:56:40 -04002541 /*
2542 * n.b. fork_it() does not check for errors from for_all_cpus()
2543 * because re-starting is problematic when forking
2544 */
2545 for_all_cpus(get_counters, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04002546 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002547 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04002548 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
2549 compute_average(EVEN_COUNTERS);
2550 format_all_counters(EVEN_COUNTERS);
2551 flush_stderr();
Len Brown103a8fe2010-10-22 23:53:03 -04002552
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07002553 fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
Len Brown103a8fe2010-10-22 23:53:03 -04002554
Len Brownd91bb172012-11-01 00:08:19 -04002555 return status;
Len Brown103a8fe2010-10-22 23:53:03 -04002556}
2557
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02002558int get_and_dump_counters(void)
2559{
2560 int status;
2561
2562 status = for_all_cpus(get_counters, ODD_COUNTERS);
2563 if (status)
2564 return status;
2565
2566 status = for_all_cpus(dump_counters, ODD_COUNTERS);
2567 if (status)
2568 return status;
2569
2570 flush_stdout();
2571
2572 return status;
2573}
2574
Len Brown103a8fe2010-10-22 23:53:03 -04002575void cmdline(int argc, char **argv)
2576{
2577 int opt;
2578
2579 progname = argv[0];
2580
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02002581 while ((opt = getopt(argc, argv, "+pPsSvi:c:C:m:M:RJT:")) != -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04002582 switch (opt) {
Len Brownf9240812012-10-06 15:26:31 -04002583 case 'p':
Len Brownc98d5d92012-06-04 00:56:40 -04002584 show_core_only++;
2585 break;
Len Brownf9240812012-10-06 15:26:31 -04002586 case 'P':
Len Brownc98d5d92012-06-04 00:56:40 -04002587 show_pkg_only++;
2588 break;
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02002589 case 's':
2590 dump_only++;
2591 break;
Len Brownf9240812012-10-06 15:26:31 -04002592 case 'S':
Len Browne23da032012-02-06 18:37:16 -05002593 summary_only++;
2594 break;
Len Brown103a8fe2010-10-22 23:53:03 -04002595 case 'v':
2596 verbose++;
2597 break;
2598 case 'i':
2599 interval_sec = atoi(optarg);
2600 break;
Len Brownf9240812012-10-06 15:26:31 -04002601 case 'c':
Len Brown8e180f32012-09-22 01:25:08 -04002602 sscanf(optarg, "%x", &extra_delta_offset32);
2603 break;
Len Brownf9240812012-10-06 15:26:31 -04002604 case 'C':
Len Brown8e180f32012-09-22 01:25:08 -04002605 sscanf(optarg, "%x", &extra_delta_offset64);
2606 break;
Len Brown2f32edf2012-09-21 23:45:46 -04002607 case 'm':
2608 sscanf(optarg, "%x", &extra_msr_offset32);
Len Brown2f32edf2012-09-21 23:45:46 -04002609 break;
2610 case 'M':
2611 sscanf(optarg, "%x", &extra_msr_offset64);
Len Brown103a8fe2010-10-22 23:53:03 -04002612 break;
Len Brown889facb2012-11-08 00:48:57 -05002613 case 'R':
2614 rapl_verbose++;
2615 break;
2616 case 'T':
2617 tcc_activation_temp_override = atoi(optarg);
2618 break;
Dirk Brandewie5c56be92013-12-16 10:23:41 -08002619 case 'J':
2620 rapl_joules++;
2621 break;
2622
Len Brown103a8fe2010-10-22 23:53:03 -04002623 default:
2624 usage();
2625 }
2626 }
2627}
2628
2629int main(int argc, char **argv)
2630{
2631 cmdline(argc, argv);
2632
Len Brown889facb2012-11-08 00:48:57 -05002633 if (verbose)
Len Browna7296172015-01-23 01:33:58 -05002634 fprintf(stderr, "turbostat v3.9 23-Jan, 2015"
Len Brown103a8fe2010-10-22 23:53:03 -04002635 " - Len Brown <lenb@kernel.org>\n");
Len Brown103a8fe2010-10-22 23:53:03 -04002636
2637 turbostat_init();
2638
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02002639 /* dump counters and exit */
2640 if (dump_only)
2641 return get_and_dump_counters();
2642
Len Brown103a8fe2010-10-22 23:53:03 -04002643 /*
2644 * if any params left, it must be a command to fork
2645 */
2646 if (argc - optind)
2647 return fork_it(argv + optind);
2648 else
2649 turbostat_loop();
2650
2651 return 0;
2652}