blob: 02afe35d8770709ee2116b7188d8b72dfd6c513d [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>
Josh Triplettb2c95d92013-08-20 17:20:18 -070026#include <err.h>
Len Brown103a8fe2010-10-22 23:53:03 -040027#include <unistd.h>
28#include <sys/types.h>
29#include <sys/wait.h>
30#include <sys/stat.h>
31#include <sys/resource.h>
32#include <fcntl.h>
33#include <signal.h>
34#include <sys/time.h>
35#include <stdlib.h>
36#include <dirent.h>
37#include <string.h>
38#include <ctype.h>
Len Brown88c32812012-03-29 21:44:40 -040039#include <sched.h>
Josh Triplett2b928652013-08-20 17:20:14 -070040#include <cpuid.h>
Len Brown103a8fe2010-10-22 23:53:03 -040041
Len Brown103a8fe2010-10-22 23:53:03 -040042char *proc_stat = "/proc/stat";
43unsigned int interval_sec = 5; /* set with -i interval_sec */
44unsigned int verbose; /* set with -v */
Len Brown889facb2012-11-08 00:48:57 -050045unsigned int rapl_verbose; /* set with -R */
46unsigned int thermal_verbose; /* set with -T */
Len Browne23da032012-02-06 18:37:16 -050047unsigned int summary_only; /* set with -s */
Len Brown103a8fe2010-10-22 23:53:03 -040048unsigned int skip_c0;
49unsigned int skip_c1;
50unsigned int do_nhm_cstates;
51unsigned int do_snb_cstates;
Kristen Carlson Accardica587102012-11-21 05:22:43 -080052unsigned int do_c8_c9_c10;
Len Brown144b44b2013-11-09 00:30:16 -050053unsigned int do_slm_cstates;
54unsigned int use_c1_residency_msr;
Len Brown103a8fe2010-10-22 23:53:03 -040055unsigned int has_aperf;
Len Brown889facb2012-11-08 00:48:57 -050056unsigned int has_epb;
Len Brown103a8fe2010-10-22 23:53:03 -040057unsigned int units = 1000000000; /* Ghz etc */
58unsigned int genuine_intel;
59unsigned int has_invariant_tsc;
60unsigned int do_nehalem_platform_info;
61unsigned int do_nehalem_turbo_ratio_limit;
Len Brown6574a5d2012-09-21 00:01:31 -040062unsigned int do_ivt_turbo_ratio_limit;
Len Brown2f32edf2012-09-21 23:45:46 -040063unsigned int extra_msr_offset32;
64unsigned int extra_msr_offset64;
Len Brown8e180f32012-09-22 01:25:08 -040065unsigned int extra_delta_offset32;
66unsigned int extra_delta_offset64;
Len Brown1ed51012013-02-10 17:19:24 -050067int do_smi;
Len Brown103a8fe2010-10-22 23:53:03 -040068double bclk;
69unsigned int show_pkg;
70unsigned int show_core;
71unsigned int show_cpu;
Len Brownc98d5d92012-06-04 00:56:40 -040072unsigned int show_pkg_only;
73unsigned int show_core_only;
74char *output_buffer, *outp;
Len Brown889facb2012-11-08 00:48:57 -050075unsigned int do_rapl;
76unsigned int do_dts;
77unsigned int do_ptm;
78unsigned int tcc_activation_temp;
79unsigned int tcc_activation_temp_override;
80double rapl_power_units, rapl_energy_units, rapl_time_units;
81double rapl_joule_counter_range;
82
Len Browne6f9bb32013-12-03 02:19:19 -050083#define RAPL_PKG (1 << 0)
84 /* 0x610 MSR_PKG_POWER_LIMIT */
85 /* 0x611 MSR_PKG_ENERGY_STATUS */
86#define RAPL_PKG_PERF_STATUS (1 << 1)
87 /* 0x613 MSR_PKG_PERF_STATUS */
88#define RAPL_PKG_POWER_INFO (1 << 2)
89 /* 0x614 MSR_PKG_POWER_INFO */
90
91#define RAPL_DRAM (1 << 3)
92 /* 0x618 MSR_DRAM_POWER_LIMIT */
93 /* 0x619 MSR_DRAM_ENERGY_STATUS */
94 /* 0x61c MSR_DRAM_POWER_INFO */
95#define RAPL_DRAM_PERF_STATUS (1 << 4)
96 /* 0x61b MSR_DRAM_PERF_STATUS */
97
98#define RAPL_CORES (1 << 5)
99 /* 0x638 MSR_PP0_POWER_LIMIT */
100 /* 0x639 MSR_PP0_ENERGY_STATUS */
101#define RAPL_CORE_POLICY (1 << 6)
102 /* 0x63a MSR_PP0_POLICY */
103
104
105#define RAPL_GFX (1 << 7)
106 /* 0x640 MSR_PP1_POWER_LIMIT */
107 /* 0x641 MSR_PP1_ENERGY_STATUS */
108 /* 0x642 MSR_PP1_POLICY */
Len Brown889facb2012-11-08 00:48:57 -0500109#define TJMAX_DEFAULT 100
110
111#define MAX(a, b) ((a) > (b) ? (a) : (b))
Len Brown103a8fe2010-10-22 23:53:03 -0400112
113int aperf_mperf_unstable;
114int backwards_count;
115char *progname;
Len Brown103a8fe2010-10-22 23:53:03 -0400116
Len Brownc98d5d92012-06-04 00:56:40 -0400117cpu_set_t *cpu_present_set, *cpu_affinity_set;
118size_t cpu_present_setsize, cpu_affinity_setsize;
Len Brown103a8fe2010-10-22 23:53:03 -0400119
Len Brownc98d5d92012-06-04 00:56:40 -0400120struct thread_data {
121 unsigned long long tsc;
122 unsigned long long aperf;
123 unsigned long long mperf;
Len Brown144b44b2013-11-09 00:30:16 -0500124 unsigned long long c1;
Len Brown2f32edf2012-09-21 23:45:46 -0400125 unsigned long long extra_msr64;
Len Brown8e180f32012-09-22 01:25:08 -0400126 unsigned long long extra_delta64;
127 unsigned long long extra_msr32;
128 unsigned long long extra_delta32;
Len Brown1ed51012013-02-10 17:19:24 -0500129 unsigned int smi_count;
Len Brownc98d5d92012-06-04 00:56:40 -0400130 unsigned int cpu_id;
131 unsigned int flags;
132#define CPU_IS_FIRST_THREAD_IN_CORE 0x2
133#define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
134} *thread_even, *thread_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400135
Len Brownc98d5d92012-06-04 00:56:40 -0400136struct core_data {
137 unsigned long long c3;
138 unsigned long long c6;
139 unsigned long long c7;
Len Brown889facb2012-11-08 00:48:57 -0500140 unsigned int core_temp_c;
Len Brownc98d5d92012-06-04 00:56:40 -0400141 unsigned int core_id;
142} *core_even, *core_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400143
Len Brownc98d5d92012-06-04 00:56:40 -0400144struct pkg_data {
145 unsigned long long pc2;
146 unsigned long long pc3;
147 unsigned long long pc6;
148 unsigned long long pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800149 unsigned long long pc8;
150 unsigned long long pc9;
151 unsigned long long pc10;
Len Brownc98d5d92012-06-04 00:56:40 -0400152 unsigned int package_id;
Len Brown889facb2012-11-08 00:48:57 -0500153 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
154 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
155 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
156 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
157 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
158 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
159 unsigned int pkg_temp_c;
160
Len Brownc98d5d92012-06-04 00:56:40 -0400161} *package_even, *package_odd;
162
163#define ODD_COUNTERS thread_odd, core_odd, package_odd
164#define EVEN_COUNTERS thread_even, core_even, package_even
165
166#define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
167 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
168 topo.num_threads_per_core + \
169 (core_no) * topo.num_threads_per_core + (thread_no))
170#define GET_CORE(core_base, core_no, pkg_no) \
171 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
172#define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
173
174struct system_summary {
175 struct thread_data threads;
176 struct core_data cores;
177 struct pkg_data packages;
178} sum, average;
179
180
181struct topo_params {
182 int num_packages;
183 int num_cpus;
184 int num_cores;
185 int max_cpu_num;
186 int num_cores_per_pkg;
187 int num_threads_per_core;
188} topo;
189
190struct timeval tv_even, tv_odd, tv_delta;
191
192void setup_all_buffers(void);
193
194int cpu_is_not_present(int cpu)
Len Brownd15cf7c2012-06-03 23:24:00 -0400195{
Len Brownc98d5d92012-06-04 00:56:40 -0400196 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brownd15cf7c2012-06-03 23:24:00 -0400197}
Len Brown88c32812012-03-29 21:44:40 -0400198/*
Len Brownc98d5d92012-06-04 00:56:40 -0400199 * run func(thread, core, package) in topology order
200 * skip non-present cpus
Len Brown88c32812012-03-29 21:44:40 -0400201 */
Len Brownd15cf7c2012-06-03 23:24:00 -0400202
Len Brownc98d5d92012-06-04 00:56:40 -0400203int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
204 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
Len Brown88c32812012-03-29 21:44:40 -0400205{
Len Brownc98d5d92012-06-04 00:56:40 -0400206 int retval, pkg_no, core_no, thread_no;
207
208 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
209 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
210 for (thread_no = 0; thread_no <
211 topo.num_threads_per_core; ++thread_no) {
212 struct thread_data *t;
213 struct core_data *c;
214 struct pkg_data *p;
215
216 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
217
218 if (cpu_is_not_present(t->cpu_id))
219 continue;
220
221 c = GET_CORE(core_base, core_no, pkg_no);
222 p = GET_PKG(pkg_base, pkg_no);
223
224 retval = func(t, c, p);
225 if (retval)
226 return retval;
227 }
228 }
229 }
230 return 0;
Len Brown88c32812012-03-29 21:44:40 -0400231}
232
233int cpu_migrate(int cpu)
234{
Len Brownc98d5d92012-06-04 00:56:40 -0400235 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
236 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
237 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
Len Brown88c32812012-03-29 21:44:40 -0400238 return -1;
239 else
240 return 0;
241}
242
Len Brown15aaa342012-03-29 22:19:58 -0400243int get_msr(int cpu, off_t offset, unsigned long long *msr)
Len Brown103a8fe2010-10-22 23:53:03 -0400244{
245 ssize_t retval;
Len Brown103a8fe2010-10-22 23:53:03 -0400246 char pathname[32];
247 int fd;
248
249 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
250 fd = open(pathname, O_RDONLY);
Len Brown15aaa342012-03-29 22:19:58 -0400251 if (fd < 0)
252 return -1;
Len Brown103a8fe2010-10-22 23:53:03 -0400253
Len Brown15aaa342012-03-29 22:19:58 -0400254 retval = pread(fd, msr, sizeof *msr, offset);
Len Brown103a8fe2010-10-22 23:53:03 -0400255 close(fd);
Len Brown15aaa342012-03-29 22:19:58 -0400256
Len Brownd91bb172012-11-01 00:08:19 -0400257 if (retval != sizeof *msr) {
Josh Triplett2e9c6bc2013-08-20 17:20:13 -0700258 fprintf(stderr, "%s offset 0x%llx read failed\n", pathname, (unsigned long long)offset);
Len Brown15aaa342012-03-29 22:19:58 -0400259 return -1;
Len Brownd91bb172012-11-01 00:08:19 -0400260 }
Len Brown15aaa342012-03-29 22:19:58 -0400261
262 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400263}
264
Len Browna829eb42011-02-10 23:36:34 -0500265void print_header(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400266{
267 if (show_pkg)
Len Brownc98d5d92012-06-04 00:56:40 -0400268 outp += sprintf(outp, "pk");
Len Browne23da032012-02-06 18:37:16 -0500269 if (show_pkg)
Len Brownc98d5d92012-06-04 00:56:40 -0400270 outp += sprintf(outp, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400271 if (show_core)
Len Brownc98d5d92012-06-04 00:56:40 -0400272 outp += sprintf(outp, "cor");
Len Brown103a8fe2010-10-22 23:53:03 -0400273 if (show_cpu)
Len Brownc98d5d92012-06-04 00:56:40 -0400274 outp += sprintf(outp, " CPU");
Len Browne23da032012-02-06 18:37:16 -0500275 if (show_pkg || show_core || show_cpu)
Len Brownc98d5d92012-06-04 00:56:40 -0400276 outp += sprintf(outp, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400277 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400278 outp += sprintf(outp, " %%c0");
Len Brown103a8fe2010-10-22 23:53:03 -0400279 if (has_aperf)
Len Brownc98d5d92012-06-04 00:56:40 -0400280 outp += sprintf(outp, " GHz");
281 outp += sprintf(outp, " TSC");
Len Brown1ed51012013-02-10 17:19:24 -0500282 if (do_smi)
283 outp += sprintf(outp, " SMI");
Len Brown8e180f32012-09-22 01:25:08 -0400284 if (extra_delta_offset32)
Len Brownf9240812012-10-06 15:26:31 -0400285 outp += sprintf(outp, " count 0x%03X", extra_delta_offset32);
Len Brown8e180f32012-09-22 01:25:08 -0400286 if (extra_delta_offset64)
Len Brownf9240812012-10-06 15:26:31 -0400287 outp += sprintf(outp, " COUNT 0x%03X", extra_delta_offset64);
Len Brown2f32edf2012-09-21 23:45:46 -0400288 if (extra_msr_offset32)
Len Brown8e180f32012-09-22 01:25:08 -0400289 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset32);
Len Brown2f32edf2012-09-21 23:45:46 -0400290 if (extra_msr_offset64)
Len Brown8e180f32012-09-22 01:25:08 -0400291 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset64);
Len Brown103a8fe2010-10-22 23:53:03 -0400292 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400293 outp += sprintf(outp, " %%c1");
Len Brown144b44b2013-11-09 00:30:16 -0500294 if (do_nhm_cstates && !do_slm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400295 outp += sprintf(outp, " %%c3");
Len Brown103a8fe2010-10-22 23:53:03 -0400296 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400297 outp += sprintf(outp, " %%c6");
Len Brown103a8fe2010-10-22 23:53:03 -0400298 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400299 outp += sprintf(outp, " %%c7");
Len Brown889facb2012-11-08 00:48:57 -0500300
301 if (do_dts)
302 outp += sprintf(outp, " CTMP");
303 if (do_ptm)
304 outp += sprintf(outp, " PTMP");
305
Len Brown103a8fe2010-10-22 23:53:03 -0400306 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400307 outp += sprintf(outp, " %%pc2");
Len Brown144b44b2013-11-09 00:30:16 -0500308 if (do_nhm_cstates && !do_slm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400309 outp += sprintf(outp, " %%pc3");
Len Brown144b44b2013-11-09 00:30:16 -0500310 if (do_nhm_cstates && !do_slm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400311 outp += sprintf(outp, " %%pc6");
Len Brown103a8fe2010-10-22 23:53:03 -0400312 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400313 outp += sprintf(outp, " %%pc7");
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800314 if (do_c8_c9_c10) {
315 outp += sprintf(outp, " %%pc8");
316 outp += sprintf(outp, " %%pc9");
317 outp += sprintf(outp, " %%pc10");
318 }
Len Brown103a8fe2010-10-22 23:53:03 -0400319
Len Brown889facb2012-11-08 00:48:57 -0500320 if (do_rapl & RAPL_PKG)
321 outp += sprintf(outp, " Pkg_W");
322 if (do_rapl & RAPL_CORES)
323 outp += sprintf(outp, " Cor_W");
324 if (do_rapl & RAPL_GFX)
325 outp += sprintf(outp, " GFX_W");
326 if (do_rapl & RAPL_DRAM)
327 outp += sprintf(outp, " RAM_W");
328 if (do_rapl & RAPL_PKG_PERF_STATUS)
329 outp += sprintf(outp, " PKG_%%");
330 if (do_rapl & RAPL_DRAM_PERF_STATUS)
331 outp += sprintf(outp, " RAM_%%");
332
Len Brownc98d5d92012-06-04 00:56:40 -0400333 outp += sprintf(outp, "\n");
Len Brown103a8fe2010-10-22 23:53:03 -0400334}
335
Len Brownc98d5d92012-06-04 00:56:40 -0400336int dump_counters(struct thread_data *t, struct core_data *c,
337 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400338{
Len Brownc98d5d92012-06-04 00:56:40 -0400339 fprintf(stderr, "t %p, c %p, p %p\n", t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400340
Len Brownc98d5d92012-06-04 00:56:40 -0400341 if (t) {
342 fprintf(stderr, "CPU: %d flags 0x%x\n", t->cpu_id, t->flags);
343 fprintf(stderr, "TSC: %016llX\n", t->tsc);
344 fprintf(stderr, "aperf: %016llX\n", t->aperf);
345 fprintf(stderr, "mperf: %016llX\n", t->mperf);
346 fprintf(stderr, "c1: %016llX\n", t->c1);
Len Brown8e180f32012-09-22 01:25:08 -0400347 fprintf(stderr, "msr0x%x: %08llX\n",
348 extra_delta_offset32, t->extra_delta32);
349 fprintf(stderr, "msr0x%x: %016llX\n",
350 extra_delta_offset64, t->extra_delta64);
351 fprintf(stderr, "msr0x%x: %08llX\n",
Len Brown2f32edf2012-09-21 23:45:46 -0400352 extra_msr_offset32, t->extra_msr32);
Len Brownc98d5d92012-06-04 00:56:40 -0400353 fprintf(stderr, "msr0x%x: %016llX\n",
Len Brown2f32edf2012-09-21 23:45:46 -0400354 extra_msr_offset64, t->extra_msr64);
Len Brown1ed51012013-02-10 17:19:24 -0500355 if (do_smi)
356 fprintf(stderr, "SMI: %08X\n", t->smi_count);
Len Brownc98d5d92012-06-04 00:56:40 -0400357 }
Len Brown103a8fe2010-10-22 23:53:03 -0400358
Len Brownc98d5d92012-06-04 00:56:40 -0400359 if (c) {
360 fprintf(stderr, "core: %d\n", c->core_id);
361 fprintf(stderr, "c3: %016llX\n", c->c3);
362 fprintf(stderr, "c6: %016llX\n", c->c6);
363 fprintf(stderr, "c7: %016llX\n", c->c7);
Len Brown889facb2012-11-08 00:48:57 -0500364 fprintf(stderr, "DTS: %dC\n", c->core_temp_c);
Len Brownc98d5d92012-06-04 00:56:40 -0400365 }
366
367 if (p) {
368 fprintf(stderr, "package: %d\n", p->package_id);
369 fprintf(stderr, "pc2: %016llX\n", p->pc2);
370 fprintf(stderr, "pc3: %016llX\n", p->pc3);
371 fprintf(stderr, "pc6: %016llX\n", p->pc6);
372 fprintf(stderr, "pc7: %016llX\n", p->pc7);
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800373 fprintf(stderr, "pc8: %016llX\n", p->pc8);
374 fprintf(stderr, "pc9: %016llX\n", p->pc9);
375 fprintf(stderr, "pc10: %016llX\n", p->pc10);
Len Brown889facb2012-11-08 00:48:57 -0500376 fprintf(stderr, "Joules PKG: %0X\n", p->energy_pkg);
377 fprintf(stderr, "Joules COR: %0X\n", p->energy_cores);
378 fprintf(stderr, "Joules GFX: %0X\n", p->energy_gfx);
379 fprintf(stderr, "Joules RAM: %0X\n", p->energy_dram);
380 fprintf(stderr, "Throttle PKG: %0X\n", p->rapl_pkg_perf_status);
381 fprintf(stderr, "Throttle RAM: %0X\n", p->rapl_dram_perf_status);
382 fprintf(stderr, "PTM: %dC\n", p->pkg_temp_c);
Len Brownc98d5d92012-06-04 00:56:40 -0400383 }
384 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400385}
386
Len Browne23da032012-02-06 18:37:16 -0500387/*
388 * column formatting convention & formats
389 * package: "pk" 2 columns %2d
390 * core: "cor" 3 columns %3d
391 * CPU: "CPU" 3 columns %3d
Len Brown889facb2012-11-08 00:48:57 -0500392 * Pkg_W: %6.2
393 * Cor_W: %6.2
394 * GFX_W: %5.2
395 * RAM_W: %5.2
Len Browne23da032012-02-06 18:37:16 -0500396 * GHz: "GHz" 3 columns %3.2
397 * TSC: "TSC" 3 columns %3.2
Len Brown1ed51012013-02-10 17:19:24 -0500398 * SMI: "SMI" 4 columns %4d
Len Browne23da032012-02-06 18:37:16 -0500399 * percentage " %pc3" %6.2
Len Brown889facb2012-11-08 00:48:57 -0500400 * Perf Status percentage: %5.2
401 * "CTMP" 4 columns %4d
Len Browne23da032012-02-06 18:37:16 -0500402 */
Len Brownc98d5d92012-06-04 00:56:40 -0400403int format_counters(struct thread_data *t, struct core_data *c,
404 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400405{
406 double interval_float;
Len Brown889facb2012-11-08 00:48:57 -0500407 char *fmt5, *fmt6;
Len Brown103a8fe2010-10-22 23:53:03 -0400408
Len Brownc98d5d92012-06-04 00:56:40 -0400409 /* if showing only 1st thread in core and this isn't one, bail out */
410 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
411 return 0;
412
413 /* if showing only 1st thread in pkg and this isn't one, bail out */
414 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
415 return 0;
416
Len Brown103a8fe2010-10-22 23:53:03 -0400417 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
418
Len Brownc98d5d92012-06-04 00:56:40 -0400419 /* topo columns, print blanks on 1st (average) line */
420 if (t == &average.threads) {
Len Brown103a8fe2010-10-22 23:53:03 -0400421 if (show_pkg)
Len Brownc98d5d92012-06-04 00:56:40 -0400422 outp += sprintf(outp, " ");
Len Browne23da032012-02-06 18:37:16 -0500423 if (show_pkg && show_core)
Len Brownc98d5d92012-06-04 00:56:40 -0400424 outp += sprintf(outp, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400425 if (show_core)
Len Brownc98d5d92012-06-04 00:56:40 -0400426 outp += sprintf(outp, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400427 if (show_cpu)
Len Brownc98d5d92012-06-04 00:56:40 -0400428 outp += sprintf(outp, " " " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400429 } else {
Len Brownc98d5d92012-06-04 00:56:40 -0400430 if (show_pkg) {
431 if (p)
432 outp += sprintf(outp, "%2d", p->package_id);
433 else
434 outp += sprintf(outp, " ");
435 }
Len Browne23da032012-02-06 18:37:16 -0500436 if (show_pkg && show_core)
Len Brownc98d5d92012-06-04 00:56:40 -0400437 outp += sprintf(outp, " ");
438 if (show_core) {
439 if (c)
440 outp += sprintf(outp, "%3d", c->core_id);
441 else
442 outp += sprintf(outp, " ");
443 }
Len Brown103a8fe2010-10-22 23:53:03 -0400444 if (show_cpu)
Len Brownc98d5d92012-06-04 00:56:40 -0400445 outp += sprintf(outp, " %3d", t->cpu_id);
Len Brown103a8fe2010-10-22 23:53:03 -0400446 }
Len Brown103a8fe2010-10-22 23:53:03 -0400447 /* %c0 */
448 if (do_nhm_cstates) {
Len Browne23da032012-02-06 18:37:16 -0500449 if (show_pkg || show_core || show_cpu)
Len Brownc98d5d92012-06-04 00:56:40 -0400450 outp += sprintf(outp, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400451 if (!skip_c0)
Len Brownc98d5d92012-06-04 00:56:40 -0400452 outp += sprintf(outp, "%6.2f", 100.0 * t->mperf/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400453 else
Len Brownc98d5d92012-06-04 00:56:40 -0400454 outp += sprintf(outp, " ****");
Len Brown103a8fe2010-10-22 23:53:03 -0400455 }
456
457 /* GHz */
458 if (has_aperf) {
459 if (!aperf_mperf_unstable) {
Len Brownc98d5d92012-06-04 00:56:40 -0400460 outp += sprintf(outp, " %3.2f",
461 1.0 * t->tsc / units * t->aperf /
462 t->mperf / interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400463 } else {
Len Brownc98d5d92012-06-04 00:56:40 -0400464 if (t->aperf > t->tsc || t->mperf > t->tsc) {
465 outp += sprintf(outp, " ***");
Len Brown103a8fe2010-10-22 23:53:03 -0400466 } else {
Len Brownc98d5d92012-06-04 00:56:40 -0400467 outp += sprintf(outp, "%3.1f*",
468 1.0 * t->tsc /
469 units * t->aperf /
470 t->mperf / interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400471 }
472 }
473 }
474
475 /* TSC */
Len Brownc98d5d92012-06-04 00:56:40 -0400476 outp += sprintf(outp, "%5.2f", 1.0 * t->tsc/units/interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400477
Len Brown1ed51012013-02-10 17:19:24 -0500478 /* SMI */
479 if (do_smi)
480 outp += sprintf(outp, "%4d", t->smi_count);
481
Len Brown8e180f32012-09-22 01:25:08 -0400482 /* delta */
483 if (extra_delta_offset32)
484 outp += sprintf(outp, " %11llu", t->extra_delta32);
485
486 /* DELTA */
487 if (extra_delta_offset64)
488 outp += sprintf(outp, " %11llu", t->extra_delta64);
Len Brown2f32edf2012-09-21 23:45:46 -0400489 /* msr */
490 if (extra_msr_offset32)
Len Brown8e180f32012-09-22 01:25:08 -0400491 outp += sprintf(outp, " 0x%08llx", t->extra_msr32);
Len Brown2f32edf2012-09-21 23:45:46 -0400492
Len Brown130ff302012-09-21 22:56:06 -0400493 /* MSR */
Len Brown2f32edf2012-09-21 23:45:46 -0400494 if (extra_msr_offset64)
495 outp += sprintf(outp, " 0x%016llx", t->extra_msr64);
Len Brown130ff302012-09-21 22:56:06 -0400496
Len Brown103a8fe2010-10-22 23:53:03 -0400497 if (do_nhm_cstates) {
498 if (!skip_c1)
Len Brownc98d5d92012-06-04 00:56:40 -0400499 outp += sprintf(outp, " %6.2f", 100.0 * t->c1/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400500 else
Len Brownc98d5d92012-06-04 00:56:40 -0400501 outp += sprintf(outp, " ****");
Len Brown103a8fe2010-10-22 23:53:03 -0400502 }
Len Brownc98d5d92012-06-04 00:56:40 -0400503
504 /* print per-core data only for 1st thread in core */
505 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
506 goto done;
507
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 * c->c3/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400510 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400511 outp += sprintf(outp, " %6.2f", 100.0 * c->c6/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 * c->c7/t->tsc);
514
Len Brown889facb2012-11-08 00:48:57 -0500515 if (do_dts)
516 outp += sprintf(outp, " %4d", c->core_temp_c);
517
Len Brownc98d5d92012-06-04 00:56:40 -0400518 /* print per-package data only for 1st core in package */
519 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
520 goto done;
521
Len Brown889facb2012-11-08 00:48:57 -0500522 if (do_ptm)
523 outp += sprintf(outp, " %4d", p->pkg_temp_c);
524
Len Brown103a8fe2010-10-22 23:53:03 -0400525 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400526 outp += sprintf(outp, " %6.2f", 100.0 * p->pc2/t->tsc);
Len Brown144b44b2013-11-09 00:30:16 -0500527 if (do_nhm_cstates && !do_slm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400528 outp += sprintf(outp, " %6.2f", 100.0 * p->pc3/t->tsc);
Len Brown144b44b2013-11-09 00:30:16 -0500529 if (do_nhm_cstates && !do_slm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400530 outp += sprintf(outp, " %6.2f", 100.0 * p->pc6/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400531 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400532 outp += sprintf(outp, " %6.2f", 100.0 * p->pc7/t->tsc);
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800533 if (do_c8_c9_c10) {
534 outp += sprintf(outp, " %6.2f", 100.0 * p->pc8/t->tsc);
535 outp += sprintf(outp, " %6.2f", 100.0 * p->pc9/t->tsc);
536 outp += sprintf(outp, " %6.2f", 100.0 * p->pc10/t->tsc);
537 }
Len Brown889facb2012-11-08 00:48:57 -0500538
539 /*
540 * If measurement interval exceeds minimum RAPL Joule Counter range,
541 * indicate that results are suspect by printing "**" in fraction place.
542 */
543 if (interval_float < rapl_joule_counter_range) {
544 fmt5 = " %5.2f";
545 fmt6 = " %6.2f";
546 } else {
547 fmt5 = " %3.0f**";
548 fmt6 = " %4.0f**";
549 }
550
551 if (do_rapl & RAPL_PKG)
552 outp += sprintf(outp, fmt6, p->energy_pkg * rapl_energy_units / interval_float);
553 if (do_rapl & RAPL_CORES)
554 outp += sprintf(outp, fmt6, p->energy_cores * rapl_energy_units / interval_float);
555 if (do_rapl & RAPL_GFX)
556 outp += sprintf(outp, fmt5, p->energy_gfx * rapl_energy_units / interval_float);
557 if (do_rapl & RAPL_DRAM)
558 outp += sprintf(outp, fmt5, p->energy_dram * rapl_energy_units / interval_float);
559 if (do_rapl & RAPL_PKG_PERF_STATUS )
560 outp += sprintf(outp, fmt5, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
561 if (do_rapl & RAPL_DRAM_PERF_STATUS )
562 outp += sprintf(outp, fmt5, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
563
Len Brownc98d5d92012-06-04 00:56:40 -0400564done:
Len Brownc98d5d92012-06-04 00:56:40 -0400565 outp += sprintf(outp, "\n");
566
567 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400568}
569
Len Brownc98d5d92012-06-04 00:56:40 -0400570void flush_stdout()
Len Brown103a8fe2010-10-22 23:53:03 -0400571{
Len Brownc98d5d92012-06-04 00:56:40 -0400572 fputs(output_buffer, stdout);
Len Brownddac0d62012-11-30 01:01:40 -0500573 fflush(stdout);
Len Brownc98d5d92012-06-04 00:56:40 -0400574 outp = output_buffer;
575}
576void flush_stderr()
577{
578 fputs(output_buffer, stderr);
579 outp = output_buffer;
580}
581void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
582{
Len Browne23da032012-02-06 18:37:16 -0500583 static int printed;
Len Brown103a8fe2010-10-22 23:53:03 -0400584
Len Browne23da032012-02-06 18:37:16 -0500585 if (!printed || !summary_only)
586 print_header();
Len Brown103a8fe2010-10-22 23:53:03 -0400587
Len Brownc98d5d92012-06-04 00:56:40 -0400588 if (topo.num_cpus > 1)
589 format_counters(&average.threads, &average.cores,
590 &average.packages);
Len Brown103a8fe2010-10-22 23:53:03 -0400591
Len Browne23da032012-02-06 18:37:16 -0500592 printed = 1;
593
594 if (summary_only)
595 return;
596
Len Brownc98d5d92012-06-04 00:56:40 -0400597 for_all_cpus(format_counters, t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400598}
599
Len Brown889facb2012-11-08 00:48:57 -0500600#define DELTA_WRAP32(new, old) \
601 if (new > old) { \
602 old = new - old; \
603 } else { \
604 old = 0x100000000 + new - old; \
605 }
606
Len Brownc98d5d92012-06-04 00:56:40 -0400607void
608delta_package(struct pkg_data *new, struct pkg_data *old)
Len Brown103a8fe2010-10-22 23:53:03 -0400609{
Len Brownc98d5d92012-06-04 00:56:40 -0400610 old->pc2 = new->pc2 - old->pc2;
611 old->pc3 = new->pc3 - old->pc3;
612 old->pc6 = new->pc6 - old->pc6;
613 old->pc7 = new->pc7 - old->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800614 old->pc8 = new->pc8 - old->pc8;
615 old->pc9 = new->pc9 - old->pc9;
616 old->pc10 = new->pc10 - old->pc10;
Len Brown889facb2012-11-08 00:48:57 -0500617 old->pkg_temp_c = new->pkg_temp_c;
618
619 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
620 DELTA_WRAP32(new->energy_cores, old->energy_cores);
621 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
622 DELTA_WRAP32(new->energy_dram, old->energy_dram);
623 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
624 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
Len Brownc98d5d92012-06-04 00:56:40 -0400625}
Len Brown103a8fe2010-10-22 23:53:03 -0400626
Len Brownc98d5d92012-06-04 00:56:40 -0400627void
628delta_core(struct core_data *new, struct core_data *old)
629{
630 old->c3 = new->c3 - old->c3;
631 old->c6 = new->c6 - old->c6;
632 old->c7 = new->c7 - old->c7;
Len Brown889facb2012-11-08 00:48:57 -0500633 old->core_temp_c = new->core_temp_c;
Len Brownc98d5d92012-06-04 00:56:40 -0400634}
Len Brown103a8fe2010-10-22 23:53:03 -0400635
Len Brownc3ae3312012-06-13 21:31:46 -0400636/*
637 * old = new - old
638 */
Len Brownc98d5d92012-06-04 00:56:40 -0400639void
640delta_thread(struct thread_data *new, struct thread_data *old,
641 struct core_data *core_delta)
642{
643 old->tsc = new->tsc - old->tsc;
Len Brown103a8fe2010-10-22 23:53:03 -0400644
Len Brownc98d5d92012-06-04 00:56:40 -0400645 /* check for TSC < 1 Mcycles over interval */
Josh Triplettb2c95d92013-08-20 17:20:18 -0700646 if (old->tsc < (1000 * 1000))
647 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
648 "You can disable all c-states by booting with \"idle=poll\"\n"
649 "or just the deep ones with \"processor.max_cstate=1\"");
Len Brown103a8fe2010-10-22 23:53:03 -0400650
Len Brownc98d5d92012-06-04 00:56:40 -0400651 old->c1 = new->c1 - old->c1;
Len Brown103a8fe2010-10-22 23:53:03 -0400652
Len Brownc98d5d92012-06-04 00:56:40 -0400653 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
654 old->aperf = new->aperf - old->aperf;
655 old->mperf = new->mperf - old->mperf;
656 } else {
Len Brown103a8fe2010-10-22 23:53:03 -0400657
Len Brownc98d5d92012-06-04 00:56:40 -0400658 if (!aperf_mperf_unstable) {
659 fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname);
660 fprintf(stderr, "* Frequency results do not cover entire interval *\n");
661 fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n");
662
663 aperf_mperf_unstable = 1;
664 }
Len Brown103a8fe2010-10-22 23:53:03 -0400665 /*
Len Brownc98d5d92012-06-04 00:56:40 -0400666 * mperf delta is likely a huge "positive" number
667 * can not use it for calculating c0 time
Len Brown103a8fe2010-10-22 23:53:03 -0400668 */
Len Brownc98d5d92012-06-04 00:56:40 -0400669 skip_c0 = 1;
670 skip_c1 = 1;
671 }
Len Brown103a8fe2010-10-22 23:53:03 -0400672
Len Brown103a8fe2010-10-22 23:53:03 -0400673
Len Brown144b44b2013-11-09 00:30:16 -0500674 if (use_c1_residency_msr) {
675 /*
676 * Some models have a dedicated C1 residency MSR,
677 * which should be more accurate than the derivation below.
678 */
679 } else {
680 /*
681 * As counter collection is not atomic,
682 * it is possible for mperf's non-halted cycles + idle states
683 * to exceed TSC's all cycles: show c1 = 0% in that case.
684 */
685 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
686 old->c1 = 0;
687 else {
688 /* normal case, derive c1 */
689 old->c1 = old->tsc - old->mperf - core_delta->c3
Len Brownc98d5d92012-06-04 00:56:40 -0400690 - core_delta->c6 - core_delta->c7;
Len Brown144b44b2013-11-09 00:30:16 -0500691 }
Len Brownc98d5d92012-06-04 00:56:40 -0400692 }
Len Brownc3ae3312012-06-13 21:31:46 -0400693
Len Brownc98d5d92012-06-04 00:56:40 -0400694 if (old->mperf == 0) {
Len Brownc3ae3312012-06-13 21:31:46 -0400695 if (verbose > 1) fprintf(stderr, "cpu%d MPERF 0!\n", old->cpu_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400696 old->mperf = 1; /* divide by 0 protection */
697 }
698
Len Brown8e180f32012-09-22 01:25:08 -0400699 old->extra_delta32 = new->extra_delta32 - old->extra_delta32;
700 old->extra_delta32 &= 0xFFFFFFFF;
701
702 old->extra_delta64 = new->extra_delta64 - old->extra_delta64;
703
Len Brownc98d5d92012-06-04 00:56:40 -0400704 /*
Len Brown8e180f32012-09-22 01:25:08 -0400705 * Extra MSR is just a snapshot, simply copy latest w/o subtracting
Len Brownc98d5d92012-06-04 00:56:40 -0400706 */
Len Brown2f32edf2012-09-21 23:45:46 -0400707 old->extra_msr32 = new->extra_msr32;
708 old->extra_msr64 = new->extra_msr64;
Len Brown1ed51012013-02-10 17:19:24 -0500709
710 if (do_smi)
711 old->smi_count = new->smi_count - old->smi_count;
Len Brownc98d5d92012-06-04 00:56:40 -0400712}
713
714int delta_cpu(struct thread_data *t, struct core_data *c,
715 struct pkg_data *p, struct thread_data *t2,
716 struct core_data *c2, struct pkg_data *p2)
717{
718 /* calculate core delta only for 1st thread in core */
719 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
720 delta_core(c, c2);
721
722 /* always calculate thread delta */
723 delta_thread(t, t2, c2); /* c2 is core delta */
724
725 /* calculate package delta only for 1st core in package */
726 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
727 delta_package(p, p2);
728
729 return 0;
730}
731
732void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
733{
734 t->tsc = 0;
735 t->aperf = 0;
736 t->mperf = 0;
737 t->c1 = 0;
738
Len Brown1ed51012013-02-10 17:19:24 -0500739 t->smi_count = 0;
Len Brown8e180f32012-09-22 01:25:08 -0400740 t->extra_delta32 = 0;
741 t->extra_delta64 = 0;
742
Len Brownc98d5d92012-06-04 00:56:40 -0400743 /* tells format_counters to dump all fields from this set */
744 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
745
746 c->c3 = 0;
747 c->c6 = 0;
748 c->c7 = 0;
Len Brown889facb2012-11-08 00:48:57 -0500749 c->core_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -0400750
751 p->pc2 = 0;
752 p->pc3 = 0;
753 p->pc6 = 0;
754 p->pc7 = 0;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800755 p->pc8 = 0;
756 p->pc9 = 0;
757 p->pc10 = 0;
Len Brown889facb2012-11-08 00:48:57 -0500758
759 p->energy_pkg = 0;
760 p->energy_dram = 0;
761 p->energy_cores = 0;
762 p->energy_gfx = 0;
763 p->rapl_pkg_perf_status = 0;
764 p->rapl_dram_perf_status = 0;
765 p->pkg_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -0400766}
767int sum_counters(struct thread_data *t, struct core_data *c,
768 struct pkg_data *p)
769{
770 average.threads.tsc += t->tsc;
771 average.threads.aperf += t->aperf;
772 average.threads.mperf += t->mperf;
773 average.threads.c1 += t->c1;
774
Len Brown8e180f32012-09-22 01:25:08 -0400775 average.threads.extra_delta32 += t->extra_delta32;
776 average.threads.extra_delta64 += t->extra_delta64;
777
Len Brownc98d5d92012-06-04 00:56:40 -0400778 /* sum per-core values only for 1st thread in core */
779 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
780 return 0;
781
782 average.cores.c3 += c->c3;
783 average.cores.c6 += c->c6;
784 average.cores.c7 += c->c7;
785
Len Brown889facb2012-11-08 00:48:57 -0500786 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
787
Len Brownc98d5d92012-06-04 00:56:40 -0400788 /* sum per-pkg values only for 1st core in pkg */
789 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
790 return 0;
791
792 average.packages.pc2 += p->pc2;
793 average.packages.pc3 += p->pc3;
794 average.packages.pc6 += p->pc6;
795 average.packages.pc7 += p->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800796 average.packages.pc8 += p->pc8;
797 average.packages.pc9 += p->pc9;
798 average.packages.pc10 += p->pc10;
Len Brownc98d5d92012-06-04 00:56:40 -0400799
Len Brown889facb2012-11-08 00:48:57 -0500800 average.packages.energy_pkg += p->energy_pkg;
801 average.packages.energy_dram += p->energy_dram;
802 average.packages.energy_cores += p->energy_cores;
803 average.packages.energy_gfx += p->energy_gfx;
804
805 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
806
807 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
808 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
Len Brownc98d5d92012-06-04 00:56:40 -0400809 return 0;
810}
811/*
812 * sum the counters for all cpus in the system
813 * compute the weighted average
814 */
815void compute_average(struct thread_data *t, struct core_data *c,
816 struct pkg_data *p)
817{
818 clear_counters(&average.threads, &average.cores, &average.packages);
819
820 for_all_cpus(sum_counters, t, c, p);
821
822 average.threads.tsc /= topo.num_cpus;
823 average.threads.aperf /= topo.num_cpus;
824 average.threads.mperf /= topo.num_cpus;
825 average.threads.c1 /= topo.num_cpus;
826
Len Brown8e180f32012-09-22 01:25:08 -0400827 average.threads.extra_delta32 /= topo.num_cpus;
828 average.threads.extra_delta32 &= 0xFFFFFFFF;
829
830 average.threads.extra_delta64 /= topo.num_cpus;
831
Len Brownc98d5d92012-06-04 00:56:40 -0400832 average.cores.c3 /= topo.num_cores;
833 average.cores.c6 /= topo.num_cores;
834 average.cores.c7 /= topo.num_cores;
835
836 average.packages.pc2 /= topo.num_packages;
837 average.packages.pc3 /= topo.num_packages;
838 average.packages.pc6 /= topo.num_packages;
839 average.packages.pc7 /= topo.num_packages;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800840
841 average.packages.pc8 /= topo.num_packages;
842 average.packages.pc9 /= topo.num_packages;
843 average.packages.pc10 /= topo.num_packages;
Len Brownc98d5d92012-06-04 00:56:40 -0400844}
845
846static unsigned long long rdtsc(void)
847{
848 unsigned int low, high;
849
850 asm volatile("rdtsc" : "=a" (low), "=d" (high));
851
852 return low | ((unsigned long long)high) << 32;
853}
854
855
856/*
857 * get_counters(...)
858 * migrate to cpu
859 * acquire and record local counters for that cpu
860 */
861int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
862{
863 int cpu = t->cpu_id;
Len Brown889facb2012-11-08 00:48:57 -0500864 unsigned long long msr;
Len Brownc98d5d92012-06-04 00:56:40 -0400865
Len Browne52966c2012-11-08 22:38:05 -0500866 if (cpu_migrate(cpu)) {
867 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
Len Brownc98d5d92012-06-04 00:56:40 -0400868 return -1;
Len Browne52966c2012-11-08 22:38:05 -0500869 }
Len Brownc98d5d92012-06-04 00:56:40 -0400870
871 t->tsc = rdtsc(); /* we are running on local CPU of interest */
872
873 if (has_aperf) {
Len Brown9c63a652012-10-31 01:29:52 -0400874 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
Len Brownc98d5d92012-06-04 00:56:40 -0400875 return -3;
Len Brown9c63a652012-10-31 01:29:52 -0400876 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
Len Brownc98d5d92012-06-04 00:56:40 -0400877 return -4;
878 }
879
Len Brown1ed51012013-02-10 17:19:24 -0500880 if (do_smi) {
881 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
882 return -5;
883 t->smi_count = msr & 0xFFFFFFFF;
884 }
Len Brown8e180f32012-09-22 01:25:08 -0400885 if (extra_delta_offset32) {
Len Brown889facb2012-11-08 00:48:57 -0500886 if (get_msr(cpu, extra_delta_offset32, &msr))
Len Brown2f32edf2012-09-21 23:45:46 -0400887 return -5;
Len Brown889facb2012-11-08 00:48:57 -0500888 t->extra_delta32 = msr & 0xFFFFFFFF;
Len Brown8e180f32012-09-22 01:25:08 -0400889 }
890
891 if (extra_delta_offset64)
892 if (get_msr(cpu, extra_delta_offset64, &t->extra_delta64))
893 return -5;
894
895 if (extra_msr_offset32) {
Len Brown889facb2012-11-08 00:48:57 -0500896 if (get_msr(cpu, extra_msr_offset32, &msr))
Len Brown8e180f32012-09-22 01:25:08 -0400897 return -5;
Len Brown889facb2012-11-08 00:48:57 -0500898 t->extra_msr32 = msr & 0xFFFFFFFF;
Len Brown8e180f32012-09-22 01:25:08 -0400899 }
Len Brown2f32edf2012-09-21 23:45:46 -0400900
901 if (extra_msr_offset64)
902 if (get_msr(cpu, extra_msr_offset64, &t->extra_msr64))
Len Brownc98d5d92012-06-04 00:56:40 -0400903 return -5;
904
Len Brown144b44b2013-11-09 00:30:16 -0500905 if (use_c1_residency_msr) {
906 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
907 return -6;
908 }
909
Len Brownc98d5d92012-06-04 00:56:40 -0400910 /* collect core counters only for 1st thread in core */
911 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
912 return 0;
913
Len Brown144b44b2013-11-09 00:30:16 -0500914 if (do_nhm_cstates && !do_slm_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -0400915 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
916 return -6;
Len Brown144b44b2013-11-09 00:30:16 -0500917 }
918
919 if (do_nhm_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -0400920 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
921 return -7;
922 }
923
924 if (do_snb_cstates)
925 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
926 return -8;
927
Len Brown889facb2012-11-08 00:48:57 -0500928 if (do_dts) {
929 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
930 return -9;
931 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
932 }
933
934
Len Brownc98d5d92012-06-04 00:56:40 -0400935 /* collect package counters only for 1st core in package */
936 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
937 return 0;
938
Len Brown144b44b2013-11-09 00:30:16 -0500939 if (do_nhm_cstates && !do_slm_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -0400940 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
941 return -9;
942 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
943 return -10;
944 }
945 if (do_snb_cstates) {
946 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
947 return -11;
948 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
949 return -12;
Len Brown103a8fe2010-10-22 23:53:03 -0400950 }
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800951 if (do_c8_c9_c10) {
952 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
953 return -13;
954 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
955 return -13;
956 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
957 return -13;
958 }
Len Brown889facb2012-11-08 00:48:57 -0500959 if (do_rapl & RAPL_PKG) {
960 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
961 return -13;
962 p->energy_pkg = msr & 0xFFFFFFFF;
963 }
964 if (do_rapl & RAPL_CORES) {
965 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
966 return -14;
967 p->energy_cores = msr & 0xFFFFFFFF;
968 }
969 if (do_rapl & RAPL_DRAM) {
970 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
971 return -15;
972 p->energy_dram = msr & 0xFFFFFFFF;
973 }
974 if (do_rapl & RAPL_GFX) {
975 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
976 return -16;
977 p->energy_gfx = msr & 0xFFFFFFFF;
978 }
979 if (do_rapl & RAPL_PKG_PERF_STATUS) {
980 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
981 return -16;
982 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
983 }
984 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
985 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
986 return -16;
987 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
988 }
989 if (do_ptm) {
990 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
991 return -17;
992 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
993 }
Len Brown103a8fe2010-10-22 23:53:03 -0400994 return 0;
995}
996
Len Brownc98d5d92012-06-04 00:56:40 -0400997void print_verbose_header(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400998{
999 unsigned long long msr;
1000 unsigned int ratio;
1001
1002 if (!do_nehalem_platform_info)
1003 return;
1004
Len Brown9c63a652012-10-31 01:29:52 -04001005 get_msr(0, MSR_NHM_PLATFORM_INFO, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001006
Len Brown67920412013-01-31 15:22:15 -05001007 fprintf(stderr, "cpu0: MSR_NHM_PLATFORM_INFO: 0x%08llx\n", msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001008
Len Brown103a8fe2010-10-22 23:53:03 -04001009 ratio = (msr >> 40) & 0xFF;
1010 fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency\n",
1011 ratio, bclk, ratio * bclk);
1012
1013 ratio = (msr >> 8) & 0xFF;
1014 fprintf(stderr, "%d * %.0f = %.0f MHz TSC frequency\n",
1015 ratio, bclk, ratio * bclk);
1016
Len Brown67920412013-01-31 15:22:15 -05001017 get_msr(0, MSR_IA32_POWER_CTL, &msr);
Len Brown144b44b2013-11-09 00:30:16 -05001018 fprintf(stderr, "cpu0: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
Len Brown67920412013-01-31 15:22:15 -05001019 msr, msr & 0x2 ? "EN" : "DIS");
1020
Len Brown6574a5d2012-09-21 00:01:31 -04001021 if (!do_ivt_turbo_ratio_limit)
1022 goto print_nhm_turbo_ratio_limits;
1023
1024 get_msr(0, MSR_IVT_TURBO_RATIO_LIMIT, &msr);
1025
Len Brown67920412013-01-31 15:22:15 -05001026 fprintf(stderr, "cpu0: MSR_IVT_TURBO_RATIO_LIMIT: 0x%08llx\n", msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001027
1028 ratio = (msr >> 56) & 0xFF;
1029 if (ratio)
1030 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 16 active cores\n",
1031 ratio, bclk, ratio * bclk);
1032
1033 ratio = (msr >> 48) & 0xFF;
1034 if (ratio)
1035 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 15 active cores\n",
1036 ratio, bclk, ratio * bclk);
1037
1038 ratio = (msr >> 40) & 0xFF;
1039 if (ratio)
1040 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 14 active cores\n",
1041 ratio, bclk, ratio * bclk);
1042
1043 ratio = (msr >> 32) & 0xFF;
1044 if (ratio)
1045 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 13 active cores\n",
1046 ratio, bclk, ratio * bclk);
1047
1048 ratio = (msr >> 24) & 0xFF;
1049 if (ratio)
1050 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 12 active cores\n",
1051 ratio, bclk, ratio * bclk);
1052
1053 ratio = (msr >> 16) & 0xFF;
1054 if (ratio)
1055 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 11 active cores\n",
1056 ratio, bclk, ratio * bclk);
1057
1058 ratio = (msr >> 8) & 0xFF;
1059 if (ratio)
1060 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 10 active cores\n",
1061 ratio, bclk, ratio * bclk);
1062
1063 ratio = (msr >> 0) & 0xFF;
1064 if (ratio)
1065 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 9 active cores\n",
1066 ratio, bclk, ratio * bclk);
1067
1068print_nhm_turbo_ratio_limits:
Len Brown889facb2012-11-08 00:48:57 -05001069 get_msr(0, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
1070
1071#define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
1072#define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
1073
1074 fprintf(stderr, "cpu0: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", msr);
1075
1076 fprintf(stderr, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: ",
1077 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
1078 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
1079 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
1080 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
1081 (msr & (1 << 15)) ? "" : "UN",
1082 (unsigned int)msr & 7);
1083
1084
1085 switch(msr & 0x7) {
1086 case 0:
Len Brown144b44b2013-11-09 00:30:16 -05001087 fprintf(stderr, do_slm_cstates ? "no pkg states" : "pc0");
Len Brown889facb2012-11-08 00:48:57 -05001088 break;
1089 case 1:
Len Brown144b44b2013-11-09 00:30:16 -05001090 fprintf(stderr, do_slm_cstates ? "no pkg states" : do_snb_cstates ? "pc2" : "pc0");
Len Brown889facb2012-11-08 00:48:57 -05001091 break;
1092 case 2:
Len Brown144b44b2013-11-09 00:30:16 -05001093 fprintf(stderr, do_slm_cstates ? "invalid" : do_snb_cstates ? "pc6-noret" : "pc3");
Len Brown889facb2012-11-08 00:48:57 -05001094 break;
1095 case 3:
Len Brown144b44b2013-11-09 00:30:16 -05001096 fprintf(stderr, do_slm_cstates ? "invalid" : "pc6");
Len Brown889facb2012-11-08 00:48:57 -05001097 break;
1098 case 4:
Len Brown144b44b2013-11-09 00:30:16 -05001099 fprintf(stderr, do_slm_cstates ? "pc4" : "pc7");
Len Brown889facb2012-11-08 00:48:57 -05001100 break;
1101 case 5:
Len Brown144b44b2013-11-09 00:30:16 -05001102 fprintf(stderr, do_slm_cstates ? "invalid" : do_snb_cstates ? "pc7s" : "invalid");
1103 break;
1104 case 6:
1105 fprintf(stderr, do_slm_cstates ? "pc6" : "invalid");
Len Brown889facb2012-11-08 00:48:57 -05001106 break;
1107 case 7:
Len Brown144b44b2013-11-09 00:30:16 -05001108 fprintf(stderr, do_slm_cstates ? "pc7" : "unlimited");
Len Brown889facb2012-11-08 00:48:57 -05001109 break;
1110 default:
1111 fprintf(stderr, "invalid");
1112 }
1113 fprintf(stderr, ")\n");
Len Brown103a8fe2010-10-22 23:53:03 -04001114
1115 if (!do_nehalem_turbo_ratio_limit)
1116 return;
1117
Len Brown9c63a652012-10-31 01:29:52 -04001118 get_msr(0, MSR_NHM_TURBO_RATIO_LIMIT, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001119
Len Brown67920412013-01-31 15:22:15 -05001120 fprintf(stderr, "cpu0: MSR_NHM_TURBO_RATIO_LIMIT: 0x%08llx\n", msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001121
1122 ratio = (msr >> 56) & 0xFF;
1123 if (ratio)
1124 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 8 active cores\n",
1125 ratio, bclk, ratio * bclk);
1126
1127 ratio = (msr >> 48) & 0xFF;
1128 if (ratio)
1129 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 7 active cores\n",
1130 ratio, bclk, ratio * bclk);
1131
1132 ratio = (msr >> 40) & 0xFF;
1133 if (ratio)
1134 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 6 active cores\n",
1135 ratio, bclk, ratio * bclk);
1136
1137 ratio = (msr >> 32) & 0xFF;
1138 if (ratio)
1139 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 5 active cores\n",
1140 ratio, bclk, ratio * bclk);
1141
Len Brown103a8fe2010-10-22 23:53:03 -04001142 ratio = (msr >> 24) & 0xFF;
1143 if (ratio)
1144 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
1145 ratio, bclk, ratio * bclk);
1146
1147 ratio = (msr >> 16) & 0xFF;
1148 if (ratio)
1149 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
1150 ratio, bclk, ratio * bclk);
1151
1152 ratio = (msr >> 8) & 0xFF;
1153 if (ratio)
1154 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
1155 ratio, bclk, ratio * bclk);
1156
1157 ratio = (msr >> 0) & 0xFF;
1158 if (ratio)
1159 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
1160 ratio, bclk, ratio * bclk);
Len Brown103a8fe2010-10-22 23:53:03 -04001161}
1162
Len Brownc98d5d92012-06-04 00:56:40 -04001163void free_all_buffers(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001164{
Len Brownc98d5d92012-06-04 00:56:40 -04001165 CPU_FREE(cpu_present_set);
1166 cpu_present_set = NULL;
1167 cpu_present_set = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001168
Len Brownc98d5d92012-06-04 00:56:40 -04001169 CPU_FREE(cpu_affinity_set);
1170 cpu_affinity_set = NULL;
1171 cpu_affinity_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001172
Len Brownc98d5d92012-06-04 00:56:40 -04001173 free(thread_even);
1174 free(core_even);
1175 free(package_even);
1176
1177 thread_even = NULL;
1178 core_even = NULL;
1179 package_even = NULL;
1180
1181 free(thread_odd);
1182 free(core_odd);
1183 free(package_odd);
1184
1185 thread_odd = NULL;
1186 core_odd = NULL;
1187 package_odd = NULL;
1188
1189 free(output_buffer);
1190 output_buffer = NULL;
1191 outp = NULL;
Len Brown103a8fe2010-10-22 23:53:03 -04001192}
1193
Len Brownc98d5d92012-06-04 00:56:40 -04001194/*
Josh Triplett57a42a32013-08-20 17:20:17 -07001195 * Open a file, and exit on failure
1196 */
1197FILE *fopen_or_die(const char *path, const char *mode)
1198{
1199 FILE *filep = fopen(path, "r");
Josh Triplettb2c95d92013-08-20 17:20:18 -07001200 if (!filep)
1201 err(1, "%s: open failed", path);
Josh Triplett57a42a32013-08-20 17:20:17 -07001202 return filep;
1203}
1204
1205/*
Josh Triplett95aebc42013-08-20 17:20:16 -07001206 * Parse a file containing a single int.
1207 */
1208int parse_int_file(const char *fmt, ...)
1209{
1210 va_list args;
1211 char path[PATH_MAX];
1212 FILE *filep;
1213 int value;
1214
1215 va_start(args, fmt);
1216 vsnprintf(path, sizeof(path), fmt, args);
1217 va_end(args);
Josh Triplett57a42a32013-08-20 17:20:17 -07001218 filep = fopen_or_die(path, "r");
Josh Triplettb2c95d92013-08-20 17:20:18 -07001219 if (fscanf(filep, "%d", &value) != 1)
1220 err(1, "%s: failed to parse number from file", path);
Josh Triplett95aebc42013-08-20 17:20:16 -07001221 fclose(filep);
1222 return value;
1223}
1224
1225/*
Len Brownc98d5d92012-06-04 00:56:40 -04001226 * cpu_is_first_sibling_in_core(cpu)
1227 * return 1 if given CPU is 1st HT sibling in the core
1228 */
1229int cpu_is_first_sibling_in_core(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001230{
Josh Triplett95aebc42013-08-20 17:20:16 -07001231 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001232}
1233
Len Brownc98d5d92012-06-04 00:56:40 -04001234/*
1235 * cpu_is_first_core_in_package(cpu)
1236 * return 1 if given CPU is 1st core in package
1237 */
1238int cpu_is_first_core_in_package(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001239{
Josh Triplett95aebc42013-08-20 17:20:16 -07001240 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001241}
1242
1243int get_physical_package_id(int cpu)
1244{
Josh Triplett95aebc42013-08-20 17:20:16 -07001245 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001246}
1247
1248int get_core_id(int cpu)
1249{
Josh Triplett95aebc42013-08-20 17:20:16 -07001250 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001251}
1252
Len Brownc98d5d92012-06-04 00:56:40 -04001253int get_num_ht_siblings(int cpu)
1254{
1255 char path[80];
1256 FILE *filep;
1257 int sib1, sib2;
1258 int matches;
1259 char character;
1260
1261 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
Josh Triplett57a42a32013-08-20 17:20:17 -07001262 filep = fopen_or_die(path, "r");
Len Brownc98d5d92012-06-04 00:56:40 -04001263 /*
1264 * file format:
1265 * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4)
1266 * otherwinse 1 sibling (self).
1267 */
1268 matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2);
1269
1270 fclose(filep);
1271
1272 if (matches == 3)
1273 return 2;
1274 else
1275 return 1;
1276}
1277
Len Brown103a8fe2010-10-22 23:53:03 -04001278/*
Len Brownc98d5d92012-06-04 00:56:40 -04001279 * run func(thread, core, package) in topology order
1280 * skip non-present cpus
Len Brown103a8fe2010-10-22 23:53:03 -04001281 */
1282
Len Brownc98d5d92012-06-04 00:56:40 -04001283int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
1284 struct pkg_data *, struct thread_data *, struct core_data *,
1285 struct pkg_data *), struct thread_data *thread_base,
1286 struct core_data *core_base, struct pkg_data *pkg_base,
1287 struct thread_data *thread_base2, struct core_data *core_base2,
1288 struct pkg_data *pkg_base2)
1289{
1290 int retval, pkg_no, core_no, thread_no;
1291
1292 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
1293 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
1294 for (thread_no = 0; thread_no <
1295 topo.num_threads_per_core; ++thread_no) {
1296 struct thread_data *t, *t2;
1297 struct core_data *c, *c2;
1298 struct pkg_data *p, *p2;
1299
1300 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
1301
1302 if (cpu_is_not_present(t->cpu_id))
1303 continue;
1304
1305 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
1306
1307 c = GET_CORE(core_base, core_no, pkg_no);
1308 c2 = GET_CORE(core_base2, core_no, pkg_no);
1309
1310 p = GET_PKG(pkg_base, pkg_no);
1311 p2 = GET_PKG(pkg_base2, pkg_no);
1312
1313 retval = func(t, c, p, t2, c2, p2);
1314 if (retval)
1315 return retval;
1316 }
1317 }
1318 }
1319 return 0;
1320}
1321
1322/*
1323 * run func(cpu) on every cpu in /proc/stat
1324 * return max_cpu number
1325 */
1326int for_all_proc_cpus(int (func)(int))
Len Brown103a8fe2010-10-22 23:53:03 -04001327{
1328 FILE *fp;
Len Brownc98d5d92012-06-04 00:56:40 -04001329 int cpu_num;
Len Brown103a8fe2010-10-22 23:53:03 -04001330 int retval;
1331
Josh Triplett57a42a32013-08-20 17:20:17 -07001332 fp = fopen_or_die(proc_stat, "r");
Len Brown103a8fe2010-10-22 23:53:03 -04001333
1334 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
Josh Triplettb2c95d92013-08-20 17:20:18 -07001335 if (retval != 0)
1336 err(1, "%s: failed to parse format", proc_stat);
Len Brown103a8fe2010-10-22 23:53:03 -04001337
Len Brownc98d5d92012-06-04 00:56:40 -04001338 while (1) {
1339 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 -04001340 if (retval != 1)
1341 break;
1342
Len Brownc98d5d92012-06-04 00:56:40 -04001343 retval = func(cpu_num);
1344 if (retval) {
1345 fclose(fp);
1346 return(retval);
1347 }
Len Brown103a8fe2010-10-22 23:53:03 -04001348 }
1349 fclose(fp);
Len Brownc98d5d92012-06-04 00:56:40 -04001350 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001351}
1352
1353void re_initialize(void)
1354{
Len Brownc98d5d92012-06-04 00:56:40 -04001355 free_all_buffers();
1356 setup_all_buffers();
1357 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04001358}
1359
Len Brownc98d5d92012-06-04 00:56:40 -04001360
Len Brown103a8fe2010-10-22 23:53:03 -04001361/*
Len Brownc98d5d92012-06-04 00:56:40 -04001362 * count_cpus()
1363 * remember the last one seen, it will be the max
Len Brown103a8fe2010-10-22 23:53:03 -04001364 */
Len Brownc98d5d92012-06-04 00:56:40 -04001365int count_cpus(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001366{
Len Brownc98d5d92012-06-04 00:56:40 -04001367 if (topo.max_cpu_num < cpu)
1368 topo.max_cpu_num = cpu;
Len Brown103a8fe2010-10-22 23:53:03 -04001369
Len Brownc98d5d92012-06-04 00:56:40 -04001370 topo.num_cpus += 1;
1371 return 0;
1372}
1373int mark_cpu_present(int cpu)
1374{
1375 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brown15aaa342012-03-29 22:19:58 -04001376 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001377}
1378
1379void turbostat_loop()
1380{
Len Brownc98d5d92012-06-04 00:56:40 -04001381 int retval;
Len Browne52966c2012-11-08 22:38:05 -05001382 int restarted = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001383
Len Brown103a8fe2010-10-22 23:53:03 -04001384restart:
Len Browne52966c2012-11-08 22:38:05 -05001385 restarted++;
1386
Len Brownc98d5d92012-06-04 00:56:40 -04001387 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001388 if (retval < -1) {
1389 exit(retval);
1390 } else if (retval == -1) {
Len Browne52966c2012-11-08 22:38:05 -05001391 if (restarted > 1) {
1392 exit(retval);
1393 }
Len Brownc98d5d92012-06-04 00:56:40 -04001394 re_initialize();
1395 goto restart;
1396 }
Len Browne52966c2012-11-08 22:38:05 -05001397 restarted = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001398 gettimeofday(&tv_even, (struct timezone *)NULL);
1399
1400 while (1) {
Len Brownc98d5d92012-06-04 00:56:40 -04001401 if (for_all_proc_cpus(cpu_is_not_present)) {
Len Brown103a8fe2010-10-22 23:53:03 -04001402 re_initialize();
1403 goto restart;
1404 }
1405 sleep(interval_sec);
Len Brownc98d5d92012-06-04 00:56:40 -04001406 retval = for_all_cpus(get_counters, ODD_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001407 if (retval < -1) {
1408 exit(retval);
1409 } else if (retval == -1) {
Len Brown15aaa342012-03-29 22:19:58 -04001410 re_initialize();
1411 goto restart;
1412 }
Len Brown103a8fe2010-10-22 23:53:03 -04001413 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04001414 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04001415 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
1416 compute_average(EVEN_COUNTERS);
1417 format_all_counters(EVEN_COUNTERS);
1418 flush_stdout();
Len Brown15aaa342012-03-29 22:19:58 -04001419 sleep(interval_sec);
Len Brownc98d5d92012-06-04 00:56:40 -04001420 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001421 if (retval < -1) {
1422 exit(retval);
1423 } else if (retval == -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04001424 re_initialize();
1425 goto restart;
1426 }
Len Brown103a8fe2010-10-22 23:53:03 -04001427 gettimeofday(&tv_even, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04001428 timersub(&tv_even, &tv_odd, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04001429 for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS);
1430 compute_average(ODD_COUNTERS);
1431 format_all_counters(ODD_COUNTERS);
1432 flush_stdout();
Len Brown103a8fe2010-10-22 23:53:03 -04001433 }
1434}
1435
1436void check_dev_msr()
1437{
1438 struct stat sb;
1439
Josh Triplettb2c95d92013-08-20 17:20:18 -07001440 if (stat("/dev/cpu/0/msr", &sb))
1441 err(-5, "no /dev/cpu/0/msr\n"
1442 "Try \"# modprobe msr\"");
Len Brown103a8fe2010-10-22 23:53:03 -04001443}
1444
1445void check_super_user()
1446{
Josh Triplettb2c95d92013-08-20 17:20:18 -07001447 if (getuid() != 0)
1448 errx(-6, "must be root");
Len Brown103a8fe2010-10-22 23:53:03 -04001449}
1450
1451int has_nehalem_turbo_ratio_limit(unsigned int family, unsigned int model)
1452{
1453 if (!genuine_intel)
1454 return 0;
1455
1456 if (family != 6)
1457 return 0;
1458
1459 switch (model) {
1460 case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
1461 case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
1462 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
1463 case 0x25: /* Westmere Client - Clarkdale, Arrandale */
1464 case 0x2C: /* Westmere EP - Gulftown */
1465 case 0x2A: /* SNB */
1466 case 0x2D: /* SNB Xeon */
Len Brown553575f2011-11-18 03:32:01 -05001467 case 0x3A: /* IVB */
Len Brown13006512012-09-26 18:11:31 -04001468 case 0x3E: /* IVB Xeon */
Len Brown70b43402013-01-08 01:26:07 -05001469 case 0x3C: /* HSW */
Len Browne6f9bb32013-12-03 02:19:19 -05001470 case 0x3F: /* HSX */
Len Brown70b43402013-01-08 01:26:07 -05001471 case 0x45: /* HSW */
Len Brown149c2312013-03-15 10:58:02 -04001472 case 0x46: /* HSW */
Len Brown144b44b2013-11-09 00:30:16 -05001473 case 0x37: /* BYT */
1474 case 0x4D: /* AVN */
Len Brown103a8fe2010-10-22 23:53:03 -04001475 return 1;
1476 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1477 case 0x2F: /* Westmere-EX Xeon - Eagleton */
1478 default:
1479 return 0;
1480 }
1481}
Len Brown6574a5d2012-09-21 00:01:31 -04001482int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
1483{
1484 if (!genuine_intel)
1485 return 0;
1486
1487 if (family != 6)
1488 return 0;
1489
1490 switch (model) {
1491 case 0x3E: /* IVB Xeon */
1492 return 1;
1493 default:
1494 return 0;
1495 }
1496}
1497
Len Brown889facb2012-11-08 00:48:57 -05001498/*
1499 * print_epb()
1500 * Decode the ENERGY_PERF_BIAS MSR
1501 */
1502int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1503{
1504 unsigned long long msr;
1505 char *epb_string;
1506 int cpu;
1507
1508 if (!has_epb)
1509 return 0;
1510
1511 cpu = t->cpu_id;
1512
1513 /* EPB is per-package */
1514 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1515 return 0;
1516
1517 if (cpu_migrate(cpu)) {
1518 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1519 return -1;
1520 }
1521
1522 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
1523 return 0;
1524
1525 switch (msr & 0x7) {
1526 case ENERGY_PERF_BIAS_PERFORMANCE:
1527 epb_string = "performance";
1528 break;
1529 case ENERGY_PERF_BIAS_NORMAL:
1530 epb_string = "balanced";
1531 break;
1532 case ENERGY_PERF_BIAS_POWERSAVE:
1533 epb_string = "powersave";
1534 break;
1535 default:
1536 epb_string = "custom";
1537 break;
1538 }
1539 fprintf(stderr, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
1540
1541 return 0;
1542}
1543
1544#define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
1545#define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
1546
Len Brown144b44b2013-11-09 00:30:16 -05001547double get_tdp(model)
1548{
1549 unsigned long long msr;
1550
1551 if (do_rapl & RAPL_PKG_POWER_INFO)
1552 if (!get_msr(0, MSR_PKG_POWER_INFO, &msr))
1553 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
1554
1555 switch (model) {
1556 case 0x37:
1557 case 0x4D:
1558 return 30.0;
1559 default:
1560 return 135.0;
1561 }
1562}
1563
1564
Len Brown889facb2012-11-08 00:48:57 -05001565/*
1566 * rapl_probe()
1567 *
Len Brown144b44b2013-11-09 00:30:16 -05001568 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
Len Brown889facb2012-11-08 00:48:57 -05001569 */
1570void rapl_probe(unsigned int family, unsigned int model)
1571{
1572 unsigned long long msr;
Len Brown144b44b2013-11-09 00:30:16 -05001573 unsigned int time_unit;
Len Brown889facb2012-11-08 00:48:57 -05001574 double tdp;
1575
1576 if (!genuine_intel)
1577 return;
1578
1579 if (family != 6)
1580 return;
1581
1582 switch (model) {
1583 case 0x2A:
1584 case 0x3A:
Len Brown70b43402013-01-08 01:26:07 -05001585 case 0x3C: /* HSW */
Len Brown70b43402013-01-08 01:26:07 -05001586 case 0x45: /* HSW */
Len Brown149c2312013-03-15 10:58:02 -04001587 case 0x46: /* HSW */
Len Brown144b44b2013-11-09 00:30:16 -05001588 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
Len Brown889facb2012-11-08 00:48:57 -05001589 break;
Len Browne6f9bb32013-12-03 02:19:19 -05001590 case 0x3F: /* HSX */
1591 do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
1592 break;
Len Brown889facb2012-11-08 00:48:57 -05001593 case 0x2D:
1594 case 0x3E:
Len Brown144b44b2013-11-09 00:30:16 -05001595 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_PKG_PERF_STATUS | RAPL_DRAM_PERF_STATUS | RAPL_PKG_POWER_INFO;
1596 break;
1597 case 0x37: /* BYT */
1598 case 0x4D: /* AVN */
1599 do_rapl = RAPL_PKG | RAPL_CORES ;
Len Brown889facb2012-11-08 00:48:57 -05001600 break;
1601 default:
1602 return;
1603 }
1604
1605 /* units on package 0, verify later other packages match */
1606 if (get_msr(0, MSR_RAPL_POWER_UNIT, &msr))
1607 return;
1608
1609 rapl_power_units = 1.0 / (1 << (msr & 0xF));
Len Brown144b44b2013-11-09 00:30:16 -05001610 if (model == 0x37)
1611 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
1612 else
1613 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
Len Brown889facb2012-11-08 00:48:57 -05001614
Len Brown144b44b2013-11-09 00:30:16 -05001615 time_unit = msr >> 16 & 0xF;
1616 if (time_unit == 0)
1617 time_unit = 0xA;
Len Brown889facb2012-11-08 00:48:57 -05001618
Len Brown144b44b2013-11-09 00:30:16 -05001619 rapl_time_units = 1.0 / (1 << (time_unit));
1620
1621 tdp = get_tdp(model);
Len Brown889facb2012-11-08 00:48:57 -05001622
1623 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
Len Brown889facb2012-11-08 00:48:57 -05001624 if (verbose)
Len Brown144b44b2013-11-09 00:30:16 -05001625 fprintf(stderr, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
Len Brown889facb2012-11-08 00:48:57 -05001626
1627 return;
1628}
1629
1630int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1631{
1632 unsigned long long msr;
1633 unsigned int dts;
1634 int cpu;
1635
1636 if (!(do_dts || do_ptm))
1637 return 0;
1638
1639 cpu = t->cpu_id;
1640
1641 /* DTS is per-core, no need to print for each thread */
1642 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1643 return 0;
1644
1645 if (cpu_migrate(cpu)) {
1646 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1647 return -1;
1648 }
1649
1650 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
1651 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1652 return 0;
1653
1654 dts = (msr >> 16) & 0x7F;
1655 fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
1656 cpu, msr, tcc_activation_temp - dts);
1657
1658#ifdef THERM_DEBUG
1659 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
1660 return 0;
1661
1662 dts = (msr >> 16) & 0x7F;
1663 dts2 = (msr >> 8) & 0x7F;
1664 fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
1665 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
1666#endif
1667 }
1668
1669
1670 if (do_dts) {
1671 unsigned int resolution;
1672
1673 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1674 return 0;
1675
1676 dts = (msr >> 16) & 0x7F;
1677 resolution = (msr >> 27) & 0xF;
1678 fprintf(stderr, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
1679 cpu, msr, tcc_activation_temp - dts, resolution);
1680
1681#ifdef THERM_DEBUG
1682 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
1683 return 0;
1684
1685 dts = (msr >> 16) & 0x7F;
1686 dts2 = (msr >> 8) & 0x7F;
1687 fprintf(stderr, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
1688 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
1689#endif
1690 }
1691
1692 return 0;
1693}
1694
1695void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
1696{
1697 fprintf(stderr, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
1698 cpu, label,
1699 ((msr >> 15) & 1) ? "EN" : "DIS",
1700 ((msr >> 0) & 0x7FFF) * rapl_power_units,
1701 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
1702 (((msr >> 16) & 1) ? "EN" : "DIS"));
1703
1704 return;
1705}
1706
1707int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1708{
1709 unsigned long long msr;
1710 int cpu;
Len Brown889facb2012-11-08 00:48:57 -05001711
1712 if (!do_rapl)
1713 return 0;
1714
1715 /* RAPL counters are per package, so print only for 1st thread/package */
1716 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1717 return 0;
1718
1719 cpu = t->cpu_id;
1720 if (cpu_migrate(cpu)) {
1721 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1722 return -1;
1723 }
1724
1725 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
1726 return -1;
1727
Len Brown889facb2012-11-08 00:48:57 -05001728 if (verbose) {
1729 fprintf(stderr, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx "
1730 "(%f Watts, %f Joules, %f sec.)\n", cpu, msr,
Len Brown144b44b2013-11-09 00:30:16 -05001731 rapl_power_units, rapl_energy_units, rapl_time_units);
Len Brown889facb2012-11-08 00:48:57 -05001732 }
Len Brown144b44b2013-11-09 00:30:16 -05001733 if (do_rapl & RAPL_PKG_POWER_INFO) {
1734
Len Brown889facb2012-11-08 00:48:57 -05001735 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
1736 return -5;
1737
1738
1739 fprintf(stderr, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
1740 cpu, msr,
1741 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1742 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1743 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1744 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
1745
Len Brown144b44b2013-11-09 00:30:16 -05001746 }
1747 if (do_rapl & RAPL_PKG) {
1748
Len Brown889facb2012-11-08 00:48:57 -05001749 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
1750 return -9;
1751
1752 fprintf(stderr, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
1753 cpu, msr, (msr >> 63) & 1 ? "": "UN");
1754
1755 print_power_limit_msr(cpu, msr, "PKG Limit #1");
1756 fprintf(stderr, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
1757 cpu,
1758 ((msr >> 47) & 1) ? "EN" : "DIS",
1759 ((msr >> 32) & 0x7FFF) * rapl_power_units,
1760 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
1761 ((msr >> 48) & 1) ? "EN" : "DIS");
1762 }
1763
1764 if (do_rapl & RAPL_DRAM) {
1765 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
1766 return -6;
1767
1768
1769 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
1770 cpu, msr,
1771 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1772 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1773 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1774 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
1775
1776
1777 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
1778 return -9;
1779 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
1780 cpu, msr, (msr >> 31) & 1 ? "": "UN");
1781
1782 print_power_limit_msr(cpu, msr, "DRAM Limit");
1783 }
Len Brown144b44b2013-11-09 00:30:16 -05001784 if (do_rapl & RAPL_CORE_POLICY) {
Len Brown889facb2012-11-08 00:48:57 -05001785 if (verbose) {
1786 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
1787 return -7;
1788
1789 fprintf(stderr, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown144b44b2013-11-09 00:30:16 -05001790 }
1791 }
1792 if (do_rapl & RAPL_CORES) {
1793 if (verbose) {
Len Brown889facb2012-11-08 00:48:57 -05001794
1795 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
1796 return -9;
1797 fprintf(stderr, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
1798 cpu, msr, (msr >> 31) & 1 ? "": "UN");
1799 print_power_limit_msr(cpu, msr, "Cores Limit");
1800 }
1801 }
1802 if (do_rapl & RAPL_GFX) {
1803 if (verbose) {
1804 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
1805 return -8;
1806
1807 fprintf(stderr, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
1808
1809 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
1810 return -9;
1811 fprintf(stderr, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
1812 cpu, msr, (msr >> 31) & 1 ? "": "UN");
1813 print_power_limit_msr(cpu, msr, "GFX Limit");
1814 }
1815 }
1816 return 0;
1817}
1818
Len Brown103a8fe2010-10-22 23:53:03 -04001819
1820int is_snb(unsigned int family, unsigned int model)
1821{
1822 if (!genuine_intel)
1823 return 0;
1824
1825 switch (model) {
1826 case 0x2A:
1827 case 0x2D:
Len Brown650a37f2012-06-03 23:34:44 -04001828 case 0x3A: /* IVB */
Len Brown13006512012-09-26 18:11:31 -04001829 case 0x3E: /* IVB Xeon */
Len Brown70b43402013-01-08 01:26:07 -05001830 case 0x3C: /* HSW */
1831 case 0x3F: /* HSW */
1832 case 0x45: /* HSW */
Len Brown149c2312013-03-15 10:58:02 -04001833 case 0x46: /* HSW */
Len Brown103a8fe2010-10-22 23:53:03 -04001834 return 1;
1835 }
1836 return 0;
1837}
1838
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001839int has_c8_c9_c10(unsigned int family, unsigned int model)
1840{
1841 if (!genuine_intel)
1842 return 0;
1843
1844 switch (model) {
1845 case 0x45:
1846 return 1;
1847 }
1848 return 0;
1849}
1850
1851
Len Brown144b44b2013-11-09 00:30:16 -05001852int is_slm(unsigned int family, unsigned int model)
1853{
1854 if (!genuine_intel)
1855 return 0;
1856 switch (model) {
1857 case 0x37: /* BYT */
1858 case 0x4D: /* AVN */
1859 return 1;
1860 }
1861 return 0;
1862}
1863
1864#define SLM_BCLK_FREQS 5
1865double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
1866
1867double slm_bclk(void)
1868{
1869 unsigned long long msr = 3;
1870 unsigned int i;
1871 double freq;
1872
1873 if (get_msr(0, MSR_FSB_FREQ, &msr))
1874 fprintf(stderr, "SLM BCLK: unknown\n");
1875
1876 i = msr & 0xf;
1877 if (i >= SLM_BCLK_FREQS) {
1878 fprintf(stderr, "SLM BCLK[%d] invalid\n", i);
1879 msr = 3;
1880 }
1881 freq = slm_freq_table[i];
1882
1883 fprintf(stderr, "SLM BCLK: %.1f Mhz\n", freq);
1884
1885 return freq;
1886}
1887
Len Brown103a8fe2010-10-22 23:53:03 -04001888double discover_bclk(unsigned int family, unsigned int model)
1889{
1890 if (is_snb(family, model))
1891 return 100.00;
Len Brown144b44b2013-11-09 00:30:16 -05001892 else if (is_slm(family, model))
1893 return slm_bclk();
Len Brown103a8fe2010-10-22 23:53:03 -04001894 else
1895 return 133.33;
1896}
1897
Len Brown889facb2012-11-08 00:48:57 -05001898/*
1899 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
1900 * the Thermal Control Circuit (TCC) activates.
1901 * This is usually equal to tjMax.
1902 *
1903 * Older processors do not have this MSR, so there we guess,
1904 * but also allow cmdline over-ride with -T.
1905 *
1906 * Several MSR temperature values are in units of degrees-C
1907 * below this value, including the Digital Thermal Sensor (DTS),
1908 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
1909 */
1910int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1911{
1912 unsigned long long msr;
1913 unsigned int target_c_local;
1914 int cpu;
1915
1916 /* tcc_activation_temp is used only for dts or ptm */
1917 if (!(do_dts || do_ptm))
1918 return 0;
1919
1920 /* this is a per-package concept */
1921 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1922 return 0;
1923
1924 cpu = t->cpu_id;
1925 if (cpu_migrate(cpu)) {
1926 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1927 return -1;
1928 }
1929
1930 if (tcc_activation_temp_override != 0) {
1931 tcc_activation_temp = tcc_activation_temp_override;
1932 fprintf(stderr, "cpu%d: Using cmdline TCC Target (%d C)\n",
1933 cpu, tcc_activation_temp);
1934 return 0;
1935 }
1936
1937 /* Temperature Target MSR is Nehalem and newer only */
1938 if (!do_nehalem_platform_info)
1939 goto guess;
1940
1941 if (get_msr(0, MSR_IA32_TEMPERATURE_TARGET, &msr))
1942 goto guess;
1943
1944 target_c_local = (msr >> 16) & 0x7F;
1945
1946 if (verbose)
1947 fprintf(stderr, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
1948 cpu, msr, target_c_local);
1949
Len Brown144b44b2013-11-09 00:30:16 -05001950 if (target_c_local < 85 || target_c_local > 127)
Len Brown889facb2012-11-08 00:48:57 -05001951 goto guess;
1952
1953 tcc_activation_temp = target_c_local;
1954
1955 return 0;
1956
1957guess:
1958 tcc_activation_temp = TJMAX_DEFAULT;
1959 fprintf(stderr, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
1960 cpu, tcc_activation_temp);
1961
1962 return 0;
1963}
Len Brown103a8fe2010-10-22 23:53:03 -04001964void check_cpuid()
1965{
1966 unsigned int eax, ebx, ecx, edx, max_level;
1967 unsigned int fms, family, model, stepping;
1968
1969 eax = ebx = ecx = edx = 0;
1970
Josh Triplett2b928652013-08-20 17:20:14 -07001971 __get_cpuid(0, &max_level, &ebx, &ecx, &edx);
Len Brown103a8fe2010-10-22 23:53:03 -04001972
1973 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
1974 genuine_intel = 1;
1975
1976 if (verbose)
Len Brown889facb2012-11-08 00:48:57 -05001977 fprintf(stderr, "CPUID(0): %.4s%.4s%.4s ",
Len Brown103a8fe2010-10-22 23:53:03 -04001978 (char *)&ebx, (char *)&edx, (char *)&ecx);
1979
Josh Triplett2b928652013-08-20 17:20:14 -07001980 __get_cpuid(1, &fms, &ebx, &ecx, &edx);
Len Brown103a8fe2010-10-22 23:53:03 -04001981 family = (fms >> 8) & 0xf;
1982 model = (fms >> 4) & 0xf;
1983 stepping = fms & 0xf;
1984 if (family == 6 || family == 0xf)
1985 model += ((fms >> 16) & 0xf) << 4;
1986
1987 if (verbose)
1988 fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
1989 max_level, family, model, stepping, family, model, stepping);
1990
Josh Triplettb2c95d92013-08-20 17:20:18 -07001991 if (!(edx & (1 << 5)))
1992 errx(1, "CPUID: no MSR");
Len Brown103a8fe2010-10-22 23:53:03 -04001993
1994 /*
1995 * check max extended function levels of CPUID.
1996 * This is needed to check for invariant TSC.
1997 * This check is valid for both Intel and AMD.
1998 */
1999 ebx = ecx = edx = 0;
Josh Triplett2b928652013-08-20 17:20:14 -07002000 __get_cpuid(0x80000000, &max_level, &ebx, &ecx, &edx);
Len Brown103a8fe2010-10-22 23:53:03 -04002001
Josh Triplettb2c95d92013-08-20 17:20:18 -07002002 if (max_level < 0x80000007)
2003 errx(1, "CPUID: no invariant TSC (max_level 0x%x)", max_level);
Len Brown103a8fe2010-10-22 23:53:03 -04002004
2005 /*
2006 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
2007 * this check is valid for both Intel and AMD
2008 */
Josh Triplett2b928652013-08-20 17:20:14 -07002009 __get_cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
Thomas Renninger8209e052011-01-21 15:11:19 +01002010 has_invariant_tsc = edx & (1 << 8);
Len Brown103a8fe2010-10-22 23:53:03 -04002011
Josh Triplettb2c95d92013-08-20 17:20:18 -07002012 if (!has_invariant_tsc)
2013 errx(1, "No invariant TSC");
Len Brown103a8fe2010-10-22 23:53:03 -04002014
2015 /*
2016 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
2017 * this check is valid for both Intel and AMD
2018 */
2019
Josh Triplett2b928652013-08-20 17:20:14 -07002020 __get_cpuid(0x6, &eax, &ebx, &ecx, &edx);
Thomas Renninger8209e052011-01-21 15:11:19 +01002021 has_aperf = ecx & (1 << 0);
Len Brown889facb2012-11-08 00:48:57 -05002022 do_dts = eax & (1 << 0);
2023 do_ptm = eax & (1 << 6);
2024 has_epb = ecx & (1 << 3);
2025
2026 if (verbose)
2027 fprintf(stderr, "CPUID(6): %s%s%s%s\n",
2028 has_aperf ? "APERF" : "No APERF!",
2029 do_dts ? ", DTS" : "",
2030 do_ptm ? ", PTM": "",
2031 has_epb ? ", EPB": "");
2032
2033 if (!has_aperf)
Josh Triplettb2c95d92013-08-20 17:20:18 -07002034 errx(-1, "No APERF");
Len Brown103a8fe2010-10-22 23:53:03 -04002035
2036 do_nehalem_platform_info = genuine_intel && has_invariant_tsc;
2037 do_nhm_cstates = genuine_intel; /* all Intel w/ non-stop TSC have NHM counters */
Len Brown1ed51012013-02-10 17:19:24 -05002038 do_smi = do_nhm_cstates;
Len Brown103a8fe2010-10-22 23:53:03 -04002039 do_snb_cstates = is_snb(family, model);
Kristen Carlson Accardica587102012-11-21 05:22:43 -08002040 do_c8_c9_c10 = has_c8_c9_c10(family, model);
Len Brown144b44b2013-11-09 00:30:16 -05002041 do_slm_cstates = is_slm(family, model);
Len Brown103a8fe2010-10-22 23:53:03 -04002042 bclk = discover_bclk(family, model);
2043
2044 do_nehalem_turbo_ratio_limit = has_nehalem_turbo_ratio_limit(family, model);
Len Brown6574a5d2012-09-21 00:01:31 -04002045 do_ivt_turbo_ratio_limit = has_ivt_turbo_ratio_limit(family, model);
Len Brown889facb2012-11-08 00:48:57 -05002046 rapl_probe(family, model);
2047
2048 return;
Len Brown103a8fe2010-10-22 23:53:03 -04002049}
2050
2051
2052void usage()
2053{
Josh Triplettb2c95d92013-08-20 17:20:18 -07002054 errx(1, "%s: [-v][-R][-T][-p|-P|-S][-c MSR# | -s]][-C MSR#][-m MSR#][-M MSR#][-i interval_sec | command ...]\n",
2055 progname);
Len Brown103a8fe2010-10-22 23:53:03 -04002056}
2057
2058
2059/*
2060 * in /dev/cpu/ return success for names that are numbers
2061 * ie. filter out ".", "..", "microcode".
2062 */
2063int dir_filter(const struct dirent *dirp)
2064{
2065 if (isdigit(dirp->d_name[0]))
2066 return 1;
2067 else
2068 return 0;
2069}
2070
2071int open_dev_cpu_msr(int dummy1)
2072{
2073 return 0;
2074}
2075
Len Brownc98d5d92012-06-04 00:56:40 -04002076void topology_probe()
2077{
2078 int i;
2079 int max_core_id = 0;
2080 int max_package_id = 0;
2081 int max_siblings = 0;
2082 struct cpu_topology {
2083 int core_id;
2084 int physical_package_id;
2085 } *cpus;
2086
2087 /* Initialize num_cpus, max_cpu_num */
2088 topo.num_cpus = 0;
2089 topo.max_cpu_num = 0;
2090 for_all_proc_cpus(count_cpus);
2091 if (!summary_only && topo.num_cpus > 1)
2092 show_cpu = 1;
2093
2094 if (verbose > 1)
2095 fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
2096
2097 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
Josh Triplettb2c95d92013-08-20 17:20:18 -07002098 if (cpus == NULL)
2099 err(1, "calloc cpus");
Len Brownc98d5d92012-06-04 00:56:40 -04002100
2101 /*
2102 * Allocate and initialize cpu_present_set
2103 */
2104 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07002105 if (cpu_present_set == NULL)
2106 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04002107 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2108 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
2109 for_all_proc_cpus(mark_cpu_present);
2110
2111 /*
2112 * Allocate and initialize cpu_affinity_set
2113 */
2114 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07002115 if (cpu_affinity_set == NULL)
2116 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04002117 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2118 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
2119
2120
2121 /*
2122 * For online cpus
2123 * find max_core_id, max_package_id
2124 */
2125 for (i = 0; i <= topo.max_cpu_num; ++i) {
2126 int siblings;
2127
2128 if (cpu_is_not_present(i)) {
2129 if (verbose > 1)
2130 fprintf(stderr, "cpu%d NOT PRESENT\n", i);
2131 continue;
2132 }
2133 cpus[i].core_id = get_core_id(i);
2134 if (cpus[i].core_id > max_core_id)
2135 max_core_id = cpus[i].core_id;
2136
2137 cpus[i].physical_package_id = get_physical_package_id(i);
2138 if (cpus[i].physical_package_id > max_package_id)
2139 max_package_id = cpus[i].physical_package_id;
2140
2141 siblings = get_num_ht_siblings(i);
2142 if (siblings > max_siblings)
2143 max_siblings = siblings;
2144 if (verbose > 1)
2145 fprintf(stderr, "cpu %d pkg %d core %d\n",
2146 i, cpus[i].physical_package_id, cpus[i].core_id);
2147 }
2148 topo.num_cores_per_pkg = max_core_id + 1;
2149 if (verbose > 1)
2150 fprintf(stderr, "max_core_id %d, sizing for %d cores per package\n",
2151 max_core_id, topo.num_cores_per_pkg);
2152 if (!summary_only && topo.num_cores_per_pkg > 1)
2153 show_core = 1;
2154
2155 topo.num_packages = max_package_id + 1;
2156 if (verbose > 1)
2157 fprintf(stderr, "max_package_id %d, sizing for %d packages\n",
2158 max_package_id, topo.num_packages);
2159 if (!summary_only && topo.num_packages > 1)
2160 show_pkg = 1;
2161
2162 topo.num_threads_per_core = max_siblings;
2163 if (verbose > 1)
2164 fprintf(stderr, "max_siblings %d\n", max_siblings);
2165
2166 free(cpus);
2167}
2168
2169void
2170allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
2171{
2172 int i;
2173
2174 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
2175 topo.num_packages, sizeof(struct thread_data));
2176 if (*t == NULL)
2177 goto error;
2178
2179 for (i = 0; i < topo.num_threads_per_core *
2180 topo.num_cores_per_pkg * topo.num_packages; i++)
2181 (*t)[i].cpu_id = -1;
2182
2183 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
2184 sizeof(struct core_data));
2185 if (*c == NULL)
2186 goto error;
2187
2188 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
2189 (*c)[i].core_id = -1;
2190
2191 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
2192 if (*p == NULL)
2193 goto error;
2194
2195 for (i = 0; i < topo.num_packages; i++)
2196 (*p)[i].package_id = i;
2197
2198 return;
2199error:
Josh Triplettb2c95d92013-08-20 17:20:18 -07002200 err(1, "calloc counters");
Len Brownc98d5d92012-06-04 00:56:40 -04002201}
2202/*
2203 * init_counter()
2204 *
2205 * set cpu_id, core_num, pkg_num
2206 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
2207 *
2208 * increment topo.num_cores when 1st core in pkg seen
2209 */
2210void init_counter(struct thread_data *thread_base, struct core_data *core_base,
2211 struct pkg_data *pkg_base, int thread_num, int core_num,
2212 int pkg_num, int cpu_id)
2213{
2214 struct thread_data *t;
2215 struct core_data *c;
2216 struct pkg_data *p;
2217
2218 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
2219 c = GET_CORE(core_base, core_num, pkg_num);
2220 p = GET_PKG(pkg_base, pkg_num);
2221
2222 t->cpu_id = cpu_id;
2223 if (thread_num == 0) {
2224 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
2225 if (cpu_is_first_core_in_package(cpu_id))
2226 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
2227 }
2228
2229 c->core_id = core_num;
2230 p->package_id = pkg_num;
2231}
2232
2233
2234int initialize_counters(int cpu_id)
2235{
2236 int my_thread_id, my_core_id, my_package_id;
2237
2238 my_package_id = get_physical_package_id(cpu_id);
2239 my_core_id = get_core_id(cpu_id);
2240
2241 if (cpu_is_first_sibling_in_core(cpu_id)) {
2242 my_thread_id = 0;
2243 topo.num_cores++;
2244 } else {
2245 my_thread_id = 1;
2246 }
2247
2248 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2249 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2250 return 0;
2251}
2252
2253void allocate_output_buffer()
2254{
Josh Triplettb844db32013-06-12 17:26:37 -07002255 output_buffer = calloc(1, (1 + topo.num_cpus) * 256);
Len Brownc98d5d92012-06-04 00:56:40 -04002256 outp = output_buffer;
Josh Triplettb2c95d92013-08-20 17:20:18 -07002257 if (outp == NULL)
2258 err(-1, "calloc output buffer");
Len Brownc98d5d92012-06-04 00:56:40 -04002259}
2260
2261void setup_all_buffers(void)
2262{
2263 topology_probe();
2264 allocate_counters(&thread_even, &core_even, &package_even);
2265 allocate_counters(&thread_odd, &core_odd, &package_odd);
2266 allocate_output_buffer();
2267 for_all_proc_cpus(initialize_counters);
2268}
Len Brown103a8fe2010-10-22 23:53:03 -04002269void turbostat_init()
2270{
2271 check_cpuid();
2272
2273 check_dev_msr();
2274 check_super_user();
2275
Len Brownc98d5d92012-06-04 00:56:40 -04002276 setup_all_buffers();
Len Brown103a8fe2010-10-22 23:53:03 -04002277
2278 if (verbose)
Len Brownc98d5d92012-06-04 00:56:40 -04002279 print_verbose_header();
Len Brown889facb2012-11-08 00:48:57 -05002280
2281 if (verbose)
2282 for_all_cpus(print_epb, ODD_COUNTERS);
2283
2284 if (verbose)
2285 for_all_cpus(print_rapl, ODD_COUNTERS);
2286
2287 for_all_cpus(set_temperature_target, ODD_COUNTERS);
2288
2289 if (verbose)
2290 for_all_cpus(print_thermal, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04002291}
2292
2293int fork_it(char **argv)
2294{
Len Brown103a8fe2010-10-22 23:53:03 -04002295 pid_t child_pid;
Len Brownd91bb172012-11-01 00:08:19 -04002296 int status;
Len Brownd15cf7c2012-06-03 23:24:00 -04002297
Len Brownd91bb172012-11-01 00:08:19 -04002298 status = for_all_cpus(get_counters, EVEN_COUNTERS);
2299 if (status)
2300 exit(status);
Len Brownc98d5d92012-06-04 00:56:40 -04002301 /* clear affinity side-effect of get_counters() */
2302 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
Len Brown103a8fe2010-10-22 23:53:03 -04002303 gettimeofday(&tv_even, (struct timezone *)NULL);
2304
2305 child_pid = fork();
2306 if (!child_pid) {
2307 /* child */
2308 execvp(argv[0], argv);
2309 } else {
Len Brown103a8fe2010-10-22 23:53:03 -04002310
2311 /* parent */
Josh Triplettb2c95d92013-08-20 17:20:18 -07002312 if (child_pid == -1)
2313 err(1, "fork");
Len Brown103a8fe2010-10-22 23:53:03 -04002314
2315 signal(SIGINT, SIG_IGN);
2316 signal(SIGQUIT, SIG_IGN);
Josh Triplettb2c95d92013-08-20 17:20:18 -07002317 if (waitpid(child_pid, &status, 0) == -1)
2318 err(status, "waitpid");
Len Brown103a8fe2010-10-22 23:53:03 -04002319 }
Len Brownc98d5d92012-06-04 00:56:40 -04002320 /*
2321 * n.b. fork_it() does not check for errors from for_all_cpus()
2322 * because re-starting is problematic when forking
2323 */
2324 for_all_cpus(get_counters, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04002325 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002326 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04002327 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
2328 compute_average(EVEN_COUNTERS);
2329 format_all_counters(EVEN_COUNTERS);
2330 flush_stderr();
Len Brown103a8fe2010-10-22 23:53:03 -04002331
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07002332 fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
Len Brown103a8fe2010-10-22 23:53:03 -04002333
Len Brownd91bb172012-11-01 00:08:19 -04002334 return status;
Len Brown103a8fe2010-10-22 23:53:03 -04002335}
2336
2337void cmdline(int argc, char **argv)
2338{
2339 int opt;
2340
2341 progname = argv[0];
2342
Len Brown889facb2012-11-08 00:48:57 -05002343 while ((opt = getopt(argc, argv, "+pPSvi:sc:sC:m:M:RT:")) != -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04002344 switch (opt) {
Len Brownf9240812012-10-06 15:26:31 -04002345 case 'p':
Len Brownc98d5d92012-06-04 00:56:40 -04002346 show_core_only++;
2347 break;
Len Brownf9240812012-10-06 15:26:31 -04002348 case 'P':
Len Brownc98d5d92012-06-04 00:56:40 -04002349 show_pkg_only++;
2350 break;
Len Brownf9240812012-10-06 15:26:31 -04002351 case 'S':
Len Browne23da032012-02-06 18:37:16 -05002352 summary_only++;
2353 break;
Len Brown103a8fe2010-10-22 23:53:03 -04002354 case 'v':
2355 verbose++;
2356 break;
2357 case 'i':
2358 interval_sec = atoi(optarg);
2359 break;
Len Brownf9240812012-10-06 15:26:31 -04002360 case 'c':
Len Brown8e180f32012-09-22 01:25:08 -04002361 sscanf(optarg, "%x", &extra_delta_offset32);
2362 break;
Len Brownf9240812012-10-06 15:26:31 -04002363 case 'C':
Len Brown8e180f32012-09-22 01:25:08 -04002364 sscanf(optarg, "%x", &extra_delta_offset64);
2365 break;
Len Brown2f32edf2012-09-21 23:45:46 -04002366 case 'm':
2367 sscanf(optarg, "%x", &extra_msr_offset32);
Len Brown2f32edf2012-09-21 23:45:46 -04002368 break;
2369 case 'M':
2370 sscanf(optarg, "%x", &extra_msr_offset64);
Len Brown103a8fe2010-10-22 23:53:03 -04002371 break;
Len Brown889facb2012-11-08 00:48:57 -05002372 case 'R':
2373 rapl_verbose++;
2374 break;
2375 case 'T':
2376 tcc_activation_temp_override = atoi(optarg);
2377 break;
Len Brown103a8fe2010-10-22 23:53:03 -04002378 default:
2379 usage();
2380 }
2381 }
2382}
2383
2384int main(int argc, char **argv)
2385{
2386 cmdline(argc, argv);
2387
Len Brown889facb2012-11-08 00:48:57 -05002388 if (verbose)
Len Browne6f9bb32013-12-03 02:19:19 -05002389 fprintf(stderr, "turbostat v3.6 Dec 2, 2013"
Len Brown103a8fe2010-10-22 23:53:03 -04002390 " - Len Brown <lenb@kernel.org>\n");
Len Brown103a8fe2010-10-22 23:53:03 -04002391
2392 turbostat_init();
2393
2394 /*
2395 * if any params left, it must be a command to fork
2396 */
2397 if (argc - optind)
2398 return fork_it(argv + optind);
2399 else
2400 turbostat_loop();
2401
2402 return 0;
2403}