blob: 93cbef56e876c153ef376a92e590327f3b5c5b78 [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
Len Brown103a8fe2010-10-22 23:53:03 -040024#include <stdio.h>
25#include <unistd.h>
26#include <sys/types.h>
27#include <sys/wait.h>
28#include <sys/stat.h>
29#include <sys/resource.h>
30#include <fcntl.h>
31#include <signal.h>
32#include <sys/time.h>
33#include <stdlib.h>
34#include <dirent.h>
35#include <string.h>
36#include <ctype.h>
Len Brown88c32812012-03-29 21:44:40 -040037#include <sched.h>
Len Brown103a8fe2010-10-22 23:53:03 -040038
Len Brown103a8fe2010-10-22 23:53:03 -040039char *proc_stat = "/proc/stat";
40unsigned int interval_sec = 5; /* set with -i interval_sec */
41unsigned int verbose; /* set with -v */
Len Brown889facb2012-11-08 00:48:57 -050042unsigned int rapl_verbose; /* set with -R */
43unsigned int thermal_verbose; /* set with -T */
Len Browne23da032012-02-06 18:37:16 -050044unsigned int summary_only; /* set with -s */
Len Brown103a8fe2010-10-22 23:53:03 -040045unsigned int skip_c0;
46unsigned int skip_c1;
47unsigned int do_nhm_cstates;
48unsigned int do_snb_cstates;
Kristen Carlson Accardica587102012-11-21 05:22:43 -080049unsigned int do_c8_c9_c10;
Len Brown144b44b2013-11-09 00:30:16 -050050unsigned int do_slm_cstates;
51unsigned int use_c1_residency_msr;
Len Brown103a8fe2010-10-22 23:53:03 -040052unsigned int has_aperf;
Len Brown889facb2012-11-08 00:48:57 -050053unsigned int has_epb;
Len Brown103a8fe2010-10-22 23:53:03 -040054unsigned int units = 1000000000; /* Ghz etc */
55unsigned int genuine_intel;
56unsigned int has_invariant_tsc;
57unsigned int do_nehalem_platform_info;
58unsigned int do_nehalem_turbo_ratio_limit;
Len Brown6574a5d2012-09-21 00:01:31 -040059unsigned int do_ivt_turbo_ratio_limit;
Len Brown2f32edf2012-09-21 23:45:46 -040060unsigned int extra_msr_offset32;
61unsigned int extra_msr_offset64;
Len Brown8e180f32012-09-22 01:25:08 -040062unsigned int extra_delta_offset32;
63unsigned int extra_delta_offset64;
Len Brown1ed51012013-02-10 17:19:24 -050064int do_smi;
Len Brown103a8fe2010-10-22 23:53:03 -040065double bclk;
66unsigned int show_pkg;
67unsigned int show_core;
68unsigned int show_cpu;
Len Brownc98d5d92012-06-04 00:56:40 -040069unsigned int show_pkg_only;
70unsigned int show_core_only;
71char *output_buffer, *outp;
Len Brown889facb2012-11-08 00:48:57 -050072unsigned int do_rapl;
73unsigned int do_dts;
74unsigned int do_ptm;
75unsigned int tcc_activation_temp;
76unsigned int tcc_activation_temp_override;
77double rapl_power_units, rapl_energy_units, rapl_time_units;
78double rapl_joule_counter_range;
79
80#define RAPL_PKG (1 << 0)
81#define RAPL_CORES (1 << 1)
82#define RAPL_GFX (1 << 2)
83#define RAPL_DRAM (1 << 3)
84#define RAPL_PKG_PERF_STATUS (1 << 4)
85#define RAPL_DRAM_PERF_STATUS (1 << 5)
Len Brown144b44b2013-11-09 00:30:16 -050086#define RAPL_PKG_POWER_INFO (1 << 6)
87#define RAPL_CORE_POLICY (1 << 7)
Len Brown889facb2012-11-08 00:48:57 -050088#define TJMAX_DEFAULT 100
89
90#define MAX(a, b) ((a) > (b) ? (a) : (b))
Len Brown103a8fe2010-10-22 23:53:03 -040091
92int aperf_mperf_unstable;
93int backwards_count;
94char *progname;
Len Brown103a8fe2010-10-22 23:53:03 -040095
Len Brownc98d5d92012-06-04 00:56:40 -040096cpu_set_t *cpu_present_set, *cpu_affinity_set;
97size_t cpu_present_setsize, cpu_affinity_setsize;
Len Brown103a8fe2010-10-22 23:53:03 -040098
Len Brownc98d5d92012-06-04 00:56:40 -040099struct thread_data {
100 unsigned long long tsc;
101 unsigned long long aperf;
102 unsigned long long mperf;
Len Brown144b44b2013-11-09 00:30:16 -0500103 unsigned long long c1;
Len Brown2f32edf2012-09-21 23:45:46 -0400104 unsigned long long extra_msr64;
Len Brown8e180f32012-09-22 01:25:08 -0400105 unsigned long long extra_delta64;
106 unsigned long long extra_msr32;
107 unsigned long long extra_delta32;
Len Brown1ed51012013-02-10 17:19:24 -0500108 unsigned int smi_count;
Len Brownc98d5d92012-06-04 00:56:40 -0400109 unsigned int cpu_id;
110 unsigned int flags;
111#define CPU_IS_FIRST_THREAD_IN_CORE 0x2
112#define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
113} *thread_even, *thread_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400114
Len Brownc98d5d92012-06-04 00:56:40 -0400115struct core_data {
116 unsigned long long c3;
117 unsigned long long c6;
118 unsigned long long c7;
Len Brown889facb2012-11-08 00:48:57 -0500119 unsigned int core_temp_c;
Len Brownc98d5d92012-06-04 00:56:40 -0400120 unsigned int core_id;
121} *core_even, *core_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400122
Len Brownc98d5d92012-06-04 00:56:40 -0400123struct pkg_data {
124 unsigned long long pc2;
125 unsigned long long pc3;
126 unsigned long long pc6;
127 unsigned long long pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800128 unsigned long long pc8;
129 unsigned long long pc9;
130 unsigned long long pc10;
Len Brownc98d5d92012-06-04 00:56:40 -0400131 unsigned int package_id;
Len Brown889facb2012-11-08 00:48:57 -0500132 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
133 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
134 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
135 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
136 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
137 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
138 unsigned int pkg_temp_c;
139
Len Brownc98d5d92012-06-04 00:56:40 -0400140} *package_even, *package_odd;
141
142#define ODD_COUNTERS thread_odd, core_odd, package_odd
143#define EVEN_COUNTERS thread_even, core_even, package_even
144
145#define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
146 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
147 topo.num_threads_per_core + \
148 (core_no) * topo.num_threads_per_core + (thread_no))
149#define GET_CORE(core_base, core_no, pkg_no) \
150 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
151#define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
152
153struct system_summary {
154 struct thread_data threads;
155 struct core_data cores;
156 struct pkg_data packages;
157} sum, average;
158
159
160struct topo_params {
161 int num_packages;
162 int num_cpus;
163 int num_cores;
164 int max_cpu_num;
165 int num_cores_per_pkg;
166 int num_threads_per_core;
167} topo;
168
169struct timeval tv_even, tv_odd, tv_delta;
170
171void setup_all_buffers(void);
172
173int cpu_is_not_present(int cpu)
Len Brownd15cf7c2012-06-03 23:24:00 -0400174{
Len Brownc98d5d92012-06-04 00:56:40 -0400175 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brownd15cf7c2012-06-03 23:24:00 -0400176}
Len Brown88c32812012-03-29 21:44:40 -0400177/*
Len Brownc98d5d92012-06-04 00:56:40 -0400178 * run func(thread, core, package) in topology order
179 * skip non-present cpus
Len Brown88c32812012-03-29 21:44:40 -0400180 */
Len Brownd15cf7c2012-06-03 23:24:00 -0400181
Len Brownc98d5d92012-06-04 00:56:40 -0400182int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
183 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
Len Brown88c32812012-03-29 21:44:40 -0400184{
Len Brownc98d5d92012-06-04 00:56:40 -0400185 int retval, pkg_no, core_no, thread_no;
186
187 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
188 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
189 for (thread_no = 0; thread_no <
190 topo.num_threads_per_core; ++thread_no) {
191 struct thread_data *t;
192 struct core_data *c;
193 struct pkg_data *p;
194
195 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
196
197 if (cpu_is_not_present(t->cpu_id))
198 continue;
199
200 c = GET_CORE(core_base, core_no, pkg_no);
201 p = GET_PKG(pkg_base, pkg_no);
202
203 retval = func(t, c, p);
204 if (retval)
205 return retval;
206 }
207 }
208 }
209 return 0;
Len Brown88c32812012-03-29 21:44:40 -0400210}
211
212int cpu_migrate(int cpu)
213{
Len Brownc98d5d92012-06-04 00:56:40 -0400214 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
215 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
216 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
Len Brown88c32812012-03-29 21:44:40 -0400217 return -1;
218 else
219 return 0;
220}
221
Len Brown15aaa342012-03-29 22:19:58 -0400222int get_msr(int cpu, off_t offset, unsigned long long *msr)
Len Brown103a8fe2010-10-22 23:53:03 -0400223{
224 ssize_t retval;
Len Brown103a8fe2010-10-22 23:53:03 -0400225 char pathname[32];
226 int fd;
227
228 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
229 fd = open(pathname, O_RDONLY);
Len Brown15aaa342012-03-29 22:19:58 -0400230 if (fd < 0)
231 return -1;
Len Brown103a8fe2010-10-22 23:53:03 -0400232
Len Brown15aaa342012-03-29 22:19:58 -0400233 retval = pread(fd, msr, sizeof *msr, offset);
Len Brown103a8fe2010-10-22 23:53:03 -0400234 close(fd);
Len Brown15aaa342012-03-29 22:19:58 -0400235
Len Brownd91bb172012-11-01 00:08:19 -0400236 if (retval != sizeof *msr) {
237 fprintf(stderr, "%s offset 0x%zx read failed\n", pathname, offset);
Len Brown15aaa342012-03-29 22:19:58 -0400238 return -1;
Len Brownd91bb172012-11-01 00:08:19 -0400239 }
Len Brown15aaa342012-03-29 22:19:58 -0400240
241 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400242}
243
Len Browna829eb42011-02-10 23:36:34 -0500244void print_header(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400245{
246 if (show_pkg)
Len Brownc98d5d92012-06-04 00:56:40 -0400247 outp += sprintf(outp, "pk");
Len Browne23da032012-02-06 18:37:16 -0500248 if (show_pkg)
Len Brownc98d5d92012-06-04 00:56:40 -0400249 outp += sprintf(outp, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400250 if (show_core)
Len Brownc98d5d92012-06-04 00:56:40 -0400251 outp += sprintf(outp, "cor");
Len Brown103a8fe2010-10-22 23:53:03 -0400252 if (show_cpu)
Len Brownc98d5d92012-06-04 00:56:40 -0400253 outp += sprintf(outp, " CPU");
Len Browne23da032012-02-06 18:37:16 -0500254 if (show_pkg || show_core || show_cpu)
Len Brownc98d5d92012-06-04 00:56:40 -0400255 outp += sprintf(outp, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400256 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400257 outp += sprintf(outp, " %%c0");
Len Brown103a8fe2010-10-22 23:53:03 -0400258 if (has_aperf)
Len Brownc98d5d92012-06-04 00:56:40 -0400259 outp += sprintf(outp, " GHz");
260 outp += sprintf(outp, " TSC");
Len Brown1ed51012013-02-10 17:19:24 -0500261 if (do_smi)
262 outp += sprintf(outp, " SMI");
Len Brown8e180f32012-09-22 01:25:08 -0400263 if (extra_delta_offset32)
Len Brownf9240812012-10-06 15:26:31 -0400264 outp += sprintf(outp, " count 0x%03X", extra_delta_offset32);
Len Brown8e180f32012-09-22 01:25:08 -0400265 if (extra_delta_offset64)
Len Brownf9240812012-10-06 15:26:31 -0400266 outp += sprintf(outp, " COUNT 0x%03X", extra_delta_offset64);
Len Brown2f32edf2012-09-21 23:45:46 -0400267 if (extra_msr_offset32)
Len Brown8e180f32012-09-22 01:25:08 -0400268 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset32);
Len Brown2f32edf2012-09-21 23:45:46 -0400269 if (extra_msr_offset64)
Len Brown8e180f32012-09-22 01:25:08 -0400270 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset64);
Len Brown103a8fe2010-10-22 23:53:03 -0400271 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400272 outp += sprintf(outp, " %%c1");
Len Brown144b44b2013-11-09 00:30:16 -0500273 if (do_nhm_cstates && !do_slm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400274 outp += sprintf(outp, " %%c3");
Len Brown103a8fe2010-10-22 23:53:03 -0400275 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400276 outp += sprintf(outp, " %%c6");
Len Brown103a8fe2010-10-22 23:53:03 -0400277 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400278 outp += sprintf(outp, " %%c7");
Len Brown889facb2012-11-08 00:48:57 -0500279
280 if (do_dts)
281 outp += sprintf(outp, " CTMP");
282 if (do_ptm)
283 outp += sprintf(outp, " PTMP");
284
Len Brown103a8fe2010-10-22 23:53:03 -0400285 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400286 outp += sprintf(outp, " %%pc2");
Len Brown144b44b2013-11-09 00:30:16 -0500287 if (do_nhm_cstates && !do_slm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400288 outp += sprintf(outp, " %%pc3");
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, " %%pc6");
Len Brown103a8fe2010-10-22 23:53:03 -0400291 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400292 outp += sprintf(outp, " %%pc7");
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800293 if (do_c8_c9_c10) {
294 outp += sprintf(outp, " %%pc8");
295 outp += sprintf(outp, " %%pc9");
296 outp += sprintf(outp, " %%pc10");
297 }
Len Brown103a8fe2010-10-22 23:53:03 -0400298
Len Brown889facb2012-11-08 00:48:57 -0500299 if (do_rapl & RAPL_PKG)
300 outp += sprintf(outp, " Pkg_W");
301 if (do_rapl & RAPL_CORES)
302 outp += sprintf(outp, " Cor_W");
303 if (do_rapl & RAPL_GFX)
304 outp += sprintf(outp, " GFX_W");
305 if (do_rapl & RAPL_DRAM)
306 outp += sprintf(outp, " RAM_W");
307 if (do_rapl & RAPL_PKG_PERF_STATUS)
308 outp += sprintf(outp, " PKG_%%");
309 if (do_rapl & RAPL_DRAM_PERF_STATUS)
310 outp += sprintf(outp, " RAM_%%");
311
Len Brownc98d5d92012-06-04 00:56:40 -0400312 outp += sprintf(outp, "\n");
Len Brown103a8fe2010-10-22 23:53:03 -0400313}
314
Len Brownc98d5d92012-06-04 00:56:40 -0400315int dump_counters(struct thread_data *t, struct core_data *c,
316 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400317{
Len Brownc98d5d92012-06-04 00:56:40 -0400318 fprintf(stderr, "t %p, c %p, p %p\n", t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400319
Len Brownc98d5d92012-06-04 00:56:40 -0400320 if (t) {
321 fprintf(stderr, "CPU: %d flags 0x%x\n", t->cpu_id, t->flags);
322 fprintf(stderr, "TSC: %016llX\n", t->tsc);
323 fprintf(stderr, "aperf: %016llX\n", t->aperf);
324 fprintf(stderr, "mperf: %016llX\n", t->mperf);
325 fprintf(stderr, "c1: %016llX\n", t->c1);
Len Brown8e180f32012-09-22 01:25:08 -0400326 fprintf(stderr, "msr0x%x: %08llX\n",
327 extra_delta_offset32, t->extra_delta32);
328 fprintf(stderr, "msr0x%x: %016llX\n",
329 extra_delta_offset64, t->extra_delta64);
330 fprintf(stderr, "msr0x%x: %08llX\n",
Len Brown2f32edf2012-09-21 23:45:46 -0400331 extra_msr_offset32, t->extra_msr32);
Len Brownc98d5d92012-06-04 00:56:40 -0400332 fprintf(stderr, "msr0x%x: %016llX\n",
Len Brown2f32edf2012-09-21 23:45:46 -0400333 extra_msr_offset64, t->extra_msr64);
Len Brown1ed51012013-02-10 17:19:24 -0500334 if (do_smi)
335 fprintf(stderr, "SMI: %08X\n", t->smi_count);
Len Brownc98d5d92012-06-04 00:56:40 -0400336 }
Len Brown103a8fe2010-10-22 23:53:03 -0400337
Len Brownc98d5d92012-06-04 00:56:40 -0400338 if (c) {
339 fprintf(stderr, "core: %d\n", c->core_id);
340 fprintf(stderr, "c3: %016llX\n", c->c3);
341 fprintf(stderr, "c6: %016llX\n", c->c6);
342 fprintf(stderr, "c7: %016llX\n", c->c7);
Len Brown889facb2012-11-08 00:48:57 -0500343 fprintf(stderr, "DTS: %dC\n", c->core_temp_c);
Len Brownc98d5d92012-06-04 00:56:40 -0400344 }
345
346 if (p) {
347 fprintf(stderr, "package: %d\n", p->package_id);
348 fprintf(stderr, "pc2: %016llX\n", p->pc2);
349 fprintf(stderr, "pc3: %016llX\n", p->pc3);
350 fprintf(stderr, "pc6: %016llX\n", p->pc6);
351 fprintf(stderr, "pc7: %016llX\n", p->pc7);
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800352 fprintf(stderr, "pc8: %016llX\n", p->pc8);
353 fprintf(stderr, "pc9: %016llX\n", p->pc9);
354 fprintf(stderr, "pc10: %016llX\n", p->pc10);
Len Brown889facb2012-11-08 00:48:57 -0500355 fprintf(stderr, "Joules PKG: %0X\n", p->energy_pkg);
356 fprintf(stderr, "Joules COR: %0X\n", p->energy_cores);
357 fprintf(stderr, "Joules GFX: %0X\n", p->energy_gfx);
358 fprintf(stderr, "Joules RAM: %0X\n", p->energy_dram);
359 fprintf(stderr, "Throttle PKG: %0X\n", p->rapl_pkg_perf_status);
360 fprintf(stderr, "Throttle RAM: %0X\n", p->rapl_dram_perf_status);
361 fprintf(stderr, "PTM: %dC\n", p->pkg_temp_c);
Len Brownc98d5d92012-06-04 00:56:40 -0400362 }
363 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400364}
365
Len Browne23da032012-02-06 18:37:16 -0500366/*
367 * column formatting convention & formats
368 * package: "pk" 2 columns %2d
369 * core: "cor" 3 columns %3d
370 * CPU: "CPU" 3 columns %3d
Len Brown889facb2012-11-08 00:48:57 -0500371 * Pkg_W: %6.2
372 * Cor_W: %6.2
373 * GFX_W: %5.2
374 * RAM_W: %5.2
Len Browne23da032012-02-06 18:37:16 -0500375 * GHz: "GHz" 3 columns %3.2
376 * TSC: "TSC" 3 columns %3.2
Len Brown1ed51012013-02-10 17:19:24 -0500377 * SMI: "SMI" 4 columns %4d
Len Browne23da032012-02-06 18:37:16 -0500378 * percentage " %pc3" %6.2
Len Brown889facb2012-11-08 00:48:57 -0500379 * Perf Status percentage: %5.2
380 * "CTMP" 4 columns %4d
Len Browne23da032012-02-06 18:37:16 -0500381 */
Len Brownc98d5d92012-06-04 00:56:40 -0400382int format_counters(struct thread_data *t, struct core_data *c,
383 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400384{
385 double interval_float;
Len Brown889facb2012-11-08 00:48:57 -0500386 char *fmt5, *fmt6;
Len Brown103a8fe2010-10-22 23:53:03 -0400387
Len Brownc98d5d92012-06-04 00:56:40 -0400388 /* if showing only 1st thread in core and this isn't one, bail out */
389 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
390 return 0;
391
392 /* if showing only 1st thread in pkg and this isn't one, bail out */
393 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
394 return 0;
395
Len Brown103a8fe2010-10-22 23:53:03 -0400396 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
397
Len Brownc98d5d92012-06-04 00:56:40 -0400398 /* topo columns, print blanks on 1st (average) line */
399 if (t == &average.threads) {
Len Brown103a8fe2010-10-22 23:53:03 -0400400 if (show_pkg)
Len Brownc98d5d92012-06-04 00:56:40 -0400401 outp += sprintf(outp, " ");
Len Browne23da032012-02-06 18:37:16 -0500402 if (show_pkg && show_core)
Len Brownc98d5d92012-06-04 00:56:40 -0400403 outp += sprintf(outp, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400404 if (show_core)
Len Brownc98d5d92012-06-04 00:56:40 -0400405 outp += sprintf(outp, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400406 if (show_cpu)
Len Brownc98d5d92012-06-04 00:56:40 -0400407 outp += sprintf(outp, " " " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400408 } else {
Len Brownc98d5d92012-06-04 00:56:40 -0400409 if (show_pkg) {
410 if (p)
411 outp += sprintf(outp, "%2d", p->package_id);
412 else
413 outp += sprintf(outp, " ");
414 }
Len Browne23da032012-02-06 18:37:16 -0500415 if (show_pkg && show_core)
Len Brownc98d5d92012-06-04 00:56:40 -0400416 outp += sprintf(outp, " ");
417 if (show_core) {
418 if (c)
419 outp += sprintf(outp, "%3d", c->core_id);
420 else
421 outp += sprintf(outp, " ");
422 }
Len Brown103a8fe2010-10-22 23:53:03 -0400423 if (show_cpu)
Len Brownc98d5d92012-06-04 00:56:40 -0400424 outp += sprintf(outp, " %3d", t->cpu_id);
Len Brown103a8fe2010-10-22 23:53:03 -0400425 }
Len Brown103a8fe2010-10-22 23:53:03 -0400426 /* %c0 */
427 if (do_nhm_cstates) {
Len Browne23da032012-02-06 18:37:16 -0500428 if (show_pkg || show_core || show_cpu)
Len Brownc98d5d92012-06-04 00:56:40 -0400429 outp += sprintf(outp, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400430 if (!skip_c0)
Len Brownc98d5d92012-06-04 00:56:40 -0400431 outp += sprintf(outp, "%6.2f", 100.0 * t->mperf/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400432 else
Len Brownc98d5d92012-06-04 00:56:40 -0400433 outp += sprintf(outp, " ****");
Len Brown103a8fe2010-10-22 23:53:03 -0400434 }
435
436 /* GHz */
437 if (has_aperf) {
438 if (!aperf_mperf_unstable) {
Len Brownc98d5d92012-06-04 00:56:40 -0400439 outp += sprintf(outp, " %3.2f",
440 1.0 * t->tsc / units * t->aperf /
441 t->mperf / interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400442 } else {
Len Brownc98d5d92012-06-04 00:56:40 -0400443 if (t->aperf > t->tsc || t->mperf > t->tsc) {
444 outp += sprintf(outp, " ***");
Len Brown103a8fe2010-10-22 23:53:03 -0400445 } else {
Len Brownc98d5d92012-06-04 00:56:40 -0400446 outp += sprintf(outp, "%3.1f*",
447 1.0 * t->tsc /
448 units * t->aperf /
449 t->mperf / interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400450 }
451 }
452 }
453
454 /* TSC */
Len Brownc98d5d92012-06-04 00:56:40 -0400455 outp += sprintf(outp, "%5.2f", 1.0 * t->tsc/units/interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400456
Len Brown1ed51012013-02-10 17:19:24 -0500457 /* SMI */
458 if (do_smi)
459 outp += sprintf(outp, "%4d", t->smi_count);
460
Len Brown8e180f32012-09-22 01:25:08 -0400461 /* delta */
462 if (extra_delta_offset32)
463 outp += sprintf(outp, " %11llu", t->extra_delta32);
464
465 /* DELTA */
466 if (extra_delta_offset64)
467 outp += sprintf(outp, " %11llu", t->extra_delta64);
Len Brown2f32edf2012-09-21 23:45:46 -0400468 /* msr */
469 if (extra_msr_offset32)
Len Brown8e180f32012-09-22 01:25:08 -0400470 outp += sprintf(outp, " 0x%08llx", t->extra_msr32);
Len Brown2f32edf2012-09-21 23:45:46 -0400471
Len Brown130ff302012-09-21 22:56:06 -0400472 /* MSR */
Len Brown2f32edf2012-09-21 23:45:46 -0400473 if (extra_msr_offset64)
474 outp += sprintf(outp, " 0x%016llx", t->extra_msr64);
Len Brown130ff302012-09-21 22:56:06 -0400475
Len Brown103a8fe2010-10-22 23:53:03 -0400476 if (do_nhm_cstates) {
477 if (!skip_c1)
Len Brownc98d5d92012-06-04 00:56:40 -0400478 outp += sprintf(outp, " %6.2f", 100.0 * t->c1/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400479 else
Len Brownc98d5d92012-06-04 00:56:40 -0400480 outp += sprintf(outp, " ****");
Len Brown103a8fe2010-10-22 23:53:03 -0400481 }
Len Brownc98d5d92012-06-04 00:56:40 -0400482
483 /* print per-core data only for 1st thread in core */
484 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
485 goto done;
486
Len Brown144b44b2013-11-09 00:30:16 -0500487 if (do_nhm_cstates && !do_slm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400488 outp += sprintf(outp, " %6.2f", 100.0 * c->c3/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400489 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400490 outp += sprintf(outp, " %6.2f", 100.0 * c->c6/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400491 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400492 outp += sprintf(outp, " %6.2f", 100.0 * c->c7/t->tsc);
493
Len Brown889facb2012-11-08 00:48:57 -0500494 if (do_dts)
495 outp += sprintf(outp, " %4d", c->core_temp_c);
496
Len Brownc98d5d92012-06-04 00:56:40 -0400497 /* print per-package data only for 1st core in package */
498 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
499 goto done;
500
Len Brown889facb2012-11-08 00:48:57 -0500501 if (do_ptm)
502 outp += sprintf(outp, " %4d", p->pkg_temp_c);
503
Len Brown103a8fe2010-10-22 23:53:03 -0400504 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400505 outp += sprintf(outp, " %6.2f", 100.0 * p->pc2/t->tsc);
Len Brown144b44b2013-11-09 00:30:16 -0500506 if (do_nhm_cstates && !do_slm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400507 outp += sprintf(outp, " %6.2f", 100.0 * p->pc3/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->pc6/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400510 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400511 outp += sprintf(outp, " %6.2f", 100.0 * p->pc7/t->tsc);
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800512 if (do_c8_c9_c10) {
513 outp += sprintf(outp, " %6.2f", 100.0 * p->pc8/t->tsc);
514 outp += sprintf(outp, " %6.2f", 100.0 * p->pc9/t->tsc);
515 outp += sprintf(outp, " %6.2f", 100.0 * p->pc10/t->tsc);
516 }
Len Brown889facb2012-11-08 00:48:57 -0500517
518 /*
519 * If measurement interval exceeds minimum RAPL Joule Counter range,
520 * indicate that results are suspect by printing "**" in fraction place.
521 */
522 if (interval_float < rapl_joule_counter_range) {
523 fmt5 = " %5.2f";
524 fmt6 = " %6.2f";
525 } else {
526 fmt5 = " %3.0f**";
527 fmt6 = " %4.0f**";
528 }
529
530 if (do_rapl & RAPL_PKG)
531 outp += sprintf(outp, fmt6, p->energy_pkg * rapl_energy_units / interval_float);
532 if (do_rapl & RAPL_CORES)
533 outp += sprintf(outp, fmt6, p->energy_cores * rapl_energy_units / interval_float);
534 if (do_rapl & RAPL_GFX)
535 outp += sprintf(outp, fmt5, p->energy_gfx * rapl_energy_units / interval_float);
536 if (do_rapl & RAPL_DRAM)
537 outp += sprintf(outp, fmt5, p->energy_dram * rapl_energy_units / interval_float);
538 if (do_rapl & RAPL_PKG_PERF_STATUS )
539 outp += sprintf(outp, fmt5, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
540 if (do_rapl & RAPL_DRAM_PERF_STATUS )
541 outp += sprintf(outp, fmt5, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
542
Len Brownc98d5d92012-06-04 00:56:40 -0400543done:
Len Brownc98d5d92012-06-04 00:56:40 -0400544 outp += sprintf(outp, "\n");
545
546 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400547}
548
Len Brownc98d5d92012-06-04 00:56:40 -0400549void flush_stdout()
Len Brown103a8fe2010-10-22 23:53:03 -0400550{
Len Brownc98d5d92012-06-04 00:56:40 -0400551 fputs(output_buffer, stdout);
Len Brownddac0d62012-11-30 01:01:40 -0500552 fflush(stdout);
Len Brownc98d5d92012-06-04 00:56:40 -0400553 outp = output_buffer;
554}
555void flush_stderr()
556{
557 fputs(output_buffer, stderr);
558 outp = output_buffer;
559}
560void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
561{
Len Browne23da032012-02-06 18:37:16 -0500562 static int printed;
Len Brown103a8fe2010-10-22 23:53:03 -0400563
Len Browne23da032012-02-06 18:37:16 -0500564 if (!printed || !summary_only)
565 print_header();
Len Brown103a8fe2010-10-22 23:53:03 -0400566
Len Brownc98d5d92012-06-04 00:56:40 -0400567 if (topo.num_cpus > 1)
568 format_counters(&average.threads, &average.cores,
569 &average.packages);
Len Brown103a8fe2010-10-22 23:53:03 -0400570
Len Browne23da032012-02-06 18:37:16 -0500571 printed = 1;
572
573 if (summary_only)
574 return;
575
Len Brownc98d5d92012-06-04 00:56:40 -0400576 for_all_cpus(format_counters, t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400577}
578
Len Brown889facb2012-11-08 00:48:57 -0500579#define DELTA_WRAP32(new, old) \
580 if (new > old) { \
581 old = new - old; \
582 } else { \
583 old = 0x100000000 + new - old; \
584 }
585
Len Brownc98d5d92012-06-04 00:56:40 -0400586void
587delta_package(struct pkg_data *new, struct pkg_data *old)
Len Brown103a8fe2010-10-22 23:53:03 -0400588{
Len Brownc98d5d92012-06-04 00:56:40 -0400589 old->pc2 = new->pc2 - old->pc2;
590 old->pc3 = new->pc3 - old->pc3;
591 old->pc6 = new->pc6 - old->pc6;
592 old->pc7 = new->pc7 - old->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800593 old->pc8 = new->pc8 - old->pc8;
594 old->pc9 = new->pc9 - old->pc9;
595 old->pc10 = new->pc10 - old->pc10;
Len Brown889facb2012-11-08 00:48:57 -0500596 old->pkg_temp_c = new->pkg_temp_c;
597
598 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
599 DELTA_WRAP32(new->energy_cores, old->energy_cores);
600 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
601 DELTA_WRAP32(new->energy_dram, old->energy_dram);
602 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
603 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
Len Brownc98d5d92012-06-04 00:56:40 -0400604}
Len Brown103a8fe2010-10-22 23:53:03 -0400605
Len Brownc98d5d92012-06-04 00:56:40 -0400606void
607delta_core(struct core_data *new, struct core_data *old)
608{
609 old->c3 = new->c3 - old->c3;
610 old->c6 = new->c6 - old->c6;
611 old->c7 = new->c7 - old->c7;
Len Brown889facb2012-11-08 00:48:57 -0500612 old->core_temp_c = new->core_temp_c;
Len Brownc98d5d92012-06-04 00:56:40 -0400613}
Len Brown103a8fe2010-10-22 23:53:03 -0400614
Len Brownc3ae3312012-06-13 21:31:46 -0400615/*
616 * old = new - old
617 */
Len Brownc98d5d92012-06-04 00:56:40 -0400618void
619delta_thread(struct thread_data *new, struct thread_data *old,
620 struct core_data *core_delta)
621{
622 old->tsc = new->tsc - old->tsc;
Len Brown103a8fe2010-10-22 23:53:03 -0400623
Len Brownc98d5d92012-06-04 00:56:40 -0400624 /* check for TSC < 1 Mcycles over interval */
625 if (old->tsc < (1000 * 1000)) {
626 fprintf(stderr, "Insanely slow TSC rate, TSC stops in idle?\n");
627 fprintf(stderr, "You can disable all c-states by booting with \"idle=poll\"\n");
628 fprintf(stderr, "or just the deep ones with \"processor.max_cstate=1\"\n");
629 exit(-3);
630 }
Len Brown103a8fe2010-10-22 23:53:03 -0400631
Len Brownc98d5d92012-06-04 00:56:40 -0400632 old->c1 = new->c1 - old->c1;
Len Brown103a8fe2010-10-22 23:53:03 -0400633
Len Brownc98d5d92012-06-04 00:56:40 -0400634 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
635 old->aperf = new->aperf - old->aperf;
636 old->mperf = new->mperf - old->mperf;
637 } else {
Len Brown103a8fe2010-10-22 23:53:03 -0400638
Len Brownc98d5d92012-06-04 00:56:40 -0400639 if (!aperf_mperf_unstable) {
640 fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname);
641 fprintf(stderr, "* Frequency results do not cover entire interval *\n");
642 fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n");
643
644 aperf_mperf_unstable = 1;
645 }
Len Brown103a8fe2010-10-22 23:53:03 -0400646 /*
Len Brownc98d5d92012-06-04 00:56:40 -0400647 * mperf delta is likely a huge "positive" number
648 * can not use it for calculating c0 time
Len Brown103a8fe2010-10-22 23:53:03 -0400649 */
Len Brownc98d5d92012-06-04 00:56:40 -0400650 skip_c0 = 1;
651 skip_c1 = 1;
652 }
Len Brown103a8fe2010-10-22 23:53:03 -0400653
Len Brown103a8fe2010-10-22 23:53:03 -0400654
Len Brown144b44b2013-11-09 00:30:16 -0500655 if (use_c1_residency_msr) {
656 /*
657 * Some models have a dedicated C1 residency MSR,
658 * which should be more accurate than the derivation below.
659 */
660 } else {
661 /*
662 * As counter collection is not atomic,
663 * it is possible for mperf's non-halted cycles + idle states
664 * to exceed TSC's all cycles: show c1 = 0% in that case.
665 */
666 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
667 old->c1 = 0;
668 else {
669 /* normal case, derive c1 */
670 old->c1 = old->tsc - old->mperf - core_delta->c3
Len Brownc98d5d92012-06-04 00:56:40 -0400671 - core_delta->c6 - core_delta->c7;
Len Brown144b44b2013-11-09 00:30:16 -0500672 }
Len Brownc98d5d92012-06-04 00:56:40 -0400673 }
Len Brownc3ae3312012-06-13 21:31:46 -0400674
Len Brownc98d5d92012-06-04 00:56:40 -0400675 if (old->mperf == 0) {
Len Brownc3ae3312012-06-13 21:31:46 -0400676 if (verbose > 1) fprintf(stderr, "cpu%d MPERF 0!\n", old->cpu_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400677 old->mperf = 1; /* divide by 0 protection */
678 }
679
Len Brown8e180f32012-09-22 01:25:08 -0400680 old->extra_delta32 = new->extra_delta32 - old->extra_delta32;
681 old->extra_delta32 &= 0xFFFFFFFF;
682
683 old->extra_delta64 = new->extra_delta64 - old->extra_delta64;
684
Len Brownc98d5d92012-06-04 00:56:40 -0400685 /*
Len Brown8e180f32012-09-22 01:25:08 -0400686 * Extra MSR is just a snapshot, simply copy latest w/o subtracting
Len Brownc98d5d92012-06-04 00:56:40 -0400687 */
Len Brown2f32edf2012-09-21 23:45:46 -0400688 old->extra_msr32 = new->extra_msr32;
689 old->extra_msr64 = new->extra_msr64;
Len Brown1ed51012013-02-10 17:19:24 -0500690
691 if (do_smi)
692 old->smi_count = new->smi_count - old->smi_count;
Len Brownc98d5d92012-06-04 00:56:40 -0400693}
694
695int delta_cpu(struct thread_data *t, struct core_data *c,
696 struct pkg_data *p, struct thread_data *t2,
697 struct core_data *c2, struct pkg_data *p2)
698{
699 /* calculate core delta only for 1st thread in core */
700 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
701 delta_core(c, c2);
702
703 /* always calculate thread delta */
704 delta_thread(t, t2, c2); /* c2 is core delta */
705
706 /* calculate package delta only for 1st core in package */
707 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
708 delta_package(p, p2);
709
710 return 0;
711}
712
713void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
714{
715 t->tsc = 0;
716 t->aperf = 0;
717 t->mperf = 0;
718 t->c1 = 0;
719
Len Brown1ed51012013-02-10 17:19:24 -0500720 t->smi_count = 0;
Len Brown8e180f32012-09-22 01:25:08 -0400721 t->extra_delta32 = 0;
722 t->extra_delta64 = 0;
723
Len Brownc98d5d92012-06-04 00:56:40 -0400724 /* tells format_counters to dump all fields from this set */
725 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
726
727 c->c3 = 0;
728 c->c6 = 0;
729 c->c7 = 0;
Len Brown889facb2012-11-08 00:48:57 -0500730 c->core_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -0400731
732 p->pc2 = 0;
733 p->pc3 = 0;
734 p->pc6 = 0;
735 p->pc7 = 0;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800736 p->pc8 = 0;
737 p->pc9 = 0;
738 p->pc10 = 0;
Len Brown889facb2012-11-08 00:48:57 -0500739
740 p->energy_pkg = 0;
741 p->energy_dram = 0;
742 p->energy_cores = 0;
743 p->energy_gfx = 0;
744 p->rapl_pkg_perf_status = 0;
745 p->rapl_dram_perf_status = 0;
746 p->pkg_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -0400747}
748int sum_counters(struct thread_data *t, struct core_data *c,
749 struct pkg_data *p)
750{
751 average.threads.tsc += t->tsc;
752 average.threads.aperf += t->aperf;
753 average.threads.mperf += t->mperf;
754 average.threads.c1 += t->c1;
755
Len Brown8e180f32012-09-22 01:25:08 -0400756 average.threads.extra_delta32 += t->extra_delta32;
757 average.threads.extra_delta64 += t->extra_delta64;
758
Len Brownc98d5d92012-06-04 00:56:40 -0400759 /* sum per-core values only for 1st thread in core */
760 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
761 return 0;
762
763 average.cores.c3 += c->c3;
764 average.cores.c6 += c->c6;
765 average.cores.c7 += c->c7;
766
Len Brown889facb2012-11-08 00:48:57 -0500767 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
768
Len Brownc98d5d92012-06-04 00:56:40 -0400769 /* sum per-pkg values only for 1st core in pkg */
770 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
771 return 0;
772
773 average.packages.pc2 += p->pc2;
774 average.packages.pc3 += p->pc3;
775 average.packages.pc6 += p->pc6;
776 average.packages.pc7 += p->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800777 average.packages.pc8 += p->pc8;
778 average.packages.pc9 += p->pc9;
779 average.packages.pc10 += p->pc10;
Len Brownc98d5d92012-06-04 00:56:40 -0400780
Len Brown889facb2012-11-08 00:48:57 -0500781 average.packages.energy_pkg += p->energy_pkg;
782 average.packages.energy_dram += p->energy_dram;
783 average.packages.energy_cores += p->energy_cores;
784 average.packages.energy_gfx += p->energy_gfx;
785
786 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
787
788 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
789 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
Len Brownc98d5d92012-06-04 00:56:40 -0400790 return 0;
791}
792/*
793 * sum the counters for all cpus in the system
794 * compute the weighted average
795 */
796void compute_average(struct thread_data *t, struct core_data *c,
797 struct pkg_data *p)
798{
799 clear_counters(&average.threads, &average.cores, &average.packages);
800
801 for_all_cpus(sum_counters, t, c, p);
802
803 average.threads.tsc /= topo.num_cpus;
804 average.threads.aperf /= topo.num_cpus;
805 average.threads.mperf /= topo.num_cpus;
806 average.threads.c1 /= topo.num_cpus;
807
Len Brown8e180f32012-09-22 01:25:08 -0400808 average.threads.extra_delta32 /= topo.num_cpus;
809 average.threads.extra_delta32 &= 0xFFFFFFFF;
810
811 average.threads.extra_delta64 /= topo.num_cpus;
812
Len Brownc98d5d92012-06-04 00:56:40 -0400813 average.cores.c3 /= topo.num_cores;
814 average.cores.c6 /= topo.num_cores;
815 average.cores.c7 /= topo.num_cores;
816
817 average.packages.pc2 /= topo.num_packages;
818 average.packages.pc3 /= topo.num_packages;
819 average.packages.pc6 /= topo.num_packages;
820 average.packages.pc7 /= topo.num_packages;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800821
822 average.packages.pc8 /= topo.num_packages;
823 average.packages.pc9 /= topo.num_packages;
824 average.packages.pc10 /= topo.num_packages;
Len Brownc98d5d92012-06-04 00:56:40 -0400825}
826
827static unsigned long long rdtsc(void)
828{
829 unsigned int low, high;
830
831 asm volatile("rdtsc" : "=a" (low), "=d" (high));
832
833 return low | ((unsigned long long)high) << 32;
834}
835
836
837/*
838 * get_counters(...)
839 * migrate to cpu
840 * acquire and record local counters for that cpu
841 */
842int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
843{
844 int cpu = t->cpu_id;
Len Brown889facb2012-11-08 00:48:57 -0500845 unsigned long long msr;
Len Brownc98d5d92012-06-04 00:56:40 -0400846
Len Browne52966c2012-11-08 22:38:05 -0500847 if (cpu_migrate(cpu)) {
848 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
Len Brownc98d5d92012-06-04 00:56:40 -0400849 return -1;
Len Browne52966c2012-11-08 22:38:05 -0500850 }
Len Brownc98d5d92012-06-04 00:56:40 -0400851
852 t->tsc = rdtsc(); /* we are running on local CPU of interest */
853
854 if (has_aperf) {
Len Brown9c63a652012-10-31 01:29:52 -0400855 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
Len Brownc98d5d92012-06-04 00:56:40 -0400856 return -3;
Len Brown9c63a652012-10-31 01:29:52 -0400857 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
Len Brownc98d5d92012-06-04 00:56:40 -0400858 return -4;
859 }
860
Len Brown1ed51012013-02-10 17:19:24 -0500861 if (do_smi) {
862 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
863 return -5;
864 t->smi_count = msr & 0xFFFFFFFF;
865 }
Len Brown8e180f32012-09-22 01:25:08 -0400866 if (extra_delta_offset32) {
Len Brown889facb2012-11-08 00:48:57 -0500867 if (get_msr(cpu, extra_delta_offset32, &msr))
Len Brown2f32edf2012-09-21 23:45:46 -0400868 return -5;
Len Brown889facb2012-11-08 00:48:57 -0500869 t->extra_delta32 = msr & 0xFFFFFFFF;
Len Brown8e180f32012-09-22 01:25:08 -0400870 }
871
872 if (extra_delta_offset64)
873 if (get_msr(cpu, extra_delta_offset64, &t->extra_delta64))
874 return -5;
875
876 if (extra_msr_offset32) {
Len Brown889facb2012-11-08 00:48:57 -0500877 if (get_msr(cpu, extra_msr_offset32, &msr))
Len Brown8e180f32012-09-22 01:25:08 -0400878 return -5;
Len Brown889facb2012-11-08 00:48:57 -0500879 t->extra_msr32 = msr & 0xFFFFFFFF;
Len Brown8e180f32012-09-22 01:25:08 -0400880 }
Len Brown2f32edf2012-09-21 23:45:46 -0400881
882 if (extra_msr_offset64)
883 if (get_msr(cpu, extra_msr_offset64, &t->extra_msr64))
Len Brownc98d5d92012-06-04 00:56:40 -0400884 return -5;
885
Len Brown144b44b2013-11-09 00:30:16 -0500886 if (use_c1_residency_msr) {
887 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
888 return -6;
889 }
890
Len Brownc98d5d92012-06-04 00:56:40 -0400891 /* collect core counters only for 1st thread in core */
892 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
893 return 0;
894
Len Brown144b44b2013-11-09 00:30:16 -0500895 if (do_nhm_cstates && !do_slm_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -0400896 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
897 return -6;
Len Brown144b44b2013-11-09 00:30:16 -0500898 }
899
900 if (do_nhm_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -0400901 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
902 return -7;
903 }
904
905 if (do_snb_cstates)
906 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
907 return -8;
908
Len Brown889facb2012-11-08 00:48:57 -0500909 if (do_dts) {
910 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
911 return -9;
912 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
913 }
914
915
Len Brownc98d5d92012-06-04 00:56:40 -0400916 /* collect package counters only for 1st core in package */
917 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
918 return 0;
919
Len Brown144b44b2013-11-09 00:30:16 -0500920 if (do_nhm_cstates && !do_slm_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -0400921 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
922 return -9;
923 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
924 return -10;
925 }
926 if (do_snb_cstates) {
927 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
928 return -11;
929 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
930 return -12;
Len Brown103a8fe2010-10-22 23:53:03 -0400931 }
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800932 if (do_c8_c9_c10) {
933 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
934 return -13;
935 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
936 return -13;
937 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
938 return -13;
939 }
Len Brown889facb2012-11-08 00:48:57 -0500940 if (do_rapl & RAPL_PKG) {
941 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
942 return -13;
943 p->energy_pkg = msr & 0xFFFFFFFF;
944 }
945 if (do_rapl & RAPL_CORES) {
946 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
947 return -14;
948 p->energy_cores = msr & 0xFFFFFFFF;
949 }
950 if (do_rapl & RAPL_DRAM) {
951 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
952 return -15;
953 p->energy_dram = msr & 0xFFFFFFFF;
954 }
955 if (do_rapl & RAPL_GFX) {
956 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
957 return -16;
958 p->energy_gfx = msr & 0xFFFFFFFF;
959 }
960 if (do_rapl & RAPL_PKG_PERF_STATUS) {
961 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
962 return -16;
963 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
964 }
965 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
966 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
967 return -16;
968 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
969 }
970 if (do_ptm) {
971 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
972 return -17;
973 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
974 }
Len Brown103a8fe2010-10-22 23:53:03 -0400975 return 0;
976}
977
Len Brownc98d5d92012-06-04 00:56:40 -0400978void print_verbose_header(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400979{
980 unsigned long long msr;
981 unsigned int ratio;
982
983 if (!do_nehalem_platform_info)
984 return;
985
Len Brown9c63a652012-10-31 01:29:52 -0400986 get_msr(0, MSR_NHM_PLATFORM_INFO, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -0400987
Len Brown67920412013-01-31 15:22:15 -0500988 fprintf(stderr, "cpu0: MSR_NHM_PLATFORM_INFO: 0x%08llx\n", msr);
Len Brown6574a5d2012-09-21 00:01:31 -0400989
Len Brown103a8fe2010-10-22 23:53:03 -0400990 ratio = (msr >> 40) & 0xFF;
991 fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency\n",
992 ratio, bclk, ratio * bclk);
993
994 ratio = (msr >> 8) & 0xFF;
995 fprintf(stderr, "%d * %.0f = %.0f MHz TSC frequency\n",
996 ratio, bclk, ratio * bclk);
997
Len Brown67920412013-01-31 15:22:15 -0500998 get_msr(0, MSR_IA32_POWER_CTL, &msr);
Len Brown144b44b2013-11-09 00:30:16 -0500999 fprintf(stderr, "cpu0: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
Len Brown67920412013-01-31 15:22:15 -05001000 msr, msr & 0x2 ? "EN" : "DIS");
1001
Len Brown6574a5d2012-09-21 00:01:31 -04001002 if (!do_ivt_turbo_ratio_limit)
1003 goto print_nhm_turbo_ratio_limits;
1004
1005 get_msr(0, MSR_IVT_TURBO_RATIO_LIMIT, &msr);
1006
Len Brown67920412013-01-31 15:22:15 -05001007 fprintf(stderr, "cpu0: MSR_IVT_TURBO_RATIO_LIMIT: 0x%08llx\n", msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001008
1009 ratio = (msr >> 56) & 0xFF;
1010 if (ratio)
1011 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 16 active cores\n",
1012 ratio, bclk, ratio * bclk);
1013
1014 ratio = (msr >> 48) & 0xFF;
1015 if (ratio)
1016 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 15 active cores\n",
1017 ratio, bclk, ratio * bclk);
1018
1019 ratio = (msr >> 40) & 0xFF;
1020 if (ratio)
1021 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 14 active cores\n",
1022 ratio, bclk, ratio * bclk);
1023
1024 ratio = (msr >> 32) & 0xFF;
1025 if (ratio)
1026 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 13 active cores\n",
1027 ratio, bclk, ratio * bclk);
1028
1029 ratio = (msr >> 24) & 0xFF;
1030 if (ratio)
1031 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 12 active cores\n",
1032 ratio, bclk, ratio * bclk);
1033
1034 ratio = (msr >> 16) & 0xFF;
1035 if (ratio)
1036 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 11 active cores\n",
1037 ratio, bclk, ratio * bclk);
1038
1039 ratio = (msr >> 8) & 0xFF;
1040 if (ratio)
1041 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 10 active cores\n",
1042 ratio, bclk, ratio * bclk);
1043
1044 ratio = (msr >> 0) & 0xFF;
1045 if (ratio)
1046 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 9 active cores\n",
1047 ratio, bclk, ratio * bclk);
1048
1049print_nhm_turbo_ratio_limits:
Len Brown889facb2012-11-08 00:48:57 -05001050 get_msr(0, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
1051
1052#define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
1053#define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
1054
1055 fprintf(stderr, "cpu0: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", msr);
1056
1057 fprintf(stderr, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: ",
1058 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
1059 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
1060 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
1061 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
1062 (msr & (1 << 15)) ? "" : "UN",
1063 (unsigned int)msr & 7);
1064
1065
1066 switch(msr & 0x7) {
1067 case 0:
Len Brown144b44b2013-11-09 00:30:16 -05001068 fprintf(stderr, do_slm_cstates ? "no pkg states" : "pc0");
Len Brown889facb2012-11-08 00:48:57 -05001069 break;
1070 case 1:
Len Brown144b44b2013-11-09 00:30:16 -05001071 fprintf(stderr, do_slm_cstates ? "no pkg states" : do_snb_cstates ? "pc2" : "pc0");
Len Brown889facb2012-11-08 00:48:57 -05001072 break;
1073 case 2:
Len Brown144b44b2013-11-09 00:30:16 -05001074 fprintf(stderr, do_slm_cstates ? "invalid" : do_snb_cstates ? "pc6-noret" : "pc3");
Len Brown889facb2012-11-08 00:48:57 -05001075 break;
1076 case 3:
Len Brown144b44b2013-11-09 00:30:16 -05001077 fprintf(stderr, do_slm_cstates ? "invalid" : "pc6");
Len Brown889facb2012-11-08 00:48:57 -05001078 break;
1079 case 4:
Len Brown144b44b2013-11-09 00:30:16 -05001080 fprintf(stderr, do_slm_cstates ? "pc4" : "pc7");
Len Brown889facb2012-11-08 00:48:57 -05001081 break;
1082 case 5:
Len Brown144b44b2013-11-09 00:30:16 -05001083 fprintf(stderr, do_slm_cstates ? "invalid" : do_snb_cstates ? "pc7s" : "invalid");
1084 break;
1085 case 6:
1086 fprintf(stderr, do_slm_cstates ? "pc6" : "invalid");
Len Brown889facb2012-11-08 00:48:57 -05001087 break;
1088 case 7:
Len Brown144b44b2013-11-09 00:30:16 -05001089 fprintf(stderr, do_slm_cstates ? "pc7" : "unlimited");
Len Brown889facb2012-11-08 00:48:57 -05001090 break;
1091 default:
1092 fprintf(stderr, "invalid");
1093 }
1094 fprintf(stderr, ")\n");
Len Brown103a8fe2010-10-22 23:53:03 -04001095
1096 if (!do_nehalem_turbo_ratio_limit)
1097 return;
1098
Len Brown9c63a652012-10-31 01:29:52 -04001099 get_msr(0, MSR_NHM_TURBO_RATIO_LIMIT, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001100
Len Brown67920412013-01-31 15:22:15 -05001101 fprintf(stderr, "cpu0: MSR_NHM_TURBO_RATIO_LIMIT: 0x%08llx\n", msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001102
1103 ratio = (msr >> 56) & 0xFF;
1104 if (ratio)
1105 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 8 active cores\n",
1106 ratio, bclk, ratio * bclk);
1107
1108 ratio = (msr >> 48) & 0xFF;
1109 if (ratio)
1110 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 7 active cores\n",
1111 ratio, bclk, ratio * bclk);
1112
1113 ratio = (msr >> 40) & 0xFF;
1114 if (ratio)
1115 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 6 active cores\n",
1116 ratio, bclk, ratio * bclk);
1117
1118 ratio = (msr >> 32) & 0xFF;
1119 if (ratio)
1120 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 5 active cores\n",
1121 ratio, bclk, ratio * bclk);
1122
Len Brown103a8fe2010-10-22 23:53:03 -04001123 ratio = (msr >> 24) & 0xFF;
1124 if (ratio)
1125 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
1126 ratio, bclk, ratio * bclk);
1127
1128 ratio = (msr >> 16) & 0xFF;
1129 if (ratio)
1130 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
1131 ratio, bclk, ratio * bclk);
1132
1133 ratio = (msr >> 8) & 0xFF;
1134 if (ratio)
1135 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
1136 ratio, bclk, ratio * bclk);
1137
1138 ratio = (msr >> 0) & 0xFF;
1139 if (ratio)
1140 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
1141 ratio, bclk, ratio * bclk);
Len Brown103a8fe2010-10-22 23:53:03 -04001142}
1143
Len Brownc98d5d92012-06-04 00:56:40 -04001144void free_all_buffers(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001145{
Len Brownc98d5d92012-06-04 00:56:40 -04001146 CPU_FREE(cpu_present_set);
1147 cpu_present_set = NULL;
1148 cpu_present_set = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001149
Len Brownc98d5d92012-06-04 00:56:40 -04001150 CPU_FREE(cpu_affinity_set);
1151 cpu_affinity_set = NULL;
1152 cpu_affinity_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001153
Len Brownc98d5d92012-06-04 00:56:40 -04001154 free(thread_even);
1155 free(core_even);
1156 free(package_even);
1157
1158 thread_even = NULL;
1159 core_even = NULL;
1160 package_even = NULL;
1161
1162 free(thread_odd);
1163 free(core_odd);
1164 free(package_odd);
1165
1166 thread_odd = NULL;
1167 core_odd = NULL;
1168 package_odd = NULL;
1169
1170 free(output_buffer);
1171 output_buffer = NULL;
1172 outp = NULL;
Len Brown103a8fe2010-10-22 23:53:03 -04001173}
1174
Len Brownc98d5d92012-06-04 00:56:40 -04001175/*
1176 * cpu_is_first_sibling_in_core(cpu)
1177 * return 1 if given CPU is 1st HT sibling in the core
1178 */
1179int cpu_is_first_sibling_in_core(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001180{
Len Brownc98d5d92012-06-04 00:56:40 -04001181 char path[64];
1182 FILE *filep;
1183 int first_cpu;
Len Brown103a8fe2010-10-22 23:53:03 -04001184
Len Brownc98d5d92012-06-04 00:56:40 -04001185 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
1186 filep = fopen(path, "r");
1187 if (filep == NULL) {
1188 perror(path);
1189 exit(1);
1190 }
1191 fscanf(filep, "%d", &first_cpu);
1192 fclose(filep);
1193 return (cpu == first_cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001194}
1195
Len Brownc98d5d92012-06-04 00:56:40 -04001196/*
1197 * cpu_is_first_core_in_package(cpu)
1198 * return 1 if given CPU is 1st core in package
1199 */
1200int cpu_is_first_core_in_package(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001201{
Len Brownc98d5d92012-06-04 00:56:40 -04001202 char path[64];
1203 FILE *filep;
1204 int first_cpu;
Len Brown103a8fe2010-10-22 23:53:03 -04001205
Len Brownc98d5d92012-06-04 00:56:40 -04001206 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
1207 filep = fopen(path, "r");
1208 if (filep == NULL) {
1209 perror(path);
Len Brown103a8fe2010-10-22 23:53:03 -04001210 exit(1);
1211 }
Len Brownc98d5d92012-06-04 00:56:40 -04001212 fscanf(filep, "%d", &first_cpu);
1213 fclose(filep);
1214 return (cpu == first_cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001215}
1216
1217int get_physical_package_id(int cpu)
1218{
Len Brownc98d5d92012-06-04 00:56:40 -04001219 char path[80];
Len Brown103a8fe2010-10-22 23:53:03 -04001220 FILE *filep;
1221 int pkg;
1222
1223 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
1224 filep = fopen(path, "r");
1225 if (filep == NULL) {
1226 perror(path);
1227 exit(1);
1228 }
1229 fscanf(filep, "%d", &pkg);
1230 fclose(filep);
1231 return pkg;
1232}
1233
1234int get_core_id(int cpu)
1235{
Len Brownc98d5d92012-06-04 00:56:40 -04001236 char path[80];
Len Brown103a8fe2010-10-22 23:53:03 -04001237 FILE *filep;
1238 int core;
1239
1240 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
1241 filep = fopen(path, "r");
1242 if (filep == NULL) {
1243 perror(path);
1244 exit(1);
1245 }
1246 fscanf(filep, "%d", &core);
1247 fclose(filep);
1248 return core;
1249}
1250
Len Brownc98d5d92012-06-04 00:56:40 -04001251int get_num_ht_siblings(int cpu)
1252{
1253 char path[80];
1254 FILE *filep;
1255 int sib1, sib2;
1256 int matches;
1257 char character;
1258
1259 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
1260 filep = fopen(path, "r");
1261 if (filep == NULL) {
1262 perror(path);
1263 exit(1);
1264 }
1265 /*
1266 * file format:
1267 * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4)
1268 * otherwinse 1 sibling (self).
1269 */
1270 matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2);
1271
1272 fclose(filep);
1273
1274 if (matches == 3)
1275 return 2;
1276 else
1277 return 1;
1278}
1279
Len Brown103a8fe2010-10-22 23:53:03 -04001280/*
Len Brownc98d5d92012-06-04 00:56:40 -04001281 * run func(thread, core, package) in topology order
1282 * skip non-present cpus
Len Brown103a8fe2010-10-22 23:53:03 -04001283 */
1284
Len Brownc98d5d92012-06-04 00:56:40 -04001285int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
1286 struct pkg_data *, struct thread_data *, struct core_data *,
1287 struct pkg_data *), struct thread_data *thread_base,
1288 struct core_data *core_base, struct pkg_data *pkg_base,
1289 struct thread_data *thread_base2, struct core_data *core_base2,
1290 struct pkg_data *pkg_base2)
1291{
1292 int retval, pkg_no, core_no, thread_no;
1293
1294 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
1295 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
1296 for (thread_no = 0; thread_no <
1297 topo.num_threads_per_core; ++thread_no) {
1298 struct thread_data *t, *t2;
1299 struct core_data *c, *c2;
1300 struct pkg_data *p, *p2;
1301
1302 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
1303
1304 if (cpu_is_not_present(t->cpu_id))
1305 continue;
1306
1307 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
1308
1309 c = GET_CORE(core_base, core_no, pkg_no);
1310 c2 = GET_CORE(core_base2, core_no, pkg_no);
1311
1312 p = GET_PKG(pkg_base, pkg_no);
1313 p2 = GET_PKG(pkg_base2, pkg_no);
1314
1315 retval = func(t, c, p, t2, c2, p2);
1316 if (retval)
1317 return retval;
1318 }
1319 }
1320 }
1321 return 0;
1322}
1323
1324/*
1325 * run func(cpu) on every cpu in /proc/stat
1326 * return max_cpu number
1327 */
1328int for_all_proc_cpus(int (func)(int))
Len Brown103a8fe2010-10-22 23:53:03 -04001329{
1330 FILE *fp;
Len Brownc98d5d92012-06-04 00:56:40 -04001331 int cpu_num;
Len Brown103a8fe2010-10-22 23:53:03 -04001332 int retval;
1333
1334 fp = fopen(proc_stat, "r");
1335 if (fp == NULL) {
1336 perror(proc_stat);
1337 exit(1);
1338 }
1339
1340 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
1341 if (retval != 0) {
1342 perror("/proc/stat format");
1343 exit(1);
1344 }
1345
Len Brownc98d5d92012-06-04 00:56:40 -04001346 while (1) {
1347 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 -04001348 if (retval != 1)
1349 break;
1350
Len Brownc98d5d92012-06-04 00:56:40 -04001351 retval = func(cpu_num);
1352 if (retval) {
1353 fclose(fp);
1354 return(retval);
1355 }
Len Brown103a8fe2010-10-22 23:53:03 -04001356 }
1357 fclose(fp);
Len Brownc98d5d92012-06-04 00:56:40 -04001358 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001359}
1360
1361void re_initialize(void)
1362{
Len Brownc98d5d92012-06-04 00:56:40 -04001363 free_all_buffers();
1364 setup_all_buffers();
1365 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04001366}
1367
Len Brownc98d5d92012-06-04 00:56:40 -04001368
Len Brown103a8fe2010-10-22 23:53:03 -04001369/*
Len Brownc98d5d92012-06-04 00:56:40 -04001370 * count_cpus()
1371 * remember the last one seen, it will be the max
Len Brown103a8fe2010-10-22 23:53:03 -04001372 */
Len Brownc98d5d92012-06-04 00:56:40 -04001373int count_cpus(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001374{
Len Brownc98d5d92012-06-04 00:56:40 -04001375 if (topo.max_cpu_num < cpu)
1376 topo.max_cpu_num = cpu;
Len Brown103a8fe2010-10-22 23:53:03 -04001377
Len Brownc98d5d92012-06-04 00:56:40 -04001378 topo.num_cpus += 1;
1379 return 0;
1380}
1381int mark_cpu_present(int cpu)
1382{
1383 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brown15aaa342012-03-29 22:19:58 -04001384 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001385}
1386
1387void turbostat_loop()
1388{
Len Brownc98d5d92012-06-04 00:56:40 -04001389 int retval;
Len Browne52966c2012-11-08 22:38:05 -05001390 int restarted = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001391
Len Brown103a8fe2010-10-22 23:53:03 -04001392restart:
Len Browne52966c2012-11-08 22:38:05 -05001393 restarted++;
1394
Len Brownc98d5d92012-06-04 00:56:40 -04001395 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001396 if (retval < -1) {
1397 exit(retval);
1398 } else if (retval == -1) {
Len Browne52966c2012-11-08 22:38:05 -05001399 if (restarted > 1) {
1400 exit(retval);
1401 }
Len Brownc98d5d92012-06-04 00:56:40 -04001402 re_initialize();
1403 goto restart;
1404 }
Len Browne52966c2012-11-08 22:38:05 -05001405 restarted = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001406 gettimeofday(&tv_even, (struct timezone *)NULL);
1407
1408 while (1) {
Len Brownc98d5d92012-06-04 00:56:40 -04001409 if (for_all_proc_cpus(cpu_is_not_present)) {
Len Brown103a8fe2010-10-22 23:53:03 -04001410 re_initialize();
1411 goto restart;
1412 }
1413 sleep(interval_sec);
Len Brownc98d5d92012-06-04 00:56:40 -04001414 retval = for_all_cpus(get_counters, ODD_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001415 if (retval < -1) {
1416 exit(retval);
1417 } else if (retval == -1) {
Len Brown15aaa342012-03-29 22:19:58 -04001418 re_initialize();
1419 goto restart;
1420 }
Len Brown103a8fe2010-10-22 23:53:03 -04001421 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04001422 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04001423 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
1424 compute_average(EVEN_COUNTERS);
1425 format_all_counters(EVEN_COUNTERS);
1426 flush_stdout();
Len Brown15aaa342012-03-29 22:19:58 -04001427 sleep(interval_sec);
Len Brownc98d5d92012-06-04 00:56:40 -04001428 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001429 if (retval < -1) {
1430 exit(retval);
1431 } else if (retval == -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04001432 re_initialize();
1433 goto restart;
1434 }
Len Brown103a8fe2010-10-22 23:53:03 -04001435 gettimeofday(&tv_even, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04001436 timersub(&tv_even, &tv_odd, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04001437 for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS);
1438 compute_average(ODD_COUNTERS);
1439 format_all_counters(ODD_COUNTERS);
1440 flush_stdout();
Len Brown103a8fe2010-10-22 23:53:03 -04001441 }
1442}
1443
1444void check_dev_msr()
1445{
1446 struct stat sb;
1447
1448 if (stat("/dev/cpu/0/msr", &sb)) {
1449 fprintf(stderr, "no /dev/cpu/0/msr\n");
1450 fprintf(stderr, "Try \"# modprobe msr\"\n");
1451 exit(-5);
1452 }
1453}
1454
1455void check_super_user()
1456{
1457 if (getuid() != 0) {
1458 fprintf(stderr, "must be root\n");
1459 exit(-6);
1460 }
1461}
1462
1463int has_nehalem_turbo_ratio_limit(unsigned int family, unsigned int model)
1464{
1465 if (!genuine_intel)
1466 return 0;
1467
1468 if (family != 6)
1469 return 0;
1470
1471 switch (model) {
1472 case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
1473 case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
1474 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
1475 case 0x25: /* Westmere Client - Clarkdale, Arrandale */
1476 case 0x2C: /* Westmere EP - Gulftown */
1477 case 0x2A: /* SNB */
1478 case 0x2D: /* SNB Xeon */
Len Brown553575f2011-11-18 03:32:01 -05001479 case 0x3A: /* IVB */
Len Brown13006512012-09-26 18:11:31 -04001480 case 0x3E: /* IVB Xeon */
Len Brown70b43402013-01-08 01:26:07 -05001481 case 0x3C: /* HSW */
1482 case 0x3F: /* HSW */
1483 case 0x45: /* HSW */
Len Brown149c2312013-03-15 10:58:02 -04001484 case 0x46: /* HSW */
Len Brown144b44b2013-11-09 00:30:16 -05001485 case 0x37: /* BYT */
1486 case 0x4D: /* AVN */
Len Brown103a8fe2010-10-22 23:53:03 -04001487 return 1;
1488 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1489 case 0x2F: /* Westmere-EX Xeon - Eagleton */
1490 default:
1491 return 0;
1492 }
1493}
Len Brown6574a5d2012-09-21 00:01:31 -04001494int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
1495{
1496 if (!genuine_intel)
1497 return 0;
1498
1499 if (family != 6)
1500 return 0;
1501
1502 switch (model) {
1503 case 0x3E: /* IVB Xeon */
1504 return 1;
1505 default:
1506 return 0;
1507 }
1508}
1509
Len Brown889facb2012-11-08 00:48:57 -05001510/*
1511 * print_epb()
1512 * Decode the ENERGY_PERF_BIAS MSR
1513 */
1514int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1515{
1516 unsigned long long msr;
1517 char *epb_string;
1518 int cpu;
1519
1520 if (!has_epb)
1521 return 0;
1522
1523 cpu = t->cpu_id;
1524
1525 /* EPB is per-package */
1526 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1527 return 0;
1528
1529 if (cpu_migrate(cpu)) {
1530 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1531 return -1;
1532 }
1533
1534 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
1535 return 0;
1536
1537 switch (msr & 0x7) {
1538 case ENERGY_PERF_BIAS_PERFORMANCE:
1539 epb_string = "performance";
1540 break;
1541 case ENERGY_PERF_BIAS_NORMAL:
1542 epb_string = "balanced";
1543 break;
1544 case ENERGY_PERF_BIAS_POWERSAVE:
1545 epb_string = "powersave";
1546 break;
1547 default:
1548 epb_string = "custom";
1549 break;
1550 }
1551 fprintf(stderr, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
1552
1553 return 0;
1554}
1555
1556#define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
1557#define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
1558
Len Brown144b44b2013-11-09 00:30:16 -05001559double get_tdp(model)
1560{
1561 unsigned long long msr;
1562
1563 if (do_rapl & RAPL_PKG_POWER_INFO)
1564 if (!get_msr(0, MSR_PKG_POWER_INFO, &msr))
1565 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
1566
1567 switch (model) {
1568 case 0x37:
1569 case 0x4D:
1570 return 30.0;
1571 default:
1572 return 135.0;
1573 }
1574}
1575
1576
Len Brown889facb2012-11-08 00:48:57 -05001577/*
1578 * rapl_probe()
1579 *
Len Brown144b44b2013-11-09 00:30:16 -05001580 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
Len Brown889facb2012-11-08 00:48:57 -05001581 */
1582void rapl_probe(unsigned int family, unsigned int model)
1583{
1584 unsigned long long msr;
Len Brown144b44b2013-11-09 00:30:16 -05001585 unsigned int time_unit;
Len Brown889facb2012-11-08 00:48:57 -05001586 double tdp;
1587
1588 if (!genuine_intel)
1589 return;
1590
1591 if (family != 6)
1592 return;
1593
1594 switch (model) {
1595 case 0x2A:
1596 case 0x3A:
Len Brown70b43402013-01-08 01:26:07 -05001597 case 0x3C: /* HSW */
1598 case 0x3F: /* HSW */
1599 case 0x45: /* HSW */
Len Brown149c2312013-03-15 10:58:02 -04001600 case 0x46: /* HSW */
Len Brown144b44b2013-11-09 00:30:16 -05001601 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
Len Brown889facb2012-11-08 00:48:57 -05001602 break;
1603 case 0x2D:
1604 case 0x3E:
Len Brown144b44b2013-11-09 00:30:16 -05001605 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_PKG_PERF_STATUS | RAPL_DRAM_PERF_STATUS | RAPL_PKG_POWER_INFO;
1606 break;
1607 case 0x37: /* BYT */
1608 case 0x4D: /* AVN */
1609 do_rapl = RAPL_PKG | RAPL_CORES ;
Len Brown889facb2012-11-08 00:48:57 -05001610 break;
1611 default:
1612 return;
1613 }
1614
1615 /* units on package 0, verify later other packages match */
1616 if (get_msr(0, MSR_RAPL_POWER_UNIT, &msr))
1617 return;
1618
1619 rapl_power_units = 1.0 / (1 << (msr & 0xF));
Len Brown144b44b2013-11-09 00:30:16 -05001620 if (model == 0x37)
1621 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
1622 else
1623 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
Len Brown889facb2012-11-08 00:48:57 -05001624
Len Brown144b44b2013-11-09 00:30:16 -05001625 time_unit = msr >> 16 & 0xF;
1626 if (time_unit == 0)
1627 time_unit = 0xA;
Len Brown889facb2012-11-08 00:48:57 -05001628
Len Brown144b44b2013-11-09 00:30:16 -05001629 rapl_time_units = 1.0 / (1 << (time_unit));
1630
1631 tdp = get_tdp(model);
Len Brown889facb2012-11-08 00:48:57 -05001632
1633 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
Len Brown889facb2012-11-08 00:48:57 -05001634 if (verbose)
Len Brown144b44b2013-11-09 00:30:16 -05001635 fprintf(stderr, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
Len Brown889facb2012-11-08 00:48:57 -05001636
1637 return;
1638}
1639
1640int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1641{
1642 unsigned long long msr;
1643 unsigned int dts;
1644 int cpu;
1645
1646 if (!(do_dts || do_ptm))
1647 return 0;
1648
1649 cpu = t->cpu_id;
1650
1651 /* DTS is per-core, no need to print for each thread */
1652 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1653 return 0;
1654
1655 if (cpu_migrate(cpu)) {
1656 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1657 return -1;
1658 }
1659
1660 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
1661 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1662 return 0;
1663
1664 dts = (msr >> 16) & 0x7F;
1665 fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
1666 cpu, msr, tcc_activation_temp - dts);
1667
1668#ifdef THERM_DEBUG
1669 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
1670 return 0;
1671
1672 dts = (msr >> 16) & 0x7F;
1673 dts2 = (msr >> 8) & 0x7F;
1674 fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
1675 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
1676#endif
1677 }
1678
1679
1680 if (do_dts) {
1681 unsigned int resolution;
1682
1683 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1684 return 0;
1685
1686 dts = (msr >> 16) & 0x7F;
1687 resolution = (msr >> 27) & 0xF;
1688 fprintf(stderr, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
1689 cpu, msr, tcc_activation_temp - dts, resolution);
1690
1691#ifdef THERM_DEBUG
1692 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
1693 return 0;
1694
1695 dts = (msr >> 16) & 0x7F;
1696 dts2 = (msr >> 8) & 0x7F;
1697 fprintf(stderr, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
1698 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
1699#endif
1700 }
1701
1702 return 0;
1703}
1704
1705void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
1706{
1707 fprintf(stderr, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
1708 cpu, label,
1709 ((msr >> 15) & 1) ? "EN" : "DIS",
1710 ((msr >> 0) & 0x7FFF) * rapl_power_units,
1711 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
1712 (((msr >> 16) & 1) ? "EN" : "DIS"));
1713
1714 return;
1715}
1716
1717int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1718{
1719 unsigned long long msr;
1720 int cpu;
Len Brown889facb2012-11-08 00:48:57 -05001721
1722 if (!do_rapl)
1723 return 0;
1724
1725 /* RAPL counters are per package, so print only for 1st thread/package */
1726 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1727 return 0;
1728
1729 cpu = t->cpu_id;
1730 if (cpu_migrate(cpu)) {
1731 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1732 return -1;
1733 }
1734
1735 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
1736 return -1;
1737
Len Brown889facb2012-11-08 00:48:57 -05001738 if (verbose) {
1739 fprintf(stderr, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx "
1740 "(%f Watts, %f Joules, %f sec.)\n", cpu, msr,
Len Brown144b44b2013-11-09 00:30:16 -05001741 rapl_power_units, rapl_energy_units, rapl_time_units);
Len Brown889facb2012-11-08 00:48:57 -05001742 }
Len Brown144b44b2013-11-09 00:30:16 -05001743 if (do_rapl & RAPL_PKG_POWER_INFO) {
1744
Len Brown889facb2012-11-08 00:48:57 -05001745 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
1746 return -5;
1747
1748
1749 fprintf(stderr, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
1750 cpu, msr,
1751 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1752 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1753 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1754 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
1755
Len Brown144b44b2013-11-09 00:30:16 -05001756 }
1757 if (do_rapl & RAPL_PKG) {
1758
Len Brown889facb2012-11-08 00:48:57 -05001759 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
1760 return -9;
1761
1762 fprintf(stderr, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
1763 cpu, msr, (msr >> 63) & 1 ? "": "UN");
1764
1765 print_power_limit_msr(cpu, msr, "PKG Limit #1");
1766 fprintf(stderr, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
1767 cpu,
1768 ((msr >> 47) & 1) ? "EN" : "DIS",
1769 ((msr >> 32) & 0x7FFF) * rapl_power_units,
1770 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
1771 ((msr >> 48) & 1) ? "EN" : "DIS");
1772 }
1773
1774 if (do_rapl & RAPL_DRAM) {
1775 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
1776 return -6;
1777
1778
1779 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
1780 cpu, msr,
1781 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1782 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1783 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1784 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
1785
1786
1787 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
1788 return -9;
1789 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
1790 cpu, msr, (msr >> 31) & 1 ? "": "UN");
1791
1792 print_power_limit_msr(cpu, msr, "DRAM Limit");
1793 }
Len Brown144b44b2013-11-09 00:30:16 -05001794 if (do_rapl & RAPL_CORE_POLICY) {
Len Brown889facb2012-11-08 00:48:57 -05001795 if (verbose) {
1796 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
1797 return -7;
1798
1799 fprintf(stderr, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown144b44b2013-11-09 00:30:16 -05001800 }
1801 }
1802 if (do_rapl & RAPL_CORES) {
1803 if (verbose) {
Len Brown889facb2012-11-08 00:48:57 -05001804
1805 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
1806 return -9;
1807 fprintf(stderr, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
1808 cpu, msr, (msr >> 31) & 1 ? "": "UN");
1809 print_power_limit_msr(cpu, msr, "Cores Limit");
1810 }
1811 }
1812 if (do_rapl & RAPL_GFX) {
1813 if (verbose) {
1814 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
1815 return -8;
1816
1817 fprintf(stderr, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
1818
1819 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
1820 return -9;
1821 fprintf(stderr, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
1822 cpu, msr, (msr >> 31) & 1 ? "": "UN");
1823 print_power_limit_msr(cpu, msr, "GFX Limit");
1824 }
1825 }
1826 return 0;
1827}
1828
Len Brown103a8fe2010-10-22 23:53:03 -04001829
1830int is_snb(unsigned int family, unsigned int model)
1831{
1832 if (!genuine_intel)
1833 return 0;
1834
1835 switch (model) {
1836 case 0x2A:
1837 case 0x2D:
Len Brown650a37f2012-06-03 23:34:44 -04001838 case 0x3A: /* IVB */
Len Brown13006512012-09-26 18:11:31 -04001839 case 0x3E: /* IVB Xeon */
Len Brown70b43402013-01-08 01:26:07 -05001840 case 0x3C: /* HSW */
1841 case 0x3F: /* HSW */
1842 case 0x45: /* HSW */
Len Brown149c2312013-03-15 10:58:02 -04001843 case 0x46: /* HSW */
Len Brown103a8fe2010-10-22 23:53:03 -04001844 return 1;
1845 }
1846 return 0;
1847}
1848
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001849int has_c8_c9_c10(unsigned int family, unsigned int model)
1850{
1851 if (!genuine_intel)
1852 return 0;
1853
1854 switch (model) {
1855 case 0x45:
1856 return 1;
1857 }
1858 return 0;
1859}
1860
1861
Len Brown144b44b2013-11-09 00:30:16 -05001862int is_slm(unsigned int family, unsigned int model)
1863{
1864 if (!genuine_intel)
1865 return 0;
1866 switch (model) {
1867 case 0x37: /* BYT */
1868 case 0x4D: /* AVN */
1869 return 1;
1870 }
1871 return 0;
1872}
1873
1874#define SLM_BCLK_FREQS 5
1875double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
1876
1877double slm_bclk(void)
1878{
1879 unsigned long long msr = 3;
1880 unsigned int i;
1881 double freq;
1882
1883 if (get_msr(0, MSR_FSB_FREQ, &msr))
1884 fprintf(stderr, "SLM BCLK: unknown\n");
1885
1886 i = msr & 0xf;
1887 if (i >= SLM_BCLK_FREQS) {
1888 fprintf(stderr, "SLM BCLK[%d] invalid\n", i);
1889 msr = 3;
1890 }
1891 freq = slm_freq_table[i];
1892
1893 fprintf(stderr, "SLM BCLK: %.1f Mhz\n", freq);
1894
1895 return freq;
1896}
1897
Len Brown103a8fe2010-10-22 23:53:03 -04001898double discover_bclk(unsigned int family, unsigned int model)
1899{
1900 if (is_snb(family, model))
1901 return 100.00;
Len Brown144b44b2013-11-09 00:30:16 -05001902 else if (is_slm(family, model))
1903 return slm_bclk();
Len Brown103a8fe2010-10-22 23:53:03 -04001904 else
1905 return 133.33;
1906}
1907
Len Brown889facb2012-11-08 00:48:57 -05001908/*
1909 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
1910 * the Thermal Control Circuit (TCC) activates.
1911 * This is usually equal to tjMax.
1912 *
1913 * Older processors do not have this MSR, so there we guess,
1914 * but also allow cmdline over-ride with -T.
1915 *
1916 * Several MSR temperature values are in units of degrees-C
1917 * below this value, including the Digital Thermal Sensor (DTS),
1918 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
1919 */
1920int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1921{
1922 unsigned long long msr;
1923 unsigned int target_c_local;
1924 int cpu;
1925
1926 /* tcc_activation_temp is used only for dts or ptm */
1927 if (!(do_dts || do_ptm))
1928 return 0;
1929
1930 /* this is a per-package concept */
1931 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1932 return 0;
1933
1934 cpu = t->cpu_id;
1935 if (cpu_migrate(cpu)) {
1936 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1937 return -1;
1938 }
1939
1940 if (tcc_activation_temp_override != 0) {
1941 tcc_activation_temp = tcc_activation_temp_override;
1942 fprintf(stderr, "cpu%d: Using cmdline TCC Target (%d C)\n",
1943 cpu, tcc_activation_temp);
1944 return 0;
1945 }
1946
1947 /* Temperature Target MSR is Nehalem and newer only */
1948 if (!do_nehalem_platform_info)
1949 goto guess;
1950
1951 if (get_msr(0, MSR_IA32_TEMPERATURE_TARGET, &msr))
1952 goto guess;
1953
1954 target_c_local = (msr >> 16) & 0x7F;
1955
1956 if (verbose)
1957 fprintf(stderr, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
1958 cpu, msr, target_c_local);
1959
Len Brown144b44b2013-11-09 00:30:16 -05001960 if (target_c_local < 85 || target_c_local > 127)
Len Brown889facb2012-11-08 00:48:57 -05001961 goto guess;
1962
1963 tcc_activation_temp = target_c_local;
1964
1965 return 0;
1966
1967guess:
1968 tcc_activation_temp = TJMAX_DEFAULT;
1969 fprintf(stderr, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
1970 cpu, tcc_activation_temp);
1971
1972 return 0;
1973}
Len Brown103a8fe2010-10-22 23:53:03 -04001974void check_cpuid()
1975{
1976 unsigned int eax, ebx, ecx, edx, max_level;
1977 unsigned int fms, family, model, stepping;
1978
1979 eax = ebx = ecx = edx = 0;
1980
1981 asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0));
1982
1983 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
1984 genuine_intel = 1;
1985
1986 if (verbose)
Len Brown889facb2012-11-08 00:48:57 -05001987 fprintf(stderr, "CPUID(0): %.4s%.4s%.4s ",
Len Brown103a8fe2010-10-22 23:53:03 -04001988 (char *)&ebx, (char *)&edx, (char *)&ecx);
1989
1990 asm("cpuid" : "=a" (fms), "=c" (ecx), "=d" (edx) : "a" (1) : "ebx");
1991 family = (fms >> 8) & 0xf;
1992 model = (fms >> 4) & 0xf;
1993 stepping = fms & 0xf;
1994 if (family == 6 || family == 0xf)
1995 model += ((fms >> 16) & 0xf) << 4;
1996
1997 if (verbose)
1998 fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
1999 max_level, family, model, stepping, family, model, stepping);
2000
2001 if (!(edx & (1 << 5))) {
2002 fprintf(stderr, "CPUID: no MSR\n");
2003 exit(1);
2004 }
2005
2006 /*
2007 * check max extended function levels of CPUID.
2008 * This is needed to check for invariant TSC.
2009 * This check is valid for both Intel and AMD.
2010 */
2011 ebx = ecx = edx = 0;
2012 asm("cpuid" : "=a" (max_level), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000000));
2013
2014 if (max_level < 0x80000007) {
2015 fprintf(stderr, "CPUID: no invariant TSC (max_level 0x%x)\n", max_level);
2016 exit(1);
2017 }
2018
2019 /*
2020 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
2021 * this check is valid for both Intel and AMD
2022 */
2023 asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000007));
Thomas Renninger8209e052011-01-21 15:11:19 +01002024 has_invariant_tsc = edx & (1 << 8);
Len Brown103a8fe2010-10-22 23:53:03 -04002025
2026 if (!has_invariant_tsc) {
2027 fprintf(stderr, "No invariant TSC\n");
2028 exit(1);
2029 }
2030
2031 /*
2032 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
2033 * this check is valid for both Intel and AMD
2034 */
2035
2036 asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x6));
Thomas Renninger8209e052011-01-21 15:11:19 +01002037 has_aperf = ecx & (1 << 0);
Len Brown889facb2012-11-08 00:48:57 -05002038 do_dts = eax & (1 << 0);
2039 do_ptm = eax & (1 << 6);
2040 has_epb = ecx & (1 << 3);
2041
2042 if (verbose)
2043 fprintf(stderr, "CPUID(6): %s%s%s%s\n",
2044 has_aperf ? "APERF" : "No APERF!",
2045 do_dts ? ", DTS" : "",
2046 do_ptm ? ", PTM": "",
2047 has_epb ? ", EPB": "");
2048
2049 if (!has_aperf)
2050 exit(-1);
Len Brown103a8fe2010-10-22 23:53:03 -04002051
2052 do_nehalem_platform_info = genuine_intel && has_invariant_tsc;
2053 do_nhm_cstates = genuine_intel; /* all Intel w/ non-stop TSC have NHM counters */
Len Brown1ed51012013-02-10 17:19:24 -05002054 do_smi = do_nhm_cstates;
Len Brown103a8fe2010-10-22 23:53:03 -04002055 do_snb_cstates = is_snb(family, model);
Kristen Carlson Accardica587102012-11-21 05:22:43 -08002056 do_c8_c9_c10 = has_c8_c9_c10(family, model);
Len Brown144b44b2013-11-09 00:30:16 -05002057 do_slm_cstates = is_slm(family, model);
Len Brown103a8fe2010-10-22 23:53:03 -04002058 bclk = discover_bclk(family, model);
2059
2060 do_nehalem_turbo_ratio_limit = has_nehalem_turbo_ratio_limit(family, model);
Len Brown6574a5d2012-09-21 00:01:31 -04002061 do_ivt_turbo_ratio_limit = has_ivt_turbo_ratio_limit(family, model);
Len Brown889facb2012-11-08 00:48:57 -05002062 rapl_probe(family, model);
2063
2064 return;
Len Brown103a8fe2010-10-22 23:53:03 -04002065}
2066
2067
2068void usage()
2069{
Len Brown889facb2012-11-08 00:48:57 -05002070 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 -04002071 progname);
2072 exit(1);
2073}
2074
2075
2076/*
2077 * in /dev/cpu/ return success for names that are numbers
2078 * ie. filter out ".", "..", "microcode".
2079 */
2080int dir_filter(const struct dirent *dirp)
2081{
2082 if (isdigit(dirp->d_name[0]))
2083 return 1;
2084 else
2085 return 0;
2086}
2087
2088int open_dev_cpu_msr(int dummy1)
2089{
2090 return 0;
2091}
2092
Len Brownc98d5d92012-06-04 00:56:40 -04002093void topology_probe()
2094{
2095 int i;
2096 int max_core_id = 0;
2097 int max_package_id = 0;
2098 int max_siblings = 0;
2099 struct cpu_topology {
2100 int core_id;
2101 int physical_package_id;
2102 } *cpus;
2103
2104 /* Initialize num_cpus, max_cpu_num */
2105 topo.num_cpus = 0;
2106 topo.max_cpu_num = 0;
2107 for_all_proc_cpus(count_cpus);
2108 if (!summary_only && topo.num_cpus > 1)
2109 show_cpu = 1;
2110
2111 if (verbose > 1)
2112 fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
2113
2114 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
2115 if (cpus == NULL) {
2116 perror("calloc cpus");
2117 exit(1);
2118 }
2119
2120 /*
2121 * Allocate and initialize cpu_present_set
2122 */
2123 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
2124 if (cpu_present_set == NULL) {
2125 perror("CPU_ALLOC");
2126 exit(3);
2127 }
2128 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2129 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
2130 for_all_proc_cpus(mark_cpu_present);
2131
2132 /*
2133 * Allocate and initialize cpu_affinity_set
2134 */
2135 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
2136 if (cpu_affinity_set == NULL) {
2137 perror("CPU_ALLOC");
2138 exit(3);
2139 }
2140 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2141 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
2142
2143
2144 /*
2145 * For online cpus
2146 * find max_core_id, max_package_id
2147 */
2148 for (i = 0; i <= topo.max_cpu_num; ++i) {
2149 int siblings;
2150
2151 if (cpu_is_not_present(i)) {
2152 if (verbose > 1)
2153 fprintf(stderr, "cpu%d NOT PRESENT\n", i);
2154 continue;
2155 }
2156 cpus[i].core_id = get_core_id(i);
2157 if (cpus[i].core_id > max_core_id)
2158 max_core_id = cpus[i].core_id;
2159
2160 cpus[i].physical_package_id = get_physical_package_id(i);
2161 if (cpus[i].physical_package_id > max_package_id)
2162 max_package_id = cpus[i].physical_package_id;
2163
2164 siblings = get_num_ht_siblings(i);
2165 if (siblings > max_siblings)
2166 max_siblings = siblings;
2167 if (verbose > 1)
2168 fprintf(stderr, "cpu %d pkg %d core %d\n",
2169 i, cpus[i].physical_package_id, cpus[i].core_id);
2170 }
2171 topo.num_cores_per_pkg = max_core_id + 1;
2172 if (verbose > 1)
2173 fprintf(stderr, "max_core_id %d, sizing for %d cores per package\n",
2174 max_core_id, topo.num_cores_per_pkg);
2175 if (!summary_only && topo.num_cores_per_pkg > 1)
2176 show_core = 1;
2177
2178 topo.num_packages = max_package_id + 1;
2179 if (verbose > 1)
2180 fprintf(stderr, "max_package_id %d, sizing for %d packages\n",
2181 max_package_id, topo.num_packages);
2182 if (!summary_only && topo.num_packages > 1)
2183 show_pkg = 1;
2184
2185 topo.num_threads_per_core = max_siblings;
2186 if (verbose > 1)
2187 fprintf(stderr, "max_siblings %d\n", max_siblings);
2188
2189 free(cpus);
2190}
2191
2192void
2193allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
2194{
2195 int i;
2196
2197 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
2198 topo.num_packages, sizeof(struct thread_data));
2199 if (*t == NULL)
2200 goto error;
2201
2202 for (i = 0; i < topo.num_threads_per_core *
2203 topo.num_cores_per_pkg * topo.num_packages; i++)
2204 (*t)[i].cpu_id = -1;
2205
2206 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
2207 sizeof(struct core_data));
2208 if (*c == NULL)
2209 goto error;
2210
2211 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
2212 (*c)[i].core_id = -1;
2213
2214 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
2215 if (*p == NULL)
2216 goto error;
2217
2218 for (i = 0; i < topo.num_packages; i++)
2219 (*p)[i].package_id = i;
2220
2221 return;
2222error:
2223 perror("calloc counters");
2224 exit(1);
2225}
2226/*
2227 * init_counter()
2228 *
2229 * set cpu_id, core_num, pkg_num
2230 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
2231 *
2232 * increment topo.num_cores when 1st core in pkg seen
2233 */
2234void init_counter(struct thread_data *thread_base, struct core_data *core_base,
2235 struct pkg_data *pkg_base, int thread_num, int core_num,
2236 int pkg_num, int cpu_id)
2237{
2238 struct thread_data *t;
2239 struct core_data *c;
2240 struct pkg_data *p;
2241
2242 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
2243 c = GET_CORE(core_base, core_num, pkg_num);
2244 p = GET_PKG(pkg_base, pkg_num);
2245
2246 t->cpu_id = cpu_id;
2247 if (thread_num == 0) {
2248 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
2249 if (cpu_is_first_core_in_package(cpu_id))
2250 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
2251 }
2252
2253 c->core_id = core_num;
2254 p->package_id = pkg_num;
2255}
2256
2257
2258int initialize_counters(int cpu_id)
2259{
2260 int my_thread_id, my_core_id, my_package_id;
2261
2262 my_package_id = get_physical_package_id(cpu_id);
2263 my_core_id = get_core_id(cpu_id);
2264
2265 if (cpu_is_first_sibling_in_core(cpu_id)) {
2266 my_thread_id = 0;
2267 topo.num_cores++;
2268 } else {
2269 my_thread_id = 1;
2270 }
2271
2272 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2273 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2274 return 0;
2275}
2276
2277void allocate_output_buffer()
2278{
Josh Triplettb844db32013-06-12 17:26:37 -07002279 output_buffer = calloc(1, (1 + topo.num_cpus) * 256);
Len Brownc98d5d92012-06-04 00:56:40 -04002280 outp = output_buffer;
2281 if (outp == NULL) {
2282 perror("calloc");
2283 exit(-1);
2284 }
2285}
2286
2287void setup_all_buffers(void)
2288{
2289 topology_probe();
2290 allocate_counters(&thread_even, &core_even, &package_even);
2291 allocate_counters(&thread_odd, &core_odd, &package_odd);
2292 allocate_output_buffer();
2293 for_all_proc_cpus(initialize_counters);
2294}
Len Brown103a8fe2010-10-22 23:53:03 -04002295void turbostat_init()
2296{
2297 check_cpuid();
2298
2299 check_dev_msr();
2300 check_super_user();
2301
Len Brownc98d5d92012-06-04 00:56:40 -04002302 setup_all_buffers();
Len Brown103a8fe2010-10-22 23:53:03 -04002303
2304 if (verbose)
Len Brownc98d5d92012-06-04 00:56:40 -04002305 print_verbose_header();
Len Brown889facb2012-11-08 00:48:57 -05002306
2307 if (verbose)
2308 for_all_cpus(print_epb, ODD_COUNTERS);
2309
2310 if (verbose)
2311 for_all_cpus(print_rapl, ODD_COUNTERS);
2312
2313 for_all_cpus(set_temperature_target, ODD_COUNTERS);
2314
2315 if (verbose)
2316 for_all_cpus(print_thermal, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04002317}
2318
2319int fork_it(char **argv)
2320{
Len Brown103a8fe2010-10-22 23:53:03 -04002321 pid_t child_pid;
Len Brownd91bb172012-11-01 00:08:19 -04002322 int status;
Len Brownd15cf7c2012-06-03 23:24:00 -04002323
Len Brownd91bb172012-11-01 00:08:19 -04002324 status = for_all_cpus(get_counters, EVEN_COUNTERS);
2325 if (status)
2326 exit(status);
Len Brownc98d5d92012-06-04 00:56:40 -04002327 /* clear affinity side-effect of get_counters() */
2328 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
Len Brown103a8fe2010-10-22 23:53:03 -04002329 gettimeofday(&tv_even, (struct timezone *)NULL);
2330
2331 child_pid = fork();
2332 if (!child_pid) {
2333 /* child */
2334 execvp(argv[0], argv);
2335 } else {
Len Brown103a8fe2010-10-22 23:53:03 -04002336
2337 /* parent */
2338 if (child_pid == -1) {
2339 perror("fork");
2340 exit(1);
2341 }
2342
2343 signal(SIGINT, SIG_IGN);
2344 signal(SIGQUIT, SIG_IGN);
2345 if (waitpid(child_pid, &status, 0) == -1) {
2346 perror("wait");
Len Brownd91bb172012-11-01 00:08:19 -04002347 exit(status);
Len Brown103a8fe2010-10-22 23:53:03 -04002348 }
2349 }
Len Brownc98d5d92012-06-04 00:56:40 -04002350 /*
2351 * n.b. fork_it() does not check for errors from for_all_cpus()
2352 * because re-starting is problematic when forking
2353 */
2354 for_all_cpus(get_counters, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04002355 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002356 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04002357 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
2358 compute_average(EVEN_COUNTERS);
2359 format_all_counters(EVEN_COUNTERS);
2360 flush_stderr();
Len Brown103a8fe2010-10-22 23:53:03 -04002361
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07002362 fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
Len Brown103a8fe2010-10-22 23:53:03 -04002363
Len Brownd91bb172012-11-01 00:08:19 -04002364 return status;
Len Brown103a8fe2010-10-22 23:53:03 -04002365}
2366
2367void cmdline(int argc, char **argv)
2368{
2369 int opt;
2370
2371 progname = argv[0];
2372
Len Brown889facb2012-11-08 00:48:57 -05002373 while ((opt = getopt(argc, argv, "+pPSvi:sc:sC:m:M:RT:")) != -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04002374 switch (opt) {
Len Brownf9240812012-10-06 15:26:31 -04002375 case 'p':
Len Brownc98d5d92012-06-04 00:56:40 -04002376 show_core_only++;
2377 break;
Len Brownf9240812012-10-06 15:26:31 -04002378 case 'P':
Len Brownc98d5d92012-06-04 00:56:40 -04002379 show_pkg_only++;
2380 break;
Len Brownf9240812012-10-06 15:26:31 -04002381 case 'S':
Len Browne23da032012-02-06 18:37:16 -05002382 summary_only++;
2383 break;
Len Brown103a8fe2010-10-22 23:53:03 -04002384 case 'v':
2385 verbose++;
2386 break;
2387 case 'i':
2388 interval_sec = atoi(optarg);
2389 break;
Len Brownf9240812012-10-06 15:26:31 -04002390 case 'c':
Len Brown8e180f32012-09-22 01:25:08 -04002391 sscanf(optarg, "%x", &extra_delta_offset32);
2392 break;
Len Brownf9240812012-10-06 15:26:31 -04002393 case 'C':
Len Brown8e180f32012-09-22 01:25:08 -04002394 sscanf(optarg, "%x", &extra_delta_offset64);
2395 break;
Len Brown2f32edf2012-09-21 23:45:46 -04002396 case 'm':
2397 sscanf(optarg, "%x", &extra_msr_offset32);
Len Brown2f32edf2012-09-21 23:45:46 -04002398 break;
2399 case 'M':
2400 sscanf(optarg, "%x", &extra_msr_offset64);
Len Brown103a8fe2010-10-22 23:53:03 -04002401 break;
Len Brown889facb2012-11-08 00:48:57 -05002402 case 'R':
2403 rapl_verbose++;
2404 break;
2405 case 'T':
2406 tcc_activation_temp_override = atoi(optarg);
2407 break;
Len Brown103a8fe2010-10-22 23:53:03 -04002408 default:
2409 usage();
2410 }
2411 }
2412}
2413
2414int main(int argc, char **argv)
2415{
2416 cmdline(argc, argv);
2417
Len Brown889facb2012-11-08 00:48:57 -05002418 if (verbose)
Len Brown144b44b2013-11-09 00:30:16 -05002419 fprintf(stderr, "turbostat v3.5 April 26, 2013"
Len Brown103a8fe2010-10-22 23:53:03 -04002420 " - Len Brown <lenb@kernel.org>\n");
Len Brown103a8fe2010-10-22 23:53:03 -04002421
2422 turbostat_init();
2423
2424 /*
2425 * if any params left, it must be a command to fork
2426 */
2427 if (argc - optind)
2428 return fork_it(argv + optind);
2429 else
2430 turbostat_loop();
2431
2432 return 0;
2433}