blob: c8cce5dcc8231a233b43d73a2c3185b44ad6d918 [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
83#define RAPL_PKG (1 << 0)
84#define RAPL_CORES (1 << 1)
85#define RAPL_GFX (1 << 2)
86#define RAPL_DRAM (1 << 3)
87#define RAPL_PKG_PERF_STATUS (1 << 4)
88#define RAPL_DRAM_PERF_STATUS (1 << 5)
Len Brown144b44b2013-11-09 00:30:16 -050089#define RAPL_PKG_POWER_INFO (1 << 6)
90#define RAPL_CORE_POLICY (1 << 7)
Len Brown889facb2012-11-08 00:48:57 -050091#define TJMAX_DEFAULT 100
92
93#define MAX(a, b) ((a) > (b) ? (a) : (b))
Len Brown103a8fe2010-10-22 23:53:03 -040094
95int aperf_mperf_unstable;
96int backwards_count;
97char *progname;
Len Brown103a8fe2010-10-22 23:53:03 -040098
Len Brownc98d5d92012-06-04 00:56:40 -040099cpu_set_t *cpu_present_set, *cpu_affinity_set;
100size_t cpu_present_setsize, cpu_affinity_setsize;
Len Brown103a8fe2010-10-22 23:53:03 -0400101
Len Brownc98d5d92012-06-04 00:56:40 -0400102struct thread_data {
103 unsigned long long tsc;
104 unsigned long long aperf;
105 unsigned long long mperf;
Len Brown144b44b2013-11-09 00:30:16 -0500106 unsigned long long c1;
Len Brown2f32edf2012-09-21 23:45:46 -0400107 unsigned long long extra_msr64;
Len Brown8e180f32012-09-22 01:25:08 -0400108 unsigned long long extra_delta64;
109 unsigned long long extra_msr32;
110 unsigned long long extra_delta32;
Len Brown1ed51012013-02-10 17:19:24 -0500111 unsigned int smi_count;
Len Brownc98d5d92012-06-04 00:56:40 -0400112 unsigned int cpu_id;
113 unsigned int flags;
114#define CPU_IS_FIRST_THREAD_IN_CORE 0x2
115#define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
116} *thread_even, *thread_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400117
Len Brownc98d5d92012-06-04 00:56:40 -0400118struct core_data {
119 unsigned long long c3;
120 unsigned long long c6;
121 unsigned long long c7;
Len Brown889facb2012-11-08 00:48:57 -0500122 unsigned int core_temp_c;
Len Brownc98d5d92012-06-04 00:56:40 -0400123 unsigned int core_id;
124} *core_even, *core_odd;
Len Brown103a8fe2010-10-22 23:53:03 -0400125
Len Brownc98d5d92012-06-04 00:56:40 -0400126struct pkg_data {
127 unsigned long long pc2;
128 unsigned long long pc3;
129 unsigned long long pc6;
130 unsigned long long pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800131 unsigned long long pc8;
132 unsigned long long pc9;
133 unsigned long long pc10;
Len Brownc98d5d92012-06-04 00:56:40 -0400134 unsigned int package_id;
Len Brown889facb2012-11-08 00:48:57 -0500135 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
136 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
137 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
138 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
139 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
140 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
141 unsigned int pkg_temp_c;
142
Len Brownc98d5d92012-06-04 00:56:40 -0400143} *package_even, *package_odd;
144
145#define ODD_COUNTERS thread_odd, core_odd, package_odd
146#define EVEN_COUNTERS thread_even, core_even, package_even
147
148#define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
149 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
150 topo.num_threads_per_core + \
151 (core_no) * topo.num_threads_per_core + (thread_no))
152#define GET_CORE(core_base, core_no, pkg_no) \
153 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
154#define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
155
156struct system_summary {
157 struct thread_data threads;
158 struct core_data cores;
159 struct pkg_data packages;
160} sum, average;
161
162
163struct topo_params {
164 int num_packages;
165 int num_cpus;
166 int num_cores;
167 int max_cpu_num;
168 int num_cores_per_pkg;
169 int num_threads_per_core;
170} topo;
171
172struct timeval tv_even, tv_odd, tv_delta;
173
174void setup_all_buffers(void);
175
176int cpu_is_not_present(int cpu)
Len Brownd15cf7c2012-06-03 23:24:00 -0400177{
Len Brownc98d5d92012-06-04 00:56:40 -0400178 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brownd15cf7c2012-06-03 23:24:00 -0400179}
Len Brown88c32812012-03-29 21:44:40 -0400180/*
Len Brownc98d5d92012-06-04 00:56:40 -0400181 * run func(thread, core, package) in topology order
182 * skip non-present cpus
Len Brown88c32812012-03-29 21:44:40 -0400183 */
Len Brownd15cf7c2012-06-03 23:24:00 -0400184
Len Brownc98d5d92012-06-04 00:56:40 -0400185int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
186 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
Len Brown88c32812012-03-29 21:44:40 -0400187{
Len Brownc98d5d92012-06-04 00:56:40 -0400188 int retval, pkg_no, core_no, thread_no;
189
190 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
191 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
192 for (thread_no = 0; thread_no <
193 topo.num_threads_per_core; ++thread_no) {
194 struct thread_data *t;
195 struct core_data *c;
196 struct pkg_data *p;
197
198 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
199
200 if (cpu_is_not_present(t->cpu_id))
201 continue;
202
203 c = GET_CORE(core_base, core_no, pkg_no);
204 p = GET_PKG(pkg_base, pkg_no);
205
206 retval = func(t, c, p);
207 if (retval)
208 return retval;
209 }
210 }
211 }
212 return 0;
Len Brown88c32812012-03-29 21:44:40 -0400213}
214
215int cpu_migrate(int cpu)
216{
Len Brownc98d5d92012-06-04 00:56:40 -0400217 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
218 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
219 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
Len Brown88c32812012-03-29 21:44:40 -0400220 return -1;
221 else
222 return 0;
223}
224
Len Brown15aaa342012-03-29 22:19:58 -0400225int get_msr(int cpu, off_t offset, unsigned long long *msr)
Len Brown103a8fe2010-10-22 23:53:03 -0400226{
227 ssize_t retval;
Len Brown103a8fe2010-10-22 23:53:03 -0400228 char pathname[32];
229 int fd;
230
231 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
232 fd = open(pathname, O_RDONLY);
Len Brown15aaa342012-03-29 22:19:58 -0400233 if (fd < 0)
234 return -1;
Len Brown103a8fe2010-10-22 23:53:03 -0400235
Len Brown15aaa342012-03-29 22:19:58 -0400236 retval = pread(fd, msr, sizeof *msr, offset);
Len Brown103a8fe2010-10-22 23:53:03 -0400237 close(fd);
Len Brown15aaa342012-03-29 22:19:58 -0400238
Len Brownd91bb172012-11-01 00:08:19 -0400239 if (retval != sizeof *msr) {
Josh Triplett2e9c6bc2013-08-20 17:20:13 -0700240 fprintf(stderr, "%s offset 0x%llx read failed\n", pathname, (unsigned long long)offset);
Len Brown15aaa342012-03-29 22:19:58 -0400241 return -1;
Len Brownd91bb172012-11-01 00:08:19 -0400242 }
Len Brown15aaa342012-03-29 22:19:58 -0400243
244 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400245}
246
Len Browna829eb42011-02-10 23:36:34 -0500247void print_header(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400248{
249 if (show_pkg)
Len Brownc98d5d92012-06-04 00:56:40 -0400250 outp += sprintf(outp, "pk");
Len Browne23da032012-02-06 18:37:16 -0500251 if (show_pkg)
Len Brownc98d5d92012-06-04 00:56:40 -0400252 outp += sprintf(outp, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400253 if (show_core)
Len Brownc98d5d92012-06-04 00:56:40 -0400254 outp += sprintf(outp, "cor");
Len Brown103a8fe2010-10-22 23:53:03 -0400255 if (show_cpu)
Len Brownc98d5d92012-06-04 00:56:40 -0400256 outp += sprintf(outp, " CPU");
Len Browne23da032012-02-06 18:37:16 -0500257 if (show_pkg || show_core || show_cpu)
Len Brownc98d5d92012-06-04 00:56:40 -0400258 outp += sprintf(outp, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400259 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400260 outp += sprintf(outp, " %%c0");
Len Brown103a8fe2010-10-22 23:53:03 -0400261 if (has_aperf)
Len Brownc98d5d92012-06-04 00:56:40 -0400262 outp += sprintf(outp, " GHz");
263 outp += sprintf(outp, " TSC");
Len Brown1ed51012013-02-10 17:19:24 -0500264 if (do_smi)
265 outp += sprintf(outp, " SMI");
Len Brown8e180f32012-09-22 01:25:08 -0400266 if (extra_delta_offset32)
Len Brownf9240812012-10-06 15:26:31 -0400267 outp += sprintf(outp, " count 0x%03X", extra_delta_offset32);
Len Brown8e180f32012-09-22 01:25:08 -0400268 if (extra_delta_offset64)
Len Brownf9240812012-10-06 15:26:31 -0400269 outp += sprintf(outp, " COUNT 0x%03X", extra_delta_offset64);
Len Brown2f32edf2012-09-21 23:45:46 -0400270 if (extra_msr_offset32)
Len Brown8e180f32012-09-22 01:25:08 -0400271 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset32);
Len Brown2f32edf2012-09-21 23:45:46 -0400272 if (extra_msr_offset64)
Len Brown8e180f32012-09-22 01:25:08 -0400273 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset64);
Len Brown103a8fe2010-10-22 23:53:03 -0400274 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400275 outp += sprintf(outp, " %%c1");
Len Brown144b44b2013-11-09 00:30:16 -0500276 if (do_nhm_cstates && !do_slm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400277 outp += sprintf(outp, " %%c3");
Len Brown103a8fe2010-10-22 23:53:03 -0400278 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400279 outp += sprintf(outp, " %%c6");
Len Brown103a8fe2010-10-22 23:53:03 -0400280 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400281 outp += sprintf(outp, " %%c7");
Len Brown889facb2012-11-08 00:48:57 -0500282
283 if (do_dts)
284 outp += sprintf(outp, " CTMP");
285 if (do_ptm)
286 outp += sprintf(outp, " PTMP");
287
Len Brown103a8fe2010-10-22 23:53:03 -0400288 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400289 outp += sprintf(outp, " %%pc2");
Len Brown144b44b2013-11-09 00:30:16 -0500290 if (do_nhm_cstates && !do_slm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400291 outp += sprintf(outp, " %%pc3");
Len Brown144b44b2013-11-09 00:30:16 -0500292 if (do_nhm_cstates && !do_slm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400293 outp += sprintf(outp, " %%pc6");
Len Brown103a8fe2010-10-22 23:53:03 -0400294 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400295 outp += sprintf(outp, " %%pc7");
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800296 if (do_c8_c9_c10) {
297 outp += sprintf(outp, " %%pc8");
298 outp += sprintf(outp, " %%pc9");
299 outp += sprintf(outp, " %%pc10");
300 }
Len Brown103a8fe2010-10-22 23:53:03 -0400301
Len Brown889facb2012-11-08 00:48:57 -0500302 if (do_rapl & RAPL_PKG)
303 outp += sprintf(outp, " Pkg_W");
304 if (do_rapl & RAPL_CORES)
305 outp += sprintf(outp, " Cor_W");
306 if (do_rapl & RAPL_GFX)
307 outp += sprintf(outp, " GFX_W");
308 if (do_rapl & RAPL_DRAM)
309 outp += sprintf(outp, " RAM_W");
310 if (do_rapl & RAPL_PKG_PERF_STATUS)
311 outp += sprintf(outp, " PKG_%%");
312 if (do_rapl & RAPL_DRAM_PERF_STATUS)
313 outp += sprintf(outp, " RAM_%%");
314
Len Brownc98d5d92012-06-04 00:56:40 -0400315 outp += sprintf(outp, "\n");
Len Brown103a8fe2010-10-22 23:53:03 -0400316}
317
Len Brownc98d5d92012-06-04 00:56:40 -0400318int dump_counters(struct thread_data *t, struct core_data *c,
319 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400320{
Len Brownc98d5d92012-06-04 00:56:40 -0400321 fprintf(stderr, "t %p, c %p, p %p\n", t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400322
Len Brownc98d5d92012-06-04 00:56:40 -0400323 if (t) {
324 fprintf(stderr, "CPU: %d flags 0x%x\n", t->cpu_id, t->flags);
325 fprintf(stderr, "TSC: %016llX\n", t->tsc);
326 fprintf(stderr, "aperf: %016llX\n", t->aperf);
327 fprintf(stderr, "mperf: %016llX\n", t->mperf);
328 fprintf(stderr, "c1: %016llX\n", t->c1);
Len Brown8e180f32012-09-22 01:25:08 -0400329 fprintf(stderr, "msr0x%x: %08llX\n",
330 extra_delta_offset32, t->extra_delta32);
331 fprintf(stderr, "msr0x%x: %016llX\n",
332 extra_delta_offset64, t->extra_delta64);
333 fprintf(stderr, "msr0x%x: %08llX\n",
Len Brown2f32edf2012-09-21 23:45:46 -0400334 extra_msr_offset32, t->extra_msr32);
Len Brownc98d5d92012-06-04 00:56:40 -0400335 fprintf(stderr, "msr0x%x: %016llX\n",
Len Brown2f32edf2012-09-21 23:45:46 -0400336 extra_msr_offset64, t->extra_msr64);
Len Brown1ed51012013-02-10 17:19:24 -0500337 if (do_smi)
338 fprintf(stderr, "SMI: %08X\n", t->smi_count);
Len Brownc98d5d92012-06-04 00:56:40 -0400339 }
Len Brown103a8fe2010-10-22 23:53:03 -0400340
Len Brownc98d5d92012-06-04 00:56:40 -0400341 if (c) {
342 fprintf(stderr, "core: %d\n", c->core_id);
343 fprintf(stderr, "c3: %016llX\n", c->c3);
344 fprintf(stderr, "c6: %016llX\n", c->c6);
345 fprintf(stderr, "c7: %016llX\n", c->c7);
Len Brown889facb2012-11-08 00:48:57 -0500346 fprintf(stderr, "DTS: %dC\n", c->core_temp_c);
Len Brownc98d5d92012-06-04 00:56:40 -0400347 }
348
349 if (p) {
350 fprintf(stderr, "package: %d\n", p->package_id);
351 fprintf(stderr, "pc2: %016llX\n", p->pc2);
352 fprintf(stderr, "pc3: %016llX\n", p->pc3);
353 fprintf(stderr, "pc6: %016llX\n", p->pc6);
354 fprintf(stderr, "pc7: %016llX\n", p->pc7);
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800355 fprintf(stderr, "pc8: %016llX\n", p->pc8);
356 fprintf(stderr, "pc9: %016llX\n", p->pc9);
357 fprintf(stderr, "pc10: %016llX\n", p->pc10);
Len Brown889facb2012-11-08 00:48:57 -0500358 fprintf(stderr, "Joules PKG: %0X\n", p->energy_pkg);
359 fprintf(stderr, "Joules COR: %0X\n", p->energy_cores);
360 fprintf(stderr, "Joules GFX: %0X\n", p->energy_gfx);
361 fprintf(stderr, "Joules RAM: %0X\n", p->energy_dram);
362 fprintf(stderr, "Throttle PKG: %0X\n", p->rapl_pkg_perf_status);
363 fprintf(stderr, "Throttle RAM: %0X\n", p->rapl_dram_perf_status);
364 fprintf(stderr, "PTM: %dC\n", p->pkg_temp_c);
Len Brownc98d5d92012-06-04 00:56:40 -0400365 }
366 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400367}
368
Len Browne23da032012-02-06 18:37:16 -0500369/*
370 * column formatting convention & formats
371 * package: "pk" 2 columns %2d
372 * core: "cor" 3 columns %3d
373 * CPU: "CPU" 3 columns %3d
Len Brown889facb2012-11-08 00:48:57 -0500374 * Pkg_W: %6.2
375 * Cor_W: %6.2
376 * GFX_W: %5.2
377 * RAM_W: %5.2
Len Browne23da032012-02-06 18:37:16 -0500378 * GHz: "GHz" 3 columns %3.2
379 * TSC: "TSC" 3 columns %3.2
Len Brown1ed51012013-02-10 17:19:24 -0500380 * SMI: "SMI" 4 columns %4d
Len Browne23da032012-02-06 18:37:16 -0500381 * percentage " %pc3" %6.2
Len Brown889facb2012-11-08 00:48:57 -0500382 * Perf Status percentage: %5.2
383 * "CTMP" 4 columns %4d
Len Browne23da032012-02-06 18:37:16 -0500384 */
Len Brownc98d5d92012-06-04 00:56:40 -0400385int format_counters(struct thread_data *t, struct core_data *c,
386 struct pkg_data *p)
Len Brown103a8fe2010-10-22 23:53:03 -0400387{
388 double interval_float;
Len Brown889facb2012-11-08 00:48:57 -0500389 char *fmt5, *fmt6;
Len Brown103a8fe2010-10-22 23:53:03 -0400390
Len Brownc98d5d92012-06-04 00:56:40 -0400391 /* if showing only 1st thread in core and this isn't one, bail out */
392 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
393 return 0;
394
395 /* if showing only 1st thread in pkg and this isn't one, bail out */
396 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
397 return 0;
398
Len Brown103a8fe2010-10-22 23:53:03 -0400399 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
400
Len Brownc98d5d92012-06-04 00:56:40 -0400401 /* topo columns, print blanks on 1st (average) line */
402 if (t == &average.threads) {
Len Brown103a8fe2010-10-22 23:53:03 -0400403 if (show_pkg)
Len Brownc98d5d92012-06-04 00:56:40 -0400404 outp += sprintf(outp, " ");
Len Browne23da032012-02-06 18:37:16 -0500405 if (show_pkg && show_core)
Len Brownc98d5d92012-06-04 00:56:40 -0400406 outp += sprintf(outp, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400407 if (show_core)
Len Brownc98d5d92012-06-04 00:56:40 -0400408 outp += sprintf(outp, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400409 if (show_cpu)
Len Brownc98d5d92012-06-04 00:56:40 -0400410 outp += sprintf(outp, " " " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400411 } else {
Len Brownc98d5d92012-06-04 00:56:40 -0400412 if (show_pkg) {
413 if (p)
414 outp += sprintf(outp, "%2d", p->package_id);
415 else
416 outp += sprintf(outp, " ");
417 }
Len Browne23da032012-02-06 18:37:16 -0500418 if (show_pkg && show_core)
Len Brownc98d5d92012-06-04 00:56:40 -0400419 outp += sprintf(outp, " ");
420 if (show_core) {
421 if (c)
422 outp += sprintf(outp, "%3d", c->core_id);
423 else
424 outp += sprintf(outp, " ");
425 }
Len Brown103a8fe2010-10-22 23:53:03 -0400426 if (show_cpu)
Len Brownc98d5d92012-06-04 00:56:40 -0400427 outp += sprintf(outp, " %3d", t->cpu_id);
Len Brown103a8fe2010-10-22 23:53:03 -0400428 }
Len Brown103a8fe2010-10-22 23:53:03 -0400429 /* %c0 */
430 if (do_nhm_cstates) {
Len Browne23da032012-02-06 18:37:16 -0500431 if (show_pkg || show_core || show_cpu)
Len Brownc98d5d92012-06-04 00:56:40 -0400432 outp += sprintf(outp, " ");
Len Brown103a8fe2010-10-22 23:53:03 -0400433 if (!skip_c0)
Len Brownc98d5d92012-06-04 00:56:40 -0400434 outp += sprintf(outp, "%6.2f", 100.0 * t->mperf/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400435 else
Len Brownc98d5d92012-06-04 00:56:40 -0400436 outp += sprintf(outp, " ****");
Len Brown103a8fe2010-10-22 23:53:03 -0400437 }
438
439 /* GHz */
440 if (has_aperf) {
441 if (!aperf_mperf_unstable) {
Len Brownc98d5d92012-06-04 00:56:40 -0400442 outp += sprintf(outp, " %3.2f",
443 1.0 * t->tsc / units * t->aperf /
444 t->mperf / interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400445 } else {
Len Brownc98d5d92012-06-04 00:56:40 -0400446 if (t->aperf > t->tsc || t->mperf > t->tsc) {
447 outp += sprintf(outp, " ***");
Len Brown103a8fe2010-10-22 23:53:03 -0400448 } else {
Len Brownc98d5d92012-06-04 00:56:40 -0400449 outp += sprintf(outp, "%3.1f*",
450 1.0 * t->tsc /
451 units * t->aperf /
452 t->mperf / interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400453 }
454 }
455 }
456
457 /* TSC */
Len Brownc98d5d92012-06-04 00:56:40 -0400458 outp += sprintf(outp, "%5.2f", 1.0 * t->tsc/units/interval_float);
Len Brown103a8fe2010-10-22 23:53:03 -0400459
Len Brown1ed51012013-02-10 17:19:24 -0500460 /* SMI */
461 if (do_smi)
462 outp += sprintf(outp, "%4d", t->smi_count);
463
Len Brown8e180f32012-09-22 01:25:08 -0400464 /* delta */
465 if (extra_delta_offset32)
466 outp += sprintf(outp, " %11llu", t->extra_delta32);
467
468 /* DELTA */
469 if (extra_delta_offset64)
470 outp += sprintf(outp, " %11llu", t->extra_delta64);
Len Brown2f32edf2012-09-21 23:45:46 -0400471 /* msr */
472 if (extra_msr_offset32)
Len Brown8e180f32012-09-22 01:25:08 -0400473 outp += sprintf(outp, " 0x%08llx", t->extra_msr32);
Len Brown2f32edf2012-09-21 23:45:46 -0400474
Len Brown130ff302012-09-21 22:56:06 -0400475 /* MSR */
Len Brown2f32edf2012-09-21 23:45:46 -0400476 if (extra_msr_offset64)
477 outp += sprintf(outp, " 0x%016llx", t->extra_msr64);
Len Brown130ff302012-09-21 22:56:06 -0400478
Len Brown103a8fe2010-10-22 23:53:03 -0400479 if (do_nhm_cstates) {
480 if (!skip_c1)
Len Brownc98d5d92012-06-04 00:56:40 -0400481 outp += sprintf(outp, " %6.2f", 100.0 * t->c1/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400482 else
Len Brownc98d5d92012-06-04 00:56:40 -0400483 outp += sprintf(outp, " ****");
Len Brown103a8fe2010-10-22 23:53:03 -0400484 }
Len Brownc98d5d92012-06-04 00:56:40 -0400485
486 /* print per-core data only for 1st thread in core */
487 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
488 goto done;
489
Len Brown144b44b2013-11-09 00:30:16 -0500490 if (do_nhm_cstates && !do_slm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400491 outp += sprintf(outp, " %6.2f", 100.0 * c->c3/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400492 if (do_nhm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400493 outp += sprintf(outp, " %6.2f", 100.0 * c->c6/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400494 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400495 outp += sprintf(outp, " %6.2f", 100.0 * c->c7/t->tsc);
496
Len Brown889facb2012-11-08 00:48:57 -0500497 if (do_dts)
498 outp += sprintf(outp, " %4d", c->core_temp_c);
499
Len Brownc98d5d92012-06-04 00:56:40 -0400500 /* print per-package data only for 1st core in package */
501 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
502 goto done;
503
Len Brown889facb2012-11-08 00:48:57 -0500504 if (do_ptm)
505 outp += sprintf(outp, " %4d", p->pkg_temp_c);
506
Len Brown103a8fe2010-10-22 23:53:03 -0400507 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400508 outp += sprintf(outp, " %6.2f", 100.0 * p->pc2/t->tsc);
Len Brown144b44b2013-11-09 00:30:16 -0500509 if (do_nhm_cstates && !do_slm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400510 outp += sprintf(outp, " %6.2f", 100.0 * p->pc3/t->tsc);
Len Brown144b44b2013-11-09 00:30:16 -0500511 if (do_nhm_cstates && !do_slm_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400512 outp += sprintf(outp, " %6.2f", 100.0 * p->pc6/t->tsc);
Len Brown103a8fe2010-10-22 23:53:03 -0400513 if (do_snb_cstates)
Len Brownc98d5d92012-06-04 00:56:40 -0400514 outp += sprintf(outp, " %6.2f", 100.0 * p->pc7/t->tsc);
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800515 if (do_c8_c9_c10) {
516 outp += sprintf(outp, " %6.2f", 100.0 * p->pc8/t->tsc);
517 outp += sprintf(outp, " %6.2f", 100.0 * p->pc9/t->tsc);
518 outp += sprintf(outp, " %6.2f", 100.0 * p->pc10/t->tsc);
519 }
Len Brown889facb2012-11-08 00:48:57 -0500520
521 /*
522 * If measurement interval exceeds minimum RAPL Joule Counter range,
523 * indicate that results are suspect by printing "**" in fraction place.
524 */
525 if (interval_float < rapl_joule_counter_range) {
526 fmt5 = " %5.2f";
527 fmt6 = " %6.2f";
528 } else {
529 fmt5 = " %3.0f**";
530 fmt6 = " %4.0f**";
531 }
532
533 if (do_rapl & RAPL_PKG)
534 outp += sprintf(outp, fmt6, p->energy_pkg * rapl_energy_units / interval_float);
535 if (do_rapl & RAPL_CORES)
536 outp += sprintf(outp, fmt6, p->energy_cores * rapl_energy_units / interval_float);
537 if (do_rapl & RAPL_GFX)
538 outp += sprintf(outp, fmt5, p->energy_gfx * rapl_energy_units / interval_float);
539 if (do_rapl & RAPL_DRAM)
540 outp += sprintf(outp, fmt5, p->energy_dram * rapl_energy_units / interval_float);
541 if (do_rapl & RAPL_PKG_PERF_STATUS )
542 outp += sprintf(outp, fmt5, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
543 if (do_rapl & RAPL_DRAM_PERF_STATUS )
544 outp += sprintf(outp, fmt5, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
545
Len Brownc98d5d92012-06-04 00:56:40 -0400546done:
Len Brownc98d5d92012-06-04 00:56:40 -0400547 outp += sprintf(outp, "\n");
548
549 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -0400550}
551
Len Brownc98d5d92012-06-04 00:56:40 -0400552void flush_stdout()
Len Brown103a8fe2010-10-22 23:53:03 -0400553{
Len Brownc98d5d92012-06-04 00:56:40 -0400554 fputs(output_buffer, stdout);
Len Brownddac0d62012-11-30 01:01:40 -0500555 fflush(stdout);
Len Brownc98d5d92012-06-04 00:56:40 -0400556 outp = output_buffer;
557}
558void flush_stderr()
559{
560 fputs(output_buffer, stderr);
561 outp = output_buffer;
562}
563void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
564{
Len Browne23da032012-02-06 18:37:16 -0500565 static int printed;
Len Brown103a8fe2010-10-22 23:53:03 -0400566
Len Browne23da032012-02-06 18:37:16 -0500567 if (!printed || !summary_only)
568 print_header();
Len Brown103a8fe2010-10-22 23:53:03 -0400569
Len Brownc98d5d92012-06-04 00:56:40 -0400570 if (topo.num_cpus > 1)
571 format_counters(&average.threads, &average.cores,
572 &average.packages);
Len Brown103a8fe2010-10-22 23:53:03 -0400573
Len Browne23da032012-02-06 18:37:16 -0500574 printed = 1;
575
576 if (summary_only)
577 return;
578
Len Brownc98d5d92012-06-04 00:56:40 -0400579 for_all_cpus(format_counters, t, c, p);
Len Brown103a8fe2010-10-22 23:53:03 -0400580}
581
Len Brown889facb2012-11-08 00:48:57 -0500582#define DELTA_WRAP32(new, old) \
583 if (new > old) { \
584 old = new - old; \
585 } else { \
586 old = 0x100000000 + new - old; \
587 }
588
Len Brownc98d5d92012-06-04 00:56:40 -0400589void
590delta_package(struct pkg_data *new, struct pkg_data *old)
Len Brown103a8fe2010-10-22 23:53:03 -0400591{
Len Brownc98d5d92012-06-04 00:56:40 -0400592 old->pc2 = new->pc2 - old->pc2;
593 old->pc3 = new->pc3 - old->pc3;
594 old->pc6 = new->pc6 - old->pc6;
595 old->pc7 = new->pc7 - old->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800596 old->pc8 = new->pc8 - old->pc8;
597 old->pc9 = new->pc9 - old->pc9;
598 old->pc10 = new->pc10 - old->pc10;
Len Brown889facb2012-11-08 00:48:57 -0500599 old->pkg_temp_c = new->pkg_temp_c;
600
601 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
602 DELTA_WRAP32(new->energy_cores, old->energy_cores);
603 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
604 DELTA_WRAP32(new->energy_dram, old->energy_dram);
605 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
606 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
Len Brownc98d5d92012-06-04 00:56:40 -0400607}
Len Brown103a8fe2010-10-22 23:53:03 -0400608
Len Brownc98d5d92012-06-04 00:56:40 -0400609void
610delta_core(struct core_data *new, struct core_data *old)
611{
612 old->c3 = new->c3 - old->c3;
613 old->c6 = new->c6 - old->c6;
614 old->c7 = new->c7 - old->c7;
Len Brown889facb2012-11-08 00:48:57 -0500615 old->core_temp_c = new->core_temp_c;
Len Brownc98d5d92012-06-04 00:56:40 -0400616}
Len Brown103a8fe2010-10-22 23:53:03 -0400617
Len Brownc3ae3312012-06-13 21:31:46 -0400618/*
619 * old = new - old
620 */
Len Brownc98d5d92012-06-04 00:56:40 -0400621void
622delta_thread(struct thread_data *new, struct thread_data *old,
623 struct core_data *core_delta)
624{
625 old->tsc = new->tsc - old->tsc;
Len Brown103a8fe2010-10-22 23:53:03 -0400626
Len Brownc98d5d92012-06-04 00:56:40 -0400627 /* check for TSC < 1 Mcycles over interval */
Josh Triplettb2c95d92013-08-20 17:20:18 -0700628 if (old->tsc < (1000 * 1000))
629 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
630 "You can disable all c-states by booting with \"idle=poll\"\n"
631 "or just the deep ones with \"processor.max_cstate=1\"");
Len Brown103a8fe2010-10-22 23:53:03 -0400632
Len Brownc98d5d92012-06-04 00:56:40 -0400633 old->c1 = new->c1 - old->c1;
Len Brown103a8fe2010-10-22 23:53:03 -0400634
Len Brownc98d5d92012-06-04 00:56:40 -0400635 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
636 old->aperf = new->aperf - old->aperf;
637 old->mperf = new->mperf - old->mperf;
638 } else {
Len Brown103a8fe2010-10-22 23:53:03 -0400639
Len Brownc98d5d92012-06-04 00:56:40 -0400640 if (!aperf_mperf_unstable) {
641 fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname);
642 fprintf(stderr, "* Frequency results do not cover entire interval *\n");
643 fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n");
644
645 aperf_mperf_unstable = 1;
646 }
Len Brown103a8fe2010-10-22 23:53:03 -0400647 /*
Len Brownc98d5d92012-06-04 00:56:40 -0400648 * mperf delta is likely a huge "positive" number
649 * can not use it for calculating c0 time
Len Brown103a8fe2010-10-22 23:53:03 -0400650 */
Len Brownc98d5d92012-06-04 00:56:40 -0400651 skip_c0 = 1;
652 skip_c1 = 1;
653 }
Len Brown103a8fe2010-10-22 23:53:03 -0400654
Len Brown103a8fe2010-10-22 23:53:03 -0400655
Len Brown144b44b2013-11-09 00:30:16 -0500656 if (use_c1_residency_msr) {
657 /*
658 * Some models have a dedicated C1 residency MSR,
659 * which should be more accurate than the derivation below.
660 */
661 } else {
662 /*
663 * As counter collection is not atomic,
664 * it is possible for mperf's non-halted cycles + idle states
665 * to exceed TSC's all cycles: show c1 = 0% in that case.
666 */
667 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
668 old->c1 = 0;
669 else {
670 /* normal case, derive c1 */
671 old->c1 = old->tsc - old->mperf - core_delta->c3
Len Brownc98d5d92012-06-04 00:56:40 -0400672 - core_delta->c6 - core_delta->c7;
Len Brown144b44b2013-11-09 00:30:16 -0500673 }
Len Brownc98d5d92012-06-04 00:56:40 -0400674 }
Len Brownc3ae3312012-06-13 21:31:46 -0400675
Len Brownc98d5d92012-06-04 00:56:40 -0400676 if (old->mperf == 0) {
Len Brownc3ae3312012-06-13 21:31:46 -0400677 if (verbose > 1) fprintf(stderr, "cpu%d MPERF 0!\n", old->cpu_id);
Len Brownc98d5d92012-06-04 00:56:40 -0400678 old->mperf = 1; /* divide by 0 protection */
679 }
680
Len Brown8e180f32012-09-22 01:25:08 -0400681 old->extra_delta32 = new->extra_delta32 - old->extra_delta32;
682 old->extra_delta32 &= 0xFFFFFFFF;
683
684 old->extra_delta64 = new->extra_delta64 - old->extra_delta64;
685
Len Brownc98d5d92012-06-04 00:56:40 -0400686 /*
Len Brown8e180f32012-09-22 01:25:08 -0400687 * Extra MSR is just a snapshot, simply copy latest w/o subtracting
Len Brownc98d5d92012-06-04 00:56:40 -0400688 */
Len Brown2f32edf2012-09-21 23:45:46 -0400689 old->extra_msr32 = new->extra_msr32;
690 old->extra_msr64 = new->extra_msr64;
Len Brown1ed51012013-02-10 17:19:24 -0500691
692 if (do_smi)
693 old->smi_count = new->smi_count - old->smi_count;
Len Brownc98d5d92012-06-04 00:56:40 -0400694}
695
696int delta_cpu(struct thread_data *t, struct core_data *c,
697 struct pkg_data *p, struct thread_data *t2,
698 struct core_data *c2, struct pkg_data *p2)
699{
700 /* calculate core delta only for 1st thread in core */
701 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
702 delta_core(c, c2);
703
704 /* always calculate thread delta */
705 delta_thread(t, t2, c2); /* c2 is core delta */
706
707 /* calculate package delta only for 1st core in package */
708 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
709 delta_package(p, p2);
710
711 return 0;
712}
713
714void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
715{
716 t->tsc = 0;
717 t->aperf = 0;
718 t->mperf = 0;
719 t->c1 = 0;
720
Len Brown1ed51012013-02-10 17:19:24 -0500721 t->smi_count = 0;
Len Brown8e180f32012-09-22 01:25:08 -0400722 t->extra_delta32 = 0;
723 t->extra_delta64 = 0;
724
Len Brownc98d5d92012-06-04 00:56:40 -0400725 /* tells format_counters to dump all fields from this set */
726 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
727
728 c->c3 = 0;
729 c->c6 = 0;
730 c->c7 = 0;
Len Brown889facb2012-11-08 00:48:57 -0500731 c->core_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -0400732
733 p->pc2 = 0;
734 p->pc3 = 0;
735 p->pc6 = 0;
736 p->pc7 = 0;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800737 p->pc8 = 0;
738 p->pc9 = 0;
739 p->pc10 = 0;
Len Brown889facb2012-11-08 00:48:57 -0500740
741 p->energy_pkg = 0;
742 p->energy_dram = 0;
743 p->energy_cores = 0;
744 p->energy_gfx = 0;
745 p->rapl_pkg_perf_status = 0;
746 p->rapl_dram_perf_status = 0;
747 p->pkg_temp_c = 0;
Len Brownc98d5d92012-06-04 00:56:40 -0400748}
749int sum_counters(struct thread_data *t, struct core_data *c,
750 struct pkg_data *p)
751{
752 average.threads.tsc += t->tsc;
753 average.threads.aperf += t->aperf;
754 average.threads.mperf += t->mperf;
755 average.threads.c1 += t->c1;
756
Len Brown8e180f32012-09-22 01:25:08 -0400757 average.threads.extra_delta32 += t->extra_delta32;
758 average.threads.extra_delta64 += t->extra_delta64;
759
Len Brownc98d5d92012-06-04 00:56:40 -0400760 /* sum per-core values only for 1st thread in core */
761 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
762 return 0;
763
764 average.cores.c3 += c->c3;
765 average.cores.c6 += c->c6;
766 average.cores.c7 += c->c7;
767
Len Brown889facb2012-11-08 00:48:57 -0500768 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
769
Len Brownc98d5d92012-06-04 00:56:40 -0400770 /* sum per-pkg values only for 1st core in pkg */
771 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
772 return 0;
773
774 average.packages.pc2 += p->pc2;
775 average.packages.pc3 += p->pc3;
776 average.packages.pc6 += p->pc6;
777 average.packages.pc7 += p->pc7;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800778 average.packages.pc8 += p->pc8;
779 average.packages.pc9 += p->pc9;
780 average.packages.pc10 += p->pc10;
Len Brownc98d5d92012-06-04 00:56:40 -0400781
Len Brown889facb2012-11-08 00:48:57 -0500782 average.packages.energy_pkg += p->energy_pkg;
783 average.packages.energy_dram += p->energy_dram;
784 average.packages.energy_cores += p->energy_cores;
785 average.packages.energy_gfx += p->energy_gfx;
786
787 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
788
789 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
790 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
Len Brownc98d5d92012-06-04 00:56:40 -0400791 return 0;
792}
793/*
794 * sum the counters for all cpus in the system
795 * compute the weighted average
796 */
797void compute_average(struct thread_data *t, struct core_data *c,
798 struct pkg_data *p)
799{
800 clear_counters(&average.threads, &average.cores, &average.packages);
801
802 for_all_cpus(sum_counters, t, c, p);
803
804 average.threads.tsc /= topo.num_cpus;
805 average.threads.aperf /= topo.num_cpus;
806 average.threads.mperf /= topo.num_cpus;
807 average.threads.c1 /= topo.num_cpus;
808
Len Brown8e180f32012-09-22 01:25:08 -0400809 average.threads.extra_delta32 /= topo.num_cpus;
810 average.threads.extra_delta32 &= 0xFFFFFFFF;
811
812 average.threads.extra_delta64 /= topo.num_cpus;
813
Len Brownc98d5d92012-06-04 00:56:40 -0400814 average.cores.c3 /= topo.num_cores;
815 average.cores.c6 /= topo.num_cores;
816 average.cores.c7 /= topo.num_cores;
817
818 average.packages.pc2 /= topo.num_packages;
819 average.packages.pc3 /= topo.num_packages;
820 average.packages.pc6 /= topo.num_packages;
821 average.packages.pc7 /= topo.num_packages;
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800822
823 average.packages.pc8 /= topo.num_packages;
824 average.packages.pc9 /= topo.num_packages;
825 average.packages.pc10 /= topo.num_packages;
Len Brownc98d5d92012-06-04 00:56:40 -0400826}
827
828static unsigned long long rdtsc(void)
829{
830 unsigned int low, high;
831
832 asm volatile("rdtsc" : "=a" (low), "=d" (high));
833
834 return low | ((unsigned long long)high) << 32;
835}
836
837
838/*
839 * get_counters(...)
840 * migrate to cpu
841 * acquire and record local counters for that cpu
842 */
843int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
844{
845 int cpu = t->cpu_id;
Len Brown889facb2012-11-08 00:48:57 -0500846 unsigned long long msr;
Len Brownc98d5d92012-06-04 00:56:40 -0400847
Len Browne52966c2012-11-08 22:38:05 -0500848 if (cpu_migrate(cpu)) {
849 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
Len Brownc98d5d92012-06-04 00:56:40 -0400850 return -1;
Len Browne52966c2012-11-08 22:38:05 -0500851 }
Len Brownc98d5d92012-06-04 00:56:40 -0400852
853 t->tsc = rdtsc(); /* we are running on local CPU of interest */
854
855 if (has_aperf) {
Len Brown9c63a652012-10-31 01:29:52 -0400856 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
Len Brownc98d5d92012-06-04 00:56:40 -0400857 return -3;
Len Brown9c63a652012-10-31 01:29:52 -0400858 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
Len Brownc98d5d92012-06-04 00:56:40 -0400859 return -4;
860 }
861
Len Brown1ed51012013-02-10 17:19:24 -0500862 if (do_smi) {
863 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
864 return -5;
865 t->smi_count = msr & 0xFFFFFFFF;
866 }
Len Brown8e180f32012-09-22 01:25:08 -0400867 if (extra_delta_offset32) {
Len Brown889facb2012-11-08 00:48:57 -0500868 if (get_msr(cpu, extra_delta_offset32, &msr))
Len Brown2f32edf2012-09-21 23:45:46 -0400869 return -5;
Len Brown889facb2012-11-08 00:48:57 -0500870 t->extra_delta32 = msr & 0xFFFFFFFF;
Len Brown8e180f32012-09-22 01:25:08 -0400871 }
872
873 if (extra_delta_offset64)
874 if (get_msr(cpu, extra_delta_offset64, &t->extra_delta64))
875 return -5;
876
877 if (extra_msr_offset32) {
Len Brown889facb2012-11-08 00:48:57 -0500878 if (get_msr(cpu, extra_msr_offset32, &msr))
Len Brown8e180f32012-09-22 01:25:08 -0400879 return -5;
Len Brown889facb2012-11-08 00:48:57 -0500880 t->extra_msr32 = msr & 0xFFFFFFFF;
Len Brown8e180f32012-09-22 01:25:08 -0400881 }
Len Brown2f32edf2012-09-21 23:45:46 -0400882
883 if (extra_msr_offset64)
884 if (get_msr(cpu, extra_msr_offset64, &t->extra_msr64))
Len Brownc98d5d92012-06-04 00:56:40 -0400885 return -5;
886
Len Brown144b44b2013-11-09 00:30:16 -0500887 if (use_c1_residency_msr) {
888 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
889 return -6;
890 }
891
Len Brownc98d5d92012-06-04 00:56:40 -0400892 /* collect core counters only for 1st thread in core */
893 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
894 return 0;
895
Len Brown144b44b2013-11-09 00:30:16 -0500896 if (do_nhm_cstates && !do_slm_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -0400897 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
898 return -6;
Len Brown144b44b2013-11-09 00:30:16 -0500899 }
900
901 if (do_nhm_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -0400902 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
903 return -7;
904 }
905
906 if (do_snb_cstates)
907 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
908 return -8;
909
Len Brown889facb2012-11-08 00:48:57 -0500910 if (do_dts) {
911 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
912 return -9;
913 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
914 }
915
916
Len Brownc98d5d92012-06-04 00:56:40 -0400917 /* collect package counters only for 1st core in package */
918 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
919 return 0;
920
Len Brown144b44b2013-11-09 00:30:16 -0500921 if (do_nhm_cstates && !do_slm_cstates) {
Len Brownc98d5d92012-06-04 00:56:40 -0400922 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
923 return -9;
924 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
925 return -10;
926 }
927 if (do_snb_cstates) {
928 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
929 return -11;
930 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
931 return -12;
Len Brown103a8fe2010-10-22 23:53:03 -0400932 }
Kristen Carlson Accardica587102012-11-21 05:22:43 -0800933 if (do_c8_c9_c10) {
934 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
935 return -13;
936 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
937 return -13;
938 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
939 return -13;
940 }
Len Brown889facb2012-11-08 00:48:57 -0500941 if (do_rapl & RAPL_PKG) {
942 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
943 return -13;
944 p->energy_pkg = msr & 0xFFFFFFFF;
945 }
946 if (do_rapl & RAPL_CORES) {
947 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
948 return -14;
949 p->energy_cores = msr & 0xFFFFFFFF;
950 }
951 if (do_rapl & RAPL_DRAM) {
952 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
953 return -15;
954 p->energy_dram = msr & 0xFFFFFFFF;
955 }
956 if (do_rapl & RAPL_GFX) {
957 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
958 return -16;
959 p->energy_gfx = msr & 0xFFFFFFFF;
960 }
961 if (do_rapl & RAPL_PKG_PERF_STATUS) {
962 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
963 return -16;
964 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
965 }
966 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
967 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
968 return -16;
969 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
970 }
971 if (do_ptm) {
972 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
973 return -17;
974 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
975 }
Len Brown103a8fe2010-10-22 23:53:03 -0400976 return 0;
977}
978
Len Brownc98d5d92012-06-04 00:56:40 -0400979void print_verbose_header(void)
Len Brown103a8fe2010-10-22 23:53:03 -0400980{
981 unsigned long long msr;
982 unsigned int ratio;
983
984 if (!do_nehalem_platform_info)
985 return;
986
Len Brown9c63a652012-10-31 01:29:52 -0400987 get_msr(0, MSR_NHM_PLATFORM_INFO, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -0400988
Len Brown67920412013-01-31 15:22:15 -0500989 fprintf(stderr, "cpu0: MSR_NHM_PLATFORM_INFO: 0x%08llx\n", msr);
Len Brown6574a5d2012-09-21 00:01:31 -0400990
Len Brown103a8fe2010-10-22 23:53:03 -0400991 ratio = (msr >> 40) & 0xFF;
992 fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency\n",
993 ratio, bclk, ratio * bclk);
994
995 ratio = (msr >> 8) & 0xFF;
996 fprintf(stderr, "%d * %.0f = %.0f MHz TSC frequency\n",
997 ratio, bclk, ratio * bclk);
998
Len Brown67920412013-01-31 15:22:15 -0500999 get_msr(0, MSR_IA32_POWER_CTL, &msr);
Len Brown144b44b2013-11-09 00:30:16 -05001000 fprintf(stderr, "cpu0: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
Len Brown67920412013-01-31 15:22:15 -05001001 msr, msr & 0x2 ? "EN" : "DIS");
1002
Len Brown6574a5d2012-09-21 00:01:31 -04001003 if (!do_ivt_turbo_ratio_limit)
1004 goto print_nhm_turbo_ratio_limits;
1005
1006 get_msr(0, MSR_IVT_TURBO_RATIO_LIMIT, &msr);
1007
Len Brown67920412013-01-31 15:22:15 -05001008 fprintf(stderr, "cpu0: MSR_IVT_TURBO_RATIO_LIMIT: 0x%08llx\n", msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001009
1010 ratio = (msr >> 56) & 0xFF;
1011 if (ratio)
1012 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 16 active cores\n",
1013 ratio, bclk, ratio * bclk);
1014
1015 ratio = (msr >> 48) & 0xFF;
1016 if (ratio)
1017 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 15 active cores\n",
1018 ratio, bclk, ratio * bclk);
1019
1020 ratio = (msr >> 40) & 0xFF;
1021 if (ratio)
1022 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 14 active cores\n",
1023 ratio, bclk, ratio * bclk);
1024
1025 ratio = (msr >> 32) & 0xFF;
1026 if (ratio)
1027 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 13 active cores\n",
1028 ratio, bclk, ratio * bclk);
1029
1030 ratio = (msr >> 24) & 0xFF;
1031 if (ratio)
1032 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 12 active cores\n",
1033 ratio, bclk, ratio * bclk);
1034
1035 ratio = (msr >> 16) & 0xFF;
1036 if (ratio)
1037 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 11 active cores\n",
1038 ratio, bclk, ratio * bclk);
1039
1040 ratio = (msr >> 8) & 0xFF;
1041 if (ratio)
1042 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 10 active cores\n",
1043 ratio, bclk, ratio * bclk);
1044
1045 ratio = (msr >> 0) & 0xFF;
1046 if (ratio)
1047 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 9 active cores\n",
1048 ratio, bclk, ratio * bclk);
1049
1050print_nhm_turbo_ratio_limits:
Len Brown889facb2012-11-08 00:48:57 -05001051 get_msr(0, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
1052
1053#define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
1054#define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
1055
1056 fprintf(stderr, "cpu0: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", msr);
1057
1058 fprintf(stderr, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: ",
1059 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
1060 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
1061 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
1062 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
1063 (msr & (1 << 15)) ? "" : "UN",
1064 (unsigned int)msr & 7);
1065
1066
1067 switch(msr & 0x7) {
1068 case 0:
Len Brown144b44b2013-11-09 00:30:16 -05001069 fprintf(stderr, do_slm_cstates ? "no pkg states" : "pc0");
Len Brown889facb2012-11-08 00:48:57 -05001070 break;
1071 case 1:
Len Brown144b44b2013-11-09 00:30:16 -05001072 fprintf(stderr, do_slm_cstates ? "no pkg states" : do_snb_cstates ? "pc2" : "pc0");
Len Brown889facb2012-11-08 00:48:57 -05001073 break;
1074 case 2:
Len Brown144b44b2013-11-09 00:30:16 -05001075 fprintf(stderr, do_slm_cstates ? "invalid" : do_snb_cstates ? "pc6-noret" : "pc3");
Len Brown889facb2012-11-08 00:48:57 -05001076 break;
1077 case 3:
Len Brown144b44b2013-11-09 00:30:16 -05001078 fprintf(stderr, do_slm_cstates ? "invalid" : "pc6");
Len Brown889facb2012-11-08 00:48:57 -05001079 break;
1080 case 4:
Len Brown144b44b2013-11-09 00:30:16 -05001081 fprintf(stderr, do_slm_cstates ? "pc4" : "pc7");
Len Brown889facb2012-11-08 00:48:57 -05001082 break;
1083 case 5:
Len Brown144b44b2013-11-09 00:30:16 -05001084 fprintf(stderr, do_slm_cstates ? "invalid" : do_snb_cstates ? "pc7s" : "invalid");
1085 break;
1086 case 6:
1087 fprintf(stderr, do_slm_cstates ? "pc6" : "invalid");
Len Brown889facb2012-11-08 00:48:57 -05001088 break;
1089 case 7:
Len Brown144b44b2013-11-09 00:30:16 -05001090 fprintf(stderr, do_slm_cstates ? "pc7" : "unlimited");
Len Brown889facb2012-11-08 00:48:57 -05001091 break;
1092 default:
1093 fprintf(stderr, "invalid");
1094 }
1095 fprintf(stderr, ")\n");
Len Brown103a8fe2010-10-22 23:53:03 -04001096
1097 if (!do_nehalem_turbo_ratio_limit)
1098 return;
1099
Len Brown9c63a652012-10-31 01:29:52 -04001100 get_msr(0, MSR_NHM_TURBO_RATIO_LIMIT, &msr);
Len Brown103a8fe2010-10-22 23:53:03 -04001101
Len Brown67920412013-01-31 15:22:15 -05001102 fprintf(stderr, "cpu0: MSR_NHM_TURBO_RATIO_LIMIT: 0x%08llx\n", msr);
Len Brown6574a5d2012-09-21 00:01:31 -04001103
1104 ratio = (msr >> 56) & 0xFF;
1105 if (ratio)
1106 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 8 active cores\n",
1107 ratio, bclk, ratio * bclk);
1108
1109 ratio = (msr >> 48) & 0xFF;
1110 if (ratio)
1111 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 7 active cores\n",
1112 ratio, bclk, ratio * bclk);
1113
1114 ratio = (msr >> 40) & 0xFF;
1115 if (ratio)
1116 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 6 active cores\n",
1117 ratio, bclk, ratio * bclk);
1118
1119 ratio = (msr >> 32) & 0xFF;
1120 if (ratio)
1121 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 5 active cores\n",
1122 ratio, bclk, ratio * bclk);
1123
Len Brown103a8fe2010-10-22 23:53:03 -04001124 ratio = (msr >> 24) & 0xFF;
1125 if (ratio)
1126 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
1127 ratio, bclk, ratio * bclk);
1128
1129 ratio = (msr >> 16) & 0xFF;
1130 if (ratio)
1131 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
1132 ratio, bclk, ratio * bclk);
1133
1134 ratio = (msr >> 8) & 0xFF;
1135 if (ratio)
1136 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
1137 ratio, bclk, ratio * bclk);
1138
1139 ratio = (msr >> 0) & 0xFF;
1140 if (ratio)
1141 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
1142 ratio, bclk, ratio * bclk);
Len Brown103a8fe2010-10-22 23:53:03 -04001143}
1144
Len Brownc98d5d92012-06-04 00:56:40 -04001145void free_all_buffers(void)
Len Brown103a8fe2010-10-22 23:53:03 -04001146{
Len Brownc98d5d92012-06-04 00:56:40 -04001147 CPU_FREE(cpu_present_set);
1148 cpu_present_set = NULL;
1149 cpu_present_set = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001150
Len Brownc98d5d92012-06-04 00:56:40 -04001151 CPU_FREE(cpu_affinity_set);
1152 cpu_affinity_set = NULL;
1153 cpu_affinity_setsize = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001154
Len Brownc98d5d92012-06-04 00:56:40 -04001155 free(thread_even);
1156 free(core_even);
1157 free(package_even);
1158
1159 thread_even = NULL;
1160 core_even = NULL;
1161 package_even = NULL;
1162
1163 free(thread_odd);
1164 free(core_odd);
1165 free(package_odd);
1166
1167 thread_odd = NULL;
1168 core_odd = NULL;
1169 package_odd = NULL;
1170
1171 free(output_buffer);
1172 output_buffer = NULL;
1173 outp = NULL;
Len Brown103a8fe2010-10-22 23:53:03 -04001174}
1175
Len Brownc98d5d92012-06-04 00:56:40 -04001176/*
Josh Triplett57a42a32013-08-20 17:20:17 -07001177 * Open a file, and exit on failure
1178 */
1179FILE *fopen_or_die(const char *path, const char *mode)
1180{
1181 FILE *filep = fopen(path, "r");
Josh Triplettb2c95d92013-08-20 17:20:18 -07001182 if (!filep)
1183 err(1, "%s: open failed", path);
Josh Triplett57a42a32013-08-20 17:20:17 -07001184 return filep;
1185}
1186
1187/*
Josh Triplett95aebc42013-08-20 17:20:16 -07001188 * Parse a file containing a single int.
1189 */
1190int parse_int_file(const char *fmt, ...)
1191{
1192 va_list args;
1193 char path[PATH_MAX];
1194 FILE *filep;
1195 int value;
1196
1197 va_start(args, fmt);
1198 vsnprintf(path, sizeof(path), fmt, args);
1199 va_end(args);
Josh Triplett57a42a32013-08-20 17:20:17 -07001200 filep = fopen_or_die(path, "r");
Josh Triplettb2c95d92013-08-20 17:20:18 -07001201 if (fscanf(filep, "%d", &value) != 1)
1202 err(1, "%s: failed to parse number from file", path);
Josh Triplett95aebc42013-08-20 17:20:16 -07001203 fclose(filep);
1204 return value;
1205}
1206
1207/*
Len Brownc98d5d92012-06-04 00:56:40 -04001208 * cpu_is_first_sibling_in_core(cpu)
1209 * return 1 if given CPU is 1st HT sibling in the core
1210 */
1211int cpu_is_first_sibling_in_core(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001212{
Josh Triplett95aebc42013-08-20 17:20:16 -07001213 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001214}
1215
Len Brownc98d5d92012-06-04 00:56:40 -04001216/*
1217 * cpu_is_first_core_in_package(cpu)
1218 * return 1 if given CPU is 1st core in package
1219 */
1220int cpu_is_first_core_in_package(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001221{
Josh Triplett95aebc42013-08-20 17:20:16 -07001222 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001223}
1224
1225int get_physical_package_id(int cpu)
1226{
Josh Triplett95aebc42013-08-20 17:20:16 -07001227 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001228}
1229
1230int get_core_id(int cpu)
1231{
Josh Triplett95aebc42013-08-20 17:20:16 -07001232 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
Len Brown103a8fe2010-10-22 23:53:03 -04001233}
1234
Len Brownc98d5d92012-06-04 00:56:40 -04001235int get_num_ht_siblings(int cpu)
1236{
1237 char path[80];
1238 FILE *filep;
1239 int sib1, sib2;
1240 int matches;
1241 char character;
1242
1243 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
Josh Triplett57a42a32013-08-20 17:20:17 -07001244 filep = fopen_or_die(path, "r");
Len Brownc98d5d92012-06-04 00:56:40 -04001245 /*
1246 * file format:
1247 * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4)
1248 * otherwinse 1 sibling (self).
1249 */
1250 matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2);
1251
1252 fclose(filep);
1253
1254 if (matches == 3)
1255 return 2;
1256 else
1257 return 1;
1258}
1259
Len Brown103a8fe2010-10-22 23:53:03 -04001260/*
Len Brownc98d5d92012-06-04 00:56:40 -04001261 * run func(thread, core, package) in topology order
1262 * skip non-present cpus
Len Brown103a8fe2010-10-22 23:53:03 -04001263 */
1264
Len Brownc98d5d92012-06-04 00:56:40 -04001265int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
1266 struct pkg_data *, struct thread_data *, struct core_data *,
1267 struct pkg_data *), struct thread_data *thread_base,
1268 struct core_data *core_base, struct pkg_data *pkg_base,
1269 struct thread_data *thread_base2, struct core_data *core_base2,
1270 struct pkg_data *pkg_base2)
1271{
1272 int retval, pkg_no, core_no, thread_no;
1273
1274 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
1275 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
1276 for (thread_no = 0; thread_no <
1277 topo.num_threads_per_core; ++thread_no) {
1278 struct thread_data *t, *t2;
1279 struct core_data *c, *c2;
1280 struct pkg_data *p, *p2;
1281
1282 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
1283
1284 if (cpu_is_not_present(t->cpu_id))
1285 continue;
1286
1287 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
1288
1289 c = GET_CORE(core_base, core_no, pkg_no);
1290 c2 = GET_CORE(core_base2, core_no, pkg_no);
1291
1292 p = GET_PKG(pkg_base, pkg_no);
1293 p2 = GET_PKG(pkg_base2, pkg_no);
1294
1295 retval = func(t, c, p, t2, c2, p2);
1296 if (retval)
1297 return retval;
1298 }
1299 }
1300 }
1301 return 0;
1302}
1303
1304/*
1305 * run func(cpu) on every cpu in /proc/stat
1306 * return max_cpu number
1307 */
1308int for_all_proc_cpus(int (func)(int))
Len Brown103a8fe2010-10-22 23:53:03 -04001309{
1310 FILE *fp;
Len Brownc98d5d92012-06-04 00:56:40 -04001311 int cpu_num;
Len Brown103a8fe2010-10-22 23:53:03 -04001312 int retval;
1313
Josh Triplett57a42a32013-08-20 17:20:17 -07001314 fp = fopen_or_die(proc_stat, "r");
Len Brown103a8fe2010-10-22 23:53:03 -04001315
1316 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
Josh Triplettb2c95d92013-08-20 17:20:18 -07001317 if (retval != 0)
1318 err(1, "%s: failed to parse format", proc_stat);
Len Brown103a8fe2010-10-22 23:53:03 -04001319
Len Brownc98d5d92012-06-04 00:56:40 -04001320 while (1) {
1321 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 -04001322 if (retval != 1)
1323 break;
1324
Len Brownc98d5d92012-06-04 00:56:40 -04001325 retval = func(cpu_num);
1326 if (retval) {
1327 fclose(fp);
1328 return(retval);
1329 }
Len Brown103a8fe2010-10-22 23:53:03 -04001330 }
1331 fclose(fp);
Len Brownc98d5d92012-06-04 00:56:40 -04001332 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001333}
1334
1335void re_initialize(void)
1336{
Len Brownc98d5d92012-06-04 00:56:40 -04001337 free_all_buffers();
1338 setup_all_buffers();
1339 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
Len Brown103a8fe2010-10-22 23:53:03 -04001340}
1341
Len Brownc98d5d92012-06-04 00:56:40 -04001342
Len Brown103a8fe2010-10-22 23:53:03 -04001343/*
Len Brownc98d5d92012-06-04 00:56:40 -04001344 * count_cpus()
1345 * remember the last one seen, it will be the max
Len Brown103a8fe2010-10-22 23:53:03 -04001346 */
Len Brownc98d5d92012-06-04 00:56:40 -04001347int count_cpus(int cpu)
Len Brown103a8fe2010-10-22 23:53:03 -04001348{
Len Brownc98d5d92012-06-04 00:56:40 -04001349 if (topo.max_cpu_num < cpu)
1350 topo.max_cpu_num = cpu;
Len Brown103a8fe2010-10-22 23:53:03 -04001351
Len Brownc98d5d92012-06-04 00:56:40 -04001352 topo.num_cpus += 1;
1353 return 0;
1354}
1355int mark_cpu_present(int cpu)
1356{
1357 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
Len Brown15aaa342012-03-29 22:19:58 -04001358 return 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001359}
1360
1361void turbostat_loop()
1362{
Len Brownc98d5d92012-06-04 00:56:40 -04001363 int retval;
Len Browne52966c2012-11-08 22:38:05 -05001364 int restarted = 0;
Len Brownc98d5d92012-06-04 00:56:40 -04001365
Len Brown103a8fe2010-10-22 23:53:03 -04001366restart:
Len Browne52966c2012-11-08 22:38:05 -05001367 restarted++;
1368
Len Brownc98d5d92012-06-04 00:56:40 -04001369 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001370 if (retval < -1) {
1371 exit(retval);
1372 } else if (retval == -1) {
Len Browne52966c2012-11-08 22:38:05 -05001373 if (restarted > 1) {
1374 exit(retval);
1375 }
Len Brownc98d5d92012-06-04 00:56:40 -04001376 re_initialize();
1377 goto restart;
1378 }
Len Browne52966c2012-11-08 22:38:05 -05001379 restarted = 0;
Len Brown103a8fe2010-10-22 23:53:03 -04001380 gettimeofday(&tv_even, (struct timezone *)NULL);
1381
1382 while (1) {
Len Brownc98d5d92012-06-04 00:56:40 -04001383 if (for_all_proc_cpus(cpu_is_not_present)) {
Len Brown103a8fe2010-10-22 23:53:03 -04001384 re_initialize();
1385 goto restart;
1386 }
1387 sleep(interval_sec);
Len Brownc98d5d92012-06-04 00:56:40 -04001388 retval = for_all_cpus(get_counters, ODD_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001389 if (retval < -1) {
1390 exit(retval);
1391 } else if (retval == -1) {
Len Brown15aaa342012-03-29 22:19:58 -04001392 re_initialize();
1393 goto restart;
1394 }
Len Brown103a8fe2010-10-22 23:53:03 -04001395 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04001396 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04001397 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
1398 compute_average(EVEN_COUNTERS);
1399 format_all_counters(EVEN_COUNTERS);
1400 flush_stdout();
Len Brown15aaa342012-03-29 22:19:58 -04001401 sleep(interval_sec);
Len Brownc98d5d92012-06-04 00:56:40 -04001402 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Len Brownd91bb172012-11-01 00:08:19 -04001403 if (retval < -1) {
1404 exit(retval);
1405 } else if (retval == -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04001406 re_initialize();
1407 goto restart;
1408 }
Len Brown103a8fe2010-10-22 23:53:03 -04001409 gettimeofday(&tv_even, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04001410 timersub(&tv_even, &tv_odd, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04001411 for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS);
1412 compute_average(ODD_COUNTERS);
1413 format_all_counters(ODD_COUNTERS);
1414 flush_stdout();
Len Brown103a8fe2010-10-22 23:53:03 -04001415 }
1416}
1417
1418void check_dev_msr()
1419{
1420 struct stat sb;
1421
Josh Triplettb2c95d92013-08-20 17:20:18 -07001422 if (stat("/dev/cpu/0/msr", &sb))
1423 err(-5, "no /dev/cpu/0/msr\n"
1424 "Try \"# modprobe msr\"");
Len Brown103a8fe2010-10-22 23:53:03 -04001425}
1426
1427void check_super_user()
1428{
Josh Triplettb2c95d92013-08-20 17:20:18 -07001429 if (getuid() != 0)
1430 errx(-6, "must be root");
Len Brown103a8fe2010-10-22 23:53:03 -04001431}
1432
1433int has_nehalem_turbo_ratio_limit(unsigned int family, unsigned int model)
1434{
1435 if (!genuine_intel)
1436 return 0;
1437
1438 if (family != 6)
1439 return 0;
1440
1441 switch (model) {
1442 case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
1443 case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
1444 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
1445 case 0x25: /* Westmere Client - Clarkdale, Arrandale */
1446 case 0x2C: /* Westmere EP - Gulftown */
1447 case 0x2A: /* SNB */
1448 case 0x2D: /* SNB Xeon */
Len Brown553575f2011-11-18 03:32:01 -05001449 case 0x3A: /* IVB */
Len Brown13006512012-09-26 18:11:31 -04001450 case 0x3E: /* IVB Xeon */
Len Brown70b43402013-01-08 01:26:07 -05001451 case 0x3C: /* HSW */
1452 case 0x3F: /* HSW */
1453 case 0x45: /* HSW */
Len Brown149c2312013-03-15 10:58:02 -04001454 case 0x46: /* HSW */
Len Brown144b44b2013-11-09 00:30:16 -05001455 case 0x37: /* BYT */
1456 case 0x4D: /* AVN */
Len Brown103a8fe2010-10-22 23:53:03 -04001457 return 1;
1458 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1459 case 0x2F: /* Westmere-EX Xeon - Eagleton */
1460 default:
1461 return 0;
1462 }
1463}
Len Brown6574a5d2012-09-21 00:01:31 -04001464int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
1465{
1466 if (!genuine_intel)
1467 return 0;
1468
1469 if (family != 6)
1470 return 0;
1471
1472 switch (model) {
1473 case 0x3E: /* IVB Xeon */
1474 return 1;
1475 default:
1476 return 0;
1477 }
1478}
1479
Len Brown889facb2012-11-08 00:48:57 -05001480/*
1481 * print_epb()
1482 * Decode the ENERGY_PERF_BIAS MSR
1483 */
1484int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1485{
1486 unsigned long long msr;
1487 char *epb_string;
1488 int cpu;
1489
1490 if (!has_epb)
1491 return 0;
1492
1493 cpu = t->cpu_id;
1494
1495 /* EPB is per-package */
1496 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1497 return 0;
1498
1499 if (cpu_migrate(cpu)) {
1500 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1501 return -1;
1502 }
1503
1504 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
1505 return 0;
1506
1507 switch (msr & 0x7) {
1508 case ENERGY_PERF_BIAS_PERFORMANCE:
1509 epb_string = "performance";
1510 break;
1511 case ENERGY_PERF_BIAS_NORMAL:
1512 epb_string = "balanced";
1513 break;
1514 case ENERGY_PERF_BIAS_POWERSAVE:
1515 epb_string = "powersave";
1516 break;
1517 default:
1518 epb_string = "custom";
1519 break;
1520 }
1521 fprintf(stderr, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
1522
1523 return 0;
1524}
1525
1526#define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
1527#define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
1528
Len Brown144b44b2013-11-09 00:30:16 -05001529double get_tdp(model)
1530{
1531 unsigned long long msr;
1532
1533 if (do_rapl & RAPL_PKG_POWER_INFO)
1534 if (!get_msr(0, MSR_PKG_POWER_INFO, &msr))
1535 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
1536
1537 switch (model) {
1538 case 0x37:
1539 case 0x4D:
1540 return 30.0;
1541 default:
1542 return 135.0;
1543 }
1544}
1545
1546
Len Brown889facb2012-11-08 00:48:57 -05001547/*
1548 * rapl_probe()
1549 *
Len Brown144b44b2013-11-09 00:30:16 -05001550 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
Len Brown889facb2012-11-08 00:48:57 -05001551 */
1552void rapl_probe(unsigned int family, unsigned int model)
1553{
1554 unsigned long long msr;
Len Brown144b44b2013-11-09 00:30:16 -05001555 unsigned int time_unit;
Len Brown889facb2012-11-08 00:48:57 -05001556 double tdp;
1557
1558 if (!genuine_intel)
1559 return;
1560
1561 if (family != 6)
1562 return;
1563
1564 switch (model) {
1565 case 0x2A:
1566 case 0x3A:
Len Brown70b43402013-01-08 01:26:07 -05001567 case 0x3C: /* HSW */
1568 case 0x3F: /* HSW */
1569 case 0x45: /* HSW */
Len Brown149c2312013-03-15 10:58:02 -04001570 case 0x46: /* HSW */
Len Brown144b44b2013-11-09 00:30:16 -05001571 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
Len Brown889facb2012-11-08 00:48:57 -05001572 break;
1573 case 0x2D:
1574 case 0x3E:
Len Brown144b44b2013-11-09 00:30:16 -05001575 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_PKG_PERF_STATUS | RAPL_DRAM_PERF_STATUS | RAPL_PKG_POWER_INFO;
1576 break;
1577 case 0x37: /* BYT */
1578 case 0x4D: /* AVN */
1579 do_rapl = RAPL_PKG | RAPL_CORES ;
Len Brown889facb2012-11-08 00:48:57 -05001580 break;
1581 default:
1582 return;
1583 }
1584
1585 /* units on package 0, verify later other packages match */
1586 if (get_msr(0, MSR_RAPL_POWER_UNIT, &msr))
1587 return;
1588
1589 rapl_power_units = 1.0 / (1 << (msr & 0xF));
Len Brown144b44b2013-11-09 00:30:16 -05001590 if (model == 0x37)
1591 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
1592 else
1593 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
Len Brown889facb2012-11-08 00:48:57 -05001594
Len Brown144b44b2013-11-09 00:30:16 -05001595 time_unit = msr >> 16 & 0xF;
1596 if (time_unit == 0)
1597 time_unit = 0xA;
Len Brown889facb2012-11-08 00:48:57 -05001598
Len Brown144b44b2013-11-09 00:30:16 -05001599 rapl_time_units = 1.0 / (1 << (time_unit));
1600
1601 tdp = get_tdp(model);
Len Brown889facb2012-11-08 00:48:57 -05001602
1603 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
Len Brown889facb2012-11-08 00:48:57 -05001604 if (verbose)
Len Brown144b44b2013-11-09 00:30:16 -05001605 fprintf(stderr, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
Len Brown889facb2012-11-08 00:48:57 -05001606
1607 return;
1608}
1609
1610int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1611{
1612 unsigned long long msr;
1613 unsigned int dts;
1614 int cpu;
1615
1616 if (!(do_dts || do_ptm))
1617 return 0;
1618
1619 cpu = t->cpu_id;
1620
1621 /* DTS is per-core, no need to print for each thread */
1622 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1623 return 0;
1624
1625 if (cpu_migrate(cpu)) {
1626 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1627 return -1;
1628 }
1629
1630 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
1631 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1632 return 0;
1633
1634 dts = (msr >> 16) & 0x7F;
1635 fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
1636 cpu, msr, tcc_activation_temp - dts);
1637
1638#ifdef THERM_DEBUG
1639 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
1640 return 0;
1641
1642 dts = (msr >> 16) & 0x7F;
1643 dts2 = (msr >> 8) & 0x7F;
1644 fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
1645 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
1646#endif
1647 }
1648
1649
1650 if (do_dts) {
1651 unsigned int resolution;
1652
1653 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1654 return 0;
1655
1656 dts = (msr >> 16) & 0x7F;
1657 resolution = (msr >> 27) & 0xF;
1658 fprintf(stderr, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
1659 cpu, msr, tcc_activation_temp - dts, resolution);
1660
1661#ifdef THERM_DEBUG
1662 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
1663 return 0;
1664
1665 dts = (msr >> 16) & 0x7F;
1666 dts2 = (msr >> 8) & 0x7F;
1667 fprintf(stderr, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
1668 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
1669#endif
1670 }
1671
1672 return 0;
1673}
1674
1675void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
1676{
1677 fprintf(stderr, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
1678 cpu, label,
1679 ((msr >> 15) & 1) ? "EN" : "DIS",
1680 ((msr >> 0) & 0x7FFF) * rapl_power_units,
1681 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
1682 (((msr >> 16) & 1) ? "EN" : "DIS"));
1683
1684 return;
1685}
1686
1687int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1688{
1689 unsigned long long msr;
1690 int cpu;
Len Brown889facb2012-11-08 00:48:57 -05001691
1692 if (!do_rapl)
1693 return 0;
1694
1695 /* RAPL counters are per package, so print only for 1st thread/package */
1696 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1697 return 0;
1698
1699 cpu = t->cpu_id;
1700 if (cpu_migrate(cpu)) {
1701 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1702 return -1;
1703 }
1704
1705 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
1706 return -1;
1707
Len Brown889facb2012-11-08 00:48:57 -05001708 if (verbose) {
1709 fprintf(stderr, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx "
1710 "(%f Watts, %f Joules, %f sec.)\n", cpu, msr,
Len Brown144b44b2013-11-09 00:30:16 -05001711 rapl_power_units, rapl_energy_units, rapl_time_units);
Len Brown889facb2012-11-08 00:48:57 -05001712 }
Len Brown144b44b2013-11-09 00:30:16 -05001713 if (do_rapl & RAPL_PKG_POWER_INFO) {
1714
Len Brown889facb2012-11-08 00:48:57 -05001715 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
1716 return -5;
1717
1718
1719 fprintf(stderr, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
1720 cpu, msr,
1721 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1722 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1723 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1724 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
1725
Len Brown144b44b2013-11-09 00:30:16 -05001726 }
1727 if (do_rapl & RAPL_PKG) {
1728
Len Brown889facb2012-11-08 00:48:57 -05001729 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
1730 return -9;
1731
1732 fprintf(stderr, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
1733 cpu, msr, (msr >> 63) & 1 ? "": "UN");
1734
1735 print_power_limit_msr(cpu, msr, "PKG Limit #1");
1736 fprintf(stderr, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
1737 cpu,
1738 ((msr >> 47) & 1) ? "EN" : "DIS",
1739 ((msr >> 32) & 0x7FFF) * rapl_power_units,
1740 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
1741 ((msr >> 48) & 1) ? "EN" : "DIS");
1742 }
1743
1744 if (do_rapl & RAPL_DRAM) {
1745 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
1746 return -6;
1747
1748
1749 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
1750 cpu, msr,
1751 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1752 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1753 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1754 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
1755
1756
1757 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
1758 return -9;
1759 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
1760 cpu, msr, (msr >> 31) & 1 ? "": "UN");
1761
1762 print_power_limit_msr(cpu, msr, "DRAM Limit");
1763 }
Len Brown144b44b2013-11-09 00:30:16 -05001764 if (do_rapl & RAPL_CORE_POLICY) {
Len Brown889facb2012-11-08 00:48:57 -05001765 if (verbose) {
1766 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
1767 return -7;
1768
1769 fprintf(stderr, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
Len Brown144b44b2013-11-09 00:30:16 -05001770 }
1771 }
1772 if (do_rapl & RAPL_CORES) {
1773 if (verbose) {
Len Brown889facb2012-11-08 00:48:57 -05001774
1775 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
1776 return -9;
1777 fprintf(stderr, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
1778 cpu, msr, (msr >> 31) & 1 ? "": "UN");
1779 print_power_limit_msr(cpu, msr, "Cores Limit");
1780 }
1781 }
1782 if (do_rapl & RAPL_GFX) {
1783 if (verbose) {
1784 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
1785 return -8;
1786
1787 fprintf(stderr, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
1788
1789 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
1790 return -9;
1791 fprintf(stderr, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
1792 cpu, msr, (msr >> 31) & 1 ? "": "UN");
1793 print_power_limit_msr(cpu, msr, "GFX Limit");
1794 }
1795 }
1796 return 0;
1797}
1798
Len Brown103a8fe2010-10-22 23:53:03 -04001799
1800int is_snb(unsigned int family, unsigned int model)
1801{
1802 if (!genuine_intel)
1803 return 0;
1804
1805 switch (model) {
1806 case 0x2A:
1807 case 0x2D:
Len Brown650a37f2012-06-03 23:34:44 -04001808 case 0x3A: /* IVB */
Len Brown13006512012-09-26 18:11:31 -04001809 case 0x3E: /* IVB Xeon */
Len Brown70b43402013-01-08 01:26:07 -05001810 case 0x3C: /* HSW */
1811 case 0x3F: /* HSW */
1812 case 0x45: /* HSW */
Len Brown149c2312013-03-15 10:58:02 -04001813 case 0x46: /* HSW */
Len Brown103a8fe2010-10-22 23:53:03 -04001814 return 1;
1815 }
1816 return 0;
1817}
1818
Kristen Carlson Accardica587102012-11-21 05:22:43 -08001819int has_c8_c9_c10(unsigned int family, unsigned int model)
1820{
1821 if (!genuine_intel)
1822 return 0;
1823
1824 switch (model) {
1825 case 0x45:
1826 return 1;
1827 }
1828 return 0;
1829}
1830
1831
Len Brown144b44b2013-11-09 00:30:16 -05001832int is_slm(unsigned int family, unsigned int model)
1833{
1834 if (!genuine_intel)
1835 return 0;
1836 switch (model) {
1837 case 0x37: /* BYT */
1838 case 0x4D: /* AVN */
1839 return 1;
1840 }
1841 return 0;
1842}
1843
1844#define SLM_BCLK_FREQS 5
1845double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
1846
1847double slm_bclk(void)
1848{
1849 unsigned long long msr = 3;
1850 unsigned int i;
1851 double freq;
1852
1853 if (get_msr(0, MSR_FSB_FREQ, &msr))
1854 fprintf(stderr, "SLM BCLK: unknown\n");
1855
1856 i = msr & 0xf;
1857 if (i >= SLM_BCLK_FREQS) {
1858 fprintf(stderr, "SLM BCLK[%d] invalid\n", i);
1859 msr = 3;
1860 }
1861 freq = slm_freq_table[i];
1862
1863 fprintf(stderr, "SLM BCLK: %.1f Mhz\n", freq);
1864
1865 return freq;
1866}
1867
Len Brown103a8fe2010-10-22 23:53:03 -04001868double discover_bclk(unsigned int family, unsigned int model)
1869{
1870 if (is_snb(family, model))
1871 return 100.00;
Len Brown144b44b2013-11-09 00:30:16 -05001872 else if (is_slm(family, model))
1873 return slm_bclk();
Len Brown103a8fe2010-10-22 23:53:03 -04001874 else
1875 return 133.33;
1876}
1877
Len Brown889facb2012-11-08 00:48:57 -05001878/*
1879 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
1880 * the Thermal Control Circuit (TCC) activates.
1881 * This is usually equal to tjMax.
1882 *
1883 * Older processors do not have this MSR, so there we guess,
1884 * but also allow cmdline over-ride with -T.
1885 *
1886 * Several MSR temperature values are in units of degrees-C
1887 * below this value, including the Digital Thermal Sensor (DTS),
1888 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
1889 */
1890int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1891{
1892 unsigned long long msr;
1893 unsigned int target_c_local;
1894 int cpu;
1895
1896 /* tcc_activation_temp is used only for dts or ptm */
1897 if (!(do_dts || do_ptm))
1898 return 0;
1899
1900 /* this is a per-package concept */
1901 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1902 return 0;
1903
1904 cpu = t->cpu_id;
1905 if (cpu_migrate(cpu)) {
1906 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1907 return -1;
1908 }
1909
1910 if (tcc_activation_temp_override != 0) {
1911 tcc_activation_temp = tcc_activation_temp_override;
1912 fprintf(stderr, "cpu%d: Using cmdline TCC Target (%d C)\n",
1913 cpu, tcc_activation_temp);
1914 return 0;
1915 }
1916
1917 /* Temperature Target MSR is Nehalem and newer only */
1918 if (!do_nehalem_platform_info)
1919 goto guess;
1920
1921 if (get_msr(0, MSR_IA32_TEMPERATURE_TARGET, &msr))
1922 goto guess;
1923
1924 target_c_local = (msr >> 16) & 0x7F;
1925
1926 if (verbose)
1927 fprintf(stderr, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
1928 cpu, msr, target_c_local);
1929
Len Brown144b44b2013-11-09 00:30:16 -05001930 if (target_c_local < 85 || target_c_local > 127)
Len Brown889facb2012-11-08 00:48:57 -05001931 goto guess;
1932
1933 tcc_activation_temp = target_c_local;
1934
1935 return 0;
1936
1937guess:
1938 tcc_activation_temp = TJMAX_DEFAULT;
1939 fprintf(stderr, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
1940 cpu, tcc_activation_temp);
1941
1942 return 0;
1943}
Len Brown103a8fe2010-10-22 23:53:03 -04001944void check_cpuid()
1945{
1946 unsigned int eax, ebx, ecx, edx, max_level;
1947 unsigned int fms, family, model, stepping;
1948
1949 eax = ebx = ecx = edx = 0;
1950
Josh Triplett2b928652013-08-20 17:20:14 -07001951 __get_cpuid(0, &max_level, &ebx, &ecx, &edx);
Len Brown103a8fe2010-10-22 23:53:03 -04001952
1953 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
1954 genuine_intel = 1;
1955
1956 if (verbose)
Len Brown889facb2012-11-08 00:48:57 -05001957 fprintf(stderr, "CPUID(0): %.4s%.4s%.4s ",
Len Brown103a8fe2010-10-22 23:53:03 -04001958 (char *)&ebx, (char *)&edx, (char *)&ecx);
1959
Josh Triplett2b928652013-08-20 17:20:14 -07001960 __get_cpuid(1, &fms, &ebx, &ecx, &edx);
Len Brown103a8fe2010-10-22 23:53:03 -04001961 family = (fms >> 8) & 0xf;
1962 model = (fms >> 4) & 0xf;
1963 stepping = fms & 0xf;
1964 if (family == 6 || family == 0xf)
1965 model += ((fms >> 16) & 0xf) << 4;
1966
1967 if (verbose)
1968 fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
1969 max_level, family, model, stepping, family, model, stepping);
1970
Josh Triplettb2c95d92013-08-20 17:20:18 -07001971 if (!(edx & (1 << 5)))
1972 errx(1, "CPUID: no MSR");
Len Brown103a8fe2010-10-22 23:53:03 -04001973
1974 /*
1975 * check max extended function levels of CPUID.
1976 * This is needed to check for invariant TSC.
1977 * This check is valid for both Intel and AMD.
1978 */
1979 ebx = ecx = edx = 0;
Josh Triplett2b928652013-08-20 17:20:14 -07001980 __get_cpuid(0x80000000, &max_level, &ebx, &ecx, &edx);
Len Brown103a8fe2010-10-22 23:53:03 -04001981
Josh Triplettb2c95d92013-08-20 17:20:18 -07001982 if (max_level < 0x80000007)
1983 errx(1, "CPUID: no invariant TSC (max_level 0x%x)", max_level);
Len Brown103a8fe2010-10-22 23:53:03 -04001984
1985 /*
1986 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
1987 * this check is valid for both Intel and AMD
1988 */
Josh Triplett2b928652013-08-20 17:20:14 -07001989 __get_cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
Thomas Renninger8209e052011-01-21 15:11:19 +01001990 has_invariant_tsc = edx & (1 << 8);
Len Brown103a8fe2010-10-22 23:53:03 -04001991
Josh Triplettb2c95d92013-08-20 17:20:18 -07001992 if (!has_invariant_tsc)
1993 errx(1, "No invariant TSC");
Len Brown103a8fe2010-10-22 23:53:03 -04001994
1995 /*
1996 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
1997 * this check is valid for both Intel and AMD
1998 */
1999
Josh Triplett2b928652013-08-20 17:20:14 -07002000 __get_cpuid(0x6, &eax, &ebx, &ecx, &edx);
Thomas Renninger8209e052011-01-21 15:11:19 +01002001 has_aperf = ecx & (1 << 0);
Len Brown889facb2012-11-08 00:48:57 -05002002 do_dts = eax & (1 << 0);
2003 do_ptm = eax & (1 << 6);
2004 has_epb = ecx & (1 << 3);
2005
2006 if (verbose)
2007 fprintf(stderr, "CPUID(6): %s%s%s%s\n",
2008 has_aperf ? "APERF" : "No APERF!",
2009 do_dts ? ", DTS" : "",
2010 do_ptm ? ", PTM": "",
2011 has_epb ? ", EPB": "");
2012
2013 if (!has_aperf)
Josh Triplettb2c95d92013-08-20 17:20:18 -07002014 errx(-1, "No APERF");
Len Brown103a8fe2010-10-22 23:53:03 -04002015
2016 do_nehalem_platform_info = genuine_intel && has_invariant_tsc;
2017 do_nhm_cstates = genuine_intel; /* all Intel w/ non-stop TSC have NHM counters */
Len Brown1ed51012013-02-10 17:19:24 -05002018 do_smi = do_nhm_cstates;
Len Brown103a8fe2010-10-22 23:53:03 -04002019 do_snb_cstates = is_snb(family, model);
Kristen Carlson Accardica587102012-11-21 05:22:43 -08002020 do_c8_c9_c10 = has_c8_c9_c10(family, model);
Len Brown144b44b2013-11-09 00:30:16 -05002021 do_slm_cstates = is_slm(family, model);
Len Brown103a8fe2010-10-22 23:53:03 -04002022 bclk = discover_bclk(family, model);
2023
2024 do_nehalem_turbo_ratio_limit = has_nehalem_turbo_ratio_limit(family, model);
Len Brown6574a5d2012-09-21 00:01:31 -04002025 do_ivt_turbo_ratio_limit = has_ivt_turbo_ratio_limit(family, model);
Len Brown889facb2012-11-08 00:48:57 -05002026 rapl_probe(family, model);
2027
2028 return;
Len Brown103a8fe2010-10-22 23:53:03 -04002029}
2030
2031
2032void usage()
2033{
Josh Triplettb2c95d92013-08-20 17:20:18 -07002034 errx(1, "%s: [-v][-R][-T][-p|-P|-S][-c MSR# | -s]][-C MSR#][-m MSR#][-M MSR#][-i interval_sec | command ...]\n",
2035 progname);
Len Brown103a8fe2010-10-22 23:53:03 -04002036}
2037
2038
2039/*
2040 * in /dev/cpu/ return success for names that are numbers
2041 * ie. filter out ".", "..", "microcode".
2042 */
2043int dir_filter(const struct dirent *dirp)
2044{
2045 if (isdigit(dirp->d_name[0]))
2046 return 1;
2047 else
2048 return 0;
2049}
2050
2051int open_dev_cpu_msr(int dummy1)
2052{
2053 return 0;
2054}
2055
Len Brownc98d5d92012-06-04 00:56:40 -04002056void topology_probe()
2057{
2058 int i;
2059 int max_core_id = 0;
2060 int max_package_id = 0;
2061 int max_siblings = 0;
2062 struct cpu_topology {
2063 int core_id;
2064 int physical_package_id;
2065 } *cpus;
2066
2067 /* Initialize num_cpus, max_cpu_num */
2068 topo.num_cpus = 0;
2069 topo.max_cpu_num = 0;
2070 for_all_proc_cpus(count_cpus);
2071 if (!summary_only && topo.num_cpus > 1)
2072 show_cpu = 1;
2073
2074 if (verbose > 1)
2075 fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
2076
2077 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
Josh Triplettb2c95d92013-08-20 17:20:18 -07002078 if (cpus == NULL)
2079 err(1, "calloc cpus");
Len Brownc98d5d92012-06-04 00:56:40 -04002080
2081 /*
2082 * Allocate and initialize cpu_present_set
2083 */
2084 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07002085 if (cpu_present_set == NULL)
2086 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04002087 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2088 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
2089 for_all_proc_cpus(mark_cpu_present);
2090
2091 /*
2092 * Allocate and initialize cpu_affinity_set
2093 */
2094 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
Josh Triplettb2c95d92013-08-20 17:20:18 -07002095 if (cpu_affinity_set == NULL)
2096 err(3, "CPU_ALLOC");
Len Brownc98d5d92012-06-04 00:56:40 -04002097 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2098 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
2099
2100
2101 /*
2102 * For online cpus
2103 * find max_core_id, max_package_id
2104 */
2105 for (i = 0; i <= topo.max_cpu_num; ++i) {
2106 int siblings;
2107
2108 if (cpu_is_not_present(i)) {
2109 if (verbose > 1)
2110 fprintf(stderr, "cpu%d NOT PRESENT\n", i);
2111 continue;
2112 }
2113 cpus[i].core_id = get_core_id(i);
2114 if (cpus[i].core_id > max_core_id)
2115 max_core_id = cpus[i].core_id;
2116
2117 cpus[i].physical_package_id = get_physical_package_id(i);
2118 if (cpus[i].physical_package_id > max_package_id)
2119 max_package_id = cpus[i].physical_package_id;
2120
2121 siblings = get_num_ht_siblings(i);
2122 if (siblings > max_siblings)
2123 max_siblings = siblings;
2124 if (verbose > 1)
2125 fprintf(stderr, "cpu %d pkg %d core %d\n",
2126 i, cpus[i].physical_package_id, cpus[i].core_id);
2127 }
2128 topo.num_cores_per_pkg = max_core_id + 1;
2129 if (verbose > 1)
2130 fprintf(stderr, "max_core_id %d, sizing for %d cores per package\n",
2131 max_core_id, topo.num_cores_per_pkg);
2132 if (!summary_only && topo.num_cores_per_pkg > 1)
2133 show_core = 1;
2134
2135 topo.num_packages = max_package_id + 1;
2136 if (verbose > 1)
2137 fprintf(stderr, "max_package_id %d, sizing for %d packages\n",
2138 max_package_id, topo.num_packages);
2139 if (!summary_only && topo.num_packages > 1)
2140 show_pkg = 1;
2141
2142 topo.num_threads_per_core = max_siblings;
2143 if (verbose > 1)
2144 fprintf(stderr, "max_siblings %d\n", max_siblings);
2145
2146 free(cpus);
2147}
2148
2149void
2150allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
2151{
2152 int i;
2153
2154 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
2155 topo.num_packages, sizeof(struct thread_data));
2156 if (*t == NULL)
2157 goto error;
2158
2159 for (i = 0; i < topo.num_threads_per_core *
2160 topo.num_cores_per_pkg * topo.num_packages; i++)
2161 (*t)[i].cpu_id = -1;
2162
2163 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
2164 sizeof(struct core_data));
2165 if (*c == NULL)
2166 goto error;
2167
2168 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
2169 (*c)[i].core_id = -1;
2170
2171 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
2172 if (*p == NULL)
2173 goto error;
2174
2175 for (i = 0; i < topo.num_packages; i++)
2176 (*p)[i].package_id = i;
2177
2178 return;
2179error:
Josh Triplettb2c95d92013-08-20 17:20:18 -07002180 err(1, "calloc counters");
Len Brownc98d5d92012-06-04 00:56:40 -04002181}
2182/*
2183 * init_counter()
2184 *
2185 * set cpu_id, core_num, pkg_num
2186 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
2187 *
2188 * increment topo.num_cores when 1st core in pkg seen
2189 */
2190void init_counter(struct thread_data *thread_base, struct core_data *core_base,
2191 struct pkg_data *pkg_base, int thread_num, int core_num,
2192 int pkg_num, int cpu_id)
2193{
2194 struct thread_data *t;
2195 struct core_data *c;
2196 struct pkg_data *p;
2197
2198 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
2199 c = GET_CORE(core_base, core_num, pkg_num);
2200 p = GET_PKG(pkg_base, pkg_num);
2201
2202 t->cpu_id = cpu_id;
2203 if (thread_num == 0) {
2204 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
2205 if (cpu_is_first_core_in_package(cpu_id))
2206 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
2207 }
2208
2209 c->core_id = core_num;
2210 p->package_id = pkg_num;
2211}
2212
2213
2214int initialize_counters(int cpu_id)
2215{
2216 int my_thread_id, my_core_id, my_package_id;
2217
2218 my_package_id = get_physical_package_id(cpu_id);
2219 my_core_id = get_core_id(cpu_id);
2220
2221 if (cpu_is_first_sibling_in_core(cpu_id)) {
2222 my_thread_id = 0;
2223 topo.num_cores++;
2224 } else {
2225 my_thread_id = 1;
2226 }
2227
2228 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2229 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2230 return 0;
2231}
2232
2233void allocate_output_buffer()
2234{
Josh Triplettb844db32013-06-12 17:26:37 -07002235 output_buffer = calloc(1, (1 + topo.num_cpus) * 256);
Len Brownc98d5d92012-06-04 00:56:40 -04002236 outp = output_buffer;
Josh Triplettb2c95d92013-08-20 17:20:18 -07002237 if (outp == NULL)
2238 err(-1, "calloc output buffer");
Len Brownc98d5d92012-06-04 00:56:40 -04002239}
2240
2241void setup_all_buffers(void)
2242{
2243 topology_probe();
2244 allocate_counters(&thread_even, &core_even, &package_even);
2245 allocate_counters(&thread_odd, &core_odd, &package_odd);
2246 allocate_output_buffer();
2247 for_all_proc_cpus(initialize_counters);
2248}
Len Brown103a8fe2010-10-22 23:53:03 -04002249void turbostat_init()
2250{
2251 check_cpuid();
2252
2253 check_dev_msr();
2254 check_super_user();
2255
Len Brownc98d5d92012-06-04 00:56:40 -04002256 setup_all_buffers();
Len Brown103a8fe2010-10-22 23:53:03 -04002257
2258 if (verbose)
Len Brownc98d5d92012-06-04 00:56:40 -04002259 print_verbose_header();
Len Brown889facb2012-11-08 00:48:57 -05002260
2261 if (verbose)
2262 for_all_cpus(print_epb, ODD_COUNTERS);
2263
2264 if (verbose)
2265 for_all_cpus(print_rapl, ODD_COUNTERS);
2266
2267 for_all_cpus(set_temperature_target, ODD_COUNTERS);
2268
2269 if (verbose)
2270 for_all_cpus(print_thermal, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04002271}
2272
2273int fork_it(char **argv)
2274{
Len Brown103a8fe2010-10-22 23:53:03 -04002275 pid_t child_pid;
Len Brownd91bb172012-11-01 00:08:19 -04002276 int status;
Len Brownd15cf7c2012-06-03 23:24:00 -04002277
Len Brownd91bb172012-11-01 00:08:19 -04002278 status = for_all_cpus(get_counters, EVEN_COUNTERS);
2279 if (status)
2280 exit(status);
Len Brownc98d5d92012-06-04 00:56:40 -04002281 /* clear affinity side-effect of get_counters() */
2282 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
Len Brown103a8fe2010-10-22 23:53:03 -04002283 gettimeofday(&tv_even, (struct timezone *)NULL);
2284
2285 child_pid = fork();
2286 if (!child_pid) {
2287 /* child */
2288 execvp(argv[0], argv);
2289 } else {
Len Brown103a8fe2010-10-22 23:53:03 -04002290
2291 /* parent */
Josh Triplettb2c95d92013-08-20 17:20:18 -07002292 if (child_pid == -1)
2293 err(1, "fork");
Len Brown103a8fe2010-10-22 23:53:03 -04002294
2295 signal(SIGINT, SIG_IGN);
2296 signal(SIGQUIT, SIG_IGN);
Josh Triplettb2c95d92013-08-20 17:20:18 -07002297 if (waitpid(child_pid, &status, 0) == -1)
2298 err(status, "waitpid");
Len Brown103a8fe2010-10-22 23:53:03 -04002299 }
Len Brownc98d5d92012-06-04 00:56:40 -04002300 /*
2301 * n.b. fork_it() does not check for errors from for_all_cpus()
2302 * because re-starting is problematic when forking
2303 */
2304 for_all_cpus(get_counters, ODD_COUNTERS);
Len Brown103a8fe2010-10-22 23:53:03 -04002305 gettimeofday(&tv_odd, (struct timezone *)NULL);
Len Brown103a8fe2010-10-22 23:53:03 -04002306 timersub(&tv_odd, &tv_even, &tv_delta);
Len Brownc98d5d92012-06-04 00:56:40 -04002307 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
2308 compute_average(EVEN_COUNTERS);
2309 format_all_counters(EVEN_COUNTERS);
2310 flush_stderr();
Len Brown103a8fe2010-10-22 23:53:03 -04002311
Justin P. Mattock6eab04a2011-04-08 19:49:08 -07002312 fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
Len Brown103a8fe2010-10-22 23:53:03 -04002313
Len Brownd91bb172012-11-01 00:08:19 -04002314 return status;
Len Brown103a8fe2010-10-22 23:53:03 -04002315}
2316
2317void cmdline(int argc, char **argv)
2318{
2319 int opt;
2320
2321 progname = argv[0];
2322
Len Brown889facb2012-11-08 00:48:57 -05002323 while ((opt = getopt(argc, argv, "+pPSvi:sc:sC:m:M:RT:")) != -1) {
Len Brown103a8fe2010-10-22 23:53:03 -04002324 switch (opt) {
Len Brownf9240812012-10-06 15:26:31 -04002325 case 'p':
Len Brownc98d5d92012-06-04 00:56:40 -04002326 show_core_only++;
2327 break;
Len Brownf9240812012-10-06 15:26:31 -04002328 case 'P':
Len Brownc98d5d92012-06-04 00:56:40 -04002329 show_pkg_only++;
2330 break;
Len Brownf9240812012-10-06 15:26:31 -04002331 case 'S':
Len Browne23da032012-02-06 18:37:16 -05002332 summary_only++;
2333 break;
Len Brown103a8fe2010-10-22 23:53:03 -04002334 case 'v':
2335 verbose++;
2336 break;
2337 case 'i':
2338 interval_sec = atoi(optarg);
2339 break;
Len Brownf9240812012-10-06 15:26:31 -04002340 case 'c':
Len Brown8e180f32012-09-22 01:25:08 -04002341 sscanf(optarg, "%x", &extra_delta_offset32);
2342 break;
Len Brownf9240812012-10-06 15:26:31 -04002343 case 'C':
Len Brown8e180f32012-09-22 01:25:08 -04002344 sscanf(optarg, "%x", &extra_delta_offset64);
2345 break;
Len Brown2f32edf2012-09-21 23:45:46 -04002346 case 'm':
2347 sscanf(optarg, "%x", &extra_msr_offset32);
Len Brown2f32edf2012-09-21 23:45:46 -04002348 break;
2349 case 'M':
2350 sscanf(optarg, "%x", &extra_msr_offset64);
Len Brown103a8fe2010-10-22 23:53:03 -04002351 break;
Len Brown889facb2012-11-08 00:48:57 -05002352 case 'R':
2353 rapl_verbose++;
2354 break;
2355 case 'T':
2356 tcc_activation_temp_override = atoi(optarg);
2357 break;
Len Brown103a8fe2010-10-22 23:53:03 -04002358 default:
2359 usage();
2360 }
2361 }
2362}
2363
2364int main(int argc, char **argv)
2365{
2366 cmdline(argc, argv);
2367
Len Brown889facb2012-11-08 00:48:57 -05002368 if (verbose)
Len Brown144b44b2013-11-09 00:30:16 -05002369 fprintf(stderr, "turbostat v3.5 April 26, 2013"
Len Brown103a8fe2010-10-22 23:53:03 -04002370 " - Len Brown <lenb@kernel.org>\n");
Len Brown103a8fe2010-10-22 23:53:03 -04002371
2372 turbostat_init();
2373
2374 /*
2375 * if any params left, it must be a command to fork
2376 */
2377 if (argc - optind)
2378 return fork_it(argv + optind);
2379 else
2380 turbostat_loop();
2381
2382 return 0;
2383}