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/common/common.h"
42 #include "utils_time.h"
44 #include "msr-index.h"
46 #ifdef HAVE_SYS_CAPABILITY_H
47 #include <sys/capability.h>
48 #endif /* HAVE_SYS_CAPABILITY_H */
50 #define PLUGIN_NAME "turbostat"
52 typedef enum affinity_policy_enum {
53 policy_restore_affinity, /* restore cpu affinity to whatever it was before */
54 policy_allcpus_affinity /* do not restore affinity, set to all cpus */
57 /* the default is to set cpu affinity to all cpus */
58 static affinity_policy_t affinity_policy = policy_allcpus_affinity;
61 * This tool uses the Model-Specific Registers (MSRs) present on Intel
63 * The general description each of these registers, depending on the
65 * can be found in the IntelĀ® 64 and IA-32 Architectures Software Developer
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 * If set, use kernel logical core numbering for all "per core" metrics.
81 static bool config_lcn;
84 * Bitmask of the list of core C states supported by the processor.
85 * Currently supported C-states (by this plugin): 3, 6, 7
87 static unsigned int do_core_cstate;
88 static unsigned int config_core_cstate;
89 static bool apply_config_core_cstate;
92 * Bitmask of the list of pacages C states supported by the processor.
93 * Currently supported C-states (by this plugin): 2, 3, 6, 7, 8, 9, 10
95 static unsigned int do_pkg_cstate;
96 static unsigned int config_pkg_cstate;
97 static bool apply_config_pkg_cstate;
100 * Boolean indicating if the processor supports 'I/O System-Management Interrupt
104 static bool config_smi;
105 static bool apply_config_smi;
108 * Boolean indicating if the processor supports 'Digital temperature sensor'
109 * This feature enables the monitoring of the temperature of each core
111 * This feature has two limitations:
112 * - if MSR_IA32_TEMPERATURE_TARGET is not supported, the absolute temperature
114 * - Temperatures above the tcc_activation_temp are not recorded
117 static bool config_dts;
118 static bool apply_config_dts;
121 * Boolean indicating if the processor supports 'Package thermal management'
122 * This feature allows the monitoring of the temperature of each package
124 * This feature has two limitations:
125 * - if MSR_IA32_TEMPERATURE_TARGET is not supported, the absolute temperature
127 * - Temperatures above the tcc_activation_temp are not recorded
130 static bool config_ptm;
131 static bool apply_config_ptm;
134 * Thermal Control Circuit Activation Temperature as configured by the user.
135 * This override the automated detection via MSR_IA32_TEMPERATURE_TARGET
136 * and should only be used if the automated detection fails.
138 static unsigned int tcc_activation_temp;
140 static unsigned int do_power_fields;
141 #define UFS_PLATFORM (1 << 0)
142 #define TURBO_PLATFORM (1 << 1)
143 #define PSTATES_PLATFORM (1 << 2)
145 static unsigned int do_rapl;
146 static unsigned int config_rapl;
147 static bool apply_config_rapl;
148 static double rapl_energy_units;
149 static double rapl_power_units;
151 #define RAPL_PKG (1 << 0)
152 /* 0x610 MSR_PKG_POWER_LIMIT */
153 /* 0x611 MSR_PKG_ENERGY_STATUS */
154 /* 0x614 MSR_PKG_POWER_INFO */
155 #define RAPL_DRAM (1 << 1)
156 /* 0x618 MSR_DRAM_POWER_LIMIT */
157 /* 0x619 MSR_DRAM_ENERGY_STATUS */
158 /* 0x61c MSR_DRAM_POWER_INFO */
159 #define RAPL_CORES (1 << 2)
160 /* 0x638 MSR_PP0_POWER_LIMIT */
161 /* 0x639 MSR_PP0_ENERGY_STATUS */
163 #define RAPL_GFX (1 << 3)
164 /* 0x640 MSR_PP1_POWER_LIMIT */
165 /* 0x641 MSR_PP1_ENERGY_STATUS */
166 /* 0x642 MSR_PP1_POLICY */
167 #define TJMAX_DEFAULT 100
169 static cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_saved_affinity_set;
170 static size_t cpu_present_setsize, cpu_affinity_setsize,
171 cpu_saved_affinity_setsize;
173 static struct thread_data {
174 unsigned long long tsc;
175 unsigned long long aperf;
176 unsigned long long mperf;
177 unsigned long long c1;
178 unsigned int smi_count;
181 #define CPU_IS_FIRST_THREAD_IN_CORE 0x2
182 #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
183 } * thread_delta, *thread_even, *thread_odd;
185 static struct core_data {
186 unsigned long long c3;
187 unsigned long long c6;
188 unsigned long long c7;
189 unsigned int core_temp_c;
190 unsigned int core_id;
191 } * core_delta, *core_even, *core_odd;
193 static struct pkg_data {
194 unsigned long long pc2;
195 unsigned long long pc3;
196 unsigned long long pc6;
197 unsigned long long pc7;
198 unsigned long long pc8;
199 unsigned long long pc9;
200 unsigned long long pc10;
201 unsigned int package_id;
202 uint32_t energy_pkg; /* MSR_PKG_ENERGY_STATUS */
203 uint32_t energy_dram; /* MSR_DRAM_ENERGY_STATUS */
204 uint32_t energy_cores; /* MSR_PP0_ENERGY_STATUS */
205 uint32_t energy_gfx; /* MSR_PP1_ENERGY_STATUS */
207 uint8_t turbo_enabled;
208 uint8_t pstates_enabled;
210 unsigned int tcc_activation_temp;
211 unsigned int pkg_temp_c;
212 } * package_delta, *package_even, *package_odd;
214 #define DELTA_COUNTERS thread_delta, core_delta, package_delta
215 #define ODD_COUNTERS thread_odd, core_odd, package_odd
216 #define EVEN_COUNTERS thread_even, core_even, package_even
217 static bool is_even = true;
219 static bool allocated;
220 static bool initialized;
222 #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
223 (thread_base + (pkg_no)*topology.num_cores * topology.num_threads + \
224 (core_no)*topology.num_threads + (thread_no))
225 #define GET_CORE(core_base, core_no, pkg_no) \
226 (core_base + (pkg_no)*topology.num_cores + (core_no))
227 #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
229 struct cpu_topology {
230 unsigned int package_id;
231 unsigned int core_id;
232 bool first_core_in_package;
233 bool first_thread_in_core;
236 static struct topology {
237 unsigned int max_cpu_id;
238 unsigned int num_packages;
239 unsigned int num_cores;
240 unsigned int num_threads;
241 struct cpu_topology *cpus;
244 static cdtime_t time_even, time_odd, time_delta;
246 static const char *config_keys[] = {
249 "SystemManagementInterrupt",
250 "DigitalTemperatureSensor",
251 "PackageThermalManagement",
253 "RunningAveragePowerLimit",
255 "RestoreAffinityPolicy",
257 static const int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
259 /*****************************
260 * MSR Manipulation helpers *
261 *****************************/
264 * Open a MSR device for reading
265 * Can change the scheduling affinity of the current process if multiple_read is
268 static int __attribute__((warn_unused_result))
269 open_msr(unsigned int cpu, bool multiple_read) {
274 * If we need to do multiple read, let's migrate to the CPU
275 * Otherwise, we would lose time calling functions on another CPU
277 * If we are not yet initialized (cpu_affinity_setsize = 0),
278 * we need to skip this optimisation.
280 if (multiple_read && cpu_affinity_setsize) {
281 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
282 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
283 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1) {
284 ERROR("turbostat plugin: Could not migrate to CPU %d", cpu);
289 snprintf(pathname, sizeof(pathname), "/dev/cpu/%d/msr", cpu);
290 fd = open(pathname, O_RDONLY);
292 ERROR("turbostat plugin: failed to open %s", pathname);
299 * Read a single MSR from an open file descriptor
301 static int __attribute__((warn_unused_result))
302 read_msr(int fd, off_t offset, unsigned long long *msr) {
305 retval = pread(fd, msr, sizeof *msr, offset);
307 if (retval != sizeof *msr) {
308 ERROR("turbostat plugin: MSR offset 0x%llx read failed",
309 (unsigned long long)offset);
316 * Open a MSR device for reading, read the value asked for and close it.
317 * This call will not affect the scheduling affinity of this thread.
319 static ssize_t __attribute__((warn_unused_result))
320 get_msr(unsigned int cpu, off_t offset, unsigned long long *msr) {
324 fd = open_msr(cpu, 0);
327 retval = read_msr(fd, offset, msr);
332 /********************************
333 * Raw data acquisition (1 CPU) *
334 ********************************/
337 * Read every data avalaible for a single CPU
339 * Core data is shared for all threads in one core: extracted only for the first
341 * Package data is shared for all core in one package: extracted only for the
342 * first thread of the first core
344 * Side effect: migrates to the targeted CPU
346 static int __attribute__((warn_unused_result))
347 get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p) {
348 unsigned int cpu = t->cpu_id;
349 unsigned long long msr;
353 msr_fd = open_msr(cpu, 1);
357 #define READ_MSR(msr, dst) \
359 if (read_msr(msr_fd, msr, dst)) { \
360 ERROR("turbostat plugin: Unable to read " #msr); \
366 READ_MSR(MSR_IA32_TSC, &t->tsc);
368 READ_MSR(MSR_IA32_APERF, &t->aperf);
369 READ_MSR(MSR_IA32_MPERF, &t->mperf);
372 READ_MSR(MSR_SMI_COUNT, &msr);
373 t->smi_count = msr & 0xFFFFFFFF;
376 /* collect core counters only for 1st thread in core */
377 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) {
382 if (do_core_cstate & (1 << 3))
383 READ_MSR(MSR_CORE_C3_RESIDENCY, &c->c3);
384 if (do_core_cstate & (1 << 6))
385 READ_MSR(MSR_CORE_C6_RESIDENCY, &c->c6);
386 if (do_core_cstate & (1 << 7))
387 READ_MSR(MSR_CORE_C7_RESIDENCY, &c->c7);
390 READ_MSR(MSR_IA32_THERM_STATUS, &msr);
391 c->core_temp_c = p->tcc_activation_temp - ((msr >> 16) & 0x7F);
394 /* collect package counters only for 1st core in package */
395 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
400 if (do_pkg_cstate & (1 << 2))
401 READ_MSR(MSR_PKG_C2_RESIDENCY, &p->pc2);
402 if (do_pkg_cstate & (1 << 3))
403 READ_MSR(MSR_PKG_C3_RESIDENCY, &p->pc3);
404 if (do_pkg_cstate & (1 << 6))
405 READ_MSR(MSR_PKG_C6_RESIDENCY, &p->pc6);
406 if (do_pkg_cstate & (1 << 7))
407 READ_MSR(MSR_PKG_C7_RESIDENCY, &p->pc7);
408 if (do_pkg_cstate & (1 << 8))
409 READ_MSR(MSR_PKG_C8_RESIDENCY, &p->pc8);
410 if (do_pkg_cstate & (1 << 9))
411 READ_MSR(MSR_PKG_C9_RESIDENCY, &p->pc9);
412 if (do_pkg_cstate & (1 << 10))
413 READ_MSR(MSR_PKG_C10_RESIDENCY, &p->pc10);
415 if (do_rapl & RAPL_PKG) {
416 READ_MSR(MSR_PKG_ENERGY_STATUS, &msr);
417 p->energy_pkg = msr & 0xFFFFFFFF;
418 READ_MSR(MSR_PKG_POWER_INFO, &msr);
419 p->tdp = msr & 0x7FFF;
421 if (do_rapl & RAPL_CORES) {
422 READ_MSR(MSR_PP0_ENERGY_STATUS, &msr);
423 p->energy_cores = msr & 0xFFFFFFFF;
425 if (do_rapl & RAPL_DRAM) {
426 READ_MSR(MSR_DRAM_ENERGY_STATUS, &msr);
427 p->energy_dram = msr & 0xFFFFFFFF;
429 if (do_rapl & RAPL_GFX) {
430 READ_MSR(MSR_PP1_ENERGY_STATUS, &msr);
431 p->energy_gfx = msr & 0xFFFFFFFF;
434 READ_MSR(MSR_IA32_PACKAGE_THERM_STATUS, &msr);
435 p->pkg_temp_c = p->tcc_activation_temp - ((msr >> 16) & 0x7F);
437 if (do_power_fields & TURBO_PLATFORM) {
438 READ_MSR(MSR_IA32_MISC_ENABLE, &msr);
439 p->turbo_enabled = !((msr >> 38) & 0x1);
441 if (do_power_fields & PSTATES_PLATFORM) {
442 READ_MSR(MSR_IA32_MISC_ENABLE, &msr);
443 p->pstates_enabled = (msr >> 16) & 0x1;
445 if (do_power_fields & UFS_PLATFORM) {
446 READ_MSR(MSR_UNCORE_FREQ_SCALING, &msr);
447 p->uncore = msr & 0x1F;
455 /**********************************
456 * Evaluating the changes (1 CPU) *
457 **********************************/
460 * Extract the evolution old->new in delta at a package level
461 * (some are not new-delta, e.g. temperature)
463 static inline void delta_package(struct pkg_data *delta,
464 const struct pkg_data *new,
465 const struct pkg_data *old) {
466 delta->pc2 = new->pc2 - old->pc2;
467 delta->pc3 = new->pc3 - old->pc3;
468 delta->pc6 = new->pc6 - old->pc6;
469 delta->pc7 = new->pc7 - old->pc7;
470 delta->pc8 = new->pc8 - old->pc8;
471 delta->pc9 = new->pc9 - old->pc9;
472 delta->pc10 = new->pc10 - old->pc10;
473 delta->pkg_temp_c = new->pkg_temp_c;
475 delta->energy_pkg = new->energy_pkg - old->energy_pkg;
476 delta->energy_cores = new->energy_cores - old->energy_cores;
477 delta->energy_gfx = new->energy_gfx - old->energy_gfx;
478 delta->energy_dram = new->energy_dram - old->energy_dram;
479 delta->tdp = new->tdp;
480 delta->turbo_enabled = new->turbo_enabled;
481 delta->pstates_enabled = new->pstates_enabled;
482 delta->tcc_activation_temp = new->tcc_activation_temp;
483 delta->uncore = new->uncore;
487 * Extract the evolution old->new in delta at a core level
488 * (some are not new-delta, e.g. temperature)
490 static inline void delta_core(struct core_data *delta,
491 const struct core_data *new,
492 const struct core_data *old) {
493 delta->c3 = new->c3 - old->c3;
494 delta->c6 = new->c6 - old->c6;
495 delta->c7 = new->c7 - old->c7;
496 delta->core_temp_c = new->core_temp_c;
500 * Extract the evolution old->new in delta at a package level
501 * core_delta is required for c1 estimation (tsc - c0 - all core cstates)
503 static inline int __attribute__((warn_unused_result))
504 delta_thread(struct thread_data *delta, const struct thread_data *new,
505 const struct thread_data *old, const struct core_data *cdelta) {
506 delta->tsc = new->tsc - old->tsc;
508 /* check for TSC < 1 Mcycles over interval */
509 if (delta->tsc < (1000 * 1000)) {
510 WARNING("turbostat plugin: Insanely slow TSC rate, TSC stops "
511 "in idle? You can disable all c-states by booting with"
512 " 'idle=poll' or just the deep ones with"
513 " 'processor.max_cstate=1'");
517 delta->c1 = new->c1 - old->c1;
519 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
520 delta->aperf = new->aperf - old->aperf;
521 delta->mperf = new->mperf - old->mperf;
523 if (!aperf_mperf_unstable) {
524 WARNING("turbostat plugin: APERF or MPERF went "
525 "backwards. Frequency results do not cover "
526 "the entire interval. Fix this by running "
527 "Linux-2.6.30 or later.");
529 aperf_mperf_unstable = true;
534 * As counter collection is not atomic,
535 * it is possible for mperf's non-halted cycles + idle states
536 * to exceed TSC's all cycles: show c1 = 0% in that case.
538 if ((delta->mperf + cdelta->c3 + cdelta->c6 + cdelta->c7) > delta->tsc)
541 /* normal case, derive c1 */
543 delta->tsc - delta->mperf - cdelta->c3 - cdelta->c6 - cdelta->c7;
546 if (delta->mperf == 0) {
547 WARNING("turbostat plugin: cpu%d MPERF 0!", old->cpu_id);
548 delta->mperf = 1; /* divide by 0 protection */
552 delta->smi_count = new->smi_count - old->smi_count;
557 /**********************************
558 * Submitting the results (1 CPU) *
559 **********************************/
562 * Submit one gauge value
564 static void turbostat_submit(const char *plugin_instance, const char *type,
565 const char *type_instance, gauge_t value) {
566 value_list_t vl = VALUE_LIST_INIT;
568 vl.values = &(value_t){.gauge = value};
570 sstrncpy(vl.plugin, PLUGIN_NAME, sizeof(vl.plugin));
571 if (plugin_instance != NULL)
572 sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
573 sstrncpy(vl.type, type, sizeof(vl.type));
574 if (type_instance != NULL)
575 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
577 plugin_dispatch_values(&vl);
581 * Submit every data for a single CPU
583 * Core data is shared for all threads in one core: submitted only for the first
585 * Package data is shared for all core in one package: submitted only for the
586 * first thread of the first core
588 static int submit_counters(struct thread_data *t, struct core_data *c,
589 struct pkg_data *p) {
590 char name[DATA_MAX_NAME_LEN];
591 double interval_float;
593 interval_float = CDTIME_T_TO_DOUBLE(time_delta);
595 DEBUG("turbostat plugin: submit stats for cpu: %d, core: %d, pkg: %d",
596 t->cpu_id, c->core_id, p->package_id);
598 snprintf(name, sizeof(name), "cpu%02d", t->cpu_id);
600 if (!aperf_mperf_unstable)
601 turbostat_submit(name, "percent", "c0", 100.0 * t->mperf / t->tsc);
602 if (!aperf_mperf_unstable)
603 turbostat_submit(name, "percent", "c1", 100.0 * t->c1 / t->tsc);
605 turbostat_submit(name, "frequency", "average",
606 1.0 / 1000000 * t->aperf / interval_float);
608 if ((!aperf_mperf_unstable) || (!(t->aperf > t->tsc || t->mperf > t->tsc)))
609 turbostat_submit(name, "frequency", "busy",
610 1.0 * t->tsc / 1000000 * t->aperf / t->mperf /
613 /* Sanity check (should stay stable) */
614 turbostat_submit(name, "gauge", "TSC",
615 1.0 * t->tsc / 1000000 / interval_float);
619 turbostat_submit(name, "count", NULL, t->smi_count);
621 /* submit per-core data only for 1st thread in core */
622 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
625 /* If not using logical core numbering, set core id */
627 if (topology.num_packages > 1)
628 snprintf(name, sizeof(name), "pkg%02d-core%02d", p->package_id,
631 snprintf(name, sizeof(name), "core%02d", c->core_id);
634 if (do_core_cstate & (1 << 3))
635 turbostat_submit(name, "percent", "c3", 100.0 * c->c3 / t->tsc);
636 if (do_core_cstate & (1 << 6))
637 turbostat_submit(name, "percent", "c6", 100.0 * c->c6 / t->tsc);
638 if (do_core_cstate & (1 << 7))
639 turbostat_submit(name, "percent", "c7", 100.0 * c->c7 / t->tsc);
642 turbostat_submit(name, "temperature", NULL, c->core_temp_c);
644 /* submit per-package data only for 1st core in package */
645 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
648 snprintf(name, sizeof(name), "pkg%02d", p->package_id);
651 turbostat_submit(name, "temperature", NULL, p->pkg_temp_c);
653 if (do_pkg_cstate & (1 << 2))
654 turbostat_submit(name, "percent", "pc2", 100.0 * p->pc2 / t->tsc);
655 if (do_pkg_cstate & (1 << 3))
656 turbostat_submit(name, "percent", "pc3", 100.0 * p->pc3 / t->tsc);
657 if (do_pkg_cstate & (1 << 6))
658 turbostat_submit(name, "percent", "pc6", 100.0 * p->pc6 / t->tsc);
659 if (do_pkg_cstate & (1 << 7))
660 turbostat_submit(name, "percent", "pc7", 100.0 * p->pc7 / t->tsc);
661 if (do_pkg_cstate & (1 << 8))
662 turbostat_submit(name, "percent", "pc8", 100.0 * p->pc8 / t->tsc);
663 if (do_pkg_cstate & (1 << 9))
664 turbostat_submit(name, "percent", "pc9", 100.0 * p->pc9 / t->tsc);
665 if (do_pkg_cstate & (1 << 10))
666 turbostat_submit(name, "percent", "pc10", 100.0 * p->pc10 / t->tsc);
669 if (do_rapl & RAPL_PKG) {
670 turbostat_submit(name, "power", "pkg",
671 p->energy_pkg * rapl_energy_units / interval_float);
672 turbostat_submit(name, "tdp", "pkg", p->tdp * rapl_power_units);
674 if (do_rapl & RAPL_CORES)
675 turbostat_submit(name, "power", "cores",
676 p->energy_cores * rapl_energy_units / interval_float);
677 if (do_rapl & RAPL_GFX)
678 turbostat_submit(name, "power", "GFX",
679 p->energy_gfx * rapl_energy_units / interval_float);
680 if (do_rapl & RAPL_DRAM)
681 turbostat_submit(name, "power", "DRAM",
682 p->energy_dram * rapl_energy_units / interval_float);
685 if (do_power_fields & TURBO_PLATFORM) {
686 turbostat_submit(name, "turbo_enabled", NULL, p->turbo_enabled);
688 if (do_power_fields & PSTATES_PLATFORM) {
689 turbostat_submit(name, "pstates_enabled", NULL, p->pstates_enabled);
691 if (do_power_fields & UFS_PLATFORM) {
692 turbostat_submit(name, "uncore_ratio", NULL, p->uncore);
694 turbostat_submit(name, "temperature", "tcc_activation",
695 p->tcc_activation_temp);
700 /**********************************
701 * Looping function over all CPUs *
702 **********************************/
705 * Check if a given cpu id is in our compiled list of existing CPUs
707 static int cpu_is_not_present(unsigned int cpu) {
708 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
712 * Loop on all CPUs in topological order
714 * Skip non-present cpus
715 * Return the error code at the first error or 0
717 static int __attribute__((warn_unused_result))
718 for_all_cpus(int(func)(struct thread_data *, struct core_data *,
720 struct thread_data *thread_base, struct core_data *core_base,
721 struct pkg_data *pkg_base) {
724 for (unsigned int pkg_no = 0; pkg_no < topology.num_packages; ++pkg_no) {
725 for (unsigned int core_no = 0; core_no < topology.num_cores; ++core_no) {
726 for (unsigned int thread_no = 0; thread_no < topology.num_threads;
728 struct thread_data *t;
732 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
734 if (cpu_is_not_present(t->cpu_id))
737 c = GET_CORE(core_base, core_no, pkg_no);
738 p = GET_PKG(pkg_base, pkg_no);
740 retval = func(t, c, p);
750 * Dedicated loop: Extract every data evolution for all CPU
752 * Skip non-present cpus
753 * Return the error code at the first error or 0
755 * Core data is shared for all threads in one core: extracted only for the first
757 * Package data is shared for all core in one package: extracted only for the
758 * first thread of the first core
760 static int __attribute__((warn_unused_result))
761 for_all_cpus_delta(const struct thread_data *thread_new_base,
762 const struct core_data *core_new_base,
763 const struct pkg_data *pkg_new_base,
764 const struct thread_data *thread_old_base,
765 const struct core_data *core_old_base,
766 const struct pkg_data *pkg_old_base) {
769 for (unsigned int pkg_no = 0; pkg_no < topology.num_packages; ++pkg_no) {
770 for (unsigned int core_no = 0; core_no < topology.num_cores; ++core_no) {
771 for (unsigned int thread_no = 0; thread_no < topology.num_threads;
773 struct thread_data *t_delta;
774 const struct thread_data *t_old, *t_new;
775 struct core_data *c_delta;
777 /* Get correct pointers for threads */
778 t_delta = GET_THREAD(thread_delta, thread_no, core_no, pkg_no);
779 t_new = GET_THREAD(thread_new_base, thread_no, core_no, pkg_no);
780 t_old = GET_THREAD(thread_old_base, thread_no, core_no, pkg_no);
782 /* Skip threads that disappeared */
783 if (cpu_is_not_present(t_delta->cpu_id))
786 /* c_delta is always required for delta_thread */
787 c_delta = GET_CORE(core_delta, core_no, pkg_no);
789 /* calculate core delta only for 1st thread in core */
790 if (t_new->flags & CPU_IS_FIRST_THREAD_IN_CORE) {
791 const struct core_data *c_old, *c_new;
793 c_new = GET_CORE(core_new_base, core_no, pkg_no);
794 c_old = GET_CORE(core_old_base, core_no, pkg_no);
796 delta_core(c_delta, c_new, c_old);
799 /* Always calculate thread delta */
800 retval = delta_thread(t_delta, t_new, t_old, c_delta);
804 /* calculate package delta only for 1st core in package */
805 if (t_new->flags & CPU_IS_FIRST_CORE_IN_PACKAGE) {
806 struct pkg_data *p_delta;
807 const struct pkg_data *p_old, *p_new;
809 p_delta = GET_PKG(package_delta, pkg_no);
810 p_new = GET_PKG(pkg_new_base, pkg_no);
811 p_old = GET_PKG(pkg_old_base, pkg_no);
813 delta_package(p_delta, p_new, p_old);
826 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
827 * the Thermal Control Circuit (TCC) activates.
828 * This is usually equal to tjMax.
830 * Older processors do not have this MSR, so there we guess,
831 * but also allow conficuration over-ride with "TCCActivationTemp".
833 * Several MSR temperature values are in units of degrees-C
834 * below this value, including the Digital Thermal Sensor (DTS),
835 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
837 static int __attribute__((warn_unused_result))
838 set_temperature_target(struct thread_data *t, struct core_data *c,
839 struct pkg_data *p) {
840 unsigned long long msr;
841 unsigned int target_c_local;
843 /* tcc_activation_temp is used only for dts or ptm */
844 if (!(do_dts || do_ptm))
847 /* this is a per-package concept */
848 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) ||
849 !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
852 if (tcc_activation_temp != 0) {
853 p->tcc_activation_temp = tcc_activation_temp;
857 if (get_msr(t->cpu_id, MSR_IA32_TEMPERATURE_TARGET, &msr))
860 target_c_local = (msr >> 16) & 0xFF;
865 p->tcc_activation_temp = target_c_local;
870 p->tcc_activation_temp = TJMAX_DEFAULT;
871 WARNING("turbostat plugin: cpu%d: Guessing tjMax %d C,"
872 " Please use TCCActivationTemp to specify it.",
873 t->cpu_id, p->tcc_activation_temp);
879 * Identify the functionality of the CPU
881 static int __attribute__((warn_unused_result)) probe_cpu(void) {
882 unsigned int eax, ebx, ecx, edx, max_level;
883 unsigned int fms, family, model;
886 * - EAX: Maximum Input Value for Basic CPUID Information
887 * - EBX: "Genu" (0x756e6547)
888 * - EDX: "ineI" (0x49656e69)
889 * - ECX: "ntel" (0x6c65746e)
891 max_level = ebx = ecx = edx = 0;
892 __get_cpuid(0, &max_level, &ebx, &ecx, &edx);
893 if (ebx != 0x756e6547 && edx != 0x49656e69 && ecx != 0x6c65746e) {
894 ERROR("turbostat plugin: Unsupported CPU (not Intel)");
899 * - EAX: Version Information: Type, Family, Model, and Stepping ID
902 * + 12-13: Processor type
903 * + 16-19: Extended Model ID
904 * + 20-27: Extended Family ID
905 * - EDX: Feature Information:
906 * + 5: Support for MSR read/write operations
908 fms = ebx = ecx = edx = 0;
909 __get_cpuid(1, &fms, &ebx, &ecx, &edx);
910 family = (fms >> 8) & 0xf;
911 model = (fms >> 4) & 0xf;
913 family += (fms >> 20) & 0xf;
914 if (family == 6 || family == 0xf)
915 model += ((fms >> 16) & 0xf) << 4;
916 if (!(edx & (1 << 5))) {
917 ERROR("turbostat plugin: Unsupported CPU (no MSR support)");
924 * + 0: Digital temperature sensor is supported if set
925 * + 6: Package thermal management is supported if set
927 * + 0: Hardware Coordination Feedback Capability (Presence of IA32_MPERF and
929 * + 3: The processor supports performance-energy bias preference if set.
930 * It also implies the presence of a new architectural MSR called
931 * IA32_ENERGY_PERF_BIAS
933 * This check is valid for both Intel and AMD
935 eax = ebx = ecx = edx = 0;
936 __get_cpuid(0x6, &eax, &ebx, &ecx, &edx);
937 do_dts = eax & (1 << 0);
938 do_ptm = eax & (1 << 6);
939 if (!(ecx & (1 << 0))) {
940 ERROR("turbostat plugin: Unsupported CPU (No APERF)");
945 * Enable or disable C states depending on the model and family
953 do_pkg_cstate = (1 << 2) | (1 << 4) | (1 << 6);
959 do_core_cstate = (1 << 1) | (1 << 6);
960 do_pkg_cstate = (1 << 6);
963 case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
964 case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper
966 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
967 case 0x2E: /* Nehalem-EX Xeon - Beckton */
969 do_core_cstate = (1 << 3) | (1 << 6);
970 do_pkg_cstate = (1 << 3) | (1 << 6) | (1 << 7);
973 case 0x25: /* Westmere Client - Clarkdale, Arrandale */
974 case 0x2C: /* Westmere EP - Gulftown */
975 case 0x2F: /* Westmere-EX Xeon - Eagleton */
977 do_core_cstate = (1 << 3) | (1 << 6);
978 do_pkg_cstate = (1 << 3) | (1 << 6) | (1 << 7);
982 case 0x2D: /* SNB Xeon */
984 do_core_cstate = (1 << 3) | (1 << 6) | (1 << 7);
985 do_pkg_cstate = (1 << 2) | (1 << 3) | (1 << 6) | (1 << 7);
989 case 0x3E: /* IVB Xeon */
991 do_core_cstate = (1 << 3) | (1 << 6) | (1 << 7);
992 do_pkg_cstate = (1 << 2) | (1 << 3) | (1 << 6) | (1 << 7);
999 do_core_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1000 do_pkg_cstate = (1 << 2) | (1 << 3) | (1 << 6) | (1 << 7);
1002 case 0x45: /* HSW */
1004 do_core_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1005 do_pkg_cstate = (1 << 2) | (1 << 3) | (1 << 6) | (1 << 7) | (1 << 8) |
1006 (1 << 9) | (1 << 10);
1009 case 0x4F: /* BDW */
1010 case 0x56: /* BDX-DE */
1012 do_core_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1013 do_pkg_cstate = (1 << 2) | (1 << 3) | (1 << 6) | (1 << 7);
1015 case 0x3D: /* BDW */
1017 do_core_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1018 do_pkg_cstate = (1 << 2) | (1 << 3) | (1 << 6) | (1 << 7) | (1 << 8) |
1019 (1 << 9) | (1 << 10);
1028 case 0x2A: /* SNB */
1029 case 0x3A: /* IVB */
1030 case 0x3C: /* HSW */
1031 case 0x45: /* HSW */
1032 case 0x46: /* HSW */
1033 case 0x3D: /* BDW */
1034 case 0x5E: /* SKL */
1035 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_GFX;
1037 case 0x3F: /* HSX */
1038 case 0x4F: /* BDX */
1039 case 0x56: /* BDX-DE */
1040 do_rapl = RAPL_PKG | RAPL_DRAM;
1041 do_power_fields = TURBO_PLATFORM | UFS_PLATFORM | PSTATES_PLATFORM;
1043 case 0x2D: /* SNB Xeon */
1044 case 0x3E: /* IVB Xeon */
1045 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_DRAM;
1046 do_power_fields = TURBO_PLATFORM | PSTATES_PLATFORM;
1048 case 0x37: /* BYT */
1049 case 0x4D: /* AVN */
1050 do_rapl = RAPL_PKG | RAPL_CORES;
1056 ERROR("turbostat plugin: Unsupported CPU (family: %#x, "
1062 /* Override detected values with configuration */
1063 if (apply_config_core_cstate)
1064 do_core_cstate = config_core_cstate;
1065 if (apply_config_pkg_cstate)
1066 do_pkg_cstate = config_pkg_cstate;
1067 if (apply_config_smi)
1068 do_smi = config_smi;
1069 if (apply_config_dts)
1070 do_dts = config_dts;
1071 if (apply_config_ptm)
1072 do_ptm = config_ptm;
1073 if (apply_config_rapl)
1074 do_rapl = config_rapl;
1077 unsigned long long msr;
1078 if (get_msr(0, MSR_RAPL_POWER_UNIT, &msr))
1081 rapl_power_units = 1.0 / (1 << (msr & 0xF));
1083 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
1085 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
1091 /********************
1092 * Topology Probing *
1093 ********************/
1096 * Read a single int from a file.
1098 static int __attribute__((format(printf, 1, 2)))
1099 parse_int_file(const char *fmt, ...) {
1101 char path[PATH_MAX];
1104 va_start(args, fmt);
1105 len = vsnprintf(path, sizeof(path), fmt, args);
1107 if (len < 0 || len >= PATH_MAX) {
1108 ERROR("turbostat plugin: path truncated: '%s'", path);
1113 if (parse_value_file(path, &v, DS_TYPE_DERIVE) != 0) {
1114 ERROR("turbostat plugin: Parsing \"%s\" failed.", path);
1118 return (int)v.derive;
1121 static int get_threads_on_core(unsigned int cpu) {
1128 snprintf(path, sizeof(path),
1129 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
1130 filep = fopen(path, "r");
1132 ERROR("turbostat plugin: Failed to open '%s'", path);
1137 * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4)
1138 * otherwinse 1 sibling (self).
1140 matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2);
1151 * run func(cpu) on every cpu in /proc/stat
1152 * return max_cpu number
1154 static int __attribute__((warn_unused_result))
1155 for_all_proc_cpus(int(func)(unsigned int)) {
1157 unsigned int cpu_num;
1160 fp = fopen("/proc/stat", "r");
1162 ERROR("turbostat plugin: Failed to open /proc/stat");
1166 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
1168 ERROR("turbostat plugin: Failed to parse /proc/stat");
1175 fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
1179 retval = func(cpu_num);
1190 * Update the stored topology.max_cpu_id
1192 static int update_max_cpu_id(unsigned int cpu) {
1193 if (topology.max_cpu_id < cpu)
1194 topology.max_cpu_id = cpu;
1198 static int mark_cpu_present(unsigned int cpu) {
1199 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
1203 static int __attribute__((warn_unused_result))
1204 allocate_cpu_set(cpu_set_t **set, size_t *size) {
1205 *set = CPU_ALLOC(topology.max_cpu_id + 1);
1207 ERROR("turbostat plugin: Unable to allocate CPU state");
1210 *size = CPU_ALLOC_SIZE(topology.max_cpu_id + 1);
1211 CPU_ZERO_S(*size, *set);
1216 * Build a local representation of the cpu distribution
1218 static int __attribute__((warn_unused_result)) topology_probe(void) {
1220 unsigned int max_package_id, max_core_id, max_threads;
1221 max_package_id = max_core_id = max_threads = 0;
1223 /* Clean topology */
1224 free(topology.cpus);
1225 memset(&topology, 0, sizeof(topology));
1227 ret = for_all_proc_cpus(update_max_cpu_id);
1232 calloc(1, (topology.max_cpu_id + 1) * sizeof(struct cpu_topology));
1233 if (topology.cpus == NULL) {
1234 ERROR("turbostat plugin: Unable to allocate memory for CPU topology");
1238 ret = allocate_cpu_set(&cpu_present_set, &cpu_present_setsize);
1241 ret = allocate_cpu_set(&cpu_affinity_set, &cpu_affinity_setsize);
1244 ret = allocate_cpu_set(&cpu_saved_affinity_set, &cpu_saved_affinity_setsize);
1248 ret = for_all_proc_cpus(mark_cpu_present);
1254 * find max_core_id, max_package_id
1256 for (unsigned int i = 0; i <= topology.max_cpu_id; ++i) {
1257 unsigned int num_threads;
1258 struct cpu_topology *cpu = &topology.cpus[i];
1260 if (cpu_is_not_present(i)) {
1261 WARNING("turbostat plugin: cpu%d NOT PRESENT", i);
1265 ret = parse_int_file(
1266 "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", i);
1270 cpu->package_id = (unsigned int)ret;
1271 if (cpu->package_id > max_package_id)
1272 max_package_id = cpu->package_id;
1274 ret = parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", i);
1278 cpu->core_id = (unsigned int)ret;
1279 if (cpu->core_id > max_core_id)
1280 max_core_id = cpu->core_id;
1281 ret = parse_int_file(
1282 "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", i);
1285 else if ((unsigned int)ret == i)
1286 cpu->first_core_in_package = true;
1288 ret = get_threads_on_core(i);
1292 num_threads = (unsigned int)ret;
1293 if (num_threads > max_threads)
1294 max_threads = num_threads;
1295 ret = parse_int_file(
1296 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", i);
1299 else if ((unsigned int)ret == i)
1300 cpu->first_thread_in_core = true;
1302 DEBUG("turbostat plugin: cpu %d pkg %d core %d\n", i, cpu->package_id,
1305 /* Num is max + 1 (need to count 0) */
1306 topology.num_packages = max_package_id + 1;
1307 topology.num_cores = max_core_id + 1;
1308 topology.num_threads = max_threads;
1312 free(topology.cpus);
1316 /************************
1317 * Main alloc/init/free *
1318 ************************/
1320 static int allocate_counters(struct thread_data **threads,
1321 struct core_data **cores,
1322 struct pkg_data **packages) {
1323 unsigned int total_threads, total_cores;
1325 if ((topology.num_threads == 0) || (topology.num_cores == 0) ||
1326 (topology.num_packages == 0)) {
1328 "turbostat plugin: Invalid topology: %u threads, %u cores, %u packages",
1329 topology.num_threads, topology.num_cores, topology.num_packages);
1334 topology.num_threads * topology.num_cores * topology.num_packages;
1335 *threads = calloc(total_threads, sizeof(struct thread_data));
1336 if (*threads == NULL) {
1337 ERROR("turbostat plugin: calloc failed");
1341 for (unsigned int i = 0; i < total_threads; ++i)
1342 (*threads)[i].cpu_id = topology.max_cpu_id + 1;
1344 total_cores = topology.num_cores * topology.num_packages;
1345 *cores = calloc(total_cores, sizeof(struct core_data));
1346 if (*cores == NULL) {
1347 ERROR("turbostat plugin: calloc failed");
1352 *packages = calloc(topology.num_packages, sizeof(struct pkg_data));
1353 if (*packages == NULL) {
1354 ERROR("turbostat plugin: calloc failed");
1363 static void init_counter(struct thread_data *thread_base,
1364 struct core_data *core_base, struct pkg_data *pkg_base,
1365 unsigned int cpu_id) {
1366 struct thread_data *t;
1367 struct core_data *c;
1369 struct cpu_topology *cpu = &topology.cpus[cpu_id];
1371 t = GET_THREAD(thread_base, !(cpu->first_thread_in_core), cpu->core_id,
1373 c = GET_CORE(core_base, cpu->core_id, cpu->package_id);
1374 p = GET_PKG(pkg_base, cpu->package_id);
1377 if (cpu->first_thread_in_core)
1378 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
1379 if (cpu->first_core_in_package)
1380 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
1382 c->core_id = cpu->core_id;
1383 p->package_id = cpu->package_id;
1386 static void initialize_counters(void) {
1387 for (unsigned int cpu_id = 0; cpu_id <= topology.max_cpu_id; ++cpu_id) {
1388 if (cpu_is_not_present(cpu_id))
1390 init_counter(EVEN_COUNTERS, cpu_id);
1391 init_counter(ODD_COUNTERS, cpu_id);
1392 init_counter(DELTA_COUNTERS, cpu_id);
1396 static void free_all_buffers(void) {
1398 initialized = false;
1400 CPU_FREE(cpu_present_set);
1401 cpu_present_set = NULL;
1402 cpu_present_setsize = 0;
1404 CPU_FREE(cpu_affinity_set);
1405 cpu_affinity_set = NULL;
1406 cpu_affinity_setsize = 0;
1408 CPU_FREE(cpu_saved_affinity_set);
1409 cpu_saved_affinity_set = NULL;
1410 cpu_saved_affinity_setsize = 0;
1418 package_even = NULL;
1430 free(package_delta);
1432 thread_delta = NULL;
1434 package_delta = NULL;
1437 /**********************
1438 * Collectd functions *
1439 **********************/
1441 #define DO_OR_GOTO_ERR(something) \
1443 ret = (something); \
1448 static int setup_all_buffers(void) {
1451 DO_OR_GOTO_ERR(topology_probe());
1452 DO_OR_GOTO_ERR(allocate_counters(&thread_even, &core_even, &package_even));
1453 DO_OR_GOTO_ERR(allocate_counters(&thread_odd, &core_odd, &package_odd));
1454 DO_OR_GOTO_ERR(allocate_counters(&thread_delta, &core_delta, &package_delta));
1455 initialize_counters();
1456 DO_OR_GOTO_ERR(for_all_cpus(set_temperature_target, EVEN_COUNTERS));
1457 DO_OR_GOTO_ERR(for_all_cpus(set_temperature_target, ODD_COUNTERS));
1466 static int save_affinity(void) {
1467 if (affinity_policy == policy_restore_affinity) {
1468 /* Try to save the scheduling affinity, as it will be modified by
1471 if (sched_getaffinity(0, cpu_saved_affinity_setsize,
1472 cpu_saved_affinity_set) != 0)
1479 static void restore_affinity(void) {
1480 /* Let's restore the affinity to the value saved in save_affinity */
1481 if (affinity_policy == policy_restore_affinity)
1482 (void)sched_setaffinity(0, cpu_saved_affinity_setsize,
1483 cpu_saved_affinity_set);
1485 /* reset the affinity to all present cpus */
1486 (void)sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
1490 static int turbostat_read(void) {
1494 if ((ret = setup_all_buffers()) < 0)
1498 if (for_all_proc_cpus(cpu_is_not_present)) {
1500 if ((ret = setup_all_buffers()) < 0)
1502 if (for_all_proc_cpus(cpu_is_not_present)) {
1503 ERROR("turbostat plugin: CPU appeared just after "
1509 if (save_affinity() != 0) {
1510 ERROR("turbostat plugin: Unable to save the CPU affinity. Please read the "
1511 "docs about RestoreAffinityPolicy option.");
1516 if ((ret = for_all_cpus(get_counters, EVEN_COUNTERS)) < 0)
1518 time_even = cdtime();
1526 if ((ret = for_all_cpus(get_counters, ODD_COUNTERS)) < 0)
1528 time_odd = cdtime();
1530 time_delta = time_odd - time_even;
1531 if ((ret = for_all_cpus_delta(ODD_COUNTERS, EVEN_COUNTERS)) < 0)
1533 if ((ret = for_all_cpus(submit_counters, DELTA_COUNTERS)) < 0)
1536 if ((ret = for_all_cpus(get_counters, EVEN_COUNTERS)) < 0)
1538 time_even = cdtime();
1540 time_delta = time_even - time_odd;
1541 if ((ret = for_all_cpus_delta(EVEN_COUNTERS, ODD_COUNTERS)) < 0)
1543 if ((ret = for_all_cpus(submit_counters, DELTA_COUNTERS)) < 0)
1553 static int check_permissions(void) {
1555 if (getuid() == 0) {
1556 /* We have everything we need */
1558 #if !defined(HAVE_SYS_CAPABILITY_H) && !defined(CAP_SYS_RAWIO)
1560 ERROR("turbostat plugin: Initialization failed: this plugin "
1561 "requires collectd to run as root");
1564 #else /* HAVE_SYS_CAPABILITY_H && CAP_SYS_RAWIO */
1569 if (check_capability(CAP_SYS_RAWIO) != 0) {
1570 WARNING("turbostat plugin: Collectd doesn't have the "
1571 "CAP_SYS_RAWIO capability. If you don't want to run "
1572 "collectd as root, try running \"setcap "
1573 "cap_sys_rawio=ep\" on collectd binary");
1577 if (euidaccess("/dev/cpu/0/msr", R_OK)) {
1578 WARNING("turbostat plugin: Collectd cannot open "
1579 "/dev/cpu/0/msr. If you don't want to run collectd as "
1580 "root, you need to change the ownership (chown) and "
1581 "permissions on /dev/cpu/*/msr to allow such access");
1586 ERROR("turbostat plugin: Initialization failed: this plugin "
1587 "requires collectd to either to run as root or give "
1588 "collectd a special capability (CAP_SYS_RAWIO) and read "
1589 "access to /dev/cpu/*/msr (see previous warnings)");
1591 #endif /* HAVE_SYS_CAPABILITY_H && CAP_SYS_RAWIO */
1594 static int turbostat_init(void) {
1598 if (stat("/dev/cpu/0/msr", &sb)) {
1599 ERROR("turbostat plugin: Initialization failed: /dev/cpu/0/msr "
1600 "does not exist while the CPU supports MSR. You may be "
1601 "missing the corresponding kernel module, please try '# "
1606 DO_OR_GOTO_ERR(check_permissions());
1608 DO_OR_GOTO_ERR(probe_cpu());
1610 DO_OR_GOTO_ERR(setup_all_buffers());
1612 plugin_register_read(PLUGIN_NAME, turbostat_read);
1620 static int turbostat_config(const char *key, const char *value) {
1621 long unsigned int tmp_val;
1624 if (strcasecmp("CoreCstates", key) == 0) {
1625 tmp_val = strtoul(value, &end, 0);
1626 if (*end != '\0' || tmp_val > UINT_MAX) {
1627 ERROR("turbostat plugin: Invalid CoreCstates '%s'", value);
1630 config_core_cstate = (unsigned int)tmp_val;
1631 apply_config_core_cstate = true;
1632 } else if (strcasecmp("PackageCstates", key) == 0) {
1633 tmp_val = strtoul(value, &end, 0);
1634 if (*end != '\0' || tmp_val > UINT_MAX) {
1635 ERROR("turbostat plugin: Invalid PackageCstates '%s'", value);
1638 config_pkg_cstate = (unsigned int)tmp_val;
1639 apply_config_pkg_cstate = true;
1640 } else if (strcasecmp("SystemManagementInterrupt", key) == 0) {
1641 config_smi = IS_TRUE(value);
1642 apply_config_smi = true;
1643 } else if (strcasecmp("DigitalTemperatureSensor", key) == 0) {
1644 config_dts = IS_TRUE(value);
1645 apply_config_dts = true;
1646 } else if (strcasecmp("PackageThermalManagement", key) == 0) {
1647 config_ptm = IS_TRUE(value);
1648 apply_config_ptm = true;
1649 } else if (strcasecmp("LogicalCoreNames", key) == 0) {
1650 config_lcn = IS_TRUE(value);
1651 } else if (strcasecmp("RunningAveragePowerLimit", key) == 0) {
1652 tmp_val = strtoul(value, &end, 0);
1653 if (*end != '\0' || tmp_val > UINT_MAX) {
1654 ERROR("turbostat plugin: Invalid RunningAveragePowerLimit '%s'", value);
1657 config_rapl = (unsigned int)tmp_val;
1658 apply_config_rapl = true;
1659 } else if (strcasecmp("TCCActivationTemp", key) == 0) {
1660 tmp_val = strtoul(value, &end, 0);
1661 if (*end != '\0' || tmp_val > UINT_MAX) {
1662 ERROR("turbostat plugin: Invalid TCCActivationTemp '%s'", value);
1665 tcc_activation_temp = (unsigned int)tmp_val;
1666 } else if (strcasecmp("RestoreAffinityPolicy", key) == 0) {
1667 if (strcasecmp("Restore", value) == 0)
1668 affinity_policy = policy_restore_affinity;
1669 else if (strcasecmp("AllCPUs", value) == 0)
1670 affinity_policy = policy_allcpus_affinity;
1672 ERROR("turbostat plugin: Invalid RestoreAffinityPolicy '%s'", value);
1676 ERROR("turbostat plugin: Invalid configuration option '%s'", key);
1682 void module_register(void) {
1683 plugin_register_init(PLUGIN_NAME, turbostat_init);
1684 plugin_register_config(PLUGIN_NAME, turbostat_config, config_keys,