blob: 5ce88dd8c95a059aa01313109cd9f871574c9d85 [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 Brown130ff302012-09-21 22:56:06 -0400226 if (extra_msr_offset)
227 outp += sprintf(outp, " MSR 0x%04X", extra_msr_offset);
Len Brown103a8fe2010-10-22 23:53:03 -0400228 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400229 outp += sprintf(outp, " %%c1");
Len Brown103a8fe2010-10-22 23:53:03 -0400230 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400231 outp += sprintf(outp, " %%c3");
Len Brown103a8fe2010-10-22 23:53:03 -0400232 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400233 outp += sprintf(outp, " %%c6");
Len Brown103a8fe2010-10-22 23:53:03 -0400234 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400235 outp += sprintf(outp, " %%c7");
Len Brown103a8fe2010-10-22 23:53:03 -0400236 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400237 outp += sprintf(outp, " %%pc2");
Len Brown103a8fe2010-10-22 23:53:03 -0400238 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400239 outp += sprintf(outp, " %%pc3");
Len Brown103a8fe2010-10-22 23:53:03 -0400240 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400241 outp += sprintf(outp, " %%pc6");
Len Brown103a8fe2010-10-22 23:53:03 -0400242 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400243 outp += sprintf(outp, " %%pc7");
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
Len Brown130ff302012-09-21 22:56:06 -0400364 /* MSR */
365 if (extra_msr_offset)
366 outp += sprintf(outp, " 0x%016llx", t->extra_msr);
367
Len Brown103a8fe2010-10-22 23:53:03 -0400368 if (do_nhm_cstates) {
369 if (!skip_c1)
Len Brownc98d5d92012-06-04 00:56:40 -0400370 outp += sprintf(outp, " %6.2f", 100.0 * t->c1/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400371 else
Len Brownc98d5d92012-06-04 00:56:40 -0400372 outp += sprintf(outp, " ****");
Len Brown103a8fe2010-10-22 23:53:03 -0400373 }
Len Brownc98d5d92012-06-04 00:56:40 -0400374
375 /* print per-core data only for 1st thread in core */
376 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
377 goto done;
378
Len Brown103a8fe2010-10-22 23:53:03 -0400379 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400380 outp += sprintf(outp, " %6.2f", 100.0 * c->c3/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400381 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400382 outp += sprintf(outp, " %6.2f", 100.0 * c->c6/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400383 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400384 outp += sprintf(outp, " %6.2f", 100.0 * c->c7/t->tsc);
385
386 /* print per-package data only for 1st core in package */
387 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
388 goto done;
389
Len Brown103a8fe2010-10-22 23:53:03 -0400390 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400391 outp += sprintf(outp, " %6.2f", 100.0 * p->pc2/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400392 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400393 outp += sprintf(outp, " %6.2f", 100.0 * p->pc3/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400394 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400395 outp += sprintf(outp, " %6.2f", 100.0 * p->pc6/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400396 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400397 outp += sprintf(outp, " %6.2f", 100.0 * p->pc7/t->tsc);
398done:
Len Brownc98d5d92012-06-04 00:56:40 -0400399 outp += sprintf(outp, "\n");
400
401 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400402}
403
Len Brownc98d5d92012-06-04 00:56:40 -0400404void flush_stdout()
Len Brown103a8fe2010-10-22 23:53:03 -0400405{
Len Brownc98d5d92012-06-04 00:56:40 -0400406 fputs(output_buffer, stdout);
407 outp = output_buffer;
408}
409void flush_stderr()
410{
411 fputs(output_buffer, stderr);
412 outp = output_buffer;
413}
414void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
415{
Len Browne23da032012-02-06 18:37:16 -0500416 static int printed;
Len Brown103a8fe2010-10-22 23:53:03 -0400417
Len Browne23da032012-02-06 18:37:16 -0500418 if (!printed || !summary_only)
419 print_header();
Len Brown103a8fe2010-10-22 23:53:03 -0400420
Len Brownc98d5d92012-06-04 00:56:40 -0400421 if (topo.num_cpus > 1)
422 format_counters(&average.threads, &average.cores,
423 &average.packages);
Len Brown103a8fe2010-10-22 23:53:03 -0400424
Len Browne23da032012-02-06 18:37:16 -0500425 printed = 1;
426
427 if (summary_only)
428 return;
429
Len Brownc98d5d92012-06-04 00:56:40 -0400430 for_all_cpus(format_counters, t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400431}
432
Len Brownc98d5d92012-06-04 00:56:40 -0400433void
434delta_package(struct pkg_data *new, struct pkg_data *old)
Len Brown103a8fe2010-10-22 23:53:03 -0400435{
Len Brownc98d5d92012-06-04 00:56:40 -0400436 old->pc2 = new->pc2 - old->pc2;
437 old->pc3 = new->pc3 - old->pc3;
438 old->pc6 = new->pc6 - old->pc6;
439 old->pc7 = new->pc7 - old->pc7;
440}
Len Brown103a8fe2010-10-22 23:53:03 -0400441
Len Brownc98d5d92012-06-04 00:56:40 -0400442void
443delta_core(struct core_data *new, struct core_data *old)
444{
445 old->c3 = new->c3 - old->c3;
446 old->c6 = new->c6 - old->c6;
447 old->c7 = new->c7 - old->c7;
448}
Len Brown103a8fe2010-10-22 23:53:03 -0400449
Len Brownc3ae3312012-06-13 21:31:46 -0400450/*
451 * old = new - old
452 */
Len Brownc98d5d92012-06-04 00:56:40 -0400453void
454delta_thread(struct thread_data *new, struct thread_data *old,
455 struct core_data *core_delta)
456{
457 old->tsc = new->tsc - old->tsc;
Len Brown103a8fe2010-10-22 23:53:03 -0400458
Len Brownc98d5d92012-06-04 00:56:40 -0400459 /* check for TSC < 1 Mcycles over interval */
460 if (old->tsc < (1000 * 1000)) {
461 fprintf(stderr, "Insanely slow TSC rate, TSC stops in idle?\n");
462 fprintf(stderr, "You can disable all c-states by booting with \"idle=poll\"\n");
463 fprintf(stderr, "or just the deep ones with \"processor.max_cstate=1\"\n");
464 exit(-3);
465 }
Len Brown103a8fe2010-10-22 23:53:03 -0400466
Len Brownc98d5d92012-06-04 00:56:40 -0400467 old->c1 = new->c1 - old->c1;
Len Brown103a8fe2010-10-22 23:53:03 -0400468
Len Brownc98d5d92012-06-04 00:56:40 -0400469 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
470 old->aperf = new->aperf - old->aperf;
471 old->mperf = new->mperf - old->mperf;
472 } else {
Len Brown103a8fe2010-10-22 23:53:03 -0400473
Len Brownc98d5d92012-06-04 00:56:40 -0400474 if (!aperf_mperf_unstable) {
475 fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname);
476 fprintf(stderr, "* Frequency results do not cover entire interval *\n");
477 fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n");
478
479 aperf_mperf_unstable = 1;
480 }
Len Brown103a8fe2010-10-22 23:53:03 -0400481 /*
Len Brownc98d5d92012-06-04 00:56:40 -0400482 * mperf delta is likely a huge "positive" number
483 * can not use it for calculating c0 time
Len Brown103a8fe2010-10-22 23:53:03 -0400484 */
Len Brownc98d5d92012-06-04 00:56:40 -0400485 skip_c0 = 1;
486 skip_c1 = 1;
487 }
Len Brown103a8fe2010-10-22 23:53:03 -0400488
Len Brown103a8fe2010-10-22 23:53:03 -0400489
Len Brownc98d5d92012-06-04 00:56:40 -0400490 /*
Len Brownc3ae3312012-06-13 21:31:46 -0400491 * As counter collection is not atomic,
492 * it is possible for mperf's non-halted cycles + idle states
Len Brownc98d5d92012-06-04 00:56:40 -0400493 * to exceed TSC's all cycles: show c1 = 0% in that case.
494 */
Len Brownc3ae3312012-06-13 21:31:46 -0400495 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
Len Brownc98d5d92012-06-04 00:56:40 -0400496 old->c1 = 0;
497 else {
498 /* normal case, derive c1 */
499 old->c1 = old->tsc - old->mperf - core_delta->c3
500 - core_delta->c6 - core_delta->c7;
501 }
Len Brownc3ae3312012-06-13 21:31:46 -0400502
Len Brownc98d5d92012-06-04 00:56:40 -0400503 if (old->mperf == 0) {
Len Brownc3ae3312012-06-13 21:31:46 -0400504 if (verbose > 1) fprintf(stderr, "cpu%d MPERF 0!\n", old->cpu_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400505 old->mperf = 1; /* divide by 0 protection */
506 }
507
508 /*
509 * for "extra msr", just copy the latest w/o subtracting
510 */
511 old->extra_msr = new->extra_msr;
512}
513
514int delta_cpu(struct thread_data *t, struct core_data *c,
515 struct pkg_data *p, struct thread_data *t2,
516 struct core_data *c2, struct pkg_data *p2)
517{
518 /* calculate core delta only for 1st thread in core */
519 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
520 delta_core(c, c2);
521
522 /* always calculate thread delta */
523 delta_thread(t, t2, c2); /* c2 is core delta */
524
525 /* calculate package delta only for 1st core in package */
526 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
527 delta_package(p, p2);
528
529 return 0;
530}
531
532void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
533{
534 t->tsc = 0;
535 t->aperf = 0;
536 t->mperf = 0;
537 t->c1 = 0;
538
539 /* tells format_counters to dump all fields from this set */
540 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
541
542 c->c3 = 0;
543 c->c6 = 0;
544 c->c7 = 0;
545
546 p->pc2 = 0;
547 p->pc3 = 0;
548 p->pc6 = 0;
549 p->pc7 = 0;
550}
551int sum_counters(struct thread_data *t, struct core_data *c,
552 struct pkg_data *p)
553{
554 average.threads.tsc += t->tsc;
555 average.threads.aperf += t->aperf;
556 average.threads.mperf += t->mperf;
557 average.threads.c1 += t->c1;
558
559 /* sum per-core values only for 1st thread in core */
560 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
561 return 0;
562
563 average.cores.c3 += c->c3;
564 average.cores.c6 += c->c6;
565 average.cores.c7 += c->c7;
566
567 /* sum per-pkg values only for 1st core in pkg */
568 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
569 return 0;
570
571 average.packages.pc2 += p->pc2;
572 average.packages.pc3 += p->pc3;
573 average.packages.pc6 += p->pc6;
574 average.packages.pc7 += p->pc7;
575
576 return 0;
577}
578/*
579 * sum the counters for all cpus in the system
580 * compute the weighted average
581 */
582void compute_average(struct thread_data *t, struct core_data *c,
583 struct pkg_data *p)
584{
585 clear_counters(&average.threads, &average.cores, &average.packages);
586
587 for_all_cpus(sum_counters, t, c, p);
588
589 average.threads.tsc /= topo.num_cpus;
590 average.threads.aperf /= topo.num_cpus;
591 average.threads.mperf /= topo.num_cpus;
592 average.threads.c1 /= topo.num_cpus;
593
594 average.cores.c3 /= topo.num_cores;
595 average.cores.c6 /= topo.num_cores;
596 average.cores.c7 /= topo.num_cores;
597
598 average.packages.pc2 /= topo.num_packages;
599 average.packages.pc3 /= topo.num_packages;
600 average.packages.pc6 /= topo.num_packages;
601 average.packages.pc7 /= topo.num_packages;
602}
603
604static unsigned long long rdtsc(void)
605{
606 unsigned int low, high;
607
608 asm volatile("rdtsc" : "=a" (low), "=d" (high));
609
610 return low | ((unsigned long long)high) << 32;
611}
612
613
614/*
615 * get_counters(...)
616 * migrate to cpu
617 * acquire and record local counters for that cpu
618 */
619int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
620{
621 int cpu = t->cpu_id;
622
623 if (cpu_migrate(cpu))
624 return -1;
625
626 t->tsc = rdtsc(); /* we are running on local CPU of interest */
627
628 if (has_aperf) {
629 if (get_msr(cpu, MSR_APERF, &t->aperf))
630 return -3;
631 if (get_msr(cpu, MSR_MPERF, &t->mperf))
632 return -4;
633 }
634
635 if (extra_msr_offset)
636 if (get_msr(cpu, extra_msr_offset, &t->extra_msr))
637 return -5;
638
639 /* collect core counters only for 1st thread in core */
640 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
641 return 0;
642
643 if (do_nhm_cstates) {
644 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
645 return -6;
646 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
647 return -7;
648 }
649
650 if (do_snb_cstates)
651 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
652 return -8;
653
654 /* collect package counters only for 1st core in package */
655 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
656 return 0;
657
658 if (do_nhm_cstates) {
659 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
660 return -9;
661 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
662 return -10;
663 }
664 if (do_snb_cstates) {
665 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
666 return -11;
667 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
668 return -12;
Len Brown103a8fe2010-10-22 23:53:03 -0400669 }
670 return 0;
671}
672
Len Brownc98d5d92012-06-04 00:56:40 -0400673void print_verbose_header(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400674{
675 unsigned long long msr;
676 unsigned int ratio;
677
678 if (!do_nehalem_platform_info)
679 return;
680
Len Brown15aaa342012-03-29 22:19:58 -0400681 get_msr(0, MSR_NEHALEM_PLATFORM_INFO, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -0400682
Len Brown6574a5d2012-09-21 00:01:31 -0400683 if (verbose > 1)
684 fprintf(stderr, "MSR_NEHALEM_PLATFORM_INFO: 0x%llx\n", msr);
685
Len Brown103a8fe2010-10-22 23:53:03 -0400686 ratio = (msr >> 40) & 0xFF;
687 fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency\n",
688 ratio, bclk, ratio * bclk);
689
690 ratio = (msr >> 8) & 0xFF;
691 fprintf(stderr, "%d * %.0f = %.0f MHz TSC frequency\n",
692 ratio, bclk, ratio * bclk);
693
Len Brown6574a5d2012-09-21 00:01:31 -0400694 if (!do_ivt_turbo_ratio_limit)
695 goto print_nhm_turbo_ratio_limits;
696
697 get_msr(0, MSR_IVT_TURBO_RATIO_LIMIT, &msr);
698
Len Brown103a8fe2010-10-22 23:53:03 -0400699 if (verbose > 1)
Len Brown6574a5d2012-09-21 00:01:31 -0400700 fprintf(stderr, "MSR_IVT_TURBO_RATIO_LIMIT: 0x%llx\n", msr);
701
702 ratio = (msr >> 56) & 0xFF;
703 if (ratio)
704 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 16 active cores\n",
705 ratio, bclk, ratio * bclk);
706
707 ratio = (msr >> 48) & 0xFF;
708 if (ratio)
709 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 15 active cores\n",
710 ratio, bclk, ratio * bclk);
711
712 ratio = (msr >> 40) & 0xFF;
713 if (ratio)
714 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 14 active cores\n",
715 ratio, bclk, ratio * bclk);
716
717 ratio = (msr >> 32) & 0xFF;
718 if (ratio)
719 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 13 active cores\n",
720 ratio, bclk, ratio * bclk);
721
722 ratio = (msr >> 24) & 0xFF;
723 if (ratio)
724 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 12 active cores\n",
725 ratio, bclk, ratio * bclk);
726
727 ratio = (msr >> 16) & 0xFF;
728 if (ratio)
729 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 11 active cores\n",
730 ratio, bclk, ratio * bclk);
731
732 ratio = (msr >> 8) & 0xFF;
733 if (ratio)
734 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 10 active cores\n",
735 ratio, bclk, ratio * bclk);
736
737 ratio = (msr >> 0) & 0xFF;
738 if (ratio)
739 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 9 active cores\n",
740 ratio, bclk, ratio * bclk);
741
742print_nhm_turbo_ratio_limits:
Len Brown103a8fe2010-10-22 23:53:03 -0400743
744 if (!do_nehalem_turbo_ratio_limit)
745 return;
746
Len Brown15aaa342012-03-29 22:19:58 -0400747 get_msr(0, MSR_NEHALEM_TURBO_RATIO_LIMIT, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -0400748
Len Brown6574a5d2012-09-21 00:01:31 -0400749 if (verbose > 1)
750 fprintf(stderr, "MSR_NEHALEM_TURBO_RATIO_LIMIT: 0x%llx\n", msr);
751
752 ratio = (msr >> 56) & 0xFF;
753 if (ratio)
754 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 8 active cores\n",
755 ratio, bclk, ratio * bclk);
756
757 ratio = (msr >> 48) & 0xFF;
758 if (ratio)
759 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 7 active cores\n",
760 ratio, bclk, ratio * bclk);
761
762 ratio = (msr >> 40) & 0xFF;
763 if (ratio)
764 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 6 active cores\n",
765 ratio, bclk, ratio * bclk);
766
767 ratio = (msr >> 32) & 0xFF;
768 if (ratio)
769 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 5 active cores\n",
770 ratio, bclk, ratio * bclk);
771
Len Brown103a8fe2010-10-22 23:53:03 -0400772 ratio = (msr >> 24) & 0xFF;
773 if (ratio)
774 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
775 ratio, bclk, ratio * bclk);
776
777 ratio = (msr >> 16) & 0xFF;
778 if (ratio)
779 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
780 ratio, bclk, ratio * bclk);
781
782 ratio = (msr >> 8) & 0xFF;
783 if (ratio)
784 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
785 ratio, bclk, ratio * bclk);
786
787 ratio = (msr >> 0) & 0xFF;
788 if (ratio)
789 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
790 ratio, bclk, ratio * bclk);
Len Brown103a8fe2010-10-22 23:53:03 -0400791}
792
Len Brownc98d5d92012-06-04 00:56:40 -0400793void free_all_buffers(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400794{
Len Brownc98d5d92012-06-04 00:56:40 -0400795 CPU_FREE(cpu_present_set);
796 cpu_present_set = NULL;
797 cpu_present_set = 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400798
Len Brownc98d5d92012-06-04 00:56:40 -0400799 CPU_FREE(cpu_affinity_set);
800 cpu_affinity_set = NULL;
801 cpu_affinity_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400802
Len Brownc98d5d92012-06-04 00:56:40 -0400803 free(thread_even);
804 free(core_even);
805 free(package_even);
806
807 thread_even = NULL;
808 core_even = NULL;
809 package_even = NULL;
810
811 free(thread_odd);
812 free(core_odd);
813 free(package_odd);
814
815 thread_odd = NULL;
816 core_odd = NULL;
817 package_odd = NULL;
818
819 free(output_buffer);
820 output_buffer = NULL;
821 outp = NULL;
Len Brown103a8fe2010-10-22 23:53:03 -0400822}
823
Len Brownc98d5d92012-06-04 00:56:40 -0400824/*
825 * cpu_is_first_sibling_in_core(cpu)
826 * return 1 if given CPU is 1st HT sibling in the core
827 */
828int cpu_is_first_sibling_in_core(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -0400829{
Len Brownc98d5d92012-06-04 00:56:40 -0400830 char path[64];
831 FILE *filep;
832 int first_cpu;
Len Brown103a8fe2010-10-22 23:53:03 -0400833
Len Brownc98d5d92012-06-04 00:56:40 -0400834 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
835 filep = fopen(path, "r");
836 if (filep == NULL) {
837 perror(path);
838 exit(1);
839 }
840 fscanf(filep, "%d", &first_cpu);
841 fclose(filep);
842 return (cpu == first_cpu);
Len Brown103a8fe2010-10-22 23:53:03 -0400843}
844
Len Brownc98d5d92012-06-04 00:56:40 -0400845/*
846 * cpu_is_first_core_in_package(cpu)
847 * return 1 if given CPU is 1st core in package
848 */
849int cpu_is_first_core_in_package(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -0400850{
Len Brownc98d5d92012-06-04 00:56:40 -0400851 char path[64];
852 FILE *filep;
853 int first_cpu;
Len Brown103a8fe2010-10-22 23:53:03 -0400854
Len Brownc98d5d92012-06-04 00:56:40 -0400855 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
856 filep = fopen(path, "r");
857 if (filep == NULL) {
858 perror(path);
Len Brown103a8fe2010-10-22 23:53:03 -0400859 exit(1);
860 }
Len Brownc98d5d92012-06-04 00:56:40 -0400861 fscanf(filep, "%d", &first_cpu);
862 fclose(filep);
863 return (cpu == first_cpu);
Len Brown103a8fe2010-10-22 23:53:03 -0400864}
865
866int get_physical_package_id(int cpu)
867{
Len Brownc98d5d92012-06-04 00:56:40 -0400868 char path[80];
Len Brown103a8fe2010-10-22 23:53:03 -0400869 FILE *filep;
870 int pkg;
871
872 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
873 filep = fopen(path, "r");
874 if (filep == NULL) {
875 perror(path);
876 exit(1);
877 }
878 fscanf(filep, "%d", &pkg);
879 fclose(filep);
880 return pkg;
881}
882
883int get_core_id(int cpu)
884{
Len Brownc98d5d92012-06-04 00:56:40 -0400885 char path[80];
Len Brown103a8fe2010-10-22 23:53:03 -0400886 FILE *filep;
887 int core;
888
889 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
890 filep = fopen(path, "r");
891 if (filep == NULL) {
892 perror(path);
893 exit(1);
894 }
895 fscanf(filep, "%d", &core);
896 fclose(filep);
897 return core;
898}
899
Len Brownc98d5d92012-06-04 00:56:40 -0400900int get_num_ht_siblings(int cpu)
901{
902 char path[80];
903 FILE *filep;
904 int sib1, sib2;
905 int matches;
906 char character;
907
908 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
909 filep = fopen(path, "r");
910 if (filep == NULL) {
911 perror(path);
912 exit(1);
913 }
914 /*
915 * file format:
916 * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4)
917 * otherwinse 1 sibling (self).
918 */
919 matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2);
920
921 fclose(filep);
922
923 if (matches == 3)
924 return 2;
925 else
926 return 1;
927}
928
Len Brown103a8fe2010-10-22 23:53:03 -0400929/*
Len Brownc98d5d92012-06-04 00:56:40 -0400930 * run func(thread, core, package) in topology order
931 * skip non-present cpus
Len Brown103a8fe2010-10-22 23:53:03 -0400932 */
933
Len Brownc98d5d92012-06-04 00:56:40 -0400934int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
935 struct pkg_data *, struct thread_data *, struct core_data *,
936 struct pkg_data *), struct thread_data *thread_base,
937 struct core_data *core_base, struct pkg_data *pkg_base,
938 struct thread_data *thread_base2, struct core_data *core_base2,
939 struct pkg_data *pkg_base2)
940{
941 int retval, pkg_no, core_no, thread_no;
942
943 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
944 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
945 for (thread_no = 0; thread_no <
946 topo.num_threads_per_core; ++thread_no) {
947 struct thread_data *t, *t2;
948 struct core_data *c, *c2;
949 struct pkg_data *p, *p2;
950
951 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
952
953 if (cpu_is_not_present(t->cpu_id))
954 continue;
955
956 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
957
958 c = GET_CORE(core_base, core_no, pkg_no);
959 c2 = GET_CORE(core_base2, core_no, pkg_no);
960
961 p = GET_PKG(pkg_base, pkg_no);
962 p2 = GET_PKG(pkg_base2, pkg_no);
963
964 retval = func(t, c, p, t2, c2, p2);
965 if (retval)
966 return retval;
967 }
968 }
969 }
970 return 0;
971}
972
973/*
974 * run func(cpu) on every cpu in /proc/stat
975 * return max_cpu number
976 */
977int for_all_proc_cpus(int (func)(int))
Len Brown103a8fe2010-10-22 23:53:03 -0400978{
979 FILE *fp;
Len Brownc98d5d92012-06-04 00:56:40 -0400980 int cpu_num;
Len Brown103a8fe2010-10-22 23:53:03 -0400981 int retval;
982
983 fp = fopen(proc_stat, "r");
984 if (fp == NULL) {
985 perror(proc_stat);
986 exit(1);
987 }
988
989 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
990 if (retval != 0) {
991 perror("/proc/stat format");
992 exit(1);
993 }
994
Len Brownc98d5d92012-06-04 00:56:40 -0400995 while (1) {
996 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 -0400997 if (retval != 1)
998 break;
999
Len Brownc98d5d92012-06-04 00:56:40 -04001000 retval = func(cpu_num);
1001 if (retval) {
1002 fclose(fp);
1003 return(retval);
1004 }
Len Brown103a8fe2010-10-22 23:53:03 -04001005 }
1006 fclose(fp);
Len Brownc98d5d92012-06-04 00:56:40 -04001007 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001008}
1009
1010void re_initialize(void)
1011{
Len Brownc98d5d92012-06-04 00:56:40 -04001012 free_all_buffers();
1013 setup_all_buffers();
1014 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04001015}
1016
Len Brownc98d5d92012-06-04 00:56:40 -04001017
Len Brown103a8fe2010-10-22 23:53:03 -04001018/*
Len Brownc98d5d92012-06-04 00:56:40 -04001019 * count_cpus()
1020 * remember the last one seen, it will be the max
Len Brown103a8fe2010-10-22 23:53:03 -04001021 */
Len Brownc98d5d92012-06-04 00:56:40 -04001022int count_cpus(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001023{
Len Brownc98d5d92012-06-04 00:56:40 -04001024 if (topo.max_cpu_num < cpu)
1025 topo.max_cpu_num = cpu;
Len Brown103a8fe2010-10-22 23:53:03 -04001026
Len Brownc98d5d92012-06-04 00:56:40 -04001027 topo.num_cpus += 1;
1028 return 0;
1029}
1030int mark_cpu_present(int cpu)
1031{
1032 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brown15aaa342012-03-29 22:19:58 -04001033 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001034}
1035
1036void turbostat_loop()
1037{
Len Brownc98d5d92012-06-04 00:56:40 -04001038 int retval;
1039
Len Brown103a8fe2010-10-22 23:53:03 -04001040restart:
Len Brownc98d5d92012-06-04 00:56:40 -04001041 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
1042 if (retval) {
1043 re_initialize();
1044 goto restart;
1045 }
Len Brown103a8fe2010-10-22 23:53:03 -04001046 gettimeofday(&tv_even, (struct timezone *)NULL);
1047
1048 while (1) {
Len Brownc98d5d92012-06-04 00:56:40 -04001049 if (for_all_proc_cpus(cpu_is_not_present)) {
Len Brown103a8fe2010-10-22 23:53:03 -04001050 re_initialize();
1051 goto restart;
1052 }
1053 sleep(interval_sec);
Len Brownc98d5d92012-06-04 00:56:40 -04001054 retval = for_all_cpus(get_counters, ODD_COUNTERS);
1055 if (retval) {
Len Brown15aaa342012-03-29 22:19:58 -04001056 re_initialize();
1057 goto restart;
1058 }
Len Brown103a8fe2010-10-22 23:53:03 -04001059 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04001060 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04001061 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
1062 compute_average(EVEN_COUNTERS);
1063 format_all_counters(EVEN_COUNTERS);
1064 flush_stdout();
Len Brown15aaa342012-03-29 22:19:58 -04001065 sleep(interval_sec);
Len Brownc98d5d92012-06-04 00:56:40 -04001066 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
1067 if (retval) {
Len Brown103a8fe2010-10-22 23:53:03 -04001068 re_initialize();
1069 goto restart;
1070 }
Len Brown103a8fe2010-10-22 23:53:03 -04001071 gettimeofday(&tv_even, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04001072 timersub(&tv_even, &tv_odd, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04001073 for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS);
1074 compute_average(ODD_COUNTERS);
1075 format_all_counters(ODD_COUNTERS);
1076 flush_stdout();
Len Brown103a8fe2010-10-22 23:53:03 -04001077 }
1078}
1079
1080void check_dev_msr()
1081{
1082 struct stat sb;
1083
1084 if (stat("/dev/cpu/0/msr", &sb)) {
1085 fprintf(stderr, "no /dev/cpu/0/msr\n");
1086 fprintf(stderr, "Try \"# modprobe msr\"\n");
1087 exit(-5);
1088 }
1089}
1090
1091void check_super_user()
1092{
1093 if (getuid() != 0) {
1094 fprintf(stderr, "must be root\n");
1095 exit(-6);
1096 }
1097}
1098
1099int has_nehalem_turbo_ratio_limit(unsigned int family, unsigned int model)
1100{
1101 if (!genuine_intel)
1102 return 0;
1103
1104 if (family != 6)
1105 return 0;
1106
1107 switch (model) {
1108 case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
1109 case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
1110 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
1111 case 0x25: /* Westmere Client - Clarkdale, Arrandale */
1112 case 0x2C: /* Westmere EP - Gulftown */
1113 case 0x2A: /* SNB */
1114 case 0x2D: /* SNB Xeon */
Len Brown553575f2011-11-18 03:32:01 -05001115 case 0x3A: /* IVB */
Len Brown13006512012-09-26 18:11:31 -04001116 case 0x3E: /* IVB Xeon */
Len Brown103a8fe2010-10-22 23:53:03 -04001117 return 1;
1118 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1119 case 0x2F: /* Westmere-EX Xeon - Eagleton */
1120 default:
1121 return 0;
1122 }
1123}
Len Brown6574a5d2012-09-21 00:01:31 -04001124int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
1125{
1126 if (!genuine_intel)
1127 return 0;
1128
1129 if (family != 6)
1130 return 0;
1131
1132 switch (model) {
1133 case 0x3E: /* IVB Xeon */
1134 return 1;
1135 default:
1136 return 0;
1137 }
1138}
1139
Len Brown103a8fe2010-10-22 23:53:03 -04001140
1141int is_snb(unsigned int family, unsigned int model)
1142{
1143 if (!genuine_intel)
1144 return 0;
1145
1146 switch (model) {
1147 case 0x2A:
1148 case 0x2D:
Len Brown650a37f2012-06-03 23:34:44 -04001149 case 0x3A: /* IVB */
Len Brown13006512012-09-26 18:11:31 -04001150 case 0x3E: /* IVB Xeon */
Len Brown103a8fe2010-10-22 23:53:03 -04001151 return 1;
1152 }
1153 return 0;
1154}
1155
1156double discover_bclk(unsigned int family, unsigned int model)
1157{
1158 if (is_snb(family, model))
1159 return 100.00;
1160 else
1161 return 133.33;
1162}
1163
1164void check_cpuid()
1165{
1166 unsigned int eax, ebx, ecx, edx, max_level;
1167 unsigned int fms, family, model, stepping;
1168
1169 eax = ebx = ecx = edx = 0;
1170
1171 asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0));
1172
1173 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
1174 genuine_intel = 1;
1175
1176 if (verbose)
1177 fprintf(stderr, "%.4s%.4s%.4s ",
1178 (char *)&ebx, (char *)&edx, (char *)&ecx);
1179
1180 asm("cpuid" : "=a" (fms), "=c" (ecx), "=d" (edx) : "a" (1) : "ebx");
1181 family = (fms >> 8) & 0xf;
1182 model = (fms >> 4) & 0xf;
1183 stepping = fms & 0xf;
1184 if (family == 6 || family == 0xf)
1185 model += ((fms >> 16) & 0xf) << 4;
1186
1187 if (verbose)
1188 fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
1189 max_level, family, model, stepping, family, model, stepping);
1190
1191 if (!(edx & (1 << 5))) {
1192 fprintf(stderr, "CPUID: no MSR\n");
1193 exit(1);
1194 }
1195
1196 /*
1197 * check max extended function levels of CPUID.
1198 * This is needed to check for invariant TSC.
1199 * This check is valid for both Intel and AMD.
1200 */
1201 ebx = ecx = edx = 0;
1202 asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000000));
1203
1204 if (max_level < 0x80000007) {
1205 fprintf(stderr, "CPUID: no invariant TSC (max_level 0x%x)\n", max_level);
1206 exit(1);
1207 }
1208
1209 /*
1210 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
1211 * this check is valid for both Intel and AMD
1212 */
1213 asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000007));
Thomas Renninger8209e052011-01-21 15:11:19 +01001214 has_invariant_tsc = edx & (1 << 8);
Len Brown103a8fe2010-10-22 23:53:03 -04001215
1216 if (!has_invariant_tsc) {
1217 fprintf(stderr, "No invariant TSC\n");
1218 exit(1);
1219 }
1220
1221 /*
1222 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
1223 * this check is valid for both Intel and AMD
1224 */
1225
1226 asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x6));
Thomas Renninger8209e052011-01-21 15:11:19 +01001227 has_aperf = ecx & (1 << 0);
Len Brown103a8fe2010-10-22 23:53:03 -04001228 if (!has_aperf) {
1229 fprintf(stderr, "No APERF MSR\n");
1230 exit(1);
1231 }
1232
1233 do_nehalem_platform_info = genuine_intel && has_invariant_tsc;
1234 do_nhm_cstates = genuine_intel; /* all Intel w/ non-stop TSC have NHM counters */
1235 do_snb_cstates = is_snb(family, model);
1236 bclk = discover_bclk(family, model);
1237
1238 do_nehalem_turbo_ratio_limit = has_nehalem_turbo_ratio_limit(family, model);
Len Brown6574a5d2012-09-21 00:01:31 -04001239 do_ivt_turbo_ratio_limit = has_ivt_turbo_ratio_limit(family, model);
Len Brown103a8fe2010-10-22 23:53:03 -04001240}
1241
1242
1243void usage()
1244{
1245 fprintf(stderr, "%s: [-v] [-M MSR#] [-i interval_sec | command ...]\n",
1246 progname);
1247 exit(1);
1248}
1249
1250
1251/*
1252 * in /dev/cpu/ return success for names that are numbers
1253 * ie. filter out ".", "..", "microcode".
1254 */
1255int dir_filter(const struct dirent *dirp)
1256{
1257 if (isdigit(dirp->d_name[0]))
1258 return 1;
1259 else
1260 return 0;
1261}
1262
1263int open_dev_cpu_msr(int dummy1)
1264{
1265 return 0;
1266}
1267
Len Brownc98d5d92012-06-04 00:56:40 -04001268void topology_probe()
1269{
1270 int i;
1271 int max_core_id = 0;
1272 int max_package_id = 0;
1273 int max_siblings = 0;
1274 struct cpu_topology {
1275 int core_id;
1276 int physical_package_id;
1277 } *cpus;
1278
1279 /* Initialize num_cpus, max_cpu_num */
1280 topo.num_cpus = 0;
1281 topo.max_cpu_num = 0;
1282 for_all_proc_cpus(count_cpus);
1283 if (!summary_only && topo.num_cpus > 1)
1284 show_cpu = 1;
1285
1286 if (verbose > 1)
1287 fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
1288
1289 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
1290 if (cpus == NULL) {
1291 perror("calloc cpus");
1292 exit(1);
1293 }
1294
1295 /*
1296 * Allocate and initialize cpu_present_set
1297 */
1298 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
1299 if (cpu_present_set == NULL) {
1300 perror("CPU_ALLOC");
1301 exit(3);
1302 }
1303 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
1304 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
1305 for_all_proc_cpus(mark_cpu_present);
1306
1307 /*
1308 * Allocate and initialize cpu_affinity_set
1309 */
1310 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
1311 if (cpu_affinity_set == NULL) {
1312 perror("CPU_ALLOC");
1313 exit(3);
1314 }
1315 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
1316 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
1317
1318
1319 /*
1320 * For online cpus
1321 * find max_core_id, max_package_id
1322 */
1323 for (i = 0; i <= topo.max_cpu_num; ++i) {
1324 int siblings;
1325
1326 if (cpu_is_not_present(i)) {
1327 if (verbose > 1)
1328 fprintf(stderr, "cpu%d NOT PRESENT\n", i);
1329 continue;
1330 }
1331 cpus[i].core_id = get_core_id(i);
1332 if (cpus[i].core_id > max_core_id)
1333 max_core_id = cpus[i].core_id;
1334
1335 cpus[i].physical_package_id = get_physical_package_id(i);
1336 if (cpus[i].physical_package_id > max_package_id)
1337 max_package_id = cpus[i].physical_package_id;
1338
1339 siblings = get_num_ht_siblings(i);
1340 if (siblings > max_siblings)
1341 max_siblings = siblings;
1342 if (verbose > 1)
1343 fprintf(stderr, "cpu %d pkg %d core %d\n",
1344 i, cpus[i].physical_package_id, cpus[i].core_id);
1345 }
1346 topo.num_cores_per_pkg = max_core_id + 1;
1347 if (verbose > 1)
1348 fprintf(stderr, "max_core_id %d, sizing for %d cores per package\n",
1349 max_core_id, topo.num_cores_per_pkg);
1350 if (!summary_only && topo.num_cores_per_pkg > 1)
1351 show_core = 1;
1352
1353 topo.num_packages = max_package_id + 1;
1354 if (verbose > 1)
1355 fprintf(stderr, "max_package_id %d, sizing for %d packages\n",
1356 max_package_id, topo.num_packages);
1357 if (!summary_only && topo.num_packages > 1)
1358 show_pkg = 1;
1359
1360 topo.num_threads_per_core = max_siblings;
1361 if (verbose > 1)
1362 fprintf(stderr, "max_siblings %d\n", max_siblings);
1363
1364 free(cpus);
1365}
1366
1367void
1368allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
1369{
1370 int i;
1371
1372 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
1373 topo.num_packages, sizeof(struct thread_data));
1374 if (*t == NULL)
1375 goto error;
1376
1377 for (i = 0; i < topo.num_threads_per_core *
1378 topo.num_cores_per_pkg * topo.num_packages; i++)
1379 (*t)[i].cpu_id = -1;
1380
1381 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
1382 sizeof(struct core_data));
1383 if (*c == NULL)
1384 goto error;
1385
1386 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
1387 (*c)[i].core_id = -1;
1388
1389 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
1390 if (*p == NULL)
1391 goto error;
1392
1393 for (i = 0; i < topo.num_packages; i++)
1394 (*p)[i].package_id = i;
1395
1396 return;
1397error:
1398 perror("calloc counters");
1399 exit(1);
1400}
1401/*
1402 * init_counter()
1403 *
1404 * set cpu_id, core_num, pkg_num
1405 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
1406 *
1407 * increment topo.num_cores when 1st core in pkg seen
1408 */
1409void init_counter(struct thread_data *thread_base, struct core_data *core_base,
1410 struct pkg_data *pkg_base, int thread_num, int core_num,
1411 int pkg_num, int cpu_id)
1412{
1413 struct thread_data *t;
1414 struct core_data *c;
1415 struct pkg_data *p;
1416
1417 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
1418 c = GET_CORE(core_base, core_num, pkg_num);
1419 p = GET_PKG(pkg_base, pkg_num);
1420
1421 t->cpu_id = cpu_id;
1422 if (thread_num == 0) {
1423 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
1424 if (cpu_is_first_core_in_package(cpu_id))
1425 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
1426 }
1427
1428 c->core_id = core_num;
1429 p->package_id = pkg_num;
1430}
1431
1432
1433int initialize_counters(int cpu_id)
1434{
1435 int my_thread_id, my_core_id, my_package_id;
1436
1437 my_package_id = get_physical_package_id(cpu_id);
1438 my_core_id = get_core_id(cpu_id);
1439
1440 if (cpu_is_first_sibling_in_core(cpu_id)) {
1441 my_thread_id = 0;
1442 topo.num_cores++;
1443 } else {
1444 my_thread_id = 1;
1445 }
1446
1447 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
1448 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
1449 return 0;
1450}
1451
1452void allocate_output_buffer()
1453{
1454 output_buffer = calloc(1, (1 + topo.num_cpus) * 128);
1455 outp = output_buffer;
1456 if (outp == NULL) {
1457 perror("calloc");
1458 exit(-1);
1459 }
1460}
1461
1462void setup_all_buffers(void)
1463{
1464 topology_probe();
1465 allocate_counters(&thread_even, &core_even, &package_even);
1466 allocate_counters(&thread_odd, &core_odd, &package_odd);
1467 allocate_output_buffer();
1468 for_all_proc_cpus(initialize_counters);
1469}
Len Brown103a8fe2010-10-22 23:53:03 -04001470void turbostat_init()
1471{
1472 check_cpuid();
1473
1474 check_dev_msr();
1475 check_super_user();
1476
Len Brownc98d5d92012-06-04 00:56:40 -04001477 setup_all_buffers();
Len Brown103a8fe2010-10-22 23:53:03 -04001478
1479 if (verbose)
Len Brownc98d5d92012-06-04 00:56:40 -04001480 print_verbose_header();
Len Brown103a8fe2010-10-22 23:53:03 -04001481}
1482
1483int fork_it(char **argv)
1484{
Len Brown103a8fe2010-10-22 23:53:03 -04001485 pid_t child_pid;
Len Brownd15cf7c2012-06-03 23:24:00 -04001486
Len Brownc98d5d92012-06-04 00:56:40 -04001487 for_all_cpus(get_counters, EVEN_COUNTERS);
1488 /* clear affinity side-effect of get_counters() */
1489 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
Len Brown103a8fe2010-10-22 23:53:03 -04001490 gettimeofday(&tv_even, (struct timezone *)NULL);
1491
1492 child_pid = fork();
1493 if (!child_pid) {
1494 /* child */
1495 execvp(argv[0], argv);
1496 } else {
1497 int status;
1498
1499 /* parent */
1500 if (child_pid == -1) {
1501 perror("fork");
1502 exit(1);
1503 }
1504
1505 signal(SIGINT, SIG_IGN);
1506 signal(SIGQUIT, SIG_IGN);
1507 if (waitpid(child_pid, &status, 0) == -1) {
1508 perror("wait");
1509 exit(1);
1510 }
1511 }
Len Brownc98d5d92012-06-04 00:56:40 -04001512 /*
1513 * n.b. fork_it() does not check for errors from for_all_cpus()
1514 * because re-starting is problematic when forking
1515 */
1516 for_all_cpus(get_counters, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04001517 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04001518 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04001519 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
1520 compute_average(EVEN_COUNTERS);
1521 format_all_counters(EVEN_COUNTERS);
1522 flush_stderr();
Len Brown103a8fe2010-10-22 23:53:03 -04001523
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07001524 fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
Len Brown103a8fe2010-10-22 23:53:03 -04001525
1526 return 0;
1527}
1528
1529void cmdline(int argc, char **argv)
1530{
1531 int opt;
1532
1533 progname = argv[0];
1534
Len Brownc98d5d92012-06-04 00:56:40 -04001535 while ((opt = getopt(argc, argv, "+cpsvi:M:")) != -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04001536 switch (opt) {
Len Brownc98d5d92012-06-04 00:56:40 -04001537 case 'c':
1538 show_core_only++;
1539 break;
1540 case 'p':
1541 show_pkg_only++;
1542 break;
Len Browne23da032012-02-06 18:37:16 -05001543 case 's':
1544 summary_only++;
1545 break;
Len Brown103a8fe2010-10-22 23:53:03 -04001546 case 'v':
1547 verbose++;
1548 break;
1549 case 'i':
1550 interval_sec = atoi(optarg);
1551 break;
1552 case 'M':
1553 sscanf(optarg, "%x", &extra_msr_offset);
1554 if (verbose > 1)
1555 fprintf(stderr, "MSR 0x%X\n", extra_msr_offset);
1556 break;
1557 default:
1558 usage();
1559 }
1560 }
1561}
1562
1563int main(int argc, char **argv)
1564{
1565 cmdline(argc, argv);
1566
1567 if (verbose > 1)
Len Brownc98d5d92012-06-04 00:56:40 -04001568 fprintf(stderr, "turbostat v2.0 May 16, 2012"
Len Brown103a8fe2010-10-22 23:53:03 -04001569 " - Len Brown <lenb@kernel.org>\n");
Len Brown103a8fe2010-10-22 23:53:03 -04001570
1571 turbostat_init();
1572
1573 /*
1574 * if any params left, it must be a command to fork
1575 */
1576 if (argc - optind)
1577 return fork_it(argv + optind);
1578 else
1579 turbostat_loop();
1580
1581 return 0;
1582}