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>
41 #include <linux/capability.h>
44 char *proc_stat = "/proc/stat";
45 unsigned int interval_sec = 5; /* set with -i interval_sec */
46 unsigned int verbose; /* set with -v */
47 unsigned int rapl_verbose; /* set with -R */
48 unsigned int rapl_joules; /* set with -J */
49 unsigned int thermal_verbose; /* set with -T */
50 unsigned int summary_only; /* set with -S */
51 unsigned int dump_only; /* set with -s */
54 unsigned int do_nhm_cstates;
55 unsigned int do_snb_cstates;
56 unsigned int do_c8_c9_c10;
57 unsigned int do_slm_cstates;
58 unsigned int use_c1_residency_msr;
59 unsigned int has_aperf;
61 unsigned int units = 1000000; /* MHz etc */
62 unsigned int genuine_intel;
63 unsigned int has_invariant_tsc;
64 unsigned int do_nehalem_platform_info;
65 unsigned int do_nehalem_turbo_ratio_limit;
66 unsigned int do_ivt_turbo_ratio_limit;
67 unsigned int extra_msr_offset32;
68 unsigned int extra_msr_offset64;
69 unsigned int extra_delta_offset32;
70 unsigned int extra_delta_offset64;
73 unsigned int show_pkg;
74 unsigned int show_core;
75 unsigned int show_cpu;
76 unsigned int show_pkg_only;
77 unsigned int show_core_only;
78 char *output_buffer, *outp;
82 unsigned int tcc_activation_temp;
83 unsigned int tcc_activation_temp_override;
84 double rapl_power_units, rapl_energy_units, rapl_time_units;
85 double rapl_joule_counter_range;
86 unsigned int do_core_perf_limit_reasons;
87 unsigned int do_gfx_perf_limit_reasons;
88 unsigned int do_ring_perf_limit_reasons;
90 #define RAPL_PKG (1 << 0)
91 /* 0x610 MSR_PKG_POWER_LIMIT */
92 /* 0x611 MSR_PKG_ENERGY_STATUS */
93 #define RAPL_PKG_PERF_STATUS (1 << 1)
94 /* 0x613 MSR_PKG_PERF_STATUS */
95 #define RAPL_PKG_POWER_INFO (1 << 2)
96 /* 0x614 MSR_PKG_POWER_INFO */
98 #define RAPL_DRAM (1 << 3)
99 /* 0x618 MSR_DRAM_POWER_LIMIT */
100 /* 0x619 MSR_DRAM_ENERGY_STATUS */
101 /* 0x61c MSR_DRAM_POWER_INFO */
102 #define RAPL_DRAM_PERF_STATUS (1 << 4)
103 /* 0x61b MSR_DRAM_PERF_STATUS */
105 #define RAPL_CORES (1 << 5)
106 /* 0x638 MSR_PP0_POWER_LIMIT */
107 /* 0x639 MSR_PP0_ENERGY_STATUS */
108 #define RAPL_CORE_POLICY (1 << 6)
109 /* 0x63a MSR_PP0_POLICY */
112 #define RAPL_GFX (1 << 7)
113 /* 0x640 MSR_PP1_POWER_LIMIT */
114 /* 0x641 MSR_PP1_ENERGY_STATUS */
115 /* 0x642 MSR_PP1_POLICY */
116 #define TJMAX_DEFAULT 100
118 #define MAX(a, b) ((a) > (b) ? (a) : (b))
120 int aperf_mperf_unstable;
124 cpu_set_t *cpu_present_set, *cpu_affinity_set;
125 size_t cpu_present_setsize, cpu_affinity_setsize;
128 unsigned long long tsc;
129 unsigned long long aperf;
130 unsigned long long mperf;
131 unsigned long long c1;
132 unsigned long long extra_msr64;
133 unsigned long long extra_delta64;
134 unsigned long long extra_msr32;
135 unsigned long long extra_delta32;
136 unsigned int smi_count;
139 #define CPU_IS_FIRST_THREAD_IN_CORE 0x2
140 #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
141 } *thread_even, *thread_odd;
144 unsigned long long c3;
145 unsigned long long c6;
146 unsigned long long c7;
147 unsigned int core_temp_c;
148 unsigned int core_id;
149 } *core_even, *core_odd;
152 unsigned long long pc2;
153 unsigned long long pc3;
154 unsigned long long pc6;
155 unsigned long long pc7;
156 unsigned long long pc8;
157 unsigned long long pc9;
158 unsigned long long pc10;
159 unsigned int package_id;
160 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
161 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
162 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
163 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
164 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
165 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
166 unsigned int pkg_temp_c;
168 } *package_even, *package_odd;
170 #define ODD_COUNTERS thread_odd, core_odd, package_odd
171 #define EVEN_COUNTERS thread_even, core_even, package_even
173 #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
174 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
175 topo.num_threads_per_core + \
176 (core_no) * topo.num_threads_per_core + (thread_no))
177 #define GET_CORE(core_base, core_no, pkg_no) \
178 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
179 #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
181 struct system_summary {
182 struct thread_data threads;
183 struct core_data cores;
184 struct pkg_data packages;
193 int num_cores_per_pkg;
194 int num_threads_per_core;
197 struct timeval tv_even, tv_odd, tv_delta;
199 void setup_all_buffers(void);
201 int cpu_is_not_present(int cpu)
203 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
206 * run func(thread, core, package) in topology order
207 * skip non-present cpus
210 int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
211 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
213 int retval, pkg_no, core_no, thread_no;
215 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
216 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
217 for (thread_no = 0; thread_no <
218 topo.num_threads_per_core; ++thread_no) {
219 struct thread_data *t;
223 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
225 if (cpu_is_not_present(t->cpu_id))
228 c = GET_CORE(core_base, core_no, pkg_no);
229 p = GET_PKG(pkg_base, pkg_no);
231 retval = func(t, c, p);
240 int cpu_migrate(int cpu)
242 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
243 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
244 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
250 int get_msr(int cpu, off_t offset, unsigned long long *msr)
256 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
257 fd = open(pathname, O_RDONLY);
259 err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname);
261 retval = pread(fd, msr, sizeof *msr, offset);
264 if (retval != sizeof *msr)
265 err(-1, "%s offset 0x%llx read failed", pathname, (unsigned long long)offset);
271 * Example Format w/ field column widths:
273 * Package Core CPU Avg_MHz Bzy_MHz TSC_MHz SMI %Busy CPU_%c1 CPU_%c3 CPU_%c6 CPU_%c7 CoreTmp PkgTmp Pkg%pc2 Pkg%pc3 Pkg%pc6 Pkg%pc7 PkgWatt CorWatt GFXWatt
274 * 123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678
277 void print_header(void)
280 outp += sprintf(outp, " Package");
282 outp += sprintf(outp, " Core");
284 outp += sprintf(outp, " CPU");
286 outp += sprintf(outp, " Avg_MHz");
288 outp += sprintf(outp, " %%Busy");
290 outp += sprintf(outp, " Bzy_MHz");
291 outp += sprintf(outp, " TSC_MHz");
293 outp += sprintf(outp, " SMI");
294 if (extra_delta_offset32)
295 outp += sprintf(outp, " count 0x%03X", extra_delta_offset32);
296 if (extra_delta_offset64)
297 outp += sprintf(outp, " COUNT 0x%03X", extra_delta_offset64);
298 if (extra_msr_offset32)
299 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset32);
300 if (extra_msr_offset64)
301 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset64);
303 outp += sprintf(outp, " CPU%%c1");
304 if (do_nhm_cstates && !do_slm_cstates)
305 outp += sprintf(outp, " CPU%%c3");
307 outp += sprintf(outp, " CPU%%c6");
309 outp += sprintf(outp, " CPU%%c7");
312 outp += sprintf(outp, " CoreTmp");
314 outp += sprintf(outp, " PkgTmp");
317 outp += sprintf(outp, " Pkg%%pc2");
318 if (do_nhm_cstates && !do_slm_cstates)
319 outp += sprintf(outp, " Pkg%%pc3");
320 if (do_nhm_cstates && !do_slm_cstates)
321 outp += sprintf(outp, " Pkg%%pc6");
323 outp += sprintf(outp, " Pkg%%pc7");
325 outp += sprintf(outp, " Pkg%%pc8");
326 outp += sprintf(outp, " Pkg%%pc9");
327 outp += sprintf(outp, " Pk%%pc10");
330 if (do_rapl && !rapl_joules) {
331 if (do_rapl & RAPL_PKG)
332 outp += sprintf(outp, " PkgWatt");
333 if (do_rapl & RAPL_CORES)
334 outp += sprintf(outp, " CorWatt");
335 if (do_rapl & RAPL_GFX)
336 outp += sprintf(outp, " GFXWatt");
337 if (do_rapl & RAPL_DRAM)
338 outp += sprintf(outp, " RAMWatt");
339 if (do_rapl & RAPL_PKG_PERF_STATUS)
340 outp += sprintf(outp, " PKG_%%");
341 if (do_rapl & RAPL_DRAM_PERF_STATUS)
342 outp += sprintf(outp, " RAM_%%");
344 if (do_rapl & RAPL_PKG)
345 outp += sprintf(outp, " Pkg_J");
346 if (do_rapl & RAPL_CORES)
347 outp += sprintf(outp, " Cor_J");
348 if (do_rapl & RAPL_GFX)
349 outp += sprintf(outp, " GFX_J");
350 if (do_rapl & RAPL_DRAM)
351 outp += sprintf(outp, " RAM_W");
352 if (do_rapl & RAPL_PKG_PERF_STATUS)
353 outp += sprintf(outp, " PKG_%%");
354 if (do_rapl & RAPL_DRAM_PERF_STATUS)
355 outp += sprintf(outp, " RAM_%%");
356 outp += sprintf(outp, " time");
359 outp += sprintf(outp, "\n");
362 int dump_counters(struct thread_data *t, struct core_data *c,
365 outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
368 outp += sprintf(outp, "CPU: %d flags 0x%x\n",
369 t->cpu_id, t->flags);
370 outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
371 outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
372 outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
373 outp += sprintf(outp, "c1: %016llX\n", t->c1);
374 outp += sprintf(outp, "msr0x%x: %08llX\n",
375 extra_delta_offset32, t->extra_delta32);
376 outp += sprintf(outp, "msr0x%x: %016llX\n",
377 extra_delta_offset64, t->extra_delta64);
378 outp += sprintf(outp, "msr0x%x: %08llX\n",
379 extra_msr_offset32, t->extra_msr32);
380 outp += sprintf(outp, "msr0x%x: %016llX\n",
381 extra_msr_offset64, t->extra_msr64);
383 outp += sprintf(outp, "SMI: %08X\n", t->smi_count);
387 outp += sprintf(outp, "core: %d\n", c->core_id);
388 outp += sprintf(outp, "c3: %016llX\n", c->c3);
389 outp += sprintf(outp, "c6: %016llX\n", c->c6);
390 outp += sprintf(outp, "c7: %016llX\n", c->c7);
391 outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
395 outp += sprintf(outp, "package: %d\n", p->package_id);
396 outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
397 outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
398 outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
399 outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
400 outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
401 outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
402 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
403 outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
404 outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
405 outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
406 outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
407 outp += sprintf(outp, "Throttle PKG: %0X\n",
408 p->rapl_pkg_perf_status);
409 outp += sprintf(outp, "Throttle RAM: %0X\n",
410 p->rapl_dram_perf_status);
411 outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
414 outp += sprintf(outp, "\n");
420 * column formatting convention & formats
422 int format_counters(struct thread_data *t, struct core_data *c,
425 double interval_float;
428 /* if showing only 1st thread in core and this isn't one, bail out */
429 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
432 /* if showing only 1st thread in pkg and this isn't one, bail out */
433 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
436 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
438 /* topo columns, print blanks on 1st (average) line */
439 if (t == &average.threads) {
441 outp += sprintf(outp, " -");
443 outp += sprintf(outp, " -");
445 outp += sprintf(outp, " -");
449 outp += sprintf(outp, "%8d", p->package_id);
451 outp += sprintf(outp, " -");
455 outp += sprintf(outp, "%8d", c->core_id);
457 outp += sprintf(outp, " -");
460 outp += sprintf(outp, "%8d", t->cpu_id);
465 outp += sprintf(outp, "%8.0f",
466 1.0 / units * t->aperf / interval_float);
469 if (do_nhm_cstates) {
471 outp += sprintf(outp, "%8.2f", 100.0 * t->mperf/t->tsc);
473 outp += sprintf(outp, "********");
478 outp += sprintf(outp, "%8.0f",
479 1.0 * t->tsc / units * t->aperf / t->mperf / interval_float);
482 outp += sprintf(outp, "%8.0f", 1.0 * t->tsc/units/interval_float);
486 outp += sprintf(outp, "%8d", t->smi_count);
489 if (extra_delta_offset32)
490 outp += sprintf(outp, " %11llu", t->extra_delta32);
493 if (extra_delta_offset64)
494 outp += sprintf(outp, " %11llu", t->extra_delta64);
496 if (extra_msr_offset32)
497 outp += sprintf(outp, " 0x%08llx", t->extra_msr32);
500 if (extra_msr_offset64)
501 outp += sprintf(outp, " 0x%016llx", t->extra_msr64);
503 if (do_nhm_cstates) {
505 outp += sprintf(outp, "%8.2f", 100.0 * t->c1/t->tsc);
507 outp += sprintf(outp, "********");
510 /* print per-core data only for 1st thread in core */
511 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
514 if (do_nhm_cstates && !do_slm_cstates)
515 outp += sprintf(outp, "%8.2f", 100.0 * c->c3/t->tsc);
517 outp += sprintf(outp, "%8.2f", 100.0 * c->c6/t->tsc);
519 outp += sprintf(outp, "%8.2f", 100.0 * c->c7/t->tsc);
522 outp += sprintf(outp, "%8d", c->core_temp_c);
524 /* print per-package data only for 1st core in package */
525 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
529 outp += sprintf(outp, "%8d", p->pkg_temp_c);
532 outp += sprintf(outp, "%8.2f", 100.0 * p->pc2/t->tsc);
533 if (do_nhm_cstates && !do_slm_cstates)
534 outp += sprintf(outp, "%8.2f", 100.0 * p->pc3/t->tsc);
535 if (do_nhm_cstates && !do_slm_cstates)
536 outp += sprintf(outp, "%8.2f", 100.0 * p->pc6/t->tsc);
538 outp += sprintf(outp, "%8.2f", 100.0 * p->pc7/t->tsc);
540 outp += sprintf(outp, "%8.2f", 100.0 * p->pc8/t->tsc);
541 outp += sprintf(outp, "%8.2f", 100.0 * p->pc9/t->tsc);
542 outp += sprintf(outp, "%8.2f", 100.0 * p->pc10/t->tsc);
546 * If measurement interval exceeds minimum RAPL Joule Counter range,
547 * indicate that results are suspect by printing "**" in fraction place.
549 if (interval_float < rapl_joule_counter_range)
554 if (do_rapl && !rapl_joules) {
555 if (do_rapl & RAPL_PKG)
556 outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units / interval_float);
557 if (do_rapl & RAPL_CORES)
558 outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units / interval_float);
559 if (do_rapl & RAPL_GFX)
560 outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units / interval_float);
561 if (do_rapl & RAPL_DRAM)
562 outp += sprintf(outp, fmt8, p->energy_dram * rapl_energy_units / interval_float);
563 if (do_rapl & RAPL_PKG_PERF_STATUS)
564 outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
565 if (do_rapl & RAPL_DRAM_PERF_STATUS)
566 outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
568 if (do_rapl & RAPL_PKG)
569 outp += sprintf(outp, fmt8,
570 p->energy_pkg * rapl_energy_units);
571 if (do_rapl & RAPL_CORES)
572 outp += sprintf(outp, fmt8,
573 p->energy_cores * rapl_energy_units);
574 if (do_rapl & RAPL_GFX)
575 outp += sprintf(outp, fmt8,
576 p->energy_gfx * rapl_energy_units);
577 if (do_rapl & RAPL_DRAM)
578 outp += sprintf(outp, fmt8,
579 p->energy_dram * rapl_energy_units);
580 if (do_rapl & RAPL_PKG_PERF_STATUS)
581 outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
582 if (do_rapl & RAPL_DRAM_PERF_STATUS)
583 outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
584 outp += sprintf(outp, fmt8, interval_float);
588 outp += sprintf(outp, "\n");
595 fputs(output_buffer, stdout);
597 outp = output_buffer;
601 fputs(output_buffer, stderr);
602 outp = output_buffer;
604 void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
608 if (!printed || !summary_only)
611 if (topo.num_cpus > 1)
612 format_counters(&average.threads, &average.cores,
620 for_all_cpus(format_counters, t, c, p);
623 #define DELTA_WRAP32(new, old) \
627 old = 0x100000000 + new - old; \
631 delta_package(struct pkg_data *new, struct pkg_data *old)
633 old->pc2 = new->pc2 - old->pc2;
634 old->pc3 = new->pc3 - old->pc3;
635 old->pc6 = new->pc6 - old->pc6;
636 old->pc7 = new->pc7 - old->pc7;
637 old->pc8 = new->pc8 - old->pc8;
638 old->pc9 = new->pc9 - old->pc9;
639 old->pc10 = new->pc10 - old->pc10;
640 old->pkg_temp_c = new->pkg_temp_c;
642 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
643 DELTA_WRAP32(new->energy_cores, old->energy_cores);
644 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
645 DELTA_WRAP32(new->energy_dram, old->energy_dram);
646 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
647 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
651 delta_core(struct core_data *new, struct core_data *old)
653 old->c3 = new->c3 - old->c3;
654 old->c6 = new->c6 - old->c6;
655 old->c7 = new->c7 - old->c7;
656 old->core_temp_c = new->core_temp_c;
663 delta_thread(struct thread_data *new, struct thread_data *old,
664 struct core_data *core_delta)
666 old->tsc = new->tsc - old->tsc;
668 /* check for TSC < 1 Mcycles over interval */
669 if (old->tsc < (1000 * 1000))
670 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
671 "You can disable all c-states by booting with \"idle=poll\"\n"
672 "or just the deep ones with \"processor.max_cstate=1\"");
674 old->c1 = new->c1 - old->c1;
676 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
677 old->aperf = new->aperf - old->aperf;
678 old->mperf = new->mperf - old->mperf;
681 if (!aperf_mperf_unstable) {
682 fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname);
683 fprintf(stderr, "* Frequency results do not cover entire interval *\n");
684 fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n");
686 aperf_mperf_unstable = 1;
689 * mperf delta is likely a huge "positive" number
690 * can not use it for calculating c0 time
697 if (use_c1_residency_msr) {
699 * Some models have a dedicated C1 residency MSR,
700 * which should be more accurate than the derivation below.
704 * As counter collection is not atomic,
705 * it is possible for mperf's non-halted cycles + idle states
706 * to exceed TSC's all cycles: show c1 = 0% in that case.
708 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
711 /* normal case, derive c1 */
712 old->c1 = old->tsc - old->mperf - core_delta->c3
713 - core_delta->c6 - core_delta->c7;
717 if (old->mperf == 0) {
718 if (verbose > 1) fprintf(stderr, "cpu%d MPERF 0!\n", old->cpu_id);
719 old->mperf = 1; /* divide by 0 protection */
722 old->extra_delta32 = new->extra_delta32 - old->extra_delta32;
723 old->extra_delta32 &= 0xFFFFFFFF;
725 old->extra_delta64 = new->extra_delta64 - old->extra_delta64;
728 * Extra MSR is just a snapshot, simply copy latest w/o subtracting
730 old->extra_msr32 = new->extra_msr32;
731 old->extra_msr64 = new->extra_msr64;
734 old->smi_count = new->smi_count - old->smi_count;
737 int delta_cpu(struct thread_data *t, struct core_data *c,
738 struct pkg_data *p, struct thread_data *t2,
739 struct core_data *c2, struct pkg_data *p2)
741 /* calculate core delta only for 1st thread in core */
742 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
745 /* always calculate thread delta */
746 delta_thread(t, t2, c2); /* c2 is core delta */
748 /* calculate package delta only for 1st core in package */
749 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
750 delta_package(p, p2);
755 void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
763 t->extra_delta32 = 0;
764 t->extra_delta64 = 0;
766 /* tells format_counters to dump all fields from this set */
767 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
786 p->rapl_pkg_perf_status = 0;
787 p->rapl_dram_perf_status = 0;
790 int sum_counters(struct thread_data *t, struct core_data *c,
793 average.threads.tsc += t->tsc;
794 average.threads.aperf += t->aperf;
795 average.threads.mperf += t->mperf;
796 average.threads.c1 += t->c1;
798 average.threads.extra_delta32 += t->extra_delta32;
799 average.threads.extra_delta64 += t->extra_delta64;
801 /* sum per-core values only for 1st thread in core */
802 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
805 average.cores.c3 += c->c3;
806 average.cores.c6 += c->c6;
807 average.cores.c7 += c->c7;
809 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
811 /* sum per-pkg values only for 1st core in pkg */
812 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
815 average.packages.pc2 += p->pc2;
816 average.packages.pc3 += p->pc3;
817 average.packages.pc6 += p->pc6;
818 average.packages.pc7 += p->pc7;
819 average.packages.pc8 += p->pc8;
820 average.packages.pc9 += p->pc9;
821 average.packages.pc10 += p->pc10;
823 average.packages.energy_pkg += p->energy_pkg;
824 average.packages.energy_dram += p->energy_dram;
825 average.packages.energy_cores += p->energy_cores;
826 average.packages.energy_gfx += p->energy_gfx;
828 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
830 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
831 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
835 * sum the counters for all cpus in the system
836 * compute the weighted average
838 void compute_average(struct thread_data *t, struct core_data *c,
841 clear_counters(&average.threads, &average.cores, &average.packages);
843 for_all_cpus(sum_counters, t, c, p);
845 average.threads.tsc /= topo.num_cpus;
846 average.threads.aperf /= topo.num_cpus;
847 average.threads.mperf /= topo.num_cpus;
848 average.threads.c1 /= topo.num_cpus;
850 average.threads.extra_delta32 /= topo.num_cpus;
851 average.threads.extra_delta32 &= 0xFFFFFFFF;
853 average.threads.extra_delta64 /= topo.num_cpus;
855 average.cores.c3 /= topo.num_cores;
856 average.cores.c6 /= topo.num_cores;
857 average.cores.c7 /= topo.num_cores;
859 average.packages.pc2 /= topo.num_packages;
860 average.packages.pc3 /= topo.num_packages;
861 average.packages.pc6 /= topo.num_packages;
862 average.packages.pc7 /= topo.num_packages;
864 average.packages.pc8 /= topo.num_packages;
865 average.packages.pc9 /= topo.num_packages;
866 average.packages.pc10 /= topo.num_packages;
869 static unsigned long long rdtsc(void)
871 unsigned int low, high;
873 asm volatile("rdtsc" : "=a" (low), "=d" (high));
875 return low | ((unsigned long long)high) << 32;
882 * acquire and record local counters for that cpu
884 int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
887 unsigned long long msr;
889 if (cpu_migrate(cpu)) {
890 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
894 t->tsc = rdtsc(); /* we are running on local CPU of interest */
897 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
899 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
904 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
906 t->smi_count = msr & 0xFFFFFFFF;
908 if (extra_delta_offset32) {
909 if (get_msr(cpu, extra_delta_offset32, &msr))
911 t->extra_delta32 = msr & 0xFFFFFFFF;
914 if (extra_delta_offset64)
915 if (get_msr(cpu, extra_delta_offset64, &t->extra_delta64))
918 if (extra_msr_offset32) {
919 if (get_msr(cpu, extra_msr_offset32, &msr))
921 t->extra_msr32 = msr & 0xFFFFFFFF;
924 if (extra_msr_offset64)
925 if (get_msr(cpu, extra_msr_offset64, &t->extra_msr64))
928 if (use_c1_residency_msr) {
929 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
933 /* collect core counters only for 1st thread in core */
934 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
937 if (do_nhm_cstates && !do_slm_cstates) {
938 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
942 if (do_nhm_cstates) {
943 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
948 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
952 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
954 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
958 /* collect package counters only for 1st core in package */
959 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
962 if (do_nhm_cstates && !do_slm_cstates) {
963 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
965 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
968 if (do_snb_cstates) {
969 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
971 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
975 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
977 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
979 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
982 if (do_rapl & RAPL_PKG) {
983 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
985 p->energy_pkg = msr & 0xFFFFFFFF;
987 if (do_rapl & RAPL_CORES) {
988 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
990 p->energy_cores = msr & 0xFFFFFFFF;
992 if (do_rapl & RAPL_DRAM) {
993 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
995 p->energy_dram = msr & 0xFFFFFFFF;
997 if (do_rapl & RAPL_GFX) {
998 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1000 p->energy_gfx = msr & 0xFFFFFFFF;
1002 if (do_rapl & RAPL_PKG_PERF_STATUS) {
1003 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1005 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1007 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1008 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1010 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1013 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1015 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1020 void print_verbose_header(void)
1022 unsigned long long msr;
1025 if (!do_nehalem_platform_info)
1028 get_msr(0, MSR_NHM_PLATFORM_INFO, &msr);
1030 fprintf(stderr, "cpu0: MSR_NHM_PLATFORM_INFO: 0x%08llx\n", msr);
1032 ratio = (msr >> 40) & 0xFF;
1033 fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency\n",
1034 ratio, bclk, ratio * bclk);
1036 ratio = (msr >> 8) & 0xFF;
1037 fprintf(stderr, "%d * %.0f = %.0f MHz TSC frequency\n",
1038 ratio, bclk, ratio * bclk);
1040 get_msr(0, MSR_IA32_POWER_CTL, &msr);
1041 fprintf(stderr, "cpu0: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
1042 msr, msr & 0x2 ? "EN" : "DIS");
1044 if (!do_ivt_turbo_ratio_limit)
1045 goto print_nhm_turbo_ratio_limits;
1047 get_msr(0, MSR_IVT_TURBO_RATIO_LIMIT, &msr);
1049 fprintf(stderr, "cpu0: MSR_IVT_TURBO_RATIO_LIMIT: 0x%08llx\n", msr);
1051 ratio = (msr >> 56) & 0xFF;
1053 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 16 active cores\n",
1054 ratio, bclk, ratio * bclk);
1056 ratio = (msr >> 48) & 0xFF;
1058 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 15 active cores\n",
1059 ratio, bclk, ratio * bclk);
1061 ratio = (msr >> 40) & 0xFF;
1063 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 14 active cores\n",
1064 ratio, bclk, ratio * bclk);
1066 ratio = (msr >> 32) & 0xFF;
1068 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 13 active cores\n",
1069 ratio, bclk, ratio * bclk);
1071 ratio = (msr >> 24) & 0xFF;
1073 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 12 active cores\n",
1074 ratio, bclk, ratio * bclk);
1076 ratio = (msr >> 16) & 0xFF;
1078 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 11 active cores\n",
1079 ratio, bclk, ratio * bclk);
1081 ratio = (msr >> 8) & 0xFF;
1083 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 10 active cores\n",
1084 ratio, bclk, ratio * bclk);
1086 ratio = (msr >> 0) & 0xFF;
1088 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 9 active cores\n",
1089 ratio, bclk, ratio * bclk);
1091 print_nhm_turbo_ratio_limits:
1092 get_msr(0, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
1094 #define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
1095 #define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
1097 fprintf(stderr, "cpu0: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", msr);
1099 fprintf(stderr, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: ",
1100 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
1101 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
1102 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
1103 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
1104 (msr & (1 << 15)) ? "" : "UN",
1105 (unsigned int)msr & 7);
1110 fprintf(stderr, do_slm_cstates ? "no pkg states" : "pc0");
1113 fprintf(stderr, do_slm_cstates ? "no pkg states" : do_snb_cstates ? "pc2" : "pc0");
1116 fprintf(stderr, do_slm_cstates ? "invalid" : do_snb_cstates ? "pc6-noret" : "pc3");
1119 fprintf(stderr, do_slm_cstates ? "invalid" : "pc6");
1122 fprintf(stderr, do_slm_cstates ? "pc4" : "pc7");
1125 fprintf(stderr, do_slm_cstates ? "invalid" : do_snb_cstates ? "pc7s" : "invalid");
1128 fprintf(stderr, do_slm_cstates ? "pc6" : "invalid");
1131 fprintf(stderr, do_slm_cstates ? "pc7" : "unlimited");
1134 fprintf(stderr, "invalid");
1136 fprintf(stderr, ")\n");
1138 if (!do_nehalem_turbo_ratio_limit)
1141 get_msr(0, MSR_NHM_TURBO_RATIO_LIMIT, &msr);
1143 fprintf(stderr, "cpu0: MSR_NHM_TURBO_RATIO_LIMIT: 0x%08llx\n", msr);
1145 ratio = (msr >> 56) & 0xFF;
1147 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 8 active cores\n",
1148 ratio, bclk, ratio * bclk);
1150 ratio = (msr >> 48) & 0xFF;
1152 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 7 active cores\n",
1153 ratio, bclk, ratio * bclk);
1155 ratio = (msr >> 40) & 0xFF;
1157 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 6 active cores\n",
1158 ratio, bclk, ratio * bclk);
1160 ratio = (msr >> 32) & 0xFF;
1162 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 5 active cores\n",
1163 ratio, bclk, ratio * bclk);
1165 ratio = (msr >> 24) & 0xFF;
1167 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
1168 ratio, bclk, ratio * bclk);
1170 ratio = (msr >> 16) & 0xFF;
1172 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
1173 ratio, bclk, ratio * bclk);
1175 ratio = (msr >> 8) & 0xFF;
1177 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
1178 ratio, bclk, ratio * bclk);
1180 ratio = (msr >> 0) & 0xFF;
1182 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
1183 ratio, bclk, ratio * bclk);
1187 void free_all_buffers(void)
1189 CPU_FREE(cpu_present_set);
1190 cpu_present_set = NULL;
1191 cpu_present_set = 0;
1193 CPU_FREE(cpu_affinity_set);
1194 cpu_affinity_set = NULL;
1195 cpu_affinity_setsize = 0;
1203 package_even = NULL;
1213 free(output_buffer);
1214 output_buffer = NULL;
1219 * Open a file, and exit on failure
1221 FILE *fopen_or_die(const char *path, const char *mode)
1223 FILE *filep = fopen(path, "r");
1225 err(1, "%s: open failed", path);
1230 * Parse a file containing a single int.
1232 int parse_int_file(const char *fmt, ...)
1235 char path[PATH_MAX];
1239 va_start(args, fmt);
1240 vsnprintf(path, sizeof(path), fmt, args);
1242 filep = fopen_or_die(path, "r");
1243 if (fscanf(filep, "%d", &value) != 1)
1244 err(1, "%s: failed to parse number from file", path);
1250 * cpu_is_first_sibling_in_core(cpu)
1251 * return 1 if given CPU is 1st HT sibling in the core
1253 int cpu_is_first_sibling_in_core(int cpu)
1255 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
1259 * cpu_is_first_core_in_package(cpu)
1260 * return 1 if given CPU is 1st core in package
1262 int cpu_is_first_core_in_package(int cpu)
1264 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
1267 int get_physical_package_id(int cpu)
1269 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
1272 int get_core_id(int cpu)
1274 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
1277 int get_num_ht_siblings(int cpu)
1285 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
1286 filep = fopen_or_die(path, "r");
1289 * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4)
1290 * otherwinse 1 sibling (self).
1292 matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2);
1303 * run func(thread, core, package) in topology order
1304 * skip non-present cpus
1307 int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
1308 struct pkg_data *, struct thread_data *, struct core_data *,
1309 struct pkg_data *), struct thread_data *thread_base,
1310 struct core_data *core_base, struct pkg_data *pkg_base,
1311 struct thread_data *thread_base2, struct core_data *core_base2,
1312 struct pkg_data *pkg_base2)
1314 int retval, pkg_no, core_no, thread_no;
1316 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
1317 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
1318 for (thread_no = 0; thread_no <
1319 topo.num_threads_per_core; ++thread_no) {
1320 struct thread_data *t, *t2;
1321 struct core_data *c, *c2;
1322 struct pkg_data *p, *p2;
1324 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
1326 if (cpu_is_not_present(t->cpu_id))
1329 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
1331 c = GET_CORE(core_base, core_no, pkg_no);
1332 c2 = GET_CORE(core_base2, core_no, pkg_no);
1334 p = GET_PKG(pkg_base, pkg_no);
1335 p2 = GET_PKG(pkg_base2, pkg_no);
1337 retval = func(t, c, p, t2, c2, p2);
1347 * run func(cpu) on every cpu in /proc/stat
1348 * return max_cpu number
1350 int for_all_proc_cpus(int (func)(int))
1356 fp = fopen_or_die(proc_stat, "r");
1358 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
1360 err(1, "%s: failed to parse format", proc_stat);
1363 retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
1367 retval = func(cpu_num);
1377 void re_initialize(void)
1380 setup_all_buffers();
1381 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
1387 * remember the last one seen, it will be the max
1389 int count_cpus(int cpu)
1391 if (topo.max_cpu_num < cpu)
1392 topo.max_cpu_num = cpu;
1397 int mark_cpu_present(int cpu)
1399 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
1403 void turbostat_loop()
1411 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
1414 } else if (retval == -1) {
1415 if (restarted > 1) {
1422 gettimeofday(&tv_even, (struct timezone *)NULL);
1425 if (for_all_proc_cpus(cpu_is_not_present)) {
1429 sleep(interval_sec);
1430 retval = for_all_cpus(get_counters, ODD_COUNTERS);
1433 } else if (retval == -1) {
1437 gettimeofday(&tv_odd, (struct timezone *)NULL);
1438 timersub(&tv_odd, &tv_even, &tv_delta);
1439 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
1440 compute_average(EVEN_COUNTERS);
1441 format_all_counters(EVEN_COUNTERS);
1443 sleep(interval_sec);
1444 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
1447 } else if (retval == -1) {
1451 gettimeofday(&tv_even, (struct timezone *)NULL);
1452 timersub(&tv_even, &tv_odd, &tv_delta);
1453 for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS);
1454 compute_average(ODD_COUNTERS);
1455 format_all_counters(ODD_COUNTERS);
1460 void check_dev_msr()
1464 if (stat("/dev/cpu/0/msr", &sb))
1465 err(-5, "no /dev/cpu/0/msr\n"
1466 "Try \"# modprobe msr\"");
1469 void check_permissions()
1471 struct __user_cap_header_struct cap_header_data;
1472 cap_user_header_t cap_header = &cap_header_data;
1473 struct __user_cap_data_struct cap_data_data;
1474 cap_user_data_t cap_data = &cap_data_data;
1475 extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
1478 /* check for CAP_SYS_RAWIO */
1479 cap_header->pid = getpid();
1480 cap_header->version = _LINUX_CAPABILITY_VERSION;
1481 if (capget(cap_header, cap_data) < 0)
1482 err(-6, "capget(2) failed");
1484 if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
1486 warnx("capget(CAP_SYS_RAWIO) failed,"
1487 " try \"# setcap cap_sys_rawio=ep %s\"", progname);
1490 /* test file permissions */
1491 if (euidaccess("/dev/cpu/0/msr", R_OK)) {
1493 warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
1496 /* if all else fails, thell them to be root */
1499 warnx("Or simply run as root");
1505 int has_nehalem_turbo_ratio_limit(unsigned int family, unsigned int model)
1514 case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
1515 case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
1516 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
1517 case 0x25: /* Westmere Client - Clarkdale, Arrandale */
1518 case 0x2C: /* Westmere EP - Gulftown */
1519 case 0x2A: /* SNB */
1520 case 0x2D: /* SNB Xeon */
1521 case 0x3A: /* IVB */
1522 case 0x3E: /* IVB Xeon */
1523 case 0x3C: /* HSW */
1524 case 0x3F: /* HSX */
1525 case 0x45: /* HSW */
1526 case 0x46: /* HSW */
1527 case 0x37: /* BYT */
1528 case 0x4D: /* AVN */
1529 case 0x3D: /* BDW */
1530 case 0x4F: /* BDX */
1531 case 0x56: /* BDX-DE */
1533 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1534 case 0x2F: /* Westmere-EX Xeon - Eagleton */
1539 int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
1548 case 0x3E: /* IVB Xeon */
1557 * Decode the ENERGY_PERF_BIAS MSR
1559 int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1561 unsigned long long msr;
1570 /* EPB is per-package */
1571 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1574 if (cpu_migrate(cpu)) {
1575 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1579 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
1582 switch (msr & 0x7) {
1583 case ENERGY_PERF_BIAS_PERFORMANCE:
1584 epb_string = "performance";
1586 case ENERGY_PERF_BIAS_NORMAL:
1587 epb_string = "balanced";
1589 case ENERGY_PERF_BIAS_POWERSAVE:
1590 epb_string = "powersave";
1593 epb_string = "custom";
1596 fprintf(stderr, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
1602 * print_perf_limit()
1604 int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1606 unsigned long long msr;
1612 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1615 if (cpu_migrate(cpu)) {
1616 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1620 if (do_core_perf_limit_reasons) {
1621 get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
1622 fprintf(stderr, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
1623 fprintf(stderr, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
1624 (msr & 1 << 0) ? "PROCHOT, " : "",
1625 (msr & 1 << 1) ? "ThermStatus, " : "",
1626 (msr & 1 << 2) ? "bit2, " : "",
1627 (msr & 1 << 4) ? "Graphics, " : "",
1628 (msr & 1 << 5) ? "Auto-HWP, " : "",
1629 (msr & 1 << 6) ? "VR-Therm, " : "",
1630 (msr & 1 << 8) ? "Amps, " : "",
1631 (msr & 1 << 9) ? "CorePwr, " : "",
1632 (msr & 1 << 10) ? "PkgPwrL1, " : "",
1633 (msr & 1 << 11) ? "PkgPwrL2, " : "",
1634 (msr & 1 << 12) ? "MultiCoreTurbo, " : "",
1635 (msr & 1 << 13) ? "Transitions, " : "",
1636 (msr & 1 << 14) ? "bit14, " : "",
1637 (msr & 1 << 15) ? "bit15, " : "");
1638 fprintf(stderr, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n",
1639 (msr & 1 << 16) ? "PROCHOT, " : "",
1640 (msr & 1 << 17) ? "ThermStatus, " : "",
1641 (msr & 1 << 18) ? "bit18, " : "",
1642 (msr & 1 << 20) ? "Graphics, " : "",
1643 (msr & 1 << 21) ? "Auto-HWP, " : "",
1644 (msr & 1 << 22) ? "VR-Therm, " : "",
1645 (msr & 1 << 24) ? "Amps, " : "",
1646 (msr & 1 << 25) ? "CorePwr, " : "",
1647 (msr & 1 << 26) ? "PkgPwrL1, " : "",
1648 (msr & 1 << 27) ? "PkgPwrL2, " : "",
1649 (msr & 1 << 28) ? "MultiCoreTurbo, " : "",
1650 (msr & 1 << 29) ? "Transitions, " : "",
1651 (msr & 1 << 30) ? "bit30, " : "",
1652 (msr & 1 << 31) ? "bit31, " : "");
1655 if (do_gfx_perf_limit_reasons) {
1656 get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
1657 fprintf(stderr, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
1658 fprintf(stderr, " (Active: %s%s%s%s%s%s%s%s)",
1659 (msr & 1 << 0) ? "PROCHOT, " : "",
1660 (msr & 1 << 1) ? "ThermStatus, " : "",
1661 (msr & 1 << 4) ? "Graphics, " : "",
1662 (msr & 1 << 6) ? "VR-Therm, " : "",
1663 (msr & 1 << 8) ? "Amps, " : "",
1664 (msr & 1 << 9) ? "GFXPwr, " : "",
1665 (msr & 1 << 10) ? "PkgPwrL1, " : "",
1666 (msr & 1 << 11) ? "PkgPwrL2, " : "");
1667 fprintf(stderr, " (Logged: %s%s%s%s%s%s%s%s)\n",
1668 (msr & 1 << 16) ? "PROCHOT, " : "",
1669 (msr & 1 << 17) ? "ThermStatus, " : "",
1670 (msr & 1 << 20) ? "Graphics, " : "",
1671 (msr & 1 << 22) ? "VR-Therm, " : "",
1672 (msr & 1 << 24) ? "Amps, " : "",
1673 (msr & 1 << 25) ? "GFXPwr, " : "",
1674 (msr & 1 << 26) ? "PkgPwrL1, " : "",
1675 (msr & 1 << 27) ? "PkgPwrL2, " : "");
1677 if (do_ring_perf_limit_reasons) {
1678 get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
1679 fprintf(stderr, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
1680 fprintf(stderr, " (Active: %s%s%s%s%s%s)",
1681 (msr & 1 << 0) ? "PROCHOT, " : "",
1682 (msr & 1 << 1) ? "ThermStatus, " : "",
1683 (msr & 1 << 6) ? "VR-Therm, " : "",
1684 (msr & 1 << 8) ? "Amps, " : "",
1685 (msr & 1 << 10) ? "PkgPwrL1, " : "",
1686 (msr & 1 << 11) ? "PkgPwrL2, " : "");
1687 fprintf(stderr, " (Logged: %s%s%s%s%s%s)\n",
1688 (msr & 1 << 16) ? "PROCHOT, " : "",
1689 (msr & 1 << 17) ? "ThermStatus, " : "",
1690 (msr & 1 << 22) ? "VR-Therm, " : "",
1691 (msr & 1 << 24) ? "Amps, " : "",
1692 (msr & 1 << 26) ? "PkgPwrL1, " : "",
1693 (msr & 1 << 27) ? "PkgPwrL2, " : "");
1698 #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
1699 #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
1701 double get_tdp(model)
1703 unsigned long long msr;
1705 if (do_rapl & RAPL_PKG_POWER_INFO)
1706 if (!get_msr(0, MSR_PKG_POWER_INFO, &msr))
1707 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
1722 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
1724 void rapl_probe(unsigned int family, unsigned int model)
1726 unsigned long long msr;
1727 unsigned int time_unit;
1739 case 0x3C: /* HSW */
1740 case 0x45: /* HSW */
1741 case 0x46: /* HSW */
1742 case 0x3D: /* BDW */
1743 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
1745 case 0x3F: /* HSX */
1746 case 0x4F: /* BDX */
1747 case 0x56: /* BDX-DE */
1748 do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
1752 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_PKG_PERF_STATUS | RAPL_DRAM_PERF_STATUS | RAPL_PKG_POWER_INFO;
1754 case 0x37: /* BYT */
1755 case 0x4D: /* AVN */
1756 do_rapl = RAPL_PKG | RAPL_CORES ;
1762 /* units on package 0, verify later other packages match */
1763 if (get_msr(0, MSR_RAPL_POWER_UNIT, &msr))
1766 rapl_power_units = 1.0 / (1 << (msr & 0xF));
1768 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
1770 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
1772 time_unit = msr >> 16 & 0xF;
1776 rapl_time_units = 1.0 / (1 << (time_unit));
1778 tdp = get_tdp(model);
1780 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
1782 fprintf(stderr, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
1787 void perf_limit_reasons_probe(family, model)
1796 case 0x3C: /* HSW */
1797 case 0x45: /* HSW */
1798 case 0x46: /* HSW */
1799 do_gfx_perf_limit_reasons = 1;
1800 case 0x3F: /* HSX */
1801 do_core_perf_limit_reasons = 1;
1802 do_ring_perf_limit_reasons = 1;
1808 int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1810 unsigned long long msr;
1814 if (!(do_dts || do_ptm))
1819 /* DTS is per-core, no need to print for each thread */
1820 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1823 if (cpu_migrate(cpu)) {
1824 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1828 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
1829 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1832 dts = (msr >> 16) & 0x7F;
1833 fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
1834 cpu, msr, tcc_activation_temp - dts);
1837 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
1840 dts = (msr >> 16) & 0x7F;
1841 dts2 = (msr >> 8) & 0x7F;
1842 fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
1843 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
1849 unsigned int resolution;
1851 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1854 dts = (msr >> 16) & 0x7F;
1855 resolution = (msr >> 27) & 0xF;
1856 fprintf(stderr, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
1857 cpu, msr, tcc_activation_temp - dts, resolution);
1860 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
1863 dts = (msr >> 16) & 0x7F;
1864 dts2 = (msr >> 8) & 0x7F;
1865 fprintf(stderr, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
1866 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
1873 void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
1875 fprintf(stderr, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
1877 ((msr >> 15) & 1) ? "EN" : "DIS",
1878 ((msr >> 0) & 0x7FFF) * rapl_power_units,
1879 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
1880 (((msr >> 16) & 1) ? "EN" : "DIS"));
1885 int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1887 unsigned long long msr;
1893 /* RAPL counters are per package, so print only for 1st thread/package */
1894 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1898 if (cpu_migrate(cpu)) {
1899 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1903 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
1907 fprintf(stderr, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx "
1908 "(%f Watts, %f Joules, %f sec.)\n", cpu, msr,
1909 rapl_power_units, rapl_energy_units, rapl_time_units);
1911 if (do_rapl & RAPL_PKG_POWER_INFO) {
1913 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
1917 fprintf(stderr, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
1919 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1920 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1921 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1922 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
1925 if (do_rapl & RAPL_PKG) {
1927 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
1930 fprintf(stderr, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
1931 cpu, msr, (msr >> 63) & 1 ? "": "UN");
1933 print_power_limit_msr(cpu, msr, "PKG Limit #1");
1934 fprintf(stderr, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
1936 ((msr >> 47) & 1) ? "EN" : "DIS",
1937 ((msr >> 32) & 0x7FFF) * rapl_power_units,
1938 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
1939 ((msr >> 48) & 1) ? "EN" : "DIS");
1942 if (do_rapl & RAPL_DRAM) {
1943 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
1947 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
1949 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1950 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1951 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1952 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
1955 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
1957 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
1958 cpu, msr, (msr >> 31) & 1 ? "": "UN");
1960 print_power_limit_msr(cpu, msr, "DRAM Limit");
1962 if (do_rapl & RAPL_CORE_POLICY) {
1964 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
1967 fprintf(stderr, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
1970 if (do_rapl & RAPL_CORES) {
1973 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
1975 fprintf(stderr, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
1976 cpu, msr, (msr >> 31) & 1 ? "": "UN");
1977 print_power_limit_msr(cpu, msr, "Cores Limit");
1980 if (do_rapl & RAPL_GFX) {
1982 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
1985 fprintf(stderr, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
1987 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
1989 fprintf(stderr, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
1990 cpu, msr, (msr >> 31) & 1 ? "": "UN");
1991 print_power_limit_msr(cpu, msr, "GFX Limit");
1998 int is_snb(unsigned int family, unsigned int model)
2006 case 0x3A: /* IVB */
2007 case 0x3E: /* IVB Xeon */
2008 case 0x3C: /* HSW */
2009 case 0x3F: /* HSW */
2010 case 0x45: /* HSW */
2011 case 0x46: /* HSW */
2012 case 0x3D: /* BDW */
2013 case 0x4F: /* BDX */
2014 case 0x56: /* BDX-DE */
2020 int has_c8_c9_c10(unsigned int family, unsigned int model)
2026 case 0x45: /* HSW */
2027 case 0x3D: /* BDW */
2034 int is_slm(unsigned int family, unsigned int model)
2039 case 0x37: /* BYT */
2040 case 0x4D: /* AVN */
2046 #define SLM_BCLK_FREQS 5
2047 double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
2049 double slm_bclk(void)
2051 unsigned long long msr = 3;
2055 if (get_msr(0, MSR_FSB_FREQ, &msr))
2056 fprintf(stderr, "SLM BCLK: unknown\n");
2059 if (i >= SLM_BCLK_FREQS) {
2060 fprintf(stderr, "SLM BCLK[%d] invalid\n", i);
2063 freq = slm_freq_table[i];
2065 fprintf(stderr, "SLM BCLK: %.1f Mhz\n", freq);
2070 double discover_bclk(unsigned int family, unsigned int model)
2072 if (is_snb(family, model))
2074 else if (is_slm(family, model))
2081 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
2082 * the Thermal Control Circuit (TCC) activates.
2083 * This is usually equal to tjMax.
2085 * Older processors do not have this MSR, so there we guess,
2086 * but also allow cmdline over-ride with -T.
2088 * Several MSR temperature values are in units of degrees-C
2089 * below this value, including the Digital Thermal Sensor (DTS),
2090 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
2092 int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2094 unsigned long long msr;
2095 unsigned int target_c_local;
2098 /* tcc_activation_temp is used only for dts or ptm */
2099 if (!(do_dts || do_ptm))
2102 /* this is a per-package concept */
2103 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2107 if (cpu_migrate(cpu)) {
2108 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
2112 if (tcc_activation_temp_override != 0) {
2113 tcc_activation_temp = tcc_activation_temp_override;
2114 fprintf(stderr, "cpu%d: Using cmdline TCC Target (%d C)\n",
2115 cpu, tcc_activation_temp);
2119 /* Temperature Target MSR is Nehalem and newer only */
2120 if (!do_nehalem_platform_info)
2123 if (get_msr(0, MSR_IA32_TEMPERATURE_TARGET, &msr))
2126 target_c_local = (msr >> 16) & 0xFF;
2129 fprintf(stderr, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
2130 cpu, msr, target_c_local);
2132 if (!target_c_local)
2135 tcc_activation_temp = target_c_local;
2140 tcc_activation_temp = TJMAX_DEFAULT;
2141 fprintf(stderr, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
2142 cpu, tcc_activation_temp);
2148 unsigned int eax, ebx, ecx, edx, max_level;
2149 unsigned int fms, family, model, stepping;
2151 eax = ebx = ecx = edx = 0;
2153 __get_cpuid(0, &max_level, &ebx, &ecx, &edx);
2155 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
2159 fprintf(stderr, "CPUID(0): %.4s%.4s%.4s ",
2160 (char *)&ebx, (char *)&edx, (char *)&ecx);
2162 __get_cpuid(1, &fms, &ebx, &ecx, &edx);
2163 family = (fms >> 8) & 0xf;
2164 model = (fms >> 4) & 0xf;
2165 stepping = fms & 0xf;
2166 if (family == 6 || family == 0xf)
2167 model += ((fms >> 16) & 0xf) << 4;
2170 fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
2171 max_level, family, model, stepping, family, model, stepping);
2173 if (!(edx & (1 << 5)))
2174 errx(1, "CPUID: no MSR");
2177 * check max extended function levels of CPUID.
2178 * This is needed to check for invariant TSC.
2179 * This check is valid for both Intel and AMD.
2181 ebx = ecx = edx = 0;
2182 __get_cpuid(0x80000000, &max_level, &ebx, &ecx, &edx);
2184 if (max_level < 0x80000007)
2185 errx(1, "CPUID: no invariant TSC (max_level 0x%x)", max_level);
2188 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
2189 * this check is valid for both Intel and AMD
2191 __get_cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
2192 has_invariant_tsc = edx & (1 << 8);
2194 if (!has_invariant_tsc)
2195 errx(1, "No invariant TSC");
2198 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
2199 * this check is valid for both Intel and AMD
2202 __get_cpuid(0x6, &eax, &ebx, &ecx, &edx);
2203 has_aperf = ecx & (1 << 0);
2204 do_dts = eax & (1 << 0);
2205 do_ptm = eax & (1 << 6);
2206 has_epb = ecx & (1 << 3);
2209 fprintf(stderr, "CPUID(6): %s%s%s%s\n",
2210 has_aperf ? "APERF" : "No APERF!",
2211 do_dts ? ", DTS" : "",
2212 do_ptm ? ", PTM": "",
2213 has_epb ? ", EPB": "");
2216 errx(-1, "No APERF");
2218 do_nehalem_platform_info = genuine_intel && has_invariant_tsc;
2219 do_nhm_cstates = genuine_intel; /* all Intel w/ non-stop TSC have NHM counters */
2220 do_smi = do_nhm_cstates;
2221 do_snb_cstates = is_snb(family, model);
2222 do_c8_c9_c10 = has_c8_c9_c10(family, model);
2223 do_slm_cstates = is_slm(family, model);
2224 bclk = discover_bclk(family, model);
2226 do_nehalem_turbo_ratio_limit = has_nehalem_turbo_ratio_limit(family, model);
2227 do_ivt_turbo_ratio_limit = has_ivt_turbo_ratio_limit(family, model);
2228 rapl_probe(family, model);
2229 perf_limit_reasons_probe(family, model);
2237 errx(1, "%s: [-v][-R][-T][-p|-P|-S][-c MSR#][-C MSR#][-m MSR#][-M MSR#][-i interval_sec | command ...]\n",
2243 * in /dev/cpu/ return success for names that are numbers
2244 * ie. filter out ".", "..", "microcode".
2246 int dir_filter(const struct dirent *dirp)
2248 if (isdigit(dirp->d_name[0]))
2254 int open_dev_cpu_msr(int dummy1)
2259 void topology_probe()
2262 int max_core_id = 0;
2263 int max_package_id = 0;
2264 int max_siblings = 0;
2265 struct cpu_topology {
2267 int physical_package_id;
2270 /* Initialize num_cpus, max_cpu_num */
2272 topo.max_cpu_num = 0;
2273 for_all_proc_cpus(count_cpus);
2274 if (!summary_only && topo.num_cpus > 1)
2278 fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
2280 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
2282 err(1, "calloc cpus");
2285 * Allocate and initialize cpu_present_set
2287 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
2288 if (cpu_present_set == NULL)
2289 err(3, "CPU_ALLOC");
2290 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2291 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
2292 for_all_proc_cpus(mark_cpu_present);
2295 * Allocate and initialize cpu_affinity_set
2297 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
2298 if (cpu_affinity_set == NULL)
2299 err(3, "CPU_ALLOC");
2300 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2301 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
2306 * find max_core_id, max_package_id
2308 for (i = 0; i <= topo.max_cpu_num; ++i) {
2311 if (cpu_is_not_present(i)) {
2313 fprintf(stderr, "cpu%d NOT PRESENT\n", i);
2316 cpus[i].core_id = get_core_id(i);
2317 if (cpus[i].core_id > max_core_id)
2318 max_core_id = cpus[i].core_id;
2320 cpus[i].physical_package_id = get_physical_package_id(i);
2321 if (cpus[i].physical_package_id > max_package_id)
2322 max_package_id = cpus[i].physical_package_id;
2324 siblings = get_num_ht_siblings(i);
2325 if (siblings > max_siblings)
2326 max_siblings = siblings;
2328 fprintf(stderr, "cpu %d pkg %d core %d\n",
2329 i, cpus[i].physical_package_id, cpus[i].core_id);
2331 topo.num_cores_per_pkg = max_core_id + 1;
2333 fprintf(stderr, "max_core_id %d, sizing for %d cores per package\n",
2334 max_core_id, topo.num_cores_per_pkg);
2335 if (!summary_only && topo.num_cores_per_pkg > 1)
2338 topo.num_packages = max_package_id + 1;
2340 fprintf(stderr, "max_package_id %d, sizing for %d packages\n",
2341 max_package_id, topo.num_packages);
2342 if (!summary_only && topo.num_packages > 1)
2345 topo.num_threads_per_core = max_siblings;
2347 fprintf(stderr, "max_siblings %d\n", max_siblings);
2353 allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
2357 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
2358 topo.num_packages, sizeof(struct thread_data));
2362 for (i = 0; i < topo.num_threads_per_core *
2363 topo.num_cores_per_pkg * topo.num_packages; i++)
2364 (*t)[i].cpu_id = -1;
2366 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
2367 sizeof(struct core_data));
2371 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
2372 (*c)[i].core_id = -1;
2374 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
2378 for (i = 0; i < topo.num_packages; i++)
2379 (*p)[i].package_id = i;
2383 err(1, "calloc counters");
2388 * set cpu_id, core_num, pkg_num
2389 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
2391 * increment topo.num_cores when 1st core in pkg seen
2393 void init_counter(struct thread_data *thread_base, struct core_data *core_base,
2394 struct pkg_data *pkg_base, int thread_num, int core_num,
2395 int pkg_num, int cpu_id)
2397 struct thread_data *t;
2398 struct core_data *c;
2401 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
2402 c = GET_CORE(core_base, core_num, pkg_num);
2403 p = GET_PKG(pkg_base, pkg_num);
2406 if (thread_num == 0) {
2407 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
2408 if (cpu_is_first_core_in_package(cpu_id))
2409 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
2412 c->core_id = core_num;
2413 p->package_id = pkg_num;
2417 int initialize_counters(int cpu_id)
2419 int my_thread_id, my_core_id, my_package_id;
2421 my_package_id = get_physical_package_id(cpu_id);
2422 my_core_id = get_core_id(cpu_id);
2424 if (cpu_is_first_sibling_in_core(cpu_id)) {
2431 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2432 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2436 void allocate_output_buffer()
2438 output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
2439 outp = output_buffer;
2441 err(-1, "calloc output buffer");
2444 void setup_all_buffers(void)
2447 allocate_counters(&thread_even, &core_even, &package_even);
2448 allocate_counters(&thread_odd, &core_odd, &package_odd);
2449 allocate_output_buffer();
2450 for_all_proc_cpus(initialize_counters);
2453 void turbostat_init()
2456 check_permissions();
2459 setup_all_buffers();
2462 print_verbose_header();
2465 for_all_cpus(print_epb, ODD_COUNTERS);
2468 for_all_cpus(print_perf_limit, ODD_COUNTERS);
2471 for_all_cpus(print_rapl, ODD_COUNTERS);
2473 for_all_cpus(set_temperature_target, ODD_COUNTERS);
2476 for_all_cpus(print_thermal, ODD_COUNTERS);
2479 int fork_it(char **argv)
2484 status = for_all_cpus(get_counters, EVEN_COUNTERS);
2487 /* clear affinity side-effect of get_counters() */
2488 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
2489 gettimeofday(&tv_even, (struct timezone *)NULL);
2494 execvp(argv[0], argv);
2498 if (child_pid == -1)
2501 signal(SIGINT, SIG_IGN);
2502 signal(SIGQUIT, SIG_IGN);
2503 if (waitpid(child_pid, &status, 0) == -1)
2504 err(status, "waitpid");
2507 * n.b. fork_it() does not check for errors from for_all_cpus()
2508 * because re-starting is problematic when forking
2510 for_all_cpus(get_counters, ODD_COUNTERS);
2511 gettimeofday(&tv_odd, (struct timezone *)NULL);
2512 timersub(&tv_odd, &tv_even, &tv_delta);
2513 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
2514 compute_average(EVEN_COUNTERS);
2515 format_all_counters(EVEN_COUNTERS);
2518 fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
2523 int get_and_dump_counters(void)
2527 status = for_all_cpus(get_counters, ODD_COUNTERS);
2531 status = for_all_cpus(dump_counters, ODD_COUNTERS);
2540 void cmdline(int argc, char **argv)
2546 while ((opt = getopt(argc, argv, "+pPsSvi:c:C:m:M:RJT:")) != -1) {
2564 interval_sec = atoi(optarg);
2567 sscanf(optarg, "%x", &extra_delta_offset32);
2570 sscanf(optarg, "%x", &extra_delta_offset64);
2573 sscanf(optarg, "%x", &extra_msr_offset32);
2576 sscanf(optarg, "%x", &extra_msr_offset64);
2582 tcc_activation_temp_override = atoi(optarg);
2594 int main(int argc, char **argv)
2596 cmdline(argc, argv);
2599 fprintf(stderr, "turbostat v3.8 14-Aug 2014"
2600 " - Len Brown <lenb@kernel.org>\n");
2604 /* dump counters and exit */
2606 return get_and_dump_counters();
2609 * if any params left, it must be a command to fork
2612 return fork_it(argv + optind);