blob: f05a3f7b5d4c0586d3052842f728fdb370fee96b [file] [log] [blame]
Len Brown103a8fe2010-10-22 23:53:03 -04001/*
2 * turbostat -- show CPU frequency and C-state residency
3 * on modern Intel turbo-capable processors.
4 *
Len Brown144b44b2013-11-09 00:30:16 -05005 * Copyright (c) 2013 Intel Corporation.
Len Brown103a8fe2010-10-22 23:53:03 -04006 * Len Brown <len.brown@intel.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
Len Brown88c32812012-03-29 21:44:40 -040022#define _GNU_SOURCE
Josh Triplettb731f312013-08-20 17:20:12 -070023#include MSRHEADER
Josh Triplett95aebc42013-08-20 17:20:16 -070024#include <stdarg.h>
Len Brown103a8fe2010-10-22 23:53:03 -040025#include <stdio.h>
Josh Triplettb2c95d92013-08-20 17:20:18 -070026#include <err.h>
Len Brown103a8fe2010-10-22 23:53:03 -040027#include <unistd.h>
28#include <sys/types.h>
29#include <sys/wait.h>
30#include <sys/stat.h>
31#include <sys/resource.h>
32#include <fcntl.h>
33#include <signal.h>
34#include <sys/time.h>
35#include <stdlib.h>
Len Brownd8af6f52015-02-10 01:56:38 -050036#include <getopt.h>
Len Brown103a8fe2010-10-22 23:53:03 -040037#include <dirent.h>
38#include <string.h>
39#include <ctype.h>
Len Brown88c32812012-03-29 21:44:40 -040040#include <sched.h>
Josh Triplett2b928652013-08-20 17:20:14 -070041#include <cpuid.h>
Len Brown98481e72014-08-15 00:36:50 -040042#include <linux/capability.h>
43#include <errno.h>
Len Brown103a8fe2010-10-22 23:53:03 -040044
Len Brown103a8fe2010-10-22 23:53:03 -040045char *proc_stat = "/proc/stat";
Len Brownd8af6f52015-02-10 01:56:38 -050046unsigned int interval_sec = 5;
47unsigned int debug;
48unsigned int rapl_joules;
49unsigned int summary_only;
50unsigned int dump_only;
Len Brown103a8fe2010-10-22 23:53:03 -040051unsigned int skip_c0;
52unsigned int skip_c1;
53unsigned int do_nhm_cstates;
54unsigned int do_snb_cstates;
Len Brownee7e38e2015-02-09 23:39:45 -050055unsigned int do_pc2;
56unsigned int do_pc3;
57unsigned int do_pc6;
58unsigned int do_pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -080059unsigned int do_c8_c9_c10;
Len Brown144b44b2013-11-09 00:30:16 -050060unsigned int do_slm_cstates;
61unsigned int use_c1_residency_msr;
Len Brown103a8fe2010-10-22 23:53:03 -040062unsigned int has_aperf;
Len Brown889facb2012-11-08 00:48:57 -050063unsigned int has_epb;
Len Brownfc04cc62014-02-06 00:55:19 -050064unsigned int units = 1000000; /* MHz etc */
Len Brown103a8fe2010-10-22 23:53:03 -040065unsigned int genuine_intel;
66unsigned int has_invariant_tsc;
Len Brownd7899442015-01-23 00:12:33 -050067unsigned int do_nhm_platform_info;
Len Brown2f32edf2012-09-21 23:45:46 -040068unsigned int extra_msr_offset32;
69unsigned int extra_msr_offset64;
Len Brown8e180f32012-09-22 01:25:08 -040070unsigned int extra_delta_offset32;
71unsigned int extra_delta_offset64;
Len Brown1ed51012013-02-10 17:19:24 -050072int do_smi;
Len Brown103a8fe2010-10-22 23:53:03 -040073double bclk;
74unsigned int show_pkg;
75unsigned int show_core;
76unsigned int show_cpu;
Len Brownc98d5d92012-06-04 00:56:40 -040077unsigned int show_pkg_only;
78unsigned int show_core_only;
79char *output_buffer, *outp;
Len Brown889facb2012-11-08 00:48:57 -050080unsigned int do_rapl;
81unsigned int do_dts;
82unsigned int do_ptm;
83unsigned int tcc_activation_temp;
84unsigned int tcc_activation_temp_override;
85double rapl_power_units, rapl_energy_units, rapl_time_units;
86double rapl_joule_counter_range;
Len Brown3a9a9412014-08-15 02:39:52 -040087unsigned int do_core_perf_limit_reasons;
88unsigned int do_gfx_perf_limit_reasons;
89unsigned int do_ring_perf_limit_reasons;
Len Brown889facb2012-11-08 00:48:57 -050090
Len Browne6f9bb32013-12-03 02:19:19 -050091#define RAPL_PKG (1 << 0)
92 /* 0x610 MSR_PKG_POWER_LIMIT */
93 /* 0x611 MSR_PKG_ENERGY_STATUS */
94#define RAPL_PKG_PERF_STATUS (1 << 1)
95 /* 0x613 MSR_PKG_PERF_STATUS */
96#define RAPL_PKG_POWER_INFO (1 << 2)
97 /* 0x614 MSR_PKG_POWER_INFO */
98
99#define RAPL_DRAM (1 << 3)
100 /* 0x618 MSR_DRAM_POWER_LIMIT */
101 /* 0x619 MSR_DRAM_ENERGY_STATUS */
102 /* 0x61c MSR_DRAM_POWER_INFO */
103#define RAPL_DRAM_PERF_STATUS (1 << 4)
104 /* 0x61b MSR_DRAM_PERF_STATUS */
105
106#define RAPL_CORES (1 << 5)
107 /* 0x638 MSR_PP0_POWER_LIMIT */
108 /* 0x639 MSR_PP0_ENERGY_STATUS */
109#define RAPL_CORE_POLICY (1 << 6)
110 /* 0x63a MSR_PP0_POLICY */
111
112
113#define RAPL_GFX (1 << 7)
114 /* 0x640 MSR_PP1_POWER_LIMIT */
115 /* 0x641 MSR_PP1_ENERGY_STATUS */
116 /* 0x642 MSR_PP1_POLICY */
Len Brown889facb2012-11-08 00:48:57 -0500117#define TJMAX_DEFAULT 100
118
119#define MAX(a, b) ((a) > (b) ? (a) : (b))
Len Brown103a8fe2010-10-22 23:53:03 -0400120
121int aperf_mperf_unstable;
122int backwards_count;
123char *progname;
Len Brown103a8fe2010-10-22 23:53:03 -0400124
Len Brownc98d5d92012-06-04 00:56:40 -0400125cpu_set_t *cpu_present_set, *cpu_affinity_set;
126size_t cpu_present_setsize, cpu_affinity_setsize;
Len Brown103a8fe2010-10-22 23:53:03 -0400127
Len Brownc98d5d92012-06-04 00:56:40 -0400128struct thread_data {
129 unsigned long long tsc;
130 unsigned long long aperf;
131 unsigned long long mperf;
Len Brown144b44b2013-11-09 00:30:16 -0500132 unsigned long long c1;
Len Brown2f32edf2012-09-21 23:45:46 -0400133 unsigned long long extra_msr64;
Len Brown8e180f32012-09-22 01:25:08 -0400134 unsigned long long extra_delta64;
135 unsigned long long extra_msr32;
136 unsigned long long extra_delta32;
Len Brown1ed51012013-02-10 17:19:24 -0500137 unsigned int smi_count;
Len Brownc98d5d92012-06-04 00:56:40 -0400138 unsigned int cpu_id;
139 unsigned int flags;
140#define CPU_IS_FIRST_THREAD_IN_CORE 0x2
141#define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
142} *thread_even, *thread_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400143
Len Brownc98d5d92012-06-04 00:56:40 -0400144struct core_data {
145 unsigned long long c3;
146 unsigned long long c6;
147 unsigned long long c7;
Len Brown889facb2012-11-08 00:48:57 -0500148 unsigned int core_temp_c;
Len Brownc98d5d92012-06-04 00:56:40 -0400149 unsigned int core_id;
150} *core_even, *core_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400151
Len Brownc98d5d92012-06-04 00:56:40 -0400152struct pkg_data {
153 unsigned long long pc2;
154 unsigned long long pc3;
155 unsigned long long pc6;
156 unsigned long long pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800157 unsigned long long pc8;
158 unsigned long long pc9;
159 unsigned long long pc10;
Len Brownc98d5d92012-06-04 00:56:40 -0400160 unsigned int package_id;
Len Brown889facb2012-11-08 00:48:57 -0500161 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
162 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
163 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
164 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
165 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
166 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
167 unsigned int pkg_temp_c;
168
Len Brownc98d5d92012-06-04 00:56:40 -0400169} *package_even, *package_odd;
170
171#define ODD_COUNTERS thread_odd, core_odd, package_odd
172#define EVEN_COUNTERS thread_even, core_even, package_even
173
174#define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
175 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
176 topo.num_threads_per_core + \
177 (core_no) * topo.num_threads_per_core + (thread_no))
178#define GET_CORE(core_base, core_no, pkg_no) \
179 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
180#define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
181
182struct system_summary {
183 struct thread_data threads;
184 struct core_data cores;
185 struct pkg_data packages;
186} sum, average;
187
188
189struct topo_params {
190 int num_packages;
191 int num_cpus;
192 int num_cores;
193 int max_cpu_num;
194 int num_cores_per_pkg;
195 int num_threads_per_core;
196} topo;
197
198struct timeval tv_even, tv_odd, tv_delta;
199
200void setup_all_buffers(void);
201
202int cpu_is_not_present(int cpu)
Len Brownd15cf7c2012-06-03 23:24:00 -0400203{
Len Brownc98d5d92012-06-04 00:56:40 -0400204 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brownd15cf7c2012-06-03 23:24:00 -0400205}
Len Brown88c32812012-03-29 21:44:40 -0400206/*
Len Brownc98d5d92012-06-04 00:56:40 -0400207 * run func(thread, core, package) in topology order
208 * skip non-present cpus
Len Brown88c32812012-03-29 21:44:40 -0400209 */
Len Brownd15cf7c2012-06-03 23:24:00 -0400210
Len Brownc98d5d92012-06-04 00:56:40 -0400211int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
212 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
Len Brown88c32812012-03-29 21:44:40 -0400213{
Len Brownc98d5d92012-06-04 00:56:40 -0400214 int retval, pkg_no, core_no, thread_no;
215
216 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
217 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
218 for (thread_no = 0; thread_no <
219 topo.num_threads_per_core; ++thread_no) {
220 struct thread_data *t;
221 struct core_data *c;
222 struct pkg_data *p;
223
224 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
225
226 if (cpu_is_not_present(t->cpu_id))
227 continue;
228
229 c = GET_CORE(core_base, core_no, pkg_no);
230 p = GET_PKG(pkg_base, pkg_no);
231
232 retval = func(t, c, p);
233 if (retval)
234 return retval;
235 }
236 }
237 }
238 return 0;
Len Brown88c32812012-03-29 21:44:40 -0400239}
240
241int cpu_migrate(int cpu)
242{
Len Brownc98d5d92012-06-04 00:56:40 -0400243 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
244 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
245 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
Len Brown88c32812012-03-29 21:44:40 -0400246 return -1;
247 else
248 return 0;
249}
250
Len Brown15aaa342012-03-29 22:19:58 -0400251int get_msr(int cpu, off_t offset, unsigned long long *msr)
Len Brown103a8fe2010-10-22 23:53:03 -0400252{
253 ssize_t retval;
Len Brown103a8fe2010-10-22 23:53:03 -0400254 char pathname[32];
255 int fd;
256
257 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
258 fd = open(pathname, O_RDONLY);
Len Brown15aaa342012-03-29 22:19:58 -0400259 if (fd < 0)
Len Brown98481e72014-08-15 00:36:50 -0400260 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 -0400261
Len Brown15aaa342012-03-29 22:19:58 -0400262 retval = pread(fd, msr, sizeof *msr, offset);
Len Brown103a8fe2010-10-22 23:53:03 -0400263 close(fd);
Len Brown15aaa342012-03-29 22:19:58 -0400264
Len Brown98481e72014-08-15 00:36:50 -0400265 if (retval != sizeof *msr)
266 err(-1, "%s offset 0x%llx read failed", pathname, (unsigned long long)offset);
Len Brown15aaa342012-03-29 22:19:58 -0400267
268 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400269}
270
Len Brownfc04cc62014-02-06 00:55:19 -0500271/*
272 * Example Format w/ field column widths:
273 *
Len Browne7c95ff2014-08-14 21:22:13 -0400274 * 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
275 * 123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678
Len Brownfc04cc62014-02-06 00:55:19 -0500276 */
277
Len Browna829eb42011-02-10 23:36:34 -0500278void print_header(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400279{
280 if (show_pkg)
Len Browne7c95ff2014-08-14 21:22:13 -0400281 outp += sprintf(outp, " Package");
Len Brown103a8fe2010-10-22 23:53:03 -0400282 if (show_core)
Len Browne7c95ff2014-08-14 21:22:13 -0400283 outp += sprintf(outp, " Core");
Len Brown103a8fe2010-10-22 23:53:03 -0400284 if (show_cpu)
Len Browne7c95ff2014-08-14 21:22:13 -0400285 outp += sprintf(outp, " CPU");
Len Brown103a8fe2010-10-22 23:53:03 -0400286 if (has_aperf)
Len Browne7c95ff2014-08-14 21:22:13 -0400287 outp += sprintf(outp, " Avg_MHz");
Len Brownd7899442015-01-23 00:12:33 -0500288 if (has_aperf)
Len Browne7c95ff2014-08-14 21:22:13 -0400289 outp += sprintf(outp, " %%Busy");
Len Brownfc04cc62014-02-06 00:55:19 -0500290 if (has_aperf)
Len Browne7c95ff2014-08-14 21:22:13 -0400291 outp += sprintf(outp, " Bzy_MHz");
292 outp += sprintf(outp, " TSC_MHz");
Len Brown1cc21f72015-02-23 00:34:57 -0500293
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 Brown1cc21f72015-02-23 00:34:57 -0500302
303 if (!debug)
304 goto done;
305
306 if (do_smi)
307 outp += sprintf(outp, " SMI");
308
Len Brown103a8fe2010-10-22 23:53:03 -0400309 if (do_nhm_cstates)
Len Browne7c95ff2014-08-14 21:22:13 -0400310 outp += sprintf(outp, " CPU%%c1");
Len Brown144b44b2013-11-09 00:30:16 -0500311 if (do_nhm_cstates && !do_slm_cstates)
Len Browne7c95ff2014-08-14 21:22:13 -0400312 outp += sprintf(outp, " CPU%%c3");
Len Brown103a8fe2010-10-22 23:53:03 -0400313 if (do_nhm_cstates)
Len Browne7c95ff2014-08-14 21:22:13 -0400314 outp += sprintf(outp, " CPU%%c6");
Len Brown103a8fe2010-10-22 23:53:03 -0400315 if (do_snb_cstates)
Len Browne7c95ff2014-08-14 21:22:13 -0400316 outp += sprintf(outp, " CPU%%c7");
Len Brown889facb2012-11-08 00:48:57 -0500317
318 if (do_dts)
Len Browne7c95ff2014-08-14 21:22:13 -0400319 outp += sprintf(outp, " CoreTmp");
Len Brown889facb2012-11-08 00:48:57 -0500320 if (do_ptm)
Len Browne7c95ff2014-08-14 21:22:13 -0400321 outp += sprintf(outp, " PkgTmp");
Len Brown889facb2012-11-08 00:48:57 -0500322
Len Brownee7e38e2015-02-09 23:39:45 -0500323 if (do_pc2)
Len Browne7c95ff2014-08-14 21:22:13 -0400324 outp += sprintf(outp, " Pkg%%pc2");
Len Brownee7e38e2015-02-09 23:39:45 -0500325 if (do_pc3)
Len Browne7c95ff2014-08-14 21:22:13 -0400326 outp += sprintf(outp, " Pkg%%pc3");
Len Brownee7e38e2015-02-09 23:39:45 -0500327 if (do_pc6)
Len Browne7c95ff2014-08-14 21:22:13 -0400328 outp += sprintf(outp, " Pkg%%pc6");
Len Brownee7e38e2015-02-09 23:39:45 -0500329 if (do_pc7)
Len Browne7c95ff2014-08-14 21:22:13 -0400330 outp += sprintf(outp, " Pkg%%pc7");
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800331 if (do_c8_c9_c10) {
Len Browne7c95ff2014-08-14 21:22:13 -0400332 outp += sprintf(outp, " Pkg%%pc8");
333 outp += sprintf(outp, " Pkg%%pc9");
334 outp += sprintf(outp, " Pk%%pc10");
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800335 }
Len Brown103a8fe2010-10-22 23:53:03 -0400336
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800337 if (do_rapl && !rapl_joules) {
338 if (do_rapl & RAPL_PKG)
Len Browne7c95ff2014-08-14 21:22:13 -0400339 outp += sprintf(outp, " PkgWatt");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800340 if (do_rapl & RAPL_CORES)
Len Browne7c95ff2014-08-14 21:22:13 -0400341 outp += sprintf(outp, " CorWatt");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800342 if (do_rapl & RAPL_GFX)
Len Browne7c95ff2014-08-14 21:22:13 -0400343 outp += sprintf(outp, " GFXWatt");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800344 if (do_rapl & RAPL_DRAM)
Len Browne7c95ff2014-08-14 21:22:13 -0400345 outp += sprintf(outp, " RAMWatt");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800346 if (do_rapl & RAPL_PKG_PERF_STATUS)
Len Browne7c95ff2014-08-14 21:22:13 -0400347 outp += sprintf(outp, " PKG_%%");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800348 if (do_rapl & RAPL_DRAM_PERF_STATUS)
Len Browne7c95ff2014-08-14 21:22:13 -0400349 outp += sprintf(outp, " RAM_%%");
Len Brownd7899442015-01-23 00:12:33 -0500350 } else if (do_rapl && rapl_joules) {
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800351 if (do_rapl & RAPL_PKG)
Len Browne7c95ff2014-08-14 21:22:13 -0400352 outp += sprintf(outp, " Pkg_J");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800353 if (do_rapl & RAPL_CORES)
Len Browne7c95ff2014-08-14 21:22:13 -0400354 outp += sprintf(outp, " Cor_J");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800355 if (do_rapl & RAPL_GFX)
Len Browne7c95ff2014-08-14 21:22:13 -0400356 outp += sprintf(outp, " GFX_J");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800357 if (do_rapl & RAPL_DRAM)
Len Browne7c95ff2014-08-14 21:22:13 -0400358 outp += sprintf(outp, " RAM_W");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800359 if (do_rapl & RAPL_PKG_PERF_STATUS)
Len Browne7c95ff2014-08-14 21:22:13 -0400360 outp += sprintf(outp, " PKG_%%");
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800361 if (do_rapl & RAPL_DRAM_PERF_STATUS)
Len Browne7c95ff2014-08-14 21:22:13 -0400362 outp += sprintf(outp, " RAM_%%");
363 outp += sprintf(outp, " time");
Len Brown889facb2012-11-08 00:48:57 -0500364
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800365 }
Len Brown1cc21f72015-02-23 00:34:57 -0500366 done:
Len Brownc98d5d92012-06-04 00:56:40 -0400367 outp += sprintf(outp, "\n");
Len Brown103a8fe2010-10-22 23:53:03 -0400368}
369
Len Brownc98d5d92012-06-04 00:56:40 -0400370int dump_counters(struct thread_data *t, struct core_data *c,
371 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400372{
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200373 outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400374
Len Brownc98d5d92012-06-04 00:56:40 -0400375 if (t) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200376 outp += sprintf(outp, "CPU: %d flags 0x%x\n",
377 t->cpu_id, t->flags);
378 outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
379 outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
380 outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
381 outp += sprintf(outp, "c1: %016llX\n", t->c1);
382 outp += sprintf(outp, "msr0x%x: %08llX\n",
Len Brown8e180f32012-09-22 01:25:08 -0400383 extra_delta_offset32, t->extra_delta32);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200384 outp += sprintf(outp, "msr0x%x: %016llX\n",
Len Brown8e180f32012-09-22 01:25:08 -0400385 extra_delta_offset64, t->extra_delta64);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200386 outp += sprintf(outp, "msr0x%x: %08llX\n",
Len Brown2f32edf2012-09-21 23:45:46 -0400387 extra_msr_offset32, t->extra_msr32);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200388 outp += sprintf(outp, "msr0x%x: %016llX\n",
Len Brown2f32edf2012-09-21 23:45:46 -0400389 extra_msr_offset64, t->extra_msr64);
Len Brown1ed51012013-02-10 17:19:24 -0500390 if (do_smi)
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200391 outp += sprintf(outp, "SMI: %08X\n", t->smi_count);
Len Brownc98d5d92012-06-04 00:56:40 -0400392 }
Len Brown103a8fe2010-10-22 23:53:03 -0400393
Len Brownc98d5d92012-06-04 00:56:40 -0400394 if (c) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200395 outp += sprintf(outp, "core: %d\n", c->core_id);
396 outp += sprintf(outp, "c3: %016llX\n", c->c3);
397 outp += sprintf(outp, "c6: %016llX\n", c->c6);
398 outp += sprintf(outp, "c7: %016llX\n", c->c7);
399 outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
Len Brownc98d5d92012-06-04 00:56:40 -0400400 }
401
402 if (p) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200403 outp += sprintf(outp, "package: %d\n", p->package_id);
404 outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
Len Brownee7e38e2015-02-09 23:39:45 -0500405 if (do_pc3)
406 outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
407 if (do_pc6)
408 outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
409 if (do_pc7)
410 outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200411 outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
412 outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
413 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
414 outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
415 outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
416 outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
417 outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
418 outp += sprintf(outp, "Throttle PKG: %0X\n",
419 p->rapl_pkg_perf_status);
420 outp += sprintf(outp, "Throttle RAM: %0X\n",
421 p->rapl_dram_perf_status);
422 outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
Len Brownc98d5d92012-06-04 00:56:40 -0400423 }
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200424
425 outp += sprintf(outp, "\n");
426
Len Brownc98d5d92012-06-04 00:56:40 -0400427 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400428}
429
Len Browne23da032012-02-06 18:37:16 -0500430/*
431 * column formatting convention & formats
Len Browne23da032012-02-06 18:37:16 -0500432 */
Len Brownc98d5d92012-06-04 00:56:40 -0400433int format_counters(struct thread_data *t, struct core_data *c,
434 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400435{
436 double interval_float;
Len Brownfc04cc62014-02-06 00:55:19 -0500437 char *fmt8;
Len Brown103a8fe2010-10-22 23:53:03 -0400438
Len Brownc98d5d92012-06-04 00:56:40 -0400439 /* if showing only 1st thread in core and this isn't one, bail out */
440 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
441 return 0;
442
443 /* if showing only 1st thread in pkg and this isn't one, bail out */
444 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
445 return 0;
446
Len Brown103a8fe2010-10-22 23:53:03 -0400447 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
448
Len Brownc98d5d92012-06-04 00:56:40 -0400449 /* topo columns, print blanks on 1st (average) line */
450 if (t == &average.threads) {
Len Brown103a8fe2010-10-22 23:53:03 -0400451 if (show_pkg)
Len Brownfc04cc62014-02-06 00:55:19 -0500452 outp += sprintf(outp, " -");
Len Brown103a8fe2010-10-22 23:53:03 -0400453 if (show_core)
Len Brownfc04cc62014-02-06 00:55:19 -0500454 outp += sprintf(outp, " -");
Len Brown103a8fe2010-10-22 23:53:03 -0400455 if (show_cpu)
Len Brownfc04cc62014-02-06 00:55:19 -0500456 outp += sprintf(outp, " -");
Len Brown103a8fe2010-10-22 23:53:03 -0400457 } else {
Len Brownc98d5d92012-06-04 00:56:40 -0400458 if (show_pkg) {
459 if (p)
Len Brownfc04cc62014-02-06 00:55:19 -0500460 outp += sprintf(outp, "%8d", p->package_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400461 else
Len Brownfc04cc62014-02-06 00:55:19 -0500462 outp += sprintf(outp, " -");
Len Brownc98d5d92012-06-04 00:56:40 -0400463 }
Len Brownc98d5d92012-06-04 00:56:40 -0400464 if (show_core) {
465 if (c)
Len Brownfc04cc62014-02-06 00:55:19 -0500466 outp += sprintf(outp, "%8d", c->core_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400467 else
Len Brownfc04cc62014-02-06 00:55:19 -0500468 outp += sprintf(outp, " -");
Len Brownc98d5d92012-06-04 00:56:40 -0400469 }
Len Brown103a8fe2010-10-22 23:53:03 -0400470 if (show_cpu)
Len Brownfc04cc62014-02-06 00:55:19 -0500471 outp += sprintf(outp, "%8d", t->cpu_id);
Len Brown103a8fe2010-10-22 23:53:03 -0400472 }
Len Brownfc04cc62014-02-06 00:55:19 -0500473
Len Brownd7899442015-01-23 00:12:33 -0500474 /* Avg_MHz */
Len Brownfc04cc62014-02-06 00:55:19 -0500475 if (has_aperf)
476 outp += sprintf(outp, "%8.0f",
477 1.0 / units * t->aperf / interval_float);
478
Len Brownd7899442015-01-23 00:12:33 -0500479 /* %Busy */
480 if (has_aperf) {
Len Brown103a8fe2010-10-22 23:53:03 -0400481 if (!skip_c0)
Len Brownfc04cc62014-02-06 00:55:19 -0500482 outp += sprintf(outp, "%8.2f", 100.0 * t->mperf/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400483 else
Len Brownfc04cc62014-02-06 00:55:19 -0500484 outp += sprintf(outp, "********");
Len Brown103a8fe2010-10-22 23:53:03 -0400485 }
486
Len Brownd7899442015-01-23 00:12:33 -0500487 /* Bzy_MHz */
Len Brownfc04cc62014-02-06 00:55:19 -0500488 if (has_aperf)
489 outp += sprintf(outp, "%8.0f",
490 1.0 * t->tsc / units * t->aperf / t->mperf / interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400491
Len Brownd7899442015-01-23 00:12:33 -0500492 /* TSC_MHz */
Len Brownfc04cc62014-02-06 00:55:19 -0500493 outp += sprintf(outp, "%8.0f", 1.0 * t->tsc/units/interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400494
Len Brown8e180f32012-09-22 01:25:08 -0400495 /* delta */
496 if (extra_delta_offset32)
497 outp += sprintf(outp, " %11llu", t->extra_delta32);
498
499 /* DELTA */
500 if (extra_delta_offset64)
501 outp += sprintf(outp, " %11llu", t->extra_delta64);
Len Brown2f32edf2012-09-21 23:45:46 -0400502 /* msr */
503 if (extra_msr_offset32)
Len Brown8e180f32012-09-22 01:25:08 -0400504 outp += sprintf(outp, " 0x%08llx", t->extra_msr32);
Len Brown2f32edf2012-09-21 23:45:46 -0400505
Len Brown130ff302012-09-21 22:56:06 -0400506 /* MSR */
Len Brown2f32edf2012-09-21 23:45:46 -0400507 if (extra_msr_offset64)
508 outp += sprintf(outp, " 0x%016llx", t->extra_msr64);
Len Brown130ff302012-09-21 22:56:06 -0400509
Len Brown1cc21f72015-02-23 00:34:57 -0500510 if (!debug)
511 goto done;
512
513 /* SMI */
514 if (do_smi)
515 outp += sprintf(outp, "%8d", t->smi_count);
516
Len Brown103a8fe2010-10-22 23:53:03 -0400517 if (do_nhm_cstates) {
518 if (!skip_c1)
Len Brownfc04cc62014-02-06 00:55:19 -0500519 outp += sprintf(outp, "%8.2f", 100.0 * t->c1/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400520 else
Len Brownfc04cc62014-02-06 00:55:19 -0500521 outp += sprintf(outp, "********");
Len Brown103a8fe2010-10-22 23:53:03 -0400522 }
Len Brownc98d5d92012-06-04 00:56:40 -0400523
524 /* print per-core data only for 1st thread in core */
525 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
526 goto done;
527
Len Brown144b44b2013-11-09 00:30:16 -0500528 if (do_nhm_cstates && !do_slm_cstates)
Len Brownfc04cc62014-02-06 00:55:19 -0500529 outp += sprintf(outp, "%8.2f", 100.0 * c->c3/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400530 if (do_nhm_cstates)
Len Brownfc04cc62014-02-06 00:55:19 -0500531 outp += sprintf(outp, "%8.2f", 100.0 * c->c6/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400532 if (do_snb_cstates)
Len Brownfc04cc62014-02-06 00:55:19 -0500533 outp += sprintf(outp, "%8.2f", 100.0 * c->c7/t->tsc);
Len Brownc98d5d92012-06-04 00:56:40 -0400534
Len Brown889facb2012-11-08 00:48:57 -0500535 if (do_dts)
Len Brownfc04cc62014-02-06 00:55:19 -0500536 outp += sprintf(outp, "%8d", c->core_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500537
Len Brownc98d5d92012-06-04 00:56:40 -0400538 /* print per-package data only for 1st core in package */
539 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
540 goto done;
541
Len Brown889facb2012-11-08 00:48:57 -0500542 if (do_ptm)
Len Brownfc04cc62014-02-06 00:55:19 -0500543 outp += sprintf(outp, "%8d", p->pkg_temp_c);
Len Brown889facb2012-11-08 00:48:57 -0500544
Len Brownee7e38e2015-02-09 23:39:45 -0500545 if (do_pc2)
Len Brownfc04cc62014-02-06 00:55:19 -0500546 outp += sprintf(outp, "%8.2f", 100.0 * p->pc2/t->tsc);
Len Brownee7e38e2015-02-09 23:39:45 -0500547 if (do_pc3)
Len Brownfc04cc62014-02-06 00:55:19 -0500548 outp += sprintf(outp, "%8.2f", 100.0 * p->pc3/t->tsc);
Len Brownee7e38e2015-02-09 23:39:45 -0500549 if (do_pc6)
Len Brownfc04cc62014-02-06 00:55:19 -0500550 outp += sprintf(outp, "%8.2f", 100.0 * p->pc6/t->tsc);
Len Brownee7e38e2015-02-09 23:39:45 -0500551 if (do_pc7)
Len Brownfc04cc62014-02-06 00:55:19 -0500552 outp += sprintf(outp, "%8.2f", 100.0 * p->pc7/t->tsc);
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800553 if (do_c8_c9_c10) {
Len Brownfc04cc62014-02-06 00:55:19 -0500554 outp += sprintf(outp, "%8.2f", 100.0 * p->pc8/t->tsc);
555 outp += sprintf(outp, "%8.2f", 100.0 * p->pc9/t->tsc);
556 outp += sprintf(outp, "%8.2f", 100.0 * p->pc10/t->tsc);
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800557 }
Len Brown889facb2012-11-08 00:48:57 -0500558
559 /*
560 * If measurement interval exceeds minimum RAPL Joule Counter range,
561 * indicate that results are suspect by printing "**" in fraction place.
562 */
Len Brownfc04cc62014-02-06 00:55:19 -0500563 if (interval_float < rapl_joule_counter_range)
564 fmt8 = "%8.2f";
565 else
566 fmt8 = " %6.0f**";
Len Brown889facb2012-11-08 00:48:57 -0500567
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800568 if (do_rapl && !rapl_joules) {
569 if (do_rapl & RAPL_PKG)
Len Brownfc04cc62014-02-06 00:55:19 -0500570 outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800571 if (do_rapl & RAPL_CORES)
Len Brownfc04cc62014-02-06 00:55:19 -0500572 outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800573 if (do_rapl & RAPL_GFX)
Len Brownfc04cc62014-02-06 00:55:19 -0500574 outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800575 if (do_rapl & RAPL_DRAM)
Len Brownfc04cc62014-02-06 00:55:19 -0500576 outp += sprintf(outp, fmt8, p->energy_dram * rapl_energy_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800577 if (do_rapl & RAPL_PKG_PERF_STATUS)
Len Brownfc04cc62014-02-06 00:55:19 -0500578 outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800579 if (do_rapl & RAPL_DRAM_PERF_STATUS)
Len Brownfc04cc62014-02-06 00:55:19 -0500580 outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
Len Brownd7899442015-01-23 00:12:33 -0500581 } else if (do_rapl && rapl_joules) {
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800582 if (do_rapl & RAPL_PKG)
Len Brownfc04cc62014-02-06 00:55:19 -0500583 outp += sprintf(outp, fmt8,
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800584 p->energy_pkg * rapl_energy_units);
585 if (do_rapl & RAPL_CORES)
Len Brownfc04cc62014-02-06 00:55:19 -0500586 outp += sprintf(outp, fmt8,
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800587 p->energy_cores * rapl_energy_units);
588 if (do_rapl & RAPL_GFX)
Len Brownfc04cc62014-02-06 00:55:19 -0500589 outp += sprintf(outp, fmt8,
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800590 p->energy_gfx * rapl_energy_units);
591 if (do_rapl & RAPL_DRAM)
Len Brownfc04cc62014-02-06 00:55:19 -0500592 outp += sprintf(outp, fmt8,
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800593 p->energy_dram * rapl_energy_units);
594 if (do_rapl & RAPL_PKG_PERF_STATUS)
Len Brownfc04cc62014-02-06 00:55:19 -0500595 outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800596 if (do_rapl & RAPL_DRAM_PERF_STATUS)
Len Brownfc04cc62014-02-06 00:55:19 -0500597 outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
Len Brown889facb2012-11-08 00:48:57 -0500598
Len Brownd7899442015-01-23 00:12:33 -0500599 outp += sprintf(outp, fmt8, interval_float);
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800600 }
Len Brownc98d5d92012-06-04 00:56:40 -0400601done:
Len Brownc98d5d92012-06-04 00:56:40 -0400602 outp += sprintf(outp, "\n");
603
604 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400605}
606
Len Brownc98d5d92012-06-04 00:56:40 -0400607void flush_stdout()
Len Brown103a8fe2010-10-22 23:53:03 -0400608{
Len Brownc98d5d92012-06-04 00:56:40 -0400609 fputs(output_buffer, stdout);
Len Brownddac0d62012-11-30 01:01:40 -0500610 fflush(stdout);
Len Brownc98d5d92012-06-04 00:56:40 -0400611 outp = output_buffer;
612}
613void flush_stderr()
614{
615 fputs(output_buffer, stderr);
616 outp = output_buffer;
617}
618void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
619{
Len Browne23da032012-02-06 18:37:16 -0500620 static int printed;
Len Brown103a8fe2010-10-22 23:53:03 -0400621
Len Browne23da032012-02-06 18:37:16 -0500622 if (!printed || !summary_only)
623 print_header();
Len Brown103a8fe2010-10-22 23:53:03 -0400624
Len Brownc98d5d92012-06-04 00:56:40 -0400625 if (topo.num_cpus > 1)
626 format_counters(&average.threads, &average.cores,
627 &average.packages);
Len Brown103a8fe2010-10-22 23:53:03 -0400628
Len Browne23da032012-02-06 18:37:16 -0500629 printed = 1;
630
631 if (summary_only)
632 return;
633
Len Brownc98d5d92012-06-04 00:56:40 -0400634 for_all_cpus(format_counters, t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400635}
636
Len Brown889facb2012-11-08 00:48:57 -0500637#define DELTA_WRAP32(new, old) \
638 if (new > old) { \
639 old = new - old; \
640 } else { \
641 old = 0x100000000 + new - old; \
642 }
643
Len Brownc98d5d92012-06-04 00:56:40 -0400644void
645delta_package(struct pkg_data *new, struct pkg_data *old)
Len Brown103a8fe2010-10-22 23:53:03 -0400646{
Len Brownc98d5d92012-06-04 00:56:40 -0400647 old->pc2 = new->pc2 - old->pc2;
Len Brownee7e38e2015-02-09 23:39:45 -0500648 if (do_pc3)
649 old->pc3 = new->pc3 - old->pc3;
650 if (do_pc6)
651 old->pc6 = new->pc6 - old->pc6;
652 if (do_pc7)
653 old->pc7 = new->pc7 - old->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800654 old->pc8 = new->pc8 - old->pc8;
655 old->pc9 = new->pc9 - old->pc9;
656 old->pc10 = new->pc10 - old->pc10;
Len Brown889facb2012-11-08 00:48:57 -0500657 old->pkg_temp_c = new->pkg_temp_c;
658
659 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
660 DELTA_WRAP32(new->energy_cores, old->energy_cores);
661 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
662 DELTA_WRAP32(new->energy_dram, old->energy_dram);
663 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
664 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
Len Brownc98d5d92012-06-04 00:56:40 -0400665}
Len Brown103a8fe2010-10-22 23:53:03 -0400666
Len Brownc98d5d92012-06-04 00:56:40 -0400667void
668delta_core(struct core_data *new, struct core_data *old)
669{
670 old->c3 = new->c3 - old->c3;
671 old->c6 = new->c6 - old->c6;
672 old->c7 = new->c7 - old->c7;
Len Brown889facb2012-11-08 00:48:57 -0500673 old->core_temp_c = new->core_temp_c;
Len Brownc98d5d92012-06-04 00:56:40 -0400674}
Len Brown103a8fe2010-10-22 23:53:03 -0400675
Len Brownc3ae3312012-06-13 21:31:46 -0400676/*
677 * old = new - old
678 */
Len Brownc98d5d92012-06-04 00:56:40 -0400679void
680delta_thread(struct thread_data *new, struct thread_data *old,
681 struct core_data *core_delta)
682{
683 old->tsc = new->tsc - old->tsc;
Len Brown103a8fe2010-10-22 23:53:03 -0400684
Len Brownc98d5d92012-06-04 00:56:40 -0400685 /* check for TSC < 1 Mcycles over interval */
Josh Triplettb2c95d92013-08-20 17:20:18 -0700686 if (old->tsc < (1000 * 1000))
687 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
688 "You can disable all c-states by booting with \"idle=poll\"\n"
689 "or just the deep ones with \"processor.max_cstate=1\"");
Len Brown103a8fe2010-10-22 23:53:03 -0400690
Len Brownc98d5d92012-06-04 00:56:40 -0400691 old->c1 = new->c1 - old->c1;
Len Brown103a8fe2010-10-22 23:53:03 -0400692
Len Browna7296172015-01-23 01:33:58 -0500693 if (has_aperf) {
694 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
695 old->aperf = new->aperf - old->aperf;
696 old->mperf = new->mperf - old->mperf;
697 } else {
Len Brown103a8fe2010-10-22 23:53:03 -0400698
Len Browna7296172015-01-23 01:33:58 -0500699 if (!aperf_mperf_unstable) {
700 fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname);
701 fprintf(stderr, "* Frequency results do not cover entire interval *\n");
702 fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n");
Len Brownc98d5d92012-06-04 00:56:40 -0400703
Len Browna7296172015-01-23 01:33:58 -0500704 aperf_mperf_unstable = 1;
705 }
706 /*
707 * mperf delta is likely a huge "positive" number
708 * can not use it for calculating c0 time
709 */
710 skip_c0 = 1;
711 skip_c1 = 1;
Len Brownc98d5d92012-06-04 00:56:40 -0400712 }
Len Brownc98d5d92012-06-04 00:56:40 -0400713 }
Len Brown103a8fe2010-10-22 23:53:03 -0400714
Len Brown103a8fe2010-10-22 23:53:03 -0400715
Len Brown144b44b2013-11-09 00:30:16 -0500716 if (use_c1_residency_msr) {
717 /*
718 * Some models have a dedicated C1 residency MSR,
719 * which should be more accurate than the derivation below.
720 */
721 } else {
722 /*
723 * As counter collection is not atomic,
724 * it is possible for mperf's non-halted cycles + idle states
725 * to exceed TSC's all cycles: show c1 = 0% in that case.
726 */
727 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
728 old->c1 = 0;
729 else {
730 /* normal case, derive c1 */
731 old->c1 = old->tsc - old->mperf - core_delta->c3
Len Brownc98d5d92012-06-04 00:56:40 -0400732 - core_delta->c6 - core_delta->c7;
Len Brown144b44b2013-11-09 00:30:16 -0500733 }
Len Brownc98d5d92012-06-04 00:56:40 -0400734 }
Len Brownc3ae3312012-06-13 21:31:46 -0400735
Len Brownc98d5d92012-06-04 00:56:40 -0400736 if (old->mperf == 0) {
Len Brownd8af6f52015-02-10 01:56:38 -0500737 if (debug > 1) fprintf(stderr, "cpu%d MPERF 0!\n", old->cpu_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400738 old->mperf = 1; /* divide by 0 protection */
739 }
740
Len Brown8e180f32012-09-22 01:25:08 -0400741 old->extra_delta32 = new->extra_delta32 - old->extra_delta32;
742 old->extra_delta32 &= 0xFFFFFFFF;
743
744 old->extra_delta64 = new->extra_delta64 - old->extra_delta64;
745
Len Brownc98d5d92012-06-04 00:56:40 -0400746 /*
Len Brown8e180f32012-09-22 01:25:08 -0400747 * Extra MSR is just a snapshot, simply copy latest w/o subtracting
Len Brownc98d5d92012-06-04 00:56:40 -0400748 */
Len Brown2f32edf2012-09-21 23:45:46 -0400749 old->extra_msr32 = new->extra_msr32;
750 old->extra_msr64 = new->extra_msr64;
Len Brown1ed51012013-02-10 17:19:24 -0500751
752 if (do_smi)
753 old->smi_count = new->smi_count - old->smi_count;
Len Brownc98d5d92012-06-04 00:56:40 -0400754}
755
756int delta_cpu(struct thread_data *t, struct core_data *c,
757 struct pkg_data *p, struct thread_data *t2,
758 struct core_data *c2, struct pkg_data *p2)
759{
760 /* calculate core delta only for 1st thread in core */
761 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
762 delta_core(c, c2);
763
764 /* always calculate thread delta */
765 delta_thread(t, t2, c2); /* c2 is core delta */
766
767 /* calculate package delta only for 1st core in package */
768 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
769 delta_package(p, p2);
770
771 return 0;
772}
773
774void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
775{
776 t->tsc = 0;
777 t->aperf = 0;
778 t->mperf = 0;
779 t->c1 = 0;
780
Len Brown1ed51012013-02-10 17:19:24 -0500781 t->smi_count = 0;
Len Brown8e180f32012-09-22 01:25:08 -0400782 t->extra_delta32 = 0;
783 t->extra_delta64 = 0;
784
Len Brownc98d5d92012-06-04 00:56:40 -0400785 /* tells format_counters to dump all fields from this set */
786 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
787
788 c->c3 = 0;
789 c->c6 = 0;
790 c->c7 = 0;
Len Brown889facb2012-11-08 00:48:57 -0500791 c->core_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -0400792
793 p->pc2 = 0;
Len Brownee7e38e2015-02-09 23:39:45 -0500794 if (do_pc3)
795 p->pc3 = 0;
796 if (do_pc6)
797 p->pc6 = 0;
798 if (do_pc7)
799 p->pc7 = 0;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800800 p->pc8 = 0;
801 p->pc9 = 0;
802 p->pc10 = 0;
Len Brown889facb2012-11-08 00:48:57 -0500803
804 p->energy_pkg = 0;
805 p->energy_dram = 0;
806 p->energy_cores = 0;
807 p->energy_gfx = 0;
808 p->rapl_pkg_perf_status = 0;
809 p->rapl_dram_perf_status = 0;
810 p->pkg_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -0400811}
812int sum_counters(struct thread_data *t, struct core_data *c,
813 struct pkg_data *p)
814{
815 average.threads.tsc += t->tsc;
816 average.threads.aperf += t->aperf;
817 average.threads.mperf += t->mperf;
818 average.threads.c1 += t->c1;
819
Len Brown8e180f32012-09-22 01:25:08 -0400820 average.threads.extra_delta32 += t->extra_delta32;
821 average.threads.extra_delta64 += t->extra_delta64;
822
Len Brownc98d5d92012-06-04 00:56:40 -0400823 /* sum per-core values only for 1st thread in core */
824 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
825 return 0;
826
827 average.cores.c3 += c->c3;
828 average.cores.c6 += c->c6;
829 average.cores.c7 += c->c7;
830
Len Brown889facb2012-11-08 00:48:57 -0500831 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
832
Len Brownc98d5d92012-06-04 00:56:40 -0400833 /* sum per-pkg values only for 1st core in pkg */
834 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
835 return 0;
836
837 average.packages.pc2 += p->pc2;
Len Brownee7e38e2015-02-09 23:39:45 -0500838 if (do_pc3)
839 average.packages.pc3 += p->pc3;
840 if (do_pc6)
841 average.packages.pc6 += p->pc6;
842 if (do_pc7)
843 average.packages.pc7 += p->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800844 average.packages.pc8 += p->pc8;
845 average.packages.pc9 += p->pc9;
846 average.packages.pc10 += p->pc10;
Len Brownc98d5d92012-06-04 00:56:40 -0400847
Len Brown889facb2012-11-08 00:48:57 -0500848 average.packages.energy_pkg += p->energy_pkg;
849 average.packages.energy_dram += p->energy_dram;
850 average.packages.energy_cores += p->energy_cores;
851 average.packages.energy_gfx += p->energy_gfx;
852
853 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
854
855 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
856 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
Len Brownc98d5d92012-06-04 00:56:40 -0400857 return 0;
858}
859/*
860 * sum the counters for all cpus in the system
861 * compute the weighted average
862 */
863void compute_average(struct thread_data *t, struct core_data *c,
864 struct pkg_data *p)
865{
866 clear_counters(&average.threads, &average.cores, &average.packages);
867
868 for_all_cpus(sum_counters, t, c, p);
869
870 average.threads.tsc /= topo.num_cpus;
871 average.threads.aperf /= topo.num_cpus;
872 average.threads.mperf /= topo.num_cpus;
873 average.threads.c1 /= topo.num_cpus;
874
Len Brown8e180f32012-09-22 01:25:08 -0400875 average.threads.extra_delta32 /= topo.num_cpus;
876 average.threads.extra_delta32 &= 0xFFFFFFFF;
877
878 average.threads.extra_delta64 /= topo.num_cpus;
879
Len Brownc98d5d92012-06-04 00:56:40 -0400880 average.cores.c3 /= topo.num_cores;
881 average.cores.c6 /= topo.num_cores;
882 average.cores.c7 /= topo.num_cores;
883
884 average.packages.pc2 /= topo.num_packages;
Len Brownee7e38e2015-02-09 23:39:45 -0500885 if (do_pc3)
886 average.packages.pc3 /= topo.num_packages;
887 if (do_pc6)
888 average.packages.pc6 /= topo.num_packages;
889 if (do_pc7)
890 average.packages.pc7 /= topo.num_packages;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800891
892 average.packages.pc8 /= topo.num_packages;
893 average.packages.pc9 /= topo.num_packages;
894 average.packages.pc10 /= topo.num_packages;
Len Brownc98d5d92012-06-04 00:56:40 -0400895}
896
897static unsigned long long rdtsc(void)
898{
899 unsigned int low, high;
900
901 asm volatile("rdtsc" : "=a" (low), "=d" (high));
902
903 return low | ((unsigned long long)high) << 32;
904}
905
906
907/*
908 * get_counters(...)
909 * migrate to cpu
910 * acquire and record local counters for that cpu
911 */
912int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
913{
914 int cpu = t->cpu_id;
Len Brown889facb2012-11-08 00:48:57 -0500915 unsigned long long msr;
Len Brownc98d5d92012-06-04 00:56:40 -0400916
Len Browne52966c2012-11-08 22:38:05 -0500917 if (cpu_migrate(cpu)) {
918 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
Len Brownc98d5d92012-06-04 00:56:40 -0400919 return -1;
Len Browne52966c2012-11-08 22:38:05 -0500920 }
Len Brownc98d5d92012-06-04 00:56:40 -0400921
922 t->tsc = rdtsc(); /* we are running on local CPU of interest */
923
924 if (has_aperf) {
Len Brown9c63a652012-10-31 01:29:52 -0400925 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
Len Brownc98d5d92012-06-04 00:56:40 -0400926 return -3;
Len Brown9c63a652012-10-31 01:29:52 -0400927 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
Len Brownc98d5d92012-06-04 00:56:40 -0400928 return -4;
929 }
930
Len Brown1ed51012013-02-10 17:19:24 -0500931 if (do_smi) {
932 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
933 return -5;
934 t->smi_count = msr & 0xFFFFFFFF;
935 }
Len Brown8e180f32012-09-22 01:25:08 -0400936 if (extra_delta_offset32) {
Len Brown889facb2012-11-08 00:48:57 -0500937 if (get_msr(cpu, extra_delta_offset32, &msr))
Len Brown2f32edf2012-09-21 23:45:46 -0400938 return -5;
Len Brown889facb2012-11-08 00:48:57 -0500939 t->extra_delta32 = msr & 0xFFFFFFFF;
Len Brown8e180f32012-09-22 01:25:08 -0400940 }
941
942 if (extra_delta_offset64)
943 if (get_msr(cpu, extra_delta_offset64, &t->extra_delta64))
944 return -5;
945
946 if (extra_msr_offset32) {
Len Brown889facb2012-11-08 00:48:57 -0500947 if (get_msr(cpu, extra_msr_offset32, &msr))
Len Brown8e180f32012-09-22 01:25:08 -0400948 return -5;
Len Brown889facb2012-11-08 00:48:57 -0500949 t->extra_msr32 = msr & 0xFFFFFFFF;
Len Brown8e180f32012-09-22 01:25:08 -0400950 }
Len Brown2f32edf2012-09-21 23:45:46 -0400951
952 if (extra_msr_offset64)
953 if (get_msr(cpu, extra_msr_offset64, &t->extra_msr64))
Len Brownc98d5d92012-06-04 00:56:40 -0400954 return -5;
955
Len Brown144b44b2013-11-09 00:30:16 -0500956 if (use_c1_residency_msr) {
957 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
958 return -6;
959 }
960
Len Brownc98d5d92012-06-04 00:56:40 -0400961 /* collect core counters only for 1st thread in core */
962 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
963 return 0;
964
Len Brown144b44b2013-11-09 00:30:16 -0500965 if (do_nhm_cstates && !do_slm_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -0400966 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
967 return -6;
Len Brown144b44b2013-11-09 00:30:16 -0500968 }
969
970 if (do_nhm_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -0400971 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
972 return -7;
973 }
974
975 if (do_snb_cstates)
976 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
977 return -8;
978
Len Brown889facb2012-11-08 00:48:57 -0500979 if (do_dts) {
980 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
981 return -9;
982 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
983 }
984
985
Len Brownc98d5d92012-06-04 00:56:40 -0400986 /* collect package counters only for 1st core in package */
987 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
988 return 0;
989
Len Brownee7e38e2015-02-09 23:39:45 -0500990 if (do_pc3)
Len Brownc98d5d92012-06-04 00:56:40 -0400991 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
992 return -9;
Len Brownee7e38e2015-02-09 23:39:45 -0500993 if (do_pc6)
Len Brownc98d5d92012-06-04 00:56:40 -0400994 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
995 return -10;
Len Brownee7e38e2015-02-09 23:39:45 -0500996 if (do_pc2)
Len Brownc98d5d92012-06-04 00:56:40 -0400997 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
998 return -11;
Len Brownee7e38e2015-02-09 23:39:45 -0500999 if (do_pc7)
Len Brownc98d5d92012-06-04 00:56:40 -04001000 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1001 return -12;
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001002 if (do_c8_c9_c10) {
1003 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1004 return -13;
1005 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1006 return -13;
1007 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1008 return -13;
1009 }
Len Brown889facb2012-11-08 00:48:57 -05001010 if (do_rapl & RAPL_PKG) {
1011 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1012 return -13;
1013 p->energy_pkg = msr & 0xFFFFFFFF;
1014 }
1015 if (do_rapl & RAPL_CORES) {
1016 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1017 return -14;
1018 p->energy_cores = msr & 0xFFFFFFFF;
1019 }
1020 if (do_rapl & RAPL_DRAM) {
1021 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1022 return -15;
1023 p->energy_dram = msr & 0xFFFFFFFF;
1024 }
1025 if (do_rapl & RAPL_GFX) {
1026 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1027 return -16;
1028 p->energy_gfx = msr & 0xFFFFFFFF;
1029 }
1030 if (do_rapl & RAPL_PKG_PERF_STATUS) {
1031 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1032 return -16;
1033 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1034 }
1035 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1036 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1037 return -16;
1038 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1039 }
1040 if (do_ptm) {
1041 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1042 return -17;
1043 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1044 }
Len Brown103a8fe2010-10-22 23:53:03 -04001045 return 0;
1046}
1047
Len Brownee7e38e2015-02-09 23:39:45 -05001048/*
1049 * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1050 * If you change the values, note they are used both in comparisons
1051 * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1052 */
1053
1054#define PCLUKN 0 /* Unknown */
1055#define PCLRSV 1 /* Reserved */
1056#define PCL__0 2 /* PC0 */
1057#define PCL__1 3 /* PC1 */
1058#define PCL__2 4 /* PC2 */
1059#define PCL__3 5 /* PC3 */
1060#define PCL__4 6 /* PC4 */
1061#define PCL__6 7 /* PC6 */
1062#define PCL_6N 8 /* PC6 No Retention */
1063#define PCL_6R 9 /* PC6 Retention */
1064#define PCL__7 10 /* PC7 */
1065#define PCL_7S 11 /* PC7 Shrink */
1066#define PCLUNL 12 /* Unlimited */
1067
1068int pkg_cstate_limit = PCLUKN;
1069char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
1070 "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "unlimited"};
1071
1072int nhm_pkg_cstate_limits[8] = {PCL__0, PCL__1, PCL__3, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLUNL};
1073int snb_pkg_cstate_limits[8] = {PCL__0, PCL__2, PCL_6N, PCL_6R, PCL__7, PCL_7S, PCLRSV, PCLUNL};
1074int hsw_pkg_cstate_limits[8] = {PCL__0, PCL__2, PCL__3, PCL__6, PCL__7, PCL_7S, PCLRSV, PCLUNL};
1075int slv_pkg_cstate_limits[8] = {PCL__0, PCL__1, PCLRSV, PCLRSV, PCL__4, PCLRSV, PCL__6, PCL__7};
1076int amt_pkg_cstate_limits[8] = {PCL__0, PCL__1, PCL__2, PCLRSV, PCLRSV, PCLRSV, PCL__6, PCL__7};
1077int phi_pkg_cstate_limits[8] = {PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLUNL};
1078
Len Brownfcd17212015-03-23 20:29:09 -04001079static void
1080dump_nhm_platform_info(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001081{
1082 unsigned long long msr;
1083 unsigned int ratio;
1084
Len Brown9c63a652012-10-31 01:29:52 -04001085 get_msr(0, MSR_NHM_PLATFORM_INFO, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001086
Len Brown67920412013-01-31 15:22:15 -05001087 fprintf(stderr, "cpu0: MSR_NHM_PLATFORM_INFO: 0x%08llx\n", msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001088
Len Brown103a8fe2010-10-22 23:53:03 -04001089 ratio = (msr >> 40) & 0xFF;
Len Brown8f61f352015-03-23 18:36:57 -04001090 fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001091 ratio, bclk, ratio * bclk);
1092
1093 ratio = (msr >> 8) & 0xFF;
Len Brown8f61f352015-03-23 18:36:57 -04001094 fprintf(stderr, "%d * %.0f = %.0f MHz base frequency\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001095 ratio, bclk, ratio * bclk);
1096
Len Brown67920412013-01-31 15:22:15 -05001097 get_msr(0, MSR_IA32_POWER_CTL, &msr);
Len Brown144b44b2013-11-09 00:30:16 -05001098 fprintf(stderr, "cpu0: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
Len Brown67920412013-01-31 15:22:15 -05001099 msr, msr & 0x2 ? "EN" : "DIS");
1100
Len Brownfcd17212015-03-23 20:29:09 -04001101 return;
1102}
1103
1104static void
1105dump_hsw_turbo_ratio_limits(void)
1106{
1107 unsigned long long msr;
1108 unsigned int ratio;
1109
1110 get_msr(0, MSR_TURBO_RATIO_LIMIT2, &msr);
1111
1112 fprintf(stderr, "cpu0: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", msr);
1113
1114 ratio = (msr >> 8) & 0xFF;
1115 if (ratio)
1116 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 18 active cores\n",
1117 ratio, bclk, ratio * bclk);
1118
1119 ratio = (msr >> 0) & 0xFF;
1120 if (ratio)
1121 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 17 active cores\n",
1122 ratio, bclk, ratio * bclk);
1123 return;
1124}
1125
1126static void
1127dump_ivt_turbo_ratio_limits(void)
1128{
1129 unsigned long long msr;
1130 unsigned int ratio;
Len Brown6574a5d2012-09-21 00:01:31 -04001131
Len Brown12bb43c2015-04-13 16:08:18 -04001132 get_msr(0, MSR_TURBO_RATIO_LIMIT1, &msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001133
Len Brown12bb43c2015-04-13 16:08:18 -04001134 fprintf(stderr, "cpu0: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001135
1136 ratio = (msr >> 56) & 0xFF;
1137 if (ratio)
1138 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 16 active cores\n",
1139 ratio, bclk, ratio * bclk);
1140
1141 ratio = (msr >> 48) & 0xFF;
1142 if (ratio)
1143 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 15 active cores\n",
1144 ratio, bclk, ratio * bclk);
1145
1146 ratio = (msr >> 40) & 0xFF;
1147 if (ratio)
1148 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 14 active cores\n",
1149 ratio, bclk, ratio * bclk);
1150
1151 ratio = (msr >> 32) & 0xFF;
1152 if (ratio)
1153 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 13 active cores\n",
1154 ratio, bclk, ratio * bclk);
1155
1156 ratio = (msr >> 24) & 0xFF;
1157 if (ratio)
1158 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 12 active cores\n",
1159 ratio, bclk, ratio * bclk);
1160
1161 ratio = (msr >> 16) & 0xFF;
1162 if (ratio)
1163 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 11 active cores\n",
1164 ratio, bclk, ratio * bclk);
1165
1166 ratio = (msr >> 8) & 0xFF;
1167 if (ratio)
1168 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 10 active cores\n",
1169 ratio, bclk, ratio * bclk);
1170
1171 ratio = (msr >> 0) & 0xFF;
1172 if (ratio)
1173 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 9 active cores\n",
1174 ratio, bclk, ratio * bclk);
Len Brownfcd17212015-03-23 20:29:09 -04001175 return;
1176}
Len Brown6574a5d2012-09-21 00:01:31 -04001177
Len Brownfcd17212015-03-23 20:29:09 -04001178static void
1179dump_nhm_turbo_ratio_limits(void)
1180{
1181 unsigned long long msr;
1182 unsigned int ratio;
Len Brown103a8fe2010-10-22 23:53:03 -04001183
Len Brown12bb43c2015-04-13 16:08:18 -04001184 get_msr(0, MSR_TURBO_RATIO_LIMIT, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001185
Len Brown12bb43c2015-04-13 16:08:18 -04001186 fprintf(stderr, "cpu0: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001187
1188 ratio = (msr >> 56) & 0xFF;
1189 if (ratio)
1190 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 8 active cores\n",
1191 ratio, bclk, ratio * bclk);
1192
1193 ratio = (msr >> 48) & 0xFF;
1194 if (ratio)
1195 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 7 active cores\n",
1196 ratio, bclk, ratio * bclk);
1197
1198 ratio = (msr >> 40) & 0xFF;
1199 if (ratio)
1200 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 6 active cores\n",
1201 ratio, bclk, ratio * bclk);
1202
1203 ratio = (msr >> 32) & 0xFF;
1204 if (ratio)
1205 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 5 active cores\n",
1206 ratio, bclk, ratio * bclk);
1207
Len Brown103a8fe2010-10-22 23:53:03 -04001208 ratio = (msr >> 24) & 0xFF;
1209 if (ratio)
1210 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
1211 ratio, bclk, ratio * bclk);
1212
1213 ratio = (msr >> 16) & 0xFF;
1214 if (ratio)
1215 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
1216 ratio, bclk, ratio * bclk);
1217
1218 ratio = (msr >> 8) & 0xFF;
1219 if (ratio)
1220 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
1221 ratio, bclk, ratio * bclk);
1222
1223 ratio = (msr >> 0) & 0xFF;
1224 if (ratio)
1225 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
1226 ratio, bclk, ratio * bclk);
Len Brownfcd17212015-03-23 20:29:09 -04001227 return;
1228}
Len Brown3a9a9412014-08-15 02:39:52 -04001229
Len Brownfcd17212015-03-23 20:29:09 -04001230static void
1231dump_nhm_cst_cfg(void)
1232{
1233 unsigned long long msr;
1234
1235 get_msr(0, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
1236
1237#define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
1238#define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
1239
1240 fprintf(stderr, "cpu0: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", msr);
1241
1242 fprintf(stderr, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: %s)\n",
1243 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
1244 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
1245 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
1246 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
1247 (msr & (1 << 15)) ? "" : "UN",
1248 (unsigned int)msr & 7,
1249 pkg_cstate_limit_strings[pkg_cstate_limit]);
1250 return;
Len Brown103a8fe2010-10-22 23:53:03 -04001251}
1252
Len Brownc98d5d92012-06-04 00:56:40 -04001253void free_all_buffers(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001254{
Len Brownc98d5d92012-06-04 00:56:40 -04001255 CPU_FREE(cpu_present_set);
1256 cpu_present_set = NULL;
1257 cpu_present_set = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001258
Len Brownc98d5d92012-06-04 00:56:40 -04001259 CPU_FREE(cpu_affinity_set);
1260 cpu_affinity_set = NULL;
1261 cpu_affinity_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001262
Len Brownc98d5d92012-06-04 00:56:40 -04001263 free(thread_even);
1264 free(core_even);
1265 free(package_even);
1266
1267 thread_even = NULL;
1268 core_even = NULL;
1269 package_even = NULL;
1270
1271 free(thread_odd);
1272 free(core_odd);
1273 free(package_odd);
1274
1275 thread_odd = NULL;
1276 core_odd = NULL;
1277 package_odd = NULL;
1278
1279 free(output_buffer);
1280 output_buffer = NULL;
1281 outp = NULL;
Len Brown103a8fe2010-10-22 23:53:03 -04001282}
1283
Len Brownc98d5d92012-06-04 00:56:40 -04001284/*
Josh Triplett57a42a32013-08-20 17:20:17 -07001285 * Open a file, and exit on failure
1286 */
1287FILE *fopen_or_die(const char *path, const char *mode)
1288{
1289 FILE *filep = fopen(path, "r");
Josh Triplettb2c95d92013-08-20 17:20:18 -07001290 if (!filep)
1291 err(1, "%s: open failed", path);
Josh Triplett57a42a32013-08-20 17:20:17 -07001292 return filep;
1293}
1294
1295/*
Josh Triplett95aebc42013-08-20 17:20:16 -07001296 * Parse a file containing a single int.
1297 */
1298int parse_int_file(const char *fmt, ...)
1299{
1300 va_list args;
1301 char path[PATH_MAX];
1302 FILE *filep;
1303 int value;
1304
1305 va_start(args, fmt);
1306 vsnprintf(path, sizeof(path), fmt, args);
1307 va_end(args);
Josh Triplett57a42a32013-08-20 17:20:17 -07001308 filep = fopen_or_die(path, "r");
Josh Triplettb2c95d92013-08-20 17:20:18 -07001309 if (fscanf(filep, "%d", &value) != 1)
1310 err(1, "%s: failed to parse number from file", path);
Josh Triplett95aebc42013-08-20 17:20:16 -07001311 fclose(filep);
1312 return value;
1313}
1314
1315/*
Len Brownc98d5d92012-06-04 00:56:40 -04001316 * cpu_is_first_sibling_in_core(cpu)
1317 * return 1 if given CPU is 1st HT sibling in the core
1318 */
1319int cpu_is_first_sibling_in_core(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001320{
Josh Triplett95aebc42013-08-20 17:20:16 -07001321 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001322}
1323
Len Brownc98d5d92012-06-04 00:56:40 -04001324/*
1325 * cpu_is_first_core_in_package(cpu)
1326 * return 1 if given CPU is 1st core in package
1327 */
1328int cpu_is_first_core_in_package(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001329{
Josh Triplett95aebc42013-08-20 17:20:16 -07001330 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001331}
1332
1333int get_physical_package_id(int cpu)
1334{
Josh Triplett95aebc42013-08-20 17:20:16 -07001335 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001336}
1337
1338int get_core_id(int cpu)
1339{
Josh Triplett95aebc42013-08-20 17:20:16 -07001340 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001341}
1342
Len Brownc98d5d92012-06-04 00:56:40 -04001343int get_num_ht_siblings(int cpu)
1344{
1345 char path[80];
1346 FILE *filep;
1347 int sib1, sib2;
1348 int matches;
1349 char character;
1350
1351 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
Josh Triplett57a42a32013-08-20 17:20:17 -07001352 filep = fopen_or_die(path, "r");
Len Brownc98d5d92012-06-04 00:56:40 -04001353 /*
1354 * file format:
1355 * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4)
1356 * otherwinse 1 sibling (self).
1357 */
1358 matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2);
1359
1360 fclose(filep);
1361
1362 if (matches == 3)
1363 return 2;
1364 else
1365 return 1;
1366}
1367
Len Brown103a8fe2010-10-22 23:53:03 -04001368/*
Len Brownc98d5d92012-06-04 00:56:40 -04001369 * run func(thread, core, package) in topology order
1370 * skip non-present cpus
Len Brown103a8fe2010-10-22 23:53:03 -04001371 */
1372
Len Brownc98d5d92012-06-04 00:56:40 -04001373int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
1374 struct pkg_data *, struct thread_data *, struct core_data *,
1375 struct pkg_data *), struct thread_data *thread_base,
1376 struct core_data *core_base, struct pkg_data *pkg_base,
1377 struct thread_data *thread_base2, struct core_data *core_base2,
1378 struct pkg_data *pkg_base2)
1379{
1380 int retval, pkg_no, core_no, thread_no;
1381
1382 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
1383 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
1384 for (thread_no = 0; thread_no <
1385 topo.num_threads_per_core; ++thread_no) {
1386 struct thread_data *t, *t2;
1387 struct core_data *c, *c2;
1388 struct pkg_data *p, *p2;
1389
1390 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
1391
1392 if (cpu_is_not_present(t->cpu_id))
1393 continue;
1394
1395 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
1396
1397 c = GET_CORE(core_base, core_no, pkg_no);
1398 c2 = GET_CORE(core_base2, core_no, pkg_no);
1399
1400 p = GET_PKG(pkg_base, pkg_no);
1401 p2 = GET_PKG(pkg_base2, pkg_no);
1402
1403 retval = func(t, c, p, t2, c2, p2);
1404 if (retval)
1405 return retval;
1406 }
1407 }
1408 }
1409 return 0;
1410}
1411
1412/*
1413 * run func(cpu) on every cpu in /proc/stat
1414 * return max_cpu number
1415 */
1416int for_all_proc_cpus(int (func)(int))
Len Brown103a8fe2010-10-22 23:53:03 -04001417{
1418 FILE *fp;
Len Brownc98d5d92012-06-04 00:56:40 -04001419 int cpu_num;
Len Brown103a8fe2010-10-22 23:53:03 -04001420 int retval;
1421
Josh Triplett57a42a32013-08-20 17:20:17 -07001422 fp = fopen_or_die(proc_stat, "r");
Len Brown103a8fe2010-10-22 23:53:03 -04001423
1424 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
Josh Triplettb2c95d92013-08-20 17:20:18 -07001425 if (retval != 0)
1426 err(1, "%s: failed to parse format", proc_stat);
Len Brown103a8fe2010-10-22 23:53:03 -04001427
Len Brownc98d5d92012-06-04 00:56:40 -04001428 while (1) {
1429 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 -04001430 if (retval != 1)
1431 break;
1432
Len Brownc98d5d92012-06-04 00:56:40 -04001433 retval = func(cpu_num);
1434 if (retval) {
1435 fclose(fp);
1436 return(retval);
1437 }
Len Brown103a8fe2010-10-22 23:53:03 -04001438 }
1439 fclose(fp);
Len Brownc98d5d92012-06-04 00:56:40 -04001440 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001441}
1442
1443void re_initialize(void)
1444{
Len Brownc98d5d92012-06-04 00:56:40 -04001445 free_all_buffers();
1446 setup_all_buffers();
1447 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04001448}
1449
Len Brownc98d5d92012-06-04 00:56:40 -04001450
Len Brown103a8fe2010-10-22 23:53:03 -04001451/*
Len Brownc98d5d92012-06-04 00:56:40 -04001452 * count_cpus()
1453 * remember the last one seen, it will be the max
Len Brown103a8fe2010-10-22 23:53:03 -04001454 */
Len Brownc98d5d92012-06-04 00:56:40 -04001455int count_cpus(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001456{
Len Brownc98d5d92012-06-04 00:56:40 -04001457 if (topo.max_cpu_num < cpu)
1458 topo.max_cpu_num = cpu;
Len Brown103a8fe2010-10-22 23:53:03 -04001459
Len Brownc98d5d92012-06-04 00:56:40 -04001460 topo.num_cpus += 1;
1461 return 0;
1462}
1463int mark_cpu_present(int cpu)
1464{
1465 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brown15aaa342012-03-29 22:19:58 -04001466 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001467}
1468
1469void turbostat_loop()
1470{
Len Brownc98d5d92012-06-04 00:56:40 -04001471 int retval;
Len Browne52966c2012-11-08 22:38:05 -05001472 int restarted = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001473
Len Brown103a8fe2010-10-22 23:53:03 -04001474restart:
Len Browne52966c2012-11-08 22:38:05 -05001475 restarted++;
1476
Len Brownc98d5d92012-06-04 00:56:40 -04001477 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001478 if (retval < -1) {
1479 exit(retval);
1480 } else if (retval == -1) {
Len Browne52966c2012-11-08 22:38:05 -05001481 if (restarted > 1) {
1482 exit(retval);
1483 }
Len Brownc98d5d92012-06-04 00:56:40 -04001484 re_initialize();
1485 goto restart;
1486 }
Len Browne52966c2012-11-08 22:38:05 -05001487 restarted = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001488 gettimeofday(&tv_even, (struct timezone *)NULL);
1489
1490 while (1) {
Len Brownc98d5d92012-06-04 00:56:40 -04001491 if (for_all_proc_cpus(cpu_is_not_present)) {
Len Brown103a8fe2010-10-22 23:53:03 -04001492 re_initialize();
1493 goto restart;
1494 }
1495 sleep(interval_sec);
Len Brownc98d5d92012-06-04 00:56:40 -04001496 retval = for_all_cpus(get_counters, ODD_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001497 if (retval < -1) {
1498 exit(retval);
1499 } else if (retval == -1) {
Len Brown15aaa342012-03-29 22:19:58 -04001500 re_initialize();
1501 goto restart;
1502 }
Len Brown103a8fe2010-10-22 23:53:03 -04001503 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04001504 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04001505 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
1506 compute_average(EVEN_COUNTERS);
1507 format_all_counters(EVEN_COUNTERS);
1508 flush_stdout();
Len Brown15aaa342012-03-29 22:19:58 -04001509 sleep(interval_sec);
Len Brownc98d5d92012-06-04 00:56:40 -04001510 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001511 if (retval < -1) {
1512 exit(retval);
1513 } else if (retval == -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04001514 re_initialize();
1515 goto restart;
1516 }
Len Brown103a8fe2010-10-22 23:53:03 -04001517 gettimeofday(&tv_even, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04001518 timersub(&tv_even, &tv_odd, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04001519 for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS);
1520 compute_average(ODD_COUNTERS);
1521 format_all_counters(ODD_COUNTERS);
1522 flush_stdout();
Len Brown103a8fe2010-10-22 23:53:03 -04001523 }
1524}
1525
1526void check_dev_msr()
1527{
1528 struct stat sb;
1529
Josh Triplettb2c95d92013-08-20 17:20:18 -07001530 if (stat("/dev/cpu/0/msr", &sb))
Len Brownd7899442015-01-23 00:12:33 -05001531 err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
Len Brown103a8fe2010-10-22 23:53:03 -04001532}
1533
Len Brown98481e72014-08-15 00:36:50 -04001534void check_permissions()
Len Brown103a8fe2010-10-22 23:53:03 -04001535{
Len Brown98481e72014-08-15 00:36:50 -04001536 struct __user_cap_header_struct cap_header_data;
1537 cap_user_header_t cap_header = &cap_header_data;
1538 struct __user_cap_data_struct cap_data_data;
1539 cap_user_data_t cap_data = &cap_data_data;
1540 extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
1541 int do_exit = 0;
1542
1543 /* check for CAP_SYS_RAWIO */
1544 cap_header->pid = getpid();
1545 cap_header->version = _LINUX_CAPABILITY_VERSION;
1546 if (capget(cap_header, cap_data) < 0)
1547 err(-6, "capget(2) failed");
1548
1549 if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
1550 do_exit++;
1551 warnx("capget(CAP_SYS_RAWIO) failed,"
1552 " try \"# setcap cap_sys_rawio=ep %s\"", progname);
1553 }
1554
1555 /* test file permissions */
1556 if (euidaccess("/dev/cpu/0/msr", R_OK)) {
1557 do_exit++;
1558 warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
1559 }
1560
1561 /* if all else fails, thell them to be root */
1562 if (do_exit)
1563 if (getuid() != 0)
Len Brownd7899442015-01-23 00:12:33 -05001564 warnx("... or simply run as root");
Len Brown98481e72014-08-15 00:36:50 -04001565
1566 if (do_exit)
1567 exit(-6);
Len Brown103a8fe2010-10-22 23:53:03 -04001568}
1569
Len Brownd7899442015-01-23 00:12:33 -05001570/*
1571 * NHM adds support for additional MSRs:
1572 *
1573 * MSR_SMI_COUNT 0x00000034
1574 *
1575 * MSR_NHM_PLATFORM_INFO 0x000000ce
1576 * MSR_NHM_SNB_PKG_CST_CFG_CTL 0x000000e2
1577 *
1578 * MSR_PKG_C3_RESIDENCY 0x000003f8
1579 * MSR_PKG_C6_RESIDENCY 0x000003f9
1580 * MSR_CORE_C3_RESIDENCY 0x000003fc
1581 * MSR_CORE_C6_RESIDENCY 0x000003fd
1582 *
Len Brownee7e38e2015-02-09 23:39:45 -05001583 * Side effect:
1584 * sets global pkg_cstate_limit to decode MSR_NHM_SNB_PKG_CST_CFG_CTL
Len Brownd7899442015-01-23 00:12:33 -05001585 */
Len Brownee7e38e2015-02-09 23:39:45 -05001586int probe_nhm_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04001587{
Len Brownee7e38e2015-02-09 23:39:45 -05001588 unsigned long long msr;
1589 int *pkg_cstate_limits;
1590
Len Brown103a8fe2010-10-22 23:53:03 -04001591 if (!genuine_intel)
1592 return 0;
1593
1594 if (family != 6)
1595 return 0;
1596
1597 switch (model) {
1598 case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
1599 case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
1600 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
1601 case 0x25: /* Westmere Client - Clarkdale, Arrandale */
1602 case 0x2C: /* Westmere EP - Gulftown */
Len Brownee7e38e2015-02-09 23:39:45 -05001603 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1604 case 0x2F: /* Westmere-EX Xeon - Eagleton */
1605 pkg_cstate_limits = nhm_pkg_cstate_limits;
1606 break;
Len Brown103a8fe2010-10-22 23:53:03 -04001607 case 0x2A: /* SNB */
1608 case 0x2D: /* SNB Xeon */
Len Brown553575f2011-11-18 03:32:01 -05001609 case 0x3A: /* IVB */
Len Brown13006512012-09-26 18:11:31 -04001610 case 0x3E: /* IVB Xeon */
Len Brownee7e38e2015-02-09 23:39:45 -05001611 pkg_cstate_limits = snb_pkg_cstate_limits;
1612 break;
Len Brown70b43402013-01-08 01:26:07 -05001613 case 0x3C: /* HSW */
Len Browne6f9bb32013-12-03 02:19:19 -05001614 case 0x3F: /* HSX */
Len Brown70b43402013-01-08 01:26:07 -05001615 case 0x45: /* HSW */
Len Brown149c2312013-03-15 10:58:02 -04001616 case 0x46: /* HSW */
Len Brown4e8e863f2014-02-27 23:28:53 -05001617 case 0x3D: /* BDW */
Len Brown48a06312015-02-10 15:38:04 -05001618 case 0x47: /* BDW */
Len Brown4e8e863f2014-02-27 23:28:53 -05001619 case 0x4F: /* BDX */
1620 case 0x56: /* BDX-DE */
Len Brownee7e38e2015-02-09 23:39:45 -05001621 pkg_cstate_limits = hsw_pkg_cstate_limits;
1622 break;
1623 case 0x37: /* BYT */
1624 case 0x4D: /* AVN */
1625 pkg_cstate_limits = slv_pkg_cstate_limits;
1626 break;
1627 case 0x4C: /* AMT */
1628 pkg_cstate_limits = amt_pkg_cstate_limits;
1629 break;
1630 case 0x57: /* PHI */
1631 pkg_cstate_limits = phi_pkg_cstate_limits;
1632 break;
Len Brown103a8fe2010-10-22 23:53:03 -04001633 default:
1634 return 0;
1635 }
Len Brownee7e38e2015-02-09 23:39:45 -05001636 get_msr(0, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
1637
1638 pkg_cstate_limit = pkg_cstate_limits[msr & 0x7];
1639
1640 return 1;
Len Brown103a8fe2010-10-22 23:53:03 -04001641}
Len Brownd7899442015-01-23 00:12:33 -05001642int has_nhm_turbo_ratio_limit(unsigned int family, unsigned int model)
1643{
Len Brownd7899442015-01-23 00:12:33 -05001644 switch (model) {
1645 /* Nehalem compatible, but do not include turbo-ratio limit support */
1646 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1647 case 0x2F: /* Westmere-EX Xeon - Eagleton */
1648 return 0;
1649 default:
1650 return 1;
1651 }
1652}
Len Brown6574a5d2012-09-21 00:01:31 -04001653int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
1654{
1655 if (!genuine_intel)
1656 return 0;
1657
1658 if (family != 6)
1659 return 0;
1660
1661 switch (model) {
1662 case 0x3E: /* IVB Xeon */
Len Brownfcd17212015-03-23 20:29:09 -04001663 case 0x3F: /* HSW Xeon */
Len Brown6574a5d2012-09-21 00:01:31 -04001664 return 1;
1665 default:
1666 return 0;
1667 }
1668}
Len Brownfcd17212015-03-23 20:29:09 -04001669int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
1670{
1671 if (!genuine_intel)
1672 return 0;
1673
1674 if (family != 6)
1675 return 0;
1676
1677 switch (model) {
1678 case 0x3F: /* HSW Xeon */
1679 return 1;
1680 default:
1681 return 0;
1682 }
1683}
1684
1685static void
1686dump_cstate_pstate_config_info(family, model)
1687{
1688 if (!do_nhm_platform_info)
1689 return;
1690
1691 dump_nhm_platform_info();
1692
1693 if (has_hsw_turbo_ratio_limit(family, model))
1694 dump_hsw_turbo_ratio_limits();
1695
1696 if (has_ivt_turbo_ratio_limit(family, model))
1697 dump_ivt_turbo_ratio_limits();
1698
1699 if (has_nhm_turbo_ratio_limit(family, model))
1700 dump_nhm_turbo_ratio_limits();
1701
1702 dump_nhm_cst_cfg();
1703}
1704
Len Brown6574a5d2012-09-21 00:01:31 -04001705
Len Brown889facb2012-11-08 00:48:57 -05001706/*
1707 * print_epb()
1708 * Decode the ENERGY_PERF_BIAS MSR
1709 */
1710int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1711{
1712 unsigned long long msr;
1713 char *epb_string;
1714 int cpu;
1715
1716 if (!has_epb)
1717 return 0;
1718
1719 cpu = t->cpu_id;
1720
1721 /* EPB is per-package */
1722 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1723 return 0;
1724
1725 if (cpu_migrate(cpu)) {
1726 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1727 return -1;
1728 }
1729
1730 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
1731 return 0;
1732
1733 switch (msr & 0x7) {
1734 case ENERGY_PERF_BIAS_PERFORMANCE:
1735 epb_string = "performance";
1736 break;
1737 case ENERGY_PERF_BIAS_NORMAL:
1738 epb_string = "balanced";
1739 break;
1740 case ENERGY_PERF_BIAS_POWERSAVE:
1741 epb_string = "powersave";
1742 break;
1743 default:
1744 epb_string = "custom";
1745 break;
1746 }
1747 fprintf(stderr, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
1748
1749 return 0;
1750}
1751
Len Brown3a9a9412014-08-15 02:39:52 -04001752/*
1753 * print_perf_limit()
1754 */
1755int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1756{
1757 unsigned long long msr;
1758 int cpu;
1759
1760 cpu = t->cpu_id;
1761
1762 /* per-package */
1763 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1764 return 0;
1765
1766 if (cpu_migrate(cpu)) {
1767 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1768 return -1;
1769 }
1770
1771 if (do_core_perf_limit_reasons) {
1772 get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
1773 fprintf(stderr, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
1774 fprintf(stderr, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
Len Browne33cbe82015-03-13 16:30:57 -04001775 (msr & 1 << 15) ? "bit15, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04001776 (msr & 1 << 14) ? "bit14, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04001777 (msr & 1 << 13) ? "Transitions, " : "",
1778 (msr & 1 << 12) ? "MultiCoreTurbo, " : "",
1779 (msr & 1 << 11) ? "PkgPwrL2, " : "",
1780 (msr & 1 << 10) ? "PkgPwrL1, " : "",
1781 (msr & 1 << 9) ? "CorePwr, " : "",
1782 (msr & 1 << 8) ? "Amps, " : "",
1783 (msr & 1 << 6) ? "VR-Therm, " : "",
1784 (msr & 1 << 5) ? "Auto-HWP, " : "",
1785 (msr & 1 << 4) ? "Graphics, " : "",
1786 (msr & 1 << 2) ? "bit2, " : "",
1787 (msr & 1 << 1) ? "ThermStatus, " : "",
1788 (msr & 1 << 0) ? "PROCHOT, " : "");
Len Brown3a9a9412014-08-15 02:39:52 -04001789 fprintf(stderr, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n",
Len Browne33cbe82015-03-13 16:30:57 -04001790 (msr & 1 << 31) ? "bit31, " : "",
Len Brown3a9a9412014-08-15 02:39:52 -04001791 (msr & 1 << 30) ? "bit30, " : "",
Len Browne33cbe82015-03-13 16:30:57 -04001792 (msr & 1 << 29) ? "Transitions, " : "",
1793 (msr & 1 << 28) ? "MultiCoreTurbo, " : "",
1794 (msr & 1 << 27) ? "PkgPwrL2, " : "",
1795 (msr & 1 << 26) ? "PkgPwrL1, " : "",
1796 (msr & 1 << 25) ? "CorePwr, " : "",
1797 (msr & 1 << 24) ? "Amps, " : "",
1798 (msr & 1 << 22) ? "VR-Therm, " : "",
1799 (msr & 1 << 21) ? "Auto-HWP, " : "",
1800 (msr & 1 << 20) ? "Graphics, " : "",
1801 (msr & 1 << 18) ? "bit18, " : "",
1802 (msr & 1 << 17) ? "ThermStatus, " : "",
1803 (msr & 1 << 16) ? "PROCHOT, " : "");
Len Brown3a9a9412014-08-15 02:39:52 -04001804
1805 }
1806 if (do_gfx_perf_limit_reasons) {
1807 get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
1808 fprintf(stderr, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
1809 fprintf(stderr, " (Active: %s%s%s%s%s%s%s%s)",
1810 (msr & 1 << 0) ? "PROCHOT, " : "",
1811 (msr & 1 << 1) ? "ThermStatus, " : "",
1812 (msr & 1 << 4) ? "Graphics, " : "",
1813 (msr & 1 << 6) ? "VR-Therm, " : "",
1814 (msr & 1 << 8) ? "Amps, " : "",
1815 (msr & 1 << 9) ? "GFXPwr, " : "",
1816 (msr & 1 << 10) ? "PkgPwrL1, " : "",
1817 (msr & 1 << 11) ? "PkgPwrL2, " : "");
1818 fprintf(stderr, " (Logged: %s%s%s%s%s%s%s%s)\n",
1819 (msr & 1 << 16) ? "PROCHOT, " : "",
1820 (msr & 1 << 17) ? "ThermStatus, " : "",
1821 (msr & 1 << 20) ? "Graphics, " : "",
1822 (msr & 1 << 22) ? "VR-Therm, " : "",
1823 (msr & 1 << 24) ? "Amps, " : "",
1824 (msr & 1 << 25) ? "GFXPwr, " : "",
1825 (msr & 1 << 26) ? "PkgPwrL1, " : "",
1826 (msr & 1 << 27) ? "PkgPwrL2, " : "");
1827 }
1828 if (do_ring_perf_limit_reasons) {
1829 get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
1830 fprintf(stderr, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
1831 fprintf(stderr, " (Active: %s%s%s%s%s%s)",
1832 (msr & 1 << 0) ? "PROCHOT, " : "",
1833 (msr & 1 << 1) ? "ThermStatus, " : "",
1834 (msr & 1 << 6) ? "VR-Therm, " : "",
1835 (msr & 1 << 8) ? "Amps, " : "",
1836 (msr & 1 << 10) ? "PkgPwrL1, " : "",
1837 (msr & 1 << 11) ? "PkgPwrL2, " : "");
1838 fprintf(stderr, " (Logged: %s%s%s%s%s%s)\n",
1839 (msr & 1 << 16) ? "PROCHOT, " : "",
1840 (msr & 1 << 17) ? "ThermStatus, " : "",
1841 (msr & 1 << 22) ? "VR-Therm, " : "",
1842 (msr & 1 << 24) ? "Amps, " : "",
1843 (msr & 1 << 26) ? "PkgPwrL1, " : "",
1844 (msr & 1 << 27) ? "PkgPwrL2, " : "");
1845 }
1846 return 0;
1847}
1848
Len Brown889facb2012-11-08 00:48:57 -05001849#define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
1850#define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
1851
Len Brown144b44b2013-11-09 00:30:16 -05001852double get_tdp(model)
1853{
1854 unsigned long long msr;
1855
1856 if (do_rapl & RAPL_PKG_POWER_INFO)
1857 if (!get_msr(0, MSR_PKG_POWER_INFO, &msr))
1858 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
1859
1860 switch (model) {
1861 case 0x37:
1862 case 0x4D:
1863 return 30.0;
1864 default:
1865 return 135.0;
1866 }
1867}
1868
1869
Len Brown889facb2012-11-08 00:48:57 -05001870/*
1871 * rapl_probe()
1872 *
Len Brown144b44b2013-11-09 00:30:16 -05001873 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
Len Brown889facb2012-11-08 00:48:57 -05001874 */
1875void rapl_probe(unsigned int family, unsigned int model)
1876{
1877 unsigned long long msr;
Len Brown144b44b2013-11-09 00:30:16 -05001878 unsigned int time_unit;
Len Brown889facb2012-11-08 00:48:57 -05001879 double tdp;
1880
1881 if (!genuine_intel)
1882 return;
1883
1884 if (family != 6)
1885 return;
1886
1887 switch (model) {
1888 case 0x2A:
1889 case 0x3A:
Len Brown70b43402013-01-08 01:26:07 -05001890 case 0x3C: /* HSW */
Len Brown70b43402013-01-08 01:26:07 -05001891 case 0x45: /* HSW */
Len Brown149c2312013-03-15 10:58:02 -04001892 case 0x46: /* HSW */
Len Brown4e8e863f2014-02-27 23:28:53 -05001893 case 0x3D: /* BDW */
Len Brown48a06312015-02-10 15:38:04 -05001894 case 0x47: /* BDW */
Len Brown144b44b2013-11-09 00:30:16 -05001895 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
Len Brown889facb2012-11-08 00:48:57 -05001896 break;
Len Browne6f9bb32013-12-03 02:19:19 -05001897 case 0x3F: /* HSX */
Len Brown4e8e863f2014-02-27 23:28:53 -05001898 case 0x4F: /* BDX */
1899 case 0x56: /* BDX-DE */
Len Browne6f9bb32013-12-03 02:19:19 -05001900 do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
1901 break;
Len Brown889facb2012-11-08 00:48:57 -05001902 case 0x2D:
1903 case 0x3E:
Len Brown144b44b2013-11-09 00:30:16 -05001904 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_PKG_PERF_STATUS | RAPL_DRAM_PERF_STATUS | RAPL_PKG_POWER_INFO;
1905 break;
1906 case 0x37: /* BYT */
1907 case 0x4D: /* AVN */
1908 do_rapl = RAPL_PKG | RAPL_CORES ;
Len Brown889facb2012-11-08 00:48:57 -05001909 break;
1910 default:
1911 return;
1912 }
1913
1914 /* units on package 0, verify later other packages match */
1915 if (get_msr(0, MSR_RAPL_POWER_UNIT, &msr))
1916 return;
1917
1918 rapl_power_units = 1.0 / (1 << (msr & 0xF));
Len Brown144b44b2013-11-09 00:30:16 -05001919 if (model == 0x37)
1920 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
1921 else
1922 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
Len Brown889facb2012-11-08 00:48:57 -05001923
Len Brown144b44b2013-11-09 00:30:16 -05001924 time_unit = msr >> 16 & 0xF;
1925 if (time_unit == 0)
1926 time_unit = 0xA;
Len Brown889facb2012-11-08 00:48:57 -05001927
Len Brown144b44b2013-11-09 00:30:16 -05001928 rapl_time_units = 1.0 / (1 << (time_unit));
1929
1930 tdp = get_tdp(model);
Len Brown889facb2012-11-08 00:48:57 -05001931
1932 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
Len Brownd8af6f52015-02-10 01:56:38 -05001933 if (debug)
Len Brown144b44b2013-11-09 00:30:16 -05001934 fprintf(stderr, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
Len Brown889facb2012-11-08 00:48:57 -05001935
1936 return;
1937}
1938
Len Brown3a9a9412014-08-15 02:39:52 -04001939void perf_limit_reasons_probe(family, model)
1940{
1941 if (!genuine_intel)
1942 return;
1943
1944 if (family != 6)
1945 return;
1946
1947 switch (model) {
1948 case 0x3C: /* HSW */
1949 case 0x45: /* HSW */
1950 case 0x46: /* HSW */
1951 do_gfx_perf_limit_reasons = 1;
1952 case 0x3F: /* HSX */
1953 do_core_perf_limit_reasons = 1;
1954 do_ring_perf_limit_reasons = 1;
1955 default:
1956 return;
1957 }
1958}
1959
Len Brown889facb2012-11-08 00:48:57 -05001960int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1961{
1962 unsigned long long msr;
1963 unsigned int dts;
1964 int cpu;
1965
1966 if (!(do_dts || do_ptm))
1967 return 0;
1968
1969 cpu = t->cpu_id;
1970
1971 /* DTS is per-core, no need to print for each thread */
1972 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1973 return 0;
1974
1975 if (cpu_migrate(cpu)) {
1976 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1977 return -1;
1978 }
1979
1980 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
1981 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1982 return 0;
1983
1984 dts = (msr >> 16) & 0x7F;
1985 fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
1986 cpu, msr, tcc_activation_temp - dts);
1987
1988#ifdef THERM_DEBUG
1989 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
1990 return 0;
1991
1992 dts = (msr >> 16) & 0x7F;
1993 dts2 = (msr >> 8) & 0x7F;
1994 fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
1995 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
1996#endif
1997 }
1998
1999
2000 if (do_dts) {
2001 unsigned int resolution;
2002
2003 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
2004 return 0;
2005
2006 dts = (msr >> 16) & 0x7F;
2007 resolution = (msr >> 27) & 0xF;
2008 fprintf(stderr, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
2009 cpu, msr, tcc_activation_temp - dts, resolution);
2010
2011#ifdef THERM_DEBUG
2012 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
2013 return 0;
2014
2015 dts = (msr >> 16) & 0x7F;
2016 dts2 = (msr >> 8) & 0x7F;
2017 fprintf(stderr, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
2018 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
2019#endif
2020 }
2021
2022 return 0;
2023}
2024
2025void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
2026{
2027 fprintf(stderr, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
2028 cpu, label,
2029 ((msr >> 15) & 1) ? "EN" : "DIS",
2030 ((msr >> 0) & 0x7FFF) * rapl_power_units,
2031 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
2032 (((msr >> 16) & 1) ? "EN" : "DIS"));
2033
2034 return;
2035}
2036
2037int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2038{
2039 unsigned long long msr;
2040 int cpu;
Len Brown889facb2012-11-08 00:48:57 -05002041
2042 if (!do_rapl)
2043 return 0;
2044
2045 /* RAPL counters are per package, so print only for 1st thread/package */
2046 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2047 return 0;
2048
2049 cpu = t->cpu_id;
2050 if (cpu_migrate(cpu)) {
2051 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
2052 return -1;
2053 }
2054
2055 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
2056 return -1;
2057
Len Brownd8af6f52015-02-10 01:56:38 -05002058 if (debug) {
Len Brown889facb2012-11-08 00:48:57 -05002059 fprintf(stderr, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx "
2060 "(%f Watts, %f Joules, %f sec.)\n", cpu, msr,
Len Brown144b44b2013-11-09 00:30:16 -05002061 rapl_power_units, rapl_energy_units, rapl_time_units);
Len Brown889facb2012-11-08 00:48:57 -05002062 }
Len Brown144b44b2013-11-09 00:30:16 -05002063 if (do_rapl & RAPL_PKG_POWER_INFO) {
2064
Len Brown889facb2012-11-08 00:48:57 -05002065 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
2066 return -5;
2067
2068
2069 fprintf(stderr, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
2070 cpu, msr,
2071 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2072 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2073 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2074 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
2075
Len Brown144b44b2013-11-09 00:30:16 -05002076 }
2077 if (do_rapl & RAPL_PKG) {
2078
Len Brown889facb2012-11-08 00:48:57 -05002079 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
2080 return -9;
2081
2082 fprintf(stderr, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
2083 cpu, msr, (msr >> 63) & 1 ? "": "UN");
2084
2085 print_power_limit_msr(cpu, msr, "PKG Limit #1");
2086 fprintf(stderr, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
2087 cpu,
2088 ((msr >> 47) & 1) ? "EN" : "DIS",
2089 ((msr >> 32) & 0x7FFF) * rapl_power_units,
2090 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
2091 ((msr >> 48) & 1) ? "EN" : "DIS");
2092 }
2093
2094 if (do_rapl & RAPL_DRAM) {
2095 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
2096 return -6;
2097
2098
2099 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
2100 cpu, msr,
2101 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2102 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2103 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2104 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
2105
2106
2107 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
2108 return -9;
2109 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
2110 cpu, msr, (msr >> 31) & 1 ? "": "UN");
2111
2112 print_power_limit_msr(cpu, msr, "DRAM Limit");
2113 }
Len Brown144b44b2013-11-09 00:30:16 -05002114 if (do_rapl & RAPL_CORE_POLICY) {
Len Brownd8af6f52015-02-10 01:56:38 -05002115 if (debug) {
Len Brown889facb2012-11-08 00:48:57 -05002116 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
2117 return -7;
2118
2119 fprintf(stderr, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown144b44b2013-11-09 00:30:16 -05002120 }
2121 }
2122 if (do_rapl & RAPL_CORES) {
Len Brownd8af6f52015-02-10 01:56:38 -05002123 if (debug) {
Len Brown889facb2012-11-08 00:48:57 -05002124
2125 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
2126 return -9;
2127 fprintf(stderr, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
2128 cpu, msr, (msr >> 31) & 1 ? "": "UN");
2129 print_power_limit_msr(cpu, msr, "Cores Limit");
2130 }
2131 }
2132 if (do_rapl & RAPL_GFX) {
Len Brownd8af6f52015-02-10 01:56:38 -05002133 if (debug) {
Len Brown889facb2012-11-08 00:48:57 -05002134 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
2135 return -8;
2136
2137 fprintf(stderr, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
2138
2139 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
2140 return -9;
2141 fprintf(stderr, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
2142 cpu, msr, (msr >> 31) & 1 ? "": "UN");
2143 print_power_limit_msr(cpu, msr, "GFX Limit");
2144 }
2145 }
2146 return 0;
2147}
2148
Len Brownd7899442015-01-23 00:12:33 -05002149/*
2150 * SNB adds support for additional MSRs:
2151 *
2152 * MSR_PKG_C7_RESIDENCY 0x000003fa
2153 * MSR_CORE_C7_RESIDENCY 0x000003fe
2154 * MSR_PKG_C2_RESIDENCY 0x0000060d
2155 */
Len Brown103a8fe2010-10-22 23:53:03 -04002156
Len Brownd7899442015-01-23 00:12:33 -05002157int has_snb_msrs(unsigned int family, unsigned int model)
Len Brown103a8fe2010-10-22 23:53:03 -04002158{
2159 if (!genuine_intel)
2160 return 0;
2161
2162 switch (model) {
2163 case 0x2A:
2164 case 0x2D:
Len Brown650a37f2012-06-03 23:34:44 -04002165 case 0x3A: /* IVB */
Len Brown13006512012-09-26 18:11:31 -04002166 case 0x3E: /* IVB Xeon */
Len Brown70b43402013-01-08 01:26:07 -05002167 case 0x3C: /* HSW */
2168 case 0x3F: /* HSW */
2169 case 0x45: /* HSW */
Len Brown149c2312013-03-15 10:58:02 -04002170 case 0x46: /* HSW */
Len Brown4e8e863f2014-02-27 23:28:53 -05002171 case 0x3D: /* BDW */
Len Brown48a06312015-02-10 15:38:04 -05002172 case 0x47: /* BDW */
Len Brown4e8e863f2014-02-27 23:28:53 -05002173 case 0x4F: /* BDX */
2174 case 0x56: /* BDX-DE */
Len Brown103a8fe2010-10-22 23:53:03 -04002175 return 1;
2176 }
2177 return 0;
2178}
2179
Len Brownd7899442015-01-23 00:12:33 -05002180/*
2181 * HSW adds support for additional MSRs:
2182 *
2183 * MSR_PKG_C8_RESIDENCY 0x00000630
2184 * MSR_PKG_C9_RESIDENCY 0x00000631
2185 * MSR_PKG_C10_RESIDENCY 0x00000632
2186 */
2187int has_hsw_msrs(unsigned int family, unsigned int model)
Kristen Carlson Accardica587102012-11-21 05:22:43 -08002188{
2189 if (!genuine_intel)
2190 return 0;
2191
2192 switch (model) {
Len Brown4e8e863f2014-02-27 23:28:53 -05002193 case 0x45: /* HSW */
2194 case 0x3D: /* BDW */
Kristen Carlson Accardica587102012-11-21 05:22:43 -08002195 return 1;
2196 }
2197 return 0;
2198}
2199
2200
Len Brown144b44b2013-11-09 00:30:16 -05002201int is_slm(unsigned int family, unsigned int model)
2202{
2203 if (!genuine_intel)
2204 return 0;
2205 switch (model) {
2206 case 0x37: /* BYT */
2207 case 0x4D: /* AVN */
2208 return 1;
2209 }
2210 return 0;
2211}
2212
2213#define SLM_BCLK_FREQS 5
2214double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
2215
2216double slm_bclk(void)
2217{
2218 unsigned long long msr = 3;
2219 unsigned int i;
2220 double freq;
2221
2222 if (get_msr(0, MSR_FSB_FREQ, &msr))
2223 fprintf(stderr, "SLM BCLK: unknown\n");
2224
2225 i = msr & 0xf;
2226 if (i >= SLM_BCLK_FREQS) {
2227 fprintf(stderr, "SLM BCLK[%d] invalid\n", i);
2228 msr = 3;
2229 }
2230 freq = slm_freq_table[i];
2231
2232 fprintf(stderr, "SLM BCLK: %.1f Mhz\n", freq);
2233
2234 return freq;
2235}
2236
Len Brown103a8fe2010-10-22 23:53:03 -04002237double discover_bclk(unsigned int family, unsigned int model)
2238{
Len Brownd7899442015-01-23 00:12:33 -05002239 if (has_snb_msrs(family, model))
Len Brown103a8fe2010-10-22 23:53:03 -04002240 return 100.00;
Len Brown144b44b2013-11-09 00:30:16 -05002241 else if (is_slm(family, model))
2242 return slm_bclk();
Len Brown103a8fe2010-10-22 23:53:03 -04002243 else
2244 return 133.33;
2245}
2246
Len Brown889facb2012-11-08 00:48:57 -05002247/*
2248 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
2249 * the Thermal Control Circuit (TCC) activates.
2250 * This is usually equal to tjMax.
2251 *
2252 * Older processors do not have this MSR, so there we guess,
2253 * but also allow cmdline over-ride with -T.
2254 *
2255 * Several MSR temperature values are in units of degrees-C
2256 * below this value, including the Digital Thermal Sensor (DTS),
2257 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
2258 */
2259int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2260{
2261 unsigned long long msr;
2262 unsigned int target_c_local;
2263 int cpu;
2264
2265 /* tcc_activation_temp is used only for dts or ptm */
2266 if (!(do_dts || do_ptm))
2267 return 0;
2268
2269 /* this is a per-package concept */
2270 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2271 return 0;
2272
2273 cpu = t->cpu_id;
2274 if (cpu_migrate(cpu)) {
2275 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
2276 return -1;
2277 }
2278
2279 if (tcc_activation_temp_override != 0) {
2280 tcc_activation_temp = tcc_activation_temp_override;
2281 fprintf(stderr, "cpu%d: Using cmdline TCC Target (%d C)\n",
2282 cpu, tcc_activation_temp);
2283 return 0;
2284 }
2285
2286 /* Temperature Target MSR is Nehalem and newer only */
Len Brownd7899442015-01-23 00:12:33 -05002287 if (!do_nhm_platform_info)
Len Brown889facb2012-11-08 00:48:57 -05002288 goto guess;
2289
2290 if (get_msr(0, MSR_IA32_TEMPERATURE_TARGET, &msr))
2291 goto guess;
2292
Jean Delvare34821242014-05-01 11:40:19 +02002293 target_c_local = (msr >> 16) & 0xFF;
Len Brown889facb2012-11-08 00:48:57 -05002294
Len Brownd8af6f52015-02-10 01:56:38 -05002295 if (debug)
Len Brown889facb2012-11-08 00:48:57 -05002296 fprintf(stderr, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
2297 cpu, msr, target_c_local);
2298
Jean Delvare34821242014-05-01 11:40:19 +02002299 if (!target_c_local)
Len Brown889facb2012-11-08 00:48:57 -05002300 goto guess;
2301
2302 tcc_activation_temp = target_c_local;
2303
2304 return 0;
2305
2306guess:
2307 tcc_activation_temp = TJMAX_DEFAULT;
2308 fprintf(stderr, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
2309 cpu, tcc_activation_temp);
2310
2311 return 0;
2312}
Len Brownfcd17212015-03-23 20:29:09 -04002313void process_cpuid()
Len Brown103a8fe2010-10-22 23:53:03 -04002314{
2315 unsigned int eax, ebx, ecx, edx, max_level;
2316 unsigned int fms, family, model, stepping;
2317
2318 eax = ebx = ecx = edx = 0;
2319
Josh Triplett2b928652013-08-20 17:20:14 -07002320 __get_cpuid(0, &max_level, &ebx, &ecx, &edx);
Len Brown103a8fe2010-10-22 23:53:03 -04002321
2322 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
2323 genuine_intel = 1;
2324
Len Brownd8af6f52015-02-10 01:56:38 -05002325 if (debug)
Len Brown889facb2012-11-08 00:48:57 -05002326 fprintf(stderr, "CPUID(0): %.4s%.4s%.4s ",
Len Brown103a8fe2010-10-22 23:53:03 -04002327 (char *)&ebx, (char *)&edx, (char *)&ecx);
2328
Josh Triplett2b928652013-08-20 17:20:14 -07002329 __get_cpuid(1, &fms, &ebx, &ecx, &edx);
Len Brown103a8fe2010-10-22 23:53:03 -04002330 family = (fms >> 8) & 0xf;
2331 model = (fms >> 4) & 0xf;
2332 stepping = fms & 0xf;
2333 if (family == 6 || family == 0xf)
2334 model += ((fms >> 16) & 0xf) << 4;
2335
Len Brownd8af6f52015-02-10 01:56:38 -05002336 if (debug)
Len Brown103a8fe2010-10-22 23:53:03 -04002337 fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
2338 max_level, family, model, stepping, family, model, stepping);
2339
Josh Triplettb2c95d92013-08-20 17:20:18 -07002340 if (!(edx & (1 << 5)))
2341 errx(1, "CPUID: no MSR");
Len Brown103a8fe2010-10-22 23:53:03 -04002342
2343 /*
2344 * check max extended function levels of CPUID.
2345 * This is needed to check for invariant TSC.
2346 * This check is valid for both Intel and AMD.
2347 */
2348 ebx = ecx = edx = 0;
Josh Triplett2b928652013-08-20 17:20:14 -07002349 __get_cpuid(0x80000000, &max_level, &ebx, &ecx, &edx);
Len Brown103a8fe2010-10-22 23:53:03 -04002350
Len Brownd7899442015-01-23 00:12:33 -05002351 if (max_level >= 0x80000007) {
Len Brown103a8fe2010-10-22 23:53:03 -04002352
Len Brownd7899442015-01-23 00:12:33 -05002353 /*
2354 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
2355 * this check is valid for both Intel and AMD
2356 */
2357 __get_cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
2358 has_invariant_tsc = edx & (1 << 8);
2359 }
Len Brown103a8fe2010-10-22 23:53:03 -04002360
2361 /*
2362 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
2363 * this check is valid for both Intel and AMD
2364 */
2365
Josh Triplett2b928652013-08-20 17:20:14 -07002366 __get_cpuid(0x6, &eax, &ebx, &ecx, &edx);
Thomas Renninger8209e052011-01-21 15:11:19 +01002367 has_aperf = ecx & (1 << 0);
Len Brown889facb2012-11-08 00:48:57 -05002368 do_dts = eax & (1 << 0);
2369 do_ptm = eax & (1 << 6);
2370 has_epb = ecx & (1 << 3);
2371
Len Brownd8af6f52015-02-10 01:56:38 -05002372 if (debug)
Len Browna7296172015-01-23 01:33:58 -05002373 fprintf(stderr, "CPUID(6): %sAPERF, %sDTS, %sPTM, %sEPB\n",
2374 has_aperf ? "" : "No ",
2375 do_dts ? "" : "No ",
2376 do_ptm ? "" : "No ",
2377 has_epb ? "" : "No ");
Len Brown103a8fe2010-10-22 23:53:03 -04002378
Len Brownee7e38e2015-02-09 23:39:45 -05002379 do_nhm_platform_info = do_nhm_cstates = do_smi = probe_nhm_msrs(family, model);
Len Brownd7899442015-01-23 00:12:33 -05002380 do_snb_cstates = has_snb_msrs(family, model);
Len Brownee7e38e2015-02-09 23:39:45 -05002381 do_pc2 = do_snb_cstates && (pkg_cstate_limit >= PCL__2);
2382 do_pc3 = (pkg_cstate_limit >= PCL__3);
2383 do_pc6 = (pkg_cstate_limit >= PCL__6);
2384 do_pc7 = do_snb_cstates && (pkg_cstate_limit >= PCL__7);
Len Brownd7899442015-01-23 00:12:33 -05002385 do_c8_c9_c10 = has_hsw_msrs(family, model);
Len Brown144b44b2013-11-09 00:30:16 -05002386 do_slm_cstates = is_slm(family, model);
Len Brown103a8fe2010-10-22 23:53:03 -04002387 bclk = discover_bclk(family, model);
2388
Len Brown889facb2012-11-08 00:48:57 -05002389 rapl_probe(family, model);
Len Brown3a9a9412014-08-15 02:39:52 -04002390 perf_limit_reasons_probe(family, model);
Len Brown889facb2012-11-08 00:48:57 -05002391
Len Brownfcd17212015-03-23 20:29:09 -04002392 if (debug)
2393 dump_cstate_pstate_config_info();
2394
Len Brown889facb2012-11-08 00:48:57 -05002395 return;
Len Brown103a8fe2010-10-22 23:53:03 -04002396}
2397
Len Brownd8af6f52015-02-10 01:56:38 -05002398void help()
Len Brown103a8fe2010-10-22 23:53:03 -04002399{
Len Brownd8af6f52015-02-10 01:56:38 -05002400 fprintf(stderr,
2401 "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
2402 "\n"
2403 "Turbostat forks the specified COMMAND and prints statistics\n"
2404 "when COMMAND completes.\n"
2405 "If no COMMAND is specified, turbostat wakes every 5-seconds\n"
2406 "to print statistics, until interrupted.\n"
2407 "--debug run in \"debug\" mode\n"
2408 "--interval sec Override default 5-second measurement interval\n"
2409 "--help print this help message\n"
2410 "--counter msr print 32-bit counter at address \"msr\"\n"
2411 "--Counter msr print 64-bit Counter at address \"msr\"\n"
2412 "--msr msr print 32-bit value at address \"msr\"\n"
2413 "--MSR msr print 64-bit Value at address \"msr\"\n"
2414 "--version print version information\n"
2415 "\n"
2416 "For more help, run \"man turbostat\"\n");
Len Brown103a8fe2010-10-22 23:53:03 -04002417}
2418
2419
2420/*
2421 * in /dev/cpu/ return success for names that are numbers
2422 * ie. filter out ".", "..", "microcode".
2423 */
2424int dir_filter(const struct dirent *dirp)
2425{
2426 if (isdigit(dirp->d_name[0]))
2427 return 1;
2428 else
2429 return 0;
2430}
2431
2432int open_dev_cpu_msr(int dummy1)
2433{
2434 return 0;
2435}
2436
Len Brownc98d5d92012-06-04 00:56:40 -04002437void topology_probe()
2438{
2439 int i;
2440 int max_core_id = 0;
2441 int max_package_id = 0;
2442 int max_siblings = 0;
2443 struct cpu_topology {
2444 int core_id;
2445 int physical_package_id;
2446 } *cpus;
2447
2448 /* Initialize num_cpus, max_cpu_num */
2449 topo.num_cpus = 0;
2450 topo.max_cpu_num = 0;
2451 for_all_proc_cpus(count_cpus);
2452 if (!summary_only && topo.num_cpus > 1)
2453 show_cpu = 1;
2454
Len Brownd8af6f52015-02-10 01:56:38 -05002455 if (debug > 1)
Len Brownc98d5d92012-06-04 00:56:40 -04002456 fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
2457
2458 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
Josh Triplettb2c95d92013-08-20 17:20:18 -07002459 if (cpus == NULL)
2460 err(1, "calloc cpus");
Len Brownc98d5d92012-06-04 00:56:40 -04002461
2462 /*
2463 * Allocate and initialize cpu_present_set
2464 */
2465 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07002466 if (cpu_present_set == NULL)
2467 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04002468 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2469 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
2470 for_all_proc_cpus(mark_cpu_present);
2471
2472 /*
2473 * Allocate and initialize cpu_affinity_set
2474 */
2475 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07002476 if (cpu_affinity_set == NULL)
2477 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04002478 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2479 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
2480
2481
2482 /*
2483 * For online cpus
2484 * find max_core_id, max_package_id
2485 */
2486 for (i = 0; i <= topo.max_cpu_num; ++i) {
2487 int siblings;
2488
2489 if (cpu_is_not_present(i)) {
Len Brownd8af6f52015-02-10 01:56:38 -05002490 if (debug > 1)
Len Brownc98d5d92012-06-04 00:56:40 -04002491 fprintf(stderr, "cpu%d NOT PRESENT\n", i);
2492 continue;
2493 }
2494 cpus[i].core_id = get_core_id(i);
2495 if (cpus[i].core_id > max_core_id)
2496 max_core_id = cpus[i].core_id;
2497
2498 cpus[i].physical_package_id = get_physical_package_id(i);
2499 if (cpus[i].physical_package_id > max_package_id)
2500 max_package_id = cpus[i].physical_package_id;
2501
2502 siblings = get_num_ht_siblings(i);
2503 if (siblings > max_siblings)
2504 max_siblings = siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05002505 if (debug > 1)
Len Brownc98d5d92012-06-04 00:56:40 -04002506 fprintf(stderr, "cpu %d pkg %d core %d\n",
2507 i, cpus[i].physical_package_id, cpus[i].core_id);
2508 }
2509 topo.num_cores_per_pkg = max_core_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05002510 if (debug > 1)
Len Brownc98d5d92012-06-04 00:56:40 -04002511 fprintf(stderr, "max_core_id %d, sizing for %d cores per package\n",
2512 max_core_id, topo.num_cores_per_pkg);
Len Brown1cc21f72015-02-23 00:34:57 -05002513 if (debug && !summary_only && topo.num_cores_per_pkg > 1)
Len Brownc98d5d92012-06-04 00:56:40 -04002514 show_core = 1;
2515
2516 topo.num_packages = max_package_id + 1;
Len Brownd8af6f52015-02-10 01:56:38 -05002517 if (debug > 1)
Len Brownc98d5d92012-06-04 00:56:40 -04002518 fprintf(stderr, "max_package_id %d, sizing for %d packages\n",
2519 max_package_id, topo.num_packages);
Len Brown1cc21f72015-02-23 00:34:57 -05002520 if (debug && !summary_only && topo.num_packages > 1)
Len Brownc98d5d92012-06-04 00:56:40 -04002521 show_pkg = 1;
2522
2523 topo.num_threads_per_core = max_siblings;
Len Brownd8af6f52015-02-10 01:56:38 -05002524 if (debug > 1)
Len Brownc98d5d92012-06-04 00:56:40 -04002525 fprintf(stderr, "max_siblings %d\n", max_siblings);
2526
2527 free(cpus);
2528}
2529
2530void
2531allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
2532{
2533 int i;
2534
2535 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
2536 topo.num_packages, sizeof(struct thread_data));
2537 if (*t == NULL)
2538 goto error;
2539
2540 for (i = 0; i < topo.num_threads_per_core *
2541 topo.num_cores_per_pkg * topo.num_packages; i++)
2542 (*t)[i].cpu_id = -1;
2543
2544 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
2545 sizeof(struct core_data));
2546 if (*c == NULL)
2547 goto error;
2548
2549 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
2550 (*c)[i].core_id = -1;
2551
2552 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
2553 if (*p == NULL)
2554 goto error;
2555
2556 for (i = 0; i < topo.num_packages; i++)
2557 (*p)[i].package_id = i;
2558
2559 return;
2560error:
Josh Triplettb2c95d92013-08-20 17:20:18 -07002561 err(1, "calloc counters");
Len Brownc98d5d92012-06-04 00:56:40 -04002562}
2563/*
2564 * init_counter()
2565 *
2566 * set cpu_id, core_num, pkg_num
2567 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
2568 *
2569 * increment topo.num_cores when 1st core in pkg seen
2570 */
2571void init_counter(struct thread_data *thread_base, struct core_data *core_base,
2572 struct pkg_data *pkg_base, int thread_num, int core_num,
2573 int pkg_num, int cpu_id)
2574{
2575 struct thread_data *t;
2576 struct core_data *c;
2577 struct pkg_data *p;
2578
2579 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
2580 c = GET_CORE(core_base, core_num, pkg_num);
2581 p = GET_PKG(pkg_base, pkg_num);
2582
2583 t->cpu_id = cpu_id;
2584 if (thread_num == 0) {
2585 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
2586 if (cpu_is_first_core_in_package(cpu_id))
2587 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
2588 }
2589
2590 c->core_id = core_num;
2591 p->package_id = pkg_num;
2592}
2593
2594
2595int initialize_counters(int cpu_id)
2596{
2597 int my_thread_id, my_core_id, my_package_id;
2598
2599 my_package_id = get_physical_package_id(cpu_id);
2600 my_core_id = get_core_id(cpu_id);
2601
2602 if (cpu_is_first_sibling_in_core(cpu_id)) {
2603 my_thread_id = 0;
2604 topo.num_cores++;
2605 } else {
2606 my_thread_id = 1;
2607 }
2608
2609 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2610 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2611 return 0;
2612}
2613
2614void allocate_output_buffer()
2615{
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02002616 output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
Len Brownc98d5d92012-06-04 00:56:40 -04002617 outp = output_buffer;
Josh Triplettb2c95d92013-08-20 17:20:18 -07002618 if (outp == NULL)
2619 err(-1, "calloc output buffer");
Len Brownc98d5d92012-06-04 00:56:40 -04002620}
2621
2622void setup_all_buffers(void)
2623{
2624 topology_probe();
2625 allocate_counters(&thread_even, &core_even, &package_even);
2626 allocate_counters(&thread_odd, &core_odd, &package_odd);
2627 allocate_output_buffer();
2628 for_all_proc_cpus(initialize_counters);
2629}
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02002630
Len Brown103a8fe2010-10-22 23:53:03 -04002631void turbostat_init()
2632{
Len Brown103a8fe2010-10-22 23:53:03 -04002633 check_dev_msr();
Len Brown98481e72014-08-15 00:36:50 -04002634 check_permissions();
Len Brownfcd17212015-03-23 20:29:09 -04002635 process_cpuid();
Len Brown103a8fe2010-10-22 23:53:03 -04002636
Len Brownc98d5d92012-06-04 00:56:40 -04002637 setup_all_buffers();
Len Brown103a8fe2010-10-22 23:53:03 -04002638
Len Brownd8af6f52015-02-10 01:56:38 -05002639 if (debug)
Len Brown889facb2012-11-08 00:48:57 -05002640 for_all_cpus(print_epb, ODD_COUNTERS);
2641
Len Brownd8af6f52015-02-10 01:56:38 -05002642 if (debug)
Len Brown3a9a9412014-08-15 02:39:52 -04002643 for_all_cpus(print_perf_limit, ODD_COUNTERS);
2644
Len Brownd8af6f52015-02-10 01:56:38 -05002645 if (debug)
Len Brown889facb2012-11-08 00:48:57 -05002646 for_all_cpus(print_rapl, ODD_COUNTERS);
2647
2648 for_all_cpus(set_temperature_target, ODD_COUNTERS);
2649
Len Brownd8af6f52015-02-10 01:56:38 -05002650 if (debug)
Len Brown889facb2012-11-08 00:48:57 -05002651 for_all_cpus(print_thermal, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04002652}
2653
2654int fork_it(char **argv)
2655{
Len Brown103a8fe2010-10-22 23:53:03 -04002656 pid_t child_pid;
Len Brownd91bb172012-11-01 00:08:19 -04002657 int status;
Len Brownd15cf7c2012-06-03 23:24:00 -04002658
Len Brownd91bb172012-11-01 00:08:19 -04002659 status = for_all_cpus(get_counters, EVEN_COUNTERS);
2660 if (status)
2661 exit(status);
Len Brownc98d5d92012-06-04 00:56:40 -04002662 /* clear affinity side-effect of get_counters() */
2663 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
Len Brown103a8fe2010-10-22 23:53:03 -04002664 gettimeofday(&tv_even, (struct timezone *)NULL);
2665
2666 child_pid = fork();
2667 if (!child_pid) {
2668 /* child */
2669 execvp(argv[0], argv);
2670 } else {
Len Brown103a8fe2010-10-22 23:53:03 -04002671
2672 /* parent */
Josh Triplettb2c95d92013-08-20 17:20:18 -07002673 if (child_pid == -1)
2674 err(1, "fork");
Len Brown103a8fe2010-10-22 23:53:03 -04002675
2676 signal(SIGINT, SIG_IGN);
2677 signal(SIGQUIT, SIG_IGN);
Josh Triplettb2c95d92013-08-20 17:20:18 -07002678 if (waitpid(child_pid, &status, 0) == -1)
2679 err(status, "waitpid");
Len Brown103a8fe2010-10-22 23:53:03 -04002680 }
Len Brownc98d5d92012-06-04 00:56:40 -04002681 /*
2682 * n.b. fork_it() does not check for errors from for_all_cpus()
2683 * because re-starting is problematic when forking
2684 */
2685 for_all_cpus(get_counters, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04002686 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002687 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04002688 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
2689 compute_average(EVEN_COUNTERS);
2690 format_all_counters(EVEN_COUNTERS);
2691 flush_stderr();
Len Brown103a8fe2010-10-22 23:53:03 -04002692
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07002693 fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
Len Brown103a8fe2010-10-22 23:53:03 -04002694
Len Brownd91bb172012-11-01 00:08:19 -04002695 return status;
Len Brown103a8fe2010-10-22 23:53:03 -04002696}
2697
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02002698int get_and_dump_counters(void)
2699{
2700 int status;
2701
2702 status = for_all_cpus(get_counters, ODD_COUNTERS);
2703 if (status)
2704 return status;
2705
2706 status = for_all_cpus(dump_counters, ODD_COUNTERS);
2707 if (status)
2708 return status;
2709
2710 flush_stdout();
2711
2712 return status;
2713}
2714
Len Brownd8af6f52015-02-10 01:56:38 -05002715void print_version() {
Len Brownfcd17212015-03-23 20:29:09 -04002716 fprintf(stderr, "turbostat version 4.2 23 Mar, 2015"
Len Brownd8af6f52015-02-10 01:56:38 -05002717 " - Len Brown <lenb@kernel.org>\n");
2718}
2719
Len Brown103a8fe2010-10-22 23:53:03 -04002720void cmdline(int argc, char **argv)
2721{
2722 int opt;
Len Brownd8af6f52015-02-10 01:56:38 -05002723 int option_index = 0;
2724 static struct option long_options[] = {
2725 {"Counter", required_argument, 0, 'C'},
2726 {"counter", required_argument, 0, 'c'},
2727 {"Dump", no_argument, 0, 'D'},
2728 {"debug", no_argument, 0, 'd'},
2729 {"interval", required_argument, 0, 'i'},
2730 {"help", no_argument, 0, 'h'},
2731 {"Joules", no_argument, 0, 'J'},
2732 {"MSR", required_argument, 0, 'M'},
2733 {"msr", required_argument, 0, 'm'},
2734 {"Package", no_argument, 0, 'p'},
2735 {"processor", no_argument, 0, 'p'},
2736 {"Summary", no_argument, 0, 'S'},
2737 {"TCC", required_argument, 0, 'T'},
2738 {"version", no_argument, 0, 'v' },
2739 {0, 0, 0, 0 }
2740 };
Len Brown103a8fe2010-10-22 23:53:03 -04002741
2742 progname = argv[0];
2743
Len Brownd8af6f52015-02-10 01:56:38 -05002744 while ((opt = getopt_long_only(argc, argv, "C:c:Ddhi:JM:m:PpST:v",
2745 long_options, &option_index)) != -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04002746 switch (opt) {
Len Brownd8af6f52015-02-10 01:56:38 -05002747 case 'C':
2748 sscanf(optarg, "%x", &extra_delta_offset64);
Len Brown103a8fe2010-10-22 23:53:03 -04002749 break;
Len Brownf9240812012-10-06 15:26:31 -04002750 case 'c':
Len Brown8e180f32012-09-22 01:25:08 -04002751 sscanf(optarg, "%x", &extra_delta_offset32);
2752 break;
Len Brownd8af6f52015-02-10 01:56:38 -05002753 case 'D':
2754 dump_only++;
Len Brown8e180f32012-09-22 01:25:08 -04002755 break;
Len Brownd8af6f52015-02-10 01:56:38 -05002756 case 'd':
2757 debug++;
Len Brown2f32edf2012-09-21 23:45:46 -04002758 break;
Len Brownd8af6f52015-02-10 01:56:38 -05002759 case 'h':
2760 default:
2761 help();
2762 exit(1);
2763 case 'i':
2764 interval_sec = atoi(optarg);
Len Brown889facb2012-11-08 00:48:57 -05002765 break;
Dirk Brandewie5c56be92013-12-16 10:23:41 -08002766 case 'J':
2767 rapl_joules++;
2768 break;
Len Brownd8af6f52015-02-10 01:56:38 -05002769 case 'M':
2770 sscanf(optarg, "%x", &extra_msr_offset64);
2771 break;
2772 case 'm':
2773 sscanf(optarg, "%x", &extra_msr_offset32);
2774 break;
2775 case 'P':
2776 show_pkg_only++;
2777 break;
2778 case 'p':
2779 show_core_only++;
2780 break;
2781 case 'S':
2782 summary_only++;
2783 break;
2784 case 'T':
2785 tcc_activation_temp_override = atoi(optarg);
2786 break;
2787 case 'v':
2788 print_version();
2789 exit(0);
2790 break;
Len Brown103a8fe2010-10-22 23:53:03 -04002791 }
2792 }
2793}
2794
2795int main(int argc, char **argv)
2796{
2797 cmdline(argc, argv);
2798
Len Brownd8af6f52015-02-10 01:56:38 -05002799 if (debug)
2800 print_version();
Len Brown103a8fe2010-10-22 23:53:03 -04002801
2802 turbostat_init();
2803
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02002804 /* dump counters and exit */
2805 if (dump_only)
2806 return get_and_dump_counters();
2807
Len Brown103a8fe2010-10-22 23:53:03 -04002808 /*
2809 * if any params left, it must be a command to fork
2810 */
2811 if (argc - optind)
2812 return fork_it(argv + optind);
2813 else
2814 turbostat_loop();
2815
2816 return 0;
2817}