blob: de634c8228b424cda4e50b68e02d62d1e14d29e9 [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 Triplett57a42a32013-08-20 17:20:17 -07001178 * Open a file, and exit on failure
1179 */
1180FILE *fopen_or_die(const char *path, const char *mode)
1181{
1182 FILE *filep = fopen(path, "r");
1183 if (!filep) {
1184 perror(path);
1185 exit(1);
1186 }
1187 return filep;
1188}
1189
1190/*
Josh Triplett95aebc42013-08-20 17:20:16 -07001191 * Parse a file containing a single int.
1192 */
1193int parse_int_file(const char *fmt, ...)
1194{
1195 va_list args;
1196 char path[PATH_MAX];
1197 FILE *filep;
1198 int value;
1199
1200 va_start(args, fmt);
1201 vsnprintf(path, sizeof(path), fmt, args);
1202 va_end(args);
Josh Triplett57a42a32013-08-20 17:20:17 -07001203 filep = fopen_or_die(path, "r");
Josh Triplett95aebc42013-08-20 17:20:16 -07001204 if (fscanf(filep, "%d", &value) != 1) {
1205 perror(path);
1206 exit(1);
1207 }
1208 fclose(filep);
1209 return value;
1210}
1211
1212/*
Len Brownc98d5d92012-06-04 00:56:40 -04001213 * cpu_is_first_sibling_in_core(cpu)
1214 * return 1 if given CPU is 1st HT sibling in the core
1215 */
1216int cpu_is_first_sibling_in_core(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/thread_siblings_list", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001219}
1220
Len Brownc98d5d92012-06-04 00:56:40 -04001221/*
1222 * cpu_is_first_core_in_package(cpu)
1223 * return 1 if given CPU is 1st core in package
1224 */
1225int cpu_is_first_core_in_package(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001226{
Josh Triplett95aebc42013-08-20 17:20:16 -07001227 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001228}
1229
1230int get_physical_package_id(int cpu)
1231{
Josh Triplett95aebc42013-08-20 17:20:16 -07001232 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001233}
1234
1235int get_core_id(int cpu)
1236{
Josh Triplett95aebc42013-08-20 17:20:16 -07001237 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001238}
1239
Len Brownc98d5d92012-06-04 00:56:40 -04001240int get_num_ht_siblings(int cpu)
1241{
1242 char path[80];
1243 FILE *filep;
1244 int sib1, sib2;
1245 int matches;
1246 char character;
1247
1248 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
Josh Triplett57a42a32013-08-20 17:20:17 -07001249 filep = fopen_or_die(path, "r");
Len Brownc98d5d92012-06-04 00:56:40 -04001250 /*
1251 * file format:
1252 * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4)
1253 * otherwinse 1 sibling (self).
1254 */
1255 matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2);
1256
1257 fclose(filep);
1258
1259 if (matches == 3)
1260 return 2;
1261 else
1262 return 1;
1263}
1264
Len Brown103a8fe2010-10-22 23:53:03 -04001265/*
Len Brownc98d5d92012-06-04 00:56:40 -04001266 * run func(thread, core, package) in topology order
1267 * skip non-present cpus
Len Brown103a8fe2010-10-22 23:53:03 -04001268 */
1269
Len Brownc98d5d92012-06-04 00:56:40 -04001270int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
1271 struct pkg_data *, struct thread_data *, struct core_data *,
1272 struct pkg_data *), struct thread_data *thread_base,
1273 struct core_data *core_base, struct pkg_data *pkg_base,
1274 struct thread_data *thread_base2, struct core_data *core_base2,
1275 struct pkg_data *pkg_base2)
1276{
1277 int retval, pkg_no, core_no, thread_no;
1278
1279 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
1280 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
1281 for (thread_no = 0; thread_no <
1282 topo.num_threads_per_core; ++thread_no) {
1283 struct thread_data *t, *t2;
1284 struct core_data *c, *c2;
1285 struct pkg_data *p, *p2;
1286
1287 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
1288
1289 if (cpu_is_not_present(t->cpu_id))
1290 continue;
1291
1292 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
1293
1294 c = GET_CORE(core_base, core_no, pkg_no);
1295 c2 = GET_CORE(core_base2, core_no, pkg_no);
1296
1297 p = GET_PKG(pkg_base, pkg_no);
1298 p2 = GET_PKG(pkg_base2, pkg_no);
1299
1300 retval = func(t, c, p, t2, c2, p2);
1301 if (retval)
1302 return retval;
1303 }
1304 }
1305 }
1306 return 0;
1307}
1308
1309/*
1310 * run func(cpu) on every cpu in /proc/stat
1311 * return max_cpu number
1312 */
1313int for_all_proc_cpus(int (func)(int))
Len Brown103a8fe2010-10-22 23:53:03 -04001314{
1315 FILE *fp;
Len Brownc98d5d92012-06-04 00:56:40 -04001316 int cpu_num;
Len Brown103a8fe2010-10-22 23:53:03 -04001317 int retval;
1318
Josh Triplett57a42a32013-08-20 17:20:17 -07001319 fp = fopen_or_die(proc_stat, "r");
Len Brown103a8fe2010-10-22 23:53:03 -04001320
1321 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
1322 if (retval != 0) {
1323 perror("/proc/stat format");
1324 exit(1);
1325 }
1326
Len Brownc98d5d92012-06-04 00:56:40 -04001327 while (1) {
1328 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 -04001329 if (retval != 1)
1330 break;
1331
Len Brownc98d5d92012-06-04 00:56:40 -04001332 retval = func(cpu_num);
1333 if (retval) {
1334 fclose(fp);
1335 return(retval);
1336 }
Len Brown103a8fe2010-10-22 23:53:03 -04001337 }
1338 fclose(fp);
Len Brownc98d5d92012-06-04 00:56:40 -04001339 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001340}
1341
1342void re_initialize(void)
1343{
Len Brownc98d5d92012-06-04 00:56:40 -04001344 free_all_buffers();
1345 setup_all_buffers();
1346 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04001347}
1348
Len Brownc98d5d92012-06-04 00:56:40 -04001349
Len Brown103a8fe2010-10-22 23:53:03 -04001350/*
Len Brownc98d5d92012-06-04 00:56:40 -04001351 * count_cpus()
1352 * remember the last one seen, it will be the max
Len Brown103a8fe2010-10-22 23:53:03 -04001353 */
Len Brownc98d5d92012-06-04 00:56:40 -04001354int count_cpus(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001355{
Len Brownc98d5d92012-06-04 00:56:40 -04001356 if (topo.max_cpu_num < cpu)
1357 topo.max_cpu_num = cpu;
Len Brown103a8fe2010-10-22 23:53:03 -04001358
Len Brownc98d5d92012-06-04 00:56:40 -04001359 topo.num_cpus += 1;
1360 return 0;
1361}
1362int mark_cpu_present(int cpu)
1363{
1364 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brown15aaa342012-03-29 22:19:58 -04001365 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001366}
1367
1368void turbostat_loop()
1369{
Len Brownc98d5d92012-06-04 00:56:40 -04001370 int retval;
Len Browne52966c2012-11-08 22:38:05 -05001371 int restarted = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001372
Len Brown103a8fe2010-10-22 23:53:03 -04001373restart:
Len Browne52966c2012-11-08 22:38:05 -05001374 restarted++;
1375
Len Brownc98d5d92012-06-04 00:56:40 -04001376 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001377 if (retval < -1) {
1378 exit(retval);
1379 } else if (retval == -1) {
Len Browne52966c2012-11-08 22:38:05 -05001380 if (restarted > 1) {
1381 exit(retval);
1382 }
Len Brownc98d5d92012-06-04 00:56:40 -04001383 re_initialize();
1384 goto restart;
1385 }
Len Browne52966c2012-11-08 22:38:05 -05001386 restarted = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001387 gettimeofday(&tv_even, (struct timezone *)NULL);
1388
1389 while (1) {
Len Brownc98d5d92012-06-04 00:56:40 -04001390 if (for_all_proc_cpus(cpu_is_not_present)) {
Len Brown103a8fe2010-10-22 23:53:03 -04001391 re_initialize();
1392 goto restart;
1393 }
1394 sleep(interval_sec);
Len Brownc98d5d92012-06-04 00:56:40 -04001395 retval = for_all_cpus(get_counters, ODD_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001396 if (retval < -1) {
1397 exit(retval);
1398 } else if (retval == -1) {
Len Brown15aaa342012-03-29 22:19:58 -04001399 re_initialize();
1400 goto restart;
1401 }
Len Brown103a8fe2010-10-22 23:53:03 -04001402 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04001403 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04001404 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
1405 compute_average(EVEN_COUNTERS);
1406 format_all_counters(EVEN_COUNTERS);
1407 flush_stdout();
Len Brown15aaa342012-03-29 22:19:58 -04001408 sleep(interval_sec);
Len Brownc98d5d92012-06-04 00:56:40 -04001409 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001410 if (retval < -1) {
1411 exit(retval);
1412 } else if (retval == -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04001413 re_initialize();
1414 goto restart;
1415 }
Len Brown103a8fe2010-10-22 23:53:03 -04001416 gettimeofday(&tv_even, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04001417 timersub(&tv_even, &tv_odd, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04001418 for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS);
1419 compute_average(ODD_COUNTERS);
1420 format_all_counters(ODD_COUNTERS);
1421 flush_stdout();
Len Brown103a8fe2010-10-22 23:53:03 -04001422 }
1423}
1424
1425void check_dev_msr()
1426{
1427 struct stat sb;
1428
1429 if (stat("/dev/cpu/0/msr", &sb)) {
1430 fprintf(stderr, "no /dev/cpu/0/msr\n");
1431 fprintf(stderr, "Try \"# modprobe msr\"\n");
1432 exit(-5);
1433 }
1434}
1435
1436void check_super_user()
1437{
1438 if (getuid() != 0) {
1439 fprintf(stderr, "must be root\n");
1440 exit(-6);
1441 }
1442}
1443
1444int has_nehalem_turbo_ratio_limit(unsigned int family, unsigned int model)
1445{
1446 if (!genuine_intel)
1447 return 0;
1448
1449 if (family != 6)
1450 return 0;
1451
1452 switch (model) {
1453 case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
1454 case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
1455 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
1456 case 0x25: /* Westmere Client - Clarkdale, Arrandale */
1457 case 0x2C: /* Westmere EP - Gulftown */
1458 case 0x2A: /* SNB */
1459 case 0x2D: /* SNB Xeon */
Len Brown553575f2011-11-18 03:32:01 -05001460 case 0x3A: /* IVB */
Len Brown13006512012-09-26 18:11:31 -04001461 case 0x3E: /* IVB Xeon */
Len Brown70b43402013-01-08 01:26:07 -05001462 case 0x3C: /* HSW */
1463 case 0x3F: /* HSW */
1464 case 0x45: /* HSW */
Len Brown149c2312013-03-15 10:58:02 -04001465 case 0x46: /* HSW */
Len Brown144b44b2013-11-09 00:30:16 -05001466 case 0x37: /* BYT */
1467 case 0x4D: /* AVN */
Len Brown103a8fe2010-10-22 23:53:03 -04001468 return 1;
1469 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1470 case 0x2F: /* Westmere-EX Xeon - Eagleton */
1471 default:
1472 return 0;
1473 }
1474}
Len Brown6574a5d2012-09-21 00:01:31 -04001475int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
1476{
1477 if (!genuine_intel)
1478 return 0;
1479
1480 if (family != 6)
1481 return 0;
1482
1483 switch (model) {
1484 case 0x3E: /* IVB Xeon */
1485 return 1;
1486 default:
1487 return 0;
1488 }
1489}
1490
Len Brown889facb2012-11-08 00:48:57 -05001491/*
1492 * print_epb()
1493 * Decode the ENERGY_PERF_BIAS MSR
1494 */
1495int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1496{
1497 unsigned long long msr;
1498 char *epb_string;
1499 int cpu;
1500
1501 if (!has_epb)
1502 return 0;
1503
1504 cpu = t->cpu_id;
1505
1506 /* EPB is per-package */
1507 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1508 return 0;
1509
1510 if (cpu_migrate(cpu)) {
1511 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1512 return -1;
1513 }
1514
1515 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
1516 return 0;
1517
1518 switch (msr & 0x7) {
1519 case ENERGY_PERF_BIAS_PERFORMANCE:
1520 epb_string = "performance";
1521 break;
1522 case ENERGY_PERF_BIAS_NORMAL:
1523 epb_string = "balanced";
1524 break;
1525 case ENERGY_PERF_BIAS_POWERSAVE:
1526 epb_string = "powersave";
1527 break;
1528 default:
1529 epb_string = "custom";
1530 break;
1531 }
1532 fprintf(stderr, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
1533
1534 return 0;
1535}
1536
1537#define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
1538#define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
1539
Len Brown144b44b2013-11-09 00:30:16 -05001540double get_tdp(model)
1541{
1542 unsigned long long msr;
1543
1544 if (do_rapl & RAPL_PKG_POWER_INFO)
1545 if (!get_msr(0, MSR_PKG_POWER_INFO, &msr))
1546 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
1547
1548 switch (model) {
1549 case 0x37:
1550 case 0x4D:
1551 return 30.0;
1552 default:
1553 return 135.0;
1554 }
1555}
1556
1557
Len Brown889facb2012-11-08 00:48:57 -05001558/*
1559 * rapl_probe()
1560 *
Len Brown144b44b2013-11-09 00:30:16 -05001561 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
Len Brown889facb2012-11-08 00:48:57 -05001562 */
1563void rapl_probe(unsigned int family, unsigned int model)
1564{
1565 unsigned long long msr;
Len Brown144b44b2013-11-09 00:30:16 -05001566 unsigned int time_unit;
Len Brown889facb2012-11-08 00:48:57 -05001567 double tdp;
1568
1569 if (!genuine_intel)
1570 return;
1571
1572 if (family != 6)
1573 return;
1574
1575 switch (model) {
1576 case 0x2A:
1577 case 0x3A:
Len Brown70b43402013-01-08 01:26:07 -05001578 case 0x3C: /* HSW */
1579 case 0x3F: /* HSW */
1580 case 0x45: /* HSW */
Len Brown149c2312013-03-15 10:58:02 -04001581 case 0x46: /* HSW */
Len Brown144b44b2013-11-09 00:30:16 -05001582 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
Len Brown889facb2012-11-08 00:48:57 -05001583 break;
1584 case 0x2D:
1585 case 0x3E:
Len Brown144b44b2013-11-09 00:30:16 -05001586 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_PKG_PERF_STATUS | RAPL_DRAM_PERF_STATUS | RAPL_PKG_POWER_INFO;
1587 break;
1588 case 0x37: /* BYT */
1589 case 0x4D: /* AVN */
1590 do_rapl = RAPL_PKG | RAPL_CORES ;
Len Brown889facb2012-11-08 00:48:57 -05001591 break;
1592 default:
1593 return;
1594 }
1595
1596 /* units on package 0, verify later other packages match */
1597 if (get_msr(0, MSR_RAPL_POWER_UNIT, &msr))
1598 return;
1599
1600 rapl_power_units = 1.0 / (1 << (msr & 0xF));
Len Brown144b44b2013-11-09 00:30:16 -05001601 if (model == 0x37)
1602 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
1603 else
1604 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
Len Brown889facb2012-11-08 00:48:57 -05001605
Len Brown144b44b2013-11-09 00:30:16 -05001606 time_unit = msr >> 16 & 0xF;
1607 if (time_unit == 0)
1608 time_unit = 0xA;
Len Brown889facb2012-11-08 00:48:57 -05001609
Len Brown144b44b2013-11-09 00:30:16 -05001610 rapl_time_units = 1.0 / (1 << (time_unit));
1611
1612 tdp = get_tdp(model);
Len Brown889facb2012-11-08 00:48:57 -05001613
1614 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
Len Brown889facb2012-11-08 00:48:57 -05001615 if (verbose)
Len Brown144b44b2013-11-09 00:30:16 -05001616 fprintf(stderr, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
Len Brown889facb2012-11-08 00:48:57 -05001617
1618 return;
1619}
1620
1621int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1622{
1623 unsigned long long msr;
1624 unsigned int dts;
1625 int cpu;
1626
1627 if (!(do_dts || do_ptm))
1628 return 0;
1629
1630 cpu = t->cpu_id;
1631
1632 /* DTS is per-core, no need to print for each thread */
1633 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1634 return 0;
1635
1636 if (cpu_migrate(cpu)) {
1637 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1638 return -1;
1639 }
1640
1641 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
1642 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1643 return 0;
1644
1645 dts = (msr >> 16) & 0x7F;
1646 fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
1647 cpu, msr, tcc_activation_temp - dts);
1648
1649#ifdef THERM_DEBUG
1650 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
1651 return 0;
1652
1653 dts = (msr >> 16) & 0x7F;
1654 dts2 = (msr >> 8) & 0x7F;
1655 fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
1656 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
1657#endif
1658 }
1659
1660
1661 if (do_dts) {
1662 unsigned int resolution;
1663
1664 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1665 return 0;
1666
1667 dts = (msr >> 16) & 0x7F;
1668 resolution = (msr >> 27) & 0xF;
1669 fprintf(stderr, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
1670 cpu, msr, tcc_activation_temp - dts, resolution);
1671
1672#ifdef THERM_DEBUG
1673 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
1674 return 0;
1675
1676 dts = (msr >> 16) & 0x7F;
1677 dts2 = (msr >> 8) & 0x7F;
1678 fprintf(stderr, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
1679 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
1680#endif
1681 }
1682
1683 return 0;
1684}
1685
1686void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
1687{
1688 fprintf(stderr, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
1689 cpu, label,
1690 ((msr >> 15) & 1) ? "EN" : "DIS",
1691 ((msr >> 0) & 0x7FFF) * rapl_power_units,
1692 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
1693 (((msr >> 16) & 1) ? "EN" : "DIS"));
1694
1695 return;
1696}
1697
1698int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1699{
1700 unsigned long long msr;
1701 int cpu;
Len Brown889facb2012-11-08 00:48:57 -05001702
1703 if (!do_rapl)
1704 return 0;
1705
1706 /* RAPL counters are per package, so print only for 1st thread/package */
1707 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1708 return 0;
1709
1710 cpu = t->cpu_id;
1711 if (cpu_migrate(cpu)) {
1712 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1713 return -1;
1714 }
1715
1716 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
1717 return -1;
1718
Len Brown889facb2012-11-08 00:48:57 -05001719 if (verbose) {
1720 fprintf(stderr, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx "
1721 "(%f Watts, %f Joules, %f sec.)\n", cpu, msr,
Len Brown144b44b2013-11-09 00:30:16 -05001722 rapl_power_units, rapl_energy_units, rapl_time_units);
Len Brown889facb2012-11-08 00:48:57 -05001723 }
Len Brown144b44b2013-11-09 00:30:16 -05001724 if (do_rapl & RAPL_PKG_POWER_INFO) {
1725
Len Brown889facb2012-11-08 00:48:57 -05001726 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
1727 return -5;
1728
1729
1730 fprintf(stderr, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
1731 cpu, msr,
1732 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1733 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1734 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1735 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
1736
Len Brown144b44b2013-11-09 00:30:16 -05001737 }
1738 if (do_rapl & RAPL_PKG) {
1739
Len Brown889facb2012-11-08 00:48:57 -05001740 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
1741 return -9;
1742
1743 fprintf(stderr, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
1744 cpu, msr, (msr >> 63) & 1 ? "": "UN");
1745
1746 print_power_limit_msr(cpu, msr, "PKG Limit #1");
1747 fprintf(stderr, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
1748 cpu,
1749 ((msr >> 47) & 1) ? "EN" : "DIS",
1750 ((msr >> 32) & 0x7FFF) * rapl_power_units,
1751 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
1752 ((msr >> 48) & 1) ? "EN" : "DIS");
1753 }
1754
1755 if (do_rapl & RAPL_DRAM) {
1756 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
1757 return -6;
1758
1759
1760 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
1761 cpu, msr,
1762 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1763 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1764 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1765 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
1766
1767
1768 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
1769 return -9;
1770 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
1771 cpu, msr, (msr >> 31) & 1 ? "": "UN");
1772
1773 print_power_limit_msr(cpu, msr, "DRAM Limit");
1774 }
Len Brown144b44b2013-11-09 00:30:16 -05001775 if (do_rapl & RAPL_CORE_POLICY) {
Len Brown889facb2012-11-08 00:48:57 -05001776 if (verbose) {
1777 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
1778 return -7;
1779
1780 fprintf(stderr, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown144b44b2013-11-09 00:30:16 -05001781 }
1782 }
1783 if (do_rapl & RAPL_CORES) {
1784 if (verbose) {
Len Brown889facb2012-11-08 00:48:57 -05001785
1786 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
1787 return -9;
1788 fprintf(stderr, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
1789 cpu, msr, (msr >> 31) & 1 ? "": "UN");
1790 print_power_limit_msr(cpu, msr, "Cores Limit");
1791 }
1792 }
1793 if (do_rapl & RAPL_GFX) {
1794 if (verbose) {
1795 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
1796 return -8;
1797
1798 fprintf(stderr, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
1799
1800 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
1801 return -9;
1802 fprintf(stderr, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
1803 cpu, msr, (msr >> 31) & 1 ? "": "UN");
1804 print_power_limit_msr(cpu, msr, "GFX Limit");
1805 }
1806 }
1807 return 0;
1808}
1809
Len Brown103a8fe2010-10-22 23:53:03 -04001810
1811int is_snb(unsigned int family, unsigned int model)
1812{
1813 if (!genuine_intel)
1814 return 0;
1815
1816 switch (model) {
1817 case 0x2A:
1818 case 0x2D:
Len Brown650a37f2012-06-03 23:34:44 -04001819 case 0x3A: /* IVB */
Len Brown13006512012-09-26 18:11:31 -04001820 case 0x3E: /* IVB Xeon */
Len Brown70b43402013-01-08 01:26:07 -05001821 case 0x3C: /* HSW */
1822 case 0x3F: /* HSW */
1823 case 0x45: /* HSW */
Len Brown149c2312013-03-15 10:58:02 -04001824 case 0x46: /* HSW */
Len Brown103a8fe2010-10-22 23:53:03 -04001825 return 1;
1826 }
1827 return 0;
1828}
1829
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001830int has_c8_c9_c10(unsigned int family, unsigned int model)
1831{
1832 if (!genuine_intel)
1833 return 0;
1834
1835 switch (model) {
1836 case 0x45:
1837 return 1;
1838 }
1839 return 0;
1840}
1841
1842
Len Brown144b44b2013-11-09 00:30:16 -05001843int is_slm(unsigned int family, unsigned int model)
1844{
1845 if (!genuine_intel)
1846 return 0;
1847 switch (model) {
1848 case 0x37: /* BYT */
1849 case 0x4D: /* AVN */
1850 return 1;
1851 }
1852 return 0;
1853}
1854
1855#define SLM_BCLK_FREQS 5
1856double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
1857
1858double slm_bclk(void)
1859{
1860 unsigned long long msr = 3;
1861 unsigned int i;
1862 double freq;
1863
1864 if (get_msr(0, MSR_FSB_FREQ, &msr))
1865 fprintf(stderr, "SLM BCLK: unknown\n");
1866
1867 i = msr & 0xf;
1868 if (i >= SLM_BCLK_FREQS) {
1869 fprintf(stderr, "SLM BCLK[%d] invalid\n", i);
1870 msr = 3;
1871 }
1872 freq = slm_freq_table[i];
1873
1874 fprintf(stderr, "SLM BCLK: %.1f Mhz\n", freq);
1875
1876 return freq;
1877}
1878
Len Brown103a8fe2010-10-22 23:53:03 -04001879double discover_bclk(unsigned int family, unsigned int model)
1880{
1881 if (is_snb(family, model))
1882 return 100.00;
Len Brown144b44b2013-11-09 00:30:16 -05001883 else if (is_slm(family, model))
1884 return slm_bclk();
Len Brown103a8fe2010-10-22 23:53:03 -04001885 else
1886 return 133.33;
1887}
1888
Len Brown889facb2012-11-08 00:48:57 -05001889/*
1890 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
1891 * the Thermal Control Circuit (TCC) activates.
1892 * This is usually equal to tjMax.
1893 *
1894 * Older processors do not have this MSR, so there we guess,
1895 * but also allow cmdline over-ride with -T.
1896 *
1897 * Several MSR temperature values are in units of degrees-C
1898 * below this value, including the Digital Thermal Sensor (DTS),
1899 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
1900 */
1901int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1902{
1903 unsigned long long msr;
1904 unsigned int target_c_local;
1905 int cpu;
1906
1907 /* tcc_activation_temp is used only for dts or ptm */
1908 if (!(do_dts || do_ptm))
1909 return 0;
1910
1911 /* this is a per-package concept */
1912 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1913 return 0;
1914
1915 cpu = t->cpu_id;
1916 if (cpu_migrate(cpu)) {
1917 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1918 return -1;
1919 }
1920
1921 if (tcc_activation_temp_override != 0) {
1922 tcc_activation_temp = tcc_activation_temp_override;
1923 fprintf(stderr, "cpu%d: Using cmdline TCC Target (%d C)\n",
1924 cpu, tcc_activation_temp);
1925 return 0;
1926 }
1927
1928 /* Temperature Target MSR is Nehalem and newer only */
1929 if (!do_nehalem_platform_info)
1930 goto guess;
1931
1932 if (get_msr(0, MSR_IA32_TEMPERATURE_TARGET, &msr))
1933 goto guess;
1934
1935 target_c_local = (msr >> 16) & 0x7F;
1936
1937 if (verbose)
1938 fprintf(stderr, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
1939 cpu, msr, target_c_local);
1940
Len Brown144b44b2013-11-09 00:30:16 -05001941 if (target_c_local < 85 || target_c_local > 127)
Len Brown889facb2012-11-08 00:48:57 -05001942 goto guess;
1943
1944 tcc_activation_temp = target_c_local;
1945
1946 return 0;
1947
1948guess:
1949 tcc_activation_temp = TJMAX_DEFAULT;
1950 fprintf(stderr, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
1951 cpu, tcc_activation_temp);
1952
1953 return 0;
1954}
Len Brown103a8fe2010-10-22 23:53:03 -04001955void check_cpuid()
1956{
1957 unsigned int eax, ebx, ecx, edx, max_level;
1958 unsigned int fms, family, model, stepping;
1959
1960 eax = ebx = ecx = edx = 0;
1961
Josh Triplett2b928652013-08-20 17:20:14 -07001962 __get_cpuid(0, &max_level, &ebx, &ecx, &edx);
Len Brown103a8fe2010-10-22 23:53:03 -04001963
1964 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
1965 genuine_intel = 1;
1966
1967 if (verbose)
Len Brown889facb2012-11-08 00:48:57 -05001968 fprintf(stderr, "CPUID(0): %.4s%.4s%.4s ",
Len Brown103a8fe2010-10-22 23:53:03 -04001969 (char *)&ebx, (char *)&edx, (char *)&ecx);
1970
Josh Triplett2b928652013-08-20 17:20:14 -07001971 __get_cpuid(1, &fms, &ebx, &ecx, &edx);
Len Brown103a8fe2010-10-22 23:53:03 -04001972 family = (fms >> 8) & 0xf;
1973 model = (fms >> 4) & 0xf;
1974 stepping = fms & 0xf;
1975 if (family == 6 || family == 0xf)
1976 model += ((fms >> 16) & 0xf) << 4;
1977
1978 if (verbose)
1979 fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
1980 max_level, family, model, stepping, family, model, stepping);
1981
1982 if (!(edx & (1 << 5))) {
1983 fprintf(stderr, "CPUID: no MSR\n");
1984 exit(1);
1985 }
1986
1987 /*
1988 * check max extended function levels of CPUID.
1989 * This is needed to check for invariant TSC.
1990 * This check is valid for both Intel and AMD.
1991 */
1992 ebx = ecx = edx = 0;
Josh Triplett2b928652013-08-20 17:20:14 -07001993 __get_cpuid(0x80000000, &max_level, &ebx, &ecx, &edx);
Len Brown103a8fe2010-10-22 23:53:03 -04001994
1995 if (max_level < 0x80000007) {
1996 fprintf(stderr, "CPUID: no invariant TSC (max_level 0x%x)\n", max_level);
1997 exit(1);
1998 }
1999
2000 /*
2001 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
2002 * this check is valid for both Intel and AMD
2003 */
Josh Triplett2b928652013-08-20 17:20:14 -07002004 __get_cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
Thomas Renninger8209e052011-01-21 15:11:19 +01002005 has_invariant_tsc = edx & (1 << 8);
Len Brown103a8fe2010-10-22 23:53:03 -04002006
2007 if (!has_invariant_tsc) {
2008 fprintf(stderr, "No invariant TSC\n");
2009 exit(1);
2010 }
2011
2012 /*
2013 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
2014 * this check is valid for both Intel and AMD
2015 */
2016
Josh Triplett2b928652013-08-20 17:20:14 -07002017 __get_cpuid(0x6, &eax, &ebx, &ecx, &edx);
Thomas Renninger8209e052011-01-21 15:11:19 +01002018 has_aperf = ecx & (1 << 0);
Len Brown889facb2012-11-08 00:48:57 -05002019 do_dts = eax & (1 << 0);
2020 do_ptm = eax & (1 << 6);
2021 has_epb = ecx & (1 << 3);
2022
2023 if (verbose)
2024 fprintf(stderr, "CPUID(6): %s%s%s%s\n",
2025 has_aperf ? "APERF" : "No APERF!",
2026 do_dts ? ", DTS" : "",
2027 do_ptm ? ", PTM": "",
2028 has_epb ? ", EPB": "");
2029
2030 if (!has_aperf)
2031 exit(-1);
Len Brown103a8fe2010-10-22 23:53:03 -04002032
2033 do_nehalem_platform_info = genuine_intel && has_invariant_tsc;
2034 do_nhm_cstates = genuine_intel; /* all Intel w/ non-stop TSC have NHM counters */
Len Brown1ed51012013-02-10 17:19:24 -05002035 do_smi = do_nhm_cstates;
Len Brown103a8fe2010-10-22 23:53:03 -04002036 do_snb_cstates = is_snb(family, model);
Kristen Carlson Accardica587102012-11-21 05:22:43 -08002037 do_c8_c9_c10 = has_c8_c9_c10(family, model);
Len Brown144b44b2013-11-09 00:30:16 -05002038 do_slm_cstates = is_slm(family, model);
Len Brown103a8fe2010-10-22 23:53:03 -04002039 bclk = discover_bclk(family, model);
2040
2041 do_nehalem_turbo_ratio_limit = has_nehalem_turbo_ratio_limit(family, model);
Len Brown6574a5d2012-09-21 00:01:31 -04002042 do_ivt_turbo_ratio_limit = has_ivt_turbo_ratio_limit(family, model);
Len Brown889facb2012-11-08 00:48:57 -05002043 rapl_probe(family, model);
2044
2045 return;
Len Brown103a8fe2010-10-22 23:53:03 -04002046}
2047
2048
2049void usage()
2050{
Len Brown889facb2012-11-08 00:48:57 -05002051 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 -04002052 progname);
2053 exit(1);
2054}
2055
2056
2057/*
2058 * in /dev/cpu/ return success for names that are numbers
2059 * ie. filter out ".", "..", "microcode".
2060 */
2061int dir_filter(const struct dirent *dirp)
2062{
2063 if (isdigit(dirp->d_name[0]))
2064 return 1;
2065 else
2066 return 0;
2067}
2068
2069int open_dev_cpu_msr(int dummy1)
2070{
2071 return 0;
2072}
2073
Len Brownc98d5d92012-06-04 00:56:40 -04002074void topology_probe()
2075{
2076 int i;
2077 int max_core_id = 0;
2078 int max_package_id = 0;
2079 int max_siblings = 0;
2080 struct cpu_topology {
2081 int core_id;
2082 int physical_package_id;
2083 } *cpus;
2084
2085 /* Initialize num_cpus, max_cpu_num */
2086 topo.num_cpus = 0;
2087 topo.max_cpu_num = 0;
2088 for_all_proc_cpus(count_cpus);
2089 if (!summary_only && topo.num_cpus > 1)
2090 show_cpu = 1;
2091
2092 if (verbose > 1)
2093 fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
2094
2095 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
2096 if (cpus == NULL) {
2097 perror("calloc cpus");
2098 exit(1);
2099 }
2100
2101 /*
2102 * Allocate and initialize cpu_present_set
2103 */
2104 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
2105 if (cpu_present_set == NULL) {
2106 perror("CPU_ALLOC");
2107 exit(3);
2108 }
2109 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2110 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
2111 for_all_proc_cpus(mark_cpu_present);
2112
2113 /*
2114 * Allocate and initialize cpu_affinity_set
2115 */
2116 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
2117 if (cpu_affinity_set == NULL) {
2118 perror("CPU_ALLOC");
2119 exit(3);
2120 }
2121 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2122 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
2123
2124
2125 /*
2126 * For online cpus
2127 * find max_core_id, max_package_id
2128 */
2129 for (i = 0; i <= topo.max_cpu_num; ++i) {
2130 int siblings;
2131
2132 if (cpu_is_not_present(i)) {
2133 if (verbose > 1)
2134 fprintf(stderr, "cpu%d NOT PRESENT\n", i);
2135 continue;
2136 }
2137 cpus[i].core_id = get_core_id(i);
2138 if (cpus[i].core_id > max_core_id)
2139 max_core_id = cpus[i].core_id;
2140
2141 cpus[i].physical_package_id = get_physical_package_id(i);
2142 if (cpus[i].physical_package_id > max_package_id)
2143 max_package_id = cpus[i].physical_package_id;
2144
2145 siblings = get_num_ht_siblings(i);
2146 if (siblings > max_siblings)
2147 max_siblings = siblings;
2148 if (verbose > 1)
2149 fprintf(stderr, "cpu %d pkg %d core %d\n",
2150 i, cpus[i].physical_package_id, cpus[i].core_id);
2151 }
2152 topo.num_cores_per_pkg = max_core_id + 1;
2153 if (verbose > 1)
2154 fprintf(stderr, "max_core_id %d, sizing for %d cores per package\n",
2155 max_core_id, topo.num_cores_per_pkg);
2156 if (!summary_only && topo.num_cores_per_pkg > 1)
2157 show_core = 1;
2158
2159 topo.num_packages = max_package_id + 1;
2160 if (verbose > 1)
2161 fprintf(stderr, "max_package_id %d, sizing for %d packages\n",
2162 max_package_id, topo.num_packages);
2163 if (!summary_only && topo.num_packages > 1)
2164 show_pkg = 1;
2165
2166 topo.num_threads_per_core = max_siblings;
2167 if (verbose > 1)
2168 fprintf(stderr, "max_siblings %d\n", max_siblings);
2169
2170 free(cpus);
2171}
2172
2173void
2174allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
2175{
2176 int i;
2177
2178 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
2179 topo.num_packages, sizeof(struct thread_data));
2180 if (*t == NULL)
2181 goto error;
2182
2183 for (i = 0; i < topo.num_threads_per_core *
2184 topo.num_cores_per_pkg * topo.num_packages; i++)
2185 (*t)[i].cpu_id = -1;
2186
2187 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
2188 sizeof(struct core_data));
2189 if (*c == NULL)
2190 goto error;
2191
2192 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
2193 (*c)[i].core_id = -1;
2194
2195 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
2196 if (*p == NULL)
2197 goto error;
2198
2199 for (i = 0; i < topo.num_packages; i++)
2200 (*p)[i].package_id = i;
2201
2202 return;
2203error:
2204 perror("calloc counters");
2205 exit(1);
2206}
2207/*
2208 * init_counter()
2209 *
2210 * set cpu_id, core_num, pkg_num
2211 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
2212 *
2213 * increment topo.num_cores when 1st core in pkg seen
2214 */
2215void init_counter(struct thread_data *thread_base, struct core_data *core_base,
2216 struct pkg_data *pkg_base, int thread_num, int core_num,
2217 int pkg_num, int cpu_id)
2218{
2219 struct thread_data *t;
2220 struct core_data *c;
2221 struct pkg_data *p;
2222
2223 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
2224 c = GET_CORE(core_base, core_num, pkg_num);
2225 p = GET_PKG(pkg_base, pkg_num);
2226
2227 t->cpu_id = cpu_id;
2228 if (thread_num == 0) {
2229 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
2230 if (cpu_is_first_core_in_package(cpu_id))
2231 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
2232 }
2233
2234 c->core_id = core_num;
2235 p->package_id = pkg_num;
2236}
2237
2238
2239int initialize_counters(int cpu_id)
2240{
2241 int my_thread_id, my_core_id, my_package_id;
2242
2243 my_package_id = get_physical_package_id(cpu_id);
2244 my_core_id = get_core_id(cpu_id);
2245
2246 if (cpu_is_first_sibling_in_core(cpu_id)) {
2247 my_thread_id = 0;
2248 topo.num_cores++;
2249 } else {
2250 my_thread_id = 1;
2251 }
2252
2253 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2254 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2255 return 0;
2256}
2257
2258void allocate_output_buffer()
2259{
Josh Triplettb844db32013-06-12 17:26:37 -07002260 output_buffer = calloc(1, (1 + topo.num_cpus) * 256);
Len Brownc98d5d92012-06-04 00:56:40 -04002261 outp = output_buffer;
2262 if (outp == NULL) {
2263 perror("calloc");
2264 exit(-1);
2265 }
2266}
2267
2268void setup_all_buffers(void)
2269{
2270 topology_probe();
2271 allocate_counters(&thread_even, &core_even, &package_even);
2272 allocate_counters(&thread_odd, &core_odd, &package_odd);
2273 allocate_output_buffer();
2274 for_all_proc_cpus(initialize_counters);
2275}
Len Brown103a8fe2010-10-22 23:53:03 -04002276void turbostat_init()
2277{
2278 check_cpuid();
2279
2280 check_dev_msr();
2281 check_super_user();
2282
Len Brownc98d5d92012-06-04 00:56:40 -04002283 setup_all_buffers();
Len Brown103a8fe2010-10-22 23:53:03 -04002284
2285 if (verbose)
Len Brownc98d5d92012-06-04 00:56:40 -04002286 print_verbose_header();
Len Brown889facb2012-11-08 00:48:57 -05002287
2288 if (verbose)
2289 for_all_cpus(print_epb, ODD_COUNTERS);
2290
2291 if (verbose)
2292 for_all_cpus(print_rapl, ODD_COUNTERS);
2293
2294 for_all_cpus(set_temperature_target, ODD_COUNTERS);
2295
2296 if (verbose)
2297 for_all_cpus(print_thermal, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04002298}
2299
2300int fork_it(char **argv)
2301{
Len Brown103a8fe2010-10-22 23:53:03 -04002302 pid_t child_pid;
Len Brownd91bb172012-11-01 00:08:19 -04002303 int status;
Len Brownd15cf7c2012-06-03 23:24:00 -04002304
Len Brownd91bb172012-11-01 00:08:19 -04002305 status = for_all_cpus(get_counters, EVEN_COUNTERS);
2306 if (status)
2307 exit(status);
Len Brownc98d5d92012-06-04 00:56:40 -04002308 /* clear affinity side-effect of get_counters() */
2309 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
Len Brown103a8fe2010-10-22 23:53:03 -04002310 gettimeofday(&tv_even, (struct timezone *)NULL);
2311
2312 child_pid = fork();
2313 if (!child_pid) {
2314 /* child */
2315 execvp(argv[0], argv);
2316 } else {
Len Brown103a8fe2010-10-22 23:53:03 -04002317
2318 /* parent */
2319 if (child_pid == -1) {
2320 perror("fork");
2321 exit(1);
2322 }
2323
2324 signal(SIGINT, SIG_IGN);
2325 signal(SIGQUIT, SIG_IGN);
2326 if (waitpid(child_pid, &status, 0) == -1) {
2327 perror("wait");
Len Brownd91bb172012-11-01 00:08:19 -04002328 exit(status);
Len Brown103a8fe2010-10-22 23:53:03 -04002329 }
2330 }
Len Brownc98d5d92012-06-04 00:56:40 -04002331 /*
2332 * n.b. fork_it() does not check for errors from for_all_cpus()
2333 * because re-starting is problematic when forking
2334 */
2335 for_all_cpus(get_counters, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04002336 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002337 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04002338 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
2339 compute_average(EVEN_COUNTERS);
2340 format_all_counters(EVEN_COUNTERS);
2341 flush_stderr();
Len Brown103a8fe2010-10-22 23:53:03 -04002342
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07002343 fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
Len Brown103a8fe2010-10-22 23:53:03 -04002344
Len Brownd91bb172012-11-01 00:08:19 -04002345 return status;
Len Brown103a8fe2010-10-22 23:53:03 -04002346}
2347
2348void cmdline(int argc, char **argv)
2349{
2350 int opt;
2351
2352 progname = argv[0];
2353
Len Brown889facb2012-11-08 00:48:57 -05002354 while ((opt = getopt(argc, argv, "+pPSvi:sc:sC:m:M:RT:")) != -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04002355 switch (opt) {
Len Brownf9240812012-10-06 15:26:31 -04002356 case 'p':
Len Brownc98d5d92012-06-04 00:56:40 -04002357 show_core_only++;
2358 break;
Len Brownf9240812012-10-06 15:26:31 -04002359 case 'P':
Len Brownc98d5d92012-06-04 00:56:40 -04002360 show_pkg_only++;
2361 break;
Len Brownf9240812012-10-06 15:26:31 -04002362 case 'S':
Len Browne23da032012-02-06 18:37:16 -05002363 summary_only++;
2364 break;
Len Brown103a8fe2010-10-22 23:53:03 -04002365 case 'v':
2366 verbose++;
2367 break;
2368 case 'i':
2369 interval_sec = atoi(optarg);
2370 break;
Len Brownf9240812012-10-06 15:26:31 -04002371 case 'c':
Len Brown8e180f32012-09-22 01:25:08 -04002372 sscanf(optarg, "%x", &extra_delta_offset32);
2373 break;
Len Brownf9240812012-10-06 15:26:31 -04002374 case 'C':
Len Brown8e180f32012-09-22 01:25:08 -04002375 sscanf(optarg, "%x", &extra_delta_offset64);
2376 break;
Len Brown2f32edf2012-09-21 23:45:46 -04002377 case 'm':
2378 sscanf(optarg, "%x", &extra_msr_offset32);
Len Brown2f32edf2012-09-21 23:45:46 -04002379 break;
2380 case 'M':
2381 sscanf(optarg, "%x", &extra_msr_offset64);
Len Brown103a8fe2010-10-22 23:53:03 -04002382 break;
Len Brown889facb2012-11-08 00:48:57 -05002383 case 'R':
2384 rapl_verbose++;
2385 break;
2386 case 'T':
2387 tcc_activation_temp_override = atoi(optarg);
2388 break;
Len Brown103a8fe2010-10-22 23:53:03 -04002389 default:
2390 usage();
2391 }
2392 }
2393}
2394
2395int main(int argc, char **argv)
2396{
2397 cmdline(argc, argv);
2398
Len Brown889facb2012-11-08 00:48:57 -05002399 if (verbose)
Len Brown144b44b2013-11-09 00:30:16 -05002400 fprintf(stderr, "turbostat v3.5 April 26, 2013"
Len Brown103a8fe2010-10-22 23:53:03 -04002401 " - Len Brown <lenb@kernel.org>\n");
Len Brown103a8fe2010-10-22 23:53:03 -04002402
2403 turbostat_init();
2404
2405 /*
2406 * if any params left, it must be a command to fork
2407 */
2408 if (argc - optind)
2409 return fork_it(argv + optind);
2410 else
2411 turbostat_loop();
2412
2413 return 0;
2414}