blob: 77e76b11382fed14a18af16e2313a4446d4cf9f4 [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 Browne23da032012-02-06 18:37:16 -050042unsigned int summary_only; /* set with -s */
Len Brown103a8fe2010-10-22 23:53:03 -040043unsigned int skip_c0;
44unsigned int skip_c1;
45unsigned int do_nhm_cstates;
46unsigned int do_snb_cstates;
47unsigned int has_aperf;
48unsigned int units = 1000000000; /* Ghz etc */
49unsigned int genuine_intel;
50unsigned int has_invariant_tsc;
51unsigned int do_nehalem_platform_info;
52unsigned int do_nehalem_turbo_ratio_limit;
Len Brown6574a5d2012-09-21 00:01:31 -040053unsigned int do_ivt_turbo_ratio_limit;
Len Brown2f32edf2012-09-21 23:45:46 -040054unsigned int extra_msr_offset32;
55unsigned int extra_msr_offset64;
Len Brown8e180f32012-09-22 01:25:08 -040056unsigned int extra_delta_offset32;
57unsigned int extra_delta_offset64;
Len Brown103a8fe2010-10-22 23:53:03 -040058double bclk;
59unsigned int show_pkg;
60unsigned int show_core;
61unsigned int show_cpu;
Len Brownc98d5d92012-06-04 00:56:40 -040062unsigned int show_pkg_only;
63unsigned int show_core_only;
64char *output_buffer, *outp;
Len Brown103a8fe2010-10-22 23:53:03 -040065
66int aperf_mperf_unstable;
67int backwards_count;
68char *progname;
Len Brown103a8fe2010-10-22 23:53:03 -040069
Len Brownc98d5d92012-06-04 00:56:40 -040070cpu_set_t *cpu_present_set, *cpu_affinity_set;
71size_t cpu_present_setsize, cpu_affinity_setsize;
Len Brown103a8fe2010-10-22 23:53:03 -040072
Len Brownc98d5d92012-06-04 00:56:40 -040073struct thread_data {
74 unsigned long long tsc;
75 unsigned long long aperf;
76 unsigned long long mperf;
77 unsigned long long c1; /* derived */
Len Brown2f32edf2012-09-21 23:45:46 -040078 unsigned long long extra_msr64;
Len Brown8e180f32012-09-22 01:25:08 -040079 unsigned long long extra_delta64;
80 unsigned long long extra_msr32;
81 unsigned long long extra_delta32;
Len Brownc98d5d92012-06-04 00:56:40 -040082 unsigned int cpu_id;
83 unsigned int flags;
84#define CPU_IS_FIRST_THREAD_IN_CORE 0x2
85#define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
86} *thread_even, *thread_odd;
Len Brown103a8fe2010-10-22 23:53:03 -040087
Len Brownc98d5d92012-06-04 00:56:40 -040088struct core_data {
89 unsigned long long c3;
90 unsigned long long c6;
91 unsigned long long c7;
92 unsigned int core_id;
93} *core_even, *core_odd;
Len Brown103a8fe2010-10-22 23:53:03 -040094
Len Brownc98d5d92012-06-04 00:56:40 -040095struct pkg_data {
96 unsigned long long pc2;
97 unsigned long long pc3;
98 unsigned long long pc6;
99 unsigned long long pc7;
100 unsigned int package_id;
101} *package_even, *package_odd;
102
103#define ODD_COUNTERS thread_odd, core_odd, package_odd
104#define EVEN_COUNTERS thread_even, core_even, package_even
105
106#define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
107 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
108 topo.num_threads_per_core + \
109 (core_no) * topo.num_threads_per_core + (thread_no))
110#define GET_CORE(core_base, core_no, pkg_no) \
111 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
112#define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
113
114struct system_summary {
115 struct thread_data threads;
116 struct core_data cores;
117 struct pkg_data packages;
118} sum, average;
119
120
121struct topo_params {
122 int num_packages;
123 int num_cpus;
124 int num_cores;
125 int max_cpu_num;
126 int num_cores_per_pkg;
127 int num_threads_per_core;
128} topo;
129
130struct timeval tv_even, tv_odd, tv_delta;
131
132void setup_all_buffers(void);
133
134int cpu_is_not_present(int cpu)
Len Brownd15cf7c2012-06-03 23:24:00 -0400135{
Len Brownc98d5d92012-06-04 00:56:40 -0400136 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brownd15cf7c2012-06-03 23:24:00 -0400137}
Len Brown88c32812012-03-29 21:44:40 -0400138/*
Len Brownc98d5d92012-06-04 00:56:40 -0400139 * run func(thread, core, package) in topology order
140 * skip non-present cpus
Len Brown88c32812012-03-29 21:44:40 -0400141 */
Len Brownd15cf7c2012-06-03 23:24:00 -0400142
Len Brownc98d5d92012-06-04 00:56:40 -0400143int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
144 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
Len Brown88c32812012-03-29 21:44:40 -0400145{
Len Brownc98d5d92012-06-04 00:56:40 -0400146 int retval, pkg_no, core_no, thread_no;
147
148 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
149 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
150 for (thread_no = 0; thread_no <
151 topo.num_threads_per_core; ++thread_no) {
152 struct thread_data *t;
153 struct core_data *c;
154 struct pkg_data *p;
155
156 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
157
158 if (cpu_is_not_present(t->cpu_id))
159 continue;
160
161 c = GET_CORE(core_base, core_no, pkg_no);
162 p = GET_PKG(pkg_base, pkg_no);
163
164 retval = func(t, c, p);
165 if (retval)
166 return retval;
167 }
168 }
169 }
170 return 0;
Len Brown88c32812012-03-29 21:44:40 -0400171}
172
173int cpu_migrate(int cpu)
174{
Len Brownc98d5d92012-06-04 00:56:40 -0400175 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
176 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
177 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
Len Brown88c32812012-03-29 21:44:40 -0400178 return -1;
179 else
180 return 0;
181}
182
Len Brown15aaa342012-03-29 22:19:58 -0400183int get_msr(int cpu, off_t offset, unsigned long long *msr)
Len Brown103a8fe2010-10-22 23:53:03 -0400184{
185 ssize_t retval;
Len Brown103a8fe2010-10-22 23:53:03 -0400186 char pathname[32];
187 int fd;
188
189 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
190 fd = open(pathname, O_RDONLY);
Len Brown15aaa342012-03-29 22:19:58 -0400191 if (fd < 0)
192 return -1;
Len Brown103a8fe2010-10-22 23:53:03 -0400193
Len Brown15aaa342012-03-29 22:19:58 -0400194 retval = pread(fd, msr, sizeof *msr, offset);
Len Brown103a8fe2010-10-22 23:53:03 -0400195 close(fd);
Len Brown15aaa342012-03-29 22:19:58 -0400196
Len Brownd91bb172012-11-01 00:08:19 -0400197 if (retval != sizeof *msr) {
198 fprintf(stderr, "%s offset 0x%zx read failed\n", pathname, offset);
Len Brown15aaa342012-03-29 22:19:58 -0400199 return -1;
Len Brownd91bb172012-11-01 00:08:19 -0400200 }
Len Brown15aaa342012-03-29 22:19:58 -0400201
202 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400203}
204
Len Browna829eb42011-02-10 23:36:34 -0500205void print_header(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400206{
207 if (show_pkg)
Len Brownc98d5d92012-06-04 00:56:40 -0400208 outp += sprintf(outp, "pk");
Len Browne23da032012-02-06 18:37:16 -0500209 if (show_pkg)
Len Brownc98d5d92012-06-04 00:56:40 -0400210 outp += sprintf(outp, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400211 if (show_core)
Len Brownc98d5d92012-06-04 00:56:40 -0400212 outp += sprintf(outp, "cor");
Len Brown103a8fe2010-10-22 23:53:03 -0400213 if (show_cpu)
Len Brownc98d5d92012-06-04 00:56:40 -0400214 outp += sprintf(outp, " CPU");
Len Browne23da032012-02-06 18:37:16 -0500215 if (show_pkg || show_core || show_cpu)
Len Brownc98d5d92012-06-04 00:56:40 -0400216 outp += sprintf(outp, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400217 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400218 outp += sprintf(outp, " %%c0");
Len Brown103a8fe2010-10-22 23:53:03 -0400219 if (has_aperf)
Len Brownc98d5d92012-06-04 00:56:40 -0400220 outp += sprintf(outp, " GHz");
221 outp += sprintf(outp, " TSC");
Len Brown8e180f32012-09-22 01:25:08 -0400222 if (extra_delta_offset32)
Len Brownf9240812012-10-06 15:26:31 -0400223 outp += sprintf(outp, " count 0x%03X", extra_delta_offset32);
Len Brown8e180f32012-09-22 01:25:08 -0400224 if (extra_delta_offset64)
Len Brownf9240812012-10-06 15:26:31 -0400225 outp += sprintf(outp, " COUNT 0x%03X", extra_delta_offset64);
Len Brown2f32edf2012-09-21 23:45:46 -0400226 if (extra_msr_offset32)
Len Brown8e180f32012-09-22 01:25:08 -0400227 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset32);
Len Brown2f32edf2012-09-21 23:45:46 -0400228 if (extra_msr_offset64)
Len Brown8e180f32012-09-22 01:25:08 -0400229 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset64);
Len Brown103a8fe2010-10-22 23:53:03 -0400230 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400231 outp += sprintf(outp, " %%c1");
Len Brown103a8fe2010-10-22 23:53:03 -0400232 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400233 outp += sprintf(outp, " %%c3");
Len Brown103a8fe2010-10-22 23:53:03 -0400234 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400235 outp += sprintf(outp, " %%c6");
Len Brown103a8fe2010-10-22 23:53:03 -0400236 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400237 outp += sprintf(outp, " %%c7");
Len Brown103a8fe2010-10-22 23:53:03 -0400238 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400239 outp += sprintf(outp, " %%pc2");
Len Brown103a8fe2010-10-22 23:53:03 -0400240 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400241 outp += sprintf(outp, " %%pc3");
Len Brown103a8fe2010-10-22 23:53:03 -0400242 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400243 outp += sprintf(outp, " %%pc6");
Len Brown103a8fe2010-10-22 23:53:03 -0400244 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400245 outp += sprintf(outp, " %%pc7");
Len Brown103a8fe2010-10-22 23:53:03 -0400246
Len Brownc98d5d92012-06-04 00:56:40 -0400247 outp += sprintf(outp, "\n");
Len Brown103a8fe2010-10-22 23:53:03 -0400248}
249
Len Brownc98d5d92012-06-04 00:56:40 -0400250int dump_counters(struct thread_data *t, struct core_data *c,
251 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400252{
Len Brownc98d5d92012-06-04 00:56:40 -0400253 fprintf(stderr, "t %p, c %p, p %p\n", t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400254
Len Brownc98d5d92012-06-04 00:56:40 -0400255 if (t) {
256 fprintf(stderr, "CPU: %d flags 0x%x\n", t->cpu_id, t->flags);
257 fprintf(stderr, "TSC: %016llX\n", t->tsc);
258 fprintf(stderr, "aperf: %016llX\n", t->aperf);
259 fprintf(stderr, "mperf: %016llX\n", t->mperf);
260 fprintf(stderr, "c1: %016llX\n", t->c1);
Len Brown8e180f32012-09-22 01:25:08 -0400261 fprintf(stderr, "msr0x%x: %08llX\n",
262 extra_delta_offset32, t->extra_delta32);
263 fprintf(stderr, "msr0x%x: %016llX\n",
264 extra_delta_offset64, t->extra_delta64);
265 fprintf(stderr, "msr0x%x: %08llX\n",
Len Brown2f32edf2012-09-21 23:45:46 -0400266 extra_msr_offset32, t->extra_msr32);
Len Brownc98d5d92012-06-04 00:56:40 -0400267 fprintf(stderr, "msr0x%x: %016llX\n",
Len Brown2f32edf2012-09-21 23:45:46 -0400268 extra_msr_offset64, t->extra_msr64);
Len Brownc98d5d92012-06-04 00:56:40 -0400269 }
Len Brown103a8fe2010-10-22 23:53:03 -0400270
Len Brownc98d5d92012-06-04 00:56:40 -0400271 if (c) {
272 fprintf(stderr, "core: %d\n", c->core_id);
273 fprintf(stderr, "c3: %016llX\n", c->c3);
274 fprintf(stderr, "c6: %016llX\n", c->c6);
275 fprintf(stderr, "c7: %016llX\n", c->c7);
276 }
277
278 if (p) {
279 fprintf(stderr, "package: %d\n", p->package_id);
280 fprintf(stderr, "pc2: %016llX\n", p->pc2);
281 fprintf(stderr, "pc3: %016llX\n", p->pc3);
282 fprintf(stderr, "pc6: %016llX\n", p->pc6);
283 fprintf(stderr, "pc7: %016llX\n", p->pc7);
284 }
285 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400286}
287
Len Browne23da032012-02-06 18:37:16 -0500288/*
289 * column formatting convention & formats
290 * package: "pk" 2 columns %2d
291 * core: "cor" 3 columns %3d
292 * CPU: "CPU" 3 columns %3d
293 * GHz: "GHz" 3 columns %3.2
294 * TSC: "TSC" 3 columns %3.2
295 * percentage " %pc3" %6.2
296 */
Len Brownc98d5d92012-06-04 00:56:40 -0400297int format_counters(struct thread_data *t, struct core_data *c,
298 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400299{
300 double interval_float;
301
Len Brownc98d5d92012-06-04 00:56:40 -0400302 /* if showing only 1st thread in core and this isn't one, bail out */
303 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
304 return 0;
305
306 /* if showing only 1st thread in pkg and this isn't one, bail out */
307 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
308 return 0;
309
Len Brown103a8fe2010-10-22 23:53:03 -0400310 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
311
Len Brownc98d5d92012-06-04 00:56:40 -0400312 /* topo columns, print blanks on 1st (average) line */
313 if (t == &average.threads) {
Len Brown103a8fe2010-10-22 23:53:03 -0400314 if (show_pkg)
Len Brownc98d5d92012-06-04 00:56:40 -0400315 outp += sprintf(outp, " ");
Len Browne23da032012-02-06 18:37:16 -0500316 if (show_pkg && show_core)
Len Brownc98d5d92012-06-04 00:56:40 -0400317 outp += sprintf(outp, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400318 if (show_core)
Len Brownc98d5d92012-06-04 00:56:40 -0400319 outp += sprintf(outp, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400320 if (show_cpu)
Len Brownc98d5d92012-06-04 00:56:40 -0400321 outp += sprintf(outp, " " " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400322 } else {
Len Brownc98d5d92012-06-04 00:56:40 -0400323 if (show_pkg) {
324 if (p)
325 outp += sprintf(outp, "%2d", p->package_id);
326 else
327 outp += sprintf(outp, " ");
328 }
Len Browne23da032012-02-06 18:37:16 -0500329 if (show_pkg && show_core)
Len Brownc98d5d92012-06-04 00:56:40 -0400330 outp += sprintf(outp, " ");
331 if (show_core) {
332 if (c)
333 outp += sprintf(outp, "%3d", c->core_id);
334 else
335 outp += sprintf(outp, " ");
336 }
Len Brown103a8fe2010-10-22 23:53:03 -0400337 if (show_cpu)
Len Brownc98d5d92012-06-04 00:56:40 -0400338 outp += sprintf(outp, " %3d", t->cpu_id);
Len Brown103a8fe2010-10-22 23:53:03 -0400339 }
340
341 /* %c0 */
342 if (do_nhm_cstates) {
Len Browne23da032012-02-06 18:37:16 -0500343 if (show_pkg || show_core || show_cpu)
Len Brownc98d5d92012-06-04 00:56:40 -0400344 outp += sprintf(outp, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400345 if (!skip_c0)
Len Brownc98d5d92012-06-04 00:56:40 -0400346 outp += sprintf(outp, "%6.2f", 100.0 * t->mperf/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400347 else
Len Brownc98d5d92012-06-04 00:56:40 -0400348 outp += sprintf(outp, " ****");
Len Brown103a8fe2010-10-22 23:53:03 -0400349 }
350
351 /* GHz */
352 if (has_aperf) {
353 if (!aperf_mperf_unstable) {
Len Brownc98d5d92012-06-04 00:56:40 -0400354 outp += sprintf(outp, " %3.2f",
355 1.0 * t->tsc / units * t->aperf /
356 t->mperf / interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400357 } else {
Len Brownc98d5d92012-06-04 00:56:40 -0400358 if (t->aperf > t->tsc || t->mperf > t->tsc) {
359 outp += sprintf(outp, " ***");
Len Brown103a8fe2010-10-22 23:53:03 -0400360 } else {
Len Brownc98d5d92012-06-04 00:56:40 -0400361 outp += sprintf(outp, "%3.1f*",
362 1.0 * t->tsc /
363 units * t->aperf /
364 t->mperf / interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400365 }
366 }
367 }
368
369 /* TSC */
Len Brownc98d5d92012-06-04 00:56:40 -0400370 outp += sprintf(outp, "%5.2f", 1.0 * t->tsc/units/interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400371
Len Brown8e180f32012-09-22 01:25:08 -0400372 /* delta */
373 if (extra_delta_offset32)
374 outp += sprintf(outp, " %11llu", t->extra_delta32);
375
376 /* DELTA */
377 if (extra_delta_offset64)
378 outp += sprintf(outp, " %11llu", t->extra_delta64);
Len Brown2f32edf2012-09-21 23:45:46 -0400379 /* msr */
380 if (extra_msr_offset32)
Len Brown8e180f32012-09-22 01:25:08 -0400381 outp += sprintf(outp, " 0x%08llx", t->extra_msr32);
Len Brown2f32edf2012-09-21 23:45:46 -0400382
Len Brown130ff302012-09-21 22:56:06 -0400383 /* MSR */
Len Brown2f32edf2012-09-21 23:45:46 -0400384 if (extra_msr_offset64)
385 outp += sprintf(outp, " 0x%016llx", t->extra_msr64);
Len Brown130ff302012-09-21 22:56:06 -0400386
Len Brown103a8fe2010-10-22 23:53:03 -0400387 if (do_nhm_cstates) {
388 if (!skip_c1)
Len Brownc98d5d92012-06-04 00:56:40 -0400389 outp += sprintf(outp, " %6.2f", 100.0 * t->c1/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400390 else
Len Brownc98d5d92012-06-04 00:56:40 -0400391 outp += sprintf(outp, " ****");
Len Brown103a8fe2010-10-22 23:53:03 -0400392 }
Len Brownc98d5d92012-06-04 00:56:40 -0400393
394 /* print per-core data only for 1st thread in core */
395 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
396 goto done;
397
Len Brown103a8fe2010-10-22 23:53:03 -0400398 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400399 outp += sprintf(outp, " %6.2f", 100.0 * c->c3/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400400 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400401 outp += sprintf(outp, " %6.2f", 100.0 * c->c6/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400402 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400403 outp += sprintf(outp, " %6.2f", 100.0 * c->c7/t->tsc);
404
405 /* print per-package data only for 1st core in package */
406 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
407 goto done;
408
Len Brown103a8fe2010-10-22 23:53:03 -0400409 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400410 outp += sprintf(outp, " %6.2f", 100.0 * p->pc2/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400411 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400412 outp += sprintf(outp, " %6.2f", 100.0 * p->pc3/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400413 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400414 outp += sprintf(outp, " %6.2f", 100.0 * p->pc6/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400415 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400416 outp += sprintf(outp, " %6.2f", 100.0 * p->pc7/t->tsc);
417done:
Len Brownc98d5d92012-06-04 00:56:40 -0400418 outp += sprintf(outp, "\n");
419
420 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400421}
422
Len Brownc98d5d92012-06-04 00:56:40 -0400423void flush_stdout()
Len Brown103a8fe2010-10-22 23:53:03 -0400424{
Len Brownc98d5d92012-06-04 00:56:40 -0400425 fputs(output_buffer, stdout);
426 outp = output_buffer;
427}
428void flush_stderr()
429{
430 fputs(output_buffer, stderr);
431 outp = output_buffer;
432}
433void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
434{
Len Browne23da032012-02-06 18:37:16 -0500435 static int printed;
Len Brown103a8fe2010-10-22 23:53:03 -0400436
Len Browne23da032012-02-06 18:37:16 -0500437 if (!printed || !summary_only)
438 print_header();
Len Brown103a8fe2010-10-22 23:53:03 -0400439
Len Brownc98d5d92012-06-04 00:56:40 -0400440 if (topo.num_cpus > 1)
441 format_counters(&average.threads, &average.cores,
442 &average.packages);
Len Brown103a8fe2010-10-22 23:53:03 -0400443
Len Browne23da032012-02-06 18:37:16 -0500444 printed = 1;
445
446 if (summary_only)
447 return;
448
Len Brownc98d5d92012-06-04 00:56:40 -0400449 for_all_cpus(format_counters, t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400450}
451
Len Brownc98d5d92012-06-04 00:56:40 -0400452void
453delta_package(struct pkg_data *new, struct pkg_data *old)
Len Brown103a8fe2010-10-22 23:53:03 -0400454{
Len Brownc98d5d92012-06-04 00:56:40 -0400455 old->pc2 = new->pc2 - old->pc2;
456 old->pc3 = new->pc3 - old->pc3;
457 old->pc6 = new->pc6 - old->pc6;
458 old->pc7 = new->pc7 - old->pc7;
459}
Len Brown103a8fe2010-10-22 23:53:03 -0400460
Len Brownc98d5d92012-06-04 00:56:40 -0400461void
462delta_core(struct core_data *new, struct core_data *old)
463{
464 old->c3 = new->c3 - old->c3;
465 old->c6 = new->c6 - old->c6;
466 old->c7 = new->c7 - old->c7;
467}
Len Brown103a8fe2010-10-22 23:53:03 -0400468
Len Brownc3ae3312012-06-13 21:31:46 -0400469/*
470 * old = new - old
471 */
Len Brownc98d5d92012-06-04 00:56:40 -0400472void
473delta_thread(struct thread_data *new, struct thread_data *old,
474 struct core_data *core_delta)
475{
476 old->tsc = new->tsc - old->tsc;
Len Brown103a8fe2010-10-22 23:53:03 -0400477
Len Brownc98d5d92012-06-04 00:56:40 -0400478 /* check for TSC < 1 Mcycles over interval */
479 if (old->tsc < (1000 * 1000)) {
480 fprintf(stderr, "Insanely slow TSC rate, TSC stops in idle?\n");
481 fprintf(stderr, "You can disable all c-states by booting with \"idle=poll\"\n");
482 fprintf(stderr, "or just the deep ones with \"processor.max_cstate=1\"\n");
483 exit(-3);
484 }
Len Brown103a8fe2010-10-22 23:53:03 -0400485
Len Brownc98d5d92012-06-04 00:56:40 -0400486 old->c1 = new->c1 - old->c1;
Len Brown103a8fe2010-10-22 23:53:03 -0400487
Len Brownc98d5d92012-06-04 00:56:40 -0400488 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
489 old->aperf = new->aperf - old->aperf;
490 old->mperf = new->mperf - old->mperf;
491 } else {
Len Brown103a8fe2010-10-22 23:53:03 -0400492
Len Brownc98d5d92012-06-04 00:56:40 -0400493 if (!aperf_mperf_unstable) {
494 fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname);
495 fprintf(stderr, "* Frequency results do not cover entire interval *\n");
496 fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n");
497
498 aperf_mperf_unstable = 1;
499 }
Len Brown103a8fe2010-10-22 23:53:03 -0400500 /*
Len Brownc98d5d92012-06-04 00:56:40 -0400501 * mperf delta is likely a huge "positive" number
502 * can not use it for calculating c0 time
Len Brown103a8fe2010-10-22 23:53:03 -0400503 */
Len Brownc98d5d92012-06-04 00:56:40 -0400504 skip_c0 = 1;
505 skip_c1 = 1;
506 }
Len Brown103a8fe2010-10-22 23:53:03 -0400507
Len Brown103a8fe2010-10-22 23:53:03 -0400508
Len Brownc98d5d92012-06-04 00:56:40 -0400509 /*
Len Brownc3ae3312012-06-13 21:31:46 -0400510 * As counter collection is not atomic,
511 * it is possible for mperf's non-halted cycles + idle states
Len Brownc98d5d92012-06-04 00:56:40 -0400512 * to exceed TSC's all cycles: show c1 = 0% in that case.
513 */
Len Brownc3ae3312012-06-13 21:31:46 -0400514 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
Len Brownc98d5d92012-06-04 00:56:40 -0400515 old->c1 = 0;
516 else {
517 /* normal case, derive c1 */
518 old->c1 = old->tsc - old->mperf - core_delta->c3
519 - core_delta->c6 - core_delta->c7;
520 }
Len Brownc3ae3312012-06-13 21:31:46 -0400521
Len Brownc98d5d92012-06-04 00:56:40 -0400522 if (old->mperf == 0) {
Len Brownc3ae3312012-06-13 21:31:46 -0400523 if (verbose > 1) fprintf(stderr, "cpu%d MPERF 0!\n", old->cpu_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400524 old->mperf = 1; /* divide by 0 protection */
525 }
526
Len Brown8e180f32012-09-22 01:25:08 -0400527 old->extra_delta32 = new->extra_delta32 - old->extra_delta32;
528 old->extra_delta32 &= 0xFFFFFFFF;
529
530 old->extra_delta64 = new->extra_delta64 - old->extra_delta64;
531
Len Brownc98d5d92012-06-04 00:56:40 -0400532 /*
Len Brown8e180f32012-09-22 01:25:08 -0400533 * Extra MSR is just a snapshot, simply copy latest w/o subtracting
Len Brownc98d5d92012-06-04 00:56:40 -0400534 */
Len Brown2f32edf2012-09-21 23:45:46 -0400535 old->extra_msr32 = new->extra_msr32;
536 old->extra_msr64 = new->extra_msr64;
Len Brownc98d5d92012-06-04 00:56:40 -0400537}
538
539int delta_cpu(struct thread_data *t, struct core_data *c,
540 struct pkg_data *p, struct thread_data *t2,
541 struct core_data *c2, struct pkg_data *p2)
542{
543 /* calculate core delta only for 1st thread in core */
544 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
545 delta_core(c, c2);
546
547 /* always calculate thread delta */
548 delta_thread(t, t2, c2); /* c2 is core delta */
549
550 /* calculate package delta only for 1st core in package */
551 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
552 delta_package(p, p2);
553
554 return 0;
555}
556
557void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
558{
559 t->tsc = 0;
560 t->aperf = 0;
561 t->mperf = 0;
562 t->c1 = 0;
563
Len Brown8e180f32012-09-22 01:25:08 -0400564 t->extra_delta32 = 0;
565 t->extra_delta64 = 0;
566
Len Brownc98d5d92012-06-04 00:56:40 -0400567 /* tells format_counters to dump all fields from this set */
568 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
569
570 c->c3 = 0;
571 c->c6 = 0;
572 c->c7 = 0;
573
574 p->pc2 = 0;
575 p->pc3 = 0;
576 p->pc6 = 0;
577 p->pc7 = 0;
578}
579int sum_counters(struct thread_data *t, struct core_data *c,
580 struct pkg_data *p)
581{
582 average.threads.tsc += t->tsc;
583 average.threads.aperf += t->aperf;
584 average.threads.mperf += t->mperf;
585 average.threads.c1 += t->c1;
586
Len Brown8e180f32012-09-22 01:25:08 -0400587 average.threads.extra_delta32 += t->extra_delta32;
588 average.threads.extra_delta64 += t->extra_delta64;
589
Len Brownc98d5d92012-06-04 00:56:40 -0400590 /* sum per-core values only for 1st thread in core */
591 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
592 return 0;
593
594 average.cores.c3 += c->c3;
595 average.cores.c6 += c->c6;
596 average.cores.c7 += c->c7;
597
598 /* sum per-pkg values only for 1st core in pkg */
599 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
600 return 0;
601
602 average.packages.pc2 += p->pc2;
603 average.packages.pc3 += p->pc3;
604 average.packages.pc6 += p->pc6;
605 average.packages.pc7 += p->pc7;
606
607 return 0;
608}
609/*
610 * sum the counters for all cpus in the system
611 * compute the weighted average
612 */
613void compute_average(struct thread_data *t, struct core_data *c,
614 struct pkg_data *p)
615{
616 clear_counters(&average.threads, &average.cores, &average.packages);
617
618 for_all_cpus(sum_counters, t, c, p);
619
620 average.threads.tsc /= topo.num_cpus;
621 average.threads.aperf /= topo.num_cpus;
622 average.threads.mperf /= topo.num_cpus;
623 average.threads.c1 /= topo.num_cpus;
624
Len Brown8e180f32012-09-22 01:25:08 -0400625 average.threads.extra_delta32 /= topo.num_cpus;
626 average.threads.extra_delta32 &= 0xFFFFFFFF;
627
628 average.threads.extra_delta64 /= topo.num_cpus;
629
Len Brownc98d5d92012-06-04 00:56:40 -0400630 average.cores.c3 /= topo.num_cores;
631 average.cores.c6 /= topo.num_cores;
632 average.cores.c7 /= topo.num_cores;
633
634 average.packages.pc2 /= topo.num_packages;
635 average.packages.pc3 /= topo.num_packages;
636 average.packages.pc6 /= topo.num_packages;
637 average.packages.pc7 /= topo.num_packages;
638}
639
640static unsigned long long rdtsc(void)
641{
642 unsigned int low, high;
643
644 asm volatile("rdtsc" : "=a" (low), "=d" (high));
645
646 return low | ((unsigned long long)high) << 32;
647}
648
649
650/*
651 * get_counters(...)
652 * migrate to cpu
653 * acquire and record local counters for that cpu
654 */
655int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
656{
657 int cpu = t->cpu_id;
658
Len Browne52966c2012-11-08 22:38:05 -0500659 if (cpu_migrate(cpu)) {
660 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
Len Brownc98d5d92012-06-04 00:56:40 -0400661 return -1;
Len Browne52966c2012-11-08 22:38:05 -0500662 }
Len Brownc98d5d92012-06-04 00:56:40 -0400663
664 t->tsc = rdtsc(); /* we are running on local CPU of interest */
665
666 if (has_aperf) {
Len Brown9c63a652012-10-31 01:29:52 -0400667 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
Len Brownc98d5d92012-06-04 00:56:40 -0400668 return -3;
Len Brown9c63a652012-10-31 01:29:52 -0400669 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
Len Brownc98d5d92012-06-04 00:56:40 -0400670 return -4;
671 }
672
Len Brown8e180f32012-09-22 01:25:08 -0400673 if (extra_delta_offset32) {
674 if (get_msr(cpu, extra_delta_offset32, &t->extra_delta32))
Len Brown2f32edf2012-09-21 23:45:46 -0400675 return -5;
Len Brown8e180f32012-09-22 01:25:08 -0400676 t->extra_delta32 &= 0xFFFFFFFF;
677 }
678
679 if (extra_delta_offset64)
680 if (get_msr(cpu, extra_delta_offset64, &t->extra_delta64))
681 return -5;
682
683 if (extra_msr_offset32) {
684 if (get_msr(cpu, extra_msr_offset32, &t->extra_msr32))
685 return -5;
686 t->extra_msr32 &= 0xFFFFFFFF;
687 }
Len Brown2f32edf2012-09-21 23:45:46 -0400688
689 if (extra_msr_offset64)
690 if (get_msr(cpu, extra_msr_offset64, &t->extra_msr64))
Len Brownc98d5d92012-06-04 00:56:40 -0400691 return -5;
692
693 /* collect core counters only for 1st thread in core */
694 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
695 return 0;
696
697 if (do_nhm_cstates) {
698 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
699 return -6;
700 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
701 return -7;
702 }
703
704 if (do_snb_cstates)
705 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
706 return -8;
707
708 /* collect package counters only for 1st core in package */
709 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
710 return 0;
711
712 if (do_nhm_cstates) {
713 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
714 return -9;
715 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
716 return -10;
717 }
718 if (do_snb_cstates) {
719 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
720 return -11;
721 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
722 return -12;
Len Brown103a8fe2010-10-22 23:53:03 -0400723 }
724 return 0;
725}
726
Len Brownc98d5d92012-06-04 00:56:40 -0400727void print_verbose_header(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400728{
729 unsigned long long msr;
730 unsigned int ratio;
731
732 if (!do_nehalem_platform_info)
733 return;
734
Len Brown9c63a652012-10-31 01:29:52 -0400735 get_msr(0, MSR_NHM_PLATFORM_INFO, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -0400736
Len Brown6574a5d2012-09-21 00:01:31 -0400737 if (verbose > 1)
Len Brown9c63a652012-10-31 01:29:52 -0400738 fprintf(stderr, "MSR_NHM_PLATFORM_INFO: 0x%llx\n", msr);
Len Brown6574a5d2012-09-21 00:01:31 -0400739
Len Brown103a8fe2010-10-22 23:53:03 -0400740 ratio = (msr >> 40) & 0xFF;
741 fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency\n",
742 ratio, bclk, ratio * bclk);
743
744 ratio = (msr >> 8) & 0xFF;
745 fprintf(stderr, "%d * %.0f = %.0f MHz TSC frequency\n",
746 ratio, bclk, ratio * bclk);
747
Len Brown6574a5d2012-09-21 00:01:31 -0400748 if (!do_ivt_turbo_ratio_limit)
749 goto print_nhm_turbo_ratio_limits;
750
751 get_msr(0, MSR_IVT_TURBO_RATIO_LIMIT, &msr);
752
Len Brown103a8fe2010-10-22 23:53:03 -0400753 if (verbose > 1)
Len Brown6574a5d2012-09-21 00:01:31 -0400754 fprintf(stderr, "MSR_IVT_TURBO_RATIO_LIMIT: 0x%llx\n", msr);
755
756 ratio = (msr >> 56) & 0xFF;
757 if (ratio)
758 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 16 active cores\n",
759 ratio, bclk, ratio * bclk);
760
761 ratio = (msr >> 48) & 0xFF;
762 if (ratio)
763 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 15 active cores\n",
764 ratio, bclk, ratio * bclk);
765
766 ratio = (msr >> 40) & 0xFF;
767 if (ratio)
768 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 14 active cores\n",
769 ratio, bclk, ratio * bclk);
770
771 ratio = (msr >> 32) & 0xFF;
772 if (ratio)
773 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 13 active cores\n",
774 ratio, bclk, ratio * bclk);
775
776 ratio = (msr >> 24) & 0xFF;
777 if (ratio)
778 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 12 active cores\n",
779 ratio, bclk, ratio * bclk);
780
781 ratio = (msr >> 16) & 0xFF;
782 if (ratio)
783 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 11 active cores\n",
784 ratio, bclk, ratio * bclk);
785
786 ratio = (msr >> 8) & 0xFF;
787 if (ratio)
788 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 10 active cores\n",
789 ratio, bclk, ratio * bclk);
790
791 ratio = (msr >> 0) & 0xFF;
792 if (ratio)
793 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 9 active cores\n",
794 ratio, bclk, ratio * bclk);
795
796print_nhm_turbo_ratio_limits:
Len Brown103a8fe2010-10-22 23:53:03 -0400797
798 if (!do_nehalem_turbo_ratio_limit)
799 return;
800
Len Brown9c63a652012-10-31 01:29:52 -0400801 get_msr(0, MSR_NHM_TURBO_RATIO_LIMIT, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -0400802
Len Brown6574a5d2012-09-21 00:01:31 -0400803 if (verbose > 1)
Len Brown9c63a652012-10-31 01:29:52 -0400804 fprintf(stderr, "MSR_NHM_TURBO_RATIO_LIMIT: 0x%llx\n", msr);
Len Brown6574a5d2012-09-21 00:01:31 -0400805
806 ratio = (msr >> 56) & 0xFF;
807 if (ratio)
808 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 8 active cores\n",
809 ratio, bclk, ratio * bclk);
810
811 ratio = (msr >> 48) & 0xFF;
812 if (ratio)
813 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 7 active cores\n",
814 ratio, bclk, ratio * bclk);
815
816 ratio = (msr >> 40) & 0xFF;
817 if (ratio)
818 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 6 active cores\n",
819 ratio, bclk, ratio * bclk);
820
821 ratio = (msr >> 32) & 0xFF;
822 if (ratio)
823 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 5 active cores\n",
824 ratio, bclk, ratio * bclk);
825
Len Brown103a8fe2010-10-22 23:53:03 -0400826 ratio = (msr >> 24) & 0xFF;
827 if (ratio)
828 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
829 ratio, bclk, ratio * bclk);
830
831 ratio = (msr >> 16) & 0xFF;
832 if (ratio)
833 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
834 ratio, bclk, ratio * bclk);
835
836 ratio = (msr >> 8) & 0xFF;
837 if (ratio)
838 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
839 ratio, bclk, ratio * bclk);
840
841 ratio = (msr >> 0) & 0xFF;
842 if (ratio)
843 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
844 ratio, bclk, ratio * bclk);
Len Brown103a8fe2010-10-22 23:53:03 -0400845}
846
Len Brownc98d5d92012-06-04 00:56:40 -0400847void free_all_buffers(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400848{
Len Brownc98d5d92012-06-04 00:56:40 -0400849 CPU_FREE(cpu_present_set);
850 cpu_present_set = NULL;
851 cpu_present_set = 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400852
Len Brownc98d5d92012-06-04 00:56:40 -0400853 CPU_FREE(cpu_affinity_set);
854 cpu_affinity_set = NULL;
855 cpu_affinity_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400856
Len Brownc98d5d92012-06-04 00:56:40 -0400857 free(thread_even);
858 free(core_even);
859 free(package_even);
860
861 thread_even = NULL;
862 core_even = NULL;
863 package_even = NULL;
864
865 free(thread_odd);
866 free(core_odd);
867 free(package_odd);
868
869 thread_odd = NULL;
870 core_odd = NULL;
871 package_odd = NULL;
872
873 free(output_buffer);
874 output_buffer = NULL;
875 outp = NULL;
Len Brown103a8fe2010-10-22 23:53:03 -0400876}
877
Len Brownc98d5d92012-06-04 00:56:40 -0400878/*
879 * cpu_is_first_sibling_in_core(cpu)
880 * return 1 if given CPU is 1st HT sibling in the core
881 */
882int cpu_is_first_sibling_in_core(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -0400883{
Len Brownc98d5d92012-06-04 00:56:40 -0400884 char path[64];
885 FILE *filep;
886 int first_cpu;
Len Brown103a8fe2010-10-22 23:53:03 -0400887
Len Brownc98d5d92012-06-04 00:56:40 -0400888 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
889 filep = fopen(path, "r");
890 if (filep == NULL) {
891 perror(path);
892 exit(1);
893 }
894 fscanf(filep, "%d", &first_cpu);
895 fclose(filep);
896 return (cpu == first_cpu);
Len Brown103a8fe2010-10-22 23:53:03 -0400897}
898
Len Brownc98d5d92012-06-04 00:56:40 -0400899/*
900 * cpu_is_first_core_in_package(cpu)
901 * return 1 if given CPU is 1st core in package
902 */
903int cpu_is_first_core_in_package(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -0400904{
Len Brownc98d5d92012-06-04 00:56:40 -0400905 char path[64];
906 FILE *filep;
907 int first_cpu;
Len Brown103a8fe2010-10-22 23:53:03 -0400908
Len Brownc98d5d92012-06-04 00:56:40 -0400909 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
910 filep = fopen(path, "r");
911 if (filep == NULL) {
912 perror(path);
Len Brown103a8fe2010-10-22 23:53:03 -0400913 exit(1);
914 }
Len Brownc98d5d92012-06-04 00:56:40 -0400915 fscanf(filep, "%d", &first_cpu);
916 fclose(filep);
917 return (cpu == first_cpu);
Len Brown103a8fe2010-10-22 23:53:03 -0400918}
919
920int get_physical_package_id(int cpu)
921{
Len Brownc98d5d92012-06-04 00:56:40 -0400922 char path[80];
Len Brown103a8fe2010-10-22 23:53:03 -0400923 FILE *filep;
924 int pkg;
925
926 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
927 filep = fopen(path, "r");
928 if (filep == NULL) {
929 perror(path);
930 exit(1);
931 }
932 fscanf(filep, "%d", &pkg);
933 fclose(filep);
934 return pkg;
935}
936
937int get_core_id(int cpu)
938{
Len Brownc98d5d92012-06-04 00:56:40 -0400939 char path[80];
Len Brown103a8fe2010-10-22 23:53:03 -0400940 FILE *filep;
941 int core;
942
943 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
944 filep = fopen(path, "r");
945 if (filep == NULL) {
946 perror(path);
947 exit(1);
948 }
949 fscanf(filep, "%d", &core);
950 fclose(filep);
951 return core;
952}
953
Len Brownc98d5d92012-06-04 00:56:40 -0400954int get_num_ht_siblings(int cpu)
955{
956 char path[80];
957 FILE *filep;
958 int sib1, sib2;
959 int matches;
960 char character;
961
962 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
963 filep = fopen(path, "r");
964 if (filep == NULL) {
965 perror(path);
966 exit(1);
967 }
968 /*
969 * file format:
970 * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4)
971 * otherwinse 1 sibling (self).
972 */
973 matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2);
974
975 fclose(filep);
976
977 if (matches == 3)
978 return 2;
979 else
980 return 1;
981}
982
Len Brown103a8fe2010-10-22 23:53:03 -0400983/*
Len Brownc98d5d92012-06-04 00:56:40 -0400984 * run func(thread, core, package) in topology order
985 * skip non-present cpus
Len Brown103a8fe2010-10-22 23:53:03 -0400986 */
987
Len Brownc98d5d92012-06-04 00:56:40 -0400988int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
989 struct pkg_data *, struct thread_data *, struct core_data *,
990 struct pkg_data *), struct thread_data *thread_base,
991 struct core_data *core_base, struct pkg_data *pkg_base,
992 struct thread_data *thread_base2, struct core_data *core_base2,
993 struct pkg_data *pkg_base2)
994{
995 int retval, pkg_no, core_no, thread_no;
996
997 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
998 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
999 for (thread_no = 0; thread_no <
1000 topo.num_threads_per_core; ++thread_no) {
1001 struct thread_data *t, *t2;
1002 struct core_data *c, *c2;
1003 struct pkg_data *p, *p2;
1004
1005 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
1006
1007 if (cpu_is_not_present(t->cpu_id))
1008 continue;
1009
1010 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
1011
1012 c = GET_CORE(core_base, core_no, pkg_no);
1013 c2 = GET_CORE(core_base2, core_no, pkg_no);
1014
1015 p = GET_PKG(pkg_base, pkg_no);
1016 p2 = GET_PKG(pkg_base2, pkg_no);
1017
1018 retval = func(t, c, p, t2, c2, p2);
1019 if (retval)
1020 return retval;
1021 }
1022 }
1023 }
1024 return 0;
1025}
1026
1027/*
1028 * run func(cpu) on every cpu in /proc/stat
1029 * return max_cpu number
1030 */
1031int for_all_proc_cpus(int (func)(int))
Len Brown103a8fe2010-10-22 23:53:03 -04001032{
1033 FILE *fp;
Len Brownc98d5d92012-06-04 00:56:40 -04001034 int cpu_num;
Len Brown103a8fe2010-10-22 23:53:03 -04001035 int retval;
1036
1037 fp = fopen(proc_stat, "r");
1038 if (fp == NULL) {
1039 perror(proc_stat);
1040 exit(1);
1041 }
1042
1043 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
1044 if (retval != 0) {
1045 perror("/proc/stat format");
1046 exit(1);
1047 }
1048
Len Brownc98d5d92012-06-04 00:56:40 -04001049 while (1) {
1050 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 -04001051 if (retval != 1)
1052 break;
1053
Len Brownc98d5d92012-06-04 00:56:40 -04001054 retval = func(cpu_num);
1055 if (retval) {
1056 fclose(fp);
1057 return(retval);
1058 }
Len Brown103a8fe2010-10-22 23:53:03 -04001059 }
1060 fclose(fp);
Len Brownc98d5d92012-06-04 00:56:40 -04001061 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001062}
1063
1064void re_initialize(void)
1065{
Len Brownc98d5d92012-06-04 00:56:40 -04001066 free_all_buffers();
1067 setup_all_buffers();
1068 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04001069}
1070
Len Brownc98d5d92012-06-04 00:56:40 -04001071
Len Brown103a8fe2010-10-22 23:53:03 -04001072/*
Len Brownc98d5d92012-06-04 00:56:40 -04001073 * count_cpus()
1074 * remember the last one seen, it will be the max
Len Brown103a8fe2010-10-22 23:53:03 -04001075 */
Len Brownc98d5d92012-06-04 00:56:40 -04001076int count_cpus(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001077{
Len Brownc98d5d92012-06-04 00:56:40 -04001078 if (topo.max_cpu_num < cpu)
1079 topo.max_cpu_num = cpu;
Len Brown103a8fe2010-10-22 23:53:03 -04001080
Len Brownc98d5d92012-06-04 00:56:40 -04001081 topo.num_cpus += 1;
1082 return 0;
1083}
1084int mark_cpu_present(int cpu)
1085{
1086 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brown15aaa342012-03-29 22:19:58 -04001087 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001088}
1089
1090void turbostat_loop()
1091{
Len Brownc98d5d92012-06-04 00:56:40 -04001092 int retval;
Len Browne52966c2012-11-08 22:38:05 -05001093 int restarted = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001094
Len Brown103a8fe2010-10-22 23:53:03 -04001095restart:
Len Browne52966c2012-11-08 22:38:05 -05001096 restarted++;
1097
Len Brownc98d5d92012-06-04 00:56:40 -04001098 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001099 if (retval < -1) {
1100 exit(retval);
1101 } else if (retval == -1) {
Len Browne52966c2012-11-08 22:38:05 -05001102 if (restarted > 1) {
1103 exit(retval);
1104 }
Len Brownc98d5d92012-06-04 00:56:40 -04001105 re_initialize();
1106 goto restart;
1107 }
Len Browne52966c2012-11-08 22:38:05 -05001108 restarted = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001109 gettimeofday(&tv_even, (struct timezone *)NULL);
1110
1111 while (1) {
Len Brownc98d5d92012-06-04 00:56:40 -04001112 if (for_all_proc_cpus(cpu_is_not_present)) {
Len Brown103a8fe2010-10-22 23:53:03 -04001113 re_initialize();
1114 goto restart;
1115 }
1116 sleep(interval_sec);
Len Brownc98d5d92012-06-04 00:56:40 -04001117 retval = for_all_cpus(get_counters, ODD_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001118 if (retval < -1) {
1119 exit(retval);
1120 } else if (retval == -1) {
Len Brown15aaa342012-03-29 22:19:58 -04001121 re_initialize();
1122 goto restart;
1123 }
Len Brown103a8fe2010-10-22 23:53:03 -04001124 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04001125 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04001126 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
1127 compute_average(EVEN_COUNTERS);
1128 format_all_counters(EVEN_COUNTERS);
1129 flush_stdout();
Len Brown15aaa342012-03-29 22:19:58 -04001130 sleep(interval_sec);
Len Brownc98d5d92012-06-04 00:56:40 -04001131 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001132 if (retval < -1) {
1133 exit(retval);
1134 } else if (retval == -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04001135 re_initialize();
1136 goto restart;
1137 }
Len Brown103a8fe2010-10-22 23:53:03 -04001138 gettimeofday(&tv_even, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04001139 timersub(&tv_even, &tv_odd, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04001140 for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS);
1141 compute_average(ODD_COUNTERS);
1142 format_all_counters(ODD_COUNTERS);
1143 flush_stdout();
Len Brown103a8fe2010-10-22 23:53:03 -04001144 }
1145}
1146
1147void check_dev_msr()
1148{
1149 struct stat sb;
1150
1151 if (stat("/dev/cpu/0/msr", &sb)) {
1152 fprintf(stderr, "no /dev/cpu/0/msr\n");
1153 fprintf(stderr, "Try \"# modprobe msr\"\n");
1154 exit(-5);
1155 }
1156}
1157
1158void check_super_user()
1159{
1160 if (getuid() != 0) {
1161 fprintf(stderr, "must be root\n");
1162 exit(-6);
1163 }
1164}
1165
1166int has_nehalem_turbo_ratio_limit(unsigned int family, unsigned int model)
1167{
1168 if (!genuine_intel)
1169 return 0;
1170
1171 if (family != 6)
1172 return 0;
1173
1174 switch (model) {
1175 case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
1176 case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
1177 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
1178 case 0x25: /* Westmere Client - Clarkdale, Arrandale */
1179 case 0x2C: /* Westmere EP - Gulftown */
1180 case 0x2A: /* SNB */
1181 case 0x2D: /* SNB Xeon */
Len Brown553575f2011-11-18 03:32:01 -05001182 case 0x3A: /* IVB */
Len Brown13006512012-09-26 18:11:31 -04001183 case 0x3E: /* IVB Xeon */
Len Brown103a8fe2010-10-22 23:53:03 -04001184 return 1;
1185 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1186 case 0x2F: /* Westmere-EX Xeon - Eagleton */
1187 default:
1188 return 0;
1189 }
1190}
Len Brown6574a5d2012-09-21 00:01:31 -04001191int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
1192{
1193 if (!genuine_intel)
1194 return 0;
1195
1196 if (family != 6)
1197 return 0;
1198
1199 switch (model) {
1200 case 0x3E: /* IVB Xeon */
1201 return 1;
1202 default:
1203 return 0;
1204 }
1205}
1206
Len Brown103a8fe2010-10-22 23:53:03 -04001207
1208int is_snb(unsigned int family, unsigned int model)
1209{
1210 if (!genuine_intel)
1211 return 0;
1212
1213 switch (model) {
1214 case 0x2A:
1215 case 0x2D:
Len Brown650a37f2012-06-03 23:34:44 -04001216 case 0x3A: /* IVB */
Len Brown13006512012-09-26 18:11:31 -04001217 case 0x3E: /* IVB Xeon */
Len Brown103a8fe2010-10-22 23:53:03 -04001218 return 1;
1219 }
1220 return 0;
1221}
1222
1223double discover_bclk(unsigned int family, unsigned int model)
1224{
1225 if (is_snb(family, model))
1226 return 100.00;
1227 else
1228 return 133.33;
1229}
1230
1231void check_cpuid()
1232{
1233 unsigned int eax, ebx, ecx, edx, max_level;
1234 unsigned int fms, family, model, stepping;
1235
1236 eax = ebx = ecx = edx = 0;
1237
1238 asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0));
1239
1240 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
1241 genuine_intel = 1;
1242
1243 if (verbose)
1244 fprintf(stderr, "%.4s%.4s%.4s ",
1245 (char *)&ebx, (char *)&edx, (char *)&ecx);
1246
1247 asm("cpuid" : "=a" (fms), "=c" (ecx), "=d" (edx) : "a" (1) : "ebx");
1248 family = (fms >> 8) & 0xf;
1249 model = (fms >> 4) & 0xf;
1250 stepping = fms & 0xf;
1251 if (family == 6 || family == 0xf)
1252 model += ((fms >> 16) & 0xf) << 4;
1253
1254 if (verbose)
1255 fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
1256 max_level, family, model, stepping, family, model, stepping);
1257
1258 if (!(edx & (1 << 5))) {
1259 fprintf(stderr, "CPUID: no MSR\n");
1260 exit(1);
1261 }
1262
1263 /*
1264 * check max extended function levels of CPUID.
1265 * This is needed to check for invariant TSC.
1266 * This check is valid for both Intel and AMD.
1267 */
1268 ebx = ecx = edx = 0;
1269 asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000000));
1270
1271 if (max_level < 0x80000007) {
1272 fprintf(stderr, "CPUID: no invariant TSC (max_level 0x%x)\n", max_level);
1273 exit(1);
1274 }
1275
1276 /*
1277 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
1278 * this check is valid for both Intel and AMD
1279 */
1280 asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000007));
Thomas Renninger8209e052011-01-21 15:11:19 +01001281 has_invariant_tsc = edx & (1 << 8);
Len Brown103a8fe2010-10-22 23:53:03 -04001282
1283 if (!has_invariant_tsc) {
1284 fprintf(stderr, "No invariant TSC\n");
1285 exit(1);
1286 }
1287
1288 /*
1289 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
1290 * this check is valid for both Intel and AMD
1291 */
1292
1293 asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x6));
Thomas Renninger8209e052011-01-21 15:11:19 +01001294 has_aperf = ecx & (1 << 0);
Len Brown103a8fe2010-10-22 23:53:03 -04001295 if (!has_aperf) {
1296 fprintf(stderr, "No APERF MSR\n");
1297 exit(1);
1298 }
1299
1300 do_nehalem_platform_info = genuine_intel && has_invariant_tsc;
1301 do_nhm_cstates = genuine_intel; /* all Intel w/ non-stop TSC have NHM counters */
1302 do_snb_cstates = is_snb(family, model);
1303 bclk = discover_bclk(family, model);
1304
1305 do_nehalem_turbo_ratio_limit = has_nehalem_turbo_ratio_limit(family, model);
Len Brown6574a5d2012-09-21 00:01:31 -04001306 do_ivt_turbo_ratio_limit = has_ivt_turbo_ratio_limit(family, model);
Len Brown103a8fe2010-10-22 23:53:03 -04001307}
1308
1309
1310void usage()
1311{
Len Brownf9240812012-10-06 15:26:31 -04001312 fprintf(stderr, "%s: [-v][-p|-P|-S][-c MSR# | -s]][-C MSR#][-m MSR#][-M MSR#][-i interval_sec | command ...]\n",
Len Brown103a8fe2010-10-22 23:53:03 -04001313 progname);
1314 exit(1);
1315}
1316
1317
1318/*
1319 * in /dev/cpu/ return success for names that are numbers
1320 * ie. filter out ".", "..", "microcode".
1321 */
1322int dir_filter(const struct dirent *dirp)
1323{
1324 if (isdigit(dirp->d_name[0]))
1325 return 1;
1326 else
1327 return 0;
1328}
1329
1330int open_dev_cpu_msr(int dummy1)
1331{
1332 return 0;
1333}
1334
Len Brownc98d5d92012-06-04 00:56:40 -04001335void topology_probe()
1336{
1337 int i;
1338 int max_core_id = 0;
1339 int max_package_id = 0;
1340 int max_siblings = 0;
1341 struct cpu_topology {
1342 int core_id;
1343 int physical_package_id;
1344 } *cpus;
1345
1346 /* Initialize num_cpus, max_cpu_num */
1347 topo.num_cpus = 0;
1348 topo.max_cpu_num = 0;
1349 for_all_proc_cpus(count_cpus);
1350 if (!summary_only && topo.num_cpus > 1)
1351 show_cpu = 1;
1352
1353 if (verbose > 1)
1354 fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
1355
1356 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
1357 if (cpus == NULL) {
1358 perror("calloc cpus");
1359 exit(1);
1360 }
1361
1362 /*
1363 * Allocate and initialize cpu_present_set
1364 */
1365 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
1366 if (cpu_present_set == NULL) {
1367 perror("CPU_ALLOC");
1368 exit(3);
1369 }
1370 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
1371 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
1372 for_all_proc_cpus(mark_cpu_present);
1373
1374 /*
1375 * Allocate and initialize cpu_affinity_set
1376 */
1377 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
1378 if (cpu_affinity_set == NULL) {
1379 perror("CPU_ALLOC");
1380 exit(3);
1381 }
1382 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
1383 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
1384
1385
1386 /*
1387 * For online cpus
1388 * find max_core_id, max_package_id
1389 */
1390 for (i = 0; i <= topo.max_cpu_num; ++i) {
1391 int siblings;
1392
1393 if (cpu_is_not_present(i)) {
1394 if (verbose > 1)
1395 fprintf(stderr, "cpu%d NOT PRESENT\n", i);
1396 continue;
1397 }
1398 cpus[i].core_id = get_core_id(i);
1399 if (cpus[i].core_id > max_core_id)
1400 max_core_id = cpus[i].core_id;
1401
1402 cpus[i].physical_package_id = get_physical_package_id(i);
1403 if (cpus[i].physical_package_id > max_package_id)
1404 max_package_id = cpus[i].physical_package_id;
1405
1406 siblings = get_num_ht_siblings(i);
1407 if (siblings > max_siblings)
1408 max_siblings = siblings;
1409 if (verbose > 1)
1410 fprintf(stderr, "cpu %d pkg %d core %d\n",
1411 i, cpus[i].physical_package_id, cpus[i].core_id);
1412 }
1413 topo.num_cores_per_pkg = max_core_id + 1;
1414 if (verbose > 1)
1415 fprintf(stderr, "max_core_id %d, sizing for %d cores per package\n",
1416 max_core_id, topo.num_cores_per_pkg);
1417 if (!summary_only && topo.num_cores_per_pkg > 1)
1418 show_core = 1;
1419
1420 topo.num_packages = max_package_id + 1;
1421 if (verbose > 1)
1422 fprintf(stderr, "max_package_id %d, sizing for %d packages\n",
1423 max_package_id, topo.num_packages);
1424 if (!summary_only && topo.num_packages > 1)
1425 show_pkg = 1;
1426
1427 topo.num_threads_per_core = max_siblings;
1428 if (verbose > 1)
1429 fprintf(stderr, "max_siblings %d\n", max_siblings);
1430
1431 free(cpus);
1432}
1433
1434void
1435allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
1436{
1437 int i;
1438
1439 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
1440 topo.num_packages, sizeof(struct thread_data));
1441 if (*t == NULL)
1442 goto error;
1443
1444 for (i = 0; i < topo.num_threads_per_core *
1445 topo.num_cores_per_pkg * topo.num_packages; i++)
1446 (*t)[i].cpu_id = -1;
1447
1448 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
1449 sizeof(struct core_data));
1450 if (*c == NULL)
1451 goto error;
1452
1453 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
1454 (*c)[i].core_id = -1;
1455
1456 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
1457 if (*p == NULL)
1458 goto error;
1459
1460 for (i = 0; i < topo.num_packages; i++)
1461 (*p)[i].package_id = i;
1462
1463 return;
1464error:
1465 perror("calloc counters");
1466 exit(1);
1467}
1468/*
1469 * init_counter()
1470 *
1471 * set cpu_id, core_num, pkg_num
1472 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
1473 *
1474 * increment topo.num_cores when 1st core in pkg seen
1475 */
1476void init_counter(struct thread_data *thread_base, struct core_data *core_base,
1477 struct pkg_data *pkg_base, int thread_num, int core_num,
1478 int pkg_num, int cpu_id)
1479{
1480 struct thread_data *t;
1481 struct core_data *c;
1482 struct pkg_data *p;
1483
1484 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
1485 c = GET_CORE(core_base, core_num, pkg_num);
1486 p = GET_PKG(pkg_base, pkg_num);
1487
1488 t->cpu_id = cpu_id;
1489 if (thread_num == 0) {
1490 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
1491 if (cpu_is_first_core_in_package(cpu_id))
1492 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
1493 }
1494
1495 c->core_id = core_num;
1496 p->package_id = pkg_num;
1497}
1498
1499
1500int initialize_counters(int cpu_id)
1501{
1502 int my_thread_id, my_core_id, my_package_id;
1503
1504 my_package_id = get_physical_package_id(cpu_id);
1505 my_core_id = get_core_id(cpu_id);
1506
1507 if (cpu_is_first_sibling_in_core(cpu_id)) {
1508 my_thread_id = 0;
1509 topo.num_cores++;
1510 } else {
1511 my_thread_id = 1;
1512 }
1513
1514 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
1515 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
1516 return 0;
1517}
1518
1519void allocate_output_buffer()
1520{
1521 output_buffer = calloc(1, (1 + topo.num_cpus) * 128);
1522 outp = output_buffer;
1523 if (outp == NULL) {
1524 perror("calloc");
1525 exit(-1);
1526 }
1527}
1528
1529void setup_all_buffers(void)
1530{
1531 topology_probe();
1532 allocate_counters(&thread_even, &core_even, &package_even);
1533 allocate_counters(&thread_odd, &core_odd, &package_odd);
1534 allocate_output_buffer();
1535 for_all_proc_cpus(initialize_counters);
1536}
Len Brown103a8fe2010-10-22 23:53:03 -04001537void turbostat_init()
1538{
1539 check_cpuid();
1540
1541 check_dev_msr();
1542 check_super_user();
1543
Len Brownc98d5d92012-06-04 00:56:40 -04001544 setup_all_buffers();
Len Brown103a8fe2010-10-22 23:53:03 -04001545
1546 if (verbose)
Len Brownc98d5d92012-06-04 00:56:40 -04001547 print_verbose_header();
Len Brown103a8fe2010-10-22 23:53:03 -04001548}
1549
1550int fork_it(char **argv)
1551{
Len Brown103a8fe2010-10-22 23:53:03 -04001552 pid_t child_pid;
Len Brownd91bb172012-11-01 00:08:19 -04001553 int status;
Len Brownd15cf7c2012-06-03 23:24:00 -04001554
Len Brownd91bb172012-11-01 00:08:19 -04001555 status = for_all_cpus(get_counters, EVEN_COUNTERS);
1556 if (status)
1557 exit(status);
Len Brownc98d5d92012-06-04 00:56:40 -04001558 /* clear affinity side-effect of get_counters() */
1559 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
Len Brown103a8fe2010-10-22 23:53:03 -04001560 gettimeofday(&tv_even, (struct timezone *)NULL);
1561
1562 child_pid = fork();
1563 if (!child_pid) {
1564 /* child */
1565 execvp(argv[0], argv);
1566 } else {
Len Brown103a8fe2010-10-22 23:53:03 -04001567
1568 /* parent */
1569 if (child_pid == -1) {
1570 perror("fork");
1571 exit(1);
1572 }
1573
1574 signal(SIGINT, SIG_IGN);
1575 signal(SIGQUIT, SIG_IGN);
1576 if (waitpid(child_pid, &status, 0) == -1) {
1577 perror("wait");
Len Brownd91bb172012-11-01 00:08:19 -04001578 exit(status);
Len Brown103a8fe2010-10-22 23:53:03 -04001579 }
1580 }
Len Brownc98d5d92012-06-04 00:56:40 -04001581 /*
1582 * n.b. fork_it() does not check for errors from for_all_cpus()
1583 * because re-starting is problematic when forking
1584 */
1585 for_all_cpus(get_counters, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04001586 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04001587 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04001588 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
1589 compute_average(EVEN_COUNTERS);
1590 format_all_counters(EVEN_COUNTERS);
1591 flush_stderr();
Len Brown103a8fe2010-10-22 23:53:03 -04001592
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001593 fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
Len Brown103a8fe2010-10-22 23:53:03 -04001594
Len Brownd91bb172012-11-01 00:08:19 -04001595 return status;
Len Brown103a8fe2010-10-22 23:53:03 -04001596}
1597
1598void cmdline(int argc, char **argv)
1599{
1600 int opt;
1601
1602 progname = argv[0];
1603
Len Brown39300ff2012-11-01 00:16:34 -04001604 while ((opt = getopt(argc, argv, "+pPSvi:sc:sC:m:M:")) != -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04001605 switch (opt) {
Len Brownf9240812012-10-06 15:26:31 -04001606 case 'p':
Len Brownc98d5d92012-06-04 00:56:40 -04001607 show_core_only++;
1608 break;
Len Brownf9240812012-10-06 15:26:31 -04001609 case 'P':
Len Brownc98d5d92012-06-04 00:56:40 -04001610 show_pkg_only++;
1611 break;
Len Brownf9240812012-10-06 15:26:31 -04001612 case 'S':
Len Browne23da032012-02-06 18:37:16 -05001613 summary_only++;
1614 break;
Len Brown103a8fe2010-10-22 23:53:03 -04001615 case 'v':
1616 verbose++;
1617 break;
1618 case 'i':
1619 interval_sec = atoi(optarg);
1620 break;
Len Brownf9240812012-10-06 15:26:31 -04001621 case 'c':
Len Brown8e180f32012-09-22 01:25:08 -04001622 sscanf(optarg, "%x", &extra_delta_offset32);
1623 break;
Len Brownf9240812012-10-06 15:26:31 -04001624 case 's':
1625 extra_delta_offset32 = 0x34; /* SMI counter */
1626 break;
1627 case 'C':
Len Brown8e180f32012-09-22 01:25:08 -04001628 sscanf(optarg, "%x", &extra_delta_offset64);
1629 break;
Len Brown2f32edf2012-09-21 23:45:46 -04001630 case 'm':
1631 sscanf(optarg, "%x", &extra_msr_offset32);
Len Brown2f32edf2012-09-21 23:45:46 -04001632 break;
1633 case 'M':
1634 sscanf(optarg, "%x", &extra_msr_offset64);
Len Brown103a8fe2010-10-22 23:53:03 -04001635 break;
1636 default:
1637 usage();
1638 }
1639 }
1640}
1641
1642int main(int argc, char **argv)
1643{
1644 cmdline(argc, argv);
1645
1646 if (verbose > 1)
Len Brownf9240812012-10-06 15:26:31 -04001647 fprintf(stderr, "turbostat v2.1 October 6, 2012"
Len Brown103a8fe2010-10-22 23:53:03 -04001648 " - Len Brown <lenb@kernel.org>\n");
Len Brown103a8fe2010-10-22 23:53:03 -04001649
1650 turbostat_init();
1651
1652 /*
1653 * if any params left, it must be a command to fork
1654 */
1655 if (argc - optind)
1656 return fork_it(argv + optind);
1657 else
1658 turbostat_loop();
1659
1660 return 0;
1661}