blob: 3c9dc54c710cd81bee9a6b6e04cef363ed5b0efe [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 Brown103a8fe2010-10-22 23:53:03 -040023#include <stdio.h>
24#include <unistd.h>
25#include <sys/types.h>
26#include <sys/wait.h>
27#include <sys/stat.h>
28#include <sys/resource.h>
29#include <fcntl.h>
30#include <signal.h>
31#include <sys/time.h>
32#include <stdlib.h>
33#include <dirent.h>
34#include <string.h>
35#include <ctype.h>
Len Brown88c32812012-03-29 21:44:40 -040036#include <sched.h>
Len Brown103a8fe2010-10-22 23:53:03 -040037
Len Brown103a8fe2010-10-22 23:53:03 -040038#define MSR_NEHALEM_PLATFORM_INFO 0xCE
39#define MSR_NEHALEM_TURBO_RATIO_LIMIT 0x1AD
40#define MSR_APERF 0xE8
41#define MSR_MPERF 0xE7
42#define MSR_PKG_C2_RESIDENCY 0x60D /* SNB only */
43#define MSR_PKG_C3_RESIDENCY 0x3F8
44#define MSR_PKG_C6_RESIDENCY 0x3F9
45#define MSR_PKG_C7_RESIDENCY 0x3FA /* SNB only */
46#define MSR_CORE_C3_RESIDENCY 0x3FC
47#define MSR_CORE_C6_RESIDENCY 0x3FD
48#define MSR_CORE_C7_RESIDENCY 0x3FE /* SNB only */
49
50char *proc_stat = "/proc/stat";
51unsigned int interval_sec = 5; /* set with -i interval_sec */
52unsigned int verbose; /* set with -v */
Len Browne23da032012-02-06 18:37:16 -050053unsigned int summary_only; /* set with -s */
Len Brown103a8fe2010-10-22 23:53:03 -040054unsigned int skip_c0;
55unsigned int skip_c1;
56unsigned int do_nhm_cstates;
57unsigned int do_snb_cstates;
58unsigned int has_aperf;
59unsigned int units = 1000000000; /* Ghz etc */
60unsigned int genuine_intel;
61unsigned int has_invariant_tsc;
62unsigned int do_nehalem_platform_info;
63unsigned int do_nehalem_turbo_ratio_limit;
64unsigned int extra_msr_offset;
65double bclk;
66unsigned int show_pkg;
67unsigned int show_core;
68unsigned int show_cpu;
Len Brownc98d5d92012-06-04 00:56:40 -040069unsigned int show_pkg_only;
70unsigned int show_core_only;
71char *output_buffer, *outp;
Len Brown103a8fe2010-10-22 23:53:03 -040072
73int aperf_mperf_unstable;
74int backwards_count;
75char *progname;
Len Brown103a8fe2010-10-22 23:53:03 -040076
Len Brownc98d5d92012-06-04 00:56:40 -040077cpu_set_t *cpu_present_set, *cpu_affinity_set;
78size_t cpu_present_setsize, cpu_affinity_setsize;
Len Brown103a8fe2010-10-22 23:53:03 -040079
Len Brownc98d5d92012-06-04 00:56:40 -040080struct thread_data {
81 unsigned long long tsc;
82 unsigned long long aperf;
83 unsigned long long mperf;
84 unsigned long long c1; /* derived */
85 unsigned long long extra_msr;
86 unsigned int cpu_id;
87 unsigned int flags;
88#define CPU_IS_FIRST_THREAD_IN_CORE 0x2
89#define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
90} *thread_even, *thread_odd;
Len Brown103a8fe2010-10-22 23:53:03 -040091
Len Brownc98d5d92012-06-04 00:56:40 -040092struct core_data {
93 unsigned long long c3;
94 unsigned long long c6;
95 unsigned long long c7;
96 unsigned int core_id;
97} *core_even, *core_odd;
Len Brown103a8fe2010-10-22 23:53:03 -040098
Len Brownc98d5d92012-06-04 00:56:40 -040099struct pkg_data {
100 unsigned long long pc2;
101 unsigned long long pc3;
102 unsigned long long pc6;
103 unsigned long long pc7;
104 unsigned int package_id;
105} *package_even, *package_odd;
106
107#define ODD_COUNTERS thread_odd, core_odd, package_odd
108#define EVEN_COUNTERS thread_even, core_even, package_even
109
110#define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
111 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
112 topo.num_threads_per_core + \
113 (core_no) * topo.num_threads_per_core + (thread_no))
114#define GET_CORE(core_base, core_no, pkg_no) \
115 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
116#define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
117
118struct system_summary {
119 struct thread_data threads;
120 struct core_data cores;
121 struct pkg_data packages;
122} sum, average;
123
124
125struct topo_params {
126 int num_packages;
127 int num_cpus;
128 int num_cores;
129 int max_cpu_num;
130 int num_cores_per_pkg;
131 int num_threads_per_core;
132} topo;
133
134struct timeval tv_even, tv_odd, tv_delta;
135
136void setup_all_buffers(void);
137
138int cpu_is_not_present(int cpu)
Len Brownd15cf7c2012-06-03 23:24:00 -0400139{
Len Brownc98d5d92012-06-04 00:56:40 -0400140 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brownd15cf7c2012-06-03 23:24:00 -0400141}
Len Brown88c32812012-03-29 21:44:40 -0400142/*
Len Brownc98d5d92012-06-04 00:56:40 -0400143 * run func(thread, core, package) in topology order
144 * skip non-present cpus
Len Brown88c32812012-03-29 21:44:40 -0400145 */
Len Brownd15cf7c2012-06-03 23:24:00 -0400146
Len Brownc98d5d92012-06-04 00:56:40 -0400147int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
148 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
Len Brown88c32812012-03-29 21:44:40 -0400149{
Len Brownc98d5d92012-06-04 00:56:40 -0400150 int retval, pkg_no, core_no, thread_no;
151
152 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
153 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
154 for (thread_no = 0; thread_no <
155 topo.num_threads_per_core; ++thread_no) {
156 struct thread_data *t;
157 struct core_data *c;
158 struct pkg_data *p;
159
160 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
161
162 if (cpu_is_not_present(t->cpu_id))
163 continue;
164
165 c = GET_CORE(core_base, core_no, pkg_no);
166 p = GET_PKG(pkg_base, pkg_no);
167
168 retval = func(t, c, p);
169 if (retval)
170 return retval;
171 }
172 }
173 }
174 return 0;
Len Brown88c32812012-03-29 21:44:40 -0400175}
176
177int cpu_migrate(int cpu)
178{
Len Brownc98d5d92012-06-04 00:56:40 -0400179 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
180 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
181 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
Len Brown88c32812012-03-29 21:44:40 -0400182 return -1;
183 else
184 return 0;
185}
186
Len Brown15aaa342012-03-29 22:19:58 -0400187int get_msr(int cpu, off_t offset, unsigned long long *msr)
Len Brown103a8fe2010-10-22 23:53:03 -0400188{
189 ssize_t retval;
Len Brown103a8fe2010-10-22 23:53:03 -0400190 char pathname[32];
191 int fd;
192
193 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
194 fd = open(pathname, O_RDONLY);
Len Brown15aaa342012-03-29 22:19:58 -0400195 if (fd < 0)
196 return -1;
Len Brown103a8fe2010-10-22 23:53:03 -0400197
Len Brown15aaa342012-03-29 22:19:58 -0400198 retval = pread(fd, msr, sizeof *msr, offset);
Len Brown103a8fe2010-10-22 23:53:03 -0400199 close(fd);
Len Brown15aaa342012-03-29 22:19:58 -0400200
201 if (retval != sizeof *msr)
202 return -1;
203
204 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400205}
206
Len Browna829eb42011-02-10 23:36:34 -0500207void print_header(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400208{
209 if (show_pkg)
Len Brownc98d5d92012-06-04 00:56:40 -0400210 outp += sprintf(outp, "pk");
Len Browne23da032012-02-06 18:37:16 -0500211 if (show_pkg)
Len Brownc98d5d92012-06-04 00:56:40 -0400212 outp += sprintf(outp, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400213 if (show_core)
Len Brownc98d5d92012-06-04 00:56:40 -0400214 outp += sprintf(outp, "cor");
Len Brown103a8fe2010-10-22 23:53:03 -0400215 if (show_cpu)
Len Brownc98d5d92012-06-04 00:56:40 -0400216 outp += sprintf(outp, " CPU");
Len Browne23da032012-02-06 18:37:16 -0500217 if (show_pkg || show_core || show_cpu)
Len Brownc98d5d92012-06-04 00:56:40 -0400218 outp += sprintf(outp, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400219 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400220 outp += sprintf(outp, " %%c0");
Len Brown103a8fe2010-10-22 23:53:03 -0400221 if (has_aperf)
Len Brownc98d5d92012-06-04 00:56:40 -0400222 outp += sprintf(outp, " GHz");
223 outp += sprintf(outp, " TSC");
Len Brown103a8fe2010-10-22 23:53:03 -0400224 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400225 outp += sprintf(outp, " %%c1");
Len Brown103a8fe2010-10-22 23:53:03 -0400226 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400227 outp += sprintf(outp, " %%c3");
Len Brown103a8fe2010-10-22 23:53:03 -0400228 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400229 outp += sprintf(outp, " %%c6");
Len Brown103a8fe2010-10-22 23:53:03 -0400230 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400231 outp += sprintf(outp, " %%c7");
Len Brown103a8fe2010-10-22 23:53:03 -0400232 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400233 outp += sprintf(outp, " %%pc2");
Len Brown103a8fe2010-10-22 23:53:03 -0400234 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400235 outp += sprintf(outp, " %%pc3");
Len Brown103a8fe2010-10-22 23:53:03 -0400236 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400237 outp += sprintf(outp, " %%pc6");
Len Brown103a8fe2010-10-22 23:53:03 -0400238 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400239 outp += sprintf(outp, " %%pc7");
Len Brown103a8fe2010-10-22 23:53:03 -0400240 if (extra_msr_offset)
Len Brownc98d5d92012-06-04 00:56:40 -0400241 outp += sprintf(outp, " MSR 0x%x ", extra_msr_offset);
Len Brown103a8fe2010-10-22 23:53:03 -0400242
Len Brownc98d5d92012-06-04 00:56:40 -0400243 outp += sprintf(outp, "\n");
Len Brown103a8fe2010-10-22 23:53:03 -0400244}
245
Len Brownc98d5d92012-06-04 00:56:40 -0400246int dump_counters(struct thread_data *t, struct core_data *c,
247 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400248{
Len Brownc98d5d92012-06-04 00:56:40 -0400249 fprintf(stderr, "t %p, c %p, p %p\n", t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400250
Len Brownc98d5d92012-06-04 00:56:40 -0400251 if (t) {
252 fprintf(stderr, "CPU: %d flags 0x%x\n", t->cpu_id, t->flags);
253 fprintf(stderr, "TSC: %016llX\n", t->tsc);
254 fprintf(stderr, "aperf: %016llX\n", t->aperf);
255 fprintf(stderr, "mperf: %016llX\n", t->mperf);
256 fprintf(stderr, "c1: %016llX\n", t->c1);
257 fprintf(stderr, "msr0x%x: %016llX\n",
258 extra_msr_offset, t->extra_msr);
259 }
Len Brown103a8fe2010-10-22 23:53:03 -0400260
Len Brownc98d5d92012-06-04 00:56:40 -0400261 if (c) {
262 fprintf(stderr, "core: %d\n", c->core_id);
263 fprintf(stderr, "c3: %016llX\n", c->c3);
264 fprintf(stderr, "c6: %016llX\n", c->c6);
265 fprintf(stderr, "c7: %016llX\n", c->c7);
266 }
267
268 if (p) {
269 fprintf(stderr, "package: %d\n", p->package_id);
270 fprintf(stderr, "pc2: %016llX\n", p->pc2);
271 fprintf(stderr, "pc3: %016llX\n", p->pc3);
272 fprintf(stderr, "pc6: %016llX\n", p->pc6);
273 fprintf(stderr, "pc7: %016llX\n", p->pc7);
274 }
275 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400276}
277
Len Browne23da032012-02-06 18:37:16 -0500278/*
279 * column formatting convention & formats
280 * package: "pk" 2 columns %2d
281 * core: "cor" 3 columns %3d
282 * CPU: "CPU" 3 columns %3d
283 * GHz: "GHz" 3 columns %3.2
284 * TSC: "TSC" 3 columns %3.2
285 * percentage " %pc3" %6.2
286 */
Len Brownc98d5d92012-06-04 00:56:40 -0400287int format_counters(struct thread_data *t, struct core_data *c,
288 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400289{
290 double interval_float;
291
Len Brownc98d5d92012-06-04 00:56:40 -0400292 /* if showing only 1st thread in core and this isn't one, bail out */
293 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
294 return 0;
295
296 /* if showing only 1st thread in pkg and this isn't one, bail out */
297 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
298 return 0;
299
Len Brown103a8fe2010-10-22 23:53:03 -0400300 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
301
Len Brownc98d5d92012-06-04 00:56:40 -0400302 /* topo columns, print blanks on 1st (average) line */
303 if (t == &average.threads) {
Len Brown103a8fe2010-10-22 23:53:03 -0400304 if (show_pkg)
Len Brownc98d5d92012-06-04 00:56:40 -0400305 outp += sprintf(outp, " ");
Len Browne23da032012-02-06 18:37:16 -0500306 if (show_pkg && show_core)
Len Brownc98d5d92012-06-04 00:56:40 -0400307 outp += sprintf(outp, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400308 if (show_core)
Len Brownc98d5d92012-06-04 00:56:40 -0400309 outp += sprintf(outp, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400310 if (show_cpu)
Len Brownc98d5d92012-06-04 00:56:40 -0400311 outp += sprintf(outp, " " " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400312 } else {
Len Brownc98d5d92012-06-04 00:56:40 -0400313 if (show_pkg) {
314 if (p)
315 outp += sprintf(outp, "%2d", p->package_id);
316 else
317 outp += sprintf(outp, " ");
318 }
Len Browne23da032012-02-06 18:37:16 -0500319 if (show_pkg && show_core)
Len Brownc98d5d92012-06-04 00:56:40 -0400320 outp += sprintf(outp, " ");
321 if (show_core) {
322 if (c)
323 outp += sprintf(outp, "%3d", c->core_id);
324 else
325 outp += sprintf(outp, " ");
326 }
Len Brown103a8fe2010-10-22 23:53:03 -0400327 if (show_cpu)
Len Brownc98d5d92012-06-04 00:56:40 -0400328 outp += sprintf(outp, " %3d", t->cpu_id);
Len Brown103a8fe2010-10-22 23:53:03 -0400329 }
330
331 /* %c0 */
332 if (do_nhm_cstates) {
Len Browne23da032012-02-06 18:37:16 -0500333 if (show_pkg || show_core || show_cpu)
Len Brownc98d5d92012-06-04 00:56:40 -0400334 outp += sprintf(outp, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400335 if (!skip_c0)
Len Brownc98d5d92012-06-04 00:56:40 -0400336 outp += sprintf(outp, "%6.2f", 100.0 * t->mperf/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400337 else
Len Brownc98d5d92012-06-04 00:56:40 -0400338 outp += sprintf(outp, " ****");
Len Brown103a8fe2010-10-22 23:53:03 -0400339 }
340
341 /* GHz */
342 if (has_aperf) {
343 if (!aperf_mperf_unstable) {
Len Brownc98d5d92012-06-04 00:56:40 -0400344 outp += sprintf(outp, " %3.2f",
345 1.0 * t->tsc / units * t->aperf /
346 t->mperf / interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400347 } else {
Len Brownc98d5d92012-06-04 00:56:40 -0400348 if (t->aperf > t->tsc || t->mperf > t->tsc) {
349 outp += sprintf(outp, " ***");
Len Brown103a8fe2010-10-22 23:53:03 -0400350 } else {
Len Brownc98d5d92012-06-04 00:56:40 -0400351 outp += sprintf(outp, "%3.1f*",
352 1.0 * t->tsc /
353 units * t->aperf /
354 t->mperf / interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400355 }
356 }
357 }
358
359 /* TSC */
Len Brownc98d5d92012-06-04 00:56:40 -0400360 outp += sprintf(outp, "%5.2f", 1.0 * t->tsc/units/interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400361
362 if (do_nhm_cstates) {
363 if (!skip_c1)
Len Brownc98d5d92012-06-04 00:56:40 -0400364 outp += sprintf(outp, " %6.2f", 100.0 * t->c1/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400365 else
Len Brownc98d5d92012-06-04 00:56:40 -0400366 outp += sprintf(outp, " ****");
Len Brown103a8fe2010-10-22 23:53:03 -0400367 }
Len Brownc98d5d92012-06-04 00:56:40 -0400368
369 /* print per-core data only for 1st thread in core */
370 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
371 goto done;
372
Len Brown103a8fe2010-10-22 23:53:03 -0400373 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400374 outp += sprintf(outp, " %6.2f", 100.0 * c->c3/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400375 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400376 outp += sprintf(outp, " %6.2f", 100.0 * c->c6/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400377 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400378 outp += sprintf(outp, " %6.2f", 100.0 * c->c7/t->tsc);
379
380 /* print per-package data only for 1st core in package */
381 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
382 goto done;
383
Len Brown103a8fe2010-10-22 23:53:03 -0400384 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400385 outp += sprintf(outp, " %6.2f", 100.0 * p->pc2/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400386 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400387 outp += sprintf(outp, " %6.2f", 100.0 * p->pc3/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400388 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400389 outp += sprintf(outp, " %6.2f", 100.0 * p->pc6/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400390 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400391 outp += sprintf(outp, " %6.2f", 100.0 * p->pc7/t->tsc);
392done:
Len Brown103a8fe2010-10-22 23:53:03 -0400393 if (extra_msr_offset)
Len Brownc98d5d92012-06-04 00:56:40 -0400394 outp += sprintf(outp, " 0x%016llx", t->extra_msr);
395 outp += sprintf(outp, "\n");
396
397 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400398}
399
Len Brownc98d5d92012-06-04 00:56:40 -0400400void flush_stdout()
Len Brown103a8fe2010-10-22 23:53:03 -0400401{
Len Brownc98d5d92012-06-04 00:56:40 -0400402 fputs(output_buffer, stdout);
403 outp = output_buffer;
404}
405void flush_stderr()
406{
407 fputs(output_buffer, stderr);
408 outp = output_buffer;
409}
410void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
411{
Len Browne23da032012-02-06 18:37:16 -0500412 static int printed;
Len Brown103a8fe2010-10-22 23:53:03 -0400413
Len Browne23da032012-02-06 18:37:16 -0500414 if (!printed || !summary_only)
415 print_header();
Len Brown103a8fe2010-10-22 23:53:03 -0400416
Len Brownc98d5d92012-06-04 00:56:40 -0400417 if (topo.num_cpus > 1)
418 format_counters(&average.threads, &average.cores,
419 &average.packages);
Len Brown103a8fe2010-10-22 23:53:03 -0400420
Len Browne23da032012-02-06 18:37:16 -0500421 printed = 1;
422
423 if (summary_only)
424 return;
425
Len Brownc98d5d92012-06-04 00:56:40 -0400426 for_all_cpus(format_counters, t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400427}
428
Len Brownc98d5d92012-06-04 00:56:40 -0400429void
430delta_package(struct pkg_data *new, struct pkg_data *old)
Len Brown103a8fe2010-10-22 23:53:03 -0400431{
Len Brownc98d5d92012-06-04 00:56:40 -0400432 old->pc2 = new->pc2 - old->pc2;
433 old->pc3 = new->pc3 - old->pc3;
434 old->pc6 = new->pc6 - old->pc6;
435 old->pc7 = new->pc7 - old->pc7;
436}
Len Brown103a8fe2010-10-22 23:53:03 -0400437
Len Brownc98d5d92012-06-04 00:56:40 -0400438void
439delta_core(struct core_data *new, struct core_data *old)
440{
441 old->c3 = new->c3 - old->c3;
442 old->c6 = new->c6 - old->c6;
443 old->c7 = new->c7 - old->c7;
444}
Len Brown103a8fe2010-10-22 23:53:03 -0400445
Len Brownc3ae3312012-06-13 21:31:46 -0400446/*
447 * old = new - old
448 */
Len Brownc98d5d92012-06-04 00:56:40 -0400449void
450delta_thread(struct thread_data *new, struct thread_data *old,
451 struct core_data *core_delta)
452{
453 old->tsc = new->tsc - old->tsc;
Len Brown103a8fe2010-10-22 23:53:03 -0400454
Len Brownc98d5d92012-06-04 00:56:40 -0400455 /* check for TSC < 1 Mcycles over interval */
456 if (old->tsc < (1000 * 1000)) {
457 fprintf(stderr, "Insanely slow TSC rate, TSC stops in idle?\n");
458 fprintf(stderr, "You can disable all c-states by booting with \"idle=poll\"\n");
459 fprintf(stderr, "or just the deep ones with \"processor.max_cstate=1\"\n");
460 exit(-3);
461 }
Len Brown103a8fe2010-10-22 23:53:03 -0400462
Len Brownc98d5d92012-06-04 00:56:40 -0400463 old->c1 = new->c1 - old->c1;
Len Brown103a8fe2010-10-22 23:53:03 -0400464
Len Brownc98d5d92012-06-04 00:56:40 -0400465 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
466 old->aperf = new->aperf - old->aperf;
467 old->mperf = new->mperf - old->mperf;
468 } else {
Len Brown103a8fe2010-10-22 23:53:03 -0400469
Len Brownc98d5d92012-06-04 00:56:40 -0400470 if (!aperf_mperf_unstable) {
471 fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname);
472 fprintf(stderr, "* Frequency results do not cover entire interval *\n");
473 fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n");
474
475 aperf_mperf_unstable = 1;
476 }
Len Brown103a8fe2010-10-22 23:53:03 -0400477 /*
Len Brownc98d5d92012-06-04 00:56:40 -0400478 * mperf delta is likely a huge "positive" number
479 * can not use it for calculating c0 time
Len Brown103a8fe2010-10-22 23:53:03 -0400480 */
Len Brownc98d5d92012-06-04 00:56:40 -0400481 skip_c0 = 1;
482 skip_c1 = 1;
483 }
Len Brown103a8fe2010-10-22 23:53:03 -0400484
Len Brown103a8fe2010-10-22 23:53:03 -0400485
Len Brownc98d5d92012-06-04 00:56:40 -0400486 /*
Len Brownc3ae3312012-06-13 21:31:46 -0400487 * As counter collection is not atomic,
488 * it is possible for mperf's non-halted cycles + idle states
Len Brownc98d5d92012-06-04 00:56:40 -0400489 * to exceed TSC's all cycles: show c1 = 0% in that case.
490 */
Len Brownc3ae3312012-06-13 21:31:46 -0400491 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
Len Brownc98d5d92012-06-04 00:56:40 -0400492 old->c1 = 0;
493 else {
494 /* normal case, derive c1 */
495 old->c1 = old->tsc - old->mperf - core_delta->c3
496 - core_delta->c6 - core_delta->c7;
497 }
Len Brownc3ae3312012-06-13 21:31:46 -0400498
Len Brownc98d5d92012-06-04 00:56:40 -0400499 if (old->mperf == 0) {
Len Brownc3ae3312012-06-13 21:31:46 -0400500 if (verbose > 1) fprintf(stderr, "cpu%d MPERF 0!\n", old->cpu_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400501 old->mperf = 1; /* divide by 0 protection */
502 }
503
504 /*
505 * for "extra msr", just copy the latest w/o subtracting
506 */
507 old->extra_msr = new->extra_msr;
508}
509
510int delta_cpu(struct thread_data *t, struct core_data *c,
511 struct pkg_data *p, struct thread_data *t2,
512 struct core_data *c2, struct pkg_data *p2)
513{
514 /* calculate core delta only for 1st thread in core */
515 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
516 delta_core(c, c2);
517
518 /* always calculate thread delta */
519 delta_thread(t, t2, c2); /* c2 is core delta */
520
521 /* calculate package delta only for 1st core in package */
522 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
523 delta_package(p, p2);
524
525 return 0;
526}
527
528void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
529{
530 t->tsc = 0;
531 t->aperf = 0;
532 t->mperf = 0;
533 t->c1 = 0;
534
535 /* tells format_counters to dump all fields from this set */
536 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
537
538 c->c3 = 0;
539 c->c6 = 0;
540 c->c7 = 0;
541
542 p->pc2 = 0;
543 p->pc3 = 0;
544 p->pc6 = 0;
545 p->pc7 = 0;
546}
547int sum_counters(struct thread_data *t, struct core_data *c,
548 struct pkg_data *p)
549{
550 average.threads.tsc += t->tsc;
551 average.threads.aperf += t->aperf;
552 average.threads.mperf += t->mperf;
553 average.threads.c1 += t->c1;
554
555 /* sum per-core values only for 1st thread in core */
556 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
557 return 0;
558
559 average.cores.c3 += c->c3;
560 average.cores.c6 += c->c6;
561 average.cores.c7 += c->c7;
562
563 /* sum per-pkg values only for 1st core in pkg */
564 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
565 return 0;
566
567 average.packages.pc2 += p->pc2;
568 average.packages.pc3 += p->pc3;
569 average.packages.pc6 += p->pc6;
570 average.packages.pc7 += p->pc7;
571
572 return 0;
573}
574/*
575 * sum the counters for all cpus in the system
576 * compute the weighted average
577 */
578void compute_average(struct thread_data *t, struct core_data *c,
579 struct pkg_data *p)
580{
581 clear_counters(&average.threads, &average.cores, &average.packages);
582
583 for_all_cpus(sum_counters, t, c, p);
584
585 average.threads.tsc /= topo.num_cpus;
586 average.threads.aperf /= topo.num_cpus;
587 average.threads.mperf /= topo.num_cpus;
588 average.threads.c1 /= topo.num_cpus;
589
590 average.cores.c3 /= topo.num_cores;
591 average.cores.c6 /= topo.num_cores;
592 average.cores.c7 /= topo.num_cores;
593
594 average.packages.pc2 /= topo.num_packages;
595 average.packages.pc3 /= topo.num_packages;
596 average.packages.pc6 /= topo.num_packages;
597 average.packages.pc7 /= topo.num_packages;
598}
599
600static unsigned long long rdtsc(void)
601{
602 unsigned int low, high;
603
604 asm volatile("rdtsc" : "=a" (low), "=d" (high));
605
606 return low | ((unsigned long long)high) << 32;
607}
608
609
610/*
611 * get_counters(...)
612 * migrate to cpu
613 * acquire and record local counters for that cpu
614 */
615int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
616{
617 int cpu = t->cpu_id;
618
619 if (cpu_migrate(cpu))
620 return -1;
621
622 t->tsc = rdtsc(); /* we are running on local CPU of interest */
623
624 if (has_aperf) {
625 if (get_msr(cpu, MSR_APERF, &t->aperf))
626 return -3;
627 if (get_msr(cpu, MSR_MPERF, &t->mperf))
628 return -4;
629 }
630
631 if (extra_msr_offset)
632 if (get_msr(cpu, extra_msr_offset, &t->extra_msr))
633 return -5;
634
635 /* collect core counters only for 1st thread in core */
636 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
637 return 0;
638
639 if (do_nhm_cstates) {
640 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
641 return -6;
642 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
643 return -7;
644 }
645
646 if (do_snb_cstates)
647 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
648 return -8;
649
650 /* collect package counters only for 1st core in package */
651 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
652 return 0;
653
654 if (do_nhm_cstates) {
655 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
656 return -9;
657 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
658 return -10;
659 }
660 if (do_snb_cstates) {
661 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
662 return -11;
663 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
664 return -12;
Len Brown103a8fe2010-10-22 23:53:03 -0400665 }
666 return 0;
667}
668
Len Brownc98d5d92012-06-04 00:56:40 -0400669void print_verbose_header(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400670{
671 unsigned long long msr;
672 unsigned int ratio;
673
674 if (!do_nehalem_platform_info)
675 return;
676
Len Brown15aaa342012-03-29 22:19:58 -0400677 get_msr(0, MSR_NEHALEM_PLATFORM_INFO, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -0400678
679 ratio = (msr >> 40) & 0xFF;
680 fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency\n",
681 ratio, bclk, ratio * bclk);
682
683 ratio = (msr >> 8) & 0xFF;
684 fprintf(stderr, "%d * %.0f = %.0f MHz TSC frequency\n",
685 ratio, bclk, ratio * bclk);
686
687 if (verbose > 1)
688 fprintf(stderr, "MSR_NEHALEM_PLATFORM_INFO: 0x%llx\n", msr);
689
690 if (!do_nehalem_turbo_ratio_limit)
691 return;
692
Len Brown15aaa342012-03-29 22:19:58 -0400693 get_msr(0, MSR_NEHALEM_TURBO_RATIO_LIMIT, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -0400694
695 ratio = (msr >> 24) & 0xFF;
696 if (ratio)
697 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
698 ratio, bclk, ratio * bclk);
699
700 ratio = (msr >> 16) & 0xFF;
701 if (ratio)
702 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
703 ratio, bclk, ratio * bclk);
704
705 ratio = (msr >> 8) & 0xFF;
706 if (ratio)
707 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
708 ratio, bclk, ratio * bclk);
709
710 ratio = (msr >> 0) & 0xFF;
711 if (ratio)
712 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
713 ratio, bclk, ratio * bclk);
714
715}
716
Len Brownc98d5d92012-06-04 00:56:40 -0400717void free_all_buffers(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400718{
Len Brownc98d5d92012-06-04 00:56:40 -0400719 CPU_FREE(cpu_present_set);
720 cpu_present_set = NULL;
721 cpu_present_set = 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400722
Len Brownc98d5d92012-06-04 00:56:40 -0400723 CPU_FREE(cpu_affinity_set);
724 cpu_affinity_set = NULL;
725 cpu_affinity_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400726
Len Brownc98d5d92012-06-04 00:56:40 -0400727 free(thread_even);
728 free(core_even);
729 free(package_even);
730
731 thread_even = NULL;
732 core_even = NULL;
733 package_even = NULL;
734
735 free(thread_odd);
736 free(core_odd);
737 free(package_odd);
738
739 thread_odd = NULL;
740 core_odd = NULL;
741 package_odd = NULL;
742
743 free(output_buffer);
744 output_buffer = NULL;
745 outp = NULL;
Len Brown103a8fe2010-10-22 23:53:03 -0400746}
747
Len Brownc98d5d92012-06-04 00:56:40 -0400748/*
749 * cpu_is_first_sibling_in_core(cpu)
750 * return 1 if given CPU is 1st HT sibling in the core
751 */
752int cpu_is_first_sibling_in_core(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -0400753{
Len Brownc98d5d92012-06-04 00:56:40 -0400754 char path[64];
755 FILE *filep;
756 int first_cpu;
Len Brown103a8fe2010-10-22 23:53:03 -0400757
Len Brownc98d5d92012-06-04 00:56:40 -0400758 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
759 filep = fopen(path, "r");
760 if (filep == NULL) {
761 perror(path);
762 exit(1);
763 }
764 fscanf(filep, "%d", &first_cpu);
765 fclose(filep);
766 return (cpu == first_cpu);
Len Brown103a8fe2010-10-22 23:53:03 -0400767}
768
Len Brownc98d5d92012-06-04 00:56:40 -0400769/*
770 * cpu_is_first_core_in_package(cpu)
771 * return 1 if given CPU is 1st core in package
772 */
773int cpu_is_first_core_in_package(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -0400774{
Len Brownc98d5d92012-06-04 00:56:40 -0400775 char path[64];
776 FILE *filep;
777 int first_cpu;
Len Brown103a8fe2010-10-22 23:53:03 -0400778
Len Brownc98d5d92012-06-04 00:56:40 -0400779 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
780 filep = fopen(path, "r");
781 if (filep == NULL) {
782 perror(path);
Len Brown103a8fe2010-10-22 23:53:03 -0400783 exit(1);
784 }
Len Brownc98d5d92012-06-04 00:56:40 -0400785 fscanf(filep, "%d", &first_cpu);
786 fclose(filep);
787 return (cpu == first_cpu);
Len Brown103a8fe2010-10-22 23:53:03 -0400788}
789
790int get_physical_package_id(int cpu)
791{
Len Brownc98d5d92012-06-04 00:56:40 -0400792 char path[80];
Len Brown103a8fe2010-10-22 23:53:03 -0400793 FILE *filep;
794 int pkg;
795
796 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
797 filep = fopen(path, "r");
798 if (filep == NULL) {
799 perror(path);
800 exit(1);
801 }
802 fscanf(filep, "%d", &pkg);
803 fclose(filep);
804 return pkg;
805}
806
807int get_core_id(int cpu)
808{
Len Brownc98d5d92012-06-04 00:56:40 -0400809 char path[80];
Len Brown103a8fe2010-10-22 23:53:03 -0400810 FILE *filep;
811 int core;
812
813 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
814 filep = fopen(path, "r");
815 if (filep == NULL) {
816 perror(path);
817 exit(1);
818 }
819 fscanf(filep, "%d", &core);
820 fclose(filep);
821 return core;
822}
823
Len Brownc98d5d92012-06-04 00:56:40 -0400824int get_num_ht_siblings(int cpu)
825{
826 char path[80];
827 FILE *filep;
828 int sib1, sib2;
829 int matches;
830 char character;
831
832 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
833 filep = fopen(path, "r");
834 if (filep == NULL) {
835 perror(path);
836 exit(1);
837 }
838 /*
839 * file format:
840 * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4)
841 * otherwinse 1 sibling (self).
842 */
843 matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2);
844
845 fclose(filep);
846
847 if (matches == 3)
848 return 2;
849 else
850 return 1;
851}
852
Len Brown103a8fe2010-10-22 23:53:03 -0400853/*
Len Brownc98d5d92012-06-04 00:56:40 -0400854 * run func(thread, core, package) in topology order
855 * skip non-present cpus
Len Brown103a8fe2010-10-22 23:53:03 -0400856 */
857
Len Brownc98d5d92012-06-04 00:56:40 -0400858int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
859 struct pkg_data *, struct thread_data *, struct core_data *,
860 struct pkg_data *), struct thread_data *thread_base,
861 struct core_data *core_base, struct pkg_data *pkg_base,
862 struct thread_data *thread_base2, struct core_data *core_base2,
863 struct pkg_data *pkg_base2)
864{
865 int retval, pkg_no, core_no, thread_no;
866
867 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
868 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
869 for (thread_no = 0; thread_no <
870 topo.num_threads_per_core; ++thread_no) {
871 struct thread_data *t, *t2;
872 struct core_data *c, *c2;
873 struct pkg_data *p, *p2;
874
875 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
876
877 if (cpu_is_not_present(t->cpu_id))
878 continue;
879
880 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
881
882 c = GET_CORE(core_base, core_no, pkg_no);
883 c2 = GET_CORE(core_base2, core_no, pkg_no);
884
885 p = GET_PKG(pkg_base, pkg_no);
886 p2 = GET_PKG(pkg_base2, pkg_no);
887
888 retval = func(t, c, p, t2, c2, p2);
889 if (retval)
890 return retval;
891 }
892 }
893 }
894 return 0;
895}
896
897/*
898 * run func(cpu) on every cpu in /proc/stat
899 * return max_cpu number
900 */
901int for_all_proc_cpus(int (func)(int))
Len Brown103a8fe2010-10-22 23:53:03 -0400902{
903 FILE *fp;
Len Brownc98d5d92012-06-04 00:56:40 -0400904 int cpu_num;
Len Brown103a8fe2010-10-22 23:53:03 -0400905 int retval;
906
907 fp = fopen(proc_stat, "r");
908 if (fp == NULL) {
909 perror(proc_stat);
910 exit(1);
911 }
912
913 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
914 if (retval != 0) {
915 perror("/proc/stat format");
916 exit(1);
917 }
918
Len Brownc98d5d92012-06-04 00:56:40 -0400919 while (1) {
920 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 -0400921 if (retval != 1)
922 break;
923
Len Brownc98d5d92012-06-04 00:56:40 -0400924 retval = func(cpu_num);
925 if (retval) {
926 fclose(fp);
927 return(retval);
928 }
Len Brown103a8fe2010-10-22 23:53:03 -0400929 }
930 fclose(fp);
Len Brownc98d5d92012-06-04 00:56:40 -0400931 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400932}
933
934void re_initialize(void)
935{
Len Brownc98d5d92012-06-04 00:56:40 -0400936 free_all_buffers();
937 setup_all_buffers();
938 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
Len Brown103a8fe2010-10-22 23:53:03 -0400939}
940
Len Brownc98d5d92012-06-04 00:56:40 -0400941
Len Brown103a8fe2010-10-22 23:53:03 -0400942/*
Len Brownc98d5d92012-06-04 00:56:40 -0400943 * count_cpus()
944 * remember the last one seen, it will be the max
Len Brown103a8fe2010-10-22 23:53:03 -0400945 */
Len Brownc98d5d92012-06-04 00:56:40 -0400946int count_cpus(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -0400947{
Len Brownc98d5d92012-06-04 00:56:40 -0400948 if (topo.max_cpu_num < cpu)
949 topo.max_cpu_num = cpu;
Len Brown103a8fe2010-10-22 23:53:03 -0400950
Len Brownc98d5d92012-06-04 00:56:40 -0400951 topo.num_cpus += 1;
952 return 0;
953}
954int mark_cpu_present(int cpu)
955{
956 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brown15aaa342012-03-29 22:19:58 -0400957 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400958}
959
960void turbostat_loop()
961{
Len Brownc98d5d92012-06-04 00:56:40 -0400962 int retval;
963
Len Brown103a8fe2010-10-22 23:53:03 -0400964restart:
Len Brownc98d5d92012-06-04 00:56:40 -0400965 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
966 if (retval) {
967 re_initialize();
968 goto restart;
969 }
Len Brown103a8fe2010-10-22 23:53:03 -0400970 gettimeofday(&tv_even, (struct timezone *)NULL);
971
972 while (1) {
Len Brownc98d5d92012-06-04 00:56:40 -0400973 if (for_all_proc_cpus(cpu_is_not_present)) {
Len Brown103a8fe2010-10-22 23:53:03 -0400974 re_initialize();
975 goto restart;
976 }
977 sleep(interval_sec);
Len Brownc98d5d92012-06-04 00:56:40 -0400978 retval = for_all_cpus(get_counters, ODD_COUNTERS);
979 if (retval) {
Len Brown15aaa342012-03-29 22:19:58 -0400980 re_initialize();
981 goto restart;
982 }
Len Brown103a8fe2010-10-22 23:53:03 -0400983 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -0400984 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -0400985 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
986 compute_average(EVEN_COUNTERS);
987 format_all_counters(EVEN_COUNTERS);
988 flush_stdout();
Len Brown15aaa342012-03-29 22:19:58 -0400989 sleep(interval_sec);
Len Brownc98d5d92012-06-04 00:56:40 -0400990 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
991 if (retval) {
Len Brown103a8fe2010-10-22 23:53:03 -0400992 re_initialize();
993 goto restart;
994 }
Len Brown103a8fe2010-10-22 23:53:03 -0400995 gettimeofday(&tv_even, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -0400996 timersub(&tv_even, &tv_odd, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -0400997 for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS);
998 compute_average(ODD_COUNTERS);
999 format_all_counters(ODD_COUNTERS);
1000 flush_stdout();
Len Brown103a8fe2010-10-22 23:53:03 -04001001 }
1002}
1003
1004void check_dev_msr()
1005{
1006 struct stat sb;
1007
1008 if (stat("/dev/cpu/0/msr", &sb)) {
1009 fprintf(stderr, "no /dev/cpu/0/msr\n");
1010 fprintf(stderr, "Try \"# modprobe msr\"\n");
1011 exit(-5);
1012 }
1013}
1014
1015void check_super_user()
1016{
1017 if (getuid() != 0) {
1018 fprintf(stderr, "must be root\n");
1019 exit(-6);
1020 }
1021}
1022
1023int has_nehalem_turbo_ratio_limit(unsigned int family, unsigned int model)
1024{
1025 if (!genuine_intel)
1026 return 0;
1027
1028 if (family != 6)
1029 return 0;
1030
1031 switch (model) {
1032 case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
1033 case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
1034 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
1035 case 0x25: /* Westmere Client - Clarkdale, Arrandale */
1036 case 0x2C: /* Westmere EP - Gulftown */
1037 case 0x2A: /* SNB */
1038 case 0x2D: /* SNB Xeon */
Len Brown553575f2011-11-18 03:32:01 -05001039 case 0x3A: /* IVB */
Len Brown13006512012-09-26 18:11:31 -04001040 case 0x3E: /* IVB Xeon */
Len Brown103a8fe2010-10-22 23:53:03 -04001041 return 1;
1042 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1043 case 0x2F: /* Westmere-EX Xeon - Eagleton */
1044 default:
1045 return 0;
1046 }
1047}
1048
1049int is_snb(unsigned int family, unsigned int model)
1050{
1051 if (!genuine_intel)
1052 return 0;
1053
1054 switch (model) {
1055 case 0x2A:
1056 case 0x2D:
Len Brown650a37f2012-06-03 23:34:44 -04001057 case 0x3A: /* IVB */
Len Brown13006512012-09-26 18:11:31 -04001058 case 0x3E: /* IVB Xeon */
Len Brown103a8fe2010-10-22 23:53:03 -04001059 return 1;
1060 }
1061 return 0;
1062}
1063
1064double discover_bclk(unsigned int family, unsigned int model)
1065{
1066 if (is_snb(family, model))
1067 return 100.00;
1068 else
1069 return 133.33;
1070}
1071
1072void check_cpuid()
1073{
1074 unsigned int eax, ebx, ecx, edx, max_level;
1075 unsigned int fms, family, model, stepping;
1076
1077 eax = ebx = ecx = edx = 0;
1078
1079 asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0));
1080
1081 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
1082 genuine_intel = 1;
1083
1084 if (verbose)
1085 fprintf(stderr, "%.4s%.4s%.4s ",
1086 (char *)&ebx, (char *)&edx, (char *)&ecx);
1087
1088 asm("cpuid" : "=a" (fms), "=c" (ecx), "=d" (edx) : "a" (1) : "ebx");
1089 family = (fms >> 8) & 0xf;
1090 model = (fms >> 4) & 0xf;
1091 stepping = fms & 0xf;
1092 if (family == 6 || family == 0xf)
1093 model += ((fms >> 16) & 0xf) << 4;
1094
1095 if (verbose)
1096 fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
1097 max_level, family, model, stepping, family, model, stepping);
1098
1099 if (!(edx & (1 << 5))) {
1100 fprintf(stderr, "CPUID: no MSR\n");
1101 exit(1);
1102 }
1103
1104 /*
1105 * check max extended function levels of CPUID.
1106 * This is needed to check for invariant TSC.
1107 * This check is valid for both Intel and AMD.
1108 */
1109 ebx = ecx = edx = 0;
1110 asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000000));
1111
1112 if (max_level < 0x80000007) {
1113 fprintf(stderr, "CPUID: no invariant TSC (max_level 0x%x)\n", max_level);
1114 exit(1);
1115 }
1116
1117 /*
1118 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
1119 * this check is valid for both Intel and AMD
1120 */
1121 asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000007));
Thomas Renninger8209e052011-01-21 15:11:19 +01001122 has_invariant_tsc = edx & (1 << 8);
Len Brown103a8fe2010-10-22 23:53:03 -04001123
1124 if (!has_invariant_tsc) {
1125 fprintf(stderr, "No invariant TSC\n");
1126 exit(1);
1127 }
1128
1129 /*
1130 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
1131 * this check is valid for both Intel and AMD
1132 */
1133
1134 asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x6));
Thomas Renninger8209e052011-01-21 15:11:19 +01001135 has_aperf = ecx & (1 << 0);
Len Brown103a8fe2010-10-22 23:53:03 -04001136 if (!has_aperf) {
1137 fprintf(stderr, "No APERF MSR\n");
1138 exit(1);
1139 }
1140
1141 do_nehalem_platform_info = genuine_intel && has_invariant_tsc;
1142 do_nhm_cstates = genuine_intel; /* all Intel w/ non-stop TSC have NHM counters */
1143 do_snb_cstates = is_snb(family, model);
1144 bclk = discover_bclk(family, model);
1145
1146 do_nehalem_turbo_ratio_limit = has_nehalem_turbo_ratio_limit(family, model);
1147}
1148
1149
1150void usage()
1151{
1152 fprintf(stderr, "%s: [-v] [-M MSR#] [-i interval_sec | command ...]\n",
1153 progname);
1154 exit(1);
1155}
1156
1157
1158/*
1159 * in /dev/cpu/ return success for names that are numbers
1160 * ie. filter out ".", "..", "microcode".
1161 */
1162int dir_filter(const struct dirent *dirp)
1163{
1164 if (isdigit(dirp->d_name[0]))
1165 return 1;
1166 else
1167 return 0;
1168}
1169
1170int open_dev_cpu_msr(int dummy1)
1171{
1172 return 0;
1173}
1174
Len Brownc98d5d92012-06-04 00:56:40 -04001175void topology_probe()
1176{
1177 int i;
1178 int max_core_id = 0;
1179 int max_package_id = 0;
1180 int max_siblings = 0;
1181 struct cpu_topology {
1182 int core_id;
1183 int physical_package_id;
1184 } *cpus;
1185
1186 /* Initialize num_cpus, max_cpu_num */
1187 topo.num_cpus = 0;
1188 topo.max_cpu_num = 0;
1189 for_all_proc_cpus(count_cpus);
1190 if (!summary_only && topo.num_cpus > 1)
1191 show_cpu = 1;
1192
1193 if (verbose > 1)
1194 fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
1195
1196 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
1197 if (cpus == NULL) {
1198 perror("calloc cpus");
1199 exit(1);
1200 }
1201
1202 /*
1203 * Allocate and initialize cpu_present_set
1204 */
1205 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
1206 if (cpu_present_set == NULL) {
1207 perror("CPU_ALLOC");
1208 exit(3);
1209 }
1210 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
1211 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
1212 for_all_proc_cpus(mark_cpu_present);
1213
1214 /*
1215 * Allocate and initialize cpu_affinity_set
1216 */
1217 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
1218 if (cpu_affinity_set == NULL) {
1219 perror("CPU_ALLOC");
1220 exit(3);
1221 }
1222 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
1223 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
1224
1225
1226 /*
1227 * For online cpus
1228 * find max_core_id, max_package_id
1229 */
1230 for (i = 0; i <= topo.max_cpu_num; ++i) {
1231 int siblings;
1232
1233 if (cpu_is_not_present(i)) {
1234 if (verbose > 1)
1235 fprintf(stderr, "cpu%d NOT PRESENT\n", i);
1236 continue;
1237 }
1238 cpus[i].core_id = get_core_id(i);
1239 if (cpus[i].core_id > max_core_id)
1240 max_core_id = cpus[i].core_id;
1241
1242 cpus[i].physical_package_id = get_physical_package_id(i);
1243 if (cpus[i].physical_package_id > max_package_id)
1244 max_package_id = cpus[i].physical_package_id;
1245
1246 siblings = get_num_ht_siblings(i);
1247 if (siblings > max_siblings)
1248 max_siblings = siblings;
1249 if (verbose > 1)
1250 fprintf(stderr, "cpu %d pkg %d core %d\n",
1251 i, cpus[i].physical_package_id, cpus[i].core_id);
1252 }
1253 topo.num_cores_per_pkg = max_core_id + 1;
1254 if (verbose > 1)
1255 fprintf(stderr, "max_core_id %d, sizing for %d cores per package\n",
1256 max_core_id, topo.num_cores_per_pkg);
1257 if (!summary_only && topo.num_cores_per_pkg > 1)
1258 show_core = 1;
1259
1260 topo.num_packages = max_package_id + 1;
1261 if (verbose > 1)
1262 fprintf(stderr, "max_package_id %d, sizing for %d packages\n",
1263 max_package_id, topo.num_packages);
1264 if (!summary_only && topo.num_packages > 1)
1265 show_pkg = 1;
1266
1267 topo.num_threads_per_core = max_siblings;
1268 if (verbose > 1)
1269 fprintf(stderr, "max_siblings %d\n", max_siblings);
1270
1271 free(cpus);
1272}
1273
1274void
1275allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
1276{
1277 int i;
1278
1279 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
1280 topo.num_packages, sizeof(struct thread_data));
1281 if (*t == NULL)
1282 goto error;
1283
1284 for (i = 0; i < topo.num_threads_per_core *
1285 topo.num_cores_per_pkg * topo.num_packages; i++)
1286 (*t)[i].cpu_id = -1;
1287
1288 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
1289 sizeof(struct core_data));
1290 if (*c == NULL)
1291 goto error;
1292
1293 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
1294 (*c)[i].core_id = -1;
1295
1296 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
1297 if (*p == NULL)
1298 goto error;
1299
1300 for (i = 0; i < topo.num_packages; i++)
1301 (*p)[i].package_id = i;
1302
1303 return;
1304error:
1305 perror("calloc counters");
1306 exit(1);
1307}
1308/*
1309 * init_counter()
1310 *
1311 * set cpu_id, core_num, pkg_num
1312 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
1313 *
1314 * increment topo.num_cores when 1st core in pkg seen
1315 */
1316void init_counter(struct thread_data *thread_base, struct core_data *core_base,
1317 struct pkg_data *pkg_base, int thread_num, int core_num,
1318 int pkg_num, int cpu_id)
1319{
1320 struct thread_data *t;
1321 struct core_data *c;
1322 struct pkg_data *p;
1323
1324 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
1325 c = GET_CORE(core_base, core_num, pkg_num);
1326 p = GET_PKG(pkg_base, pkg_num);
1327
1328 t->cpu_id = cpu_id;
1329 if (thread_num == 0) {
1330 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
1331 if (cpu_is_first_core_in_package(cpu_id))
1332 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
1333 }
1334
1335 c->core_id = core_num;
1336 p->package_id = pkg_num;
1337}
1338
1339
1340int initialize_counters(int cpu_id)
1341{
1342 int my_thread_id, my_core_id, my_package_id;
1343
1344 my_package_id = get_physical_package_id(cpu_id);
1345 my_core_id = get_core_id(cpu_id);
1346
1347 if (cpu_is_first_sibling_in_core(cpu_id)) {
1348 my_thread_id = 0;
1349 topo.num_cores++;
1350 } else {
1351 my_thread_id = 1;
1352 }
1353
1354 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
1355 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
1356 return 0;
1357}
1358
1359void allocate_output_buffer()
1360{
1361 output_buffer = calloc(1, (1 + topo.num_cpus) * 128);
1362 outp = output_buffer;
1363 if (outp == NULL) {
1364 perror("calloc");
1365 exit(-1);
1366 }
1367}
1368
1369void setup_all_buffers(void)
1370{
1371 topology_probe();
1372 allocate_counters(&thread_even, &core_even, &package_even);
1373 allocate_counters(&thread_odd, &core_odd, &package_odd);
1374 allocate_output_buffer();
1375 for_all_proc_cpus(initialize_counters);
1376}
Len Brown103a8fe2010-10-22 23:53:03 -04001377void turbostat_init()
1378{
1379 check_cpuid();
1380
1381 check_dev_msr();
1382 check_super_user();
1383
Len Brownc98d5d92012-06-04 00:56:40 -04001384 setup_all_buffers();
Len Brown103a8fe2010-10-22 23:53:03 -04001385
1386 if (verbose)
Len Brownc98d5d92012-06-04 00:56:40 -04001387 print_verbose_header();
Len Brown103a8fe2010-10-22 23:53:03 -04001388}
1389
1390int fork_it(char **argv)
1391{
Len Brown103a8fe2010-10-22 23:53:03 -04001392 pid_t child_pid;
Len Brownd15cf7c2012-06-03 23:24:00 -04001393
Len Brownc98d5d92012-06-04 00:56:40 -04001394 for_all_cpus(get_counters, EVEN_COUNTERS);
1395 /* clear affinity side-effect of get_counters() */
1396 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
Len Brown103a8fe2010-10-22 23:53:03 -04001397 gettimeofday(&tv_even, (struct timezone *)NULL);
1398
1399 child_pid = fork();
1400 if (!child_pid) {
1401 /* child */
1402 execvp(argv[0], argv);
1403 } else {
1404 int status;
1405
1406 /* parent */
1407 if (child_pid == -1) {
1408 perror("fork");
1409 exit(1);
1410 }
1411
1412 signal(SIGINT, SIG_IGN);
1413 signal(SIGQUIT, SIG_IGN);
1414 if (waitpid(child_pid, &status, 0) == -1) {
1415 perror("wait");
1416 exit(1);
1417 }
1418 }
Len Brownc98d5d92012-06-04 00:56:40 -04001419 /*
1420 * n.b. fork_it() does not check for errors from for_all_cpus()
1421 * because re-starting is problematic when forking
1422 */
1423 for_all_cpus(get_counters, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04001424 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04001425 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04001426 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
1427 compute_average(EVEN_COUNTERS);
1428 format_all_counters(EVEN_COUNTERS);
1429 flush_stderr();
Len Brown103a8fe2010-10-22 23:53:03 -04001430
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001431 fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
Len Brown103a8fe2010-10-22 23:53:03 -04001432
1433 return 0;
1434}
1435
1436void cmdline(int argc, char **argv)
1437{
1438 int opt;
1439
1440 progname = argv[0];
1441
Len Brownc98d5d92012-06-04 00:56:40 -04001442 while ((opt = getopt(argc, argv, "+cpsvi:M:")) != -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04001443 switch (opt) {
Len Brownc98d5d92012-06-04 00:56:40 -04001444 case 'c':
1445 show_core_only++;
1446 break;
1447 case 'p':
1448 show_pkg_only++;
1449 break;
Len Browne23da032012-02-06 18:37:16 -05001450 case 's':
1451 summary_only++;
1452 break;
Len Brown103a8fe2010-10-22 23:53:03 -04001453 case 'v':
1454 verbose++;
1455 break;
1456 case 'i':
1457 interval_sec = atoi(optarg);
1458 break;
1459 case 'M':
1460 sscanf(optarg, "%x", &extra_msr_offset);
1461 if (verbose > 1)
1462 fprintf(stderr, "MSR 0x%X\n", extra_msr_offset);
1463 break;
1464 default:
1465 usage();
1466 }
1467 }
1468}
1469
1470int main(int argc, char **argv)
1471{
1472 cmdline(argc, argv);
1473
1474 if (verbose > 1)
Len Brownc98d5d92012-06-04 00:56:40 -04001475 fprintf(stderr, "turbostat v2.0 May 16, 2012"
Len Brown103a8fe2010-10-22 23:53:03 -04001476 " - Len Brown <lenb@kernel.org>\n");
Len Brown103a8fe2010-10-22 23:53:03 -04001477
1478 turbostat_init();
1479
1480 /*
1481 * if any params left, it must be a command to fork
1482 */
1483 if (argc - optind)
1484 return fork_it(argv + optind);
1485 else
1486 turbostat_loop();
1487
1488 return 0;
1489}