blob: 75f64e05ec30813c4ce2649ec30abf741cb97d4e [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 Browne23da032012-02-06 18:37:16 -05005 * Copyright (c) 2012 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
Len Brown9c63a652012-10-31 01:29:52 -040023#include <asm/msr.h>
Len Brown103a8fe2010-10-22 23:53:03 -040024#include <stdio.h>
25#include <unistd.h>
26#include <sys/types.h>
27#include <sys/wait.h>
28#include <sys/stat.h>
29#include <sys/resource.h>
30#include <fcntl.h>
31#include <signal.h>
32#include <sys/time.h>
33#include <stdlib.h>
34#include <dirent.h>
35#include <string.h>
36#include <ctype.h>
Len Brown88c32812012-03-29 21:44:40 -040037#include <sched.h>
Len Brown103a8fe2010-10-22 23:53:03 -040038
Len Brown103a8fe2010-10-22 23:53:03 -040039char *proc_stat = "/proc/stat";
40unsigned int interval_sec = 5; /* set with -i interval_sec */
41unsigned int verbose; /* set with -v */
Len Brown889facb2012-11-08 00:48:57 -050042unsigned int rapl_verbose; /* set with -R */
43unsigned int thermal_verbose; /* set with -T */
Len Browne23da032012-02-06 18:37:16 -050044unsigned int summary_only; /* set with -s */
Len Brown103a8fe2010-10-22 23:53:03 -040045unsigned int skip_c0;
46unsigned int skip_c1;
47unsigned int do_nhm_cstates;
48unsigned int do_snb_cstates;
49unsigned int has_aperf;
Len Brown889facb2012-11-08 00:48:57 -050050unsigned int has_epb;
Len Brown103a8fe2010-10-22 23:53:03 -040051unsigned int units = 1000000000; /* Ghz etc */
52unsigned int genuine_intel;
53unsigned int has_invariant_tsc;
54unsigned int do_nehalem_platform_info;
55unsigned int do_nehalem_turbo_ratio_limit;
Len Brown6574a5d2012-09-21 00:01:31 -040056unsigned int do_ivt_turbo_ratio_limit;
Len Brown2f32edf2012-09-21 23:45:46 -040057unsigned int extra_msr_offset32;
58unsigned int extra_msr_offset64;
Len Brown8e180f32012-09-22 01:25:08 -040059unsigned int extra_delta_offset32;
60unsigned int extra_delta_offset64;
Len Brown103a8fe2010-10-22 23:53:03 -040061double bclk;
62unsigned int show_pkg;
63unsigned int show_core;
64unsigned int show_cpu;
Len Brownc98d5d92012-06-04 00:56:40 -040065unsigned int show_pkg_only;
66unsigned int show_core_only;
67char *output_buffer, *outp;
Len Brown889facb2012-11-08 00:48:57 -050068unsigned int do_rapl;
69unsigned int do_dts;
70unsigned int do_ptm;
71unsigned int tcc_activation_temp;
72unsigned int tcc_activation_temp_override;
73double rapl_power_units, rapl_energy_units, rapl_time_units;
74double rapl_joule_counter_range;
75
76#define RAPL_PKG (1 << 0)
77#define RAPL_CORES (1 << 1)
78#define RAPL_GFX (1 << 2)
79#define RAPL_DRAM (1 << 3)
80#define RAPL_PKG_PERF_STATUS (1 << 4)
81#define RAPL_DRAM_PERF_STATUS (1 << 5)
82#define TJMAX_DEFAULT 100
83
84#define MAX(a, b) ((a) > (b) ? (a) : (b))
Len Brown103a8fe2010-10-22 23:53:03 -040085
86int aperf_mperf_unstable;
87int backwards_count;
88char *progname;
Len Brown103a8fe2010-10-22 23:53:03 -040089
Len Brownc98d5d92012-06-04 00:56:40 -040090cpu_set_t *cpu_present_set, *cpu_affinity_set;
91size_t cpu_present_setsize, cpu_affinity_setsize;
Len Brown103a8fe2010-10-22 23:53:03 -040092
Len Brownc98d5d92012-06-04 00:56:40 -040093struct thread_data {
94 unsigned long long tsc;
95 unsigned long long aperf;
96 unsigned long long mperf;
97 unsigned long long c1; /* derived */
Len Brown2f32edf2012-09-21 23:45:46 -040098 unsigned long long extra_msr64;
Len Brown8e180f32012-09-22 01:25:08 -040099 unsigned long long extra_delta64;
100 unsigned long long extra_msr32;
101 unsigned long long extra_delta32;
Len Brownc98d5d92012-06-04 00:56:40 -0400102 unsigned int cpu_id;
103 unsigned int flags;
104#define CPU_IS_FIRST_THREAD_IN_CORE 0x2
105#define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
106} *thread_even, *thread_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400107
Len Brownc98d5d92012-06-04 00:56:40 -0400108struct core_data {
109 unsigned long long c3;
110 unsigned long long c6;
111 unsigned long long c7;
Len Brown889facb2012-11-08 00:48:57 -0500112 unsigned int core_temp_c;
Len Brownc98d5d92012-06-04 00:56:40 -0400113 unsigned int core_id;
114} *core_even, *core_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400115
Len Brownc98d5d92012-06-04 00:56:40 -0400116struct pkg_data {
117 unsigned long long pc2;
118 unsigned long long pc3;
119 unsigned long long pc6;
120 unsigned long long pc7;
121 unsigned int package_id;
Len Brown889facb2012-11-08 00:48:57 -0500122 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
123 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
124 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
125 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
126 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
127 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
128 unsigned int pkg_temp_c;
129
Len Brownc98d5d92012-06-04 00:56:40 -0400130} *package_even, *package_odd;
131
132#define ODD_COUNTERS thread_odd, core_odd, package_odd
133#define EVEN_COUNTERS thread_even, core_even, package_even
134
135#define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
136 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
137 topo.num_threads_per_core + \
138 (core_no) * topo.num_threads_per_core + (thread_no))
139#define GET_CORE(core_base, core_no, pkg_no) \
140 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
141#define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
142
143struct system_summary {
144 struct thread_data threads;
145 struct core_data cores;
146 struct pkg_data packages;
147} sum, average;
148
149
150struct topo_params {
151 int num_packages;
152 int num_cpus;
153 int num_cores;
154 int max_cpu_num;
155 int num_cores_per_pkg;
156 int num_threads_per_core;
157} topo;
158
159struct timeval tv_even, tv_odd, tv_delta;
160
161void setup_all_buffers(void);
162
163int cpu_is_not_present(int cpu)
Len Brownd15cf7c2012-06-03 23:24:00 -0400164{
Len Brownc98d5d92012-06-04 00:56:40 -0400165 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brownd15cf7c2012-06-03 23:24:00 -0400166}
Len Brown88c32812012-03-29 21:44:40 -0400167/*
Len Brownc98d5d92012-06-04 00:56:40 -0400168 * run func(thread, core, package) in topology order
169 * skip non-present cpus
Len Brown88c32812012-03-29 21:44:40 -0400170 */
Len Brownd15cf7c2012-06-03 23:24:00 -0400171
Len Brownc98d5d92012-06-04 00:56:40 -0400172int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
173 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
Len Brown88c32812012-03-29 21:44:40 -0400174{
Len Brownc98d5d92012-06-04 00:56:40 -0400175 int retval, pkg_no, core_no, thread_no;
176
177 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
178 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
179 for (thread_no = 0; thread_no <
180 topo.num_threads_per_core; ++thread_no) {
181 struct thread_data *t;
182 struct core_data *c;
183 struct pkg_data *p;
184
185 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
186
187 if (cpu_is_not_present(t->cpu_id))
188 continue;
189
190 c = GET_CORE(core_base, core_no, pkg_no);
191 p = GET_PKG(pkg_base, pkg_no);
192
193 retval = func(t, c, p);
194 if (retval)
195 return retval;
196 }
197 }
198 }
199 return 0;
Len Brown88c32812012-03-29 21:44:40 -0400200}
201
202int cpu_migrate(int cpu)
203{
Len Brownc98d5d92012-06-04 00:56:40 -0400204 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
205 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
206 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
Len Brown88c32812012-03-29 21:44:40 -0400207 return -1;
208 else
209 return 0;
210}
211
Len Brown15aaa342012-03-29 22:19:58 -0400212int get_msr(int cpu, off_t offset, unsigned long long *msr)
Len Brown103a8fe2010-10-22 23:53:03 -0400213{
214 ssize_t retval;
Len Brown103a8fe2010-10-22 23:53:03 -0400215 char pathname[32];
216 int fd;
217
218 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
219 fd = open(pathname, O_RDONLY);
Len Brown15aaa342012-03-29 22:19:58 -0400220 if (fd < 0)
221 return -1;
Len Brown103a8fe2010-10-22 23:53:03 -0400222
Len Brown15aaa342012-03-29 22:19:58 -0400223 retval = pread(fd, msr, sizeof *msr, offset);
Len Brown103a8fe2010-10-22 23:53:03 -0400224 close(fd);
Len Brown15aaa342012-03-29 22:19:58 -0400225
Len Brownd91bb172012-11-01 00:08:19 -0400226 if (retval != sizeof *msr) {
227 fprintf(stderr, "%s offset 0x%zx read failed\n", pathname, offset);
Len Brown15aaa342012-03-29 22:19:58 -0400228 return -1;
Len Brownd91bb172012-11-01 00:08:19 -0400229 }
Len Brown15aaa342012-03-29 22:19:58 -0400230
231 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400232}
233
Len Browna829eb42011-02-10 23:36:34 -0500234void print_header(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400235{
236 if (show_pkg)
Len Brownc98d5d92012-06-04 00:56:40 -0400237 outp += sprintf(outp, "pk");
Len Browne23da032012-02-06 18:37:16 -0500238 if (show_pkg)
Len Brownc98d5d92012-06-04 00:56:40 -0400239 outp += sprintf(outp, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400240 if (show_core)
Len Brownc98d5d92012-06-04 00:56:40 -0400241 outp += sprintf(outp, "cor");
Len Brown103a8fe2010-10-22 23:53:03 -0400242 if (show_cpu)
Len Brownc98d5d92012-06-04 00:56:40 -0400243 outp += sprintf(outp, " CPU");
Len Browne23da032012-02-06 18:37:16 -0500244 if (show_pkg || show_core || show_cpu)
Len Brownc98d5d92012-06-04 00:56:40 -0400245 outp += sprintf(outp, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400246 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400247 outp += sprintf(outp, " %%c0");
Len Brown103a8fe2010-10-22 23:53:03 -0400248 if (has_aperf)
Len Brownc98d5d92012-06-04 00:56:40 -0400249 outp += sprintf(outp, " GHz");
250 outp += sprintf(outp, " TSC");
Len Brown8e180f32012-09-22 01:25:08 -0400251 if (extra_delta_offset32)
Len Brownf9240812012-10-06 15:26:31 -0400252 outp += sprintf(outp, " count 0x%03X", extra_delta_offset32);
Len Brown8e180f32012-09-22 01:25:08 -0400253 if (extra_delta_offset64)
Len Brownf9240812012-10-06 15:26:31 -0400254 outp += sprintf(outp, " COUNT 0x%03X", extra_delta_offset64);
Len Brown2f32edf2012-09-21 23:45:46 -0400255 if (extra_msr_offset32)
Len Brown8e180f32012-09-22 01:25:08 -0400256 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset32);
Len Brown2f32edf2012-09-21 23:45:46 -0400257 if (extra_msr_offset64)
Len Brown8e180f32012-09-22 01:25:08 -0400258 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset64);
Len Brown103a8fe2010-10-22 23:53:03 -0400259 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400260 outp += sprintf(outp, " %%c1");
Len Brown103a8fe2010-10-22 23:53:03 -0400261 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400262 outp += sprintf(outp, " %%c3");
Len Brown103a8fe2010-10-22 23:53:03 -0400263 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400264 outp += sprintf(outp, " %%c6");
Len Brown103a8fe2010-10-22 23:53:03 -0400265 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400266 outp += sprintf(outp, " %%c7");
Len Brown889facb2012-11-08 00:48:57 -0500267
268 if (do_dts)
269 outp += sprintf(outp, " CTMP");
270 if (do_ptm)
271 outp += sprintf(outp, " PTMP");
272
Len Brown103a8fe2010-10-22 23:53:03 -0400273 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400274 outp += sprintf(outp, " %%pc2");
Len Brown103a8fe2010-10-22 23:53:03 -0400275 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400276 outp += sprintf(outp, " %%pc3");
Len Brown103a8fe2010-10-22 23:53:03 -0400277 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400278 outp += sprintf(outp, " %%pc6");
Len Brown103a8fe2010-10-22 23:53:03 -0400279 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400280 outp += sprintf(outp, " %%pc7");
Len Brown103a8fe2010-10-22 23:53:03 -0400281
Len Brown889facb2012-11-08 00:48:57 -0500282 if (do_rapl & RAPL_PKG)
283 outp += sprintf(outp, " Pkg_W");
284 if (do_rapl & RAPL_CORES)
285 outp += sprintf(outp, " Cor_W");
286 if (do_rapl & RAPL_GFX)
287 outp += sprintf(outp, " GFX_W");
288 if (do_rapl & RAPL_DRAM)
289 outp += sprintf(outp, " RAM_W");
290 if (do_rapl & RAPL_PKG_PERF_STATUS)
291 outp += sprintf(outp, " PKG_%%");
292 if (do_rapl & RAPL_DRAM_PERF_STATUS)
293 outp += sprintf(outp, " RAM_%%");
294
Len Brownc98d5d92012-06-04 00:56:40 -0400295 outp += sprintf(outp, "\n");
Len Brown103a8fe2010-10-22 23:53:03 -0400296}
297
Len Brownc98d5d92012-06-04 00:56:40 -0400298int dump_counters(struct thread_data *t, struct core_data *c,
299 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400300{
Len Brownc98d5d92012-06-04 00:56:40 -0400301 fprintf(stderr, "t %p, c %p, p %p\n", t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400302
Len Brownc98d5d92012-06-04 00:56:40 -0400303 if (t) {
304 fprintf(stderr, "CPU: %d flags 0x%x\n", t->cpu_id, t->flags);
305 fprintf(stderr, "TSC: %016llX\n", t->tsc);
306 fprintf(stderr, "aperf: %016llX\n", t->aperf);
307 fprintf(stderr, "mperf: %016llX\n", t->mperf);
308 fprintf(stderr, "c1: %016llX\n", t->c1);
Len Brown8e180f32012-09-22 01:25:08 -0400309 fprintf(stderr, "msr0x%x: %08llX\n",
310 extra_delta_offset32, t->extra_delta32);
311 fprintf(stderr, "msr0x%x: %016llX\n",
312 extra_delta_offset64, t->extra_delta64);
313 fprintf(stderr, "msr0x%x: %08llX\n",
Len Brown2f32edf2012-09-21 23:45:46 -0400314 extra_msr_offset32, t->extra_msr32);
Len Brownc98d5d92012-06-04 00:56:40 -0400315 fprintf(stderr, "msr0x%x: %016llX\n",
Len Brown2f32edf2012-09-21 23:45:46 -0400316 extra_msr_offset64, t->extra_msr64);
Len Brownc98d5d92012-06-04 00:56:40 -0400317 }
Len Brown103a8fe2010-10-22 23:53:03 -0400318
Len Brownc98d5d92012-06-04 00:56:40 -0400319 if (c) {
320 fprintf(stderr, "core: %d\n", c->core_id);
321 fprintf(stderr, "c3: %016llX\n", c->c3);
322 fprintf(stderr, "c6: %016llX\n", c->c6);
323 fprintf(stderr, "c7: %016llX\n", c->c7);
Len Brown889facb2012-11-08 00:48:57 -0500324 fprintf(stderr, "DTS: %dC\n", c->core_temp_c);
Len Brownc98d5d92012-06-04 00:56:40 -0400325 }
326
327 if (p) {
328 fprintf(stderr, "package: %d\n", p->package_id);
329 fprintf(stderr, "pc2: %016llX\n", p->pc2);
330 fprintf(stderr, "pc3: %016llX\n", p->pc3);
331 fprintf(stderr, "pc6: %016llX\n", p->pc6);
332 fprintf(stderr, "pc7: %016llX\n", p->pc7);
Len Brown889facb2012-11-08 00:48:57 -0500333 fprintf(stderr, "Joules PKG: %0X\n", p->energy_pkg);
334 fprintf(stderr, "Joules COR: %0X\n", p->energy_cores);
335 fprintf(stderr, "Joules GFX: %0X\n", p->energy_gfx);
336 fprintf(stderr, "Joules RAM: %0X\n", p->energy_dram);
337 fprintf(stderr, "Throttle PKG: %0X\n", p->rapl_pkg_perf_status);
338 fprintf(stderr, "Throttle RAM: %0X\n", p->rapl_dram_perf_status);
339 fprintf(stderr, "PTM: %dC\n", p->pkg_temp_c);
Len Brownc98d5d92012-06-04 00:56:40 -0400340 }
341 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400342}
343
Len Browne23da032012-02-06 18:37:16 -0500344/*
345 * column formatting convention & formats
346 * package: "pk" 2 columns %2d
347 * core: "cor" 3 columns %3d
348 * CPU: "CPU" 3 columns %3d
Len Brown889facb2012-11-08 00:48:57 -0500349 * Pkg_W: %6.2
350 * Cor_W: %6.2
351 * GFX_W: %5.2
352 * RAM_W: %5.2
Len Browne23da032012-02-06 18:37:16 -0500353 * GHz: "GHz" 3 columns %3.2
354 * TSC: "TSC" 3 columns %3.2
355 * percentage " %pc3" %6.2
Len Brown889facb2012-11-08 00:48:57 -0500356 * Perf Status percentage: %5.2
357 * "CTMP" 4 columns %4d
Len Browne23da032012-02-06 18:37:16 -0500358 */
Len Brownc98d5d92012-06-04 00:56:40 -0400359int format_counters(struct thread_data *t, struct core_data *c,
360 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400361{
362 double interval_float;
Len Brown889facb2012-11-08 00:48:57 -0500363 char *fmt5, *fmt6;
Len Brown103a8fe2010-10-22 23:53:03 -0400364
Len Brownc98d5d92012-06-04 00:56:40 -0400365 /* if showing only 1st thread in core and this isn't one, bail out */
366 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
367 return 0;
368
369 /* if showing only 1st thread in pkg and this isn't one, bail out */
370 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
371 return 0;
372
Len Brown103a8fe2010-10-22 23:53:03 -0400373 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
374
Len Brownc98d5d92012-06-04 00:56:40 -0400375 /* topo columns, print blanks on 1st (average) line */
376 if (t == &average.threads) {
Len Brown103a8fe2010-10-22 23:53:03 -0400377 if (show_pkg)
Len Brownc98d5d92012-06-04 00:56:40 -0400378 outp += sprintf(outp, " ");
Len Browne23da032012-02-06 18:37:16 -0500379 if (show_pkg && show_core)
Len Brownc98d5d92012-06-04 00:56:40 -0400380 outp += sprintf(outp, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400381 if (show_core)
Len Brownc98d5d92012-06-04 00:56:40 -0400382 outp += sprintf(outp, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400383 if (show_cpu)
Len Brownc98d5d92012-06-04 00:56:40 -0400384 outp += sprintf(outp, " " " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400385 } else {
Len Brownc98d5d92012-06-04 00:56:40 -0400386 if (show_pkg) {
387 if (p)
388 outp += sprintf(outp, "%2d", p->package_id);
389 else
390 outp += sprintf(outp, " ");
391 }
Len Browne23da032012-02-06 18:37:16 -0500392 if (show_pkg && show_core)
Len Brownc98d5d92012-06-04 00:56:40 -0400393 outp += sprintf(outp, " ");
394 if (show_core) {
395 if (c)
396 outp += sprintf(outp, "%3d", c->core_id);
397 else
398 outp += sprintf(outp, " ");
399 }
Len Brown103a8fe2010-10-22 23:53:03 -0400400 if (show_cpu)
Len Brownc98d5d92012-06-04 00:56:40 -0400401 outp += sprintf(outp, " %3d", t->cpu_id);
Len Brown103a8fe2010-10-22 23:53:03 -0400402 }
Len Brown103a8fe2010-10-22 23:53:03 -0400403 /* %c0 */
404 if (do_nhm_cstates) {
Len Browne23da032012-02-06 18:37:16 -0500405 if (show_pkg || show_core || show_cpu)
Len Brownc98d5d92012-06-04 00:56:40 -0400406 outp += sprintf(outp, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400407 if (!skip_c0)
Len Brownc98d5d92012-06-04 00:56:40 -0400408 outp += sprintf(outp, "%6.2f", 100.0 * t->mperf/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400409 else
Len Brownc98d5d92012-06-04 00:56:40 -0400410 outp += sprintf(outp, " ****");
Len Brown103a8fe2010-10-22 23:53:03 -0400411 }
412
413 /* GHz */
414 if (has_aperf) {
415 if (!aperf_mperf_unstable) {
Len Brownc98d5d92012-06-04 00:56:40 -0400416 outp += sprintf(outp, " %3.2f",
417 1.0 * t->tsc / units * t->aperf /
418 t->mperf / interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400419 } else {
Len Brownc98d5d92012-06-04 00:56:40 -0400420 if (t->aperf > t->tsc || t->mperf > t->tsc) {
421 outp += sprintf(outp, " ***");
Len Brown103a8fe2010-10-22 23:53:03 -0400422 } else {
Len Brownc98d5d92012-06-04 00:56:40 -0400423 outp += sprintf(outp, "%3.1f*",
424 1.0 * t->tsc /
425 units * t->aperf /
426 t->mperf / interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400427 }
428 }
429 }
430
431 /* TSC */
Len Brownc98d5d92012-06-04 00:56:40 -0400432 outp += sprintf(outp, "%5.2f", 1.0 * t->tsc/units/interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400433
Len Brown8e180f32012-09-22 01:25:08 -0400434 /* delta */
435 if (extra_delta_offset32)
436 outp += sprintf(outp, " %11llu", t->extra_delta32);
437
438 /* DELTA */
439 if (extra_delta_offset64)
440 outp += sprintf(outp, " %11llu", t->extra_delta64);
Len Brown2f32edf2012-09-21 23:45:46 -0400441 /* msr */
442 if (extra_msr_offset32)
Len Brown8e180f32012-09-22 01:25:08 -0400443 outp += sprintf(outp, " 0x%08llx", t->extra_msr32);
Len Brown2f32edf2012-09-21 23:45:46 -0400444
Len Brown130ff302012-09-21 22:56:06 -0400445 /* MSR */
Len Brown2f32edf2012-09-21 23:45:46 -0400446 if (extra_msr_offset64)
447 outp += sprintf(outp, " 0x%016llx", t->extra_msr64);
Len Brown130ff302012-09-21 22:56:06 -0400448
Len Brown103a8fe2010-10-22 23:53:03 -0400449 if (do_nhm_cstates) {
450 if (!skip_c1)
Len Brownc98d5d92012-06-04 00:56:40 -0400451 outp += sprintf(outp, " %6.2f", 100.0 * t->c1/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400452 else
Len Brownc98d5d92012-06-04 00:56:40 -0400453 outp += sprintf(outp, " ****");
Len Brown103a8fe2010-10-22 23:53:03 -0400454 }
Len Brownc98d5d92012-06-04 00:56:40 -0400455
456 /* print per-core data only for 1st thread in core */
457 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
458 goto done;
459
Len Brown103a8fe2010-10-22 23:53:03 -0400460 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400461 outp += sprintf(outp, " %6.2f", 100.0 * c->c3/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400462 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400463 outp += sprintf(outp, " %6.2f", 100.0 * c->c6/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400464 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400465 outp += sprintf(outp, " %6.2f", 100.0 * c->c7/t->tsc);
466
Len Brown889facb2012-11-08 00:48:57 -0500467 if (do_dts)
468 outp += sprintf(outp, " %4d", c->core_temp_c);
469
Len Brownc98d5d92012-06-04 00:56:40 -0400470 /* print per-package data only for 1st core in package */
471 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
472 goto done;
473
Len Brown889facb2012-11-08 00:48:57 -0500474 if (do_ptm)
475 outp += sprintf(outp, " %4d", p->pkg_temp_c);
476
Len Brown103a8fe2010-10-22 23:53:03 -0400477 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400478 outp += sprintf(outp, " %6.2f", 100.0 * p->pc2/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400479 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400480 outp += sprintf(outp, " %6.2f", 100.0 * p->pc3/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400481 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400482 outp += sprintf(outp, " %6.2f", 100.0 * p->pc6/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400483 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400484 outp += sprintf(outp, " %6.2f", 100.0 * p->pc7/t->tsc);
Len Brown889facb2012-11-08 00:48:57 -0500485
486 /*
487 * If measurement interval exceeds minimum RAPL Joule Counter range,
488 * indicate that results are suspect by printing "**" in fraction place.
489 */
490 if (interval_float < rapl_joule_counter_range) {
491 fmt5 = " %5.2f";
492 fmt6 = " %6.2f";
493 } else {
494 fmt5 = " %3.0f**";
495 fmt6 = " %4.0f**";
496 }
497
498 if (do_rapl & RAPL_PKG)
499 outp += sprintf(outp, fmt6, p->energy_pkg * rapl_energy_units / interval_float);
500 if (do_rapl & RAPL_CORES)
501 outp += sprintf(outp, fmt6, p->energy_cores * rapl_energy_units / interval_float);
502 if (do_rapl & RAPL_GFX)
503 outp += sprintf(outp, fmt5, p->energy_gfx * rapl_energy_units / interval_float);
504 if (do_rapl & RAPL_DRAM)
505 outp += sprintf(outp, fmt5, p->energy_dram * rapl_energy_units / interval_float);
506 if (do_rapl & RAPL_PKG_PERF_STATUS )
507 outp += sprintf(outp, fmt5, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
508 if (do_rapl & RAPL_DRAM_PERF_STATUS )
509 outp += sprintf(outp, fmt5, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
510
Len Brownc98d5d92012-06-04 00:56:40 -0400511done:
Len Brownc98d5d92012-06-04 00:56:40 -0400512 outp += sprintf(outp, "\n");
513
514 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400515}
516
Len Brownc98d5d92012-06-04 00:56:40 -0400517void flush_stdout()
Len Brown103a8fe2010-10-22 23:53:03 -0400518{
Len Brownc98d5d92012-06-04 00:56:40 -0400519 fputs(output_buffer, stdout);
Len Brownddac0d62012-11-30 01:01:40 -0500520 fflush(stdout);
Len Brownc98d5d92012-06-04 00:56:40 -0400521 outp = output_buffer;
522}
523void flush_stderr()
524{
525 fputs(output_buffer, stderr);
526 outp = output_buffer;
527}
528void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
529{
Len Browne23da032012-02-06 18:37:16 -0500530 static int printed;
Len Brown103a8fe2010-10-22 23:53:03 -0400531
Len Browne23da032012-02-06 18:37:16 -0500532 if (!printed || !summary_only)
533 print_header();
Len Brown103a8fe2010-10-22 23:53:03 -0400534
Len Brownc98d5d92012-06-04 00:56:40 -0400535 if (topo.num_cpus > 1)
536 format_counters(&average.threads, &average.cores,
537 &average.packages);
Len Brown103a8fe2010-10-22 23:53:03 -0400538
Len Browne23da032012-02-06 18:37:16 -0500539 printed = 1;
540
541 if (summary_only)
542 return;
543
Len Brownc98d5d92012-06-04 00:56:40 -0400544 for_all_cpus(format_counters, t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400545}
546
Len Brown889facb2012-11-08 00:48:57 -0500547#define DELTA_WRAP32(new, old) \
548 if (new > old) { \
549 old = new - old; \
550 } else { \
551 old = 0x100000000 + new - old; \
552 }
553
Len Brownc98d5d92012-06-04 00:56:40 -0400554void
555delta_package(struct pkg_data *new, struct pkg_data *old)
Len Brown103a8fe2010-10-22 23:53:03 -0400556{
Len Brownc98d5d92012-06-04 00:56:40 -0400557 old->pc2 = new->pc2 - old->pc2;
558 old->pc3 = new->pc3 - old->pc3;
559 old->pc6 = new->pc6 - old->pc6;
560 old->pc7 = new->pc7 - old->pc7;
Len Brown889facb2012-11-08 00:48:57 -0500561 old->pkg_temp_c = new->pkg_temp_c;
562
563 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
564 DELTA_WRAP32(new->energy_cores, old->energy_cores);
565 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
566 DELTA_WRAP32(new->energy_dram, old->energy_dram);
567 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
568 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
Len Brownc98d5d92012-06-04 00:56:40 -0400569}
Len Brown103a8fe2010-10-22 23:53:03 -0400570
Len Brownc98d5d92012-06-04 00:56:40 -0400571void
572delta_core(struct core_data *new, struct core_data *old)
573{
574 old->c3 = new->c3 - old->c3;
575 old->c6 = new->c6 - old->c6;
576 old->c7 = new->c7 - old->c7;
Len Brown889facb2012-11-08 00:48:57 -0500577 old->core_temp_c = new->core_temp_c;
Len Brownc98d5d92012-06-04 00:56:40 -0400578}
Len Brown103a8fe2010-10-22 23:53:03 -0400579
Len Brownc3ae3312012-06-13 21:31:46 -0400580/*
581 * old = new - old
582 */
Len Brownc98d5d92012-06-04 00:56:40 -0400583void
584delta_thread(struct thread_data *new, struct thread_data *old,
585 struct core_data *core_delta)
586{
587 old->tsc = new->tsc - old->tsc;
Len Brown103a8fe2010-10-22 23:53:03 -0400588
Len Brownc98d5d92012-06-04 00:56:40 -0400589 /* check for TSC < 1 Mcycles over interval */
590 if (old->tsc < (1000 * 1000)) {
591 fprintf(stderr, "Insanely slow TSC rate, TSC stops in idle?\n");
592 fprintf(stderr, "You can disable all c-states by booting with \"idle=poll\"\n");
593 fprintf(stderr, "or just the deep ones with \"processor.max_cstate=1\"\n");
594 exit(-3);
595 }
Len Brown103a8fe2010-10-22 23:53:03 -0400596
Len Brownc98d5d92012-06-04 00:56:40 -0400597 old->c1 = new->c1 - old->c1;
Len Brown103a8fe2010-10-22 23:53:03 -0400598
Len Brownc98d5d92012-06-04 00:56:40 -0400599 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
600 old->aperf = new->aperf - old->aperf;
601 old->mperf = new->mperf - old->mperf;
602 } else {
Len Brown103a8fe2010-10-22 23:53:03 -0400603
Len Brownc98d5d92012-06-04 00:56:40 -0400604 if (!aperf_mperf_unstable) {
605 fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname);
606 fprintf(stderr, "* Frequency results do not cover entire interval *\n");
607 fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n");
608
609 aperf_mperf_unstable = 1;
610 }
Len Brown103a8fe2010-10-22 23:53:03 -0400611 /*
Len Brownc98d5d92012-06-04 00:56:40 -0400612 * mperf delta is likely a huge "positive" number
613 * can not use it for calculating c0 time
Len Brown103a8fe2010-10-22 23:53:03 -0400614 */
Len Brownc98d5d92012-06-04 00:56:40 -0400615 skip_c0 = 1;
616 skip_c1 = 1;
617 }
Len Brown103a8fe2010-10-22 23:53:03 -0400618
Len Brown103a8fe2010-10-22 23:53:03 -0400619
Len Brownc98d5d92012-06-04 00:56:40 -0400620 /*
Len Brownc3ae3312012-06-13 21:31:46 -0400621 * As counter collection is not atomic,
622 * it is possible for mperf's non-halted cycles + idle states
Len Brownc98d5d92012-06-04 00:56:40 -0400623 * to exceed TSC's all cycles: show c1 = 0% in that case.
624 */
Len Brownc3ae3312012-06-13 21:31:46 -0400625 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
Len Brownc98d5d92012-06-04 00:56:40 -0400626 old->c1 = 0;
627 else {
628 /* normal case, derive c1 */
629 old->c1 = old->tsc - old->mperf - core_delta->c3
630 - core_delta->c6 - core_delta->c7;
631 }
Len Brownc3ae3312012-06-13 21:31:46 -0400632
Len Brownc98d5d92012-06-04 00:56:40 -0400633 if (old->mperf == 0) {
Len Brownc3ae3312012-06-13 21:31:46 -0400634 if (verbose > 1) fprintf(stderr, "cpu%d MPERF 0!\n", old->cpu_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400635 old->mperf = 1; /* divide by 0 protection */
636 }
637
Len Brown8e180f32012-09-22 01:25:08 -0400638 old->extra_delta32 = new->extra_delta32 - old->extra_delta32;
639 old->extra_delta32 &= 0xFFFFFFFF;
640
641 old->extra_delta64 = new->extra_delta64 - old->extra_delta64;
642
Len Brownc98d5d92012-06-04 00:56:40 -0400643 /*
Len Brown8e180f32012-09-22 01:25:08 -0400644 * Extra MSR is just a snapshot, simply copy latest w/o subtracting
Len Brownc98d5d92012-06-04 00:56:40 -0400645 */
Len Brown2f32edf2012-09-21 23:45:46 -0400646 old->extra_msr32 = new->extra_msr32;
647 old->extra_msr64 = new->extra_msr64;
Len Brownc98d5d92012-06-04 00:56:40 -0400648}
649
650int delta_cpu(struct thread_data *t, struct core_data *c,
651 struct pkg_data *p, struct thread_data *t2,
652 struct core_data *c2, struct pkg_data *p2)
653{
654 /* calculate core delta only for 1st thread in core */
655 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
656 delta_core(c, c2);
657
658 /* always calculate thread delta */
659 delta_thread(t, t2, c2); /* c2 is core delta */
660
661 /* calculate package delta only for 1st core in package */
662 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
663 delta_package(p, p2);
664
665 return 0;
666}
667
668void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
669{
670 t->tsc = 0;
671 t->aperf = 0;
672 t->mperf = 0;
673 t->c1 = 0;
674
Len Brown8e180f32012-09-22 01:25:08 -0400675 t->extra_delta32 = 0;
676 t->extra_delta64 = 0;
677
Len Brownc98d5d92012-06-04 00:56:40 -0400678 /* tells format_counters to dump all fields from this set */
679 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
680
681 c->c3 = 0;
682 c->c6 = 0;
683 c->c7 = 0;
Len Brown889facb2012-11-08 00:48:57 -0500684 c->core_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -0400685
686 p->pc2 = 0;
687 p->pc3 = 0;
688 p->pc6 = 0;
689 p->pc7 = 0;
Len Brown889facb2012-11-08 00:48:57 -0500690
691 p->energy_pkg = 0;
692 p->energy_dram = 0;
693 p->energy_cores = 0;
694 p->energy_gfx = 0;
695 p->rapl_pkg_perf_status = 0;
696 p->rapl_dram_perf_status = 0;
697 p->pkg_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -0400698}
699int sum_counters(struct thread_data *t, struct core_data *c,
700 struct pkg_data *p)
701{
702 average.threads.tsc += t->tsc;
703 average.threads.aperf += t->aperf;
704 average.threads.mperf += t->mperf;
705 average.threads.c1 += t->c1;
706
Len Brown8e180f32012-09-22 01:25:08 -0400707 average.threads.extra_delta32 += t->extra_delta32;
708 average.threads.extra_delta64 += t->extra_delta64;
709
Len Brownc98d5d92012-06-04 00:56:40 -0400710 /* sum per-core values only for 1st thread in core */
711 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
712 return 0;
713
714 average.cores.c3 += c->c3;
715 average.cores.c6 += c->c6;
716 average.cores.c7 += c->c7;
717
Len Brown889facb2012-11-08 00:48:57 -0500718 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
719
Len Brownc98d5d92012-06-04 00:56:40 -0400720 /* sum per-pkg values only for 1st core in pkg */
721 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
722 return 0;
723
724 average.packages.pc2 += p->pc2;
725 average.packages.pc3 += p->pc3;
726 average.packages.pc6 += p->pc6;
727 average.packages.pc7 += p->pc7;
728
Len Brown889facb2012-11-08 00:48:57 -0500729 average.packages.energy_pkg += p->energy_pkg;
730 average.packages.energy_dram += p->energy_dram;
731 average.packages.energy_cores += p->energy_cores;
732 average.packages.energy_gfx += p->energy_gfx;
733
734 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
735
736 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
737 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
Len Brownc98d5d92012-06-04 00:56:40 -0400738 return 0;
739}
740/*
741 * sum the counters for all cpus in the system
742 * compute the weighted average
743 */
744void compute_average(struct thread_data *t, struct core_data *c,
745 struct pkg_data *p)
746{
747 clear_counters(&average.threads, &average.cores, &average.packages);
748
749 for_all_cpus(sum_counters, t, c, p);
750
751 average.threads.tsc /= topo.num_cpus;
752 average.threads.aperf /= topo.num_cpus;
753 average.threads.mperf /= topo.num_cpus;
754 average.threads.c1 /= topo.num_cpus;
755
Len Brown8e180f32012-09-22 01:25:08 -0400756 average.threads.extra_delta32 /= topo.num_cpus;
757 average.threads.extra_delta32 &= 0xFFFFFFFF;
758
759 average.threads.extra_delta64 /= topo.num_cpus;
760
Len Brownc98d5d92012-06-04 00:56:40 -0400761 average.cores.c3 /= topo.num_cores;
762 average.cores.c6 /= topo.num_cores;
763 average.cores.c7 /= topo.num_cores;
764
765 average.packages.pc2 /= topo.num_packages;
766 average.packages.pc3 /= topo.num_packages;
767 average.packages.pc6 /= topo.num_packages;
768 average.packages.pc7 /= topo.num_packages;
769}
770
771static unsigned long long rdtsc(void)
772{
773 unsigned int low, high;
774
775 asm volatile("rdtsc" : "=a" (low), "=d" (high));
776
777 return low | ((unsigned long long)high) << 32;
778}
779
780
781/*
782 * get_counters(...)
783 * migrate to cpu
784 * acquire and record local counters for that cpu
785 */
786int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
787{
788 int cpu = t->cpu_id;
Len Brown889facb2012-11-08 00:48:57 -0500789 unsigned long long msr;
Len Brownc98d5d92012-06-04 00:56:40 -0400790
Len Browne52966c2012-11-08 22:38:05 -0500791 if (cpu_migrate(cpu)) {
792 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
Len Brownc98d5d92012-06-04 00:56:40 -0400793 return -1;
Len Browne52966c2012-11-08 22:38:05 -0500794 }
Len Brownc98d5d92012-06-04 00:56:40 -0400795
796 t->tsc = rdtsc(); /* we are running on local CPU of interest */
797
798 if (has_aperf) {
Len Brown9c63a652012-10-31 01:29:52 -0400799 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
Len Brownc98d5d92012-06-04 00:56:40 -0400800 return -3;
Len Brown9c63a652012-10-31 01:29:52 -0400801 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
Len Brownc98d5d92012-06-04 00:56:40 -0400802 return -4;
803 }
804
Len Brown8e180f32012-09-22 01:25:08 -0400805 if (extra_delta_offset32) {
Len Brown889facb2012-11-08 00:48:57 -0500806 if (get_msr(cpu, extra_delta_offset32, &msr))
Len Brown2f32edf2012-09-21 23:45:46 -0400807 return -5;
Len Brown889facb2012-11-08 00:48:57 -0500808 t->extra_delta32 = msr & 0xFFFFFFFF;
Len Brown8e180f32012-09-22 01:25:08 -0400809 }
810
811 if (extra_delta_offset64)
812 if (get_msr(cpu, extra_delta_offset64, &t->extra_delta64))
813 return -5;
814
815 if (extra_msr_offset32) {
Len Brown889facb2012-11-08 00:48:57 -0500816 if (get_msr(cpu, extra_msr_offset32, &msr))
Len Brown8e180f32012-09-22 01:25:08 -0400817 return -5;
Len Brown889facb2012-11-08 00:48:57 -0500818 t->extra_msr32 = msr & 0xFFFFFFFF;
Len Brown8e180f32012-09-22 01:25:08 -0400819 }
Len Brown2f32edf2012-09-21 23:45:46 -0400820
821 if (extra_msr_offset64)
822 if (get_msr(cpu, extra_msr_offset64, &t->extra_msr64))
Len Brownc98d5d92012-06-04 00:56:40 -0400823 return -5;
824
825 /* collect core counters only for 1st thread in core */
826 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
827 return 0;
828
829 if (do_nhm_cstates) {
830 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
831 return -6;
832 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
833 return -7;
834 }
835
836 if (do_snb_cstates)
837 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
838 return -8;
839
Len Brown889facb2012-11-08 00:48:57 -0500840 if (do_dts) {
841 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
842 return -9;
843 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
844 }
845
846
Len Brownc98d5d92012-06-04 00:56:40 -0400847 /* collect package counters only for 1st core in package */
848 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
849 return 0;
850
851 if (do_nhm_cstates) {
852 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
853 return -9;
854 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
855 return -10;
856 }
857 if (do_snb_cstates) {
858 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
859 return -11;
860 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
861 return -12;
Len Brown103a8fe2010-10-22 23:53:03 -0400862 }
Len Brown889facb2012-11-08 00:48:57 -0500863 if (do_rapl & RAPL_PKG) {
864 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
865 return -13;
866 p->energy_pkg = msr & 0xFFFFFFFF;
867 }
868 if (do_rapl & RAPL_CORES) {
869 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
870 return -14;
871 p->energy_cores = msr & 0xFFFFFFFF;
872 }
873 if (do_rapl & RAPL_DRAM) {
874 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
875 return -15;
876 p->energy_dram = msr & 0xFFFFFFFF;
877 }
878 if (do_rapl & RAPL_GFX) {
879 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
880 return -16;
881 p->energy_gfx = msr & 0xFFFFFFFF;
882 }
883 if (do_rapl & RAPL_PKG_PERF_STATUS) {
884 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
885 return -16;
886 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
887 }
888 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
889 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
890 return -16;
891 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
892 }
893 if (do_ptm) {
894 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
895 return -17;
896 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
897 }
Len Brown103a8fe2010-10-22 23:53:03 -0400898 return 0;
899}
900
Len Brownc98d5d92012-06-04 00:56:40 -0400901void print_verbose_header(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400902{
903 unsigned long long msr;
904 unsigned int ratio;
905
906 if (!do_nehalem_platform_info)
907 return;
908
Len Brown9c63a652012-10-31 01:29:52 -0400909 get_msr(0, MSR_NHM_PLATFORM_INFO, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -0400910
Len Brown67920412013-01-31 15:22:15 -0500911 fprintf(stderr, "cpu0: MSR_NHM_PLATFORM_INFO: 0x%08llx\n", msr);
Len Brown6574a5d2012-09-21 00:01:31 -0400912
Len Brown103a8fe2010-10-22 23:53:03 -0400913 ratio = (msr >> 40) & 0xFF;
914 fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency\n",
915 ratio, bclk, ratio * bclk);
916
917 ratio = (msr >> 8) & 0xFF;
918 fprintf(stderr, "%d * %.0f = %.0f MHz TSC frequency\n",
919 ratio, bclk, ratio * bclk);
920
Len Brown67920412013-01-31 15:22:15 -0500921 get_msr(0, MSR_IA32_POWER_CTL, &msr);
922 fprintf(stderr, "cpu0: MSR_IA32_POWER_CTL: 0x%08llx (C1E: %sabled)\n",
923 msr, msr & 0x2 ? "EN" : "DIS");
924
Len Brown6574a5d2012-09-21 00:01:31 -0400925 if (!do_ivt_turbo_ratio_limit)
926 goto print_nhm_turbo_ratio_limits;
927
928 get_msr(0, MSR_IVT_TURBO_RATIO_LIMIT, &msr);
929
Len Brown67920412013-01-31 15:22:15 -0500930 fprintf(stderr, "cpu0: MSR_IVT_TURBO_RATIO_LIMIT: 0x%08llx\n", msr);
Len Brown6574a5d2012-09-21 00:01:31 -0400931
932 ratio = (msr >> 56) & 0xFF;
933 if (ratio)
934 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 16 active cores\n",
935 ratio, bclk, ratio * bclk);
936
937 ratio = (msr >> 48) & 0xFF;
938 if (ratio)
939 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 15 active cores\n",
940 ratio, bclk, ratio * bclk);
941
942 ratio = (msr >> 40) & 0xFF;
943 if (ratio)
944 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 14 active cores\n",
945 ratio, bclk, ratio * bclk);
946
947 ratio = (msr >> 32) & 0xFF;
948 if (ratio)
949 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 13 active cores\n",
950 ratio, bclk, ratio * bclk);
951
952 ratio = (msr >> 24) & 0xFF;
953 if (ratio)
954 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 12 active cores\n",
955 ratio, bclk, ratio * bclk);
956
957 ratio = (msr >> 16) & 0xFF;
958 if (ratio)
959 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 11 active cores\n",
960 ratio, bclk, ratio * bclk);
961
962 ratio = (msr >> 8) & 0xFF;
963 if (ratio)
964 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 10 active cores\n",
965 ratio, bclk, ratio * bclk);
966
967 ratio = (msr >> 0) & 0xFF;
968 if (ratio)
969 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 9 active cores\n",
970 ratio, bclk, ratio * bclk);
971
972print_nhm_turbo_ratio_limits:
Len Brown889facb2012-11-08 00:48:57 -0500973 get_msr(0, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
974
975#define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
976#define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
977
978 fprintf(stderr, "cpu0: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", msr);
979
980 fprintf(stderr, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: ",
981 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
982 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
983 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
984 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
985 (msr & (1 << 15)) ? "" : "UN",
986 (unsigned int)msr & 7);
987
988
989 switch(msr & 0x7) {
990 case 0:
991 fprintf(stderr, "pc0");
992 break;
993 case 1:
994 fprintf(stderr, do_snb_cstates ? "pc2" : "pc0");
995 break;
996 case 2:
997 fprintf(stderr, do_snb_cstates ? "pc6-noret" : "pc3");
998 break;
999 case 3:
1000 fprintf(stderr, "pc6");
1001 break;
1002 case 4:
1003 fprintf(stderr, "pc7");
1004 break;
1005 case 5:
1006 fprintf(stderr, do_snb_cstates ? "pc7s" : "invalid");
1007 break;
1008 case 7:
1009 fprintf(stderr, "unlimited");
1010 break;
1011 default:
1012 fprintf(stderr, "invalid");
1013 }
1014 fprintf(stderr, ")\n");
Len Brown103a8fe2010-10-22 23:53:03 -04001015
1016 if (!do_nehalem_turbo_ratio_limit)
1017 return;
1018
Len Brown9c63a652012-10-31 01:29:52 -04001019 get_msr(0, MSR_NHM_TURBO_RATIO_LIMIT, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001020
Len Brown67920412013-01-31 15:22:15 -05001021 fprintf(stderr, "cpu0: MSR_NHM_TURBO_RATIO_LIMIT: 0x%08llx\n", msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001022
1023 ratio = (msr >> 56) & 0xFF;
1024 if (ratio)
1025 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 8 active cores\n",
1026 ratio, bclk, ratio * bclk);
1027
1028 ratio = (msr >> 48) & 0xFF;
1029 if (ratio)
1030 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 7 active cores\n",
1031 ratio, bclk, ratio * bclk);
1032
1033 ratio = (msr >> 40) & 0xFF;
1034 if (ratio)
1035 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 6 active cores\n",
1036 ratio, bclk, ratio * bclk);
1037
1038 ratio = (msr >> 32) & 0xFF;
1039 if (ratio)
1040 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 5 active cores\n",
1041 ratio, bclk, ratio * bclk);
1042
Len Brown103a8fe2010-10-22 23:53:03 -04001043 ratio = (msr >> 24) & 0xFF;
1044 if (ratio)
1045 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
1046 ratio, bclk, ratio * bclk);
1047
1048 ratio = (msr >> 16) & 0xFF;
1049 if (ratio)
1050 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
1051 ratio, bclk, ratio * bclk);
1052
1053 ratio = (msr >> 8) & 0xFF;
1054 if (ratio)
1055 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
1056 ratio, bclk, ratio * bclk);
1057
1058 ratio = (msr >> 0) & 0xFF;
1059 if (ratio)
1060 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
1061 ratio, bclk, ratio * bclk);
Len Brown103a8fe2010-10-22 23:53:03 -04001062}
1063
Len Brownc98d5d92012-06-04 00:56:40 -04001064void free_all_buffers(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001065{
Len Brownc98d5d92012-06-04 00:56:40 -04001066 CPU_FREE(cpu_present_set);
1067 cpu_present_set = NULL;
1068 cpu_present_set = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001069
Len Brownc98d5d92012-06-04 00:56:40 -04001070 CPU_FREE(cpu_affinity_set);
1071 cpu_affinity_set = NULL;
1072 cpu_affinity_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001073
Len Brownc98d5d92012-06-04 00:56:40 -04001074 free(thread_even);
1075 free(core_even);
1076 free(package_even);
1077
1078 thread_even = NULL;
1079 core_even = NULL;
1080 package_even = NULL;
1081
1082 free(thread_odd);
1083 free(core_odd);
1084 free(package_odd);
1085
1086 thread_odd = NULL;
1087 core_odd = NULL;
1088 package_odd = NULL;
1089
1090 free(output_buffer);
1091 output_buffer = NULL;
1092 outp = NULL;
Len Brown103a8fe2010-10-22 23:53:03 -04001093}
1094
Len Brownc98d5d92012-06-04 00:56:40 -04001095/*
1096 * cpu_is_first_sibling_in_core(cpu)
1097 * return 1 if given CPU is 1st HT sibling in the core
1098 */
1099int cpu_is_first_sibling_in_core(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001100{
Len Brownc98d5d92012-06-04 00:56:40 -04001101 char path[64];
1102 FILE *filep;
1103 int first_cpu;
Len Brown103a8fe2010-10-22 23:53:03 -04001104
Len Brownc98d5d92012-06-04 00:56:40 -04001105 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
1106 filep = fopen(path, "r");
1107 if (filep == NULL) {
1108 perror(path);
1109 exit(1);
1110 }
1111 fscanf(filep, "%d", &first_cpu);
1112 fclose(filep);
1113 return (cpu == first_cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001114}
1115
Len Brownc98d5d92012-06-04 00:56:40 -04001116/*
1117 * cpu_is_first_core_in_package(cpu)
1118 * return 1 if given CPU is 1st core in package
1119 */
1120int cpu_is_first_core_in_package(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001121{
Len Brownc98d5d92012-06-04 00:56:40 -04001122 char path[64];
1123 FILE *filep;
1124 int first_cpu;
Len Brown103a8fe2010-10-22 23:53:03 -04001125
Len Brownc98d5d92012-06-04 00:56:40 -04001126 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
1127 filep = fopen(path, "r");
1128 if (filep == NULL) {
1129 perror(path);
Len Brown103a8fe2010-10-22 23:53:03 -04001130 exit(1);
1131 }
Len Brownc98d5d92012-06-04 00:56:40 -04001132 fscanf(filep, "%d", &first_cpu);
1133 fclose(filep);
1134 return (cpu == first_cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001135}
1136
1137int get_physical_package_id(int cpu)
1138{
Len Brownc98d5d92012-06-04 00:56:40 -04001139 char path[80];
Len Brown103a8fe2010-10-22 23:53:03 -04001140 FILE *filep;
1141 int pkg;
1142
1143 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
1144 filep = fopen(path, "r");
1145 if (filep == NULL) {
1146 perror(path);
1147 exit(1);
1148 }
1149 fscanf(filep, "%d", &pkg);
1150 fclose(filep);
1151 return pkg;
1152}
1153
1154int get_core_id(int cpu)
1155{
Len Brownc98d5d92012-06-04 00:56:40 -04001156 char path[80];
Len Brown103a8fe2010-10-22 23:53:03 -04001157 FILE *filep;
1158 int core;
1159
1160 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
1161 filep = fopen(path, "r");
1162 if (filep == NULL) {
1163 perror(path);
1164 exit(1);
1165 }
1166 fscanf(filep, "%d", &core);
1167 fclose(filep);
1168 return core;
1169}
1170
Len Brownc98d5d92012-06-04 00:56:40 -04001171int get_num_ht_siblings(int cpu)
1172{
1173 char path[80];
1174 FILE *filep;
1175 int sib1, sib2;
1176 int matches;
1177 char character;
1178
1179 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
1180 filep = fopen(path, "r");
1181 if (filep == NULL) {
1182 perror(path);
1183 exit(1);
1184 }
1185 /*
1186 * file format:
1187 * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4)
1188 * otherwinse 1 sibling (self).
1189 */
1190 matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2);
1191
1192 fclose(filep);
1193
1194 if (matches == 3)
1195 return 2;
1196 else
1197 return 1;
1198}
1199
Len Brown103a8fe2010-10-22 23:53:03 -04001200/*
Len Brownc98d5d92012-06-04 00:56:40 -04001201 * run func(thread, core, package) in topology order
1202 * skip non-present cpus
Len Brown103a8fe2010-10-22 23:53:03 -04001203 */
1204
Len Brownc98d5d92012-06-04 00:56:40 -04001205int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
1206 struct pkg_data *, struct thread_data *, struct core_data *,
1207 struct pkg_data *), struct thread_data *thread_base,
1208 struct core_data *core_base, struct pkg_data *pkg_base,
1209 struct thread_data *thread_base2, struct core_data *core_base2,
1210 struct pkg_data *pkg_base2)
1211{
1212 int retval, pkg_no, core_no, thread_no;
1213
1214 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
1215 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
1216 for (thread_no = 0; thread_no <
1217 topo.num_threads_per_core; ++thread_no) {
1218 struct thread_data *t, *t2;
1219 struct core_data *c, *c2;
1220 struct pkg_data *p, *p2;
1221
1222 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
1223
1224 if (cpu_is_not_present(t->cpu_id))
1225 continue;
1226
1227 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
1228
1229 c = GET_CORE(core_base, core_no, pkg_no);
1230 c2 = GET_CORE(core_base2, core_no, pkg_no);
1231
1232 p = GET_PKG(pkg_base, pkg_no);
1233 p2 = GET_PKG(pkg_base2, pkg_no);
1234
1235 retval = func(t, c, p, t2, c2, p2);
1236 if (retval)
1237 return retval;
1238 }
1239 }
1240 }
1241 return 0;
1242}
1243
1244/*
1245 * run func(cpu) on every cpu in /proc/stat
1246 * return max_cpu number
1247 */
1248int for_all_proc_cpus(int (func)(int))
Len Brown103a8fe2010-10-22 23:53:03 -04001249{
1250 FILE *fp;
Len Brownc98d5d92012-06-04 00:56:40 -04001251 int cpu_num;
Len Brown103a8fe2010-10-22 23:53:03 -04001252 int retval;
1253
1254 fp = fopen(proc_stat, "r");
1255 if (fp == NULL) {
1256 perror(proc_stat);
1257 exit(1);
1258 }
1259
1260 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
1261 if (retval != 0) {
1262 perror("/proc/stat format");
1263 exit(1);
1264 }
1265
Len Brownc98d5d92012-06-04 00:56:40 -04001266 while (1) {
1267 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 -04001268 if (retval != 1)
1269 break;
1270
Len Brownc98d5d92012-06-04 00:56:40 -04001271 retval = func(cpu_num);
1272 if (retval) {
1273 fclose(fp);
1274 return(retval);
1275 }
Len Brown103a8fe2010-10-22 23:53:03 -04001276 }
1277 fclose(fp);
Len Brownc98d5d92012-06-04 00:56:40 -04001278 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001279}
1280
1281void re_initialize(void)
1282{
Len Brownc98d5d92012-06-04 00:56:40 -04001283 free_all_buffers();
1284 setup_all_buffers();
1285 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04001286}
1287
Len Brownc98d5d92012-06-04 00:56:40 -04001288
Len Brown103a8fe2010-10-22 23:53:03 -04001289/*
Len Brownc98d5d92012-06-04 00:56:40 -04001290 * count_cpus()
1291 * remember the last one seen, it will be the max
Len Brown103a8fe2010-10-22 23:53:03 -04001292 */
Len Brownc98d5d92012-06-04 00:56:40 -04001293int count_cpus(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001294{
Len Brownc98d5d92012-06-04 00:56:40 -04001295 if (topo.max_cpu_num < cpu)
1296 topo.max_cpu_num = cpu;
Len Brown103a8fe2010-10-22 23:53:03 -04001297
Len Brownc98d5d92012-06-04 00:56:40 -04001298 topo.num_cpus += 1;
1299 return 0;
1300}
1301int mark_cpu_present(int cpu)
1302{
1303 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brown15aaa342012-03-29 22:19:58 -04001304 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001305}
1306
1307void turbostat_loop()
1308{
Len Brownc98d5d92012-06-04 00:56:40 -04001309 int retval;
Len Browne52966c2012-11-08 22:38:05 -05001310 int restarted = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001311
Len Brown103a8fe2010-10-22 23:53:03 -04001312restart:
Len Browne52966c2012-11-08 22:38:05 -05001313 restarted++;
1314
Len Brownc98d5d92012-06-04 00:56:40 -04001315 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001316 if (retval < -1) {
1317 exit(retval);
1318 } else if (retval == -1) {
Len Browne52966c2012-11-08 22:38:05 -05001319 if (restarted > 1) {
1320 exit(retval);
1321 }
Len Brownc98d5d92012-06-04 00:56:40 -04001322 re_initialize();
1323 goto restart;
1324 }
Len Browne52966c2012-11-08 22:38:05 -05001325 restarted = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001326 gettimeofday(&tv_even, (struct timezone *)NULL);
1327
1328 while (1) {
Len Brownc98d5d92012-06-04 00:56:40 -04001329 if (for_all_proc_cpus(cpu_is_not_present)) {
Len Brown103a8fe2010-10-22 23:53:03 -04001330 re_initialize();
1331 goto restart;
1332 }
1333 sleep(interval_sec);
Len Brownc98d5d92012-06-04 00:56:40 -04001334 retval = for_all_cpus(get_counters, ODD_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001335 if (retval < -1) {
1336 exit(retval);
1337 } else if (retval == -1) {
Len Brown15aaa342012-03-29 22:19:58 -04001338 re_initialize();
1339 goto restart;
1340 }
Len Brown103a8fe2010-10-22 23:53:03 -04001341 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04001342 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04001343 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
1344 compute_average(EVEN_COUNTERS);
1345 format_all_counters(EVEN_COUNTERS);
1346 flush_stdout();
Len Brown15aaa342012-03-29 22:19:58 -04001347 sleep(interval_sec);
Len Brownc98d5d92012-06-04 00:56:40 -04001348 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001349 if (retval < -1) {
1350 exit(retval);
1351 } else if (retval == -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04001352 re_initialize();
1353 goto restart;
1354 }
Len Brown103a8fe2010-10-22 23:53:03 -04001355 gettimeofday(&tv_even, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04001356 timersub(&tv_even, &tv_odd, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04001357 for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS);
1358 compute_average(ODD_COUNTERS);
1359 format_all_counters(ODD_COUNTERS);
1360 flush_stdout();
Len Brown103a8fe2010-10-22 23:53:03 -04001361 }
1362}
1363
1364void check_dev_msr()
1365{
1366 struct stat sb;
1367
1368 if (stat("/dev/cpu/0/msr", &sb)) {
1369 fprintf(stderr, "no /dev/cpu/0/msr\n");
1370 fprintf(stderr, "Try \"# modprobe msr\"\n");
1371 exit(-5);
1372 }
1373}
1374
1375void check_super_user()
1376{
1377 if (getuid() != 0) {
1378 fprintf(stderr, "must be root\n");
1379 exit(-6);
1380 }
1381}
1382
1383int has_nehalem_turbo_ratio_limit(unsigned int family, unsigned int model)
1384{
1385 if (!genuine_intel)
1386 return 0;
1387
1388 if (family != 6)
1389 return 0;
1390
1391 switch (model) {
1392 case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
1393 case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
1394 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
1395 case 0x25: /* Westmere Client - Clarkdale, Arrandale */
1396 case 0x2C: /* Westmere EP - Gulftown */
1397 case 0x2A: /* SNB */
1398 case 0x2D: /* SNB Xeon */
Len Brown553575f2011-11-18 03:32:01 -05001399 case 0x3A: /* IVB */
Len Brown13006512012-09-26 18:11:31 -04001400 case 0x3E: /* IVB Xeon */
Len Brown70b43402013-01-08 01:26:07 -05001401 case 0x3C: /* HSW */
1402 case 0x3F: /* HSW */
1403 case 0x45: /* HSW */
Len Brown103a8fe2010-10-22 23:53:03 -04001404 return 1;
1405 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1406 case 0x2F: /* Westmere-EX Xeon - Eagleton */
1407 default:
1408 return 0;
1409 }
1410}
Len Brown6574a5d2012-09-21 00:01:31 -04001411int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
1412{
1413 if (!genuine_intel)
1414 return 0;
1415
1416 if (family != 6)
1417 return 0;
1418
1419 switch (model) {
1420 case 0x3E: /* IVB Xeon */
1421 return 1;
1422 default:
1423 return 0;
1424 }
1425}
1426
Len Brown889facb2012-11-08 00:48:57 -05001427/*
1428 * print_epb()
1429 * Decode the ENERGY_PERF_BIAS MSR
1430 */
1431int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1432{
1433 unsigned long long msr;
1434 char *epb_string;
1435 int cpu;
1436
1437 if (!has_epb)
1438 return 0;
1439
1440 cpu = t->cpu_id;
1441
1442 /* EPB is per-package */
1443 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1444 return 0;
1445
1446 if (cpu_migrate(cpu)) {
1447 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1448 return -1;
1449 }
1450
1451 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
1452 return 0;
1453
1454 switch (msr & 0x7) {
1455 case ENERGY_PERF_BIAS_PERFORMANCE:
1456 epb_string = "performance";
1457 break;
1458 case ENERGY_PERF_BIAS_NORMAL:
1459 epb_string = "balanced";
1460 break;
1461 case ENERGY_PERF_BIAS_POWERSAVE:
1462 epb_string = "powersave";
1463 break;
1464 default:
1465 epb_string = "custom";
1466 break;
1467 }
1468 fprintf(stderr, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
1469
1470 return 0;
1471}
1472
1473#define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
1474#define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
1475
1476/*
1477 * rapl_probe()
1478 *
1479 * sets do_rapl
1480 */
1481void rapl_probe(unsigned int family, unsigned int model)
1482{
1483 unsigned long long msr;
1484 double tdp;
1485
1486 if (!genuine_intel)
1487 return;
1488
1489 if (family != 6)
1490 return;
1491
1492 switch (model) {
1493 case 0x2A:
1494 case 0x3A:
Len Brown70b43402013-01-08 01:26:07 -05001495 case 0x3C: /* HSW */
1496 case 0x3F: /* HSW */
1497 case 0x45: /* HSW */
Len Brown889facb2012-11-08 00:48:57 -05001498 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_GFX;
1499 break;
1500 case 0x2D:
1501 case 0x3E:
1502 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_DRAM | RAPL_PKG_PERF_STATUS | RAPL_DRAM_PERF_STATUS;
1503 break;
1504 default:
1505 return;
1506 }
1507
1508 /* units on package 0, verify later other packages match */
1509 if (get_msr(0, MSR_RAPL_POWER_UNIT, &msr))
1510 return;
1511
1512 rapl_power_units = 1.0 / (1 << (msr & 0xF));
1513 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
1514 rapl_time_units = 1.0 / (1 << (msr >> 16 & 0xF));
1515
1516 /* get TDP to determine energy counter range */
1517 if (get_msr(0, MSR_PKG_POWER_INFO, &msr))
1518 return;
1519
1520 tdp = ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
1521
1522 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
1523
1524 if (verbose)
1525 fprintf(stderr, "RAPL: %.0f sec. Joule Counter Range\n", rapl_joule_counter_range);
1526
1527 return;
1528}
1529
1530int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1531{
1532 unsigned long long msr;
1533 unsigned int dts;
1534 int cpu;
1535
1536 if (!(do_dts || do_ptm))
1537 return 0;
1538
1539 cpu = t->cpu_id;
1540
1541 /* DTS is per-core, no need to print for each thread */
1542 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1543 return 0;
1544
1545 if (cpu_migrate(cpu)) {
1546 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1547 return -1;
1548 }
1549
1550 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
1551 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1552 return 0;
1553
1554 dts = (msr >> 16) & 0x7F;
1555 fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
1556 cpu, msr, tcc_activation_temp - dts);
1557
1558#ifdef THERM_DEBUG
1559 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
1560 return 0;
1561
1562 dts = (msr >> 16) & 0x7F;
1563 dts2 = (msr >> 8) & 0x7F;
1564 fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
1565 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
1566#endif
1567 }
1568
1569
1570 if (do_dts) {
1571 unsigned int resolution;
1572
1573 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1574 return 0;
1575
1576 dts = (msr >> 16) & 0x7F;
1577 resolution = (msr >> 27) & 0xF;
1578 fprintf(stderr, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
1579 cpu, msr, tcc_activation_temp - dts, resolution);
1580
1581#ifdef THERM_DEBUG
1582 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
1583 return 0;
1584
1585 dts = (msr >> 16) & 0x7F;
1586 dts2 = (msr >> 8) & 0x7F;
1587 fprintf(stderr, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
1588 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
1589#endif
1590 }
1591
1592 return 0;
1593}
1594
1595void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
1596{
1597 fprintf(stderr, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
1598 cpu, label,
1599 ((msr >> 15) & 1) ? "EN" : "DIS",
1600 ((msr >> 0) & 0x7FFF) * rapl_power_units,
1601 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
1602 (((msr >> 16) & 1) ? "EN" : "DIS"));
1603
1604 return;
1605}
1606
1607int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1608{
1609 unsigned long long msr;
1610 int cpu;
1611 double local_rapl_power_units, local_rapl_energy_units, local_rapl_time_units;
1612
1613 if (!do_rapl)
1614 return 0;
1615
1616 /* RAPL counters are per package, so print only for 1st thread/package */
1617 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1618 return 0;
1619
1620 cpu = t->cpu_id;
1621 if (cpu_migrate(cpu)) {
1622 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1623 return -1;
1624 }
1625
1626 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
1627 return -1;
1628
1629 local_rapl_power_units = 1.0 / (1 << (msr & 0xF));
1630 local_rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
1631 local_rapl_time_units = 1.0 / (1 << (msr >> 16 & 0xF));
1632
1633 if (local_rapl_power_units != rapl_power_units)
1634 fprintf(stderr, "cpu%d, ERROR: Power units mis-match\n", cpu);
1635 if (local_rapl_energy_units != rapl_energy_units)
1636 fprintf(stderr, "cpu%d, ERROR: Energy units mis-match\n", cpu);
1637 if (local_rapl_time_units != rapl_time_units)
1638 fprintf(stderr, "cpu%d, ERROR: Time units mis-match\n", cpu);
1639
1640 if (verbose) {
1641 fprintf(stderr, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx "
1642 "(%f Watts, %f Joules, %f sec.)\n", cpu, msr,
1643 local_rapl_power_units, local_rapl_energy_units, local_rapl_time_units);
1644 }
1645 if (do_rapl & RAPL_PKG) {
1646 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
1647 return -5;
1648
1649
1650 fprintf(stderr, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
1651 cpu, msr,
1652 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1653 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1654 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1655 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
1656
1657 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
1658 return -9;
1659
1660 fprintf(stderr, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
1661 cpu, msr, (msr >> 63) & 1 ? "": "UN");
1662
1663 print_power_limit_msr(cpu, msr, "PKG Limit #1");
1664 fprintf(stderr, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
1665 cpu,
1666 ((msr >> 47) & 1) ? "EN" : "DIS",
1667 ((msr >> 32) & 0x7FFF) * rapl_power_units,
1668 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
1669 ((msr >> 48) & 1) ? "EN" : "DIS");
1670 }
1671
1672 if (do_rapl & RAPL_DRAM) {
1673 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
1674 return -6;
1675
1676
1677 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
1678 cpu, msr,
1679 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1680 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1681 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1682 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
1683
1684
1685 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
1686 return -9;
1687 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
1688 cpu, msr, (msr >> 31) & 1 ? "": "UN");
1689
1690 print_power_limit_msr(cpu, msr, "DRAM Limit");
1691 }
1692 if (do_rapl & RAPL_CORES) {
1693 if (verbose) {
1694 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
1695 return -7;
1696
1697 fprintf(stderr, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
1698
1699 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
1700 return -9;
1701 fprintf(stderr, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
1702 cpu, msr, (msr >> 31) & 1 ? "": "UN");
1703 print_power_limit_msr(cpu, msr, "Cores Limit");
1704 }
1705 }
1706 if (do_rapl & RAPL_GFX) {
1707 if (verbose) {
1708 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
1709 return -8;
1710
1711 fprintf(stderr, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
1712
1713 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
1714 return -9;
1715 fprintf(stderr, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
1716 cpu, msr, (msr >> 31) & 1 ? "": "UN");
1717 print_power_limit_msr(cpu, msr, "GFX Limit");
1718 }
1719 }
1720 return 0;
1721}
1722
Len Brown103a8fe2010-10-22 23:53:03 -04001723
1724int is_snb(unsigned int family, unsigned int model)
1725{
1726 if (!genuine_intel)
1727 return 0;
1728
1729 switch (model) {
1730 case 0x2A:
1731 case 0x2D:
Len Brown650a37f2012-06-03 23:34:44 -04001732 case 0x3A: /* IVB */
Len Brown13006512012-09-26 18:11:31 -04001733 case 0x3E: /* IVB Xeon */
Len Brown70b43402013-01-08 01:26:07 -05001734 case 0x3C: /* HSW */
1735 case 0x3F: /* HSW */
1736 case 0x45: /* HSW */
Len Brown103a8fe2010-10-22 23:53:03 -04001737 return 1;
1738 }
1739 return 0;
1740}
1741
1742double discover_bclk(unsigned int family, unsigned int model)
1743{
1744 if (is_snb(family, model))
1745 return 100.00;
1746 else
1747 return 133.33;
1748}
1749
Len Brown889facb2012-11-08 00:48:57 -05001750/*
1751 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
1752 * the Thermal Control Circuit (TCC) activates.
1753 * This is usually equal to tjMax.
1754 *
1755 * Older processors do not have this MSR, so there we guess,
1756 * but also allow cmdline over-ride with -T.
1757 *
1758 * Several MSR temperature values are in units of degrees-C
1759 * below this value, including the Digital Thermal Sensor (DTS),
1760 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
1761 */
1762int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1763{
1764 unsigned long long msr;
1765 unsigned int target_c_local;
1766 int cpu;
1767
1768 /* tcc_activation_temp is used only for dts or ptm */
1769 if (!(do_dts || do_ptm))
1770 return 0;
1771
1772 /* this is a per-package concept */
1773 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1774 return 0;
1775
1776 cpu = t->cpu_id;
1777 if (cpu_migrate(cpu)) {
1778 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1779 return -1;
1780 }
1781
1782 if (tcc_activation_temp_override != 0) {
1783 tcc_activation_temp = tcc_activation_temp_override;
1784 fprintf(stderr, "cpu%d: Using cmdline TCC Target (%d C)\n",
1785 cpu, tcc_activation_temp);
1786 return 0;
1787 }
1788
1789 /* Temperature Target MSR is Nehalem and newer only */
1790 if (!do_nehalem_platform_info)
1791 goto guess;
1792
1793 if (get_msr(0, MSR_IA32_TEMPERATURE_TARGET, &msr))
1794 goto guess;
1795
1796 target_c_local = (msr >> 16) & 0x7F;
1797
1798 if (verbose)
1799 fprintf(stderr, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
1800 cpu, msr, target_c_local);
1801
1802 if (target_c_local < 85 || target_c_local > 120)
1803 goto guess;
1804
1805 tcc_activation_temp = target_c_local;
1806
1807 return 0;
1808
1809guess:
1810 tcc_activation_temp = TJMAX_DEFAULT;
1811 fprintf(stderr, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
1812 cpu, tcc_activation_temp);
1813
1814 return 0;
1815}
Len Brown103a8fe2010-10-22 23:53:03 -04001816void check_cpuid()
1817{
1818 unsigned int eax, ebx, ecx, edx, max_level;
1819 unsigned int fms, family, model, stepping;
1820
1821 eax = ebx = ecx = edx = 0;
1822
1823 asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0));
1824
1825 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
1826 genuine_intel = 1;
1827
1828 if (verbose)
Len Brown889facb2012-11-08 00:48:57 -05001829 fprintf(stderr, "CPUID(0): %.4s%.4s%.4s ",
Len Brown103a8fe2010-10-22 23:53:03 -04001830 (char *)&ebx, (char *)&edx, (char *)&ecx);
1831
1832 asm("cpuid" : "=a" (fms), "=c" (ecx), "=d" (edx) : "a" (1) : "ebx");
1833 family = (fms >> 8) & 0xf;
1834 model = (fms >> 4) & 0xf;
1835 stepping = fms & 0xf;
1836 if (family == 6 || family == 0xf)
1837 model += ((fms >> 16) & 0xf) << 4;
1838
1839 if (verbose)
1840 fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
1841 max_level, family, model, stepping, family, model, stepping);
1842
1843 if (!(edx & (1 << 5))) {
1844 fprintf(stderr, "CPUID: no MSR\n");
1845 exit(1);
1846 }
1847
1848 /*
1849 * check max extended function levels of CPUID.
1850 * This is needed to check for invariant TSC.
1851 * This check is valid for both Intel and AMD.
1852 */
1853 ebx = ecx = edx = 0;
1854 asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000000));
1855
1856 if (max_level < 0x80000007) {
1857 fprintf(stderr, "CPUID: no invariant TSC (max_level 0x%x)\n", max_level);
1858 exit(1);
1859 }
1860
1861 /*
1862 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
1863 * this check is valid for both Intel and AMD
1864 */
1865 asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000007));
Thomas Renninger8209e052011-01-21 15:11:19 +01001866 has_invariant_tsc = edx & (1 << 8);
Len Brown103a8fe2010-10-22 23:53:03 -04001867
1868 if (!has_invariant_tsc) {
1869 fprintf(stderr, "No invariant TSC\n");
1870 exit(1);
1871 }
1872
1873 /*
1874 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
1875 * this check is valid for both Intel and AMD
1876 */
1877
1878 asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x6));
Thomas Renninger8209e052011-01-21 15:11:19 +01001879 has_aperf = ecx & (1 << 0);
Len Brown889facb2012-11-08 00:48:57 -05001880 do_dts = eax & (1 << 0);
1881 do_ptm = eax & (1 << 6);
1882 has_epb = ecx & (1 << 3);
1883
1884 if (verbose)
1885 fprintf(stderr, "CPUID(6): %s%s%s%s\n",
1886 has_aperf ? "APERF" : "No APERF!",
1887 do_dts ? ", DTS" : "",
1888 do_ptm ? ", PTM": "",
1889 has_epb ? ", EPB": "");
1890
1891 if (!has_aperf)
1892 exit(-1);
Len Brown103a8fe2010-10-22 23:53:03 -04001893
1894 do_nehalem_platform_info = genuine_intel && has_invariant_tsc;
1895 do_nhm_cstates = genuine_intel; /* all Intel w/ non-stop TSC have NHM counters */
1896 do_snb_cstates = is_snb(family, model);
1897 bclk = discover_bclk(family, model);
1898
1899 do_nehalem_turbo_ratio_limit = has_nehalem_turbo_ratio_limit(family, model);
Len Brown6574a5d2012-09-21 00:01:31 -04001900 do_ivt_turbo_ratio_limit = has_ivt_turbo_ratio_limit(family, model);
Len Brown889facb2012-11-08 00:48:57 -05001901 rapl_probe(family, model);
1902
1903 return;
Len Brown103a8fe2010-10-22 23:53:03 -04001904}
1905
1906
1907void usage()
1908{
Len Brown889facb2012-11-08 00:48:57 -05001909 fprintf(stderr, "%s: [-v][-R][-T][-p|-P|-S][-c MSR# | -s]][-C MSR#][-m MSR#][-M MSR#][-i interval_sec | command ...]\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001910 progname);
1911 exit(1);
1912}
1913
1914
1915/*
1916 * in /dev/cpu/ return success for names that are numbers
1917 * ie. filter out ".", "..", "microcode".
1918 */
1919int dir_filter(const struct dirent *dirp)
1920{
1921 if (isdigit(dirp->d_name[0]))
1922 return 1;
1923 else
1924 return 0;
1925}
1926
1927int open_dev_cpu_msr(int dummy1)
1928{
1929 return 0;
1930}
1931
Len Brownc98d5d92012-06-04 00:56:40 -04001932void topology_probe()
1933{
1934 int i;
1935 int max_core_id = 0;
1936 int max_package_id = 0;
1937 int max_siblings = 0;
1938 struct cpu_topology {
1939 int core_id;
1940 int physical_package_id;
1941 } *cpus;
1942
1943 /* Initialize num_cpus, max_cpu_num */
1944 topo.num_cpus = 0;
1945 topo.max_cpu_num = 0;
1946 for_all_proc_cpus(count_cpus);
1947 if (!summary_only && topo.num_cpus > 1)
1948 show_cpu = 1;
1949
1950 if (verbose > 1)
1951 fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
1952
1953 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
1954 if (cpus == NULL) {
1955 perror("calloc cpus");
1956 exit(1);
1957 }
1958
1959 /*
1960 * Allocate and initialize cpu_present_set
1961 */
1962 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
1963 if (cpu_present_set == NULL) {
1964 perror("CPU_ALLOC");
1965 exit(3);
1966 }
1967 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
1968 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
1969 for_all_proc_cpus(mark_cpu_present);
1970
1971 /*
1972 * Allocate and initialize cpu_affinity_set
1973 */
1974 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
1975 if (cpu_affinity_set == NULL) {
1976 perror("CPU_ALLOC");
1977 exit(3);
1978 }
1979 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
1980 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
1981
1982
1983 /*
1984 * For online cpus
1985 * find max_core_id, max_package_id
1986 */
1987 for (i = 0; i <= topo.max_cpu_num; ++i) {
1988 int siblings;
1989
1990 if (cpu_is_not_present(i)) {
1991 if (verbose > 1)
1992 fprintf(stderr, "cpu%d NOT PRESENT\n", i);
1993 continue;
1994 }
1995 cpus[i].core_id = get_core_id(i);
1996 if (cpus[i].core_id > max_core_id)
1997 max_core_id = cpus[i].core_id;
1998
1999 cpus[i].physical_package_id = get_physical_package_id(i);
2000 if (cpus[i].physical_package_id > max_package_id)
2001 max_package_id = cpus[i].physical_package_id;
2002
2003 siblings = get_num_ht_siblings(i);
2004 if (siblings > max_siblings)
2005 max_siblings = siblings;
2006 if (verbose > 1)
2007 fprintf(stderr, "cpu %d pkg %d core %d\n",
2008 i, cpus[i].physical_package_id, cpus[i].core_id);
2009 }
2010 topo.num_cores_per_pkg = max_core_id + 1;
2011 if (verbose > 1)
2012 fprintf(stderr, "max_core_id %d, sizing for %d cores per package\n",
2013 max_core_id, topo.num_cores_per_pkg);
2014 if (!summary_only && topo.num_cores_per_pkg > 1)
2015 show_core = 1;
2016
2017 topo.num_packages = max_package_id + 1;
2018 if (verbose > 1)
2019 fprintf(stderr, "max_package_id %d, sizing for %d packages\n",
2020 max_package_id, topo.num_packages);
2021 if (!summary_only && topo.num_packages > 1)
2022 show_pkg = 1;
2023
2024 topo.num_threads_per_core = max_siblings;
2025 if (verbose > 1)
2026 fprintf(stderr, "max_siblings %d\n", max_siblings);
2027
2028 free(cpus);
2029}
2030
2031void
2032allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
2033{
2034 int i;
2035
2036 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
2037 topo.num_packages, sizeof(struct thread_data));
2038 if (*t == NULL)
2039 goto error;
2040
2041 for (i = 0; i < topo.num_threads_per_core *
2042 topo.num_cores_per_pkg * topo.num_packages; i++)
2043 (*t)[i].cpu_id = -1;
2044
2045 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
2046 sizeof(struct core_data));
2047 if (*c == NULL)
2048 goto error;
2049
2050 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
2051 (*c)[i].core_id = -1;
2052
2053 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
2054 if (*p == NULL)
2055 goto error;
2056
2057 for (i = 0; i < topo.num_packages; i++)
2058 (*p)[i].package_id = i;
2059
2060 return;
2061error:
2062 perror("calloc counters");
2063 exit(1);
2064}
2065/*
2066 * init_counter()
2067 *
2068 * set cpu_id, core_num, pkg_num
2069 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
2070 *
2071 * increment topo.num_cores when 1st core in pkg seen
2072 */
2073void init_counter(struct thread_data *thread_base, struct core_data *core_base,
2074 struct pkg_data *pkg_base, int thread_num, int core_num,
2075 int pkg_num, int cpu_id)
2076{
2077 struct thread_data *t;
2078 struct core_data *c;
2079 struct pkg_data *p;
2080
2081 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
2082 c = GET_CORE(core_base, core_num, pkg_num);
2083 p = GET_PKG(pkg_base, pkg_num);
2084
2085 t->cpu_id = cpu_id;
2086 if (thread_num == 0) {
2087 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
2088 if (cpu_is_first_core_in_package(cpu_id))
2089 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
2090 }
2091
2092 c->core_id = core_num;
2093 p->package_id = pkg_num;
2094}
2095
2096
2097int initialize_counters(int cpu_id)
2098{
2099 int my_thread_id, my_core_id, my_package_id;
2100
2101 my_package_id = get_physical_package_id(cpu_id);
2102 my_core_id = get_core_id(cpu_id);
2103
2104 if (cpu_is_first_sibling_in_core(cpu_id)) {
2105 my_thread_id = 0;
2106 topo.num_cores++;
2107 } else {
2108 my_thread_id = 1;
2109 }
2110
2111 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2112 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2113 return 0;
2114}
2115
2116void allocate_output_buffer()
2117{
2118 output_buffer = calloc(1, (1 + topo.num_cpus) * 128);
2119 outp = output_buffer;
2120 if (outp == NULL) {
2121 perror("calloc");
2122 exit(-1);
2123 }
2124}
2125
2126void setup_all_buffers(void)
2127{
2128 topology_probe();
2129 allocate_counters(&thread_even, &core_even, &package_even);
2130 allocate_counters(&thread_odd, &core_odd, &package_odd);
2131 allocate_output_buffer();
2132 for_all_proc_cpus(initialize_counters);
2133}
Len Brown103a8fe2010-10-22 23:53:03 -04002134void turbostat_init()
2135{
2136 check_cpuid();
2137
2138 check_dev_msr();
2139 check_super_user();
2140
Len Brownc98d5d92012-06-04 00:56:40 -04002141 setup_all_buffers();
Len Brown103a8fe2010-10-22 23:53:03 -04002142
2143 if (verbose)
Len Brownc98d5d92012-06-04 00:56:40 -04002144 print_verbose_header();
Len Brown889facb2012-11-08 00:48:57 -05002145
2146 if (verbose)
2147 for_all_cpus(print_epb, ODD_COUNTERS);
2148
2149 if (verbose)
2150 for_all_cpus(print_rapl, ODD_COUNTERS);
2151
2152 for_all_cpus(set_temperature_target, ODD_COUNTERS);
2153
2154 if (verbose)
2155 for_all_cpus(print_thermal, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04002156}
2157
2158int fork_it(char **argv)
2159{
Len Brown103a8fe2010-10-22 23:53:03 -04002160 pid_t child_pid;
Len Brownd91bb172012-11-01 00:08:19 -04002161 int status;
Len Brownd15cf7c2012-06-03 23:24:00 -04002162
Len Brownd91bb172012-11-01 00:08:19 -04002163 status = for_all_cpus(get_counters, EVEN_COUNTERS);
2164 if (status)
2165 exit(status);
Len Brownc98d5d92012-06-04 00:56:40 -04002166 /* clear affinity side-effect of get_counters() */
2167 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
Len Brown103a8fe2010-10-22 23:53:03 -04002168 gettimeofday(&tv_even, (struct timezone *)NULL);
2169
2170 child_pid = fork();
2171 if (!child_pid) {
2172 /* child */
2173 execvp(argv[0], argv);
2174 } else {
Len Brown103a8fe2010-10-22 23:53:03 -04002175
2176 /* parent */
2177 if (child_pid == -1) {
2178 perror("fork");
2179 exit(1);
2180 }
2181
2182 signal(SIGINT, SIG_IGN);
2183 signal(SIGQUIT, SIG_IGN);
2184 if (waitpid(child_pid, &status, 0) == -1) {
2185 perror("wait");
Len Brownd91bb172012-11-01 00:08:19 -04002186 exit(status);
Len Brown103a8fe2010-10-22 23:53:03 -04002187 }
2188 }
Len Brownc98d5d92012-06-04 00:56:40 -04002189 /*
2190 * n.b. fork_it() does not check for errors from for_all_cpus()
2191 * because re-starting is problematic when forking
2192 */
2193 for_all_cpus(get_counters, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04002194 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002195 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04002196 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
2197 compute_average(EVEN_COUNTERS);
2198 format_all_counters(EVEN_COUNTERS);
2199 flush_stderr();
Len Brown103a8fe2010-10-22 23:53:03 -04002200
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07002201 fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
Len Brown103a8fe2010-10-22 23:53:03 -04002202
Len Brownd91bb172012-11-01 00:08:19 -04002203 return status;
Len Brown103a8fe2010-10-22 23:53:03 -04002204}
2205
2206void cmdline(int argc, char **argv)
2207{
2208 int opt;
2209
2210 progname = argv[0];
2211
Len Brown889facb2012-11-08 00:48:57 -05002212 while ((opt = getopt(argc, argv, "+pPSvi:sc:sC:m:M:RT:")) != -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04002213 switch (opt) {
Len Brownf9240812012-10-06 15:26:31 -04002214 case 'p':
Len Brownc98d5d92012-06-04 00:56:40 -04002215 show_core_only++;
2216 break;
Len Brownf9240812012-10-06 15:26:31 -04002217 case 'P':
Len Brownc98d5d92012-06-04 00:56:40 -04002218 show_pkg_only++;
2219 break;
Len Brownf9240812012-10-06 15:26:31 -04002220 case 'S':
Len Browne23da032012-02-06 18:37:16 -05002221 summary_only++;
2222 break;
Len Brown103a8fe2010-10-22 23:53:03 -04002223 case 'v':
2224 verbose++;
2225 break;
2226 case 'i':
2227 interval_sec = atoi(optarg);
2228 break;
Len Brownf9240812012-10-06 15:26:31 -04002229 case 'c':
Len Brown8e180f32012-09-22 01:25:08 -04002230 sscanf(optarg, "%x", &extra_delta_offset32);
2231 break;
Len Brownf9240812012-10-06 15:26:31 -04002232 case 's':
2233 extra_delta_offset32 = 0x34; /* SMI counter */
2234 break;
2235 case 'C':
Len Brown8e180f32012-09-22 01:25:08 -04002236 sscanf(optarg, "%x", &extra_delta_offset64);
2237 break;
Len Brown2f32edf2012-09-21 23:45:46 -04002238 case 'm':
2239 sscanf(optarg, "%x", &extra_msr_offset32);
Len Brown2f32edf2012-09-21 23:45:46 -04002240 break;
2241 case 'M':
2242 sscanf(optarg, "%x", &extra_msr_offset64);
Len Brown103a8fe2010-10-22 23:53:03 -04002243 break;
Len Brown889facb2012-11-08 00:48:57 -05002244 case 'R':
2245 rapl_verbose++;
2246 break;
2247 case 'T':
2248 tcc_activation_temp_override = atoi(optarg);
2249 break;
Len Brown103a8fe2010-10-22 23:53:03 -04002250 default:
2251 usage();
2252 }
2253 }
2254}
2255
2256int main(int argc, char **argv)
2257{
2258 cmdline(argc, argv);
2259
Len Brown889facb2012-11-08 00:48:57 -05002260 if (verbose)
Len Brown70b43402013-01-08 01:26:07 -05002261 fprintf(stderr, "turbostat v3.1 January 8, 2013"
Len Brown103a8fe2010-10-22 23:53:03 -04002262 " - Len Brown <lenb@kernel.org>\n");
Len Brown103a8fe2010-10-22 23:53:03 -04002263
2264 turbostat_init();
2265
2266 /*
2267 * if any params left, it must be a command to fork
2268 */
2269 if (argc - optind)
2270 return fork_it(argv + optind);
2271 else
2272 turbostat_loop();
2273
2274 return 0;
2275}