blob: b326878bd5d1975768b283bf7e6cf66e3db91bc7 [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 Brown889facb2012-11-08 00:48:57 -0500911 if (verbose)
912 fprintf(stderr, "cpu0: MSR_NHM_PLATFORM_INFO: 0x%08llx\n", msr);
Len Brown6574a5d2012-09-21 00:01:31 -0400913
Len Brown103a8fe2010-10-22 23:53:03 -0400914 ratio = (msr >> 40) & 0xFF;
915 fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency\n",
916 ratio, bclk, ratio * bclk);
917
918 ratio = (msr >> 8) & 0xFF;
919 fprintf(stderr, "%d * %.0f = %.0f MHz TSC frequency\n",
920 ratio, bclk, ratio * bclk);
921
Len Brown6574a5d2012-09-21 00:01:31 -0400922 if (!do_ivt_turbo_ratio_limit)
923 goto print_nhm_turbo_ratio_limits;
924
925 get_msr(0, MSR_IVT_TURBO_RATIO_LIMIT, &msr);
926
Len Brown889facb2012-11-08 00:48:57 -0500927 if (verbose)
928 fprintf(stderr, "cpu0: MSR_IVT_TURBO_RATIO_LIMIT: 0x%08llx\n", msr);
Len Brown6574a5d2012-09-21 00:01:31 -0400929
930 ratio = (msr >> 56) & 0xFF;
931 if (ratio)
932 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 16 active cores\n",
933 ratio, bclk, ratio * bclk);
934
935 ratio = (msr >> 48) & 0xFF;
936 if (ratio)
937 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 15 active cores\n",
938 ratio, bclk, ratio * bclk);
939
940 ratio = (msr >> 40) & 0xFF;
941 if (ratio)
942 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 14 active cores\n",
943 ratio, bclk, ratio * bclk);
944
945 ratio = (msr >> 32) & 0xFF;
946 if (ratio)
947 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 13 active cores\n",
948 ratio, bclk, ratio * bclk);
949
950 ratio = (msr >> 24) & 0xFF;
951 if (ratio)
952 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 12 active cores\n",
953 ratio, bclk, ratio * bclk);
954
955 ratio = (msr >> 16) & 0xFF;
956 if (ratio)
957 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 11 active cores\n",
958 ratio, bclk, ratio * bclk);
959
960 ratio = (msr >> 8) & 0xFF;
961 if (ratio)
962 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 10 active cores\n",
963 ratio, bclk, ratio * bclk);
964
965 ratio = (msr >> 0) & 0xFF;
966 if (ratio)
967 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 9 active cores\n",
968 ratio, bclk, ratio * bclk);
969
970print_nhm_turbo_ratio_limits:
Len Brown889facb2012-11-08 00:48:57 -0500971 get_msr(0, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
972
973#define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
974#define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
975
976 fprintf(stderr, "cpu0: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", msr);
977
978 fprintf(stderr, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: ",
979 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
980 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
981 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
982 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
983 (msr & (1 << 15)) ? "" : "UN",
984 (unsigned int)msr & 7);
985
986
987 switch(msr & 0x7) {
988 case 0:
989 fprintf(stderr, "pc0");
990 break;
991 case 1:
992 fprintf(stderr, do_snb_cstates ? "pc2" : "pc0");
993 break;
994 case 2:
995 fprintf(stderr, do_snb_cstates ? "pc6-noret" : "pc3");
996 break;
997 case 3:
998 fprintf(stderr, "pc6");
999 break;
1000 case 4:
1001 fprintf(stderr, "pc7");
1002 break;
1003 case 5:
1004 fprintf(stderr, do_snb_cstates ? "pc7s" : "invalid");
1005 break;
1006 case 7:
1007 fprintf(stderr, "unlimited");
1008 break;
1009 default:
1010 fprintf(stderr, "invalid");
1011 }
1012 fprintf(stderr, ")\n");
Len Brown103a8fe2010-10-22 23:53:03 -04001013
1014 if (!do_nehalem_turbo_ratio_limit)
1015 return;
1016
Len Brown9c63a652012-10-31 01:29:52 -04001017 get_msr(0, MSR_NHM_TURBO_RATIO_LIMIT, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001018
Len Brown889facb2012-11-08 00:48:57 -05001019 if (verbose)
1020 fprintf(stderr, "cpu0: MSR_NHM_TURBO_RATIO_LIMIT: 0x%08llx\n", msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001021
1022 ratio = (msr >> 56) & 0xFF;
1023 if (ratio)
1024 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 8 active cores\n",
1025 ratio, bclk, ratio * bclk);
1026
1027 ratio = (msr >> 48) & 0xFF;
1028 if (ratio)
1029 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 7 active cores\n",
1030 ratio, bclk, ratio * bclk);
1031
1032 ratio = (msr >> 40) & 0xFF;
1033 if (ratio)
1034 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 6 active cores\n",
1035 ratio, bclk, ratio * bclk);
1036
1037 ratio = (msr >> 32) & 0xFF;
1038 if (ratio)
1039 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 5 active cores\n",
1040 ratio, bclk, ratio * bclk);
1041
Len Brown103a8fe2010-10-22 23:53:03 -04001042 ratio = (msr >> 24) & 0xFF;
1043 if (ratio)
1044 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
1045 ratio, bclk, ratio * bclk);
1046
1047 ratio = (msr >> 16) & 0xFF;
1048 if (ratio)
1049 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
1050 ratio, bclk, ratio * bclk);
1051
1052 ratio = (msr >> 8) & 0xFF;
1053 if (ratio)
1054 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
1055 ratio, bclk, ratio * bclk);
1056
1057 ratio = (msr >> 0) & 0xFF;
1058 if (ratio)
1059 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
1060 ratio, bclk, ratio * bclk);
Len Brown103a8fe2010-10-22 23:53:03 -04001061}
1062
Len Brownc98d5d92012-06-04 00:56:40 -04001063void free_all_buffers(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001064{
Len Brownc98d5d92012-06-04 00:56:40 -04001065 CPU_FREE(cpu_present_set);
1066 cpu_present_set = NULL;
1067 cpu_present_set = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001068
Len Brownc98d5d92012-06-04 00:56:40 -04001069 CPU_FREE(cpu_affinity_set);
1070 cpu_affinity_set = NULL;
1071 cpu_affinity_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001072
Len Brownc98d5d92012-06-04 00:56:40 -04001073 free(thread_even);
1074 free(core_even);
1075 free(package_even);
1076
1077 thread_even = NULL;
1078 core_even = NULL;
1079 package_even = NULL;
1080
1081 free(thread_odd);
1082 free(core_odd);
1083 free(package_odd);
1084
1085 thread_odd = NULL;
1086 core_odd = NULL;
1087 package_odd = NULL;
1088
1089 free(output_buffer);
1090 output_buffer = NULL;
1091 outp = NULL;
Len Brown103a8fe2010-10-22 23:53:03 -04001092}
1093
Len Brownc98d5d92012-06-04 00:56:40 -04001094/*
1095 * cpu_is_first_sibling_in_core(cpu)
1096 * return 1 if given CPU is 1st HT sibling in the core
1097 */
1098int cpu_is_first_sibling_in_core(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001099{
Len Brownc98d5d92012-06-04 00:56:40 -04001100 char path[64];
1101 FILE *filep;
1102 int first_cpu;
Len Brown103a8fe2010-10-22 23:53:03 -04001103
Len Brownc98d5d92012-06-04 00:56:40 -04001104 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
1105 filep = fopen(path, "r");
1106 if (filep == NULL) {
1107 perror(path);
1108 exit(1);
1109 }
1110 fscanf(filep, "%d", &first_cpu);
1111 fclose(filep);
1112 return (cpu == first_cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001113}
1114
Len Brownc98d5d92012-06-04 00:56:40 -04001115/*
1116 * cpu_is_first_core_in_package(cpu)
1117 * return 1 if given CPU is 1st core in package
1118 */
1119int cpu_is_first_core_in_package(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001120{
Len Brownc98d5d92012-06-04 00:56:40 -04001121 char path[64];
1122 FILE *filep;
1123 int first_cpu;
Len Brown103a8fe2010-10-22 23:53:03 -04001124
Len Brownc98d5d92012-06-04 00:56:40 -04001125 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
1126 filep = fopen(path, "r");
1127 if (filep == NULL) {
1128 perror(path);
Len Brown103a8fe2010-10-22 23:53:03 -04001129 exit(1);
1130 }
Len Brownc98d5d92012-06-04 00:56:40 -04001131 fscanf(filep, "%d", &first_cpu);
1132 fclose(filep);
1133 return (cpu == first_cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001134}
1135
1136int get_physical_package_id(int cpu)
1137{
Len Brownc98d5d92012-06-04 00:56:40 -04001138 char path[80];
Len Brown103a8fe2010-10-22 23:53:03 -04001139 FILE *filep;
1140 int pkg;
1141
1142 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
1143 filep = fopen(path, "r");
1144 if (filep == NULL) {
1145 perror(path);
1146 exit(1);
1147 }
1148 fscanf(filep, "%d", &pkg);
1149 fclose(filep);
1150 return pkg;
1151}
1152
1153int get_core_id(int cpu)
1154{
Len Brownc98d5d92012-06-04 00:56:40 -04001155 char path[80];
Len Brown103a8fe2010-10-22 23:53:03 -04001156 FILE *filep;
1157 int core;
1158
1159 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
1160 filep = fopen(path, "r");
1161 if (filep == NULL) {
1162 perror(path);
1163 exit(1);
1164 }
1165 fscanf(filep, "%d", &core);
1166 fclose(filep);
1167 return core;
1168}
1169
Len Brownc98d5d92012-06-04 00:56:40 -04001170int get_num_ht_siblings(int cpu)
1171{
1172 char path[80];
1173 FILE *filep;
1174 int sib1, sib2;
1175 int matches;
1176 char character;
1177
1178 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
1179 filep = fopen(path, "r");
1180 if (filep == NULL) {
1181 perror(path);
1182 exit(1);
1183 }
1184 /*
1185 * file format:
1186 * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4)
1187 * otherwinse 1 sibling (self).
1188 */
1189 matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2);
1190
1191 fclose(filep);
1192
1193 if (matches == 3)
1194 return 2;
1195 else
1196 return 1;
1197}
1198
Len Brown103a8fe2010-10-22 23:53:03 -04001199/*
Len Brownc98d5d92012-06-04 00:56:40 -04001200 * run func(thread, core, package) in topology order
1201 * skip non-present cpus
Len Brown103a8fe2010-10-22 23:53:03 -04001202 */
1203
Len Brownc98d5d92012-06-04 00:56:40 -04001204int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
1205 struct pkg_data *, struct thread_data *, struct core_data *,
1206 struct pkg_data *), struct thread_data *thread_base,
1207 struct core_data *core_base, struct pkg_data *pkg_base,
1208 struct thread_data *thread_base2, struct core_data *core_base2,
1209 struct pkg_data *pkg_base2)
1210{
1211 int retval, pkg_no, core_no, thread_no;
1212
1213 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
1214 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
1215 for (thread_no = 0; thread_no <
1216 topo.num_threads_per_core; ++thread_no) {
1217 struct thread_data *t, *t2;
1218 struct core_data *c, *c2;
1219 struct pkg_data *p, *p2;
1220
1221 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
1222
1223 if (cpu_is_not_present(t->cpu_id))
1224 continue;
1225
1226 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
1227
1228 c = GET_CORE(core_base, core_no, pkg_no);
1229 c2 = GET_CORE(core_base2, core_no, pkg_no);
1230
1231 p = GET_PKG(pkg_base, pkg_no);
1232 p2 = GET_PKG(pkg_base2, pkg_no);
1233
1234 retval = func(t, c, p, t2, c2, p2);
1235 if (retval)
1236 return retval;
1237 }
1238 }
1239 }
1240 return 0;
1241}
1242
1243/*
1244 * run func(cpu) on every cpu in /proc/stat
1245 * return max_cpu number
1246 */
1247int for_all_proc_cpus(int (func)(int))
Len Brown103a8fe2010-10-22 23:53:03 -04001248{
1249 FILE *fp;
Len Brownc98d5d92012-06-04 00:56:40 -04001250 int cpu_num;
Len Brown103a8fe2010-10-22 23:53:03 -04001251 int retval;
1252
1253 fp = fopen(proc_stat, "r");
1254 if (fp == NULL) {
1255 perror(proc_stat);
1256 exit(1);
1257 }
1258
1259 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
1260 if (retval != 0) {
1261 perror("/proc/stat format");
1262 exit(1);
1263 }
1264
Len Brownc98d5d92012-06-04 00:56:40 -04001265 while (1) {
1266 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 -04001267 if (retval != 1)
1268 break;
1269
Len Brownc98d5d92012-06-04 00:56:40 -04001270 retval = func(cpu_num);
1271 if (retval) {
1272 fclose(fp);
1273 return(retval);
1274 }
Len Brown103a8fe2010-10-22 23:53:03 -04001275 }
1276 fclose(fp);
Len Brownc98d5d92012-06-04 00:56:40 -04001277 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001278}
1279
1280void re_initialize(void)
1281{
Len Brownc98d5d92012-06-04 00:56:40 -04001282 free_all_buffers();
1283 setup_all_buffers();
1284 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04001285}
1286
Len Brownc98d5d92012-06-04 00:56:40 -04001287
Len Brown103a8fe2010-10-22 23:53:03 -04001288/*
Len Brownc98d5d92012-06-04 00:56:40 -04001289 * count_cpus()
1290 * remember the last one seen, it will be the max
Len Brown103a8fe2010-10-22 23:53:03 -04001291 */
Len Brownc98d5d92012-06-04 00:56:40 -04001292int count_cpus(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001293{
Len Brownc98d5d92012-06-04 00:56:40 -04001294 if (topo.max_cpu_num < cpu)
1295 topo.max_cpu_num = cpu;
Len Brown103a8fe2010-10-22 23:53:03 -04001296
Len Brownc98d5d92012-06-04 00:56:40 -04001297 topo.num_cpus += 1;
1298 return 0;
1299}
1300int mark_cpu_present(int cpu)
1301{
1302 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brown15aaa342012-03-29 22:19:58 -04001303 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001304}
1305
1306void turbostat_loop()
1307{
Len Brownc98d5d92012-06-04 00:56:40 -04001308 int retval;
Len Browne52966c2012-11-08 22:38:05 -05001309 int restarted = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001310
Len Brown103a8fe2010-10-22 23:53:03 -04001311restart:
Len Browne52966c2012-11-08 22:38:05 -05001312 restarted++;
1313
Len Brownc98d5d92012-06-04 00:56:40 -04001314 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001315 if (retval < -1) {
1316 exit(retval);
1317 } else if (retval == -1) {
Len Browne52966c2012-11-08 22:38:05 -05001318 if (restarted > 1) {
1319 exit(retval);
1320 }
Len Brownc98d5d92012-06-04 00:56:40 -04001321 re_initialize();
1322 goto restart;
1323 }
Len Browne52966c2012-11-08 22:38:05 -05001324 restarted = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001325 gettimeofday(&tv_even, (struct timezone *)NULL);
1326
1327 while (1) {
Len Brownc98d5d92012-06-04 00:56:40 -04001328 if (for_all_proc_cpus(cpu_is_not_present)) {
Len Brown103a8fe2010-10-22 23:53:03 -04001329 re_initialize();
1330 goto restart;
1331 }
1332 sleep(interval_sec);
Len Brownc98d5d92012-06-04 00:56:40 -04001333 retval = for_all_cpus(get_counters, ODD_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001334 if (retval < -1) {
1335 exit(retval);
1336 } else if (retval == -1) {
Len Brown15aaa342012-03-29 22:19:58 -04001337 re_initialize();
1338 goto restart;
1339 }
Len Brown103a8fe2010-10-22 23:53:03 -04001340 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04001341 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04001342 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
1343 compute_average(EVEN_COUNTERS);
1344 format_all_counters(EVEN_COUNTERS);
1345 flush_stdout();
Len Brown15aaa342012-03-29 22:19:58 -04001346 sleep(interval_sec);
Len Brownc98d5d92012-06-04 00:56:40 -04001347 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001348 if (retval < -1) {
1349 exit(retval);
1350 } else if (retval == -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04001351 re_initialize();
1352 goto restart;
1353 }
Len Brown103a8fe2010-10-22 23:53:03 -04001354 gettimeofday(&tv_even, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04001355 timersub(&tv_even, &tv_odd, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04001356 for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS);
1357 compute_average(ODD_COUNTERS);
1358 format_all_counters(ODD_COUNTERS);
1359 flush_stdout();
Len Brown103a8fe2010-10-22 23:53:03 -04001360 }
1361}
1362
1363void check_dev_msr()
1364{
1365 struct stat sb;
1366
1367 if (stat("/dev/cpu/0/msr", &sb)) {
1368 fprintf(stderr, "no /dev/cpu/0/msr\n");
1369 fprintf(stderr, "Try \"# modprobe msr\"\n");
1370 exit(-5);
1371 }
1372}
1373
1374void check_super_user()
1375{
1376 if (getuid() != 0) {
1377 fprintf(stderr, "must be root\n");
1378 exit(-6);
1379 }
1380}
1381
1382int has_nehalem_turbo_ratio_limit(unsigned int family, unsigned int model)
1383{
1384 if (!genuine_intel)
1385 return 0;
1386
1387 if (family != 6)
1388 return 0;
1389
1390 switch (model) {
1391 case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
1392 case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
1393 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
1394 case 0x25: /* Westmere Client - Clarkdale, Arrandale */
1395 case 0x2C: /* Westmere EP - Gulftown */
1396 case 0x2A: /* SNB */
1397 case 0x2D: /* SNB Xeon */
Len Brown553575f2011-11-18 03:32:01 -05001398 case 0x3A: /* IVB */
Len Brown1300651b2012-09-26 18:11:31 -04001399 case 0x3E: /* IVB Xeon */
Len Brown70b43402013-01-08 01:26:07 -05001400 case 0x3C: /* HSW */
1401 case 0x3F: /* HSW */
1402 case 0x45: /* HSW */
Len Brown103a8fe2010-10-22 23:53:03 -04001403 return 1;
1404 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1405 case 0x2F: /* Westmere-EX Xeon - Eagleton */
1406 default:
1407 return 0;
1408 }
1409}
Len Brown6574a5d2012-09-21 00:01:31 -04001410int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
1411{
1412 if (!genuine_intel)
1413 return 0;
1414
1415 if (family != 6)
1416 return 0;
1417
1418 switch (model) {
1419 case 0x3E: /* IVB Xeon */
1420 return 1;
1421 default:
1422 return 0;
1423 }
1424}
1425
Len Brown889facb2012-11-08 00:48:57 -05001426/*
1427 * print_epb()
1428 * Decode the ENERGY_PERF_BIAS MSR
1429 */
1430int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1431{
1432 unsigned long long msr;
1433 char *epb_string;
1434 int cpu;
1435
1436 if (!has_epb)
1437 return 0;
1438
1439 cpu = t->cpu_id;
1440
1441 /* EPB is per-package */
1442 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1443 return 0;
1444
1445 if (cpu_migrate(cpu)) {
1446 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1447 return -1;
1448 }
1449
1450 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
1451 return 0;
1452
1453 switch (msr & 0x7) {
1454 case ENERGY_PERF_BIAS_PERFORMANCE:
1455 epb_string = "performance";
1456 break;
1457 case ENERGY_PERF_BIAS_NORMAL:
1458 epb_string = "balanced";
1459 break;
1460 case ENERGY_PERF_BIAS_POWERSAVE:
1461 epb_string = "powersave";
1462 break;
1463 default:
1464 epb_string = "custom";
1465 break;
1466 }
1467 fprintf(stderr, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
1468
1469 return 0;
1470}
1471
1472#define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
1473#define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
1474
1475/*
1476 * rapl_probe()
1477 *
1478 * sets do_rapl
1479 */
1480void rapl_probe(unsigned int family, unsigned int model)
1481{
1482 unsigned long long msr;
1483 double tdp;
1484
1485 if (!genuine_intel)
1486 return;
1487
1488 if (family != 6)
1489 return;
1490
1491 switch (model) {
1492 case 0x2A:
1493 case 0x3A:
Len Brown70b43402013-01-08 01:26:07 -05001494 case 0x3C: /* HSW */
1495 case 0x3F: /* HSW */
1496 case 0x45: /* HSW */
Len Brown889facb2012-11-08 00:48:57 -05001497 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_GFX;
1498 break;
1499 case 0x2D:
1500 case 0x3E:
1501 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_DRAM | RAPL_PKG_PERF_STATUS | RAPL_DRAM_PERF_STATUS;
1502 break;
1503 default:
1504 return;
1505 }
1506
1507 /* units on package 0, verify later other packages match */
1508 if (get_msr(0, MSR_RAPL_POWER_UNIT, &msr))
1509 return;
1510
1511 rapl_power_units = 1.0 / (1 << (msr & 0xF));
1512 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
1513 rapl_time_units = 1.0 / (1 << (msr >> 16 & 0xF));
1514
1515 /* get TDP to determine energy counter range */
1516 if (get_msr(0, MSR_PKG_POWER_INFO, &msr))
1517 return;
1518
1519 tdp = ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
1520
1521 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
1522
1523 if (verbose)
1524 fprintf(stderr, "RAPL: %.0f sec. Joule Counter Range\n", rapl_joule_counter_range);
1525
1526 return;
1527}
1528
1529int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1530{
1531 unsigned long long msr;
1532 unsigned int dts;
1533 int cpu;
1534
1535 if (!(do_dts || do_ptm))
1536 return 0;
1537
1538 cpu = t->cpu_id;
1539
1540 /* DTS is per-core, no need to print for each thread */
1541 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1542 return 0;
1543
1544 if (cpu_migrate(cpu)) {
1545 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1546 return -1;
1547 }
1548
1549 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
1550 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1551 return 0;
1552
1553 dts = (msr >> 16) & 0x7F;
1554 fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
1555 cpu, msr, tcc_activation_temp - dts);
1556
1557#ifdef THERM_DEBUG
1558 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
1559 return 0;
1560
1561 dts = (msr >> 16) & 0x7F;
1562 dts2 = (msr >> 8) & 0x7F;
1563 fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
1564 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
1565#endif
1566 }
1567
1568
1569 if (do_dts) {
1570 unsigned int resolution;
1571
1572 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1573 return 0;
1574
1575 dts = (msr >> 16) & 0x7F;
1576 resolution = (msr >> 27) & 0xF;
1577 fprintf(stderr, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
1578 cpu, msr, tcc_activation_temp - dts, resolution);
1579
1580#ifdef THERM_DEBUG
1581 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
1582 return 0;
1583
1584 dts = (msr >> 16) & 0x7F;
1585 dts2 = (msr >> 8) & 0x7F;
1586 fprintf(stderr, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
1587 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
1588#endif
1589 }
1590
1591 return 0;
1592}
1593
1594void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
1595{
1596 fprintf(stderr, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
1597 cpu, label,
1598 ((msr >> 15) & 1) ? "EN" : "DIS",
1599 ((msr >> 0) & 0x7FFF) * rapl_power_units,
1600 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
1601 (((msr >> 16) & 1) ? "EN" : "DIS"));
1602
1603 return;
1604}
1605
1606int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1607{
1608 unsigned long long msr;
1609 int cpu;
1610 double local_rapl_power_units, local_rapl_energy_units, local_rapl_time_units;
1611
1612 if (!do_rapl)
1613 return 0;
1614
1615 /* RAPL counters are per package, so print only for 1st thread/package */
1616 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1617 return 0;
1618
1619 cpu = t->cpu_id;
1620 if (cpu_migrate(cpu)) {
1621 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1622 return -1;
1623 }
1624
1625 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
1626 return -1;
1627
1628 local_rapl_power_units = 1.0 / (1 << (msr & 0xF));
1629 local_rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
1630 local_rapl_time_units = 1.0 / (1 << (msr >> 16 & 0xF));
1631
1632 if (local_rapl_power_units != rapl_power_units)
1633 fprintf(stderr, "cpu%d, ERROR: Power units mis-match\n", cpu);
1634 if (local_rapl_energy_units != rapl_energy_units)
1635 fprintf(stderr, "cpu%d, ERROR: Energy units mis-match\n", cpu);
1636 if (local_rapl_time_units != rapl_time_units)
1637 fprintf(stderr, "cpu%d, ERROR: Time units mis-match\n", cpu);
1638
1639 if (verbose) {
1640 fprintf(stderr, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx "
1641 "(%f Watts, %f Joules, %f sec.)\n", cpu, msr,
1642 local_rapl_power_units, local_rapl_energy_units, local_rapl_time_units);
1643 }
1644 if (do_rapl & RAPL_PKG) {
1645 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
1646 return -5;
1647
1648
1649 fprintf(stderr, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
1650 cpu, msr,
1651 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1652 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1653 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1654 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
1655
1656 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
1657 return -9;
1658
1659 fprintf(stderr, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
1660 cpu, msr, (msr >> 63) & 1 ? "": "UN");
1661
1662 print_power_limit_msr(cpu, msr, "PKG Limit #1");
1663 fprintf(stderr, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
1664 cpu,
1665 ((msr >> 47) & 1) ? "EN" : "DIS",
1666 ((msr >> 32) & 0x7FFF) * rapl_power_units,
1667 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
1668 ((msr >> 48) & 1) ? "EN" : "DIS");
1669 }
1670
1671 if (do_rapl & RAPL_DRAM) {
1672 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
1673 return -6;
1674
1675
1676 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
1677 cpu, msr,
1678 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1679 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1680 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1681 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
1682
1683
1684 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
1685 return -9;
1686 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
1687 cpu, msr, (msr >> 31) & 1 ? "": "UN");
1688
1689 print_power_limit_msr(cpu, msr, "DRAM Limit");
1690 }
1691 if (do_rapl & RAPL_CORES) {
1692 if (verbose) {
1693 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
1694 return -7;
1695
1696 fprintf(stderr, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
1697
1698 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
1699 return -9;
1700 fprintf(stderr, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
1701 cpu, msr, (msr >> 31) & 1 ? "": "UN");
1702 print_power_limit_msr(cpu, msr, "Cores Limit");
1703 }
1704 }
1705 if (do_rapl & RAPL_GFX) {
1706 if (verbose) {
1707 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
1708 return -8;
1709
1710 fprintf(stderr, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
1711
1712 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
1713 return -9;
1714 fprintf(stderr, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
1715 cpu, msr, (msr >> 31) & 1 ? "": "UN");
1716 print_power_limit_msr(cpu, msr, "GFX Limit");
1717 }
1718 }
1719 return 0;
1720}
1721
Len Brown103a8fe2010-10-22 23:53:03 -04001722
1723int is_snb(unsigned int family, unsigned int model)
1724{
1725 if (!genuine_intel)
1726 return 0;
1727
1728 switch (model) {
1729 case 0x2A:
1730 case 0x2D:
Len Brown650a37f2012-06-03 23:34:44 -04001731 case 0x3A: /* IVB */
Len Brown1300651b2012-09-26 18:11:31 -04001732 case 0x3E: /* IVB Xeon */
Len Brown70b43402013-01-08 01:26:07 -05001733 case 0x3C: /* HSW */
1734 case 0x3F: /* HSW */
1735 case 0x45: /* HSW */
Len Brown103a8fe2010-10-22 23:53:03 -04001736 return 1;
1737 }
1738 return 0;
1739}
1740
1741double discover_bclk(unsigned int family, unsigned int model)
1742{
1743 if (is_snb(family, model))
1744 return 100.00;
1745 else
1746 return 133.33;
1747}
1748
Len Brown889facb2012-11-08 00:48:57 -05001749/*
1750 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
1751 * the Thermal Control Circuit (TCC) activates.
1752 * This is usually equal to tjMax.
1753 *
1754 * Older processors do not have this MSR, so there we guess,
1755 * but also allow cmdline over-ride with -T.
1756 *
1757 * Several MSR temperature values are in units of degrees-C
1758 * below this value, including the Digital Thermal Sensor (DTS),
1759 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
1760 */
1761int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1762{
1763 unsigned long long msr;
1764 unsigned int target_c_local;
1765 int cpu;
1766
1767 /* tcc_activation_temp is used only for dts or ptm */
1768 if (!(do_dts || do_ptm))
1769 return 0;
1770
1771 /* this is a per-package concept */
1772 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1773 return 0;
1774
1775 cpu = t->cpu_id;
1776 if (cpu_migrate(cpu)) {
1777 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1778 return -1;
1779 }
1780
1781 if (tcc_activation_temp_override != 0) {
1782 tcc_activation_temp = tcc_activation_temp_override;
1783 fprintf(stderr, "cpu%d: Using cmdline TCC Target (%d C)\n",
1784 cpu, tcc_activation_temp);
1785 return 0;
1786 }
1787
1788 /* Temperature Target MSR is Nehalem and newer only */
1789 if (!do_nehalem_platform_info)
1790 goto guess;
1791
1792 if (get_msr(0, MSR_IA32_TEMPERATURE_TARGET, &msr))
1793 goto guess;
1794
1795 target_c_local = (msr >> 16) & 0x7F;
1796
1797 if (verbose)
1798 fprintf(stderr, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
1799 cpu, msr, target_c_local);
1800
1801 if (target_c_local < 85 || target_c_local > 120)
1802 goto guess;
1803
1804 tcc_activation_temp = target_c_local;
1805
1806 return 0;
1807
1808guess:
1809 tcc_activation_temp = TJMAX_DEFAULT;
1810 fprintf(stderr, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
1811 cpu, tcc_activation_temp);
1812
1813 return 0;
1814}
Len Brown103a8fe2010-10-22 23:53:03 -04001815void check_cpuid()
1816{
1817 unsigned int eax, ebx, ecx, edx, max_level;
1818 unsigned int fms, family, model, stepping;
1819
1820 eax = ebx = ecx = edx = 0;
1821
1822 asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0));
1823
1824 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
1825 genuine_intel = 1;
1826
1827 if (verbose)
Len Brown889facb2012-11-08 00:48:57 -05001828 fprintf(stderr, "CPUID(0): %.4s%.4s%.4s ",
Len Brown103a8fe2010-10-22 23:53:03 -04001829 (char *)&ebx, (char *)&edx, (char *)&ecx);
1830
1831 asm("cpuid" : "=a" (fms), "=c" (ecx), "=d" (edx) : "a" (1) : "ebx");
1832 family = (fms >> 8) & 0xf;
1833 model = (fms >> 4) & 0xf;
1834 stepping = fms & 0xf;
1835 if (family == 6 || family == 0xf)
1836 model += ((fms >> 16) & 0xf) << 4;
1837
1838 if (verbose)
1839 fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
1840 max_level, family, model, stepping, family, model, stepping);
1841
1842 if (!(edx & (1 << 5))) {
1843 fprintf(stderr, "CPUID: no MSR\n");
1844 exit(1);
1845 }
1846
1847 /*
1848 * check max extended function levels of CPUID.
1849 * This is needed to check for invariant TSC.
1850 * This check is valid for both Intel and AMD.
1851 */
1852 ebx = ecx = edx = 0;
1853 asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000000));
1854
1855 if (max_level < 0x80000007) {
1856 fprintf(stderr, "CPUID: no invariant TSC (max_level 0x%x)\n", max_level);
1857 exit(1);
1858 }
1859
1860 /*
1861 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
1862 * this check is valid for both Intel and AMD
1863 */
1864 asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000007));
Thomas Renninger8209e052011-01-21 15:11:19 +01001865 has_invariant_tsc = edx & (1 << 8);
Len Brown103a8fe2010-10-22 23:53:03 -04001866
1867 if (!has_invariant_tsc) {
1868 fprintf(stderr, "No invariant TSC\n");
1869 exit(1);
1870 }
1871
1872 /*
1873 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
1874 * this check is valid for both Intel and AMD
1875 */
1876
1877 asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x6));
Thomas Renninger8209e052011-01-21 15:11:19 +01001878 has_aperf = ecx & (1 << 0);
Len Brown889facb2012-11-08 00:48:57 -05001879 do_dts = eax & (1 << 0);
1880 do_ptm = eax & (1 << 6);
1881 has_epb = ecx & (1 << 3);
1882
1883 if (verbose)
1884 fprintf(stderr, "CPUID(6): %s%s%s%s\n",
1885 has_aperf ? "APERF" : "No APERF!",
1886 do_dts ? ", DTS" : "",
1887 do_ptm ? ", PTM": "",
1888 has_epb ? ", EPB": "");
1889
1890 if (!has_aperf)
1891 exit(-1);
Len Brown103a8fe2010-10-22 23:53:03 -04001892
1893 do_nehalem_platform_info = genuine_intel && has_invariant_tsc;
1894 do_nhm_cstates = genuine_intel; /* all Intel w/ non-stop TSC have NHM counters */
1895 do_snb_cstates = is_snb(family, model);
1896 bclk = discover_bclk(family, model);
1897
1898 do_nehalem_turbo_ratio_limit = has_nehalem_turbo_ratio_limit(family, model);
Len Brown6574a5d2012-09-21 00:01:31 -04001899 do_ivt_turbo_ratio_limit = has_ivt_turbo_ratio_limit(family, model);
Len Brown889facb2012-11-08 00:48:57 -05001900 rapl_probe(family, model);
1901
1902 return;
Len Brown103a8fe2010-10-22 23:53:03 -04001903}
1904
1905
1906void usage()
1907{
Len Brown889facb2012-11-08 00:48:57 -05001908 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 -04001909 progname);
1910 exit(1);
1911}
1912
1913
1914/*
1915 * in /dev/cpu/ return success for names that are numbers
1916 * ie. filter out ".", "..", "microcode".
1917 */
1918int dir_filter(const struct dirent *dirp)
1919{
1920 if (isdigit(dirp->d_name[0]))
1921 return 1;
1922 else
1923 return 0;
1924}
1925
1926int open_dev_cpu_msr(int dummy1)
1927{
1928 return 0;
1929}
1930
Len Brownc98d5d92012-06-04 00:56:40 -04001931void topology_probe()
1932{
1933 int i;
1934 int max_core_id = 0;
1935 int max_package_id = 0;
1936 int max_siblings = 0;
1937 struct cpu_topology {
1938 int core_id;
1939 int physical_package_id;
1940 } *cpus;
1941
1942 /* Initialize num_cpus, max_cpu_num */
1943 topo.num_cpus = 0;
1944 topo.max_cpu_num = 0;
1945 for_all_proc_cpus(count_cpus);
1946 if (!summary_only && topo.num_cpus > 1)
1947 show_cpu = 1;
1948
1949 if (verbose > 1)
1950 fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
1951
1952 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
1953 if (cpus == NULL) {
1954 perror("calloc cpus");
1955 exit(1);
1956 }
1957
1958 /*
1959 * Allocate and initialize cpu_present_set
1960 */
1961 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
1962 if (cpu_present_set == NULL) {
1963 perror("CPU_ALLOC");
1964 exit(3);
1965 }
1966 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
1967 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
1968 for_all_proc_cpus(mark_cpu_present);
1969
1970 /*
1971 * Allocate and initialize cpu_affinity_set
1972 */
1973 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
1974 if (cpu_affinity_set == NULL) {
1975 perror("CPU_ALLOC");
1976 exit(3);
1977 }
1978 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
1979 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
1980
1981
1982 /*
1983 * For online cpus
1984 * find max_core_id, max_package_id
1985 */
1986 for (i = 0; i <= topo.max_cpu_num; ++i) {
1987 int siblings;
1988
1989 if (cpu_is_not_present(i)) {
1990 if (verbose > 1)
1991 fprintf(stderr, "cpu%d NOT PRESENT\n", i);
1992 continue;
1993 }
1994 cpus[i].core_id = get_core_id(i);
1995 if (cpus[i].core_id > max_core_id)
1996 max_core_id = cpus[i].core_id;
1997
1998 cpus[i].physical_package_id = get_physical_package_id(i);
1999 if (cpus[i].physical_package_id > max_package_id)
2000 max_package_id = cpus[i].physical_package_id;
2001
2002 siblings = get_num_ht_siblings(i);
2003 if (siblings > max_siblings)
2004 max_siblings = siblings;
2005 if (verbose > 1)
2006 fprintf(stderr, "cpu %d pkg %d core %d\n",
2007 i, cpus[i].physical_package_id, cpus[i].core_id);
2008 }
2009 topo.num_cores_per_pkg = max_core_id + 1;
2010 if (verbose > 1)
2011 fprintf(stderr, "max_core_id %d, sizing for %d cores per package\n",
2012 max_core_id, topo.num_cores_per_pkg);
2013 if (!summary_only && topo.num_cores_per_pkg > 1)
2014 show_core = 1;
2015
2016 topo.num_packages = max_package_id + 1;
2017 if (verbose > 1)
2018 fprintf(stderr, "max_package_id %d, sizing for %d packages\n",
2019 max_package_id, topo.num_packages);
2020 if (!summary_only && topo.num_packages > 1)
2021 show_pkg = 1;
2022
2023 topo.num_threads_per_core = max_siblings;
2024 if (verbose > 1)
2025 fprintf(stderr, "max_siblings %d\n", max_siblings);
2026
2027 free(cpus);
2028}
2029
2030void
2031allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
2032{
2033 int i;
2034
2035 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
2036 topo.num_packages, sizeof(struct thread_data));
2037 if (*t == NULL)
2038 goto error;
2039
2040 for (i = 0; i < topo.num_threads_per_core *
2041 topo.num_cores_per_pkg * topo.num_packages; i++)
2042 (*t)[i].cpu_id = -1;
2043
2044 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
2045 sizeof(struct core_data));
2046 if (*c == NULL)
2047 goto error;
2048
2049 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
2050 (*c)[i].core_id = -1;
2051
2052 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
2053 if (*p == NULL)
2054 goto error;
2055
2056 for (i = 0; i < topo.num_packages; i++)
2057 (*p)[i].package_id = i;
2058
2059 return;
2060error:
2061 perror("calloc counters");
2062 exit(1);
2063}
2064/*
2065 * init_counter()
2066 *
2067 * set cpu_id, core_num, pkg_num
2068 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
2069 *
2070 * increment topo.num_cores when 1st core in pkg seen
2071 */
2072void init_counter(struct thread_data *thread_base, struct core_data *core_base,
2073 struct pkg_data *pkg_base, int thread_num, int core_num,
2074 int pkg_num, int cpu_id)
2075{
2076 struct thread_data *t;
2077 struct core_data *c;
2078 struct pkg_data *p;
2079
2080 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
2081 c = GET_CORE(core_base, core_num, pkg_num);
2082 p = GET_PKG(pkg_base, pkg_num);
2083
2084 t->cpu_id = cpu_id;
2085 if (thread_num == 0) {
2086 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
2087 if (cpu_is_first_core_in_package(cpu_id))
2088 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
2089 }
2090
2091 c->core_id = core_num;
2092 p->package_id = pkg_num;
2093}
2094
2095
2096int initialize_counters(int cpu_id)
2097{
2098 int my_thread_id, my_core_id, my_package_id;
2099
2100 my_package_id = get_physical_package_id(cpu_id);
2101 my_core_id = get_core_id(cpu_id);
2102
2103 if (cpu_is_first_sibling_in_core(cpu_id)) {
2104 my_thread_id = 0;
2105 topo.num_cores++;
2106 } else {
2107 my_thread_id = 1;
2108 }
2109
2110 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2111 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2112 return 0;
2113}
2114
2115void allocate_output_buffer()
2116{
2117 output_buffer = calloc(1, (1 + topo.num_cpus) * 128);
2118 outp = output_buffer;
2119 if (outp == NULL) {
2120 perror("calloc");
2121 exit(-1);
2122 }
2123}
2124
2125void setup_all_buffers(void)
2126{
2127 topology_probe();
2128 allocate_counters(&thread_even, &core_even, &package_even);
2129 allocate_counters(&thread_odd, &core_odd, &package_odd);
2130 allocate_output_buffer();
2131 for_all_proc_cpus(initialize_counters);
2132}
Len Brown103a8fe2010-10-22 23:53:03 -04002133void turbostat_init()
2134{
2135 check_cpuid();
2136
2137 check_dev_msr();
2138 check_super_user();
2139
Len Brownc98d5d92012-06-04 00:56:40 -04002140 setup_all_buffers();
Len Brown103a8fe2010-10-22 23:53:03 -04002141
2142 if (verbose)
Len Brownc98d5d92012-06-04 00:56:40 -04002143 print_verbose_header();
Len Brown889facb2012-11-08 00:48:57 -05002144
2145 if (verbose)
2146 for_all_cpus(print_epb, ODD_COUNTERS);
2147
2148 if (verbose)
2149 for_all_cpus(print_rapl, ODD_COUNTERS);
2150
2151 for_all_cpus(set_temperature_target, ODD_COUNTERS);
2152
2153 if (verbose)
2154 for_all_cpus(print_thermal, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04002155}
2156
2157int fork_it(char **argv)
2158{
Len Brown103a8fe2010-10-22 23:53:03 -04002159 pid_t child_pid;
Len Brownd91bb172012-11-01 00:08:19 -04002160 int status;
Len Brownd15cf7c2012-06-03 23:24:00 -04002161
Len Brownd91bb172012-11-01 00:08:19 -04002162 status = for_all_cpus(get_counters, EVEN_COUNTERS);
2163 if (status)
2164 exit(status);
Len Brownc98d5d92012-06-04 00:56:40 -04002165 /* clear affinity side-effect of get_counters() */
2166 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
Len Brown103a8fe2010-10-22 23:53:03 -04002167 gettimeofday(&tv_even, (struct timezone *)NULL);
2168
2169 child_pid = fork();
2170 if (!child_pid) {
2171 /* child */
2172 execvp(argv[0], argv);
2173 } else {
Len Brown103a8fe2010-10-22 23:53:03 -04002174
2175 /* parent */
2176 if (child_pid == -1) {
2177 perror("fork");
2178 exit(1);
2179 }
2180
2181 signal(SIGINT, SIG_IGN);
2182 signal(SIGQUIT, SIG_IGN);
2183 if (waitpid(child_pid, &status, 0) == -1) {
2184 perror("wait");
Len Brownd91bb172012-11-01 00:08:19 -04002185 exit(status);
Len Brown103a8fe2010-10-22 23:53:03 -04002186 }
2187 }
Len Brownc98d5d92012-06-04 00:56:40 -04002188 /*
2189 * n.b. fork_it() does not check for errors from for_all_cpus()
2190 * because re-starting is problematic when forking
2191 */
2192 for_all_cpus(get_counters, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04002193 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002194 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04002195 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
2196 compute_average(EVEN_COUNTERS);
2197 format_all_counters(EVEN_COUNTERS);
2198 flush_stderr();
Len Brown103a8fe2010-10-22 23:53:03 -04002199
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07002200 fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
Len Brown103a8fe2010-10-22 23:53:03 -04002201
Len Brownd91bb172012-11-01 00:08:19 -04002202 return status;
Len Brown103a8fe2010-10-22 23:53:03 -04002203}
2204
2205void cmdline(int argc, char **argv)
2206{
2207 int opt;
2208
2209 progname = argv[0];
2210
Len Brown889facb2012-11-08 00:48:57 -05002211 while ((opt = getopt(argc, argv, "+pPSvi:sc:sC:m:M:RT:")) != -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04002212 switch (opt) {
Len Brownf9240812012-10-06 15:26:31 -04002213 case 'p':
Len Brownc98d5d92012-06-04 00:56:40 -04002214 show_core_only++;
2215 break;
Len Brownf9240812012-10-06 15:26:31 -04002216 case 'P':
Len Brownc98d5d92012-06-04 00:56:40 -04002217 show_pkg_only++;
2218 break;
Len Brownf9240812012-10-06 15:26:31 -04002219 case 'S':
Len Browne23da032012-02-06 18:37:16 -05002220 summary_only++;
2221 break;
Len Brown103a8fe2010-10-22 23:53:03 -04002222 case 'v':
2223 verbose++;
2224 break;
2225 case 'i':
2226 interval_sec = atoi(optarg);
2227 break;
Len Brownf9240812012-10-06 15:26:31 -04002228 case 'c':
Len Brown8e180f32012-09-22 01:25:08 -04002229 sscanf(optarg, "%x", &extra_delta_offset32);
2230 break;
Len Brownf9240812012-10-06 15:26:31 -04002231 case 's':
2232 extra_delta_offset32 = 0x34; /* SMI counter */
2233 break;
2234 case 'C':
Len Brown8e180f32012-09-22 01:25:08 -04002235 sscanf(optarg, "%x", &extra_delta_offset64);
2236 break;
Len Brown2f32edf2012-09-21 23:45:46 -04002237 case 'm':
2238 sscanf(optarg, "%x", &extra_msr_offset32);
Len Brown2f32edf2012-09-21 23:45:46 -04002239 break;
2240 case 'M':
2241 sscanf(optarg, "%x", &extra_msr_offset64);
Len Brown103a8fe2010-10-22 23:53:03 -04002242 break;
Len Brown889facb2012-11-08 00:48:57 -05002243 case 'R':
2244 rapl_verbose++;
2245 break;
2246 case 'T':
2247 tcc_activation_temp_override = atoi(optarg);
2248 break;
Len Brown103a8fe2010-10-22 23:53:03 -04002249 default:
2250 usage();
2251 }
2252 }
2253}
2254
2255int main(int argc, char **argv)
2256{
2257 cmdline(argc, argv);
2258
Len Brown889facb2012-11-08 00:48:57 -05002259 if (verbose)
Len Brown70b43402013-01-08 01:26:07 -05002260 fprintf(stderr, "turbostat v3.1 January 8, 2013"
Len Brown103a8fe2010-10-22 23:53:03 -04002261 " - Len Brown <lenb@kernel.org>\n");
Len Brown103a8fe2010-10-22 23:53:03 -04002262
2263 turbostat_init();
2264
2265 /*
2266 * if any params left, it must be a command to fork
2267 */
2268 if (argc - optind)
2269 return fork_it(argv + optind);
2270 else
2271 turbostat_loop();
2272
2273 return 0;
2274}