blob: cb031472bedebb953ebad91639a1d0ba31fe12db [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);
Len Brownddac0d62012-11-30 01:01:40 -0500426 fflush(stdout);
Len Brownc98d5d92012-06-04 00:56:40 -0400427 outp = output_buffer;
428}
429void flush_stderr()
430{
431 fputs(output_buffer, stderr);
432 outp = output_buffer;
433}
434void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
435{
Len Browne23da032012-02-06 18:37:16 -0500436 static int printed;
Len Brown103a8fe2010-10-22 23:53:03 -0400437
Len Browne23da032012-02-06 18:37:16 -0500438 if (!printed || !summary_only)
439 print_header();
Len Brown103a8fe2010-10-22 23:53:03 -0400440
Len Brownc98d5d92012-06-04 00:56:40 -0400441 if (topo.num_cpus > 1)
442 format_counters(&average.threads, &average.cores,
443 &average.packages);
Len Brown103a8fe2010-10-22 23:53:03 -0400444
Len Browne23da032012-02-06 18:37:16 -0500445 printed = 1;
446
447 if (summary_only)
448 return;
449
Len Brownc98d5d92012-06-04 00:56:40 -0400450 for_all_cpus(format_counters, t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400451}
452
Len Brownc98d5d92012-06-04 00:56:40 -0400453void
454delta_package(struct pkg_data *new, struct pkg_data *old)
Len Brown103a8fe2010-10-22 23:53:03 -0400455{
Len Brownc98d5d92012-06-04 00:56:40 -0400456 old->pc2 = new->pc2 - old->pc2;
457 old->pc3 = new->pc3 - old->pc3;
458 old->pc6 = new->pc6 - old->pc6;
459 old->pc7 = new->pc7 - old->pc7;
460}
Len Brown103a8fe2010-10-22 23:53:03 -0400461
Len Brownc98d5d92012-06-04 00:56:40 -0400462void
463delta_core(struct core_data *new, struct core_data *old)
464{
465 old->c3 = new->c3 - old->c3;
466 old->c6 = new->c6 - old->c6;
467 old->c7 = new->c7 - old->c7;
468}
Len Brown103a8fe2010-10-22 23:53:03 -0400469
Len Brownc3ae3312012-06-13 21:31:46 -0400470/*
471 * old = new - old
472 */
Len Brownc98d5d92012-06-04 00:56:40 -0400473void
474delta_thread(struct thread_data *new, struct thread_data *old,
475 struct core_data *core_delta)
476{
477 old->tsc = new->tsc - old->tsc;
Len Brown103a8fe2010-10-22 23:53:03 -0400478
Len Brownc98d5d92012-06-04 00:56:40 -0400479 /* check for TSC < 1 Mcycles over interval */
480 if (old->tsc < (1000 * 1000)) {
481 fprintf(stderr, "Insanely slow TSC rate, TSC stops in idle?\n");
482 fprintf(stderr, "You can disable all c-states by booting with \"idle=poll\"\n");
483 fprintf(stderr, "or just the deep ones with \"processor.max_cstate=1\"\n");
484 exit(-3);
485 }
Len Brown103a8fe2010-10-22 23:53:03 -0400486
Len Brownc98d5d92012-06-04 00:56:40 -0400487 old->c1 = new->c1 - old->c1;
Len Brown103a8fe2010-10-22 23:53:03 -0400488
Len Brownc98d5d92012-06-04 00:56:40 -0400489 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
490 old->aperf = new->aperf - old->aperf;
491 old->mperf = new->mperf - old->mperf;
492 } else {
Len Brown103a8fe2010-10-22 23:53:03 -0400493
Len Brownc98d5d92012-06-04 00:56:40 -0400494 if (!aperf_mperf_unstable) {
495 fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname);
496 fprintf(stderr, "* Frequency results do not cover entire interval *\n");
497 fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n");
498
499 aperf_mperf_unstable = 1;
500 }
Len Brown103a8fe2010-10-22 23:53:03 -0400501 /*
Len Brownc98d5d92012-06-04 00:56:40 -0400502 * mperf delta is likely a huge "positive" number
503 * can not use it for calculating c0 time
Len Brown103a8fe2010-10-22 23:53:03 -0400504 */
Len Brownc98d5d92012-06-04 00:56:40 -0400505 skip_c0 = 1;
506 skip_c1 = 1;
507 }
Len Brown103a8fe2010-10-22 23:53:03 -0400508
Len Brown103a8fe2010-10-22 23:53:03 -0400509
Len Brownc98d5d92012-06-04 00:56:40 -0400510 /*
Len Brownc3ae3312012-06-13 21:31:46 -0400511 * As counter collection is not atomic,
512 * it is possible for mperf's non-halted cycles + idle states
Len Brownc98d5d92012-06-04 00:56:40 -0400513 * to exceed TSC's all cycles: show c1 = 0% in that case.
514 */
Len Brownc3ae3312012-06-13 21:31:46 -0400515 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
Len Brownc98d5d92012-06-04 00:56:40 -0400516 old->c1 = 0;
517 else {
518 /* normal case, derive c1 */
519 old->c1 = old->tsc - old->mperf - core_delta->c3
520 - core_delta->c6 - core_delta->c7;
521 }
Len Brownc3ae3312012-06-13 21:31:46 -0400522
Len Brownc98d5d92012-06-04 00:56:40 -0400523 if (old->mperf == 0) {
Len Brownc3ae3312012-06-13 21:31:46 -0400524 if (verbose > 1) fprintf(stderr, "cpu%d MPERF 0!\n", old->cpu_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400525 old->mperf = 1; /* divide by 0 protection */
526 }
527
Len Brown8e180f32012-09-22 01:25:08 -0400528 old->extra_delta32 = new->extra_delta32 - old->extra_delta32;
529 old->extra_delta32 &= 0xFFFFFFFF;
530
531 old->extra_delta64 = new->extra_delta64 - old->extra_delta64;
532
Len Brownc98d5d92012-06-04 00:56:40 -0400533 /*
Len Brown8e180f32012-09-22 01:25:08 -0400534 * Extra MSR is just a snapshot, simply copy latest w/o subtracting
Len Brownc98d5d92012-06-04 00:56:40 -0400535 */
Len Brown2f32edf2012-09-21 23:45:46 -0400536 old->extra_msr32 = new->extra_msr32;
537 old->extra_msr64 = new->extra_msr64;
Len Brownc98d5d92012-06-04 00:56:40 -0400538}
539
540int delta_cpu(struct thread_data *t, struct core_data *c,
541 struct pkg_data *p, struct thread_data *t2,
542 struct core_data *c2, struct pkg_data *p2)
543{
544 /* calculate core delta only for 1st thread in core */
545 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
546 delta_core(c, c2);
547
548 /* always calculate thread delta */
549 delta_thread(t, t2, c2); /* c2 is core delta */
550
551 /* calculate package delta only for 1st core in package */
552 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
553 delta_package(p, p2);
554
555 return 0;
556}
557
558void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
559{
560 t->tsc = 0;
561 t->aperf = 0;
562 t->mperf = 0;
563 t->c1 = 0;
564
Len Brown8e180f32012-09-22 01:25:08 -0400565 t->extra_delta32 = 0;
566 t->extra_delta64 = 0;
567
Len Brownc98d5d92012-06-04 00:56:40 -0400568 /* tells format_counters to dump all fields from this set */
569 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
570
571 c->c3 = 0;
572 c->c6 = 0;
573 c->c7 = 0;
574
575 p->pc2 = 0;
576 p->pc3 = 0;
577 p->pc6 = 0;
578 p->pc7 = 0;
579}
580int sum_counters(struct thread_data *t, struct core_data *c,
581 struct pkg_data *p)
582{
583 average.threads.tsc += t->tsc;
584 average.threads.aperf += t->aperf;
585 average.threads.mperf += t->mperf;
586 average.threads.c1 += t->c1;
587
Len Brown8e180f32012-09-22 01:25:08 -0400588 average.threads.extra_delta32 += t->extra_delta32;
589 average.threads.extra_delta64 += t->extra_delta64;
590
Len Brownc98d5d92012-06-04 00:56:40 -0400591 /* sum per-core values only for 1st thread in core */
592 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
593 return 0;
594
595 average.cores.c3 += c->c3;
596 average.cores.c6 += c->c6;
597 average.cores.c7 += c->c7;
598
599 /* sum per-pkg values only for 1st core in pkg */
600 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
601 return 0;
602
603 average.packages.pc2 += p->pc2;
604 average.packages.pc3 += p->pc3;
605 average.packages.pc6 += p->pc6;
606 average.packages.pc7 += p->pc7;
607
608 return 0;
609}
610/*
611 * sum the counters for all cpus in the system
612 * compute the weighted average
613 */
614void compute_average(struct thread_data *t, struct core_data *c,
615 struct pkg_data *p)
616{
617 clear_counters(&average.threads, &average.cores, &average.packages);
618
619 for_all_cpus(sum_counters, t, c, p);
620
621 average.threads.tsc /= topo.num_cpus;
622 average.threads.aperf /= topo.num_cpus;
623 average.threads.mperf /= topo.num_cpus;
624 average.threads.c1 /= topo.num_cpus;
625
Len Brown8e180f32012-09-22 01:25:08 -0400626 average.threads.extra_delta32 /= topo.num_cpus;
627 average.threads.extra_delta32 &= 0xFFFFFFFF;
628
629 average.threads.extra_delta64 /= topo.num_cpus;
630
Len Brownc98d5d92012-06-04 00:56:40 -0400631 average.cores.c3 /= topo.num_cores;
632 average.cores.c6 /= topo.num_cores;
633 average.cores.c7 /= topo.num_cores;
634
635 average.packages.pc2 /= topo.num_packages;
636 average.packages.pc3 /= topo.num_packages;
637 average.packages.pc6 /= topo.num_packages;
638 average.packages.pc7 /= topo.num_packages;
639}
640
641static unsigned long long rdtsc(void)
642{
643 unsigned int low, high;
644
645 asm volatile("rdtsc" : "=a" (low), "=d" (high));
646
647 return low | ((unsigned long long)high) << 32;
648}
649
650
651/*
652 * get_counters(...)
653 * migrate to cpu
654 * acquire and record local counters for that cpu
655 */
656int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
657{
658 int cpu = t->cpu_id;
659
Len Browne52966c2012-11-08 22:38:05 -0500660 if (cpu_migrate(cpu)) {
661 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
Len Brownc98d5d92012-06-04 00:56:40 -0400662 return -1;
Len Browne52966c2012-11-08 22:38:05 -0500663 }
Len Brownc98d5d92012-06-04 00:56:40 -0400664
665 t->tsc = rdtsc(); /* we are running on local CPU of interest */
666
667 if (has_aperf) {
Len Brown9c63a652012-10-31 01:29:52 -0400668 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
Len Brownc98d5d92012-06-04 00:56:40 -0400669 return -3;
Len Brown9c63a652012-10-31 01:29:52 -0400670 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
Len Brownc98d5d92012-06-04 00:56:40 -0400671 return -4;
672 }
673
Len Brown8e180f32012-09-22 01:25:08 -0400674 if (extra_delta_offset32) {
675 if (get_msr(cpu, extra_delta_offset32, &t->extra_delta32))
Len Brown2f32edf2012-09-21 23:45:46 -0400676 return -5;
Len Brown8e180f32012-09-22 01:25:08 -0400677 t->extra_delta32 &= 0xFFFFFFFF;
678 }
679
680 if (extra_delta_offset64)
681 if (get_msr(cpu, extra_delta_offset64, &t->extra_delta64))
682 return -5;
683
684 if (extra_msr_offset32) {
685 if (get_msr(cpu, extra_msr_offset32, &t->extra_msr32))
686 return -5;
687 t->extra_msr32 &= 0xFFFFFFFF;
688 }
Len Brown2f32edf2012-09-21 23:45:46 -0400689
690 if (extra_msr_offset64)
691 if (get_msr(cpu, extra_msr_offset64, &t->extra_msr64))
Len Brownc98d5d92012-06-04 00:56:40 -0400692 return -5;
693
694 /* collect core counters only for 1st thread in core */
695 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
696 return 0;
697
698 if (do_nhm_cstates) {
699 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
700 return -6;
701 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
702 return -7;
703 }
704
705 if (do_snb_cstates)
706 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
707 return -8;
708
709 /* collect package counters only for 1st core in package */
710 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
711 return 0;
712
713 if (do_nhm_cstates) {
714 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
715 return -9;
716 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
717 return -10;
718 }
719 if (do_snb_cstates) {
720 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
721 return -11;
722 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
723 return -12;
Len Brown103a8fe2010-10-22 23:53:03 -0400724 }
725 return 0;
726}
727
Len Brownc98d5d92012-06-04 00:56:40 -0400728void print_verbose_header(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400729{
730 unsigned long long msr;
731 unsigned int ratio;
732
733 if (!do_nehalem_platform_info)
734 return;
735
Len Brown9c63a652012-10-31 01:29:52 -0400736 get_msr(0, MSR_NHM_PLATFORM_INFO, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -0400737
Len Brown6574a5d2012-09-21 00:01:31 -0400738 if (verbose > 1)
Len Brown9c63a652012-10-31 01:29:52 -0400739 fprintf(stderr, "MSR_NHM_PLATFORM_INFO: 0x%llx\n", msr);
Len Brown6574a5d2012-09-21 00:01:31 -0400740
Len Brown103a8fe2010-10-22 23:53:03 -0400741 ratio = (msr >> 40) & 0xFF;
742 fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency\n",
743 ratio, bclk, ratio * bclk);
744
745 ratio = (msr >> 8) & 0xFF;
746 fprintf(stderr, "%d * %.0f = %.0f MHz TSC frequency\n",
747 ratio, bclk, ratio * bclk);
748
Len Brown6574a5d2012-09-21 00:01:31 -0400749 if (!do_ivt_turbo_ratio_limit)
750 goto print_nhm_turbo_ratio_limits;
751
752 get_msr(0, MSR_IVT_TURBO_RATIO_LIMIT, &msr);
753
Len Brown103a8fe2010-10-22 23:53:03 -0400754 if (verbose > 1)
Len Brown6574a5d2012-09-21 00:01:31 -0400755 fprintf(stderr, "MSR_IVT_TURBO_RATIO_LIMIT: 0x%llx\n", msr);
756
757 ratio = (msr >> 56) & 0xFF;
758 if (ratio)
759 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 16 active cores\n",
760 ratio, bclk, ratio * bclk);
761
762 ratio = (msr >> 48) & 0xFF;
763 if (ratio)
764 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 15 active cores\n",
765 ratio, bclk, ratio * bclk);
766
767 ratio = (msr >> 40) & 0xFF;
768 if (ratio)
769 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 14 active cores\n",
770 ratio, bclk, ratio * bclk);
771
772 ratio = (msr >> 32) & 0xFF;
773 if (ratio)
774 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 13 active cores\n",
775 ratio, bclk, ratio * bclk);
776
777 ratio = (msr >> 24) & 0xFF;
778 if (ratio)
779 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 12 active cores\n",
780 ratio, bclk, ratio * bclk);
781
782 ratio = (msr >> 16) & 0xFF;
783 if (ratio)
784 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 11 active cores\n",
785 ratio, bclk, ratio * bclk);
786
787 ratio = (msr >> 8) & 0xFF;
788 if (ratio)
789 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 10 active cores\n",
790 ratio, bclk, ratio * bclk);
791
792 ratio = (msr >> 0) & 0xFF;
793 if (ratio)
794 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 9 active cores\n",
795 ratio, bclk, ratio * bclk);
796
797print_nhm_turbo_ratio_limits:
Len Brown103a8fe2010-10-22 23:53:03 -0400798
799 if (!do_nehalem_turbo_ratio_limit)
800 return;
801
Len Brown9c63a652012-10-31 01:29:52 -0400802 get_msr(0, MSR_NHM_TURBO_RATIO_LIMIT, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -0400803
Len Brown6574a5d2012-09-21 00:01:31 -0400804 if (verbose > 1)
Len Brown9c63a652012-10-31 01:29:52 -0400805 fprintf(stderr, "MSR_NHM_TURBO_RATIO_LIMIT: 0x%llx\n", msr);
Len Brown6574a5d2012-09-21 00:01:31 -0400806
807 ratio = (msr >> 56) & 0xFF;
808 if (ratio)
809 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 8 active cores\n",
810 ratio, bclk, ratio * bclk);
811
812 ratio = (msr >> 48) & 0xFF;
813 if (ratio)
814 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 7 active cores\n",
815 ratio, bclk, ratio * bclk);
816
817 ratio = (msr >> 40) & 0xFF;
818 if (ratio)
819 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 6 active cores\n",
820 ratio, bclk, ratio * bclk);
821
822 ratio = (msr >> 32) & 0xFF;
823 if (ratio)
824 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 5 active cores\n",
825 ratio, bclk, ratio * bclk);
826
Len Brown103a8fe2010-10-22 23:53:03 -0400827 ratio = (msr >> 24) & 0xFF;
828 if (ratio)
829 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
830 ratio, bclk, ratio * bclk);
831
832 ratio = (msr >> 16) & 0xFF;
833 if (ratio)
834 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
835 ratio, bclk, ratio * bclk);
836
837 ratio = (msr >> 8) & 0xFF;
838 if (ratio)
839 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
840 ratio, bclk, ratio * bclk);
841
842 ratio = (msr >> 0) & 0xFF;
843 if (ratio)
844 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
845 ratio, bclk, ratio * bclk);
Len Brown103a8fe2010-10-22 23:53:03 -0400846}
847
Len Brownc98d5d92012-06-04 00:56:40 -0400848void free_all_buffers(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400849{
Len Brownc98d5d92012-06-04 00:56:40 -0400850 CPU_FREE(cpu_present_set);
851 cpu_present_set = NULL;
852 cpu_present_set = 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400853
Len Brownc98d5d92012-06-04 00:56:40 -0400854 CPU_FREE(cpu_affinity_set);
855 cpu_affinity_set = NULL;
856 cpu_affinity_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400857
Len Brownc98d5d92012-06-04 00:56:40 -0400858 free(thread_even);
859 free(core_even);
860 free(package_even);
861
862 thread_even = NULL;
863 core_even = NULL;
864 package_even = NULL;
865
866 free(thread_odd);
867 free(core_odd);
868 free(package_odd);
869
870 thread_odd = NULL;
871 core_odd = NULL;
872 package_odd = NULL;
873
874 free(output_buffer);
875 output_buffer = NULL;
876 outp = NULL;
Len Brown103a8fe2010-10-22 23:53:03 -0400877}
878
Len Brownc98d5d92012-06-04 00:56:40 -0400879/*
880 * cpu_is_first_sibling_in_core(cpu)
881 * return 1 if given CPU is 1st HT sibling in the core
882 */
883int cpu_is_first_sibling_in_core(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -0400884{
Len Brownc98d5d92012-06-04 00:56:40 -0400885 char path[64];
886 FILE *filep;
887 int first_cpu;
Len Brown103a8fe2010-10-22 23:53:03 -0400888
Len Brownc98d5d92012-06-04 00:56:40 -0400889 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
890 filep = fopen(path, "r");
891 if (filep == NULL) {
892 perror(path);
893 exit(1);
894 }
895 fscanf(filep, "%d", &first_cpu);
896 fclose(filep);
897 return (cpu == first_cpu);
Len Brown103a8fe2010-10-22 23:53:03 -0400898}
899
Len Brownc98d5d92012-06-04 00:56:40 -0400900/*
901 * cpu_is_first_core_in_package(cpu)
902 * return 1 if given CPU is 1st core in package
903 */
904int cpu_is_first_core_in_package(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -0400905{
Len Brownc98d5d92012-06-04 00:56:40 -0400906 char path[64];
907 FILE *filep;
908 int first_cpu;
Len Brown103a8fe2010-10-22 23:53:03 -0400909
Len Brownc98d5d92012-06-04 00:56:40 -0400910 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
911 filep = fopen(path, "r");
912 if (filep == NULL) {
913 perror(path);
Len Brown103a8fe2010-10-22 23:53:03 -0400914 exit(1);
915 }
Len Brownc98d5d92012-06-04 00:56:40 -0400916 fscanf(filep, "%d", &first_cpu);
917 fclose(filep);
918 return (cpu == first_cpu);
Len Brown103a8fe2010-10-22 23:53:03 -0400919}
920
921int get_physical_package_id(int cpu)
922{
Len Brownc98d5d92012-06-04 00:56:40 -0400923 char path[80];
Len Brown103a8fe2010-10-22 23:53:03 -0400924 FILE *filep;
925 int pkg;
926
927 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
928 filep = fopen(path, "r");
929 if (filep == NULL) {
930 perror(path);
931 exit(1);
932 }
933 fscanf(filep, "%d", &pkg);
934 fclose(filep);
935 return pkg;
936}
937
938int get_core_id(int cpu)
939{
Len Brownc98d5d92012-06-04 00:56:40 -0400940 char path[80];
Len Brown103a8fe2010-10-22 23:53:03 -0400941 FILE *filep;
942 int core;
943
944 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
945 filep = fopen(path, "r");
946 if (filep == NULL) {
947 perror(path);
948 exit(1);
949 }
950 fscanf(filep, "%d", &core);
951 fclose(filep);
952 return core;
953}
954
Len Brownc98d5d92012-06-04 00:56:40 -0400955int get_num_ht_siblings(int cpu)
956{
957 char path[80];
958 FILE *filep;
959 int sib1, sib2;
960 int matches;
961 char character;
962
963 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
964 filep = fopen(path, "r");
965 if (filep == NULL) {
966 perror(path);
967 exit(1);
968 }
969 /*
970 * file format:
971 * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4)
972 * otherwinse 1 sibling (self).
973 */
974 matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2);
975
976 fclose(filep);
977
978 if (matches == 3)
979 return 2;
980 else
981 return 1;
982}
983
Len Brown103a8fe2010-10-22 23:53:03 -0400984/*
Len Brownc98d5d92012-06-04 00:56:40 -0400985 * run func(thread, core, package) in topology order
986 * skip non-present cpus
Len Brown103a8fe2010-10-22 23:53:03 -0400987 */
988
Len Brownc98d5d92012-06-04 00:56:40 -0400989int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
990 struct pkg_data *, struct thread_data *, struct core_data *,
991 struct pkg_data *), struct thread_data *thread_base,
992 struct core_data *core_base, struct pkg_data *pkg_base,
993 struct thread_data *thread_base2, struct core_data *core_base2,
994 struct pkg_data *pkg_base2)
995{
996 int retval, pkg_no, core_no, thread_no;
997
998 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
999 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
1000 for (thread_no = 0; thread_no <
1001 topo.num_threads_per_core; ++thread_no) {
1002 struct thread_data *t, *t2;
1003 struct core_data *c, *c2;
1004 struct pkg_data *p, *p2;
1005
1006 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
1007
1008 if (cpu_is_not_present(t->cpu_id))
1009 continue;
1010
1011 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
1012
1013 c = GET_CORE(core_base, core_no, pkg_no);
1014 c2 = GET_CORE(core_base2, core_no, pkg_no);
1015
1016 p = GET_PKG(pkg_base, pkg_no);
1017 p2 = GET_PKG(pkg_base2, pkg_no);
1018
1019 retval = func(t, c, p, t2, c2, p2);
1020 if (retval)
1021 return retval;
1022 }
1023 }
1024 }
1025 return 0;
1026}
1027
1028/*
1029 * run func(cpu) on every cpu in /proc/stat
1030 * return max_cpu number
1031 */
1032int for_all_proc_cpus(int (func)(int))
Len Brown103a8fe2010-10-22 23:53:03 -04001033{
1034 FILE *fp;
Len Brownc98d5d92012-06-04 00:56:40 -04001035 int cpu_num;
Len Brown103a8fe2010-10-22 23:53:03 -04001036 int retval;
1037
1038 fp = fopen(proc_stat, "r");
1039 if (fp == NULL) {
1040 perror(proc_stat);
1041 exit(1);
1042 }
1043
1044 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
1045 if (retval != 0) {
1046 perror("/proc/stat format");
1047 exit(1);
1048 }
1049
Len Brownc98d5d92012-06-04 00:56:40 -04001050 while (1) {
1051 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 -04001052 if (retval != 1)
1053 break;
1054
Len Brownc98d5d92012-06-04 00:56:40 -04001055 retval = func(cpu_num);
1056 if (retval) {
1057 fclose(fp);
1058 return(retval);
1059 }
Len Brown103a8fe2010-10-22 23:53:03 -04001060 }
1061 fclose(fp);
Len Brownc98d5d92012-06-04 00:56:40 -04001062 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001063}
1064
1065void re_initialize(void)
1066{
Len Brownc98d5d92012-06-04 00:56:40 -04001067 free_all_buffers();
1068 setup_all_buffers();
1069 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04001070}
1071
Len Brownc98d5d92012-06-04 00:56:40 -04001072
Len Brown103a8fe2010-10-22 23:53:03 -04001073/*
Len Brownc98d5d92012-06-04 00:56:40 -04001074 * count_cpus()
1075 * remember the last one seen, it will be the max
Len Brown103a8fe2010-10-22 23:53:03 -04001076 */
Len Brownc98d5d92012-06-04 00:56:40 -04001077int count_cpus(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001078{
Len Brownc98d5d92012-06-04 00:56:40 -04001079 if (topo.max_cpu_num < cpu)
1080 topo.max_cpu_num = cpu;
Len Brown103a8fe2010-10-22 23:53:03 -04001081
Len Brownc98d5d92012-06-04 00:56:40 -04001082 topo.num_cpus += 1;
1083 return 0;
1084}
1085int mark_cpu_present(int cpu)
1086{
1087 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brown15aaa342012-03-29 22:19:58 -04001088 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001089}
1090
1091void turbostat_loop()
1092{
Len Brownc98d5d92012-06-04 00:56:40 -04001093 int retval;
Len Browne52966c2012-11-08 22:38:05 -05001094 int restarted = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001095
Len Brown103a8fe2010-10-22 23:53:03 -04001096restart:
Len Browne52966c2012-11-08 22:38:05 -05001097 restarted++;
1098
Len Brownc98d5d92012-06-04 00:56:40 -04001099 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001100 if (retval < -1) {
1101 exit(retval);
1102 } else if (retval == -1) {
Len Browne52966c2012-11-08 22:38:05 -05001103 if (restarted > 1) {
1104 exit(retval);
1105 }
Len Brownc98d5d92012-06-04 00:56:40 -04001106 re_initialize();
1107 goto restart;
1108 }
Len Browne52966c2012-11-08 22:38:05 -05001109 restarted = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001110 gettimeofday(&tv_even, (struct timezone *)NULL);
1111
1112 while (1) {
Len Brownc98d5d92012-06-04 00:56:40 -04001113 if (for_all_proc_cpus(cpu_is_not_present)) {
Len Brown103a8fe2010-10-22 23:53:03 -04001114 re_initialize();
1115 goto restart;
1116 }
1117 sleep(interval_sec);
Len Brownc98d5d92012-06-04 00:56:40 -04001118 retval = for_all_cpus(get_counters, ODD_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001119 if (retval < -1) {
1120 exit(retval);
1121 } else if (retval == -1) {
Len Brown15aaa342012-03-29 22:19:58 -04001122 re_initialize();
1123 goto restart;
1124 }
Len Brown103a8fe2010-10-22 23:53:03 -04001125 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04001126 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04001127 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
1128 compute_average(EVEN_COUNTERS);
1129 format_all_counters(EVEN_COUNTERS);
1130 flush_stdout();
Len Brown15aaa342012-03-29 22:19:58 -04001131 sleep(interval_sec);
Len Brownc98d5d92012-06-04 00:56:40 -04001132 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001133 if (retval < -1) {
1134 exit(retval);
1135 } else if (retval == -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04001136 re_initialize();
1137 goto restart;
1138 }
Len Brown103a8fe2010-10-22 23:53:03 -04001139 gettimeofday(&tv_even, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04001140 timersub(&tv_even, &tv_odd, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04001141 for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS);
1142 compute_average(ODD_COUNTERS);
1143 format_all_counters(ODD_COUNTERS);
1144 flush_stdout();
Len Brown103a8fe2010-10-22 23:53:03 -04001145 }
1146}
1147
1148void check_dev_msr()
1149{
1150 struct stat sb;
1151
1152 if (stat("/dev/cpu/0/msr", &sb)) {
1153 fprintf(stderr, "no /dev/cpu/0/msr\n");
1154 fprintf(stderr, "Try \"# modprobe msr\"\n");
1155 exit(-5);
1156 }
1157}
1158
1159void check_super_user()
1160{
1161 if (getuid() != 0) {
1162 fprintf(stderr, "must be root\n");
1163 exit(-6);
1164 }
1165}
1166
1167int has_nehalem_turbo_ratio_limit(unsigned int family, unsigned int model)
1168{
1169 if (!genuine_intel)
1170 return 0;
1171
1172 if (family != 6)
1173 return 0;
1174
1175 switch (model) {
1176 case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
1177 case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
1178 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
1179 case 0x25: /* Westmere Client - Clarkdale, Arrandale */
1180 case 0x2C: /* Westmere EP - Gulftown */
1181 case 0x2A: /* SNB */
1182 case 0x2D: /* SNB Xeon */
Len Brown553575f2011-11-18 03:32:01 -05001183 case 0x3A: /* IVB */
Len Brown13006512012-09-26 18:11:31 -04001184 case 0x3E: /* IVB Xeon */
Len Brown103a8fe2010-10-22 23:53:03 -04001185 return 1;
1186 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1187 case 0x2F: /* Westmere-EX Xeon - Eagleton */
1188 default:
1189 return 0;
1190 }
1191}
Len Brown6574a5d2012-09-21 00:01:31 -04001192int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
1193{
1194 if (!genuine_intel)
1195 return 0;
1196
1197 if (family != 6)
1198 return 0;
1199
1200 switch (model) {
1201 case 0x3E: /* IVB Xeon */
1202 return 1;
1203 default:
1204 return 0;
1205 }
1206}
1207
Len Brown103a8fe2010-10-22 23:53:03 -04001208
1209int is_snb(unsigned int family, unsigned int model)
1210{
1211 if (!genuine_intel)
1212 return 0;
1213
1214 switch (model) {
1215 case 0x2A:
1216 case 0x2D:
Len Brown650a37f2012-06-03 23:34:44 -04001217 case 0x3A: /* IVB */
Len Brown13006512012-09-26 18:11:31 -04001218 case 0x3E: /* IVB Xeon */
Len Brown103a8fe2010-10-22 23:53:03 -04001219 return 1;
1220 }
1221 return 0;
1222}
1223
1224double discover_bclk(unsigned int family, unsigned int model)
1225{
1226 if (is_snb(family, model))
1227 return 100.00;
1228 else
1229 return 133.33;
1230}
1231
1232void check_cpuid()
1233{
1234 unsigned int eax, ebx, ecx, edx, max_level;
1235 unsigned int fms, family, model, stepping;
1236
1237 eax = ebx = ecx = edx = 0;
1238
1239 asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0));
1240
1241 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
1242 genuine_intel = 1;
1243
1244 if (verbose)
1245 fprintf(stderr, "%.4s%.4s%.4s ",
1246 (char *)&ebx, (char *)&edx, (char *)&ecx);
1247
1248 asm("cpuid" : "=a" (fms), "=c" (ecx), "=d" (edx) : "a" (1) : "ebx");
1249 family = (fms >> 8) & 0xf;
1250 model = (fms >> 4) & 0xf;
1251 stepping = fms & 0xf;
1252 if (family == 6 || family == 0xf)
1253 model += ((fms >> 16) & 0xf) << 4;
1254
1255 if (verbose)
1256 fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
1257 max_level, family, model, stepping, family, model, stepping);
1258
1259 if (!(edx & (1 << 5))) {
1260 fprintf(stderr, "CPUID: no MSR\n");
1261 exit(1);
1262 }
1263
1264 /*
1265 * check max extended function levels of CPUID.
1266 * This is needed to check for invariant TSC.
1267 * This check is valid for both Intel and AMD.
1268 */
1269 ebx = ecx = edx = 0;
1270 asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000000));
1271
1272 if (max_level < 0x80000007) {
1273 fprintf(stderr, "CPUID: no invariant TSC (max_level 0x%x)\n", max_level);
1274 exit(1);
1275 }
1276
1277 /*
1278 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
1279 * this check is valid for both Intel and AMD
1280 */
1281 asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000007));
Thomas Renninger8209e052011-01-21 15:11:19 +01001282 has_invariant_tsc = edx & (1 << 8);
Len Brown103a8fe2010-10-22 23:53:03 -04001283
1284 if (!has_invariant_tsc) {
1285 fprintf(stderr, "No invariant TSC\n");
1286 exit(1);
1287 }
1288
1289 /*
1290 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
1291 * this check is valid for both Intel and AMD
1292 */
1293
1294 asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x6));
Thomas Renninger8209e052011-01-21 15:11:19 +01001295 has_aperf = ecx & (1 << 0);
Len Brown103a8fe2010-10-22 23:53:03 -04001296 if (!has_aperf) {
1297 fprintf(stderr, "No APERF MSR\n");
1298 exit(1);
1299 }
1300
1301 do_nehalem_platform_info = genuine_intel && has_invariant_tsc;
1302 do_nhm_cstates = genuine_intel; /* all Intel w/ non-stop TSC have NHM counters */
1303 do_snb_cstates = is_snb(family, model);
1304 bclk = discover_bclk(family, model);
1305
1306 do_nehalem_turbo_ratio_limit = has_nehalem_turbo_ratio_limit(family, model);
Len Brown6574a5d2012-09-21 00:01:31 -04001307 do_ivt_turbo_ratio_limit = has_ivt_turbo_ratio_limit(family, model);
Len Brown103a8fe2010-10-22 23:53:03 -04001308}
1309
1310
1311void usage()
1312{
Len Brownf9240812012-10-06 15:26:31 -04001313 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 -04001314 progname);
1315 exit(1);
1316}
1317
1318
1319/*
1320 * in /dev/cpu/ return success for names that are numbers
1321 * ie. filter out ".", "..", "microcode".
1322 */
1323int dir_filter(const struct dirent *dirp)
1324{
1325 if (isdigit(dirp->d_name[0]))
1326 return 1;
1327 else
1328 return 0;
1329}
1330
1331int open_dev_cpu_msr(int dummy1)
1332{
1333 return 0;
1334}
1335
Len Brownc98d5d92012-06-04 00:56:40 -04001336void topology_probe()
1337{
1338 int i;
1339 int max_core_id = 0;
1340 int max_package_id = 0;
1341 int max_siblings = 0;
1342 struct cpu_topology {
1343 int core_id;
1344 int physical_package_id;
1345 } *cpus;
1346
1347 /* Initialize num_cpus, max_cpu_num */
1348 topo.num_cpus = 0;
1349 topo.max_cpu_num = 0;
1350 for_all_proc_cpus(count_cpus);
1351 if (!summary_only && topo.num_cpus > 1)
1352 show_cpu = 1;
1353
1354 if (verbose > 1)
1355 fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
1356
1357 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
1358 if (cpus == NULL) {
1359 perror("calloc cpus");
1360 exit(1);
1361 }
1362
1363 /*
1364 * Allocate and initialize cpu_present_set
1365 */
1366 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
1367 if (cpu_present_set == NULL) {
1368 perror("CPU_ALLOC");
1369 exit(3);
1370 }
1371 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
1372 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
1373 for_all_proc_cpus(mark_cpu_present);
1374
1375 /*
1376 * Allocate and initialize cpu_affinity_set
1377 */
1378 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
1379 if (cpu_affinity_set == NULL) {
1380 perror("CPU_ALLOC");
1381 exit(3);
1382 }
1383 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
1384 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
1385
1386
1387 /*
1388 * For online cpus
1389 * find max_core_id, max_package_id
1390 */
1391 for (i = 0; i <= topo.max_cpu_num; ++i) {
1392 int siblings;
1393
1394 if (cpu_is_not_present(i)) {
1395 if (verbose > 1)
1396 fprintf(stderr, "cpu%d NOT PRESENT\n", i);
1397 continue;
1398 }
1399 cpus[i].core_id = get_core_id(i);
1400 if (cpus[i].core_id > max_core_id)
1401 max_core_id = cpus[i].core_id;
1402
1403 cpus[i].physical_package_id = get_physical_package_id(i);
1404 if (cpus[i].physical_package_id > max_package_id)
1405 max_package_id = cpus[i].physical_package_id;
1406
1407 siblings = get_num_ht_siblings(i);
1408 if (siblings > max_siblings)
1409 max_siblings = siblings;
1410 if (verbose > 1)
1411 fprintf(stderr, "cpu %d pkg %d core %d\n",
1412 i, cpus[i].physical_package_id, cpus[i].core_id);
1413 }
1414 topo.num_cores_per_pkg = max_core_id + 1;
1415 if (verbose > 1)
1416 fprintf(stderr, "max_core_id %d, sizing for %d cores per package\n",
1417 max_core_id, topo.num_cores_per_pkg);
1418 if (!summary_only && topo.num_cores_per_pkg > 1)
1419 show_core = 1;
1420
1421 topo.num_packages = max_package_id + 1;
1422 if (verbose > 1)
1423 fprintf(stderr, "max_package_id %d, sizing for %d packages\n",
1424 max_package_id, topo.num_packages);
1425 if (!summary_only && topo.num_packages > 1)
1426 show_pkg = 1;
1427
1428 topo.num_threads_per_core = max_siblings;
1429 if (verbose > 1)
1430 fprintf(stderr, "max_siblings %d\n", max_siblings);
1431
1432 free(cpus);
1433}
1434
1435void
1436allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
1437{
1438 int i;
1439
1440 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
1441 topo.num_packages, sizeof(struct thread_data));
1442 if (*t == NULL)
1443 goto error;
1444
1445 for (i = 0; i < topo.num_threads_per_core *
1446 topo.num_cores_per_pkg * topo.num_packages; i++)
1447 (*t)[i].cpu_id = -1;
1448
1449 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
1450 sizeof(struct core_data));
1451 if (*c == NULL)
1452 goto error;
1453
1454 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
1455 (*c)[i].core_id = -1;
1456
1457 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
1458 if (*p == NULL)
1459 goto error;
1460
1461 for (i = 0; i < topo.num_packages; i++)
1462 (*p)[i].package_id = i;
1463
1464 return;
1465error:
1466 perror("calloc counters");
1467 exit(1);
1468}
1469/*
1470 * init_counter()
1471 *
1472 * set cpu_id, core_num, pkg_num
1473 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
1474 *
1475 * increment topo.num_cores when 1st core in pkg seen
1476 */
1477void init_counter(struct thread_data *thread_base, struct core_data *core_base,
1478 struct pkg_data *pkg_base, int thread_num, int core_num,
1479 int pkg_num, int cpu_id)
1480{
1481 struct thread_data *t;
1482 struct core_data *c;
1483 struct pkg_data *p;
1484
1485 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
1486 c = GET_CORE(core_base, core_num, pkg_num);
1487 p = GET_PKG(pkg_base, pkg_num);
1488
1489 t->cpu_id = cpu_id;
1490 if (thread_num == 0) {
1491 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
1492 if (cpu_is_first_core_in_package(cpu_id))
1493 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
1494 }
1495
1496 c->core_id = core_num;
1497 p->package_id = pkg_num;
1498}
1499
1500
1501int initialize_counters(int cpu_id)
1502{
1503 int my_thread_id, my_core_id, my_package_id;
1504
1505 my_package_id = get_physical_package_id(cpu_id);
1506 my_core_id = get_core_id(cpu_id);
1507
1508 if (cpu_is_first_sibling_in_core(cpu_id)) {
1509 my_thread_id = 0;
1510 topo.num_cores++;
1511 } else {
1512 my_thread_id = 1;
1513 }
1514
1515 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
1516 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
1517 return 0;
1518}
1519
1520void allocate_output_buffer()
1521{
1522 output_buffer = calloc(1, (1 + topo.num_cpus) * 128);
1523 outp = output_buffer;
1524 if (outp == NULL) {
1525 perror("calloc");
1526 exit(-1);
1527 }
1528}
1529
1530void setup_all_buffers(void)
1531{
1532 topology_probe();
1533 allocate_counters(&thread_even, &core_even, &package_even);
1534 allocate_counters(&thread_odd, &core_odd, &package_odd);
1535 allocate_output_buffer();
1536 for_all_proc_cpus(initialize_counters);
1537}
Len Brown103a8fe2010-10-22 23:53:03 -04001538void turbostat_init()
1539{
1540 check_cpuid();
1541
1542 check_dev_msr();
1543 check_super_user();
1544
Len Brownc98d5d92012-06-04 00:56:40 -04001545 setup_all_buffers();
Len Brown103a8fe2010-10-22 23:53:03 -04001546
1547 if (verbose)
Len Brownc98d5d92012-06-04 00:56:40 -04001548 print_verbose_header();
Len Brown103a8fe2010-10-22 23:53:03 -04001549}
1550
1551int fork_it(char **argv)
1552{
Len Brown103a8fe2010-10-22 23:53:03 -04001553 pid_t child_pid;
Len Brownd91bb172012-11-01 00:08:19 -04001554 int status;
Len Brownd15cf7c2012-06-03 23:24:00 -04001555
Len Brownd91bb172012-11-01 00:08:19 -04001556 status = for_all_cpus(get_counters, EVEN_COUNTERS);
1557 if (status)
1558 exit(status);
Len Brownc98d5d92012-06-04 00:56:40 -04001559 /* clear affinity side-effect of get_counters() */
1560 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
Len Brown103a8fe2010-10-22 23:53:03 -04001561 gettimeofday(&tv_even, (struct timezone *)NULL);
1562
1563 child_pid = fork();
1564 if (!child_pid) {
1565 /* child */
1566 execvp(argv[0], argv);
1567 } else {
Len Brown103a8fe2010-10-22 23:53:03 -04001568
1569 /* parent */
1570 if (child_pid == -1) {
1571 perror("fork");
1572 exit(1);
1573 }
1574
1575 signal(SIGINT, SIG_IGN);
1576 signal(SIGQUIT, SIG_IGN);
1577 if (waitpid(child_pid, &status, 0) == -1) {
1578 perror("wait");
Len Brownd91bb172012-11-01 00:08:19 -04001579 exit(status);
Len Brown103a8fe2010-10-22 23:53:03 -04001580 }
1581 }
Len Brownc98d5d92012-06-04 00:56:40 -04001582 /*
1583 * n.b. fork_it() does not check for errors from for_all_cpus()
1584 * because re-starting is problematic when forking
1585 */
1586 for_all_cpus(get_counters, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04001587 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04001588 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04001589 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
1590 compute_average(EVEN_COUNTERS);
1591 format_all_counters(EVEN_COUNTERS);
1592 flush_stderr();
Len Brown103a8fe2010-10-22 23:53:03 -04001593
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001594 fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
Len Brown103a8fe2010-10-22 23:53:03 -04001595
Len Brownd91bb172012-11-01 00:08:19 -04001596 return status;
Len Brown103a8fe2010-10-22 23:53:03 -04001597}
1598
1599void cmdline(int argc, char **argv)
1600{
1601 int opt;
1602
1603 progname = argv[0];
1604
Len Brown39300ff2012-11-01 00:16:34 -04001605 while ((opt = getopt(argc, argv, "+pPSvi:sc:sC:m:M:")) != -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04001606 switch (opt) {
Len Brownf9240812012-10-06 15:26:31 -04001607 case 'p':
Len Brownc98d5d92012-06-04 00:56:40 -04001608 show_core_only++;
1609 break;
Len Brownf9240812012-10-06 15:26:31 -04001610 case 'P':
Len Brownc98d5d92012-06-04 00:56:40 -04001611 show_pkg_only++;
1612 break;
Len Brownf9240812012-10-06 15:26:31 -04001613 case 'S':
Len Browne23da032012-02-06 18:37:16 -05001614 summary_only++;
1615 break;
Len Brown103a8fe2010-10-22 23:53:03 -04001616 case 'v':
1617 verbose++;
1618 break;
1619 case 'i':
1620 interval_sec = atoi(optarg);
1621 break;
Len Brownf9240812012-10-06 15:26:31 -04001622 case 'c':
Len Brown8e180f32012-09-22 01:25:08 -04001623 sscanf(optarg, "%x", &extra_delta_offset32);
1624 break;
Len Brownf9240812012-10-06 15:26:31 -04001625 case 's':
1626 extra_delta_offset32 = 0x34; /* SMI counter */
1627 break;
1628 case 'C':
Len Brown8e180f32012-09-22 01:25:08 -04001629 sscanf(optarg, "%x", &extra_delta_offset64);
1630 break;
Len Brown2f32edf2012-09-21 23:45:46 -04001631 case 'm':
1632 sscanf(optarg, "%x", &extra_msr_offset32);
Len Brown2f32edf2012-09-21 23:45:46 -04001633 break;
1634 case 'M':
1635 sscanf(optarg, "%x", &extra_msr_offset64);
Len Brown103a8fe2010-10-22 23:53:03 -04001636 break;
1637 default:
1638 usage();
1639 }
1640 }
1641}
1642
1643int main(int argc, char **argv)
1644{
1645 cmdline(argc, argv);
1646
1647 if (verbose > 1)
Len Brownf9240812012-10-06 15:26:31 -04001648 fprintf(stderr, "turbostat v2.1 October 6, 2012"
Len Brown103a8fe2010-10-22 23:53:03 -04001649 " - Len Brown <lenb@kernel.org>\n");
Len Brown103a8fe2010-10-22 23:53:03 -04001650
1651 turbostat_init();
1652
1653 /*
1654 * if any params left, it must be a command to fork
1655 */
1656 if (argc - optind)
1657 return fork_it(argv + optind);
1658 else
1659 turbostat_loop();
1660
1661 return 0;
1662}