2 * turbostat -- Log CPU frequency and C-state residency
3 * on modern Intel turbo-capable processors for collectd.
5 * Based on the 'turbostat' tool of the Linux kernel, found at
6 * linux/tools/power/x86/turbostat/turbostat.c:
8 * Copyright (c) 2013 Intel Corporation.
9 * Len Brown <len.brown@intel.com>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms and conditions of the GNU General Public License,
13 * version 2, as published by the Free Software Foundation.
15 * This program is distributed in the hope it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
20 * You should have received a copy of the GNU General Public License along with
21 * this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
24 * Ported to collectd by Vincent Brillault <git@lerya.net>
28 * _GNU_SOURCE is required because of the following functions:
38 #include <asm/msr-index.h>
43 #include <sys/types.h>
46 #include <sys/resource.h>
61 #define PLUGIN_NAME "turbostat"
63 static const char *proc_stat = "/proc/stat";
64 static unsigned int skip_c0;
65 static unsigned int skip_c1;
66 static unsigned int do_core_cstate;
67 static unsigned int do_pkg_cstate;
68 static unsigned int do_rapl;
69 static unsigned int do_dts;
70 static unsigned int do_ptm;
71 static unsigned int tcc_activation_temp;
72 static double rapl_energy_units;
74 #define RAPL_PKG (1 << 0)
75 /* 0x610 MSR_PKG_POWER_LIMIT */
76 /* 0x611 MSR_PKG_ENERGY_STATUS */
77 #define RAPL_PKG_PERF_STATUS (1 << 1)
78 /* 0x613 MSR_PKG_PERF_STATUS */
79 #define RAPL_PKG_POWER_INFO (1 << 2)
80 /* 0x614 MSR_PKG_POWER_INFO */
82 #define RAPL_DRAM (1 << 3)
83 /* 0x618 MSR_DRAM_POWER_LIMIT */
84 /* 0x619 MSR_DRAM_ENERGY_STATUS */
85 /* 0x61c MSR_DRAM_POWER_INFO */
86 #define RAPL_DRAM_PERF_STATUS (1 << 4)
87 /* 0x61b MSR_DRAM_PERF_STATUS */
89 #define RAPL_CORES (1 << 5)
90 /* 0x638 MSR_PP0_POWER_LIMIT */
91 /* 0x639 MSR_PP0_ENERGY_STATUS */
92 #define RAPL_CORE_POLICY (1 << 6)
93 /* 0x63a MSR_PP0_POLICY */
96 #define RAPL_GFX (1 << 7)
97 /* 0x640 MSR_PP1_POWER_LIMIT */
98 /* 0x641 MSR_PP1_ENERGY_STATUS */
99 /* 0x642 MSR_PP1_POLICY */
100 #define TJMAX_DEFAULT 100
102 int aperf_mperf_unstable;
105 cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_saved_affinity_set;
106 size_t cpu_present_setsize, cpu_affinity_setsize, cpu_saved_affinity_setsize;
109 unsigned long long tsc;
110 unsigned long long aperf;
111 unsigned long long mperf;
112 unsigned long long c1;
113 unsigned int smi_count;
116 #define CPU_IS_FIRST_THREAD_IN_CORE 0x2
117 #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
118 } *thread_even, *thread_odd;
121 unsigned long long c3;
122 unsigned long long c6;
123 unsigned long long c7;
124 unsigned int core_temp_c;
125 unsigned int core_id;
126 } *core_even, *core_odd;
129 unsigned long long pc2;
130 unsigned long long pc3;
131 unsigned long long pc6;
132 unsigned long long pc7;
133 unsigned long long pc8;
134 unsigned long long pc9;
135 unsigned long long pc10;
136 unsigned int package_id;
137 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
138 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
139 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
140 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
141 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
142 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
143 unsigned int tcc_activation_temp;
144 unsigned int pkg_temp_c;
145 } *package_even, *package_odd;
147 #define ODD_COUNTERS thread_odd, core_odd, package_odd
148 #define EVEN_COUNTERS thread_even, core_even, package_even
149 static _Bool is_even = 1;
151 static _Bool allocated = 0;
152 static _Bool initialized = 0;
154 #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
155 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
156 topo.num_threads_per_core + \
157 (core_no) * topo.num_threads_per_core + (thread_no))
158 #define GET_CORE(core_base, core_no, pkg_no) \
159 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
160 #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
167 int num_cores_per_pkg;
168 int num_threads_per_core;
171 struct timeval tv_even, tv_odd, tv_delta;
176 ERR_CPU_SAVE_SCHED_AFFINITY,
180 ERR_MSR_CORE_C3_RESIDENCY,
181 ERR_MSR_CORE_C6_RESIDENCY,
182 ERR_MSR_CORE_C7_RESIDENCY,
183 ERR_MSR_IA32_THERM_STATUS,
184 ERR_MSR_PKG_C3_RESIDENCY,
185 ERR_MSR_PKG_C6_RESIDENCY,
186 ERR_MSR_PKG_C2_RESIDENCY,
187 ERR_MSR_PKG_C7_RESIDENCY,
188 ERR_MSR_PKG_C8_RESIDENCY,
189 ERR_MSR_PKG_C9_RESIDENCY,
190 ERR_MSR_PKG_C10_RESIDENCY,
191 ERR_MSR_PKG_ENERGY_STATUS,
192 ERR_MSR_PKG_POWER_INFO,
193 ERR_MSR_PP0_ENERGY_STATUS,
194 ERR_MSR_DRAM_ENERGY_STATUS,
195 ERR_MSR_PP1_ENERGY_STATUS,
196 ERR_MSR_PKG_PERF_STATUS,
197 ERR_MSR_DRAM_PERF_STATUS,
198 ERR_MSR_IA32_PACKAGE_THERM_STATUS,
204 ERR_CANT_READ_NUMBER,
205 ERR_CANT_READ_PROC_STAT,
206 ERR_NO_INVARIANT_TSC,
214 static int setup_all_buffers(void);
217 cpu_is_not_present(int cpu)
219 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
222 * run func(thread, core, package) in topology order
223 * skip non-present cpus
226 static int __attribute__((warn_unused_result))
227 for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
228 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
230 int retval, pkg_no, core_no, thread_no;
232 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
233 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
234 for (thread_no = 0; thread_no <
235 topo.num_threads_per_core; ++thread_no) {
236 struct thread_data *t;
240 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
242 if (cpu_is_not_present(t->cpu_id))
245 c = GET_CORE(core_base, core_no, pkg_no);
246 p = GET_PKG(pkg_base, pkg_no);
248 retval = func(t, c, p);
257 static int __attribute__((warn_unused_result))
258 open_msr(int cpu, _Bool multiple_read)
264 * If we need to do multiple read, let's migrate to the CPU
265 * Otherwise, we would lose time calling functions on another CPU
267 * If we are not yet initialized (cpu_affinity_setsize = 0),
268 * we need to skip this optimisation.
270 if (multiple_read && cpu_affinity_setsize) {
271 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
272 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
273 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1) {
274 ERROR("Could not migrate to CPU %d", cpu);
275 return -ERR_CPU_MIGRATE;
279 ssnprintf(pathname, sizeof(pathname), "/dev/cpu/%d/msr", cpu);
280 fd = open(pathname, O_RDONLY);
282 return -ERR_CANT_OPEN_MSR;
286 static int __attribute__((warn_unused_result))
287 read_msr(int fd, off_t offset, unsigned long long *msr)
291 retval = pread(fd, msr, sizeof *msr, offset);
293 if (retval != sizeof *msr) {
294 ERROR("MSR offset 0x%llx read failed", (unsigned long long)offset);
300 static int __attribute__((warn_unused_result))
301 get_msr(int cpu, off_t offset, unsigned long long *msr)
306 fd = open_msr(cpu, 0);
309 retval = read_msr(fd, offset, msr);
314 #define DELTA_WRAP32(new, old) \
318 old = 0x100000000 + new - old; \
322 delta_package(struct pkg_data *new, struct pkg_data *old)
324 old->pc2 = new->pc2 - old->pc2;
325 old->pc3 = new->pc3 - old->pc3;
326 old->pc6 = new->pc6 - old->pc6;
327 old->pc7 = new->pc7 - old->pc7;
328 old->pc8 = new->pc8 - old->pc8;
329 old->pc9 = new->pc9 - old->pc9;
330 old->pc10 = new->pc10 - old->pc10;
331 old->pkg_temp_c = new->pkg_temp_c;
333 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
334 DELTA_WRAP32(new->energy_cores, old->energy_cores);
335 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
336 DELTA_WRAP32(new->energy_dram, old->energy_dram);
337 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
338 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
342 delta_core(struct core_data *new, struct core_data *old)
344 old->c3 = new->c3 - old->c3;
345 old->c6 = new->c6 - old->c6;
346 old->c7 = new->c7 - old->c7;
347 old->core_temp_c = new->core_temp_c;
353 static int __attribute__((warn_unused_result))
354 delta_thread(struct thread_data *new, struct thread_data *old,
355 struct core_data *core_delta)
357 old->tsc = new->tsc - old->tsc;
359 /* check for TSC < 1 Mcycles over interval */
360 if (old->tsc < (1000 * 1000)) {
361 WARNING("Insanely slow TSC rate, TSC stops in idle? ");
362 WARNING("You can disable all c-states by booting with \"idle=poll\" ");
363 WARNING("or just the deep ones with \"processor.max_cstate=1\"");
367 old->c1 = new->c1 - old->c1;
369 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
370 old->aperf = new->aperf - old->aperf;
371 old->mperf = new->mperf - old->mperf;
374 if (!aperf_mperf_unstable) {
375 WARNING(" APERF or MPERF went backwards * ");
376 WARNING("* Frequency results do not cover entire interval *");
377 WARNING("* fix this by running Linux-2.6.30 or later *");
379 aperf_mperf_unstable = 1;
382 * mperf delta is likely a huge "positive" number
383 * can not use it for calculating c0 time
391 * As counter collection is not atomic,
392 * it is possible for mperf's non-halted cycles + idle states
393 * to exceed TSC's all cycles: show c1 = 0% in that case.
395 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
398 /* normal case, derive c1 */
399 old->c1 = old->tsc - old->mperf - core_delta->c3
400 - core_delta->c6 - core_delta->c7;
403 if (old->mperf == 0) {
404 WARNING("cpu%d MPERF 0!", old->cpu_id);
405 old->mperf = 1; /* divide by 0 protection */
408 old->smi_count = new->smi_count - old->smi_count;
413 static int __attribute__((warn_unused_result))
414 delta_cpu(struct thread_data *t, struct core_data *c,
415 struct pkg_data *p, struct thread_data *t2,
416 struct core_data *c2, struct pkg_data *p2)
420 /* calculate core delta only for 1st thread in core */
421 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
424 /* always calculate thread delta */
425 ret = delta_thread(t, t2, c2); /* c2 is core delta */
429 /* calculate package delta only for 1st core in package */
430 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
431 delta_package(p, p2);
439 * acquire and record local counters for that cpu
441 static int __attribute__((warn_unused_result))
442 get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
445 unsigned long long msr;
449 msr_fd = open_msr(cpu, 1);
453 #define READ_MSR(msr, dst) \
455 if (read_msr(msr_fd, msr, dst)) { \
456 retval = -ERR_##msr; \
461 READ_MSR(MSR_IA32_TSC, &t->tsc);
463 READ_MSR(MSR_IA32_APERF, &t->aperf);
464 READ_MSR(MSR_IA32_MPERF, &t->mperf);
466 READ_MSR(MSR_SMI_COUNT, &msr);
467 t->smi_count = msr & 0xFFFFFFFF;
469 /* collect core counters only for 1st thread in core */
470 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) {
475 if (do_core_cstate & (1 << 3))
476 READ_MSR(MSR_CORE_C3_RESIDENCY, &c->c3);
477 if (do_core_cstate & (1 << 6))
478 READ_MSR(MSR_CORE_C6_RESIDENCY, &c->c6);
479 if (do_core_cstate & (1 << 7))
480 READ_MSR(MSR_CORE_C7_RESIDENCY, &c->c7);
483 READ_MSR(MSR_IA32_THERM_STATUS, &msr);
484 c->core_temp_c = p->tcc_activation_temp - ((msr >> 16) & 0x7F);
487 /* collect package counters only for 1st core in package */
488 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
493 if (do_pkg_cstate & (1 << 2))
494 READ_MSR(MSR_PKG_C2_RESIDENCY, &p->pc2);
495 if (do_pkg_cstate & (1 << 3))
496 READ_MSR(MSR_PKG_C3_RESIDENCY, &p->pc3);
497 if (do_pkg_cstate & (1 << 6))
498 READ_MSR(MSR_PKG_C6_RESIDENCY, &p->pc6);
499 if (do_pkg_cstate & (1 << 7))
500 READ_MSR(MSR_PKG_C7_RESIDENCY, &p->pc7);
501 if (do_pkg_cstate & (1 << 8))
502 READ_MSR(MSR_PKG_C8_RESIDENCY, &p->pc8);
503 if (do_pkg_cstate & (1 << 9))
504 READ_MSR(MSR_PKG_C9_RESIDENCY, &p->pc9);
505 if (do_pkg_cstate & (1 << 10))
506 READ_MSR(MSR_PKG_C10_RESIDENCY, &p->pc10);
508 if (do_rapl & RAPL_PKG) {
509 READ_MSR(MSR_PKG_ENERGY_STATUS, &msr);
510 p->energy_pkg = msr & 0xFFFFFFFF;
512 if (do_rapl & RAPL_CORES) {
513 READ_MSR(MSR_PP0_ENERGY_STATUS, &msr);
514 p->energy_cores = msr & 0xFFFFFFFF;
516 if (do_rapl & RAPL_DRAM) {
517 READ_MSR(MSR_DRAM_ENERGY_STATUS, &msr);
518 p->energy_dram = msr & 0xFFFFFFFF;
520 if (do_rapl & RAPL_GFX) {
521 READ_MSR(MSR_PP1_ENERGY_STATUS, &msr);
522 p->energy_gfx = msr & 0xFFFFFFFF;
524 if (do_rapl & RAPL_PKG_PERF_STATUS) {
525 READ_MSR(MSR_PKG_PERF_STATUS, &msr);
526 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
528 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
529 READ_MSR(MSR_DRAM_PERF_STATUS, &msr);
530 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
533 READ_MSR(MSR_IA32_PACKAGE_THERM_STATUS, &msr);
534 p->pkg_temp_c = p->tcc_activation_temp - ((msr >> 16) & 0x7F);
543 free_all_buffers(void)
548 CPU_FREE(cpu_present_set);
549 cpu_present_set = NULL;
552 CPU_FREE(cpu_affinity_set);
553 cpu_affinity_set = NULL;
554 cpu_affinity_setsize = 0;
556 CPU_FREE(cpu_saved_affinity_set);
557 cpu_saved_affinity_set = NULL;
558 cpu_saved_affinity_setsize = 0;
578 * Parse a file containing a single int.
580 static int __attribute__ ((format(printf,1,2)))
581 parse_int_file(const char *fmt, ...)
589 vsnprintf(path, sizeof(path), fmt, args);
591 filep = fopen(path, "r");
593 ERROR("%s: open failed", path);
594 return -ERR_CANT_OPEN_FILE;
596 if (fscanf(filep, "%d", &value) != 1) {
597 ERROR("%s: failed to parse number from file", path);
598 return -ERR_CANT_READ_NUMBER;
605 * cpu_is_first_sibling_in_core(cpu)
606 * return 1 if given CPU is 1st HT sibling in the core
609 cpu_is_first_sibling_in_core(int cpu)
611 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
615 * cpu_is_first_core_in_package(cpu)
616 * return 1 if given CPU is 1st core in package
619 cpu_is_first_core_in_package(int cpu)
621 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
625 get_physical_package_id(int cpu)
627 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
633 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
637 get_num_ht_siblings(int cpu)
645 ssnprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
646 filep = fopen(path, "r");
648 ERROR("%s: open failed", path);
649 return -ERR_CANT_OPEN_FILE;
653 * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4)
654 * otherwinse 1 sibling (self).
656 matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2);
667 * run func(thread, core, package) in topology order
668 * skip non-present cpus
672 static int __attribute__((warn_unused_result))
673 for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
674 struct pkg_data *, struct thread_data *, struct core_data *,
675 struct pkg_data *), struct thread_data *thread_base,
676 struct core_data *core_base, struct pkg_data *pkg_base,
677 struct thread_data *thread_base2, struct core_data *core_base2,
678 struct pkg_data *pkg_base2)
680 int retval, pkg_no, core_no, thread_no;
682 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
683 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
684 for (thread_no = 0; thread_no <
685 topo.num_threads_per_core; ++thread_no) {
686 struct thread_data *t, *t2;
687 struct core_data *c, *c2;
688 struct pkg_data *p, *p2;
690 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
692 if (cpu_is_not_present(t->cpu_id))
695 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
697 c = GET_CORE(core_base, core_no, pkg_no);
698 c2 = GET_CORE(core_base2, core_no, pkg_no);
700 p = GET_PKG(pkg_base, pkg_no);
701 p2 = GET_PKG(pkg_base2, pkg_no);
703 retval = func(t, c, p, t2, c2, p2);
713 * run func(cpu) on every cpu in /proc/stat
714 * return max_cpu number
716 static int __attribute__((warn_unused_result))
717 for_all_proc_cpus(int (func)(int))
723 fp = fopen(proc_stat, "r");
725 ERROR("%s: open failed", proc_stat);
726 return -ERR_CANT_OPEN_FILE;
729 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
731 ERROR("%s: failed to parse format", proc_stat);
732 return -ERR_CANT_READ_PROC_STAT;
736 retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
740 retval = func(cpu_num);
752 * remember the last one seen, it will be the max
757 if (topo.max_cpu_num < cpu)
758 topo.max_cpu_num = cpu;
764 mark_cpu_present(int cpu)
766 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
772 turbostat_submit (const char *plugin_instance,
773 const char *type, const char *type_instance,
776 value_list_t vl = VALUE_LIST_INIT;
782 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
783 sstrncpy (vl.plugin, PLUGIN_NAME, sizeof (vl.plugin));
784 if (plugin_instance != NULL)
785 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
786 sstrncpy (vl.type, type, sizeof (vl.type));
787 if (type_instance != NULL)
788 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
790 plugin_dispatch_values (&vl);
794 * column formatting convention & formats
795 * package: "pk" 2 columns %2d
796 * core: "cor" 3 columns %3d
797 * CPU: "CPU" 3 columns %3d
802 * GHz: "GHz" 3 columns %3.2
803 * TSC: "TSC" 3 columns %3.2
804 * SMI: "SMI" 4 columns %4d
805 * percentage " %pc3" %6.2
806 * Perf Status percentage: %5.2
807 * "CTMP" 4 columns %4d
811 submit_counters(struct thread_data *t, struct core_data *c,
815 double interval_float;
817 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
819 ssnprintf(name, sizeof(name), "cpu%02d", t->cpu_id);
822 turbostat_submit(name, "percent", "c0", 100.0 * t->mperf/t->tsc);
824 turbostat_submit(name, "percent", "c1", 100.0 * t->c1/t->tsc);
827 if ((!aperf_mperf_unstable) || (!(t->aperf > t->tsc || t->mperf > t->tsc)))
828 turbostat_submit(NULL, "frequency", name, 1.0 * t->tsc / 1000000000 * t->aperf / t->mperf / interval_float);
831 turbostat_submit(NULL, "current", name, t->smi_count);
833 /* print per-core data only for 1st thread in core */
834 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
837 ssnprintf(name, sizeof(name), "core%02d", c->core_id);
839 if (do_core_cstate & (1 << 3))
840 turbostat_submit(name, "percent", "c3", 100.0 * c->c3/t->tsc);
841 if (do_core_cstate & (1 << 6))
842 turbostat_submit(name, "percent", "c6", 100.0 * c->c6/t->tsc);
843 if (do_core_cstate & (1 << 7))
844 turbostat_submit(name, "percent", "c7", 100.0 * c->c7/t->tsc);
847 turbostat_submit(NULL, "temperature", name, c->core_temp_c);
849 /* print per-package data only for 1st core in package */
850 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
853 ssnprintf(name, sizeof(name), "pkg%02d", p->package_id);
856 turbostat_submit(NULL, "temperature", name, p->pkg_temp_c);
858 if (do_pkg_cstate & (1 << 2))
859 turbostat_submit(name, "percent", "pc2", 100.0 * p->pc2/t->tsc);
860 if (do_pkg_cstate & (1 << 3))
861 turbostat_submit(name, "percent", "pc3", 100.0 * p->pc3/t->tsc);
862 if (do_pkg_cstate & (1 << 6))
863 turbostat_submit(name, "percent", "pc6", 100.0 * p->pc6/t->tsc);
864 if (do_pkg_cstate & (1 << 7))
865 turbostat_submit(name, "percent", "pc7", 100.0 * p->pc7/t->tsc);
866 if (do_pkg_cstate & (1 << 8))
867 turbostat_submit(name, "percent", "pc8", 100.0 * p->pc8/t->tsc);
868 if (do_pkg_cstate & (1 << 9))
869 turbostat_submit(name, "percent", "pc9", 100.0 * p->pc9/t->tsc);
870 if (do_pkg_cstate & (1 << 10))
871 turbostat_submit(name, "percent", "pc10", 100.0 * p->pc10/t->tsc);
874 if (do_rapl & RAPL_PKG)
875 turbostat_submit(name, "power", "Pkg_W", p->energy_pkg * rapl_energy_units / interval_float);
876 if (do_rapl & RAPL_CORES)
877 turbostat_submit(name, "power", "Cor_W", p->energy_cores * rapl_energy_units / interval_float);
878 if (do_rapl & RAPL_GFX)
879 turbostat_submit(name, "power", "GFX_W", p->energy_gfx * rapl_energy_units / interval_float);
880 if (do_rapl & RAPL_DRAM)
881 turbostat_submit(name, "power", "RAM_W", p->energy_dram * rapl_energy_units / interval_float);
888 turbostat_read(user_data_t * not_used)
893 if ((ret = setup_all_buffers()) < 0)
897 if (for_all_proc_cpus(cpu_is_not_present)) {
899 if ((ret = setup_all_buffers()) < 0)
901 if (for_all_proc_cpus(cpu_is_not_present))
902 return -ERR_CPU_NOT_PRESENT;
905 /* Saving the scheduling affinity, as it will be modified by get_counters */
906 if (sched_getaffinity(0, cpu_saved_affinity_setsize, cpu_saved_affinity_set) != 0)
907 return -ERR_CPU_SAVE_SCHED_AFFINITY;
910 if ((ret = for_all_cpus(get_counters, EVEN_COUNTERS)) < 0)
912 gettimeofday(&tv_even, (struct timezone *)NULL);
920 if ((ret = for_all_cpus(get_counters, ODD_COUNTERS)) < 0)
922 gettimeofday(&tv_odd, (struct timezone *)NULL);
924 timersub(&tv_odd, &tv_even, &tv_delta);
925 if ((ret = for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) < 0)
927 if ((ret = for_all_cpus(submit_counters, EVEN_COUNTERS)) < 0)
930 if ((ret = for_all_cpus(get_counters, EVEN_COUNTERS)) < 0)
932 gettimeofday(&tv_even, (struct timezone *)NULL);
934 timersub(&tv_even, &tv_odd, &tv_delta);
935 if ((ret = for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) < 0)
937 if ((ret = for_all_cpus(submit_counters, ODD_COUNTERS)) < 0)
943 * Let's restore the affinity
944 * This might fail if the number of CPU changed, but we can't do anything in that case..
946 (void)sched_setaffinity(0, cpu_saved_affinity_setsize, cpu_saved_affinity_set);
950 static int __attribute__((warn_unused_result))
955 if (stat("/dev/cpu/0/msr", &sb)) {
956 ERROR("no /dev/cpu/0/msr, try \"# modprobe msr\"");
962 static int __attribute__((warn_unused_result))
966 ERROR("must be root");
967 return -ERR_NOT_ROOT;
973 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
974 * the Thermal Control Circuit (TCC) activates.
975 * This is usually equal to tjMax.
977 * Older processors do not have this MSR, so there we guess,
978 * but also allow cmdline over-ride with -T.
980 * Several MSR temperature values are in units of degrees-C
981 * below this value, including the Digital Thermal Sensor (DTS),
982 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
984 static int __attribute__((warn_unused_result))
985 set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
987 unsigned long long msr;
988 unsigned int target_c_local;
990 /* tcc_activation_temp is used only for dts or ptm */
991 if (!(do_dts || do_ptm))
994 /* this is a per-package concept */
995 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
998 if (tcc_activation_temp != 0) {
999 p->tcc_activation_temp = tcc_activation_temp;
1003 if (get_msr(t->cpu_id, MSR_IA32_TEMPERATURE_TARGET, &msr))
1006 target_c_local = (msr >> 16) & 0x7F;
1008 if (target_c_local < 85 || target_c_local > 127)
1011 p->tcc_activation_temp = target_c_local;
1016 p->tcc_activation_temp = TJMAX_DEFAULT;
1017 WARNING("cpu%d: Guessing tjMax %d C, Please use TCCActivationTemp to specify",
1018 t->cpu_id, p->tcc_activation_temp);
1024 * Identify the functionality of the CPU
1026 static int __attribute__((warn_unused_result))
1029 unsigned int eax, ebx, ecx, edx, max_level;
1030 unsigned int fms, family, model;
1033 * - EAX: Maximum Input Value for Basic CPUID Information
1034 * - EBX: "Genu" (0x756e6547)
1035 * - EDX: "ineI" (0x49656e69)
1036 * - ECX: "ntel" (0x6c65746e)
1038 max_level = ebx = ecx = edx = 0;
1039 __get_cpuid(0, &max_level, &ebx, &ecx, &edx);
1040 if (ebx != 0x756e6547 && edx != 0x49656e69 && ecx != 0x6c65746e) {
1041 ERROR("Unsupported CPU");
1042 return -UNSUPPORTED_CPU;
1046 * - EAX: Version Information: Type, Family, Model, and Stepping ID
1049 * + 12-13: Processor type
1050 * + 16-19: Extended Model ID
1051 * + 20-27: Extended Family ID
1052 * - EDX: Feature Information:
1053 * + 5: Support for MSR read/write operations
1055 fms = ebx = ecx = edx = 0;
1056 __get_cpuid(1, &fms, &ebx, &ecx, &edx);
1057 family = (fms >> 8) & 0xf;
1058 model = (fms >> 4) & 0xf;
1060 family += (fms >> 20) & 0xf;
1061 if (family == 6 || family == 0xf)
1062 model += ((fms >> 16) & 0xf) << 4;
1063 if (!(edx & (1 << 5))) {
1064 ERROR("CPUID: no MSR");
1069 * CPUID(0x80000000):
1070 * - EAX: Maximum Input Value for Extended Function CPUID Information
1072 * This allows us to verify if the CPUID(0x80000007) can be called
1074 * This check is valid for both Intel and AMD.
1076 max_level = ebx = ecx = edx = 0;
1077 __get_cpuid(0x80000000, &max_level, &ebx, &ecx, &edx);
1078 if (max_level < 0x80000007) {
1079 ERROR("CPUID: no invariant TSC (max_level 0x%x)", max_level);
1080 return -ERR_NO_INVARIANT_TSC;
1084 * CPUID(0x80000007):
1086 * + 8: Invariant TSC available if set
1088 * This check is valid for both Intel and AMD
1090 __get_cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
1091 if (!(edx & (1 << 8))) {
1092 ERROR("No invariant TSC");
1093 return -ERR_NO_INVARIANT_TSC;
1099 * + 0: Digital temperature sensor is supported if set
1100 * + 6: Package thermal management is supported if set
1102 * + 0: Hardware Coordination Feedback Capability (Presence of IA32_MPERF and IA32_APERF).
1103 * + 3: The processor supports performance-energy bias preference if set.
1104 * It also implies the presence of a new architectural MSR called IA32_ENERGY_PERF_BIAS
1106 * This check is valid for both Intel and AMD
1108 __get_cpuid(0x6, &eax, &ebx, &ecx, &edx);
1109 do_dts = eax & (1 << 0);
1110 do_ptm = eax & (1 << 6);
1111 if (!(ecx & (1 << 0))) {
1113 return -ERR_NO_APERF;
1117 * Enable or disable C states depending on the model and family
1121 /* Atom (partial) */
1124 do_pkg_cstate = (1 << 2) | (1 << 4) | (1 << 6);
1127 case 0x37: /* BYT */
1129 case 0x4D: /* AVN */
1132 do_core_cstate = (1 << 1) | (1 << 6);
1133 do_pkg_cstate = (1 << 6);
1136 case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
1137 case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
1138 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
1139 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1140 do_core_cstate = (1 << 3) | (1 << 6);
1141 do_pkg_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1144 case 0x25: /* Westmere Client - Clarkdale, Arrandale */
1145 case 0x2C: /* Westmere EP - Gulftown */
1146 case 0x2F: /* Westmere-EX Xeon - Eagleton */
1147 do_core_cstate = (1 << 3) | (1 << 6);
1148 do_pkg_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1151 case 0x2A: /* SNB */
1152 case 0x2D: /* SNB Xeon */
1153 do_core_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1154 do_pkg_cstate = (1 << 2) | (1 << 3) | (1 << 6) | (1 << 7);
1157 case 0x3A: /* IVB */
1158 case 0x3E: /* IVB Xeon */
1159 do_core_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1160 do_pkg_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1162 /* Haswell Bridge */
1163 case 0x3C: /* HSW */
1164 case 0x3F: /* HSW */
1165 case 0x46: /* HSW */
1166 do_core_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1167 do_pkg_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1169 case 0x45: /* HSW */
1170 do_core_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1171 do_pkg_cstate = (1 << 3) | (1 << 6) | (1 << 7) | (1 << 8) | (1 << 9) | (1 << 10);
1174 case 0x4F: /* BDW */
1175 case 0x56: /* BDX-DE */
1176 do_core_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1177 do_pkg_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1179 case 0x3D: /* BDW */
1180 do_core_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1181 do_pkg_cstate = (1 << 3) | (1 << 6) | (1 << 7) | (1 << 8) | (1 << 9) | (1 << 10);
1184 ERROR("Unsupported CPU");
1192 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_PKG_POWER_INFO | RAPL_GFX;
1195 do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO | RAPL_PKG_PERF_STATUS | RAPL_DRAM | RAPL_DRAM_PERF_STATUS;
1199 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_PKG_POWER_INFO | RAPL_PKG_PERF_STATUS | RAPL_DRAM | RAPL_DRAM_PERF_STATUS;
1203 do_rapl = RAPL_PKG | RAPL_CORES;
1209 ERROR("Unsupported CPU");
1210 return -UNSUPPORTED_CPU;
1215 if (get_msr(0, MSR_RAPL_POWER_UNIT, &msr))
1219 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
1221 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
1229 static int __attribute__((warn_unused_result))
1234 int max_core_id = 0;
1235 int max_package_id = 0;
1236 int max_siblings = 0;
1237 struct cpu_topology {
1239 int physical_package_id;
1242 /* Initialize num_cpus, max_cpu_num */
1244 topo.max_cpu_num = 0;
1245 ret = for_all_proc_cpus(count_cpus);
1249 DEBUG("num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
1251 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
1253 ERROR("calloc cpus");
1258 * Allocate and initialize cpu_present_set
1260 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
1261 if (cpu_present_set == NULL) {
1264 return -ERR_CPU_ALLOC;
1266 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
1267 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
1268 ret = for_all_proc_cpus(mark_cpu_present);
1275 * Allocate and initialize cpu_affinity_set
1277 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
1278 if (cpu_affinity_set == NULL) {
1281 return -ERR_CPU_ALLOC;
1283 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
1284 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
1288 * Allocate and initialize cpu_saved_affinity_set
1290 cpu_saved_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
1291 if (cpu_saved_affinity_set == NULL) {
1294 return -ERR_CPU_ALLOC;
1296 cpu_saved_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
1297 CPU_ZERO_S(cpu_saved_affinity_setsize, cpu_saved_affinity_set);
1302 * find max_core_id, max_package_id
1304 for (i = 0; i <= topo.max_cpu_num; ++i) {
1307 if (cpu_is_not_present(i)) {
1308 WARNING("cpu%d NOT PRESENT", i);
1311 cpus[i].core_id = get_core_id(i);
1312 if (cpus[i].core_id < 0)
1313 return cpus[i].core_id;
1314 if (cpus[i].core_id > max_core_id)
1315 max_core_id = cpus[i].core_id;
1317 cpus[i].physical_package_id = get_physical_package_id(i);
1318 if (cpus[i].physical_package_id < 0)
1319 return cpus[i].physical_package_id;
1320 if (cpus[i].physical_package_id > max_package_id)
1321 max_package_id = cpus[i].physical_package_id;
1323 siblings = get_num_ht_siblings(i);
1326 if (siblings > max_siblings)
1327 max_siblings = siblings;
1328 DEBUG("cpu %d pkg %d core %d\n",
1329 i, cpus[i].physical_package_id, cpus[i].core_id);
1331 topo.num_cores_per_pkg = max_core_id + 1;
1332 DEBUG("max_core_id %d, sizing for %d cores per package\n",
1333 max_core_id, topo.num_cores_per_pkg);
1335 topo.num_packages = max_package_id + 1;
1336 DEBUG("max_package_id %d, sizing for %d packages\n",
1337 max_package_id, topo.num_packages);
1339 topo.num_threads_per_core = max_siblings;
1340 DEBUG("max_siblings %d\n", max_siblings);
1347 allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
1351 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
1352 topo.num_packages, sizeof(struct thread_data));
1356 for (i = 0; i < topo.num_threads_per_core *
1357 topo.num_cores_per_pkg * topo.num_packages; i++)
1358 (*t)[i].cpu_id = -1;
1360 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
1361 sizeof(struct core_data));
1365 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
1366 (*c)[i].core_id = -1;
1368 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
1372 for (i = 0; i < topo.num_packages; i++)
1373 (*p)[i].package_id = i;
1377 ERROR("calloc counters");
1383 * set cpu_id, core_num, pkg_num
1384 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
1386 * increment topo.num_cores when 1st core in pkg seen
1389 init_counter(struct thread_data *thread_base, struct core_data *core_base,
1390 struct pkg_data *pkg_base, int thread_num, int core_num,
1391 int pkg_num, int cpu_id)
1394 struct thread_data *t;
1395 struct core_data *c;
1398 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
1399 c = GET_CORE(core_base, core_num, pkg_num);
1400 p = GET_PKG(pkg_base, pkg_num);
1403 if (thread_num == 0) {
1404 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
1405 if ((ret = cpu_is_first_core_in_package(cpu_id)) < 0) {
1407 } else if (ret != 0) {
1408 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
1412 c->core_id = core_num;
1413 p->package_id = pkg_num;
1420 initialize_counters(int cpu_id)
1422 int my_thread_id, my_core_id, my_package_id;
1425 my_package_id = get_physical_package_id(cpu_id);
1426 if (my_package_id < 0)
1427 return my_package_id;
1428 my_core_id = get_core_id(cpu_id);
1432 if ((ret = cpu_is_first_sibling_in_core(cpu_id)) < 0) {
1434 } else if (ret != 0) {
1441 ret = init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
1444 ret = init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
1450 #define DO_OR_GOTO_ERR(something) \
1452 ret = (something); \
1457 static int setup_all_buffers(void)
1461 DO_OR_GOTO_ERR(topology_probe());
1462 DO_OR_GOTO_ERR(allocate_counters(&thread_even, &core_even, &package_even));
1463 DO_OR_GOTO_ERR(allocate_counters(&thread_odd, &core_odd, &package_odd));
1464 DO_OR_GOTO_ERR(for_all_proc_cpus(initialize_counters));
1474 turbostat_init(void)
1478 DO_OR_GOTO_ERR(check_super_user());
1479 DO_OR_GOTO_ERR(probe_cpu());
1480 DO_OR_GOTO_ERR(check_dev_msr());
1481 DO_OR_GOTO_ERR(setup_all_buffers());
1482 DO_OR_GOTO_ERR(for_all_cpus(set_temperature_target, EVEN_COUNTERS));
1483 DO_OR_GOTO_ERR(for_all_cpus(set_temperature_target, ODD_COUNTERS));
1485 plugin_register_complex_read(NULL, PLUGIN_NAME, turbostat_read, NULL, NULL);
1493 static const char *config_keys[] =
1495 "TCCActivationTemp",
1497 static const int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
1500 turbostat_config(const char *key, const char *value)
1502 long unsigned int tmp_val;
1505 if (strcasecmp("TCCActivationTemp", key) == 0) {
1506 tmp_val = strtoul(value, &end, 0);
1507 if (*end != '\0' || tmp_val > UINT_MAX)
1509 tcc_activation_temp = (unsigned int) tmp_val;
1516 void module_register(void);
1517 void module_register(void)
1519 plugin_register_init(PLUGIN_NAME, turbostat_init);
1520 plugin_register_config(PLUGIN_NAME, turbostat_config, config_keys, config_keys_num);