2 * turbostat -- Log CPU frequency and C-state residency
3 * on modern Intel turbo-capable processors for collectd.
5 * Based on the 'turbostat' tool of the Linux kernel, found at
6 * linux/tools/power/x86/turbostat/turbostat.c:
8 * Copyright (c) 2013 Intel Corporation.
9 * Len Brown <len.brown@intel.com>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms and conditions of the GNU General Public License,
13 * version 2, as published by the Free Software Foundation.
15 * This program is distributed in the hope it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
20 * You should have received a copy of the GNU General Public License along with
21 * this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
24 * Ported to collectd by Vincent Brillault <git@lerya.net>
28 * _GNU_SOURCE is required because of the following functions:
38 #include <asm/msr-index.h>
43 #include <sys/types.h>
46 #include <sys/resource.h>
61 #define PLUGIN_NAME "turbostat"
64 * This tool uses the Model-Specific Registers (MSRs) present on Intel processors.
65 * The general description each of these registers, depending on the architecture,
66 * can be found in the IntelĀ® 64 and IA-32 Architectures Software Developer Manual,
67 * Volume 3 Chapter 35.
71 * If set, aperf_mperf_unstable disables a/mperf based stats.
72 * This includes: C0 & C1 states, frequency
74 * This value is automatically set if mperf or aperf go backward
76 static _Bool aperf_mperf_unstable;
79 * Bitmask of the list of core C states supported by the processor.
80 * Currently supported C-states (by this plugin): 3, 6, 7
82 static unsigned int do_core_cstate;
85 * Bitmask of the list of pacages C states supported by the processor.
86 * Currently supported C-states (by this plugin): 2, 3, 6, 7, 8, 9, 10
88 static unsigned int do_pkg_cstate;
91 * Boolean indicating if the processor supports 'Digital temperature sensor'
92 * This feature enables the monitoring of the temperature of each core
94 * This feature has two limitations:
95 * - if MSR_IA32_TEMPERATURE_TARGET is not supported, the absolute temperature might be wrong
96 * - Temperatures above the tcc_activation_temp are not recorded
101 * Boolean indicating if the processor supports 'Package thermal management'
102 * This feature allows the monitoring of the temperature of each package
104 * This feature has two limitations:
105 * - if MSR_IA32_TEMPERATURE_TARGET is not supported, the absolute temperature might be wrong
106 * - Temperatures above the tcc_activation_temp are not recorded
111 * Thermal Control Circuit Activation Temperature as configured by the user.
112 * This override the automated detection via MSR_IA32_TEMPERATURE_TARGET
113 * and should only be used if the automated detection fails.
115 static unsigned int tcc_activation_temp;
117 static unsigned int do_rapl;
118 static double rapl_energy_units;
120 #define RAPL_PKG (1 << 0)
121 /* 0x610 MSR_PKG_POWER_LIMIT */
122 /* 0x611 MSR_PKG_ENERGY_STATUS */
123 #define RAPL_PKG_PERF_STATUS (1 << 1)
124 /* 0x613 MSR_PKG_PERF_STATUS */
125 #define RAPL_PKG_POWER_INFO (1 << 2)
126 /* 0x614 MSR_PKG_POWER_INFO */
128 #define RAPL_DRAM (1 << 3)
129 /* 0x618 MSR_DRAM_POWER_LIMIT */
130 /* 0x619 MSR_DRAM_ENERGY_STATUS */
131 /* 0x61c MSR_DRAM_POWER_INFO */
132 #define RAPL_DRAM_PERF_STATUS (1 << 4)
133 /* 0x61b MSR_DRAM_PERF_STATUS */
135 #define RAPL_CORES (1 << 5)
136 /* 0x638 MSR_PP0_POWER_LIMIT */
137 /* 0x639 MSR_PP0_ENERGY_STATUS */
138 #define RAPL_CORE_POLICY (1 << 6)
139 /* 0x63a MSR_PP0_POLICY */
142 #define RAPL_GFX (1 << 7)
143 /* 0x640 MSR_PP1_POWER_LIMIT */
144 /* 0x641 MSR_PP1_ENERGY_STATUS */
145 /* 0x642 MSR_PP1_POLICY */
146 #define TJMAX_DEFAULT 100
148 cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_saved_affinity_set;
149 size_t cpu_present_setsize, cpu_affinity_setsize, cpu_saved_affinity_setsize;
152 unsigned long long tsc;
153 unsigned long long aperf;
154 unsigned long long mperf;
155 unsigned long long c1;
156 unsigned int smi_count;
159 #define CPU_IS_FIRST_THREAD_IN_CORE 0x2
160 #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
161 } *thread_delta, *thread_even, *thread_odd;
164 unsigned long long c3;
165 unsigned long long c6;
166 unsigned long long c7;
167 unsigned int core_temp_c;
168 unsigned int core_id;
169 } *core_delta, *core_even, *core_odd;
172 unsigned long long pc2;
173 unsigned long long pc3;
174 unsigned long long pc6;
175 unsigned long long pc7;
176 unsigned long long pc8;
177 unsigned long long pc9;
178 unsigned long long pc10;
179 unsigned int package_id;
180 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
181 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
182 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
183 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
184 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
185 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
186 unsigned int tcc_activation_temp;
187 unsigned int pkg_temp_c;
188 } *package_delta, *package_even, *package_odd;
190 #define DELTA_COUNTERS thread_delta, core_delta, package_delta
191 #define ODD_COUNTERS thread_odd, core_odd, package_odd
192 #define EVEN_COUNTERS thread_even, core_even, package_even
193 static _Bool is_even = 1;
195 static _Bool allocated = 0;
196 static _Bool initialized = 0;
198 #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
199 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
200 topo.num_threads_per_core + \
201 (core_no) * topo.num_threads_per_core + (thread_no))
202 #define GET_CORE(core_base, core_no, pkg_no) \
203 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
204 #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
211 int num_cores_per_pkg;
212 int num_threads_per_core;
215 struct timeval tv_even, tv_odd, tv_delta;
220 ERR_CPU_SAVE_SCHED_AFFINITY,
224 ERR_MSR_CORE_C3_RESIDENCY,
225 ERR_MSR_CORE_C6_RESIDENCY,
226 ERR_MSR_CORE_C7_RESIDENCY,
227 ERR_MSR_IA32_THERM_STATUS,
228 ERR_MSR_PKG_C3_RESIDENCY,
229 ERR_MSR_PKG_C6_RESIDENCY,
230 ERR_MSR_PKG_C2_RESIDENCY,
231 ERR_MSR_PKG_C7_RESIDENCY,
232 ERR_MSR_PKG_C8_RESIDENCY,
233 ERR_MSR_PKG_C9_RESIDENCY,
234 ERR_MSR_PKG_C10_RESIDENCY,
235 ERR_MSR_PKG_ENERGY_STATUS,
236 ERR_MSR_PKG_POWER_INFO,
237 ERR_MSR_PP0_ENERGY_STATUS,
238 ERR_MSR_DRAM_ENERGY_STATUS,
239 ERR_MSR_PP1_ENERGY_STATUS,
240 ERR_MSR_PKG_PERF_STATUS,
241 ERR_MSR_DRAM_PERF_STATUS,
242 ERR_MSR_IA32_PACKAGE_THERM_STATUS,
248 ERR_CANT_READ_NUMBER,
249 ERR_CANT_READ_PROC_STAT,
250 ERR_NO_INVARIANT_TSC,
258 static int setup_all_buffers(void);
261 cpu_is_not_present(int cpu)
263 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
266 * run func(thread, core, package) in topology order
267 * skip non-present cpus
270 static int __attribute__((warn_unused_result))
271 for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
272 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
274 int retval, pkg_no, core_no, thread_no;
276 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
277 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
278 for (thread_no = 0; thread_no <
279 topo.num_threads_per_core; ++thread_no) {
280 struct thread_data *t;
284 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
286 if (cpu_is_not_present(t->cpu_id))
289 c = GET_CORE(core_base, core_no, pkg_no);
290 p = GET_PKG(pkg_base, pkg_no);
292 retval = func(t, c, p);
302 * Open a MSR device for reading
303 * Can change the scheduling affinity of the current process if multiple_read is 1
305 static int __attribute__((warn_unused_result))
306 open_msr(int cpu, _Bool multiple_read)
312 * If we need to do multiple read, let's migrate to the CPU
313 * Otherwise, we would lose time calling functions on another CPU
315 * If we are not yet initialized (cpu_affinity_setsize = 0),
316 * we need to skip this optimisation.
318 if (multiple_read && cpu_affinity_setsize) {
319 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
320 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
321 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1) {
322 ERROR("Could not migrate to CPU %d", cpu);
323 return -ERR_CPU_MIGRATE;
327 ssnprintf(pathname, sizeof(pathname), "/dev/cpu/%d/msr", cpu);
328 fd = open(pathname, O_RDONLY);
330 return -ERR_CANT_OPEN_MSR;
335 * Read a single MSR from an open file descriptor
337 static int __attribute__((warn_unused_result))
338 read_msr(int fd, off_t offset, unsigned long long *msr)
342 retval = pread(fd, msr, sizeof *msr, offset);
344 if (retval != sizeof *msr) {
345 ERROR("MSR offset 0x%llx read failed", (unsigned long long)offset);
352 * Open a MSR device for reading, read the value asked for and close it.
353 * This call will not affect the scheduling affinity of this thread.
355 static int __attribute__((warn_unused_result))
356 get_msr(int cpu, off_t offset, unsigned long long *msr)
361 fd = open_msr(cpu, 0);
364 retval = read_msr(fd, offset, msr);
369 #define DELTA_WRAP32(delta, new, old) \
373 delta = 0x100000000 + new - old; \
377 delta_package(struct pkg_data *delta, const struct pkg_data *new, const struct pkg_data *old)
379 delta->pc2 = new->pc2 - old->pc2;
380 delta->pc3 = new->pc3 - old->pc3;
381 delta->pc6 = new->pc6 - old->pc6;
382 delta->pc7 = new->pc7 - old->pc7;
383 delta->pc8 = new->pc8 - old->pc8;
384 delta->pc9 = new->pc9 - old->pc9;
385 delta->pc10 = new->pc10 - old->pc10;
386 delta->pkg_temp_c = new->pkg_temp_c;
388 DELTA_WRAP32(delta->energy_pkg, new->energy_pkg, old->energy_pkg);
389 DELTA_WRAP32(delta->energy_cores, new->energy_cores, old->energy_cores);
390 DELTA_WRAP32(delta->energy_gfx, new->energy_gfx, old->energy_gfx);
391 DELTA_WRAP32(delta->energy_dram, new->energy_dram, old->energy_dram);
392 DELTA_WRAP32(delta->rapl_pkg_perf_status, new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
393 DELTA_WRAP32(delta->rapl_dram_perf_status, new->rapl_dram_perf_status, old->rapl_dram_perf_status);
397 delta_core(struct core_data *delta, const struct core_data *new, const struct core_data *old)
399 delta->c3 = new->c3 - old->c3;
400 delta->c6 = new->c6 - old->c6;
401 delta->c7 = new->c7 - old->c7;
402 delta->core_temp_c = new->core_temp_c;
405 static int __attribute__((warn_unused_result))
406 delta_thread(struct thread_data *delta, const struct thread_data *new, const struct thread_data *old,
407 const struct core_data *core_delta)
409 delta->tsc = new->tsc - old->tsc;
411 /* check for TSC < 1 Mcycles over interval */
412 if (delta->tsc < (1000 * 1000)) {
413 WARNING("Insanely slow TSC rate, TSC stops in idle? ");
414 WARNING("You can disable all c-states by booting with \"idle=poll\" ");
415 WARNING("or just the deep ones with \"processor.max_cstate=1\"");
419 delta->c1 = new->c1 - old->c1;
421 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
422 delta->aperf = new->aperf - old->aperf;
423 delta->mperf = new->mperf - old->mperf;
425 if (!aperf_mperf_unstable) {
426 WARNING(" APERF or MPERF went backwards * ");
427 WARNING("* Frequency results do not cover entire interval *");
428 WARNING("* fix this by running Linux-2.6.30 or later *");
430 aperf_mperf_unstable = 1;
435 * As counter collection is not atomic,
436 * it is possible for mperf's non-halted cycles + idle states
437 * to exceed TSC's all cycles: show c1 = 0% in that case.
439 if ((delta->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > delta->tsc)
442 /* normal case, derive c1 */
443 delta->c1 = delta->tsc - delta->mperf - core_delta->c3
444 - core_delta->c6 - core_delta->c7;
447 if (delta->mperf == 0) {
448 WARNING("cpu%d MPERF 0!", old->cpu_id);
449 delta->mperf = 1; /* divide by 0 protection */
452 delta->smi_count = new->smi_count - old->smi_count;
457 static int __attribute__((warn_unused_result))
458 delta_cpu(struct thread_data *t_delta, struct core_data *c_delta, struct pkg_data *p_delta,
459 const struct thread_data *t_new, const struct core_data *c_new, const struct pkg_data *p_new,
460 const struct thread_data *t_old, const struct core_data *c_old, const struct pkg_data *p_old)
464 /* calculate core delta only for 1st thread in core */
465 if (t_new->flags & CPU_IS_FIRST_THREAD_IN_CORE)
466 delta_core(c_delta, c_new, c_old);
468 /* always calculate thread delta */
469 ret = delta_thread(t_delta, t_new, t_old, c_delta);
473 /* calculate package delta only for 1st core in package */
474 if (t_new->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
475 delta_package(p_delta, p_new, p_old);
484 * acquire and record local counters for that cpu
486 static int __attribute__((warn_unused_result))
487 get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
490 unsigned long long msr;
494 msr_fd = open_msr(cpu, 1);
498 #define READ_MSR(msr, dst) \
500 if (read_msr(msr_fd, msr, dst)) { \
501 retval = -ERR_##msr; \
506 READ_MSR(MSR_IA32_TSC, &t->tsc);
508 READ_MSR(MSR_IA32_APERF, &t->aperf);
509 READ_MSR(MSR_IA32_MPERF, &t->mperf);
511 READ_MSR(MSR_SMI_COUNT, &msr);
512 t->smi_count = msr & 0xFFFFFFFF;
514 /* collect core counters only for 1st thread in core */
515 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) {
520 if (do_core_cstate & (1 << 3))
521 READ_MSR(MSR_CORE_C3_RESIDENCY, &c->c3);
522 if (do_core_cstate & (1 << 6))
523 READ_MSR(MSR_CORE_C6_RESIDENCY, &c->c6);
524 if (do_core_cstate & (1 << 7))
525 READ_MSR(MSR_CORE_C7_RESIDENCY, &c->c7);
528 READ_MSR(MSR_IA32_THERM_STATUS, &msr);
529 c->core_temp_c = p->tcc_activation_temp - ((msr >> 16) & 0x7F);
532 /* collect package counters only for 1st core in package */
533 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
538 if (do_pkg_cstate & (1 << 2))
539 READ_MSR(MSR_PKG_C2_RESIDENCY, &p->pc2);
540 if (do_pkg_cstate & (1 << 3))
541 READ_MSR(MSR_PKG_C3_RESIDENCY, &p->pc3);
542 if (do_pkg_cstate & (1 << 6))
543 READ_MSR(MSR_PKG_C6_RESIDENCY, &p->pc6);
544 if (do_pkg_cstate & (1 << 7))
545 READ_MSR(MSR_PKG_C7_RESIDENCY, &p->pc7);
546 if (do_pkg_cstate & (1 << 8))
547 READ_MSR(MSR_PKG_C8_RESIDENCY, &p->pc8);
548 if (do_pkg_cstate & (1 << 9))
549 READ_MSR(MSR_PKG_C9_RESIDENCY, &p->pc9);
550 if (do_pkg_cstate & (1 << 10))
551 READ_MSR(MSR_PKG_C10_RESIDENCY, &p->pc10);
553 if (do_rapl & RAPL_PKG) {
554 READ_MSR(MSR_PKG_ENERGY_STATUS, &msr);
555 p->energy_pkg = msr & 0xFFFFFFFF;
557 if (do_rapl & RAPL_CORES) {
558 READ_MSR(MSR_PP0_ENERGY_STATUS, &msr);
559 p->energy_cores = msr & 0xFFFFFFFF;
561 if (do_rapl & RAPL_DRAM) {
562 READ_MSR(MSR_DRAM_ENERGY_STATUS, &msr);
563 p->energy_dram = msr & 0xFFFFFFFF;
565 if (do_rapl & RAPL_GFX) {
566 READ_MSR(MSR_PP1_ENERGY_STATUS, &msr);
567 p->energy_gfx = msr & 0xFFFFFFFF;
569 if (do_rapl & RAPL_PKG_PERF_STATUS) {
570 READ_MSR(MSR_PKG_PERF_STATUS, &msr);
571 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
573 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
574 READ_MSR(MSR_DRAM_PERF_STATUS, &msr);
575 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
578 READ_MSR(MSR_IA32_PACKAGE_THERM_STATUS, &msr);
579 p->pkg_temp_c = p->tcc_activation_temp - ((msr >> 16) & 0x7F);
588 free_all_buffers(void)
593 CPU_FREE(cpu_present_set);
594 cpu_present_set = NULL;
597 CPU_FREE(cpu_affinity_set);
598 cpu_affinity_set = NULL;
599 cpu_affinity_setsize = 0;
601 CPU_FREE(cpu_saved_affinity_set);
602 cpu_saved_affinity_set = NULL;
603 cpu_saved_affinity_setsize = 0;
627 package_delta = NULL;
631 * Parse a file containing a single int.
633 static int __attribute__ ((format(printf,1,2)))
634 parse_int_file(const char *fmt, ...)
642 vsnprintf(path, sizeof(path), fmt, args);
644 filep = fopen(path, "r");
646 ERROR("%s: open failed", path);
647 return -ERR_CANT_OPEN_FILE;
649 if (fscanf(filep, "%d", &value) != 1) {
650 ERROR("%s: failed to parse number from file", path);
651 return -ERR_CANT_READ_NUMBER;
658 * cpu_is_first_sibling_in_core(cpu)
659 * return 1 if given CPU is 1st HT sibling in the core
662 cpu_is_first_sibling_in_core(int cpu)
664 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
668 * cpu_is_first_core_in_package(cpu)
669 * return 1 if given CPU is 1st core in package
672 cpu_is_first_core_in_package(int cpu)
674 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
678 get_physical_package_id(int cpu)
680 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
686 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
690 get_num_ht_siblings(int cpu)
698 ssnprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
699 filep = fopen(path, "r");
701 ERROR("%s: open failed", path);
702 return -ERR_CANT_OPEN_FILE;
706 * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4)
707 * otherwinse 1 sibling (self).
709 matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2);
719 static int __attribute__((warn_unused_result))
720 for_all_cpus_delta(const struct thread_data *thread_new_base, const struct core_data *core_new_base, const struct pkg_data *pkg_new_base,
721 const struct thread_data *thread_old_base, const struct core_data *core_old_base, const struct pkg_data *pkg_old_base)
723 int retval, pkg_no, core_no, thread_no;
725 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
726 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
727 for (thread_no = 0; thread_no <
728 topo.num_threads_per_core; ++thread_no) {
729 struct thread_data *t_delta;
730 const struct thread_data *t_old, *t_new;
731 struct core_data *c_delta;
732 const struct core_data *c_old, *c_new;
733 struct pkg_data *p_delta;
734 const struct pkg_data *p_old, *p_new;
736 t_delta = GET_THREAD(thread_delta, thread_no, core_no, pkg_no);
737 t_new = GET_THREAD(thread_new_base, thread_no, core_no, pkg_no);
738 t_old = GET_THREAD(thread_old_base, thread_no, core_no, pkg_no);
739 if (cpu_is_not_present(t_delta->cpu_id))
742 c_delta = GET_CORE(core_delta, core_no, pkg_no);
743 c_new = GET_CORE(core_new_base, core_no, pkg_no);
744 c_old = GET_CORE(core_old_base, core_no, pkg_no);
746 p_delta = GET_PKG(package_delta, pkg_no);
747 p_new = GET_PKG(pkg_new_base, pkg_no);
748 p_old = GET_PKG(pkg_old_base, pkg_no);
750 retval = delta_cpu(t_delta, c_delta, p_delta,
752 t_old, c_old, p_old);
762 * run func(cpu) on every cpu in /proc/stat
763 * return max_cpu number
765 static int __attribute__((warn_unused_result))
766 for_all_proc_cpus(int (func)(int))
772 fp = fopen("/proc/stat", "r");
774 ERROR("Failed to open /proc/stat");
775 return -ERR_CANT_OPEN_FILE;
778 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
780 ERROR("Failed to parse /proc/stat");
782 return -ERR_CANT_READ_PROC_STAT;
786 retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
790 retval = func(cpu_num);
802 * remember the last one seen, it will be the max
807 if (topo.max_cpu_num < cpu)
808 topo.max_cpu_num = cpu;
814 mark_cpu_present(int cpu)
816 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
822 turbostat_submit (const char *plugin_instance,
823 const char *type, const char *type_instance,
826 value_list_t vl = VALUE_LIST_INIT;
832 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
833 sstrncpy (vl.plugin, PLUGIN_NAME, sizeof (vl.plugin));
834 if (plugin_instance != NULL)
835 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
836 sstrncpy (vl.type, type, sizeof (vl.type));
837 if (type_instance != NULL)
838 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
840 plugin_dispatch_values (&vl);
844 * column formatting convention & formats
845 * package: "pk" 2 columns %2d
846 * core: "cor" 3 columns %3d
847 * CPU: "CPU" 3 columns %3d
852 * GHz: "GHz" 3 columns %3.2
853 * TSC: "TSC" 3 columns %3.2
854 * SMI: "SMI" 4 columns %4d
855 * percentage " %pc3" %6.2
856 * Perf Status percentage: %5.2
857 * "CTMP" 4 columns %4d
860 submit_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
863 double interval_float;
865 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
867 ssnprintf(name, sizeof(name), "cpu%02d", t->cpu_id);
869 if (!aperf_mperf_unstable)
870 turbostat_submit(name, "percent", "c0", 100.0 * t->mperf/t->tsc);
871 if (!aperf_mperf_unstable)
872 turbostat_submit(name, "percent", "c1", 100.0 * t->c1/t->tsc);
875 if ((!aperf_mperf_unstable) || (!(t->aperf > t->tsc || t->mperf > t->tsc)))
876 turbostat_submit(NULL, "frequency", name, 1.0 * t->tsc / 1000000000 * t->aperf / t->mperf / interval_float);
879 turbostat_submit(NULL, "current", name, t->smi_count);
881 /* print per-core data only for 1st thread in core */
882 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
885 ssnprintf(name, sizeof(name), "core%02d", c->core_id);
887 if (do_core_cstate & (1 << 3))
888 turbostat_submit(name, "percent", "c3", 100.0 * c->c3/t->tsc);
889 if (do_core_cstate & (1 << 6))
890 turbostat_submit(name, "percent", "c6", 100.0 * c->c6/t->tsc);
891 if (do_core_cstate & (1 << 7))
892 turbostat_submit(name, "percent", "c7", 100.0 * c->c7/t->tsc);
895 turbostat_submit(NULL, "temperature", name, c->core_temp_c);
897 /* print per-package data only for 1st core in package */
898 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
901 ssnprintf(name, sizeof(name), "pkg%02d", p->package_id);
904 turbostat_submit(NULL, "temperature", name, p->pkg_temp_c);
906 if (do_pkg_cstate & (1 << 2))
907 turbostat_submit(name, "percent", "pc2", 100.0 * p->pc2/t->tsc);
908 if (do_pkg_cstate & (1 << 3))
909 turbostat_submit(name, "percent", "pc3", 100.0 * p->pc3/t->tsc);
910 if (do_pkg_cstate & (1 << 6))
911 turbostat_submit(name, "percent", "pc6", 100.0 * p->pc6/t->tsc);
912 if (do_pkg_cstate & (1 << 7))
913 turbostat_submit(name, "percent", "pc7", 100.0 * p->pc7/t->tsc);
914 if (do_pkg_cstate & (1 << 8))
915 turbostat_submit(name, "percent", "pc8", 100.0 * p->pc8/t->tsc);
916 if (do_pkg_cstate & (1 << 9))
917 turbostat_submit(name, "percent", "pc9", 100.0 * p->pc9/t->tsc);
918 if (do_pkg_cstate & (1 << 10))
919 turbostat_submit(name, "percent", "pc10", 100.0 * p->pc10/t->tsc);
922 if (do_rapl & RAPL_PKG)
923 turbostat_submit(name, "power", "Pkg_W", p->energy_pkg * rapl_energy_units / interval_float);
924 if (do_rapl & RAPL_CORES)
925 turbostat_submit(name, "power", "Cor_W", p->energy_cores * rapl_energy_units / interval_float);
926 if (do_rapl & RAPL_GFX)
927 turbostat_submit(name, "power", "GFX_W", p->energy_gfx * rapl_energy_units / interval_float);
928 if (do_rapl & RAPL_DRAM)
929 turbostat_submit(name, "power", "RAM_W", p->energy_dram * rapl_energy_units / interval_float);
936 turbostat_read(user_data_t * not_used)
941 if ((ret = setup_all_buffers()) < 0)
945 if (for_all_proc_cpus(cpu_is_not_present)) {
947 if ((ret = setup_all_buffers()) < 0)
949 if (for_all_proc_cpus(cpu_is_not_present))
950 return -ERR_CPU_NOT_PRESENT;
953 /* Saving the scheduling affinity, as it will be modified by get_counters */
954 if (sched_getaffinity(0, cpu_saved_affinity_setsize, cpu_saved_affinity_set) != 0)
955 return -ERR_CPU_SAVE_SCHED_AFFINITY;
958 if ((ret = for_all_cpus(get_counters, EVEN_COUNTERS)) < 0)
960 gettimeofday(&tv_even, (struct timezone *)NULL);
968 if ((ret = for_all_cpus(get_counters, ODD_COUNTERS)) < 0)
970 gettimeofday(&tv_odd, (struct timezone *)NULL);
972 timersub(&tv_odd, &tv_even, &tv_delta);
973 if ((ret = for_all_cpus_delta(ODD_COUNTERS, EVEN_COUNTERS)) < 0)
975 if ((ret = for_all_cpus(submit_counters, DELTA_COUNTERS)) < 0)
978 if ((ret = for_all_cpus(get_counters, EVEN_COUNTERS)) < 0)
980 gettimeofday(&tv_even, (struct timezone *)NULL);
982 timersub(&tv_even, &tv_odd, &tv_delta);
983 if ((ret = for_all_cpus_delta(EVEN_COUNTERS, ODD_COUNTERS)) < 0)
985 if ((ret = for_all_cpus(submit_counters, DELTA_COUNTERS)) < 0)
991 * Let's restore the affinity
992 * This might fail if the number of CPU changed, but we can't do anything in that case..
994 (void)sched_setaffinity(0, cpu_saved_affinity_setsize, cpu_saved_affinity_set);
998 static int __attribute__((warn_unused_result))
1003 if (stat("/dev/cpu/0/msr", &sb)) {
1004 ERROR("no /dev/cpu/0/msr, try \"# modprobe msr\"");
1010 static int __attribute__((warn_unused_result))
1013 if (getuid() != 0) {
1014 ERROR("must be root");
1015 return -ERR_NOT_ROOT;
1021 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
1022 * the Thermal Control Circuit (TCC) activates.
1023 * This is usually equal to tjMax.
1025 * Older processors do not have this MSR, so there we guess,
1026 * but also allow conficuration over-ride with "TCCActivationTemp".
1028 * Several MSR temperature values are in units of degrees-C
1029 * below this value, including the Digital Thermal Sensor (DTS),
1030 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
1032 static int __attribute__((warn_unused_result))
1033 set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1035 unsigned long long msr;
1036 unsigned int target_c_local;
1038 /* tcc_activation_temp is used only for dts or ptm */
1039 if (!(do_dts || do_ptm))
1042 /* this is a per-package concept */
1043 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1046 if (tcc_activation_temp != 0) {
1047 p->tcc_activation_temp = tcc_activation_temp;
1051 if (get_msr(t->cpu_id, MSR_IA32_TEMPERATURE_TARGET, &msr))
1054 target_c_local = (msr >> 16) & 0x7F;
1056 if (target_c_local < 85 || target_c_local > 127)
1059 p->tcc_activation_temp = target_c_local;
1064 p->tcc_activation_temp = TJMAX_DEFAULT;
1065 WARNING("cpu%d: Guessing tjMax %d C, Please use TCCActivationTemp to specify",
1066 t->cpu_id, p->tcc_activation_temp);
1072 * Identify the functionality of the CPU
1074 static int __attribute__((warn_unused_result))
1077 unsigned int eax, ebx, ecx, edx, max_level;
1078 unsigned int fms, family, model;
1081 * - EAX: Maximum Input Value for Basic CPUID Information
1082 * - EBX: "Genu" (0x756e6547)
1083 * - EDX: "ineI" (0x49656e69)
1084 * - ECX: "ntel" (0x6c65746e)
1086 max_level = ebx = ecx = edx = 0;
1087 __get_cpuid(0, &max_level, &ebx, &ecx, &edx);
1088 if (ebx != 0x756e6547 && edx != 0x49656e69 && ecx != 0x6c65746e) {
1089 ERROR("Unsupported CPU");
1090 return -UNSUPPORTED_CPU;
1094 * - EAX: Version Information: Type, Family, Model, and Stepping ID
1097 * + 12-13: Processor type
1098 * + 16-19: Extended Model ID
1099 * + 20-27: Extended Family ID
1100 * - EDX: Feature Information:
1101 * + 5: Support for MSR read/write operations
1103 fms = ebx = ecx = edx = 0;
1104 __get_cpuid(1, &fms, &ebx, &ecx, &edx);
1105 family = (fms >> 8) & 0xf;
1106 model = (fms >> 4) & 0xf;
1108 family += (fms >> 20) & 0xf;
1109 if (family == 6 || family == 0xf)
1110 model += ((fms >> 16) & 0xf) << 4;
1111 if (!(edx & (1 << 5))) {
1112 ERROR("CPUID: no MSR");
1117 * CPUID(0x80000000):
1118 * - EAX: Maximum Input Value for Extended Function CPUID Information
1120 * This allows us to verify if the CPUID(0x80000007) can be called
1122 * This check is valid for both Intel and AMD.
1124 max_level = ebx = ecx = edx = 0;
1125 __get_cpuid(0x80000000, &max_level, &ebx, &ecx, &edx);
1126 if (max_level < 0x80000007) {
1127 ERROR("CPUID: no invariant TSC (max_level 0x%x)", max_level);
1128 return -ERR_NO_INVARIANT_TSC;
1132 * CPUID(0x80000007):
1134 * + 8: Invariant TSC available if set
1136 * This check is valid for both Intel and AMD
1138 __get_cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
1139 if (!(edx & (1 << 8))) {
1140 ERROR("No invariant TSC");
1141 return -ERR_NO_INVARIANT_TSC;
1147 * + 0: Digital temperature sensor is supported if set
1148 * + 6: Package thermal management is supported if set
1150 * + 0: Hardware Coordination Feedback Capability (Presence of IA32_MPERF and IA32_APERF).
1151 * + 3: The processor supports performance-energy bias preference if set.
1152 * It also implies the presence of a new architectural MSR called IA32_ENERGY_PERF_BIAS
1154 * This check is valid for both Intel and AMD
1156 __get_cpuid(0x6, &eax, &ebx, &ecx, &edx);
1157 do_dts = eax & (1 << 0);
1158 do_ptm = eax & (1 << 6);
1159 if (!(ecx & (1 << 0))) {
1161 return -ERR_NO_APERF;
1165 * Enable or disable C states depending on the model and family
1169 /* Atom (partial) */
1172 do_pkg_cstate = (1 << 2) | (1 << 4) | (1 << 6);
1175 case 0x37: /* BYT */
1177 case 0x4D: /* AVN */
1180 do_core_cstate = (1 << 1) | (1 << 6);
1181 do_pkg_cstate = (1 << 6);
1184 case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
1185 case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
1186 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
1187 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1188 do_core_cstate = (1 << 3) | (1 << 6);
1189 do_pkg_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1192 case 0x25: /* Westmere Client - Clarkdale, Arrandale */
1193 case 0x2C: /* Westmere EP - Gulftown */
1194 case 0x2F: /* Westmere-EX Xeon - Eagleton */
1195 do_core_cstate = (1 << 3) | (1 << 6);
1196 do_pkg_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1199 case 0x2A: /* SNB */
1200 case 0x2D: /* SNB Xeon */
1201 do_core_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1202 do_pkg_cstate = (1 << 2) | (1 << 3) | (1 << 6) | (1 << 7);
1205 case 0x3A: /* IVB */
1206 case 0x3E: /* IVB Xeon */
1207 do_core_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1208 do_pkg_cstate = (1 << 2) | (1 << 3) | (1 << 6) | (1 << 7);
1210 /* Haswell Bridge */
1211 case 0x3C: /* HSW */
1212 case 0x3F: /* HSW */
1213 case 0x46: /* HSW */
1214 do_core_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1215 do_pkg_cstate = (1 << 2) | (1 << 3) | (1 << 6) | (1 << 7);
1217 case 0x45: /* HSW */
1218 do_core_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1219 do_pkg_cstate = (1 << 2) | (1 << 3) | (1 << 6) | (1 << 7) | (1 << 8) | (1 << 9) | (1 << 10);
1222 case 0x4F: /* BDW */
1223 case 0x56: /* BDX-DE */
1224 do_core_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1225 do_pkg_cstate = (1 << 2) | (1 << 3) | (1 << 6) | (1 << 7);
1227 case 0x3D: /* BDW */
1228 do_core_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1229 do_pkg_cstate = (1 << 2) | (1 << 3) | (1 << 6) | (1 << 7) | (1 << 8) | (1 << 9) | (1 << 10);
1232 ERROR("Unsupported CPU");
1240 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_PKG_POWER_INFO | RAPL_GFX;
1243 do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO | RAPL_PKG_PERF_STATUS | RAPL_DRAM | RAPL_DRAM_PERF_STATUS;
1247 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_PKG_POWER_INFO | RAPL_PKG_PERF_STATUS | RAPL_DRAM | RAPL_DRAM_PERF_STATUS;
1251 do_rapl = RAPL_PKG | RAPL_CORES;
1257 ERROR("Unsupported CPU");
1258 return -UNSUPPORTED_CPU;
1263 if (get_msr(0, MSR_RAPL_POWER_UNIT, &msr))
1267 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
1269 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
1277 static int __attribute__((warn_unused_result))
1282 int max_core_id = 0;
1283 int max_package_id = 0;
1284 int max_siblings = 0;
1285 struct cpu_topology {
1287 int physical_package_id;
1290 /* Initialize num_cpus, max_cpu_num */
1292 topo.max_cpu_num = 0;
1293 ret = for_all_proc_cpus(count_cpus);
1297 DEBUG("num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
1299 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
1301 ERROR("calloc cpus");
1306 * Allocate and initialize cpu_present_set
1308 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
1309 if (cpu_present_set == NULL) {
1312 return -ERR_CPU_ALLOC;
1314 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
1315 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
1316 ret = for_all_proc_cpus(mark_cpu_present);
1323 * Allocate and initialize cpu_affinity_set
1325 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
1326 if (cpu_affinity_set == NULL) {
1329 return -ERR_CPU_ALLOC;
1331 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
1332 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
1336 * Allocate and initialize cpu_saved_affinity_set
1338 cpu_saved_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
1339 if (cpu_saved_affinity_set == NULL) {
1342 return -ERR_CPU_ALLOC;
1344 cpu_saved_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
1345 CPU_ZERO_S(cpu_saved_affinity_setsize, cpu_saved_affinity_set);
1350 * find max_core_id, max_package_id
1352 for (i = 0; i <= topo.max_cpu_num; ++i) {
1355 if (cpu_is_not_present(i)) {
1356 WARNING("cpu%d NOT PRESENT", i);
1359 cpus[i].core_id = get_core_id(i);
1360 if (cpus[i].core_id < 0)
1361 return cpus[i].core_id;
1362 if (cpus[i].core_id > max_core_id)
1363 max_core_id = cpus[i].core_id;
1365 cpus[i].physical_package_id = get_physical_package_id(i);
1366 if (cpus[i].physical_package_id < 0)
1367 return cpus[i].physical_package_id;
1368 if (cpus[i].physical_package_id > max_package_id)
1369 max_package_id = cpus[i].physical_package_id;
1371 siblings = get_num_ht_siblings(i);
1374 if (siblings > max_siblings)
1375 max_siblings = siblings;
1376 DEBUG("cpu %d pkg %d core %d\n",
1377 i, cpus[i].physical_package_id, cpus[i].core_id);
1379 topo.num_cores_per_pkg = max_core_id + 1;
1380 DEBUG("max_core_id %d, sizing for %d cores per package\n",
1381 max_core_id, topo.num_cores_per_pkg);
1383 topo.num_packages = max_package_id + 1;
1384 DEBUG("max_package_id %d, sizing for %d packages\n",
1385 max_package_id, topo.num_packages);
1387 topo.num_threads_per_core = max_siblings;
1388 DEBUG("max_siblings %d\n", max_siblings);
1395 allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
1399 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
1400 topo.num_packages, sizeof(struct thread_data));
1404 for (i = 0; i < topo.num_threads_per_core *
1405 topo.num_cores_per_pkg * topo.num_packages; i++)
1406 (*t)[i].cpu_id = -1;
1408 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
1409 sizeof(struct core_data));
1413 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
1414 (*c)[i].core_id = -1;
1416 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
1420 for (i = 0; i < topo.num_packages; i++)
1421 (*p)[i].package_id = i;
1425 ERROR("calloc counters");
1431 * set cpu_id, core_num, pkg_num
1432 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
1434 * increment topo.num_cores when 1st core in pkg seen
1437 init_counter(struct thread_data *thread_base, struct core_data *core_base,
1438 struct pkg_data *pkg_base, int thread_num, int core_num,
1439 int pkg_num, int cpu_id)
1442 struct thread_data *t;
1443 struct core_data *c;
1446 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
1447 c = GET_CORE(core_base, core_num, pkg_num);
1448 p = GET_PKG(pkg_base, pkg_num);
1451 if (thread_num == 0) {
1452 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
1453 if ((ret = cpu_is_first_core_in_package(cpu_id)) < 0) {
1455 } else if (ret != 0) {
1456 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
1460 c->core_id = core_num;
1461 p->package_id = pkg_num;
1468 initialize_counters(int cpu_id)
1470 int my_thread_id, my_core_id, my_package_id;
1473 my_package_id = get_physical_package_id(cpu_id);
1474 if (my_package_id < 0)
1475 return my_package_id;
1476 my_core_id = get_core_id(cpu_id);
1480 if ((ret = cpu_is_first_sibling_in_core(cpu_id)) < 0) {
1482 } else if (ret != 0) {
1489 ret = init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
1492 ret = init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
1495 ret = init_counter(DELTA_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
1501 #define DO_OR_GOTO_ERR(something) \
1503 ret = (something); \
1508 static int setup_all_buffers(void)
1512 DO_OR_GOTO_ERR(topology_probe());
1513 DO_OR_GOTO_ERR(allocate_counters(&thread_even, &core_even, &package_even));
1514 DO_OR_GOTO_ERR(allocate_counters(&thread_odd, &core_odd, &package_odd));
1515 DO_OR_GOTO_ERR(allocate_counters(&thread_delta, &core_delta, &package_delta));
1516 DO_OR_GOTO_ERR(for_all_proc_cpus(initialize_counters));
1517 DO_OR_GOTO_ERR(for_all_cpus(set_temperature_target, EVEN_COUNTERS));
1518 DO_OR_GOTO_ERR(for_all_cpus(set_temperature_target, ODD_COUNTERS));
1528 turbostat_init(void)
1532 DO_OR_GOTO_ERR(check_super_user());
1533 DO_OR_GOTO_ERR(probe_cpu());
1534 DO_OR_GOTO_ERR(check_dev_msr());
1535 DO_OR_GOTO_ERR(setup_all_buffers());
1537 plugin_register_complex_read(NULL, PLUGIN_NAME, turbostat_read, NULL, NULL);
1545 static const char *config_keys[] =
1547 "TCCActivationTemp",
1549 static const int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
1552 turbostat_config(const char *key, const char *value)
1554 long unsigned int tmp_val;
1557 if (strcasecmp("TCCActivationTemp", key) == 0) {
1558 tmp_val = strtoul(value, &end, 0);
1559 if (*end != '\0' || tmp_val > UINT_MAX)
1561 tcc_activation_temp = (unsigned int) tmp_val;
1568 void module_register(void);
1569 void module_register(void)
1571 plugin_register_init(PLUGIN_NAME, turbostat_init);
1572 plugin_register_config(PLUGIN_NAME, turbostat_config, config_keys, config_keys_num);