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 /*
64 * If set, aperf_mperf_unstable disables a/mperf based stats.
65 * This includes: C0 & C1 states, frequency
66 *
67 * This value is automatically set if mperf or aperf decreases
68 */
69 static _Bool aperf_mperf_unstable;
71 static unsigned int do_core_cstate;
72 static unsigned int do_pkg_cstate;
73 static _Bool do_dts;
74 static _Bool do_ptm;
75 static unsigned int tcc_activation_temp;
77 static unsigned int do_rapl;
78 static double rapl_energy_units;
80 #define RAPL_PKG (1 << 0)
81 /* 0x610 MSR_PKG_POWER_LIMIT */
82 /* 0x611 MSR_PKG_ENERGY_STATUS */
83 #define RAPL_PKG_PERF_STATUS (1 << 1)
84 /* 0x613 MSR_PKG_PERF_STATUS */
85 #define RAPL_PKG_POWER_INFO (1 << 2)
86 /* 0x614 MSR_PKG_POWER_INFO */
88 #define RAPL_DRAM (1 << 3)
89 /* 0x618 MSR_DRAM_POWER_LIMIT */
90 /* 0x619 MSR_DRAM_ENERGY_STATUS */
91 /* 0x61c MSR_DRAM_POWER_INFO */
92 #define RAPL_DRAM_PERF_STATUS (1 << 4)
93 /* 0x61b MSR_DRAM_PERF_STATUS */
95 #define RAPL_CORES (1 << 5)
96 /* 0x638 MSR_PP0_POWER_LIMIT */
97 /* 0x639 MSR_PP0_ENERGY_STATUS */
98 #define RAPL_CORE_POLICY (1 << 6)
99 /* 0x63a MSR_PP0_POLICY */
102 #define RAPL_GFX (1 << 7)
103 /* 0x640 MSR_PP1_POWER_LIMIT */
104 /* 0x641 MSR_PP1_ENERGY_STATUS */
105 /* 0x642 MSR_PP1_POLICY */
106 #define TJMAX_DEFAULT 100
108 cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_saved_affinity_set;
109 size_t cpu_present_setsize, cpu_affinity_setsize, cpu_saved_affinity_setsize;
111 struct thread_data {
112 unsigned long long tsc;
113 unsigned long long aperf;
114 unsigned long long mperf;
115 unsigned long long c1;
116 unsigned int smi_count;
117 unsigned int cpu_id;
118 unsigned int flags;
119 #define CPU_IS_FIRST_THREAD_IN_CORE 0x2
120 #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
121 } *thread_delta, *thread_even, *thread_odd;
123 struct core_data {
124 unsigned long long c3;
125 unsigned long long c6;
126 unsigned long long c7;
127 unsigned int core_temp_c;
128 unsigned int core_id;
129 } *core_delta, *core_even, *core_odd;
131 struct pkg_data {
132 unsigned long long pc2;
133 unsigned long long pc3;
134 unsigned long long pc6;
135 unsigned long long pc7;
136 unsigned long long pc8;
137 unsigned long long pc9;
138 unsigned long long pc10;
139 unsigned int package_id;
140 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
141 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
142 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
143 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
144 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
145 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
146 unsigned int tcc_activation_temp;
147 unsigned int pkg_temp_c;
148 } *package_delta, *package_even, *package_odd;
150 #define DELTA_COUNTERS thread_delta, core_delta, package_delta
151 #define ODD_COUNTERS thread_odd, core_odd, package_odd
152 #define EVEN_COUNTERS thread_even, core_even, package_even
153 static _Bool is_even = 1;
155 static _Bool allocated = 0;
156 static _Bool initialized = 0;
158 #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
159 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
160 topo.num_threads_per_core + \
161 (core_no) * topo.num_threads_per_core + (thread_no))
162 #define GET_CORE(core_base, core_no, pkg_no) \
163 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
164 #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
166 struct topo_params {
167 int num_packages;
168 int num_cpus;
169 int num_cores;
170 int max_cpu_num;
171 int num_cores_per_pkg;
172 int num_threads_per_core;
173 } topo;
175 struct timeval tv_even, tv_odd, tv_delta;
177 enum return_values {
178 OK = 0,
179 ERR_CPU_MIGRATE,
180 ERR_CPU_SAVE_SCHED_AFFINITY,
181 ERR_MSR_IA32_APERF,
182 ERR_MSR_IA32_MPERF,
183 ERR_MSR_SMI_COUNT,
184 ERR_MSR_CORE_C3_RESIDENCY,
185 ERR_MSR_CORE_C6_RESIDENCY,
186 ERR_MSR_CORE_C7_RESIDENCY,
187 ERR_MSR_IA32_THERM_STATUS,
188 ERR_MSR_PKG_C3_RESIDENCY,
189 ERR_MSR_PKG_C6_RESIDENCY,
190 ERR_MSR_PKG_C2_RESIDENCY,
191 ERR_MSR_PKG_C7_RESIDENCY,
192 ERR_MSR_PKG_C8_RESIDENCY,
193 ERR_MSR_PKG_C9_RESIDENCY,
194 ERR_MSR_PKG_C10_RESIDENCY,
195 ERR_MSR_PKG_ENERGY_STATUS,
196 ERR_MSR_PKG_POWER_INFO,
197 ERR_MSR_PP0_ENERGY_STATUS,
198 ERR_MSR_DRAM_ENERGY_STATUS,
199 ERR_MSR_PP1_ENERGY_STATUS,
200 ERR_MSR_PKG_PERF_STATUS,
201 ERR_MSR_DRAM_PERF_STATUS,
202 ERR_MSR_IA32_PACKAGE_THERM_STATUS,
203 ERR_MSR_IA32_TSC,
204 ERR_CPU_NOT_PRESENT,
205 ERR_NO_MSR,
206 ERR_CANT_OPEN_MSR,
207 ERR_CANT_OPEN_FILE,
208 ERR_CANT_READ_NUMBER,
209 ERR_CANT_READ_PROC_STAT,
210 ERR_NO_INVARIANT_TSC,
211 ERR_NO_APERF,
212 ERR_CALLOC,
213 ERR_CPU_ALLOC,
214 ERR_NOT_ROOT,
215 UNSUPPORTED_CPU,
216 };
218 static int setup_all_buffers(void);
220 static int
221 cpu_is_not_present(int cpu)
222 {
223 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
224 }
225 /*
226 * run func(thread, core, package) in topology order
227 * skip non-present cpus
228 */
230 static int __attribute__((warn_unused_result))
231 for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
232 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
233 {
234 int retval, pkg_no, core_no, thread_no;
236 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
237 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
238 for (thread_no = 0; thread_no <
239 topo.num_threads_per_core; ++thread_no) {
240 struct thread_data *t;
241 struct core_data *c;
242 struct pkg_data *p;
244 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
246 if (cpu_is_not_present(t->cpu_id))
247 continue;
249 c = GET_CORE(core_base, core_no, pkg_no);
250 p = GET_PKG(pkg_base, pkg_no);
252 retval = func(t, c, p);
253 if (retval)
254 return retval;
255 }
256 }
257 }
258 return 0;
259 }
261 static int __attribute__((warn_unused_result))
262 open_msr(int cpu, _Bool multiple_read)
263 {
264 char pathname[32];
265 int fd;
267 /*
268 * If we need to do multiple read, let's migrate to the CPU
269 * Otherwise, we would lose time calling functions on another CPU
270 *
271 * If we are not yet initialized (cpu_affinity_setsize = 0),
272 * we need to skip this optimisation.
273 */
274 if (multiple_read && cpu_affinity_setsize) {
275 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
276 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
277 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1) {
278 ERROR("Could not migrate to CPU %d", cpu);
279 return -ERR_CPU_MIGRATE;
280 }
281 }
283 ssnprintf(pathname, sizeof(pathname), "/dev/cpu/%d/msr", cpu);
284 fd = open(pathname, O_RDONLY);
285 if (fd < 0)
286 return -ERR_CANT_OPEN_MSR;
287 return fd;
288 }
290 static int __attribute__((warn_unused_result))
291 read_msr(int fd, off_t offset, unsigned long long *msr)
292 {
293 ssize_t retval;
295 retval = pread(fd, msr, sizeof *msr, offset);
297 if (retval != sizeof *msr) {
298 ERROR("MSR offset 0x%llx read failed", (unsigned long long)offset);
299 return -1;
300 }
301 return 0;
302 }
304 static int __attribute__((warn_unused_result))
305 get_msr(int cpu, off_t offset, unsigned long long *msr)
306 {
307 ssize_t retval;
308 int fd;
310 fd = open_msr(cpu, 0);
311 if (fd < 0)
312 return fd;
313 retval = read_msr(fd, offset, msr);
314 close(fd);
315 return retval;
316 }
318 #define DELTA_WRAP32(delta, new, old) \
319 if (new > old) { \
320 delta = new - old; \
321 } else { \
322 delta = 0x100000000 + new - old; \
323 }
325 static void
326 delta_package(struct pkg_data *delta, const struct pkg_data *new, const struct pkg_data *old)
327 {
328 delta->pc2 = new->pc2 - old->pc2;
329 delta->pc3 = new->pc3 - old->pc3;
330 delta->pc6 = new->pc6 - old->pc6;
331 delta->pc7 = new->pc7 - old->pc7;
332 delta->pc8 = new->pc8 - old->pc8;
333 delta->pc9 = new->pc9 - old->pc9;
334 delta->pc10 = new->pc10 - old->pc10;
335 delta->pkg_temp_c = new->pkg_temp_c;
337 DELTA_WRAP32(delta->energy_pkg, new->energy_pkg, old->energy_pkg);
338 DELTA_WRAP32(delta->energy_cores, new->energy_cores, old->energy_cores);
339 DELTA_WRAP32(delta->energy_gfx, new->energy_gfx, old->energy_gfx);
340 DELTA_WRAP32(delta->energy_dram, new->energy_dram, old->energy_dram);
341 DELTA_WRAP32(delta->rapl_pkg_perf_status, new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
342 DELTA_WRAP32(delta->rapl_dram_perf_status, new->rapl_dram_perf_status, old->rapl_dram_perf_status);
343 }
345 static void
346 delta_core(struct core_data *delta, const struct core_data *new, const struct core_data *old)
347 {
348 delta->c3 = new->c3 - old->c3;
349 delta->c6 = new->c6 - old->c6;
350 delta->c7 = new->c7 - old->c7;
351 delta->core_temp_c = new->core_temp_c;
352 }
354 static int __attribute__((warn_unused_result))
355 delta_thread(struct thread_data *delta, const struct thread_data *new, const struct thread_data *old,
356 const struct core_data *core_delta)
357 {
358 delta->tsc = new->tsc - old->tsc;
360 /* check for TSC < 1 Mcycles over interval */
361 if (delta->tsc < (1000 * 1000)) {
362 WARNING("Insanely slow TSC rate, TSC stops in idle? ");
363 WARNING("You can disable all c-states by booting with \"idle=poll\" ");
364 WARNING("or just the deep ones with \"processor.max_cstate=1\"");
365 return -1;
366 }
368 delta->c1 = new->c1 - old->c1;
370 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
371 delta->aperf = new->aperf - old->aperf;
372 delta->mperf = new->mperf - old->mperf;
373 } 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 }
383 /*
384 * As counter collection is not atomic,
385 * it is possible for mperf's non-halted cycles + idle states
386 * to exceed TSC's all cycles: show c1 = 0% in that case.
387 */
388 if ((delta->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > delta->tsc)
389 delta->c1 = 0;
390 else {
391 /* normal case, derive c1 */
392 delta->c1 = delta->tsc - delta->mperf - core_delta->c3
393 - core_delta->c6 - core_delta->c7;
394 }
396 if (delta->mperf == 0) {
397 WARNING("cpu%d MPERF 0!", old->cpu_id);
398 delta->mperf = 1; /* divide by 0 protection */
399 }
401 delta->smi_count = new->smi_count - old->smi_count;
403 return 0;
404 }
406 static int __attribute__((warn_unused_result))
407 delta_cpu(struct thread_data *t_delta, struct core_data *c_delta, struct pkg_data *p_delta,
408 const struct thread_data *t_new, const struct core_data *c_new, const struct pkg_data *p_new,
409 const struct thread_data *t_old, const struct core_data *c_old, const struct pkg_data *p_old)
410 {
411 int ret;
413 /* calculate core delta only for 1st thread in core */
414 if (t_new->flags & CPU_IS_FIRST_THREAD_IN_CORE)
415 delta_core(c_delta, c_new, c_old);
417 /* always calculate thread delta */
418 ret = delta_thread(t_delta, t_new, t_old, c_delta);
419 if (ret != 0)
420 return ret;
422 /* calculate package delta only for 1st core in package */
423 if (t_new->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
424 delta_package(p_delta, p_new, p_old);
426 return 0;
427 }
430 /*
431 * get_counters(...)
432 * migrate to cpu
433 * acquire and record local counters for that cpu
434 */
435 static int __attribute__((warn_unused_result))
436 get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
437 {
438 int cpu = t->cpu_id;
439 unsigned long long msr;
440 int msr_fd;
441 int retval = 0;
443 msr_fd = open_msr(cpu, 1);
444 if (msr_fd < 0)
445 return msr_fd;
447 #define READ_MSR(msr, dst) \
448 do { \
449 if (read_msr(msr_fd, msr, dst)) { \
450 retval = -ERR_##msr; \
451 goto out; \
452 } \
453 } while (0)
455 READ_MSR(MSR_IA32_TSC, &t->tsc);
457 READ_MSR(MSR_IA32_APERF, &t->aperf);
458 READ_MSR(MSR_IA32_MPERF, &t->mperf);
460 READ_MSR(MSR_SMI_COUNT, &msr);
461 t->smi_count = msr & 0xFFFFFFFF;
463 /* collect core counters only for 1st thread in core */
464 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) {
465 retval = 0;
466 goto out;
467 }
469 if (do_core_cstate & (1 << 3))
470 READ_MSR(MSR_CORE_C3_RESIDENCY, &c->c3);
471 if (do_core_cstate & (1 << 6))
472 READ_MSR(MSR_CORE_C6_RESIDENCY, &c->c6);
473 if (do_core_cstate & (1 << 7))
474 READ_MSR(MSR_CORE_C7_RESIDENCY, &c->c7);
476 if (do_dts) {
477 READ_MSR(MSR_IA32_THERM_STATUS, &msr);
478 c->core_temp_c = p->tcc_activation_temp - ((msr >> 16) & 0x7F);
479 }
481 /* collect package counters only for 1st core in package */
482 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
483 retval = 0;
484 goto out;
485 }
487 if (do_pkg_cstate & (1 << 2))
488 READ_MSR(MSR_PKG_C2_RESIDENCY, &p->pc2);
489 if (do_pkg_cstate & (1 << 3))
490 READ_MSR(MSR_PKG_C3_RESIDENCY, &p->pc3);
491 if (do_pkg_cstate & (1 << 6))
492 READ_MSR(MSR_PKG_C6_RESIDENCY, &p->pc6);
493 if (do_pkg_cstate & (1 << 7))
494 READ_MSR(MSR_PKG_C7_RESIDENCY, &p->pc7);
495 if (do_pkg_cstate & (1 << 8))
496 READ_MSR(MSR_PKG_C8_RESIDENCY, &p->pc8);
497 if (do_pkg_cstate & (1 << 9))
498 READ_MSR(MSR_PKG_C9_RESIDENCY, &p->pc9);
499 if (do_pkg_cstate & (1 << 10))
500 READ_MSR(MSR_PKG_C10_RESIDENCY, &p->pc10);
502 if (do_rapl & RAPL_PKG) {
503 READ_MSR(MSR_PKG_ENERGY_STATUS, &msr);
504 p->energy_pkg = msr & 0xFFFFFFFF;
505 }
506 if (do_rapl & RAPL_CORES) {
507 READ_MSR(MSR_PP0_ENERGY_STATUS, &msr);
508 p->energy_cores = msr & 0xFFFFFFFF;
509 }
510 if (do_rapl & RAPL_DRAM) {
511 READ_MSR(MSR_DRAM_ENERGY_STATUS, &msr);
512 p->energy_dram = msr & 0xFFFFFFFF;
513 }
514 if (do_rapl & RAPL_GFX) {
515 READ_MSR(MSR_PP1_ENERGY_STATUS, &msr);
516 p->energy_gfx = msr & 0xFFFFFFFF;
517 }
518 if (do_rapl & RAPL_PKG_PERF_STATUS) {
519 READ_MSR(MSR_PKG_PERF_STATUS, &msr);
520 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
521 }
522 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
523 READ_MSR(MSR_DRAM_PERF_STATUS, &msr);
524 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
525 }
526 if (do_ptm) {
527 READ_MSR(MSR_IA32_PACKAGE_THERM_STATUS, &msr);
528 p->pkg_temp_c = p->tcc_activation_temp - ((msr >> 16) & 0x7F);
529 }
531 out:
532 close(msr_fd);
533 return retval;
534 }
536 static void
537 free_all_buffers(void)
538 {
539 allocated = 0;
540 initialized = 0;
542 CPU_FREE(cpu_present_set);
543 cpu_present_set = NULL;
544 cpu_present_set = 0;
546 CPU_FREE(cpu_affinity_set);
547 cpu_affinity_set = NULL;
548 cpu_affinity_setsize = 0;
550 CPU_FREE(cpu_saved_affinity_set);
551 cpu_saved_affinity_set = NULL;
552 cpu_saved_affinity_setsize = 0;
554 free(thread_even);
555 free(core_even);
556 free(package_even);
558 thread_even = NULL;
559 core_even = NULL;
560 package_even = NULL;
562 free(thread_odd);
563 free(core_odd);
564 free(package_odd);
566 thread_odd = NULL;
567 core_odd = NULL;
568 package_odd = NULL;
570 free(thread_delta);
571 free(core_delta);
572 free(package_delta);
574 thread_delta = NULL;
575 core_delta = NULL;
576 package_delta = NULL;
577 }
579 /*
580 * Parse a file containing a single int.
581 */
582 static int __attribute__ ((format(printf,1,2)))
583 parse_int_file(const char *fmt, ...)
584 {
585 va_list args;
586 char path[PATH_MAX];
587 FILE *filep;
588 int value;
590 va_start(args, fmt);
591 vsnprintf(path, sizeof(path), fmt, args);
592 va_end(args);
593 filep = fopen(path, "r");
594 if (!filep) {
595 ERROR("%s: open failed", path);
596 return -ERR_CANT_OPEN_FILE;
597 }
598 if (fscanf(filep, "%d", &value) != 1) {
599 ERROR("%s: failed to parse number from file", path);
600 return -ERR_CANT_READ_NUMBER;
601 }
602 fclose(filep);
603 return value;
604 }
606 /*
607 * cpu_is_first_sibling_in_core(cpu)
608 * return 1 if given CPU is 1st HT sibling in the core
609 */
610 static int
611 cpu_is_first_sibling_in_core(int cpu)
612 {
613 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
614 }
616 /*
617 * cpu_is_first_core_in_package(cpu)
618 * return 1 if given CPU is 1st core in package
619 */
620 static int
621 cpu_is_first_core_in_package(int cpu)
622 {
623 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
624 }
626 static int
627 get_physical_package_id(int cpu)
628 {
629 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
630 }
632 static int
633 get_core_id(int cpu)
634 {
635 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
636 }
638 static int
639 get_num_ht_siblings(int cpu)
640 {
641 char path[80];
642 FILE *filep;
643 int sib1, sib2;
644 int matches;
645 char character;
647 ssnprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
648 filep = fopen(path, "r");
649 if (!filep) {
650 ERROR("%s: open failed", path);
651 return -ERR_CANT_OPEN_FILE;
652 }
653 /*
654 * file format:
655 * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4)
656 * otherwinse 1 sibling (self).
657 */
658 matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2);
660 fclose(filep);
662 if (matches == 3)
663 return 2;
664 else
665 return 1;
666 }
668 static int __attribute__((warn_unused_result))
669 for_all_cpus_delta(const struct thread_data *thread_new_base, const struct core_data *core_new_base, const struct pkg_data *pkg_new_base,
670 const struct thread_data *thread_old_base, const struct core_data *core_old_base, const struct pkg_data *pkg_old_base)
671 {
672 int retval, pkg_no, core_no, thread_no;
674 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
675 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
676 for (thread_no = 0; thread_no <
677 topo.num_threads_per_core; ++thread_no) {
678 struct thread_data *t_delta;
679 const struct thread_data *t_old, *t_new;
680 struct core_data *c_delta;
681 const struct core_data *c_old, *c_new;
682 struct pkg_data *p_delta;
683 const struct pkg_data *p_old, *p_new;
685 t_delta = GET_THREAD(thread_delta, thread_no, core_no, pkg_no);
686 t_new = GET_THREAD(thread_new_base, thread_no, core_no, pkg_no);
687 t_old = GET_THREAD(thread_old_base, thread_no, core_no, pkg_no);
688 if (cpu_is_not_present(t_delta->cpu_id))
689 continue;
691 c_delta = GET_CORE(core_delta, core_no, pkg_no);
692 c_new = GET_CORE(core_new_base, core_no, pkg_no);
693 c_old = GET_CORE(core_old_base, core_no, pkg_no);
695 p_delta = GET_PKG(package_delta, pkg_no);
696 p_new = GET_PKG(pkg_new_base, pkg_no);
697 p_old = GET_PKG(pkg_old_base, pkg_no);
699 retval = delta_cpu(t_delta, c_delta, p_delta,
700 t_new, c_new, p_new,
701 t_old, c_old, p_old);
702 if (retval)
703 return retval;
704 }
705 }
706 }
707 return 0;
708 }
710 /*
711 * run func(cpu) on every cpu in /proc/stat
712 * return max_cpu number
713 */
714 static int __attribute__((warn_unused_result))
715 for_all_proc_cpus(int (func)(int))
716 {
717 FILE *fp;
718 int cpu_num;
719 int retval;
721 fp = fopen("/proc/stat", "r");
722 if (!fp) {
723 ERROR("Failed to open /proc/stat");
724 return -ERR_CANT_OPEN_FILE;
725 }
727 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
728 if (retval != 0) {
729 ERROR("Failed to parse /proc/stat");
730 return -ERR_CANT_READ_PROC_STAT;
731 }
733 while (1) {
734 retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
735 if (retval != 1)
736 break;
738 retval = func(cpu_num);
739 if (retval) {
740 fclose(fp);
741 return(retval);
742 }
743 }
744 fclose(fp);
745 return 0;
746 }
748 /*
749 * count_cpus()
750 * remember the last one seen, it will be the max
751 */
752 static int
753 count_cpus(int cpu)
754 {
755 if (topo.max_cpu_num < cpu)
756 topo.max_cpu_num = cpu;
758 topo.num_cpus += 1;
759 return 0;
760 }
761 static int
762 mark_cpu_present(int cpu)
763 {
764 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
765 return 0;
766 }
769 static void
770 turbostat_submit (const char *plugin_instance,
771 const char *type, const char *type_instance,
772 gauge_t value)
773 {
774 value_list_t vl = VALUE_LIST_INIT;
775 value_t v;
777 v.gauge = value;
778 vl.values = &v;
779 vl.values_len = 1;
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);
789 }
791 /*
792 * column formatting convention & formats
793 * package: "pk" 2 columns %2d
794 * core: "cor" 3 columns %3d
795 * CPU: "CPU" 3 columns %3d
796 * Pkg_W: %6.2
797 * Cor_W: %6.2
798 * GFX_W: %5.2
799 * RAM_W: %5.2
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
806 */
807 #define NAME_LEN 12
808 static int
809 submit_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
810 {
811 char name[NAME_LEN];
812 double interval_float;
814 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
816 ssnprintf(name, sizeof(name), "cpu%02d", t->cpu_id);
818 if (!aperf_mperf_unstable)
819 turbostat_submit(name, "percent", "c0", 100.0 * t->mperf/t->tsc);
820 if (!aperf_mperf_unstable)
821 turbostat_submit(name, "percent", "c1", 100.0 * t->c1/t->tsc);
823 /* GHz */
824 if ((!aperf_mperf_unstable) || (!(t->aperf > t->tsc || t->mperf > t->tsc)))
825 turbostat_submit(NULL, "frequency", name, 1.0 * t->tsc / 1000000000 * t->aperf / t->mperf / interval_float);
827 /* SMI */
828 turbostat_submit(NULL, "current", name, t->smi_count);
830 /* print per-core data only for 1st thread in core */
831 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
832 goto done;
834 ssnprintf(name, sizeof(name), "core%02d", c->core_id);
836 if (do_core_cstate & (1 << 3))
837 turbostat_submit(name, "percent", "c3", 100.0 * c->c3/t->tsc);
838 if (do_core_cstate & (1 << 6))
839 turbostat_submit(name, "percent", "c6", 100.0 * c->c6/t->tsc);
840 if (do_core_cstate & (1 << 7))
841 turbostat_submit(name, "percent", "c7", 100.0 * c->c7/t->tsc);
843 if (do_dts)
844 turbostat_submit(NULL, "temperature", name, c->core_temp_c);
846 /* print per-package data only for 1st core in package */
847 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
848 goto done;
850 ssnprintf(name, sizeof(name), "pkg%02d", p->package_id);
852 if (do_ptm)
853 turbostat_submit(NULL, "temperature", name, p->pkg_temp_c);
855 if (do_pkg_cstate & (1 << 2))
856 turbostat_submit(name, "percent", "pc2", 100.0 * p->pc2/t->tsc);
857 if (do_pkg_cstate & (1 << 3))
858 turbostat_submit(name, "percent", "pc3", 100.0 * p->pc3/t->tsc);
859 if (do_pkg_cstate & (1 << 6))
860 turbostat_submit(name, "percent", "pc6", 100.0 * p->pc6/t->tsc);
861 if (do_pkg_cstate & (1 << 7))
862 turbostat_submit(name, "percent", "pc7", 100.0 * p->pc7/t->tsc);
863 if (do_pkg_cstate & (1 << 8))
864 turbostat_submit(name, "percent", "pc8", 100.0 * p->pc8/t->tsc);
865 if (do_pkg_cstate & (1 << 9))
866 turbostat_submit(name, "percent", "pc9", 100.0 * p->pc9/t->tsc);
867 if (do_pkg_cstate & (1 << 10))
868 turbostat_submit(name, "percent", "pc10", 100.0 * p->pc10/t->tsc);
870 if (do_rapl) {
871 if (do_rapl & RAPL_PKG)
872 turbostat_submit(name, "power", "Pkg_W", p->energy_pkg * rapl_energy_units / interval_float);
873 if (do_rapl & RAPL_CORES)
874 turbostat_submit(name, "power", "Cor_W", p->energy_cores * rapl_energy_units / interval_float);
875 if (do_rapl & RAPL_GFX)
876 turbostat_submit(name, "power", "GFX_W", p->energy_gfx * rapl_energy_units / interval_float);
877 if (do_rapl & RAPL_DRAM)
878 turbostat_submit(name, "power", "RAM_W", p->energy_dram * rapl_energy_units / interval_float);
879 }
880 done:
881 return 0;
882 }
884 static int
885 turbostat_read(user_data_t * not_used)
886 {
887 int ret;
889 if (!allocated) {
890 if ((ret = setup_all_buffers()) < 0)
891 return ret;
892 }
894 if (for_all_proc_cpus(cpu_is_not_present)) {
895 free_all_buffers();
896 if ((ret = setup_all_buffers()) < 0)
897 return ret;
898 if (for_all_proc_cpus(cpu_is_not_present))
899 return -ERR_CPU_NOT_PRESENT;
900 }
902 /* Saving the scheduling affinity, as it will be modified by get_counters */
903 if (sched_getaffinity(0, cpu_saved_affinity_setsize, cpu_saved_affinity_set) != 0)
904 return -ERR_CPU_SAVE_SCHED_AFFINITY;
906 if (!initialized) {
907 if ((ret = for_all_cpus(get_counters, EVEN_COUNTERS)) < 0)
908 goto out;
909 gettimeofday(&tv_even, (struct timezone *)NULL);
910 is_even = 1;
911 initialized = 1;
912 ret = 0;
913 goto out;
914 }
916 if (is_even) {
917 if ((ret = for_all_cpus(get_counters, ODD_COUNTERS)) < 0)
918 goto out;
919 gettimeofday(&tv_odd, (struct timezone *)NULL);
920 is_even = 0;
921 timersub(&tv_odd, &tv_even, &tv_delta);
922 if ((ret = for_all_cpus_delta(ODD_COUNTERS, EVEN_COUNTERS)) < 0)
923 goto out;
924 if ((ret = for_all_cpus(submit_counters, DELTA_COUNTERS)) < 0)
925 goto out;
926 } else {
927 if ((ret = for_all_cpus(get_counters, EVEN_COUNTERS)) < 0)
928 goto out;
929 gettimeofday(&tv_even, (struct timezone *)NULL);
930 is_even = 1;
931 timersub(&tv_even, &tv_odd, &tv_delta);
932 if ((ret = for_all_cpus_delta(EVEN_COUNTERS, ODD_COUNTERS)) < 0)
933 goto out;
934 if ((ret = for_all_cpus(submit_counters, DELTA_COUNTERS)) < 0)
935 goto out;
936 }
937 ret = 0;
938 out:
939 /*
940 * Let's restore the affinity
941 * This might fail if the number of CPU changed, but we can't do anything in that case..
942 */
943 (void)sched_setaffinity(0, cpu_saved_affinity_setsize, cpu_saved_affinity_set);
944 return ret;
945 }
947 static int __attribute__((warn_unused_result))
948 check_dev_msr()
949 {
950 struct stat sb;
952 if (stat("/dev/cpu/0/msr", &sb)) {
953 ERROR("no /dev/cpu/0/msr, try \"# modprobe msr\"");
954 return -ERR_NO_MSR;
955 }
956 return 0;
957 }
959 static int __attribute__((warn_unused_result))
960 check_super_user()
961 {
962 if (getuid() != 0) {
963 ERROR("must be root");
964 return -ERR_NOT_ROOT;
965 }
966 return 0;
967 }
969 /*
970 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
971 * the Thermal Control Circuit (TCC) activates.
972 * This is usually equal to tjMax.
973 *
974 * Older processors do not have this MSR, so there we guess,
975 * but also allow cmdline over-ride with -T.
976 *
977 * Several MSR temperature values are in units of degrees-C
978 * below this value, including the Digital Thermal Sensor (DTS),
979 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
980 */
981 static int __attribute__((warn_unused_result))
982 set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
983 {
984 unsigned long long msr;
985 unsigned int target_c_local;
987 /* tcc_activation_temp is used only for dts or ptm */
988 if (!(do_dts || do_ptm))
989 return 0;
991 /* this is a per-package concept */
992 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
993 return 0;
995 if (tcc_activation_temp != 0) {
996 p->tcc_activation_temp = tcc_activation_temp;
997 return 0;
998 }
1000 if (get_msr(t->cpu_id, MSR_IA32_TEMPERATURE_TARGET, &msr))
1001 goto guess;
1003 target_c_local = (msr >> 16) & 0x7F;
1005 if (target_c_local < 85 || target_c_local > 127)
1006 goto guess;
1008 p->tcc_activation_temp = target_c_local;
1010 return 0;
1012 guess:
1013 p->tcc_activation_temp = TJMAX_DEFAULT;
1014 WARNING("cpu%d: Guessing tjMax %d C, Please use TCCActivationTemp to specify",
1015 t->cpu_id, p->tcc_activation_temp);
1017 return 0;
1018 }
1020 /*
1021 * Identify the functionality of the CPU
1022 */
1023 static int __attribute__((warn_unused_result))
1024 probe_cpu()
1025 {
1026 unsigned int eax, ebx, ecx, edx, max_level;
1027 unsigned int fms, family, model;
1029 /* CPUID(0):
1030 * - EAX: Maximum Input Value for Basic CPUID Information
1031 * - EBX: "Genu" (0x756e6547)
1032 * - EDX: "ineI" (0x49656e69)
1033 * - ECX: "ntel" (0x6c65746e)
1034 */
1035 max_level = ebx = ecx = edx = 0;
1036 __get_cpuid(0, &max_level, &ebx, &ecx, &edx);
1037 if (ebx != 0x756e6547 && edx != 0x49656e69 && ecx != 0x6c65746e) {
1038 ERROR("Unsupported CPU");
1039 return -UNSUPPORTED_CPU;
1040 }
1042 /* CPUID(1):
1043 * - EAX: Version Information: Type, Family, Model, and Stepping ID
1044 * + 4-7: Model ID
1045 * + 8-11: Family ID
1046 * + 12-13: Processor type
1047 * + 16-19: Extended Model ID
1048 * + 20-27: Extended Family ID
1049 * - EDX: Feature Information:
1050 * + 5: Support for MSR read/write operations
1051 */
1052 fms = ebx = ecx = edx = 0;
1053 __get_cpuid(1, &fms, &ebx, &ecx, &edx);
1054 family = (fms >> 8) & 0xf;
1055 model = (fms >> 4) & 0xf;
1056 if (family == 0xf)
1057 family += (fms >> 20) & 0xf;
1058 if (family == 6 || family == 0xf)
1059 model += ((fms >> 16) & 0xf) << 4;
1060 if (!(edx & (1 << 5))) {
1061 ERROR("CPUID: no MSR");
1062 return -ERR_NO_MSR;
1063 }
1065 /*
1066 * CPUID(0x80000000):
1067 * - EAX: Maximum Input Value for Extended Function CPUID Information
1068 *
1069 * This allows us to verify if the CPUID(0x80000007) can be called
1070 *
1071 * This check is valid for both Intel and AMD.
1072 */
1073 max_level = ebx = ecx = edx = 0;
1074 __get_cpuid(0x80000000, &max_level, &ebx, &ecx, &edx);
1075 if (max_level < 0x80000007) {
1076 ERROR("CPUID: no invariant TSC (max_level 0x%x)", max_level);
1077 return -ERR_NO_INVARIANT_TSC;
1078 }
1080 /*
1081 * CPUID(0x80000007):
1082 * - EDX:
1083 * + 8: Invariant TSC available if set
1084 *
1085 * This check is valid for both Intel and AMD
1086 */
1087 __get_cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
1088 if (!(edx & (1 << 8))) {
1089 ERROR("No invariant TSC");
1090 return -ERR_NO_INVARIANT_TSC;
1091 }
1093 /*
1094 * CPUID(6):
1095 * - EAX:
1096 * + 0: Digital temperature sensor is supported if set
1097 * + 6: Package thermal management is supported if set
1098 * - ECX:
1099 * + 0: Hardware Coordination Feedback Capability (Presence of IA32_MPERF and IA32_APERF).
1100 * + 3: The processor supports performance-energy bias preference if set.
1101 * It also implies the presence of a new architectural MSR called IA32_ENERGY_PERF_BIAS
1102 *
1103 * This check is valid for both Intel and AMD
1104 */
1105 __get_cpuid(0x6, &eax, &ebx, &ecx, &edx);
1106 do_dts = eax & (1 << 0);
1107 do_ptm = eax & (1 << 6);
1108 if (!(ecx & (1 << 0))) {
1109 ERROR("No APERF");
1110 return -ERR_NO_APERF;
1111 }
1113 /*
1114 * Enable or disable C states depending on the model and family
1115 */
1116 if (family == 6) {
1117 switch (model) {
1118 /* Atom (partial) */
1119 case 0x27:
1120 do_core_cstate = 0;
1121 do_pkg_cstate = (1 << 2) | (1 << 4) | (1 << 6);
1122 break;
1123 /* Silvermont */
1124 case 0x37: /* BYT */
1125 case 0x4A:
1126 case 0x4D: /* AVN */
1127 case 0x5A:
1128 case 0x5D:
1129 do_core_cstate = (1 << 1) | (1 << 6);
1130 do_pkg_cstate = (1 << 6);
1131 break;
1132 /* Nehalem */
1133 case 0x1A: /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
1134 case 0x1E: /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
1135 case 0x1F: /* Core i7 and i5 Processor - Nehalem */
1136 case 0x2E: /* Nehalem-EX Xeon - Beckton */
1137 do_core_cstate = (1 << 3) | (1 << 6);
1138 do_pkg_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1139 break;
1140 /* Westmere */
1141 case 0x25: /* Westmere Client - Clarkdale, Arrandale */
1142 case 0x2C: /* Westmere EP - Gulftown */
1143 case 0x2F: /* Westmere-EX Xeon - Eagleton */
1144 do_core_cstate = (1 << 3) | (1 << 6);
1145 do_pkg_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1146 break;
1147 /* Sandy Bridge */
1148 case 0x2A: /* SNB */
1149 case 0x2D: /* SNB Xeon */
1150 do_core_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1151 do_pkg_cstate = (1 << 2) | (1 << 3) | (1 << 6) | (1 << 7);
1152 break;
1153 /* Ivy Bridge */
1154 case 0x3A: /* IVB */
1155 case 0x3E: /* IVB Xeon */
1156 do_core_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1157 do_pkg_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1158 break;
1159 /* Haswell Bridge */
1160 case 0x3C: /* HSW */
1161 case 0x3F: /* HSW */
1162 case 0x46: /* HSW */
1163 do_core_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1164 do_pkg_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1165 break;
1166 case 0x45: /* HSW */
1167 do_core_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1168 do_pkg_cstate = (1 << 3) | (1 << 6) | (1 << 7) | (1 << 8) | (1 << 9) | (1 << 10);
1169 break;
1170 /* Broadwel */
1171 case 0x4F: /* BDW */
1172 case 0x56: /* BDX-DE */
1173 do_core_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1174 do_pkg_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1175 break;
1176 case 0x3D: /* BDW */
1177 do_core_cstate = (1 << 3) | (1 << 6) | (1 << 7);
1178 do_pkg_cstate = (1 << 3) | (1 << 6) | (1 << 7) | (1 << 8) | (1 << 9) | (1 << 10);
1179 break;
1180 default:
1181 ERROR("Unsupported CPU");
1182 }
1183 switch (model) {
1184 case 0x2A:
1185 case 0x3A:
1186 case 0x3C:
1187 case 0x45:
1188 case 0x46:
1189 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_PKG_POWER_INFO | RAPL_GFX;
1190 break;
1191 case 0x3F:
1192 do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO | RAPL_PKG_PERF_STATUS | RAPL_DRAM | RAPL_DRAM_PERF_STATUS;
1193 break;
1194 case 0x2D:
1195 case 0x3E:
1196 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_PKG_POWER_INFO | RAPL_PKG_PERF_STATUS | RAPL_DRAM | RAPL_DRAM_PERF_STATUS;
1197 break;
1198 case 0x37:
1199 case 0x4D:
1200 do_rapl = RAPL_PKG | RAPL_CORES;
1201 break;
1202 default:
1203 do_rapl = 0;
1204 }
1205 } else {
1206 ERROR("Unsupported CPU");
1207 return -UNSUPPORTED_CPU;
1208 }
1210 if (do_rapl) {
1211 unsigned long msr;
1212 if (get_msr(0, MSR_RAPL_POWER_UNIT, &msr))
1213 return 0;
1215 if (model == 0x37)
1216 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
1217 else
1218 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
1219 }
1221 return 0;
1222 }
1226 static int __attribute__((warn_unused_result))
1227 topology_probe()
1228 {
1229 int i;
1230 int ret;
1231 int max_core_id = 0;
1232 int max_package_id = 0;
1233 int max_siblings = 0;
1234 struct cpu_topology {
1235 int core_id;
1236 int physical_package_id;
1237 } *cpus;
1239 /* Initialize num_cpus, max_cpu_num */
1240 topo.num_cpus = 0;
1241 topo.max_cpu_num = 0;
1242 ret = for_all_proc_cpus(count_cpus);
1243 if (ret < 0)
1244 return ret;
1246 DEBUG("num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
1248 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
1249 if (cpus == NULL) {
1250 ERROR("calloc cpus");
1251 return -ERR_CALLOC;
1252 }
1254 /*
1255 * Allocate and initialize cpu_present_set
1256 */
1257 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
1258 if (cpu_present_set == NULL) {
1259 free(cpus);
1260 ERROR("CPU_ALLOC");
1261 return -ERR_CPU_ALLOC;
1262 }
1263 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
1264 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
1265 ret = for_all_proc_cpus(mark_cpu_present);
1266 if (ret < 0) {
1267 free(cpus);
1268 return ret;
1269 }
1271 /*
1272 * Allocate and initialize cpu_affinity_set
1273 */
1274 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
1275 if (cpu_affinity_set == NULL) {
1276 free(cpus);
1277 ERROR("CPU_ALLOC");
1278 return -ERR_CPU_ALLOC;
1279 }
1280 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
1281 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
1284 /*
1285 * Allocate and initialize cpu_saved_affinity_set
1286 */
1287 cpu_saved_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
1288 if (cpu_saved_affinity_set == NULL) {
1289 free(cpus);
1290 ERROR("CPU_ALLOC");
1291 return -ERR_CPU_ALLOC;
1292 }
1293 cpu_saved_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
1294 CPU_ZERO_S(cpu_saved_affinity_setsize, cpu_saved_affinity_set);
1297 /*
1298 * For online cpus
1299 * find max_core_id, max_package_id
1300 */
1301 for (i = 0; i <= topo.max_cpu_num; ++i) {
1302 int siblings;
1304 if (cpu_is_not_present(i)) {
1305 WARNING("cpu%d NOT PRESENT", i);
1306 continue;
1307 }
1308 cpus[i].core_id = get_core_id(i);
1309 if (cpus[i].core_id < 0)
1310 return cpus[i].core_id;
1311 if (cpus[i].core_id > max_core_id)
1312 max_core_id = cpus[i].core_id;
1314 cpus[i].physical_package_id = get_physical_package_id(i);
1315 if (cpus[i].physical_package_id < 0)
1316 return cpus[i].physical_package_id;
1317 if (cpus[i].physical_package_id > max_package_id)
1318 max_package_id = cpus[i].physical_package_id;
1320 siblings = get_num_ht_siblings(i);
1321 if (siblings < 0)
1322 return siblings;
1323 if (siblings > max_siblings)
1324 max_siblings = siblings;
1325 DEBUG("cpu %d pkg %d core %d\n",
1326 i, cpus[i].physical_package_id, cpus[i].core_id);
1327 }
1328 topo.num_cores_per_pkg = max_core_id + 1;
1329 DEBUG("max_core_id %d, sizing for %d cores per package\n",
1330 max_core_id, topo.num_cores_per_pkg);
1332 topo.num_packages = max_package_id + 1;
1333 DEBUG("max_package_id %d, sizing for %d packages\n",
1334 max_package_id, topo.num_packages);
1336 topo.num_threads_per_core = max_siblings;
1337 DEBUG("max_siblings %d\n", max_siblings);
1339 free(cpus);
1340 return 0;
1341 }
1343 static int
1344 allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
1345 {
1346 int i;
1348 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
1349 topo.num_packages, sizeof(struct thread_data));
1350 if (*t == NULL)
1351 goto error;
1353 for (i = 0; i < topo.num_threads_per_core *
1354 topo.num_cores_per_pkg * topo.num_packages; i++)
1355 (*t)[i].cpu_id = -1;
1357 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
1358 sizeof(struct core_data));
1359 if (*c == NULL)
1360 goto error;
1362 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
1363 (*c)[i].core_id = -1;
1365 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
1366 if (*p == NULL)
1367 goto error;
1369 for (i = 0; i < topo.num_packages; i++)
1370 (*p)[i].package_id = i;
1372 return 0;
1373 error:
1374 ERROR("calloc counters");
1375 return -ERR_CALLOC;
1376 }
1377 /*
1378 * init_counter()
1379 *
1380 * set cpu_id, core_num, pkg_num
1381 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
1382 *
1383 * increment topo.num_cores when 1st core in pkg seen
1384 */
1385 static int
1386 init_counter(struct thread_data *thread_base, struct core_data *core_base,
1387 struct pkg_data *pkg_base, int thread_num, int core_num,
1388 int pkg_num, int cpu_id)
1389 {
1390 int ret;
1391 struct thread_data *t;
1392 struct core_data *c;
1393 struct pkg_data *p;
1395 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
1396 c = GET_CORE(core_base, core_num, pkg_num);
1397 p = GET_PKG(pkg_base, pkg_num);
1399 t->cpu_id = cpu_id;
1400 if (thread_num == 0) {
1401 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
1402 if ((ret = cpu_is_first_core_in_package(cpu_id)) < 0) {
1403 return ret;
1404 } else if (ret != 0) {
1405 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
1406 }
1407 }
1409 c->core_id = core_num;
1410 p->package_id = pkg_num;
1412 return 0;
1413 }
1416 static int
1417 initialize_counters(int cpu_id)
1418 {
1419 int my_thread_id, my_core_id, my_package_id;
1420 int ret;
1422 my_package_id = get_physical_package_id(cpu_id);
1423 if (my_package_id < 0)
1424 return my_package_id;
1425 my_core_id = get_core_id(cpu_id);
1426 if (my_core_id < 0)
1427 return my_core_id;
1429 if ((ret = cpu_is_first_sibling_in_core(cpu_id)) < 0) {
1430 return ret;
1431 } else if (ret != 0) {
1432 my_thread_id = 0;
1433 topo.num_cores++;
1434 } else {
1435 my_thread_id = 1;
1436 }
1438 ret = init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
1439 if (ret < 0)
1440 return ret;
1441 ret = init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
1442 if (ret < 0)
1443 return ret;
1444 ret = init_counter(DELTA_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
1445 if (ret < 0)
1446 return ret;
1447 return 0;
1448 }
1450 #define DO_OR_GOTO_ERR(something) \
1451 do { \
1452 ret = (something); \
1453 if (ret < 0) \
1454 goto err; \
1455 } while (0)
1457 static int setup_all_buffers(void)
1458 {
1459 int ret;
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(allocate_counters(&thread_delta, &core_delta, &package_delta));
1465 DO_OR_GOTO_ERR(for_all_proc_cpus(initialize_counters));
1467 allocated = 1;
1468 return 0;
1469 err:
1470 free_all_buffers();
1471 return ret;
1472 }
1474 static int
1475 turbostat_init(void)
1476 {
1477 int ret;
1479 DO_OR_GOTO_ERR(check_super_user());
1480 DO_OR_GOTO_ERR(probe_cpu());
1481 DO_OR_GOTO_ERR(check_dev_msr());
1482 DO_OR_GOTO_ERR(setup_all_buffers());
1483 DO_OR_GOTO_ERR(for_all_cpus(set_temperature_target, EVEN_COUNTERS));
1484 DO_OR_GOTO_ERR(for_all_cpus(set_temperature_target, ODD_COUNTERS));
1486 plugin_register_complex_read(NULL, PLUGIN_NAME, turbostat_read, NULL, NULL);
1488 return 0;
1489 err:
1490 free_all_buffers();
1491 return ret;
1492 }
1494 static const char *config_keys[] =
1495 {
1496 "TCCActivationTemp",
1497 };
1498 static const int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
1500 static int
1501 turbostat_config(const char *key, const char *value)
1502 {
1503 long unsigned int tmp_val;
1504 char *end;
1506 if (strcasecmp("TCCActivationTemp", key) == 0) {
1507 tmp_val = strtoul(value, &end, 0);
1508 if (*end != '\0' || tmp_val > UINT_MAX)
1509 return -1;
1510 tcc_activation_temp = (unsigned int) tmp_val;
1511 } else {
1512 return -1;
1513 }
1514 return 0;
1515 }
1517 void module_register(void);
1518 void module_register(void)
1519 {
1520 plugin_register_init(PLUGIN_NAME, turbostat_init);
1521 plugin_register_config(PLUGIN_NAME, turbostat_config, config_keys, config_keys_num);
1522 }