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 #include <linux/capability.h>
45 char *proc_stat = "/proc/stat";
46 unsigned int interval_sec = 5;
48 unsigned int rapl_joules;
49 unsigned int summary_only;
50 unsigned int dump_only;
53 unsigned int do_nhm_cstates;
54 unsigned int do_snb_cstates;
55 unsigned int do_knl_cstates;
60 unsigned int do_c8_c9_c10;
61 unsigned int do_skl_residency;
62 unsigned int do_slm_cstates;
63 unsigned int use_c1_residency_msr;
64 unsigned int has_aperf;
66 unsigned int units = 1000000; /* MHz etc */
67 unsigned int genuine_intel;
68 unsigned int has_invariant_tsc;
69 unsigned int do_nhm_platform_info;
70 unsigned int extra_msr_offset32;
71 unsigned int extra_msr_offset64;
72 unsigned int extra_delta_offset32;
73 unsigned int extra_delta_offset64;
76 unsigned int show_pkg;
77 unsigned int show_core;
78 unsigned int show_cpu;
79 unsigned int show_pkg_only;
80 unsigned int show_core_only;
81 char *output_buffer, *outp;
85 unsigned int tcc_activation_temp;
86 unsigned int tcc_activation_temp_override;
87 double rapl_power_units, rapl_time_units;
88 double rapl_dram_energy_units, rapl_energy_units;
89 double rapl_joule_counter_range;
90 unsigned int do_core_perf_limit_reasons;
91 unsigned int do_gfx_perf_limit_reasons;
92 unsigned int do_ring_perf_limit_reasons;
93 unsigned int crystal_hz;
94 unsigned long long tsc_hz;
97 #define RAPL_PKG (1 << 0)
98 /* 0x610 MSR_PKG_POWER_LIMIT */
99 /* 0x611 MSR_PKG_ENERGY_STATUS */
100 #define RAPL_PKG_PERF_STATUS (1 << 1)
101 /* 0x613 MSR_PKG_PERF_STATUS */
102 #define RAPL_PKG_POWER_INFO (1 << 2)
103 /* 0x614 MSR_PKG_POWER_INFO */
105 #define RAPL_DRAM (1 << 3)
106 /* 0x618 MSR_DRAM_POWER_LIMIT */
107 /* 0x619 MSR_DRAM_ENERGY_STATUS */
108 #define RAPL_DRAM_PERF_STATUS (1 << 4)
109 /* 0x61b MSR_DRAM_PERF_STATUS */
110 #define RAPL_DRAM_POWER_INFO (1 << 5)
111 /* 0x61c MSR_DRAM_POWER_INFO */
113 #define RAPL_CORES (1 << 6)
114 /* 0x638 MSR_PP0_POWER_LIMIT */
115 /* 0x639 MSR_PP0_ENERGY_STATUS */
116 #define RAPL_CORE_POLICY (1 << 7)
117 /* 0x63a MSR_PP0_POLICY */
119 #define RAPL_GFX (1 << 8)
120 /* 0x640 MSR_PP1_POWER_LIMIT */
121 /* 0x641 MSR_PP1_ENERGY_STATUS */
122 /* 0x642 MSR_PP1_POLICY */
123 #define TJMAX_DEFAULT 100
125 #define MAX(a, b) ((a) > (b) ? (a) : (b))
127 int aperf_mperf_unstable;
131 cpu_set_t *cpu_present_set, *cpu_affinity_set;
132 size_t cpu_present_setsize, cpu_affinity_setsize;
135 unsigned long long tsc;
136 unsigned long long aperf;
137 unsigned long long mperf;
138 unsigned long long c1;
139 unsigned long long extra_msr64;
140 unsigned long long extra_delta64;
141 unsigned long long extra_msr32;
142 unsigned long long extra_delta32;
143 unsigned int smi_count;
146 #define CPU_IS_FIRST_THREAD_IN_CORE 0x2
147 #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
148 } *thread_even, *thread_odd;
151 unsigned long long c3;
152 unsigned long long c6;
153 unsigned long long c7;
154 unsigned int core_temp_c;
155 unsigned int core_id;
156 } *core_even, *core_odd;
159 unsigned long long pc2;
160 unsigned long long pc3;
161 unsigned long long pc6;
162 unsigned long long pc7;
163 unsigned long long pc8;
164 unsigned long long pc9;
165 unsigned long long pc10;
166 unsigned long long pkg_wtd_core_c0;
167 unsigned long long pkg_any_core_c0;
168 unsigned long long pkg_any_gfxe_c0;
169 unsigned long long pkg_both_core_gfxe_c0;
170 unsigned int package_id;
171 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
172 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
173 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
174 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
175 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
176 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
177 unsigned int pkg_temp_c;
179 } *package_even, *package_odd;
181 #define ODD_COUNTERS thread_odd, core_odd, package_odd
182 #define EVEN_COUNTERS thread_even, core_even, package_even
184 #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
185 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
186 topo.num_threads_per_core + \
187 (core_no) * topo.num_threads_per_core + (thread_no))
188 #define GET_CORE(core_base, core_no, pkg_no) \
189 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
190 #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
192 struct system_summary {
193 struct thread_data threads;
194 struct core_data cores;
195 struct pkg_data packages;
204 int num_cores_per_pkg;
205 int num_threads_per_core;
208 struct timeval tv_even, tv_odd, tv_delta;
210 void setup_all_buffers(void);
212 int cpu_is_not_present(int cpu)
214 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
217 * run func(thread, core, package) in topology order
218 * skip non-present cpus
221 int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
222 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
224 int retval, pkg_no, core_no, thread_no;
226 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
227 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
228 for (thread_no = 0; thread_no <
229 topo.num_threads_per_core; ++thread_no) {
230 struct thread_data *t;
234 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
236 if (cpu_is_not_present(t->cpu_id))
239 c = GET_CORE(core_base, core_no, pkg_no);
240 p = GET_PKG(pkg_base, pkg_no);
242 retval = func(t, c, p);
251 int cpu_migrate(int cpu)
253 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
254 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
255 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
261 int get_msr(int cpu, off_t offset, unsigned long long *msr)
267 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
268 fd = open(pathname, O_RDONLY);
270 err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname);
272 retval = pread(fd, msr, sizeof *msr, offset);
275 if (retval != sizeof *msr)
276 err(-1, "%s offset 0x%llx read failed", pathname, (unsigned long long)offset);
282 * Example Format w/ field column widths:
284 * 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
285 * 123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678
288 void print_header(void)
291 outp += sprintf(outp, " Package");
293 outp += sprintf(outp, " Core");
295 outp += sprintf(outp, " CPU");
297 outp += sprintf(outp, " Avg_MHz");
299 outp += sprintf(outp, " %%Busy");
301 outp += sprintf(outp, " Bzy_MHz");
302 outp += sprintf(outp, " TSC_MHz");
304 if (extra_delta_offset32)
305 outp += sprintf(outp, " count 0x%03X", extra_delta_offset32);
306 if (extra_delta_offset64)
307 outp += sprintf(outp, " COUNT 0x%03X", extra_delta_offset64);
308 if (extra_msr_offset32)
309 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset32);
310 if (extra_msr_offset64)
311 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset64);
317 outp += sprintf(outp, " SMI");
320 outp += sprintf(outp, " CPU%%c1");
321 if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates)
322 outp += sprintf(outp, " CPU%%c3");
324 outp += sprintf(outp, " CPU%%c6");
326 outp += sprintf(outp, " CPU%%c7");
329 outp += sprintf(outp, " CoreTmp");
331 outp += sprintf(outp, " PkgTmp");
333 if (do_skl_residency) {
334 outp += sprintf(outp, " Totl%%C0");
335 outp += sprintf(outp, " Any%%C0");
336 outp += sprintf(outp, " GFX%%C0");
337 outp += sprintf(outp, " CPUGFX%%");
341 outp += sprintf(outp, " Pkg%%pc2");
343 outp += sprintf(outp, " Pkg%%pc3");
345 outp += sprintf(outp, " Pkg%%pc6");
347 outp += sprintf(outp, " Pkg%%pc7");
349 outp += sprintf(outp, " Pkg%%pc8");
350 outp += sprintf(outp, " Pkg%%pc9");
351 outp += sprintf(outp, " Pk%%pc10");
354 if (do_rapl && !rapl_joules) {
355 if (do_rapl & RAPL_PKG)
356 outp += sprintf(outp, " PkgWatt");
357 if (do_rapl & RAPL_CORES)
358 outp += sprintf(outp, " CorWatt");
359 if (do_rapl & RAPL_GFX)
360 outp += sprintf(outp, " GFXWatt");
361 if (do_rapl & RAPL_DRAM)
362 outp += sprintf(outp, " RAMWatt");
363 if (do_rapl & RAPL_PKG_PERF_STATUS)
364 outp += sprintf(outp, " PKG_%%");
365 if (do_rapl & RAPL_DRAM_PERF_STATUS)
366 outp += sprintf(outp, " RAM_%%");
367 } else if (do_rapl && rapl_joules) {
368 if (do_rapl & RAPL_PKG)
369 outp += sprintf(outp, " Pkg_J");
370 if (do_rapl & RAPL_CORES)
371 outp += sprintf(outp, " Cor_J");
372 if (do_rapl & RAPL_GFX)
373 outp += sprintf(outp, " GFX_J");
374 if (do_rapl & RAPL_DRAM)
375 outp += sprintf(outp, " RAM_J");
376 if (do_rapl & RAPL_PKG_PERF_STATUS)
377 outp += sprintf(outp, " PKG_%%");
378 if (do_rapl & RAPL_DRAM_PERF_STATUS)
379 outp += sprintf(outp, " RAM_%%");
380 outp += sprintf(outp, " time");
384 outp += sprintf(outp, "\n");
387 int dump_counters(struct thread_data *t, struct core_data *c,
390 outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
393 outp += sprintf(outp, "CPU: %d flags 0x%x\n",
394 t->cpu_id, t->flags);
395 outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
396 outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
397 outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
398 outp += sprintf(outp, "c1: %016llX\n", t->c1);
399 outp += sprintf(outp, "msr0x%x: %08llX\n",
400 extra_delta_offset32, t->extra_delta32);
401 outp += sprintf(outp, "msr0x%x: %016llX\n",
402 extra_delta_offset64, t->extra_delta64);
403 outp += sprintf(outp, "msr0x%x: %08llX\n",
404 extra_msr_offset32, t->extra_msr32);
405 outp += sprintf(outp, "msr0x%x: %016llX\n",
406 extra_msr_offset64, t->extra_msr64);
408 outp += sprintf(outp, "SMI: %08X\n", t->smi_count);
412 outp += sprintf(outp, "core: %d\n", c->core_id);
413 outp += sprintf(outp, "c3: %016llX\n", c->c3);
414 outp += sprintf(outp, "c6: %016llX\n", c->c6);
415 outp += sprintf(outp, "c7: %016llX\n", c->c7);
416 outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
420 outp += sprintf(outp, "package: %d\n", p->package_id);
422 outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
423 outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
424 outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
425 outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
427 outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
429 outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
431 outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
433 outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
434 outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
435 outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
436 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
437 outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
438 outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
439 outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
440 outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
441 outp += sprintf(outp, "Throttle PKG: %0X\n",
442 p->rapl_pkg_perf_status);
443 outp += sprintf(outp, "Throttle RAM: %0X\n",
444 p->rapl_dram_perf_status);
445 outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
448 outp += sprintf(outp, "\n");
454 * column formatting convention & formats
456 int format_counters(struct thread_data *t, struct core_data *c,
459 double interval_float;
462 /* if showing only 1st thread in core and this isn't one, bail out */
463 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
466 /* if showing only 1st thread in pkg and this isn't one, bail out */
467 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
470 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
472 /* topo columns, print blanks on 1st (average) line */
473 if (t == &average.threads) {
475 outp += sprintf(outp, " -");
477 outp += sprintf(outp, " -");
479 outp += sprintf(outp, " -");
483 outp += sprintf(outp, "%8d", p->package_id);
485 outp += sprintf(outp, " -");
489 outp += sprintf(outp, "%8d", c->core_id);
491 outp += sprintf(outp, " -");
494 outp += sprintf(outp, "%8d", t->cpu_id);
499 outp += sprintf(outp, "%8.0f",
500 1.0 / units * t->aperf / interval_float);
505 outp += sprintf(outp, "%8.2f", 100.0 * t->mperf/t->tsc);
507 outp += sprintf(outp, "********");
512 outp += sprintf(outp, "%8.0f",
513 1.0 * t->tsc / units * t->aperf / t->mperf / interval_float);
516 outp += sprintf(outp, "%8.0f", 1.0 * t->tsc/units/interval_float);
519 if (extra_delta_offset32)
520 outp += sprintf(outp, " %11llu", t->extra_delta32);
523 if (extra_delta_offset64)
524 outp += sprintf(outp, " %11llu", t->extra_delta64);
526 if (extra_msr_offset32)
527 outp += sprintf(outp, " 0x%08llx", t->extra_msr32);
530 if (extra_msr_offset64)
531 outp += sprintf(outp, " 0x%016llx", t->extra_msr64);
538 outp += sprintf(outp, "%8d", t->smi_count);
540 if (do_nhm_cstates) {
542 outp += sprintf(outp, "%8.2f", 100.0 * t->c1/t->tsc);
544 outp += sprintf(outp, "********");
547 /* print per-core data only for 1st thread in core */
548 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
551 if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates)
552 outp += sprintf(outp, "%8.2f", 100.0 * c->c3/t->tsc);
554 outp += sprintf(outp, "%8.2f", 100.0 * c->c6/t->tsc);
556 outp += sprintf(outp, "%8.2f", 100.0 * c->c7/t->tsc);
559 outp += sprintf(outp, "%8d", c->core_temp_c);
561 /* print per-package data only for 1st core in package */
562 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
567 outp += sprintf(outp, "%8d", p->pkg_temp_c);
569 /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
570 if (do_skl_residency) {
571 outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_wtd_core_c0/t->tsc);
572 outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_any_core_c0/t->tsc);
573 outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_any_gfxe_c0/t->tsc);
574 outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_both_core_gfxe_c0/t->tsc);
578 outp += sprintf(outp, "%8.2f", 100.0 * p->pc2/t->tsc);
580 outp += sprintf(outp, "%8.2f", 100.0 * p->pc3/t->tsc);
582 outp += sprintf(outp, "%8.2f", 100.0 * p->pc6/t->tsc);
584 outp += sprintf(outp, "%8.2f", 100.0 * p->pc7/t->tsc);
586 outp += sprintf(outp, "%8.2f", 100.0 * p->pc8/t->tsc);
587 outp += sprintf(outp, "%8.2f", 100.0 * p->pc9/t->tsc);
588 outp += sprintf(outp, "%8.2f", 100.0 * p->pc10/t->tsc);
592 * If measurement interval exceeds minimum RAPL Joule Counter range,
593 * indicate that results are suspect by printing "**" in fraction place.
595 if (interval_float < rapl_joule_counter_range)
600 if (do_rapl && !rapl_joules) {
601 if (do_rapl & RAPL_PKG)
602 outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units / interval_float);
603 if (do_rapl & RAPL_CORES)
604 outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units / interval_float);
605 if (do_rapl & RAPL_GFX)
606 outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units / interval_float);
607 if (do_rapl & RAPL_DRAM)
608 outp += sprintf(outp, fmt8, p->energy_dram * rapl_dram_energy_units / interval_float);
609 if (do_rapl & RAPL_PKG_PERF_STATUS)
610 outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
611 if (do_rapl & RAPL_DRAM_PERF_STATUS)
612 outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
613 } else if (do_rapl && rapl_joules) {
614 if (do_rapl & RAPL_PKG)
615 outp += sprintf(outp, fmt8,
616 p->energy_pkg * rapl_energy_units);
617 if (do_rapl & RAPL_CORES)
618 outp += sprintf(outp, fmt8,
619 p->energy_cores * rapl_energy_units);
620 if (do_rapl & RAPL_GFX)
621 outp += sprintf(outp, fmt8,
622 p->energy_gfx * rapl_energy_units);
623 if (do_rapl & RAPL_DRAM)
624 outp += sprintf(outp, fmt8,
625 p->energy_dram * rapl_dram_energy_units);
626 if (do_rapl & RAPL_PKG_PERF_STATUS)
627 outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
628 if (do_rapl & RAPL_DRAM_PERF_STATUS)
629 outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
631 outp += sprintf(outp, fmt8, interval_float);
634 outp += sprintf(outp, "\n");
641 fputs(output_buffer, stdout);
643 outp = output_buffer;
647 fputs(output_buffer, stderr);
648 outp = output_buffer;
650 void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
654 if (!printed || !summary_only)
657 if (topo.num_cpus > 1)
658 format_counters(&average.threads, &average.cores,
666 for_all_cpus(format_counters, t, c, p);
669 #define DELTA_WRAP32(new, old) \
673 old = 0x100000000 + new - old; \
677 delta_package(struct pkg_data *new, struct pkg_data *old)
680 if (do_skl_residency) {
681 old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
682 old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
683 old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
684 old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0;
686 old->pc2 = new->pc2 - old->pc2;
688 old->pc3 = new->pc3 - old->pc3;
690 old->pc6 = new->pc6 - old->pc6;
692 old->pc7 = new->pc7 - old->pc7;
693 old->pc8 = new->pc8 - old->pc8;
694 old->pc9 = new->pc9 - old->pc9;
695 old->pc10 = new->pc10 - old->pc10;
696 old->pkg_temp_c = new->pkg_temp_c;
698 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
699 DELTA_WRAP32(new->energy_cores, old->energy_cores);
700 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
701 DELTA_WRAP32(new->energy_dram, old->energy_dram);
702 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
703 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
707 delta_core(struct core_data *new, struct core_data *old)
709 old->c3 = new->c3 - old->c3;
710 old->c6 = new->c6 - old->c6;
711 old->c7 = new->c7 - old->c7;
712 old->core_temp_c = new->core_temp_c;
719 delta_thread(struct thread_data *new, struct thread_data *old,
720 struct core_data *core_delta)
722 old->tsc = new->tsc - old->tsc;
724 /* check for TSC < 1 Mcycles over interval */
725 if (old->tsc < (1000 * 1000))
726 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
727 "You can disable all c-states by booting with \"idle=poll\"\n"
728 "or just the deep ones with \"processor.max_cstate=1\"");
730 old->c1 = new->c1 - old->c1;
733 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
734 old->aperf = new->aperf - old->aperf;
735 old->mperf = new->mperf - old->mperf;
738 if (!aperf_mperf_unstable) {
739 fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname);
740 fprintf(stderr, "* Frequency results do not cover entire interval *\n");
741 fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n");
743 aperf_mperf_unstable = 1;
746 * mperf delta is likely a huge "positive" number
747 * can not use it for calculating c0 time
755 if (use_c1_residency_msr) {
757 * Some models have a dedicated C1 residency MSR,
758 * which should be more accurate than the derivation below.
762 * As counter collection is not atomic,
763 * it is possible for mperf's non-halted cycles + idle states
764 * to exceed TSC's all cycles: show c1 = 0% in that case.
766 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
769 /* normal case, derive c1 */
770 old->c1 = old->tsc - old->mperf - core_delta->c3
771 - core_delta->c6 - core_delta->c7;
775 if (old->mperf == 0) {
776 if (debug > 1) fprintf(stderr, "cpu%d MPERF 0!\n", old->cpu_id);
777 old->mperf = 1; /* divide by 0 protection */
780 old->extra_delta32 = new->extra_delta32 - old->extra_delta32;
781 old->extra_delta32 &= 0xFFFFFFFF;
783 old->extra_delta64 = new->extra_delta64 - old->extra_delta64;
786 * Extra MSR is just a snapshot, simply copy latest w/o subtracting
788 old->extra_msr32 = new->extra_msr32;
789 old->extra_msr64 = new->extra_msr64;
792 old->smi_count = new->smi_count - old->smi_count;
795 int delta_cpu(struct thread_data *t, struct core_data *c,
796 struct pkg_data *p, struct thread_data *t2,
797 struct core_data *c2, struct pkg_data *p2)
799 /* calculate core delta only for 1st thread in core */
800 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
803 /* always calculate thread delta */
804 delta_thread(t, t2, c2); /* c2 is core delta */
806 /* calculate package delta only for 1st core in package */
807 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
808 delta_package(p, p2);
813 void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
821 t->extra_delta32 = 0;
822 t->extra_delta64 = 0;
824 /* tells format_counters to dump all fields from this set */
825 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
832 p->pkg_wtd_core_c0 = 0;
833 p->pkg_any_core_c0 = 0;
834 p->pkg_any_gfxe_c0 = 0;
835 p->pkg_both_core_gfxe_c0 = 0;
852 p->rapl_pkg_perf_status = 0;
853 p->rapl_dram_perf_status = 0;
856 int sum_counters(struct thread_data *t, struct core_data *c,
859 average.threads.tsc += t->tsc;
860 average.threads.aperf += t->aperf;
861 average.threads.mperf += t->mperf;
862 average.threads.c1 += t->c1;
864 average.threads.extra_delta32 += t->extra_delta32;
865 average.threads.extra_delta64 += t->extra_delta64;
867 /* sum per-core values only for 1st thread in core */
868 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
871 average.cores.c3 += c->c3;
872 average.cores.c6 += c->c6;
873 average.cores.c7 += c->c7;
875 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
877 /* sum per-pkg values only for 1st core in pkg */
878 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
881 if (do_skl_residency) {
882 average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
883 average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
884 average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
885 average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
888 average.packages.pc2 += p->pc2;
890 average.packages.pc3 += p->pc3;
892 average.packages.pc6 += p->pc6;
894 average.packages.pc7 += p->pc7;
895 average.packages.pc8 += p->pc8;
896 average.packages.pc9 += p->pc9;
897 average.packages.pc10 += p->pc10;
899 average.packages.energy_pkg += p->energy_pkg;
900 average.packages.energy_dram += p->energy_dram;
901 average.packages.energy_cores += p->energy_cores;
902 average.packages.energy_gfx += p->energy_gfx;
904 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
906 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
907 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
911 * sum the counters for all cpus in the system
912 * compute the weighted average
914 void compute_average(struct thread_data *t, struct core_data *c,
917 clear_counters(&average.threads, &average.cores, &average.packages);
919 for_all_cpus(sum_counters, t, c, p);
921 average.threads.tsc /= topo.num_cpus;
922 average.threads.aperf /= topo.num_cpus;
923 average.threads.mperf /= topo.num_cpus;
924 average.threads.c1 /= topo.num_cpus;
926 average.threads.extra_delta32 /= topo.num_cpus;
927 average.threads.extra_delta32 &= 0xFFFFFFFF;
929 average.threads.extra_delta64 /= topo.num_cpus;
931 average.cores.c3 /= topo.num_cores;
932 average.cores.c6 /= topo.num_cores;
933 average.cores.c7 /= topo.num_cores;
935 if (do_skl_residency) {
936 average.packages.pkg_wtd_core_c0 /= topo.num_packages;
937 average.packages.pkg_any_core_c0 /= topo.num_packages;
938 average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
939 average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
942 average.packages.pc2 /= topo.num_packages;
944 average.packages.pc3 /= topo.num_packages;
946 average.packages.pc6 /= topo.num_packages;
948 average.packages.pc7 /= topo.num_packages;
950 average.packages.pc8 /= topo.num_packages;
951 average.packages.pc9 /= topo.num_packages;
952 average.packages.pc10 /= topo.num_packages;
955 static unsigned long long rdtsc(void)
957 unsigned int low, high;
959 asm volatile("rdtsc" : "=a" (low), "=d" (high));
961 return low | ((unsigned long long)high) << 32;
968 * acquire and record local counters for that cpu
970 int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
973 unsigned long long msr;
975 if (cpu_migrate(cpu)) {
976 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
980 t->tsc = rdtsc(); /* we are running on local CPU of interest */
983 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
985 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
990 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
992 t->smi_count = msr & 0xFFFFFFFF;
994 if (extra_delta_offset32) {
995 if (get_msr(cpu, extra_delta_offset32, &msr))
997 t->extra_delta32 = msr & 0xFFFFFFFF;
1000 if (extra_delta_offset64)
1001 if (get_msr(cpu, extra_delta_offset64, &t->extra_delta64))
1004 if (extra_msr_offset32) {
1005 if (get_msr(cpu, extra_msr_offset32, &msr))
1007 t->extra_msr32 = msr & 0xFFFFFFFF;
1010 if (extra_msr_offset64)
1011 if (get_msr(cpu, extra_msr_offset64, &t->extra_msr64))
1014 if (use_c1_residency_msr) {
1015 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1019 /* collect core counters only for 1st thread in core */
1020 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1023 if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates) {
1024 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1028 if (do_nhm_cstates && !do_knl_cstates) {
1029 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1031 } else if (do_knl_cstates) {
1032 if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1037 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1041 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1043 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1047 /* collect package counters only for 1st core in package */
1048 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1051 if (do_skl_residency) {
1052 if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
1054 if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
1056 if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
1058 if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
1062 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1065 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1068 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1071 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1074 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1076 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1078 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1081 if (do_rapl & RAPL_PKG) {
1082 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1084 p->energy_pkg = msr & 0xFFFFFFFF;
1086 if (do_rapl & RAPL_CORES) {
1087 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1089 p->energy_cores = msr & 0xFFFFFFFF;
1091 if (do_rapl & RAPL_DRAM) {
1092 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1094 p->energy_dram = msr & 0xFFFFFFFF;
1096 if (do_rapl & RAPL_GFX) {
1097 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1099 p->energy_gfx = msr & 0xFFFFFFFF;
1101 if (do_rapl & RAPL_PKG_PERF_STATUS) {
1102 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1104 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1106 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1107 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1109 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1112 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1114 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1120 * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1121 * If you change the values, note they are used both in comparisons
1122 * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1125 #define PCLUKN 0 /* Unknown */
1126 #define PCLRSV 1 /* Reserved */
1127 #define PCL__0 2 /* PC0 */
1128 #define PCL__1 3 /* PC1 */
1129 #define PCL__2 4 /* PC2 */
1130 #define PCL__3 5 /* PC3 */
1131 #define PCL__4 6 /* PC4 */
1132 #define PCL__6 7 /* PC6 */
1133 #define PCL_6N 8 /* PC6 No Retention */
1134 #define PCL_6R 9 /* PC6 Retention */
1135 #define PCL__7 10 /* PC7 */
1136 #define PCL_7S 11 /* PC7 Shrink */
1137 #define PCL__8 12 /* PC8 */
1138 #define PCL__9 13 /* PC9 */
1139 #define PCLUNL 14 /* Unlimited */
1141 int pkg_cstate_limit = PCLUKN;
1142 char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
1143 "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"};
1145 int nhm_pkg_cstate_limits[16] = {PCL__0, PCL__1, PCL__3, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1146 int snb_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL_6N, PCL_6R, PCL__7, PCL_7S, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1147 int hsw_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1148 int slv_pkg_cstate_limits[16] = {PCL__0, PCL__1, PCLRSV, PCLRSV, PCL__4, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1149 int amt_pkg_cstate_limits[16] = {PCL__0, PCL__1, PCL__2, PCLRSV, PCLRSV, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1150 int phi_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1153 dump_nhm_platform_info(void)
1155 unsigned long long msr;
1158 get_msr(base_cpu, MSR_NHM_PLATFORM_INFO, &msr);
1160 fprintf(stderr, "cpu%d: MSR_NHM_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
1162 ratio = (msr >> 40) & 0xFF;
1163 fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency frequency\n",
1164 ratio, bclk, ratio * bclk);
1166 ratio = (msr >> 8) & 0xFF;
1167 fprintf(stderr, "%d * %.0f = %.0f MHz base frequency\n",
1168 ratio, bclk, ratio * bclk);
1170 get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
1171 fprintf(stderr, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
1172 base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
1178 dump_hsw_turbo_ratio_limits(void)
1180 unsigned long long msr;
1183 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
1185 fprintf(stderr, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
1187 ratio = (msr >> 8) & 0xFF;
1189 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 18 active cores\n",
1190 ratio, bclk, ratio * bclk);
1192 ratio = (msr >> 0) & 0xFF;
1194 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 17 active cores\n",
1195 ratio, bclk, ratio * bclk);
1200 dump_ivt_turbo_ratio_limits(void)
1202 unsigned long long msr;
1205 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
1207 fprintf(stderr, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
1209 ratio = (msr >> 56) & 0xFF;
1211 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 16 active cores\n",
1212 ratio, bclk, ratio * bclk);
1214 ratio = (msr >> 48) & 0xFF;
1216 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 15 active cores\n",
1217 ratio, bclk, ratio * bclk);
1219 ratio = (msr >> 40) & 0xFF;
1221 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 14 active cores\n",
1222 ratio, bclk, ratio * bclk);
1224 ratio = (msr >> 32) & 0xFF;
1226 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 13 active cores\n",
1227 ratio, bclk, ratio * bclk);
1229 ratio = (msr >> 24) & 0xFF;
1231 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 12 active cores\n",
1232 ratio, bclk, ratio * bclk);
1234 ratio = (msr >> 16) & 0xFF;
1236 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 11 active cores\n",
1237 ratio, bclk, ratio * bclk);
1239 ratio = (msr >> 8) & 0xFF;
1241 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 10 active cores\n",
1242 ratio, bclk, ratio * bclk);
1244 ratio = (msr >> 0) & 0xFF;
1246 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 9 active cores\n",
1247 ratio, bclk, ratio * bclk);
1252 dump_nhm_turbo_ratio_limits(void)
1254 unsigned long long msr;
1257 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
1259 fprintf(stderr, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
1261 ratio = (msr >> 56) & 0xFF;
1263 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 8 active cores\n",
1264 ratio, bclk, ratio * bclk);
1266 ratio = (msr >> 48) & 0xFF;
1268 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 7 active cores\n",
1269 ratio, bclk, ratio * bclk);
1271 ratio = (msr >> 40) & 0xFF;
1273 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 6 active cores\n",
1274 ratio, bclk, ratio * bclk);
1276 ratio = (msr >> 32) & 0xFF;
1278 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 5 active cores\n",
1279 ratio, bclk, ratio * bclk);
1281 ratio = (msr >> 24) & 0xFF;
1283 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
1284 ratio, bclk, ratio * bclk);
1286 ratio = (msr >> 16) & 0xFF;
1288 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
1289 ratio, bclk, ratio * bclk);
1291 ratio = (msr >> 8) & 0xFF;
1293 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
1294 ratio, bclk, ratio * bclk);
1296 ratio = (msr >> 0) & 0xFF;
1298 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
1299 ratio, bclk, ratio * bclk);
1304 dump_knl_turbo_ratio_limits(void)
1308 unsigned long long msr;
1313 get_msr(base_cpu, MSR_NHM_TURBO_RATIO_LIMIT, &msr);
1315 fprintf(stderr, "cpu%d: MSR_NHM_TURBO_RATIO_LIMIT: 0x%08llx\n",
1319 * Turbo encoding in KNL is as follows:
1320 * [7:0] -- Base value of number of active cores of bucket 1.
1321 * [15:8] -- Base value of freq ratio of bucket 1.
1322 * [20:16] -- +ve delta of number of active cores of bucket 2.
1323 * i.e. active cores of bucket 2 =
1324 * active cores of bucket 1 + delta
1325 * [23:21] -- Negative delta of freq ratio of bucket 2.
1326 * i.e. freq ratio of bucket 2 =
1327 * freq ratio of bucket 1 - delta
1328 * [28:24]-- +ve delta of number of active cores of bucket 3.
1329 * [31:29]-- -ve delta of freq ratio of bucket 3.
1330 * [36:32]-- +ve delta of number of active cores of bucket 4.
1331 * [39:37]-- -ve delta of freq ratio of bucket 4.
1332 * [44:40]-- +ve delta of number of active cores of bucket 5.
1333 * [47:45]-- -ve delta of freq ratio of bucket 5.
1334 * [52:48]-- +ve delta of number of active cores of bucket 6.
1335 * [55:53]-- -ve delta of freq ratio of bucket 6.
1336 * [60:56]-- +ve delta of number of active cores of bucket 7.
1337 * [63:61]-- -ve delta of freq ratio of bucket 7.
1340 ratio = (msr >> 8) && 0xFF;
1343 "%d * %.0f = %.0f MHz max turbo %d active cores\n",
1344 ratio, bclk, ratio * bclk, cores);
1346 for (i = 16; i < 64; i = i + 8) {
1347 delta_cores = (msr >> i) & 0x1F;
1348 delta_ratio = (msr >> (i + 5)) && 0x7;
1349 if (!delta_cores || !delta_ratio)
1351 cores = cores + delta_cores;
1352 ratio = ratio - delta_ratio;
1354 /** -ve ratios will make successive ratio calculations
1355 * negative. Hence return instead of carrying on.
1359 "%d * %.0f = %.0f MHz max turbo %d active cores\n",
1360 ratio, bclk, ratio * bclk, cores);
1365 dump_nhm_cst_cfg(void)
1367 unsigned long long msr;
1369 get_msr(base_cpu, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
1371 #define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
1372 #define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
1374 fprintf(stderr, "cpu%d: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", base_cpu, msr);
1376 fprintf(stderr, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: %s)\n",
1377 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
1378 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
1379 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
1380 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
1381 (msr & (1 << 15)) ? "" : "UN",
1382 (unsigned int)msr & 7,
1383 pkg_cstate_limit_strings[pkg_cstate_limit]);
1388 dump_config_tdp(void)
1390 unsigned long long msr;
1392 get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
1393 fprintf(stderr, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
1394 fprintf(stderr, " (base_ratio=%d)\n", (unsigned int)msr & 0xEF);
1396 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
1397 fprintf(stderr, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
1399 fprintf(stderr, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0xEFFF);
1400 fprintf(stderr, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0xEFFF);
1401 fprintf(stderr, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xEF);
1402 fprintf(stderr, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0xEFFF);
1404 fprintf(stderr, ")\n");
1406 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
1407 fprintf(stderr, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
1409 fprintf(stderr, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0xEFFF);
1410 fprintf(stderr, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0xEFFF);
1411 fprintf(stderr, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xEF);
1412 fprintf(stderr, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0xEFFF);
1414 fprintf(stderr, ")\n");
1416 get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
1417 fprintf(stderr, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
1419 fprintf(stderr, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
1420 fprintf(stderr, " lock=%d", (unsigned int)(msr >> 31) & 1);
1421 fprintf(stderr, ")\n");
1423 get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
1424 fprintf(stderr, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
1425 fprintf(stderr, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xEF);
1426 fprintf(stderr, " lock=%d", (unsigned int)(msr >> 31) & 1);
1427 fprintf(stderr, ")\n");
1430 void free_all_buffers(void)
1432 CPU_FREE(cpu_present_set);
1433 cpu_present_set = NULL;
1434 cpu_present_set = 0;
1436 CPU_FREE(cpu_affinity_set);
1437 cpu_affinity_set = NULL;
1438 cpu_affinity_setsize = 0;
1446 package_even = NULL;
1456 free(output_buffer);
1457 output_buffer = NULL;
1462 * Open a file, and exit on failure
1464 FILE *fopen_or_die(const char *path, const char *mode)
1466 FILE *filep = fopen(path, "r");
1468 err(1, "%s: open failed", path);
1473 * Parse a file containing a single int.
1475 int parse_int_file(const char *fmt, ...)
1478 char path[PATH_MAX];
1482 va_start(args, fmt);
1483 vsnprintf(path, sizeof(path), fmt, args);
1485 filep = fopen_or_die(path, "r");
1486 if (fscanf(filep, "%d", &value) != 1)
1487 err(1, "%s: failed to parse number from file", path);
1493 * get_cpu_position_in_core(cpu)
1494 * return the position of the CPU among its HT siblings in the core
1495 * return -1 if the sibling is not in list
1497 int get_cpu_position_in_core(int cpu)
1506 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list",
1508 filep = fopen(path, "r");
1509 if (filep == NULL) {
1514 for (i = 0; i < topo.num_threads_per_core; i++) {
1515 fscanf(filep, "%d", &this_cpu);
1516 if (this_cpu == cpu) {
1521 /* Account for no separator after last thread*/
1522 if (i != (topo.num_threads_per_core - 1))
1523 fscanf(filep, "%c", &character);
1531 * cpu_is_first_core_in_package(cpu)
1532 * return 1 if given CPU is 1st core in package
1534 int cpu_is_first_core_in_package(int cpu)
1536 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
1539 int get_physical_package_id(int cpu)
1541 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
1544 int get_core_id(int cpu)
1546 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
1549 int get_num_ht_siblings(int cpu)
1559 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
1560 filep = fopen_or_die(path, "r");
1564 * A ',' separated or '-' separated set of numbers
1565 * (eg 1-2 or 1,3,4,5)
1567 fscanf(filep, "%d%c\n", &sib1, &character);
1568 fseek(filep, 0, SEEK_SET);
1569 fgets(str, 100, filep);
1570 ch = strchr(str, character);
1571 while (ch != NULL) {
1573 ch = strchr(ch+1, character);
1581 * run func(thread, core, package) in topology order
1582 * skip non-present cpus
1585 int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
1586 struct pkg_data *, struct thread_data *, struct core_data *,
1587 struct pkg_data *), struct thread_data *thread_base,
1588 struct core_data *core_base, struct pkg_data *pkg_base,
1589 struct thread_data *thread_base2, struct core_data *core_base2,
1590 struct pkg_data *pkg_base2)
1592 int retval, pkg_no, core_no, thread_no;
1594 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
1595 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
1596 for (thread_no = 0; thread_no <
1597 topo.num_threads_per_core; ++thread_no) {
1598 struct thread_data *t, *t2;
1599 struct core_data *c, *c2;
1600 struct pkg_data *p, *p2;
1602 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
1604 if (cpu_is_not_present(t->cpu_id))
1607 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
1609 c = GET_CORE(core_base, core_no, pkg_no);
1610 c2 = GET_CORE(core_base2, core_no, pkg_no);
1612 p = GET_PKG(pkg_base, pkg_no);
1613 p2 = GET_PKG(pkg_base2, pkg_no);
1615 retval = func(t, c, p, t2, c2, p2);
1625 * run func(cpu) on every cpu in /proc/stat
1626 * return max_cpu number
1628 int for_all_proc_cpus(int (func)(int))
1634 fp = fopen_or_die(proc_stat, "r");
1636 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
1638 err(1, "%s: failed to parse format", proc_stat);
1641 retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
1645 retval = func(cpu_num);
1655 void re_initialize(void)
1658 setup_all_buffers();
1659 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
1665 * remember the last one seen, it will be the max
1667 int count_cpus(int cpu)
1669 if (topo.max_cpu_num < cpu)
1670 topo.max_cpu_num = cpu;
1675 int mark_cpu_present(int cpu)
1677 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
1681 void turbostat_loop()
1689 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
1692 } else if (retval == -1) {
1693 if (restarted > 1) {
1700 gettimeofday(&tv_even, (struct timezone *)NULL);
1703 if (for_all_proc_cpus(cpu_is_not_present)) {
1707 sleep(interval_sec);
1708 retval = for_all_cpus(get_counters, ODD_COUNTERS);
1711 } else if (retval == -1) {
1715 gettimeofday(&tv_odd, (struct timezone *)NULL);
1716 timersub(&tv_odd, &tv_even, &tv_delta);
1717 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
1718 compute_average(EVEN_COUNTERS);
1719 format_all_counters(EVEN_COUNTERS);
1721 sleep(interval_sec);
1722 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
1725 } else if (retval == -1) {
1729 gettimeofday(&tv_even, (struct timezone *)NULL);
1730 timersub(&tv_even, &tv_odd, &tv_delta);
1731 for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS);
1732 compute_average(ODD_COUNTERS);
1733 format_all_counters(ODD_COUNTERS);
1738 void check_dev_msr()
1743 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
1744 if (stat(pathname, &sb))
1745 if (system("/sbin/modprobe msr > /dev/null 2>&1"))
1746 err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
1749 void check_permissions()
1751 struct __user_cap_header_struct cap_header_data;
1752 cap_user_header_t cap_header = &cap_header_data;
1753 struct __user_cap_data_struct cap_data_data;
1754 cap_user_data_t cap_data = &cap_data_data;
1755 extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
1759 /* check for CAP_SYS_RAWIO */
1760 cap_header->pid = getpid();
1761 cap_header->version = _LINUX_CAPABILITY_VERSION;
1762 if (capget(cap_header, cap_data) < 0)
1763 err(-6, "capget(2) failed");
1765 if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
1767 warnx("capget(CAP_SYS_RAWIO) failed,"
1768 " try \"# setcap cap_sys_rawio=ep %s\"", progname);
1771 /* test file permissions */
1772 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
1773 if (euidaccess(pathname, R_OK)) {
1775 warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
1778 /* if all else fails, thell them to be root */
1781 warnx("... or simply run as root");
1788 * NHM adds support for additional MSRs:
1790 * MSR_SMI_COUNT 0x00000034
1792 * MSR_NHM_PLATFORM_INFO 0x000000ce
1793 * MSR_NHM_SNB_PKG_CST_CFG_CTL 0x000000e2
1795 * MSR_PKG_C3_RESIDENCY 0x000003f8
1796 * MSR_PKG_C6_RESIDENCY 0x000003f9
1797 * MSR_CORE_C3_RESIDENCY 0x000003fc
1798 * MSR_CORE_C6_RESIDENCY 0x000003fd
1801 * sets global pkg_cstate_limit to decode MSR_NHM_SNB_PKG_CST_CFG_CTL
1803 int probe_nhm_msrs(unsigned int family, unsigned int model)
1805 unsigned long long msr;
1806 int *pkg_cstate_limits;
1815 case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
1816 case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
1817 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
1818 case 0x25: /* Westmere Client - Clarkdale, Arrandale */
1819 case 0x2C: /* Westmere EP - Gulftown */
1820 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1821 case 0x2F: /* Westmere-EX Xeon - Eagleton */
1822 pkg_cstate_limits = nhm_pkg_cstate_limits;
1824 case 0x2A: /* SNB */
1825 case 0x2D: /* SNB Xeon */
1826 case 0x3A: /* IVB */
1827 case 0x3E: /* IVB Xeon */
1828 pkg_cstate_limits = snb_pkg_cstate_limits;
1830 case 0x3C: /* HSW */
1831 case 0x3F: /* HSX */
1832 case 0x45: /* HSW */
1833 case 0x46: /* HSW */
1834 case 0x3D: /* BDW */
1835 case 0x47: /* BDW */
1836 case 0x4F: /* BDX */
1837 case 0x56: /* BDX-DE */
1838 case 0x4E: /* SKL */
1839 case 0x5E: /* SKL */
1840 pkg_cstate_limits = hsw_pkg_cstate_limits;
1842 case 0x37: /* BYT */
1843 case 0x4D: /* AVN */
1844 pkg_cstate_limits = slv_pkg_cstate_limits;
1846 case 0x4C: /* AMT */
1847 pkg_cstate_limits = amt_pkg_cstate_limits;
1849 case 0x57: /* PHI */
1850 pkg_cstate_limits = phi_pkg_cstate_limits;
1855 get_msr(base_cpu, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
1857 pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
1861 int has_nhm_turbo_ratio_limit(unsigned int family, unsigned int model)
1864 /* Nehalem compatible, but do not include turbo-ratio limit support */
1865 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1866 case 0x2F: /* Westmere-EX Xeon - Eagleton */
1872 int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
1881 case 0x3E: /* IVB Xeon */
1882 case 0x3F: /* HSW Xeon */
1888 int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
1897 case 0x3F: /* HSW Xeon */
1904 int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
1913 case 0x57: /* Knights Landing */
1919 int has_config_tdp(unsigned int family, unsigned int model)
1928 case 0x3A: /* IVB */
1929 case 0x3C: /* HSW */
1930 case 0x3F: /* HSX */
1931 case 0x45: /* HSW */
1932 case 0x46: /* HSW */
1933 case 0x3D: /* BDW */
1934 case 0x47: /* BDW */
1935 case 0x4F: /* BDX */
1936 case 0x56: /* BDX-DE */
1937 case 0x4E: /* SKL */
1938 case 0x5E: /* SKL */
1940 case 0x57: /* Knights Landing */
1948 dump_cstate_pstate_config_info(family, model)
1950 if (!do_nhm_platform_info)
1953 dump_nhm_platform_info();
1955 if (has_hsw_turbo_ratio_limit(family, model))
1956 dump_hsw_turbo_ratio_limits();
1958 if (has_ivt_turbo_ratio_limit(family, model))
1959 dump_ivt_turbo_ratio_limits();
1961 if (has_nhm_turbo_ratio_limit(family, model))
1962 dump_nhm_turbo_ratio_limits();
1964 if (has_knl_turbo_ratio_limit(family, model))
1965 dump_knl_turbo_ratio_limits();
1967 if (has_config_tdp(family, model))
1976 * Decode the ENERGY_PERF_BIAS MSR
1978 int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1980 unsigned long long msr;
1989 /* EPB is per-package */
1990 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1993 if (cpu_migrate(cpu)) {
1994 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1998 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
2001 switch (msr & 0xF) {
2002 case ENERGY_PERF_BIAS_PERFORMANCE:
2003 epb_string = "performance";
2005 case ENERGY_PERF_BIAS_NORMAL:
2006 epb_string = "balanced";
2008 case ENERGY_PERF_BIAS_POWERSAVE:
2009 epb_string = "powersave";
2012 epb_string = "custom";
2015 fprintf(stderr, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
2021 * print_perf_limit()
2023 int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2025 unsigned long long msr;
2031 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2034 if (cpu_migrate(cpu)) {
2035 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
2039 if (do_core_perf_limit_reasons) {
2040 get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
2041 fprintf(stderr, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2042 fprintf(stderr, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
2043 (msr & 1 << 15) ? "bit15, " : "",
2044 (msr & 1 << 14) ? "bit14, " : "",
2045 (msr & 1 << 13) ? "Transitions, " : "",
2046 (msr & 1 << 12) ? "MultiCoreTurbo, " : "",
2047 (msr & 1 << 11) ? "PkgPwrL2, " : "",
2048 (msr & 1 << 10) ? "PkgPwrL1, " : "",
2049 (msr & 1 << 9) ? "CorePwr, " : "",
2050 (msr & 1 << 8) ? "Amps, " : "",
2051 (msr & 1 << 6) ? "VR-Therm, " : "",
2052 (msr & 1 << 5) ? "Auto-HWP, " : "",
2053 (msr & 1 << 4) ? "Graphics, " : "",
2054 (msr & 1 << 2) ? "bit2, " : "",
2055 (msr & 1 << 1) ? "ThermStatus, " : "",
2056 (msr & 1 << 0) ? "PROCHOT, " : "");
2057 fprintf(stderr, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n",
2058 (msr & 1 << 31) ? "bit31, " : "",
2059 (msr & 1 << 30) ? "bit30, " : "",
2060 (msr & 1 << 29) ? "Transitions, " : "",
2061 (msr & 1 << 28) ? "MultiCoreTurbo, " : "",
2062 (msr & 1 << 27) ? "PkgPwrL2, " : "",
2063 (msr & 1 << 26) ? "PkgPwrL1, " : "",
2064 (msr & 1 << 25) ? "CorePwr, " : "",
2065 (msr & 1 << 24) ? "Amps, " : "",
2066 (msr & 1 << 22) ? "VR-Therm, " : "",
2067 (msr & 1 << 21) ? "Auto-HWP, " : "",
2068 (msr & 1 << 20) ? "Graphics, " : "",
2069 (msr & 1 << 18) ? "bit18, " : "",
2070 (msr & 1 << 17) ? "ThermStatus, " : "",
2071 (msr & 1 << 16) ? "PROCHOT, " : "");
2074 if (do_gfx_perf_limit_reasons) {
2075 get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
2076 fprintf(stderr, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2077 fprintf(stderr, " (Active: %s%s%s%s%s%s%s%s)",
2078 (msr & 1 << 0) ? "PROCHOT, " : "",
2079 (msr & 1 << 1) ? "ThermStatus, " : "",
2080 (msr & 1 << 4) ? "Graphics, " : "",
2081 (msr & 1 << 6) ? "VR-Therm, " : "",
2082 (msr & 1 << 8) ? "Amps, " : "",
2083 (msr & 1 << 9) ? "GFXPwr, " : "",
2084 (msr & 1 << 10) ? "PkgPwrL1, " : "",
2085 (msr & 1 << 11) ? "PkgPwrL2, " : "");
2086 fprintf(stderr, " (Logged: %s%s%s%s%s%s%s%s)\n",
2087 (msr & 1 << 16) ? "PROCHOT, " : "",
2088 (msr & 1 << 17) ? "ThermStatus, " : "",
2089 (msr & 1 << 20) ? "Graphics, " : "",
2090 (msr & 1 << 22) ? "VR-Therm, " : "",
2091 (msr & 1 << 24) ? "Amps, " : "",
2092 (msr & 1 << 25) ? "GFXPwr, " : "",
2093 (msr & 1 << 26) ? "PkgPwrL1, " : "",
2094 (msr & 1 << 27) ? "PkgPwrL2, " : "");
2096 if (do_ring_perf_limit_reasons) {
2097 get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
2098 fprintf(stderr, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2099 fprintf(stderr, " (Active: %s%s%s%s%s%s)",
2100 (msr & 1 << 0) ? "PROCHOT, " : "",
2101 (msr & 1 << 1) ? "ThermStatus, " : "",
2102 (msr & 1 << 6) ? "VR-Therm, " : "",
2103 (msr & 1 << 8) ? "Amps, " : "",
2104 (msr & 1 << 10) ? "PkgPwrL1, " : "",
2105 (msr & 1 << 11) ? "PkgPwrL2, " : "");
2106 fprintf(stderr, " (Logged: %s%s%s%s%s%s)\n",
2107 (msr & 1 << 16) ? "PROCHOT, " : "",
2108 (msr & 1 << 17) ? "ThermStatus, " : "",
2109 (msr & 1 << 22) ? "VR-Therm, " : "",
2110 (msr & 1 << 24) ? "Amps, " : "",
2111 (msr & 1 << 26) ? "PkgPwrL1, " : "",
2112 (msr & 1 << 27) ? "PkgPwrL2, " : "");
2117 #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
2118 #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
2120 double get_tdp(model)
2122 unsigned long long msr;
2124 if (do_rapl & RAPL_PKG_POWER_INFO)
2125 if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
2126 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
2138 * rapl_dram_energy_units_probe()
2139 * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
2142 rapl_dram_energy_units_probe(int model, double rapl_energy_units)
2144 /* only called for genuine_intel, family 6 */
2147 case 0x3F: /* HSX */
2148 case 0x4F: /* BDX */
2149 case 0x56: /* BDX-DE */
2150 case 0x57: /* KNL */
2151 return (rapl_dram_energy_units = 15.3 / 1000000);
2153 return (rapl_energy_units);
2161 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
2163 void rapl_probe(unsigned int family, unsigned int model)
2165 unsigned long long msr;
2166 unsigned int time_unit;
2178 case 0x3C: /* HSW */
2179 case 0x45: /* HSW */
2180 case 0x46: /* HSW */
2181 case 0x3D: /* BDW */
2182 case 0x47: /* BDW */
2183 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
2185 case 0x4E: /* SKL */
2186 case 0x5E: /* SKL */
2187 do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
2189 case 0x3F: /* HSX */
2190 case 0x4F: /* BDX */
2191 case 0x56: /* BDX-DE */
2192 case 0x57: /* KNL */
2193 do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
2197 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_PKG_PERF_STATUS | RAPL_DRAM_PERF_STATUS | RAPL_PKG_POWER_INFO;
2199 case 0x37: /* BYT */
2200 case 0x4D: /* AVN */
2201 do_rapl = RAPL_PKG | RAPL_CORES ;
2207 /* units on package 0, verify later other packages match */
2208 if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
2211 rapl_power_units = 1.0 / (1 << (msr & 0xF));
2213 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
2215 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
2217 rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
2219 time_unit = msr >> 16 & 0xF;
2223 rapl_time_units = 1.0 / (1 << (time_unit));
2225 tdp = get_tdp(model);
2227 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
2229 fprintf(stderr, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
2234 void perf_limit_reasons_probe(family, model)
2243 case 0x3C: /* HSW */
2244 case 0x45: /* HSW */
2245 case 0x46: /* HSW */
2246 do_gfx_perf_limit_reasons = 1;
2247 case 0x3F: /* HSX */
2248 do_core_perf_limit_reasons = 1;
2249 do_ring_perf_limit_reasons = 1;
2255 int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2257 unsigned long long msr;
2261 if (!(do_dts || do_ptm))
2266 /* DTS is per-core, no need to print for each thread */
2267 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
2270 if (cpu_migrate(cpu)) {
2271 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
2275 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
2276 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
2279 dts = (msr >> 16) & 0x7F;
2280 fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
2281 cpu, msr, tcc_activation_temp - dts);
2284 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
2287 dts = (msr >> 16) & 0x7F;
2288 dts2 = (msr >> 8) & 0x7F;
2289 fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
2290 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
2296 unsigned int resolution;
2298 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
2301 dts = (msr >> 16) & 0x7F;
2302 resolution = (msr >> 27) & 0xF;
2303 fprintf(stderr, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
2304 cpu, msr, tcc_activation_temp - dts, resolution);
2307 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
2310 dts = (msr >> 16) & 0x7F;
2311 dts2 = (msr >> 8) & 0x7F;
2312 fprintf(stderr, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
2313 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
2320 void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
2322 fprintf(stderr, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
2324 ((msr >> 15) & 1) ? "EN" : "DIS",
2325 ((msr >> 0) & 0x7FFF) * rapl_power_units,
2326 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
2327 (((msr >> 16) & 1) ? "EN" : "DIS"));
2332 int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2334 unsigned long long msr;
2340 /* RAPL counters are per package, so print only for 1st thread/package */
2341 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2345 if (cpu_migrate(cpu)) {
2346 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
2350 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
2354 fprintf(stderr, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx "
2355 "(%f Watts, %f Joules, %f sec.)\n", cpu, msr,
2356 rapl_power_units, rapl_energy_units, rapl_time_units);
2358 if (do_rapl & RAPL_PKG_POWER_INFO) {
2360 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
2364 fprintf(stderr, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
2366 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2367 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2368 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2369 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
2372 if (do_rapl & RAPL_PKG) {
2374 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
2377 fprintf(stderr, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
2378 cpu, msr, (msr >> 63) & 1 ? "": "UN");
2380 print_power_limit_msr(cpu, msr, "PKG Limit #1");
2381 fprintf(stderr, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
2383 ((msr >> 47) & 1) ? "EN" : "DIS",
2384 ((msr >> 32) & 0x7FFF) * rapl_power_units,
2385 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
2386 ((msr >> 48) & 1) ? "EN" : "DIS");
2389 if (do_rapl & RAPL_DRAM_POWER_INFO) {
2390 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
2393 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
2395 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2396 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2397 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2398 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
2400 if (do_rapl & RAPL_DRAM) {
2401 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
2403 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
2404 cpu, msr, (msr >> 31) & 1 ? "": "UN");
2406 print_power_limit_msr(cpu, msr, "DRAM Limit");
2408 if (do_rapl & RAPL_CORE_POLICY) {
2410 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
2413 fprintf(stderr, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
2416 if (do_rapl & RAPL_CORES) {
2419 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
2421 fprintf(stderr, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
2422 cpu, msr, (msr >> 31) & 1 ? "": "UN");
2423 print_power_limit_msr(cpu, msr, "Cores Limit");
2426 if (do_rapl & RAPL_GFX) {
2428 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
2431 fprintf(stderr, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
2433 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
2435 fprintf(stderr, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
2436 cpu, msr, (msr >> 31) & 1 ? "": "UN");
2437 print_power_limit_msr(cpu, msr, "GFX Limit");
2444 * SNB adds support for additional MSRs:
2446 * MSR_PKG_C7_RESIDENCY 0x000003fa
2447 * MSR_CORE_C7_RESIDENCY 0x000003fe
2448 * MSR_PKG_C2_RESIDENCY 0x0000060d
2451 int has_snb_msrs(unsigned int family, unsigned int model)
2459 case 0x3A: /* IVB */
2460 case 0x3E: /* IVB Xeon */
2461 case 0x3C: /* HSW */
2462 case 0x3F: /* HSW */
2463 case 0x45: /* HSW */
2464 case 0x46: /* HSW */
2465 case 0x3D: /* BDW */
2466 case 0x47: /* BDW */
2467 case 0x4F: /* BDX */
2468 case 0x56: /* BDX-DE */
2469 case 0x4E: /* SKL */
2470 case 0x5E: /* SKL */
2477 * HSW adds support for additional MSRs:
2479 * MSR_PKG_C8_RESIDENCY 0x00000630
2480 * MSR_PKG_C9_RESIDENCY 0x00000631
2481 * MSR_PKG_C10_RESIDENCY 0x00000632
2483 int has_hsw_msrs(unsigned int family, unsigned int model)
2489 case 0x45: /* HSW */
2490 case 0x3D: /* BDW */
2491 case 0x4E: /* SKL */
2492 case 0x5E: /* SKL */
2499 * SKL adds support for additional MSRS:
2501 * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658
2502 * MSR_PKG_ANY_CORE_C0_RES 0x00000659
2503 * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A
2504 * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B
2506 int has_skl_msrs(unsigned int family, unsigned int model)
2512 case 0x4E: /* SKL */
2513 case 0x5E: /* SKL */
2521 int is_slm(unsigned int family, unsigned int model)
2526 case 0x37: /* BYT */
2527 case 0x4D: /* AVN */
2533 int is_knl(unsigned int family, unsigned int model)
2538 case 0x57: /* KNL */
2544 #define SLM_BCLK_FREQS 5
2545 double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
2547 double slm_bclk(void)
2549 unsigned long long msr = 3;
2553 if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
2554 fprintf(stderr, "SLM BCLK: unknown\n");
2557 if (i >= SLM_BCLK_FREQS) {
2558 fprintf(stderr, "SLM BCLK[%d] invalid\n", i);
2561 freq = slm_freq_table[i];
2563 fprintf(stderr, "SLM BCLK: %.1f Mhz\n", freq);
2568 double discover_bclk(unsigned int family, unsigned int model)
2570 if (has_snb_msrs(family, model))
2572 else if (is_slm(family, model))
2579 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
2580 * the Thermal Control Circuit (TCC) activates.
2581 * This is usually equal to tjMax.
2583 * Older processors do not have this MSR, so there we guess,
2584 * but also allow cmdline over-ride with -T.
2586 * Several MSR temperature values are in units of degrees-C
2587 * below this value, including the Digital Thermal Sensor (DTS),
2588 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
2590 int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2592 unsigned long long msr;
2593 unsigned int target_c_local;
2596 /* tcc_activation_temp is used only for dts or ptm */
2597 if (!(do_dts || do_ptm))
2600 /* this is a per-package concept */
2601 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2605 if (cpu_migrate(cpu)) {
2606 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
2610 if (tcc_activation_temp_override != 0) {
2611 tcc_activation_temp = tcc_activation_temp_override;
2612 fprintf(stderr, "cpu%d: Using cmdline TCC Target (%d C)\n",
2613 cpu, tcc_activation_temp);
2617 /* Temperature Target MSR is Nehalem and newer only */
2618 if (!do_nhm_platform_info)
2621 if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
2624 target_c_local = (msr >> 16) & 0xFF;
2627 fprintf(stderr, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
2628 cpu, msr, target_c_local);
2630 if (!target_c_local)
2633 tcc_activation_temp = target_c_local;
2638 tcc_activation_temp = TJMAX_DEFAULT;
2639 fprintf(stderr, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
2640 cpu, tcc_activation_temp);
2644 void process_cpuid()
2646 unsigned int eax, ebx, ecx, edx, max_level;
2647 unsigned int fms, family, model, stepping;
2649 eax = ebx = ecx = edx = 0;
2651 __get_cpuid(0, &max_level, &ebx, &ecx, &edx);
2653 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
2657 fprintf(stderr, "CPUID(0): %.4s%.4s%.4s ",
2658 (char *)&ebx, (char *)&edx, (char *)&ecx);
2660 __get_cpuid(1, &fms, &ebx, &ecx, &edx);
2661 family = (fms >> 8) & 0xf;
2662 model = (fms >> 4) & 0xf;
2663 stepping = fms & 0xf;
2664 if (family == 6 || family == 0xf)
2665 model += ((fms >> 16) & 0xf) << 4;
2668 fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
2669 max_level, family, model, stepping, family, model, stepping);
2671 if (!(edx & (1 << 5)))
2672 errx(1, "CPUID: no MSR");
2675 * check max extended function levels of CPUID.
2676 * This is needed to check for invariant TSC.
2677 * This check is valid for both Intel and AMD.
2679 ebx = ecx = edx = 0;
2680 __get_cpuid(0x80000000, &max_level, &ebx, &ecx, &edx);
2682 if (max_level >= 0x80000007) {
2685 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
2686 * this check is valid for both Intel and AMD
2688 __get_cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
2689 has_invariant_tsc = edx & (1 << 8);
2693 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
2694 * this check is valid for both Intel and AMD
2697 __get_cpuid(0x6, &eax, &ebx, &ecx, &edx);
2698 has_aperf = ecx & (1 << 0);
2699 do_dts = eax & (1 << 0);
2700 do_ptm = eax & (1 << 6);
2701 has_epb = ecx & (1 << 3);
2704 fprintf(stderr, "CPUID(6): %sAPERF, %sDTS, %sPTM, %sEPB\n",
2705 has_aperf ? "" : "No ",
2706 do_dts ? "" : "No ",
2707 do_ptm ? "" : "No ",
2708 has_epb ? "" : "No ");
2710 if (max_level > 0x15) {
2711 unsigned int eax_crystal;
2712 unsigned int ebx_tsc;
2715 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
2717 eax_crystal = ebx_tsc = crystal_hz = edx = 0;
2718 __get_cpuid(0x15, &eax_crystal, &ebx_tsc, &crystal_hz, &edx);
2722 if (debug && (ebx != 0))
2723 fprintf(stderr, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
2724 eax_crystal, ebx_tsc, crystal_hz);
2726 if (crystal_hz == 0)
2728 case 0x4E: /* SKL */
2729 case 0x5E: /* SKL */
2730 crystal_hz = 24000000; /* 24 MHz */
2737 tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
2739 fprintf(stderr, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
2740 tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal);
2745 do_nhm_platform_info = do_nhm_cstates = do_smi = probe_nhm_msrs(family, model);
2746 do_snb_cstates = has_snb_msrs(family, model);
2747 do_pc2 = do_snb_cstates && (pkg_cstate_limit >= PCL__2);
2748 do_pc3 = (pkg_cstate_limit >= PCL__3);
2749 do_pc6 = (pkg_cstate_limit >= PCL__6);
2750 do_pc7 = do_snb_cstates && (pkg_cstate_limit >= PCL__7);
2751 do_c8_c9_c10 = has_hsw_msrs(family, model);
2752 do_skl_residency = has_skl_msrs(family, model);
2753 do_slm_cstates = is_slm(family, model);
2754 do_knl_cstates = is_knl(family, model);
2755 bclk = discover_bclk(family, model);
2757 rapl_probe(family, model);
2758 perf_limit_reasons_probe(family, model);
2761 dump_cstate_pstate_config_info();
2769 "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
2771 "Turbostat forks the specified COMMAND and prints statistics\n"
2772 "when COMMAND completes.\n"
2773 "If no COMMAND is specified, turbostat wakes every 5-seconds\n"
2774 "to print statistics, until interrupted.\n"
2775 "--debug run in \"debug\" mode\n"
2776 "--interval sec Override default 5-second measurement interval\n"
2777 "--help print this help message\n"
2778 "--counter msr print 32-bit counter at address \"msr\"\n"
2779 "--Counter msr print 64-bit Counter at address \"msr\"\n"
2780 "--msr msr print 32-bit value at address \"msr\"\n"
2781 "--MSR msr print 64-bit Value at address \"msr\"\n"
2782 "--version print version information\n"
2784 "For more help, run \"man turbostat\"\n");
2789 * in /dev/cpu/ return success for names that are numbers
2790 * ie. filter out ".", "..", "microcode".
2792 int dir_filter(const struct dirent *dirp)
2794 if (isdigit(dirp->d_name[0]))
2800 int open_dev_cpu_msr(int dummy1)
2805 void topology_probe()
2808 int max_core_id = 0;
2809 int max_package_id = 0;
2810 int max_siblings = 0;
2811 struct cpu_topology {
2813 int physical_package_id;
2816 /* Initialize num_cpus, max_cpu_num */
2818 topo.max_cpu_num = 0;
2819 for_all_proc_cpus(count_cpus);
2820 if (!summary_only && topo.num_cpus > 1)
2824 fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
2826 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
2828 err(1, "calloc cpus");
2831 * Allocate and initialize cpu_present_set
2833 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
2834 if (cpu_present_set == NULL)
2835 err(3, "CPU_ALLOC");
2836 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2837 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
2838 for_all_proc_cpus(mark_cpu_present);
2841 * Allocate and initialize cpu_affinity_set
2843 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
2844 if (cpu_affinity_set == NULL)
2845 err(3, "CPU_ALLOC");
2846 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2847 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
2852 * find max_core_id, max_package_id
2854 for (i = 0; i <= topo.max_cpu_num; ++i) {
2857 if (cpu_is_not_present(i)) {
2859 fprintf(stderr, "cpu%d NOT PRESENT\n", i);
2862 cpus[i].core_id = get_core_id(i);
2863 if (cpus[i].core_id > max_core_id)
2864 max_core_id = cpus[i].core_id;
2866 cpus[i].physical_package_id = get_physical_package_id(i);
2867 if (cpus[i].physical_package_id > max_package_id)
2868 max_package_id = cpus[i].physical_package_id;
2870 siblings = get_num_ht_siblings(i);
2871 if (siblings > max_siblings)
2872 max_siblings = siblings;
2874 fprintf(stderr, "cpu %d pkg %d core %d\n",
2875 i, cpus[i].physical_package_id, cpus[i].core_id);
2877 topo.num_cores_per_pkg = max_core_id + 1;
2879 fprintf(stderr, "max_core_id %d, sizing for %d cores per package\n",
2880 max_core_id, topo.num_cores_per_pkg);
2881 if (debug && !summary_only && topo.num_cores_per_pkg > 1)
2884 topo.num_packages = max_package_id + 1;
2886 fprintf(stderr, "max_package_id %d, sizing for %d packages\n",
2887 max_package_id, topo.num_packages);
2888 if (debug && !summary_only && topo.num_packages > 1)
2891 topo.num_threads_per_core = max_siblings;
2893 fprintf(stderr, "max_siblings %d\n", max_siblings);
2899 allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
2903 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
2904 topo.num_packages, sizeof(struct thread_data));
2908 for (i = 0; i < topo.num_threads_per_core *
2909 topo.num_cores_per_pkg * topo.num_packages; i++)
2910 (*t)[i].cpu_id = -1;
2912 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
2913 sizeof(struct core_data));
2917 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
2918 (*c)[i].core_id = -1;
2920 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
2924 for (i = 0; i < topo.num_packages; i++)
2925 (*p)[i].package_id = i;
2929 err(1, "calloc counters");
2934 * set cpu_id, core_num, pkg_num
2935 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
2937 * increment topo.num_cores when 1st core in pkg seen
2939 void init_counter(struct thread_data *thread_base, struct core_data *core_base,
2940 struct pkg_data *pkg_base, int thread_num, int core_num,
2941 int pkg_num, int cpu_id)
2943 struct thread_data *t;
2944 struct core_data *c;
2947 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
2948 c = GET_CORE(core_base, core_num, pkg_num);
2949 p = GET_PKG(pkg_base, pkg_num);
2952 if (thread_num == 0) {
2953 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
2954 if (cpu_is_first_core_in_package(cpu_id))
2955 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
2958 c->core_id = core_num;
2959 p->package_id = pkg_num;
2963 int initialize_counters(int cpu_id)
2965 int my_thread_id, my_core_id, my_package_id;
2967 my_package_id = get_physical_package_id(cpu_id);
2968 my_core_id = get_core_id(cpu_id);
2969 my_thread_id = get_cpu_position_in_core(cpu_id);
2973 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2974 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2978 void allocate_output_buffer()
2980 output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
2981 outp = output_buffer;
2983 err(-1, "calloc output buffer");
2986 void setup_all_buffers(void)
2989 allocate_counters(&thread_even, &core_even, &package_even);
2990 allocate_counters(&thread_odd, &core_odd, &package_odd);
2991 allocate_output_buffer();
2992 for_all_proc_cpus(initialize_counters);
2995 void set_base_cpu(void)
2997 base_cpu = sched_getcpu();
2999 err(-ENODEV, "No valid cpus found");
3002 fprintf(stderr, "base_cpu = %d\n", base_cpu);
3005 void turbostat_init()
3007 setup_all_buffers();
3010 check_permissions();
3015 for_all_cpus(print_epb, ODD_COUNTERS);
3018 for_all_cpus(print_perf_limit, ODD_COUNTERS);
3021 for_all_cpus(print_rapl, ODD_COUNTERS);
3023 for_all_cpus(set_temperature_target, ODD_COUNTERS);
3026 for_all_cpus(print_thermal, ODD_COUNTERS);
3029 int fork_it(char **argv)
3034 status = for_all_cpus(get_counters, EVEN_COUNTERS);
3037 /* clear affinity side-effect of get_counters() */
3038 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
3039 gettimeofday(&tv_even, (struct timezone *)NULL);
3044 execvp(argv[0], argv);
3048 if (child_pid == -1)
3051 signal(SIGINT, SIG_IGN);
3052 signal(SIGQUIT, SIG_IGN);
3053 if (waitpid(child_pid, &status, 0) == -1)
3054 err(status, "waitpid");
3057 * n.b. fork_it() does not check for errors from for_all_cpus()
3058 * because re-starting is problematic when forking
3060 for_all_cpus(get_counters, ODD_COUNTERS);
3061 gettimeofday(&tv_odd, (struct timezone *)NULL);
3062 timersub(&tv_odd, &tv_even, &tv_delta);
3063 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
3064 compute_average(EVEN_COUNTERS);
3065 format_all_counters(EVEN_COUNTERS);
3068 fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
3073 int get_and_dump_counters(void)
3077 status = for_all_cpus(get_counters, ODD_COUNTERS);
3081 status = for_all_cpus(dump_counters, ODD_COUNTERS);
3090 void print_version() {
3091 fprintf(stderr, "turbostat version 4.7 17-June, 2015"
3092 " - Len Brown <lenb@kernel.org>\n");
3095 void cmdline(int argc, char **argv)
3098 int option_index = 0;
3099 static struct option long_options[] = {
3100 {"Counter", required_argument, 0, 'C'},
3101 {"counter", required_argument, 0, 'c'},
3102 {"Dump", no_argument, 0, 'D'},
3103 {"debug", no_argument, 0, 'd'},
3104 {"interval", required_argument, 0, 'i'},
3105 {"help", no_argument, 0, 'h'},
3106 {"Joules", no_argument, 0, 'J'},
3107 {"MSR", required_argument, 0, 'M'},
3108 {"msr", required_argument, 0, 'm'},
3109 {"Package", no_argument, 0, 'p'},
3110 {"processor", no_argument, 0, 'p'},
3111 {"Summary", no_argument, 0, 'S'},
3112 {"TCC", required_argument, 0, 'T'},
3113 {"version", no_argument, 0, 'v' },
3119 while ((opt = getopt_long_only(argc, argv, "+C:c:Ddhi:JM:m:PpST:v",
3120 long_options, &option_index)) != -1) {
3123 sscanf(optarg, "%x", &extra_delta_offset64);
3126 sscanf(optarg, "%x", &extra_delta_offset32);
3139 interval_sec = atoi(optarg);
3145 sscanf(optarg, "%x", &extra_msr_offset64);
3148 sscanf(optarg, "%x", &extra_msr_offset32);
3160 tcc_activation_temp_override = atoi(optarg);
3170 int main(int argc, char **argv)
3172 cmdline(argc, argv);
3179 /* dump counters and exit */
3181 return get_and_dump_counters();
3184 * if any params left, it must be a command to fork
3187 return fork_it(argv + optind);