x86/xen: resume timer irqs early
[linux/fpc-iii.git] / tools / power / x86 / turbostat / turbostat.c
blob6a7ee5f21c9bcea5a75ee868852202a04ba5d04a
1 /*
2 * turbostat -- show CPU frequency and C-state residency
3 * on modern Intel turbo-capable processors.
5 * Copyright (c) 2012 Intel Corporation.
6 * Len Brown <len.brown@intel.com>
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.
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.
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.
22 #define _GNU_SOURCE
23 #include MSRHEADER
24 #include <stdio.h>
25 #include <unistd.h>
26 #include <sys/types.h>
27 #include <sys/wait.h>
28 #include <sys/stat.h>
29 #include <sys/resource.h>
30 #include <fcntl.h>
31 #include <signal.h>
32 #include <sys/time.h>
33 #include <stdlib.h>
34 #include <dirent.h>
35 #include <string.h>
36 #include <ctype.h>
37 #include <sched.h>
38 #include <cpuid.h>
40 char *proc_stat = "/proc/stat";
41 unsigned int interval_sec = 5; /* set with -i interval_sec */
42 unsigned int verbose; /* set with -v */
43 unsigned int rapl_verbose; /* set with -R */
44 unsigned int thermal_verbose; /* set with -T */
45 unsigned int summary_only; /* set with -s */
46 unsigned int skip_c0;
47 unsigned int skip_c1;
48 unsigned int do_nhm_cstates;
49 unsigned int do_snb_cstates;
50 unsigned int do_c8_c9_c10;
51 unsigned int has_aperf;
52 unsigned int has_epb;
53 unsigned int units = 1000000000; /* Ghz etc */
54 unsigned int genuine_intel;
55 unsigned int has_invariant_tsc;
56 unsigned int do_nehalem_platform_info;
57 unsigned int do_nehalem_turbo_ratio_limit;
58 unsigned int do_ivt_turbo_ratio_limit;
59 unsigned int extra_msr_offset32;
60 unsigned int extra_msr_offset64;
61 unsigned int extra_delta_offset32;
62 unsigned int extra_delta_offset64;
63 int do_smi;
64 double bclk;
65 unsigned int show_pkg;
66 unsigned int show_core;
67 unsigned int show_cpu;
68 unsigned int show_pkg_only;
69 unsigned int show_core_only;
70 char *output_buffer, *outp;
71 unsigned int do_rapl;
72 unsigned int do_dts;
73 unsigned int do_ptm;
74 unsigned int tcc_activation_temp;
75 unsigned int tcc_activation_temp_override;
76 double rapl_power_units, rapl_energy_units, rapl_time_units;
77 double rapl_joule_counter_range;
79 #define RAPL_PKG (1 << 0)
80 #define RAPL_CORES (1 << 1)
81 #define RAPL_GFX (1 << 2)
82 #define RAPL_DRAM (1 << 3)
83 #define RAPL_PKG_PERF_STATUS (1 << 4)
84 #define RAPL_DRAM_PERF_STATUS (1 << 5)
85 #define TJMAX_DEFAULT 100
87 #define MAX(a, b) ((a) > (b) ? (a) : (b))
89 int aperf_mperf_unstable;
90 int backwards_count;
91 char *progname;
93 cpu_set_t *cpu_present_set, *cpu_affinity_set;
94 size_t cpu_present_setsize, cpu_affinity_setsize;
96 struct thread_data {
97 unsigned long long tsc;
98 unsigned long long aperf;
99 unsigned long long mperf;
100 unsigned long long c1; /* derived */
101 unsigned long long extra_msr64;
102 unsigned long long extra_delta64;
103 unsigned long long extra_msr32;
104 unsigned long long extra_delta32;
105 unsigned int smi_count;
106 unsigned int cpu_id;
107 unsigned int flags;
108 #define CPU_IS_FIRST_THREAD_IN_CORE 0x2
109 #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
110 } *thread_even, *thread_odd;
112 struct core_data {
113 unsigned long long c3;
114 unsigned long long c6;
115 unsigned long long c7;
116 unsigned int core_temp_c;
117 unsigned int core_id;
118 } *core_even, *core_odd;
120 struct pkg_data {
121 unsigned long long pc2;
122 unsigned long long pc3;
123 unsigned long long pc6;
124 unsigned long long pc7;
125 unsigned long long pc8;
126 unsigned long long pc9;
127 unsigned long long pc10;
128 unsigned int package_id;
129 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
130 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
131 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
132 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
133 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
134 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
135 unsigned int pkg_temp_c;
137 } *package_even, *package_odd;
139 #define ODD_COUNTERS thread_odd, core_odd, package_odd
140 #define EVEN_COUNTERS thread_even, core_even, package_even
142 #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
143 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
144 topo.num_threads_per_core + \
145 (core_no) * topo.num_threads_per_core + (thread_no))
146 #define GET_CORE(core_base, core_no, pkg_no) \
147 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
148 #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
150 struct system_summary {
151 struct thread_data threads;
152 struct core_data cores;
153 struct pkg_data packages;
154 } sum, average;
157 struct topo_params {
158 int num_packages;
159 int num_cpus;
160 int num_cores;
161 int max_cpu_num;
162 int num_cores_per_pkg;
163 int num_threads_per_core;
164 } topo;
166 struct timeval tv_even, tv_odd, tv_delta;
168 void setup_all_buffers(void);
170 int cpu_is_not_present(int cpu)
172 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
175 * run func(thread, core, package) in topology order
176 * skip non-present cpus
179 int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
180 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
182 int retval, pkg_no, core_no, thread_no;
184 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
185 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
186 for (thread_no = 0; thread_no <
187 topo.num_threads_per_core; ++thread_no) {
188 struct thread_data *t;
189 struct core_data *c;
190 struct pkg_data *p;
192 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
194 if (cpu_is_not_present(t->cpu_id))
195 continue;
197 c = GET_CORE(core_base, core_no, pkg_no);
198 p = GET_PKG(pkg_base, pkg_no);
200 retval = func(t, c, p);
201 if (retval)
202 return retval;
206 return 0;
209 int cpu_migrate(int cpu)
211 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
212 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
213 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
214 return -1;
215 else
216 return 0;
219 int get_msr(int cpu, off_t offset, unsigned long long *msr)
221 ssize_t retval;
222 char pathname[32];
223 int fd;
225 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
226 fd = open(pathname, O_RDONLY);
227 if (fd < 0)
228 return -1;
230 retval = pread(fd, msr, sizeof *msr, offset);
231 close(fd);
233 if (retval != sizeof *msr) {
234 fprintf(stderr, "%s offset 0x%zx read failed\n", pathname, offset);
235 return -1;
238 return 0;
241 void print_header(void)
243 if (show_pkg)
244 outp += sprintf(outp, "pk");
245 if (show_pkg)
246 outp += sprintf(outp, " ");
247 if (show_core)
248 outp += sprintf(outp, "cor");
249 if (show_cpu)
250 outp += sprintf(outp, " CPU");
251 if (show_pkg || show_core || show_cpu)
252 outp += sprintf(outp, " ");
253 if (do_nhm_cstates)
254 outp += sprintf(outp, " %%c0");
255 if (has_aperf)
256 outp += sprintf(outp, " GHz");
257 outp += sprintf(outp, " TSC");
258 if (do_smi)
259 outp += sprintf(outp, " SMI");
260 if (extra_delta_offset32)
261 outp += sprintf(outp, " count 0x%03X", extra_delta_offset32);
262 if (extra_delta_offset64)
263 outp += sprintf(outp, " COUNT 0x%03X", extra_delta_offset64);
264 if (extra_msr_offset32)
265 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset32);
266 if (extra_msr_offset64)
267 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset64);
268 if (do_nhm_cstates)
269 outp += sprintf(outp, " %%c1");
270 if (do_nhm_cstates)
271 outp += sprintf(outp, " %%c3");
272 if (do_nhm_cstates)
273 outp += sprintf(outp, " %%c6");
274 if (do_snb_cstates)
275 outp += sprintf(outp, " %%c7");
277 if (do_dts)
278 outp += sprintf(outp, " CTMP");
279 if (do_ptm)
280 outp += sprintf(outp, " PTMP");
282 if (do_snb_cstates)
283 outp += sprintf(outp, " %%pc2");
284 if (do_nhm_cstates)
285 outp += sprintf(outp, " %%pc3");
286 if (do_nhm_cstates)
287 outp += sprintf(outp, " %%pc6");
288 if (do_snb_cstates)
289 outp += sprintf(outp, " %%pc7");
290 if (do_c8_c9_c10) {
291 outp += sprintf(outp, " %%pc8");
292 outp += sprintf(outp, " %%pc9");
293 outp += sprintf(outp, " %%pc10");
296 if (do_rapl & RAPL_PKG)
297 outp += sprintf(outp, " Pkg_W");
298 if (do_rapl & RAPL_CORES)
299 outp += sprintf(outp, " Cor_W");
300 if (do_rapl & RAPL_GFX)
301 outp += sprintf(outp, " GFX_W");
302 if (do_rapl & RAPL_DRAM)
303 outp += sprintf(outp, " RAM_W");
304 if (do_rapl & RAPL_PKG_PERF_STATUS)
305 outp += sprintf(outp, " PKG_%%");
306 if (do_rapl & RAPL_DRAM_PERF_STATUS)
307 outp += sprintf(outp, " RAM_%%");
309 outp += sprintf(outp, "\n");
312 int dump_counters(struct thread_data *t, struct core_data *c,
313 struct pkg_data *p)
315 fprintf(stderr, "t %p, c %p, p %p\n", t, c, p);
317 if (t) {
318 fprintf(stderr, "CPU: %d flags 0x%x\n", t->cpu_id, t->flags);
319 fprintf(stderr, "TSC: %016llX\n", t->tsc);
320 fprintf(stderr, "aperf: %016llX\n", t->aperf);
321 fprintf(stderr, "mperf: %016llX\n", t->mperf);
322 fprintf(stderr, "c1: %016llX\n", t->c1);
323 fprintf(stderr, "msr0x%x: %08llX\n",
324 extra_delta_offset32, t->extra_delta32);
325 fprintf(stderr, "msr0x%x: %016llX\n",
326 extra_delta_offset64, t->extra_delta64);
327 fprintf(stderr, "msr0x%x: %08llX\n",
328 extra_msr_offset32, t->extra_msr32);
329 fprintf(stderr, "msr0x%x: %016llX\n",
330 extra_msr_offset64, t->extra_msr64);
331 if (do_smi)
332 fprintf(stderr, "SMI: %08X\n", t->smi_count);
335 if (c) {
336 fprintf(stderr, "core: %d\n", c->core_id);
337 fprintf(stderr, "c3: %016llX\n", c->c3);
338 fprintf(stderr, "c6: %016llX\n", c->c6);
339 fprintf(stderr, "c7: %016llX\n", c->c7);
340 fprintf(stderr, "DTS: %dC\n", c->core_temp_c);
343 if (p) {
344 fprintf(stderr, "package: %d\n", p->package_id);
345 fprintf(stderr, "pc2: %016llX\n", p->pc2);
346 fprintf(stderr, "pc3: %016llX\n", p->pc3);
347 fprintf(stderr, "pc6: %016llX\n", p->pc6);
348 fprintf(stderr, "pc7: %016llX\n", p->pc7);
349 fprintf(stderr, "pc8: %016llX\n", p->pc8);
350 fprintf(stderr, "pc9: %016llX\n", p->pc9);
351 fprintf(stderr, "pc10: %016llX\n", p->pc10);
352 fprintf(stderr, "Joules PKG: %0X\n", p->energy_pkg);
353 fprintf(stderr, "Joules COR: %0X\n", p->energy_cores);
354 fprintf(stderr, "Joules GFX: %0X\n", p->energy_gfx);
355 fprintf(stderr, "Joules RAM: %0X\n", p->energy_dram);
356 fprintf(stderr, "Throttle PKG: %0X\n", p->rapl_pkg_perf_status);
357 fprintf(stderr, "Throttle RAM: %0X\n", p->rapl_dram_perf_status);
358 fprintf(stderr, "PTM: %dC\n", p->pkg_temp_c);
360 return 0;
364 * column formatting convention & formats
365 * package: "pk" 2 columns %2d
366 * core: "cor" 3 columns %3d
367 * CPU: "CPU" 3 columns %3d
368 * Pkg_W: %6.2
369 * Cor_W: %6.2
370 * GFX_W: %5.2
371 * RAM_W: %5.2
372 * GHz: "GHz" 3 columns %3.2
373 * TSC: "TSC" 3 columns %3.2
374 * SMI: "SMI" 4 columns %4d
375 * percentage " %pc3" %6.2
376 * Perf Status percentage: %5.2
377 * "CTMP" 4 columns %4d
379 int format_counters(struct thread_data *t, struct core_data *c,
380 struct pkg_data *p)
382 double interval_float;
383 char *fmt5, *fmt6;
385 /* if showing only 1st thread in core and this isn't one, bail out */
386 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
387 return 0;
389 /* if showing only 1st thread in pkg and this isn't one, bail out */
390 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
391 return 0;
393 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
395 /* topo columns, print blanks on 1st (average) line */
396 if (t == &average.threads) {
397 if (show_pkg)
398 outp += sprintf(outp, " ");
399 if (show_pkg && show_core)
400 outp += sprintf(outp, " ");
401 if (show_core)
402 outp += sprintf(outp, " ");
403 if (show_cpu)
404 outp += sprintf(outp, " " " ");
405 } else {
406 if (show_pkg) {
407 if (p)
408 outp += sprintf(outp, "%2d", p->package_id);
409 else
410 outp += sprintf(outp, " ");
412 if (show_pkg && show_core)
413 outp += sprintf(outp, " ");
414 if (show_core) {
415 if (c)
416 outp += sprintf(outp, "%3d", c->core_id);
417 else
418 outp += sprintf(outp, " ");
420 if (show_cpu)
421 outp += sprintf(outp, " %3d", t->cpu_id);
423 /* %c0 */
424 if (do_nhm_cstates) {
425 if (show_pkg || show_core || show_cpu)
426 outp += sprintf(outp, " ");
427 if (!skip_c0)
428 outp += sprintf(outp, "%6.2f", 100.0 * t->mperf/t->tsc);
429 else
430 outp += sprintf(outp, " ****");
433 /* GHz */
434 if (has_aperf) {
435 if (!aperf_mperf_unstable) {
436 outp += sprintf(outp, " %3.2f",
437 1.0 * t->tsc / units * t->aperf /
438 t->mperf / interval_float);
439 } else {
440 if (t->aperf > t->tsc || t->mperf > t->tsc) {
441 outp += sprintf(outp, " ***");
442 } else {
443 outp += sprintf(outp, "%3.1f*",
444 1.0 * t->tsc /
445 units * t->aperf /
446 t->mperf / interval_float);
451 /* TSC */
452 outp += sprintf(outp, "%5.2f", 1.0 * t->tsc/units/interval_float);
454 /* SMI */
455 if (do_smi)
456 outp += sprintf(outp, "%4d", t->smi_count);
458 /* delta */
459 if (extra_delta_offset32)
460 outp += sprintf(outp, " %11llu", t->extra_delta32);
462 /* DELTA */
463 if (extra_delta_offset64)
464 outp += sprintf(outp, " %11llu", t->extra_delta64);
465 /* msr */
466 if (extra_msr_offset32)
467 outp += sprintf(outp, " 0x%08llx", t->extra_msr32);
469 /* MSR */
470 if (extra_msr_offset64)
471 outp += sprintf(outp, " 0x%016llx", t->extra_msr64);
473 if (do_nhm_cstates) {
474 if (!skip_c1)
475 outp += sprintf(outp, " %6.2f", 100.0 * t->c1/t->tsc);
476 else
477 outp += sprintf(outp, " ****");
480 /* print per-core data only for 1st thread in core */
481 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
482 goto done;
484 if (do_nhm_cstates)
485 outp += sprintf(outp, " %6.2f", 100.0 * c->c3/t->tsc);
486 if (do_nhm_cstates)
487 outp += sprintf(outp, " %6.2f", 100.0 * c->c6/t->tsc);
488 if (do_snb_cstates)
489 outp += sprintf(outp, " %6.2f", 100.0 * c->c7/t->tsc);
491 if (do_dts)
492 outp += sprintf(outp, " %4d", c->core_temp_c);
494 /* print per-package data only for 1st core in package */
495 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
496 goto done;
498 if (do_ptm)
499 outp += sprintf(outp, " %4d", p->pkg_temp_c);
501 if (do_snb_cstates)
502 outp += sprintf(outp, " %6.2f", 100.0 * p->pc2/t->tsc);
503 if (do_nhm_cstates)
504 outp += sprintf(outp, " %6.2f", 100.0 * p->pc3/t->tsc);
505 if (do_nhm_cstates)
506 outp += sprintf(outp, " %6.2f", 100.0 * p->pc6/t->tsc);
507 if (do_snb_cstates)
508 outp += sprintf(outp, " %6.2f", 100.0 * p->pc7/t->tsc);
509 if (do_c8_c9_c10) {
510 outp += sprintf(outp, " %6.2f", 100.0 * p->pc8/t->tsc);
511 outp += sprintf(outp, " %6.2f", 100.0 * p->pc9/t->tsc);
512 outp += sprintf(outp, " %6.2f", 100.0 * p->pc10/t->tsc);
516 * If measurement interval exceeds minimum RAPL Joule Counter range,
517 * indicate that results are suspect by printing "**" in fraction place.
519 if (interval_float < rapl_joule_counter_range) {
520 fmt5 = " %5.2f";
521 fmt6 = " %6.2f";
522 } else {
523 fmt5 = " %3.0f**";
524 fmt6 = " %4.0f**";
527 if (do_rapl & RAPL_PKG)
528 outp += sprintf(outp, fmt6, p->energy_pkg * rapl_energy_units / interval_float);
529 if (do_rapl & RAPL_CORES)
530 outp += sprintf(outp, fmt6, p->energy_cores * rapl_energy_units / interval_float);
531 if (do_rapl & RAPL_GFX)
532 outp += sprintf(outp, fmt5, p->energy_gfx * rapl_energy_units / interval_float);
533 if (do_rapl & RAPL_DRAM)
534 outp += sprintf(outp, fmt5, p->energy_dram * rapl_energy_units / interval_float);
535 if (do_rapl & RAPL_PKG_PERF_STATUS )
536 outp += sprintf(outp, fmt5, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
537 if (do_rapl & RAPL_DRAM_PERF_STATUS )
538 outp += sprintf(outp, fmt5, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
540 done:
541 outp += sprintf(outp, "\n");
543 return 0;
546 void flush_stdout()
548 fputs(output_buffer, stdout);
549 fflush(stdout);
550 outp = output_buffer;
552 void flush_stderr()
554 fputs(output_buffer, stderr);
555 outp = output_buffer;
557 void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
559 static int printed;
561 if (!printed || !summary_only)
562 print_header();
564 if (topo.num_cpus > 1)
565 format_counters(&average.threads, &average.cores,
566 &average.packages);
568 printed = 1;
570 if (summary_only)
571 return;
573 for_all_cpus(format_counters, t, c, p);
576 #define DELTA_WRAP32(new, old) \
577 if (new > old) { \
578 old = new - old; \
579 } else { \
580 old = 0x100000000 + new - old; \
583 void
584 delta_package(struct pkg_data *new, struct pkg_data *old)
586 old->pc2 = new->pc2 - old->pc2;
587 old->pc3 = new->pc3 - old->pc3;
588 old->pc6 = new->pc6 - old->pc6;
589 old->pc7 = new->pc7 - old->pc7;
590 old->pc8 = new->pc8 - old->pc8;
591 old->pc9 = new->pc9 - old->pc9;
592 old->pc10 = new->pc10 - old->pc10;
593 old->pkg_temp_c = new->pkg_temp_c;
595 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
596 DELTA_WRAP32(new->energy_cores, old->energy_cores);
597 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
598 DELTA_WRAP32(new->energy_dram, old->energy_dram);
599 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
600 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
603 void
604 delta_core(struct core_data *new, struct core_data *old)
606 old->c3 = new->c3 - old->c3;
607 old->c6 = new->c6 - old->c6;
608 old->c7 = new->c7 - old->c7;
609 old->core_temp_c = new->core_temp_c;
613 * old = new - old
615 void
616 delta_thread(struct thread_data *new, struct thread_data *old,
617 struct core_data *core_delta)
619 old->tsc = new->tsc - old->tsc;
621 /* check for TSC < 1 Mcycles over interval */
622 if (old->tsc < (1000 * 1000)) {
623 fprintf(stderr, "Insanely slow TSC rate, TSC stops in idle?\n");
624 fprintf(stderr, "You can disable all c-states by booting with \"idle=poll\"\n");
625 fprintf(stderr, "or just the deep ones with \"processor.max_cstate=1\"\n");
626 exit(-3);
629 old->c1 = new->c1 - old->c1;
631 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
632 old->aperf = new->aperf - old->aperf;
633 old->mperf = new->mperf - old->mperf;
634 } else {
636 if (!aperf_mperf_unstable) {
637 fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname);
638 fprintf(stderr, "* Frequency results do not cover entire interval *\n");
639 fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n");
641 aperf_mperf_unstable = 1;
644 * mperf delta is likely a huge "positive" number
645 * can not use it for calculating c0 time
647 skip_c0 = 1;
648 skip_c1 = 1;
653 * As counter collection is not atomic,
654 * it is possible for mperf's non-halted cycles + idle states
655 * to exceed TSC's all cycles: show c1 = 0% in that case.
657 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
658 old->c1 = 0;
659 else {
660 /* normal case, derive c1 */
661 old->c1 = old->tsc - old->mperf - core_delta->c3
662 - core_delta->c6 - core_delta->c7;
665 if (old->mperf == 0) {
666 if (verbose > 1) fprintf(stderr, "cpu%d MPERF 0!\n", old->cpu_id);
667 old->mperf = 1; /* divide by 0 protection */
670 old->extra_delta32 = new->extra_delta32 - old->extra_delta32;
671 old->extra_delta32 &= 0xFFFFFFFF;
673 old->extra_delta64 = new->extra_delta64 - old->extra_delta64;
676 * Extra MSR is just a snapshot, simply copy latest w/o subtracting
678 old->extra_msr32 = new->extra_msr32;
679 old->extra_msr64 = new->extra_msr64;
681 if (do_smi)
682 old->smi_count = new->smi_count - old->smi_count;
685 int delta_cpu(struct thread_data *t, struct core_data *c,
686 struct pkg_data *p, struct thread_data *t2,
687 struct core_data *c2, struct pkg_data *p2)
689 /* calculate core delta only for 1st thread in core */
690 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
691 delta_core(c, c2);
693 /* always calculate thread delta */
694 delta_thread(t, t2, c2); /* c2 is core delta */
696 /* calculate package delta only for 1st core in package */
697 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
698 delta_package(p, p2);
700 return 0;
703 void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
705 t->tsc = 0;
706 t->aperf = 0;
707 t->mperf = 0;
708 t->c1 = 0;
710 t->smi_count = 0;
711 t->extra_delta32 = 0;
712 t->extra_delta64 = 0;
714 /* tells format_counters to dump all fields from this set */
715 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
717 c->c3 = 0;
718 c->c6 = 0;
719 c->c7 = 0;
720 c->core_temp_c = 0;
722 p->pc2 = 0;
723 p->pc3 = 0;
724 p->pc6 = 0;
725 p->pc7 = 0;
726 p->pc8 = 0;
727 p->pc9 = 0;
728 p->pc10 = 0;
730 p->energy_pkg = 0;
731 p->energy_dram = 0;
732 p->energy_cores = 0;
733 p->energy_gfx = 0;
734 p->rapl_pkg_perf_status = 0;
735 p->rapl_dram_perf_status = 0;
736 p->pkg_temp_c = 0;
738 int sum_counters(struct thread_data *t, struct core_data *c,
739 struct pkg_data *p)
741 average.threads.tsc += t->tsc;
742 average.threads.aperf += t->aperf;
743 average.threads.mperf += t->mperf;
744 average.threads.c1 += t->c1;
746 average.threads.extra_delta32 += t->extra_delta32;
747 average.threads.extra_delta64 += t->extra_delta64;
749 /* sum per-core values only for 1st thread in core */
750 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
751 return 0;
753 average.cores.c3 += c->c3;
754 average.cores.c6 += c->c6;
755 average.cores.c7 += c->c7;
757 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
759 /* sum per-pkg values only for 1st core in pkg */
760 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
761 return 0;
763 average.packages.pc2 += p->pc2;
764 average.packages.pc3 += p->pc3;
765 average.packages.pc6 += p->pc6;
766 average.packages.pc7 += p->pc7;
767 average.packages.pc8 += p->pc8;
768 average.packages.pc9 += p->pc9;
769 average.packages.pc10 += p->pc10;
771 average.packages.energy_pkg += p->energy_pkg;
772 average.packages.energy_dram += p->energy_dram;
773 average.packages.energy_cores += p->energy_cores;
774 average.packages.energy_gfx += p->energy_gfx;
776 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
778 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
779 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
780 return 0;
783 * sum the counters for all cpus in the system
784 * compute the weighted average
786 void compute_average(struct thread_data *t, struct core_data *c,
787 struct pkg_data *p)
789 clear_counters(&average.threads, &average.cores, &average.packages);
791 for_all_cpus(sum_counters, t, c, p);
793 average.threads.tsc /= topo.num_cpus;
794 average.threads.aperf /= topo.num_cpus;
795 average.threads.mperf /= topo.num_cpus;
796 average.threads.c1 /= topo.num_cpus;
798 average.threads.extra_delta32 /= topo.num_cpus;
799 average.threads.extra_delta32 &= 0xFFFFFFFF;
801 average.threads.extra_delta64 /= topo.num_cpus;
803 average.cores.c3 /= topo.num_cores;
804 average.cores.c6 /= topo.num_cores;
805 average.cores.c7 /= topo.num_cores;
807 average.packages.pc2 /= topo.num_packages;
808 average.packages.pc3 /= topo.num_packages;
809 average.packages.pc6 /= topo.num_packages;
810 average.packages.pc7 /= topo.num_packages;
812 average.packages.pc8 /= topo.num_packages;
813 average.packages.pc9 /= topo.num_packages;
814 average.packages.pc10 /= topo.num_packages;
817 static unsigned long long rdtsc(void)
819 unsigned int low, high;
821 asm volatile("rdtsc" : "=a" (low), "=d" (high));
823 return low | ((unsigned long long)high) << 32;
828 * get_counters(...)
829 * migrate to cpu
830 * acquire and record local counters for that cpu
832 int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
834 int cpu = t->cpu_id;
835 unsigned long long msr;
837 if (cpu_migrate(cpu)) {
838 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
839 return -1;
842 t->tsc = rdtsc(); /* we are running on local CPU of interest */
844 if (has_aperf) {
845 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
846 return -3;
847 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
848 return -4;
851 if (do_smi) {
852 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
853 return -5;
854 t->smi_count = msr & 0xFFFFFFFF;
856 if (extra_delta_offset32) {
857 if (get_msr(cpu, extra_delta_offset32, &msr))
858 return -5;
859 t->extra_delta32 = msr & 0xFFFFFFFF;
862 if (extra_delta_offset64)
863 if (get_msr(cpu, extra_delta_offset64, &t->extra_delta64))
864 return -5;
866 if (extra_msr_offset32) {
867 if (get_msr(cpu, extra_msr_offset32, &msr))
868 return -5;
869 t->extra_msr32 = msr & 0xFFFFFFFF;
872 if (extra_msr_offset64)
873 if (get_msr(cpu, extra_msr_offset64, &t->extra_msr64))
874 return -5;
876 /* collect core counters only for 1st thread in core */
877 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
878 return 0;
880 if (do_nhm_cstates) {
881 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
882 return -6;
883 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
884 return -7;
887 if (do_snb_cstates)
888 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
889 return -8;
891 if (do_dts) {
892 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
893 return -9;
894 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
898 /* collect package counters only for 1st core in package */
899 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
900 return 0;
902 if (do_nhm_cstates) {
903 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
904 return -9;
905 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
906 return -10;
908 if (do_snb_cstates) {
909 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
910 return -11;
911 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
912 return -12;
914 if (do_c8_c9_c10) {
915 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
916 return -13;
917 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
918 return -13;
919 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
920 return -13;
922 if (do_rapl & RAPL_PKG) {
923 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
924 return -13;
925 p->energy_pkg = msr & 0xFFFFFFFF;
927 if (do_rapl & RAPL_CORES) {
928 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
929 return -14;
930 p->energy_cores = msr & 0xFFFFFFFF;
932 if (do_rapl & RAPL_DRAM) {
933 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
934 return -15;
935 p->energy_dram = msr & 0xFFFFFFFF;
937 if (do_rapl & RAPL_GFX) {
938 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
939 return -16;
940 p->energy_gfx = msr & 0xFFFFFFFF;
942 if (do_rapl & RAPL_PKG_PERF_STATUS) {
943 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
944 return -16;
945 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
947 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
948 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
949 return -16;
950 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
952 if (do_ptm) {
953 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
954 return -17;
955 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
957 return 0;
960 void print_verbose_header(void)
962 unsigned long long msr;
963 unsigned int ratio;
965 if (!do_nehalem_platform_info)
966 return;
968 get_msr(0, MSR_NHM_PLATFORM_INFO, &msr);
970 fprintf(stderr, "cpu0: MSR_NHM_PLATFORM_INFO: 0x%08llx\n", msr);
972 ratio = (msr >> 40) & 0xFF;
973 fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency\n",
974 ratio, bclk, ratio * bclk);
976 ratio = (msr >> 8) & 0xFF;
977 fprintf(stderr, "%d * %.0f = %.0f MHz TSC frequency\n",
978 ratio, bclk, ratio * bclk);
980 get_msr(0, MSR_IA32_POWER_CTL, &msr);
981 fprintf(stderr, "cpu0: MSR_IA32_POWER_CTL: 0x%08llx (C1E: %sabled)\n",
982 msr, msr & 0x2 ? "EN" : "DIS");
984 if (!do_ivt_turbo_ratio_limit)
985 goto print_nhm_turbo_ratio_limits;
987 get_msr(0, MSR_IVT_TURBO_RATIO_LIMIT, &msr);
989 fprintf(stderr, "cpu0: MSR_IVT_TURBO_RATIO_LIMIT: 0x%08llx\n", msr);
991 ratio = (msr >> 56) & 0xFF;
992 if (ratio)
993 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 16 active cores\n",
994 ratio, bclk, ratio * bclk);
996 ratio = (msr >> 48) & 0xFF;
997 if (ratio)
998 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 15 active cores\n",
999 ratio, bclk, ratio * bclk);
1001 ratio = (msr >> 40) & 0xFF;
1002 if (ratio)
1003 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 14 active cores\n",
1004 ratio, bclk, ratio * bclk);
1006 ratio = (msr >> 32) & 0xFF;
1007 if (ratio)
1008 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 13 active cores\n",
1009 ratio, bclk, ratio * bclk);
1011 ratio = (msr >> 24) & 0xFF;
1012 if (ratio)
1013 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 12 active cores\n",
1014 ratio, bclk, ratio * bclk);
1016 ratio = (msr >> 16) & 0xFF;
1017 if (ratio)
1018 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 11 active cores\n",
1019 ratio, bclk, ratio * bclk);
1021 ratio = (msr >> 8) & 0xFF;
1022 if (ratio)
1023 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 10 active cores\n",
1024 ratio, bclk, ratio * bclk);
1026 ratio = (msr >> 0) & 0xFF;
1027 if (ratio)
1028 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 9 active cores\n",
1029 ratio, bclk, ratio * bclk);
1031 print_nhm_turbo_ratio_limits:
1032 get_msr(0, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
1034 #define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
1035 #define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
1037 fprintf(stderr, "cpu0: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", msr);
1039 fprintf(stderr, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: ",
1040 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
1041 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
1042 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
1043 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
1044 (msr & (1 << 15)) ? "" : "UN",
1045 (unsigned int)msr & 7);
1048 switch(msr & 0x7) {
1049 case 0:
1050 fprintf(stderr, "pc0");
1051 break;
1052 case 1:
1053 fprintf(stderr, do_snb_cstates ? "pc2" : "pc0");
1054 break;
1055 case 2:
1056 fprintf(stderr, do_snb_cstates ? "pc6-noret" : "pc3");
1057 break;
1058 case 3:
1059 fprintf(stderr, "pc6");
1060 break;
1061 case 4:
1062 fprintf(stderr, "pc7");
1063 break;
1064 case 5:
1065 fprintf(stderr, do_snb_cstates ? "pc7s" : "invalid");
1066 break;
1067 case 7:
1068 fprintf(stderr, "unlimited");
1069 break;
1070 default:
1071 fprintf(stderr, "invalid");
1073 fprintf(stderr, ")\n");
1075 if (!do_nehalem_turbo_ratio_limit)
1076 return;
1078 get_msr(0, MSR_NHM_TURBO_RATIO_LIMIT, &msr);
1080 fprintf(stderr, "cpu0: MSR_NHM_TURBO_RATIO_LIMIT: 0x%08llx\n", msr);
1082 ratio = (msr >> 56) & 0xFF;
1083 if (ratio)
1084 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 8 active cores\n",
1085 ratio, bclk, ratio * bclk);
1087 ratio = (msr >> 48) & 0xFF;
1088 if (ratio)
1089 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 7 active cores\n",
1090 ratio, bclk, ratio * bclk);
1092 ratio = (msr >> 40) & 0xFF;
1093 if (ratio)
1094 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 6 active cores\n",
1095 ratio, bclk, ratio * bclk);
1097 ratio = (msr >> 32) & 0xFF;
1098 if (ratio)
1099 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 5 active cores\n",
1100 ratio, bclk, ratio * bclk);
1102 ratio = (msr >> 24) & 0xFF;
1103 if (ratio)
1104 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
1105 ratio, bclk, ratio * bclk);
1107 ratio = (msr >> 16) & 0xFF;
1108 if (ratio)
1109 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
1110 ratio, bclk, ratio * bclk);
1112 ratio = (msr >> 8) & 0xFF;
1113 if (ratio)
1114 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
1115 ratio, bclk, ratio * bclk);
1117 ratio = (msr >> 0) & 0xFF;
1118 if (ratio)
1119 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
1120 ratio, bclk, ratio * bclk);
1123 void free_all_buffers(void)
1125 CPU_FREE(cpu_present_set);
1126 cpu_present_set = NULL;
1127 cpu_present_set = 0;
1129 CPU_FREE(cpu_affinity_set);
1130 cpu_affinity_set = NULL;
1131 cpu_affinity_setsize = 0;
1133 free(thread_even);
1134 free(core_even);
1135 free(package_even);
1137 thread_even = NULL;
1138 core_even = NULL;
1139 package_even = NULL;
1141 free(thread_odd);
1142 free(core_odd);
1143 free(package_odd);
1145 thread_odd = NULL;
1146 core_odd = NULL;
1147 package_odd = NULL;
1149 free(output_buffer);
1150 output_buffer = NULL;
1151 outp = NULL;
1155 * cpu_is_first_sibling_in_core(cpu)
1156 * return 1 if given CPU is 1st HT sibling in the core
1158 int cpu_is_first_sibling_in_core(int cpu)
1160 char path[64];
1161 FILE *filep;
1162 int first_cpu;
1164 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
1165 filep = fopen(path, "r");
1166 if (filep == NULL) {
1167 perror(path);
1168 exit(1);
1170 fscanf(filep, "%d", &first_cpu);
1171 fclose(filep);
1172 return (cpu == first_cpu);
1176 * cpu_is_first_core_in_package(cpu)
1177 * return 1 if given CPU is 1st core in package
1179 int cpu_is_first_core_in_package(int cpu)
1181 char path[64];
1182 FILE *filep;
1183 int first_cpu;
1185 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
1186 filep = fopen(path, "r");
1187 if (filep == NULL) {
1188 perror(path);
1189 exit(1);
1191 fscanf(filep, "%d", &first_cpu);
1192 fclose(filep);
1193 return (cpu == first_cpu);
1196 int get_physical_package_id(int cpu)
1198 char path[80];
1199 FILE *filep;
1200 int pkg;
1202 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
1203 filep = fopen(path, "r");
1204 if (filep == NULL) {
1205 perror(path);
1206 exit(1);
1208 fscanf(filep, "%d", &pkg);
1209 fclose(filep);
1210 return pkg;
1213 int get_core_id(int cpu)
1215 char path[80];
1216 FILE *filep;
1217 int core;
1219 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
1220 filep = fopen(path, "r");
1221 if (filep == NULL) {
1222 perror(path);
1223 exit(1);
1225 fscanf(filep, "%d", &core);
1226 fclose(filep);
1227 return core;
1230 int get_num_ht_siblings(int cpu)
1232 char path[80];
1233 FILE *filep;
1234 int sib1, sib2;
1235 int matches;
1236 char character;
1238 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
1239 filep = fopen(path, "r");
1240 if (filep == NULL) {
1241 perror(path);
1242 exit(1);
1245 * file format:
1246 * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4)
1247 * otherwinse 1 sibling (self).
1249 matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2);
1251 fclose(filep);
1253 if (matches == 3)
1254 return 2;
1255 else
1256 return 1;
1260 * run func(thread, core, package) in topology order
1261 * skip non-present cpus
1264 int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
1265 struct pkg_data *, struct thread_data *, struct core_data *,
1266 struct pkg_data *), struct thread_data *thread_base,
1267 struct core_data *core_base, struct pkg_data *pkg_base,
1268 struct thread_data *thread_base2, struct core_data *core_base2,
1269 struct pkg_data *pkg_base2)
1271 int retval, pkg_no, core_no, thread_no;
1273 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
1274 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
1275 for (thread_no = 0; thread_no <
1276 topo.num_threads_per_core; ++thread_no) {
1277 struct thread_data *t, *t2;
1278 struct core_data *c, *c2;
1279 struct pkg_data *p, *p2;
1281 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
1283 if (cpu_is_not_present(t->cpu_id))
1284 continue;
1286 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
1288 c = GET_CORE(core_base, core_no, pkg_no);
1289 c2 = GET_CORE(core_base2, core_no, pkg_no);
1291 p = GET_PKG(pkg_base, pkg_no);
1292 p2 = GET_PKG(pkg_base2, pkg_no);
1294 retval = func(t, c, p, t2, c2, p2);
1295 if (retval)
1296 return retval;
1300 return 0;
1304 * run func(cpu) on every cpu in /proc/stat
1305 * return max_cpu number
1307 int for_all_proc_cpus(int (func)(int))
1309 FILE *fp;
1310 int cpu_num;
1311 int retval;
1313 fp = fopen(proc_stat, "r");
1314 if (fp == NULL) {
1315 perror(proc_stat);
1316 exit(1);
1319 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
1320 if (retval != 0) {
1321 perror("/proc/stat format");
1322 exit(1);
1325 while (1) {
1326 retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
1327 if (retval != 1)
1328 break;
1330 retval = func(cpu_num);
1331 if (retval) {
1332 fclose(fp);
1333 return(retval);
1336 fclose(fp);
1337 return 0;
1340 void re_initialize(void)
1342 free_all_buffers();
1343 setup_all_buffers();
1344 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
1349 * count_cpus()
1350 * remember the last one seen, it will be the max
1352 int count_cpus(int cpu)
1354 if (topo.max_cpu_num < cpu)
1355 topo.max_cpu_num = cpu;
1357 topo.num_cpus += 1;
1358 return 0;
1360 int mark_cpu_present(int cpu)
1362 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
1363 return 0;
1366 void turbostat_loop()
1368 int retval;
1369 int restarted = 0;
1371 restart:
1372 restarted++;
1374 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
1375 if (retval < -1) {
1376 exit(retval);
1377 } else if (retval == -1) {
1378 if (restarted > 1) {
1379 exit(retval);
1381 re_initialize();
1382 goto restart;
1384 restarted = 0;
1385 gettimeofday(&tv_even, (struct timezone *)NULL);
1387 while (1) {
1388 if (for_all_proc_cpus(cpu_is_not_present)) {
1389 re_initialize();
1390 goto restart;
1392 sleep(interval_sec);
1393 retval = for_all_cpus(get_counters, ODD_COUNTERS);
1394 if (retval < -1) {
1395 exit(retval);
1396 } else if (retval == -1) {
1397 re_initialize();
1398 goto restart;
1400 gettimeofday(&tv_odd, (struct timezone *)NULL);
1401 timersub(&tv_odd, &tv_even, &tv_delta);
1402 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
1403 compute_average(EVEN_COUNTERS);
1404 format_all_counters(EVEN_COUNTERS);
1405 flush_stdout();
1406 sleep(interval_sec);
1407 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
1408 if (retval < -1) {
1409 exit(retval);
1410 } else if (retval == -1) {
1411 re_initialize();
1412 goto restart;
1414 gettimeofday(&tv_even, (struct timezone *)NULL);
1415 timersub(&tv_even, &tv_odd, &tv_delta);
1416 for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS);
1417 compute_average(ODD_COUNTERS);
1418 format_all_counters(ODD_COUNTERS);
1419 flush_stdout();
1423 void check_dev_msr()
1425 struct stat sb;
1427 if (stat("/dev/cpu/0/msr", &sb)) {
1428 fprintf(stderr, "no /dev/cpu/0/msr\n");
1429 fprintf(stderr, "Try \"# modprobe msr\"\n");
1430 exit(-5);
1434 void check_super_user()
1436 if (getuid() != 0) {
1437 fprintf(stderr, "must be root\n");
1438 exit(-6);
1442 int has_nehalem_turbo_ratio_limit(unsigned int family, unsigned int model)
1444 if (!genuine_intel)
1445 return 0;
1447 if (family != 6)
1448 return 0;
1450 switch (model) {
1451 case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
1452 case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
1453 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
1454 case 0x25: /* Westmere Client - Clarkdale, Arrandale */
1455 case 0x2C: /* Westmere EP - Gulftown */
1456 case 0x2A: /* SNB */
1457 case 0x2D: /* SNB Xeon */
1458 case 0x3A: /* IVB */
1459 case 0x3E: /* IVB Xeon */
1460 case 0x3C: /* HSW */
1461 case 0x3F: /* HSW */
1462 case 0x45: /* HSW */
1463 case 0x46: /* HSW */
1464 return 1;
1465 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1466 case 0x2F: /* Westmere-EX Xeon - Eagleton */
1467 default:
1468 return 0;
1471 int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
1473 if (!genuine_intel)
1474 return 0;
1476 if (family != 6)
1477 return 0;
1479 switch (model) {
1480 case 0x3E: /* IVB Xeon */
1481 return 1;
1482 default:
1483 return 0;
1488 * print_epb()
1489 * Decode the ENERGY_PERF_BIAS MSR
1491 int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1493 unsigned long long msr;
1494 char *epb_string;
1495 int cpu;
1497 if (!has_epb)
1498 return 0;
1500 cpu = t->cpu_id;
1502 /* EPB is per-package */
1503 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1504 return 0;
1506 if (cpu_migrate(cpu)) {
1507 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1508 return -1;
1511 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
1512 return 0;
1514 switch (msr & 0x7) {
1515 case ENERGY_PERF_BIAS_PERFORMANCE:
1516 epb_string = "performance";
1517 break;
1518 case ENERGY_PERF_BIAS_NORMAL:
1519 epb_string = "balanced";
1520 break;
1521 case ENERGY_PERF_BIAS_POWERSAVE:
1522 epb_string = "powersave";
1523 break;
1524 default:
1525 epb_string = "custom";
1526 break;
1528 fprintf(stderr, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
1530 return 0;
1533 #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
1534 #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
1537 * rapl_probe()
1539 * sets do_rapl
1541 void rapl_probe(unsigned int family, unsigned int model)
1543 unsigned long long msr;
1544 double tdp;
1546 if (!genuine_intel)
1547 return;
1549 if (family != 6)
1550 return;
1552 switch (model) {
1553 case 0x2A:
1554 case 0x3A:
1555 case 0x3C: /* HSW */
1556 case 0x3F: /* HSW */
1557 case 0x45: /* HSW */
1558 case 0x46: /* HSW */
1559 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_GFX;
1560 break;
1561 case 0x2D:
1562 case 0x3E:
1563 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_DRAM | RAPL_PKG_PERF_STATUS | RAPL_DRAM_PERF_STATUS;
1564 break;
1565 default:
1566 return;
1569 /* units on package 0, verify later other packages match */
1570 if (get_msr(0, MSR_RAPL_POWER_UNIT, &msr))
1571 return;
1573 rapl_power_units = 1.0 / (1 << (msr & 0xF));
1574 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
1575 rapl_time_units = 1.0 / (1 << (msr >> 16 & 0xF));
1577 /* get TDP to determine energy counter range */
1578 if (get_msr(0, MSR_PKG_POWER_INFO, &msr))
1579 return;
1581 tdp = ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
1583 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
1585 if (verbose)
1586 fprintf(stderr, "RAPL: %.0f sec. Joule Counter Range\n", rapl_joule_counter_range);
1588 return;
1591 int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1593 unsigned long long msr;
1594 unsigned int dts;
1595 int cpu;
1597 if (!(do_dts || do_ptm))
1598 return 0;
1600 cpu = t->cpu_id;
1602 /* DTS is per-core, no need to print for each thread */
1603 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1604 return 0;
1606 if (cpu_migrate(cpu)) {
1607 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1608 return -1;
1611 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
1612 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1613 return 0;
1615 dts = (msr >> 16) & 0x7F;
1616 fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
1617 cpu, msr, tcc_activation_temp - dts);
1619 #ifdef THERM_DEBUG
1620 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
1621 return 0;
1623 dts = (msr >> 16) & 0x7F;
1624 dts2 = (msr >> 8) & 0x7F;
1625 fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
1626 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
1627 #endif
1631 if (do_dts) {
1632 unsigned int resolution;
1634 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1635 return 0;
1637 dts = (msr >> 16) & 0x7F;
1638 resolution = (msr >> 27) & 0xF;
1639 fprintf(stderr, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
1640 cpu, msr, tcc_activation_temp - dts, resolution);
1642 #ifdef THERM_DEBUG
1643 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
1644 return 0;
1646 dts = (msr >> 16) & 0x7F;
1647 dts2 = (msr >> 8) & 0x7F;
1648 fprintf(stderr, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
1649 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
1650 #endif
1653 return 0;
1656 void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
1658 fprintf(stderr, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
1659 cpu, label,
1660 ((msr >> 15) & 1) ? "EN" : "DIS",
1661 ((msr >> 0) & 0x7FFF) * rapl_power_units,
1662 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
1663 (((msr >> 16) & 1) ? "EN" : "DIS"));
1665 return;
1668 int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1670 unsigned long long msr;
1671 int cpu;
1672 double local_rapl_power_units, local_rapl_energy_units, local_rapl_time_units;
1674 if (!do_rapl)
1675 return 0;
1677 /* RAPL counters are per package, so print only for 1st thread/package */
1678 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1679 return 0;
1681 cpu = t->cpu_id;
1682 if (cpu_migrate(cpu)) {
1683 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1684 return -1;
1687 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
1688 return -1;
1690 local_rapl_power_units = 1.0 / (1 << (msr & 0xF));
1691 local_rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
1692 local_rapl_time_units = 1.0 / (1 << (msr >> 16 & 0xF));
1694 if (local_rapl_power_units != rapl_power_units)
1695 fprintf(stderr, "cpu%d, ERROR: Power units mis-match\n", cpu);
1696 if (local_rapl_energy_units != rapl_energy_units)
1697 fprintf(stderr, "cpu%d, ERROR: Energy units mis-match\n", cpu);
1698 if (local_rapl_time_units != rapl_time_units)
1699 fprintf(stderr, "cpu%d, ERROR: Time units mis-match\n", cpu);
1701 if (verbose) {
1702 fprintf(stderr, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx "
1703 "(%f Watts, %f Joules, %f sec.)\n", cpu, msr,
1704 local_rapl_power_units, local_rapl_energy_units, local_rapl_time_units);
1706 if (do_rapl & RAPL_PKG) {
1707 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
1708 return -5;
1711 fprintf(stderr, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
1712 cpu, msr,
1713 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1714 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1715 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1716 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
1718 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
1719 return -9;
1721 fprintf(stderr, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
1722 cpu, msr, (msr >> 63) & 1 ? "": "UN");
1724 print_power_limit_msr(cpu, msr, "PKG Limit #1");
1725 fprintf(stderr, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
1726 cpu,
1727 ((msr >> 47) & 1) ? "EN" : "DIS",
1728 ((msr >> 32) & 0x7FFF) * rapl_power_units,
1729 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
1730 ((msr >> 48) & 1) ? "EN" : "DIS");
1733 if (do_rapl & RAPL_DRAM) {
1734 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
1735 return -6;
1738 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
1739 cpu, msr,
1740 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1741 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1742 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1743 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
1746 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
1747 return -9;
1748 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
1749 cpu, msr, (msr >> 31) & 1 ? "": "UN");
1751 print_power_limit_msr(cpu, msr, "DRAM Limit");
1753 if (do_rapl & RAPL_CORES) {
1754 if (verbose) {
1755 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
1756 return -7;
1758 fprintf(stderr, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
1760 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
1761 return -9;
1762 fprintf(stderr, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
1763 cpu, msr, (msr >> 31) & 1 ? "": "UN");
1764 print_power_limit_msr(cpu, msr, "Cores Limit");
1767 if (do_rapl & RAPL_GFX) {
1768 if (verbose) {
1769 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
1770 return -8;
1772 fprintf(stderr, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
1774 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
1775 return -9;
1776 fprintf(stderr, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
1777 cpu, msr, (msr >> 31) & 1 ? "": "UN");
1778 print_power_limit_msr(cpu, msr, "GFX Limit");
1781 return 0;
1785 int is_snb(unsigned int family, unsigned int model)
1787 if (!genuine_intel)
1788 return 0;
1790 switch (model) {
1791 case 0x2A:
1792 case 0x2D:
1793 case 0x3A: /* IVB */
1794 case 0x3E: /* IVB Xeon */
1795 case 0x3C: /* HSW */
1796 case 0x3F: /* HSW */
1797 case 0x45: /* HSW */
1798 case 0x46: /* HSW */
1799 return 1;
1801 return 0;
1804 int has_c8_c9_c10(unsigned int family, unsigned int model)
1806 if (!genuine_intel)
1807 return 0;
1809 switch (model) {
1810 case 0x45:
1811 return 1;
1813 return 0;
1817 double discover_bclk(unsigned int family, unsigned int model)
1819 if (is_snb(family, model))
1820 return 100.00;
1821 else
1822 return 133.33;
1826 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
1827 * the Thermal Control Circuit (TCC) activates.
1828 * This is usually equal to tjMax.
1830 * Older processors do not have this MSR, so there we guess,
1831 * but also allow cmdline over-ride with -T.
1833 * Several MSR temperature values are in units of degrees-C
1834 * below this value, including the Digital Thermal Sensor (DTS),
1835 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
1837 int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1839 unsigned long long msr;
1840 unsigned int target_c_local;
1841 int cpu;
1843 /* tcc_activation_temp is used only for dts or ptm */
1844 if (!(do_dts || do_ptm))
1845 return 0;
1847 /* this is a per-package concept */
1848 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1849 return 0;
1851 cpu = t->cpu_id;
1852 if (cpu_migrate(cpu)) {
1853 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1854 return -1;
1857 if (tcc_activation_temp_override != 0) {
1858 tcc_activation_temp = tcc_activation_temp_override;
1859 fprintf(stderr, "cpu%d: Using cmdline TCC Target (%d C)\n",
1860 cpu, tcc_activation_temp);
1861 return 0;
1864 /* Temperature Target MSR is Nehalem and newer only */
1865 if (!do_nehalem_platform_info)
1866 goto guess;
1868 if (get_msr(0, MSR_IA32_TEMPERATURE_TARGET, &msr))
1869 goto guess;
1871 target_c_local = (msr >> 16) & 0x7F;
1873 if (verbose)
1874 fprintf(stderr, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
1875 cpu, msr, target_c_local);
1877 if (target_c_local < 85 || target_c_local > 120)
1878 goto guess;
1880 tcc_activation_temp = target_c_local;
1882 return 0;
1884 guess:
1885 tcc_activation_temp = TJMAX_DEFAULT;
1886 fprintf(stderr, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
1887 cpu, tcc_activation_temp);
1889 return 0;
1891 void check_cpuid()
1893 unsigned int eax, ebx, ecx, edx, max_level;
1894 unsigned int fms, family, model, stepping;
1896 eax = ebx = ecx = edx = 0;
1898 __get_cpuid(0, &max_level, &ebx, &ecx, &edx);
1900 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
1901 genuine_intel = 1;
1903 if (verbose)
1904 fprintf(stderr, "CPUID(0): %.4s%.4s%.4s ",
1905 (char *)&ebx, (char *)&edx, (char *)&ecx);
1907 __get_cpuid(1, &fms, &ebx, &ecx, &edx);
1908 family = (fms >> 8) & 0xf;
1909 model = (fms >> 4) & 0xf;
1910 stepping = fms & 0xf;
1911 if (family == 6 || family == 0xf)
1912 model += ((fms >> 16) & 0xf) << 4;
1914 if (verbose)
1915 fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
1916 max_level, family, model, stepping, family, model, stepping);
1918 if (!(edx & (1 << 5))) {
1919 fprintf(stderr, "CPUID: no MSR\n");
1920 exit(1);
1924 * check max extended function levels of CPUID.
1925 * This is needed to check for invariant TSC.
1926 * This check is valid for both Intel and AMD.
1928 ebx = ecx = edx = 0;
1929 __get_cpuid(0x80000000, &max_level, &ebx, &ecx, &edx);
1931 if (max_level < 0x80000007) {
1932 fprintf(stderr, "CPUID: no invariant TSC (max_level 0x%x)\n", max_level);
1933 exit(1);
1937 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
1938 * this check is valid for both Intel and AMD
1940 __get_cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
1941 has_invariant_tsc = edx & (1 << 8);
1943 if (!has_invariant_tsc) {
1944 fprintf(stderr, "No invariant TSC\n");
1945 exit(1);
1949 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
1950 * this check is valid for both Intel and AMD
1953 __get_cpuid(0x6, &eax, &ebx, &ecx, &edx);
1954 has_aperf = ecx & (1 << 0);
1955 do_dts = eax & (1 << 0);
1956 do_ptm = eax & (1 << 6);
1957 has_epb = ecx & (1 << 3);
1959 if (verbose)
1960 fprintf(stderr, "CPUID(6): %s%s%s%s\n",
1961 has_aperf ? "APERF" : "No APERF!",
1962 do_dts ? ", DTS" : "",
1963 do_ptm ? ", PTM": "",
1964 has_epb ? ", EPB": "");
1966 if (!has_aperf)
1967 exit(-1);
1969 do_nehalem_platform_info = genuine_intel && has_invariant_tsc;
1970 do_nhm_cstates = genuine_intel; /* all Intel w/ non-stop TSC have NHM counters */
1971 do_smi = do_nhm_cstates;
1972 do_snb_cstates = is_snb(family, model);
1973 do_c8_c9_c10 = has_c8_c9_c10(family, model);
1974 bclk = discover_bclk(family, model);
1976 do_nehalem_turbo_ratio_limit = has_nehalem_turbo_ratio_limit(family, model);
1977 do_ivt_turbo_ratio_limit = has_ivt_turbo_ratio_limit(family, model);
1978 rapl_probe(family, model);
1980 return;
1984 void usage()
1986 fprintf(stderr, "%s: [-v][-R][-T][-p|-P|-S][-c MSR# | -s]][-C MSR#][-m MSR#][-M MSR#][-i interval_sec | command ...]\n",
1987 progname);
1988 exit(1);
1993 * in /dev/cpu/ return success for names that are numbers
1994 * ie. filter out ".", "..", "microcode".
1996 int dir_filter(const struct dirent *dirp)
1998 if (isdigit(dirp->d_name[0]))
1999 return 1;
2000 else
2001 return 0;
2004 int open_dev_cpu_msr(int dummy1)
2006 return 0;
2009 void topology_probe()
2011 int i;
2012 int max_core_id = 0;
2013 int max_package_id = 0;
2014 int max_siblings = 0;
2015 struct cpu_topology {
2016 int core_id;
2017 int physical_package_id;
2018 } *cpus;
2020 /* Initialize num_cpus, max_cpu_num */
2021 topo.num_cpus = 0;
2022 topo.max_cpu_num = 0;
2023 for_all_proc_cpus(count_cpus);
2024 if (!summary_only && topo.num_cpus > 1)
2025 show_cpu = 1;
2027 if (verbose > 1)
2028 fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
2030 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
2031 if (cpus == NULL) {
2032 perror("calloc cpus");
2033 exit(1);
2037 * Allocate and initialize cpu_present_set
2039 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
2040 if (cpu_present_set == NULL) {
2041 perror("CPU_ALLOC");
2042 exit(3);
2044 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2045 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
2046 for_all_proc_cpus(mark_cpu_present);
2049 * Allocate and initialize cpu_affinity_set
2051 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
2052 if (cpu_affinity_set == NULL) {
2053 perror("CPU_ALLOC");
2054 exit(3);
2056 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2057 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
2061 * For online cpus
2062 * find max_core_id, max_package_id
2064 for (i = 0; i <= topo.max_cpu_num; ++i) {
2065 int siblings;
2067 if (cpu_is_not_present(i)) {
2068 if (verbose > 1)
2069 fprintf(stderr, "cpu%d NOT PRESENT\n", i);
2070 continue;
2072 cpus[i].core_id = get_core_id(i);
2073 if (cpus[i].core_id > max_core_id)
2074 max_core_id = cpus[i].core_id;
2076 cpus[i].physical_package_id = get_physical_package_id(i);
2077 if (cpus[i].physical_package_id > max_package_id)
2078 max_package_id = cpus[i].physical_package_id;
2080 siblings = get_num_ht_siblings(i);
2081 if (siblings > max_siblings)
2082 max_siblings = siblings;
2083 if (verbose > 1)
2084 fprintf(stderr, "cpu %d pkg %d core %d\n",
2085 i, cpus[i].physical_package_id, cpus[i].core_id);
2087 topo.num_cores_per_pkg = max_core_id + 1;
2088 if (verbose > 1)
2089 fprintf(stderr, "max_core_id %d, sizing for %d cores per package\n",
2090 max_core_id, topo.num_cores_per_pkg);
2091 if (!summary_only && topo.num_cores_per_pkg > 1)
2092 show_core = 1;
2094 topo.num_packages = max_package_id + 1;
2095 if (verbose > 1)
2096 fprintf(stderr, "max_package_id %d, sizing for %d packages\n",
2097 max_package_id, topo.num_packages);
2098 if (!summary_only && topo.num_packages > 1)
2099 show_pkg = 1;
2101 topo.num_threads_per_core = max_siblings;
2102 if (verbose > 1)
2103 fprintf(stderr, "max_siblings %d\n", max_siblings);
2105 free(cpus);
2108 void
2109 allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
2111 int i;
2113 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
2114 topo.num_packages, sizeof(struct thread_data));
2115 if (*t == NULL)
2116 goto error;
2118 for (i = 0; i < topo.num_threads_per_core *
2119 topo.num_cores_per_pkg * topo.num_packages; i++)
2120 (*t)[i].cpu_id = -1;
2122 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
2123 sizeof(struct core_data));
2124 if (*c == NULL)
2125 goto error;
2127 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
2128 (*c)[i].core_id = -1;
2130 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
2131 if (*p == NULL)
2132 goto error;
2134 for (i = 0; i < topo.num_packages; i++)
2135 (*p)[i].package_id = i;
2137 return;
2138 error:
2139 perror("calloc counters");
2140 exit(1);
2143 * init_counter()
2145 * set cpu_id, core_num, pkg_num
2146 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
2148 * increment topo.num_cores when 1st core in pkg seen
2150 void init_counter(struct thread_data *thread_base, struct core_data *core_base,
2151 struct pkg_data *pkg_base, int thread_num, int core_num,
2152 int pkg_num, int cpu_id)
2154 struct thread_data *t;
2155 struct core_data *c;
2156 struct pkg_data *p;
2158 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
2159 c = GET_CORE(core_base, core_num, pkg_num);
2160 p = GET_PKG(pkg_base, pkg_num);
2162 t->cpu_id = cpu_id;
2163 if (thread_num == 0) {
2164 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
2165 if (cpu_is_first_core_in_package(cpu_id))
2166 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
2169 c->core_id = core_num;
2170 p->package_id = pkg_num;
2174 int initialize_counters(int cpu_id)
2176 int my_thread_id, my_core_id, my_package_id;
2178 my_package_id = get_physical_package_id(cpu_id);
2179 my_core_id = get_core_id(cpu_id);
2181 if (cpu_is_first_sibling_in_core(cpu_id)) {
2182 my_thread_id = 0;
2183 topo.num_cores++;
2184 } else {
2185 my_thread_id = 1;
2188 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2189 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2190 return 0;
2193 void allocate_output_buffer()
2195 output_buffer = calloc(1, (1 + topo.num_cpus) * 256);
2196 outp = output_buffer;
2197 if (outp == NULL) {
2198 perror("calloc");
2199 exit(-1);
2203 void setup_all_buffers(void)
2205 topology_probe();
2206 allocate_counters(&thread_even, &core_even, &package_even);
2207 allocate_counters(&thread_odd, &core_odd, &package_odd);
2208 allocate_output_buffer();
2209 for_all_proc_cpus(initialize_counters);
2211 void turbostat_init()
2213 check_cpuid();
2215 check_dev_msr();
2216 check_super_user();
2218 setup_all_buffers();
2220 if (verbose)
2221 print_verbose_header();
2223 if (verbose)
2224 for_all_cpus(print_epb, ODD_COUNTERS);
2226 if (verbose)
2227 for_all_cpus(print_rapl, ODD_COUNTERS);
2229 for_all_cpus(set_temperature_target, ODD_COUNTERS);
2231 if (verbose)
2232 for_all_cpus(print_thermal, ODD_COUNTERS);
2235 int fork_it(char **argv)
2237 pid_t child_pid;
2238 int status;
2240 status = for_all_cpus(get_counters, EVEN_COUNTERS);
2241 if (status)
2242 exit(status);
2243 /* clear affinity side-effect of get_counters() */
2244 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
2245 gettimeofday(&tv_even, (struct timezone *)NULL);
2247 child_pid = fork();
2248 if (!child_pid) {
2249 /* child */
2250 execvp(argv[0], argv);
2251 } else {
2253 /* parent */
2254 if (child_pid == -1) {
2255 perror("fork");
2256 exit(1);
2259 signal(SIGINT, SIG_IGN);
2260 signal(SIGQUIT, SIG_IGN);
2261 if (waitpid(child_pid, &status, 0) == -1) {
2262 perror("wait");
2263 exit(status);
2267 * n.b. fork_it() does not check for errors from for_all_cpus()
2268 * because re-starting is problematic when forking
2270 for_all_cpus(get_counters, ODD_COUNTERS);
2271 gettimeofday(&tv_odd, (struct timezone *)NULL);
2272 timersub(&tv_odd, &tv_even, &tv_delta);
2273 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
2274 compute_average(EVEN_COUNTERS);
2275 format_all_counters(EVEN_COUNTERS);
2276 flush_stderr();
2278 fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
2280 return status;
2283 void cmdline(int argc, char **argv)
2285 int opt;
2287 progname = argv[0];
2289 while ((opt = getopt(argc, argv, "+pPSvi:sc:sC:m:M:RT:")) != -1) {
2290 switch (opt) {
2291 case 'p':
2292 show_core_only++;
2293 break;
2294 case 'P':
2295 show_pkg_only++;
2296 break;
2297 case 'S':
2298 summary_only++;
2299 break;
2300 case 'v':
2301 verbose++;
2302 break;
2303 case 'i':
2304 interval_sec = atoi(optarg);
2305 break;
2306 case 'c':
2307 sscanf(optarg, "%x", &extra_delta_offset32);
2308 break;
2309 case 'C':
2310 sscanf(optarg, "%x", &extra_delta_offset64);
2311 break;
2312 case 'm':
2313 sscanf(optarg, "%x", &extra_msr_offset32);
2314 break;
2315 case 'M':
2316 sscanf(optarg, "%x", &extra_msr_offset64);
2317 break;
2318 case 'R':
2319 rapl_verbose++;
2320 break;
2321 case 'T':
2322 tcc_activation_temp_override = atoi(optarg);
2323 break;
2324 default:
2325 usage();
2330 int main(int argc, char **argv)
2332 cmdline(argc, argv);
2334 if (verbose)
2335 fprintf(stderr, "turbostat v3.4 April 17, 2013"
2336 " - Len Brown <lenb@kernel.org>\n");
2338 turbostat_init();
2341 * if any params left, it must be a command to fork
2343 if (argc - optind)
2344 return fork_it(argv + optind);
2345 else
2346 turbostat_loop();
2348 return 0;