2 * turbostat -- show CPU frequency and C-state residency
3 * on modern Intel turbo-capable processors.
5 * Copyright (c) 2013 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
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.
28 #include <sys/types.h>
31 #include <sys/resource.h>
42 char *proc_stat = "/proc/stat";
43 unsigned int interval_sec = 5; /* set with -i interval_sec */
44 unsigned int verbose; /* set with -v */
45 unsigned int rapl_verbose; /* set with -R */
46 unsigned int thermal_verbose; /* set with -T */
47 unsigned int summary_only; /* set with -s */
50 unsigned int do_nhm_cstates;
51 unsigned int do_snb_cstates;
52 unsigned int do_c8_c9_c10;
53 unsigned int do_slm_cstates;
54 unsigned int use_c1_residency_msr;
55 unsigned int has_aperf;
57 unsigned int units = 1000000000; /* Ghz etc */
58 unsigned int genuine_intel;
59 unsigned int has_invariant_tsc;
60 unsigned int do_nehalem_platform_info;
61 unsigned int do_nehalem_turbo_ratio_limit;
62 unsigned int do_ivt_turbo_ratio_limit;
63 unsigned int extra_msr_offset32;
64 unsigned int extra_msr_offset64;
65 unsigned int extra_delta_offset32;
66 unsigned int extra_delta_offset64;
69 unsigned int show_pkg;
70 unsigned int show_core;
71 unsigned int show_cpu;
72 unsigned int show_pkg_only;
73 unsigned int show_core_only;
74 char *output_buffer, *outp;
78 unsigned int tcc_activation_temp;
79 unsigned int tcc_activation_temp_override;
80 double rapl_power_units, rapl_energy_units, rapl_time_units;
81 double rapl_joule_counter_range;
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)
89 #define RAPL_PKG_POWER_INFO (1 << 6)
90 #define RAPL_CORE_POLICY (1 << 7)
91 #define TJMAX_DEFAULT 100
93 #define MAX(a, b) ((a) > (b) ? (a) : (b))
95 int aperf_mperf_unstable;
99 cpu_set_t *cpu_present_set, *cpu_affinity_set;
100 size_t cpu_present_setsize, cpu_affinity_setsize;
103 unsigned long long tsc;
104 unsigned long long aperf;
105 unsigned long long mperf;
106 unsigned long long c1;
107 unsigned long long extra_msr64;
108 unsigned long long extra_delta64;
109 unsigned long long extra_msr32;
110 unsigned long long extra_delta32;
111 unsigned int smi_count;
114 #define CPU_IS_FIRST_THREAD_IN_CORE 0x2
115 #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
116 } *thread_even, *thread_odd;
119 unsigned long long c3;
120 unsigned long long c6;
121 unsigned long long c7;
122 unsigned int core_temp_c;
123 unsigned int core_id;
124 } *core_even, *core_odd;
127 unsigned long long pc2;
128 unsigned long long pc3;
129 unsigned long long pc6;
130 unsigned long long pc7;
131 unsigned long long pc8;
132 unsigned long long pc9;
133 unsigned long long pc10;
134 unsigned int package_id;
135 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;
143 } *package_even, *package_odd;
145 #define ODD_COUNTERS thread_odd, core_odd, package_odd
146 #define EVEN_COUNTERS thread_even, core_even, package_even
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)
156 struct system_summary {
157 struct thread_data threads;
158 struct core_data cores;
159 struct pkg_data packages;
168 int num_cores_per_pkg;
169 int num_threads_per_core;
172 struct timeval tv_even, tv_odd, tv_delta;
174 void setup_all_buffers(void);
176 int cpu_is_not_present(int cpu)
178 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
181 * run func(thread, core, package) in topology order
182 * skip non-present cpus
185 int 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)
188 int retval, pkg_no, core_no, thread_no;
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;
198 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
200 if (cpu_is_not_present(t->cpu_id))
203 c = GET_CORE(core_base, core_no, pkg_no);
204 p = GET_PKG(pkg_base, pkg_no);
206 retval = func(t, c, p);
215 int cpu_migrate(int cpu)
217 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)
225 int get_msr(int cpu, off_t offset, unsigned long long *msr)
231 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
232 fd = open(pathname, O_RDONLY);
236 retval = pread(fd, msr, sizeof *msr, offset);
239 if (retval != sizeof *msr) {
240 fprintf(stderr, "%s offset 0x%llx read failed\n", pathname, (unsigned long long)offset);
247 void print_header(void)
250 outp += sprintf(outp, "pk");
252 outp += sprintf(outp, " ");
254 outp += sprintf(outp, "cor");
256 outp += sprintf(outp, " CPU");
257 if (show_pkg || show_core || show_cpu)
258 outp += sprintf(outp, " ");
260 outp += sprintf(outp, " %%c0");
262 outp += sprintf(outp, " GHz");
263 outp += sprintf(outp, " TSC");
265 outp += sprintf(outp, " SMI");
266 if (extra_delta_offset32)
267 outp += sprintf(outp, " count 0x%03X", extra_delta_offset32);
268 if (extra_delta_offset64)
269 outp += sprintf(outp, " COUNT 0x%03X", extra_delta_offset64);
270 if (extra_msr_offset32)
271 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset32);
272 if (extra_msr_offset64)
273 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset64);
275 outp += sprintf(outp, " %%c1");
276 if (do_nhm_cstates && !do_slm_cstates)
277 outp += sprintf(outp, " %%c3");
279 outp += sprintf(outp, " %%c6");
281 outp += sprintf(outp, " %%c7");
284 outp += sprintf(outp, " CTMP");
286 outp += sprintf(outp, " PTMP");
289 outp += sprintf(outp, " %%pc2");
290 if (do_nhm_cstates && !do_slm_cstates)
291 outp += sprintf(outp, " %%pc3");
292 if (do_nhm_cstates && !do_slm_cstates)
293 outp += sprintf(outp, " %%pc6");
295 outp += sprintf(outp, " %%pc7");
297 outp += sprintf(outp, " %%pc8");
298 outp += sprintf(outp, " %%pc9");
299 outp += sprintf(outp, " %%pc10");
302 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_%%");
315 outp += sprintf(outp, "\n");
318 int dump_counters(struct thread_data *t, struct core_data *c,
321 fprintf(stderr, "t %p, c %p, p %p\n", t, c, p);
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);
329 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",
334 extra_msr_offset32, t->extra_msr32);
335 fprintf(stderr, "msr0x%x: %016llX\n",
336 extra_msr_offset64, t->extra_msr64);
338 fprintf(stderr, "SMI: %08X\n", t->smi_count);
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);
346 fprintf(stderr, "DTS: %dC\n", c->core_temp_c);
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);
355 fprintf(stderr, "pc8: %016llX\n", p->pc8);
356 fprintf(stderr, "pc9: %016llX\n", p->pc9);
357 fprintf(stderr, "pc10: %016llX\n", p->pc10);
358 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);
370 * column formatting convention & formats
371 * package: "pk" 2 columns %2d
372 * core: "cor" 3 columns %3d
373 * CPU: "CPU" 3 columns %3d
378 * GHz: "GHz" 3 columns %3.2
379 * TSC: "TSC" 3 columns %3.2
380 * SMI: "SMI" 4 columns %4d
381 * percentage " %pc3" %6.2
382 * Perf Status percentage: %5.2
383 * "CTMP" 4 columns %4d
385 int format_counters(struct thread_data *t, struct core_data *c,
388 double interval_float;
391 /* 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))
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))
399 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
401 /* topo columns, print blanks on 1st (average) line */
402 if (t == &average.threads) {
404 outp += sprintf(outp, " ");
405 if (show_pkg && show_core)
406 outp += sprintf(outp, " ");
408 outp += sprintf(outp, " ");
410 outp += sprintf(outp, " " " ");
414 outp += sprintf(outp, "%2d", p->package_id);
416 outp += sprintf(outp, " ");
418 if (show_pkg && show_core)
419 outp += sprintf(outp, " ");
422 outp += sprintf(outp, "%3d", c->core_id);
424 outp += sprintf(outp, " ");
427 outp += sprintf(outp, " %3d", t->cpu_id);
430 if (do_nhm_cstates) {
431 if (show_pkg || show_core || show_cpu)
432 outp += sprintf(outp, " ");
434 outp += sprintf(outp, "%6.2f", 100.0 * t->mperf/t->tsc);
436 outp += sprintf(outp, " ****");
441 if (!aperf_mperf_unstable) {
442 outp += sprintf(outp, " %3.2f",
443 1.0 * t->tsc / units * t->aperf /
444 t->mperf / interval_float);
446 if (t->aperf > t->tsc || t->mperf > t->tsc) {
447 outp += sprintf(outp, " ***");
449 outp += sprintf(outp, "%3.1f*",
452 t->mperf / interval_float);
458 outp += sprintf(outp, "%5.2f", 1.0 * t->tsc/units/interval_float);
462 outp += sprintf(outp, "%4d", t->smi_count);
465 if (extra_delta_offset32)
466 outp += sprintf(outp, " %11llu", t->extra_delta32);
469 if (extra_delta_offset64)
470 outp += sprintf(outp, " %11llu", t->extra_delta64);
472 if (extra_msr_offset32)
473 outp += sprintf(outp, " 0x%08llx", t->extra_msr32);
476 if (extra_msr_offset64)
477 outp += sprintf(outp, " 0x%016llx", t->extra_msr64);
479 if (do_nhm_cstates) {
481 outp += sprintf(outp, " %6.2f", 100.0 * t->c1/t->tsc);
483 outp += sprintf(outp, " ****");
486 /* print per-core data only for 1st thread in core */
487 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
490 if (do_nhm_cstates && !do_slm_cstates)
491 outp += sprintf(outp, " %6.2f", 100.0 * c->c3/t->tsc);
493 outp += sprintf(outp, " %6.2f", 100.0 * c->c6/t->tsc);
495 outp += sprintf(outp, " %6.2f", 100.0 * c->c7/t->tsc);
498 outp += sprintf(outp, " %4d", c->core_temp_c);
500 /* print per-package data only for 1st core in package */
501 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
505 outp += sprintf(outp, " %4d", p->pkg_temp_c);
508 outp += sprintf(outp, " %6.2f", 100.0 * p->pc2/t->tsc);
509 if (do_nhm_cstates && !do_slm_cstates)
510 outp += sprintf(outp, " %6.2f", 100.0 * p->pc3/t->tsc);
511 if (do_nhm_cstates && !do_slm_cstates)
512 outp += sprintf(outp, " %6.2f", 100.0 * p->pc6/t->tsc);
514 outp += sprintf(outp, " %6.2f", 100.0 * p->pc7/t->tsc);
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);
522 * If measurement interval exceeds minimum RAPL Joule Counter range,
523 * indicate that results are suspect by printing "**" in fraction place.
525 if (interval_float < rapl_joule_counter_range) {
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);
547 outp += sprintf(outp, "\n");
554 fputs(output_buffer, stdout);
556 outp = output_buffer;
560 fputs(output_buffer, stderr);
561 outp = output_buffer;
563 void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
567 if (!printed || !summary_only)
570 if (topo.num_cpus > 1)
571 format_counters(&average.threads, &average.cores,
579 for_all_cpus(format_counters, t, c, p);
582 #define DELTA_WRAP32(new, old) \
586 old = 0x100000000 + new - old; \
590 delta_package(struct pkg_data *new, struct pkg_data *old)
592 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;
596 old->pc8 = new->pc8 - old->pc8;
597 old->pc9 = new->pc9 - old->pc9;
598 old->pc10 = new->pc10 - old->pc10;
599 old->pkg_temp_c = new->pkg_temp_c;
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);
610 delta_core(struct core_data *new, struct core_data *old)
612 old->c3 = new->c3 - old->c3;
613 old->c6 = new->c6 - old->c6;
614 old->c7 = new->c7 - old->c7;
615 old->core_temp_c = new->core_temp_c;
622 delta_thread(struct thread_data *new, struct thread_data *old,
623 struct core_data *core_delta)
625 old->tsc = new->tsc - old->tsc;
627 /* check for TSC < 1 Mcycles over interval */
628 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\"");
633 old->c1 = new->c1 - old->c1;
635 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
636 old->aperf = new->aperf - old->aperf;
637 old->mperf = new->mperf - old->mperf;
640 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");
645 aperf_mperf_unstable = 1;
648 * mperf delta is likely a huge "positive" number
649 * can not use it for calculating c0 time
656 if (use_c1_residency_msr) {
658 * Some models have a dedicated C1 residency MSR,
659 * which should be more accurate than the derivation below.
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.
667 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
670 /* normal case, derive c1 */
671 old->c1 = old->tsc - old->mperf - core_delta->c3
672 - core_delta->c6 - core_delta->c7;
676 if (old->mperf == 0) {
677 if (verbose > 1) fprintf(stderr, "cpu%d MPERF 0!\n", old->cpu_id);
678 old->mperf = 1; /* divide by 0 protection */
681 old->extra_delta32 = new->extra_delta32 - old->extra_delta32;
682 old->extra_delta32 &= 0xFFFFFFFF;
684 old->extra_delta64 = new->extra_delta64 - old->extra_delta64;
687 * Extra MSR is just a snapshot, simply copy latest w/o subtracting
689 old->extra_msr32 = new->extra_msr32;
690 old->extra_msr64 = new->extra_msr64;
693 old->smi_count = new->smi_count - old->smi_count;
696 int 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)
700 /* calculate core delta only for 1st thread in core */
701 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
704 /* always calculate thread delta */
705 delta_thread(t, t2, c2); /* c2 is core delta */
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);
714 void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
722 t->extra_delta32 = 0;
723 t->extra_delta64 = 0;
725 /* 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;
745 p->rapl_pkg_perf_status = 0;
746 p->rapl_dram_perf_status = 0;
749 int sum_counters(struct thread_data *t, struct core_data *c,
752 average.threads.tsc += t->tsc;
753 average.threads.aperf += t->aperf;
754 average.threads.mperf += t->mperf;
755 average.threads.c1 += t->c1;
757 average.threads.extra_delta32 += t->extra_delta32;
758 average.threads.extra_delta64 += t->extra_delta64;
760 /* sum per-core values only for 1st thread in core */
761 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
764 average.cores.c3 += c->c3;
765 average.cores.c6 += c->c6;
766 average.cores.c7 += c->c7;
768 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
770 /* sum per-pkg values only for 1st core in pkg */
771 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
774 average.packages.pc2 += p->pc2;
775 average.packages.pc3 += p->pc3;
776 average.packages.pc6 += p->pc6;
777 average.packages.pc7 += p->pc7;
778 average.packages.pc8 += p->pc8;
779 average.packages.pc9 += p->pc9;
780 average.packages.pc10 += p->pc10;
782 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;
787 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
789 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
790 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
794 * sum the counters for all cpus in the system
795 * compute the weighted average
797 void compute_average(struct thread_data *t, struct core_data *c,
800 clear_counters(&average.threads, &average.cores, &average.packages);
802 for_all_cpus(sum_counters, t, c, p);
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;
809 average.threads.extra_delta32 /= topo.num_cpus;
810 average.threads.extra_delta32 &= 0xFFFFFFFF;
812 average.threads.extra_delta64 /= topo.num_cpus;
814 average.cores.c3 /= topo.num_cores;
815 average.cores.c6 /= topo.num_cores;
816 average.cores.c7 /= topo.num_cores;
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;
823 average.packages.pc8 /= topo.num_packages;
824 average.packages.pc9 /= topo.num_packages;
825 average.packages.pc10 /= topo.num_packages;
828 static unsigned long long rdtsc(void)
830 unsigned int low, high;
832 asm volatile("rdtsc" : "=a" (low), "=d" (high));
834 return low | ((unsigned long long)high) << 32;
841 * acquire and record local counters for that cpu
843 int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
846 unsigned long long msr;
848 if (cpu_migrate(cpu)) {
849 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
853 t->tsc = rdtsc(); /* we are running on local CPU of interest */
856 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
858 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
863 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
865 t->smi_count = msr & 0xFFFFFFFF;
867 if (extra_delta_offset32) {
868 if (get_msr(cpu, extra_delta_offset32, &msr))
870 t->extra_delta32 = msr & 0xFFFFFFFF;
873 if (extra_delta_offset64)
874 if (get_msr(cpu, extra_delta_offset64, &t->extra_delta64))
877 if (extra_msr_offset32) {
878 if (get_msr(cpu, extra_msr_offset32, &msr))
880 t->extra_msr32 = msr & 0xFFFFFFFF;
883 if (extra_msr_offset64)
884 if (get_msr(cpu, extra_msr_offset64, &t->extra_msr64))
887 if (use_c1_residency_msr) {
888 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
892 /* collect core counters only for 1st thread in core */
893 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
896 if (do_nhm_cstates && !do_slm_cstates) {
897 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
901 if (do_nhm_cstates) {
902 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
907 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
911 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
913 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
917 /* collect package counters only for 1st core in package */
918 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
921 if (do_nhm_cstates && !do_slm_cstates) {
922 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
924 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
927 if (do_snb_cstates) {
928 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
930 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
934 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
936 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
938 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
941 if (do_rapl & RAPL_PKG) {
942 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
944 p->energy_pkg = msr & 0xFFFFFFFF;
946 if (do_rapl & RAPL_CORES) {
947 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
949 p->energy_cores = msr & 0xFFFFFFFF;
951 if (do_rapl & RAPL_DRAM) {
952 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
954 p->energy_dram = msr & 0xFFFFFFFF;
956 if (do_rapl & RAPL_GFX) {
957 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
959 p->energy_gfx = msr & 0xFFFFFFFF;
961 if (do_rapl & RAPL_PKG_PERF_STATUS) {
962 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
964 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
966 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
967 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
969 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
972 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
974 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
979 void print_verbose_header(void)
981 unsigned long long msr;
984 if (!do_nehalem_platform_info)
987 get_msr(0, MSR_NHM_PLATFORM_INFO, &msr);
989 fprintf(stderr, "cpu0: MSR_NHM_PLATFORM_INFO: 0x%08llx\n", msr);
991 ratio = (msr >> 40) & 0xFF;
992 fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency\n",
993 ratio, bclk, ratio * bclk);
995 ratio = (msr >> 8) & 0xFF;
996 fprintf(stderr, "%d * %.0f = %.0f MHz TSC frequency\n",
997 ratio, bclk, ratio * bclk);
999 get_msr(0, MSR_IA32_POWER_CTL, &msr);
1000 fprintf(stderr, "cpu0: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
1001 msr, msr & 0x2 ? "EN" : "DIS");
1003 if (!do_ivt_turbo_ratio_limit)
1004 goto print_nhm_turbo_ratio_limits;
1006 get_msr(0, MSR_IVT_TURBO_RATIO_LIMIT, &msr);
1008 fprintf(stderr, "cpu0: MSR_IVT_TURBO_RATIO_LIMIT: 0x%08llx\n", msr);
1010 ratio = (msr >> 56) & 0xFF;
1012 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 16 active cores\n",
1013 ratio, bclk, ratio * bclk);
1015 ratio = (msr >> 48) & 0xFF;
1017 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 15 active cores\n",
1018 ratio, bclk, ratio * bclk);
1020 ratio = (msr >> 40) & 0xFF;
1022 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 14 active cores\n",
1023 ratio, bclk, ratio * bclk);
1025 ratio = (msr >> 32) & 0xFF;
1027 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 13 active cores\n",
1028 ratio, bclk, ratio * bclk);
1030 ratio = (msr >> 24) & 0xFF;
1032 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 12 active cores\n",
1033 ratio, bclk, ratio * bclk);
1035 ratio = (msr >> 16) & 0xFF;
1037 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 11 active cores\n",
1038 ratio, bclk, ratio * bclk);
1040 ratio = (msr >> 8) & 0xFF;
1042 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 10 active cores\n",
1043 ratio, bclk, ratio * bclk);
1045 ratio = (msr >> 0) & 0xFF;
1047 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 9 active cores\n",
1048 ratio, bclk, ratio * bclk);
1050 print_nhm_turbo_ratio_limits:
1051 get_msr(0, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
1053 #define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
1054 #define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
1056 fprintf(stderr, "cpu0: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", msr);
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);
1069 fprintf(stderr, do_slm_cstates ? "no pkg states" : "pc0");
1072 fprintf(stderr, do_slm_cstates ? "no pkg states" : do_snb_cstates ? "pc2" : "pc0");
1075 fprintf(stderr, do_slm_cstates ? "invalid" : do_snb_cstates ? "pc6-noret" : "pc3");
1078 fprintf(stderr, do_slm_cstates ? "invalid" : "pc6");
1081 fprintf(stderr, do_slm_cstates ? "pc4" : "pc7");
1084 fprintf(stderr, do_slm_cstates ? "invalid" : do_snb_cstates ? "pc7s" : "invalid");
1087 fprintf(stderr, do_slm_cstates ? "pc6" : "invalid");
1090 fprintf(stderr, do_slm_cstates ? "pc7" : "unlimited");
1093 fprintf(stderr, "invalid");
1095 fprintf(stderr, ")\n");
1097 if (!do_nehalem_turbo_ratio_limit)
1100 get_msr(0, MSR_NHM_TURBO_RATIO_LIMIT, &msr);
1102 fprintf(stderr, "cpu0: MSR_NHM_TURBO_RATIO_LIMIT: 0x%08llx\n", msr);
1104 ratio = (msr >> 56) & 0xFF;
1106 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 8 active cores\n",
1107 ratio, bclk, ratio * bclk);
1109 ratio = (msr >> 48) & 0xFF;
1111 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 7 active cores\n",
1112 ratio, bclk, ratio * bclk);
1114 ratio = (msr >> 40) & 0xFF;
1116 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 6 active cores\n",
1117 ratio, bclk, ratio * bclk);
1119 ratio = (msr >> 32) & 0xFF;
1121 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 5 active cores\n",
1122 ratio, bclk, ratio * bclk);
1124 ratio = (msr >> 24) & 0xFF;
1126 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
1127 ratio, bclk, ratio * bclk);
1129 ratio = (msr >> 16) & 0xFF;
1131 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
1132 ratio, bclk, ratio * bclk);
1134 ratio = (msr >> 8) & 0xFF;
1136 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
1137 ratio, bclk, ratio * bclk);
1139 ratio = (msr >> 0) & 0xFF;
1141 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
1142 ratio, bclk, ratio * bclk);
1145 void free_all_buffers(void)
1147 CPU_FREE(cpu_present_set);
1148 cpu_present_set = NULL;
1149 cpu_present_set = 0;
1151 CPU_FREE(cpu_affinity_set);
1152 cpu_affinity_set = NULL;
1153 cpu_affinity_setsize = 0;
1161 package_even = NULL;
1171 free(output_buffer);
1172 output_buffer = NULL;
1177 * Open a file, and exit on failure
1179 FILE *fopen_or_die(const char *path, const char *mode)
1181 FILE *filep = fopen(path, "r");
1183 err(1, "%s: open failed", path);
1188 * Parse a file containing a single int.
1190 int parse_int_file(const char *fmt, ...)
1193 char path[PATH_MAX];
1197 va_start(args, fmt);
1198 vsnprintf(path, sizeof(path), fmt, args);
1200 filep = fopen_or_die(path, "r");
1201 if (fscanf(filep, "%d", &value) != 1)
1202 err(1, "%s: failed to parse number from file", path);
1208 * cpu_is_first_sibling_in_core(cpu)
1209 * return 1 if given CPU is 1st HT sibling in the core
1211 int cpu_is_first_sibling_in_core(int cpu)
1213 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
1217 * cpu_is_first_core_in_package(cpu)
1218 * return 1 if given CPU is 1st core in package
1220 int cpu_is_first_core_in_package(int cpu)
1222 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
1225 int get_physical_package_id(int cpu)
1227 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
1230 int get_core_id(int cpu)
1232 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
1235 int get_num_ht_siblings(int cpu)
1243 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
1244 filep = fopen_or_die(path, "r");
1247 * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4)
1248 * otherwinse 1 sibling (self).
1250 matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2);
1261 * run func(thread, core, package) in topology order
1262 * skip non-present cpus
1265 int 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)
1272 int retval, pkg_no, core_no, thread_no;
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;
1282 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
1284 if (cpu_is_not_present(t->cpu_id))
1287 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
1289 c = GET_CORE(core_base, core_no, pkg_no);
1290 c2 = GET_CORE(core_base2, core_no, pkg_no);
1292 p = GET_PKG(pkg_base, pkg_no);
1293 p2 = GET_PKG(pkg_base2, pkg_no);
1295 retval = func(t, c, p, t2, c2, p2);
1305 * run func(cpu) on every cpu in /proc/stat
1306 * return max_cpu number
1308 int for_all_proc_cpus(int (func)(int))
1314 fp = fopen_or_die(proc_stat, "r");
1316 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
1318 err(1, "%s: failed to parse format", proc_stat);
1321 retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
1325 retval = func(cpu_num);
1335 void re_initialize(void)
1338 setup_all_buffers();
1339 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
1345 * remember the last one seen, it will be the max
1347 int count_cpus(int cpu)
1349 if (topo.max_cpu_num < cpu)
1350 topo.max_cpu_num = cpu;
1355 int mark_cpu_present(int cpu)
1357 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
1361 void turbostat_loop()
1369 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
1372 } else if (retval == -1) {
1373 if (restarted > 1) {
1380 gettimeofday(&tv_even, (struct timezone *)NULL);
1383 if (for_all_proc_cpus(cpu_is_not_present)) {
1387 sleep(interval_sec);
1388 retval = for_all_cpus(get_counters, ODD_COUNTERS);
1391 } else if (retval == -1) {
1395 gettimeofday(&tv_odd, (struct timezone *)NULL);
1396 timersub(&tv_odd, &tv_even, &tv_delta);
1397 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
1398 compute_average(EVEN_COUNTERS);
1399 format_all_counters(EVEN_COUNTERS);
1401 sleep(interval_sec);
1402 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
1405 } else if (retval == -1) {
1409 gettimeofday(&tv_even, (struct timezone *)NULL);
1410 timersub(&tv_even, &tv_odd, &tv_delta);
1411 for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS);
1412 compute_average(ODD_COUNTERS);
1413 format_all_counters(ODD_COUNTERS);
1418 void check_dev_msr()
1422 if (stat("/dev/cpu/0/msr", &sb))
1423 err(-5, "no /dev/cpu/0/msr\n"
1424 "Try \"# modprobe msr\"");
1427 void check_super_user()
1430 errx(-6, "must be root");
1433 int has_nehalem_turbo_ratio_limit(unsigned int family, unsigned int 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 */
1449 case 0x3A: /* IVB */
1450 case 0x3E: /* IVB Xeon */
1451 case 0x3C: /* HSW */
1452 case 0x3F: /* HSW */
1453 case 0x45: /* HSW */
1454 case 0x46: /* HSW */
1455 case 0x37: /* BYT */
1456 case 0x4D: /* AVN */
1458 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1459 case 0x2F: /* Westmere-EX Xeon - Eagleton */
1464 int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
1473 case 0x3E: /* IVB Xeon */
1482 * Decode the ENERGY_PERF_BIAS MSR
1484 int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1486 unsigned long long msr;
1495 /* EPB is per-package */
1496 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1499 if (cpu_migrate(cpu)) {
1500 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1504 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
1507 switch (msr & 0x7) {
1508 case ENERGY_PERF_BIAS_PERFORMANCE:
1509 epb_string = "performance";
1511 case ENERGY_PERF_BIAS_NORMAL:
1512 epb_string = "balanced";
1514 case ENERGY_PERF_BIAS_POWERSAVE:
1515 epb_string = "powersave";
1518 epb_string = "custom";
1521 fprintf(stderr, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
1526 #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
1527 #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
1529 double get_tdp(model)
1531 unsigned long long msr;
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;
1550 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
1552 void rapl_probe(unsigned int family, unsigned int model)
1554 unsigned long long msr;
1555 unsigned int time_unit;
1567 case 0x3C: /* HSW */
1568 case 0x3F: /* HSW */
1569 case 0x45: /* HSW */
1570 case 0x46: /* HSW */
1571 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
1575 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_PKG_PERF_STATUS | RAPL_DRAM_PERF_STATUS | RAPL_PKG_POWER_INFO;
1577 case 0x37: /* BYT */
1578 case 0x4D: /* AVN */
1579 do_rapl = RAPL_PKG | RAPL_CORES ;
1585 /* units on package 0, verify later other packages match */
1586 if (get_msr(0, MSR_RAPL_POWER_UNIT, &msr))
1589 rapl_power_units = 1.0 / (1 << (msr & 0xF));
1591 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
1593 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
1595 time_unit = msr >> 16 & 0xF;
1599 rapl_time_units = 1.0 / (1 << (time_unit));
1601 tdp = get_tdp(model);
1603 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
1605 fprintf(stderr, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
1610 int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1612 unsigned long long msr;
1616 if (!(do_dts || do_ptm))
1621 /* DTS is per-core, no need to print for each thread */
1622 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1625 if (cpu_migrate(cpu)) {
1626 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1630 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
1631 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
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);
1639 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
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);
1651 unsigned int resolution;
1653 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
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);
1662 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
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);
1675 void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
1677 fprintf(stderr, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
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"));
1687 int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1689 unsigned long long msr;
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))
1700 if (cpu_migrate(cpu)) {
1701 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1705 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
1709 fprintf(stderr, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx "
1710 "(%f Watts, %f Joules, %f sec.)\n", cpu, msr,
1711 rapl_power_units, rapl_energy_units, rapl_time_units);
1713 if (do_rapl & RAPL_PKG_POWER_INFO) {
1715 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
1719 fprintf(stderr, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
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);
1727 if (do_rapl & RAPL_PKG) {
1729 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
1732 fprintf(stderr, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
1733 cpu, msr, (msr >> 63) & 1 ? "": "UN");
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",
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");
1744 if (do_rapl & RAPL_DRAM) {
1745 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
1749 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
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);
1757 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
1759 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
1760 cpu, msr, (msr >> 31) & 1 ? "": "UN");
1762 print_power_limit_msr(cpu, msr, "DRAM Limit");
1764 if (do_rapl & RAPL_CORE_POLICY) {
1766 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
1769 fprintf(stderr, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
1772 if (do_rapl & RAPL_CORES) {
1775 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
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");
1782 if (do_rapl & RAPL_GFX) {
1784 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
1787 fprintf(stderr, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
1789 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
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");
1800 int is_snb(unsigned int family, unsigned int model)
1808 case 0x3A: /* IVB */
1809 case 0x3E: /* IVB Xeon */
1810 case 0x3C: /* HSW */
1811 case 0x3F: /* HSW */
1812 case 0x45: /* HSW */
1813 case 0x46: /* HSW */
1819 int has_c8_c9_c10(unsigned int family, unsigned int model)
1832 int is_slm(unsigned int family, unsigned int model)
1837 case 0x37: /* BYT */
1838 case 0x4D: /* AVN */
1844 #define SLM_BCLK_FREQS 5
1845 double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
1847 double slm_bclk(void)
1849 unsigned long long msr = 3;
1853 if (get_msr(0, MSR_FSB_FREQ, &msr))
1854 fprintf(stderr, "SLM BCLK: unknown\n");
1857 if (i >= SLM_BCLK_FREQS) {
1858 fprintf(stderr, "SLM BCLK[%d] invalid\n", i);
1861 freq = slm_freq_table[i];
1863 fprintf(stderr, "SLM BCLK: %.1f Mhz\n", freq);
1868 double discover_bclk(unsigned int family, unsigned int model)
1870 if (is_snb(family, model))
1872 else if (is_slm(family, model))
1879 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
1880 * the Thermal Control Circuit (TCC) activates.
1881 * This is usually equal to tjMax.
1883 * Older processors do not have this MSR, so there we guess,
1884 * but also allow cmdline over-ride with -T.
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.
1890 int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1892 unsigned long long msr;
1893 unsigned int target_c_local;
1896 /* tcc_activation_temp is used only for dts or ptm */
1897 if (!(do_dts || do_ptm))
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))
1905 if (cpu_migrate(cpu)) {
1906 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
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);
1917 /* Temperature Target MSR is Nehalem and newer only */
1918 if (!do_nehalem_platform_info)
1921 if (get_msr(0, MSR_IA32_TEMPERATURE_TARGET, &msr))
1924 target_c_local = (msr >> 16) & 0x7F;
1927 fprintf(stderr, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
1928 cpu, msr, target_c_local);
1930 if (target_c_local < 85 || target_c_local > 127)
1933 tcc_activation_temp = target_c_local;
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);
1946 unsigned int eax, ebx, ecx, edx, max_level;
1947 unsigned int fms, family, model, stepping;
1949 eax = ebx = ecx = edx = 0;
1951 __get_cpuid(0, &max_level, &ebx, &ecx, &edx);
1953 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
1957 fprintf(stderr, "CPUID(0): %.4s%.4s%.4s ",
1958 (char *)&ebx, (char *)&edx, (char *)&ecx);
1960 __get_cpuid(1, &fms, &ebx, &ecx, &edx);
1961 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;
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);
1971 if (!(edx & (1 << 5)))
1972 errx(1, "CPUID: no MSR");
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.
1979 ebx = ecx = edx = 0;
1980 __get_cpuid(0x80000000, &max_level, &ebx, &ecx, &edx);
1982 if (max_level < 0x80000007)
1983 errx(1, "CPUID: no invariant TSC (max_level 0x%x)", max_level);
1986 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
1987 * this check is valid for both Intel and AMD
1989 __get_cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
1990 has_invariant_tsc = edx & (1 << 8);
1992 if (!has_invariant_tsc)
1993 errx(1, "No invariant TSC");
1996 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
1997 * this check is valid for both Intel and AMD
2000 __get_cpuid(0x6, &eax, &ebx, &ecx, &edx);
2001 has_aperf = ecx & (1 << 0);
2002 do_dts = eax & (1 << 0);
2003 do_ptm = eax & (1 << 6);
2004 has_epb = ecx & (1 << 3);
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": "");
2014 errx(-1, "No APERF");
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 */
2018 do_smi = do_nhm_cstates;
2019 do_snb_cstates = is_snb(family, model);
2020 do_c8_c9_c10 = has_c8_c9_c10(family, model);
2021 do_slm_cstates = is_slm(family, model);
2022 bclk = discover_bclk(family, model);
2024 do_nehalem_turbo_ratio_limit = has_nehalem_turbo_ratio_limit(family, model);
2025 do_ivt_turbo_ratio_limit = has_ivt_turbo_ratio_limit(family, model);
2026 rapl_probe(family, model);
2034 errx(1, "%s: [-v][-R][-T][-p|-P|-S][-c MSR# | -s]][-C MSR#][-m MSR#][-M MSR#][-i interval_sec | command ...]\n",
2040 * in /dev/cpu/ return success for names that are numbers
2041 * ie. filter out ".", "..", "microcode".
2043 int dir_filter(const struct dirent *dirp)
2045 if (isdigit(dirp->d_name[0]))
2051 int open_dev_cpu_msr(int dummy1)
2056 void topology_probe()
2059 int max_core_id = 0;
2060 int max_package_id = 0;
2061 int max_siblings = 0;
2062 struct cpu_topology {
2064 int physical_package_id;
2067 /* Initialize num_cpus, max_cpu_num */
2069 topo.max_cpu_num = 0;
2070 for_all_proc_cpus(count_cpus);
2071 if (!summary_only && topo.num_cpus > 1)
2075 fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
2077 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
2079 err(1, "calloc cpus");
2082 * Allocate and initialize cpu_present_set
2084 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
2085 if (cpu_present_set == NULL)
2086 err(3, "CPU_ALLOC");
2087 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);
2092 * Allocate and initialize cpu_affinity_set
2094 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
2095 if (cpu_affinity_set == NULL)
2096 err(3, "CPU_ALLOC");
2097 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2098 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
2103 * find max_core_id, max_package_id
2105 for (i = 0; i <= topo.max_cpu_num; ++i) {
2108 if (cpu_is_not_present(i)) {
2110 fprintf(stderr, "cpu%d NOT PRESENT\n", i);
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;
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;
2121 siblings = get_num_ht_siblings(i);
2122 if (siblings > max_siblings)
2123 max_siblings = siblings;
2125 fprintf(stderr, "cpu %d pkg %d core %d\n",
2126 i, cpus[i].physical_package_id, cpus[i].core_id);
2128 topo.num_cores_per_pkg = max_core_id + 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)
2135 topo.num_packages = max_package_id + 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)
2142 topo.num_threads_per_core = max_siblings;
2144 fprintf(stderr, "max_siblings %d\n", max_siblings);
2150 allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
2154 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
2155 topo.num_packages, sizeof(struct thread_data));
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;
2163 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
2164 sizeof(struct core_data));
2168 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
2169 (*c)[i].core_id = -1;
2171 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
2175 for (i = 0; i < topo.num_packages; i++)
2176 (*p)[i].package_id = i;
2180 err(1, "calloc counters");
2185 * set cpu_id, core_num, pkg_num
2186 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
2188 * increment topo.num_cores when 1st core in pkg seen
2190 void 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)
2194 struct thread_data *t;
2195 struct core_data *c;
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);
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;
2209 c->core_id = core_num;
2210 p->package_id = pkg_num;
2214 int initialize_counters(int cpu_id)
2216 int my_thread_id, my_core_id, my_package_id;
2218 my_package_id = get_physical_package_id(cpu_id);
2219 my_core_id = get_core_id(cpu_id);
2221 if (cpu_is_first_sibling_in_core(cpu_id)) {
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);
2233 void allocate_output_buffer()
2235 output_buffer = calloc(1, (1 + topo.num_cpus) * 256);
2236 outp = output_buffer;
2238 err(-1, "calloc output buffer");
2241 void setup_all_buffers(void)
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);
2249 void turbostat_init()
2256 setup_all_buffers();
2259 print_verbose_header();
2262 for_all_cpus(print_epb, ODD_COUNTERS);
2265 for_all_cpus(print_rapl, ODD_COUNTERS);
2267 for_all_cpus(set_temperature_target, ODD_COUNTERS);
2270 for_all_cpus(print_thermal, ODD_COUNTERS);
2273 int fork_it(char **argv)
2278 status = for_all_cpus(get_counters, EVEN_COUNTERS);
2281 /* clear affinity side-effect of get_counters() */
2282 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
2283 gettimeofday(&tv_even, (struct timezone *)NULL);
2288 execvp(argv[0], argv);
2292 if (child_pid == -1)
2295 signal(SIGINT, SIG_IGN);
2296 signal(SIGQUIT, SIG_IGN);
2297 if (waitpid(child_pid, &status, 0) == -1)
2298 err(status, "waitpid");
2301 * n.b. fork_it() does not check for errors from for_all_cpus()
2302 * because re-starting is problematic when forking
2304 for_all_cpus(get_counters, ODD_COUNTERS);
2305 gettimeofday(&tv_odd, (struct timezone *)NULL);
2306 timersub(&tv_odd, &tv_even, &tv_delta);
2307 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
2308 compute_average(EVEN_COUNTERS);
2309 format_all_counters(EVEN_COUNTERS);
2312 fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
2317 void cmdline(int argc, char **argv)
2323 while ((opt = getopt(argc, argv, "+pPSvi:sc:sC:m:M:RT:")) != -1) {
2338 interval_sec = atoi(optarg);
2341 sscanf(optarg, "%x", &extra_delta_offset32);
2344 sscanf(optarg, "%x", &extra_delta_offset64);
2347 sscanf(optarg, "%x", &extra_msr_offset32);
2350 sscanf(optarg, "%x", &extra_msr_offset64);
2356 tcc_activation_temp_override = atoi(optarg);
2364 int main(int argc, char **argv)
2366 cmdline(argc, argv);
2369 fprintf(stderr, "turbostat v3.5 April 26, 2013"
2370 " - Len Brown <lenb@kernel.org>\n");
2375 * if any params left, it must be a command to fork
2378 return fork_it(argv + optind);