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:
41 #include "utils_time.h"
43 #include <asm/msr-index.h>
48 #include <sys/types.h>
51 #include <sys/resource.h>
60 #include <sys/capability.h>
62 #define PLUGIN_NAME "turbostat"
65 * This tool uses the Model-Specific Registers (MSRs) present on Intel processors.
66 * The general description each of these registers, depending on the architecture,
67 * can be found in the IntelĀ® 64 and IA-32 Architectures Software Developer Manual,
68 * Volume 3 Chapter 35.
72 * If set, aperf_mperf_unstable disables a/mperf based stats.
73 * This includes: C0 & C1 states, frequency
75 * This value is automatically set if mperf or aperf go backward
77 static _Bool aperf_mperf_unstable;
80 * Bitmask of the list of core C states supported by the processor.
81 * Currently supported C-states (by this plugin): 3, 6, 7
83 static unsigned int do_core_cstate;
86 * Bitmask of the list of pacages C states supported by the processor.
87 * Currently supported C-states (by this plugin): 2, 3, 6, 7, 8, 9, 10
89 static unsigned int do_pkg_cstate;
92 * Boolean indicating if the processor supports 'I/O System-Management Interrupt counter'
97 * Boolean indicating if the processor supports 'Digital temperature sensor'
98 * This feature enables the monitoring of the temperature of each core
100 * This feature has two limitations:
101 * - if MSR_IA32_TEMPERATURE_TARGET is not supported, the absolute temperature might be wrong
102 * - Temperatures above the tcc_activation_temp are not recorded
107 * Boolean indicating if the processor supports 'Package thermal management'
108 * This feature allows the monitoring of the temperature of each package
110 * This feature has two limitations:
111 * - if MSR_IA32_TEMPERATURE_TARGET is not supported, the absolute temperature might be wrong
112 * - Temperatures above the tcc_activation_temp are not recorded
117 * Thermal Control Circuit Activation Temperature as configured by the user.
118 * This override the automated detection via MSR_IA32_TEMPERATURE_TARGET
119 * and should only be used if the automated detection fails.
121 static unsigned int tcc_activation_temp;
123 static unsigned int do_rapl;
124 static double rapl_energy_units;
126 #define RAPL_PKG (1 << 0)
127 /* 0x610 MSR_PKG_POWER_LIMIT */
128 /* 0x611 MSR_PKG_ENERGY_STATUS */
129 #define RAPL_PKG_PERF_STATUS (1 << 1)
130 /* 0x613 MSR_PKG_PERF_STATUS */
131 #define RAPL_PKG_POWER_INFO (1 << 2)
132 /* 0x614 MSR_PKG_POWER_INFO */
134 #define RAPL_DRAM (1 << 3)
135 /* 0x618 MSR_DRAM_POWER_LIMIT */
136 /* 0x619 MSR_DRAM_ENERGY_STATUS */
137 /* 0x61c MSR_DRAM_POWER_INFO */
138 #define RAPL_DRAM_PERF_STATUS (1 << 4)
139 /* 0x61b MSR_DRAM_PERF_STATUS */
141 #define RAPL_CORES (1 << 5)
142 /* 0x638 MSR_PP0_POWER_LIMIT */
143 /* 0x639 MSR_PP0_ENERGY_STATUS */
144 #define RAPL_CORE_POLICY (1 << 6)
145 /* 0x63a MSR_PP0_POLICY */
148 #define RAPL_GFX (1 << 7)
149 /* 0x640 MSR_PP1_POWER_LIMIT */
150 /* 0x641 MSR_PP1_ENERGY_STATUS */
151 /* 0x642 MSR_PP1_POLICY */
152 #define TJMAX_DEFAULT 100
154 static cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_saved_affinity_set;
155 static size_t cpu_present_setsize, cpu_affinity_setsize, cpu_saved_affinity_setsize;
157 static struct thread_data {
158 unsigned long long tsc;
159 unsigned long long aperf;
160 unsigned long long mperf;
161 unsigned long long c1;
162 unsigned int smi_count;
165 #define CPU_IS_FIRST_THREAD_IN_CORE 0x2
166 #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
167 } *thread_delta, *thread_even, *thread_odd;
169 static struct core_data {
170 unsigned long long c3;
171 unsigned long long c6;
172 unsigned long long c7;
173 unsigned int core_temp_c;
174 unsigned int core_id;
175 } *core_delta, *core_even, *core_odd;
177 static struct pkg_data {
178 unsigned long long pc2;
179 unsigned long long pc3;
180 unsigned long long pc6;
181 unsigned long long pc7;
182 unsigned long long pc8;
183 unsigned long long pc9;
184 unsigned long long pc10;
185 unsigned int package_id;
186 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
187 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
188 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
189 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
190 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
191 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
192 unsigned int tcc_activation_temp;
193 unsigned int pkg_temp_c;
194 } *package_delta, *package_even, *package_odd;
196 #define DELTA_COUNTERS thread_delta, core_delta, package_delta
197 #define ODD_COUNTERS thread_odd, core_odd, package_odd
198 #define EVEN_COUNTERS thread_even, core_even, package_even
199 static _Bool is_even = 1;
201 static _Bool allocated = 0;
202 static _Bool initialized = 0;
204 #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
206 (pkg_no) * topology.num_cores * topology.num_threads + \
207 (core_no) * topology.num_threads + \
209 #define GET_CORE(core_base, core_no, pkg_no) \
211 (pkg_no) * topology.num_cores + \
213 #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
215 struct cpu_topology {
218 _Bool first_core_in_package;
219 _Bool first_thread_in_core;
222 static struct topology {
227 struct cpu_topology *cpus;
230 static cdtime_t time_even, time_odd, time_delta;
232 static const char *config_keys[] =
236 static const int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
238 /*****************************
239 * MSR Manipulation helpers *
240 *****************************/
243 * Open a MSR device for reading
244 * Can change the scheduling affinity of the current process if multiple_read is 1
246 static int __attribute__((warn_unused_result))
247 open_msr(int cpu, _Bool multiple_read)
253 * If we need to do multiple read, let's migrate to the CPU
254 * Otherwise, we would lose time calling functions on another CPU
256 * If we are not yet initialized (cpu_affinity_setsize = 0),
257 * we need to skip this optimisation.
259 if (multiple_read && cpu_affinity_setsize) {
260 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
261 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
262 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1) {
263 ERROR("Turbostat plugin: Could not migrate to CPU %d", cpu);
268 ssnprintf(pathname, sizeof(pathname), "/dev/cpu/%d/msr", cpu);
269 fd = open(pathname, O_RDONLY);
271 ERROR("Turbostat plugin: failed to open %s", pathname);
278 * Read a single MSR from an open file descriptor
280 static int __attribute__((warn_unused_result))
281 read_msr(int fd, off_t offset, unsigned long long *msr)
285 retval = pread(fd, msr, sizeof *msr, offset);
287 if (retval != sizeof *msr) {
288 ERROR("Turbostat plugin: MSR offset 0x%llx read failed",
289 (unsigned long long)offset);
296 * Open a MSR device for reading, read the value asked for and close it.
297 * This call will not affect the scheduling affinity of this thread.
299 static int __attribute__((warn_unused_result))
300 get_msr(int cpu, off_t offset, unsigned long long *msr)
305 fd = open_msr(cpu, 0);
308 retval = read_msr(fd, offset, msr);
314 /********************************
315 * Raw data acquisition (1 CPU) *
316 ********************************/
319 * Read every data avalaible for a single CPU
321 * Core data is shared for all threads in one core: extracted only for the first thread
322 * Package data is shared for all core in one package: extracted only for the first thread of the first core
324 * Side effect: migrates to the targeted CPU
326 static int __attribute__((warn_unused_result))
327 get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
330 unsigned long long msr;
334 msr_fd = open_msr(cpu, 1);
338 #define READ_MSR(msr, dst) \
340 if (read_msr(msr_fd, msr, dst)) { \
341 ERROR("Turbostat plugin: Unable to read " #msr); \
347 READ_MSR(MSR_IA32_TSC, &t->tsc);
349 READ_MSR(MSR_IA32_APERF, &t->aperf);
350 READ_MSR(MSR_IA32_MPERF, &t->mperf);
353 READ_MSR(MSR_SMI_COUNT, &msr);
354 t->smi_count = msr & 0xFFFFFFFF;
357 /* collect core counters only for 1st thread in core */
358 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) {
363 if (do_core_cstate & (1 << 3))
364 READ_MSR(MSR_CORE_C3_RESIDENCY, &c->c3);
365 if (do_core_cstate & (1 << 6))
366 READ_MSR(MSR_CORE_C6_RESIDENCY, &c->c6);
367 if (do_core_cstate & (1 << 7))
368 READ_MSR(MSR_CORE_C7_RESIDENCY, &c->c7);
371 READ_MSR(MSR_IA32_THERM_STATUS, &msr);
372 c->core_temp_c = p->tcc_activation_temp - ((msr >> 16) & 0x7F);
375 /* collect package counters only for 1st core in package */
376 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
381 if (do_pkg_cstate & (1 << 2))
382 READ_MSR(MSR_PKG_C2_RESIDENCY, &p->pc2);
383 if (do_pkg_cstate & (1 << 3))
384 READ_MSR(MSR_PKG_C3_RESIDENCY, &p->pc3);
385 if (do_pkg_cstate & (1 << 6))
386 READ_MSR(MSR_PKG_C6_RESIDENCY, &p->pc6);
387 if (do_pkg_cstate & (1 << 7))
388 READ_MSR(MSR_PKG_C7_RESIDENCY, &p->pc7);
389 if (do_pkg_cstate & (1 << 8))
390 READ_MSR(MSR_PKG_C8_RESIDENCY, &p->pc8);
391 if (do_pkg_cstate & (1 << 9))
392 READ_MSR(MSR_PKG_C9_RESIDENCY, &p->pc9);
393 if (do_pkg_cstate & (1 << 10))
394 READ_MSR(MSR_PKG_C10_RESIDENCY, &p->pc10);
396 if (do_rapl & RAPL_PKG) {
397 READ_MSR(MSR_PKG_ENERGY_STATUS, &msr);
398 p->energy_pkg = msr & 0xFFFFFFFF;
400 if (do_rapl & RAPL_CORES) {
401 READ_MSR(MSR_PP0_ENERGY_STATUS, &msr);
402 p->energy_cores = msr & 0xFFFFFFFF;
404 if (do_rapl & RAPL_DRAM) {
405 READ_MSR(MSR_DRAM_ENERGY_STATUS, &msr);
406 p->energy_dram = msr & 0xFFFFFFFF;
408 if (do_rapl & RAPL_GFX) {
409 READ_MSR(MSR_PP1_ENERGY_STATUS, &msr);
410 p->energy_gfx = msr & 0xFFFFFFFF;
412 if (do_rapl & RAPL_PKG_PERF_STATUS) {
413 READ_MSR(MSR_PKG_PERF_STATUS, &msr);
414 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
416 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
417 READ_MSR(MSR_DRAM_PERF_STATUS, &msr);
418 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
421 READ_MSR(MSR_IA32_PACKAGE_THERM_STATUS, &msr);
422 p->pkg_temp_c = p->tcc_activation_temp - ((msr >> 16) & 0x7F);
431 /**********************************
432 * Evaluating the changes (1 CPU) *
433 **********************************/
436 * Do delta = new - old on 32bits cyclique intergers
438 #define DELTA_WRAP32(delta, new, old) \
442 delta = 0x100000000 + new - old; \
446 * Extract the evolution old->new in delta at a package level
447 * (some are not new-delta, e.g. temperature)
450 delta_package(struct pkg_data *delta, const struct pkg_data *new, const struct pkg_data *old)
452 delta->pc2 = new->pc2 - old->pc2;
453 delta->pc3 = new->pc3 - old->pc3;
454 delta->pc6 = new->pc6 - old->pc6;
455 delta->pc7 = new->pc7 - old->pc7;
456 delta->pc8 = new->pc8 - old->pc8;
457 delta->pc9 = new->pc9 - old->pc9;
458 delta->pc10 = new->pc10 - old->pc10;
459 delta->pkg_temp_c = new->pkg_temp_c;
461 DELTA_WRAP32(delta->energy_pkg, new->energy_pkg, old->energy_pkg);
462 DELTA_WRAP32(delta->energy_cores, new->energy_cores, old->energy_cores);
463 DELTA_WRAP32(delta->energy_gfx, new->energy_gfx, old->energy_gfx);
464 DELTA_WRAP32(delta->energy_dram, new->energy_dram, old->energy_dram);
465 DELTA_WRAP32(delta->rapl_pkg_perf_status, new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
466 DELTA_WRAP32(delta->rapl_dram_perf_status, new->rapl_dram_perf_status, old->rapl_dram_perf_status);
470 * Extract the evolution old->new in delta at a core level
471 * (some are not new-delta, e.g. temperature)
474 delta_core(struct core_data *delta, const struct core_data *new, const struct core_data *old)
476 delta->c3 = new->c3 - old->c3;
477 delta->c6 = new->c6 - old->c6;
478 delta->c7 = new->c7 - old->c7;
479 delta->core_temp_c = new->core_temp_c;
483 * Extract the evolution old->new in delta at a package level
484 * core_delta is required for c1 estimation (tsc - c0 - all core cstates)
486 static inline int __attribute__((warn_unused_result))
487 delta_thread(struct thread_data *delta, const struct thread_data *new, const struct thread_data *old,
488 const struct core_data *core_delta)
490 delta->tsc = new->tsc - old->tsc;
492 /* check for TSC < 1 Mcycles over interval */
493 if (delta->tsc < (1000 * 1000)) {
494 WARNING("Turbostat plugin: Insanely slow TSC rate, TSC stops "
495 "in idle? You can disable all c-states by booting with"
496 " 'idle=poll' or just the deep ones with"
497 " 'processor.max_cstate=1'");
501 delta->c1 = new->c1 - old->c1;
503 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
504 delta->aperf = new->aperf - old->aperf;
505 delta->mperf = new->mperf - old->mperf;
507 if (!aperf_mperf_unstable) {
508 WARNING("Turbostat plugin: APERF or MPERF went "
509 "backwards. Frequency results do not cover "
510 "the entire interval. Fix this by running "
511 "Linux-2.6.30 or later.");
513 aperf_mperf_unstable = 1;
518 * As counter collection is not atomic,
519 * it is possible for mperf's non-halted cycles + idle states
520 * to exceed TSC's all cycles: show c1 = 0% in that case.
522 if ((delta->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > delta->tsc)
525 /* normal case, derive c1 */
526 delta->c1 = delta->tsc - delta->mperf - core_delta->c3
527 - core_delta->c6 - core_delta->c7;
530 if (delta->mperf == 0) {
531 WARNING("Turbostat plugin: cpu%d MPERF 0!", old->cpu_id);
532 delta->mperf = 1; /* divide by 0 protection */
536 delta->smi_count = new->smi_count - old->smi_count;
541 /**********************************
542 * Submitting the results (1 CPU) *
543 **********************************/
546 * Submit one gauge value
549 turbostat_submit (const char *plugin_instance,
550 const char *type, const char *type_instance,
553 value_list_t vl = VALUE_LIST_INIT;
559 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
560 sstrncpy (vl.plugin, PLUGIN_NAME, sizeof (vl.plugin));
561 if (plugin_instance != NULL)
562 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
563 sstrncpy (vl.type, type, sizeof (vl.type));
564 if (type_instance != NULL)
565 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
567 plugin_dispatch_values (&vl);
571 * Submit every data for a single CPU
573 * Core data is shared for all threads in one core: submitted only for the first thread
574 * Package data is shared for all core in one package: submitted only for the first thread of the first core
577 submit_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
579 char name[DATA_MAX_NAME_LEN];
580 double interval_float;
582 interval_float = CDTIME_T_TO_DOUBLE(time_delta);
584 ssnprintf(name, sizeof(name), "cpu%02d", t->cpu_id);
586 if (!aperf_mperf_unstable)
587 turbostat_submit(name, "percent", "c0", 100.0 * t->mperf/t->tsc);
588 if (!aperf_mperf_unstable)
589 turbostat_submit(name, "percent", "c1", 100.0 * t->c1/t->tsc);
591 turbostat_submit("Average", "frequency", name, 1.0 / 1000000 * t->aperf / interval_float);
593 if ((!aperf_mperf_unstable) || (!(t->aperf > t->tsc || t->mperf > t->tsc)))
594 turbostat_submit("Buzy", "frequency", name, 1.0 * t->tsc / 1000000 * t->aperf / t->mperf / interval_float);
596 /* Sanity check (should stay stable) */
597 turbostat_submit("TSC", "gauge", name, 1.0 * t->tsc / 1000000 / interval_float);
601 turbostat_submit(NULL, "current", name, t->smi_count);
603 /* submit per-core data only for 1st thread in core */
604 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
607 ssnprintf(name, sizeof(name), "core%02d", c->core_id);
609 if (do_core_cstate & (1 << 3))
610 turbostat_submit(name, "percent", "c3", 100.0 * c->c3/t->tsc);
611 if (do_core_cstate & (1 << 6))
612 turbostat_submit(name, "percent", "c6", 100.0 * c->c6/t->tsc);
613 if (do_core_cstate & (1 << 7))
614 turbostat_submit(name, "percent", "c7", 100.0 * c->c7/t->tsc);
617 turbostat_submit(NULL, "temperature", name, c->core_temp_c);
619 /* submit per-package data only for 1st core in package */
620 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
623 ssnprintf(name, sizeof(name), "pkg%02d", p->package_id);
626 turbostat_submit(NULL, "temperature", name, p->pkg_temp_c);
628 if (do_pkg_cstate & (1 << 2))
629 turbostat_submit(name, "percent", "pc2", 100.0 * p->pc2/t->tsc);
630 if (do_pkg_cstate & (1 << 3))
631 turbostat_submit(name, "percent", "pc3", 100.0 * p->pc3/t->tsc);
632 if (do_pkg_cstate & (1 << 6))
633 turbostat_submit(name, "percent", "pc6", 100.0 * p->pc6/t->tsc);
634 if (do_pkg_cstate & (1 << 7))
635 turbostat_submit(name, "percent", "pc7", 100.0 * p->pc7/t->tsc);
636 if (do_pkg_cstate & (1 << 8))
637 turbostat_submit(name, "percent", "pc8", 100.0 * p->pc8/t->tsc);
638 if (do_pkg_cstate & (1 << 9))
639 turbostat_submit(name, "percent", "pc9", 100.0 * p->pc9/t->tsc);
640 if (do_pkg_cstate & (1 << 10))
641 turbostat_submit(name, "percent", "pc10", 100.0 * p->pc10/t->tsc);
644 if (do_rapl & RAPL_PKG)
645 turbostat_submit(name, "power", "Pkg_W", p->energy_pkg * rapl_energy_units / interval_float);
646 if (do_rapl & RAPL_CORES)
647 turbostat_submit(name, "power", "Cor_W", p->energy_cores * rapl_energy_units / interval_float);
648 if (do_rapl & RAPL_GFX)
649 turbostat_submit(name, "power", "GFX_W", p->energy_gfx * rapl_energy_units / interval_float);
650 if (do_rapl & RAPL_DRAM)
651 turbostat_submit(name, "power", "RAM_W", p->energy_dram * rapl_energy_units / interval_float);
658 /**********************************
659 * Looping function over all CPUs *
660 **********************************/
663 * Check if a given cpu id is in our compiled list of existing CPUs
666 cpu_is_not_present(int cpu)
668 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
672 * Loop on all CPUs in topological order
674 * Skip non-present cpus
675 * Return the error code at the first error or 0
677 static int __attribute__((warn_unused_result))
678 for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
679 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
681 int retval, pkg_no, core_no, thread_no;
683 for (pkg_no = 0; pkg_no < topology.num_packages; ++pkg_no) {
684 for (core_no = 0; core_no < topology.num_cores; ++core_no) {
685 for (thread_no = 0; thread_no < topology.num_threads; ++thread_no) {
686 struct thread_data *t;
690 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
692 if (cpu_is_not_present(t->cpu_id))
695 c = GET_CORE(core_base, core_no, pkg_no);
696 p = GET_PKG(pkg_base, pkg_no);
698 retval = func(t, c, p);
708 * Dedicated loop: Extract every data evolution for all CPU
710 * Skip non-present cpus
711 * Return the error code at the first error or 0
713 * Core data is shared for all threads in one core: extracted only for the first thread
714 * Package data is shared for all core in one package: extracted only for the first thread of the first core
716 static int __attribute__((warn_unused_result))
717 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,
718 const struct thread_data *thread_old_base, const struct core_data *core_old_base, const struct pkg_data *pkg_old_base)
720 int retval, pkg_no, core_no, thread_no;
722 for (pkg_no = 0; pkg_no < topology.num_packages; ++pkg_no) {
723 for (core_no = 0; core_no < topology.num_cores; ++core_no) {
724 for (thread_no = 0; thread_no < topology.num_threads; ++thread_no) {
725 struct thread_data *t_delta;
726 const struct thread_data *t_old, *t_new;
727 struct core_data *c_delta;
729 /* Get correct pointers for threads */
730 t_delta = GET_THREAD(thread_delta, thread_no, core_no, pkg_no);
731 t_new = GET_THREAD(thread_new_base, thread_no, core_no, pkg_no);
732 t_old = GET_THREAD(thread_old_base, thread_no, core_no, pkg_no);
734 /* Skip threads that disappeared */
735 if (cpu_is_not_present(t_delta->cpu_id))
738 /* c_delta is always required for delta_thread */
739 c_delta = GET_CORE(core_delta, core_no, pkg_no);
741 /* calculate core delta only for 1st thread in core */
742 if (t_new->flags & CPU_IS_FIRST_THREAD_IN_CORE) {
743 const struct core_data *c_old, *c_new;
745 c_new = GET_CORE(core_new_base, core_no, pkg_no);
746 c_old = GET_CORE(core_old_base, core_no, pkg_no);
748 delta_core(c_delta, c_new, c_old);
751 /* Always calculate thread delta */
752 retval = delta_thread(t_delta, t_new, t_old, c_delta);
756 /* calculate package delta only for 1st core in package */
757 if (t_new->flags & CPU_IS_FIRST_CORE_IN_PACKAGE) {
758 struct pkg_data *p_delta;
759 const struct pkg_data *p_old, *p_new;
761 p_delta = GET_PKG(package_delta, pkg_no);
762 p_new = GET_PKG(pkg_new_base, pkg_no);
763 p_old = GET_PKG(pkg_old_base, pkg_no);
765 delta_package(p_delta, p_new, p_old);
779 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
780 * the Thermal Control Circuit (TCC) activates.
781 * This is usually equal to tjMax.
783 * Older processors do not have this MSR, so there we guess,
784 * but also allow conficuration over-ride with "TCCActivationTemp".
786 * Several MSR temperature values are in units of degrees-C
787 * below this value, including the Digital Thermal Sensor (DTS),
788 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
790 static int __attribute__((warn_unused_result))
791 set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
793 unsigned long long msr;
794 unsigned int target_c_local;
796 /* tcc_activation_temp is used only for dts or ptm */
797 if (!(do_dts || do_ptm))
800 /* this is a per-package concept */
801 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
804 if (tcc_activation_temp != 0) {
805 p->tcc_activation_temp = tcc_activation_temp;
809 if (get_msr(t->cpu_id, MSR_IA32_TEMPERATURE_TARGET, &msr))
812 target_c_local = (msr >> 16) & 0xFF;
817 p->tcc_activation_temp = target_c_local;
822 p->tcc_activation_temp = TJMAX_DEFAULT;
823 WARNING("Turbostat plugin: cpu%d: Guessing tjMax %d C,"
824 " Please use TCCActivationTemp to specify it.",
825 t->cpu_id, p->tcc_activation_temp);
831 * Identify the functionality of the CPU
833 static int __attribute__((warn_unused_result))
836 unsigned int eax, ebx, ecx, edx, max_level;
837 unsigned int fms, family, model;
840 * - EAX: Maximum Input Value for Basic CPUID Information
841 * - EBX: "Genu" (0x756e6547)
842 * - EDX: "ineI" (0x49656e69)
843 * - ECX: "ntel" (0x6c65746e)
845 max_level = ebx = ecx = edx = 0;
846 __get_cpuid(0, &max_level, &ebx, &ecx, &edx);
847 if (ebx != 0x756e6547 && edx != 0x49656e69 && ecx != 0x6c65746e) {
848 ERROR("Turbostat plugin: Unsupported CPU (not Intel)");
853 * - EAX: Version Information: Type, Family, Model, and Stepping ID
856 * + 12-13: Processor type
857 * + 16-19: Extended Model ID
858 * + 20-27: Extended Family ID
859 * - EDX: Feature Information:
860 * + 5: Support for MSR read/write operations
862 fms = ebx = ecx = edx = 0;
863 __get_cpuid(1, &fms, &ebx, &ecx, &edx);
864 family = (fms >> 8) & 0xf;
865 model = (fms >> 4) & 0xf;
867 family += (fms >> 20) & 0xf;
868 if (family == 6 || family == 0xf)
869 model += ((fms >> 16) & 0xf) << 4;
870 if (!(edx & (1 << 5))) {
871 ERROR("Turbostat plugin: Unsupported CPU (no MSR support)");
878 * + 0: Digital temperature sensor is supported if set
879 * + 6: Package thermal management is supported if set
881 * + 0: Hardware Coordination Feedback Capability (Presence of IA32_MPERF and IA32_APERF).
882 * + 3: The processor supports performance-energy bias preference if set.
883 * It also implies the presence of a new architectural MSR called IA32_ENERGY_PERF_BIAS
885 * This check is valid for both Intel and AMD
887 eax = ebx = ecx = edx = 0;
888 __get_cpuid(0x6, &eax, &ebx, &ecx, &edx);
889 do_dts = eax & (1 << 0);
890 do_ptm = eax & (1 << 6);
891 if (!(ecx & (1 << 0))) {
892 ERROR("Turbostat plugin: Unsupported CPU (No APERF)");
897 * Enable or disable C states depending on the model and family
905 do_pkg_cstate = (1 << 2) | (1 << 4) | (1 << 6);
911 do_core_cstate = (1 << 1) | (1 << 6);
912 do_pkg_cstate = (1 << 6);
915 case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
916 case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
917 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
918 case 0x2E: /* Nehalem-EX Xeon - Beckton */
920 do_core_cstate = (1 << 3) | (1 << 6);
921 do_pkg_cstate = (1 << 3) | (1 << 6) | (1 << 7);
924 case 0x25: /* Westmere Client - Clarkdale, Arrandale */
925 case 0x2C: /* Westmere EP - Gulftown */
926 case 0x2F: /* Westmere-EX Xeon - Eagleton */
928 do_core_cstate = (1 << 3) | (1 << 6);
929 do_pkg_cstate = (1 << 3) | (1 << 6) | (1 << 7);
933 case 0x2D: /* SNB Xeon */
935 do_core_cstate = (1 << 3) | (1 << 6) | (1 << 7);
936 do_pkg_cstate = (1 << 2) | (1 << 3) | (1 << 6) | (1 << 7);
940 case 0x3E: /* IVB Xeon */
942 do_core_cstate = (1 << 3) | (1 << 6) | (1 << 7);
943 do_pkg_cstate = (1 << 2) | (1 << 3) | (1 << 6) | (1 << 7);
950 do_core_cstate = (1 << 3) | (1 << 6) | (1 << 7);
951 do_pkg_cstate = (1 << 2) | (1 << 3) | (1 << 6) | (1 << 7);
955 do_core_cstate = (1 << 3) | (1 << 6) | (1 << 7);
956 do_pkg_cstate = (1 << 2) | (1 << 3) | (1 << 6) | (1 << 7) | (1 << 8) | (1 << 9) | (1 << 10);
960 case 0x56: /* BDX-DE */
962 do_core_cstate = (1 << 3) | (1 << 6) | (1 << 7);
963 do_pkg_cstate = (1 << 2) | (1 << 3) | (1 << 6) | (1 << 7);
967 do_core_cstate = (1 << 3) | (1 << 6) | (1 << 7);
968 do_pkg_cstate = (1 << 2) | (1 << 3) | (1 << 6) | (1 << 7) | (1 << 8) | (1 << 9) | (1 << 10);
983 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_PKG_POWER_INFO | RAPL_GFX;
987 case 0x56: /* BDX-DE */
988 do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO | RAPL_PKG_PERF_STATUS | RAPL_DRAM | RAPL_DRAM_PERF_STATUS;
990 case 0x2D: /* SNB Xeon */
991 case 0x3E: /* IVB Xeon */
992 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_PKG_POWER_INFO | RAPL_PKG_PERF_STATUS | RAPL_DRAM | RAPL_DRAM_PERF_STATUS;
996 do_rapl = RAPL_PKG | RAPL_CORES;
1002 ERROR("Turbostat plugin: Unsupported CPU (family: %#x, "
1003 "model: %#x)", family, model);
1008 unsigned long long msr;
1009 if (get_msr(0, MSR_RAPL_POWER_UNIT, &msr))
1013 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
1015 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
1022 /********************
1023 * Topology Probing *
1024 ********************/
1027 * Read a single int from a file.
1029 static int __attribute__ ((format(printf,1,2)))
1030 parse_int_file(const char *fmt, ...)
1033 char path[PATH_MAX];
1037 va_start(args, fmt);
1038 len = vsnprintf(path, sizeof(path), fmt, args);
1040 if (len < 0 || len >= PATH_MAX) {
1041 ERROR("Turbostat plugin: path truncated: '%s'", path);
1045 filep = fopen(path, "r");
1047 ERROR("Turbostat plugin: Failed to open '%s'", path);
1050 if (fscanf(filep, "%d", &value) != 1) {
1051 ERROR("Turbostat plugin: Failed to parse number from '%s'", path);
1059 get_threads_on_core(int cpu)
1067 ssnprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
1068 filep = fopen(path, "r");
1070 ERROR("Turbostat plugin: Failed to open '%s'", path);
1075 * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4)
1076 * otherwinse 1 sibling (self).
1078 matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2);
1089 * run func(cpu) on every cpu in /proc/stat
1090 * return max_cpu number
1092 static int __attribute__((warn_unused_result))
1093 for_all_proc_cpus(int (func)(int))
1099 fp = fopen("/proc/stat", "r");
1101 ERROR("Turbostat plugin: Failed to open /proc/stat");
1105 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
1107 ERROR("Turbostat plugin: Failed to parse /proc/stat");
1113 retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
1117 retval = func(cpu_num);
1128 * Update the stored topology.max_cpu_id
1131 update_max_cpu_id(int cpu)
1133 if (topology.max_cpu_id < cpu)
1134 topology.max_cpu_id = cpu;
1139 mark_cpu_present(int cpu)
1141 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
1145 static int __attribute__((warn_unused_result))
1146 allocate_cpu_set(cpu_set_t ** set, size_t * size) {
1147 *set = CPU_ALLOC(topology.max_cpu_id + 1);
1149 ERROR("Turbostat plugin: Unable to allocate CPU state");
1152 *size = CPU_ALLOC_SIZE(topology.max_cpu_id + 1);
1153 CPU_ZERO_S(*size, *set);
1158 * Build a local representation of the cpu distribution
1160 static int __attribute__((warn_unused_result))
1165 int max_package_id, max_core_id, max_thread_id;
1166 max_package_id = max_core_id = max_thread_id = 0;
1168 /* Clean topology */
1169 free(topology.cpus);
1170 memset(&topology, 0, sizeof(topology));
1172 ret = for_all_proc_cpus(update_max_cpu_id);
1176 topology.cpus = calloc(1, (topology.max_cpu_id + 1) * sizeof(struct cpu_topology));
1177 if (topology.cpus == NULL) {
1178 ERROR("Turbostat plugin: Unable to allocate memory for CPU topology");
1182 ret = allocate_cpu_set(&cpu_present_set, &cpu_present_setsize);
1185 ret = allocate_cpu_set(&cpu_affinity_set, &cpu_affinity_setsize);
1188 ret = allocate_cpu_set(&cpu_saved_affinity_set, &cpu_saved_affinity_setsize);
1192 ret = for_all_proc_cpus(mark_cpu_present);
1198 * find max_core_id, max_package_id
1200 for (i = 0; i <= topology.max_cpu_id; ++i) {
1202 struct cpu_topology *cpu = &topology.cpus[i];
1204 if (cpu_is_not_present(i)) {
1205 WARNING("Turbostat plugin: cpu%d NOT PRESENT", i);
1209 ret = parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", i);
1213 cpu->package_id = ret;
1214 if (cpu->package_id > max_package_id)
1215 max_package_id = cpu->package_id;
1217 ret = parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", i);
1222 if (cpu->core_id > max_core_id)
1223 max_core_id = cpu->core_id;
1224 ret = parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", i);
1228 cpu->first_core_in_package = 1;
1230 ret = get_threads_on_core(i);
1235 if (num_threads > max_thread_id)
1236 max_thread_id = num_threads;
1237 ret = parse_int_file("/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", i);
1241 cpu->first_thread_in_core = 1;
1243 DEBUG("Turbostat plugin: cpu %d pkg %d core %d\n",
1244 i, cpu->package_id, cpu->core_id);
1246 /* Num is max + 1 (need to count 0) */
1247 topology.num_packages = max_package_id + 1;
1248 topology.num_cores = max_core_id + 1;
1249 topology.num_threads = max_thread_id + 1;
1253 free(topology.cpus);
1258 /************************
1259 * Main alloc/init/free *
1260 ************************/
1263 allocate_counters(struct thread_data **threads, struct core_data **cores, struct pkg_data **packages)
1266 int total_threads, total_cores;
1268 total_threads = topology.num_threads * topology.num_cores * topology.num_packages;
1269 *threads = calloc(total_threads, sizeof(struct thread_data));
1270 if (*threads == NULL)
1273 for (i = 0; i < total_threads; ++i)
1274 (*threads)[i].cpu_id = -1;
1276 total_cores = topology.num_cores * topology.num_packages;
1277 *cores = calloc(total_cores, sizeof(struct core_data));
1279 goto err_clean_threads;
1281 for (i = 0; i < total_cores; ++i)
1282 (*cores)[i].core_id = -1;
1284 *packages = calloc(topology.num_packages, sizeof(struct pkg_data));
1285 if (*packages == NULL)
1286 goto err_clean_cores;
1288 for (i = 0; i < topology.num_packages; i++)
1289 (*packages)[i].package_id = i;
1298 ERROR("Turbostat plugin: Failled to allocate memory for counters");
1303 init_counter(struct thread_data *thread_base, struct core_data *core_base,
1304 struct pkg_data *pkg_base, int cpu_id)
1306 struct thread_data *t;
1307 struct core_data *c;
1309 struct cpu_topology *cpu = &topology.cpus[cpu_id];
1311 t = GET_THREAD(thread_base, !(cpu->first_thread_in_core), cpu->core_id, cpu->package_id);
1312 c = GET_CORE(core_base, cpu->core_id, cpu->package_id);
1313 p = GET_PKG(pkg_base, cpu->package_id);
1316 if (cpu->first_thread_in_core)
1317 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
1318 if (cpu->first_core_in_package)
1319 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
1321 c->core_id = cpu->core_id;
1322 p->package_id = cpu->package_id;
1326 initialize_counters(void)
1330 for (cpu_id = 0; cpu_id <= topology.max_cpu_id; ++cpu_id) {
1331 if (cpu_is_not_present(cpu_id))
1333 init_counter(EVEN_COUNTERS, cpu_id);
1334 init_counter(ODD_COUNTERS, cpu_id);
1335 init_counter(DELTA_COUNTERS, cpu_id);
1342 free_all_buffers(void)
1347 CPU_FREE(cpu_present_set);
1348 cpu_present_set = NULL;
1349 cpu_present_set = 0;
1351 CPU_FREE(cpu_affinity_set);
1352 cpu_affinity_set = NULL;
1353 cpu_affinity_setsize = 0;
1355 CPU_FREE(cpu_saved_affinity_set);
1356 cpu_saved_affinity_set = NULL;
1357 cpu_saved_affinity_setsize = 0;
1365 package_even = NULL;
1377 free(package_delta);
1379 thread_delta = NULL;
1381 package_delta = NULL;
1385 /**********************
1386 * Collectd functions *
1387 **********************/
1389 #define DO_OR_GOTO_ERR(something) \
1391 ret = (something); \
1396 static int setup_all_buffers(void)
1400 DO_OR_GOTO_ERR(topology_probe());
1401 DO_OR_GOTO_ERR(allocate_counters(&thread_even, &core_even, &package_even));
1402 DO_OR_GOTO_ERR(allocate_counters(&thread_odd, &core_odd, &package_odd));
1403 DO_OR_GOTO_ERR(allocate_counters(&thread_delta, &core_delta, &package_delta));
1404 initialize_counters();
1405 DO_OR_GOTO_ERR(for_all_cpus(set_temperature_target, EVEN_COUNTERS));
1406 DO_OR_GOTO_ERR(for_all_cpus(set_temperature_target, ODD_COUNTERS));
1416 turbostat_read(void)
1421 if ((ret = setup_all_buffers()) < 0)
1425 if (for_all_proc_cpus(cpu_is_not_present)) {
1427 if ((ret = setup_all_buffers()) < 0)
1429 if (for_all_proc_cpus(cpu_is_not_present)) {
1430 ERROR("Turbostat plugin: CPU appeared just after "
1436 /* Saving the scheduling affinity, as it will be modified by get_counters */
1437 if (sched_getaffinity(0, cpu_saved_affinity_setsize, cpu_saved_affinity_set) != 0) {
1438 ERROR("Turbostat plugin: Unable to save the CPU affinity");
1443 if ((ret = for_all_cpus(get_counters, EVEN_COUNTERS)) < 0)
1445 time_even = cdtime();
1453 if ((ret = for_all_cpus(get_counters, ODD_COUNTERS)) < 0)
1455 time_odd = cdtime();
1457 time_delta = time_odd - time_even;
1458 if ((ret = for_all_cpus_delta(ODD_COUNTERS, EVEN_COUNTERS)) < 0)
1460 if ((ret = for_all_cpus(submit_counters, DELTA_COUNTERS)) < 0)
1463 if ((ret = for_all_cpus(get_counters, EVEN_COUNTERS)) < 0)
1465 time_even = cdtime();
1467 time_delta = time_even - time_odd;
1468 if ((ret = for_all_cpus_delta(EVEN_COUNTERS, ODD_COUNTERS)) < 0)
1470 if ((ret = for_all_cpus(submit_counters, DELTA_COUNTERS)) < 0)
1476 * Let's restore the affinity
1477 * This might fail if the number of CPU changed, but we can't do anything in that case..
1479 (void)sched_setaffinity(0, cpu_saved_affinity_setsize, cpu_saved_affinity_set);
1484 check_permissions(void)
1486 struct __user_cap_header_struct cap_header_data;
1487 cap_user_header_t cap_header = &cap_header_data;
1488 struct __user_cap_data_struct cap_data_data;
1489 cap_user_data_t cap_data = &cap_data_data;
1492 if (getuid() == 0) {
1493 /* We have everything we need */
1497 /* check for CAP_SYS_RAWIO */
1498 cap_header->pid = getpid();
1499 cap_header->version = _LINUX_CAPABILITY_VERSION;
1500 if (capget(cap_header, cap_data) < 0) {
1501 ERROR("Turbostat plugin: capget failed");
1505 if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
1506 WARNING("Turbostat plugin: Collectd doesn't have the "
1507 "CAP_SYS_RAWIO capability. If you don't want to run "
1508 "collectd as root, try running \"setcap "
1509 "cap_sys_rawio=ep\" on collectd binary");
1513 if (euidaccess("/dev/cpu/0/msr", R_OK)) {
1514 WARNING("Turbostat plugin: Collectd cannot open"
1515 "/dev/cpu/0/msr. If you don't want to run collectd as "
1516 "root, you need to change the ownership (chown) and "
1517 "permissions on /dev/cpu/*/msr to allow such access");
1522 ERROR("Turbostat plugin: Initialization failed: this plugin "
1523 "requires collectd to either to run as root or give "
1524 "collectd a special capability (CAP_SYS_RAWIO) and read "
1525 "access to /dev/cpu/*/msr (see previous warnings)");
1530 turbostat_init(void)
1535 if (stat("/dev/cpu/0/msr", &sb)) {
1536 ERROR("Turbostat plugin: Initialization failed: /dev/cpu/0/msr"
1537 " does not exist while the CPU supports MSR. You may be "
1538 "missing the corresponding kernel module, please try '# "
1543 DO_OR_GOTO_ERR(check_permissions());
1545 DO_OR_GOTO_ERR(probe_cpu());
1547 DO_OR_GOTO_ERR(setup_all_buffers());
1549 plugin_register_read(PLUGIN_NAME, turbostat_read);
1558 turbostat_config(const char *key, const char *value)
1560 long unsigned int tmp_val;
1563 if (strcasecmp("TCCActivationTemp", key) == 0) {
1564 tmp_val = strtoul(value, &end, 0);
1565 if (*end != '\0' || tmp_val > UINT_MAX) {
1566 ERROR("Turbostat plugin: Invalid TCCActivationTemp '%s'",
1570 tcc_activation_temp = (unsigned int) tmp_val;
1572 ERROR("Turbostat plugin: Invalid configuration option '%s'",
1579 void module_register(void)
1581 plugin_register_init(PLUGIN_NAME, turbostat_init);
1582 plugin_register_config(PLUGIN_NAME, turbostat_config, config_keys, config_keys_num);