blob: f7b5d6f83d28ece3cc82028b1b1d0ba91d709111 [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 Brown144b44b2013-11-09 00:30:16 -05005 * Copyright (c) 2013 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
Josh Triplettb731f312013-08-20 17:20:12 -070023#include MSRHEADER
Josh Triplett95aebc42013-08-20 17:20:16 -070024#include <stdarg.h>
Len Brown103a8fe2010-10-22 23:53:03 -040025#include <stdio.h>
26#include <unistd.h>
27#include <sys/types.h>
28#include <sys/wait.h>
29#include <sys/stat.h>
30#include <sys/resource.h>
31#include <fcntl.h>
32#include <signal.h>
33#include <sys/time.h>
34#include <stdlib.h>
35#include <dirent.h>
36#include <string.h>
37#include <ctype.h>
Len Brown88c32812012-03-29 21:44:40 -040038#include <sched.h>
Josh Triplett2b928652013-08-20 17:20:14 -070039#include <cpuid.h>
Len Brown103a8fe2010-10-22 23:53:03 -040040
Len Brown103a8fe2010-10-22 23:53:03 -040041char *proc_stat = "/proc/stat";
42unsigned int interval_sec = 5; /* set with -i interval_sec */
43unsigned int verbose; /* set with -v */
Len Brown889facb2012-11-08 00:48:57 -050044unsigned int rapl_verbose; /* set with -R */
45unsigned int thermal_verbose; /* set with -T */
Len Browne23da032012-02-06 18:37:16 -050046unsigned int summary_only; /* set with -s */
Len Brown103a8fe2010-10-22 23:53:03 -040047unsigned int skip_c0;
48unsigned int skip_c1;
49unsigned int do_nhm_cstates;
50unsigned int do_snb_cstates;
Kristen Carlson Accardica587102012-11-21 05:22:43 -080051unsigned int do_c8_c9_c10;
Len Brown144b44b2013-11-09 00:30:16 -050052unsigned int do_slm_cstates;
53unsigned int use_c1_residency_msr;
Len Brown103a8fe2010-10-22 23:53:03 -040054unsigned int has_aperf;
Len Brown889facb2012-11-08 00:48:57 -050055unsigned int has_epb;
Len Brown103a8fe2010-10-22 23:53:03 -040056unsigned int units = 1000000000; /* Ghz etc */
57unsigned int genuine_intel;
58unsigned int has_invariant_tsc;
59unsigned int do_nehalem_platform_info;
60unsigned int do_nehalem_turbo_ratio_limit;
Len Brown6574a5d2012-09-21 00:01:31 -040061unsigned int do_ivt_turbo_ratio_limit;
Len Brown2f32edf2012-09-21 23:45:46 -040062unsigned int extra_msr_offset32;
63unsigned int extra_msr_offset64;
Len Brown8e180f32012-09-22 01:25:08 -040064unsigned int extra_delta_offset32;
65unsigned int extra_delta_offset64;
Len Brown1ed51012013-02-10 17:19:24 -050066int do_smi;
Len Brown103a8fe2010-10-22 23:53:03 -040067double 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 Brown889facb2012-11-08 00:48:57 -050074unsigned int do_rapl;
75unsigned int do_dts;
76unsigned int do_ptm;
77unsigned int tcc_activation_temp;
78unsigned int tcc_activation_temp_override;
79double rapl_power_units, rapl_energy_units, rapl_time_units;
80double rapl_joule_counter_range;
81
82#define RAPL_PKG (1 << 0)
83#define RAPL_CORES (1 << 1)
84#define RAPL_GFX (1 << 2)
85#define RAPL_DRAM (1 << 3)
86#define RAPL_PKG_PERF_STATUS (1 << 4)
87#define RAPL_DRAM_PERF_STATUS (1 << 5)
Len Brown144b44b2013-11-09 00:30:16 -050088#define RAPL_PKG_POWER_INFO (1 << 6)
89#define RAPL_CORE_POLICY (1 << 7)
Len Brown889facb2012-11-08 00:48:57 -050090#define TJMAX_DEFAULT 100
91
92#define MAX(a, b) ((a) > (b) ? (a) : (b))
Len Brown103a8fe2010-10-22 23:53:03 -040093
94int aperf_mperf_unstable;
95int backwards_count;
96char *progname;
Len Brown103a8fe2010-10-22 23:53:03 -040097
Len Brownc98d5d92012-06-04 00:56:40 -040098cpu_set_t *cpu_present_set, *cpu_affinity_set;
99size_t cpu_present_setsize, cpu_affinity_setsize;
Len Brown103a8fe2010-10-22 23:53:03 -0400100
Len Brownc98d5d92012-06-04 00:56:40 -0400101struct thread_data {
102 unsigned long long tsc;
103 unsigned long long aperf;
104 unsigned long long mperf;
Len Brown144b44b2013-11-09 00:30:16 -0500105 unsigned long long c1;
Len Brown2f32edf2012-09-21 23:45:46 -0400106 unsigned long long extra_msr64;
Len Brown8e180f32012-09-22 01:25:08 -0400107 unsigned long long extra_delta64;
108 unsigned long long extra_msr32;
109 unsigned long long extra_delta32;
Len Brown1ed51012013-02-10 17:19:24 -0500110 unsigned int smi_count;
Len Brownc98d5d92012-06-04 00:56:40 -0400111 unsigned int cpu_id;
112 unsigned int flags;
113#define CPU_IS_FIRST_THREAD_IN_CORE 0x2
114#define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
115} *thread_even, *thread_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400116
Len Brownc98d5d92012-06-04 00:56:40 -0400117struct core_data {
118 unsigned long long c3;
119 unsigned long long c6;
120 unsigned long long c7;
Len Brown889facb2012-11-08 00:48:57 -0500121 unsigned int core_temp_c;
Len Brownc98d5d92012-06-04 00:56:40 -0400122 unsigned int core_id;
123} *core_even, *core_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400124
Len Brownc98d5d92012-06-04 00:56:40 -0400125struct pkg_data {
126 unsigned long long pc2;
127 unsigned long long pc3;
128 unsigned long long pc6;
129 unsigned long long pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800130 unsigned long long pc8;
131 unsigned long long pc9;
132 unsigned long long pc10;
Len Brownc98d5d92012-06-04 00:56:40 -0400133 unsigned int package_id;
Len Brown889facb2012-11-08 00:48:57 -0500134 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
135 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
136 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
137 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
138 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
139 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
140 unsigned int pkg_temp_c;
141
Len Brownc98d5d92012-06-04 00:56:40 -0400142} *package_even, *package_odd;
143
144#define ODD_COUNTERS thread_odd, core_odd, package_odd
145#define EVEN_COUNTERS thread_even, core_even, package_even
146
147#define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
148 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
149 topo.num_threads_per_core + \
150 (core_no) * topo.num_threads_per_core + (thread_no))
151#define GET_CORE(core_base, core_no, pkg_no) \
152 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
153#define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
154
155struct system_summary {
156 struct thread_data threads;
157 struct core_data cores;
158 struct pkg_data packages;
159} sum, average;
160
161
162struct topo_params {
163 int num_packages;
164 int num_cpus;
165 int num_cores;
166 int max_cpu_num;
167 int num_cores_per_pkg;
168 int num_threads_per_core;
169} topo;
170
171struct timeval tv_even, tv_odd, tv_delta;
172
173void setup_all_buffers(void);
174
175int cpu_is_not_present(int cpu)
Len Brownd15cf7c2012-06-03 23:24:00 -0400176{
Len Brownc98d5d92012-06-04 00:56:40 -0400177 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brownd15cf7c2012-06-03 23:24:00 -0400178}
Len Brown88c32812012-03-29 21:44:40 -0400179/*
Len Brownc98d5d92012-06-04 00:56:40 -0400180 * run func(thread, core, package) in topology order
181 * skip non-present cpus
Len Brown88c32812012-03-29 21:44:40 -0400182 */
Len Brownd15cf7c2012-06-03 23:24:00 -0400183
Len Brownc98d5d92012-06-04 00:56:40 -0400184int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
185 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
Len Brown88c32812012-03-29 21:44:40 -0400186{
Len Brownc98d5d92012-06-04 00:56:40 -0400187 int retval, pkg_no, core_no, thread_no;
188
189 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
190 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
191 for (thread_no = 0; thread_no <
192 topo.num_threads_per_core; ++thread_no) {
193 struct thread_data *t;
194 struct core_data *c;
195 struct pkg_data *p;
196
197 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
198
199 if (cpu_is_not_present(t->cpu_id))
200 continue;
201
202 c = GET_CORE(core_base, core_no, pkg_no);
203 p = GET_PKG(pkg_base, pkg_no);
204
205 retval = func(t, c, p);
206 if (retval)
207 return retval;
208 }
209 }
210 }
211 return 0;
Len Brown88c32812012-03-29 21:44:40 -0400212}
213
214int cpu_migrate(int cpu)
215{
Len Brownc98d5d92012-06-04 00:56:40 -0400216 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
217 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
218 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
Len Brown88c32812012-03-29 21:44:40 -0400219 return -1;
220 else
221 return 0;
222}
223
Len Brown15aaa342012-03-29 22:19:58 -0400224int get_msr(int cpu, off_t offset, unsigned long long *msr)
Len Brown103a8fe2010-10-22 23:53:03 -0400225{
226 ssize_t retval;
Len Brown103a8fe2010-10-22 23:53:03 -0400227 char pathname[32];
228 int fd;
229
230 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
231 fd = open(pathname, O_RDONLY);
Len Brown15aaa342012-03-29 22:19:58 -0400232 if (fd < 0)
233 return -1;
Len Brown103a8fe2010-10-22 23:53:03 -0400234
Len Brown15aaa342012-03-29 22:19:58 -0400235 retval = pread(fd, msr, sizeof *msr, offset);
Len Brown103a8fe2010-10-22 23:53:03 -0400236 close(fd);
Len Brown15aaa342012-03-29 22:19:58 -0400237
Len Brownd91bb172012-11-01 00:08:19 -0400238 if (retval != sizeof *msr) {
Josh Triplett2e9c6bc2013-08-20 17:20:13 -0700239 fprintf(stderr, "%s offset 0x%llx read failed\n", pathname, (unsigned long long)offset);
Len Brown15aaa342012-03-29 22:19:58 -0400240 return -1;
Len Brownd91bb172012-11-01 00:08:19 -0400241 }
Len Brown15aaa342012-03-29 22:19:58 -0400242
243 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400244}
245
Len Browna829eb42011-02-10 23:36:34 -0500246void print_header(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400247{
248 if (show_pkg)
Len Brownc98d5d92012-06-04 00:56:40 -0400249 outp += sprintf(outp, "pk");
Len Browne23da032012-02-06 18:37:16 -0500250 if (show_pkg)
Len Brownc98d5d92012-06-04 00:56:40 -0400251 outp += sprintf(outp, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400252 if (show_core)
Len Brownc98d5d92012-06-04 00:56:40 -0400253 outp += sprintf(outp, "cor");
Len Brown103a8fe2010-10-22 23:53:03 -0400254 if (show_cpu)
Len Brownc98d5d92012-06-04 00:56:40 -0400255 outp += sprintf(outp, " CPU");
Len Browne23da032012-02-06 18:37:16 -0500256 if (show_pkg || show_core || show_cpu)
Len Brownc98d5d92012-06-04 00:56:40 -0400257 outp += sprintf(outp, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400258 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400259 outp += sprintf(outp, " %%c0");
Len Brown103a8fe2010-10-22 23:53:03 -0400260 if (has_aperf)
Len Brownc98d5d92012-06-04 00:56:40 -0400261 outp += sprintf(outp, " GHz");
262 outp += sprintf(outp, " TSC");
Len Brown1ed51012013-02-10 17:19:24 -0500263 if (do_smi)
264 outp += sprintf(outp, " SMI");
Len Brown8e180f32012-09-22 01:25:08 -0400265 if (extra_delta_offset32)
Len Brownf9240812012-10-06 15:26:31 -0400266 outp += sprintf(outp, " count 0x%03X", extra_delta_offset32);
Len Brown8e180f32012-09-22 01:25:08 -0400267 if (extra_delta_offset64)
Len Brownf9240812012-10-06 15:26:31 -0400268 outp += sprintf(outp, " COUNT 0x%03X", extra_delta_offset64);
Len Brown2f32edf2012-09-21 23:45:46 -0400269 if (extra_msr_offset32)
Len Brown8e180f32012-09-22 01:25:08 -0400270 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset32);
Len Brown2f32edf2012-09-21 23:45:46 -0400271 if (extra_msr_offset64)
Len Brown8e180f32012-09-22 01:25:08 -0400272 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset64);
Len Brown103a8fe2010-10-22 23:53:03 -0400273 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400274 outp += sprintf(outp, " %%c1");
Len Brown144b44b2013-11-09 00:30:16 -0500275 if (do_nhm_cstates && !do_slm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400276 outp += sprintf(outp, " %%c3");
Len Brown103a8fe2010-10-22 23:53:03 -0400277 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400278 outp += sprintf(outp, " %%c6");
Len Brown103a8fe2010-10-22 23:53:03 -0400279 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400280 outp += sprintf(outp, " %%c7");
Len Brown889facb2012-11-08 00:48:57 -0500281
282 if (do_dts)
283 outp += sprintf(outp, " CTMP");
284 if (do_ptm)
285 outp += sprintf(outp, " PTMP");
286
Len Brown103a8fe2010-10-22 23:53:03 -0400287 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400288 outp += sprintf(outp, " %%pc2");
Len Brown144b44b2013-11-09 00:30:16 -0500289 if (do_nhm_cstates && !do_slm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400290 outp += sprintf(outp, " %%pc3");
Len Brown144b44b2013-11-09 00:30:16 -0500291 if (do_nhm_cstates && !do_slm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400292 outp += sprintf(outp, " %%pc6");
Len Brown103a8fe2010-10-22 23:53:03 -0400293 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400294 outp += sprintf(outp, " %%pc7");
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800295 if (do_c8_c9_c10) {
296 outp += sprintf(outp, " %%pc8");
297 outp += sprintf(outp, " %%pc9");
298 outp += sprintf(outp, " %%pc10");
299 }
Len Brown103a8fe2010-10-22 23:53:03 -0400300
Len Brown889facb2012-11-08 00:48:57 -0500301 if (do_rapl & RAPL_PKG)
302 outp += sprintf(outp, " Pkg_W");
303 if (do_rapl & RAPL_CORES)
304 outp += sprintf(outp, " Cor_W");
305 if (do_rapl & RAPL_GFX)
306 outp += sprintf(outp, " GFX_W");
307 if (do_rapl & RAPL_DRAM)
308 outp += sprintf(outp, " RAM_W");
309 if (do_rapl & RAPL_PKG_PERF_STATUS)
310 outp += sprintf(outp, " PKG_%%");
311 if (do_rapl & RAPL_DRAM_PERF_STATUS)
312 outp += sprintf(outp, " RAM_%%");
313
Len Brownc98d5d92012-06-04 00:56:40 -0400314 outp += sprintf(outp, "\n");
Len Brown103a8fe2010-10-22 23:53:03 -0400315}
316
Len Brownc98d5d92012-06-04 00:56:40 -0400317int dump_counters(struct thread_data *t, struct core_data *c,
318 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400319{
Len Brownc98d5d92012-06-04 00:56:40 -0400320 fprintf(stderr, "t %p, c %p, p %p\n", t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400321
Len Brownc98d5d92012-06-04 00:56:40 -0400322 if (t) {
323 fprintf(stderr, "CPU: %d flags 0x%x\n", t->cpu_id, t->flags);
324 fprintf(stderr, "TSC: %016llX\n", t->tsc);
325 fprintf(stderr, "aperf: %016llX\n", t->aperf);
326 fprintf(stderr, "mperf: %016llX\n", t->mperf);
327 fprintf(stderr, "c1: %016llX\n", t->c1);
Len Brown8e180f32012-09-22 01:25:08 -0400328 fprintf(stderr, "msr0x%x: %08llX\n",
329 extra_delta_offset32, t->extra_delta32);
330 fprintf(stderr, "msr0x%x: %016llX\n",
331 extra_delta_offset64, t->extra_delta64);
332 fprintf(stderr, "msr0x%x: %08llX\n",
Len Brown2f32edf2012-09-21 23:45:46 -0400333 extra_msr_offset32, t->extra_msr32);
Len Brownc98d5d92012-06-04 00:56:40 -0400334 fprintf(stderr, "msr0x%x: %016llX\n",
Len Brown2f32edf2012-09-21 23:45:46 -0400335 extra_msr_offset64, t->extra_msr64);
Len Brown1ed51012013-02-10 17:19:24 -0500336 if (do_smi)
337 fprintf(stderr, "SMI: %08X\n", t->smi_count);
Len Brownc98d5d92012-06-04 00:56:40 -0400338 }
Len Brown103a8fe2010-10-22 23:53:03 -0400339
Len Brownc98d5d92012-06-04 00:56:40 -0400340 if (c) {
341 fprintf(stderr, "core: %d\n", c->core_id);
342 fprintf(stderr, "c3: %016llX\n", c->c3);
343 fprintf(stderr, "c6: %016llX\n", c->c6);
344 fprintf(stderr, "c7: %016llX\n", c->c7);
Len Brown889facb2012-11-08 00:48:57 -0500345 fprintf(stderr, "DTS: %dC\n", c->core_temp_c);
Len Brownc98d5d92012-06-04 00:56:40 -0400346 }
347
348 if (p) {
349 fprintf(stderr, "package: %d\n", p->package_id);
350 fprintf(stderr, "pc2: %016llX\n", p->pc2);
351 fprintf(stderr, "pc3: %016llX\n", p->pc3);
352 fprintf(stderr, "pc6: %016llX\n", p->pc6);
353 fprintf(stderr, "pc7: %016llX\n", p->pc7);
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800354 fprintf(stderr, "pc8: %016llX\n", p->pc8);
355 fprintf(stderr, "pc9: %016llX\n", p->pc9);
356 fprintf(stderr, "pc10: %016llX\n", p->pc10);
Len Brown889facb2012-11-08 00:48:57 -0500357 fprintf(stderr, "Joules PKG: %0X\n", p->energy_pkg);
358 fprintf(stderr, "Joules COR: %0X\n", p->energy_cores);
359 fprintf(stderr, "Joules GFX: %0X\n", p->energy_gfx);
360 fprintf(stderr, "Joules RAM: %0X\n", p->energy_dram);
361 fprintf(stderr, "Throttle PKG: %0X\n", p->rapl_pkg_perf_status);
362 fprintf(stderr, "Throttle RAM: %0X\n", p->rapl_dram_perf_status);
363 fprintf(stderr, "PTM: %dC\n", p->pkg_temp_c);
Len Brownc98d5d92012-06-04 00:56:40 -0400364 }
365 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400366}
367
Len Browne23da032012-02-06 18:37:16 -0500368/*
369 * column formatting convention & formats
370 * package: "pk" 2 columns %2d
371 * core: "cor" 3 columns %3d
372 * CPU: "CPU" 3 columns %3d
Len Brown889facb2012-11-08 00:48:57 -0500373 * Pkg_W: %6.2
374 * Cor_W: %6.2
375 * GFX_W: %5.2
376 * RAM_W: %5.2
Len Browne23da032012-02-06 18:37:16 -0500377 * GHz: "GHz" 3 columns %3.2
378 * TSC: "TSC" 3 columns %3.2
Len Brown1ed51012013-02-10 17:19:24 -0500379 * SMI: "SMI" 4 columns %4d
Len Browne23da032012-02-06 18:37:16 -0500380 * percentage " %pc3" %6.2
Len Brown889facb2012-11-08 00:48:57 -0500381 * Perf Status percentage: %5.2
382 * "CTMP" 4 columns %4d
Len Browne23da032012-02-06 18:37:16 -0500383 */
Len Brownc98d5d92012-06-04 00:56:40 -0400384int format_counters(struct thread_data *t, struct core_data *c,
385 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400386{
387 double interval_float;
Len Brown889facb2012-11-08 00:48:57 -0500388 char *fmt5, *fmt6;
Len Brown103a8fe2010-10-22 23:53:03 -0400389
Len Brownc98d5d92012-06-04 00:56:40 -0400390 /* if showing only 1st thread in core and this isn't one, bail out */
391 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
392 return 0;
393
394 /* if showing only 1st thread in pkg and this isn't one, bail out */
395 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
396 return 0;
397
Len Brown103a8fe2010-10-22 23:53:03 -0400398 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
399
Len Brownc98d5d92012-06-04 00:56:40 -0400400 /* topo columns, print blanks on 1st (average) line */
401 if (t == &average.threads) {
Len Brown103a8fe2010-10-22 23:53:03 -0400402 if (show_pkg)
Len Brownc98d5d92012-06-04 00:56:40 -0400403 outp += sprintf(outp, " ");
Len Browne23da032012-02-06 18:37:16 -0500404 if (show_pkg && show_core)
Len Brownc98d5d92012-06-04 00:56:40 -0400405 outp += sprintf(outp, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400406 if (show_core)
Len Brownc98d5d92012-06-04 00:56:40 -0400407 outp += sprintf(outp, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400408 if (show_cpu)
Len Brownc98d5d92012-06-04 00:56:40 -0400409 outp += sprintf(outp, " " " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400410 } else {
Len Brownc98d5d92012-06-04 00:56:40 -0400411 if (show_pkg) {
412 if (p)
413 outp += sprintf(outp, "%2d", p->package_id);
414 else
415 outp += sprintf(outp, " ");
416 }
Len Browne23da032012-02-06 18:37:16 -0500417 if (show_pkg && show_core)
Len Brownc98d5d92012-06-04 00:56:40 -0400418 outp += sprintf(outp, " ");
419 if (show_core) {
420 if (c)
421 outp += sprintf(outp, "%3d", c->core_id);
422 else
423 outp += sprintf(outp, " ");
424 }
Len Brown103a8fe2010-10-22 23:53:03 -0400425 if (show_cpu)
Len Brownc98d5d92012-06-04 00:56:40 -0400426 outp += sprintf(outp, " %3d", t->cpu_id);
Len Brown103a8fe2010-10-22 23:53:03 -0400427 }
Len Brown103a8fe2010-10-22 23:53:03 -0400428 /* %c0 */
429 if (do_nhm_cstates) {
Len Browne23da032012-02-06 18:37:16 -0500430 if (show_pkg || show_core || show_cpu)
Len Brownc98d5d92012-06-04 00:56:40 -0400431 outp += sprintf(outp, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400432 if (!skip_c0)
Len Brownc98d5d92012-06-04 00:56:40 -0400433 outp += sprintf(outp, "%6.2f", 100.0 * t->mperf/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400434 else
Len Brownc98d5d92012-06-04 00:56:40 -0400435 outp += sprintf(outp, " ****");
Len Brown103a8fe2010-10-22 23:53:03 -0400436 }
437
438 /* GHz */
439 if (has_aperf) {
440 if (!aperf_mperf_unstable) {
Len Brownc98d5d92012-06-04 00:56:40 -0400441 outp += sprintf(outp, " %3.2f",
442 1.0 * t->tsc / units * t->aperf /
443 t->mperf / interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400444 } else {
Len Brownc98d5d92012-06-04 00:56:40 -0400445 if (t->aperf > t->tsc || t->mperf > t->tsc) {
446 outp += sprintf(outp, " ***");
Len Brown103a8fe2010-10-22 23:53:03 -0400447 } else {
Len Brownc98d5d92012-06-04 00:56:40 -0400448 outp += sprintf(outp, "%3.1f*",
449 1.0 * t->tsc /
450 units * t->aperf /
451 t->mperf / interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400452 }
453 }
454 }
455
456 /* TSC */
Len Brownc98d5d92012-06-04 00:56:40 -0400457 outp += sprintf(outp, "%5.2f", 1.0 * t->tsc/units/interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400458
Len Brown1ed51012013-02-10 17:19:24 -0500459 /* SMI */
460 if (do_smi)
461 outp += sprintf(outp, "%4d", t->smi_count);
462
Len Brown8e180f32012-09-22 01:25:08 -0400463 /* delta */
464 if (extra_delta_offset32)
465 outp += sprintf(outp, " %11llu", t->extra_delta32);
466
467 /* DELTA */
468 if (extra_delta_offset64)
469 outp += sprintf(outp, " %11llu", t->extra_delta64);
Len Brown2f32edf2012-09-21 23:45:46 -0400470 /* msr */
471 if (extra_msr_offset32)
Len Brown8e180f32012-09-22 01:25:08 -0400472 outp += sprintf(outp, " 0x%08llx", t->extra_msr32);
Len Brown2f32edf2012-09-21 23:45:46 -0400473
Len Brown130ff302012-09-21 22:56:06 -0400474 /* MSR */
Len Brown2f32edf2012-09-21 23:45:46 -0400475 if (extra_msr_offset64)
476 outp += sprintf(outp, " 0x%016llx", t->extra_msr64);
Len Brown130ff302012-09-21 22:56:06 -0400477
Len Brown103a8fe2010-10-22 23:53:03 -0400478 if (do_nhm_cstates) {
479 if (!skip_c1)
Len Brownc98d5d92012-06-04 00:56:40 -0400480 outp += sprintf(outp, " %6.2f", 100.0 * t->c1/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400481 else
Len Brownc98d5d92012-06-04 00:56:40 -0400482 outp += sprintf(outp, " ****");
Len Brown103a8fe2010-10-22 23:53:03 -0400483 }
Len Brownc98d5d92012-06-04 00:56:40 -0400484
485 /* print per-core data only for 1st thread in core */
486 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
487 goto done;
488
Len Brown144b44b2013-11-09 00:30:16 -0500489 if (do_nhm_cstates && !do_slm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400490 outp += sprintf(outp, " %6.2f", 100.0 * c->c3/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400491 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400492 outp += sprintf(outp, " %6.2f", 100.0 * c->c6/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400493 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400494 outp += sprintf(outp, " %6.2f", 100.0 * c->c7/t->tsc);
495
Len Brown889facb2012-11-08 00:48:57 -0500496 if (do_dts)
497 outp += sprintf(outp, " %4d", c->core_temp_c);
498
Len Brownc98d5d92012-06-04 00:56:40 -0400499 /* print per-package data only for 1st core in package */
500 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
501 goto done;
502
Len Brown889facb2012-11-08 00:48:57 -0500503 if (do_ptm)
504 outp += sprintf(outp, " %4d", p->pkg_temp_c);
505
Len Brown103a8fe2010-10-22 23:53:03 -0400506 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400507 outp += sprintf(outp, " %6.2f", 100.0 * p->pc2/t->tsc);
Len Brown144b44b2013-11-09 00:30:16 -0500508 if (do_nhm_cstates && !do_slm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400509 outp += sprintf(outp, " %6.2f", 100.0 * p->pc3/t->tsc);
Len Brown144b44b2013-11-09 00:30:16 -0500510 if (do_nhm_cstates && !do_slm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400511 outp += sprintf(outp, " %6.2f", 100.0 * p->pc6/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400512 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400513 outp += sprintf(outp, " %6.2f", 100.0 * p->pc7/t->tsc);
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800514 if (do_c8_c9_c10) {
515 outp += sprintf(outp, " %6.2f", 100.0 * p->pc8/t->tsc);
516 outp += sprintf(outp, " %6.2f", 100.0 * p->pc9/t->tsc);
517 outp += sprintf(outp, " %6.2f", 100.0 * p->pc10/t->tsc);
518 }
Len Brown889facb2012-11-08 00:48:57 -0500519
520 /*
521 * If measurement interval exceeds minimum RAPL Joule Counter range,
522 * indicate that results are suspect by printing "**" in fraction place.
523 */
524 if (interval_float < rapl_joule_counter_range) {
525 fmt5 = " %5.2f";
526 fmt6 = " %6.2f";
527 } else {
528 fmt5 = " %3.0f**";
529 fmt6 = " %4.0f**";
530 }
531
532 if (do_rapl & RAPL_PKG)
533 outp += sprintf(outp, fmt6, p->energy_pkg * rapl_energy_units / interval_float);
534 if (do_rapl & RAPL_CORES)
535 outp += sprintf(outp, fmt6, p->energy_cores * rapl_energy_units / interval_float);
536 if (do_rapl & RAPL_GFX)
537 outp += sprintf(outp, fmt5, p->energy_gfx * rapl_energy_units / interval_float);
538 if (do_rapl & RAPL_DRAM)
539 outp += sprintf(outp, fmt5, p->energy_dram * rapl_energy_units / interval_float);
540 if (do_rapl & RAPL_PKG_PERF_STATUS )
541 outp += sprintf(outp, fmt5, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
542 if (do_rapl & RAPL_DRAM_PERF_STATUS )
543 outp += sprintf(outp, fmt5, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
544
Len Brownc98d5d92012-06-04 00:56:40 -0400545done:
Len Brownc98d5d92012-06-04 00:56:40 -0400546 outp += sprintf(outp, "\n");
547
548 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400549}
550
Len Brownc98d5d92012-06-04 00:56:40 -0400551void flush_stdout()
Len Brown103a8fe2010-10-22 23:53:03 -0400552{
Len Brownc98d5d92012-06-04 00:56:40 -0400553 fputs(output_buffer, stdout);
Len Brownddac0d62012-11-30 01:01:40 -0500554 fflush(stdout);
Len Brownc98d5d92012-06-04 00:56:40 -0400555 outp = output_buffer;
556}
557void flush_stderr()
558{
559 fputs(output_buffer, stderr);
560 outp = output_buffer;
561}
562void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
563{
Len Browne23da032012-02-06 18:37:16 -0500564 static int printed;
Len Brown103a8fe2010-10-22 23:53:03 -0400565
Len Browne23da032012-02-06 18:37:16 -0500566 if (!printed || !summary_only)
567 print_header();
Len Brown103a8fe2010-10-22 23:53:03 -0400568
Len Brownc98d5d92012-06-04 00:56:40 -0400569 if (topo.num_cpus > 1)
570 format_counters(&average.threads, &average.cores,
571 &average.packages);
Len Brown103a8fe2010-10-22 23:53:03 -0400572
Len Browne23da032012-02-06 18:37:16 -0500573 printed = 1;
574
575 if (summary_only)
576 return;
577
Len Brownc98d5d92012-06-04 00:56:40 -0400578 for_all_cpus(format_counters, t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400579}
580
Len Brown889facb2012-11-08 00:48:57 -0500581#define DELTA_WRAP32(new, old) \
582 if (new > old) { \
583 old = new - old; \
584 } else { \
585 old = 0x100000000 + new - old; \
586 }
587
Len Brownc98d5d92012-06-04 00:56:40 -0400588void
589delta_package(struct pkg_data *new, struct pkg_data *old)
Len Brown103a8fe2010-10-22 23:53:03 -0400590{
Len Brownc98d5d92012-06-04 00:56:40 -0400591 old->pc2 = new->pc2 - old->pc2;
592 old->pc3 = new->pc3 - old->pc3;
593 old->pc6 = new->pc6 - old->pc6;
594 old->pc7 = new->pc7 - old->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800595 old->pc8 = new->pc8 - old->pc8;
596 old->pc9 = new->pc9 - old->pc9;
597 old->pc10 = new->pc10 - old->pc10;
Len Brown889facb2012-11-08 00:48:57 -0500598 old->pkg_temp_c = new->pkg_temp_c;
599
600 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
601 DELTA_WRAP32(new->energy_cores, old->energy_cores);
602 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
603 DELTA_WRAP32(new->energy_dram, old->energy_dram);
604 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
605 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
Len Brownc98d5d92012-06-04 00:56:40 -0400606}
Len Brown103a8fe2010-10-22 23:53:03 -0400607
Len Brownc98d5d92012-06-04 00:56:40 -0400608void
609delta_core(struct core_data *new, struct core_data *old)
610{
611 old->c3 = new->c3 - old->c3;
612 old->c6 = new->c6 - old->c6;
613 old->c7 = new->c7 - old->c7;
Len Brown889facb2012-11-08 00:48:57 -0500614 old->core_temp_c = new->core_temp_c;
Len Brownc98d5d92012-06-04 00:56:40 -0400615}
Len Brown103a8fe2010-10-22 23:53:03 -0400616
Len Brownc3ae3312012-06-13 21:31:46 -0400617/*
618 * old = new - old
619 */
Len Brownc98d5d92012-06-04 00:56:40 -0400620void
621delta_thread(struct thread_data *new, struct thread_data *old,
622 struct core_data *core_delta)
623{
624 old->tsc = new->tsc - old->tsc;
Len Brown103a8fe2010-10-22 23:53:03 -0400625
Len Brownc98d5d92012-06-04 00:56:40 -0400626 /* check for TSC < 1 Mcycles over interval */
627 if (old->tsc < (1000 * 1000)) {
628 fprintf(stderr, "Insanely slow TSC rate, TSC stops in idle?\n");
629 fprintf(stderr, "You can disable all c-states by booting with \"idle=poll\"\n");
630 fprintf(stderr, "or just the deep ones with \"processor.max_cstate=1\"\n");
631 exit(-3);
632 }
Len Brown103a8fe2010-10-22 23:53:03 -0400633
Len Brownc98d5d92012-06-04 00:56:40 -0400634 old->c1 = new->c1 - old->c1;
Len Brown103a8fe2010-10-22 23:53:03 -0400635
Len Brownc98d5d92012-06-04 00:56:40 -0400636 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
637 old->aperf = new->aperf - old->aperf;
638 old->mperf = new->mperf - old->mperf;
639 } else {
Len Brown103a8fe2010-10-22 23:53:03 -0400640
Len Brownc98d5d92012-06-04 00:56:40 -0400641 if (!aperf_mperf_unstable) {
642 fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname);
643 fprintf(stderr, "* Frequency results do not cover entire interval *\n");
644 fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n");
645
646 aperf_mperf_unstable = 1;
647 }
Len Brown103a8fe2010-10-22 23:53:03 -0400648 /*
Len Brownc98d5d92012-06-04 00:56:40 -0400649 * mperf delta is likely a huge "positive" number
650 * can not use it for calculating c0 time
Len Brown103a8fe2010-10-22 23:53:03 -0400651 */
Len Brownc98d5d92012-06-04 00:56:40 -0400652 skip_c0 = 1;
653 skip_c1 = 1;
654 }
Len Brown103a8fe2010-10-22 23:53:03 -0400655
Len Brown103a8fe2010-10-22 23:53:03 -0400656
Len Brown144b44b2013-11-09 00:30:16 -0500657 if (use_c1_residency_msr) {
658 /*
659 * Some models have a dedicated C1 residency MSR,
660 * which should be more accurate than the derivation below.
661 */
662 } else {
663 /*
664 * As counter collection is not atomic,
665 * it is possible for mperf's non-halted cycles + idle states
666 * to exceed TSC's all cycles: show c1 = 0% in that case.
667 */
668 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
669 old->c1 = 0;
670 else {
671 /* normal case, derive c1 */
672 old->c1 = old->tsc - old->mperf - core_delta->c3
Len Brownc98d5d92012-06-04 00:56:40 -0400673 - core_delta->c6 - core_delta->c7;
Len Brown144b44b2013-11-09 00:30:16 -0500674 }
Len Brownc98d5d92012-06-04 00:56:40 -0400675 }
Len Brownc3ae3312012-06-13 21:31:46 -0400676
Len Brownc98d5d92012-06-04 00:56:40 -0400677 if (old->mperf == 0) {
Len Brownc3ae3312012-06-13 21:31:46 -0400678 if (verbose > 1) fprintf(stderr, "cpu%d MPERF 0!\n", old->cpu_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400679 old->mperf = 1; /* divide by 0 protection */
680 }
681
Len Brown8e180f32012-09-22 01:25:08 -0400682 old->extra_delta32 = new->extra_delta32 - old->extra_delta32;
683 old->extra_delta32 &= 0xFFFFFFFF;
684
685 old->extra_delta64 = new->extra_delta64 - old->extra_delta64;
686
Len Brownc98d5d92012-06-04 00:56:40 -0400687 /*
Len Brown8e180f32012-09-22 01:25:08 -0400688 * Extra MSR is just a snapshot, simply copy latest w/o subtracting
Len Brownc98d5d92012-06-04 00:56:40 -0400689 */
Len Brown2f32edf2012-09-21 23:45:46 -0400690 old->extra_msr32 = new->extra_msr32;
691 old->extra_msr64 = new->extra_msr64;
Len Brown1ed51012013-02-10 17:19:24 -0500692
693 if (do_smi)
694 old->smi_count = new->smi_count - old->smi_count;
Len Brownc98d5d92012-06-04 00:56:40 -0400695}
696
697int delta_cpu(struct thread_data *t, struct core_data *c,
698 struct pkg_data *p, struct thread_data *t2,
699 struct core_data *c2, struct pkg_data *p2)
700{
701 /* calculate core delta only for 1st thread in core */
702 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
703 delta_core(c, c2);
704
705 /* always calculate thread delta */
706 delta_thread(t, t2, c2); /* c2 is core delta */
707
708 /* calculate package delta only for 1st core in package */
709 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
710 delta_package(p, p2);
711
712 return 0;
713}
714
715void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
716{
717 t->tsc = 0;
718 t->aperf = 0;
719 t->mperf = 0;
720 t->c1 = 0;
721
Len Brown1ed51012013-02-10 17:19:24 -0500722 t->smi_count = 0;
Len Brown8e180f32012-09-22 01:25:08 -0400723 t->extra_delta32 = 0;
724 t->extra_delta64 = 0;
725
Len Brownc98d5d92012-06-04 00:56:40 -0400726 /* tells format_counters to dump all fields from this set */
727 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
728
729 c->c3 = 0;
730 c->c6 = 0;
731 c->c7 = 0;
Len Brown889facb2012-11-08 00:48:57 -0500732 c->core_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -0400733
734 p->pc2 = 0;
735 p->pc3 = 0;
736 p->pc6 = 0;
737 p->pc7 = 0;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800738 p->pc8 = 0;
739 p->pc9 = 0;
740 p->pc10 = 0;
Len Brown889facb2012-11-08 00:48:57 -0500741
742 p->energy_pkg = 0;
743 p->energy_dram = 0;
744 p->energy_cores = 0;
745 p->energy_gfx = 0;
746 p->rapl_pkg_perf_status = 0;
747 p->rapl_dram_perf_status = 0;
748 p->pkg_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -0400749}
750int sum_counters(struct thread_data *t, struct core_data *c,
751 struct pkg_data *p)
752{
753 average.threads.tsc += t->tsc;
754 average.threads.aperf += t->aperf;
755 average.threads.mperf += t->mperf;
756 average.threads.c1 += t->c1;
757
Len Brown8e180f32012-09-22 01:25:08 -0400758 average.threads.extra_delta32 += t->extra_delta32;
759 average.threads.extra_delta64 += t->extra_delta64;
760
Len Brownc98d5d92012-06-04 00:56:40 -0400761 /* sum per-core values only for 1st thread in core */
762 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
763 return 0;
764
765 average.cores.c3 += c->c3;
766 average.cores.c6 += c->c6;
767 average.cores.c7 += c->c7;
768
Len Brown889facb2012-11-08 00:48:57 -0500769 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
770
Len Brownc98d5d92012-06-04 00:56:40 -0400771 /* sum per-pkg values only for 1st core in pkg */
772 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
773 return 0;
774
775 average.packages.pc2 += p->pc2;
776 average.packages.pc3 += p->pc3;
777 average.packages.pc6 += p->pc6;
778 average.packages.pc7 += p->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800779 average.packages.pc8 += p->pc8;
780 average.packages.pc9 += p->pc9;
781 average.packages.pc10 += p->pc10;
Len Brownc98d5d92012-06-04 00:56:40 -0400782
Len Brown889facb2012-11-08 00:48:57 -0500783 average.packages.energy_pkg += p->energy_pkg;
784 average.packages.energy_dram += p->energy_dram;
785 average.packages.energy_cores += p->energy_cores;
786 average.packages.energy_gfx += p->energy_gfx;
787
788 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
789
790 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
791 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
Len Brownc98d5d92012-06-04 00:56:40 -0400792 return 0;
793}
794/*
795 * sum the counters for all cpus in the system
796 * compute the weighted average
797 */
798void compute_average(struct thread_data *t, struct core_data *c,
799 struct pkg_data *p)
800{
801 clear_counters(&average.threads, &average.cores, &average.packages);
802
803 for_all_cpus(sum_counters, t, c, p);
804
805 average.threads.tsc /= topo.num_cpus;
806 average.threads.aperf /= topo.num_cpus;
807 average.threads.mperf /= topo.num_cpus;
808 average.threads.c1 /= topo.num_cpus;
809
Len Brown8e180f32012-09-22 01:25:08 -0400810 average.threads.extra_delta32 /= topo.num_cpus;
811 average.threads.extra_delta32 &= 0xFFFFFFFF;
812
813 average.threads.extra_delta64 /= topo.num_cpus;
814
Len Brownc98d5d92012-06-04 00:56:40 -0400815 average.cores.c3 /= topo.num_cores;
816 average.cores.c6 /= topo.num_cores;
817 average.cores.c7 /= topo.num_cores;
818
819 average.packages.pc2 /= topo.num_packages;
820 average.packages.pc3 /= topo.num_packages;
821 average.packages.pc6 /= topo.num_packages;
822 average.packages.pc7 /= topo.num_packages;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800823
824 average.packages.pc8 /= topo.num_packages;
825 average.packages.pc9 /= topo.num_packages;
826 average.packages.pc10 /= topo.num_packages;
Len Brownc98d5d92012-06-04 00:56:40 -0400827}
828
829static unsigned long long rdtsc(void)
830{
831 unsigned int low, high;
832
833 asm volatile("rdtsc" : "=a" (low), "=d" (high));
834
835 return low | ((unsigned long long)high) << 32;
836}
837
838
839/*
840 * get_counters(...)
841 * migrate to cpu
842 * acquire and record local counters for that cpu
843 */
844int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
845{
846 int cpu = t->cpu_id;
Len Brown889facb2012-11-08 00:48:57 -0500847 unsigned long long msr;
Len Brownc98d5d92012-06-04 00:56:40 -0400848
Len Browne52966c2012-11-08 22:38:05 -0500849 if (cpu_migrate(cpu)) {
850 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
Len Brownc98d5d92012-06-04 00:56:40 -0400851 return -1;
Len Browne52966c2012-11-08 22:38:05 -0500852 }
Len Brownc98d5d92012-06-04 00:56:40 -0400853
854 t->tsc = rdtsc(); /* we are running on local CPU of interest */
855
856 if (has_aperf) {
Len Brown9c63a652012-10-31 01:29:52 -0400857 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
Len Brownc98d5d92012-06-04 00:56:40 -0400858 return -3;
Len Brown9c63a652012-10-31 01:29:52 -0400859 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
Len Brownc98d5d92012-06-04 00:56:40 -0400860 return -4;
861 }
862
Len Brown1ed51012013-02-10 17:19:24 -0500863 if (do_smi) {
864 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
865 return -5;
866 t->smi_count = msr & 0xFFFFFFFF;
867 }
Len Brown8e180f32012-09-22 01:25:08 -0400868 if (extra_delta_offset32) {
Len Brown889facb2012-11-08 00:48:57 -0500869 if (get_msr(cpu, extra_delta_offset32, &msr))
Len Brown2f32edf2012-09-21 23:45:46 -0400870 return -5;
Len Brown889facb2012-11-08 00:48:57 -0500871 t->extra_delta32 = msr & 0xFFFFFFFF;
Len Brown8e180f32012-09-22 01:25:08 -0400872 }
873
874 if (extra_delta_offset64)
875 if (get_msr(cpu, extra_delta_offset64, &t->extra_delta64))
876 return -5;
877
878 if (extra_msr_offset32) {
Len Brown889facb2012-11-08 00:48:57 -0500879 if (get_msr(cpu, extra_msr_offset32, &msr))
Len Brown8e180f32012-09-22 01:25:08 -0400880 return -5;
Len Brown889facb2012-11-08 00:48:57 -0500881 t->extra_msr32 = msr & 0xFFFFFFFF;
Len Brown8e180f32012-09-22 01:25:08 -0400882 }
Len Brown2f32edf2012-09-21 23:45:46 -0400883
884 if (extra_msr_offset64)
885 if (get_msr(cpu, extra_msr_offset64, &t->extra_msr64))
Len Brownc98d5d92012-06-04 00:56:40 -0400886 return -5;
887
Len Brown144b44b2013-11-09 00:30:16 -0500888 if (use_c1_residency_msr) {
889 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
890 return -6;
891 }
892
Len Brownc98d5d92012-06-04 00:56:40 -0400893 /* collect core counters only for 1st thread in core */
894 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
895 return 0;
896
Len Brown144b44b2013-11-09 00:30:16 -0500897 if (do_nhm_cstates && !do_slm_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -0400898 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
899 return -6;
Len Brown144b44b2013-11-09 00:30:16 -0500900 }
901
902 if (do_nhm_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -0400903 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
904 return -7;
905 }
906
907 if (do_snb_cstates)
908 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
909 return -8;
910
Len Brown889facb2012-11-08 00:48:57 -0500911 if (do_dts) {
912 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
913 return -9;
914 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
915 }
916
917
Len Brownc98d5d92012-06-04 00:56:40 -0400918 /* collect package counters only for 1st core in package */
919 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
920 return 0;
921
Len Brown144b44b2013-11-09 00:30:16 -0500922 if (do_nhm_cstates && !do_slm_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -0400923 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
924 return -9;
925 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
926 return -10;
927 }
928 if (do_snb_cstates) {
929 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
930 return -11;
931 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
932 return -12;
Len Brown103a8fe2010-10-22 23:53:03 -0400933 }
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800934 if (do_c8_c9_c10) {
935 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
936 return -13;
937 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
938 return -13;
939 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
940 return -13;
941 }
Len Brown889facb2012-11-08 00:48:57 -0500942 if (do_rapl & RAPL_PKG) {
943 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
944 return -13;
945 p->energy_pkg = msr & 0xFFFFFFFF;
946 }
947 if (do_rapl & RAPL_CORES) {
948 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
949 return -14;
950 p->energy_cores = msr & 0xFFFFFFFF;
951 }
952 if (do_rapl & RAPL_DRAM) {
953 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
954 return -15;
955 p->energy_dram = msr & 0xFFFFFFFF;
956 }
957 if (do_rapl & RAPL_GFX) {
958 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
959 return -16;
960 p->energy_gfx = msr & 0xFFFFFFFF;
961 }
962 if (do_rapl & RAPL_PKG_PERF_STATUS) {
963 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
964 return -16;
965 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
966 }
967 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
968 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
969 return -16;
970 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
971 }
972 if (do_ptm) {
973 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
974 return -17;
975 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
976 }
Len Brown103a8fe2010-10-22 23:53:03 -0400977 return 0;
978}
979
Len Brownc98d5d92012-06-04 00:56:40 -0400980void print_verbose_header(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400981{
982 unsigned long long msr;
983 unsigned int ratio;
984
985 if (!do_nehalem_platform_info)
986 return;
987
Len Brown9c63a652012-10-31 01:29:52 -0400988 get_msr(0, MSR_NHM_PLATFORM_INFO, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -0400989
Len Brown67920412013-01-31 15:22:15 -0500990 fprintf(stderr, "cpu0: MSR_NHM_PLATFORM_INFO: 0x%08llx\n", msr);
Len Brown6574a5d2012-09-21 00:01:31 -0400991
Len Brown103a8fe2010-10-22 23:53:03 -0400992 ratio = (msr >> 40) & 0xFF;
993 fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency\n",
994 ratio, bclk, ratio * bclk);
995
996 ratio = (msr >> 8) & 0xFF;
997 fprintf(stderr, "%d * %.0f = %.0f MHz TSC frequency\n",
998 ratio, bclk, ratio * bclk);
999
Len Brown67920412013-01-31 15:22:15 -05001000 get_msr(0, MSR_IA32_POWER_CTL, &msr);
Len Brown144b44b2013-11-09 00:30:16 -05001001 fprintf(stderr, "cpu0: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
Len Brown67920412013-01-31 15:22:15 -05001002 msr, msr & 0x2 ? "EN" : "DIS");
1003
Len Brown6574a5d2012-09-21 00:01:31 -04001004 if (!do_ivt_turbo_ratio_limit)
1005 goto print_nhm_turbo_ratio_limits;
1006
1007 get_msr(0, MSR_IVT_TURBO_RATIO_LIMIT, &msr);
1008
Len Brown67920412013-01-31 15:22:15 -05001009 fprintf(stderr, "cpu0: MSR_IVT_TURBO_RATIO_LIMIT: 0x%08llx\n", msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001010
1011 ratio = (msr >> 56) & 0xFF;
1012 if (ratio)
1013 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 16 active cores\n",
1014 ratio, bclk, ratio * bclk);
1015
1016 ratio = (msr >> 48) & 0xFF;
1017 if (ratio)
1018 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 15 active cores\n",
1019 ratio, bclk, ratio * bclk);
1020
1021 ratio = (msr >> 40) & 0xFF;
1022 if (ratio)
1023 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 14 active cores\n",
1024 ratio, bclk, ratio * bclk);
1025
1026 ratio = (msr >> 32) & 0xFF;
1027 if (ratio)
1028 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 13 active cores\n",
1029 ratio, bclk, ratio * bclk);
1030
1031 ratio = (msr >> 24) & 0xFF;
1032 if (ratio)
1033 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 12 active cores\n",
1034 ratio, bclk, ratio * bclk);
1035
1036 ratio = (msr >> 16) & 0xFF;
1037 if (ratio)
1038 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 11 active cores\n",
1039 ratio, bclk, ratio * bclk);
1040
1041 ratio = (msr >> 8) & 0xFF;
1042 if (ratio)
1043 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 10 active cores\n",
1044 ratio, bclk, ratio * bclk);
1045
1046 ratio = (msr >> 0) & 0xFF;
1047 if (ratio)
1048 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 9 active cores\n",
1049 ratio, bclk, ratio * bclk);
1050
1051print_nhm_turbo_ratio_limits:
Len Brown889facb2012-11-08 00:48:57 -05001052 get_msr(0, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
1053
1054#define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
1055#define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
1056
1057 fprintf(stderr, "cpu0: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", msr);
1058
1059 fprintf(stderr, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: ",
1060 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
1061 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
1062 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
1063 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
1064 (msr & (1 << 15)) ? "" : "UN",
1065 (unsigned int)msr & 7);
1066
1067
1068 switch(msr & 0x7) {
1069 case 0:
Len Brown144b44b2013-11-09 00:30:16 -05001070 fprintf(stderr, do_slm_cstates ? "no pkg states" : "pc0");
Len Brown889facb2012-11-08 00:48:57 -05001071 break;
1072 case 1:
Len Brown144b44b2013-11-09 00:30:16 -05001073 fprintf(stderr, do_slm_cstates ? "no pkg states" : do_snb_cstates ? "pc2" : "pc0");
Len Brown889facb2012-11-08 00:48:57 -05001074 break;
1075 case 2:
Len Brown144b44b2013-11-09 00:30:16 -05001076 fprintf(stderr, do_slm_cstates ? "invalid" : do_snb_cstates ? "pc6-noret" : "pc3");
Len Brown889facb2012-11-08 00:48:57 -05001077 break;
1078 case 3:
Len Brown144b44b2013-11-09 00:30:16 -05001079 fprintf(stderr, do_slm_cstates ? "invalid" : "pc6");
Len Brown889facb2012-11-08 00:48:57 -05001080 break;
1081 case 4:
Len Brown144b44b2013-11-09 00:30:16 -05001082 fprintf(stderr, do_slm_cstates ? "pc4" : "pc7");
Len Brown889facb2012-11-08 00:48:57 -05001083 break;
1084 case 5:
Len Brown144b44b2013-11-09 00:30:16 -05001085 fprintf(stderr, do_slm_cstates ? "invalid" : do_snb_cstates ? "pc7s" : "invalid");
1086 break;
1087 case 6:
1088 fprintf(stderr, do_slm_cstates ? "pc6" : "invalid");
Len Brown889facb2012-11-08 00:48:57 -05001089 break;
1090 case 7:
Len Brown144b44b2013-11-09 00:30:16 -05001091 fprintf(stderr, do_slm_cstates ? "pc7" : "unlimited");
Len Brown889facb2012-11-08 00:48:57 -05001092 break;
1093 default:
1094 fprintf(stderr, "invalid");
1095 }
1096 fprintf(stderr, ")\n");
Len Brown103a8fe2010-10-22 23:53:03 -04001097
1098 if (!do_nehalem_turbo_ratio_limit)
1099 return;
1100
Len Brown9c63a652012-10-31 01:29:52 -04001101 get_msr(0, MSR_NHM_TURBO_RATIO_LIMIT, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001102
Len Brown67920412013-01-31 15:22:15 -05001103 fprintf(stderr, "cpu0: MSR_NHM_TURBO_RATIO_LIMIT: 0x%08llx\n", msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001104
1105 ratio = (msr >> 56) & 0xFF;
1106 if (ratio)
1107 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 8 active cores\n",
1108 ratio, bclk, ratio * bclk);
1109
1110 ratio = (msr >> 48) & 0xFF;
1111 if (ratio)
1112 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 7 active cores\n",
1113 ratio, bclk, ratio * bclk);
1114
1115 ratio = (msr >> 40) & 0xFF;
1116 if (ratio)
1117 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 6 active cores\n",
1118 ratio, bclk, ratio * bclk);
1119
1120 ratio = (msr >> 32) & 0xFF;
1121 if (ratio)
1122 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 5 active cores\n",
1123 ratio, bclk, ratio * bclk);
1124
Len Brown103a8fe2010-10-22 23:53:03 -04001125 ratio = (msr >> 24) & 0xFF;
1126 if (ratio)
1127 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
1128 ratio, bclk, ratio * bclk);
1129
1130 ratio = (msr >> 16) & 0xFF;
1131 if (ratio)
1132 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
1133 ratio, bclk, ratio * bclk);
1134
1135 ratio = (msr >> 8) & 0xFF;
1136 if (ratio)
1137 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
1138 ratio, bclk, ratio * bclk);
1139
1140 ratio = (msr >> 0) & 0xFF;
1141 if (ratio)
1142 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
1143 ratio, bclk, ratio * bclk);
Len Brown103a8fe2010-10-22 23:53:03 -04001144}
1145
Len Brownc98d5d92012-06-04 00:56:40 -04001146void free_all_buffers(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001147{
Len Brownc98d5d92012-06-04 00:56:40 -04001148 CPU_FREE(cpu_present_set);
1149 cpu_present_set = NULL;
1150 cpu_present_set = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001151
Len Brownc98d5d92012-06-04 00:56:40 -04001152 CPU_FREE(cpu_affinity_set);
1153 cpu_affinity_set = NULL;
1154 cpu_affinity_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001155
Len Brownc98d5d92012-06-04 00:56:40 -04001156 free(thread_even);
1157 free(core_even);
1158 free(package_even);
1159
1160 thread_even = NULL;
1161 core_even = NULL;
1162 package_even = NULL;
1163
1164 free(thread_odd);
1165 free(core_odd);
1166 free(package_odd);
1167
1168 thread_odd = NULL;
1169 core_odd = NULL;
1170 package_odd = NULL;
1171
1172 free(output_buffer);
1173 output_buffer = NULL;
1174 outp = NULL;
Len Brown103a8fe2010-10-22 23:53:03 -04001175}
1176
Len Brownc98d5d92012-06-04 00:56:40 -04001177/*
Josh Triplett95aebc42013-08-20 17:20:16 -07001178 * Parse a file containing a single int.
1179 */
1180int parse_int_file(const char *fmt, ...)
1181{
1182 va_list args;
1183 char path[PATH_MAX];
1184 FILE *filep;
1185 int value;
1186
1187 va_start(args, fmt);
1188 vsnprintf(path, sizeof(path), fmt, args);
1189 va_end(args);
1190 filep = fopen(path, "r");
1191 if (!filep) {
1192 perror(path);
1193 exit(1);
1194 }
1195 if (fscanf(filep, "%d", &value) != 1) {
1196 perror(path);
1197 exit(1);
1198 }
1199 fclose(filep);
1200 return value;
1201}
1202
1203/*
Len Brownc98d5d92012-06-04 00:56:40 -04001204 * cpu_is_first_sibling_in_core(cpu)
1205 * return 1 if given CPU is 1st HT sibling in the core
1206 */
1207int cpu_is_first_sibling_in_core(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001208{
Josh Triplett95aebc42013-08-20 17:20:16 -07001209 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001210}
1211
Len Brownc98d5d92012-06-04 00:56:40 -04001212/*
1213 * cpu_is_first_core_in_package(cpu)
1214 * return 1 if given CPU is 1st core in package
1215 */
1216int cpu_is_first_core_in_package(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001217{
Josh Triplett95aebc42013-08-20 17:20:16 -07001218 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001219}
1220
1221int get_physical_package_id(int cpu)
1222{
Josh Triplett95aebc42013-08-20 17:20:16 -07001223 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001224}
1225
1226int get_core_id(int cpu)
1227{
Josh Triplett95aebc42013-08-20 17:20:16 -07001228 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001229}
1230
Len Brownc98d5d92012-06-04 00:56:40 -04001231int get_num_ht_siblings(int cpu)
1232{
1233 char path[80];
1234 FILE *filep;
1235 int sib1, sib2;
1236 int matches;
1237 char character;
1238
1239 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
1240 filep = fopen(path, "r");
1241 if (filep == NULL) {
1242 perror(path);
1243 exit(1);
1244 }
1245 /*
1246 * file format:
1247 * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4)
1248 * otherwinse 1 sibling (self).
1249 */
1250 matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2);
1251
1252 fclose(filep);
1253
1254 if (matches == 3)
1255 return 2;
1256 else
1257 return 1;
1258}
1259
Len Brown103a8fe2010-10-22 23:53:03 -04001260/*
Len Brownc98d5d92012-06-04 00:56:40 -04001261 * run func(thread, core, package) in topology order
1262 * skip non-present cpus
Len Brown103a8fe2010-10-22 23:53:03 -04001263 */
1264
Len Brownc98d5d92012-06-04 00:56:40 -04001265int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
1266 struct pkg_data *, struct thread_data *, struct core_data *,
1267 struct pkg_data *), struct thread_data *thread_base,
1268 struct core_data *core_base, struct pkg_data *pkg_base,
1269 struct thread_data *thread_base2, struct core_data *core_base2,
1270 struct pkg_data *pkg_base2)
1271{
1272 int retval, pkg_no, core_no, thread_no;
1273
1274 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
1275 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
1276 for (thread_no = 0; thread_no <
1277 topo.num_threads_per_core; ++thread_no) {
1278 struct thread_data *t, *t2;
1279 struct core_data *c, *c2;
1280 struct pkg_data *p, *p2;
1281
1282 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
1283
1284 if (cpu_is_not_present(t->cpu_id))
1285 continue;
1286
1287 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
1288
1289 c = GET_CORE(core_base, core_no, pkg_no);
1290 c2 = GET_CORE(core_base2, core_no, pkg_no);
1291
1292 p = GET_PKG(pkg_base, pkg_no);
1293 p2 = GET_PKG(pkg_base2, pkg_no);
1294
1295 retval = func(t, c, p, t2, c2, p2);
1296 if (retval)
1297 return retval;
1298 }
1299 }
1300 }
1301 return 0;
1302}
1303
1304/*
1305 * run func(cpu) on every cpu in /proc/stat
1306 * return max_cpu number
1307 */
1308int for_all_proc_cpus(int (func)(int))
Len Brown103a8fe2010-10-22 23:53:03 -04001309{
1310 FILE *fp;
Len Brownc98d5d92012-06-04 00:56:40 -04001311 int cpu_num;
Len Brown103a8fe2010-10-22 23:53:03 -04001312 int retval;
1313
1314 fp = fopen(proc_stat, "r");
1315 if (fp == NULL) {
1316 perror(proc_stat);
1317 exit(1);
1318 }
1319
1320 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
1321 if (retval != 0) {
1322 perror("/proc/stat format");
1323 exit(1);
1324 }
1325
Len Brownc98d5d92012-06-04 00:56:40 -04001326 while (1) {
1327 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 -04001328 if (retval != 1)
1329 break;
1330
Len Brownc98d5d92012-06-04 00:56:40 -04001331 retval = func(cpu_num);
1332 if (retval) {
1333 fclose(fp);
1334 return(retval);
1335 }
Len Brown103a8fe2010-10-22 23:53:03 -04001336 }
1337 fclose(fp);
Len Brownc98d5d92012-06-04 00:56:40 -04001338 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001339}
1340
1341void re_initialize(void)
1342{
Len Brownc98d5d92012-06-04 00:56:40 -04001343 free_all_buffers();
1344 setup_all_buffers();
1345 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04001346}
1347
Len Brownc98d5d92012-06-04 00:56:40 -04001348
Len Brown103a8fe2010-10-22 23:53:03 -04001349/*
Len Brownc98d5d92012-06-04 00:56:40 -04001350 * count_cpus()
1351 * remember the last one seen, it will be the max
Len Brown103a8fe2010-10-22 23:53:03 -04001352 */
Len Brownc98d5d92012-06-04 00:56:40 -04001353int count_cpus(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001354{
Len Brownc98d5d92012-06-04 00:56:40 -04001355 if (topo.max_cpu_num < cpu)
1356 topo.max_cpu_num = cpu;
Len Brown103a8fe2010-10-22 23:53:03 -04001357
Len Brownc98d5d92012-06-04 00:56:40 -04001358 topo.num_cpus += 1;
1359 return 0;
1360}
1361int mark_cpu_present(int cpu)
1362{
1363 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brown15aaa342012-03-29 22:19:58 -04001364 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001365}
1366
1367void turbostat_loop()
1368{
Len Brownc98d5d92012-06-04 00:56:40 -04001369 int retval;
Len Browne52966c2012-11-08 22:38:05 -05001370 int restarted = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001371
Len Brown103a8fe2010-10-22 23:53:03 -04001372restart:
Len Browne52966c2012-11-08 22:38:05 -05001373 restarted++;
1374
Len Brownc98d5d92012-06-04 00:56:40 -04001375 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001376 if (retval < -1) {
1377 exit(retval);
1378 } else if (retval == -1) {
Len Browne52966c2012-11-08 22:38:05 -05001379 if (restarted > 1) {
1380 exit(retval);
1381 }
Len Brownc98d5d92012-06-04 00:56:40 -04001382 re_initialize();
1383 goto restart;
1384 }
Len Browne52966c2012-11-08 22:38:05 -05001385 restarted = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001386 gettimeofday(&tv_even, (struct timezone *)NULL);
1387
1388 while (1) {
Len Brownc98d5d92012-06-04 00:56:40 -04001389 if (for_all_proc_cpus(cpu_is_not_present)) {
Len Brown103a8fe2010-10-22 23:53:03 -04001390 re_initialize();
1391 goto restart;
1392 }
1393 sleep(interval_sec);
Len Brownc98d5d92012-06-04 00:56:40 -04001394 retval = for_all_cpus(get_counters, ODD_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001395 if (retval < -1) {
1396 exit(retval);
1397 } else if (retval == -1) {
Len Brown15aaa342012-03-29 22:19:58 -04001398 re_initialize();
1399 goto restart;
1400 }
Len Brown103a8fe2010-10-22 23:53:03 -04001401 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04001402 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04001403 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
1404 compute_average(EVEN_COUNTERS);
1405 format_all_counters(EVEN_COUNTERS);
1406 flush_stdout();
Len Brown15aaa342012-03-29 22:19:58 -04001407 sleep(interval_sec);
Len Brownc98d5d92012-06-04 00:56:40 -04001408 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001409 if (retval < -1) {
1410 exit(retval);
1411 } else if (retval == -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04001412 re_initialize();
1413 goto restart;
1414 }
Len Brown103a8fe2010-10-22 23:53:03 -04001415 gettimeofday(&tv_even, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04001416 timersub(&tv_even, &tv_odd, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04001417 for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS);
1418 compute_average(ODD_COUNTERS);
1419 format_all_counters(ODD_COUNTERS);
1420 flush_stdout();
Len Brown103a8fe2010-10-22 23:53:03 -04001421 }
1422}
1423
1424void check_dev_msr()
1425{
1426 struct stat sb;
1427
1428 if (stat("/dev/cpu/0/msr", &sb)) {
1429 fprintf(stderr, "no /dev/cpu/0/msr\n");
1430 fprintf(stderr, "Try \"# modprobe msr\"\n");
1431 exit(-5);
1432 }
1433}
1434
1435void check_super_user()
1436{
1437 if (getuid() != 0) {
1438 fprintf(stderr, "must be root\n");
1439 exit(-6);
1440 }
1441}
1442
1443int has_nehalem_turbo_ratio_limit(unsigned int family, unsigned int model)
1444{
1445 if (!genuine_intel)
1446 return 0;
1447
1448 if (family != 6)
1449 return 0;
1450
1451 switch (model) {
1452 case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
1453 case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
1454 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
1455 case 0x25: /* Westmere Client - Clarkdale, Arrandale */
1456 case 0x2C: /* Westmere EP - Gulftown */
1457 case 0x2A: /* SNB */
1458 case 0x2D: /* SNB Xeon */
Len Brown553575f2011-11-18 03:32:01 -05001459 case 0x3A: /* IVB */
Len Brown13006512012-09-26 18:11:31 -04001460 case 0x3E: /* IVB Xeon */
Len Brown70b43402013-01-08 01:26:07 -05001461 case 0x3C: /* HSW */
1462 case 0x3F: /* HSW */
1463 case 0x45: /* HSW */
Len Brown149c2312013-03-15 10:58:02 -04001464 case 0x46: /* HSW */
Len Brown144b44b2013-11-09 00:30:16 -05001465 case 0x37: /* BYT */
1466 case 0x4D: /* AVN */
Len Brown103a8fe2010-10-22 23:53:03 -04001467 return 1;
1468 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1469 case 0x2F: /* Westmere-EX Xeon - Eagleton */
1470 default:
1471 return 0;
1472 }
1473}
Len Brown6574a5d2012-09-21 00:01:31 -04001474int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
1475{
1476 if (!genuine_intel)
1477 return 0;
1478
1479 if (family != 6)
1480 return 0;
1481
1482 switch (model) {
1483 case 0x3E: /* IVB Xeon */
1484 return 1;
1485 default:
1486 return 0;
1487 }
1488}
1489
Len Brown889facb2012-11-08 00:48:57 -05001490/*
1491 * print_epb()
1492 * Decode the ENERGY_PERF_BIAS MSR
1493 */
1494int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1495{
1496 unsigned long long msr;
1497 char *epb_string;
1498 int cpu;
1499
1500 if (!has_epb)
1501 return 0;
1502
1503 cpu = t->cpu_id;
1504
1505 /* EPB is per-package */
1506 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1507 return 0;
1508
1509 if (cpu_migrate(cpu)) {
1510 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1511 return -1;
1512 }
1513
1514 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
1515 return 0;
1516
1517 switch (msr & 0x7) {
1518 case ENERGY_PERF_BIAS_PERFORMANCE:
1519 epb_string = "performance";
1520 break;
1521 case ENERGY_PERF_BIAS_NORMAL:
1522 epb_string = "balanced";
1523 break;
1524 case ENERGY_PERF_BIAS_POWERSAVE:
1525 epb_string = "powersave";
1526 break;
1527 default:
1528 epb_string = "custom";
1529 break;
1530 }
1531 fprintf(stderr, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
1532
1533 return 0;
1534}
1535
1536#define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
1537#define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
1538
Len Brown144b44b2013-11-09 00:30:16 -05001539double get_tdp(model)
1540{
1541 unsigned long long msr;
1542
1543 if (do_rapl & RAPL_PKG_POWER_INFO)
1544 if (!get_msr(0, MSR_PKG_POWER_INFO, &msr))
1545 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
1546
1547 switch (model) {
1548 case 0x37:
1549 case 0x4D:
1550 return 30.0;
1551 default:
1552 return 135.0;
1553 }
1554}
1555
1556
Len Brown889facb2012-11-08 00:48:57 -05001557/*
1558 * rapl_probe()
1559 *
Len Brown144b44b2013-11-09 00:30:16 -05001560 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
Len Brown889facb2012-11-08 00:48:57 -05001561 */
1562void rapl_probe(unsigned int family, unsigned int model)
1563{
1564 unsigned long long msr;
Len Brown144b44b2013-11-09 00:30:16 -05001565 unsigned int time_unit;
Len Brown889facb2012-11-08 00:48:57 -05001566 double tdp;
1567
1568 if (!genuine_intel)
1569 return;
1570
1571 if (family != 6)
1572 return;
1573
1574 switch (model) {
1575 case 0x2A:
1576 case 0x3A:
Len Brown70b43402013-01-08 01:26:07 -05001577 case 0x3C: /* HSW */
1578 case 0x3F: /* HSW */
1579 case 0x45: /* HSW */
Len Brown149c2312013-03-15 10:58:02 -04001580 case 0x46: /* HSW */
Len Brown144b44b2013-11-09 00:30:16 -05001581 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
Len Brown889facb2012-11-08 00:48:57 -05001582 break;
1583 case 0x2D:
1584 case 0x3E:
Len Brown144b44b2013-11-09 00:30:16 -05001585 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_PKG_PERF_STATUS | RAPL_DRAM_PERF_STATUS | RAPL_PKG_POWER_INFO;
1586 break;
1587 case 0x37: /* BYT */
1588 case 0x4D: /* AVN */
1589 do_rapl = RAPL_PKG | RAPL_CORES ;
Len Brown889facb2012-11-08 00:48:57 -05001590 break;
1591 default:
1592 return;
1593 }
1594
1595 /* units on package 0, verify later other packages match */
1596 if (get_msr(0, MSR_RAPL_POWER_UNIT, &msr))
1597 return;
1598
1599 rapl_power_units = 1.0 / (1 << (msr & 0xF));
Len Brown144b44b2013-11-09 00:30:16 -05001600 if (model == 0x37)
1601 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
1602 else
1603 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
Len Brown889facb2012-11-08 00:48:57 -05001604
Len Brown144b44b2013-11-09 00:30:16 -05001605 time_unit = msr >> 16 & 0xF;
1606 if (time_unit == 0)
1607 time_unit = 0xA;
Len Brown889facb2012-11-08 00:48:57 -05001608
Len Brown144b44b2013-11-09 00:30:16 -05001609 rapl_time_units = 1.0 / (1 << (time_unit));
1610
1611 tdp = get_tdp(model);
Len Brown889facb2012-11-08 00:48:57 -05001612
1613 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
Len Brown889facb2012-11-08 00:48:57 -05001614 if (verbose)
Len Brown144b44b2013-11-09 00:30:16 -05001615 fprintf(stderr, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
Len Brown889facb2012-11-08 00:48:57 -05001616
1617 return;
1618}
1619
1620int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1621{
1622 unsigned long long msr;
1623 unsigned int dts;
1624 int cpu;
1625
1626 if (!(do_dts || do_ptm))
1627 return 0;
1628
1629 cpu = t->cpu_id;
1630
1631 /* DTS is per-core, no need to print for each thread */
1632 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1633 return 0;
1634
1635 if (cpu_migrate(cpu)) {
1636 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1637 return -1;
1638 }
1639
1640 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
1641 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1642 return 0;
1643
1644 dts = (msr >> 16) & 0x7F;
1645 fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
1646 cpu, msr, tcc_activation_temp - dts);
1647
1648#ifdef THERM_DEBUG
1649 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
1650 return 0;
1651
1652 dts = (msr >> 16) & 0x7F;
1653 dts2 = (msr >> 8) & 0x7F;
1654 fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
1655 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
1656#endif
1657 }
1658
1659
1660 if (do_dts) {
1661 unsigned int resolution;
1662
1663 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1664 return 0;
1665
1666 dts = (msr >> 16) & 0x7F;
1667 resolution = (msr >> 27) & 0xF;
1668 fprintf(stderr, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
1669 cpu, msr, tcc_activation_temp - dts, resolution);
1670
1671#ifdef THERM_DEBUG
1672 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
1673 return 0;
1674
1675 dts = (msr >> 16) & 0x7F;
1676 dts2 = (msr >> 8) & 0x7F;
1677 fprintf(stderr, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
1678 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
1679#endif
1680 }
1681
1682 return 0;
1683}
1684
1685void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
1686{
1687 fprintf(stderr, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
1688 cpu, label,
1689 ((msr >> 15) & 1) ? "EN" : "DIS",
1690 ((msr >> 0) & 0x7FFF) * rapl_power_units,
1691 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
1692 (((msr >> 16) & 1) ? "EN" : "DIS"));
1693
1694 return;
1695}
1696
1697int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1698{
1699 unsigned long long msr;
1700 int cpu;
Len Brown889facb2012-11-08 00:48:57 -05001701
1702 if (!do_rapl)
1703 return 0;
1704
1705 /* RAPL counters are per package, so print only for 1st thread/package */
1706 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1707 return 0;
1708
1709 cpu = t->cpu_id;
1710 if (cpu_migrate(cpu)) {
1711 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1712 return -1;
1713 }
1714
1715 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
1716 return -1;
1717
Len Brown889facb2012-11-08 00:48:57 -05001718 if (verbose) {
1719 fprintf(stderr, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx "
1720 "(%f Watts, %f Joules, %f sec.)\n", cpu, msr,
Len Brown144b44b2013-11-09 00:30:16 -05001721 rapl_power_units, rapl_energy_units, rapl_time_units);
Len Brown889facb2012-11-08 00:48:57 -05001722 }
Len Brown144b44b2013-11-09 00:30:16 -05001723 if (do_rapl & RAPL_PKG_POWER_INFO) {
1724
Len Brown889facb2012-11-08 00:48:57 -05001725 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
1726 return -5;
1727
1728
1729 fprintf(stderr, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
1730 cpu, msr,
1731 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1732 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1733 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1734 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
1735
Len Brown144b44b2013-11-09 00:30:16 -05001736 }
1737 if (do_rapl & RAPL_PKG) {
1738
Len Brown889facb2012-11-08 00:48:57 -05001739 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
1740 return -9;
1741
1742 fprintf(stderr, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
1743 cpu, msr, (msr >> 63) & 1 ? "": "UN");
1744
1745 print_power_limit_msr(cpu, msr, "PKG Limit #1");
1746 fprintf(stderr, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
1747 cpu,
1748 ((msr >> 47) & 1) ? "EN" : "DIS",
1749 ((msr >> 32) & 0x7FFF) * rapl_power_units,
1750 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
1751 ((msr >> 48) & 1) ? "EN" : "DIS");
1752 }
1753
1754 if (do_rapl & RAPL_DRAM) {
1755 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
1756 return -6;
1757
1758
1759 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
1760 cpu, msr,
1761 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1762 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1763 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1764 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
1765
1766
1767 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
1768 return -9;
1769 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
1770 cpu, msr, (msr >> 31) & 1 ? "": "UN");
1771
1772 print_power_limit_msr(cpu, msr, "DRAM Limit");
1773 }
Len Brown144b44b2013-11-09 00:30:16 -05001774 if (do_rapl & RAPL_CORE_POLICY) {
Len Brown889facb2012-11-08 00:48:57 -05001775 if (verbose) {
1776 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
1777 return -7;
1778
1779 fprintf(stderr, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown144b44b2013-11-09 00:30:16 -05001780 }
1781 }
1782 if (do_rapl & RAPL_CORES) {
1783 if (verbose) {
Len Brown889facb2012-11-08 00:48:57 -05001784
1785 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
1786 return -9;
1787 fprintf(stderr, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
1788 cpu, msr, (msr >> 31) & 1 ? "": "UN");
1789 print_power_limit_msr(cpu, msr, "Cores Limit");
1790 }
1791 }
1792 if (do_rapl & RAPL_GFX) {
1793 if (verbose) {
1794 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
1795 return -8;
1796
1797 fprintf(stderr, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
1798
1799 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
1800 return -9;
1801 fprintf(stderr, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
1802 cpu, msr, (msr >> 31) & 1 ? "": "UN");
1803 print_power_limit_msr(cpu, msr, "GFX Limit");
1804 }
1805 }
1806 return 0;
1807}
1808
Len Brown103a8fe2010-10-22 23:53:03 -04001809
1810int is_snb(unsigned int family, unsigned int model)
1811{
1812 if (!genuine_intel)
1813 return 0;
1814
1815 switch (model) {
1816 case 0x2A:
1817 case 0x2D:
Len Brown650a37f2012-06-03 23:34:44 -04001818 case 0x3A: /* IVB */
Len Brown13006512012-09-26 18:11:31 -04001819 case 0x3E: /* IVB Xeon */
Len Brown70b43402013-01-08 01:26:07 -05001820 case 0x3C: /* HSW */
1821 case 0x3F: /* HSW */
1822 case 0x45: /* HSW */
Len Brown149c2312013-03-15 10:58:02 -04001823 case 0x46: /* HSW */
Len Brown103a8fe2010-10-22 23:53:03 -04001824 return 1;
1825 }
1826 return 0;
1827}
1828
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001829int has_c8_c9_c10(unsigned int family, unsigned int model)
1830{
1831 if (!genuine_intel)
1832 return 0;
1833
1834 switch (model) {
1835 case 0x45:
1836 return 1;
1837 }
1838 return 0;
1839}
1840
1841
Len Brown144b44b2013-11-09 00:30:16 -05001842int is_slm(unsigned int family, unsigned int model)
1843{
1844 if (!genuine_intel)
1845 return 0;
1846 switch (model) {
1847 case 0x37: /* BYT */
1848 case 0x4D: /* AVN */
1849 return 1;
1850 }
1851 return 0;
1852}
1853
1854#define SLM_BCLK_FREQS 5
1855double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
1856
1857double slm_bclk(void)
1858{
1859 unsigned long long msr = 3;
1860 unsigned int i;
1861 double freq;
1862
1863 if (get_msr(0, MSR_FSB_FREQ, &msr))
1864 fprintf(stderr, "SLM BCLK: unknown\n");
1865
1866 i = msr & 0xf;
1867 if (i >= SLM_BCLK_FREQS) {
1868 fprintf(stderr, "SLM BCLK[%d] invalid\n", i);
1869 msr = 3;
1870 }
1871 freq = slm_freq_table[i];
1872
1873 fprintf(stderr, "SLM BCLK: %.1f Mhz\n", freq);
1874
1875 return freq;
1876}
1877
Len Brown103a8fe2010-10-22 23:53:03 -04001878double discover_bclk(unsigned int family, unsigned int model)
1879{
1880 if (is_snb(family, model))
1881 return 100.00;
Len Brown144b44b2013-11-09 00:30:16 -05001882 else if (is_slm(family, model))
1883 return slm_bclk();
Len Brown103a8fe2010-10-22 23:53:03 -04001884 else
1885 return 133.33;
1886}
1887
Len Brown889facb2012-11-08 00:48:57 -05001888/*
1889 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
1890 * the Thermal Control Circuit (TCC) activates.
1891 * This is usually equal to tjMax.
1892 *
1893 * Older processors do not have this MSR, so there we guess,
1894 * but also allow cmdline over-ride with -T.
1895 *
1896 * Several MSR temperature values are in units of degrees-C
1897 * below this value, including the Digital Thermal Sensor (DTS),
1898 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
1899 */
1900int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1901{
1902 unsigned long long msr;
1903 unsigned int target_c_local;
1904 int cpu;
1905
1906 /* tcc_activation_temp is used only for dts or ptm */
1907 if (!(do_dts || do_ptm))
1908 return 0;
1909
1910 /* this is a per-package concept */
1911 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1912 return 0;
1913
1914 cpu = t->cpu_id;
1915 if (cpu_migrate(cpu)) {
1916 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1917 return -1;
1918 }
1919
1920 if (tcc_activation_temp_override != 0) {
1921 tcc_activation_temp = tcc_activation_temp_override;
1922 fprintf(stderr, "cpu%d: Using cmdline TCC Target (%d C)\n",
1923 cpu, tcc_activation_temp);
1924 return 0;
1925 }
1926
1927 /* Temperature Target MSR is Nehalem and newer only */
1928 if (!do_nehalem_platform_info)
1929 goto guess;
1930
1931 if (get_msr(0, MSR_IA32_TEMPERATURE_TARGET, &msr))
1932 goto guess;
1933
1934 target_c_local = (msr >> 16) & 0x7F;
1935
1936 if (verbose)
1937 fprintf(stderr, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
1938 cpu, msr, target_c_local);
1939
Len Brown144b44b2013-11-09 00:30:16 -05001940 if (target_c_local < 85 || target_c_local > 127)
Len Brown889facb2012-11-08 00:48:57 -05001941 goto guess;
1942
1943 tcc_activation_temp = target_c_local;
1944
1945 return 0;
1946
1947guess:
1948 tcc_activation_temp = TJMAX_DEFAULT;
1949 fprintf(stderr, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
1950 cpu, tcc_activation_temp);
1951
1952 return 0;
1953}
Len Brown103a8fe2010-10-22 23:53:03 -04001954void check_cpuid()
1955{
1956 unsigned int eax, ebx, ecx, edx, max_level;
1957 unsigned int fms, family, model, stepping;
1958
1959 eax = ebx = ecx = edx = 0;
1960
Josh Triplett2b928652013-08-20 17:20:14 -07001961 __get_cpuid(0, &max_level, &ebx, &ecx, &edx);
Len Brown103a8fe2010-10-22 23:53:03 -04001962
1963 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
1964 genuine_intel = 1;
1965
1966 if (verbose)
Len Brown889facb2012-11-08 00:48:57 -05001967 fprintf(stderr, "CPUID(0): %.4s%.4s%.4s ",
Len Brown103a8fe2010-10-22 23:53:03 -04001968 (char *)&ebx, (char *)&edx, (char *)&ecx);
1969
Josh Triplett2b928652013-08-20 17:20:14 -07001970 __get_cpuid(1, &fms, &ebx, &ecx, &edx);
Len Brown103a8fe2010-10-22 23:53:03 -04001971 family = (fms >> 8) & 0xf;
1972 model = (fms >> 4) & 0xf;
1973 stepping = fms & 0xf;
1974 if (family == 6 || family == 0xf)
1975 model += ((fms >> 16) & 0xf) << 4;
1976
1977 if (verbose)
1978 fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
1979 max_level, family, model, stepping, family, model, stepping);
1980
1981 if (!(edx & (1 << 5))) {
1982 fprintf(stderr, "CPUID: no MSR\n");
1983 exit(1);
1984 }
1985
1986 /*
1987 * check max extended function levels of CPUID.
1988 * This is needed to check for invariant TSC.
1989 * This check is valid for both Intel and AMD.
1990 */
1991 ebx = ecx = edx = 0;
Josh Triplett2b928652013-08-20 17:20:14 -07001992 __get_cpuid(0x80000000, &max_level, &ebx, &ecx, &edx);
Len Brown103a8fe2010-10-22 23:53:03 -04001993
1994 if (max_level < 0x80000007) {
1995 fprintf(stderr, "CPUID: no invariant TSC (max_level 0x%x)\n", max_level);
1996 exit(1);
1997 }
1998
1999 /*
2000 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
2001 * this check is valid for both Intel and AMD
2002 */
Josh Triplett2b928652013-08-20 17:20:14 -07002003 __get_cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
Thomas Renninger8209e052011-01-21 15:11:19 +01002004 has_invariant_tsc = edx & (1 << 8);
Len Brown103a8fe2010-10-22 23:53:03 -04002005
2006 if (!has_invariant_tsc) {
2007 fprintf(stderr, "No invariant TSC\n");
2008 exit(1);
2009 }
2010
2011 /*
2012 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
2013 * this check is valid for both Intel and AMD
2014 */
2015
Josh Triplett2b928652013-08-20 17:20:14 -07002016 __get_cpuid(0x6, &eax, &ebx, &ecx, &edx);
Thomas Renninger8209e052011-01-21 15:11:19 +01002017 has_aperf = ecx & (1 << 0);
Len Brown889facb2012-11-08 00:48:57 -05002018 do_dts = eax & (1 << 0);
2019 do_ptm = eax & (1 << 6);
2020 has_epb = ecx & (1 << 3);
2021
2022 if (verbose)
2023 fprintf(stderr, "CPUID(6): %s%s%s%s\n",
2024 has_aperf ? "APERF" : "No APERF!",
2025 do_dts ? ", DTS" : "",
2026 do_ptm ? ", PTM": "",
2027 has_epb ? ", EPB": "");
2028
2029 if (!has_aperf)
2030 exit(-1);
Len Brown103a8fe2010-10-22 23:53:03 -04002031
2032 do_nehalem_platform_info = genuine_intel && has_invariant_tsc;
2033 do_nhm_cstates = genuine_intel; /* all Intel w/ non-stop TSC have NHM counters */
Len Brown1ed51012013-02-10 17:19:24 -05002034 do_smi = do_nhm_cstates;
Len Brown103a8fe2010-10-22 23:53:03 -04002035 do_snb_cstates = is_snb(family, model);
Kristen Carlson Accardica587102012-11-21 05:22:43 -08002036 do_c8_c9_c10 = has_c8_c9_c10(family, model);
Len Brown144b44b2013-11-09 00:30:16 -05002037 do_slm_cstates = is_slm(family, model);
Len Brown103a8fe2010-10-22 23:53:03 -04002038 bclk = discover_bclk(family, model);
2039
2040 do_nehalem_turbo_ratio_limit = has_nehalem_turbo_ratio_limit(family, model);
Len Brown6574a5d2012-09-21 00:01:31 -04002041 do_ivt_turbo_ratio_limit = has_ivt_turbo_ratio_limit(family, model);
Len Brown889facb2012-11-08 00:48:57 -05002042 rapl_probe(family, model);
2043
2044 return;
Len Brown103a8fe2010-10-22 23:53:03 -04002045}
2046
2047
2048void usage()
2049{
Len Brown889facb2012-11-08 00:48:57 -05002050 fprintf(stderr, "%s: [-v][-R][-T][-p|-P|-S][-c MSR# | -s]][-C MSR#][-m MSR#][-M MSR#][-i interval_sec | command ...]\n",
Len Brown103a8fe2010-10-22 23:53:03 -04002051 progname);
2052 exit(1);
2053}
2054
2055
2056/*
2057 * in /dev/cpu/ return success for names that are numbers
2058 * ie. filter out ".", "..", "microcode".
2059 */
2060int dir_filter(const struct dirent *dirp)
2061{
2062 if (isdigit(dirp->d_name[0]))
2063 return 1;
2064 else
2065 return 0;
2066}
2067
2068int open_dev_cpu_msr(int dummy1)
2069{
2070 return 0;
2071}
2072
Len Brownc98d5d92012-06-04 00:56:40 -04002073void topology_probe()
2074{
2075 int i;
2076 int max_core_id = 0;
2077 int max_package_id = 0;
2078 int max_siblings = 0;
2079 struct cpu_topology {
2080 int core_id;
2081 int physical_package_id;
2082 } *cpus;
2083
2084 /* Initialize num_cpus, max_cpu_num */
2085 topo.num_cpus = 0;
2086 topo.max_cpu_num = 0;
2087 for_all_proc_cpus(count_cpus);
2088 if (!summary_only && topo.num_cpus > 1)
2089 show_cpu = 1;
2090
2091 if (verbose > 1)
2092 fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
2093
2094 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
2095 if (cpus == NULL) {
2096 perror("calloc cpus");
2097 exit(1);
2098 }
2099
2100 /*
2101 * Allocate and initialize cpu_present_set
2102 */
2103 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
2104 if (cpu_present_set == NULL) {
2105 perror("CPU_ALLOC");
2106 exit(3);
2107 }
2108 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2109 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
2110 for_all_proc_cpus(mark_cpu_present);
2111
2112 /*
2113 * Allocate and initialize cpu_affinity_set
2114 */
2115 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
2116 if (cpu_affinity_set == NULL) {
2117 perror("CPU_ALLOC");
2118 exit(3);
2119 }
2120 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2121 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
2122
2123
2124 /*
2125 * For online cpus
2126 * find max_core_id, max_package_id
2127 */
2128 for (i = 0; i <= topo.max_cpu_num; ++i) {
2129 int siblings;
2130
2131 if (cpu_is_not_present(i)) {
2132 if (verbose > 1)
2133 fprintf(stderr, "cpu%d NOT PRESENT\n", i);
2134 continue;
2135 }
2136 cpus[i].core_id = get_core_id(i);
2137 if (cpus[i].core_id > max_core_id)
2138 max_core_id = cpus[i].core_id;
2139
2140 cpus[i].physical_package_id = get_physical_package_id(i);
2141 if (cpus[i].physical_package_id > max_package_id)
2142 max_package_id = cpus[i].physical_package_id;
2143
2144 siblings = get_num_ht_siblings(i);
2145 if (siblings > max_siblings)
2146 max_siblings = siblings;
2147 if (verbose > 1)
2148 fprintf(stderr, "cpu %d pkg %d core %d\n",
2149 i, cpus[i].physical_package_id, cpus[i].core_id);
2150 }
2151 topo.num_cores_per_pkg = max_core_id + 1;
2152 if (verbose > 1)
2153 fprintf(stderr, "max_core_id %d, sizing for %d cores per package\n",
2154 max_core_id, topo.num_cores_per_pkg);
2155 if (!summary_only && topo.num_cores_per_pkg > 1)
2156 show_core = 1;
2157
2158 topo.num_packages = max_package_id + 1;
2159 if (verbose > 1)
2160 fprintf(stderr, "max_package_id %d, sizing for %d packages\n",
2161 max_package_id, topo.num_packages);
2162 if (!summary_only && topo.num_packages > 1)
2163 show_pkg = 1;
2164
2165 topo.num_threads_per_core = max_siblings;
2166 if (verbose > 1)
2167 fprintf(stderr, "max_siblings %d\n", max_siblings);
2168
2169 free(cpus);
2170}
2171
2172void
2173allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
2174{
2175 int i;
2176
2177 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
2178 topo.num_packages, sizeof(struct thread_data));
2179 if (*t == NULL)
2180 goto error;
2181
2182 for (i = 0; i < topo.num_threads_per_core *
2183 topo.num_cores_per_pkg * topo.num_packages; i++)
2184 (*t)[i].cpu_id = -1;
2185
2186 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
2187 sizeof(struct core_data));
2188 if (*c == NULL)
2189 goto error;
2190
2191 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
2192 (*c)[i].core_id = -1;
2193
2194 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
2195 if (*p == NULL)
2196 goto error;
2197
2198 for (i = 0; i < topo.num_packages; i++)
2199 (*p)[i].package_id = i;
2200
2201 return;
2202error:
2203 perror("calloc counters");
2204 exit(1);
2205}
2206/*
2207 * init_counter()
2208 *
2209 * set cpu_id, core_num, pkg_num
2210 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
2211 *
2212 * increment topo.num_cores when 1st core in pkg seen
2213 */
2214void init_counter(struct thread_data *thread_base, struct core_data *core_base,
2215 struct pkg_data *pkg_base, int thread_num, int core_num,
2216 int pkg_num, int cpu_id)
2217{
2218 struct thread_data *t;
2219 struct core_data *c;
2220 struct pkg_data *p;
2221
2222 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
2223 c = GET_CORE(core_base, core_num, pkg_num);
2224 p = GET_PKG(pkg_base, pkg_num);
2225
2226 t->cpu_id = cpu_id;
2227 if (thread_num == 0) {
2228 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
2229 if (cpu_is_first_core_in_package(cpu_id))
2230 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
2231 }
2232
2233 c->core_id = core_num;
2234 p->package_id = pkg_num;
2235}
2236
2237
2238int initialize_counters(int cpu_id)
2239{
2240 int my_thread_id, my_core_id, my_package_id;
2241
2242 my_package_id = get_physical_package_id(cpu_id);
2243 my_core_id = get_core_id(cpu_id);
2244
2245 if (cpu_is_first_sibling_in_core(cpu_id)) {
2246 my_thread_id = 0;
2247 topo.num_cores++;
2248 } else {
2249 my_thread_id = 1;
2250 }
2251
2252 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2253 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2254 return 0;
2255}
2256
2257void allocate_output_buffer()
2258{
Josh Triplettb844db32013-06-12 17:26:37 -07002259 output_buffer = calloc(1, (1 + topo.num_cpus) * 256);
Len Brownc98d5d92012-06-04 00:56:40 -04002260 outp = output_buffer;
2261 if (outp == NULL) {
2262 perror("calloc");
2263 exit(-1);
2264 }
2265}
2266
2267void setup_all_buffers(void)
2268{
2269 topology_probe();
2270 allocate_counters(&thread_even, &core_even, &package_even);
2271 allocate_counters(&thread_odd, &core_odd, &package_odd);
2272 allocate_output_buffer();
2273 for_all_proc_cpus(initialize_counters);
2274}
Len Brown103a8fe2010-10-22 23:53:03 -04002275void turbostat_init()
2276{
2277 check_cpuid();
2278
2279 check_dev_msr();
2280 check_super_user();
2281
Len Brownc98d5d92012-06-04 00:56:40 -04002282 setup_all_buffers();
Len Brown103a8fe2010-10-22 23:53:03 -04002283
2284 if (verbose)
Len Brownc98d5d92012-06-04 00:56:40 -04002285 print_verbose_header();
Len Brown889facb2012-11-08 00:48:57 -05002286
2287 if (verbose)
2288 for_all_cpus(print_epb, ODD_COUNTERS);
2289
2290 if (verbose)
2291 for_all_cpus(print_rapl, ODD_COUNTERS);
2292
2293 for_all_cpus(set_temperature_target, ODD_COUNTERS);
2294
2295 if (verbose)
2296 for_all_cpus(print_thermal, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04002297}
2298
2299int fork_it(char **argv)
2300{
Len Brown103a8fe2010-10-22 23:53:03 -04002301 pid_t child_pid;
Len Brownd91bb172012-11-01 00:08:19 -04002302 int status;
Len Brownd15cf7c2012-06-03 23:24:00 -04002303
Len Brownd91bb172012-11-01 00:08:19 -04002304 status = for_all_cpus(get_counters, EVEN_COUNTERS);
2305 if (status)
2306 exit(status);
Len Brownc98d5d92012-06-04 00:56:40 -04002307 /* clear affinity side-effect of get_counters() */
2308 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
Len Brown103a8fe2010-10-22 23:53:03 -04002309 gettimeofday(&tv_even, (struct timezone *)NULL);
2310
2311 child_pid = fork();
2312 if (!child_pid) {
2313 /* child */
2314 execvp(argv[0], argv);
2315 } else {
Len Brown103a8fe2010-10-22 23:53:03 -04002316
2317 /* parent */
2318 if (child_pid == -1) {
2319 perror("fork");
2320 exit(1);
2321 }
2322
2323 signal(SIGINT, SIG_IGN);
2324 signal(SIGQUIT, SIG_IGN);
2325 if (waitpid(child_pid, &status, 0) == -1) {
2326 perror("wait");
Len Brownd91bb172012-11-01 00:08:19 -04002327 exit(status);
Len Brown103a8fe2010-10-22 23:53:03 -04002328 }
2329 }
Len Brownc98d5d92012-06-04 00:56:40 -04002330 /*
2331 * n.b. fork_it() does not check for errors from for_all_cpus()
2332 * because re-starting is problematic when forking
2333 */
2334 for_all_cpus(get_counters, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04002335 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002336 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04002337 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
2338 compute_average(EVEN_COUNTERS);
2339 format_all_counters(EVEN_COUNTERS);
2340 flush_stderr();
Len Brown103a8fe2010-10-22 23:53:03 -04002341
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07002342 fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
Len Brown103a8fe2010-10-22 23:53:03 -04002343
Len Brownd91bb172012-11-01 00:08:19 -04002344 return status;
Len Brown103a8fe2010-10-22 23:53:03 -04002345}
2346
2347void cmdline(int argc, char **argv)
2348{
2349 int opt;
2350
2351 progname = argv[0];
2352
Len Brown889facb2012-11-08 00:48:57 -05002353 while ((opt = getopt(argc, argv, "+pPSvi:sc:sC:m:M:RT:")) != -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04002354 switch (opt) {
Len Brownf9240812012-10-06 15:26:31 -04002355 case 'p':
Len Brownc98d5d92012-06-04 00:56:40 -04002356 show_core_only++;
2357 break;
Len Brownf9240812012-10-06 15:26:31 -04002358 case 'P':
Len Brownc98d5d92012-06-04 00:56:40 -04002359 show_pkg_only++;
2360 break;
Len Brownf9240812012-10-06 15:26:31 -04002361 case 'S':
Len Browne23da032012-02-06 18:37:16 -05002362 summary_only++;
2363 break;
Len Brown103a8fe2010-10-22 23:53:03 -04002364 case 'v':
2365 verbose++;
2366 break;
2367 case 'i':
2368 interval_sec = atoi(optarg);
2369 break;
Len Brownf9240812012-10-06 15:26:31 -04002370 case 'c':
Len Brown8e180f32012-09-22 01:25:08 -04002371 sscanf(optarg, "%x", &extra_delta_offset32);
2372 break;
Len Brownf9240812012-10-06 15:26:31 -04002373 case 'C':
Len Brown8e180f32012-09-22 01:25:08 -04002374 sscanf(optarg, "%x", &extra_delta_offset64);
2375 break;
Len Brown2f32edf2012-09-21 23:45:46 -04002376 case 'm':
2377 sscanf(optarg, "%x", &extra_msr_offset32);
Len Brown2f32edf2012-09-21 23:45:46 -04002378 break;
2379 case 'M':
2380 sscanf(optarg, "%x", &extra_msr_offset64);
Len Brown103a8fe2010-10-22 23:53:03 -04002381 break;
Len Brown889facb2012-11-08 00:48:57 -05002382 case 'R':
2383 rapl_verbose++;
2384 break;
2385 case 'T':
2386 tcc_activation_temp_override = atoi(optarg);
2387 break;
Len Brown103a8fe2010-10-22 23:53:03 -04002388 default:
2389 usage();
2390 }
2391 }
2392}
2393
2394int main(int argc, char **argv)
2395{
2396 cmdline(argc, argv);
2397
Len Brown889facb2012-11-08 00:48:57 -05002398 if (verbose)
Len Brown144b44b2013-11-09 00:30:16 -05002399 fprintf(stderr, "turbostat v3.5 April 26, 2013"
Len Brown103a8fe2010-10-22 23:53:03 -04002400 " - Len Brown <lenb@kernel.org>\n");
Len Brown103a8fe2010-10-22 23:53:03 -04002401
2402 turbostat_init();
2403
2404 /*
2405 * if any params left, it must be a command to fork
2406 */
2407 if (argc - optind)
2408 return fork_it(argv + optind);
2409 else
2410 turbostat_loop();
2411
2412 return 0;
2413}