blob: 77eb130168da376ad976d8adb9abcea3f04ee53d [file] [log] [blame]
Len Brown103a8fe2010-10-22 23:53:03 -04001/*
2 * turbostat -- show CPU frequency and C-state residency
3 * on modern Intel turbo-capable processors.
4 *
Len Brown144b44b2013-11-09 00:30:16 -05005 * Copyright (c) 2013 Intel Corporation.
Len Brown103a8fe2010-10-22 23:53:03 -04006 * Len Brown <len.brown@intel.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
Len Brown88c32812012-03-29 21:44:40 -040022#define _GNU_SOURCE
Josh Triplettb731f312013-08-20 17:20:12 -070023#include MSRHEADER
Josh Triplett95aebc42013-08-20 17:20:16 -070024#include <stdarg.h>
Len Brown103a8fe2010-10-22 23:53:03 -040025#include <stdio.h>
Josh Triplettb2c95d92013-08-20 17:20:18 -070026#include <err.h>
Len Brown103a8fe2010-10-22 23:53:03 -040027#include <unistd.h>
28#include <sys/types.h>
29#include <sys/wait.h>
30#include <sys/stat.h>
31#include <sys/resource.h>
32#include <fcntl.h>
33#include <signal.h>
34#include <sys/time.h>
35#include <stdlib.h>
36#include <dirent.h>
37#include <string.h>
38#include <ctype.h>
Len Brown88c32812012-03-29 21:44:40 -040039#include <sched.h>
Josh Triplett2b928652013-08-20 17:20:14 -070040#include <cpuid.h>
Len Brown103a8fe2010-10-22 23:53:03 -040041
Len Brown103a8fe2010-10-22 23:53:03 -040042char *proc_stat = "/proc/stat";
43unsigned int interval_sec = 5; /* set with -i interval_sec */
44unsigned int verbose; /* set with -v */
Len Brown889facb2012-11-08 00:48:57 -050045unsigned int rapl_verbose; /* set with -R */
Dirk Brandewie5c56be92013-12-16 10:23:41 -080046unsigned int rapl_joules; /* set with -J */
Len Brown889facb2012-11-08 00:48:57 -050047unsigned int thermal_verbose; /* set with -T */
Andy Shevchenkof591c382014-01-23 17:13:14 +020048unsigned int summary_only; /* set with -S */
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +020049unsigned int dump_only; /* set with -s */
Len Brown103a8fe2010-10-22 23:53:03 -040050unsigned int skip_c0;
51unsigned int skip_c1;
52unsigned int do_nhm_cstates;
53unsigned int do_snb_cstates;
Kristen Carlson Accardica587102012-11-21 05:22:43 -080054unsigned int do_c8_c9_c10;
Len Brown144b44b2013-11-09 00:30:16 -050055unsigned int do_slm_cstates;
56unsigned int use_c1_residency_msr;
Len Brown103a8fe2010-10-22 23:53:03 -040057unsigned int has_aperf;
Len Brown889facb2012-11-08 00:48:57 -050058unsigned int has_epb;
Len Brown103a8fe2010-10-22 23:53:03 -040059unsigned int units = 1000000000; /* Ghz etc */
60unsigned int genuine_intel;
61unsigned int has_invariant_tsc;
62unsigned int do_nehalem_platform_info;
63unsigned int do_nehalem_turbo_ratio_limit;
Len Brown6574a5d2012-09-21 00:01:31 -040064unsigned int do_ivt_turbo_ratio_limit;
Len Brown2f32edf2012-09-21 23:45:46 -040065unsigned int extra_msr_offset32;
66unsigned int extra_msr_offset64;
Len Brown8e180f32012-09-22 01:25:08 -040067unsigned int extra_delta_offset32;
68unsigned int extra_delta_offset64;
Len Brown1ed51012013-02-10 17:19:24 -050069int do_smi;
Len Brown103a8fe2010-10-22 23:53:03 -040070double bclk;
71unsigned int show_pkg;
72unsigned int show_core;
73unsigned int show_cpu;
Len Brownc98d5d92012-06-04 00:56:40 -040074unsigned int show_pkg_only;
75unsigned int show_core_only;
76char *output_buffer, *outp;
Len Brown889facb2012-11-08 00:48:57 -050077unsigned int do_rapl;
78unsigned int do_dts;
79unsigned int do_ptm;
80unsigned int tcc_activation_temp;
81unsigned int tcc_activation_temp_override;
82double rapl_power_units, rapl_energy_units, rapl_time_units;
83double rapl_joule_counter_range;
84
Len Browne6f9bb32013-12-03 02:19:19 -050085#define RAPL_PKG (1 << 0)
86 /* 0x610 MSR_PKG_POWER_LIMIT */
87 /* 0x611 MSR_PKG_ENERGY_STATUS */
88#define RAPL_PKG_PERF_STATUS (1 << 1)
89 /* 0x613 MSR_PKG_PERF_STATUS */
90#define RAPL_PKG_POWER_INFO (1 << 2)
91 /* 0x614 MSR_PKG_POWER_INFO */
92
93#define RAPL_DRAM (1 << 3)
94 /* 0x618 MSR_DRAM_POWER_LIMIT */
95 /* 0x619 MSR_DRAM_ENERGY_STATUS */
96 /* 0x61c MSR_DRAM_POWER_INFO */
97#define RAPL_DRAM_PERF_STATUS (1 << 4)
98 /* 0x61b MSR_DRAM_PERF_STATUS */
99
100#define RAPL_CORES (1 << 5)
101 /* 0x638 MSR_PP0_POWER_LIMIT */
102 /* 0x639 MSR_PP0_ENERGY_STATUS */
103#define RAPL_CORE_POLICY (1 << 6)
104 /* 0x63a MSR_PP0_POLICY */
105
106
107#define RAPL_GFX (1 << 7)
108 /* 0x640 MSR_PP1_POWER_LIMIT */
109 /* 0x641 MSR_PP1_ENERGY_STATUS */
110 /* 0x642 MSR_PP1_POLICY */
Len Brown889facb2012-11-08 00:48:57 -0500111#define TJMAX_DEFAULT 100
112
113#define MAX(a, b) ((a) > (b) ? (a) : (b))
Len Brown103a8fe2010-10-22 23:53:03 -0400114
115int aperf_mperf_unstable;
116int backwards_count;
117char *progname;
Len Brown103a8fe2010-10-22 23:53:03 -0400118
Len Brownc98d5d92012-06-04 00:56:40 -0400119cpu_set_t *cpu_present_set, *cpu_affinity_set;
120size_t cpu_present_setsize, cpu_affinity_setsize;
Len Brown103a8fe2010-10-22 23:53:03 -0400121
Len Brownc98d5d92012-06-04 00:56:40 -0400122struct thread_data {
123 unsigned long long tsc;
124 unsigned long long aperf;
125 unsigned long long mperf;
Len Brown144b44b2013-11-09 00:30:16 -0500126 unsigned long long c1;
Len Brown2f32edf2012-09-21 23:45:46 -0400127 unsigned long long extra_msr64;
Len Brown8e180f32012-09-22 01:25:08 -0400128 unsigned long long extra_delta64;
129 unsigned long long extra_msr32;
130 unsigned long long extra_delta32;
Len Brown1ed51012013-02-10 17:19:24 -0500131 unsigned int smi_count;
Len Brownc98d5d92012-06-04 00:56:40 -0400132 unsigned int cpu_id;
133 unsigned int flags;
134#define CPU_IS_FIRST_THREAD_IN_CORE 0x2
135#define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
136} *thread_even, *thread_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400137
Len Brownc98d5d92012-06-04 00:56:40 -0400138struct core_data {
139 unsigned long long c3;
140 unsigned long long c6;
141 unsigned long long c7;
Len Brown889facb2012-11-08 00:48:57 -0500142 unsigned int core_temp_c;
Len Brownc98d5d92012-06-04 00:56:40 -0400143 unsigned int core_id;
144} *core_even, *core_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400145
Len Brownc98d5d92012-06-04 00:56:40 -0400146struct pkg_data {
147 unsigned long long pc2;
148 unsigned long long pc3;
149 unsigned long long pc6;
150 unsigned long long pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800151 unsigned long long pc8;
152 unsigned long long pc9;
153 unsigned long long pc10;
Len Brownc98d5d92012-06-04 00:56:40 -0400154 unsigned int package_id;
Len Brown889facb2012-11-08 00:48:57 -0500155 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
156 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
157 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
158 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
159 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
160 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
161 unsigned int pkg_temp_c;
162
Len Brownc98d5d92012-06-04 00:56:40 -0400163} *package_even, *package_odd;
164
165#define ODD_COUNTERS thread_odd, core_odd, package_odd
166#define EVEN_COUNTERS thread_even, core_even, package_even
167
168#define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
169 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
170 topo.num_threads_per_core + \
171 (core_no) * topo.num_threads_per_core + (thread_no))
172#define GET_CORE(core_base, core_no, pkg_no) \
173 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
174#define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
175
176struct system_summary {
177 struct thread_data threads;
178 struct core_data cores;
179 struct pkg_data packages;
180} sum, average;
181
182
183struct topo_params {
184 int num_packages;
185 int num_cpus;
186 int num_cores;
187 int max_cpu_num;
188 int num_cores_per_pkg;
189 int num_threads_per_core;
190} topo;
191
192struct timeval tv_even, tv_odd, tv_delta;
193
194void setup_all_buffers(void);
195
196int cpu_is_not_present(int cpu)
Len Brownd15cf7c2012-06-03 23:24:00 -0400197{
Len Brownc98d5d92012-06-04 00:56:40 -0400198 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brownd15cf7c2012-06-03 23:24:00 -0400199}
Len Brown88c32812012-03-29 21:44:40 -0400200/*
Len Brownc98d5d92012-06-04 00:56:40 -0400201 * run func(thread, core, package) in topology order
202 * skip non-present cpus
Len Brown88c32812012-03-29 21:44:40 -0400203 */
Len Brownd15cf7c2012-06-03 23:24:00 -0400204
Len Brownc98d5d92012-06-04 00:56:40 -0400205int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
206 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
Len Brown88c32812012-03-29 21:44:40 -0400207{
Len Brownc98d5d92012-06-04 00:56:40 -0400208 int retval, pkg_no, core_no, thread_no;
209
210 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
211 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
212 for (thread_no = 0; thread_no <
213 topo.num_threads_per_core; ++thread_no) {
214 struct thread_data *t;
215 struct core_data *c;
216 struct pkg_data *p;
217
218 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
219
220 if (cpu_is_not_present(t->cpu_id))
221 continue;
222
223 c = GET_CORE(core_base, core_no, pkg_no);
224 p = GET_PKG(pkg_base, pkg_no);
225
226 retval = func(t, c, p);
227 if (retval)
228 return retval;
229 }
230 }
231 }
232 return 0;
Len Brown88c32812012-03-29 21:44:40 -0400233}
234
235int cpu_migrate(int cpu)
236{
Len Brownc98d5d92012-06-04 00:56:40 -0400237 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
238 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
239 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
Len Brown88c32812012-03-29 21:44:40 -0400240 return -1;
241 else
242 return 0;
243}
244
Len Brown15aaa342012-03-29 22:19:58 -0400245int get_msr(int cpu, off_t offset, unsigned long long *msr)
Len Brown103a8fe2010-10-22 23:53:03 -0400246{
247 ssize_t retval;
Len Brown103a8fe2010-10-22 23:53:03 -0400248 char pathname[32];
249 int fd;
250
251 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
252 fd = open(pathname, O_RDONLY);
Len Brown15aaa342012-03-29 22:19:58 -0400253 if (fd < 0)
254 return -1;
Len Brown103a8fe2010-10-22 23:53:03 -0400255
Len Brown15aaa342012-03-29 22:19:58 -0400256 retval = pread(fd, msr, sizeof *msr, offset);
Len Brown103a8fe2010-10-22 23:53:03 -0400257 close(fd);
Len Brown15aaa342012-03-29 22:19:58 -0400258
Len Brownd91bb172012-11-01 00:08:19 -0400259 if (retval != sizeof *msr) {
Josh Triplett2e9c6bc2013-08-20 17:20:13 -0700260 fprintf(stderr, "%s offset 0x%llx read failed\n", pathname, (unsigned long long)offset);
Len Brown15aaa342012-03-29 22:19:58 -0400261 return -1;
Len Brownd91bb172012-11-01 00:08:19 -0400262 }
Len Brown15aaa342012-03-29 22:19:58 -0400263
264 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400265}
266
Len Browna829eb42011-02-10 23:36:34 -0500267void print_header(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400268{
269 if (show_pkg)
Len Brownc98d5d92012-06-04 00:56:40 -0400270 outp += sprintf(outp, "pk");
Len Browne23da032012-02-06 18:37:16 -0500271 if (show_pkg)
Len Brownc98d5d92012-06-04 00:56:40 -0400272 outp += sprintf(outp, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400273 if (show_core)
Len Brownc98d5d92012-06-04 00:56:40 -0400274 outp += sprintf(outp, "cor");
Len Brown103a8fe2010-10-22 23:53:03 -0400275 if (show_cpu)
Len Brownc98d5d92012-06-04 00:56:40 -0400276 outp += sprintf(outp, " CPU");
Len Browne23da032012-02-06 18:37:16 -0500277 if (show_pkg || show_core || show_cpu)
Len Brownc98d5d92012-06-04 00:56:40 -0400278 outp += sprintf(outp, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400279 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400280 outp += sprintf(outp, " %%c0");
Len Brown103a8fe2010-10-22 23:53:03 -0400281 if (has_aperf)
Len Brownc98d5d92012-06-04 00:56:40 -0400282 outp += sprintf(outp, " GHz");
283 outp += sprintf(outp, " TSC");
Len Brown1ed51012013-02-10 17:19:24 -0500284 if (do_smi)
285 outp += sprintf(outp, " SMI");
Len Brown8e180f32012-09-22 01:25:08 -0400286 if (extra_delta_offset32)
Len Brownf9240812012-10-06 15:26:31 -0400287 outp += sprintf(outp, " count 0x%03X", extra_delta_offset32);
Len Brown8e180f32012-09-22 01:25:08 -0400288 if (extra_delta_offset64)
Len Brownf9240812012-10-06 15:26:31 -0400289 outp += sprintf(outp, " COUNT 0x%03X", extra_delta_offset64);
Len Brown2f32edf2012-09-21 23:45:46 -0400290 if (extra_msr_offset32)
Len Brown8e180f32012-09-22 01:25:08 -0400291 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset32);
Len Brown2f32edf2012-09-21 23:45:46 -0400292 if (extra_msr_offset64)
Len Brown8e180f32012-09-22 01:25:08 -0400293 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset64);
Len Brown103a8fe2010-10-22 23:53:03 -0400294 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400295 outp += sprintf(outp, " %%c1");
Len Brown144b44b2013-11-09 00:30:16 -0500296 if (do_nhm_cstates && !do_slm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400297 outp += sprintf(outp, " %%c3");
Len Brown103a8fe2010-10-22 23:53:03 -0400298 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400299 outp += sprintf(outp, " %%c6");
Len Brown103a8fe2010-10-22 23:53:03 -0400300 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400301 outp += sprintf(outp, " %%c7");
Len Brown889facb2012-11-08 00:48:57 -0500302
303 if (do_dts)
304 outp += sprintf(outp, " CTMP");
305 if (do_ptm)
306 outp += sprintf(outp, " PTMP");
307
Len Brown103a8fe2010-10-22 23:53:03 -0400308 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400309 outp += sprintf(outp, " %%pc2");
Len Brown144b44b2013-11-09 00:30:16 -0500310 if (do_nhm_cstates && !do_slm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400311 outp += sprintf(outp, " %%pc3");
Len Brown144b44b2013-11-09 00:30:16 -0500312 if (do_nhm_cstates && !do_slm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400313 outp += sprintf(outp, " %%pc6");
Len Brown103a8fe2010-10-22 23:53:03 -0400314 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400315 outp += sprintf(outp, " %%pc7");
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800316 if (do_c8_c9_c10) {
317 outp += sprintf(outp, " %%pc8");
318 outp += sprintf(outp, " %%pc9");
319 outp += sprintf(outp, " %%pc10");
320 }
Len Brown103a8fe2010-10-22 23:53:03 -0400321
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800322 if (do_rapl && !rapl_joules) {
323 if (do_rapl & RAPL_PKG)
324 outp += sprintf(outp, " Pkg_W");
325 if (do_rapl & RAPL_CORES)
326 outp += sprintf(outp, " Cor_W");
327 if (do_rapl & RAPL_GFX)
328 outp += sprintf(outp, " GFX_W");
329 if (do_rapl & RAPL_DRAM)
330 outp += sprintf(outp, " RAM_W");
331 if (do_rapl & RAPL_PKG_PERF_STATUS)
332 outp += sprintf(outp, " PKG_%%");
333 if (do_rapl & RAPL_DRAM_PERF_STATUS)
334 outp += sprintf(outp, " RAM_%%");
335 } else {
336 if (do_rapl & RAPL_PKG)
337 outp += sprintf(outp, " Pkg_J");
338 if (do_rapl & RAPL_CORES)
339 outp += sprintf(outp, " Cor_J");
340 if (do_rapl & RAPL_GFX)
341 outp += sprintf(outp, " GFX_J");
342 if (do_rapl & RAPL_DRAM)
343 outp += sprintf(outp, " RAM_W");
344 if (do_rapl & RAPL_PKG_PERF_STATUS)
345 outp += sprintf(outp, " PKG_%%");
346 if (do_rapl & RAPL_DRAM_PERF_STATUS)
347 outp += sprintf(outp, " RAM_%%");
348 outp += sprintf(outp, " time");
Len Brown889facb2012-11-08 00:48:57 -0500349
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800350 }
Len Brownc98d5d92012-06-04 00:56:40 -0400351 outp += sprintf(outp, "\n");
Len Brown103a8fe2010-10-22 23:53:03 -0400352}
353
Len Brownc98d5d92012-06-04 00:56:40 -0400354int dump_counters(struct thread_data *t, struct core_data *c,
355 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400356{
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200357 outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400358
Len Brownc98d5d92012-06-04 00:56:40 -0400359 if (t) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200360 outp += sprintf(outp, "CPU: %d flags 0x%x\n",
361 t->cpu_id, t->flags);
362 outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
363 outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
364 outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
365 outp += sprintf(outp, "c1: %016llX\n", t->c1);
366 outp += sprintf(outp, "msr0x%x: %08llX\n",
Len Brown8e180f32012-09-22 01:25:08 -0400367 extra_delta_offset32, t->extra_delta32);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200368 outp += sprintf(outp, "msr0x%x: %016llX\n",
Len Brown8e180f32012-09-22 01:25:08 -0400369 extra_delta_offset64, t->extra_delta64);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200370 outp += sprintf(outp, "msr0x%x: %08llX\n",
Len Brown2f32edf2012-09-21 23:45:46 -0400371 extra_msr_offset32, t->extra_msr32);
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200372 outp += sprintf(outp, "msr0x%x: %016llX\n",
Len Brown2f32edf2012-09-21 23:45:46 -0400373 extra_msr_offset64, t->extra_msr64);
Len Brown1ed51012013-02-10 17:19:24 -0500374 if (do_smi)
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200375 outp += sprintf(outp, "SMI: %08X\n", t->smi_count);
Len Brownc98d5d92012-06-04 00:56:40 -0400376 }
Len Brown103a8fe2010-10-22 23:53:03 -0400377
Len Brownc98d5d92012-06-04 00:56:40 -0400378 if (c) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200379 outp += sprintf(outp, "core: %d\n", c->core_id);
380 outp += sprintf(outp, "c3: %016llX\n", c->c3);
381 outp += sprintf(outp, "c6: %016llX\n", c->c6);
382 outp += sprintf(outp, "c7: %016llX\n", c->c7);
383 outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
Len Brownc98d5d92012-06-04 00:56:40 -0400384 }
385
386 if (p) {
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200387 outp += sprintf(outp, "package: %d\n", p->package_id);
388 outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
389 outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
390 outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
391 outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
392 outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
393 outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
394 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
395 outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
396 outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
397 outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
398 outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
399 outp += sprintf(outp, "Throttle PKG: %0X\n",
400 p->rapl_pkg_perf_status);
401 outp += sprintf(outp, "Throttle RAM: %0X\n",
402 p->rapl_dram_perf_status);
403 outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
Len Brownc98d5d92012-06-04 00:56:40 -0400404 }
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +0200405
406 outp += sprintf(outp, "\n");
407
Len Brownc98d5d92012-06-04 00:56:40 -0400408 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400409}
410
Len Browne23da032012-02-06 18:37:16 -0500411/*
412 * column formatting convention & formats
413 * package: "pk" 2 columns %2d
414 * core: "cor" 3 columns %3d
415 * CPU: "CPU" 3 columns %3d
Len Brown889facb2012-11-08 00:48:57 -0500416 * Pkg_W: %6.2
417 * Cor_W: %6.2
418 * GFX_W: %5.2
419 * RAM_W: %5.2
Len Browne23da032012-02-06 18:37:16 -0500420 * GHz: "GHz" 3 columns %3.2
421 * TSC: "TSC" 3 columns %3.2
Len Brown1ed51012013-02-10 17:19:24 -0500422 * SMI: "SMI" 4 columns %4d
Len Browne23da032012-02-06 18:37:16 -0500423 * percentage " %pc3" %6.2
Len Brown889facb2012-11-08 00:48:57 -0500424 * Perf Status percentage: %5.2
425 * "CTMP" 4 columns %4d
Len Browne23da032012-02-06 18:37:16 -0500426 */
Len Brownc98d5d92012-06-04 00:56:40 -0400427int format_counters(struct thread_data *t, struct core_data *c,
428 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400429{
430 double interval_float;
Len Brown889facb2012-11-08 00:48:57 -0500431 char *fmt5, *fmt6;
Len Brown103a8fe2010-10-22 23:53:03 -0400432
Len Brownc98d5d92012-06-04 00:56:40 -0400433 /* if showing only 1st thread in core and this isn't one, bail out */
434 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
435 return 0;
436
437 /* if showing only 1st thread in pkg and this isn't one, bail out */
438 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
439 return 0;
440
Len Brown103a8fe2010-10-22 23:53:03 -0400441 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
442
Len Brownc98d5d92012-06-04 00:56:40 -0400443 /* topo columns, print blanks on 1st (average) line */
444 if (t == &average.threads) {
Len Brown103a8fe2010-10-22 23:53:03 -0400445 if (show_pkg)
Len Brownc98d5d92012-06-04 00:56:40 -0400446 outp += sprintf(outp, " ");
Len Browne23da032012-02-06 18:37:16 -0500447 if (show_pkg && show_core)
Len Brownc98d5d92012-06-04 00:56:40 -0400448 outp += sprintf(outp, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400449 if (show_core)
Len Brownc98d5d92012-06-04 00:56:40 -0400450 outp += sprintf(outp, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400451 if (show_cpu)
Len Brownc98d5d92012-06-04 00:56:40 -0400452 outp += sprintf(outp, " " " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400453 } else {
Len Brownc98d5d92012-06-04 00:56:40 -0400454 if (show_pkg) {
455 if (p)
456 outp += sprintf(outp, "%2d", p->package_id);
457 else
458 outp += sprintf(outp, " ");
459 }
Len Browne23da032012-02-06 18:37:16 -0500460 if (show_pkg && show_core)
Len Brownc98d5d92012-06-04 00:56:40 -0400461 outp += sprintf(outp, " ");
462 if (show_core) {
463 if (c)
464 outp += sprintf(outp, "%3d", c->core_id);
465 else
466 outp += sprintf(outp, " ");
467 }
Len Brown103a8fe2010-10-22 23:53:03 -0400468 if (show_cpu)
Len Brownc98d5d92012-06-04 00:56:40 -0400469 outp += sprintf(outp, " %3d", t->cpu_id);
Len Brown103a8fe2010-10-22 23:53:03 -0400470 }
Len Brown103a8fe2010-10-22 23:53:03 -0400471 /* %c0 */
472 if (do_nhm_cstates) {
Len Browne23da032012-02-06 18:37:16 -0500473 if (show_pkg || show_core || show_cpu)
Len Brownc98d5d92012-06-04 00:56:40 -0400474 outp += sprintf(outp, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400475 if (!skip_c0)
Len Brownc98d5d92012-06-04 00:56:40 -0400476 outp += sprintf(outp, "%6.2f", 100.0 * t->mperf/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400477 else
Len Brownc98d5d92012-06-04 00:56:40 -0400478 outp += sprintf(outp, " ****");
Len Brown103a8fe2010-10-22 23:53:03 -0400479 }
480
481 /* GHz */
482 if (has_aperf) {
483 if (!aperf_mperf_unstable) {
Len Brownc98d5d92012-06-04 00:56:40 -0400484 outp += sprintf(outp, " %3.2f",
485 1.0 * t->tsc / units * t->aperf /
486 t->mperf / interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400487 } else {
Len Brownc98d5d92012-06-04 00:56:40 -0400488 if (t->aperf > t->tsc || t->mperf > t->tsc) {
489 outp += sprintf(outp, " ***");
Len Brown103a8fe2010-10-22 23:53:03 -0400490 } else {
Len Brownc98d5d92012-06-04 00:56:40 -0400491 outp += sprintf(outp, "%3.1f*",
492 1.0 * t->tsc /
493 units * t->aperf /
494 t->mperf / interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400495 }
496 }
497 }
498
499 /* TSC */
Len Brownc98d5d92012-06-04 00:56:40 -0400500 outp += sprintf(outp, "%5.2f", 1.0 * t->tsc/units/interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400501
Len Brown1ed51012013-02-10 17:19:24 -0500502 /* SMI */
503 if (do_smi)
504 outp += sprintf(outp, "%4d", t->smi_count);
505
Len Brown8e180f32012-09-22 01:25:08 -0400506 /* delta */
507 if (extra_delta_offset32)
508 outp += sprintf(outp, " %11llu", t->extra_delta32);
509
510 /* DELTA */
511 if (extra_delta_offset64)
512 outp += sprintf(outp, " %11llu", t->extra_delta64);
Len Brown2f32edf2012-09-21 23:45:46 -0400513 /* msr */
514 if (extra_msr_offset32)
Len Brown8e180f32012-09-22 01:25:08 -0400515 outp += sprintf(outp, " 0x%08llx", t->extra_msr32);
Len Brown2f32edf2012-09-21 23:45:46 -0400516
Len Brown130ff302012-09-21 22:56:06 -0400517 /* MSR */
Len Brown2f32edf2012-09-21 23:45:46 -0400518 if (extra_msr_offset64)
519 outp += sprintf(outp, " 0x%016llx", t->extra_msr64);
Len Brown130ff302012-09-21 22:56:06 -0400520
Len Brown103a8fe2010-10-22 23:53:03 -0400521 if (do_nhm_cstates) {
522 if (!skip_c1)
Len Brownc98d5d92012-06-04 00:56:40 -0400523 outp += sprintf(outp, " %6.2f", 100.0 * t->c1/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400524 else
Len Brownc98d5d92012-06-04 00:56:40 -0400525 outp += sprintf(outp, " ****");
Len Brown103a8fe2010-10-22 23:53:03 -0400526 }
Len Brownc98d5d92012-06-04 00:56:40 -0400527
528 /* print per-core data only for 1st thread in core */
529 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
530 goto done;
531
Len Brown144b44b2013-11-09 00:30:16 -0500532 if (do_nhm_cstates && !do_slm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400533 outp += sprintf(outp, " %6.2f", 100.0 * c->c3/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400534 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400535 outp += sprintf(outp, " %6.2f", 100.0 * c->c6/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400536 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400537 outp += sprintf(outp, " %6.2f", 100.0 * c->c7/t->tsc);
538
Len Brown889facb2012-11-08 00:48:57 -0500539 if (do_dts)
540 outp += sprintf(outp, " %4d", c->core_temp_c);
541
Len Brownc98d5d92012-06-04 00:56:40 -0400542 /* print per-package data only for 1st core in package */
543 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
544 goto done;
545
Len Brown889facb2012-11-08 00:48:57 -0500546 if (do_ptm)
547 outp += sprintf(outp, " %4d", p->pkg_temp_c);
548
Len Brown103a8fe2010-10-22 23:53:03 -0400549 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400550 outp += sprintf(outp, " %6.2f", 100.0 * p->pc2/t->tsc);
Len Brown144b44b2013-11-09 00:30:16 -0500551 if (do_nhm_cstates && !do_slm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400552 outp += sprintf(outp, " %6.2f", 100.0 * p->pc3/t->tsc);
Len Brown144b44b2013-11-09 00:30:16 -0500553 if (do_nhm_cstates && !do_slm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400554 outp += sprintf(outp, " %6.2f", 100.0 * p->pc6/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400555 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400556 outp += sprintf(outp, " %6.2f", 100.0 * p->pc7/t->tsc);
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800557 if (do_c8_c9_c10) {
558 outp += sprintf(outp, " %6.2f", 100.0 * p->pc8/t->tsc);
559 outp += sprintf(outp, " %6.2f", 100.0 * p->pc9/t->tsc);
560 outp += sprintf(outp, " %6.2f", 100.0 * p->pc10/t->tsc);
561 }
Len Brown889facb2012-11-08 00:48:57 -0500562
563 /*
564 * If measurement interval exceeds minimum RAPL Joule Counter range,
565 * indicate that results are suspect by printing "**" in fraction place.
566 */
567 if (interval_float < rapl_joule_counter_range) {
568 fmt5 = " %5.2f";
569 fmt6 = " %6.2f";
570 } else {
571 fmt5 = " %3.0f**";
572 fmt6 = " %4.0f**";
573 }
574
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800575 if (do_rapl && !rapl_joules) {
576 if (do_rapl & RAPL_PKG)
577 outp += sprintf(outp, fmt6, p->energy_pkg * rapl_energy_units / interval_float);
578 if (do_rapl & RAPL_CORES)
579 outp += sprintf(outp, fmt6, p->energy_cores * rapl_energy_units / interval_float);
580 if (do_rapl & RAPL_GFX)
581 outp += sprintf(outp, fmt5, p->energy_gfx * rapl_energy_units / interval_float);
582 if (do_rapl & RAPL_DRAM)
583 outp += sprintf(outp, fmt5, p->energy_dram * rapl_energy_units / interval_float);
584 if (do_rapl & RAPL_PKG_PERF_STATUS)
585 outp += sprintf(outp, fmt5, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
586 if (do_rapl & RAPL_DRAM_PERF_STATUS)
587 outp += sprintf(outp, fmt5, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
588 } else {
589 if (do_rapl & RAPL_PKG)
590 outp += sprintf(outp, fmt6,
591 p->energy_pkg * rapl_energy_units);
592 if (do_rapl & RAPL_CORES)
593 outp += sprintf(outp, fmt6,
594 p->energy_cores * rapl_energy_units);
595 if (do_rapl & RAPL_GFX)
596 outp += sprintf(outp, fmt5,
597 p->energy_gfx * rapl_energy_units);
598 if (do_rapl & RAPL_DRAM)
599 outp += sprintf(outp, fmt5,
600 p->energy_dram * rapl_energy_units);
601 if (do_rapl & RAPL_PKG_PERF_STATUS)
602 outp += sprintf(outp, fmt5, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
603 if (do_rapl & RAPL_DRAM_PERF_STATUS)
604 outp += sprintf(outp, fmt5, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
605 outp += sprintf(outp, fmt5, interval_float);
Len Brown889facb2012-11-08 00:48:57 -0500606
Dirk Brandewie5c56be92013-12-16 10:23:41 -0800607 }
Len Brownc98d5d92012-06-04 00:56:40 -0400608done:
Len Brownc98d5d92012-06-04 00:56:40 -0400609 outp += sprintf(outp, "\n");
610
611 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400612}
613
Len Brownc98d5d92012-06-04 00:56:40 -0400614void flush_stdout()
Len Brown103a8fe2010-10-22 23:53:03 -0400615{
Len Brownc98d5d92012-06-04 00:56:40 -0400616 fputs(output_buffer, stdout);
Len Brownddac0d62012-11-30 01:01:40 -0500617 fflush(stdout);
Len Brownc98d5d92012-06-04 00:56:40 -0400618 outp = output_buffer;
619}
620void flush_stderr()
621{
622 fputs(output_buffer, stderr);
623 outp = output_buffer;
624}
625void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
626{
Len Browne23da032012-02-06 18:37:16 -0500627 static int printed;
Len Brown103a8fe2010-10-22 23:53:03 -0400628
Len Browne23da032012-02-06 18:37:16 -0500629 if (!printed || !summary_only)
630 print_header();
Len Brown103a8fe2010-10-22 23:53:03 -0400631
Len Brownc98d5d92012-06-04 00:56:40 -0400632 if (topo.num_cpus > 1)
633 format_counters(&average.threads, &average.cores,
634 &average.packages);
Len Brown103a8fe2010-10-22 23:53:03 -0400635
Len Browne23da032012-02-06 18:37:16 -0500636 printed = 1;
637
638 if (summary_only)
639 return;
640
Len Brownc98d5d92012-06-04 00:56:40 -0400641 for_all_cpus(format_counters, t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400642}
643
Len Brown889facb2012-11-08 00:48:57 -0500644#define DELTA_WRAP32(new, old) \
645 if (new > old) { \
646 old = new - old; \
647 } else { \
648 old = 0x100000000 + new - old; \
649 }
650
Len Brownc98d5d92012-06-04 00:56:40 -0400651void
652delta_package(struct pkg_data *new, struct pkg_data *old)
Len Brown103a8fe2010-10-22 23:53:03 -0400653{
Len Brownc98d5d92012-06-04 00:56:40 -0400654 old->pc2 = new->pc2 - old->pc2;
655 old->pc3 = new->pc3 - old->pc3;
656 old->pc6 = new->pc6 - old->pc6;
657 old->pc7 = new->pc7 - old->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800658 old->pc8 = new->pc8 - old->pc8;
659 old->pc9 = new->pc9 - old->pc9;
660 old->pc10 = new->pc10 - old->pc10;
Len Brown889facb2012-11-08 00:48:57 -0500661 old->pkg_temp_c = new->pkg_temp_c;
662
663 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
664 DELTA_WRAP32(new->energy_cores, old->energy_cores);
665 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
666 DELTA_WRAP32(new->energy_dram, old->energy_dram);
667 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
668 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
Len Brownc98d5d92012-06-04 00:56:40 -0400669}
Len Brown103a8fe2010-10-22 23:53:03 -0400670
Len Brownc98d5d92012-06-04 00:56:40 -0400671void
672delta_core(struct core_data *new, struct core_data *old)
673{
674 old->c3 = new->c3 - old->c3;
675 old->c6 = new->c6 - old->c6;
676 old->c7 = new->c7 - old->c7;
Len Brown889facb2012-11-08 00:48:57 -0500677 old->core_temp_c = new->core_temp_c;
Len Brownc98d5d92012-06-04 00:56:40 -0400678}
Len Brown103a8fe2010-10-22 23:53:03 -0400679
Len Brownc3ae3312012-06-13 21:31:46 -0400680/*
681 * old = new - old
682 */
Len Brownc98d5d92012-06-04 00:56:40 -0400683void
684delta_thread(struct thread_data *new, struct thread_data *old,
685 struct core_data *core_delta)
686{
687 old->tsc = new->tsc - old->tsc;
Len Brown103a8fe2010-10-22 23:53:03 -0400688
Len Brownc98d5d92012-06-04 00:56:40 -0400689 /* check for TSC < 1 Mcycles over interval */
Josh Triplettb2c95d92013-08-20 17:20:18 -0700690 if (old->tsc < (1000 * 1000))
691 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
692 "You can disable all c-states by booting with \"idle=poll\"\n"
693 "or just the deep ones with \"processor.max_cstate=1\"");
Len Brown103a8fe2010-10-22 23:53:03 -0400694
Len Brownc98d5d92012-06-04 00:56:40 -0400695 old->c1 = new->c1 - old->c1;
Len Brown103a8fe2010-10-22 23:53:03 -0400696
Len Brownc98d5d92012-06-04 00:56:40 -0400697 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
698 old->aperf = new->aperf - old->aperf;
699 old->mperf = new->mperf - old->mperf;
700 } else {
Len Brown103a8fe2010-10-22 23:53:03 -0400701
Len Brownc98d5d92012-06-04 00:56:40 -0400702 if (!aperf_mperf_unstable) {
703 fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname);
704 fprintf(stderr, "* Frequency results do not cover entire interval *\n");
705 fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n");
706
707 aperf_mperf_unstable = 1;
708 }
Len Brown103a8fe2010-10-22 23:53:03 -0400709 /*
Len Brownc98d5d92012-06-04 00:56:40 -0400710 * mperf delta is likely a huge "positive" number
711 * can not use it for calculating c0 time
Len Brown103a8fe2010-10-22 23:53:03 -0400712 */
Len Brownc98d5d92012-06-04 00:56:40 -0400713 skip_c0 = 1;
714 skip_c1 = 1;
715 }
Len Brown103a8fe2010-10-22 23:53:03 -0400716
Len Brown103a8fe2010-10-22 23:53:03 -0400717
Len Brown144b44b2013-11-09 00:30:16 -0500718 if (use_c1_residency_msr) {
719 /*
720 * Some models have a dedicated C1 residency MSR,
721 * which should be more accurate than the derivation below.
722 */
723 } else {
724 /*
725 * As counter collection is not atomic,
726 * it is possible for mperf's non-halted cycles + idle states
727 * to exceed TSC's all cycles: show c1 = 0% in that case.
728 */
729 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
730 old->c1 = 0;
731 else {
732 /* normal case, derive c1 */
733 old->c1 = old->tsc - old->mperf - core_delta->c3
Len Brownc98d5d92012-06-04 00:56:40 -0400734 - core_delta->c6 - core_delta->c7;
Len Brown144b44b2013-11-09 00:30:16 -0500735 }
Len Brownc98d5d92012-06-04 00:56:40 -0400736 }
Len Brownc3ae3312012-06-13 21:31:46 -0400737
Len Brownc98d5d92012-06-04 00:56:40 -0400738 if (old->mperf == 0) {
Len Brownc3ae3312012-06-13 21:31:46 -0400739 if (verbose > 1) fprintf(stderr, "cpu%d MPERF 0!\n", old->cpu_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400740 old->mperf = 1; /* divide by 0 protection */
741 }
742
Len Brown8e180f32012-09-22 01:25:08 -0400743 old->extra_delta32 = new->extra_delta32 - old->extra_delta32;
744 old->extra_delta32 &= 0xFFFFFFFF;
745
746 old->extra_delta64 = new->extra_delta64 - old->extra_delta64;
747
Len Brownc98d5d92012-06-04 00:56:40 -0400748 /*
Len Brown8e180f32012-09-22 01:25:08 -0400749 * Extra MSR is just a snapshot, simply copy latest w/o subtracting
Len Brownc98d5d92012-06-04 00:56:40 -0400750 */
Len Brown2f32edf2012-09-21 23:45:46 -0400751 old->extra_msr32 = new->extra_msr32;
752 old->extra_msr64 = new->extra_msr64;
Len Brown1ed51012013-02-10 17:19:24 -0500753
754 if (do_smi)
755 old->smi_count = new->smi_count - old->smi_count;
Len Brownc98d5d92012-06-04 00:56:40 -0400756}
757
758int delta_cpu(struct thread_data *t, struct core_data *c,
759 struct pkg_data *p, struct thread_data *t2,
760 struct core_data *c2, struct pkg_data *p2)
761{
762 /* calculate core delta only for 1st thread in core */
763 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
764 delta_core(c, c2);
765
766 /* always calculate thread delta */
767 delta_thread(t, t2, c2); /* c2 is core delta */
768
769 /* calculate package delta only for 1st core in package */
770 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
771 delta_package(p, p2);
772
773 return 0;
774}
775
776void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
777{
778 t->tsc = 0;
779 t->aperf = 0;
780 t->mperf = 0;
781 t->c1 = 0;
782
Len Brown1ed51012013-02-10 17:19:24 -0500783 t->smi_count = 0;
Len Brown8e180f32012-09-22 01:25:08 -0400784 t->extra_delta32 = 0;
785 t->extra_delta64 = 0;
786
Len Brownc98d5d92012-06-04 00:56:40 -0400787 /* tells format_counters to dump all fields from this set */
788 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
789
790 c->c3 = 0;
791 c->c6 = 0;
792 c->c7 = 0;
Len Brown889facb2012-11-08 00:48:57 -0500793 c->core_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -0400794
795 p->pc2 = 0;
796 p->pc3 = 0;
797 p->pc6 = 0;
798 p->pc7 = 0;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800799 p->pc8 = 0;
800 p->pc9 = 0;
801 p->pc10 = 0;
Len Brown889facb2012-11-08 00:48:57 -0500802
803 p->energy_pkg = 0;
804 p->energy_dram = 0;
805 p->energy_cores = 0;
806 p->energy_gfx = 0;
807 p->rapl_pkg_perf_status = 0;
808 p->rapl_dram_perf_status = 0;
809 p->pkg_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -0400810}
811int sum_counters(struct thread_data *t, struct core_data *c,
812 struct pkg_data *p)
813{
814 average.threads.tsc += t->tsc;
815 average.threads.aperf += t->aperf;
816 average.threads.mperf += t->mperf;
817 average.threads.c1 += t->c1;
818
Len Brown8e180f32012-09-22 01:25:08 -0400819 average.threads.extra_delta32 += t->extra_delta32;
820 average.threads.extra_delta64 += t->extra_delta64;
821
Len Brownc98d5d92012-06-04 00:56:40 -0400822 /* sum per-core values only for 1st thread in core */
823 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
824 return 0;
825
826 average.cores.c3 += c->c3;
827 average.cores.c6 += c->c6;
828 average.cores.c7 += c->c7;
829
Len Brown889facb2012-11-08 00:48:57 -0500830 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
831
Len Brownc98d5d92012-06-04 00:56:40 -0400832 /* sum per-pkg values only for 1st core in pkg */
833 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
834 return 0;
835
836 average.packages.pc2 += p->pc2;
837 average.packages.pc3 += p->pc3;
838 average.packages.pc6 += p->pc6;
839 average.packages.pc7 += p->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800840 average.packages.pc8 += p->pc8;
841 average.packages.pc9 += p->pc9;
842 average.packages.pc10 += p->pc10;
Len Brownc98d5d92012-06-04 00:56:40 -0400843
Len Brown889facb2012-11-08 00:48:57 -0500844 average.packages.energy_pkg += p->energy_pkg;
845 average.packages.energy_dram += p->energy_dram;
846 average.packages.energy_cores += p->energy_cores;
847 average.packages.energy_gfx += p->energy_gfx;
848
849 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
850
851 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
852 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
Len Brownc98d5d92012-06-04 00:56:40 -0400853 return 0;
854}
855/*
856 * sum the counters for all cpus in the system
857 * compute the weighted average
858 */
859void compute_average(struct thread_data *t, struct core_data *c,
860 struct pkg_data *p)
861{
862 clear_counters(&average.threads, &average.cores, &average.packages);
863
864 for_all_cpus(sum_counters, t, c, p);
865
866 average.threads.tsc /= topo.num_cpus;
867 average.threads.aperf /= topo.num_cpus;
868 average.threads.mperf /= topo.num_cpus;
869 average.threads.c1 /= topo.num_cpus;
870
Len Brown8e180f32012-09-22 01:25:08 -0400871 average.threads.extra_delta32 /= topo.num_cpus;
872 average.threads.extra_delta32 &= 0xFFFFFFFF;
873
874 average.threads.extra_delta64 /= topo.num_cpus;
875
Len Brownc98d5d92012-06-04 00:56:40 -0400876 average.cores.c3 /= topo.num_cores;
877 average.cores.c6 /= topo.num_cores;
878 average.cores.c7 /= topo.num_cores;
879
880 average.packages.pc2 /= topo.num_packages;
881 average.packages.pc3 /= topo.num_packages;
882 average.packages.pc6 /= topo.num_packages;
883 average.packages.pc7 /= topo.num_packages;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800884
885 average.packages.pc8 /= topo.num_packages;
886 average.packages.pc9 /= topo.num_packages;
887 average.packages.pc10 /= topo.num_packages;
Len Brownc98d5d92012-06-04 00:56:40 -0400888}
889
890static unsigned long long rdtsc(void)
891{
892 unsigned int low, high;
893
894 asm volatile("rdtsc" : "=a" (low), "=d" (high));
895
896 return low | ((unsigned long long)high) << 32;
897}
898
899
900/*
901 * get_counters(...)
902 * migrate to cpu
903 * acquire and record local counters for that cpu
904 */
905int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
906{
907 int cpu = t->cpu_id;
Len Brown889facb2012-11-08 00:48:57 -0500908 unsigned long long msr;
Len Brownc98d5d92012-06-04 00:56:40 -0400909
Len Browne52966c2012-11-08 22:38:05 -0500910 if (cpu_migrate(cpu)) {
911 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
Len Brownc98d5d92012-06-04 00:56:40 -0400912 return -1;
Len Browne52966c2012-11-08 22:38:05 -0500913 }
Len Brownc98d5d92012-06-04 00:56:40 -0400914
915 t->tsc = rdtsc(); /* we are running on local CPU of interest */
916
917 if (has_aperf) {
Len Brown9c63a652012-10-31 01:29:52 -0400918 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
Len Brownc98d5d92012-06-04 00:56:40 -0400919 return -3;
Len Brown9c63a652012-10-31 01:29:52 -0400920 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
Len Brownc98d5d92012-06-04 00:56:40 -0400921 return -4;
922 }
923
Len Brown1ed51012013-02-10 17:19:24 -0500924 if (do_smi) {
925 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
926 return -5;
927 t->smi_count = msr & 0xFFFFFFFF;
928 }
Len Brown8e180f32012-09-22 01:25:08 -0400929 if (extra_delta_offset32) {
Len Brown889facb2012-11-08 00:48:57 -0500930 if (get_msr(cpu, extra_delta_offset32, &msr))
Len Brown2f32edf2012-09-21 23:45:46 -0400931 return -5;
Len Brown889facb2012-11-08 00:48:57 -0500932 t->extra_delta32 = msr & 0xFFFFFFFF;
Len Brown8e180f32012-09-22 01:25:08 -0400933 }
934
935 if (extra_delta_offset64)
936 if (get_msr(cpu, extra_delta_offset64, &t->extra_delta64))
937 return -5;
938
939 if (extra_msr_offset32) {
Len Brown889facb2012-11-08 00:48:57 -0500940 if (get_msr(cpu, extra_msr_offset32, &msr))
Len Brown8e180f32012-09-22 01:25:08 -0400941 return -5;
Len Brown889facb2012-11-08 00:48:57 -0500942 t->extra_msr32 = msr & 0xFFFFFFFF;
Len Brown8e180f32012-09-22 01:25:08 -0400943 }
Len Brown2f32edf2012-09-21 23:45:46 -0400944
945 if (extra_msr_offset64)
946 if (get_msr(cpu, extra_msr_offset64, &t->extra_msr64))
Len Brownc98d5d92012-06-04 00:56:40 -0400947 return -5;
948
Len Brown144b44b2013-11-09 00:30:16 -0500949 if (use_c1_residency_msr) {
950 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
951 return -6;
952 }
953
Len Brownc98d5d92012-06-04 00:56:40 -0400954 /* collect core counters only for 1st thread in core */
955 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
956 return 0;
957
Len Brown144b44b2013-11-09 00:30:16 -0500958 if (do_nhm_cstates && !do_slm_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -0400959 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
960 return -6;
Len Brown144b44b2013-11-09 00:30:16 -0500961 }
962
963 if (do_nhm_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -0400964 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
965 return -7;
966 }
967
968 if (do_snb_cstates)
969 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
970 return -8;
971
Len Brown889facb2012-11-08 00:48:57 -0500972 if (do_dts) {
973 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
974 return -9;
975 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
976 }
977
978
Len Brownc98d5d92012-06-04 00:56:40 -0400979 /* collect package counters only for 1st core in package */
980 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
981 return 0;
982
Len Brown144b44b2013-11-09 00:30:16 -0500983 if (do_nhm_cstates && !do_slm_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -0400984 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
985 return -9;
986 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
987 return -10;
988 }
989 if (do_snb_cstates) {
990 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
991 return -11;
992 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
993 return -12;
Len Brown103a8fe2010-10-22 23:53:03 -0400994 }
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800995 if (do_c8_c9_c10) {
996 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
997 return -13;
998 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
999 return -13;
1000 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1001 return -13;
1002 }
Len Brown889facb2012-11-08 00:48:57 -05001003 if (do_rapl & RAPL_PKG) {
1004 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1005 return -13;
1006 p->energy_pkg = msr & 0xFFFFFFFF;
1007 }
1008 if (do_rapl & RAPL_CORES) {
1009 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1010 return -14;
1011 p->energy_cores = msr & 0xFFFFFFFF;
1012 }
1013 if (do_rapl & RAPL_DRAM) {
1014 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1015 return -15;
1016 p->energy_dram = msr & 0xFFFFFFFF;
1017 }
1018 if (do_rapl & RAPL_GFX) {
1019 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1020 return -16;
1021 p->energy_gfx = msr & 0xFFFFFFFF;
1022 }
1023 if (do_rapl & RAPL_PKG_PERF_STATUS) {
1024 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1025 return -16;
1026 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1027 }
1028 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1029 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1030 return -16;
1031 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1032 }
1033 if (do_ptm) {
1034 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1035 return -17;
1036 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1037 }
Len Brown103a8fe2010-10-22 23:53:03 -04001038 return 0;
1039}
1040
Len Brownc98d5d92012-06-04 00:56:40 -04001041void print_verbose_header(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001042{
1043 unsigned long long msr;
1044 unsigned int ratio;
1045
1046 if (!do_nehalem_platform_info)
1047 return;
1048
Len Brown9c63a652012-10-31 01:29:52 -04001049 get_msr(0, MSR_NHM_PLATFORM_INFO, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001050
Len Brown67920412013-01-31 15:22:15 -05001051 fprintf(stderr, "cpu0: MSR_NHM_PLATFORM_INFO: 0x%08llx\n", msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001052
Len Brown103a8fe2010-10-22 23:53:03 -04001053 ratio = (msr >> 40) & 0xFF;
1054 fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency\n",
1055 ratio, bclk, ratio * bclk);
1056
1057 ratio = (msr >> 8) & 0xFF;
1058 fprintf(stderr, "%d * %.0f = %.0f MHz TSC frequency\n",
1059 ratio, bclk, ratio * bclk);
1060
Len Brown67920412013-01-31 15:22:15 -05001061 get_msr(0, MSR_IA32_POWER_CTL, &msr);
Len Brown144b44b2013-11-09 00:30:16 -05001062 fprintf(stderr, "cpu0: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
Len Brown67920412013-01-31 15:22:15 -05001063 msr, msr & 0x2 ? "EN" : "DIS");
1064
Len Brown6574a5d2012-09-21 00:01:31 -04001065 if (!do_ivt_turbo_ratio_limit)
1066 goto print_nhm_turbo_ratio_limits;
1067
1068 get_msr(0, MSR_IVT_TURBO_RATIO_LIMIT, &msr);
1069
Len Brown67920412013-01-31 15:22:15 -05001070 fprintf(stderr, "cpu0: MSR_IVT_TURBO_RATIO_LIMIT: 0x%08llx\n", msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001071
1072 ratio = (msr >> 56) & 0xFF;
1073 if (ratio)
1074 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 16 active cores\n",
1075 ratio, bclk, ratio * bclk);
1076
1077 ratio = (msr >> 48) & 0xFF;
1078 if (ratio)
1079 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 15 active cores\n",
1080 ratio, bclk, ratio * bclk);
1081
1082 ratio = (msr >> 40) & 0xFF;
1083 if (ratio)
1084 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 14 active cores\n",
1085 ratio, bclk, ratio * bclk);
1086
1087 ratio = (msr >> 32) & 0xFF;
1088 if (ratio)
1089 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 13 active cores\n",
1090 ratio, bclk, ratio * bclk);
1091
1092 ratio = (msr >> 24) & 0xFF;
1093 if (ratio)
1094 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 12 active cores\n",
1095 ratio, bclk, ratio * bclk);
1096
1097 ratio = (msr >> 16) & 0xFF;
1098 if (ratio)
1099 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 11 active cores\n",
1100 ratio, bclk, ratio * bclk);
1101
1102 ratio = (msr >> 8) & 0xFF;
1103 if (ratio)
1104 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 10 active cores\n",
1105 ratio, bclk, ratio * bclk);
1106
1107 ratio = (msr >> 0) & 0xFF;
1108 if (ratio)
1109 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 9 active cores\n",
1110 ratio, bclk, ratio * bclk);
1111
1112print_nhm_turbo_ratio_limits:
Len Brown889facb2012-11-08 00:48:57 -05001113 get_msr(0, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
1114
1115#define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
1116#define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
1117
1118 fprintf(stderr, "cpu0: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", msr);
1119
1120 fprintf(stderr, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: ",
1121 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
1122 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
1123 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
1124 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
1125 (msr & (1 << 15)) ? "" : "UN",
1126 (unsigned int)msr & 7);
1127
1128
1129 switch(msr & 0x7) {
1130 case 0:
Len Brown144b44b2013-11-09 00:30:16 -05001131 fprintf(stderr, do_slm_cstates ? "no pkg states" : "pc0");
Len Brown889facb2012-11-08 00:48:57 -05001132 break;
1133 case 1:
Len Brown144b44b2013-11-09 00:30:16 -05001134 fprintf(stderr, do_slm_cstates ? "no pkg states" : do_snb_cstates ? "pc2" : "pc0");
Len Brown889facb2012-11-08 00:48:57 -05001135 break;
1136 case 2:
Len Brown144b44b2013-11-09 00:30:16 -05001137 fprintf(stderr, do_slm_cstates ? "invalid" : do_snb_cstates ? "pc6-noret" : "pc3");
Len Brown889facb2012-11-08 00:48:57 -05001138 break;
1139 case 3:
Len Brown144b44b2013-11-09 00:30:16 -05001140 fprintf(stderr, do_slm_cstates ? "invalid" : "pc6");
Len Brown889facb2012-11-08 00:48:57 -05001141 break;
1142 case 4:
Len Brown144b44b2013-11-09 00:30:16 -05001143 fprintf(stderr, do_slm_cstates ? "pc4" : "pc7");
Len Brown889facb2012-11-08 00:48:57 -05001144 break;
1145 case 5:
Len Brown144b44b2013-11-09 00:30:16 -05001146 fprintf(stderr, do_slm_cstates ? "invalid" : do_snb_cstates ? "pc7s" : "invalid");
1147 break;
1148 case 6:
1149 fprintf(stderr, do_slm_cstates ? "pc6" : "invalid");
Len Brown889facb2012-11-08 00:48:57 -05001150 break;
1151 case 7:
Len Brown144b44b2013-11-09 00:30:16 -05001152 fprintf(stderr, do_slm_cstates ? "pc7" : "unlimited");
Len Brown889facb2012-11-08 00:48:57 -05001153 break;
1154 default:
1155 fprintf(stderr, "invalid");
1156 }
1157 fprintf(stderr, ")\n");
Len Brown103a8fe2010-10-22 23:53:03 -04001158
1159 if (!do_nehalem_turbo_ratio_limit)
1160 return;
1161
Len Brown9c63a652012-10-31 01:29:52 -04001162 get_msr(0, MSR_NHM_TURBO_RATIO_LIMIT, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001163
Len Brown67920412013-01-31 15:22:15 -05001164 fprintf(stderr, "cpu0: MSR_NHM_TURBO_RATIO_LIMIT: 0x%08llx\n", msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001165
1166 ratio = (msr >> 56) & 0xFF;
1167 if (ratio)
1168 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 8 active cores\n",
1169 ratio, bclk, ratio * bclk);
1170
1171 ratio = (msr >> 48) & 0xFF;
1172 if (ratio)
1173 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 7 active cores\n",
1174 ratio, bclk, ratio * bclk);
1175
1176 ratio = (msr >> 40) & 0xFF;
1177 if (ratio)
1178 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 6 active cores\n",
1179 ratio, bclk, ratio * bclk);
1180
1181 ratio = (msr >> 32) & 0xFF;
1182 if (ratio)
1183 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 5 active cores\n",
1184 ratio, bclk, ratio * bclk);
1185
Len Brown103a8fe2010-10-22 23:53:03 -04001186 ratio = (msr >> 24) & 0xFF;
1187 if (ratio)
1188 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
1189 ratio, bclk, ratio * bclk);
1190
1191 ratio = (msr >> 16) & 0xFF;
1192 if (ratio)
1193 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
1194 ratio, bclk, ratio * bclk);
1195
1196 ratio = (msr >> 8) & 0xFF;
1197 if (ratio)
1198 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
1199 ratio, bclk, ratio * bclk);
1200
1201 ratio = (msr >> 0) & 0xFF;
1202 if (ratio)
1203 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
1204 ratio, bclk, ratio * bclk);
Len Brown103a8fe2010-10-22 23:53:03 -04001205}
1206
Len Brownc98d5d92012-06-04 00:56:40 -04001207void free_all_buffers(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001208{
Len Brownc98d5d92012-06-04 00:56:40 -04001209 CPU_FREE(cpu_present_set);
1210 cpu_present_set = NULL;
1211 cpu_present_set = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001212
Len Brownc98d5d92012-06-04 00:56:40 -04001213 CPU_FREE(cpu_affinity_set);
1214 cpu_affinity_set = NULL;
1215 cpu_affinity_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001216
Len Brownc98d5d92012-06-04 00:56:40 -04001217 free(thread_even);
1218 free(core_even);
1219 free(package_even);
1220
1221 thread_even = NULL;
1222 core_even = NULL;
1223 package_even = NULL;
1224
1225 free(thread_odd);
1226 free(core_odd);
1227 free(package_odd);
1228
1229 thread_odd = NULL;
1230 core_odd = NULL;
1231 package_odd = NULL;
1232
1233 free(output_buffer);
1234 output_buffer = NULL;
1235 outp = NULL;
Len Brown103a8fe2010-10-22 23:53:03 -04001236}
1237
Len Brownc98d5d92012-06-04 00:56:40 -04001238/*
Josh Triplett57a42a32013-08-20 17:20:17 -07001239 * Open a file, and exit on failure
1240 */
1241FILE *fopen_or_die(const char *path, const char *mode)
1242{
1243 FILE *filep = fopen(path, "r");
Josh Triplettb2c95d92013-08-20 17:20:18 -07001244 if (!filep)
1245 err(1, "%s: open failed", path);
Josh Triplett57a42a32013-08-20 17:20:17 -07001246 return filep;
1247}
1248
1249/*
Josh Triplett95aebc42013-08-20 17:20:16 -07001250 * Parse a file containing a single int.
1251 */
1252int parse_int_file(const char *fmt, ...)
1253{
1254 va_list args;
1255 char path[PATH_MAX];
1256 FILE *filep;
1257 int value;
1258
1259 va_start(args, fmt);
1260 vsnprintf(path, sizeof(path), fmt, args);
1261 va_end(args);
Josh Triplett57a42a32013-08-20 17:20:17 -07001262 filep = fopen_or_die(path, "r");
Josh Triplettb2c95d92013-08-20 17:20:18 -07001263 if (fscanf(filep, "%d", &value) != 1)
1264 err(1, "%s: failed to parse number from file", path);
Josh Triplett95aebc42013-08-20 17:20:16 -07001265 fclose(filep);
1266 return value;
1267}
1268
1269/*
Len Brownc98d5d92012-06-04 00:56:40 -04001270 * cpu_is_first_sibling_in_core(cpu)
1271 * return 1 if given CPU is 1st HT sibling in the core
1272 */
1273int cpu_is_first_sibling_in_core(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001274{
Josh Triplett95aebc42013-08-20 17:20:16 -07001275 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001276}
1277
Len Brownc98d5d92012-06-04 00:56:40 -04001278/*
1279 * cpu_is_first_core_in_package(cpu)
1280 * return 1 if given CPU is 1st core in package
1281 */
1282int cpu_is_first_core_in_package(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001283{
Josh Triplett95aebc42013-08-20 17:20:16 -07001284 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001285}
1286
1287int get_physical_package_id(int cpu)
1288{
Josh Triplett95aebc42013-08-20 17:20:16 -07001289 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001290}
1291
1292int get_core_id(int cpu)
1293{
Josh Triplett95aebc42013-08-20 17:20:16 -07001294 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001295}
1296
Len Brownc98d5d92012-06-04 00:56:40 -04001297int get_num_ht_siblings(int cpu)
1298{
1299 char path[80];
1300 FILE *filep;
1301 int sib1, sib2;
1302 int matches;
1303 char character;
1304
1305 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
Josh Triplett57a42a32013-08-20 17:20:17 -07001306 filep = fopen_or_die(path, "r");
Len Brownc98d5d92012-06-04 00:56:40 -04001307 /*
1308 * file format:
1309 * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4)
1310 * otherwinse 1 sibling (self).
1311 */
1312 matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2);
1313
1314 fclose(filep);
1315
1316 if (matches == 3)
1317 return 2;
1318 else
1319 return 1;
1320}
1321
Len Brown103a8fe2010-10-22 23:53:03 -04001322/*
Len Brownc98d5d92012-06-04 00:56:40 -04001323 * run func(thread, core, package) in topology order
1324 * skip non-present cpus
Len Brown103a8fe2010-10-22 23:53:03 -04001325 */
1326
Len Brownc98d5d92012-06-04 00:56:40 -04001327int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
1328 struct pkg_data *, struct thread_data *, struct core_data *,
1329 struct pkg_data *), struct thread_data *thread_base,
1330 struct core_data *core_base, struct pkg_data *pkg_base,
1331 struct thread_data *thread_base2, struct core_data *core_base2,
1332 struct pkg_data *pkg_base2)
1333{
1334 int retval, pkg_no, core_no, thread_no;
1335
1336 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
1337 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
1338 for (thread_no = 0; thread_no <
1339 topo.num_threads_per_core; ++thread_no) {
1340 struct thread_data *t, *t2;
1341 struct core_data *c, *c2;
1342 struct pkg_data *p, *p2;
1343
1344 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
1345
1346 if (cpu_is_not_present(t->cpu_id))
1347 continue;
1348
1349 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
1350
1351 c = GET_CORE(core_base, core_no, pkg_no);
1352 c2 = GET_CORE(core_base2, core_no, pkg_no);
1353
1354 p = GET_PKG(pkg_base, pkg_no);
1355 p2 = GET_PKG(pkg_base2, pkg_no);
1356
1357 retval = func(t, c, p, t2, c2, p2);
1358 if (retval)
1359 return retval;
1360 }
1361 }
1362 }
1363 return 0;
1364}
1365
1366/*
1367 * run func(cpu) on every cpu in /proc/stat
1368 * return max_cpu number
1369 */
1370int for_all_proc_cpus(int (func)(int))
Len Brown103a8fe2010-10-22 23:53:03 -04001371{
1372 FILE *fp;
Len Brownc98d5d92012-06-04 00:56:40 -04001373 int cpu_num;
Len Brown103a8fe2010-10-22 23:53:03 -04001374 int retval;
1375
Josh Triplett57a42a32013-08-20 17:20:17 -07001376 fp = fopen_or_die(proc_stat, "r");
Len Brown103a8fe2010-10-22 23:53:03 -04001377
1378 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
Josh Triplettb2c95d92013-08-20 17:20:18 -07001379 if (retval != 0)
1380 err(1, "%s: failed to parse format", proc_stat);
Len Brown103a8fe2010-10-22 23:53:03 -04001381
Len Brownc98d5d92012-06-04 00:56:40 -04001382 while (1) {
1383 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 -04001384 if (retval != 1)
1385 break;
1386
Len Brownc98d5d92012-06-04 00:56:40 -04001387 retval = func(cpu_num);
1388 if (retval) {
1389 fclose(fp);
1390 return(retval);
1391 }
Len Brown103a8fe2010-10-22 23:53:03 -04001392 }
1393 fclose(fp);
Len Brownc98d5d92012-06-04 00:56:40 -04001394 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001395}
1396
1397void re_initialize(void)
1398{
Len Brownc98d5d92012-06-04 00:56:40 -04001399 free_all_buffers();
1400 setup_all_buffers();
1401 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04001402}
1403
Len Brownc98d5d92012-06-04 00:56:40 -04001404
Len Brown103a8fe2010-10-22 23:53:03 -04001405/*
Len Brownc98d5d92012-06-04 00:56:40 -04001406 * count_cpus()
1407 * remember the last one seen, it will be the max
Len Brown103a8fe2010-10-22 23:53:03 -04001408 */
Len Brownc98d5d92012-06-04 00:56:40 -04001409int count_cpus(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001410{
Len Brownc98d5d92012-06-04 00:56:40 -04001411 if (topo.max_cpu_num < cpu)
1412 topo.max_cpu_num = cpu;
Len Brown103a8fe2010-10-22 23:53:03 -04001413
Len Brownc98d5d92012-06-04 00:56:40 -04001414 topo.num_cpus += 1;
1415 return 0;
1416}
1417int mark_cpu_present(int cpu)
1418{
1419 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brown15aaa342012-03-29 22:19:58 -04001420 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001421}
1422
1423void turbostat_loop()
1424{
Len Brownc98d5d92012-06-04 00:56:40 -04001425 int retval;
Len Browne52966c2012-11-08 22:38:05 -05001426 int restarted = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001427
Len Brown103a8fe2010-10-22 23:53:03 -04001428restart:
Len Browne52966c2012-11-08 22:38:05 -05001429 restarted++;
1430
Len Brownc98d5d92012-06-04 00:56:40 -04001431 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001432 if (retval < -1) {
1433 exit(retval);
1434 } else if (retval == -1) {
Len Browne52966c2012-11-08 22:38:05 -05001435 if (restarted > 1) {
1436 exit(retval);
1437 }
Len Brownc98d5d92012-06-04 00:56:40 -04001438 re_initialize();
1439 goto restart;
1440 }
Len Browne52966c2012-11-08 22:38:05 -05001441 restarted = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001442 gettimeofday(&tv_even, (struct timezone *)NULL);
1443
1444 while (1) {
Len Brownc98d5d92012-06-04 00:56:40 -04001445 if (for_all_proc_cpus(cpu_is_not_present)) {
Len Brown103a8fe2010-10-22 23:53:03 -04001446 re_initialize();
1447 goto restart;
1448 }
1449 sleep(interval_sec);
Len Brownc98d5d92012-06-04 00:56:40 -04001450 retval = for_all_cpus(get_counters, ODD_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001451 if (retval < -1) {
1452 exit(retval);
1453 } else if (retval == -1) {
Len Brown15aaa342012-03-29 22:19:58 -04001454 re_initialize();
1455 goto restart;
1456 }
Len Brown103a8fe2010-10-22 23:53:03 -04001457 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04001458 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04001459 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
1460 compute_average(EVEN_COUNTERS);
1461 format_all_counters(EVEN_COUNTERS);
1462 flush_stdout();
Len Brown15aaa342012-03-29 22:19:58 -04001463 sleep(interval_sec);
Len Brownc98d5d92012-06-04 00:56:40 -04001464 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001465 if (retval < -1) {
1466 exit(retval);
1467 } else if (retval == -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04001468 re_initialize();
1469 goto restart;
1470 }
Len Brown103a8fe2010-10-22 23:53:03 -04001471 gettimeofday(&tv_even, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04001472 timersub(&tv_even, &tv_odd, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04001473 for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS);
1474 compute_average(ODD_COUNTERS);
1475 format_all_counters(ODD_COUNTERS);
1476 flush_stdout();
Len Brown103a8fe2010-10-22 23:53:03 -04001477 }
1478}
1479
1480void check_dev_msr()
1481{
1482 struct stat sb;
1483
Josh Triplettb2c95d92013-08-20 17:20:18 -07001484 if (stat("/dev/cpu/0/msr", &sb))
1485 err(-5, "no /dev/cpu/0/msr\n"
1486 "Try \"# modprobe msr\"");
Len Brown103a8fe2010-10-22 23:53:03 -04001487}
1488
1489void check_super_user()
1490{
Josh Triplettb2c95d92013-08-20 17:20:18 -07001491 if (getuid() != 0)
1492 errx(-6, "must be root");
Len Brown103a8fe2010-10-22 23:53:03 -04001493}
1494
1495int has_nehalem_turbo_ratio_limit(unsigned int family, unsigned int model)
1496{
1497 if (!genuine_intel)
1498 return 0;
1499
1500 if (family != 6)
1501 return 0;
1502
1503 switch (model) {
1504 case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
1505 case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
1506 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
1507 case 0x25: /* Westmere Client - Clarkdale, Arrandale */
1508 case 0x2C: /* Westmere EP - Gulftown */
1509 case 0x2A: /* SNB */
1510 case 0x2D: /* SNB Xeon */
Len Brown553575f2011-11-18 03:32:01 -05001511 case 0x3A: /* IVB */
Len Brown13006512012-09-26 18:11:31 -04001512 case 0x3E: /* IVB Xeon */
Len Brown70b43402013-01-08 01:26:07 -05001513 case 0x3C: /* HSW */
Len Browne6f9bb32013-12-03 02:19:19 -05001514 case 0x3F: /* HSX */
Len Brown70b43402013-01-08 01:26:07 -05001515 case 0x45: /* HSW */
Len Brown149c2312013-03-15 10:58:02 -04001516 case 0x46: /* HSW */
Len Brown144b44b2013-11-09 00:30:16 -05001517 case 0x37: /* BYT */
1518 case 0x4D: /* AVN */
Len Brown103a8fe2010-10-22 23:53:03 -04001519 return 1;
1520 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1521 case 0x2F: /* Westmere-EX Xeon - Eagleton */
1522 default:
1523 return 0;
1524 }
1525}
Len Brown6574a5d2012-09-21 00:01:31 -04001526int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
1527{
1528 if (!genuine_intel)
1529 return 0;
1530
1531 if (family != 6)
1532 return 0;
1533
1534 switch (model) {
1535 case 0x3E: /* IVB Xeon */
1536 return 1;
1537 default:
1538 return 0;
1539 }
1540}
1541
Len Brown889facb2012-11-08 00:48:57 -05001542/*
1543 * print_epb()
1544 * Decode the ENERGY_PERF_BIAS MSR
1545 */
1546int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1547{
1548 unsigned long long msr;
1549 char *epb_string;
1550 int cpu;
1551
1552 if (!has_epb)
1553 return 0;
1554
1555 cpu = t->cpu_id;
1556
1557 /* EPB is per-package */
1558 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1559 return 0;
1560
1561 if (cpu_migrate(cpu)) {
1562 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1563 return -1;
1564 }
1565
1566 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
1567 return 0;
1568
1569 switch (msr & 0x7) {
1570 case ENERGY_PERF_BIAS_PERFORMANCE:
1571 epb_string = "performance";
1572 break;
1573 case ENERGY_PERF_BIAS_NORMAL:
1574 epb_string = "balanced";
1575 break;
1576 case ENERGY_PERF_BIAS_POWERSAVE:
1577 epb_string = "powersave";
1578 break;
1579 default:
1580 epb_string = "custom";
1581 break;
1582 }
1583 fprintf(stderr, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
1584
1585 return 0;
1586}
1587
1588#define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
1589#define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
1590
Len Brown144b44b2013-11-09 00:30:16 -05001591double get_tdp(model)
1592{
1593 unsigned long long msr;
1594
1595 if (do_rapl & RAPL_PKG_POWER_INFO)
1596 if (!get_msr(0, MSR_PKG_POWER_INFO, &msr))
1597 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
1598
1599 switch (model) {
1600 case 0x37:
1601 case 0x4D:
1602 return 30.0;
1603 default:
1604 return 135.0;
1605 }
1606}
1607
1608
Len Brown889facb2012-11-08 00:48:57 -05001609/*
1610 * rapl_probe()
1611 *
Len Brown144b44b2013-11-09 00:30:16 -05001612 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
Len Brown889facb2012-11-08 00:48:57 -05001613 */
1614void rapl_probe(unsigned int family, unsigned int model)
1615{
1616 unsigned long long msr;
Len Brown144b44b2013-11-09 00:30:16 -05001617 unsigned int time_unit;
Len Brown889facb2012-11-08 00:48:57 -05001618 double tdp;
1619
1620 if (!genuine_intel)
1621 return;
1622
1623 if (family != 6)
1624 return;
1625
1626 switch (model) {
1627 case 0x2A:
1628 case 0x3A:
Len Brown70b43402013-01-08 01:26:07 -05001629 case 0x3C: /* HSW */
Len Brown70b43402013-01-08 01:26:07 -05001630 case 0x45: /* HSW */
Len Brown149c2312013-03-15 10:58:02 -04001631 case 0x46: /* HSW */
Len Brown144b44b2013-11-09 00:30:16 -05001632 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
Len Brown889facb2012-11-08 00:48:57 -05001633 break;
Len Browne6f9bb32013-12-03 02:19:19 -05001634 case 0x3F: /* HSX */
1635 do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
1636 break;
Len Brown889facb2012-11-08 00:48:57 -05001637 case 0x2D:
1638 case 0x3E:
Len Brown144b44b2013-11-09 00:30:16 -05001639 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_PKG_PERF_STATUS | RAPL_DRAM_PERF_STATUS | RAPL_PKG_POWER_INFO;
1640 break;
1641 case 0x37: /* BYT */
1642 case 0x4D: /* AVN */
1643 do_rapl = RAPL_PKG | RAPL_CORES ;
Len Brown889facb2012-11-08 00:48:57 -05001644 break;
1645 default:
1646 return;
1647 }
1648
1649 /* units on package 0, verify later other packages match */
1650 if (get_msr(0, MSR_RAPL_POWER_UNIT, &msr))
1651 return;
1652
1653 rapl_power_units = 1.0 / (1 << (msr & 0xF));
Len Brown144b44b2013-11-09 00:30:16 -05001654 if (model == 0x37)
1655 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
1656 else
1657 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
Len Brown889facb2012-11-08 00:48:57 -05001658
Len Brown144b44b2013-11-09 00:30:16 -05001659 time_unit = msr >> 16 & 0xF;
1660 if (time_unit == 0)
1661 time_unit = 0xA;
Len Brown889facb2012-11-08 00:48:57 -05001662
Len Brown144b44b2013-11-09 00:30:16 -05001663 rapl_time_units = 1.0 / (1 << (time_unit));
1664
1665 tdp = get_tdp(model);
Len Brown889facb2012-11-08 00:48:57 -05001666
1667 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
Len Brown889facb2012-11-08 00:48:57 -05001668 if (verbose)
Len Brown144b44b2013-11-09 00:30:16 -05001669 fprintf(stderr, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
Len Brown889facb2012-11-08 00:48:57 -05001670
1671 return;
1672}
1673
1674int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1675{
1676 unsigned long long msr;
1677 unsigned int dts;
1678 int cpu;
1679
1680 if (!(do_dts || do_ptm))
1681 return 0;
1682
1683 cpu = t->cpu_id;
1684
1685 /* DTS is per-core, no need to print for each thread */
1686 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1687 return 0;
1688
1689 if (cpu_migrate(cpu)) {
1690 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1691 return -1;
1692 }
1693
1694 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
1695 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1696 return 0;
1697
1698 dts = (msr >> 16) & 0x7F;
1699 fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
1700 cpu, msr, tcc_activation_temp - dts);
1701
1702#ifdef THERM_DEBUG
1703 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
1704 return 0;
1705
1706 dts = (msr >> 16) & 0x7F;
1707 dts2 = (msr >> 8) & 0x7F;
1708 fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
1709 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
1710#endif
1711 }
1712
1713
1714 if (do_dts) {
1715 unsigned int resolution;
1716
1717 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1718 return 0;
1719
1720 dts = (msr >> 16) & 0x7F;
1721 resolution = (msr >> 27) & 0xF;
1722 fprintf(stderr, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
1723 cpu, msr, tcc_activation_temp - dts, resolution);
1724
1725#ifdef THERM_DEBUG
1726 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
1727 return 0;
1728
1729 dts = (msr >> 16) & 0x7F;
1730 dts2 = (msr >> 8) & 0x7F;
1731 fprintf(stderr, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
1732 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
1733#endif
1734 }
1735
1736 return 0;
1737}
1738
1739void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
1740{
1741 fprintf(stderr, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
1742 cpu, label,
1743 ((msr >> 15) & 1) ? "EN" : "DIS",
1744 ((msr >> 0) & 0x7FFF) * rapl_power_units,
1745 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
1746 (((msr >> 16) & 1) ? "EN" : "DIS"));
1747
1748 return;
1749}
1750
1751int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1752{
1753 unsigned long long msr;
1754 int cpu;
Len Brown889facb2012-11-08 00:48:57 -05001755
1756 if (!do_rapl)
1757 return 0;
1758
1759 /* RAPL counters are per package, so print only for 1st thread/package */
1760 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1761 return 0;
1762
1763 cpu = t->cpu_id;
1764 if (cpu_migrate(cpu)) {
1765 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1766 return -1;
1767 }
1768
1769 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
1770 return -1;
1771
Len Brown889facb2012-11-08 00:48:57 -05001772 if (verbose) {
1773 fprintf(stderr, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx "
1774 "(%f Watts, %f Joules, %f sec.)\n", cpu, msr,
Len Brown144b44b2013-11-09 00:30:16 -05001775 rapl_power_units, rapl_energy_units, rapl_time_units);
Len Brown889facb2012-11-08 00:48:57 -05001776 }
Len Brown144b44b2013-11-09 00:30:16 -05001777 if (do_rapl & RAPL_PKG_POWER_INFO) {
1778
Len Brown889facb2012-11-08 00:48:57 -05001779 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
1780 return -5;
1781
1782
1783 fprintf(stderr, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
1784 cpu, msr,
1785 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1786 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1787 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1788 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
1789
Len Brown144b44b2013-11-09 00:30:16 -05001790 }
1791 if (do_rapl & RAPL_PKG) {
1792
Len Brown889facb2012-11-08 00:48:57 -05001793 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
1794 return -9;
1795
1796 fprintf(stderr, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
1797 cpu, msr, (msr >> 63) & 1 ? "": "UN");
1798
1799 print_power_limit_msr(cpu, msr, "PKG Limit #1");
1800 fprintf(stderr, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
1801 cpu,
1802 ((msr >> 47) & 1) ? "EN" : "DIS",
1803 ((msr >> 32) & 0x7FFF) * rapl_power_units,
1804 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
1805 ((msr >> 48) & 1) ? "EN" : "DIS");
1806 }
1807
1808 if (do_rapl & RAPL_DRAM) {
1809 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
1810 return -6;
1811
1812
1813 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
1814 cpu, msr,
1815 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1816 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1817 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1818 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
1819
1820
1821 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
1822 return -9;
1823 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
1824 cpu, msr, (msr >> 31) & 1 ? "": "UN");
1825
1826 print_power_limit_msr(cpu, msr, "DRAM Limit");
1827 }
Len Brown144b44b2013-11-09 00:30:16 -05001828 if (do_rapl & RAPL_CORE_POLICY) {
Len Brown889facb2012-11-08 00:48:57 -05001829 if (verbose) {
1830 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
1831 return -7;
1832
1833 fprintf(stderr, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown144b44b2013-11-09 00:30:16 -05001834 }
1835 }
1836 if (do_rapl & RAPL_CORES) {
1837 if (verbose) {
Len Brown889facb2012-11-08 00:48:57 -05001838
1839 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
1840 return -9;
1841 fprintf(stderr, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
1842 cpu, msr, (msr >> 31) & 1 ? "": "UN");
1843 print_power_limit_msr(cpu, msr, "Cores Limit");
1844 }
1845 }
1846 if (do_rapl & RAPL_GFX) {
1847 if (verbose) {
1848 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
1849 return -8;
1850
1851 fprintf(stderr, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
1852
1853 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
1854 return -9;
1855 fprintf(stderr, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
1856 cpu, msr, (msr >> 31) & 1 ? "": "UN");
1857 print_power_limit_msr(cpu, msr, "GFX Limit");
1858 }
1859 }
1860 return 0;
1861}
1862
Len Brown103a8fe2010-10-22 23:53:03 -04001863
1864int is_snb(unsigned int family, unsigned int model)
1865{
1866 if (!genuine_intel)
1867 return 0;
1868
1869 switch (model) {
1870 case 0x2A:
1871 case 0x2D:
Len Brown650a37f2012-06-03 23:34:44 -04001872 case 0x3A: /* IVB */
Len Brown13006512012-09-26 18:11:31 -04001873 case 0x3E: /* IVB Xeon */
Len Brown70b43402013-01-08 01:26:07 -05001874 case 0x3C: /* HSW */
1875 case 0x3F: /* HSW */
1876 case 0x45: /* HSW */
Len Brown149c2312013-03-15 10:58:02 -04001877 case 0x46: /* HSW */
Len Brown103a8fe2010-10-22 23:53:03 -04001878 return 1;
1879 }
1880 return 0;
1881}
1882
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001883int has_c8_c9_c10(unsigned int family, unsigned int model)
1884{
1885 if (!genuine_intel)
1886 return 0;
1887
1888 switch (model) {
1889 case 0x45:
1890 return 1;
1891 }
1892 return 0;
1893}
1894
1895
Len Brown144b44b2013-11-09 00:30:16 -05001896int is_slm(unsigned int family, unsigned int model)
1897{
1898 if (!genuine_intel)
1899 return 0;
1900 switch (model) {
1901 case 0x37: /* BYT */
1902 case 0x4D: /* AVN */
1903 return 1;
1904 }
1905 return 0;
1906}
1907
1908#define SLM_BCLK_FREQS 5
1909double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
1910
1911double slm_bclk(void)
1912{
1913 unsigned long long msr = 3;
1914 unsigned int i;
1915 double freq;
1916
1917 if (get_msr(0, MSR_FSB_FREQ, &msr))
1918 fprintf(stderr, "SLM BCLK: unknown\n");
1919
1920 i = msr & 0xf;
1921 if (i >= SLM_BCLK_FREQS) {
1922 fprintf(stderr, "SLM BCLK[%d] invalid\n", i);
1923 msr = 3;
1924 }
1925 freq = slm_freq_table[i];
1926
1927 fprintf(stderr, "SLM BCLK: %.1f Mhz\n", freq);
1928
1929 return freq;
1930}
1931
Len Brown103a8fe2010-10-22 23:53:03 -04001932double discover_bclk(unsigned int family, unsigned int model)
1933{
1934 if (is_snb(family, model))
1935 return 100.00;
Len Brown144b44b2013-11-09 00:30:16 -05001936 else if (is_slm(family, model))
1937 return slm_bclk();
Len Brown103a8fe2010-10-22 23:53:03 -04001938 else
1939 return 133.33;
1940}
1941
Len Brown889facb2012-11-08 00:48:57 -05001942/*
1943 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
1944 * the Thermal Control Circuit (TCC) activates.
1945 * This is usually equal to tjMax.
1946 *
1947 * Older processors do not have this MSR, so there we guess,
1948 * but also allow cmdline over-ride with -T.
1949 *
1950 * Several MSR temperature values are in units of degrees-C
1951 * below this value, including the Digital Thermal Sensor (DTS),
1952 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
1953 */
1954int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1955{
1956 unsigned long long msr;
1957 unsigned int target_c_local;
1958 int cpu;
1959
1960 /* tcc_activation_temp is used only for dts or ptm */
1961 if (!(do_dts || do_ptm))
1962 return 0;
1963
1964 /* this is a per-package concept */
1965 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1966 return 0;
1967
1968 cpu = t->cpu_id;
1969 if (cpu_migrate(cpu)) {
1970 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1971 return -1;
1972 }
1973
1974 if (tcc_activation_temp_override != 0) {
1975 tcc_activation_temp = tcc_activation_temp_override;
1976 fprintf(stderr, "cpu%d: Using cmdline TCC Target (%d C)\n",
1977 cpu, tcc_activation_temp);
1978 return 0;
1979 }
1980
1981 /* Temperature Target MSR is Nehalem and newer only */
1982 if (!do_nehalem_platform_info)
1983 goto guess;
1984
1985 if (get_msr(0, MSR_IA32_TEMPERATURE_TARGET, &msr))
1986 goto guess;
1987
1988 target_c_local = (msr >> 16) & 0x7F;
1989
1990 if (verbose)
1991 fprintf(stderr, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
1992 cpu, msr, target_c_local);
1993
Len Brown144b44b2013-11-09 00:30:16 -05001994 if (target_c_local < 85 || target_c_local > 127)
Len Brown889facb2012-11-08 00:48:57 -05001995 goto guess;
1996
1997 tcc_activation_temp = target_c_local;
1998
1999 return 0;
2000
2001guess:
2002 tcc_activation_temp = TJMAX_DEFAULT;
2003 fprintf(stderr, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
2004 cpu, tcc_activation_temp);
2005
2006 return 0;
2007}
Len Brown103a8fe2010-10-22 23:53:03 -04002008void check_cpuid()
2009{
2010 unsigned int eax, ebx, ecx, edx, max_level;
2011 unsigned int fms, family, model, stepping;
2012
2013 eax = ebx = ecx = edx = 0;
2014
Josh Triplett2b928652013-08-20 17:20:14 -07002015 __get_cpuid(0, &max_level, &ebx, &ecx, &edx);
Len Brown103a8fe2010-10-22 23:53:03 -04002016
2017 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
2018 genuine_intel = 1;
2019
2020 if (verbose)
Len Brown889facb2012-11-08 00:48:57 -05002021 fprintf(stderr, "CPUID(0): %.4s%.4s%.4s ",
Len Brown103a8fe2010-10-22 23:53:03 -04002022 (char *)&ebx, (char *)&edx, (char *)&ecx);
2023
Josh Triplett2b928652013-08-20 17:20:14 -07002024 __get_cpuid(1, &fms, &ebx, &ecx, &edx);
Len Brown103a8fe2010-10-22 23:53:03 -04002025 family = (fms >> 8) & 0xf;
2026 model = (fms >> 4) & 0xf;
2027 stepping = fms & 0xf;
2028 if (family == 6 || family == 0xf)
2029 model += ((fms >> 16) & 0xf) << 4;
2030
2031 if (verbose)
2032 fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
2033 max_level, family, model, stepping, family, model, stepping);
2034
Josh Triplettb2c95d92013-08-20 17:20:18 -07002035 if (!(edx & (1 << 5)))
2036 errx(1, "CPUID: no MSR");
Len Brown103a8fe2010-10-22 23:53:03 -04002037
2038 /*
2039 * check max extended function levels of CPUID.
2040 * This is needed to check for invariant TSC.
2041 * This check is valid for both Intel and AMD.
2042 */
2043 ebx = ecx = edx = 0;
Josh Triplett2b928652013-08-20 17:20:14 -07002044 __get_cpuid(0x80000000, &max_level, &ebx, &ecx, &edx);
Len Brown103a8fe2010-10-22 23:53:03 -04002045
Josh Triplettb2c95d92013-08-20 17:20:18 -07002046 if (max_level < 0x80000007)
2047 errx(1, "CPUID: no invariant TSC (max_level 0x%x)", max_level);
Len Brown103a8fe2010-10-22 23:53:03 -04002048
2049 /*
2050 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
2051 * this check is valid for both Intel and AMD
2052 */
Josh Triplett2b928652013-08-20 17:20:14 -07002053 __get_cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
Thomas Renninger8209e052011-01-21 15:11:19 +01002054 has_invariant_tsc = edx & (1 << 8);
Len Brown103a8fe2010-10-22 23:53:03 -04002055
Josh Triplettb2c95d92013-08-20 17:20:18 -07002056 if (!has_invariant_tsc)
2057 errx(1, "No invariant TSC");
Len Brown103a8fe2010-10-22 23:53:03 -04002058
2059 /*
2060 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
2061 * this check is valid for both Intel and AMD
2062 */
2063
Josh Triplett2b928652013-08-20 17:20:14 -07002064 __get_cpuid(0x6, &eax, &ebx, &ecx, &edx);
Thomas Renninger8209e052011-01-21 15:11:19 +01002065 has_aperf = ecx & (1 << 0);
Len Brown889facb2012-11-08 00:48:57 -05002066 do_dts = eax & (1 << 0);
2067 do_ptm = eax & (1 << 6);
2068 has_epb = ecx & (1 << 3);
2069
2070 if (verbose)
2071 fprintf(stderr, "CPUID(6): %s%s%s%s\n",
2072 has_aperf ? "APERF" : "No APERF!",
2073 do_dts ? ", DTS" : "",
2074 do_ptm ? ", PTM": "",
2075 has_epb ? ", EPB": "");
2076
2077 if (!has_aperf)
Josh Triplettb2c95d92013-08-20 17:20:18 -07002078 errx(-1, "No APERF");
Len Brown103a8fe2010-10-22 23:53:03 -04002079
2080 do_nehalem_platform_info = genuine_intel && has_invariant_tsc;
2081 do_nhm_cstates = genuine_intel; /* all Intel w/ non-stop TSC have NHM counters */
Len Brown1ed51012013-02-10 17:19:24 -05002082 do_smi = do_nhm_cstates;
Len Brown103a8fe2010-10-22 23:53:03 -04002083 do_snb_cstates = is_snb(family, model);
Kristen Carlson Accardica587102012-11-21 05:22:43 -08002084 do_c8_c9_c10 = has_c8_c9_c10(family, model);
Len Brown144b44b2013-11-09 00:30:16 -05002085 do_slm_cstates = is_slm(family, model);
Len Brown103a8fe2010-10-22 23:53:03 -04002086 bclk = discover_bclk(family, model);
2087
2088 do_nehalem_turbo_ratio_limit = has_nehalem_turbo_ratio_limit(family, model);
Len Brown6574a5d2012-09-21 00:01:31 -04002089 do_ivt_turbo_ratio_limit = has_ivt_turbo_ratio_limit(family, model);
Len Brown889facb2012-11-08 00:48:57 -05002090 rapl_probe(family, model);
2091
2092 return;
Len Brown103a8fe2010-10-22 23:53:03 -04002093}
2094
2095
2096void usage()
2097{
Andy Shevchenkof591c382014-01-23 17:13:14 +02002098 errx(1, "%s: [-v][-R][-T][-p|-P|-S][-c MSR#][-C MSR#][-m MSR#][-M MSR#][-i interval_sec | command ...]\n",
Josh Triplettb2c95d92013-08-20 17:20:18 -07002099 progname);
Len Brown103a8fe2010-10-22 23:53:03 -04002100}
2101
2102
2103/*
2104 * in /dev/cpu/ return success for names that are numbers
2105 * ie. filter out ".", "..", "microcode".
2106 */
2107int dir_filter(const struct dirent *dirp)
2108{
2109 if (isdigit(dirp->d_name[0]))
2110 return 1;
2111 else
2112 return 0;
2113}
2114
2115int open_dev_cpu_msr(int dummy1)
2116{
2117 return 0;
2118}
2119
Len Brownc98d5d92012-06-04 00:56:40 -04002120void topology_probe()
2121{
2122 int i;
2123 int max_core_id = 0;
2124 int max_package_id = 0;
2125 int max_siblings = 0;
2126 struct cpu_topology {
2127 int core_id;
2128 int physical_package_id;
2129 } *cpus;
2130
2131 /* Initialize num_cpus, max_cpu_num */
2132 topo.num_cpus = 0;
2133 topo.max_cpu_num = 0;
2134 for_all_proc_cpus(count_cpus);
2135 if (!summary_only && topo.num_cpus > 1)
2136 show_cpu = 1;
2137
2138 if (verbose > 1)
2139 fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
2140
2141 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
Josh Triplettb2c95d92013-08-20 17:20:18 -07002142 if (cpus == NULL)
2143 err(1, "calloc cpus");
Len Brownc98d5d92012-06-04 00:56:40 -04002144
2145 /*
2146 * Allocate and initialize cpu_present_set
2147 */
2148 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07002149 if (cpu_present_set == NULL)
2150 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04002151 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2152 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
2153 for_all_proc_cpus(mark_cpu_present);
2154
2155 /*
2156 * Allocate and initialize cpu_affinity_set
2157 */
2158 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07002159 if (cpu_affinity_set == NULL)
2160 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04002161 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2162 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
2163
2164
2165 /*
2166 * For online cpus
2167 * find max_core_id, max_package_id
2168 */
2169 for (i = 0; i <= topo.max_cpu_num; ++i) {
2170 int siblings;
2171
2172 if (cpu_is_not_present(i)) {
2173 if (verbose > 1)
2174 fprintf(stderr, "cpu%d NOT PRESENT\n", i);
2175 continue;
2176 }
2177 cpus[i].core_id = get_core_id(i);
2178 if (cpus[i].core_id > max_core_id)
2179 max_core_id = cpus[i].core_id;
2180
2181 cpus[i].physical_package_id = get_physical_package_id(i);
2182 if (cpus[i].physical_package_id > max_package_id)
2183 max_package_id = cpus[i].physical_package_id;
2184
2185 siblings = get_num_ht_siblings(i);
2186 if (siblings > max_siblings)
2187 max_siblings = siblings;
2188 if (verbose > 1)
2189 fprintf(stderr, "cpu %d pkg %d core %d\n",
2190 i, cpus[i].physical_package_id, cpus[i].core_id);
2191 }
2192 topo.num_cores_per_pkg = max_core_id + 1;
2193 if (verbose > 1)
2194 fprintf(stderr, "max_core_id %d, sizing for %d cores per package\n",
2195 max_core_id, topo.num_cores_per_pkg);
2196 if (!summary_only && topo.num_cores_per_pkg > 1)
2197 show_core = 1;
2198
2199 topo.num_packages = max_package_id + 1;
2200 if (verbose > 1)
2201 fprintf(stderr, "max_package_id %d, sizing for %d packages\n",
2202 max_package_id, topo.num_packages);
2203 if (!summary_only && topo.num_packages > 1)
2204 show_pkg = 1;
2205
2206 topo.num_threads_per_core = max_siblings;
2207 if (verbose > 1)
2208 fprintf(stderr, "max_siblings %d\n", max_siblings);
2209
2210 free(cpus);
2211}
2212
2213void
2214allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
2215{
2216 int i;
2217
2218 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
2219 topo.num_packages, sizeof(struct thread_data));
2220 if (*t == NULL)
2221 goto error;
2222
2223 for (i = 0; i < topo.num_threads_per_core *
2224 topo.num_cores_per_pkg * topo.num_packages; i++)
2225 (*t)[i].cpu_id = -1;
2226
2227 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
2228 sizeof(struct core_data));
2229 if (*c == NULL)
2230 goto error;
2231
2232 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
2233 (*c)[i].core_id = -1;
2234
2235 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
2236 if (*p == NULL)
2237 goto error;
2238
2239 for (i = 0; i < topo.num_packages; i++)
2240 (*p)[i].package_id = i;
2241
2242 return;
2243error:
Josh Triplettb2c95d92013-08-20 17:20:18 -07002244 err(1, "calloc counters");
Len Brownc98d5d92012-06-04 00:56:40 -04002245}
2246/*
2247 * init_counter()
2248 *
2249 * set cpu_id, core_num, pkg_num
2250 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
2251 *
2252 * increment topo.num_cores when 1st core in pkg seen
2253 */
2254void init_counter(struct thread_data *thread_base, struct core_data *core_base,
2255 struct pkg_data *pkg_base, int thread_num, int core_num,
2256 int pkg_num, int cpu_id)
2257{
2258 struct thread_data *t;
2259 struct core_data *c;
2260 struct pkg_data *p;
2261
2262 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
2263 c = GET_CORE(core_base, core_num, pkg_num);
2264 p = GET_PKG(pkg_base, pkg_num);
2265
2266 t->cpu_id = cpu_id;
2267 if (thread_num == 0) {
2268 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
2269 if (cpu_is_first_core_in_package(cpu_id))
2270 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
2271 }
2272
2273 c->core_id = core_num;
2274 p->package_id = pkg_num;
2275}
2276
2277
2278int initialize_counters(int cpu_id)
2279{
2280 int my_thread_id, my_core_id, my_package_id;
2281
2282 my_package_id = get_physical_package_id(cpu_id);
2283 my_core_id = get_core_id(cpu_id);
2284
2285 if (cpu_is_first_sibling_in_core(cpu_id)) {
2286 my_thread_id = 0;
2287 topo.num_cores++;
2288 } else {
2289 my_thread_id = 1;
2290 }
2291
2292 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2293 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2294 return 0;
2295}
2296
2297void allocate_output_buffer()
2298{
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02002299 output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
Len Brownc98d5d92012-06-04 00:56:40 -04002300 outp = output_buffer;
Josh Triplettb2c95d92013-08-20 17:20:18 -07002301 if (outp == NULL)
2302 err(-1, "calloc output buffer");
Len Brownc98d5d92012-06-04 00:56:40 -04002303}
2304
2305void setup_all_buffers(void)
2306{
2307 topology_probe();
2308 allocate_counters(&thread_even, &core_even, &package_even);
2309 allocate_counters(&thread_odd, &core_odd, &package_odd);
2310 allocate_output_buffer();
2311 for_all_proc_cpus(initialize_counters);
2312}
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02002313
Len Brown103a8fe2010-10-22 23:53:03 -04002314void turbostat_init()
2315{
2316 check_cpuid();
2317
2318 check_dev_msr();
2319 check_super_user();
2320
Len Brownc98d5d92012-06-04 00:56:40 -04002321 setup_all_buffers();
Len Brown103a8fe2010-10-22 23:53:03 -04002322
2323 if (verbose)
Len Brownc98d5d92012-06-04 00:56:40 -04002324 print_verbose_header();
Len Brown889facb2012-11-08 00:48:57 -05002325
2326 if (verbose)
2327 for_all_cpus(print_epb, ODD_COUNTERS);
2328
2329 if (verbose)
2330 for_all_cpus(print_rapl, ODD_COUNTERS);
2331
2332 for_all_cpus(set_temperature_target, ODD_COUNTERS);
2333
2334 if (verbose)
2335 for_all_cpus(print_thermal, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04002336}
2337
2338int fork_it(char **argv)
2339{
Len Brown103a8fe2010-10-22 23:53:03 -04002340 pid_t child_pid;
Len Brownd91bb172012-11-01 00:08:19 -04002341 int status;
Len Brownd15cf7c2012-06-03 23:24:00 -04002342
Len Brownd91bb172012-11-01 00:08:19 -04002343 status = for_all_cpus(get_counters, EVEN_COUNTERS);
2344 if (status)
2345 exit(status);
Len Brownc98d5d92012-06-04 00:56:40 -04002346 /* clear affinity side-effect of get_counters() */
2347 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
Len Brown103a8fe2010-10-22 23:53:03 -04002348 gettimeofday(&tv_even, (struct timezone *)NULL);
2349
2350 child_pid = fork();
2351 if (!child_pid) {
2352 /* child */
2353 execvp(argv[0], argv);
2354 } else {
Len Brown103a8fe2010-10-22 23:53:03 -04002355
2356 /* parent */
Josh Triplettb2c95d92013-08-20 17:20:18 -07002357 if (child_pid == -1)
2358 err(1, "fork");
Len Brown103a8fe2010-10-22 23:53:03 -04002359
2360 signal(SIGINT, SIG_IGN);
2361 signal(SIGQUIT, SIG_IGN);
Josh Triplettb2c95d92013-08-20 17:20:18 -07002362 if (waitpid(child_pid, &status, 0) == -1)
2363 err(status, "waitpid");
Len Brown103a8fe2010-10-22 23:53:03 -04002364 }
Len Brownc98d5d92012-06-04 00:56:40 -04002365 /*
2366 * n.b. fork_it() does not check for errors from for_all_cpus()
2367 * because re-starting is problematic when forking
2368 */
2369 for_all_cpus(get_counters, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04002370 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002371 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04002372 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
2373 compute_average(EVEN_COUNTERS);
2374 format_all_counters(EVEN_COUNTERS);
2375 flush_stderr();
Len Brown103a8fe2010-10-22 23:53:03 -04002376
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07002377 fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
Len Brown103a8fe2010-10-22 23:53:03 -04002378
Len Brownd91bb172012-11-01 00:08:19 -04002379 return status;
Len Brown103a8fe2010-10-22 23:53:03 -04002380}
2381
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02002382int get_and_dump_counters(void)
2383{
2384 int status;
2385
2386 status = for_all_cpus(get_counters, ODD_COUNTERS);
2387 if (status)
2388 return status;
2389
2390 status = for_all_cpus(dump_counters, ODD_COUNTERS);
2391 if (status)
2392 return status;
2393
2394 flush_stdout();
2395
2396 return status;
2397}
2398
Len Brown103a8fe2010-10-22 23:53:03 -04002399void cmdline(int argc, char **argv)
2400{
2401 int opt;
2402
2403 progname = argv[0];
2404
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02002405 while ((opt = getopt(argc, argv, "+pPsSvi:c:C:m:M:RJT:")) != -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04002406 switch (opt) {
Len Brownf9240812012-10-06 15:26:31 -04002407 case 'p':
Len Brownc98d5d92012-06-04 00:56:40 -04002408 show_core_only++;
2409 break;
Len Brownf9240812012-10-06 15:26:31 -04002410 case 'P':
Len Brownc98d5d92012-06-04 00:56:40 -04002411 show_pkg_only++;
2412 break;
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02002413 case 's':
2414 dump_only++;
2415 break;
Len Brownf9240812012-10-06 15:26:31 -04002416 case 'S':
Len Browne23da032012-02-06 18:37:16 -05002417 summary_only++;
2418 break;
Len Brown103a8fe2010-10-22 23:53:03 -04002419 case 'v':
2420 verbose++;
2421 break;
2422 case 'i':
2423 interval_sec = atoi(optarg);
2424 break;
Len Brownf9240812012-10-06 15:26:31 -04002425 case 'c':
Len Brown8e180f32012-09-22 01:25:08 -04002426 sscanf(optarg, "%x", &extra_delta_offset32);
2427 break;
Len Brownf9240812012-10-06 15:26:31 -04002428 case 'C':
Len Brown8e180f32012-09-22 01:25:08 -04002429 sscanf(optarg, "%x", &extra_delta_offset64);
2430 break;
Len Brown2f32edf2012-09-21 23:45:46 -04002431 case 'm':
2432 sscanf(optarg, "%x", &extra_msr_offset32);
Len Brown2f32edf2012-09-21 23:45:46 -04002433 break;
2434 case 'M':
2435 sscanf(optarg, "%x", &extra_msr_offset64);
Len Brown103a8fe2010-10-22 23:53:03 -04002436 break;
Len Brown889facb2012-11-08 00:48:57 -05002437 case 'R':
2438 rapl_verbose++;
2439 break;
2440 case 'T':
2441 tcc_activation_temp_override = atoi(optarg);
2442 break;
Dirk Brandewie5c56be92013-12-16 10:23:41 -08002443 case 'J':
2444 rapl_joules++;
2445 break;
2446
Len Brown103a8fe2010-10-22 23:53:03 -04002447 default:
2448 usage();
2449 }
2450 }
2451}
2452
2453int main(int argc, char **argv)
2454{
2455 cmdline(argc, argv);
2456
Len Brown889facb2012-11-08 00:48:57 -05002457 if (verbose)
Len Browne6f9bb32013-12-03 02:19:19 -05002458 fprintf(stderr, "turbostat v3.6 Dec 2, 2013"
Len Brown103a8fe2010-10-22 23:53:03 -04002459 " - Len Brown <lenb@kernel.org>\n");
Len Brown103a8fe2010-10-22 23:53:03 -04002460
2461 turbostat_init();
2462
Andy Shevchenko3b4d5c72014-01-23 17:13:15 +02002463 /* dump counters and exit */
2464 if (dump_only)
2465 return get_and_dump_counters();
2466
Len Brown103a8fe2010-10-22 23:53:03 -04002467 /*
2468 * if any params left, it must be a command to fork
2469 */
2470 if (argc - optind)
2471 return fork_it(argv + optind);
2472 else
2473 turbostat_loop();
2474
2475 return 0;
2476}