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:
42 #include "utils_time.h"
44 #include <asm/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"
53 * This tool uses the Model-Specific Registers (MSRs) present on Intel processors.
54 * The general description each of these registers, depending on the architecture,
55 * can be found in the IntelĀ® 64 and IA-32 Architectures Software Developer Manual,
56 * Volume 3 Chapter 35.
60 * If set, aperf_mperf_unstable disables a/mperf based stats.
61 * This includes: C0 & C1 states, frequency
63 * This value is automatically set if mperf or aperf go backward
65 static _Bool aperf_mperf_unstable;
68 * Bitmask of the list of core C states supported by the processor.
69 * Currently supported C-states (by this plugin): 3, 6, 7
71 static unsigned int do_core_cstate;
72 static unsigned int config_core_cstate;
73 static _Bool apply_config_core_cstate;
76 * Bitmask of the list of pacages C states supported by the processor.
77 * Currently supported C-states (by this plugin): 2, 3, 6, 7, 8, 9, 10
79 static unsigned int do_pkg_cstate;
80 static unsigned int config_pkg_cstate;
81 static _Bool apply_config_pkg_cstate;
84 * Boolean indicating if the processor supports 'I/O System-Management Interrupt counter'
87 static _Bool config_smi;
88 static _Bool apply_config_smi;
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
99 static _Bool config_dts;
100 static _Bool apply_config_dts;
103 * Boolean indicating if the processor supports 'Package thermal management'
104 * This feature allows the monitoring of the temperature of each package
106 * This feature has two limitations:
107 * - if MSR_IA32_TEMPERATURE_TARGET is not supported, the absolute temperature might be wrong
108 * - Temperatures above the tcc_activation_temp are not recorded
111 static _Bool config_ptm;
112 static _Bool apply_config_ptm;
115 * Thermal Control Circuit Activation Temperature as configured by the user.
116 * This override the automated detection via MSR_IA32_TEMPERATURE_TARGET
117 * and should only be used if the automated detection fails.
119 static unsigned int tcc_activation_temp;
121 static unsigned int do_rapl;
122 static unsigned int config_rapl;
123 static _Bool apply_config_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_DRAM (1 << 1)
130 /* 0x618 MSR_DRAM_POWER_LIMIT */
131 /* 0x619 MSR_DRAM_ENERGY_STATUS */
132 /* 0x61c MSR_DRAM_POWER_INFO */
133 #define RAPL_CORES (1 << 2)
134 /* 0x638 MSR_PP0_POWER_LIMIT */
135 /* 0x639 MSR_PP0_ENERGY_STATUS */
137 #define RAPL_GFX (1 << 3)
138 /* 0x640 MSR_PP1_POWER_LIMIT */
139 /* 0x641 MSR_PP1_ENERGY_STATUS */
140 /* 0x642 MSR_PP1_POLICY */
141 #define TJMAX_DEFAULT 100
143 static cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_saved_affinity_set;
144 static size_t cpu_present_setsize, cpu_affinity_setsize, cpu_saved_affinity_setsize;
146 static struct thread_data {
147 unsigned long long tsc;
148 unsigned long long aperf;
149 unsigned long long mperf;
150 unsigned long long c1;
151 unsigned int smi_count;
154 #define CPU_IS_FIRST_THREAD_IN_CORE 0x2
155 #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
156 } *thread_delta, *thread_even, *thread_odd;
158 static struct core_data {
159 unsigned long long c3;
160 unsigned long long c6;
161 unsigned long long c7;
162 unsigned int core_temp_c;
163 unsigned int core_id;
164 } *core_delta, *core_even, *core_odd;
166 static struct pkg_data {
167 unsigned long long pc2;
168 unsigned long long pc3;
169 unsigned long long pc6;
170 unsigned long long pc7;
171 unsigned long long pc8;
172 unsigned long long pc9;
173 unsigned long long pc10;
174 unsigned int package_id;
175 uint32_t energy_pkg; /* MSR_PKG_ENERGY_STATUS */
176 uint32_t energy_dram; /* MSR_DRAM_ENERGY_STATUS */
177 uint32_t energy_cores; /* MSR_PP0_ENERGY_STATUS */
178 uint32_t energy_gfx; /* MSR_PP1_ENERGY_STATUS */
179 unsigned int tcc_activation_temp;
180 unsigned int pkg_temp_c;
181 } *package_delta, *package_even, *package_odd;
183 #define DELTA_COUNTERS thread_delta, core_delta, package_delta
184 #define ODD_COUNTERS thread_odd, core_odd, package_odd
185 #define EVEN_COUNTERS thread_even, core_even, package_even
186 static _Bool is_even = 1;
188 static _Bool allocated = 0;
189 static _Bool initialized = 0;
191 #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
193 (pkg_no) * topology.num_cores * topology.num_threads + \
194 (core_no) * topology.num_threads + \
196 #define GET_CORE(core_base, core_no, pkg_no) \
198 (pkg_no) * topology.num_cores + \
200 #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
202 struct cpu_topology {
203 unsigned int package_id;
204 unsigned int core_id;
205 _Bool first_core_in_package;
206 _Bool first_thread_in_core;
209 static struct topology {
210 unsigned int max_cpu_id;
211 unsigned int num_packages;
212 unsigned int num_cores;
213 unsigned int num_threads;
214 struct cpu_topology *cpus;
217 static cdtime_t time_even, time_odd, time_delta;
219 static const char *config_keys[] =
223 "SystemManagementInterrupt",
224 "DigitalTemperatureSensor",
225 "PackageThermalManagement",
227 "RunningAveragePowerLimit",
229 static const int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
231 /*****************************
232 * MSR Manipulation helpers *
233 *****************************/
236 * Open a MSR device for reading
237 * Can change the scheduling affinity of the current process if multiple_read is 1
239 static int __attribute__((warn_unused_result))
240 open_msr(unsigned int cpu, _Bool multiple_read)
246 * If we need to do multiple read, let's migrate to the CPU
247 * Otherwise, we would lose time calling functions on another CPU
249 * If we are not yet initialized (cpu_affinity_setsize = 0),
250 * we need to skip this optimisation.
252 if (multiple_read && cpu_affinity_setsize) {
253 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
254 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
255 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1) {
256 ERROR("turbostat plugin: Could not migrate to CPU %d", cpu);
261 ssnprintf(pathname, sizeof(pathname), "/dev/cpu/%d/msr", cpu);
262 fd = open(pathname, O_RDONLY);
264 ERROR("turbostat plugin: failed to open %s", pathname);
271 * Read a single MSR from an open file descriptor
273 static int __attribute__((warn_unused_result))
274 read_msr(int fd, off_t offset, unsigned long long *msr)
278 retval = pread(fd, msr, sizeof *msr, offset);
280 if (retval != sizeof *msr) {
281 ERROR("turbostat plugin: MSR offset 0x%llx read failed",
282 (unsigned long long)offset);
289 * Open a MSR device for reading, read the value asked for and close it.
290 * This call will not affect the scheduling affinity of this thread.
292 static ssize_t __attribute__((warn_unused_result))
293 get_msr(unsigned int cpu, off_t offset, unsigned long long *msr)
298 fd = open_msr(cpu, 0);
301 retval = read_msr(fd, offset, msr);
307 /********************************
308 * Raw data acquisition (1 CPU) *
309 ********************************/
312 * Read every data avalaible for a single CPU
314 * Core data is shared for all threads in one core: extracted only for the first thread
315 * Package data is shared for all core in one package: extracted only for the first thread of the first core
317 * Side effect: migrates to the targeted CPU
319 static int __attribute__((warn_unused_result))
320 get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
322 unsigned int cpu = t->cpu_id;
323 unsigned long long msr;
327 msr_fd = open_msr(cpu, 1);
331 #define READ_MSR(msr, dst) \
333 if (read_msr(msr_fd, msr, dst)) { \
334 ERROR("turbostat plugin: Unable to read " #msr); \
340 READ_MSR(MSR_IA32_TSC, &t->tsc);
342 READ_MSR(MSR_IA32_APERF, &t->aperf);
343 READ_MSR(MSR_IA32_MPERF, &t->mperf);
346 READ_MSR(MSR_SMI_COUNT, &msr);
347 t->smi_count = msr & 0xFFFFFFFF;
350 /* collect core counters only for 1st thread in core */
351 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) {
356 if (do_core_cstate & (1 << 3))
357 READ_MSR(MSR_CORE_C3_RESIDENCY, &c->c3);
358 if (do_core_cstate & (1 << 6))
359 READ_MSR(MSR_CORE_C6_RESIDENCY, &c->c6);
360 if (do_core_cstate & (1 << 7))
361 READ_MSR(MSR_CORE_C7_RESIDENCY, &c->c7);
364 READ_MSR(MSR_IA32_THERM_STATUS, &msr);
365 c->core_temp_c = p->tcc_activation_temp - ((msr >> 16) & 0x7F);
368 /* collect package counters only for 1st core in package */
369 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
374 if (do_pkg_cstate & (1 << 2))
375 READ_MSR(MSR_PKG_C2_RESIDENCY, &p->pc2);
376 if (do_pkg_cstate & (1 << 3))
377 READ_MSR(MSR_PKG_C3_RESIDENCY, &p->pc3);
378 if (do_pkg_cstate & (1 << 6))
379 READ_MSR(MSR_PKG_C6_RESIDENCY, &p->pc6);
380 if (do_pkg_cstate & (1 << 7))
381 READ_MSR(MSR_PKG_C7_RESIDENCY, &p->pc7);
382 if (do_pkg_cstate & (1 << 8))
383 READ_MSR(MSR_PKG_C8_RESIDENCY, &p->pc8);
384 if (do_pkg_cstate & (1 << 9))
385 READ_MSR(MSR_PKG_C9_RESIDENCY, &p->pc9);
386 if (do_pkg_cstate & (1 << 10))
387 READ_MSR(MSR_PKG_C10_RESIDENCY, &p->pc10);
389 if (do_rapl & RAPL_PKG) {
390 READ_MSR(MSR_PKG_ENERGY_STATUS, &msr);
391 p->energy_pkg = msr & 0xFFFFFFFF;
393 if (do_rapl & RAPL_CORES) {
394 READ_MSR(MSR_PP0_ENERGY_STATUS, &msr);
395 p->energy_cores = msr & 0xFFFFFFFF;
397 if (do_rapl & RAPL_DRAM) {
398 READ_MSR(MSR_DRAM_ENERGY_STATUS, &msr);
399 p->energy_dram = msr & 0xFFFFFFFF;
401 if (do_rapl & RAPL_GFX) {
402 READ_MSR(MSR_PP1_ENERGY_STATUS, &msr);
403 p->energy_gfx = msr & 0xFFFFFFFF;
406 READ_MSR(MSR_IA32_PACKAGE_THERM_STATUS, &msr);
407 p->pkg_temp_c = p->tcc_activation_temp - ((msr >> 16) & 0x7F);
416 /**********************************
417 * Evaluating the changes (1 CPU) *
418 **********************************/
421 * Extract the evolution old->new in delta at a package level
422 * (some are not new-delta, e.g. temperature)
425 delta_package(struct pkg_data *delta, const struct pkg_data *new, const struct pkg_data *old)
427 delta->pc2 = new->pc2 - old->pc2;
428 delta->pc3 = new->pc3 - old->pc3;
429 delta->pc6 = new->pc6 - old->pc6;
430 delta->pc7 = new->pc7 - old->pc7;
431 delta->pc8 = new->pc8 - old->pc8;
432 delta->pc9 = new->pc9 - old->pc9;
433 delta->pc10 = new->pc10 - old->pc10;
434 delta->pkg_temp_c = new->pkg_temp_c;
436 delta->energy_pkg = new->energy_pkg - old->energy_pkg;
437 delta->energy_cores = new->energy_cores - old->energy_cores;
438 delta->energy_gfx = new->energy_gfx - old->energy_gfx;
439 delta->energy_dram = new->energy_dram - old->energy_dram;
443 * Extract the evolution old->new in delta at a core level
444 * (some are not new-delta, e.g. temperature)
447 delta_core(struct core_data *delta, const struct core_data *new, const struct core_data *old)
449 delta->c3 = new->c3 - old->c3;
450 delta->c6 = new->c6 - old->c6;
451 delta->c7 = new->c7 - old->c7;
452 delta->core_temp_c = new->core_temp_c;
456 * Extract the evolution old->new in delta at a package level
457 * core_delta is required for c1 estimation (tsc - c0 - all core cstates)
459 static inline int __attribute__((warn_unused_result))
460 delta_thread(struct thread_data *delta, const struct thread_data *new, const struct thread_data *old,
461 const struct core_data *cdelta)
463 delta->tsc = new->tsc - old->tsc;
465 /* check for TSC < 1 Mcycles over interval */
466 if (delta->tsc < (1000 * 1000)) {
467 WARNING("turbostat plugin: Insanely slow TSC rate, TSC stops "
468 "in idle? You can disable all c-states by booting with"
469 " 'idle=poll' or just the deep ones with"
470 " 'processor.max_cstate=1'");
474 delta->c1 = new->c1 - old->c1;
476 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
477 delta->aperf = new->aperf - old->aperf;
478 delta->mperf = new->mperf - old->mperf;
480 if (!aperf_mperf_unstable) {
481 WARNING("turbostat plugin: APERF or MPERF went "
482 "backwards. Frequency results do not cover "
483 "the entire interval. Fix this by running "
484 "Linux-2.6.30 or later.");
486 aperf_mperf_unstable = 1;
491 * As counter collection is not atomic,
492 * it is possible for mperf's non-halted cycles + idle states
493 * to exceed TSC's all cycles: show c1 = 0% in that case.
495 if ((delta->mperf + cdelta->c3 + cdelta->c6 + cdelta->c7) > delta->tsc)
498 /* normal case, derive c1 */
499 delta->c1 = delta->tsc - delta->mperf - cdelta->c3
500 - cdelta->c6 - cdelta->c7;
503 if (delta->mperf == 0) {
504 WARNING("turbostat plugin: cpu%d MPERF 0!", old->cpu_id);
505 delta->mperf = 1; /* divide by 0 protection */
509 delta->smi_count = new->smi_count - old->smi_count;
514 /**********************************
515 * Submitting the results (1 CPU) *
516 **********************************/
519 * Submit one gauge value
522 turbostat_submit (const char *plugin_instance,
523 const char *type, const char *type_instance,
526 value_list_t vl = VALUE_LIST_INIT;
532 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
533 sstrncpy (vl.plugin, PLUGIN_NAME, sizeof (vl.plugin));
534 if (plugin_instance != NULL)
535 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
536 sstrncpy (vl.type, type, sizeof (vl.type));
537 if (type_instance != NULL)
538 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
540 plugin_dispatch_values (&vl);
544 * Submit every data for a single CPU
546 * Core data is shared for all threads in one core: submitted only for the first thread
547 * Package data is shared for all core in one package: submitted only for the first thread of the first core
550 submit_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
552 char name[DATA_MAX_NAME_LEN];
553 double interval_float;
555 interval_float = CDTIME_T_TO_DOUBLE(time_delta);
557 ssnprintf(name, sizeof(name), "cpu%02d", t->cpu_id);
559 if (!aperf_mperf_unstable)
560 turbostat_submit(name, "percent", "c0", 100.0 * t->mperf/t->tsc);
561 if (!aperf_mperf_unstable)
562 turbostat_submit(name, "percent", "c1", 100.0 * t->c1/t->tsc);
564 turbostat_submit(name, "frequency", "average", 1.0 / 1000000 * t->aperf / interval_float);
566 if ((!aperf_mperf_unstable) || (!(t->aperf > t->tsc || t->mperf > t->tsc)))
567 turbostat_submit(name, "frequency", "busy", 1.0 * t->tsc / 1000000 * t->aperf / t->mperf / interval_float);
569 /* Sanity check (should stay stable) */
570 turbostat_submit(name, "gauge", "TSC", 1.0 * t->tsc / 1000000 / interval_float);
574 turbostat_submit(name, "count", NULL, t->smi_count);
576 /* submit per-core data only for 1st thread in core */
577 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
580 ssnprintf(name, sizeof(name), "core%02d", c->core_id);
582 if (do_core_cstate & (1 << 3))
583 turbostat_submit(name, "percent", "c3", 100.0 * c->c3/t->tsc);
584 if (do_core_cstate & (1 << 6))
585 turbostat_submit(name, "percent", "c6", 100.0 * c->c6/t->tsc);
586 if (do_core_cstate & (1 << 7))
587 turbostat_submit(name, "percent", "c7", 100.0 * c->c7/t->tsc);
590 turbostat_submit(name, "temperature", NULL, c->core_temp_c);
592 /* submit per-package data only for 1st core in package */
593 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
596 ssnprintf(name, sizeof(name), "pkg%02d", p->package_id);
599 turbostat_submit(name, "temperature", NULL, p->pkg_temp_c);
601 if (do_pkg_cstate & (1 << 2))
602 turbostat_submit(name, "percent", "pc2", 100.0 * p->pc2/t->tsc);
603 if (do_pkg_cstate & (1 << 3))
604 turbostat_submit(name, "percent", "pc3", 100.0 * p->pc3/t->tsc);
605 if (do_pkg_cstate & (1 << 6))
606 turbostat_submit(name, "percent", "pc6", 100.0 * p->pc6/t->tsc);
607 if (do_pkg_cstate & (1 << 7))
608 turbostat_submit(name, "percent", "pc7", 100.0 * p->pc7/t->tsc);
609 if (do_pkg_cstate & (1 << 8))
610 turbostat_submit(name, "percent", "pc8", 100.0 * p->pc8/t->tsc);
611 if (do_pkg_cstate & (1 << 9))
612 turbostat_submit(name, "percent", "pc9", 100.0 * p->pc9/t->tsc);
613 if (do_pkg_cstate & (1 << 10))
614 turbostat_submit(name, "percent", "pc10", 100.0 * p->pc10/t->tsc);
617 if (do_rapl & RAPL_PKG)
618 turbostat_submit(name, "power", "pkg", p->energy_pkg * rapl_energy_units / interval_float);
619 if (do_rapl & RAPL_CORES)
620 turbostat_submit(name, "power", "cores", p->energy_cores * rapl_energy_units / interval_float);
621 if (do_rapl & RAPL_GFX)
622 turbostat_submit(name, "power", "GFX", p->energy_gfx * rapl_energy_units / interval_float);
623 if (do_rapl & RAPL_DRAM)
624 turbostat_submit(name, "power", "DRAM", p->energy_dram * rapl_energy_units / interval_float);
631 /**********************************
632 * Looping function over all CPUs *
633 **********************************/
636 * Check if a given cpu id is in our compiled list of existing CPUs
639 cpu_is_not_present(unsigned int cpu)
641 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
645 * Loop on all CPUs in topological order
647 * Skip non-present cpus
648 * Return the error code at the first error or 0
650 static int __attribute__((warn_unused_result))
651 for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
652 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
656 for (unsigned int pkg_no = 0; pkg_no < topology.num_packages; ++pkg_no) {
657 for (unsigned int core_no = 0; core_no < topology.num_cores; ++core_no) {
658 for (unsigned int thread_no = 0; thread_no < topology.num_threads; ++thread_no) {
659 struct thread_data *t;
663 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
665 if (cpu_is_not_present(t->cpu_id))
668 c = GET_CORE(core_base, core_no, pkg_no);
669 p = GET_PKG(pkg_base, pkg_no);
671 retval = func(t, c, p);
681 * Dedicated loop: Extract every data evolution for all CPU
683 * Skip non-present cpus
684 * Return the error code at the first error or 0
686 * Core data is shared for all threads in one core: extracted only for the first thread
687 * Package data is shared for all core in one package: extracted only for the first thread of the first core
689 static int __attribute__((warn_unused_result))
690 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,
691 const struct thread_data *thread_old_base, const struct core_data *core_old_base, const struct pkg_data *pkg_old_base)
695 for (unsigned int pkg_no = 0; pkg_no < topology.num_packages; ++pkg_no) {
696 for (unsigned int core_no = 0; core_no < topology.num_cores; ++core_no) {
697 for (unsigned int thread_no = 0; thread_no < topology.num_threads; ++thread_no) {
698 struct thread_data *t_delta;
699 const struct thread_data *t_old, *t_new;
700 struct core_data *c_delta;
702 /* Get correct pointers for threads */
703 t_delta = GET_THREAD(thread_delta, thread_no, core_no, pkg_no);
704 t_new = GET_THREAD(thread_new_base, thread_no, core_no, pkg_no);
705 t_old = GET_THREAD(thread_old_base, thread_no, core_no, pkg_no);
707 /* Skip threads that disappeared */
708 if (cpu_is_not_present(t_delta->cpu_id))
711 /* c_delta is always required for delta_thread */
712 c_delta = GET_CORE(core_delta, core_no, pkg_no);
714 /* calculate core delta only for 1st thread in core */
715 if (t_new->flags & CPU_IS_FIRST_THREAD_IN_CORE) {
716 const struct core_data *c_old, *c_new;
718 c_new = GET_CORE(core_new_base, core_no, pkg_no);
719 c_old = GET_CORE(core_old_base, core_no, pkg_no);
721 delta_core(c_delta, c_new, c_old);
724 /* Always calculate thread delta */
725 retval = delta_thread(t_delta, t_new, t_old, c_delta);
729 /* calculate package delta only for 1st core in package */
730 if (t_new->flags & CPU_IS_FIRST_CORE_IN_PACKAGE) {
731 struct pkg_data *p_delta;
732 const struct pkg_data *p_old, *p_new;
734 p_delta = GET_PKG(package_delta, pkg_no);
735 p_new = GET_PKG(pkg_new_base, pkg_no);
736 p_old = GET_PKG(pkg_old_base, pkg_no);
738 delta_package(p_delta, p_new, p_old);
752 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
753 * the Thermal Control Circuit (TCC) activates.
754 * This is usually equal to tjMax.
756 * Older processors do not have this MSR, so there we guess,
757 * but also allow conficuration over-ride with "TCCActivationTemp".
759 * Several MSR temperature values are in units of degrees-C
760 * below this value, including the Digital Thermal Sensor (DTS),
761 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
763 static int __attribute__((warn_unused_result))
764 set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
766 unsigned long long msr;
767 unsigned int target_c_local;
769 /* tcc_activation_temp is used only for dts or ptm */
770 if (!(do_dts || do_ptm))
773 /* this is a per-package concept */
774 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
777 if (tcc_activation_temp != 0) {
778 p->tcc_activation_temp = tcc_activation_temp;
782 if (get_msr(t->cpu_id, MSR_IA32_TEMPERATURE_TARGET, &msr))
785 target_c_local = (msr >> 16) & 0xFF;
790 p->tcc_activation_temp = target_c_local;
795 p->tcc_activation_temp = TJMAX_DEFAULT;
796 WARNING("turbostat plugin: cpu%d: Guessing tjMax %d C,"
797 " Please use TCCActivationTemp to specify it.",
798 t->cpu_id, p->tcc_activation_temp);
804 * Identify the functionality of the CPU
806 static int __attribute__((warn_unused_result))
809 unsigned int eax, ebx, ecx, edx, max_level;
810 unsigned int fms, family, model;
813 * - EAX: Maximum Input Value for Basic CPUID Information
814 * - EBX: "Genu" (0x756e6547)
815 * - EDX: "ineI" (0x49656e69)
816 * - ECX: "ntel" (0x6c65746e)
818 max_level = ebx = ecx = edx = 0;
819 __get_cpuid(0, &max_level, &ebx, &ecx, &edx);
820 if (ebx != 0x756e6547 && edx != 0x49656e69 && ecx != 0x6c65746e) {
821 ERROR("turbostat plugin: Unsupported CPU (not Intel)");
826 * - EAX: Version Information: Type, Family, Model, and Stepping ID
829 * + 12-13: Processor type
830 * + 16-19: Extended Model ID
831 * + 20-27: Extended Family ID
832 * - EDX: Feature Information:
833 * + 5: Support for MSR read/write operations
835 fms = ebx = ecx = edx = 0;
836 __get_cpuid(1, &fms, &ebx, &ecx, &edx);
837 family = (fms >> 8) & 0xf;
838 model = (fms >> 4) & 0xf;
840 family += (fms >> 20) & 0xf;
841 if (family == 6 || family == 0xf)
842 model += ((fms >> 16) & 0xf) << 4;
843 if (!(edx & (1 << 5))) {
844 ERROR("turbostat plugin: Unsupported CPU (no MSR support)");
851 * + 0: Digital temperature sensor is supported if set
852 * + 6: Package thermal management is supported if set
854 * + 0: Hardware Coordination Feedback Capability (Presence of IA32_MPERF and IA32_APERF).
855 * + 3: The processor supports performance-energy bias preference if set.
856 * It also implies the presence of a new architectural MSR called IA32_ENERGY_PERF_BIAS
858 * This check is valid for both Intel and AMD
860 eax = ebx = ecx = edx = 0;
861 __get_cpuid(0x6, &eax, &ebx, &ecx, &edx);
862 do_dts = eax & (1 << 0);
863 do_ptm = eax & (1 << 6);
864 if (!(ecx & (1 << 0))) {
865 ERROR("turbostat plugin: Unsupported CPU (No APERF)");
870 * Enable or disable C states depending on the model and family
878 do_pkg_cstate = (1 << 2) | (1 << 4) | (1 << 6);
884 do_core_cstate = (1 << 1) | (1 << 6);
885 do_pkg_cstate = (1 << 6);
888 case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
889 case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
890 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
891 case 0x2E: /* Nehalem-EX Xeon - Beckton */
893 do_core_cstate = (1 << 3) | (1 << 6);
894 do_pkg_cstate = (1 << 3) | (1 << 6) | (1 << 7);
897 case 0x25: /* Westmere Client - Clarkdale, Arrandale */
898 case 0x2C: /* Westmere EP - Gulftown */
899 case 0x2F: /* Westmere-EX Xeon - Eagleton */
901 do_core_cstate = (1 << 3) | (1 << 6);
902 do_pkg_cstate = (1 << 3) | (1 << 6) | (1 << 7);
906 case 0x2D: /* SNB Xeon */
908 do_core_cstate = (1 << 3) | (1 << 6) | (1 << 7);
909 do_pkg_cstate = (1 << 2) | (1 << 3) | (1 << 6) | (1 << 7);
913 case 0x3E: /* IVB Xeon */
915 do_core_cstate = (1 << 3) | (1 << 6) | (1 << 7);
916 do_pkg_cstate = (1 << 2) | (1 << 3) | (1 << 6) | (1 << 7);
923 do_core_cstate = (1 << 3) | (1 << 6) | (1 << 7);
924 do_pkg_cstate = (1 << 2) | (1 << 3) | (1 << 6) | (1 << 7);
928 do_core_cstate = (1 << 3) | (1 << 6) | (1 << 7);
929 do_pkg_cstate = (1 << 2) | (1 << 3) | (1 << 6) | (1 << 7) | (1 << 8) | (1 << 9) | (1 << 10);
933 case 0x56: /* BDX-DE */
935 do_core_cstate = (1 << 3) | (1 << 6) | (1 << 7);
936 do_pkg_cstate = (1 << 2) | (1 << 3) | (1 << 6) | (1 << 7);
940 do_core_cstate = (1 << 3) | (1 << 6) | (1 << 7);
941 do_pkg_cstate = (1 << 2) | (1 << 3) | (1 << 6) | (1 << 7) | (1 << 8) | (1 << 9) | (1 << 10);
956 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_GFX;
960 case 0x56: /* BDX-DE */
961 do_rapl = RAPL_PKG | RAPL_DRAM ;
963 case 0x2D: /* SNB Xeon */
964 case 0x3E: /* IVB Xeon */
965 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_DRAM;
969 do_rapl = RAPL_PKG | RAPL_CORES;
975 ERROR("turbostat plugin: Unsupported CPU (family: %#x, "
976 "model: %#x)", family, model);
980 /* Override detected values with configuration */
981 if (apply_config_core_cstate)
982 do_core_cstate = config_core_cstate;
983 if (apply_config_pkg_cstate)
984 do_pkg_cstate = config_pkg_cstate;
985 if (apply_config_smi)
987 if (apply_config_dts)
989 if (apply_config_ptm)
991 if (apply_config_rapl)
992 do_rapl = config_rapl;
995 unsigned long long msr;
996 if (get_msr(0, MSR_RAPL_POWER_UNIT, &msr))
1000 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
1002 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
1009 /********************
1010 * Topology Probing *
1011 ********************/
1014 * Read a single int from a file.
1016 static int __attribute__ ((format(printf,1,2)))
1017 parse_int_file(const char *fmt, ...)
1020 char path[PATH_MAX];
1024 va_start(args, fmt);
1025 len = vsnprintf(path, sizeof(path), fmt, args);
1027 if (len < 0 || len >= PATH_MAX) {
1028 ERROR("turbostat plugin: path truncated: '%s'", path);
1032 filep = fopen(path, "r");
1034 ERROR("turbostat plugin: Failed to open '%s'", path);
1037 if (fscanf(filep, "%d", &value) != 1) {
1038 ERROR("turbostat plugin: Failed to parse number from '%s'", path);
1047 get_threads_on_core(unsigned int cpu)
1055 ssnprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
1056 filep = fopen(path, "r");
1058 ERROR("turbostat plugin: Failed to open '%s'", path);
1063 * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4)
1064 * otherwinse 1 sibling (self).
1066 matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2);
1077 * run func(cpu) on every cpu in /proc/stat
1078 * return max_cpu number
1080 static int __attribute__((warn_unused_result))
1081 for_all_proc_cpus(int (func)(unsigned int))
1084 unsigned int cpu_num;
1087 fp = fopen("/proc/stat", "r");
1089 ERROR("turbostat plugin: Failed to open /proc/stat");
1093 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
1095 ERROR("turbostat plugin: Failed to parse /proc/stat");
1101 retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
1105 retval = func(cpu_num);
1116 * Update the stored topology.max_cpu_id
1119 update_max_cpu_id(unsigned int cpu)
1121 if (topology.max_cpu_id < cpu)
1122 topology.max_cpu_id = cpu;
1127 mark_cpu_present(unsigned int cpu)
1129 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
1133 static int __attribute__((warn_unused_result))
1134 allocate_cpu_set(cpu_set_t ** set, size_t * size) {
1135 *set = CPU_ALLOC(topology.max_cpu_id + 1);
1137 ERROR("turbostat plugin: Unable to allocate CPU state");
1140 *size = CPU_ALLOC_SIZE(topology.max_cpu_id + 1);
1141 CPU_ZERO_S(*size, *set);
1146 * Build a local representation of the cpu distribution
1148 static int __attribute__((warn_unused_result))
1149 topology_probe(void)
1152 unsigned int max_package_id, max_core_id, max_threads;
1153 max_package_id = max_core_id = max_threads = 0;
1155 /* Clean topology */
1156 free(topology.cpus);
1157 memset(&topology, 0, sizeof(topology));
1159 ret = for_all_proc_cpus(update_max_cpu_id);
1163 topology.cpus = calloc(1, (topology.max_cpu_id + 1) * sizeof(struct cpu_topology));
1164 if (topology.cpus == NULL) {
1165 ERROR("turbostat plugin: Unable to allocate memory for CPU topology");
1169 ret = allocate_cpu_set(&cpu_present_set, &cpu_present_setsize);
1172 ret = allocate_cpu_set(&cpu_affinity_set, &cpu_affinity_setsize);
1175 ret = allocate_cpu_set(&cpu_saved_affinity_set, &cpu_saved_affinity_setsize);
1179 ret = for_all_proc_cpus(mark_cpu_present);
1185 * find max_core_id, max_package_id
1187 for (unsigned int i = 0; i <= topology.max_cpu_id; ++i) {
1188 unsigned int num_threads;
1189 struct cpu_topology *cpu = &topology.cpus[i];
1191 if (cpu_is_not_present(i)) {
1192 WARNING("turbostat plugin: cpu%d NOT PRESENT", i);
1196 ret = parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", i);
1200 cpu->package_id = (unsigned int) ret;
1201 if (cpu->package_id > max_package_id)
1202 max_package_id = cpu->package_id;
1204 ret = parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", i);
1208 cpu->core_id = (unsigned int) ret;
1209 if (cpu->core_id > max_core_id)
1210 max_core_id = cpu->core_id;
1211 ret = parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", i);
1214 else if ((unsigned int) ret == i)
1215 cpu->first_core_in_package = 1;
1217 ret = get_threads_on_core(i);
1221 num_threads = (unsigned int) ret;
1222 if (num_threads > max_threads)
1223 max_threads = num_threads;
1224 ret = parse_int_file("/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", i);
1227 else if ((unsigned int) ret == i)
1228 cpu->first_thread_in_core = 1;
1230 DEBUG("turbostat plugin: cpu %d pkg %d core %d\n",
1231 i, cpu->package_id, cpu->core_id);
1233 /* Num is max + 1 (need to count 0) */
1234 topology.num_packages = max_package_id + 1;
1235 topology.num_cores = max_core_id + 1;
1236 topology.num_threads = max_threads;
1240 free(topology.cpus);
1245 /************************
1246 * Main alloc/init/free *
1247 ************************/
1250 allocate_counters(struct thread_data **threads, struct core_data **cores, struct pkg_data **packages)
1252 unsigned int total_threads, total_cores;
1254 if ((topology.num_threads == 0)
1255 || (topology.num_cores == 0)
1256 || (topology.num_packages == 0))
1258 ERROR ("turbostat plugin: Invalid topology: %u threads, %u cores, %u packages",
1259 topology.num_threads, topology.num_cores, topology.num_packages);
1263 total_threads = topology.num_threads * topology.num_cores * topology.num_packages;
1264 *threads = calloc(total_threads, sizeof(struct thread_data));
1265 if (*threads == NULL)
1267 ERROR ("turbostat plugin: calloc failed");
1271 for (unsigned int i = 0; i < total_threads; ++i)
1272 (*threads)[i].cpu_id = topology.max_cpu_id + 1;
1274 total_cores = topology.num_cores * topology.num_packages;
1275 *cores = calloc(total_cores, sizeof(struct core_data));
1278 ERROR ("turbostat plugin: calloc failed");
1283 *packages = calloc(topology.num_packages, sizeof(struct pkg_data));
1284 if (*packages == NULL)
1286 ERROR ("turbostat plugin: calloc failed");
1296 init_counter(struct thread_data *thread_base, struct core_data *core_base,
1297 struct pkg_data *pkg_base, unsigned int cpu_id)
1299 struct thread_data *t;
1300 struct core_data *c;
1302 struct cpu_topology *cpu = &topology.cpus[cpu_id];
1304 t = GET_THREAD(thread_base, !(cpu->first_thread_in_core), cpu->core_id, cpu->package_id);
1305 c = GET_CORE(core_base, cpu->core_id, cpu->package_id);
1306 p = GET_PKG(pkg_base, cpu->package_id);
1309 if (cpu->first_thread_in_core)
1310 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
1311 if (cpu->first_core_in_package)
1312 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
1314 c->core_id = cpu->core_id;
1315 p->package_id = cpu->package_id;
1319 initialize_counters(void)
1321 for (unsigned int cpu_id = 0; cpu_id <= topology.max_cpu_id; ++cpu_id) {
1322 if (cpu_is_not_present(cpu_id))
1324 init_counter(EVEN_COUNTERS, cpu_id);
1325 init_counter(ODD_COUNTERS, cpu_id);
1326 init_counter(DELTA_COUNTERS, cpu_id);
1333 free_all_buffers(void)
1338 CPU_FREE(cpu_present_set);
1339 cpu_present_set = NULL;
1340 cpu_present_setsize = 0;
1342 CPU_FREE(cpu_affinity_set);
1343 cpu_affinity_set = NULL;
1344 cpu_affinity_setsize = 0;
1346 CPU_FREE(cpu_saved_affinity_set);
1347 cpu_saved_affinity_set = NULL;
1348 cpu_saved_affinity_setsize = 0;
1356 package_even = NULL;
1368 free(package_delta);
1370 thread_delta = NULL;
1372 package_delta = NULL;
1376 /**********************
1377 * Collectd functions *
1378 **********************/
1380 #define DO_OR_GOTO_ERR(something) \
1382 ret = (something); \
1387 static int setup_all_buffers(void)
1391 DO_OR_GOTO_ERR(topology_probe());
1392 DO_OR_GOTO_ERR(allocate_counters(&thread_even, &core_even, &package_even));
1393 DO_OR_GOTO_ERR(allocate_counters(&thread_odd, &core_odd, &package_odd));
1394 DO_OR_GOTO_ERR(allocate_counters(&thread_delta, &core_delta, &package_delta));
1395 initialize_counters();
1396 DO_OR_GOTO_ERR(for_all_cpus(set_temperature_target, EVEN_COUNTERS));
1397 DO_OR_GOTO_ERR(for_all_cpus(set_temperature_target, ODD_COUNTERS));
1407 turbostat_read(void)
1412 if ((ret = setup_all_buffers()) < 0)
1416 if (for_all_proc_cpus(cpu_is_not_present)) {
1418 if ((ret = setup_all_buffers()) < 0)
1420 if (for_all_proc_cpus(cpu_is_not_present)) {
1421 ERROR("turbostat plugin: CPU appeared just after "
1427 /* Saving the scheduling affinity, as it will be modified by get_counters */
1428 if (sched_getaffinity(0, cpu_saved_affinity_setsize, cpu_saved_affinity_set) != 0) {
1429 ERROR("turbostat plugin: Unable to save the CPU affinity");
1434 if ((ret = for_all_cpus(get_counters, EVEN_COUNTERS)) < 0)
1436 time_even = cdtime();
1444 if ((ret = for_all_cpus(get_counters, ODD_COUNTERS)) < 0)
1446 time_odd = cdtime();
1448 time_delta = time_odd - time_even;
1449 if ((ret = for_all_cpus_delta(ODD_COUNTERS, EVEN_COUNTERS)) < 0)
1451 if ((ret = for_all_cpus(submit_counters, DELTA_COUNTERS)) < 0)
1454 if ((ret = for_all_cpus(get_counters, EVEN_COUNTERS)) < 0)
1456 time_even = cdtime();
1458 time_delta = time_even - time_odd;
1459 if ((ret = for_all_cpus_delta(EVEN_COUNTERS, ODD_COUNTERS)) < 0)
1461 if ((ret = for_all_cpus(submit_counters, DELTA_COUNTERS)) < 0)
1467 * Let's restore the affinity
1468 * This might fail if the number of CPU changed, but we can't do anything in that case..
1470 (void)sched_setaffinity(0, cpu_saved_affinity_setsize, cpu_saved_affinity_set);
1475 check_permissions(void)
1478 if (getuid() == 0) {
1479 /* We have everything we need */
1481 #if !defined(HAVE_SYS_CAPABILITY_H) && !defined(CAP_SYS_RAWIO)
1483 ERROR("turbostat plugin: Initialization failed: this plugin "
1484 "requires collectd to run as root");
1487 #else /* HAVE_SYS_CAPABILITY_H && CAP_SYS_RAWIO */
1492 if (check_capability(CAP_SYS_RAWIO) != 0) {
1493 WARNING("turbostat plugin: Collectd doesn't have the "
1494 "CAP_SYS_RAWIO capability. If you don't want to run "
1495 "collectd as root, try running \"setcap "
1496 "cap_sys_rawio=ep\" on collectd binary");
1500 if (euidaccess("/dev/cpu/0/msr", R_OK)) {
1501 WARNING("turbostat plugin: Collectd cannot open "
1502 "/dev/cpu/0/msr. If you don't want to run collectd as "
1503 "root, you need to change the ownership (chown) and "
1504 "permissions on /dev/cpu/*/msr to allow such access");
1509 ERROR("turbostat plugin: Initialization failed: this plugin "
1510 "requires collectd to either to run as root or give "
1511 "collectd a special capability (CAP_SYS_RAWIO) and read "
1512 "access to /dev/cpu/*/msr (see previous warnings)");
1514 #endif /* HAVE_SYS_CAPABILITY_H && CAP_SYS_RAWIO */
1518 turbostat_init(void)
1523 if (stat("/dev/cpu/0/msr", &sb)) {
1524 ERROR("turbostat plugin: Initialization failed: /dev/cpu/0/msr "
1525 "does not exist while the CPU supports MSR. You may be "
1526 "missing the corresponding kernel module, please try '# "
1531 DO_OR_GOTO_ERR(check_permissions());
1533 DO_OR_GOTO_ERR(probe_cpu());
1535 DO_OR_GOTO_ERR(setup_all_buffers());
1537 plugin_register_read(PLUGIN_NAME, turbostat_read);
1546 turbostat_config(const char *key, const char *value)
1548 long unsigned int tmp_val;
1551 if (strcasecmp("CoreCstates", key) == 0) {
1552 tmp_val = strtoul(value, &end, 0);
1553 if (*end != '\0' || tmp_val > UINT_MAX) {
1554 ERROR("turbostat plugin: Invalid CoreCstates '%s'",
1558 config_core_cstate = (unsigned int) tmp_val;
1559 apply_config_core_cstate = 1;
1560 } else if (strcasecmp("PackageCstates", key) == 0) {
1561 tmp_val = strtoul(value, &end, 0);
1562 if (*end != '\0' || tmp_val > UINT_MAX) {
1563 ERROR("turbostat plugin: Invalid PackageCstates '%s'",
1567 config_pkg_cstate = (unsigned int) tmp_val;
1568 apply_config_pkg_cstate = 1;
1569 } else if (strcasecmp("SystemManagementInterrupt", key) == 0) {
1570 config_smi = IS_TRUE(value);
1571 apply_config_smi = 1;
1572 } else if (strcasecmp("DigitalTemperatureSensor", key) == 0) {
1573 config_dts = IS_TRUE(value);
1574 apply_config_dts = 1;
1575 } else if (strcasecmp("PackageThermalManagement", key) == 0) {
1576 config_ptm = IS_TRUE(value);
1577 apply_config_ptm = 1;
1578 } else if (strcasecmp("RunningAveragePowerLimit", key) == 0) {
1579 tmp_val = strtoul(value, &end, 0);
1580 if (*end != '\0' || tmp_val > UINT_MAX) {
1581 ERROR("turbostat plugin: Invalid RunningAveragePowerLimit '%s'",
1585 config_rapl = (unsigned int) tmp_val;
1586 apply_config_rapl = 1;
1587 } else if (strcasecmp("TCCActivationTemp", key) == 0) {
1588 tmp_val = strtoul(value, &end, 0);
1589 if (*end != '\0' || tmp_val > UINT_MAX) {
1590 ERROR("turbostat plugin: Invalid TCCActivationTemp '%s'",
1594 tcc_activation_temp = (unsigned int) tmp_val;
1596 ERROR("turbostat plugin: Invalid configuration option '%s'",
1603 void module_register(void)
1605 plugin_register_init(PLUGIN_NAME, turbostat_init);
1606 plugin_register_config(PLUGIN_NAME, turbostat_config, config_keys, config_keys_num);