111eed244156f18fdb3cf4e400ac016ef982972f
1 /*
2 * turbostat -- Log CPU frequency and C-state residency
3 * on modern Intel turbo-capable processors for collectd.
4 *
5 * Based on the 'turbostat' tool of the Linux kernel, found at
6 * linux/tools/power/x86/turbostat/turbostat.c:
7 * ----
8 * Copyright (c) 2013 Intel Corporation.
9 * Len Brown <len.brown@intel.com>
10 *
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.
14 *
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
18 * more details.
19 *
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.
23 * ----
24 * Ported to collectd by Vincent Brillault <git@lerya.net>
25 */
27 /*
28 * _GNU_SOURCE is required because of the following functions:
29 * - CPU_ISSET_S
30 * - CPU_ZERO_S
31 * - CPU_SET_S
32 * - CPU_FREE
33 * - CPU_ALLOC
34 * - CPU_ALLOC_SIZE
35 */
36 #define _GNU_SOURCE
38 #include <asm/msr-index.h>
39 #include <stdarg.h>
40 #include <stdio.h>
41 #include <err.h>
42 #include <unistd.h>
43 #include <sys/types.h>
44 #include <sys/wait.h>
45 #include <sys/stat.h>
46 #include <sys/resource.h>
47 #include <fcntl.h>
48 #include <signal.h>
49 #include <sys/time.h>
50 #include <stdlib.h>
51 #include <dirent.h>
52 #include <string.h>
53 #include <ctype.h>
54 #include <sched.h>
55 #include <cpuid.h>
57 #include "collectd.h"
58 #include "common.h"
59 #include "plugin.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;
103 int backwards_count;
105 cpu_set_t *cpu_present_set, *cpu_affinity_set;
106 size_t cpu_present_setsize, cpu_affinity_setsize;
108 struct thread_data {
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;
114 unsigned int cpu_id;
115 unsigned int flags;
116 #define CPU_IS_FIRST_THREAD_IN_CORE 0x2
117 #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
118 } *thread_even, *thread_odd;
120 struct core_data {
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;
128 struct pkg_data {
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)
162 struct topo_params {
163 int num_packages;
164 int num_cpus;
165 int num_cores;
166 int max_cpu_num;
167 int num_cores_per_pkg;
168 int num_threads_per_core;
169 } topo;
171 struct timeval tv_even, tv_odd, tv_delta;
173 enum return_values {
174 OK = 0,
175 ERR_CPU_MIGRATE,
176 ERR_MSR_IA32_APERF,
177 ERR_MSR_IA32_MPERF,
178 ERR_MSR_SMI_COUNT,
179 ERR_MSR_CORE_C3_RESIDENCY,
180 ERR_MSR_CORE_C6_RESIDENCY,
181 ERR_MSR_CORE_C7_RESIDENCY,
182 ERR_MSR_IA32_THERM_STATUS,
183 ERR_MSR_PKG_C3_RESIDENCY,
184 ERR_MSR_PKG_C6_RESIDENCY,
185 ERR_MSR_PKG_C2_RESIDENCY,
186 ERR_MSR_PKG_C7_RESIDENCY,
187 ERR_MSR_PKG_C8_RESIDENCY,
188 ERR_MSR_PKG_C9_RESIDENCY,
189 ERR_MSR_PKG_C10_RESIDENCY,
190 ERR_MSR_PKG_ENERGY_STATUS,
191 ERR_MSR_PKG_POWER_INFO,
192 ERR_MSR_PP0_ENERGY_STATUS,
193 ERR_MSR_DRAM_ENERGY_STATUS,
194 ERR_MSR_PP1_ENERGY_STATUS,
195 ERR_MSR_PKG_PERF_STATUS,
196 ERR_MSR_DRAM_PERF_STATUS,
197 ERR_MSR_IA32_PACKAGE_THERM_STATUS,
198 ERR_MSR_IA32_TSC,
199 ERR_CPU_NOT_PRESENT,
200 ERR_NO_MSR,
201 ERR_CANT_OPEN_MSR,
202 ERR_CANT_OPEN_FILE,
203 ERR_CANT_READ_NUMBER,
204 ERR_CANT_READ_PROC_STAT,
205 ERR_NO_INVARIANT_TSC,
206 ERR_NO_APERF,
207 ERR_CALLOC,
208 ERR_CPU_ALLOC,
209 ERR_NOT_ROOT,
210 UNSUPPORTED_CPU,
211 };
213 static int setup_all_buffers(void);
215 static int
216 cpu_is_not_present(int cpu)
217 {
218 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
219 }
220 /*
221 * run func(thread, core, package) in topology order
222 * skip non-present cpus
223 */
225 static int __attribute__((warn_unused_result))
226 for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
227 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
228 {
229 int retval, pkg_no, core_no, thread_no;
231 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
232 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
233 for (thread_no = 0; thread_no <
234 topo.num_threads_per_core; ++thread_no) {
235 struct thread_data *t;
236 struct core_data *c;
237 struct pkg_data *p;
239 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
241 if (cpu_is_not_present(t->cpu_id))
242 continue;
244 c = GET_CORE(core_base, core_no, pkg_no);
245 p = GET_PKG(pkg_base, pkg_no);
247 retval = func(t, c, p);
248 if (retval)
249 return retval;
250 }
251 }
252 }
253 return 0;
254 }
256 static int __attribute__((warn_unused_result))
257 cpu_migrate(int cpu)
258 {
259 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
260 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
261 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
262 return -ERR_CPU_MIGRATE;
263 else
264 return 0;
265 }
267 static int __attribute__((warn_unused_result))
268 open_msr(int cpu)
269 {
270 char pathname[32];
271 int fd;
273 /* FIXME: Do we really need this, why? */
274 if (cpu_migrate(cpu)) {
275 ERROR("Could not migrate to CPU %d", cpu);
276 return -ERR_CPU_MIGRATE;
277 }
279 ssnprintf(pathname, sizeof(pathname), "/dev/cpu/%d/msr", cpu);
280 fd = open(pathname, O_RDONLY);
281 if (fd < 0)
282 return -ERR_CANT_OPEN_MSR;
283 return fd;
284 }
286 static int __attribute__((warn_unused_result))
287 read_msr(int fd, off_t offset, unsigned long long *msr)
288 {
289 ssize_t retval;
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);
295 return -1;
296 }
297 return 0;
298 }
300 static int __attribute__((warn_unused_result))
301 get_msr(int cpu, off_t offset, unsigned long long *msr)
302 {
303 ssize_t retval;
304 int fd;
306 fd = open_msr(cpu);
307 if (fd < 0)
308 return fd;
309 retval = read_msr(fd, offset, msr);
310 close(fd);
311 return retval;
312 }
314 #define DELTA_WRAP32(new, old) \
315 if (new > old) { \
316 old = new - old; \
317 } else { \
318 old = 0x100000000 + new - old; \
319 }
321 static void
322 delta_package(struct pkg_data *new, struct pkg_data *old)
323 {
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);
339 }
341 static void
342 delta_core(struct core_data *new, struct core_data *old)
343 {
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;
348 }
350 /*
351 * old = new - old
352 */
353 static int __attribute__((warn_unused_result))
354 delta_thread(struct thread_data *new, struct thread_data *old,
355 struct core_data *core_delta)
356 {
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\"");
364 return -1;
365 }
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;
372 } else {
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;
380 }
381 /*
382 * mperf delta is likely a huge "positive" number
383 * can not use it for calculating c0 time
384 */
385 skip_c0 = 1;
386 skip_c1 = 1;
387 }
390 /*
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.
394 */
395 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
396 old->c1 = 0;
397 else {
398 /* normal case, derive c1 */
399 old->c1 = old->tsc - old->mperf - core_delta->c3
400 - core_delta->c6 - core_delta->c7;
401 }
403 if (old->mperf == 0) {
404 WARNING("cpu%d MPERF 0!", old->cpu_id);
405 old->mperf = 1; /* divide by 0 protection */
406 }
408 old->smi_count = new->smi_count - old->smi_count;
410 return 0;
411 }
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)
417 {
418 int ret;
420 /* calculate core delta only for 1st thread in core */
421 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
422 delta_core(c, c2);
424 /* always calculate thread delta */
425 ret = delta_thread(t, t2, c2); /* c2 is core delta */
426 if (ret != 0)
427 return ret;
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);
433 return 0;
434 }
436 /*
437 * get_counters(...)
438 * migrate to cpu
439 * acquire and record local counters for that cpu
440 */
441 static int __attribute__((warn_unused_result))
442 get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
443 {
444 int cpu = t->cpu_id;
445 unsigned long long msr;
446 int msr_fd;
447 int retval = 0;
449 msr_fd = open_msr(cpu);
450 if (msr_fd < 0)
451 return msr_fd;
453 #define READ_MSR(msr, dst) \
454 do { \
455 if (read_msr(msr_fd, msr, dst)) { \
456 retval = -ERR_##msr; \
457 goto out; \
458 } \
459 } while (0)
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)) {
471 retval = 0;
472 goto out;
473 }
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);
482 if (do_dts) {
483 READ_MSR(MSR_IA32_THERM_STATUS, &msr);
484 c->core_temp_c = p->tcc_activation_temp - ((msr >> 16) & 0x7F);
485 }
487 /* collect package counters only for 1st core in package */
488 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
489 retval = 0;
490 goto out;
491 }
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;
511 }
512 if (do_rapl & RAPL_CORES) {
513 READ_MSR(MSR_PP0_ENERGY_STATUS, &msr);
514 p->energy_cores = msr & 0xFFFFFFFF;
515 }
516 if (do_rapl & RAPL_DRAM) {
517 READ_MSR(MSR_DRAM_ENERGY_STATUS, &msr);
518 p->energy_dram = msr & 0xFFFFFFFF;
519 }
520 if (do_rapl & RAPL_GFX) {
521 READ_MSR(MSR_PP1_ENERGY_STATUS, &msr);
522 p->energy_gfx = msr & 0xFFFFFFFF;
523 }
524 if (do_rapl & RAPL_PKG_PERF_STATUS) {
525 READ_MSR(MSR_PKG_PERF_STATUS, &msr);
526 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
527 }
528 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
529 READ_MSR(MSR_DRAM_PERF_STATUS, &msr);
530 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
531 }
532 if (do_ptm) {
533 READ_MSR(MSR_IA32_PACKAGE_THERM_STATUS, &msr);
534 p->pkg_temp_c = p->tcc_activation_temp - ((msr >> 16) & 0x7F);
535 }
537 out:
538 close(msr_fd);
539 return retval;
540 }
542 static void
543 free_all_buffers(void)
544 {
545 allocated = 0;
546 initialized = 0;
548 CPU_FREE(cpu_present_set);
549 cpu_present_set = NULL;
550 cpu_present_set = 0;
552 CPU_FREE(cpu_affinity_set);
553 cpu_affinity_set = NULL;
554 cpu_affinity_setsize = 0;
556 free(thread_even);
557 free(core_even);
558 free(package_even);
560 thread_even = NULL;
561 core_even = NULL;
562 package_even = NULL;
564 free(thread_odd);
565 free(core_odd);
566 free(package_odd);
568 thread_odd = NULL;
569 core_odd = NULL;
570 package_odd = NULL;
571 }
573 /*
574 * Parse a file containing a single int.
575 */
576 static int __attribute__ ((format(printf,1,2)))
577 parse_int_file(const char *fmt, ...)
578 {
579 va_list args;
580 char path[PATH_MAX];
581 FILE *filep;
582 int value;
584 va_start(args, fmt);
585 vsnprintf(path, sizeof(path), fmt, args);
586 va_end(args);
587 filep = fopen(path, "r");
588 if (!filep) {
589 ERROR("%s: open failed", path);
590 return -ERR_CANT_OPEN_FILE;
591 }
592 if (fscanf(filep, "%d", &value) != 1) {
593 ERROR("%s: failed to parse number from file", path);
594 return -ERR_CANT_READ_NUMBER;
595 }
596 fclose(filep);
597 return value;
598 }
600 /*
601 * cpu_is_first_sibling_in_core(cpu)
602 * return 1 if given CPU is 1st HT sibling in the core
603 */
604 static int
605 cpu_is_first_sibling_in_core(int cpu)
606 {
607 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
608 }
610 /*
611 * cpu_is_first_core_in_package(cpu)
612 * return 1 if given CPU is 1st core in package
613 */
614 static int
615 cpu_is_first_core_in_package(int cpu)
616 {
617 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
618 }
620 static int
621 get_physical_package_id(int cpu)
622 {
623 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
624 }
626 static int
627 get_core_id(int cpu)
628 {
629 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
630 }
632 static int
633 get_num_ht_siblings(int cpu)
634 {
635 char path[80];
636 FILE *filep;
637 int sib1, sib2;
638 int matches;
639 char character;
641 ssnprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
642 filep = fopen(path, "r");
643 if (!filep) {
644 ERROR("%s: open failed", path);
645 return -ERR_CANT_OPEN_FILE;
646 }
647 /*
648 * file format:
649 * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4)
650 * otherwinse 1 sibling (self).
651 */
652 matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2);
654 fclose(filep);
656 if (matches == 3)
657 return 2;
658 else
659 return 1;
660 }
662 /*
663 * run func(thread, core, package) in topology order
664 * skip non-present cpus
665 */
668 static int __attribute__((warn_unused_result))
669 for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
670 struct pkg_data *, struct thread_data *, struct core_data *,
671 struct pkg_data *), struct thread_data *thread_base,
672 struct core_data *core_base, struct pkg_data *pkg_base,
673 struct thread_data *thread_base2, struct core_data *core_base2,
674 struct pkg_data *pkg_base2)
675 {
676 int retval, pkg_no, core_no, thread_no;
678 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
679 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
680 for (thread_no = 0; thread_no <
681 topo.num_threads_per_core; ++thread_no) {
682 struct thread_data *t, *t2;
683 struct core_data *c, *c2;
684 struct pkg_data *p, *p2;
686 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
688 if (cpu_is_not_present(t->cpu_id))
689 continue;
691 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
693 c = GET_CORE(core_base, core_no, pkg_no);
694 c2 = GET_CORE(core_base2, core_no, pkg_no);
696 p = GET_PKG(pkg_base, pkg_no);
697 p2 = GET_PKG(pkg_base2, pkg_no);
699 retval = func(t, c, p, t2, c2, p2);
700 if (retval)
701 return retval;
702 }
703 }
704 }
705 return 0;
706 }
708 /*
709 * run func(cpu) on every cpu in /proc/stat
710 * return max_cpu number
711 */
712 static int __attribute__((warn_unused_result))
713 for_all_proc_cpus(int (func)(int))
714 {
715 FILE *fp;
716 int cpu_num;
717 int retval;
719 fp = fopen(proc_stat, "r");
720 if (!fp) {
721 ERROR("%s: open failed", proc_stat);
722 return -ERR_CANT_OPEN_FILE;
723 }
725 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
726 if (retval != 0) {
727 ERROR("%s: failed to parse format", proc_stat);
728 return -ERR_CANT_READ_PROC_STAT;
729 }
731 while (1) {
732 retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
733 if (retval != 1)
734 break;
736 retval = func(cpu_num);
737 if (retval) {
738 fclose(fp);
739 return(retval);
740 }
741 }
742 fclose(fp);
743 return 0;
744 }
746 /*
747 * count_cpus()
748 * remember the last one seen, it will be the max
749 */
750 static int
751 count_cpus(int cpu)
752 {
753 if (topo.max_cpu_num < cpu)
754 topo.max_cpu_num = cpu;
756 topo.num_cpus += 1;
757 return 0;
758 }
759 static int
760 mark_cpu_present(int cpu)
761 {
762 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
763 return 0;
764 }
767 static void
768 turbostat_submit (const char *plugin_instance,
769 const char *type, const char *type_instance,
770 gauge_t value)
771 {
772 value_list_t vl = VALUE_LIST_INIT;
773 value_t v;
775 v.gauge = value;
776 vl.values = &v;
777 vl.values_len = 1;
778 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
779 sstrncpy (vl.plugin, PLUGIN_NAME, sizeof (vl.plugin));
780 if (plugin_instance != NULL)
781 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
782 sstrncpy (vl.type, type, sizeof (vl.type));
783 if (type_instance != NULL)
784 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
786 plugin_dispatch_values (&vl);
787 }
789 /*
790 * column formatting convention & formats
791 * package: "pk" 2 columns %2d
792 * core: "cor" 3 columns %3d
793 * CPU: "CPU" 3 columns %3d
794 * Pkg_W: %6.2
795 * Cor_W: %6.2
796 * GFX_W: %5.2
797 * RAM_W: %5.2
798 * GHz: "GHz" 3 columns %3.2
799 * TSC: "TSC" 3 columns %3.2
800 * SMI: "SMI" 4 columns %4d
801 * percentage " %pc3" %6.2
802 * Perf Status percentage: %5.2
803 * "CTMP" 4 columns %4d
804 */
805 #define NAME_LEN 12
806 static int
807 submit_counters(struct thread_data *t, struct core_data *c,
808 struct pkg_data *p)
809 {
810 char name[NAME_LEN];
811 double interval_float;
813 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
815 ssnprintf(name, sizeof(name), "cpu%02d", t->cpu_id);
817 if (!skip_c0)
818 turbostat_submit(name, "percent", "c0", 100.0 * t->mperf/t->tsc);
819 if (!skip_c1)
820 turbostat_submit(name, "percent", "c1", 100.0 * t->c1/t->tsc);
822 /* GHz */
823 if ((!aperf_mperf_unstable) || (!(t->aperf > t->tsc || t->mperf > t->tsc)))
824 turbostat_submit(NULL, "frequency", name, 1.0 * t->tsc / 1000000000 * t->aperf / t->mperf / interval_float);
826 /* SMI */
827 turbostat_submit(NULL, "current", name, t->smi_count);
829 /* print per-core data only for 1st thread in core */
830 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
831 goto done;
833 ssnprintf(name, sizeof(name), "core%02d", c->core_id);
835 if (do_core_cstate & (1 << 3))
836 turbostat_submit(name, "percent", "c3", 100.0 * c->c3/t->tsc);
837 if (do_core_cstate & (1 << 6))
838 turbostat_submit(name, "percent", "c6", 100.0 * c->c6/t->tsc);
839 if (do_core_cstate & (1 << 7))
840 turbostat_submit(name, "percent", "c7", 100.0 * c->c7/t->tsc);
842 if (do_dts)
843 turbostat_submit(NULL, "temperature", name, c->core_temp_c);
845 /* print per-package data only for 1st core in package */
846 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
847 goto done;
849 ssnprintf(name, sizeof(name), "pkg%02d", p->package_id);
851 if (do_ptm)
852 turbostat_submit(NULL, "temperature", name, p->pkg_temp_c);
854 if (do_pkg_cstate & (1 << 2))
855 turbostat_submit(name, "percent", "pc2", 100.0 * p->pc2/t->tsc);
856 if (do_pkg_cstate & (1 << 3))
857 turbostat_submit(name, "percent", "pc3", 100.0 * p->pc3/t->tsc);
858 if (do_pkg_cstate & (1 << 6))
859 turbostat_submit(name, "percent", "pc6", 100.0 * p->pc6/t->tsc);
860 if (do_pkg_cstate & (1 << 7))
861 turbostat_submit(name, "percent", "pc7", 100.0 * p->pc7/t->tsc);
862 if (do_pkg_cstate & (1 << 8))
863 turbostat_submit(name, "percent", "pc8", 100.0 * p->pc8/t->tsc);
864 if (do_pkg_cstate & (1 << 9))
865 turbostat_submit(name, "percent", "pc9", 100.0 * p->pc9/t->tsc);
866 if (do_pkg_cstate & (1 << 10))
867 turbostat_submit(name, "percent", "pc10", 100.0 * p->pc10/t->tsc);
869 if (do_rapl) {
870 if (do_rapl & RAPL_PKG)
871 turbostat_submit(name, "power", "Pkg_W", p->energy_pkg * rapl_energy_units / interval_float);
872 if (do_rapl & RAPL_CORES)
873 turbostat_submit(name, "power", "Cor_W", p->energy_cores * rapl_energy_units / interval_float);
874 if (do_rapl & RAPL_GFX)
875 turbostat_submit(name, "power", "GFX_W", p->energy_gfx * rapl_energy_units / interval_float);
876 if (do_rapl & RAPL_DRAM)
877 turbostat_submit(name, "power", "RAM_W", p->energy_dram * rapl_energy_units / interval_float);
878 }
879 done:
880 return 0;
881 }
883 static int
884 turbostat_read(user_data_t * not_used)
885 {
886 int ret;
888 if (!allocated) {
889 if ((ret = setup_all_buffers()) < 0)
890 return ret;
891 }
893 if (for_all_proc_cpus(cpu_is_not_present)) {
894 free_all_buffers();
895 if ((ret = setup_all_buffers()) < 0)
896 return ret;
897 if (for_all_proc_cpus(cpu_is_not_present))
898 return -ERR_CPU_NOT_PRESENT;
899 }
901 if (!initialized) {
902 if ((ret = for_all_cpus(get_counters, EVEN_COUNTERS)) < 0)
903 return ret;
904 gettimeofday(&tv_even, (struct timezone *)NULL);
905 is_even = 1;
906 initialized = 1;
907 return 0;
908 }
910 if (is_even) {
911 if ((ret = for_all_cpus(get_counters, ODD_COUNTERS)) < 0)
912 return ret;
913 gettimeofday(&tv_odd, (struct timezone *)NULL);
914 is_even = 0;
915 timersub(&tv_odd, &tv_even, &tv_delta);
916 if ((ret = for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) < 0)
917 return ret;
918 if ((ret = for_all_cpus(submit_counters, EVEN_COUNTERS)) < 0)
919 return ret;
920 } else {
921 if ((ret = for_all_cpus(get_counters, EVEN_COUNTERS)) < 0)
922 return ret;
923 gettimeofday(&tv_even, (struct timezone *)NULL);
924 is_even = 1;
925 timersub(&tv_even, &tv_odd, &tv_delta);
926 if ((ret = for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) < 0)
927 return ret;
928 if ((ret = for_all_cpus(submit_counters, ODD_COUNTERS)) < 0)
929 return ret;
930 }
931 return 0;
932 }
934 static int __attribute__((warn_unused_result))
935 check_dev_msr()
936 {
937 struct stat sb;
939 if (stat("/dev/cpu/0/msr", &sb)) {
940 ERROR("no /dev/cpu/0/msr, try \"# modprobe msr\"");
941 return -ERR_NO_MSR;
942 }
943 return 0;
944 }
946 static int __attribute__((warn_unused_result))
947 check_super_user()
948 {
949 if (getuid() != 0) {
950 ERROR("must be root");
951 return -ERR_NOT_ROOT;
952 }
953 return 0;
954 }
956 /*
957 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
958 * the Thermal Control Circuit (TCC) activates.
959 * This is usually equal to tjMax.
960 *
961 * Older processors do not have this MSR, so there we guess,
962 * but also allow cmdline over-ride with -T.
963 *
964 * Several MSR temperature values are in units of degrees-C
965 * below this value, including the Digital Thermal Sensor (DTS),
966 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
967 */
968 static int __attribute__((warn_unused_result))
969 set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
970 {
971 unsigned long long msr;
972 unsigned int target_c_local;
974 /* tcc_activation_temp is used only for dts or ptm */
975 if (!(do_dts || do_ptm))
976 return 0;
978 /* this is a per-package concept */
979 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
980 return 0;
982 if (tcc_activation_temp != 0) {
983 p->tcc_activation_temp = tcc_activation_temp;
984 return 0;
985 }
987 if (get_msr(t->cpu_id, MSR_IA32_TEMPERATURE_TARGET, &msr))
988 goto guess;
990 target_c_local = (msr >> 16) & 0x7F;
992 if (target_c_local < 85 || target_c_local > 127)
993 goto guess;
995 p->tcc_activation_temp = target_c_local;
997 return 0;
999 guess:
1000 p->tcc_activation_temp = TJMAX_DEFAULT;
1001 WARNING("cpu%d: Guessing tjMax %d C, Please use TCCActivationTemp to specify",
1002 t->cpu_id, p->tcc_activation_temp);
1004 return 0;
1005 }
1007 /*
1008 * Identify the functionality of the CPU
1009 */
1010 static int __attribute__((warn_unused_result))
1011 probe_cpu()
1012 {
1013 unsigned int eax, ebx, ecx, edx, max_level;
1014 unsigned int fms, family, model;
1016 /* CPUID(0):
1017 * - EAX: Maximum Input Value for Basic CPUID Information
1018 * - EBX: "Genu" (0x756e6547)
1019 * - EDX: "ineI" (0x49656e69)
1020 * - ECX: "ntel" (0x6c65746e)
1021 */
1022 max_level = ebx = ecx = edx = 0;
1023 __get_cpuid(0, &max_level, &ebx, &ecx, &edx);
1024 if (ebx != 0x756e6547 && edx != 0x49656e69 && ecx != 0x6c65746e) {
1025 ERROR("Unsupported CPU");
1026 return -UNSUPPORTED_CPU;
1027 }
1029 /* CPUID(1):
1030 * - EAX: Version Information: Type, Family, Model, and Stepping ID
1031 * + 4-7: Model ID
1032 * + 8-11: Family ID
1033 * + 12-13: Processor type
1034 * + 16-19: Extended Model ID
1035 * + 20-27: Extended Family ID
1036 * - EDX: Feature Information:
1037 * + 5: Support for MSR read/write operations
1038 */
1039 fms = ebx = ecx = edx = 0;
1040 __get_cpuid(1, &fms, &ebx, &ecx, &edx);
1041 family = (fms >> 8) & 0xf;
1042 model = (fms >> 4) & 0xf;
1043 if (family == 0xf)
1044 family += (fms >> 20) & 0xf;
1045 if (family == 6 || family == 0xf)
1046 model += ((fms >> 16) & 0xf) << 4;
1047 if (!(edx & (1 << 5))) {
1048 ERROR("CPUID: no MSR");
1049 return -ERR_NO_MSR;
1050 }
1052 /*
1053 * CPUID(0x80000000):
1054 * - EAX: Maximum Input Value for Extended Function CPUID Information
1055 *
1056 * This allows us to verify if the CPUID(0x80000007) can be called
1057 *
1058 * This check is valid for both Intel and AMD.
1059 */
1060 max_level = ebx = ecx = edx = 0;
1061 __get_cpuid(0x80000000, &max_level, &ebx, &ecx, &edx);
1062 if (max_level < 0x80000007) {
1063 ERROR("CPUID: no invariant TSC (max_level 0x%x)", max_level);
1064 return -ERR_NO_INVARIANT_TSC;
1065 }
1067 /*
1068 * CPUID(0x80000007):
1069 * - EDX:
1070 * + 8: Invariant TSC available if set
1071 *
1072 * This check is valid for both Intel and AMD
1073 */
1074 __get_cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
1075 if (!(edx & (1 << 8))) {
1076 ERROR("No invariant TSC");
1077 return -ERR_NO_INVARIANT_TSC;
1078 }
1080 /*
1081 * CPUID(6):
1082 * - EAX:
1083 * + 0: Digital temperature sensor is supported if set
1084 * + 6: Package thermal management is supported if set
1085 * - ECX:
1086 * + 0: Hardware Coordination Feedback Capability (Presence of IA32_MPERF and IA32_APERF).
1087 * + 3: The processor supports performance-energy bias preference if set.
1088 * It also implies the presence of a new architectural MSR called IA32_ENERGY_PERF_BIAS
1089 *
1090 * This check is valid for both Intel and AMD
1091 */
1092 __get_cpuid(0x6, &eax, &ebx, &ecx, &edx);
1093 do_dts = eax & (1 << 0);
1094 do_ptm = eax & (1 << 6);
1095 if (!(ecx & (1 << 0))) {
1096 ERROR("No APERF");
1097 return -ERR_NO_APERF;
1098 }
1100 /*
1101 * Enable or disable C states depending on the model and family
1102 */
1103 if (family == 6) {
1104 switch (model) {
1105 /* Atom (partial) */
1106 case 0x27:
1107 do_core_cstate = 0;
1108 do_pkg_cstate = (1 << 2) | (1 << 4) | (1 << 6);
1109 break;
1110 /* Silvermont */
1111 case 0x37: /* BYT */
1112 case 0x4A:
1113 case 0x4D: /* AVN */
1114 case 0x5A:
1115 case 0x5D:
1116 do_core_cstate = (1 << 1) | (1 << 6);
1117 do_pkg_cstate = (1 << 6);
1118 break;
1119 /* Nehalem */
1120 case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
1121 case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
1122 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
1123 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1124 do_core_cstate = (1 << 3) | (1 << 6);
1125 do_pkg_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1126 break;
1127 /* Westmere */
1128 case 0x25: /* Westmere Client - Clarkdale, Arrandale */
1129 case 0x2C: /* Westmere EP - Gulftown */
1130 case 0x2F: /* Westmere-EX Xeon - Eagleton */
1131 do_core_cstate = (1 << 3) | (1 << 6);
1132 do_pkg_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1133 break;
1134 /* Sandy Bridge */
1135 case 0x2A: /* SNB */
1136 case 0x2D: /* SNB Xeon */
1137 do_core_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1138 do_pkg_cstate = (1 << 2) | (1 << 3) | (1 << 6) | (1 << 7);
1139 break;
1140 /* Ivy Bridge */
1141 case 0x3A: /* IVB */
1142 case 0x3E: /* IVB Xeon */
1143 do_core_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1144 do_pkg_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1145 break;
1146 /* Haswell Bridge */
1147 case 0x3C: /* HSW */
1148 case 0x3F: /* HSW */
1149 case 0x46: /* HSW */
1150 do_core_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1151 do_pkg_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1152 break;
1153 case 0x45: /* HSW */
1154 do_core_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1155 do_pkg_cstate = (1 << 3) | (1 << 6) | (1 << 7) | (1 << 8) | (1 << 9) | (1 << 10);
1156 break;
1157 /* Broadwel */
1158 case 0x4F: /* BDW */
1159 case 0x56: /* BDX-DE */
1160 do_core_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1161 do_pkg_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1162 break;
1163 case 0x3D: /* BDW */
1164 do_core_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1165 do_pkg_cstate = (1 << 3) | (1 << 6) | (1 << 7) | (1 << 8) | (1 << 9) | (1 << 10);
1166 break;
1167 default:
1168 ERROR("Unsupported CPU");
1169 }
1170 switch (model) {
1171 case 0x2A:
1172 case 0x3A:
1173 case 0x3C:
1174 case 0x45:
1175 case 0x46:
1176 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_PKG_POWER_INFO | RAPL_GFX;
1177 break;
1178 case 0x3F:
1179 do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO | RAPL_PKG_PERF_STATUS | RAPL_DRAM | RAPL_DRAM_PERF_STATUS;
1180 break;
1181 case 0x2D:
1182 case 0x3E:
1183 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_PKG_POWER_INFO | RAPL_PKG_PERF_STATUS | RAPL_DRAM | RAPL_DRAM_PERF_STATUS;
1184 break;
1185 case 0x37:
1186 case 0x4D:
1187 do_rapl = RAPL_PKG | RAPL_CORES;
1188 break;
1189 default:
1190 do_rapl = 0;
1191 }
1192 } else {
1193 ERROR("Unsupported CPU");
1194 return -UNSUPPORTED_CPU;
1195 }
1197 if (do_rapl) {
1198 unsigned long msr;
1199 if (get_msr(0, MSR_RAPL_POWER_UNIT, &msr))
1200 return 0;
1202 if (model == 0x37)
1203 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
1204 else
1205 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
1206 }
1208 return 0;
1209 }
1213 static int __attribute__((warn_unused_result))
1214 topology_probe()
1215 {
1216 int i;
1217 int ret;
1218 int max_core_id = 0;
1219 int max_package_id = 0;
1220 int max_siblings = 0;
1221 struct cpu_topology {
1222 int core_id;
1223 int physical_package_id;
1224 } *cpus;
1226 /* Initialize num_cpus, max_cpu_num */
1227 topo.num_cpus = 0;
1228 topo.max_cpu_num = 0;
1229 ret = for_all_proc_cpus(count_cpus);
1230 if (ret < 0)
1231 return ret;
1233 DEBUG("num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
1235 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
1236 if (cpus == NULL) {
1237 ERROR("calloc cpus");
1238 return -ERR_CALLOC;
1239 }
1241 /*
1242 * Allocate and initialize cpu_present_set
1243 */
1244 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
1245 if (cpu_present_set == NULL) {
1246 free(cpus);
1247 ERROR("CPU_ALLOC");
1248 return -ERR_CPU_ALLOC;
1249 }
1250 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
1251 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
1252 ret = for_all_proc_cpus(mark_cpu_present);
1253 if (ret < 0) {
1254 free(cpus);
1255 return ret;
1256 }
1258 /*
1259 * Allocate and initialize cpu_affinity_set
1260 */
1261 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
1262 if (cpu_affinity_set == NULL) {
1263 free(cpus);
1264 ERROR("CPU_ALLOC");
1265 return -ERR_CPU_ALLOC;
1266 }
1267 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
1268 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
1271 /*
1272 * For online cpus
1273 * find max_core_id, max_package_id
1274 */
1275 for (i = 0; i <= topo.max_cpu_num; ++i) {
1276 int siblings;
1278 if (cpu_is_not_present(i)) {
1279 WARNING("cpu%d NOT PRESENT", i);
1280 continue;
1281 }
1282 cpus[i].core_id = get_core_id(i);
1283 if (cpus[i].core_id < 0)
1284 return cpus[i].core_id;
1285 if (cpus[i].core_id > max_core_id)
1286 max_core_id = cpus[i].core_id;
1288 cpus[i].physical_package_id = get_physical_package_id(i);
1289 if (cpus[i].physical_package_id < 0)
1290 return cpus[i].physical_package_id;
1291 if (cpus[i].physical_package_id > max_package_id)
1292 max_package_id = cpus[i].physical_package_id;
1294 siblings = get_num_ht_siblings(i);
1295 if (siblings < 0)
1296 return siblings;
1297 if (siblings > max_siblings)
1298 max_siblings = siblings;
1299 DEBUG("cpu %d pkg %d core %d\n",
1300 i, cpus[i].physical_package_id, cpus[i].core_id);
1301 }
1302 topo.num_cores_per_pkg = max_core_id + 1;
1303 DEBUG("max_core_id %d, sizing for %d cores per package\n",
1304 max_core_id, topo.num_cores_per_pkg);
1306 topo.num_packages = max_package_id + 1;
1307 DEBUG("max_package_id %d, sizing for %d packages\n",
1308 max_package_id, topo.num_packages);
1310 topo.num_threads_per_core = max_siblings;
1311 DEBUG("max_siblings %d\n", max_siblings);
1313 free(cpus);
1314 return 0;
1315 }
1317 static int
1318 allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
1319 {
1320 int i;
1322 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
1323 topo.num_packages, sizeof(struct thread_data));
1324 if (*t == NULL)
1325 goto error;
1327 for (i = 0; i < topo.num_threads_per_core *
1328 topo.num_cores_per_pkg * topo.num_packages; i++)
1329 (*t)[i].cpu_id = -1;
1331 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
1332 sizeof(struct core_data));
1333 if (*c == NULL)
1334 goto error;
1336 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
1337 (*c)[i].core_id = -1;
1339 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
1340 if (*p == NULL)
1341 goto error;
1343 for (i = 0; i < topo.num_packages; i++)
1344 (*p)[i].package_id = i;
1346 return 0;
1347 error:
1348 ERROR("calloc counters");
1349 return -ERR_CALLOC;
1350 }
1351 /*
1352 * init_counter()
1353 *
1354 * set cpu_id, core_num, pkg_num
1355 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
1356 *
1357 * increment topo.num_cores when 1st core in pkg seen
1358 */
1359 static int
1360 init_counter(struct thread_data *thread_base, struct core_data *core_base,
1361 struct pkg_data *pkg_base, int thread_num, int core_num,
1362 int pkg_num, int cpu_id)
1363 {
1364 int ret;
1365 struct thread_data *t;
1366 struct core_data *c;
1367 struct pkg_data *p;
1369 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
1370 c = GET_CORE(core_base, core_num, pkg_num);
1371 p = GET_PKG(pkg_base, pkg_num);
1373 t->cpu_id = cpu_id;
1374 if (thread_num == 0) {
1375 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
1376 if ((ret = cpu_is_first_core_in_package(cpu_id)) < 0) {
1377 return ret;
1378 } else if (ret != 0) {
1379 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
1380 }
1381 }
1383 c->core_id = core_num;
1384 p->package_id = pkg_num;
1386 return 0;
1387 }
1390 static int
1391 initialize_counters(int cpu_id)
1392 {
1393 int my_thread_id, my_core_id, my_package_id;
1394 int ret;
1396 my_package_id = get_physical_package_id(cpu_id);
1397 if (my_package_id < 0)
1398 return my_package_id;
1399 my_core_id = get_core_id(cpu_id);
1400 if (my_core_id < 0)
1401 return my_core_id;
1403 if ((ret = cpu_is_first_sibling_in_core(cpu_id)) < 0) {
1404 return ret;
1405 } else if (ret != 0) {
1406 my_thread_id = 0;
1407 topo.num_cores++;
1408 } else {
1409 my_thread_id = 1;
1410 }
1412 ret = init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
1413 if (ret < 0)
1414 return ret;
1415 ret = init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
1416 if (ret < 0)
1417 return ret;
1418 return 0;
1419 }
1421 #define DO_OR_GOTO_ERR(something) \
1422 do { \
1423 ret = (something); \
1424 if (ret < 0) \
1425 goto err; \
1426 } while (0)
1428 static int setup_all_buffers(void)
1429 {
1430 int ret;
1432 DO_OR_GOTO_ERR(topology_probe());
1433 DO_OR_GOTO_ERR(allocate_counters(&thread_even, &core_even, &package_even));
1434 DO_OR_GOTO_ERR(allocate_counters(&thread_odd, &core_odd, &package_odd));
1435 DO_OR_GOTO_ERR(for_all_proc_cpus(initialize_counters));
1437 allocated = 1;
1438 return 0;
1439 err:
1440 free_all_buffers();
1441 return ret;
1442 }
1444 static int
1445 turbostat_init(void)
1446 {
1447 int ret;
1449 DO_OR_GOTO_ERR(check_super_user());
1450 DO_OR_GOTO_ERR(probe_cpu());
1451 DO_OR_GOTO_ERR(check_dev_msr());
1452 DO_OR_GOTO_ERR(setup_all_buffers());
1453 DO_OR_GOTO_ERR(for_all_cpus(set_temperature_target, EVEN_COUNTERS));
1454 DO_OR_GOTO_ERR(for_all_cpus(set_temperature_target, ODD_COUNTERS));
1456 plugin_register_complex_read(NULL, PLUGIN_NAME, turbostat_read, NULL, NULL);
1458 return 0;
1459 err:
1460 free_all_buffers();
1461 return ret;
1462 }
1464 static const char *config_keys[] =
1465 {
1466 "TCCActivationTemp",
1467 };
1468 static const int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
1470 static int
1471 turbostat_config(const char *key, const char *value)
1472 {
1473 long unsigned int tmp_val;
1474 char *end;
1476 if (strcasecmp("TCCActivationTemp", key) == 0) {
1477 tmp_val = strtoul(value, &end, 0);
1478 if (*end != '\0' || tmp_val > UINT_MAX)
1479 return -1;
1480 tcc_activation_temp = (unsigned int) tmp_val;
1481 } else {
1482 return -1;
1483 }
1484 return 0;
1485 }
1487 void module_register(void);
1488 void module_register(void)
1489 {
1490 plugin_register_init(PLUGIN_NAME, turbostat_init);
1491 plugin_register_config(PLUGIN_NAME, turbostat_config, config_keys, config_keys_num);
1492 }