2 * turbostat -- show CPU frequency and C-state residency
3 * on modern Intel turbo-capable processors.
5 * Copyright (c) 2013 Intel Corporation.
6 * Len Brown <len.brown@intel.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
28 #include <sys/types.h>
31 #include <sys/resource.h>
42 char *proc_stat = "/proc/stat";
43 unsigned int interval_sec = 5; /* set with -i interval_sec */
44 unsigned int verbose; /* set with -v */
45 unsigned int rapl_verbose; /* set with -R */
46 unsigned int rapl_joules; /* set with -J */
47 unsigned int thermal_verbose; /* set with -T */
48 unsigned int summary_only; /* set with -S */
49 unsigned int dump_only; /* set with -s */
52 unsigned int do_nhm_cstates;
53 unsigned int do_snb_cstates;
54 unsigned int do_c8_c9_c10;
55 unsigned int do_slm_cstates;
56 unsigned int use_c1_residency_msr;
57 unsigned int has_aperf;
59 unsigned int units = 1000000000; /* Ghz etc */
60 unsigned int genuine_intel;
61 unsigned int has_invariant_tsc;
62 unsigned int do_nehalem_platform_info;
63 unsigned int do_nehalem_turbo_ratio_limit;
64 unsigned int do_ivt_turbo_ratio_limit;
65 unsigned int extra_msr_offset32;
66 unsigned int extra_msr_offset64;
67 unsigned int extra_delta_offset32;
68 unsigned int extra_delta_offset64;
71 unsigned int show_pkg;
72 unsigned int show_core;
73 unsigned int show_cpu;
74 unsigned int show_pkg_only;
75 unsigned int show_core_only;
76 char *output_buffer, *outp;
80 unsigned int tcc_activation_temp;
81 unsigned int tcc_activation_temp_override;
82 double rapl_power_units, rapl_energy_units, rapl_time_units;
83 double rapl_joule_counter_range;
85 #define RAPL_PKG (1 << 0)
86 /* 0x610 MSR_PKG_POWER_LIMIT */
87 /* 0x611 MSR_PKG_ENERGY_STATUS */
88 #define RAPL_PKG_PERF_STATUS (1 << 1)
89 /* 0x613 MSR_PKG_PERF_STATUS */
90 #define RAPL_PKG_POWER_INFO (1 << 2)
91 /* 0x614 MSR_PKG_POWER_INFO */
93 #define RAPL_DRAM (1 << 3)
94 /* 0x618 MSR_DRAM_POWER_LIMIT */
95 /* 0x619 MSR_DRAM_ENERGY_STATUS */
96 /* 0x61c MSR_DRAM_POWER_INFO */
97 #define RAPL_DRAM_PERF_STATUS (1 << 4)
98 /* 0x61b MSR_DRAM_PERF_STATUS */
100 #define RAPL_CORES (1 << 5)
101 /* 0x638 MSR_PP0_POWER_LIMIT */
102 /* 0x639 MSR_PP0_ENERGY_STATUS */
103 #define RAPL_CORE_POLICY (1 << 6)
104 /* 0x63a MSR_PP0_POLICY */
107 #define RAPL_GFX (1 << 7)
108 /* 0x640 MSR_PP1_POWER_LIMIT */
109 /* 0x641 MSR_PP1_ENERGY_STATUS */
110 /* 0x642 MSR_PP1_POLICY */
111 #define TJMAX_DEFAULT 100
113 #define MAX(a, b) ((a) > (b) ? (a) : (b))
115 int aperf_mperf_unstable;
119 cpu_set_t *cpu_present_set, *cpu_affinity_set;
120 size_t cpu_present_setsize, cpu_affinity_setsize;
123 unsigned long long tsc;
124 unsigned long long aperf;
125 unsigned long long mperf;
126 unsigned long long c1;
127 unsigned long long extra_msr64;
128 unsigned long long extra_delta64;
129 unsigned long long extra_msr32;
130 unsigned long long extra_delta32;
131 unsigned int smi_count;
134 #define CPU_IS_FIRST_THREAD_IN_CORE 0x2
135 #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
136 } *thread_even, *thread_odd;
139 unsigned long long c3;
140 unsigned long long c6;
141 unsigned long long c7;
142 unsigned int core_temp_c;
143 unsigned int core_id;
144 } *core_even, *core_odd;
147 unsigned long long pc2;
148 unsigned long long pc3;
149 unsigned long long pc6;
150 unsigned long long pc7;
151 unsigned long long pc8;
152 unsigned long long pc9;
153 unsigned long long pc10;
154 unsigned int package_id;
155 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
156 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
157 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
158 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
159 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
160 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
161 unsigned int pkg_temp_c;
163 } *package_even, *package_odd;
165 #define ODD_COUNTERS thread_odd, core_odd, package_odd
166 #define EVEN_COUNTERS thread_even, core_even, package_even
168 #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
169 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
170 topo.num_threads_per_core + \
171 (core_no) * topo.num_threads_per_core + (thread_no))
172 #define GET_CORE(core_base, core_no, pkg_no) \
173 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
174 #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
176 struct system_summary {
177 struct thread_data threads;
178 struct core_data cores;
179 struct pkg_data packages;
188 int num_cores_per_pkg;
189 int num_threads_per_core;
192 struct timeval tv_even, tv_odd, tv_delta;
194 void setup_all_buffers(void);
196 int cpu_is_not_present(int cpu)
198 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
201 * run func(thread, core, package) in topology order
202 * skip non-present cpus
205 int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
206 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
208 int retval, pkg_no, core_no, thread_no;
210 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
211 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
212 for (thread_no = 0; thread_no <
213 topo.num_threads_per_core; ++thread_no) {
214 struct thread_data *t;
218 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
220 if (cpu_is_not_present(t->cpu_id))
223 c = GET_CORE(core_base, core_no, pkg_no);
224 p = GET_PKG(pkg_base, pkg_no);
226 retval = func(t, c, p);
235 int cpu_migrate(int cpu)
237 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
238 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
239 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
245 int get_msr(int cpu, off_t offset, unsigned long long *msr)
251 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
252 fd = open(pathname, O_RDONLY);
256 retval = pread(fd, msr, sizeof *msr, offset);
259 if (retval != sizeof *msr) {
260 fprintf(stderr, "%s offset 0x%llx read failed\n", pathname, (unsigned long long)offset);
267 void print_header(void)
270 outp += sprintf(outp, "pk");
272 outp += sprintf(outp, " ");
274 outp += sprintf(outp, "cor");
276 outp += sprintf(outp, " CPU");
277 if (show_pkg || show_core || show_cpu)
278 outp += sprintf(outp, " ");
280 outp += sprintf(outp, " %%c0");
282 outp += sprintf(outp, " GHz");
283 outp += sprintf(outp, " TSC");
285 outp += sprintf(outp, " SMI");
286 if (extra_delta_offset32)
287 outp += sprintf(outp, " count 0x%03X", extra_delta_offset32);
288 if (extra_delta_offset64)
289 outp += sprintf(outp, " COUNT 0x%03X", extra_delta_offset64);
290 if (extra_msr_offset32)
291 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset32);
292 if (extra_msr_offset64)
293 outp += sprintf(outp, " MSR 0x%03X", extra_msr_offset64);
295 outp += sprintf(outp, " %%c1");
296 if (do_nhm_cstates && !do_slm_cstates)
297 outp += sprintf(outp, " %%c3");
299 outp += sprintf(outp, " %%c6");
301 outp += sprintf(outp, " %%c7");
304 outp += sprintf(outp, " CTMP");
306 outp += sprintf(outp, " PTMP");
309 outp += sprintf(outp, " %%pc2");
310 if (do_nhm_cstates && !do_slm_cstates)
311 outp += sprintf(outp, " %%pc3");
312 if (do_nhm_cstates && !do_slm_cstates)
313 outp += sprintf(outp, " %%pc6");
315 outp += sprintf(outp, " %%pc7");
317 outp += sprintf(outp, " %%pc8");
318 outp += sprintf(outp, " %%pc9");
319 outp += sprintf(outp, " %%pc10");
322 if (do_rapl && !rapl_joules) {
323 if (do_rapl & RAPL_PKG)
324 outp += sprintf(outp, " Pkg_W");
325 if (do_rapl & RAPL_CORES)
326 outp += sprintf(outp, " Cor_W");
327 if (do_rapl & RAPL_GFX)
328 outp += sprintf(outp, " GFX_W");
329 if (do_rapl & RAPL_DRAM)
330 outp += sprintf(outp, " RAM_W");
331 if (do_rapl & RAPL_PKG_PERF_STATUS)
332 outp += sprintf(outp, " PKG_%%");
333 if (do_rapl & RAPL_DRAM_PERF_STATUS)
334 outp += sprintf(outp, " RAM_%%");
336 if (do_rapl & RAPL_PKG)
337 outp += sprintf(outp, " Pkg_J");
338 if (do_rapl & RAPL_CORES)
339 outp += sprintf(outp, " Cor_J");
340 if (do_rapl & RAPL_GFX)
341 outp += sprintf(outp, " GFX_J");
342 if (do_rapl & RAPL_DRAM)
343 outp += sprintf(outp, " RAM_W");
344 if (do_rapl & RAPL_PKG_PERF_STATUS)
345 outp += sprintf(outp, " PKG_%%");
346 if (do_rapl & RAPL_DRAM_PERF_STATUS)
347 outp += sprintf(outp, " RAM_%%");
348 outp += sprintf(outp, " time");
351 outp += sprintf(outp, "\n");
354 int dump_counters(struct thread_data *t, struct core_data *c,
357 outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
360 outp += sprintf(outp, "CPU: %d flags 0x%x\n",
361 t->cpu_id, t->flags);
362 outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
363 outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
364 outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
365 outp += sprintf(outp, "c1: %016llX\n", t->c1);
366 outp += sprintf(outp, "msr0x%x: %08llX\n",
367 extra_delta_offset32, t->extra_delta32);
368 outp += sprintf(outp, "msr0x%x: %016llX\n",
369 extra_delta_offset64, t->extra_delta64);
370 outp += sprintf(outp, "msr0x%x: %08llX\n",
371 extra_msr_offset32, t->extra_msr32);
372 outp += sprintf(outp, "msr0x%x: %016llX\n",
373 extra_msr_offset64, t->extra_msr64);
375 outp += sprintf(outp, "SMI: %08X\n", t->smi_count);
379 outp += sprintf(outp, "core: %d\n", c->core_id);
380 outp += sprintf(outp, "c3: %016llX\n", c->c3);
381 outp += sprintf(outp, "c6: %016llX\n", c->c6);
382 outp += sprintf(outp, "c7: %016llX\n", c->c7);
383 outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
387 outp += sprintf(outp, "package: %d\n", p->package_id);
388 outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
389 outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
390 outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
391 outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
392 outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
393 outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
394 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
395 outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
396 outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
397 outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
398 outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
399 outp += sprintf(outp, "Throttle PKG: %0X\n",
400 p->rapl_pkg_perf_status);
401 outp += sprintf(outp, "Throttle RAM: %0X\n",
402 p->rapl_dram_perf_status);
403 outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
406 outp += sprintf(outp, "\n");
412 * column formatting convention & formats
413 * package: "pk" 2 columns %2d
414 * core: "cor" 3 columns %3d
415 * CPU: "CPU" 3 columns %3d
420 * GHz: "GHz" 3 columns %3.2
421 * TSC: "TSC" 3 columns %3.2
422 * SMI: "SMI" 4 columns %4d
423 * percentage " %pc3" %6.2
424 * Perf Status percentage: %5.2
425 * "CTMP" 4 columns %4d
427 int format_counters(struct thread_data *t, struct core_data *c,
430 double interval_float;
433 /* if showing only 1st thread in core and this isn't one, bail out */
434 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
437 /* if showing only 1st thread in pkg and this isn't one, bail out */
438 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
441 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
443 /* topo columns, print blanks on 1st (average) line */
444 if (t == &average.threads) {
446 outp += sprintf(outp, " ");
447 if (show_pkg && show_core)
448 outp += sprintf(outp, " ");
450 outp += sprintf(outp, " ");
452 outp += sprintf(outp, " " " ");
456 outp += sprintf(outp, "%2d", p->package_id);
458 outp += sprintf(outp, " ");
460 if (show_pkg && show_core)
461 outp += sprintf(outp, " ");
464 outp += sprintf(outp, "%3d", c->core_id);
466 outp += sprintf(outp, " ");
469 outp += sprintf(outp, " %3d", t->cpu_id);
472 if (do_nhm_cstates) {
473 if (show_pkg || show_core || show_cpu)
474 outp += sprintf(outp, " ");
476 outp += sprintf(outp, "%6.2f", 100.0 * t->mperf/t->tsc);
478 outp += sprintf(outp, " ****");
483 if (!aperf_mperf_unstable) {
484 outp += sprintf(outp, " %3.2f",
485 1.0 * t->tsc / units * t->aperf /
486 t->mperf / interval_float);
488 if (t->aperf > t->tsc || t->mperf > t->tsc) {
489 outp += sprintf(outp, " ***");
491 outp += sprintf(outp, "%3.1f*",
494 t->mperf / interval_float);
500 outp += sprintf(outp, "%5.2f", 1.0 * t->tsc/units/interval_float);
504 outp += sprintf(outp, "%4d", t->smi_count);
507 if (extra_delta_offset32)
508 outp += sprintf(outp, " %11llu", t->extra_delta32);
511 if (extra_delta_offset64)
512 outp += sprintf(outp, " %11llu", t->extra_delta64);
514 if (extra_msr_offset32)
515 outp += sprintf(outp, " 0x%08llx", t->extra_msr32);
518 if (extra_msr_offset64)
519 outp += sprintf(outp, " 0x%016llx", t->extra_msr64);
521 if (do_nhm_cstates) {
523 outp += sprintf(outp, " %6.2f", 100.0 * t->c1/t->tsc);
525 outp += sprintf(outp, " ****");
528 /* print per-core data only for 1st thread in core */
529 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
532 if (do_nhm_cstates && !do_slm_cstates)
533 outp += sprintf(outp, " %6.2f", 100.0 * c->c3/t->tsc);
535 outp += sprintf(outp, " %6.2f", 100.0 * c->c6/t->tsc);
537 outp += sprintf(outp, " %6.2f", 100.0 * c->c7/t->tsc);
540 outp += sprintf(outp, " %4d", c->core_temp_c);
542 /* print per-package data only for 1st core in package */
543 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
547 outp += sprintf(outp, " %4d", p->pkg_temp_c);
550 outp += sprintf(outp, " %6.2f", 100.0 * p->pc2/t->tsc);
551 if (do_nhm_cstates && !do_slm_cstates)
552 outp += sprintf(outp, " %6.2f", 100.0 * p->pc3/t->tsc);
553 if (do_nhm_cstates && !do_slm_cstates)
554 outp += sprintf(outp, " %6.2f", 100.0 * p->pc6/t->tsc);
556 outp += sprintf(outp, " %6.2f", 100.0 * p->pc7/t->tsc);
558 outp += sprintf(outp, " %6.2f", 100.0 * p->pc8/t->tsc);
559 outp += sprintf(outp, " %6.2f", 100.0 * p->pc9/t->tsc);
560 outp += sprintf(outp, " %6.2f", 100.0 * p->pc10/t->tsc);
564 * If measurement interval exceeds minimum RAPL Joule Counter range,
565 * indicate that results are suspect by printing "**" in fraction place.
567 if (interval_float < rapl_joule_counter_range) {
575 if (do_rapl && !rapl_joules) {
576 if (do_rapl & RAPL_PKG)
577 outp += sprintf(outp, fmt6, p->energy_pkg * rapl_energy_units / interval_float);
578 if (do_rapl & RAPL_CORES)
579 outp += sprintf(outp, fmt6, p->energy_cores * rapl_energy_units / interval_float);
580 if (do_rapl & RAPL_GFX)
581 outp += sprintf(outp, fmt5, p->energy_gfx * rapl_energy_units / interval_float);
582 if (do_rapl & RAPL_DRAM)
583 outp += sprintf(outp, fmt5, p->energy_dram * rapl_energy_units / interval_float);
584 if (do_rapl & RAPL_PKG_PERF_STATUS)
585 outp += sprintf(outp, fmt5, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
586 if (do_rapl & RAPL_DRAM_PERF_STATUS)
587 outp += sprintf(outp, fmt5, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
589 if (do_rapl & RAPL_PKG)
590 outp += sprintf(outp, fmt6,
591 p->energy_pkg * rapl_energy_units);
592 if (do_rapl & RAPL_CORES)
593 outp += sprintf(outp, fmt6,
594 p->energy_cores * rapl_energy_units);
595 if (do_rapl & RAPL_GFX)
596 outp += sprintf(outp, fmt5,
597 p->energy_gfx * rapl_energy_units);
598 if (do_rapl & RAPL_DRAM)
599 outp += sprintf(outp, fmt5,
600 p->energy_dram * rapl_energy_units);
601 if (do_rapl & RAPL_PKG_PERF_STATUS)
602 outp += sprintf(outp, fmt5, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
603 if (do_rapl & RAPL_DRAM_PERF_STATUS)
604 outp += sprintf(outp, fmt5, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
605 outp += sprintf(outp, fmt5, interval_float);
609 outp += sprintf(outp, "\n");
616 fputs(output_buffer, stdout);
618 outp = output_buffer;
622 fputs(output_buffer, stderr);
623 outp = output_buffer;
625 void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
629 if (!printed || !summary_only)
632 if (topo.num_cpus > 1)
633 format_counters(&average.threads, &average.cores,
641 for_all_cpus(format_counters, t, c, p);
644 #define DELTA_WRAP32(new, old) \
648 old = 0x100000000 + new - old; \
652 delta_package(struct pkg_data *new, struct pkg_data *old)
654 old->pc2 = new->pc2 - old->pc2;
655 old->pc3 = new->pc3 - old->pc3;
656 old->pc6 = new->pc6 - old->pc6;
657 old->pc7 = new->pc7 - old->pc7;
658 old->pc8 = new->pc8 - old->pc8;
659 old->pc9 = new->pc9 - old->pc9;
660 old->pc10 = new->pc10 - old->pc10;
661 old->pkg_temp_c = new->pkg_temp_c;
663 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
664 DELTA_WRAP32(new->energy_cores, old->energy_cores);
665 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
666 DELTA_WRAP32(new->energy_dram, old->energy_dram);
667 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
668 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
672 delta_core(struct core_data *new, struct core_data *old)
674 old->c3 = new->c3 - old->c3;
675 old->c6 = new->c6 - old->c6;
676 old->c7 = new->c7 - old->c7;
677 old->core_temp_c = new->core_temp_c;
684 delta_thread(struct thread_data *new, struct thread_data *old,
685 struct core_data *core_delta)
687 old->tsc = new->tsc - old->tsc;
689 /* check for TSC < 1 Mcycles over interval */
690 if (old->tsc < (1000 * 1000))
691 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
692 "You can disable all c-states by booting with \"idle=poll\"\n"
693 "or just the deep ones with \"processor.max_cstate=1\"");
695 old->c1 = new->c1 - old->c1;
697 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
698 old->aperf = new->aperf - old->aperf;
699 old->mperf = new->mperf - old->mperf;
702 if (!aperf_mperf_unstable) {
703 fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname);
704 fprintf(stderr, "* Frequency results do not cover entire interval *\n");
705 fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n");
707 aperf_mperf_unstable = 1;
710 * mperf delta is likely a huge "positive" number
711 * can not use it for calculating c0 time
718 if (use_c1_residency_msr) {
720 * Some models have a dedicated C1 residency MSR,
721 * which should be more accurate than the derivation below.
725 * As counter collection is not atomic,
726 * it is possible for mperf's non-halted cycles + idle states
727 * to exceed TSC's all cycles: show c1 = 0% in that case.
729 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
732 /* normal case, derive c1 */
733 old->c1 = old->tsc - old->mperf - core_delta->c3
734 - core_delta->c6 - core_delta->c7;
738 if (old->mperf == 0) {
739 if (verbose > 1) fprintf(stderr, "cpu%d MPERF 0!\n", old->cpu_id);
740 old->mperf = 1; /* divide by 0 protection */
743 old->extra_delta32 = new->extra_delta32 - old->extra_delta32;
744 old->extra_delta32 &= 0xFFFFFFFF;
746 old->extra_delta64 = new->extra_delta64 - old->extra_delta64;
749 * Extra MSR is just a snapshot, simply copy latest w/o subtracting
751 old->extra_msr32 = new->extra_msr32;
752 old->extra_msr64 = new->extra_msr64;
755 old->smi_count = new->smi_count - old->smi_count;
758 int delta_cpu(struct thread_data *t, struct core_data *c,
759 struct pkg_data *p, struct thread_data *t2,
760 struct core_data *c2, struct pkg_data *p2)
762 /* calculate core delta only for 1st thread in core */
763 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
766 /* always calculate thread delta */
767 delta_thread(t, t2, c2); /* c2 is core delta */
769 /* calculate package delta only for 1st core in package */
770 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
771 delta_package(p, p2);
776 void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
784 t->extra_delta32 = 0;
785 t->extra_delta64 = 0;
787 /* tells format_counters to dump all fields from this set */
788 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
807 p->rapl_pkg_perf_status = 0;
808 p->rapl_dram_perf_status = 0;
811 int sum_counters(struct thread_data *t, struct core_data *c,
814 average.threads.tsc += t->tsc;
815 average.threads.aperf += t->aperf;
816 average.threads.mperf += t->mperf;
817 average.threads.c1 += t->c1;
819 average.threads.extra_delta32 += t->extra_delta32;
820 average.threads.extra_delta64 += t->extra_delta64;
822 /* sum per-core values only for 1st thread in core */
823 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
826 average.cores.c3 += c->c3;
827 average.cores.c6 += c->c6;
828 average.cores.c7 += c->c7;
830 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
832 /* sum per-pkg values only for 1st core in pkg */
833 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
836 average.packages.pc2 += p->pc2;
837 average.packages.pc3 += p->pc3;
838 average.packages.pc6 += p->pc6;
839 average.packages.pc7 += p->pc7;
840 average.packages.pc8 += p->pc8;
841 average.packages.pc9 += p->pc9;
842 average.packages.pc10 += p->pc10;
844 average.packages.energy_pkg += p->energy_pkg;
845 average.packages.energy_dram += p->energy_dram;
846 average.packages.energy_cores += p->energy_cores;
847 average.packages.energy_gfx += p->energy_gfx;
849 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
851 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
852 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
856 * sum the counters for all cpus in the system
857 * compute the weighted average
859 void compute_average(struct thread_data *t, struct core_data *c,
862 clear_counters(&average.threads, &average.cores, &average.packages);
864 for_all_cpus(sum_counters, t, c, p);
866 average.threads.tsc /= topo.num_cpus;
867 average.threads.aperf /= topo.num_cpus;
868 average.threads.mperf /= topo.num_cpus;
869 average.threads.c1 /= topo.num_cpus;
871 average.threads.extra_delta32 /= topo.num_cpus;
872 average.threads.extra_delta32 &= 0xFFFFFFFF;
874 average.threads.extra_delta64 /= topo.num_cpus;
876 average.cores.c3 /= topo.num_cores;
877 average.cores.c6 /= topo.num_cores;
878 average.cores.c7 /= topo.num_cores;
880 average.packages.pc2 /= topo.num_packages;
881 average.packages.pc3 /= topo.num_packages;
882 average.packages.pc6 /= topo.num_packages;
883 average.packages.pc7 /= topo.num_packages;
885 average.packages.pc8 /= topo.num_packages;
886 average.packages.pc9 /= topo.num_packages;
887 average.packages.pc10 /= topo.num_packages;
890 static unsigned long long rdtsc(void)
892 unsigned int low, high;
894 asm volatile("rdtsc" : "=a" (low), "=d" (high));
896 return low | ((unsigned long long)high) << 32;
903 * acquire and record local counters for that cpu
905 int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
908 unsigned long long msr;
910 if (cpu_migrate(cpu)) {
911 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
915 t->tsc = rdtsc(); /* we are running on local CPU of interest */
918 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
920 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
925 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
927 t->smi_count = msr & 0xFFFFFFFF;
929 if (extra_delta_offset32) {
930 if (get_msr(cpu, extra_delta_offset32, &msr))
932 t->extra_delta32 = msr & 0xFFFFFFFF;
935 if (extra_delta_offset64)
936 if (get_msr(cpu, extra_delta_offset64, &t->extra_delta64))
939 if (extra_msr_offset32) {
940 if (get_msr(cpu, extra_msr_offset32, &msr))
942 t->extra_msr32 = msr & 0xFFFFFFFF;
945 if (extra_msr_offset64)
946 if (get_msr(cpu, extra_msr_offset64, &t->extra_msr64))
949 if (use_c1_residency_msr) {
950 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
954 /* collect core counters only for 1st thread in core */
955 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
958 if (do_nhm_cstates && !do_slm_cstates) {
959 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
963 if (do_nhm_cstates) {
964 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
969 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
973 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
975 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
979 /* collect package counters only for 1st core in package */
980 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
983 if (do_nhm_cstates && !do_slm_cstates) {
984 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
986 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
989 if (do_snb_cstates) {
990 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
992 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
996 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
998 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1000 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1003 if (do_rapl & RAPL_PKG) {
1004 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1006 p->energy_pkg = msr & 0xFFFFFFFF;
1008 if (do_rapl & RAPL_CORES) {
1009 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1011 p->energy_cores = msr & 0xFFFFFFFF;
1013 if (do_rapl & RAPL_DRAM) {
1014 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1016 p->energy_dram = msr & 0xFFFFFFFF;
1018 if (do_rapl & RAPL_GFX) {
1019 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1021 p->energy_gfx = msr & 0xFFFFFFFF;
1023 if (do_rapl & RAPL_PKG_PERF_STATUS) {
1024 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1026 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1028 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1029 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1031 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1034 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1036 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1041 void print_verbose_header(void)
1043 unsigned long long msr;
1046 if (!do_nehalem_platform_info)
1049 get_msr(0, MSR_NHM_PLATFORM_INFO, &msr);
1051 fprintf(stderr, "cpu0: MSR_NHM_PLATFORM_INFO: 0x%08llx\n", msr);
1053 ratio = (msr >> 40) & 0xFF;
1054 fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency\n",
1055 ratio, bclk, ratio * bclk);
1057 ratio = (msr >> 8) & 0xFF;
1058 fprintf(stderr, "%d * %.0f = %.0f MHz TSC frequency\n",
1059 ratio, bclk, ratio * bclk);
1061 get_msr(0, MSR_IA32_POWER_CTL, &msr);
1062 fprintf(stderr, "cpu0: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
1063 msr, msr & 0x2 ? "EN" : "DIS");
1065 if (!do_ivt_turbo_ratio_limit)
1066 goto print_nhm_turbo_ratio_limits;
1068 get_msr(0, MSR_IVT_TURBO_RATIO_LIMIT, &msr);
1070 fprintf(stderr, "cpu0: MSR_IVT_TURBO_RATIO_LIMIT: 0x%08llx\n", msr);
1072 ratio = (msr >> 56) & 0xFF;
1074 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 16 active cores\n",
1075 ratio, bclk, ratio * bclk);
1077 ratio = (msr >> 48) & 0xFF;
1079 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 15 active cores\n",
1080 ratio, bclk, ratio * bclk);
1082 ratio = (msr >> 40) & 0xFF;
1084 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 14 active cores\n",
1085 ratio, bclk, ratio * bclk);
1087 ratio = (msr >> 32) & 0xFF;
1089 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 13 active cores\n",
1090 ratio, bclk, ratio * bclk);
1092 ratio = (msr >> 24) & 0xFF;
1094 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 12 active cores\n",
1095 ratio, bclk, ratio * bclk);
1097 ratio = (msr >> 16) & 0xFF;
1099 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 11 active cores\n",
1100 ratio, bclk, ratio * bclk);
1102 ratio = (msr >> 8) & 0xFF;
1104 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 10 active cores\n",
1105 ratio, bclk, ratio * bclk);
1107 ratio = (msr >> 0) & 0xFF;
1109 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 9 active cores\n",
1110 ratio, bclk, ratio * bclk);
1112 print_nhm_turbo_ratio_limits:
1113 get_msr(0, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
1115 #define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
1116 #define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
1118 fprintf(stderr, "cpu0: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", msr);
1120 fprintf(stderr, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: ",
1121 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
1122 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
1123 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
1124 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
1125 (msr & (1 << 15)) ? "" : "UN",
1126 (unsigned int)msr & 7);
1131 fprintf(stderr, do_slm_cstates ? "no pkg states" : "pc0");
1134 fprintf(stderr, do_slm_cstates ? "no pkg states" : do_snb_cstates ? "pc2" : "pc0");
1137 fprintf(stderr, do_slm_cstates ? "invalid" : do_snb_cstates ? "pc6-noret" : "pc3");
1140 fprintf(stderr, do_slm_cstates ? "invalid" : "pc6");
1143 fprintf(stderr, do_slm_cstates ? "pc4" : "pc7");
1146 fprintf(stderr, do_slm_cstates ? "invalid" : do_snb_cstates ? "pc7s" : "invalid");
1149 fprintf(stderr, do_slm_cstates ? "pc6" : "invalid");
1152 fprintf(stderr, do_slm_cstates ? "pc7" : "unlimited");
1155 fprintf(stderr, "invalid");
1157 fprintf(stderr, ")\n");
1159 if (!do_nehalem_turbo_ratio_limit)
1162 get_msr(0, MSR_NHM_TURBO_RATIO_LIMIT, &msr);
1164 fprintf(stderr, "cpu0: MSR_NHM_TURBO_RATIO_LIMIT: 0x%08llx\n", msr);
1166 ratio = (msr >> 56) & 0xFF;
1168 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 8 active cores\n",
1169 ratio, bclk, ratio * bclk);
1171 ratio = (msr >> 48) & 0xFF;
1173 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 7 active cores\n",
1174 ratio, bclk, ratio * bclk);
1176 ratio = (msr >> 40) & 0xFF;
1178 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 6 active cores\n",
1179 ratio, bclk, ratio * bclk);
1181 ratio = (msr >> 32) & 0xFF;
1183 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 5 active cores\n",
1184 ratio, bclk, ratio * bclk);
1186 ratio = (msr >> 24) & 0xFF;
1188 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
1189 ratio, bclk, ratio * bclk);
1191 ratio = (msr >> 16) & 0xFF;
1193 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
1194 ratio, bclk, ratio * bclk);
1196 ratio = (msr >> 8) & 0xFF;
1198 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
1199 ratio, bclk, ratio * bclk);
1201 ratio = (msr >> 0) & 0xFF;
1203 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
1204 ratio, bclk, ratio * bclk);
1207 void free_all_buffers(void)
1209 CPU_FREE(cpu_present_set);
1210 cpu_present_set = NULL;
1211 cpu_present_set = 0;
1213 CPU_FREE(cpu_affinity_set);
1214 cpu_affinity_set = NULL;
1215 cpu_affinity_setsize = 0;
1223 package_even = NULL;
1233 free(output_buffer);
1234 output_buffer = NULL;
1239 * Open a file, and exit on failure
1241 FILE *fopen_or_die(const char *path, const char *mode)
1243 FILE *filep = fopen(path, "r");
1245 err(1, "%s: open failed", path);
1250 * Parse a file containing a single int.
1252 int parse_int_file(const char *fmt, ...)
1255 char path[PATH_MAX];
1259 va_start(args, fmt);
1260 vsnprintf(path, sizeof(path), fmt, args);
1262 filep = fopen_or_die(path, "r");
1263 if (fscanf(filep, "%d", &value) != 1)
1264 err(1, "%s: failed to parse number from file", path);
1270 * cpu_is_first_sibling_in_core(cpu)
1271 * return 1 if given CPU is 1st HT sibling in the core
1273 int cpu_is_first_sibling_in_core(int cpu)
1275 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
1279 * cpu_is_first_core_in_package(cpu)
1280 * return 1 if given CPU is 1st core in package
1282 int cpu_is_first_core_in_package(int cpu)
1284 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
1287 int get_physical_package_id(int cpu)
1289 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
1292 int get_core_id(int cpu)
1294 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
1297 int get_num_ht_siblings(int cpu)
1305 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
1306 filep = fopen_or_die(path, "r");
1309 * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4)
1310 * otherwinse 1 sibling (self).
1312 matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2);
1323 * run func(thread, core, package) in topology order
1324 * skip non-present cpus
1327 int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
1328 struct pkg_data *, struct thread_data *, struct core_data *,
1329 struct pkg_data *), struct thread_data *thread_base,
1330 struct core_data *core_base, struct pkg_data *pkg_base,
1331 struct thread_data *thread_base2, struct core_data *core_base2,
1332 struct pkg_data *pkg_base2)
1334 int retval, pkg_no, core_no, thread_no;
1336 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
1337 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
1338 for (thread_no = 0; thread_no <
1339 topo.num_threads_per_core; ++thread_no) {
1340 struct thread_data *t, *t2;
1341 struct core_data *c, *c2;
1342 struct pkg_data *p, *p2;
1344 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
1346 if (cpu_is_not_present(t->cpu_id))
1349 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
1351 c = GET_CORE(core_base, core_no, pkg_no);
1352 c2 = GET_CORE(core_base2, core_no, pkg_no);
1354 p = GET_PKG(pkg_base, pkg_no);
1355 p2 = GET_PKG(pkg_base2, pkg_no);
1357 retval = func(t, c, p, t2, c2, p2);
1367 * run func(cpu) on every cpu in /proc/stat
1368 * return max_cpu number
1370 int for_all_proc_cpus(int (func)(int))
1376 fp = fopen_or_die(proc_stat, "r");
1378 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
1380 err(1, "%s: failed to parse format", proc_stat);
1383 retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
1387 retval = func(cpu_num);
1397 void re_initialize(void)
1400 setup_all_buffers();
1401 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
1407 * remember the last one seen, it will be the max
1409 int count_cpus(int cpu)
1411 if (topo.max_cpu_num < cpu)
1412 topo.max_cpu_num = cpu;
1417 int mark_cpu_present(int cpu)
1419 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
1423 void turbostat_loop()
1431 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
1434 } else if (retval == -1) {
1435 if (restarted > 1) {
1442 gettimeofday(&tv_even, (struct timezone *)NULL);
1445 if (for_all_proc_cpus(cpu_is_not_present)) {
1449 sleep(interval_sec);
1450 retval = for_all_cpus(get_counters, ODD_COUNTERS);
1453 } else if (retval == -1) {
1457 gettimeofday(&tv_odd, (struct timezone *)NULL);
1458 timersub(&tv_odd, &tv_even, &tv_delta);
1459 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
1460 compute_average(EVEN_COUNTERS);
1461 format_all_counters(EVEN_COUNTERS);
1463 sleep(interval_sec);
1464 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
1467 } else if (retval == -1) {
1471 gettimeofday(&tv_even, (struct timezone *)NULL);
1472 timersub(&tv_even, &tv_odd, &tv_delta);
1473 for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS);
1474 compute_average(ODD_COUNTERS);
1475 format_all_counters(ODD_COUNTERS);
1480 void check_dev_msr()
1484 if (stat("/dev/cpu/0/msr", &sb))
1485 err(-5, "no /dev/cpu/0/msr\n"
1486 "Try \"# modprobe msr\"");
1489 void check_super_user()
1492 errx(-6, "must be root");
1495 int has_nehalem_turbo_ratio_limit(unsigned int family, unsigned int model)
1504 case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
1505 case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
1506 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
1507 case 0x25: /* Westmere Client - Clarkdale, Arrandale */
1508 case 0x2C: /* Westmere EP - Gulftown */
1509 case 0x2A: /* SNB */
1510 case 0x2D: /* SNB Xeon */
1511 case 0x3A: /* IVB */
1512 case 0x3E: /* IVB Xeon */
1513 case 0x3C: /* HSW */
1514 case 0x3F: /* HSX */
1515 case 0x45: /* HSW */
1516 case 0x46: /* HSW */
1517 case 0x37: /* BYT */
1518 case 0x4D: /* AVN */
1520 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1521 case 0x2F: /* Westmere-EX Xeon - Eagleton */
1526 int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
1535 case 0x3E: /* IVB Xeon */
1544 * Decode the ENERGY_PERF_BIAS MSR
1546 int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1548 unsigned long long msr;
1557 /* EPB is per-package */
1558 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1561 if (cpu_migrate(cpu)) {
1562 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1566 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
1569 switch (msr & 0x7) {
1570 case ENERGY_PERF_BIAS_PERFORMANCE:
1571 epb_string = "performance";
1573 case ENERGY_PERF_BIAS_NORMAL:
1574 epb_string = "balanced";
1576 case ENERGY_PERF_BIAS_POWERSAVE:
1577 epb_string = "powersave";
1580 epb_string = "custom";
1583 fprintf(stderr, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
1588 #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
1589 #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
1591 double get_tdp(model)
1593 unsigned long long msr;
1595 if (do_rapl & RAPL_PKG_POWER_INFO)
1596 if (!get_msr(0, MSR_PKG_POWER_INFO, &msr))
1597 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
1612 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
1614 void rapl_probe(unsigned int family, unsigned int model)
1616 unsigned long long msr;
1617 unsigned int time_unit;
1629 case 0x3C: /* HSW */
1630 case 0x45: /* HSW */
1631 case 0x46: /* HSW */
1632 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
1634 case 0x3F: /* HSX */
1635 do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
1639 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_PKG_PERF_STATUS | RAPL_DRAM_PERF_STATUS | RAPL_PKG_POWER_INFO;
1641 case 0x37: /* BYT */
1642 case 0x4D: /* AVN */
1643 do_rapl = RAPL_PKG | RAPL_CORES ;
1649 /* units on package 0, verify later other packages match */
1650 if (get_msr(0, MSR_RAPL_POWER_UNIT, &msr))
1653 rapl_power_units = 1.0 / (1 << (msr & 0xF));
1655 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
1657 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
1659 time_unit = msr >> 16 & 0xF;
1663 rapl_time_units = 1.0 / (1 << (time_unit));
1665 tdp = get_tdp(model);
1667 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
1669 fprintf(stderr, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
1674 int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1676 unsigned long long msr;
1680 if (!(do_dts || do_ptm))
1685 /* DTS is per-core, no need to print for each thread */
1686 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1689 if (cpu_migrate(cpu)) {
1690 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1694 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
1695 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1698 dts = (msr >> 16) & 0x7F;
1699 fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
1700 cpu, msr, tcc_activation_temp - dts);
1703 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
1706 dts = (msr >> 16) & 0x7F;
1707 dts2 = (msr >> 8) & 0x7F;
1708 fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
1709 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
1715 unsigned int resolution;
1717 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1720 dts = (msr >> 16) & 0x7F;
1721 resolution = (msr >> 27) & 0xF;
1722 fprintf(stderr, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
1723 cpu, msr, tcc_activation_temp - dts, resolution);
1726 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
1729 dts = (msr >> 16) & 0x7F;
1730 dts2 = (msr >> 8) & 0x7F;
1731 fprintf(stderr, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
1732 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
1739 void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
1741 fprintf(stderr, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
1743 ((msr >> 15) & 1) ? "EN" : "DIS",
1744 ((msr >> 0) & 0x7FFF) * rapl_power_units,
1745 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
1746 (((msr >> 16) & 1) ? "EN" : "DIS"));
1751 int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1753 unsigned long long msr;
1759 /* RAPL counters are per package, so print only for 1st thread/package */
1760 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1764 if (cpu_migrate(cpu)) {
1765 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1769 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
1773 fprintf(stderr, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx "
1774 "(%f Watts, %f Joules, %f sec.)\n", cpu, msr,
1775 rapl_power_units, rapl_energy_units, rapl_time_units);
1777 if (do_rapl & RAPL_PKG_POWER_INFO) {
1779 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
1783 fprintf(stderr, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
1785 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1786 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1787 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1788 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
1791 if (do_rapl & RAPL_PKG) {
1793 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
1796 fprintf(stderr, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
1797 cpu, msr, (msr >> 63) & 1 ? "": "UN");
1799 print_power_limit_msr(cpu, msr, "PKG Limit #1");
1800 fprintf(stderr, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
1802 ((msr >> 47) & 1) ? "EN" : "DIS",
1803 ((msr >> 32) & 0x7FFF) * rapl_power_units,
1804 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
1805 ((msr >> 48) & 1) ? "EN" : "DIS");
1808 if (do_rapl & RAPL_DRAM) {
1809 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
1813 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
1815 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1816 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1817 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
1818 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
1821 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
1823 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
1824 cpu, msr, (msr >> 31) & 1 ? "": "UN");
1826 print_power_limit_msr(cpu, msr, "DRAM Limit");
1828 if (do_rapl & RAPL_CORE_POLICY) {
1830 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
1833 fprintf(stderr, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
1836 if (do_rapl & RAPL_CORES) {
1839 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
1841 fprintf(stderr, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
1842 cpu, msr, (msr >> 31) & 1 ? "": "UN");
1843 print_power_limit_msr(cpu, msr, "Cores Limit");
1846 if (do_rapl & RAPL_GFX) {
1848 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
1851 fprintf(stderr, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
1853 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
1855 fprintf(stderr, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
1856 cpu, msr, (msr >> 31) & 1 ? "": "UN");
1857 print_power_limit_msr(cpu, msr, "GFX Limit");
1864 int is_snb(unsigned int family, unsigned int model)
1872 case 0x3A: /* IVB */
1873 case 0x3E: /* IVB Xeon */
1874 case 0x3C: /* HSW */
1875 case 0x3F: /* HSW */
1876 case 0x45: /* HSW */
1877 case 0x46: /* HSW */
1883 int has_c8_c9_c10(unsigned int family, unsigned int model)
1896 int is_slm(unsigned int family, unsigned int model)
1901 case 0x37: /* BYT */
1902 case 0x4D: /* AVN */
1908 #define SLM_BCLK_FREQS 5
1909 double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
1911 double slm_bclk(void)
1913 unsigned long long msr = 3;
1917 if (get_msr(0, MSR_FSB_FREQ, &msr))
1918 fprintf(stderr, "SLM BCLK: unknown\n");
1921 if (i >= SLM_BCLK_FREQS) {
1922 fprintf(stderr, "SLM BCLK[%d] invalid\n", i);
1925 freq = slm_freq_table[i];
1927 fprintf(stderr, "SLM BCLK: %.1f Mhz\n", freq);
1932 double discover_bclk(unsigned int family, unsigned int model)
1934 if (is_snb(family, model))
1936 else if (is_slm(family, model))
1943 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
1944 * the Thermal Control Circuit (TCC) activates.
1945 * This is usually equal to tjMax.
1947 * Older processors do not have this MSR, so there we guess,
1948 * but also allow cmdline over-ride with -T.
1950 * Several MSR temperature values are in units of degrees-C
1951 * below this value, including the Digital Thermal Sensor (DTS),
1952 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
1954 int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1956 unsigned long long msr;
1957 unsigned int target_c_local;
1960 /* tcc_activation_temp is used only for dts or ptm */
1961 if (!(do_dts || do_ptm))
1964 /* this is a per-package concept */
1965 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1969 if (cpu_migrate(cpu)) {
1970 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1974 if (tcc_activation_temp_override != 0) {
1975 tcc_activation_temp = tcc_activation_temp_override;
1976 fprintf(stderr, "cpu%d: Using cmdline TCC Target (%d C)\n",
1977 cpu, tcc_activation_temp);
1981 /* Temperature Target MSR is Nehalem and newer only */
1982 if (!do_nehalem_platform_info)
1985 if (get_msr(0, MSR_IA32_TEMPERATURE_TARGET, &msr))
1988 target_c_local = (msr >> 16) & 0x7F;
1991 fprintf(stderr, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
1992 cpu, msr, target_c_local);
1994 if (target_c_local < 85 || target_c_local > 127)
1997 tcc_activation_temp = target_c_local;
2002 tcc_activation_temp = TJMAX_DEFAULT;
2003 fprintf(stderr, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
2004 cpu, tcc_activation_temp);
2010 unsigned int eax, ebx, ecx, edx, max_level;
2011 unsigned int fms, family, model, stepping;
2013 eax = ebx = ecx = edx = 0;
2015 __get_cpuid(0, &max_level, &ebx, &ecx, &edx);
2017 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
2021 fprintf(stderr, "CPUID(0): %.4s%.4s%.4s ",
2022 (char *)&ebx, (char *)&edx, (char *)&ecx);
2024 __get_cpuid(1, &fms, &ebx, &ecx, &edx);
2025 family = (fms >> 8) & 0xf;
2026 model = (fms >> 4) & 0xf;
2027 stepping = fms & 0xf;
2028 if (family == 6 || family == 0xf)
2029 model += ((fms >> 16) & 0xf) << 4;
2032 fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
2033 max_level, family, model, stepping, family, model, stepping);
2035 if (!(edx & (1 << 5)))
2036 errx(1, "CPUID: no MSR");
2039 * check max extended function levels of CPUID.
2040 * This is needed to check for invariant TSC.
2041 * This check is valid for both Intel and AMD.
2043 ebx = ecx = edx = 0;
2044 __get_cpuid(0x80000000, &max_level, &ebx, &ecx, &edx);
2046 if (max_level < 0x80000007)
2047 errx(1, "CPUID: no invariant TSC (max_level 0x%x)", max_level);
2050 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
2051 * this check is valid for both Intel and AMD
2053 __get_cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
2054 has_invariant_tsc = edx & (1 << 8);
2056 if (!has_invariant_tsc)
2057 errx(1, "No invariant TSC");
2060 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
2061 * this check is valid for both Intel and AMD
2064 __get_cpuid(0x6, &eax, &ebx, &ecx, &edx);
2065 has_aperf = ecx & (1 << 0);
2066 do_dts = eax & (1 << 0);
2067 do_ptm = eax & (1 << 6);
2068 has_epb = ecx & (1 << 3);
2071 fprintf(stderr, "CPUID(6): %s%s%s%s\n",
2072 has_aperf ? "APERF" : "No APERF!",
2073 do_dts ? ", DTS" : "",
2074 do_ptm ? ", PTM": "",
2075 has_epb ? ", EPB": "");
2078 errx(-1, "No APERF");
2080 do_nehalem_platform_info = genuine_intel && has_invariant_tsc;
2081 do_nhm_cstates = genuine_intel; /* all Intel w/ non-stop TSC have NHM counters */
2082 do_smi = do_nhm_cstates;
2083 do_snb_cstates = is_snb(family, model);
2084 do_c8_c9_c10 = has_c8_c9_c10(family, model);
2085 do_slm_cstates = is_slm(family, model);
2086 bclk = discover_bclk(family, model);
2088 do_nehalem_turbo_ratio_limit = has_nehalem_turbo_ratio_limit(family, model);
2089 do_ivt_turbo_ratio_limit = has_ivt_turbo_ratio_limit(family, model);
2090 rapl_probe(family, model);
2098 errx(1, "%s: [-v][-R][-T][-p|-P|-S][-c MSR#][-C MSR#][-m MSR#][-M MSR#][-i interval_sec | command ...]\n",
2104 * in /dev/cpu/ return success for names that are numbers
2105 * ie. filter out ".", "..", "microcode".
2107 int dir_filter(const struct dirent *dirp)
2109 if (isdigit(dirp->d_name[0]))
2115 int open_dev_cpu_msr(int dummy1)
2120 void topology_probe()
2123 int max_core_id = 0;
2124 int max_package_id = 0;
2125 int max_siblings = 0;
2126 struct cpu_topology {
2128 int physical_package_id;
2131 /* Initialize num_cpus, max_cpu_num */
2133 topo.max_cpu_num = 0;
2134 for_all_proc_cpus(count_cpus);
2135 if (!summary_only && topo.num_cpus > 1)
2139 fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
2141 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
2143 err(1, "calloc cpus");
2146 * Allocate and initialize cpu_present_set
2148 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
2149 if (cpu_present_set == NULL)
2150 err(3, "CPU_ALLOC");
2151 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2152 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
2153 for_all_proc_cpus(mark_cpu_present);
2156 * Allocate and initialize cpu_affinity_set
2158 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
2159 if (cpu_affinity_set == NULL)
2160 err(3, "CPU_ALLOC");
2161 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2162 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
2167 * find max_core_id, max_package_id
2169 for (i = 0; i <= topo.max_cpu_num; ++i) {
2172 if (cpu_is_not_present(i)) {
2174 fprintf(stderr, "cpu%d NOT PRESENT\n", i);
2177 cpus[i].core_id = get_core_id(i);
2178 if (cpus[i].core_id > max_core_id)
2179 max_core_id = cpus[i].core_id;
2181 cpus[i].physical_package_id = get_physical_package_id(i);
2182 if (cpus[i].physical_package_id > max_package_id)
2183 max_package_id = cpus[i].physical_package_id;
2185 siblings = get_num_ht_siblings(i);
2186 if (siblings > max_siblings)
2187 max_siblings = siblings;
2189 fprintf(stderr, "cpu %d pkg %d core %d\n",
2190 i, cpus[i].physical_package_id, cpus[i].core_id);
2192 topo.num_cores_per_pkg = max_core_id + 1;
2194 fprintf(stderr, "max_core_id %d, sizing for %d cores per package\n",
2195 max_core_id, topo.num_cores_per_pkg);
2196 if (!summary_only && topo.num_cores_per_pkg > 1)
2199 topo.num_packages = max_package_id + 1;
2201 fprintf(stderr, "max_package_id %d, sizing for %d packages\n",
2202 max_package_id, topo.num_packages);
2203 if (!summary_only && topo.num_packages > 1)
2206 topo.num_threads_per_core = max_siblings;
2208 fprintf(stderr, "max_siblings %d\n", max_siblings);
2214 allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
2218 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
2219 topo.num_packages, sizeof(struct thread_data));
2223 for (i = 0; i < topo.num_threads_per_core *
2224 topo.num_cores_per_pkg * topo.num_packages; i++)
2225 (*t)[i].cpu_id = -1;
2227 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
2228 sizeof(struct core_data));
2232 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
2233 (*c)[i].core_id = -1;
2235 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
2239 for (i = 0; i < topo.num_packages; i++)
2240 (*p)[i].package_id = i;
2244 err(1, "calloc counters");
2249 * set cpu_id, core_num, pkg_num
2250 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
2252 * increment topo.num_cores when 1st core in pkg seen
2254 void init_counter(struct thread_data *thread_base, struct core_data *core_base,
2255 struct pkg_data *pkg_base, int thread_num, int core_num,
2256 int pkg_num, int cpu_id)
2258 struct thread_data *t;
2259 struct core_data *c;
2262 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
2263 c = GET_CORE(core_base, core_num, pkg_num);
2264 p = GET_PKG(pkg_base, pkg_num);
2267 if (thread_num == 0) {
2268 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
2269 if (cpu_is_first_core_in_package(cpu_id))
2270 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
2273 c->core_id = core_num;
2274 p->package_id = pkg_num;
2278 int initialize_counters(int cpu_id)
2280 int my_thread_id, my_core_id, my_package_id;
2282 my_package_id = get_physical_package_id(cpu_id);
2283 my_core_id = get_core_id(cpu_id);
2285 if (cpu_is_first_sibling_in_core(cpu_id)) {
2292 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2293 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2297 void allocate_output_buffer()
2299 output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
2300 outp = output_buffer;
2302 err(-1, "calloc output buffer");
2305 void setup_all_buffers(void)
2308 allocate_counters(&thread_even, &core_even, &package_even);
2309 allocate_counters(&thread_odd, &core_odd, &package_odd);
2310 allocate_output_buffer();
2311 for_all_proc_cpus(initialize_counters);
2314 void turbostat_init()
2321 setup_all_buffers();
2324 print_verbose_header();
2327 for_all_cpus(print_epb, ODD_COUNTERS);
2330 for_all_cpus(print_rapl, ODD_COUNTERS);
2332 for_all_cpus(set_temperature_target, ODD_COUNTERS);
2335 for_all_cpus(print_thermal, ODD_COUNTERS);
2338 int fork_it(char **argv)
2343 status = for_all_cpus(get_counters, EVEN_COUNTERS);
2346 /* clear affinity side-effect of get_counters() */
2347 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
2348 gettimeofday(&tv_even, (struct timezone *)NULL);
2353 execvp(argv[0], argv);
2357 if (child_pid == -1)
2360 signal(SIGINT, SIG_IGN);
2361 signal(SIGQUIT, SIG_IGN);
2362 if (waitpid(child_pid, &status, 0) == -1)
2363 err(status, "waitpid");
2366 * n.b. fork_it() does not check for errors from for_all_cpus()
2367 * because re-starting is problematic when forking
2369 for_all_cpus(get_counters, ODD_COUNTERS);
2370 gettimeofday(&tv_odd, (struct timezone *)NULL);
2371 timersub(&tv_odd, &tv_even, &tv_delta);
2372 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
2373 compute_average(EVEN_COUNTERS);
2374 format_all_counters(EVEN_COUNTERS);
2377 fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
2382 int get_and_dump_counters(void)
2386 status = for_all_cpus(get_counters, ODD_COUNTERS);
2390 status = for_all_cpus(dump_counters, ODD_COUNTERS);
2399 void cmdline(int argc, char **argv)
2405 while ((opt = getopt(argc, argv, "+pPsSvi:c:C:m:M:RJT:")) != -1) {
2423 interval_sec = atoi(optarg);
2426 sscanf(optarg, "%x", &extra_delta_offset32);
2429 sscanf(optarg, "%x", &extra_delta_offset64);
2432 sscanf(optarg, "%x", &extra_msr_offset32);
2435 sscanf(optarg, "%x", &extra_msr_offset64);
2441 tcc_activation_temp_override = atoi(optarg);
2453 int main(int argc, char **argv)
2455 cmdline(argc, argv);
2458 fprintf(stderr, "turbostat v3.6 Dec 2, 2013"
2459 " - Len Brown <lenb@kernel.org>\n");
2463 /* dump counters and exit */
2465 return get_and_dump_counters();
2468 * if any params left, it must be a command to fork
2471 return fork_it(argv + optind);