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 unsigned int tcc_activation_temp_override;
73 static double rapl_energy_units;
75 #define RAPL_PKG (1 << 0)
76 /* 0x610 MSR_PKG_POWER_LIMIT */
77 /* 0x611 MSR_PKG_ENERGY_STATUS */
78 #define RAPL_PKG_PERF_STATUS (1 << 1)
79 /* 0x613 MSR_PKG_PERF_STATUS */
80 #define RAPL_PKG_POWER_INFO (1 << 2)
81 /* 0x614 MSR_PKG_POWER_INFO */
83 #define RAPL_DRAM (1 << 3)
84 /* 0x618 MSR_DRAM_POWER_LIMIT */
85 /* 0x619 MSR_DRAM_ENERGY_STATUS */
86 /* 0x61c MSR_DRAM_POWER_INFO */
87 #define RAPL_DRAM_PERF_STATUS (1 << 4)
88 /* 0x61b MSR_DRAM_PERF_STATUS */
90 #define RAPL_CORES (1 << 5)
91 /* 0x638 MSR_PP0_POWER_LIMIT */
92 /* 0x639 MSR_PP0_ENERGY_STATUS */
93 #define RAPL_CORE_POLICY (1 << 6)
94 /* 0x63a MSR_PP0_POLICY */
97 #define RAPL_GFX (1 << 7)
98 /* 0x640 MSR_PP1_POWER_LIMIT */
99 /* 0x641 MSR_PP1_ENERGY_STATUS */
100 /* 0x642 MSR_PP1_POLICY */
101 #define TJMAX_DEFAULT 100
103 int aperf_mperf_unstable;
107 cpu_set_t *cpu_present_set, *cpu_affinity_set;
108 size_t cpu_present_setsize, cpu_affinity_setsize;
111 unsigned long long tsc;
112 unsigned long long aperf;
113 unsigned long long mperf;
114 unsigned long long c1;
115 unsigned int smi_count;
118 #define CPU_IS_FIRST_THREAD_IN_CORE 0x2
119 #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
120 } *thread_even, *thread_odd;
123 unsigned long long c3;
124 unsigned long long c6;
125 unsigned long long c7;
126 unsigned int core_temp_c;
127 unsigned int core_id;
128 } *core_even, *core_odd;
131 unsigned long long pc2;
132 unsigned long long pc3;
133 unsigned long long pc6;
134 unsigned long long pc7;
135 unsigned long long pc8;
136 unsigned long long pc9;
137 unsigned long long pc10;
138 unsigned int package_id;
139 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
140 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
141 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
142 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
143 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
144 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
145 unsigned int pkg_temp_c;
147 } *package_even, *package_odd;
149 #define ODD_COUNTERS thread_odd, core_odd, package_odd
150 #define EVEN_COUNTERS thread_even, core_even, package_even
151 static _Bool is_even = 1;
153 static _Bool allocated = 0;
154 static _Bool initialized = 0;
156 #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
157 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
158 topo.num_threads_per_core + \
159 (core_no) * topo.num_threads_per_core + (thread_no))
160 #define GET_CORE(core_base, core_no, pkg_no) \
161 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
162 #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
169 int num_cores_per_pkg;
170 int num_threads_per_core;
173 struct timeval tv_even, tv_odd, tv_delta;
181 ERR_MSR_CORE_C3_RESIDENCY,
182 ERR_MSR_CORE_C6_RESIDENCY,
183 ERR_MSR_CORE_C7_RESIDENCY,
184 ERR_MSR_IA32_THERM_STATUS,
185 ERR_MSR_PKG_C3_RESIDENCY,
186 ERR_MSR_PKG_C6_RESIDENCY,
187 ERR_MSR_PKG_C2_RESIDENCY,
188 ERR_MSR_PKG_C7_RESIDENCY,
189 ERR_MSR_PKG_C8_RESIDENCY,
190 ERR_MSR_PKG_C9_RESIDENCY,
191 ERR_MSR_PKG_C10_RESIDENCY,
192 ERR_MSR_PKG_ENERGY_STATUS,
193 ERR_MSR_PKG_POWER_INFO,
194 ERR_MSR_PP0_ENERGY_STATUS,
195 ERR_MSR_DRAM_ENERGY_STATUS,
196 ERR_MSR_PP1_ENERGY_STATUS,
197 ERR_MSR_PKG_PERF_STATUS,
198 ERR_MSR_DRAM_PERF_STATUS,
199 ERR_MSR_IA32_PACKAGE_THERM_STATUS,
205 ERR_CANT_READ_NUMBER,
206 ERR_CANT_READ_PROC_STAT,
207 ERR_NO_INVARIANT_TSC,
215 static int setup_all_buffers(void);
218 cpu_is_not_present(int cpu)
220 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
223 * run func(thread, core, package) in topology order
224 * skip non-present cpus
227 static int __attribute__((warn_unused_result))
228 for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
229 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
231 int retval, pkg_no, core_no, thread_no;
233 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
234 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
235 for (thread_no = 0; thread_no <
236 topo.num_threads_per_core; ++thread_no) {
237 struct thread_data *t;
241 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
243 if (cpu_is_not_present(t->cpu_id))
246 c = GET_CORE(core_base, core_no, pkg_no);
247 p = GET_PKG(pkg_base, pkg_no);
249 retval = func(t, c, p);
258 static int __attribute__((warn_unused_result))
261 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
262 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
263 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
264 return -ERR_CPU_MIGRATE;
269 static int __attribute__((warn_unused_result))
275 /* FIXME: Do we really need this, why? */
276 if (cpu_migrate(cpu)) {
277 ERROR("Could not migrate to CPU %d\n", cpu);
278 return -ERR_CPU_MIGRATE;
281 ssnprintf(pathname, sizeof(pathname), "/dev/cpu/%d/msr", cpu);
282 fd = open(pathname, O_RDONLY);
284 return -ERR_CANT_OPEN_MSR;
288 static int __attribute__((warn_unused_result))
289 read_msr(int fd, off_t offset, unsigned long long *msr)
293 retval = pread(fd, msr, sizeof *msr, offset);
295 if (retval != sizeof *msr) {
296 ERROR ("MSR offset 0x%llx read failed", (unsigned long long)offset);
302 static int __attribute__((warn_unused_result))
303 get_msr(int cpu, off_t offset, unsigned long long *msr)
311 retval = read_msr(fd, offset, msr);
316 #define DELTA_WRAP32(new, old) \
320 old = 0x100000000 + new - old; \
324 delta_package(struct pkg_data *new, struct pkg_data *old)
326 old->pc2 = new->pc2 - old->pc2;
327 old->pc3 = new->pc3 - old->pc3;
328 old->pc6 = new->pc6 - old->pc6;
329 old->pc7 = new->pc7 - old->pc7;
330 old->pc8 = new->pc8 - old->pc8;
331 old->pc9 = new->pc9 - old->pc9;
332 old->pc10 = new->pc10 - old->pc10;
333 old->pkg_temp_c = new->pkg_temp_c;
335 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
336 DELTA_WRAP32(new->energy_cores, old->energy_cores);
337 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
338 DELTA_WRAP32(new->energy_dram, old->energy_dram);
339 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
340 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
344 delta_core(struct core_data *new, struct core_data *old)
346 old->c3 = new->c3 - old->c3;
347 old->c6 = new->c6 - old->c6;
348 old->c7 = new->c7 - old->c7;
349 old->core_temp_c = new->core_temp_c;
355 static int __attribute__((warn_unused_result))
356 delta_thread(struct thread_data *new, struct thread_data *old,
357 struct core_data *core_delta)
359 old->tsc = new->tsc - old->tsc;
361 /* check for TSC < 1 Mcycles over interval */
362 if (old->tsc < (1000 * 1000)) {
363 WARNING("Insanely slow TSC rate, TSC stops in idle?\n"
364 "You can disable all c-states by booting with \"idle=poll\"\n"
365 "or just the deep ones with \"processor.max_cstate=1\"");
369 old->c1 = new->c1 - old->c1;
371 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
372 old->aperf = new->aperf - old->aperf;
373 old->mperf = new->mperf - old->mperf;
376 if (!aperf_mperf_unstable) {
377 WARNING("%s: APERF or MPERF went backwards *\n", progname);
378 WARNING("* Frequency results do not cover entire interval *\n");
379 WARNING("* fix this by running Linux-2.6.30 or later *\n");
381 aperf_mperf_unstable = 1;
384 * mperf delta is likely a huge "positive" number
385 * can not use it for calculating c0 time
393 * As counter collection is not atomic,
394 * it is possible for mperf's non-halted cycles + idle states
395 * to exceed TSC's all cycles: show c1 = 0% in that case.
397 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
400 /* normal case, derive c1 */
401 old->c1 = old->tsc - old->mperf - core_delta->c3
402 - core_delta->c6 - core_delta->c7;
405 if (old->mperf == 0) {
406 WARNING("cpu%d MPERF 0!\n", old->cpu_id);
407 old->mperf = 1; /* divide by 0 protection */
410 old->smi_count = new->smi_count - old->smi_count;
415 static int __attribute__((warn_unused_result))
416 delta_cpu(struct thread_data *t, struct core_data *c,
417 struct pkg_data *p, struct thread_data *t2,
418 struct core_data *c2, struct pkg_data *p2)
422 /* calculate core delta only for 1st thread in core */
423 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
426 /* always calculate thread delta */
427 ret = delta_thread(t, t2, c2); /* c2 is core delta */
431 /* calculate package delta only for 1st core in package */
432 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
433 delta_package(p, p2);
441 * acquire and record local counters for that cpu
443 static int __attribute__((warn_unused_result))
444 get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
447 unsigned long long msr;
451 msr_fd = open_msr(cpu);
455 #define READ_MSR(msr, dst) \
457 if (read_msr(msr_fd, msr, dst)) { \
458 retval = -ERR_##msr; \
463 READ_MSR(MSR_IA32_TSC, &t->tsc);
465 READ_MSR(MSR_IA32_APERF, &t->aperf);
466 READ_MSR(MSR_IA32_MPERF, &t->mperf);
468 READ_MSR(MSR_SMI_COUNT, &msr);
469 t->smi_count = msr & 0xFFFFFFFF;
471 /* collect core counters only for 1st thread in core */
472 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) {
477 if (do_core_cstate & (1 << 3))
478 READ_MSR(MSR_CORE_C3_RESIDENCY, &c->c3);
479 if (do_core_cstate & (1 << 6))
480 READ_MSR(MSR_CORE_C6_RESIDENCY, &c->c6);
481 if (do_core_cstate & (1 << 7))
482 READ_MSR(MSR_CORE_C7_RESIDENCY, &c->c7);
485 READ_MSR(MSR_IA32_THERM_STATUS, &msr);
486 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
489 /* collect package counters only for 1st core in package */
490 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
495 if (do_pkg_cstate & (1 << 2))
496 READ_MSR(MSR_PKG_C2_RESIDENCY, &p->pc2);
497 if (do_pkg_cstate & (1 << 3))
498 READ_MSR(MSR_PKG_C3_RESIDENCY, &p->pc3);
499 if (do_pkg_cstate & (1 << 6))
500 READ_MSR(MSR_PKG_C6_RESIDENCY, &p->pc6);
501 if (do_pkg_cstate & (1 << 7))
502 READ_MSR(MSR_PKG_C7_RESIDENCY, &p->pc7);
503 if (do_pkg_cstate & (1 << 8))
504 READ_MSR(MSR_PKG_C8_RESIDENCY, &p->pc8);
505 if (do_pkg_cstate & (1 << 9))
506 READ_MSR(MSR_PKG_C9_RESIDENCY, &p->pc9);
507 if (do_pkg_cstate & (1 << 10))
508 READ_MSR(MSR_PKG_C10_RESIDENCY, &p->pc10);
510 if (do_rapl & RAPL_PKG) {
511 READ_MSR(MSR_PKG_ENERGY_STATUS, &msr);
512 p->energy_pkg = msr & 0xFFFFFFFF;
514 if (do_rapl & RAPL_CORES) {
515 READ_MSR(MSR_PP0_ENERGY_STATUS, &msr);
516 p->energy_cores = msr & 0xFFFFFFFF;
518 if (do_rapl & RAPL_DRAM) {
519 READ_MSR(MSR_DRAM_ENERGY_STATUS, &msr);
520 p->energy_dram = msr & 0xFFFFFFFF;
522 if (do_rapl & RAPL_GFX) {
523 READ_MSR(MSR_PP1_ENERGY_STATUS, &msr);
524 p->energy_gfx = msr & 0xFFFFFFFF;
526 if (do_rapl & RAPL_PKG_PERF_STATUS) {
527 READ_MSR(MSR_PKG_PERF_STATUS, &msr);
528 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
530 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
531 READ_MSR(MSR_DRAM_PERF_STATUS, &msr);
532 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
535 READ_MSR(MSR_IA32_PACKAGE_THERM_STATUS, &msr);
536 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
545 free_all_buffers(void)
550 CPU_FREE(cpu_present_set);
551 cpu_present_set = NULL;
554 CPU_FREE(cpu_affinity_set);
555 cpu_affinity_set = NULL;
556 cpu_affinity_setsize = 0;
576 * Parse a file containing a single int.
578 static int __attribute__ ((format(printf,1,2)))
579 parse_int_file(const char *fmt, ...)
587 vsnprintf(path, sizeof(path), fmt, args);
589 filep = fopen(path, "r");
591 ERROR("%s: open failed", path);
592 return -ERR_CANT_OPEN_FILE;
594 if (fscanf(filep, "%d", &value) != 1) {
595 ERROR("%s: failed to parse number from file", path);
596 return -ERR_CANT_READ_NUMBER;
603 * cpu_is_first_sibling_in_core(cpu)
604 * return 1 if given CPU is 1st HT sibling in the core
607 cpu_is_first_sibling_in_core(int cpu)
609 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
613 * cpu_is_first_core_in_package(cpu)
614 * return 1 if given CPU is 1st core in package
617 cpu_is_first_core_in_package(int cpu)
619 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
623 get_physical_package_id(int cpu)
625 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
631 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
635 get_num_ht_siblings(int cpu)
643 ssnprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
644 filep = fopen(path, "r");
646 ERROR("%s: open failed", path);
647 return -ERR_CANT_OPEN_FILE;
651 * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4)
652 * otherwinse 1 sibling (self).
654 matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2);
665 * run func(thread, core, package) in topology order
666 * skip non-present cpus
670 static int __attribute__((warn_unused_result))
671 for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
672 struct pkg_data *, struct thread_data *, struct core_data *,
673 struct pkg_data *), struct thread_data *thread_base,
674 struct core_data *core_base, struct pkg_data *pkg_base,
675 struct thread_data *thread_base2, struct core_data *core_base2,
676 struct pkg_data *pkg_base2)
678 int retval, pkg_no, core_no, thread_no;
680 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
681 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
682 for (thread_no = 0; thread_no <
683 topo.num_threads_per_core; ++thread_no) {
684 struct thread_data *t, *t2;
685 struct core_data *c, *c2;
686 struct pkg_data *p, *p2;
688 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
690 if (cpu_is_not_present(t->cpu_id))
693 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
695 c = GET_CORE(core_base, core_no, pkg_no);
696 c2 = GET_CORE(core_base2, core_no, pkg_no);
698 p = GET_PKG(pkg_base, pkg_no);
699 p2 = GET_PKG(pkg_base2, pkg_no);
701 retval = func(t, c, p, t2, c2, p2);
711 * run func(cpu) on every cpu in /proc/stat
712 * return max_cpu number
714 static int __attribute__((warn_unused_result))
715 for_all_proc_cpus(int (func)(int))
721 fp = fopen(proc_stat, "r");
723 ERROR("%s: open failed", proc_stat);
724 return -ERR_CANT_OPEN_FILE;
727 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
729 ERROR("%s: failed to parse format", proc_stat);
730 return -ERR_CANT_READ_PROC_STAT;
734 retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
738 retval = func(cpu_num);
750 * remember the last one seen, it will be the max
755 if (topo.max_cpu_num < cpu)
756 topo.max_cpu_num = cpu;
762 mark_cpu_present(int cpu)
764 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
770 turbostat_submit (const char *plugin_instance,
771 const char *type, const char *type_instance,
774 value_list_t vl = VALUE_LIST_INIT;
780 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
781 sstrncpy (vl.plugin, PLUGIN_NAME, sizeof (vl.plugin));
782 if (plugin_instance != NULL)
783 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
784 sstrncpy (vl.type, type, sizeof (vl.type));
785 if (type_instance != NULL)
786 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
788 plugin_dispatch_values (&vl);
792 * column formatting convention & formats
793 * package: "pk" 2 columns %2d
794 * core: "cor" 3 columns %3d
795 * CPU: "CPU" 3 columns %3d
800 * GHz: "GHz" 3 columns %3.2
801 * TSC: "TSC" 3 columns %3.2
802 * SMI: "SMI" 4 columns %4d
803 * percentage " %pc3" %6.2
804 * Perf Status percentage: %5.2
805 * "CTMP" 4 columns %4d
809 submit_counters(struct thread_data *t, struct core_data *c,
813 double interval_float;
815 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
817 snprintf(name, sizeof(name), "cpu%02d", t->cpu_id);
820 turbostat_submit(name, "percent", "c0", 100.0 * t->mperf/t->tsc);
822 turbostat_submit(name, "percent", "c1", 100.0 * t->c1/t->tsc);
825 if ((!aperf_mperf_unstable) || (!(t->aperf > t->tsc || t->mperf > t->tsc)))
826 turbostat_submit(NULL, "frequency", name, 1.0 * t->tsc / 1000000000 * t->aperf / t->mperf / interval_float);
829 turbostat_submit(NULL, "current", name, t->smi_count);
831 /* print per-core data only for 1st thread in core */
832 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
835 snprintf(name, sizeof(name), "core%02d", c->core_id);
837 if (do_core_cstate & (1 << 3))
838 turbostat_submit(name, "percent", "c3", 100.0 * c->c3/t->tsc);
839 if (do_core_cstate & (1 << 6))
840 turbostat_submit(name, "percent", "c6", 100.0 * c->c6/t->tsc);
841 if (do_core_cstate & (1 << 7))
842 turbostat_submit(name, "percent", "c7", 100.0 * c->c7/t->tsc);
845 turbostat_submit(NULL, "temperature", name, c->core_temp_c);
847 /* print per-package data only for 1st core in package */
848 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
851 snprintf(name, sizeof(name), "pkg%02d", p->package_id);
854 turbostat_submit(NULL, "temperature", name, p->pkg_temp_c);
856 if (do_pkg_cstate & (1 << 2))
857 turbostat_submit(name, "percent", "pc2", 100.0 * p->pc2/t->tsc);
858 if (do_pkg_cstate & (1 << 3))
859 turbostat_submit(name, "percent", "pc3", 100.0 * p->pc3/t->tsc);
860 if (do_pkg_cstate & (1 << 6))
861 turbostat_submit(name, "percent", "pc6", 100.0 * p->pc6/t->tsc);
862 if (do_pkg_cstate & (1 << 7))
863 turbostat_submit(name, "percent", "pc7", 100.0 * p->pc7/t->tsc);
864 if (do_pkg_cstate & (1 << 8))
865 turbostat_submit(name, "percent", "pc8", 100.0 * p->pc8/t->tsc);
866 if (do_pkg_cstate & (1 << 9))
867 turbostat_submit(name, "percent", "pc9", 100.0 * p->pc9/t->tsc);
868 if (do_pkg_cstate & (1 << 10))
869 turbostat_submit(name, "percent", "pc10", 100.0 * p->pc10/t->tsc);
872 if (do_rapl & RAPL_PKG)
873 turbostat_submit(name, "power", "Pkg_W", p->energy_pkg * rapl_energy_units / interval_float);
874 if (do_rapl & RAPL_CORES)
875 turbostat_submit(name, "power", "Cor_W", p->energy_cores * rapl_energy_units / interval_float);
876 if (do_rapl & RAPL_GFX)
877 turbostat_submit(name, "power", "GFX_W", p->energy_gfx * rapl_energy_units / interval_float);
878 if (do_rapl & RAPL_DRAM)
879 turbostat_submit(name, "power", "RAM_W", p->energy_dram * rapl_energy_units / interval_float);
886 turbostat_read(user_data_t * not_used)
891 if ((ret = setup_all_buffers()) < 0)
895 if (for_all_proc_cpus(cpu_is_not_present)) {
897 if ((ret = setup_all_buffers()) < 0)
899 if (for_all_proc_cpus(cpu_is_not_present))
900 return -ERR_CPU_NOT_PRESENT;
904 if ((ret = for_all_cpus(get_counters, EVEN_COUNTERS)) < 0)
906 gettimeofday(&tv_even, (struct timezone *)NULL);
913 if ((ret = for_all_cpus(get_counters, ODD_COUNTERS)) < 0)
915 gettimeofday(&tv_odd, (struct timezone *)NULL);
917 timersub(&tv_odd, &tv_even, &tv_delta);
918 if ((ret = for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) < 0)
920 if ((ret = for_all_cpus(submit_counters, EVEN_COUNTERS)) < 0)
923 if ((ret = for_all_cpus(get_counters, EVEN_COUNTERS)) < 0)
925 gettimeofday(&tv_even, (struct timezone *)NULL);
927 timersub(&tv_even, &tv_odd, &tv_delta);
928 if ((ret = for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) < 0)
930 if ((ret = for_all_cpus(submit_counters, ODD_COUNTERS)) < 0)
936 static int __attribute__((warn_unused_result))
941 if (stat("/dev/cpu/0/msr", &sb)) {
942 ERROR("no /dev/cpu/0/msr\n"
943 "Try \"# modprobe msr\"");
949 static int __attribute__((warn_unused_result))
953 ERROR("must be root");
954 return -ERR_NOT_ROOT;
960 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
961 * the Thermal Control Circuit (TCC) activates.
962 * This is usually equal to tjMax.
964 * Older processors do not have this MSR, so there we guess,
965 * but also allow cmdline over-ride with -T.
967 * Several MSR temperature values are in units of degrees-C
968 * below this value, including the Digital Thermal Sensor (DTS),
969 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
971 static int __attribute__((warn_unused_result))
972 set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
974 unsigned long long msr;
975 unsigned int target_c_local;
977 /* tcc_activation_temp is used only for dts or ptm */
978 if (!(do_dts || do_ptm))
981 /* this is a per-package concept */
982 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
985 if (tcc_activation_temp_override != 0) {
986 tcc_activation_temp = tcc_activation_temp_override;
987 ERROR("cpu%d: Using cmdline TCC Target (%d C)\n",
988 cpu, tcc_activation_temp);
992 if (get_msr(t->cpu_id, MSR_IA32_TEMPERATURE_TARGET, &msr))
995 target_c_local = (msr >> 16) & 0x7F;
997 if (target_c_local < 85 || target_c_local > 127)
1000 tcc_activation_temp = target_c_local;
1005 tcc_activation_temp = TJMAX_DEFAULT;
1006 WARNING("cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
1007 t->cpu_id, tcc_activation_temp);
1013 * Identify the functionality of the CPU
1015 static int __attribute__((warn_unused_result))
1018 unsigned int eax, ebx, ecx, edx, max_level;
1019 unsigned int fms, family, model;
1022 * - EAX: Maximum Input Value for Basic CPUID Information
1023 * - EBX: "Genu" (0x756e6547)
1024 * - EDX: "ineI" (0x49656e69)
1025 * - ECX: "ntel" (0x6c65746e)
1027 max_level = ebx = ecx = edx = 0;
1028 __get_cpuid(0, &max_level, &ebx, &ecx, &edx);
1029 if (ebx != 0x756e6547 && edx != 0x49656e69 && ecx != 0x6c65746e) {
1030 ERROR("Unsupported CPU");
1031 return -UNSUPPORTED_CPU;
1035 * - EAX: Version Information: Type, Family, Model, and Stepping ID
1038 * + 12-13: Processor type
1039 * + 16-19: Extended Model ID
1040 * + 20-27: Extended Family ID
1041 * - EDX: Feature Information:
1042 * + 5: Support for MSR read/write operations
1044 fms = ebx = ecx = edx = 0;
1045 __get_cpuid(1, &fms, &ebx, &ecx, &edx);
1046 family = (fms >> 8) & 0xf;
1047 model = (fms >> 4) & 0xf;
1049 family += (fms >> 20) & 0xf;
1050 if (family == 6 || family == 0xf)
1051 model += ((fms >> 16) & 0xf) << 4;
1052 if (!(edx & (1 << 5))) {
1053 ERROR("CPUID: no MSR");
1058 * CPUID(0x80000000):
1059 * - EAX: Maximum Input Value for Extended Function CPUID Information
1061 * This allows us to verify if the CPUID(0x80000007) can be called
1063 * This check is valid for both Intel and AMD.
1065 max_level = ebx = ecx = edx = 0;
1066 __get_cpuid(0x80000000, &max_level, &ebx, &ecx, &edx);
1067 if (max_level < 0x80000007) {
1068 ERROR("CPUID: no invariant TSC (max_level 0x%x)", max_level);
1069 return -ERR_NO_INVARIANT_TSC;
1073 * CPUID(0x80000007):
1075 * + 8: Invariant TSC available if set
1077 * This check is valid for both Intel and AMD
1079 __get_cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
1080 if (!(edx & (1 << 8))) {
1081 ERROR("No invariant TSC");
1082 return -ERR_NO_INVARIANT_TSC;
1088 * + 0: Digital temperature sensor is supported if set
1089 * + 6: Package thermal management is supported if set
1091 * + 0: Hardware Coordination Feedback Capability (Presence of IA32_MPERF and IA32_APERF).
1092 * + 3: The processor supports performance-energy bias preference if set.
1093 * It also implies the presence of a new architectural MSR called IA32_ENERGY_PERF_BIAS
1095 * This check is valid for both Intel and AMD
1097 __get_cpuid(0x6, &eax, &ebx, &ecx, &edx);
1098 do_dts = eax & (1 << 0);
1099 do_ptm = eax & (1 << 6);
1100 if (!(ecx & (1 << 0))) {
1102 return -ERR_NO_APERF;
1106 * Enable or disable C states depending on the model and family
1110 /* Atom (partial) */
1113 do_pkg_cstate = (1 << 2) | (1 << 4) | (1 << 6);
1116 case 0x37: /* BYT */
1118 case 0x4D: /* AVN */
1121 do_core_cstate = (1 << 1) | (1 << 6);
1122 do_pkg_cstate = (1 << 6);
1125 case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
1126 case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
1127 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
1128 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1129 do_core_cstate = (1 << 3) | (1 << 6);
1130 do_pkg_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1133 case 0x25: /* Westmere Client - Clarkdale, Arrandale */
1134 case 0x2C: /* Westmere EP - Gulftown */
1135 case 0x2F: /* Westmere-EX Xeon - Eagleton */
1136 do_core_cstate = (1 << 3) | (1 << 6);
1137 do_pkg_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1140 case 0x2A: /* SNB */
1141 case 0x2D: /* SNB Xeon */
1142 do_core_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1143 do_pkg_cstate = (1 << 2) | (1 << 3) | (1 << 6) | (1 << 7);
1146 case 0x3A: /* IVB */
1147 case 0x3E: /* IVB Xeon */
1148 do_core_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1149 do_pkg_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1151 /* Haswell Bridge */
1152 case 0x3C: /* HSW */
1153 case 0x3F: /* HSW */
1154 case 0x46: /* HSW */
1155 do_core_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1156 do_pkg_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1158 case 0x45: /* HSW */
1159 do_core_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1160 do_pkg_cstate = (1 << 3) | (1 << 6) | (1 << 7) | (1 << 8) | (1 << 9) | (1 << 10);
1163 case 0x4F: /* BDW */
1164 case 0x56: /* BDX-DE */
1165 do_core_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1166 do_pkg_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1168 case 0x3D: /* BDW */
1169 do_core_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1170 do_pkg_cstate = (1 << 3) | (1 << 6) | (1 << 7) | (1 << 8) | (1 << 9) | (1 << 10);
1173 ERROR("Unsupported CPU");
1181 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_PKG_POWER_INFO | RAPL_GFX;
1184 do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO | RAPL_PKG_PERF_STATUS | RAPL_DRAM | RAPL_DRAM_PERF_STATUS;
1188 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_PKG_POWER_INFO | RAPL_PKG_PERF_STATUS | RAPL_DRAM | RAPL_DRAM_PERF_STATUS;
1192 do_rapl = RAPL_PKG | RAPL_CORES;
1198 ERROR("Unsupported CPU");
1199 return -UNSUPPORTED_CPU;
1204 if (get_msr(0, MSR_RAPL_POWER_UNIT, &msr))
1208 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
1210 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
1218 static int __attribute__((warn_unused_result))
1223 int max_core_id = 0;
1224 int max_package_id = 0;
1225 int max_siblings = 0;
1226 struct cpu_topology {
1228 int physical_package_id;
1231 /* Initialize num_cpus, max_cpu_num */
1233 topo.max_cpu_num = 0;
1234 ret = for_all_proc_cpus(count_cpus);
1238 DEBUG("num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
1240 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
1242 ERROR("calloc cpus");
1247 * Allocate and initialize cpu_present_set
1249 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
1250 if (cpu_present_set == NULL) {
1253 return -ERR_CPU_ALLOC;
1255 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
1256 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
1257 ret = for_all_proc_cpus(mark_cpu_present);
1264 * Allocate and initialize cpu_affinity_set
1266 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
1267 if (cpu_affinity_set == NULL) {
1270 return -ERR_CPU_ALLOC;
1272 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
1273 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
1278 * find max_core_id, max_package_id
1280 for (i = 0; i <= topo.max_cpu_num; ++i) {
1283 if (cpu_is_not_present(i)) {
1285 fprintf(stderr, "cpu%d NOT PRESENT\n", i);
1288 cpus[i].core_id = get_core_id(i);
1289 if (cpus[i].core_id < 0)
1290 return cpus[i].core_id;
1291 if (cpus[i].core_id > max_core_id)
1292 max_core_id = cpus[i].core_id;
1294 cpus[i].physical_package_id = get_physical_package_id(i);
1295 if (cpus[i].physical_package_id < 0)
1296 return cpus[i].physical_package_id;
1297 if (cpus[i].physical_package_id > max_package_id)
1298 max_package_id = cpus[i].physical_package_id;
1300 siblings = get_num_ht_siblings(i);
1303 if (siblings > max_siblings)
1304 max_siblings = siblings;
1305 DEBUG("cpu %d pkg %d core %d\n",
1306 i, cpus[i].physical_package_id, cpus[i].core_id);
1308 topo.num_cores_per_pkg = max_core_id + 1;
1309 DEBUG("max_core_id %d, sizing for %d cores per package\n",
1310 max_core_id, topo.num_cores_per_pkg);
1312 topo.num_packages = max_package_id + 1;
1313 DEBUG("max_package_id %d, sizing for %d packages\n",
1314 max_package_id, topo.num_packages);
1316 topo.num_threads_per_core = max_siblings;
1317 DEBUG("max_siblings %d\n", max_siblings);
1324 allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
1328 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
1329 topo.num_packages, sizeof(struct thread_data));
1333 for (i = 0; i < topo.num_threads_per_core *
1334 topo.num_cores_per_pkg * topo.num_packages; i++)
1335 (*t)[i].cpu_id = -1;
1337 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
1338 sizeof(struct core_data));
1342 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
1343 (*c)[i].core_id = -1;
1345 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
1349 for (i = 0; i < topo.num_packages; i++)
1350 (*p)[i].package_id = i;
1354 ERROR("calloc counters");
1360 * set cpu_id, core_num, pkg_num
1361 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
1363 * increment topo.num_cores when 1st core in pkg seen
1366 init_counter(struct thread_data *thread_base, struct core_data *core_base,
1367 struct pkg_data *pkg_base, int thread_num, int core_num,
1368 int pkg_num, int cpu_id)
1371 struct thread_data *t;
1372 struct core_data *c;
1375 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
1376 c = GET_CORE(core_base, core_num, pkg_num);
1377 p = GET_PKG(pkg_base, pkg_num);
1380 if (thread_num == 0) {
1381 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
1382 if ((ret = cpu_is_first_core_in_package(cpu_id)) < 0) {
1384 } else if (ret != 0) {
1385 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
1389 c->core_id = core_num;
1390 p->package_id = pkg_num;
1397 initialize_counters(int cpu_id)
1399 int my_thread_id, my_core_id, my_package_id;
1402 my_package_id = get_physical_package_id(cpu_id);
1403 if (my_package_id < 0)
1404 return my_package_id;
1405 my_core_id = get_core_id(cpu_id);
1409 if ((ret = cpu_is_first_sibling_in_core(cpu_id)) < 0) {
1411 } else if (ret != 0) {
1418 ret = init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
1421 ret = init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
1427 #define DO_OR_GOTO_ERR(something) \
1429 ret = (something); \
1434 static int setup_all_buffers(void)
1438 DO_OR_GOTO_ERR(topology_probe());
1439 DO_OR_GOTO_ERR(allocate_counters(&thread_even, &core_even, &package_even));
1440 DO_OR_GOTO_ERR(allocate_counters(&thread_odd, &core_odd, &package_odd));
1441 DO_OR_GOTO_ERR(for_all_proc_cpus(initialize_counters));
1451 turbostat_init(void)
1455 DO_OR_GOTO_ERR(check_super_user());
1456 DO_OR_GOTO_ERR(probe_cpu());
1457 DO_OR_GOTO_ERR(check_dev_msr());
1458 DO_OR_GOTO_ERR(setup_all_buffers());
1459 DO_OR_GOTO_ERR(for_all_cpus(set_temperature_target, EVEN_COUNTERS));
1461 plugin_register_complex_read(NULL, PLUGIN_NAME, turbostat_read, NULL, NULL);
1469 void module_register(void);
1470 void module_register(void)
1472 plugin_register_init(PLUGIN_NAME, turbostat_init);