blob: 5db4addbe1d91ce31981ebeae3096b9de811a24b [file] [log] [blame]
Len Brown103a8fe2010-10-22 23:53:03 -04001/*
2 * turbostat -- show CPU frequency and C-state residency
3 * on modern Intel turbo-capable processors.
4 *
Len Browne23da032012-02-06 18:37:16 -05005 * Copyright (c) 2012 Intel Corporation.
Len Brown103a8fe2010-10-22 23:53:03 -04006 * Len Brown <len.brown@intel.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
Len Brown88c32812012-03-29 21:44:40 -040022#define _GNU_SOURCE
Len Brown103a8fe2010-10-22 23:53:03 -040023#include <stdio.h>
24#include <unistd.h>
25#include <sys/types.h>
26#include <sys/wait.h>
27#include <sys/stat.h>
28#include <sys/resource.h>
29#include <fcntl.h>
30#include <signal.h>
31#include <sys/time.h>
32#include <stdlib.h>
33#include <dirent.h>
34#include <string.h>
35#include <ctype.h>
Len Brown88c32812012-03-29 21:44:40 -040036#include <sched.h>
Len Brown103a8fe2010-10-22 23:53:03 -040037
Len Brown103a8fe2010-10-22 23:53:03 -040038#define MSR_NEHALEM_PLATFORM_INFO 0xCE
39#define MSR_NEHALEM_TURBO_RATIO_LIMIT 0x1AD
Len Brown6574a5d2012-09-21 00:01:31 -040040#define MSR_IVT_TURBO_RATIO_LIMIT 0x1AE
Len Brown103a8fe2010-10-22 23:53:03 -040041#define MSR_APERF 0xE8
42#define MSR_MPERF 0xE7
43#define MSR_PKG_C2_RESIDENCY 0x60D /* SNB only */
44#define MSR_PKG_C3_RESIDENCY 0x3F8
45#define MSR_PKG_C6_RESIDENCY 0x3F9
46#define MSR_PKG_C7_RESIDENCY 0x3FA /* SNB only */
47#define MSR_CORE_C3_RESIDENCY 0x3FC
48#define MSR_CORE_C6_RESIDENCY 0x3FD
49#define MSR_CORE_C7_RESIDENCY 0x3FE /* SNB only */
50
51char *proc_stat = "/proc/stat";
52unsigned int interval_sec = 5; /* set with -i interval_sec */
53unsigned int verbose; /* set with -v */
Len Browne23da032012-02-06 18:37:16 -050054unsigned int summary_only; /* set with -s */
Len Brown103a8fe2010-10-22 23:53:03 -040055unsigned int skip_c0;
56unsigned int skip_c1;
57unsigned int do_nhm_cstates;
58unsigned int do_snb_cstates;
59unsigned int has_aperf;
60unsigned int units = 1000000000; /* Ghz etc */
61unsigned int genuine_intel;
62unsigned int has_invariant_tsc;
63unsigned int do_nehalem_platform_info;
64unsigned int do_nehalem_turbo_ratio_limit;
Len Brown6574a5d2012-09-21 00:01:31 -040065unsigned int do_ivt_turbo_ratio_limit;
Len Brown103a8fe2010-10-22 23:53:03 -040066unsigned int extra_msr_offset;
67double bclk;
68unsigned int show_pkg;
69unsigned int show_core;
70unsigned int show_cpu;
Len Brownc98d5d92012-06-04 00:56:40 -040071unsigned int show_pkg_only;
72unsigned int show_core_only;
73char *output_buffer, *outp;
Len Brown103a8fe2010-10-22 23:53:03 -040074
75int aperf_mperf_unstable;
76int backwards_count;
77char *progname;
Len Brown103a8fe2010-10-22 23:53:03 -040078
Len Brownc98d5d92012-06-04 00:56:40 -040079cpu_set_t *cpu_present_set, *cpu_affinity_set;
80size_t cpu_present_setsize, cpu_affinity_setsize;
Len Brown103a8fe2010-10-22 23:53:03 -040081
Len Brownc98d5d92012-06-04 00:56:40 -040082struct thread_data {
83 unsigned long long tsc;
84 unsigned long long aperf;
85 unsigned long long mperf;
86 unsigned long long c1; /* derived */
87 unsigned long long extra_msr;
88 unsigned int cpu_id;
89 unsigned int flags;
90#define CPU_IS_FIRST_THREAD_IN_CORE 0x2
91#define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
92} *thread_even, *thread_odd;
Len Brown103a8fe2010-10-22 23:53:03 -040093
Len Brownc98d5d92012-06-04 00:56:40 -040094struct core_data {
95 unsigned long long c3;
96 unsigned long long c6;
97 unsigned long long c7;
98 unsigned int core_id;
99} *core_even, *core_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400100
Len Brownc98d5d92012-06-04 00:56:40 -0400101struct pkg_data {
102 unsigned long long pc2;
103 unsigned long long pc3;
104 unsigned long long pc6;
105 unsigned long long pc7;
106 unsigned int package_id;
107} *package_even, *package_odd;
108
109#define ODD_COUNTERS thread_odd, core_odd, package_odd
110#define EVEN_COUNTERS thread_even, core_even, package_even
111
112#define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
113 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
114 topo.num_threads_per_core + \
115 (core_no) * topo.num_threads_per_core + (thread_no))
116#define GET_CORE(core_base, core_no, pkg_no) \
117 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
118#define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
119
120struct system_summary {
121 struct thread_data threads;
122 struct core_data cores;
123 struct pkg_data packages;
124} sum, average;
125
126
127struct topo_params {
128 int num_packages;
129 int num_cpus;
130 int num_cores;
131 int max_cpu_num;
132 int num_cores_per_pkg;
133 int num_threads_per_core;
134} topo;
135
136struct timeval tv_even, tv_odd, tv_delta;
137
138void setup_all_buffers(void);
139
140int cpu_is_not_present(int cpu)
Len Brownd15cf7c2012-06-03 23:24:00 -0400141{
Len Brownc98d5d92012-06-04 00:56:40 -0400142 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brownd15cf7c2012-06-03 23:24:00 -0400143}
Len Brown88c32812012-03-29 21:44:40 -0400144/*
Len Brownc98d5d92012-06-04 00:56:40 -0400145 * run func(thread, core, package) in topology order
146 * skip non-present cpus
Len Brown88c32812012-03-29 21:44:40 -0400147 */
Len Brownd15cf7c2012-06-03 23:24:00 -0400148
Len Brownc98d5d92012-06-04 00:56:40 -0400149int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
150 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
Len Brown88c32812012-03-29 21:44:40 -0400151{
Len Brownc98d5d92012-06-04 00:56:40 -0400152 int retval, pkg_no, core_no, thread_no;
153
154 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
155 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
156 for (thread_no = 0; thread_no <
157 topo.num_threads_per_core; ++thread_no) {
158 struct thread_data *t;
159 struct core_data *c;
160 struct pkg_data *p;
161
162 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
163
164 if (cpu_is_not_present(t->cpu_id))
165 continue;
166
167 c = GET_CORE(core_base, core_no, pkg_no);
168 p = GET_PKG(pkg_base, pkg_no);
169
170 retval = func(t, c, p);
171 if (retval)
172 return retval;
173 }
174 }
175 }
176 return 0;
Len Brown88c32812012-03-29 21:44:40 -0400177}
178
179int cpu_migrate(int cpu)
180{
Len Brownc98d5d92012-06-04 00:56:40 -0400181 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
182 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
183 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
Len Brown88c32812012-03-29 21:44:40 -0400184 return -1;
185 else
186 return 0;
187}
188
Len Brown15aaa342012-03-29 22:19:58 -0400189int get_msr(int cpu, off_t offset, unsigned long long *msr)
Len Brown103a8fe2010-10-22 23:53:03 -0400190{
191 ssize_t retval;
Len Brown103a8fe2010-10-22 23:53:03 -0400192 char pathname[32];
193 int fd;
194
195 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
196 fd = open(pathname, O_RDONLY);
Len Brown15aaa342012-03-29 22:19:58 -0400197 if (fd < 0)
198 return -1;
Len Brown103a8fe2010-10-22 23:53:03 -0400199
Len Brown15aaa342012-03-29 22:19:58 -0400200 retval = pread(fd, msr, sizeof *msr, offset);
Len Brown103a8fe2010-10-22 23:53:03 -0400201 close(fd);
Len Brown15aaa342012-03-29 22:19:58 -0400202
203 if (retval != sizeof *msr)
204 return -1;
205
206 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400207}
208
Len Browna829eb42011-02-10 23:36:34 -0500209void print_header(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400210{
211 if (show_pkg)
Len Brownc98d5d92012-06-04 00:56:40 -0400212 outp += sprintf(outp, "pk");
Len Browne23da032012-02-06 18:37:16 -0500213 if (show_pkg)
Len Brownc98d5d92012-06-04 00:56:40 -0400214 outp += sprintf(outp, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400215 if (show_core)
Len Brownc98d5d92012-06-04 00:56:40 -0400216 outp += sprintf(outp, "cor");
Len Brown103a8fe2010-10-22 23:53:03 -0400217 if (show_cpu)
Len Brownc98d5d92012-06-04 00:56:40 -0400218 outp += sprintf(outp, " CPU");
Len Browne23da032012-02-06 18:37:16 -0500219 if (show_pkg || show_core || show_cpu)
Len Brownc98d5d92012-06-04 00:56:40 -0400220 outp += sprintf(outp, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400221 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400222 outp += sprintf(outp, " %%c0");
Len Brown103a8fe2010-10-22 23:53:03 -0400223 if (has_aperf)
Len Brownc98d5d92012-06-04 00:56:40 -0400224 outp += sprintf(outp, " GHz");
225 outp += sprintf(outp, " TSC");
Len Brown103a8fe2010-10-22 23:53:03 -0400226 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400227 outp += sprintf(outp, " %%c1");
Len Brown103a8fe2010-10-22 23:53:03 -0400228 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400229 outp += sprintf(outp, " %%c3");
Len Brown103a8fe2010-10-22 23:53:03 -0400230 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400231 outp += sprintf(outp, " %%c6");
Len Brown103a8fe2010-10-22 23:53:03 -0400232 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400233 outp += sprintf(outp, " %%c7");
Len Brown103a8fe2010-10-22 23:53:03 -0400234 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400235 outp += sprintf(outp, " %%pc2");
Len Brown103a8fe2010-10-22 23:53:03 -0400236 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400237 outp += sprintf(outp, " %%pc3");
Len Brown103a8fe2010-10-22 23:53:03 -0400238 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400239 outp += sprintf(outp, " %%pc6");
Len Brown103a8fe2010-10-22 23:53:03 -0400240 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400241 outp += sprintf(outp, " %%pc7");
Len Brown103a8fe2010-10-22 23:53:03 -0400242 if (extra_msr_offset)
Len Brownc98d5d92012-06-04 00:56:40 -0400243 outp += sprintf(outp, " MSR 0x%x ", extra_msr_offset);
Len Brown103a8fe2010-10-22 23:53:03 -0400244
Len Brownc98d5d92012-06-04 00:56:40 -0400245 outp += sprintf(outp, "\n");
Len Brown103a8fe2010-10-22 23:53:03 -0400246}
247
Len Brownc98d5d92012-06-04 00:56:40 -0400248int dump_counters(struct thread_data *t, struct core_data *c,
249 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400250{
Len Brownc98d5d92012-06-04 00:56:40 -0400251 fprintf(stderr, "t %p, c %p, p %p\n", t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400252
Len Brownc98d5d92012-06-04 00:56:40 -0400253 if (t) {
254 fprintf(stderr, "CPU: %d flags 0x%x\n", t->cpu_id, t->flags);
255 fprintf(stderr, "TSC: %016llX\n", t->tsc);
256 fprintf(stderr, "aperf: %016llX\n", t->aperf);
257 fprintf(stderr, "mperf: %016llX\n", t->mperf);
258 fprintf(stderr, "c1: %016llX\n", t->c1);
259 fprintf(stderr, "msr0x%x: %016llX\n",
260 extra_msr_offset, t->extra_msr);
261 }
Len Brown103a8fe2010-10-22 23:53:03 -0400262
Len Brownc98d5d92012-06-04 00:56:40 -0400263 if (c) {
264 fprintf(stderr, "core: %d\n", c->core_id);
265 fprintf(stderr, "c3: %016llX\n", c->c3);
266 fprintf(stderr, "c6: %016llX\n", c->c6);
267 fprintf(stderr, "c7: %016llX\n", c->c7);
268 }
269
270 if (p) {
271 fprintf(stderr, "package: %d\n", p->package_id);
272 fprintf(stderr, "pc2: %016llX\n", p->pc2);
273 fprintf(stderr, "pc3: %016llX\n", p->pc3);
274 fprintf(stderr, "pc6: %016llX\n", p->pc6);
275 fprintf(stderr, "pc7: %016llX\n", p->pc7);
276 }
277 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400278}
279
Len Browne23da032012-02-06 18:37:16 -0500280/*
281 * column formatting convention & formats
282 * package: "pk" 2 columns %2d
283 * core: "cor" 3 columns %3d
284 * CPU: "CPU" 3 columns %3d
285 * GHz: "GHz" 3 columns %3.2
286 * TSC: "TSC" 3 columns %3.2
287 * percentage " %pc3" %6.2
288 */
Len Brownc98d5d92012-06-04 00:56:40 -0400289int format_counters(struct thread_data *t, struct core_data *c,
290 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400291{
292 double interval_float;
293
Len Brownc98d5d92012-06-04 00:56:40 -0400294 /* if showing only 1st thread in core and this isn't one, bail out */
295 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
296 return 0;
297
298 /* if showing only 1st thread in pkg and this isn't one, bail out */
299 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
300 return 0;
301
Len Brown103a8fe2010-10-22 23:53:03 -0400302 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
303
Len Brownc98d5d92012-06-04 00:56:40 -0400304 /* topo columns, print blanks on 1st (average) line */
305 if (t == &average.threads) {
Len Brown103a8fe2010-10-22 23:53:03 -0400306 if (show_pkg)
Len Brownc98d5d92012-06-04 00:56:40 -0400307 outp += sprintf(outp, " ");
Len Browne23da032012-02-06 18:37:16 -0500308 if (show_pkg && show_core)
Len Brownc98d5d92012-06-04 00:56:40 -0400309 outp += sprintf(outp, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400310 if (show_core)
Len Brownc98d5d92012-06-04 00:56:40 -0400311 outp += sprintf(outp, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400312 if (show_cpu)
Len Brownc98d5d92012-06-04 00:56:40 -0400313 outp += sprintf(outp, " " " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400314 } else {
Len Brownc98d5d92012-06-04 00:56:40 -0400315 if (show_pkg) {
316 if (p)
317 outp += sprintf(outp, "%2d", p->package_id);
318 else
319 outp += sprintf(outp, " ");
320 }
Len Browne23da032012-02-06 18:37:16 -0500321 if (show_pkg && show_core)
Len Brownc98d5d92012-06-04 00:56:40 -0400322 outp += sprintf(outp, " ");
323 if (show_core) {
324 if (c)
325 outp += sprintf(outp, "%3d", c->core_id);
326 else
327 outp += sprintf(outp, " ");
328 }
Len Brown103a8fe2010-10-22 23:53:03 -0400329 if (show_cpu)
Len Brownc98d5d92012-06-04 00:56:40 -0400330 outp += sprintf(outp, " %3d", t->cpu_id);
Len Brown103a8fe2010-10-22 23:53:03 -0400331 }
332
333 /* %c0 */
334 if (do_nhm_cstates) {
Len Browne23da032012-02-06 18:37:16 -0500335 if (show_pkg || show_core || show_cpu)
Len Brownc98d5d92012-06-04 00:56:40 -0400336 outp += sprintf(outp, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400337 if (!skip_c0)
Len Brownc98d5d92012-06-04 00:56:40 -0400338 outp += sprintf(outp, "%6.2f", 100.0 * t->mperf/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400339 else
Len Brownc98d5d92012-06-04 00:56:40 -0400340 outp += sprintf(outp, " ****");
Len Brown103a8fe2010-10-22 23:53:03 -0400341 }
342
343 /* GHz */
344 if (has_aperf) {
345 if (!aperf_mperf_unstable) {
Len Brownc98d5d92012-06-04 00:56:40 -0400346 outp += sprintf(outp, " %3.2f",
347 1.0 * t->tsc / units * t->aperf /
348 t->mperf / interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400349 } else {
Len Brownc98d5d92012-06-04 00:56:40 -0400350 if (t->aperf > t->tsc || t->mperf > t->tsc) {
351 outp += sprintf(outp, " ***");
Len Brown103a8fe2010-10-22 23:53:03 -0400352 } else {
Len Brownc98d5d92012-06-04 00:56:40 -0400353 outp += sprintf(outp, "%3.1f*",
354 1.0 * t->tsc /
355 units * t->aperf /
356 t->mperf / interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400357 }
358 }
359 }
360
361 /* TSC */
Len Brownc98d5d92012-06-04 00:56:40 -0400362 outp += sprintf(outp, "%5.2f", 1.0 * t->tsc/units/interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400363
364 if (do_nhm_cstates) {
365 if (!skip_c1)
Len Brownc98d5d92012-06-04 00:56:40 -0400366 outp += sprintf(outp, " %6.2f", 100.0 * t->c1/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400367 else
Len Brownc98d5d92012-06-04 00:56:40 -0400368 outp += sprintf(outp, " ****");
Len Brown103a8fe2010-10-22 23:53:03 -0400369 }
Len Brownc98d5d92012-06-04 00:56:40 -0400370
371 /* print per-core data only for 1st thread in core */
372 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
373 goto done;
374
Len Brown103a8fe2010-10-22 23:53:03 -0400375 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400376 outp += sprintf(outp, " %6.2f", 100.0 * c->c3/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400377 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400378 outp += sprintf(outp, " %6.2f", 100.0 * c->c6/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400379 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400380 outp += sprintf(outp, " %6.2f", 100.0 * c->c7/t->tsc);
381
382 /* print per-package data only for 1st core in package */
383 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
384 goto done;
385
Len Brown103a8fe2010-10-22 23:53:03 -0400386 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400387 outp += sprintf(outp, " %6.2f", 100.0 * p->pc2/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400388 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400389 outp += sprintf(outp, " %6.2f", 100.0 * p->pc3/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400390 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400391 outp += sprintf(outp, " %6.2f", 100.0 * p->pc6/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400392 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400393 outp += sprintf(outp, " %6.2f", 100.0 * p->pc7/t->tsc);
394done:
Len Brown103a8fe2010-10-22 23:53:03 -0400395 if (extra_msr_offset)
Len Brownc98d5d92012-06-04 00:56:40 -0400396 outp += sprintf(outp, " 0x%016llx", t->extra_msr);
397 outp += sprintf(outp, "\n");
398
399 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400400}
401
Len Brownc98d5d92012-06-04 00:56:40 -0400402void flush_stdout()
Len Brown103a8fe2010-10-22 23:53:03 -0400403{
Len Brownc98d5d92012-06-04 00:56:40 -0400404 fputs(output_buffer, stdout);
405 outp = output_buffer;
406}
407void flush_stderr()
408{
409 fputs(output_buffer, stderr);
410 outp = output_buffer;
411}
412void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
413{
Len Browne23da032012-02-06 18:37:16 -0500414 static int printed;
Len Brown103a8fe2010-10-22 23:53:03 -0400415
Len Browne23da032012-02-06 18:37:16 -0500416 if (!printed || !summary_only)
417 print_header();
Len Brown103a8fe2010-10-22 23:53:03 -0400418
Len Brownc98d5d92012-06-04 00:56:40 -0400419 if (topo.num_cpus > 1)
420 format_counters(&average.threads, &average.cores,
421 &average.packages);
Len Brown103a8fe2010-10-22 23:53:03 -0400422
Len Browne23da032012-02-06 18:37:16 -0500423 printed = 1;
424
425 if (summary_only)
426 return;
427
Len Brownc98d5d92012-06-04 00:56:40 -0400428 for_all_cpus(format_counters, t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400429}
430
Len Brownc98d5d92012-06-04 00:56:40 -0400431void
432delta_package(struct pkg_data *new, struct pkg_data *old)
Len Brown103a8fe2010-10-22 23:53:03 -0400433{
Len Brownc98d5d92012-06-04 00:56:40 -0400434 old->pc2 = new->pc2 - old->pc2;
435 old->pc3 = new->pc3 - old->pc3;
436 old->pc6 = new->pc6 - old->pc6;
437 old->pc7 = new->pc7 - old->pc7;
438}
Len Brown103a8fe2010-10-22 23:53:03 -0400439
Len Brownc98d5d92012-06-04 00:56:40 -0400440void
441delta_core(struct core_data *new, struct core_data *old)
442{
443 old->c3 = new->c3 - old->c3;
444 old->c6 = new->c6 - old->c6;
445 old->c7 = new->c7 - old->c7;
446}
Len Brown103a8fe2010-10-22 23:53:03 -0400447
Len Brownc3ae3312012-06-13 21:31:46 -0400448/*
449 * old = new - old
450 */
Len Brownc98d5d92012-06-04 00:56:40 -0400451void
452delta_thread(struct thread_data *new, struct thread_data *old,
453 struct core_data *core_delta)
454{
455 old->tsc = new->tsc - old->tsc;
Len Brown103a8fe2010-10-22 23:53:03 -0400456
Len Brownc98d5d92012-06-04 00:56:40 -0400457 /* check for TSC < 1 Mcycles over interval */
458 if (old->tsc < (1000 * 1000)) {
459 fprintf(stderr, "Insanely slow TSC rate, TSC stops in idle?\n");
460 fprintf(stderr, "You can disable all c-states by booting with \"idle=poll\"\n");
461 fprintf(stderr, "or just the deep ones with \"processor.max_cstate=1\"\n");
462 exit(-3);
463 }
Len Brown103a8fe2010-10-22 23:53:03 -0400464
Len Brownc98d5d92012-06-04 00:56:40 -0400465 old->c1 = new->c1 - old->c1;
Len Brown103a8fe2010-10-22 23:53:03 -0400466
Len Brownc98d5d92012-06-04 00:56:40 -0400467 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
468 old->aperf = new->aperf - old->aperf;
469 old->mperf = new->mperf - old->mperf;
470 } else {
Len Brown103a8fe2010-10-22 23:53:03 -0400471
Len Brownc98d5d92012-06-04 00:56:40 -0400472 if (!aperf_mperf_unstable) {
473 fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname);
474 fprintf(stderr, "* Frequency results do not cover entire interval *\n");
475 fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n");
476
477 aperf_mperf_unstable = 1;
478 }
Len Brown103a8fe2010-10-22 23:53:03 -0400479 /*
Len Brownc98d5d92012-06-04 00:56:40 -0400480 * mperf delta is likely a huge "positive" number
481 * can not use it for calculating c0 time
Len Brown103a8fe2010-10-22 23:53:03 -0400482 */
Len Brownc98d5d92012-06-04 00:56:40 -0400483 skip_c0 = 1;
484 skip_c1 = 1;
485 }
Len Brown103a8fe2010-10-22 23:53:03 -0400486
Len Brown103a8fe2010-10-22 23:53:03 -0400487
Len Brownc98d5d92012-06-04 00:56:40 -0400488 /*
Len Brownc3ae3312012-06-13 21:31:46 -0400489 * As counter collection is not atomic,
490 * it is possible for mperf's non-halted cycles + idle states
Len Brownc98d5d92012-06-04 00:56:40 -0400491 * to exceed TSC's all cycles: show c1 = 0% in that case.
492 */
Len Brownc3ae3312012-06-13 21:31:46 -0400493 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
Len Brownc98d5d92012-06-04 00:56:40 -0400494 old->c1 = 0;
495 else {
496 /* normal case, derive c1 */
497 old->c1 = old->tsc - old->mperf - core_delta->c3
498 - core_delta->c6 - core_delta->c7;
499 }
Len Brownc3ae3312012-06-13 21:31:46 -0400500
Len Brownc98d5d92012-06-04 00:56:40 -0400501 if (old->mperf == 0) {
Len Brownc3ae3312012-06-13 21:31:46 -0400502 if (verbose > 1) fprintf(stderr, "cpu%d MPERF 0!\n", old->cpu_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400503 old->mperf = 1; /* divide by 0 protection */
504 }
505
506 /*
507 * for "extra msr", just copy the latest w/o subtracting
508 */
509 old->extra_msr = new->extra_msr;
510}
511
512int delta_cpu(struct thread_data *t, struct core_data *c,
513 struct pkg_data *p, struct thread_data *t2,
514 struct core_data *c2, struct pkg_data *p2)
515{
516 /* calculate core delta only for 1st thread in core */
517 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
518 delta_core(c, c2);
519
520 /* always calculate thread delta */
521 delta_thread(t, t2, c2); /* c2 is core delta */
522
523 /* calculate package delta only for 1st core in package */
524 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
525 delta_package(p, p2);
526
527 return 0;
528}
529
530void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
531{
532 t->tsc = 0;
533 t->aperf = 0;
534 t->mperf = 0;
535 t->c1 = 0;
536
537 /* tells format_counters to dump all fields from this set */
538 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
539
540 c->c3 = 0;
541 c->c6 = 0;
542 c->c7 = 0;
543
544 p->pc2 = 0;
545 p->pc3 = 0;
546 p->pc6 = 0;
547 p->pc7 = 0;
548}
549int sum_counters(struct thread_data *t, struct core_data *c,
550 struct pkg_data *p)
551{
552 average.threads.tsc += t->tsc;
553 average.threads.aperf += t->aperf;
554 average.threads.mperf += t->mperf;
555 average.threads.c1 += t->c1;
556
557 /* sum per-core values only for 1st thread in core */
558 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
559 return 0;
560
561 average.cores.c3 += c->c3;
562 average.cores.c6 += c->c6;
563 average.cores.c7 += c->c7;
564
565 /* sum per-pkg values only for 1st core in pkg */
566 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
567 return 0;
568
569 average.packages.pc2 += p->pc2;
570 average.packages.pc3 += p->pc3;
571 average.packages.pc6 += p->pc6;
572 average.packages.pc7 += p->pc7;
573
574 return 0;
575}
576/*
577 * sum the counters for all cpus in the system
578 * compute the weighted average
579 */
580void compute_average(struct thread_data *t, struct core_data *c,
581 struct pkg_data *p)
582{
583 clear_counters(&average.threads, &average.cores, &average.packages);
584
585 for_all_cpus(sum_counters, t, c, p);
586
587 average.threads.tsc /= topo.num_cpus;
588 average.threads.aperf /= topo.num_cpus;
589 average.threads.mperf /= topo.num_cpus;
590 average.threads.c1 /= topo.num_cpus;
591
592 average.cores.c3 /= topo.num_cores;
593 average.cores.c6 /= topo.num_cores;
594 average.cores.c7 /= topo.num_cores;
595
596 average.packages.pc2 /= topo.num_packages;
597 average.packages.pc3 /= topo.num_packages;
598 average.packages.pc6 /= topo.num_packages;
599 average.packages.pc7 /= topo.num_packages;
600}
601
602static unsigned long long rdtsc(void)
603{
604 unsigned int low, high;
605
606 asm volatile("rdtsc" : "=a" (low), "=d" (high));
607
608 return low | ((unsigned long long)high) << 32;
609}
610
611
612/*
613 * get_counters(...)
614 * migrate to cpu
615 * acquire and record local counters for that cpu
616 */
617int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
618{
619 int cpu = t->cpu_id;
620
621 if (cpu_migrate(cpu))
622 return -1;
623
624 t->tsc = rdtsc(); /* we are running on local CPU of interest */
625
626 if (has_aperf) {
627 if (get_msr(cpu, MSR_APERF, &t->aperf))
628 return -3;
629 if (get_msr(cpu, MSR_MPERF, &t->mperf))
630 return -4;
631 }
632
633 if (extra_msr_offset)
634 if (get_msr(cpu, extra_msr_offset, &t->extra_msr))
635 return -5;
636
637 /* collect core counters only for 1st thread in core */
638 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
639 return 0;
640
641 if (do_nhm_cstates) {
642 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
643 return -6;
644 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
645 return -7;
646 }
647
648 if (do_snb_cstates)
649 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
650 return -8;
651
652 /* collect package counters only for 1st core in package */
653 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
654 return 0;
655
656 if (do_nhm_cstates) {
657 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
658 return -9;
659 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
660 return -10;
661 }
662 if (do_snb_cstates) {
663 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
664 return -11;
665 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
666 return -12;
Len Brown103a8fe2010-10-22 23:53:03 -0400667 }
668 return 0;
669}
670
Len Brownc98d5d92012-06-04 00:56:40 -0400671void print_verbose_header(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400672{
673 unsigned long long msr;
674 unsigned int ratio;
675
676 if (!do_nehalem_platform_info)
677 return;
678
Len Brown15aaa342012-03-29 22:19:58 -0400679 get_msr(0, MSR_NEHALEM_PLATFORM_INFO, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -0400680
Len Brown6574a5d2012-09-21 00:01:31 -0400681 if (verbose > 1)
682 fprintf(stderr, "MSR_NEHALEM_PLATFORM_INFO: 0x%llx\n", msr);
683
Len Brown103a8fe2010-10-22 23:53:03 -0400684 ratio = (msr >> 40) & 0xFF;
685 fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency\n",
686 ratio, bclk, ratio * bclk);
687
688 ratio = (msr >> 8) & 0xFF;
689 fprintf(stderr, "%d * %.0f = %.0f MHz TSC frequency\n",
690 ratio, bclk, ratio * bclk);
691
Len Brown6574a5d2012-09-21 00:01:31 -0400692 if (!do_ivt_turbo_ratio_limit)
693 goto print_nhm_turbo_ratio_limits;
694
695 get_msr(0, MSR_IVT_TURBO_RATIO_LIMIT, &msr);
696
Len Brown103a8fe2010-10-22 23:53:03 -0400697 if (verbose > 1)
Len Brown6574a5d2012-09-21 00:01:31 -0400698 fprintf(stderr, "MSR_IVT_TURBO_RATIO_LIMIT: 0x%llx\n", msr);
699
700 ratio = (msr >> 56) & 0xFF;
701 if (ratio)
702 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 16 active cores\n",
703 ratio, bclk, ratio * bclk);
704
705 ratio = (msr >> 48) & 0xFF;
706 if (ratio)
707 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 15 active cores\n",
708 ratio, bclk, ratio * bclk);
709
710 ratio = (msr >> 40) & 0xFF;
711 if (ratio)
712 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 14 active cores\n",
713 ratio, bclk, ratio * bclk);
714
715 ratio = (msr >> 32) & 0xFF;
716 if (ratio)
717 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 13 active cores\n",
718 ratio, bclk, ratio * bclk);
719
720 ratio = (msr >> 24) & 0xFF;
721 if (ratio)
722 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 12 active cores\n",
723 ratio, bclk, ratio * bclk);
724
725 ratio = (msr >> 16) & 0xFF;
726 if (ratio)
727 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 11 active cores\n",
728 ratio, bclk, ratio * bclk);
729
730 ratio = (msr >> 8) & 0xFF;
731 if (ratio)
732 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 10 active cores\n",
733 ratio, bclk, ratio * bclk);
734
735 ratio = (msr >> 0) & 0xFF;
736 if (ratio)
737 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 9 active cores\n",
738 ratio, bclk, ratio * bclk);
739
740print_nhm_turbo_ratio_limits:
Len Brown103a8fe2010-10-22 23:53:03 -0400741
742 if (!do_nehalem_turbo_ratio_limit)
743 return;
744
Len Brown15aaa342012-03-29 22:19:58 -0400745 get_msr(0, MSR_NEHALEM_TURBO_RATIO_LIMIT, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -0400746
Len Brown6574a5d2012-09-21 00:01:31 -0400747 if (verbose > 1)
748 fprintf(stderr, "MSR_NEHALEM_TURBO_RATIO_LIMIT: 0x%llx\n", msr);
749
750 ratio = (msr >> 56) & 0xFF;
751 if (ratio)
752 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 8 active cores\n",
753 ratio, bclk, ratio * bclk);
754
755 ratio = (msr >> 48) & 0xFF;
756 if (ratio)
757 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 7 active cores\n",
758 ratio, bclk, ratio * bclk);
759
760 ratio = (msr >> 40) & 0xFF;
761 if (ratio)
762 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 6 active cores\n",
763 ratio, bclk, ratio * bclk);
764
765 ratio = (msr >> 32) & 0xFF;
766 if (ratio)
767 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 5 active cores\n",
768 ratio, bclk, ratio * bclk);
769
Len Brown103a8fe2010-10-22 23:53:03 -0400770 ratio = (msr >> 24) & 0xFF;
771 if (ratio)
772 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
773 ratio, bclk, ratio * bclk);
774
775 ratio = (msr >> 16) & 0xFF;
776 if (ratio)
777 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
778 ratio, bclk, ratio * bclk);
779
780 ratio = (msr >> 8) & 0xFF;
781 if (ratio)
782 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
783 ratio, bclk, ratio * bclk);
784
785 ratio = (msr >> 0) & 0xFF;
786 if (ratio)
787 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
788 ratio, bclk, ratio * bclk);
Len Brown103a8fe2010-10-22 23:53:03 -0400789}
790
Len Brownc98d5d92012-06-04 00:56:40 -0400791void free_all_buffers(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400792{
Len Brownc98d5d92012-06-04 00:56:40 -0400793 CPU_FREE(cpu_present_set);
794 cpu_present_set = NULL;
795 cpu_present_set = 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400796
Len Brownc98d5d92012-06-04 00:56:40 -0400797 CPU_FREE(cpu_affinity_set);
798 cpu_affinity_set = NULL;
799 cpu_affinity_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400800
Len Brownc98d5d92012-06-04 00:56:40 -0400801 free(thread_even);
802 free(core_even);
803 free(package_even);
804
805 thread_even = NULL;
806 core_even = NULL;
807 package_even = NULL;
808
809 free(thread_odd);
810 free(core_odd);
811 free(package_odd);
812
813 thread_odd = NULL;
814 core_odd = NULL;
815 package_odd = NULL;
816
817 free(output_buffer);
818 output_buffer = NULL;
819 outp = NULL;
Len Brown103a8fe2010-10-22 23:53:03 -0400820}
821
Len Brownc98d5d92012-06-04 00:56:40 -0400822/*
823 * cpu_is_first_sibling_in_core(cpu)
824 * return 1 if given CPU is 1st HT sibling in the core
825 */
826int cpu_is_first_sibling_in_core(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -0400827{
Len Brownc98d5d92012-06-04 00:56:40 -0400828 char path[64];
829 FILE *filep;
830 int first_cpu;
Len Brown103a8fe2010-10-22 23:53:03 -0400831
Len Brownc98d5d92012-06-04 00:56:40 -0400832 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
833 filep = fopen(path, "r");
834 if (filep == NULL) {
835 perror(path);
836 exit(1);
837 }
838 fscanf(filep, "%d", &first_cpu);
839 fclose(filep);
840 return (cpu == first_cpu);
Len Brown103a8fe2010-10-22 23:53:03 -0400841}
842
Len Brownc98d5d92012-06-04 00:56:40 -0400843/*
844 * cpu_is_first_core_in_package(cpu)
845 * return 1 if given CPU is 1st core in package
846 */
847int cpu_is_first_core_in_package(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -0400848{
Len Brownc98d5d92012-06-04 00:56:40 -0400849 char path[64];
850 FILE *filep;
851 int first_cpu;
Len Brown103a8fe2010-10-22 23:53:03 -0400852
Len Brownc98d5d92012-06-04 00:56:40 -0400853 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
854 filep = fopen(path, "r");
855 if (filep == NULL) {
856 perror(path);
Len Brown103a8fe2010-10-22 23:53:03 -0400857 exit(1);
858 }
Len Brownc98d5d92012-06-04 00:56:40 -0400859 fscanf(filep, "%d", &first_cpu);
860 fclose(filep);
861 return (cpu == first_cpu);
Len Brown103a8fe2010-10-22 23:53:03 -0400862}
863
864int get_physical_package_id(int cpu)
865{
Len Brownc98d5d92012-06-04 00:56:40 -0400866 char path[80];
Len Brown103a8fe2010-10-22 23:53:03 -0400867 FILE *filep;
868 int pkg;
869
870 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
871 filep = fopen(path, "r");
872 if (filep == NULL) {
873 perror(path);
874 exit(1);
875 }
876 fscanf(filep, "%d", &pkg);
877 fclose(filep);
878 return pkg;
879}
880
881int get_core_id(int cpu)
882{
Len Brownc98d5d92012-06-04 00:56:40 -0400883 char path[80];
Len Brown103a8fe2010-10-22 23:53:03 -0400884 FILE *filep;
885 int core;
886
887 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
888 filep = fopen(path, "r");
889 if (filep == NULL) {
890 perror(path);
891 exit(1);
892 }
893 fscanf(filep, "%d", &core);
894 fclose(filep);
895 return core;
896}
897
Len Brownc98d5d92012-06-04 00:56:40 -0400898int get_num_ht_siblings(int cpu)
899{
900 char path[80];
901 FILE *filep;
902 int sib1, sib2;
903 int matches;
904 char character;
905
906 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
907 filep = fopen(path, "r");
908 if (filep == NULL) {
909 perror(path);
910 exit(1);
911 }
912 /*
913 * file format:
914 * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4)
915 * otherwinse 1 sibling (self).
916 */
917 matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2);
918
919 fclose(filep);
920
921 if (matches == 3)
922 return 2;
923 else
924 return 1;
925}
926
Len Brown103a8fe2010-10-22 23:53:03 -0400927/*
Len Brownc98d5d92012-06-04 00:56:40 -0400928 * run func(thread, core, package) in topology order
929 * skip non-present cpus
Len Brown103a8fe2010-10-22 23:53:03 -0400930 */
931
Len Brownc98d5d92012-06-04 00:56:40 -0400932int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
933 struct pkg_data *, struct thread_data *, struct core_data *,
934 struct pkg_data *), struct thread_data *thread_base,
935 struct core_data *core_base, struct pkg_data *pkg_base,
936 struct thread_data *thread_base2, struct core_data *core_base2,
937 struct pkg_data *pkg_base2)
938{
939 int retval, pkg_no, core_no, thread_no;
940
941 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
942 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
943 for (thread_no = 0; thread_no <
944 topo.num_threads_per_core; ++thread_no) {
945 struct thread_data *t, *t2;
946 struct core_data *c, *c2;
947 struct pkg_data *p, *p2;
948
949 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
950
951 if (cpu_is_not_present(t->cpu_id))
952 continue;
953
954 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
955
956 c = GET_CORE(core_base, core_no, pkg_no);
957 c2 = GET_CORE(core_base2, core_no, pkg_no);
958
959 p = GET_PKG(pkg_base, pkg_no);
960 p2 = GET_PKG(pkg_base2, pkg_no);
961
962 retval = func(t, c, p, t2, c2, p2);
963 if (retval)
964 return retval;
965 }
966 }
967 }
968 return 0;
969}
970
971/*
972 * run func(cpu) on every cpu in /proc/stat
973 * return max_cpu number
974 */
975int for_all_proc_cpus(int (func)(int))
Len Brown103a8fe2010-10-22 23:53:03 -0400976{
977 FILE *fp;
Len Brownc98d5d92012-06-04 00:56:40 -0400978 int cpu_num;
Len Brown103a8fe2010-10-22 23:53:03 -0400979 int retval;
980
981 fp = fopen(proc_stat, "r");
982 if (fp == NULL) {
983 perror(proc_stat);
984 exit(1);
985 }
986
987 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
988 if (retval != 0) {
989 perror("/proc/stat format");
990 exit(1);
991 }
992
Len Brownc98d5d92012-06-04 00:56:40 -0400993 while (1) {
994 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 -0400995 if (retval != 1)
996 break;
997
Len Brownc98d5d92012-06-04 00:56:40 -0400998 retval = func(cpu_num);
999 if (retval) {
1000 fclose(fp);
1001 return(retval);
1002 }
Len Brown103a8fe2010-10-22 23:53:03 -04001003 }
1004 fclose(fp);
Len Brownc98d5d92012-06-04 00:56:40 -04001005 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001006}
1007
1008void re_initialize(void)
1009{
Len Brownc98d5d92012-06-04 00:56:40 -04001010 free_all_buffers();
1011 setup_all_buffers();
1012 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04001013}
1014
Len Brownc98d5d92012-06-04 00:56:40 -04001015
Len Brown103a8fe2010-10-22 23:53:03 -04001016/*
Len Brownc98d5d92012-06-04 00:56:40 -04001017 * count_cpus()
1018 * remember the last one seen, it will be the max
Len Brown103a8fe2010-10-22 23:53:03 -04001019 */
Len Brownc98d5d92012-06-04 00:56:40 -04001020int count_cpus(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001021{
Len Brownc98d5d92012-06-04 00:56:40 -04001022 if (topo.max_cpu_num < cpu)
1023 topo.max_cpu_num = cpu;
Len Brown103a8fe2010-10-22 23:53:03 -04001024
Len Brownc98d5d92012-06-04 00:56:40 -04001025 topo.num_cpus += 1;
1026 return 0;
1027}
1028int mark_cpu_present(int cpu)
1029{
1030 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brown15aaa342012-03-29 22:19:58 -04001031 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001032}
1033
1034void turbostat_loop()
1035{
Len Brownc98d5d92012-06-04 00:56:40 -04001036 int retval;
1037
Len Brown103a8fe2010-10-22 23:53:03 -04001038restart:
Len Brownc98d5d92012-06-04 00:56:40 -04001039 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
1040 if (retval) {
1041 re_initialize();
1042 goto restart;
1043 }
Len Brown103a8fe2010-10-22 23:53:03 -04001044 gettimeofday(&tv_even, (struct timezone *)NULL);
1045
1046 while (1) {
Len Brownc98d5d92012-06-04 00:56:40 -04001047 if (for_all_proc_cpus(cpu_is_not_present)) {
Len Brown103a8fe2010-10-22 23:53:03 -04001048 re_initialize();
1049 goto restart;
1050 }
1051 sleep(interval_sec);
Len Brownc98d5d92012-06-04 00:56:40 -04001052 retval = for_all_cpus(get_counters, ODD_COUNTERS);
1053 if (retval) {
Len Brown15aaa342012-03-29 22:19:58 -04001054 re_initialize();
1055 goto restart;
1056 }
Len Brown103a8fe2010-10-22 23:53:03 -04001057 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04001058 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04001059 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
1060 compute_average(EVEN_COUNTERS);
1061 format_all_counters(EVEN_COUNTERS);
1062 flush_stdout();
Len Brown15aaa342012-03-29 22:19:58 -04001063 sleep(interval_sec);
Len Brownc98d5d92012-06-04 00:56:40 -04001064 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
1065 if (retval) {
Len Brown103a8fe2010-10-22 23:53:03 -04001066 re_initialize();
1067 goto restart;
1068 }
Len Brown103a8fe2010-10-22 23:53:03 -04001069 gettimeofday(&tv_even, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04001070 timersub(&tv_even, &tv_odd, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04001071 for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS);
1072 compute_average(ODD_COUNTERS);
1073 format_all_counters(ODD_COUNTERS);
1074 flush_stdout();
Len Brown103a8fe2010-10-22 23:53:03 -04001075 }
1076}
1077
1078void check_dev_msr()
1079{
1080 struct stat sb;
1081
1082 if (stat("/dev/cpu/0/msr", &sb)) {
1083 fprintf(stderr, "no /dev/cpu/0/msr\n");
1084 fprintf(stderr, "Try \"# modprobe msr\"\n");
1085 exit(-5);
1086 }
1087}
1088
1089void check_super_user()
1090{
1091 if (getuid() != 0) {
1092 fprintf(stderr, "must be root\n");
1093 exit(-6);
1094 }
1095}
1096
1097int has_nehalem_turbo_ratio_limit(unsigned int family, unsigned int model)
1098{
1099 if (!genuine_intel)
1100 return 0;
1101
1102 if (family != 6)
1103 return 0;
1104
1105 switch (model) {
1106 case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
1107 case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
1108 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
1109 case 0x25: /* Westmere Client - Clarkdale, Arrandale */
1110 case 0x2C: /* Westmere EP - Gulftown */
1111 case 0x2A: /* SNB */
1112 case 0x2D: /* SNB Xeon */
Len Brown553575f2011-11-18 03:32:01 -05001113 case 0x3A: /* IVB */
Len Brown13006512012-09-26 18:11:31 -04001114 case 0x3E: /* IVB Xeon */
Len Brown103a8fe2010-10-22 23:53:03 -04001115 return 1;
1116 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1117 case 0x2F: /* Westmere-EX Xeon - Eagleton */
1118 default:
1119 return 0;
1120 }
1121}
Len Brown6574a5d2012-09-21 00:01:31 -04001122int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
1123{
1124 if (!genuine_intel)
1125 return 0;
1126
1127 if (family != 6)
1128 return 0;
1129
1130 switch (model) {
1131 case 0x3E: /* IVB Xeon */
1132 return 1;
1133 default:
1134 return 0;
1135 }
1136}
1137
Len Brown103a8fe2010-10-22 23:53:03 -04001138
1139int is_snb(unsigned int family, unsigned int model)
1140{
1141 if (!genuine_intel)
1142 return 0;
1143
1144 switch (model) {
1145 case 0x2A:
1146 case 0x2D:
Len Brown650a37f2012-06-03 23:34:44 -04001147 case 0x3A: /* IVB */
Len Brown13006512012-09-26 18:11:31 -04001148 case 0x3E: /* IVB Xeon */
Len Brown103a8fe2010-10-22 23:53:03 -04001149 return 1;
1150 }
1151 return 0;
1152}
1153
1154double discover_bclk(unsigned int family, unsigned int model)
1155{
1156 if (is_snb(family, model))
1157 return 100.00;
1158 else
1159 return 133.33;
1160}
1161
1162void check_cpuid()
1163{
1164 unsigned int eax, ebx, ecx, edx, max_level;
1165 unsigned int fms, family, model, stepping;
1166
1167 eax = ebx = ecx = edx = 0;
1168
1169 asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0));
1170
1171 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
1172 genuine_intel = 1;
1173
1174 if (verbose)
1175 fprintf(stderr, "%.4s%.4s%.4s ",
1176 (char *)&ebx, (char *)&edx, (char *)&ecx);
1177
1178 asm("cpuid" : "=a" (fms), "=c" (ecx), "=d" (edx) : "a" (1) : "ebx");
1179 family = (fms >> 8) & 0xf;
1180 model = (fms >> 4) & 0xf;
1181 stepping = fms & 0xf;
1182 if (family == 6 || family == 0xf)
1183 model += ((fms >> 16) & 0xf) << 4;
1184
1185 if (verbose)
1186 fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
1187 max_level, family, model, stepping, family, model, stepping);
1188
1189 if (!(edx & (1 << 5))) {
1190 fprintf(stderr, "CPUID: no MSR\n");
1191 exit(1);
1192 }
1193
1194 /*
1195 * check max extended function levels of CPUID.
1196 * This is needed to check for invariant TSC.
1197 * This check is valid for both Intel and AMD.
1198 */
1199 ebx = ecx = edx = 0;
1200 asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000000));
1201
1202 if (max_level < 0x80000007) {
1203 fprintf(stderr, "CPUID: no invariant TSC (max_level 0x%x)\n", max_level);
1204 exit(1);
1205 }
1206
1207 /*
1208 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
1209 * this check is valid for both Intel and AMD
1210 */
1211 asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000007));
Thomas Renninger8209e052011-01-21 15:11:19 +01001212 has_invariant_tsc = edx & (1 << 8);
Len Brown103a8fe2010-10-22 23:53:03 -04001213
1214 if (!has_invariant_tsc) {
1215 fprintf(stderr, "No invariant TSC\n");
1216 exit(1);
1217 }
1218
1219 /*
1220 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
1221 * this check is valid for both Intel and AMD
1222 */
1223
1224 asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x6));
Thomas Renninger8209e052011-01-21 15:11:19 +01001225 has_aperf = ecx & (1 << 0);
Len Brown103a8fe2010-10-22 23:53:03 -04001226 if (!has_aperf) {
1227 fprintf(stderr, "No APERF MSR\n");
1228 exit(1);
1229 }
1230
1231 do_nehalem_platform_info = genuine_intel && has_invariant_tsc;
1232 do_nhm_cstates = genuine_intel; /* all Intel w/ non-stop TSC have NHM counters */
1233 do_snb_cstates = is_snb(family, model);
1234 bclk = discover_bclk(family, model);
1235
1236 do_nehalem_turbo_ratio_limit = has_nehalem_turbo_ratio_limit(family, model);
Len Brown6574a5d2012-09-21 00:01:31 -04001237 do_ivt_turbo_ratio_limit = has_ivt_turbo_ratio_limit(family, model);
Len Brown103a8fe2010-10-22 23:53:03 -04001238}
1239
1240
1241void usage()
1242{
1243 fprintf(stderr, "%s: [-v] [-M MSR#] [-i interval_sec | command ...]\n",
1244 progname);
1245 exit(1);
1246}
1247
1248
1249/*
1250 * in /dev/cpu/ return success for names that are numbers
1251 * ie. filter out ".", "..", "microcode".
1252 */
1253int dir_filter(const struct dirent *dirp)
1254{
1255 if (isdigit(dirp->d_name[0]))
1256 return 1;
1257 else
1258 return 0;
1259}
1260
1261int open_dev_cpu_msr(int dummy1)
1262{
1263 return 0;
1264}
1265
Len Brownc98d5d92012-06-04 00:56:40 -04001266void topology_probe()
1267{
1268 int i;
1269 int max_core_id = 0;
1270 int max_package_id = 0;
1271 int max_siblings = 0;
1272 struct cpu_topology {
1273 int core_id;
1274 int physical_package_id;
1275 } *cpus;
1276
1277 /* Initialize num_cpus, max_cpu_num */
1278 topo.num_cpus = 0;
1279 topo.max_cpu_num = 0;
1280 for_all_proc_cpus(count_cpus);
1281 if (!summary_only && topo.num_cpus > 1)
1282 show_cpu = 1;
1283
1284 if (verbose > 1)
1285 fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
1286
1287 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
1288 if (cpus == NULL) {
1289 perror("calloc cpus");
1290 exit(1);
1291 }
1292
1293 /*
1294 * Allocate and initialize cpu_present_set
1295 */
1296 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
1297 if (cpu_present_set == NULL) {
1298 perror("CPU_ALLOC");
1299 exit(3);
1300 }
1301 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
1302 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
1303 for_all_proc_cpus(mark_cpu_present);
1304
1305 /*
1306 * Allocate and initialize cpu_affinity_set
1307 */
1308 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
1309 if (cpu_affinity_set == NULL) {
1310 perror("CPU_ALLOC");
1311 exit(3);
1312 }
1313 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
1314 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
1315
1316
1317 /*
1318 * For online cpus
1319 * find max_core_id, max_package_id
1320 */
1321 for (i = 0; i <= topo.max_cpu_num; ++i) {
1322 int siblings;
1323
1324 if (cpu_is_not_present(i)) {
1325 if (verbose > 1)
1326 fprintf(stderr, "cpu%d NOT PRESENT\n", i);
1327 continue;
1328 }
1329 cpus[i].core_id = get_core_id(i);
1330 if (cpus[i].core_id > max_core_id)
1331 max_core_id = cpus[i].core_id;
1332
1333 cpus[i].physical_package_id = get_physical_package_id(i);
1334 if (cpus[i].physical_package_id > max_package_id)
1335 max_package_id = cpus[i].physical_package_id;
1336
1337 siblings = get_num_ht_siblings(i);
1338 if (siblings > max_siblings)
1339 max_siblings = siblings;
1340 if (verbose > 1)
1341 fprintf(stderr, "cpu %d pkg %d core %d\n",
1342 i, cpus[i].physical_package_id, cpus[i].core_id);
1343 }
1344 topo.num_cores_per_pkg = max_core_id + 1;
1345 if (verbose > 1)
1346 fprintf(stderr, "max_core_id %d, sizing for %d cores per package\n",
1347 max_core_id, topo.num_cores_per_pkg);
1348 if (!summary_only && topo.num_cores_per_pkg > 1)
1349 show_core = 1;
1350
1351 topo.num_packages = max_package_id + 1;
1352 if (verbose > 1)
1353 fprintf(stderr, "max_package_id %d, sizing for %d packages\n",
1354 max_package_id, topo.num_packages);
1355 if (!summary_only && topo.num_packages > 1)
1356 show_pkg = 1;
1357
1358 topo.num_threads_per_core = max_siblings;
1359 if (verbose > 1)
1360 fprintf(stderr, "max_siblings %d\n", max_siblings);
1361
1362 free(cpus);
1363}
1364
1365void
1366allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
1367{
1368 int i;
1369
1370 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
1371 topo.num_packages, sizeof(struct thread_data));
1372 if (*t == NULL)
1373 goto error;
1374
1375 for (i = 0; i < topo.num_threads_per_core *
1376 topo.num_cores_per_pkg * topo.num_packages; i++)
1377 (*t)[i].cpu_id = -1;
1378
1379 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
1380 sizeof(struct core_data));
1381 if (*c == NULL)
1382 goto error;
1383
1384 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
1385 (*c)[i].core_id = -1;
1386
1387 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
1388 if (*p == NULL)
1389 goto error;
1390
1391 for (i = 0; i < topo.num_packages; i++)
1392 (*p)[i].package_id = i;
1393
1394 return;
1395error:
1396 perror("calloc counters");
1397 exit(1);
1398}
1399/*
1400 * init_counter()
1401 *
1402 * set cpu_id, core_num, pkg_num
1403 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
1404 *
1405 * increment topo.num_cores when 1st core in pkg seen
1406 */
1407void init_counter(struct thread_data *thread_base, struct core_data *core_base,
1408 struct pkg_data *pkg_base, int thread_num, int core_num,
1409 int pkg_num, int cpu_id)
1410{
1411 struct thread_data *t;
1412 struct core_data *c;
1413 struct pkg_data *p;
1414
1415 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
1416 c = GET_CORE(core_base, core_num, pkg_num);
1417 p = GET_PKG(pkg_base, pkg_num);
1418
1419 t->cpu_id = cpu_id;
1420 if (thread_num == 0) {
1421 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
1422 if (cpu_is_first_core_in_package(cpu_id))
1423 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
1424 }
1425
1426 c->core_id = core_num;
1427 p->package_id = pkg_num;
1428}
1429
1430
1431int initialize_counters(int cpu_id)
1432{
1433 int my_thread_id, my_core_id, my_package_id;
1434
1435 my_package_id = get_physical_package_id(cpu_id);
1436 my_core_id = get_core_id(cpu_id);
1437
1438 if (cpu_is_first_sibling_in_core(cpu_id)) {
1439 my_thread_id = 0;
1440 topo.num_cores++;
1441 } else {
1442 my_thread_id = 1;
1443 }
1444
1445 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
1446 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
1447 return 0;
1448}
1449
1450void allocate_output_buffer()
1451{
1452 output_buffer = calloc(1, (1 + topo.num_cpus) * 128);
1453 outp = output_buffer;
1454 if (outp == NULL) {
1455 perror("calloc");
1456 exit(-1);
1457 }
1458}
1459
1460void setup_all_buffers(void)
1461{
1462 topology_probe();
1463 allocate_counters(&thread_even, &core_even, &package_even);
1464 allocate_counters(&thread_odd, &core_odd, &package_odd);
1465 allocate_output_buffer();
1466 for_all_proc_cpus(initialize_counters);
1467}
Len Brown103a8fe2010-10-22 23:53:03 -04001468void turbostat_init()
1469{
1470 check_cpuid();
1471
1472 check_dev_msr();
1473 check_super_user();
1474
Len Brownc98d5d92012-06-04 00:56:40 -04001475 setup_all_buffers();
Len Brown103a8fe2010-10-22 23:53:03 -04001476
1477 if (verbose)
Len Brownc98d5d92012-06-04 00:56:40 -04001478 print_verbose_header();
Len Brown103a8fe2010-10-22 23:53:03 -04001479}
1480
1481int fork_it(char **argv)
1482{
Len Brown103a8fe2010-10-22 23:53:03 -04001483 pid_t child_pid;
Len Brownd15cf7c2012-06-03 23:24:00 -04001484
Len Brownc98d5d92012-06-04 00:56:40 -04001485 for_all_cpus(get_counters, EVEN_COUNTERS);
1486 /* clear affinity side-effect of get_counters() */
1487 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
Len Brown103a8fe2010-10-22 23:53:03 -04001488 gettimeofday(&tv_even, (struct timezone *)NULL);
1489
1490 child_pid = fork();
1491 if (!child_pid) {
1492 /* child */
1493 execvp(argv[0], argv);
1494 } else {
1495 int status;
1496
1497 /* parent */
1498 if (child_pid == -1) {
1499 perror("fork");
1500 exit(1);
1501 }
1502
1503 signal(SIGINT, SIG_IGN);
1504 signal(SIGQUIT, SIG_IGN);
1505 if (waitpid(child_pid, &status, 0) == -1) {
1506 perror("wait");
1507 exit(1);
1508 }
1509 }
Len Brownc98d5d92012-06-04 00:56:40 -04001510 /*
1511 * n.b. fork_it() does not check for errors from for_all_cpus()
1512 * because re-starting is problematic when forking
1513 */
1514 for_all_cpus(get_counters, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04001515 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04001516 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04001517 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
1518 compute_average(EVEN_COUNTERS);
1519 format_all_counters(EVEN_COUNTERS);
1520 flush_stderr();
Len Brown103a8fe2010-10-22 23:53:03 -04001521
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001522 fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
Len Brown103a8fe2010-10-22 23:53:03 -04001523
1524 return 0;
1525}
1526
1527void cmdline(int argc, char **argv)
1528{
1529 int opt;
1530
1531 progname = argv[0];
1532
Len Brownc98d5d92012-06-04 00:56:40 -04001533 while ((opt = getopt(argc, argv, "+cpsvi:M:")) != -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04001534 switch (opt) {
Len Brownc98d5d92012-06-04 00:56:40 -04001535 case 'c':
1536 show_core_only++;
1537 break;
1538 case 'p':
1539 show_pkg_only++;
1540 break;
Len Browne23da032012-02-06 18:37:16 -05001541 case 's':
1542 summary_only++;
1543 break;
Len Brown103a8fe2010-10-22 23:53:03 -04001544 case 'v':
1545 verbose++;
1546 break;
1547 case 'i':
1548 interval_sec = atoi(optarg);
1549 break;
1550 case 'M':
1551 sscanf(optarg, "%x", &extra_msr_offset);
1552 if (verbose > 1)
1553 fprintf(stderr, "MSR 0x%X\n", extra_msr_offset);
1554 break;
1555 default:
1556 usage();
1557 }
1558 }
1559}
1560
1561int main(int argc, char **argv)
1562{
1563 cmdline(argc, argv);
1564
1565 if (verbose > 1)
Len Brownc98d5d92012-06-04 00:56:40 -04001566 fprintf(stderr, "turbostat v2.0 May 16, 2012"
Len Brown103a8fe2010-10-22 23:53:03 -04001567 " - Len Brown <lenb@kernel.org>\n");
Len Brown103a8fe2010-10-22 23:53:03 -04001568
1569 turbostat_init();
1570
1571 /*
1572 * if any params left, it must be a command to fork
1573 */
1574 if (argc - optind)
1575 return fork_it(argv + optind);
1576 else
1577 turbostat_loop();
1578
1579 return 0;
1580}