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.
24 #include INTEL_FAMILY_HEADER
29 #include <sys/types.h>
32 #include <sys/resource.h>
44 #include <linux/capability.h>
47 char *proc_stat = "/proc/stat";
50 struct timespec interval_ts = {5, 0};
54 unsigned int sums_need_wide_columns;
55 unsigned int rapl_joules;
56 unsigned int summary_only;
57 unsigned int list_header_only;
58 unsigned int dump_only;
59 unsigned int do_snb_cstates;
60 unsigned int do_knl_cstates;
61 unsigned int do_slm_cstates;
62 unsigned int use_c1_residency_msr;
63 unsigned int has_aperf;
65 unsigned int do_irtl_snb;
66 unsigned int do_irtl_hsw;
67 unsigned int units = 1000000; /* MHz etc */
68 unsigned int genuine_intel;
69 unsigned int has_invariant_tsc;
70 unsigned int do_nhm_platform_info;
71 unsigned int no_MSR_MISC_PWR_MGMT;
72 unsigned int aperf_mperf_multiplier = 1;
75 unsigned int has_base_hz;
76 double tsc_tweak = 1.0;
77 unsigned int show_pkg_only;
78 unsigned int show_core_only;
79 char *output_buffer, *outp;
83 unsigned long long gfx_cur_rc6_ms;
84 unsigned int gfx_cur_mhz;
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;
96 double discover_bclk(unsigned int family, unsigned int model);
97 unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */
98 /* IA32_HWP_REQUEST, IA32_HWP_STATUS */
99 unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */
100 unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */
101 unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */
102 unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */
103 unsigned int has_misc_feature_control;
105 #define RAPL_PKG (1 << 0)
106 /* 0x610 MSR_PKG_POWER_LIMIT */
107 /* 0x611 MSR_PKG_ENERGY_STATUS */
108 #define RAPL_PKG_PERF_STATUS (1 << 1)
109 /* 0x613 MSR_PKG_PERF_STATUS */
110 #define RAPL_PKG_POWER_INFO (1 << 2)
111 /* 0x614 MSR_PKG_POWER_INFO */
113 #define RAPL_DRAM (1 << 3)
114 /* 0x618 MSR_DRAM_POWER_LIMIT */
115 /* 0x619 MSR_DRAM_ENERGY_STATUS */
116 #define RAPL_DRAM_PERF_STATUS (1 << 4)
117 /* 0x61b MSR_DRAM_PERF_STATUS */
118 #define RAPL_DRAM_POWER_INFO (1 << 5)
119 /* 0x61c MSR_DRAM_POWER_INFO */
121 #define RAPL_CORES_POWER_LIMIT (1 << 6)
122 /* 0x638 MSR_PP0_POWER_LIMIT */
123 #define RAPL_CORE_POLICY (1 << 7)
124 /* 0x63a MSR_PP0_POLICY */
126 #define RAPL_GFX (1 << 8)
127 /* 0x640 MSR_PP1_POWER_LIMIT */
128 /* 0x641 MSR_PP1_ENERGY_STATUS */
129 /* 0x642 MSR_PP1_POLICY */
131 #define RAPL_CORES_ENERGY_STATUS (1 << 9)
132 /* 0x639 MSR_PP0_ENERGY_STATUS */
133 #define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT)
134 #define TJMAX_DEFAULT 100
136 #define MAX(a, b) ((a) > (b) ? (a) : (b))
139 * buffer size used by sscanf() for added column names
140 * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters
142 #define NAME_BYTES 20
143 #define PATH_BYTES 128
148 #define CPU_SUBSET_MAXCPUS 1024 /* need to use before probe... */
149 cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_subset;
150 size_t cpu_present_setsize, cpu_affinity_setsize, cpu_subset_size;
151 #define MAX_ADDED_COUNTERS 16
154 struct timeval tv_begin;
155 struct timeval tv_end;
156 unsigned long long tsc;
157 unsigned long long aperf;
158 unsigned long long mperf;
159 unsigned long long c1;
160 unsigned long long irq_count;
161 unsigned int smi_count;
164 #define CPU_IS_FIRST_THREAD_IN_CORE 0x2
165 #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
166 unsigned long long counter[MAX_ADDED_COUNTERS];
167 } *thread_even, *thread_odd;
170 unsigned long long c3;
171 unsigned long long c6;
172 unsigned long long c7;
173 unsigned long long mc6_us; /* duplicate as per-core for now, even though per module */
174 unsigned int core_temp_c;
175 unsigned int core_id;
176 unsigned long long counter[MAX_ADDED_COUNTERS];
177 } *core_even, *core_odd;
180 unsigned long long pc2;
181 unsigned long long pc3;
182 unsigned long long pc6;
183 unsigned long long pc7;
184 unsigned long long pc8;
185 unsigned long long pc9;
186 unsigned long long pc10;
187 unsigned long long pkg_wtd_core_c0;
188 unsigned long long pkg_any_core_c0;
189 unsigned long long pkg_any_gfxe_c0;
190 unsigned long long pkg_both_core_gfxe_c0;
191 long long gfx_rc6_ms;
192 unsigned int gfx_mhz;
193 unsigned int package_id;
194 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
195 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
196 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
197 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
198 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
199 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
200 unsigned int pkg_temp_c;
201 unsigned long long counter[MAX_ADDED_COUNTERS];
202 } *package_even, *package_odd;
204 #define ODD_COUNTERS thread_odd, core_odd, package_odd
205 #define EVEN_COUNTERS thread_even, core_even, package_even
207 #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
208 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
209 topo.num_threads_per_core + \
210 (core_no) * topo.num_threads_per_core + (thread_no))
211 #define GET_CORE(core_base, core_no, pkg_no) \
212 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
213 #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
215 enum counter_scope {SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE};
216 enum counter_type {COUNTER_ITEMS, COUNTER_CYCLES, COUNTER_SECONDS, COUNTER_USEC};
217 enum counter_format {FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT};
220 unsigned int msr_num;
221 char name[NAME_BYTES];
222 char path[PATH_BYTES];
224 enum counter_type type;
225 enum counter_format format;
226 struct msr_counter *next;
228 #define FLAGS_HIDE (1 << 0)
229 #define FLAGS_SHOW (1 << 1)
230 #define SYSFS_PERCPU (1 << 1)
233 struct sys_counters {
234 unsigned int added_thread_counters;
235 unsigned int added_core_counters;
236 unsigned int added_package_counters;
237 struct msr_counter *tp;
238 struct msr_counter *cp;
239 struct msr_counter *pp;
242 struct system_summary {
243 struct thread_data threads;
244 struct core_data cores;
245 struct pkg_data packages;
254 int num_cores_per_pkg;
255 int num_threads_per_core;
258 struct timeval tv_even, tv_odd, tv_delta;
260 int *irq_column_2_cpu; /* /proc/interrupts column numbers */
261 int *irqs_per_cpu; /* indexed by cpu_num */
263 void setup_all_buffers(void);
265 int cpu_is_not_present(int cpu)
267 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
270 * run func(thread, core, package) in topology order
271 * skip non-present cpus
274 int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
275 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
277 int retval, pkg_no, core_no, thread_no;
279 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
280 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
281 for (thread_no = 0; thread_no <
282 topo.num_threads_per_core; ++thread_no) {
283 struct thread_data *t;
287 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
289 if (cpu_is_not_present(t->cpu_id))
292 c = GET_CORE(core_base, core_no, pkg_no);
293 p = GET_PKG(pkg_base, pkg_no);
295 retval = func(t, c, p);
304 int cpu_migrate(int cpu)
306 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
307 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
308 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
313 int get_msr_fd(int cpu)
323 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
324 fd = open(pathname, O_RDONLY);
326 err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname);
333 int get_msr(int cpu, off_t offset, unsigned long long *msr)
337 retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset);
339 if (retval != sizeof *msr)
340 err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset);
346 * Each string in this array is compared in --show and --hide cmdline.
347 * Thus, strings that are proper sub-sets must follow their more specific peers.
349 struct msr_counter bic[] = {
351 { 0x0, "Time_Of_Day_Seconds" },
357 { 0x0, "SMI", "", 32, 0, FORMAT_DELTA, NULL},
399 #define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter))
400 #define BIC_USEC (1ULL << 0)
401 #define BIC_TOD (1ULL << 1)
402 #define BIC_Package (1ULL << 2)
403 #define BIC_Avg_MHz (1ULL << 3)
404 #define BIC_Bzy_MHz (1ULL << 4)
405 #define BIC_TSC_MHz (1ULL << 5)
406 #define BIC_IRQ (1ULL << 6)
407 #define BIC_SMI (1ULL << 7)
408 #define BIC_Busy (1ULL << 8)
409 #define BIC_CPU_c1 (1ULL << 9)
410 #define BIC_CPU_c3 (1ULL << 10)
411 #define BIC_CPU_c6 (1ULL << 11)
412 #define BIC_CPU_c7 (1ULL << 12)
413 #define BIC_ThreadC (1ULL << 13)
414 #define BIC_CoreTmp (1ULL << 14)
415 #define BIC_CoreCnt (1ULL << 15)
416 #define BIC_PkgTmp (1ULL << 16)
417 #define BIC_GFX_rc6 (1ULL << 17)
418 #define BIC_GFXMHz (1ULL << 18)
419 #define BIC_Pkgpc2 (1ULL << 19)
420 #define BIC_Pkgpc3 (1ULL << 20)
421 #define BIC_Pkgpc6 (1ULL << 21)
422 #define BIC_Pkgpc7 (1ULL << 22)
423 #define BIC_Pkgpc8 (1ULL << 23)
424 #define BIC_Pkgpc9 (1ULL << 24)
425 #define BIC_Pkgpc10 (1ULL << 25)
426 #define BIC_PkgWatt (1ULL << 26)
427 #define BIC_CorWatt (1ULL << 27)
428 #define BIC_GFXWatt (1ULL << 28)
429 #define BIC_PkgCnt (1ULL << 29)
430 #define BIC_RAMWatt (1ULL << 30)
431 #define BIC_PKG__ (1ULL << 31)
432 #define BIC_RAM__ (1ULL << 32)
433 #define BIC_Pkg_J (1ULL << 33)
434 #define BIC_Cor_J (1ULL << 34)
435 #define BIC_GFX_J (1ULL << 35)
436 #define BIC_RAM_J (1ULL << 36)
437 #define BIC_Core (1ULL << 37)
438 #define BIC_CPU (1ULL << 38)
439 #define BIC_Mod_c6 (1ULL << 39)
440 #define BIC_sysfs (1ULL << 40)
441 #define BIC_Totl_c0 (1ULL << 41)
442 #define BIC_Any_c0 (1ULL << 42)
443 #define BIC_GFX_c0 (1ULL << 43)
444 #define BIC_CPUGFX (1ULL << 44)
446 #define BIC_DISABLED_BY_DEFAULT (BIC_USEC | BIC_TOD)
448 unsigned long long bic_enabled = (0xFFFFFFFFFFFFFFFFULL & ~BIC_DISABLED_BY_DEFAULT);
449 unsigned long long bic_present = BIC_USEC | BIC_TOD | BIC_sysfs;
451 #define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME)
452 #define ENABLE_BIC(COUNTER_NAME) (bic_enabled |= COUNTER_NAME)
453 #define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT)
454 #define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT)
457 #define MAX_DEFERRED 16
458 char *deferred_skip_names[MAX_DEFERRED];
459 int deferred_skip_index;
462 * HIDE_LIST - hide this list of counters, show the rest [default]
463 * SHOW_LIST - show this list of counters, hide the rest
465 enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST;
470 "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
472 "Turbostat forks the specified COMMAND and prints statistics\n"
473 "when COMMAND completes.\n"
474 "If no COMMAND is specified, turbostat wakes every 5-seconds\n"
475 "to print statistics, until interrupted.\n"
476 "--add add a counter\n"
477 " eg. --add msr0x10,u64,cpu,delta,MY_TSC\n"
478 "--cpu cpu-set limit output to summary plus cpu-set:\n"
479 " {core | package | j,k,l..m,n-p }\n"
480 "--quiet skip decoding system configuration header\n"
481 "--interval sec Override default 5-second measurement interval\n"
482 "--help print this help message\n"
483 "--list list column headers only\n"
484 "--out file create or truncate \"file\" for all output\n"
485 "--version print version information\n"
487 "For more help, run \"man turbostat\"\n");
492 * for all the strings in comma separate name_list,
493 * set the approprate bit in return value.
495 unsigned long long bic_lookup(char *name_list, enum show_hide_mode mode)
498 unsigned long long retval = 0;
503 comma = strchr(name_list, ',');
508 if (!strcmp(name_list, "all"))
511 for (i = 0; i < MAX_BIC; ++i) {
512 if (!strcmp(name_list, bic[i].name)) {
513 retval |= (1ULL << i);
518 if (mode == SHOW_LIST) {
519 fprintf(stderr, "Invalid counter name: %s\n", name_list);
522 deferred_skip_names[deferred_skip_index++] = name_list;
524 fprintf(stderr, "deferred \"%s\"\n", name_list);
525 if (deferred_skip_index >= MAX_DEFERRED) {
526 fprintf(stderr, "More than max %d un-recognized --skip options '%s'\n",
527 MAX_DEFERRED, name_list);
542 void print_header(char *delim)
544 struct msr_counter *mp;
547 if (DO_BIC(BIC_USEC))
548 outp += sprintf(outp, "%susec", (printed++ ? delim : ""));
550 outp += sprintf(outp, "%sTime_Of_Day_Seconds", (printed++ ? delim : ""));
551 if (DO_BIC(BIC_Package))
552 outp += sprintf(outp, "%sPackage", (printed++ ? delim : ""));
553 if (DO_BIC(BIC_Core))
554 outp += sprintf(outp, "%sCore", (printed++ ? delim : ""));
556 outp += sprintf(outp, "%sCPU", (printed++ ? delim : ""));
557 if (DO_BIC(BIC_Avg_MHz))
558 outp += sprintf(outp, "%sAvg_MHz", (printed++ ? delim : ""));
559 if (DO_BIC(BIC_Busy))
560 outp += sprintf(outp, "%sBusy%%", (printed++ ? delim : ""));
561 if (DO_BIC(BIC_Bzy_MHz))
562 outp += sprintf(outp, "%sBzy_MHz", (printed++ ? delim : ""));
563 if (DO_BIC(BIC_TSC_MHz))
564 outp += sprintf(outp, "%sTSC_MHz", (printed++ ? delim : ""));
566 if (DO_BIC(BIC_IRQ)) {
567 if (sums_need_wide_columns)
568 outp += sprintf(outp, "%s IRQ", (printed++ ? delim : ""));
570 outp += sprintf(outp, "%sIRQ", (printed++ ? delim : ""));
574 outp += sprintf(outp, "%sSMI", (printed++ ? delim : ""));
576 for (mp = sys.tp; mp; mp = mp->next) {
578 if (mp->format == FORMAT_RAW) {
580 outp += sprintf(outp, "%s%18.18s", (printed++ ? delim : ""), mp->name);
582 outp += sprintf(outp, "%s%10.10s", (printed++ ? delim : ""), mp->name);
584 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
585 outp += sprintf(outp, "%s%8s", (printed++ ? delim : ""), mp->name);
587 outp += sprintf(outp, "%s%s", (printed++ ? delim : ""), mp->name);
591 if (DO_BIC(BIC_CPU_c1))
592 outp += sprintf(outp, "%sCPU%%c1", (printed++ ? delim : ""));
593 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates)
594 outp += sprintf(outp, "%sCPU%%c3", (printed++ ? delim : ""));
595 if (DO_BIC(BIC_CPU_c6))
596 outp += sprintf(outp, "%sCPU%%c6", (printed++ ? delim : ""));
597 if (DO_BIC(BIC_CPU_c7))
598 outp += sprintf(outp, "%sCPU%%c7", (printed++ ? delim : ""));
600 if (DO_BIC(BIC_Mod_c6))
601 outp += sprintf(outp, "%sMod%%c6", (printed++ ? delim : ""));
603 if (DO_BIC(BIC_CoreTmp))
604 outp += sprintf(outp, "%sCoreTmp", (printed++ ? delim : ""));
606 for (mp = sys.cp; mp; mp = mp->next) {
607 if (mp->format == FORMAT_RAW) {
609 outp += sprintf(outp, "%s%18.18s", delim, mp->name);
611 outp += sprintf(outp, "%s%10.10s", delim, mp->name);
613 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
614 outp += sprintf(outp, "%s%8s", delim, mp->name);
616 outp += sprintf(outp, "%s%s", delim, mp->name);
620 if (DO_BIC(BIC_PkgTmp))
621 outp += sprintf(outp, "%sPkgTmp", (printed++ ? delim : ""));
623 if (DO_BIC(BIC_GFX_rc6))
624 outp += sprintf(outp, "%sGFX%%rc6", (printed++ ? delim : ""));
626 if (DO_BIC(BIC_GFXMHz))
627 outp += sprintf(outp, "%sGFXMHz", (printed++ ? delim : ""));
629 if (DO_BIC(BIC_Totl_c0))
630 outp += sprintf(outp, "%sTotl%%C0", (printed++ ? delim : ""));
631 if (DO_BIC(BIC_Any_c0))
632 outp += sprintf(outp, "%sAny%%C0", (printed++ ? delim : ""));
633 if (DO_BIC(BIC_GFX_c0))
634 outp += sprintf(outp, "%sGFX%%C0", (printed++ ? delim : ""));
635 if (DO_BIC(BIC_CPUGFX))
636 outp += sprintf(outp, "%sCPUGFX%%", (printed++ ? delim : ""));
638 if (DO_BIC(BIC_Pkgpc2))
639 outp += sprintf(outp, "%sPkg%%pc2", (printed++ ? delim : ""));
640 if (DO_BIC(BIC_Pkgpc3))
641 outp += sprintf(outp, "%sPkg%%pc3", (printed++ ? delim : ""));
642 if (DO_BIC(BIC_Pkgpc6))
643 outp += sprintf(outp, "%sPkg%%pc6", (printed++ ? delim : ""));
644 if (DO_BIC(BIC_Pkgpc7))
645 outp += sprintf(outp, "%sPkg%%pc7", (printed++ ? delim : ""));
646 if (DO_BIC(BIC_Pkgpc8))
647 outp += sprintf(outp, "%sPkg%%pc8", (printed++ ? delim : ""));
648 if (DO_BIC(BIC_Pkgpc9))
649 outp += sprintf(outp, "%sPkg%%pc9", (printed++ ? delim : ""));
650 if (DO_BIC(BIC_Pkgpc10))
651 outp += sprintf(outp, "%sPk%%pc10", (printed++ ? delim : ""));
653 if (do_rapl && !rapl_joules) {
654 if (DO_BIC(BIC_PkgWatt))
655 outp += sprintf(outp, "%sPkgWatt", (printed++ ? delim : ""));
656 if (DO_BIC(BIC_CorWatt))
657 outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : ""));
658 if (DO_BIC(BIC_GFXWatt))
659 outp += sprintf(outp, "%sGFXWatt", (printed++ ? delim : ""));
660 if (DO_BIC(BIC_RAMWatt))
661 outp += sprintf(outp, "%sRAMWatt", (printed++ ? delim : ""));
662 if (DO_BIC(BIC_PKG__))
663 outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
664 if (DO_BIC(BIC_RAM__))
665 outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
666 } else if (do_rapl && rapl_joules) {
667 if (DO_BIC(BIC_Pkg_J))
668 outp += sprintf(outp, "%sPkg_J", (printed++ ? delim : ""));
669 if (DO_BIC(BIC_Cor_J))
670 outp += sprintf(outp, "%sCor_J", (printed++ ? delim : ""));
671 if (DO_BIC(BIC_GFX_J))
672 outp += sprintf(outp, "%sGFX_J", (printed++ ? delim : ""));
673 if (DO_BIC(BIC_RAM_J))
674 outp += sprintf(outp, "%sRAM_J", (printed++ ? delim : ""));
675 if (DO_BIC(BIC_PKG__))
676 outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
677 if (DO_BIC(BIC_RAM__))
678 outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
680 for (mp = sys.pp; mp; mp = mp->next) {
681 if (mp->format == FORMAT_RAW) {
683 outp += sprintf(outp, "%s%18.18s", delim, mp->name);
685 outp += sprintf(outp, "%s%10.10s", delim, mp->name);
687 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
688 outp += sprintf(outp, "%s%8s", delim, mp->name);
690 outp += sprintf(outp, "%s%s", delim, mp->name);
694 outp += sprintf(outp, "\n");
697 int dump_counters(struct thread_data *t, struct core_data *c,
701 struct msr_counter *mp;
703 outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
706 outp += sprintf(outp, "CPU: %d flags 0x%x\n",
707 t->cpu_id, t->flags);
708 outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
709 outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
710 outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
711 outp += sprintf(outp, "c1: %016llX\n", t->c1);
714 outp += sprintf(outp, "IRQ: %lld\n", t->irq_count);
716 outp += sprintf(outp, "SMI: %d\n", t->smi_count);
718 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
719 outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n",
720 i, mp->msr_num, t->counter[i]);
725 outp += sprintf(outp, "core: %d\n", c->core_id);
726 outp += sprintf(outp, "c3: %016llX\n", c->c3);
727 outp += sprintf(outp, "c6: %016llX\n", c->c6);
728 outp += sprintf(outp, "c7: %016llX\n", c->c7);
729 outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
731 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
732 outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n",
733 i, mp->msr_num, c->counter[i]);
735 outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us);
739 outp += sprintf(outp, "package: %d\n", p->package_id);
741 outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
742 outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
743 outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
744 outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
746 outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
747 if (DO_BIC(BIC_Pkgpc3))
748 outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
749 if (DO_BIC(BIC_Pkgpc6))
750 outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
751 if (DO_BIC(BIC_Pkgpc7))
752 outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
753 outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
754 outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
755 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
756 outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
757 outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
758 outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
759 outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
760 outp += sprintf(outp, "Throttle PKG: %0X\n",
761 p->rapl_pkg_perf_status);
762 outp += sprintf(outp, "Throttle RAM: %0X\n",
763 p->rapl_dram_perf_status);
764 outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
766 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
767 outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n",
768 i, mp->msr_num, p->counter[i]);
772 outp += sprintf(outp, "\n");
778 * column formatting convention & formats
780 int format_counters(struct thread_data *t, struct core_data *c,
783 double interval_float, tsc;
786 struct msr_counter *mp;
790 /* if showing only 1st thread in core and this isn't one, bail out */
791 if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
794 /* if showing only 1st thread in pkg and this isn't one, bail out */
795 if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
798 /*if not summary line and --cpu is used */
799 if ((t != &average.threads) &&
800 (cpu_subset && !CPU_ISSET_S(t->cpu_id, cpu_subset_size, cpu_subset)))
803 if (DO_BIC(BIC_USEC)) {
804 /* on each row, print how many usec each timestamp took to gather */
807 timersub(&t->tv_end, &t->tv_begin, &tv);
808 outp += sprintf(outp, "%5ld\t", tv.tv_sec * 1000000 + tv.tv_usec);
811 /* Time_Of_Day_Seconds: on each row, print sec.usec last timestamp taken */
813 outp += sprintf(outp, "%10ld.%06ld\t", t->tv_end.tv_sec, t->tv_end.tv_usec);
815 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
817 tsc = t->tsc * tsc_tweak;
819 /* topo columns, print blanks on 1st (average) line */
820 if (t == &average.threads) {
821 if (DO_BIC(BIC_Package))
822 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
823 if (DO_BIC(BIC_Core))
824 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
826 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
828 if (DO_BIC(BIC_Package)) {
830 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->package_id);
832 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
834 if (DO_BIC(BIC_Core)) {
836 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_id);
838 outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
841 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->cpu_id);
844 if (DO_BIC(BIC_Avg_MHz))
845 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
846 1.0 / units * t->aperf / interval_float);
848 if (DO_BIC(BIC_Busy))
849 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->mperf/tsc);
851 if (DO_BIC(BIC_Bzy_MHz)) {
853 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), base_hz / units * t->aperf / t->mperf);
855 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
856 tsc / units * t->aperf / t->mperf / interval_float);
859 if (DO_BIC(BIC_TSC_MHz))
860 outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 * t->tsc/units/interval_float);
863 if (DO_BIC(BIC_IRQ)) {
864 if (sums_need_wide_columns)
865 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->irq_count);
867 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->irq_count);
872 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->smi_count);
875 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
876 if (mp->format == FORMAT_RAW) {
878 outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) t->counter[i]);
880 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), t->counter[i]);
881 } else if (mp->format == FORMAT_DELTA) {
882 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
883 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->counter[i]);
885 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->counter[i]);
886 } else if (mp->format == FORMAT_PERCENT) {
887 if (mp->type == COUNTER_USEC)
888 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), t->counter[i]/interval_float/10000);
890 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->counter[i]/tsc);
895 if (DO_BIC(BIC_CPU_c1))
896 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->c1/tsc);
899 /* print per-core data only for 1st thread in core */
900 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
903 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates)
904 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c3/tsc);
905 if (DO_BIC(BIC_CPU_c6))
906 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c6/tsc);
907 if (DO_BIC(BIC_CPU_c7))
908 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c7/tsc);
911 if (DO_BIC(BIC_Mod_c6))
912 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->mc6_us / tsc);
914 if (DO_BIC(BIC_CoreTmp))
915 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_temp_c);
917 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
918 if (mp->format == FORMAT_RAW) {
920 outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) c->counter[i]);
922 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), c->counter[i]);
923 } else if (mp->format == FORMAT_DELTA) {
924 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
925 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), c->counter[i]);
927 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->counter[i]);
928 } else if (mp->format == FORMAT_PERCENT) {
929 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->counter[i]/tsc);
933 /* print per-package data only for 1st core in package */
934 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
938 if (DO_BIC(BIC_PkgTmp))
939 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->pkg_temp_c);
942 if (DO_BIC(BIC_GFX_rc6)) {
943 if (p->gfx_rc6_ms == -1) { /* detect GFX counter reset */
944 outp += sprintf(outp, "%s**.**", (printed++ ? delim : ""));
946 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""),
947 p->gfx_rc6_ms / 10.0 / interval_float);
952 if (DO_BIC(BIC_GFXMHz))
953 outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_mhz);
955 /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
956 if (DO_BIC(BIC_Totl_c0))
957 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_wtd_core_c0/tsc);
958 if (DO_BIC(BIC_Any_c0))
959 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_core_c0/tsc);
960 if (DO_BIC(BIC_GFX_c0))
961 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_gfxe_c0/tsc);
962 if (DO_BIC(BIC_CPUGFX))
963 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_both_core_gfxe_c0/tsc);
965 if (DO_BIC(BIC_Pkgpc2))
966 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc2/tsc);
967 if (DO_BIC(BIC_Pkgpc3))
968 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc3/tsc);
969 if (DO_BIC(BIC_Pkgpc6))
970 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc6/tsc);
971 if (DO_BIC(BIC_Pkgpc7))
972 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc7/tsc);
973 if (DO_BIC(BIC_Pkgpc8))
974 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc8/tsc);
975 if (DO_BIC(BIC_Pkgpc9))
976 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc9/tsc);
977 if (DO_BIC(BIC_Pkgpc10))
978 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc10/tsc);
981 * If measurement interval exceeds minimum RAPL Joule Counter range,
982 * indicate that results are suspect by printing "**" in fraction place.
984 if (interval_float < rapl_joule_counter_range)
989 if (DO_BIC(BIC_PkgWatt))
990 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units / interval_float);
991 if (DO_BIC(BIC_CorWatt))
992 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units / interval_float);
993 if (DO_BIC(BIC_GFXWatt))
994 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units / interval_float);
995 if (DO_BIC(BIC_RAMWatt))
996 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units / interval_float);
997 if (DO_BIC(BIC_Pkg_J))
998 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units);
999 if (DO_BIC(BIC_Cor_J))
1000 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units);
1001 if (DO_BIC(BIC_GFX_J))
1002 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units);
1003 if (DO_BIC(BIC_RAM_J))
1004 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units);
1005 if (DO_BIC(BIC_PKG__))
1006 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
1007 if (DO_BIC(BIC_RAM__))
1008 outp += sprintf(outp, fmt8, (printed++ ? delim : ""), 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
1010 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1011 if (mp->format == FORMAT_RAW) {
1012 if (mp->width == 32)
1013 outp += sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int) p->counter[i]);
1015 outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), p->counter[i]);
1016 } else if (mp->format == FORMAT_DELTA) {
1017 if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
1018 outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), p->counter[i]);
1020 outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), p->counter[i]);
1021 } else if (mp->format == FORMAT_PERCENT) {
1022 outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->counter[i]/tsc);
1027 outp += sprintf(outp, "\n");
1032 void flush_output_stdout(void)
1041 fputs(output_buffer, filep);
1044 outp = output_buffer;
1046 void flush_output_stderr(void)
1048 fputs(output_buffer, outf);
1050 outp = output_buffer;
1052 void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1056 if (!printed || !summary_only)
1059 if (topo.num_cpus > 1)
1060 format_counters(&average.threads, &average.cores,
1068 for_all_cpus(format_counters, t, c, p);
1071 #define DELTA_WRAP32(new, old) \
1075 old = 0x100000000 + new - old; \
1079 delta_package(struct pkg_data *new, struct pkg_data *old)
1082 struct msr_counter *mp;
1085 if (DO_BIC(BIC_Totl_c0))
1086 old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
1087 if (DO_BIC(BIC_Any_c0))
1088 old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
1089 if (DO_BIC(BIC_GFX_c0))
1090 old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
1091 if (DO_BIC(BIC_CPUGFX))
1092 old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0;
1094 old->pc2 = new->pc2 - old->pc2;
1095 if (DO_BIC(BIC_Pkgpc3))
1096 old->pc3 = new->pc3 - old->pc3;
1097 if (DO_BIC(BIC_Pkgpc6))
1098 old->pc6 = new->pc6 - old->pc6;
1099 if (DO_BIC(BIC_Pkgpc7))
1100 old->pc7 = new->pc7 - old->pc7;
1101 old->pc8 = new->pc8 - old->pc8;
1102 old->pc9 = new->pc9 - old->pc9;
1103 old->pc10 = new->pc10 - old->pc10;
1104 old->pkg_temp_c = new->pkg_temp_c;
1106 /* flag an error when rc6 counter resets/wraps */
1107 if (old->gfx_rc6_ms > new->gfx_rc6_ms)
1108 old->gfx_rc6_ms = -1;
1110 old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms;
1112 old->gfx_mhz = new->gfx_mhz;
1114 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
1115 DELTA_WRAP32(new->energy_cores, old->energy_cores);
1116 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
1117 DELTA_WRAP32(new->energy_dram, old->energy_dram);
1118 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
1119 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
1121 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1122 if (mp->format == FORMAT_RAW)
1123 old->counter[i] = new->counter[i];
1125 old->counter[i] = new->counter[i] - old->counter[i];
1132 delta_core(struct core_data *new, struct core_data *old)
1135 struct msr_counter *mp;
1137 old->c3 = new->c3 - old->c3;
1138 old->c6 = new->c6 - old->c6;
1139 old->c7 = new->c7 - old->c7;
1140 old->core_temp_c = new->core_temp_c;
1141 old->mc6_us = new->mc6_us - old->mc6_us;
1143 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1144 if (mp->format == FORMAT_RAW)
1145 old->counter[i] = new->counter[i];
1147 old->counter[i] = new->counter[i] - old->counter[i];
1155 delta_thread(struct thread_data *new, struct thread_data *old,
1156 struct core_data *core_delta)
1159 struct msr_counter *mp;
1162 * the timestamps from start of measurement interval are in "old"
1163 * the timestamp from end of measurement interval are in "new"
1164 * over-write old w/ new so we can print end of interval values
1167 old->tv_begin = new->tv_begin;
1168 old->tv_end = new->tv_end;
1170 old->tsc = new->tsc - old->tsc;
1172 /* check for TSC < 1 Mcycles over interval */
1173 if (old->tsc < (1000 * 1000))
1174 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
1175 "You can disable all c-states by booting with \"idle=poll\"\n"
1176 "or just the deep ones with \"processor.max_cstate=1\"");
1178 old->c1 = new->c1 - old->c1;
1180 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
1181 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
1182 old->aperf = new->aperf - old->aperf;
1183 old->mperf = new->mperf - old->mperf;
1190 if (use_c1_residency_msr) {
1192 * Some models have a dedicated C1 residency MSR,
1193 * which should be more accurate than the derivation below.
1197 * As counter collection is not atomic,
1198 * it is possible for mperf's non-halted cycles + idle states
1199 * to exceed TSC's all cycles: show c1 = 0% in that case.
1201 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > (old->tsc * tsc_tweak))
1204 /* normal case, derive c1 */
1205 old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3
1206 - core_delta->c6 - core_delta->c7;
1210 if (old->mperf == 0) {
1212 fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
1213 old->mperf = 1; /* divide by 0 protection */
1216 if (DO_BIC(BIC_IRQ))
1217 old->irq_count = new->irq_count - old->irq_count;
1219 if (DO_BIC(BIC_SMI))
1220 old->smi_count = new->smi_count - old->smi_count;
1222 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1223 if (mp->format == FORMAT_RAW)
1224 old->counter[i] = new->counter[i];
1226 old->counter[i] = new->counter[i] - old->counter[i];
1231 int delta_cpu(struct thread_data *t, struct core_data *c,
1232 struct pkg_data *p, struct thread_data *t2,
1233 struct core_data *c2, struct pkg_data *p2)
1237 /* calculate core delta only for 1st thread in core */
1238 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
1241 /* always calculate thread delta */
1242 retval = delta_thread(t, t2, c2); /* c2 is core delta */
1246 /* calculate package delta only for 1st core in package */
1247 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
1248 retval = delta_package(p, p2);
1253 void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1256 struct msr_counter *mp;
1258 t->tv_begin.tv_sec = 0;
1259 t->tv_begin.tv_usec = 0;
1260 t->tv_end.tv_sec = 0;
1261 t->tv_end.tv_usec = 0;
1271 /* tells format_counters to dump all fields from this set */
1272 t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
1280 p->pkg_wtd_core_c0 = 0;
1281 p->pkg_any_core_c0 = 0;
1282 p->pkg_any_gfxe_c0 = 0;
1283 p->pkg_both_core_gfxe_c0 = 0;
1286 if (DO_BIC(BIC_Pkgpc3))
1288 if (DO_BIC(BIC_Pkgpc6))
1290 if (DO_BIC(BIC_Pkgpc7))
1298 p->energy_cores = 0;
1300 p->rapl_pkg_perf_status = 0;
1301 p->rapl_dram_perf_status = 0;
1306 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next)
1309 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next)
1312 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next)
1315 int sum_counters(struct thread_data *t, struct core_data *c,
1319 struct msr_counter *mp;
1321 /* remember first tv_begin */
1322 if (average.threads.tv_begin.tv_sec == 0)
1323 average.threads.tv_begin = t->tv_begin;
1325 /* remember last tv_end */
1326 average.threads.tv_end = t->tv_end;
1328 average.threads.tsc += t->tsc;
1329 average.threads.aperf += t->aperf;
1330 average.threads.mperf += t->mperf;
1331 average.threads.c1 += t->c1;
1333 average.threads.irq_count += t->irq_count;
1334 average.threads.smi_count += t->smi_count;
1336 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1337 if (mp->format == FORMAT_RAW)
1339 average.threads.counter[i] += t->counter[i];
1342 /* sum per-core values only for 1st thread in core */
1343 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1346 average.cores.c3 += c->c3;
1347 average.cores.c6 += c->c6;
1348 average.cores.c7 += c->c7;
1349 average.cores.mc6_us += c->mc6_us;
1351 average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
1353 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1354 if (mp->format == FORMAT_RAW)
1356 average.cores.counter[i] += c->counter[i];
1359 /* sum per-pkg values only for 1st core in pkg */
1360 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1363 if (DO_BIC(BIC_Totl_c0))
1364 average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
1365 if (DO_BIC(BIC_Any_c0))
1366 average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
1367 if (DO_BIC(BIC_GFX_c0))
1368 average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
1369 if (DO_BIC(BIC_CPUGFX))
1370 average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
1372 average.packages.pc2 += p->pc2;
1373 if (DO_BIC(BIC_Pkgpc3))
1374 average.packages.pc3 += p->pc3;
1375 if (DO_BIC(BIC_Pkgpc6))
1376 average.packages.pc6 += p->pc6;
1377 if (DO_BIC(BIC_Pkgpc7))
1378 average.packages.pc7 += p->pc7;
1379 average.packages.pc8 += p->pc8;
1380 average.packages.pc9 += p->pc9;
1381 average.packages.pc10 += p->pc10;
1383 average.packages.energy_pkg += p->energy_pkg;
1384 average.packages.energy_dram += p->energy_dram;
1385 average.packages.energy_cores += p->energy_cores;
1386 average.packages.energy_gfx += p->energy_gfx;
1388 average.packages.gfx_rc6_ms = p->gfx_rc6_ms;
1389 average.packages.gfx_mhz = p->gfx_mhz;
1391 average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
1393 average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
1394 average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
1396 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1397 if (mp->format == FORMAT_RAW)
1399 average.packages.counter[i] += p->counter[i];
1404 * sum the counters for all cpus in the system
1405 * compute the weighted average
1407 void compute_average(struct thread_data *t, struct core_data *c,
1411 struct msr_counter *mp;
1413 clear_counters(&average.threads, &average.cores, &average.packages);
1415 for_all_cpus(sum_counters, t, c, p);
1417 average.threads.tsc /= topo.num_cpus;
1418 average.threads.aperf /= topo.num_cpus;
1419 average.threads.mperf /= topo.num_cpus;
1420 average.threads.c1 /= topo.num_cpus;
1422 if (average.threads.irq_count > 9999999)
1423 sums_need_wide_columns = 1;
1425 average.cores.c3 /= topo.num_cores;
1426 average.cores.c6 /= topo.num_cores;
1427 average.cores.c7 /= topo.num_cores;
1428 average.cores.mc6_us /= topo.num_cores;
1430 if (DO_BIC(BIC_Totl_c0))
1431 average.packages.pkg_wtd_core_c0 /= topo.num_packages;
1432 if (DO_BIC(BIC_Any_c0))
1433 average.packages.pkg_any_core_c0 /= topo.num_packages;
1434 if (DO_BIC(BIC_GFX_c0))
1435 average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
1436 if (DO_BIC(BIC_CPUGFX))
1437 average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
1439 average.packages.pc2 /= topo.num_packages;
1440 if (DO_BIC(BIC_Pkgpc3))
1441 average.packages.pc3 /= topo.num_packages;
1442 if (DO_BIC(BIC_Pkgpc6))
1443 average.packages.pc6 /= topo.num_packages;
1444 if (DO_BIC(BIC_Pkgpc7))
1445 average.packages.pc7 /= topo.num_packages;
1447 average.packages.pc8 /= topo.num_packages;
1448 average.packages.pc9 /= topo.num_packages;
1449 average.packages.pc10 /= topo.num_packages;
1451 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1452 if (mp->format == FORMAT_RAW)
1454 if (mp->type == COUNTER_ITEMS) {
1455 if (average.threads.counter[i] > 9999999)
1456 sums_need_wide_columns = 1;
1459 average.threads.counter[i] /= topo.num_cpus;
1461 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1462 if (mp->format == FORMAT_RAW)
1464 if (mp->type == COUNTER_ITEMS) {
1465 if (average.cores.counter[i] > 9999999)
1466 sums_need_wide_columns = 1;
1468 average.cores.counter[i] /= topo.num_cores;
1470 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1471 if (mp->format == FORMAT_RAW)
1473 if (mp->type == COUNTER_ITEMS) {
1474 if (average.packages.counter[i] > 9999999)
1475 sums_need_wide_columns = 1;
1477 average.packages.counter[i] /= topo.num_packages;
1481 static unsigned long long rdtsc(void)
1483 unsigned int low, high;
1485 asm volatile("rdtsc" : "=a" (low), "=d" (high));
1487 return low | ((unsigned long long)high) << 32;
1491 * Open a file, and exit on failure
1493 FILE *fopen_or_die(const char *path, const char *mode)
1495 FILE *filep = fopen(path, mode);
1498 err(1, "%s: open failed", path);
1502 * snapshot_sysfs_counter()
1504 * return snapshot of given counter
1506 unsigned long long snapshot_sysfs_counter(char *path)
1510 unsigned long long counter;
1512 fp = fopen_or_die(path, "r");
1514 retval = fscanf(fp, "%lld", &counter);
1516 err(1, "snapshot_sysfs_counter(%s)", path);
1523 int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp)
1525 if (mp->msr_num != 0) {
1526 if (get_msr(cpu, mp->msr_num, counterp))
1531 if (mp->flags & SYSFS_PERCPU) {
1532 sprintf(path, "/sys/devices/system/cpu/cpu%d/%s",
1535 *counterp = snapshot_sysfs_counter(path);
1537 *counterp = snapshot_sysfs_counter(mp->path);
1547 * acquire and record local counters for that cpu
1549 int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1551 int cpu = t->cpu_id;
1552 unsigned long long msr;
1553 int aperf_mperf_retry_count = 0;
1554 struct msr_counter *mp;
1558 gettimeofday(&t->tv_begin, (struct timezone *)NULL);
1560 if (cpu_migrate(cpu)) {
1561 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
1566 t->tsc = rdtsc(); /* we are running on local CPU of interest */
1568 if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
1569 unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time;
1572 * The TSC, APERF and MPERF must be read together for
1573 * APERF/MPERF and MPERF/TSC to give accurate results.
1575 * Unfortunately, APERF and MPERF are read by
1576 * individual system call, so delays may occur
1577 * between them. If the time to read them
1578 * varies by a large amount, we re-read them.
1582 * This initial dummy APERF read has been seen to
1583 * reduce jitter in the subsequent reads.
1586 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
1589 t->tsc = rdtsc(); /* re-read close to APERF */
1591 tsc_before = t->tsc;
1593 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
1596 tsc_between = rdtsc();
1598 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
1601 tsc_after = rdtsc();
1603 aperf_time = tsc_between - tsc_before;
1604 mperf_time = tsc_after - tsc_between;
1607 * If the system call latency to read APERF and MPERF
1608 * differ by more than 2x, then try again.
1610 if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) {
1611 aperf_mperf_retry_count++;
1612 if (aperf_mperf_retry_count < 5)
1615 warnx("cpu%d jitter %lld %lld",
1616 cpu, aperf_time, mperf_time);
1618 aperf_mperf_retry_count = 0;
1620 t->aperf = t->aperf * aperf_mperf_multiplier;
1621 t->mperf = t->mperf * aperf_mperf_multiplier;
1624 if (DO_BIC(BIC_IRQ))
1625 t->irq_count = irqs_per_cpu[cpu];
1626 if (DO_BIC(BIC_SMI)) {
1627 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
1629 t->smi_count = msr & 0xFFFFFFFF;
1631 if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) {
1632 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1636 for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1637 if (get_mp(cpu, mp, &t->counter[i]))
1641 /* collect core counters only for 1st thread in core */
1642 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1645 if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates) {
1646 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1650 if (DO_BIC(BIC_CPU_c6) && !do_knl_cstates) {
1651 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1653 } else if (do_knl_cstates) {
1654 if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1658 if (DO_BIC(BIC_CPU_c7))
1659 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1662 if (DO_BIC(BIC_Mod_c6))
1663 if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us))
1666 if (DO_BIC(BIC_CoreTmp)) {
1667 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1669 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1672 for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1673 if (get_mp(cpu, mp, &c->counter[i]))
1677 /* collect package counters only for 1st core in package */
1678 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1681 if (DO_BIC(BIC_Totl_c0)) {
1682 if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
1685 if (DO_BIC(BIC_Any_c0)) {
1686 if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
1689 if (DO_BIC(BIC_GFX_c0)) {
1690 if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
1693 if (DO_BIC(BIC_CPUGFX)) {
1694 if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
1697 if (DO_BIC(BIC_Pkgpc3))
1698 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1700 if (DO_BIC(BIC_Pkgpc6)) {
1701 if (do_slm_cstates) {
1702 if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6))
1705 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1710 if (DO_BIC(BIC_Pkgpc2))
1711 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1713 if (DO_BIC(BIC_Pkgpc7))
1714 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1716 if (DO_BIC(BIC_Pkgpc8))
1717 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1719 if (DO_BIC(BIC_Pkgpc9))
1720 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1722 if (DO_BIC(BIC_Pkgpc10))
1723 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1726 if (do_rapl & RAPL_PKG) {
1727 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1729 p->energy_pkg = msr & 0xFFFFFFFF;
1731 if (do_rapl & RAPL_CORES_ENERGY_STATUS) {
1732 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1734 p->energy_cores = msr & 0xFFFFFFFF;
1736 if (do_rapl & RAPL_DRAM) {
1737 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1739 p->energy_dram = msr & 0xFFFFFFFF;
1741 if (do_rapl & RAPL_GFX) {
1742 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1744 p->energy_gfx = msr & 0xFFFFFFFF;
1746 if (do_rapl & RAPL_PKG_PERF_STATUS) {
1747 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1749 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1751 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1752 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1754 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1756 if (DO_BIC(BIC_PkgTmp)) {
1757 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1759 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1762 if (DO_BIC(BIC_GFX_rc6))
1763 p->gfx_rc6_ms = gfx_cur_rc6_ms;
1765 if (DO_BIC(BIC_GFXMHz))
1766 p->gfx_mhz = gfx_cur_mhz;
1768 for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1769 if (get_mp(cpu, mp, &p->counter[i]))
1773 gettimeofday(&t->tv_end, (struct timezone *)NULL);
1779 * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1780 * If you change the values, note they are used both in comparisons
1781 * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1784 #define PCLUKN 0 /* Unknown */
1785 #define PCLRSV 1 /* Reserved */
1786 #define PCL__0 2 /* PC0 */
1787 #define PCL__1 3 /* PC1 */
1788 #define PCL__2 4 /* PC2 */
1789 #define PCL__3 5 /* PC3 */
1790 #define PCL__4 6 /* PC4 */
1791 #define PCL__6 7 /* PC6 */
1792 #define PCL_6N 8 /* PC6 No Retention */
1793 #define PCL_6R 9 /* PC6 Retention */
1794 #define PCL__7 10 /* PC7 */
1795 #define PCL_7S 11 /* PC7 Shrink */
1796 #define PCL__8 12 /* PC8 */
1797 #define PCL__9 13 /* PC9 */
1798 #define PCLUNL 14 /* Unlimited */
1800 int pkg_cstate_limit = PCLUKN;
1801 char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
1802 "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"};
1804 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};
1805 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};
1806 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};
1807 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, PCL__6, PCL__7};
1808 int amt_pkg_cstate_limits[16] = {PCLUNL, PCL__1, PCL__2, PCLRSV, PCLRSV, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1809 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};
1810 int bxt_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1811 int skx_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1815 calculate_tsc_tweak()
1817 tsc_tweak = base_hz / tsc_hz;
1821 dump_nhm_platform_info(void)
1823 unsigned long long msr;
1826 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
1828 fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
1830 ratio = (msr >> 40) & 0xFF;
1831 fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n",
1832 ratio, bclk, ratio * bclk);
1834 ratio = (msr >> 8) & 0xFF;
1835 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
1836 ratio, bclk, ratio * bclk);
1838 get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
1839 fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
1840 base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
1846 dump_hsw_turbo_ratio_limits(void)
1848 unsigned long long msr;
1851 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
1853 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
1855 ratio = (msr >> 8) & 0xFF;
1857 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n",
1858 ratio, bclk, ratio * bclk);
1860 ratio = (msr >> 0) & 0xFF;
1862 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n",
1863 ratio, bclk, ratio * bclk);
1868 dump_ivt_turbo_ratio_limits(void)
1870 unsigned long long msr;
1873 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
1875 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
1877 ratio = (msr >> 56) & 0xFF;
1879 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n",
1880 ratio, bclk, ratio * bclk);
1882 ratio = (msr >> 48) & 0xFF;
1884 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n",
1885 ratio, bclk, ratio * bclk);
1887 ratio = (msr >> 40) & 0xFF;
1889 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n",
1890 ratio, bclk, ratio * bclk);
1892 ratio = (msr >> 32) & 0xFF;
1894 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n",
1895 ratio, bclk, ratio * bclk);
1897 ratio = (msr >> 24) & 0xFF;
1899 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n",
1900 ratio, bclk, ratio * bclk);
1902 ratio = (msr >> 16) & 0xFF;
1904 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n",
1905 ratio, bclk, ratio * bclk);
1907 ratio = (msr >> 8) & 0xFF;
1909 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n",
1910 ratio, bclk, ratio * bclk);
1912 ratio = (msr >> 0) & 0xFF;
1914 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n",
1915 ratio, bclk, ratio * bclk);
1918 int has_turbo_ratio_group_limits(int family, int model)
1925 case INTEL_FAM6_ATOM_GOLDMONT:
1926 case INTEL_FAM6_SKYLAKE_X:
1927 case INTEL_FAM6_ATOM_DENVERTON:
1934 dump_turbo_ratio_limits(int family, int model)
1936 unsigned long long msr, core_counts;
1937 unsigned int ratio, group_size;
1939 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
1940 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
1942 if (has_turbo_ratio_group_limits(family, model)) {
1943 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts);
1944 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts);
1946 core_counts = 0x0807060504030201;
1949 ratio = (msr >> 56) & 0xFF;
1950 group_size = (core_counts >> 56) & 0xFF;
1952 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1953 ratio, bclk, ratio * bclk, group_size);
1955 ratio = (msr >> 48) & 0xFF;
1956 group_size = (core_counts >> 48) & 0xFF;
1958 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1959 ratio, bclk, ratio * bclk, group_size);
1961 ratio = (msr >> 40) & 0xFF;
1962 group_size = (core_counts >> 40) & 0xFF;
1964 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1965 ratio, bclk, ratio * bclk, group_size);
1967 ratio = (msr >> 32) & 0xFF;
1968 group_size = (core_counts >> 32) & 0xFF;
1970 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1971 ratio, bclk, ratio * bclk, group_size);
1973 ratio = (msr >> 24) & 0xFF;
1974 group_size = (core_counts >> 24) & 0xFF;
1976 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1977 ratio, bclk, ratio * bclk, group_size);
1979 ratio = (msr >> 16) & 0xFF;
1980 group_size = (core_counts >> 16) & 0xFF;
1982 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1983 ratio, bclk, ratio * bclk, group_size);
1985 ratio = (msr >> 8) & 0xFF;
1986 group_size = (core_counts >> 8) & 0xFF;
1988 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1989 ratio, bclk, ratio * bclk, group_size);
1991 ratio = (msr >> 0) & 0xFF;
1992 group_size = (core_counts >> 0) & 0xFF;
1994 fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
1995 ratio, bclk, ratio * bclk, group_size);
2000 dump_atom_turbo_ratio_limits(void)
2002 unsigned long long msr;
2005 get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr);
2006 fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
2008 ratio = (msr >> 0) & 0x3F;
2010 fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n",
2011 ratio, bclk, ratio * bclk);
2013 ratio = (msr >> 8) & 0x3F;
2015 fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n",
2016 ratio, bclk, ratio * bclk);
2018 ratio = (msr >> 16) & 0x3F;
2020 fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
2021 ratio, bclk, ratio * bclk);
2023 get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr);
2024 fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
2026 ratio = (msr >> 24) & 0x3F;
2028 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n",
2029 ratio, bclk, ratio * bclk);
2031 ratio = (msr >> 16) & 0x3F;
2033 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n",
2034 ratio, bclk, ratio * bclk);
2036 ratio = (msr >> 8) & 0x3F;
2038 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n",
2039 ratio, bclk, ratio * bclk);
2041 ratio = (msr >> 0) & 0x3F;
2043 fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n",
2044 ratio, bclk, ratio * bclk);
2048 dump_knl_turbo_ratio_limits(void)
2050 const unsigned int buckets_no = 7;
2052 unsigned long long msr;
2053 int delta_cores, delta_ratio;
2055 unsigned int cores[buckets_no];
2056 unsigned int ratio[buckets_no];
2058 get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
2060 fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n",
2064 * Turbo encoding in KNL is as follows:
2066 * [7:1] -- Base value of number of active cores of bucket 1.
2067 * [15:8] -- Base value of freq ratio of bucket 1.
2068 * [20:16] -- +ve delta of number of active cores of bucket 2.
2069 * i.e. active cores of bucket 2 =
2070 * active cores of bucket 1 + delta
2071 * [23:21] -- Negative delta of freq ratio of bucket 2.
2072 * i.e. freq ratio of bucket 2 =
2073 * freq ratio of bucket 1 - delta
2074 * [28:24]-- +ve delta of number of active cores of bucket 3.
2075 * [31:29]-- -ve delta of freq ratio of bucket 3.
2076 * [36:32]-- +ve delta of number of active cores of bucket 4.
2077 * [39:37]-- -ve delta of freq ratio of bucket 4.
2078 * [44:40]-- +ve delta of number of active cores of bucket 5.
2079 * [47:45]-- -ve delta of freq ratio of bucket 5.
2080 * [52:48]-- +ve delta of number of active cores of bucket 6.
2081 * [55:53]-- -ve delta of freq ratio of bucket 6.
2082 * [60:56]-- +ve delta of number of active cores of bucket 7.
2083 * [63:61]-- -ve delta of freq ratio of bucket 7.
2087 cores[b_nr] = (msr & 0xFF) >> 1;
2088 ratio[b_nr] = (msr >> 8) & 0xFF;
2090 for (i = 16; i < 64; i += 8) {
2091 delta_cores = (msr >> i) & 0x1F;
2092 delta_ratio = (msr >> (i + 5)) & 0x7;
2094 cores[b_nr + 1] = cores[b_nr] + delta_cores;
2095 ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
2099 for (i = buckets_no - 1; i >= 0; i--)
2100 if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
2102 "%d * %.1f = %.1f MHz max turbo %d active cores\n",
2103 ratio[i], bclk, ratio[i] * bclk, cores[i]);
2107 dump_nhm_cst_cfg(void)
2109 unsigned long long msr;
2111 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
2113 #define SNB_C1_AUTO_UNDEMOTE (1UL << 27)
2114 #define SNB_C3_AUTO_UNDEMOTE (1UL << 28)
2116 fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr);
2118 fprintf(outf, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: %s)\n",
2119 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
2120 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
2121 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
2122 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
2123 (msr & (1 << 15)) ? "" : "UN",
2124 (unsigned int)msr & 0xF,
2125 pkg_cstate_limit_strings[pkg_cstate_limit]);
2130 dump_config_tdp(void)
2132 unsigned long long msr;
2134 get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
2135 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
2136 fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF);
2138 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
2139 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
2141 fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2142 fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2143 fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2144 fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF);
2146 fprintf(outf, ")\n");
2148 get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
2149 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
2151 fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2152 fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2153 fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2154 fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF);
2156 fprintf(outf, ")\n");
2158 get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
2159 fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
2161 fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
2162 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2163 fprintf(outf, ")\n");
2165 get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
2166 fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
2167 fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF);
2168 fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2169 fprintf(outf, ")\n");
2172 unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
2174 void print_irtl(void)
2176 unsigned long long msr;
2178 get_msr(base_cpu, MSR_PKGC3_IRTL, &msr);
2179 fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr);
2180 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2181 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2183 get_msr(base_cpu, MSR_PKGC6_IRTL, &msr);
2184 fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr);
2185 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2186 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2188 get_msr(base_cpu, MSR_PKGC7_IRTL, &msr);
2189 fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr);
2190 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2191 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2196 get_msr(base_cpu, MSR_PKGC8_IRTL, &msr);
2197 fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr);
2198 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2199 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2201 get_msr(base_cpu, MSR_PKGC9_IRTL, &msr);
2202 fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr);
2203 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2204 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2206 get_msr(base_cpu, MSR_PKGC10_IRTL, &msr);
2207 fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr);
2208 fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
2209 (msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
2212 void free_fd_percpu(void)
2216 for (i = 0; i < topo.max_cpu_num + 1; ++i) {
2217 if (fd_percpu[i] != 0)
2218 close(fd_percpu[i]);
2224 void free_all_buffers(void)
2226 CPU_FREE(cpu_present_set);
2227 cpu_present_set = NULL;
2228 cpu_present_setsize = 0;
2230 CPU_FREE(cpu_affinity_set);
2231 cpu_affinity_set = NULL;
2232 cpu_affinity_setsize = 0;
2240 package_even = NULL;
2250 free(output_buffer);
2251 output_buffer = NULL;
2256 free(irq_column_2_cpu);
2262 * Parse a file containing a single int.
2264 int parse_int_file(const char *fmt, ...)
2267 char path[PATH_MAX];
2271 va_start(args, fmt);
2272 vsnprintf(path, sizeof(path), fmt, args);
2274 filep = fopen_or_die(path, "r");
2275 if (fscanf(filep, "%d", &value) != 1)
2276 err(1, "%s: failed to parse number from file", path);
2282 * get_cpu_position_in_core(cpu)
2283 * return the position of the CPU among its HT siblings in the core
2284 * return -1 if the sibling is not in list
2286 int get_cpu_position_in_core(int cpu)
2295 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list",
2297 filep = fopen(path, "r");
2298 if (filep == NULL) {
2303 for (i = 0; i < topo.num_threads_per_core; i++) {
2304 fscanf(filep, "%d", &this_cpu);
2305 if (this_cpu == cpu) {
2310 /* Account for no separator after last thread*/
2311 if (i != (topo.num_threads_per_core - 1))
2312 fscanf(filep, "%c", &character);
2320 * cpu_is_first_core_in_package(cpu)
2321 * return 1 if given CPU is 1st core in package
2323 int cpu_is_first_core_in_package(int cpu)
2325 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
2328 int get_physical_package_id(int cpu)
2330 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
2333 int get_core_id(int cpu)
2335 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
2338 int get_num_ht_siblings(int cpu)
2348 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
2349 filep = fopen_or_die(path, "r");
2353 * A ',' separated or '-' separated set of numbers
2354 * (eg 1-2 or 1,3,4,5)
2356 fscanf(filep, "%d%c\n", &sib1, &character);
2357 fseek(filep, 0, SEEK_SET);
2358 fgets(str, 100, filep);
2359 ch = strchr(str, character);
2360 while (ch != NULL) {
2362 ch = strchr(ch+1, character);
2370 * run func(thread, core, package) in topology order
2371 * skip non-present cpus
2374 int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
2375 struct pkg_data *, struct thread_data *, struct core_data *,
2376 struct pkg_data *), struct thread_data *thread_base,
2377 struct core_data *core_base, struct pkg_data *pkg_base,
2378 struct thread_data *thread_base2, struct core_data *core_base2,
2379 struct pkg_data *pkg_base2)
2381 int retval, pkg_no, core_no, thread_no;
2383 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
2384 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
2385 for (thread_no = 0; thread_no <
2386 topo.num_threads_per_core; ++thread_no) {
2387 struct thread_data *t, *t2;
2388 struct core_data *c, *c2;
2389 struct pkg_data *p, *p2;
2391 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
2393 if (cpu_is_not_present(t->cpu_id))
2396 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
2398 c = GET_CORE(core_base, core_no, pkg_no);
2399 c2 = GET_CORE(core_base2, core_no, pkg_no);
2401 p = GET_PKG(pkg_base, pkg_no);
2402 p2 = GET_PKG(pkg_base2, pkg_no);
2404 retval = func(t, c, p, t2, c2, p2);
2414 * run func(cpu) on every cpu in /proc/stat
2415 * return max_cpu number
2417 int for_all_proc_cpus(int (func)(int))
2423 fp = fopen_or_die(proc_stat, "r");
2425 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
2427 err(1, "%s: failed to parse format", proc_stat);
2430 retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
2434 retval = func(cpu_num);
2444 void re_initialize(void)
2447 setup_all_buffers();
2448 printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
2454 * remember the last one seen, it will be the max
2456 int count_cpus(int cpu)
2458 if (topo.max_cpu_num < cpu)
2459 topo.max_cpu_num = cpu;
2464 int mark_cpu_present(int cpu)
2466 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
2471 * snapshot_proc_interrupts()
2473 * read and record summary of /proc/interrupts
2475 * return 1 if config change requires a restart, else return 0
2477 int snapshot_proc_interrupts(void)
2483 fp = fopen_or_die("/proc/interrupts", "r");
2487 /* read 1st line of /proc/interrupts to get cpu* name for each column */
2488 for (column = 0; column < topo.num_cpus; ++column) {
2491 retval = fscanf(fp, " CPU%d", &cpu_number);
2495 if (cpu_number > topo.max_cpu_num) {
2496 warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num);
2500 irq_column_2_cpu[column] = cpu_number;
2501 irqs_per_cpu[cpu_number] = 0;
2504 /* read /proc/interrupt count lines and sum up irqs per cpu */
2509 retval = fscanf(fp, " %s:", buf); /* flush irq# "N:" */
2513 /* read the count per cpu */
2514 for (column = 0; column < topo.num_cpus; ++column) {
2516 int cpu_number, irq_count;
2518 retval = fscanf(fp, " %d", &irq_count);
2522 cpu_number = irq_column_2_cpu[column];
2523 irqs_per_cpu[cpu_number] += irq_count;
2527 while (getc(fp) != '\n')
2528 ; /* flush interrupt description */
2534 * snapshot_gfx_rc6_ms()
2536 * record snapshot of
2537 * /sys/class/drm/card0/power/rc6_residency_ms
2539 * return 1 if config change requires a restart, else return 0
2541 int snapshot_gfx_rc6_ms(void)
2546 fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r");
2548 retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms);
2557 * snapshot_gfx_mhz()
2559 * record snapshot of
2560 * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz
2562 * return 1 if config change requires a restart, else return 0
2564 int snapshot_gfx_mhz(void)
2570 fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r");
2576 retval = fscanf(fp, "%d", &gfx_cur_mhz);
2584 * snapshot /proc and /sys files
2586 * return 1 if configuration restart needed, else return 0
2588 int snapshot_proc_sysfs_files(void)
2590 if (DO_BIC(BIC_IRQ))
2591 if (snapshot_proc_interrupts())
2594 if (DO_BIC(BIC_GFX_rc6))
2595 snapshot_gfx_rc6_ms();
2597 if (DO_BIC(BIC_GFXMHz))
2605 static void signal_handler (int signal)
2611 fprintf(stderr, " SIGINT\n");
2615 fprintf(stderr, "SIGUSR1\n");
2620 void setup_signal_handler(void)
2622 struct sigaction sa;
2624 memset(&sa, 0, sizeof(sa));
2626 sa.sa_handler = &signal_handler;
2628 if (sigaction(SIGINT, &sa, NULL) < 0)
2629 err(1, "sigaction SIGINT");
2630 if (sigaction(SIGUSR1, &sa, NULL) < 0)
2631 err(1, "sigaction SIGUSR1");
2633 void turbostat_loop()
2638 setup_signal_handler();
2643 snapshot_proc_sysfs_files();
2644 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
2647 } else if (retval == -1) {
2648 if (restarted > 1) {
2655 gettimeofday(&tv_even, (struct timezone *)NULL);
2658 if (for_all_proc_cpus(cpu_is_not_present)) {
2662 nanosleep(&interval_ts, NULL);
2663 if (snapshot_proc_sysfs_files())
2665 retval = for_all_cpus(get_counters, ODD_COUNTERS);
2668 } else if (retval == -1) {
2672 gettimeofday(&tv_odd, (struct timezone *)NULL);
2673 timersub(&tv_odd, &tv_even, &tv_delta);
2674 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) {
2678 compute_average(EVEN_COUNTERS);
2679 format_all_counters(EVEN_COUNTERS);
2680 flush_output_stdout();
2683 nanosleep(&interval_ts, NULL);
2684 if (snapshot_proc_sysfs_files())
2686 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
2689 } else if (retval == -1) {
2693 gettimeofday(&tv_even, (struct timezone *)NULL);
2694 timersub(&tv_even, &tv_odd, &tv_delta);
2695 if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) {
2699 compute_average(ODD_COUNTERS);
2700 format_all_counters(ODD_COUNTERS);
2701 flush_output_stdout();
2707 void check_dev_msr()
2712 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2713 if (stat(pathname, &sb))
2714 if (system("/sbin/modprobe msr > /dev/null 2>&1"))
2715 err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
2718 void check_permissions()
2720 struct __user_cap_header_struct cap_header_data;
2721 cap_user_header_t cap_header = &cap_header_data;
2722 struct __user_cap_data_struct cap_data_data;
2723 cap_user_data_t cap_data = &cap_data_data;
2724 extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
2728 /* check for CAP_SYS_RAWIO */
2729 cap_header->pid = getpid();
2730 cap_header->version = _LINUX_CAPABILITY_VERSION;
2731 if (capget(cap_header, cap_data) < 0)
2732 err(-6, "capget(2) failed");
2734 if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
2736 warnx("capget(CAP_SYS_RAWIO) failed,"
2737 " try \"# setcap cap_sys_rawio=ep %s\"", progname);
2740 /* test file permissions */
2741 sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
2742 if (euidaccess(pathname, R_OK)) {
2744 warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
2747 /* if all else fails, thell them to be root */
2750 warnx("... or simply run as root");
2757 * NHM adds support for additional MSRs:
2759 * MSR_SMI_COUNT 0x00000034
2761 * MSR_PLATFORM_INFO 0x000000ce
2762 * MSR_PKG_CST_CONFIG_CONTROL 0x000000e2
2764 * MSR_MISC_PWR_MGMT 0x000001aa
2766 * MSR_PKG_C3_RESIDENCY 0x000003f8
2767 * MSR_PKG_C6_RESIDENCY 0x000003f9
2768 * MSR_CORE_C3_RESIDENCY 0x000003fc
2769 * MSR_CORE_C6_RESIDENCY 0x000003fd
2772 * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL
2773 * sets has_misc_feature_control
2775 int probe_nhm_msrs(unsigned int family, unsigned int model)
2777 unsigned long long msr;
2778 unsigned int base_ratio;
2779 int *pkg_cstate_limits;
2787 bclk = discover_bclk(family, model);
2790 case INTEL_FAM6_NEHALEM_EP: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
2791 case INTEL_FAM6_NEHALEM: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
2792 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
2793 case INTEL_FAM6_WESTMERE: /* Westmere Client - Clarkdale, Arrandale */
2794 case INTEL_FAM6_WESTMERE_EP: /* Westmere EP - Gulftown */
2795 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
2796 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
2797 pkg_cstate_limits = nhm_pkg_cstate_limits;
2799 case INTEL_FAM6_SANDYBRIDGE: /* SNB */
2800 case INTEL_FAM6_SANDYBRIDGE_X: /* SNB Xeon */
2801 case INTEL_FAM6_IVYBRIDGE: /* IVB */
2802 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
2803 pkg_cstate_limits = snb_pkg_cstate_limits;
2804 has_misc_feature_control = 1;
2806 case INTEL_FAM6_HASWELL_CORE: /* HSW */
2807 case INTEL_FAM6_HASWELL_X: /* HSX */
2808 case INTEL_FAM6_HASWELL_ULT: /* HSW */
2809 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
2810 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
2811 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
2812 case INTEL_FAM6_BROADWELL_X: /* BDX */
2813 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
2814 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
2815 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
2816 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
2817 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
2818 pkg_cstate_limits = hsw_pkg_cstate_limits;
2819 has_misc_feature_control = 1;
2821 case INTEL_FAM6_SKYLAKE_X: /* SKX */
2822 pkg_cstate_limits = skx_pkg_cstate_limits;
2823 has_misc_feature_control = 1;
2825 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
2826 no_MSR_MISC_PWR_MGMT = 1;
2827 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
2828 pkg_cstate_limits = slv_pkg_cstate_limits;
2830 case INTEL_FAM6_ATOM_AIRMONT: /* AMT */
2831 pkg_cstate_limits = amt_pkg_cstate_limits;
2832 no_MSR_MISC_PWR_MGMT = 1;
2834 case INTEL_FAM6_XEON_PHI_KNL: /* PHI */
2835 case INTEL_FAM6_XEON_PHI_KNM:
2836 pkg_cstate_limits = phi_pkg_cstate_limits;
2838 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
2839 case INTEL_FAM6_ATOM_GEMINI_LAKE:
2840 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
2841 pkg_cstate_limits = bxt_pkg_cstate_limits;
2846 get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
2847 pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
2849 get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
2850 base_ratio = (msr >> 8) & 0xFF;
2852 base_hz = base_ratio * bclk * 1000000;
2857 * SLV client has support for unique MSRs:
2859 * MSR_CC6_DEMOTION_POLICY_CONFIG
2860 * MSR_MC6_DEMOTION_POLICY_CONFIG
2863 int has_slv_msrs(unsigned int family, unsigned int model)
2869 case INTEL_FAM6_ATOM_SILVERMONT1:
2870 case INTEL_FAM6_ATOM_MERRIFIELD:
2871 case INTEL_FAM6_ATOM_MOOREFIELD:
2876 int is_dnv(unsigned int family, unsigned int model)
2883 case INTEL_FAM6_ATOM_DENVERTON:
2888 int is_bdx(unsigned int family, unsigned int model)
2895 case INTEL_FAM6_BROADWELL_X:
2896 case INTEL_FAM6_BROADWELL_XEON_D:
2901 int is_skx(unsigned int family, unsigned int model)
2908 case INTEL_FAM6_SKYLAKE_X:
2914 int has_turbo_ratio_limit(unsigned int family, unsigned int model)
2916 if (has_slv_msrs(family, model))
2920 /* Nehalem compatible, but do not include turbo-ratio limit support */
2921 case INTEL_FAM6_NEHALEM_EX: /* Nehalem-EX Xeon - Beckton */
2922 case INTEL_FAM6_WESTMERE_EX: /* Westmere-EX Xeon - Eagleton */
2923 case INTEL_FAM6_XEON_PHI_KNL: /* PHI - Knights Landing (different MSR definition) */
2924 case INTEL_FAM6_XEON_PHI_KNM:
2930 int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model)
2932 if (has_slv_msrs(family, model))
2937 int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
2946 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
2947 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
2953 int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
2962 case INTEL_FAM6_HASWELL_X: /* HSW Xeon */
2969 int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
2978 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
2979 case INTEL_FAM6_XEON_PHI_KNM:
2985 int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model)
2994 case INTEL_FAM6_ATOM_GOLDMONT:
2995 case INTEL_FAM6_SKYLAKE_X:
3001 int has_config_tdp(unsigned int family, unsigned int model)
3010 case INTEL_FAM6_IVYBRIDGE: /* IVB */
3011 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3012 case INTEL_FAM6_HASWELL_X: /* HSX */
3013 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3014 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3015 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3016 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
3017 case INTEL_FAM6_BROADWELL_X: /* BDX */
3018 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3019 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3020 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3021 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3022 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
3023 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3025 case INTEL_FAM6_XEON_PHI_KNL: /* Knights Landing */
3026 case INTEL_FAM6_XEON_PHI_KNM:
3034 dump_cstate_pstate_config_info(unsigned int family, unsigned int model)
3036 if (!do_nhm_platform_info)
3039 dump_nhm_platform_info();
3041 if (has_hsw_turbo_ratio_limit(family, model))
3042 dump_hsw_turbo_ratio_limits();
3044 if (has_ivt_turbo_ratio_limit(family, model))
3045 dump_ivt_turbo_ratio_limits();
3047 if (has_turbo_ratio_limit(family, model))
3048 dump_turbo_ratio_limits(family, model);
3050 if (has_atom_turbo_ratio_limit(family, model))
3051 dump_atom_turbo_ratio_limits();
3053 if (has_knl_turbo_ratio_limit(family, model))
3054 dump_knl_turbo_ratio_limits();
3056 if (has_config_tdp(family, model))
3063 dump_sysfs_cstate_config(void)
3072 if (!DO_BIC(BIC_sysfs))
3075 for (state = 0; state < 10; ++state) {
3077 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
3079 input = fopen(path, "r");
3082 fgets(name_buf, sizeof(name_buf), input);
3084 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
3085 sp = strchr(name_buf, '-');
3087 sp = strchrnul(name_buf, '\n');
3092 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc",
3094 input = fopen(path, "r");
3097 fgets(desc, sizeof(desc), input);
3099 fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc);
3104 dump_sysfs_pstate_config(void)
3107 char driver_buf[64];
3108 char governor_buf[64];
3112 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_driver",
3114 input = fopen(path, "r");
3115 if (input == NULL) {
3116 fprintf(stderr, "NSFOD %s\n", path);
3119 fgets(driver_buf, sizeof(driver_buf), input);
3122 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor",
3124 input = fopen(path, "r");
3125 if (input == NULL) {
3126 fprintf(stderr, "NSFOD %s\n", path);
3129 fgets(governor_buf, sizeof(governor_buf), input);
3132 fprintf(outf, "cpu%d: cpufreq driver: %s", base_cpu, driver_buf);
3133 fprintf(outf, "cpu%d: cpufreq governor: %s", base_cpu, governor_buf);
3135 sprintf(path, "/sys/devices/system/cpu/cpufreq/boost");
3136 input = fopen(path, "r");
3137 if (input != NULL) {
3138 fscanf(input, "%d", &turbo);
3139 fprintf(outf, "cpufreq boost: %d\n", turbo);
3143 sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo");
3144 input = fopen(path, "r");
3145 if (input != NULL) {
3146 fscanf(input, "%d", &turbo);
3147 fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo);
3155 * Decode the ENERGY_PERF_BIAS MSR
3157 int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3159 unsigned long long msr;
3168 /* EPB is per-package */
3169 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3172 if (cpu_migrate(cpu)) {
3173 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
3177 if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
3180 switch (msr & 0xF) {
3181 case ENERGY_PERF_BIAS_PERFORMANCE:
3182 epb_string = "performance";
3184 case ENERGY_PERF_BIAS_NORMAL:
3185 epb_string = "balanced";
3187 case ENERGY_PERF_BIAS_POWERSAVE:
3188 epb_string = "powersave";
3191 epb_string = "custom";
3194 fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
3200 * Decode the MSR_HWP_CAPABILITIES
3202 int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3204 unsigned long long msr;
3212 /* MSR_HWP_CAPABILITIES is per-package */
3213 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3216 if (cpu_migrate(cpu)) {
3217 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
3221 if (get_msr(cpu, MSR_PM_ENABLE, &msr))
3224 fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n",
3225 cpu, msr, (msr & (1 << 0)) ? "" : "No-");
3227 /* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
3228 if ((msr & (1 << 0)) == 0)
3231 if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
3234 fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
3235 "(high %d guar %d eff %d low %d)\n",
3237 (unsigned int)HWP_HIGHEST_PERF(msr),
3238 (unsigned int)HWP_GUARANTEED_PERF(msr),
3239 (unsigned int)HWP_MOSTEFFICIENT_PERF(msr),
3240 (unsigned int)HWP_LOWEST_PERF(msr));
3242 if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
3245 fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
3246 "(min %d max %d des %d epp 0x%x window 0x%x pkg 0x%x)\n",
3248 (unsigned int)(((msr) >> 0) & 0xff),
3249 (unsigned int)(((msr) >> 8) & 0xff),
3250 (unsigned int)(((msr) >> 16) & 0xff),
3251 (unsigned int)(((msr) >> 24) & 0xff),
3252 (unsigned int)(((msr) >> 32) & 0xff3),
3253 (unsigned int)(((msr) >> 42) & 0x1));
3256 if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
3259 fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
3260 "(min %d max %d des %d epp 0x%x window 0x%x)\n",
3262 (unsigned int)(((msr) >> 0) & 0xff),
3263 (unsigned int)(((msr) >> 8) & 0xff),
3264 (unsigned int)(((msr) >> 16) & 0xff),
3265 (unsigned int)(((msr) >> 24) & 0xff),
3266 (unsigned int)(((msr) >> 32) & 0xff3));
3268 if (has_hwp_notify) {
3269 if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
3272 fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
3273 "(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
3275 ((msr) & 0x1) ? "EN" : "Dis",
3276 ((msr) & 0x2) ? "EN" : "Dis");
3278 if (get_msr(cpu, MSR_HWP_STATUS, &msr))
3281 fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
3282 "(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
3284 ((msr) & 0x1) ? "" : "No-",
3285 ((msr) & 0x2) ? "" : "No-");
3291 * print_perf_limit()
3293 int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3295 unsigned long long msr;
3301 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3304 if (cpu_migrate(cpu)) {
3305 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
3309 if (do_core_perf_limit_reasons) {
3310 get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
3311 fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3312 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
3313 (msr & 1 << 15) ? "bit15, " : "",
3314 (msr & 1 << 14) ? "bit14, " : "",
3315 (msr & 1 << 13) ? "Transitions, " : "",
3316 (msr & 1 << 12) ? "MultiCoreTurbo, " : "",
3317 (msr & 1 << 11) ? "PkgPwrL2, " : "",
3318 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3319 (msr & 1 << 9) ? "CorePwr, " : "",
3320 (msr & 1 << 8) ? "Amps, " : "",
3321 (msr & 1 << 6) ? "VR-Therm, " : "",
3322 (msr & 1 << 5) ? "Auto-HWP, " : "",
3323 (msr & 1 << 4) ? "Graphics, " : "",
3324 (msr & 1 << 2) ? "bit2, " : "",
3325 (msr & 1 << 1) ? "ThermStatus, " : "",
3326 (msr & 1 << 0) ? "PROCHOT, " : "");
3327 fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n",
3328 (msr & 1 << 31) ? "bit31, " : "",
3329 (msr & 1 << 30) ? "bit30, " : "",
3330 (msr & 1 << 29) ? "Transitions, " : "",
3331 (msr & 1 << 28) ? "MultiCoreTurbo, " : "",
3332 (msr & 1 << 27) ? "PkgPwrL2, " : "",
3333 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3334 (msr & 1 << 25) ? "CorePwr, " : "",
3335 (msr & 1 << 24) ? "Amps, " : "",
3336 (msr & 1 << 22) ? "VR-Therm, " : "",
3337 (msr & 1 << 21) ? "Auto-HWP, " : "",
3338 (msr & 1 << 20) ? "Graphics, " : "",
3339 (msr & 1 << 18) ? "bit18, " : "",
3340 (msr & 1 << 17) ? "ThermStatus, " : "",
3341 (msr & 1 << 16) ? "PROCHOT, " : "");
3344 if (do_gfx_perf_limit_reasons) {
3345 get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
3346 fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3347 fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
3348 (msr & 1 << 0) ? "PROCHOT, " : "",
3349 (msr & 1 << 1) ? "ThermStatus, " : "",
3350 (msr & 1 << 4) ? "Graphics, " : "",
3351 (msr & 1 << 6) ? "VR-Therm, " : "",
3352 (msr & 1 << 8) ? "Amps, " : "",
3353 (msr & 1 << 9) ? "GFXPwr, " : "",
3354 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3355 (msr & 1 << 11) ? "PkgPwrL2, " : "");
3356 fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
3357 (msr & 1 << 16) ? "PROCHOT, " : "",
3358 (msr & 1 << 17) ? "ThermStatus, " : "",
3359 (msr & 1 << 20) ? "Graphics, " : "",
3360 (msr & 1 << 22) ? "VR-Therm, " : "",
3361 (msr & 1 << 24) ? "Amps, " : "",
3362 (msr & 1 << 25) ? "GFXPwr, " : "",
3363 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3364 (msr & 1 << 27) ? "PkgPwrL2, " : "");
3366 if (do_ring_perf_limit_reasons) {
3367 get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
3368 fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3369 fprintf(outf, " (Active: %s%s%s%s%s%s)",
3370 (msr & 1 << 0) ? "PROCHOT, " : "",
3371 (msr & 1 << 1) ? "ThermStatus, " : "",
3372 (msr & 1 << 6) ? "VR-Therm, " : "",
3373 (msr & 1 << 8) ? "Amps, " : "",
3374 (msr & 1 << 10) ? "PkgPwrL1, " : "",
3375 (msr & 1 << 11) ? "PkgPwrL2, " : "");
3376 fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
3377 (msr & 1 << 16) ? "PROCHOT, " : "",
3378 (msr & 1 << 17) ? "ThermStatus, " : "",
3379 (msr & 1 << 22) ? "VR-Therm, " : "",
3380 (msr & 1 << 24) ? "Amps, " : "",
3381 (msr & 1 << 26) ? "PkgPwrL1, " : "",
3382 (msr & 1 << 27) ? "PkgPwrL2, " : "");
3387 #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
3388 #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
3390 double get_tdp(unsigned int model)
3392 unsigned long long msr;
3394 if (do_rapl & RAPL_PKG_POWER_INFO)
3395 if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
3396 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
3399 case INTEL_FAM6_ATOM_SILVERMONT1:
3400 case INTEL_FAM6_ATOM_SILVERMONT2:
3408 * rapl_dram_energy_units_probe()
3409 * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
3412 rapl_dram_energy_units_probe(int model, double rapl_energy_units)
3414 /* only called for genuine_intel, family 6 */
3417 case INTEL_FAM6_HASWELL_X: /* HSX */
3418 case INTEL_FAM6_BROADWELL_X: /* BDX */
3419 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3420 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
3421 case INTEL_FAM6_XEON_PHI_KNM:
3422 return (rapl_dram_energy_units = 15.3 / 1000000);
3424 return (rapl_energy_units);
3432 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
3434 void rapl_probe(unsigned int family, unsigned int model)
3436 unsigned long long msr;
3437 unsigned int time_unit;
3447 case INTEL_FAM6_SANDYBRIDGE:
3448 case INTEL_FAM6_IVYBRIDGE:
3449 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3450 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3451 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3452 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3453 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
3454 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
3456 BIC_PRESENT(BIC_Pkg_J);
3457 BIC_PRESENT(BIC_Cor_J);
3458 BIC_PRESENT(BIC_GFX_J);
3460 BIC_PRESENT(BIC_PkgWatt);
3461 BIC_PRESENT(BIC_CorWatt);
3462 BIC_PRESENT(BIC_GFXWatt);
3465 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
3466 case INTEL_FAM6_ATOM_GEMINI_LAKE:
3467 do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO;
3469 BIC_PRESENT(BIC_Pkg_J);
3471 BIC_PRESENT(BIC_PkgWatt);
3473 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3474 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3475 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3476 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
3477 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_GFX | RAPL_PKG_POWER_INFO;
3478 BIC_PRESENT(BIC_PKG__);
3479 BIC_PRESENT(BIC_RAM__);
3481 BIC_PRESENT(BIC_Pkg_J);
3482 BIC_PRESENT(BIC_Cor_J);
3483 BIC_PRESENT(BIC_RAM_J);
3484 BIC_PRESENT(BIC_GFX_J);
3486 BIC_PRESENT(BIC_PkgWatt);
3487 BIC_PRESENT(BIC_CorWatt);
3488 BIC_PRESENT(BIC_RAMWatt);
3489 BIC_PRESENT(BIC_GFXWatt);
3492 case INTEL_FAM6_HASWELL_X: /* HSX */
3493 case INTEL_FAM6_BROADWELL_X: /* BDX */
3494 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3495 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3496 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
3497 case INTEL_FAM6_XEON_PHI_KNM:
3498 do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
3499 BIC_PRESENT(BIC_PKG__);
3500 BIC_PRESENT(BIC_RAM__);
3502 BIC_PRESENT(BIC_Pkg_J);
3503 BIC_PRESENT(BIC_RAM_J);
3505 BIC_PRESENT(BIC_PkgWatt);
3506 BIC_PRESENT(BIC_RAMWatt);
3509 case INTEL_FAM6_SANDYBRIDGE_X:
3510 case INTEL_FAM6_IVYBRIDGE_X:
3511 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;
3512 BIC_PRESENT(BIC_PKG__);
3513 BIC_PRESENT(BIC_RAM__);
3515 BIC_PRESENT(BIC_Pkg_J);
3516 BIC_PRESENT(BIC_Cor_J);
3517 BIC_PRESENT(BIC_RAM_J);
3519 BIC_PRESENT(BIC_PkgWatt);
3520 BIC_PRESENT(BIC_CorWatt);
3521 BIC_PRESENT(BIC_RAMWatt);
3524 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
3525 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
3526 do_rapl = RAPL_PKG | RAPL_CORES;
3528 BIC_PRESENT(BIC_Pkg_J);
3529 BIC_PRESENT(BIC_Cor_J);
3531 BIC_PRESENT(BIC_PkgWatt);
3532 BIC_PRESENT(BIC_CorWatt);
3535 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
3536 do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO | RAPL_CORES_ENERGY_STATUS;
3537 BIC_PRESENT(BIC_PKG__);
3538 BIC_PRESENT(BIC_RAM__);
3540 BIC_PRESENT(BIC_Pkg_J);
3541 BIC_PRESENT(BIC_Cor_J);
3542 BIC_PRESENT(BIC_RAM_J);
3544 BIC_PRESENT(BIC_PkgWatt);
3545 BIC_PRESENT(BIC_CorWatt);
3546 BIC_PRESENT(BIC_RAMWatt);
3553 /* units on package 0, verify later other packages match */
3554 if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
3557 rapl_power_units = 1.0 / (1 << (msr & 0xF));
3558 if (model == INTEL_FAM6_ATOM_SILVERMONT1)
3559 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
3561 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
3563 rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
3565 time_unit = msr >> 16 & 0xF;
3569 rapl_time_units = 1.0 / (1 << (time_unit));
3571 tdp = get_tdp(model);
3573 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
3575 fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
3580 void perf_limit_reasons_probe(unsigned int family, unsigned int model)
3589 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3590 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3591 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3592 do_gfx_perf_limit_reasons = 1;
3593 case INTEL_FAM6_HASWELL_X: /* HSX */
3594 do_core_perf_limit_reasons = 1;
3595 do_ring_perf_limit_reasons = 1;
3601 int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3603 unsigned long long msr;
3604 unsigned int dts, dts2;
3607 if (!(do_dts || do_ptm))
3612 /* DTS is per-core, no need to print for each thread */
3613 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
3616 if (cpu_migrate(cpu)) {
3617 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
3621 if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
3622 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
3625 dts = (msr >> 16) & 0x7F;
3626 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
3627 cpu, msr, tcc_activation_temp - dts);
3629 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
3632 dts = (msr >> 16) & 0x7F;
3633 dts2 = (msr >> 8) & 0x7F;
3634 fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
3635 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
3639 if (do_dts && debug) {
3640 unsigned int resolution;
3642 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
3645 dts = (msr >> 16) & 0x7F;
3646 resolution = (msr >> 27) & 0xF;
3647 fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
3648 cpu, msr, tcc_activation_temp - dts, resolution);
3650 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
3653 dts = (msr >> 16) & 0x7F;
3654 dts2 = (msr >> 8) & 0x7F;
3655 fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
3656 cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
3662 void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
3664 fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
3666 ((msr >> 15) & 1) ? "EN" : "DIS",
3667 ((msr >> 0) & 0x7FFF) * rapl_power_units,
3668 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
3669 (((msr >> 16) & 1) ? "EN" : "DIS"));
3674 int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3676 unsigned long long msr;
3682 /* RAPL counters are per package, so print only for 1st thread/package */
3683 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3687 if (cpu_migrate(cpu)) {
3688 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
3692 if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
3695 fprintf(outf, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr,
3696 rapl_power_units, rapl_energy_units, rapl_time_units);
3698 if (do_rapl & RAPL_PKG_POWER_INFO) {
3700 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
3704 fprintf(outf, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
3706 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3707 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3708 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3709 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
3712 if (do_rapl & RAPL_PKG) {
3714 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
3717 fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
3718 cpu, msr, (msr >> 63) & 1 ? "" : "UN");
3720 print_power_limit_msr(cpu, msr, "PKG Limit #1");
3721 fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
3723 ((msr >> 47) & 1) ? "EN" : "DIS",
3724 ((msr >> 32) & 0x7FFF) * rapl_power_units,
3725 (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
3726 ((msr >> 48) & 1) ? "EN" : "DIS");
3729 if (do_rapl & RAPL_DRAM_POWER_INFO) {
3730 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
3733 fprintf(outf, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
3735 ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3736 ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3737 ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3738 ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
3740 if (do_rapl & RAPL_DRAM) {
3741 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
3743 fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
3744 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
3746 print_power_limit_msr(cpu, msr, "DRAM Limit");
3748 if (do_rapl & RAPL_CORE_POLICY) {
3749 if (get_msr(cpu, MSR_PP0_POLICY, &msr))
3752 fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
3754 if (do_rapl & RAPL_CORES_POWER_LIMIT) {
3755 if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
3757 fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
3758 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
3759 print_power_limit_msr(cpu, msr, "Cores Limit");
3761 if (do_rapl & RAPL_GFX) {
3762 if (get_msr(cpu, MSR_PP1_POLICY, &msr))
3765 fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
3767 if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
3769 fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
3770 cpu, msr, (msr >> 31) & 1 ? "" : "UN");
3771 print_power_limit_msr(cpu, msr, "GFX Limit");
3777 * SNB adds support for additional MSRs:
3779 * MSR_PKG_C7_RESIDENCY 0x000003fa
3780 * MSR_CORE_C7_RESIDENCY 0x000003fe
3781 * MSR_PKG_C2_RESIDENCY 0x0000060d
3784 int has_snb_msrs(unsigned int family, unsigned int model)
3790 case INTEL_FAM6_SANDYBRIDGE:
3791 case INTEL_FAM6_SANDYBRIDGE_X:
3792 case INTEL_FAM6_IVYBRIDGE: /* IVB */
3793 case INTEL_FAM6_IVYBRIDGE_X: /* IVB Xeon */
3794 case INTEL_FAM6_HASWELL_CORE: /* HSW */
3795 case INTEL_FAM6_HASWELL_X: /* HSW */
3796 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3797 case INTEL_FAM6_HASWELL_GT3E: /* HSW */
3798 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3799 case INTEL_FAM6_BROADWELL_GT3E: /* BDW */
3800 case INTEL_FAM6_BROADWELL_X: /* BDX */
3801 case INTEL_FAM6_BROADWELL_XEON_D: /* BDX-DE */
3802 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3803 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3804 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3805 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
3806 case INTEL_FAM6_SKYLAKE_X: /* SKX */
3807 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
3808 case INTEL_FAM6_ATOM_GEMINI_LAKE:
3809 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
3816 * HSW adds support for additional MSRs:
3818 * MSR_PKG_C8_RESIDENCY 0x00000630
3819 * MSR_PKG_C9_RESIDENCY 0x00000631
3820 * MSR_PKG_C10_RESIDENCY 0x00000632
3822 * MSR_PKGC8_IRTL 0x00000633
3823 * MSR_PKGC9_IRTL 0x00000634
3824 * MSR_PKGC10_IRTL 0x00000635
3827 int has_hsw_msrs(unsigned int family, unsigned int model)
3833 case INTEL_FAM6_HASWELL_ULT: /* HSW */
3834 case INTEL_FAM6_BROADWELL_CORE: /* BDW */
3835 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3836 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3837 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3838 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
3839 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
3840 case INTEL_FAM6_ATOM_GEMINI_LAKE:
3847 * SKL adds support for additional MSRS:
3849 * MSR_PKG_WEIGHTED_CORE_C0_RES 0x00000658
3850 * MSR_PKG_ANY_CORE_C0_RES 0x00000659
3851 * MSR_PKG_ANY_GFXE_C0_RES 0x0000065A
3852 * MSR_PKG_BOTH_CORE_GFXE_C0_RES 0x0000065B
3854 int has_skl_msrs(unsigned int family, unsigned int model)
3860 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
3861 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
3862 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
3863 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
3869 int is_slm(unsigned int family, unsigned int model)
3874 case INTEL_FAM6_ATOM_SILVERMONT1: /* BYT */
3875 case INTEL_FAM6_ATOM_SILVERMONT2: /* AVN */
3881 int is_knl(unsigned int family, unsigned int model)
3886 case INTEL_FAM6_XEON_PHI_KNL: /* KNL */
3887 case INTEL_FAM6_XEON_PHI_KNM:
3893 unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
3895 if (is_knl(family, model))
3900 #define SLM_BCLK_FREQS 5
3901 double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
3903 double slm_bclk(void)
3905 unsigned long long msr = 3;
3909 if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
3910 fprintf(outf, "SLM BCLK: unknown\n");
3913 if (i >= SLM_BCLK_FREQS) {
3914 fprintf(outf, "SLM BCLK[%d] invalid\n", i);
3917 freq = slm_freq_table[i];
3920 fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
3925 double discover_bclk(unsigned int family, unsigned int model)
3927 if (has_snb_msrs(family, model) || is_knl(family, model))
3929 else if (is_slm(family, model))
3936 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
3937 * the Thermal Control Circuit (TCC) activates.
3938 * This is usually equal to tjMax.
3940 * Older processors do not have this MSR, so there we guess,
3941 * but also allow cmdline over-ride with -T.
3943 * Several MSR temperature values are in units of degrees-C
3944 * below this value, including the Digital Thermal Sensor (DTS),
3945 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
3947 int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3949 unsigned long long msr;
3950 unsigned int target_c_local;
3953 /* tcc_activation_temp is used only for dts or ptm */
3954 if (!(do_dts || do_ptm))
3957 /* this is a per-package concept */
3958 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3962 if (cpu_migrate(cpu)) {
3963 fprintf(outf, "Could not migrate to CPU %d\n", cpu);
3967 if (tcc_activation_temp_override != 0) {
3968 tcc_activation_temp = tcc_activation_temp_override;
3969 fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n",
3970 cpu, tcc_activation_temp);
3974 /* Temperature Target MSR is Nehalem and newer only */
3975 if (!do_nhm_platform_info)
3978 if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
3981 target_c_local = (msr >> 16) & 0xFF;
3984 fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
3985 cpu, msr, target_c_local);
3987 if (!target_c_local)
3990 tcc_activation_temp = target_c_local;
3995 tcc_activation_temp = TJMAX_DEFAULT;
3996 fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
3997 cpu, tcc_activation_temp);
4002 void decode_feature_control_msr(void)
4004 unsigned long long msr;
4006 if (!get_msr(base_cpu, MSR_IA32_FEATURE_CONTROL, &msr))
4007 fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
4009 msr & FEATURE_CONTROL_LOCKED ? "" : "UN-",
4010 msr & (1 << 18) ? "SGX" : "");
4013 void decode_misc_enable_msr(void)
4015 unsigned long long msr;
4020 if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
4021 fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n",
4023 msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-",
4024 msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-",
4025 msr & MSR_IA32_MISC_ENABLE_MWAIT ? "No-" : "",
4026 msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "",
4027 msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : "");
4030 void decode_misc_feature_control(void)
4032 unsigned long long msr;
4034 if (!has_misc_feature_control)
4037 if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr))
4038 fprintf(outf, "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n",
4040 msr & (0 << 0) ? "No-" : "",
4041 msr & (1 << 0) ? "No-" : "",
4042 msr & (2 << 0) ? "No-" : "",
4043 msr & (3 << 0) ? "No-" : "");
4046 * Decode MSR_MISC_PWR_MGMT
4048 * Decode the bits according to the Nehalem documentation
4049 * bit[0] seems to continue to have same meaning going forward
4052 void decode_misc_pwr_mgmt_msr(void)
4054 unsigned long long msr;
4056 if (!do_nhm_platform_info)
4059 if (no_MSR_MISC_PWR_MGMT)
4062 if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
4063 fprintf(outf, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB %sable-OOB)\n",
4065 msr & (1 << 0) ? "DIS" : "EN",
4066 msr & (1 << 1) ? "EN" : "DIS",
4067 msr & (1 << 8) ? "EN" : "DIS");
4070 * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG
4072 * This MSRs are present on Silvermont processors,
4073 * Intel Atom processor E3000 series (Baytrail), and friends.
4075 void decode_c6_demotion_policy_msr(void)
4077 unsigned long long msr;
4079 if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr))
4080 fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n",
4081 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
4083 if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr))
4084 fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n",
4085 base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
4088 void process_cpuid()
4090 unsigned int eax, ebx, ecx, edx, max_level, max_extended_level;
4091 unsigned int fms, family, model, stepping;
4092 unsigned int has_turbo;
4094 eax = ebx = ecx = edx = 0;
4096 __cpuid(0, max_level, ebx, ecx, edx);
4098 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
4102 fprintf(outf, "CPUID(0): %.4s%.4s%.4s ",
4103 (char *)&ebx, (char *)&edx, (char *)&ecx);
4105 __cpuid(1, fms, ebx, ecx, edx);
4106 family = (fms >> 8) & 0xf;
4107 model = (fms >> 4) & 0xf;
4108 stepping = fms & 0xf;
4109 if (family == 6 || family == 0xf)
4110 model += ((fms >> 16) & 0xf) << 4;
4113 fprintf(outf, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
4114 max_level, family, model, stepping, family, model, stepping);
4115 fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s\n",
4116 ecx & (1 << 0) ? "SSE3" : "-",
4117 ecx & (1 << 3) ? "MONITOR" : "-",
4118 ecx & (1 << 6) ? "SMX" : "-",
4119 ecx & (1 << 7) ? "EIST" : "-",
4120 ecx & (1 << 8) ? "TM2" : "-",
4121 edx & (1 << 4) ? "TSC" : "-",
4122 edx & (1 << 5) ? "MSR" : "-",
4123 edx & (1 << 22) ? "ACPI-TM" : "-",
4124 edx & (1 << 29) ? "TM" : "-");
4127 if (!(edx & (1 << 5)))
4128 errx(1, "CPUID: no MSR");
4131 * check max extended function levels of CPUID.
4132 * This is needed to check for invariant TSC.
4133 * This check is valid for both Intel and AMD.
4135 ebx = ecx = edx = 0;
4136 __cpuid(0x80000000, max_extended_level, ebx, ecx, edx);
4138 if (max_extended_level >= 0x80000007) {
4141 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
4142 * this check is valid for both Intel and AMD
4144 __cpuid(0x80000007, eax, ebx, ecx, edx);
4145 has_invariant_tsc = edx & (1 << 8);
4149 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
4150 * this check is valid for both Intel and AMD
4153 __cpuid(0x6, eax, ebx, ecx, edx);
4154 has_aperf = ecx & (1 << 0);
4156 BIC_PRESENT(BIC_Avg_MHz);
4157 BIC_PRESENT(BIC_Busy);
4158 BIC_PRESENT(BIC_Bzy_MHz);
4160 do_dts = eax & (1 << 0);
4162 BIC_PRESENT(BIC_CoreTmp);
4163 has_turbo = eax & (1 << 1);
4164 do_ptm = eax & (1 << 6);
4166 BIC_PRESENT(BIC_PkgTmp);
4167 has_hwp = eax & (1 << 7);
4168 has_hwp_notify = eax & (1 << 8);
4169 has_hwp_activity_window = eax & (1 << 9);
4170 has_hwp_epp = eax & (1 << 10);
4171 has_hwp_pkg = eax & (1 << 11);
4172 has_epb = ecx & (1 << 3);
4175 fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, "
4176 "%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
4177 has_aperf ? "" : "No-",
4178 has_turbo ? "" : "No-",
4179 do_dts ? "" : "No-",
4180 do_ptm ? "" : "No-",
4181 has_hwp ? "" : "No-",
4182 has_hwp_notify ? "" : "No-",
4183 has_hwp_activity_window ? "" : "No-",
4184 has_hwp_epp ? "" : "No-",
4185 has_hwp_pkg ? "" : "No-",
4186 has_epb ? "" : "No-");
4189 decode_misc_enable_msr();
4192 if (max_level >= 0x7 && !quiet) {
4197 __cpuid_count(0x7, 0, eax, ebx, ecx, edx);
4199 has_sgx = ebx & (1 << 2);
4200 fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-");
4203 decode_feature_control_msr();
4206 if (max_level >= 0x15) {
4207 unsigned int eax_crystal;
4208 unsigned int ebx_tsc;
4211 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
4213 eax_crystal = ebx_tsc = crystal_hz = edx = 0;
4214 __cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx);
4218 if (!quiet && (ebx != 0))
4219 fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
4220 eax_crystal, ebx_tsc, crystal_hz);
4222 if (crystal_hz == 0)
4224 case INTEL_FAM6_SKYLAKE_MOBILE: /* SKL */
4225 case INTEL_FAM6_SKYLAKE_DESKTOP: /* SKL */
4226 case INTEL_FAM6_KABYLAKE_MOBILE: /* KBL */
4227 case INTEL_FAM6_KABYLAKE_DESKTOP: /* KBL */
4228 crystal_hz = 24000000; /* 24.0 MHz */
4230 case INTEL_FAM6_SKYLAKE_X: /* SKX */
4231 case INTEL_FAM6_ATOM_DENVERTON: /* DNV */
4232 crystal_hz = 25000000; /* 25.0 MHz */
4234 case INTEL_FAM6_ATOM_GOLDMONT: /* BXT */
4235 case INTEL_FAM6_ATOM_GEMINI_LAKE:
4236 crystal_hz = 19200000; /* 19.2 MHz */
4243 tsc_hz = (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
4245 fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
4246 tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal);
4250 if (max_level >= 0x16) {
4251 unsigned int base_mhz, max_mhz, bus_mhz, edx;
4254 * CPUID 16H Base MHz, Max MHz, Bus MHz
4256 base_mhz = max_mhz = bus_mhz = edx = 0;
4258 __cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx);
4260 fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
4261 base_mhz, max_mhz, bus_mhz);
4265 aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
4267 BIC_PRESENT(BIC_IRQ);
4268 BIC_PRESENT(BIC_TSC_MHz);
4270 if (probe_nhm_msrs(family, model)) {
4271 do_nhm_platform_info = 1;
4272 BIC_PRESENT(BIC_CPU_c1);
4273 BIC_PRESENT(BIC_CPU_c3);
4274 BIC_PRESENT(BIC_CPU_c6);
4275 BIC_PRESENT(BIC_SMI);
4277 do_snb_cstates = has_snb_msrs(family, model);
4280 BIC_PRESENT(BIC_CPU_c7);
4282 do_irtl_snb = has_snb_msrs(family, model);
4283 if (do_snb_cstates && (pkg_cstate_limit >= PCL__2))
4284 BIC_PRESENT(BIC_Pkgpc2);
4285 if (pkg_cstate_limit >= PCL__3)
4286 BIC_PRESENT(BIC_Pkgpc3);
4287 if (pkg_cstate_limit >= PCL__6)
4288 BIC_PRESENT(BIC_Pkgpc6);
4289 if (do_snb_cstates && (pkg_cstate_limit >= PCL__7))
4290 BIC_PRESENT(BIC_Pkgpc7);
4291 if (has_slv_msrs(family, model)) {
4292 BIC_NOT_PRESENT(BIC_Pkgpc2);
4293 BIC_NOT_PRESENT(BIC_Pkgpc3);
4294 BIC_PRESENT(BIC_Pkgpc6);
4295 BIC_NOT_PRESENT(BIC_Pkgpc7);
4296 BIC_PRESENT(BIC_Mod_c6);
4297 use_c1_residency_msr = 1;
4299 if (is_dnv(family, model)) {
4300 BIC_PRESENT(BIC_CPU_c1);
4301 BIC_NOT_PRESENT(BIC_CPU_c3);
4302 BIC_NOT_PRESENT(BIC_Pkgpc3);
4303 BIC_NOT_PRESENT(BIC_CPU_c7);
4304 BIC_NOT_PRESENT(BIC_Pkgpc7);
4305 use_c1_residency_msr = 1;
4307 if (is_skx(family, model)) {
4308 BIC_NOT_PRESENT(BIC_CPU_c3);
4309 BIC_NOT_PRESENT(BIC_Pkgpc3);
4310 BIC_NOT_PRESENT(BIC_CPU_c7);
4311 BIC_NOT_PRESENT(BIC_Pkgpc7);
4313 if (is_bdx(family, model)) {
4314 BIC_NOT_PRESENT(BIC_CPU_c7);
4315 BIC_NOT_PRESENT(BIC_Pkgpc7);
4317 if (has_hsw_msrs(family, model)) {
4318 BIC_PRESENT(BIC_Pkgpc8);
4319 BIC_PRESENT(BIC_Pkgpc9);
4320 BIC_PRESENT(BIC_Pkgpc10);
4322 do_irtl_hsw = has_hsw_msrs(family, model);
4323 if (has_skl_msrs(family, model)) {
4324 BIC_PRESENT(BIC_Totl_c0);
4325 BIC_PRESENT(BIC_Any_c0);
4326 BIC_PRESENT(BIC_GFX_c0);
4327 BIC_PRESENT(BIC_CPUGFX);
4329 do_slm_cstates = is_slm(family, model);
4330 do_knl_cstates = is_knl(family, model);
4333 decode_misc_pwr_mgmt_msr();
4335 if (!quiet && has_slv_msrs(family, model))
4336 decode_c6_demotion_policy_msr();
4338 rapl_probe(family, model);
4339 perf_limit_reasons_probe(family, model);
4342 dump_cstate_pstate_config_info(family, model);
4345 dump_sysfs_cstate_config();
4347 dump_sysfs_pstate_config();
4349 if (has_skl_msrs(family, model))
4350 calculate_tsc_tweak();
4352 if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK))
4353 BIC_PRESENT(BIC_GFX_rc6);
4355 if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK))
4356 BIC_PRESENT(BIC_GFXMHz);
4359 decode_misc_feature_control();
4366 * in /dev/cpu/ return success for names that are numbers
4367 * ie. filter out ".", "..", "microcode".
4369 int dir_filter(const struct dirent *dirp)
4371 if (isdigit(dirp->d_name[0]))
4377 int open_dev_cpu_msr(int dummy1)
4382 void topology_probe()
4385 int max_core_id = 0;
4386 int max_package_id = 0;
4387 int max_siblings = 0;
4388 struct cpu_topology {
4390 int physical_package_id;
4393 /* Initialize num_cpus, max_cpu_num */
4395 topo.max_cpu_num = 0;
4396 for_all_proc_cpus(count_cpus);
4397 if (!summary_only && topo.num_cpus > 1)
4398 BIC_PRESENT(BIC_CPU);
4401 fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
4403 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
4405 err(1, "calloc cpus");
4408 * Allocate and initialize cpu_present_set
4410 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
4411 if (cpu_present_set == NULL)
4412 err(3, "CPU_ALLOC");
4413 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4414 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
4415 for_all_proc_cpus(mark_cpu_present);
4418 * Validate that all cpus in cpu_subset are also in cpu_present_set
4420 for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) {
4421 if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset))
4422 if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set))
4423 err(1, "cpu%d not present", i);
4427 * Allocate and initialize cpu_affinity_set
4429 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
4430 if (cpu_affinity_set == NULL)
4431 err(3, "CPU_ALLOC");
4432 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4433 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
4438 * find max_core_id, max_package_id
4440 for (i = 0; i <= topo.max_cpu_num; ++i) {
4443 if (cpu_is_not_present(i)) {
4445 fprintf(outf, "cpu%d NOT PRESENT\n", i);
4448 cpus[i].core_id = get_core_id(i);
4449 if (cpus[i].core_id > max_core_id)
4450 max_core_id = cpus[i].core_id;
4452 cpus[i].physical_package_id = get_physical_package_id(i);
4453 if (cpus[i].physical_package_id > max_package_id)
4454 max_package_id = cpus[i].physical_package_id;
4456 siblings = get_num_ht_siblings(i);
4457 if (siblings > max_siblings)
4458 max_siblings = siblings;
4460 fprintf(outf, "cpu %d pkg %d core %d\n",
4461 i, cpus[i].physical_package_id, cpus[i].core_id);
4463 topo.num_cores_per_pkg = max_core_id + 1;
4465 fprintf(outf, "max_core_id %d, sizing for %d cores per package\n",
4466 max_core_id, topo.num_cores_per_pkg);
4467 if (!summary_only && topo.num_cores_per_pkg > 1)
4468 BIC_PRESENT(BIC_Core);
4470 topo.num_packages = max_package_id + 1;
4472 fprintf(outf, "max_package_id %d, sizing for %d packages\n",
4473 max_package_id, topo.num_packages);
4474 if (!summary_only && topo.num_packages > 1)
4475 BIC_PRESENT(BIC_Package);
4477 topo.num_threads_per_core = max_siblings;
4479 fprintf(outf, "max_siblings %d\n", max_siblings);
4485 allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
4489 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
4490 topo.num_packages, sizeof(struct thread_data));
4494 for (i = 0; i < topo.num_threads_per_core *
4495 topo.num_cores_per_pkg * topo.num_packages; i++)
4496 (*t)[i].cpu_id = -1;
4498 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
4499 sizeof(struct core_data));
4503 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
4504 (*c)[i].core_id = -1;
4506 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
4510 for (i = 0; i < topo.num_packages; i++)
4511 (*p)[i].package_id = i;
4515 err(1, "calloc counters");
4520 * set cpu_id, core_num, pkg_num
4521 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
4523 * increment topo.num_cores when 1st core in pkg seen
4525 void init_counter(struct thread_data *thread_base, struct core_data *core_base,
4526 struct pkg_data *pkg_base, int thread_num, int core_num,
4527 int pkg_num, int cpu_id)
4529 struct thread_data *t;
4530 struct core_data *c;
4533 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
4534 c = GET_CORE(core_base, core_num, pkg_num);
4535 p = GET_PKG(pkg_base, pkg_num);
4538 if (thread_num == 0) {
4539 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
4540 if (cpu_is_first_core_in_package(cpu_id))
4541 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
4544 c->core_id = core_num;
4545 p->package_id = pkg_num;
4549 int initialize_counters(int cpu_id)
4551 int my_thread_id, my_core_id, my_package_id;
4553 my_package_id = get_physical_package_id(cpu_id);
4554 my_core_id = get_core_id(cpu_id);
4555 my_thread_id = get_cpu_position_in_core(cpu_id);
4559 init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
4560 init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
4564 void allocate_output_buffer()
4566 output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
4567 outp = output_buffer;
4569 err(-1, "calloc output buffer");
4571 void allocate_fd_percpu(void)
4573 fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
4574 if (fd_percpu == NULL)
4575 err(-1, "calloc fd_percpu");
4577 void allocate_irq_buffers(void)
4579 irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int));
4580 if (irq_column_2_cpu == NULL)
4581 err(-1, "calloc %d", topo.num_cpus);
4583 irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int));
4584 if (irqs_per_cpu == NULL)
4585 err(-1, "calloc %d", topo.max_cpu_num + 1);
4587 void setup_all_buffers(void)
4590 allocate_irq_buffers();
4591 allocate_fd_percpu();
4592 allocate_counters(&thread_even, &core_even, &package_even);
4593 allocate_counters(&thread_odd, &core_odd, &package_odd);
4594 allocate_output_buffer();
4595 for_all_proc_cpus(initialize_counters);
4598 void set_base_cpu(void)
4600 base_cpu = sched_getcpu();
4602 err(-ENODEV, "No valid cpus found");
4605 fprintf(outf, "base_cpu = %d\n", base_cpu);
4608 void turbostat_init()
4610 setup_all_buffers();
4613 check_permissions();
4618 for_all_cpus(print_hwp, ODD_COUNTERS);
4621 for_all_cpus(print_epb, ODD_COUNTERS);
4624 for_all_cpus(print_perf_limit, ODD_COUNTERS);
4627 for_all_cpus(print_rapl, ODD_COUNTERS);
4629 for_all_cpus(set_temperature_target, ODD_COUNTERS);
4632 for_all_cpus(print_thermal, ODD_COUNTERS);
4634 if (!quiet && do_irtl_snb)
4638 int fork_it(char **argv)
4643 snapshot_proc_sysfs_files();
4644 status = for_all_cpus(get_counters, EVEN_COUNTERS);
4647 /* clear affinity side-effect of get_counters() */
4648 sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
4649 gettimeofday(&tv_even, (struct timezone *)NULL);
4654 execvp(argv[0], argv);
4655 err(errno, "exec %s", argv[0]);
4659 if (child_pid == -1)
4662 signal(SIGINT, SIG_IGN);
4663 signal(SIGQUIT, SIG_IGN);
4664 if (waitpid(child_pid, &status, 0) == -1)
4665 err(status, "waitpid");
4668 * n.b. fork_it() does not check for errors from for_all_cpus()
4669 * because re-starting is problematic when forking
4671 snapshot_proc_sysfs_files();
4672 for_all_cpus(get_counters, ODD_COUNTERS);
4673 gettimeofday(&tv_odd, (struct timezone *)NULL);
4674 timersub(&tv_odd, &tv_even, &tv_delta);
4675 if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS))
4676 fprintf(outf, "%s: Counter reset detected\n", progname);
4678 compute_average(EVEN_COUNTERS);
4679 format_all_counters(EVEN_COUNTERS);
4682 fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
4684 flush_output_stderr();
4689 int get_and_dump_counters(void)
4693 snapshot_proc_sysfs_files();
4694 status = for_all_cpus(get_counters, ODD_COUNTERS);
4698 status = for_all_cpus(dump_counters, ODD_COUNTERS);
4702 flush_output_stdout();
4707 void print_version() {
4708 fprintf(outf, "turbostat version 17.06.23"
4709 " - Len Brown <lenb@kernel.org>\n");
4712 int add_counter(unsigned int msr_num, char *path, char *name,
4713 unsigned int width, enum counter_scope scope,
4714 enum counter_type type, enum counter_format format, int flags)
4716 struct msr_counter *msrp;
4718 msrp = calloc(1, sizeof(struct msr_counter));
4724 msrp->msr_num = msr_num;
4725 strncpy(msrp->name, name, NAME_BYTES);
4727 strncpy(msrp->path, path, PATH_BYTES);
4728 msrp->width = width;
4730 msrp->format = format;
4731 msrp->flags = flags;
4736 msrp->next = sys.tp;
4738 sys.added_thread_counters++;
4739 if (sys.added_thread_counters > MAX_ADDED_COUNTERS) {
4740 fprintf(stderr, "exceeded max %d added thread counters\n",
4741 MAX_ADDED_COUNTERS);
4747 msrp->next = sys.cp;
4749 sys.added_core_counters++;
4750 if (sys.added_core_counters > MAX_ADDED_COUNTERS) {
4751 fprintf(stderr, "exceeded max %d added core counters\n",
4752 MAX_ADDED_COUNTERS);
4758 msrp->next = sys.pp;
4760 sys.added_package_counters++;
4761 if (sys.added_package_counters > MAX_ADDED_COUNTERS) {
4762 fprintf(stderr, "exceeded max %d added package counters\n",
4763 MAX_ADDED_COUNTERS);
4772 void parse_add_command(char *add_command)
4776 char name_buffer[NAME_BYTES] = "";
4779 enum counter_scope scope = SCOPE_CPU;
4780 enum counter_type type = COUNTER_CYCLES;
4781 enum counter_format format = FORMAT_DELTA;
4783 while (add_command) {
4785 if (sscanf(add_command, "msr0x%x", &msr_num) == 1)
4788 if (sscanf(add_command, "msr%d", &msr_num) == 1)
4791 if (*add_command == '/') {
4796 if (sscanf(add_command, "u%d", &width) == 1) {
4797 if ((width == 32) || (width == 64))
4801 if (!strncmp(add_command, "cpu", strlen("cpu"))) {
4805 if (!strncmp(add_command, "core", strlen("core"))) {
4809 if (!strncmp(add_command, "package", strlen("package"))) {
4810 scope = SCOPE_PACKAGE;
4813 if (!strncmp(add_command, "cycles", strlen("cycles"))) {
4814 type = COUNTER_CYCLES;
4817 if (!strncmp(add_command, "seconds", strlen("seconds"))) {
4818 type = COUNTER_SECONDS;
4821 if (!strncmp(add_command, "usec", strlen("usec"))) {
4822 type = COUNTER_USEC;
4825 if (!strncmp(add_command, "raw", strlen("raw"))) {
4826 format = FORMAT_RAW;
4829 if (!strncmp(add_command, "delta", strlen("delta"))) {
4830 format = FORMAT_DELTA;
4833 if (!strncmp(add_command, "percent", strlen("percent"))) {
4834 format = FORMAT_PERCENT;
4838 if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) { /* 18 < NAME_BYTES */
4841 eos = strchr(name_buffer, ',');
4848 add_command = strchr(add_command, ',');
4850 *add_command = '\0';
4855 if ((msr_num == 0) && (path == NULL)) {
4856 fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n");
4860 /* generate default column header */
4861 if (*name_buffer == '\0') {
4863 sprintf(name_buffer, "M0x%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
4865 sprintf(name_buffer, "M0X%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
4868 if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0))
4877 int is_deferred_skip(char *name)
4881 for (i = 0; i < deferred_skip_index; ++i)
4882 if (!strcmp(name, deferred_skip_names[i]))
4887 void probe_sysfs(void)
4895 if (!DO_BIC(BIC_sysfs))
4898 for (state = 10; state > 0; --state) {
4900 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
4902 input = fopen(path, "r");
4905 fgets(name_buf, sizeof(name_buf), input);
4907 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
4908 sp = strchr(name_buf, '-');
4910 sp = strchrnul(name_buf, '\n');
4916 sprintf(path, "cpuidle/state%d/time", state);
4918 if (is_deferred_skip(name_buf))
4921 add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC,
4922 FORMAT_PERCENT, SYSFS_PERCPU);
4925 for (state = 10; state > 0; --state) {
4927 sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
4929 input = fopen(path, "r");
4932 fgets(name_buf, sizeof(name_buf), input);
4933 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
4934 sp = strchr(name_buf, '-');
4936 sp = strchrnul(name_buf, '\n');
4940 sprintf(path, "cpuidle/state%d/usage", state);
4942 if (is_deferred_skip(name_buf))
4945 add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS,
4946 FORMAT_DELTA, SYSFS_PERCPU);
4953 * parse cpuset with following syntax
4954 * 1,2,4..6,8-10 and set bits in cpu_subset
4956 void parse_cpu_command(char *optarg)
4958 unsigned int start, end;
4961 if (!strcmp(optarg, "core")) {
4967 if (!strcmp(optarg, "package")) {
4973 if (show_core_only || show_pkg_only)
4976 cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS);
4977 if (cpu_subset == NULL)
4978 err(3, "CPU_ALLOC");
4979 cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS);
4981 CPU_ZERO_S(cpu_subset_size, cpu_subset);
4985 while (next && *next) {
4987 if (*next == '-') /* no negative cpu numbers */
4990 start = strtoul(next, &next, 10);
4992 if (start >= CPU_SUBSET_MAXCPUS)
4994 CPU_SET_S(start, cpu_subset_size, cpu_subset);
5005 next += 1; /* start range */
5006 } else if (*next == '.') {
5009 next += 1; /* start range */
5014 end = strtoul(next, &next, 10);
5018 while (++start <= end) {
5019 if (start >= CPU_SUBSET_MAXCPUS)
5021 CPU_SET_S(start, cpu_subset_size, cpu_subset);
5026 else if (*next != '\0')
5033 fprintf(stderr, "\"--cpu %s\" malformed\n", optarg);
5039 void cmdline(int argc, char **argv)
5042 int option_index = 0;
5043 static struct option long_options[] = {
5044 {"add", required_argument, 0, 'a'},
5045 {"cpu", required_argument, 0, 'c'},
5046 {"Dump", no_argument, 0, 'D'},
5047 {"debug", no_argument, 0, 'd'}, /* internal, not documented */
5048 {"enable", required_argument, 0, 'e'},
5049 {"interval", required_argument, 0, 'i'},
5050 {"help", no_argument, 0, 'h'},
5051 {"hide", required_argument, 0, 'H'}, // meh, -h taken by --help
5052 {"Joules", no_argument, 0, 'J'},
5053 {"list", no_argument, 0, 'l'},
5054 {"out", required_argument, 0, 'o'},
5055 {"quiet", no_argument, 0, 'q'},
5056 {"show", required_argument, 0, 's'},
5057 {"Summary", no_argument, 0, 'S'},
5058 {"TCC", required_argument, 0, 'T'},
5059 {"version", no_argument, 0, 'v' },
5065 while ((opt = getopt_long_only(argc, argv, "+C:c:Dde:hi:Jo:qST:v",
5066 long_options, &option_index)) != -1) {
5069 parse_add_command(optarg);
5072 parse_cpu_command(optarg);
5078 /* --enable specified counter */
5079 bic_enabled |= bic_lookup(optarg, SHOW_LIST);
5083 ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
5087 * --hide: do not show those specified
5088 * multiple invocations simply clear more bits in enabled mask
5090 bic_enabled &= ~bic_lookup(optarg, HIDE_LIST);
5098 double interval = strtod(optarg, NULL);
5100 if (interval < 0.001) {
5101 fprintf(outf, "interval %f seconds is too small\n",
5106 interval_ts.tv_sec = interval;
5107 interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
5114 ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
5119 outf = fopen_or_die(optarg, "w");
5126 * --show: show only those specified
5127 * The 1st invocation will clear and replace the enabled mask
5128 * subsequent invocations can add to it.
5131 bic_enabled = bic_lookup(optarg, SHOW_LIST);
5133 bic_enabled |= bic_lookup(optarg, SHOW_LIST);
5140 tcc_activation_temp_override = atoi(optarg);
5150 int main(int argc, char **argv)
5154 cmdline(argc, argv);
5163 /* dump counters and exit */
5165 return get_and_dump_counters();
5167 /* list header and exit */
5168 if (list_header_only) {
5170 flush_output_stdout();
5175 * if any params left, it must be a command to fork
5178 return fork_it(argv + optind);