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 #define _GNU_SOURCE
28 #include <asm/msr-index.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <err.h>
32 #include <unistd.h>
33 #include <sys/types.h>
34 #include <sys/wait.h>
35 #include <sys/stat.h>
36 #include <sys/resource.h>
37 #include <fcntl.h>
38 #include <signal.h>
39 #include <sys/time.h>
40 #include <stdlib.h>
41 #include <dirent.h>
42 #include <string.h>
43 #include <ctype.h>
44 #include <sched.h>
45 #include <cpuid.h>
47 #include "collectd.h"
48 #include "common.h"
49 #include "plugin.h"
51 #define PLUGIN_NAME "turbostat"
53 static const char *proc_stat = "/proc/stat";
54 static unsigned int interval_sec = 5; /* set with -i interval_sec */
55 static unsigned int skip_c0;
56 static unsigned int skip_c1;
57 static unsigned int do_nhm_cstates;
58 static unsigned int do_snb_cstates;
59 static unsigned int do_c8_c9_c10;
60 static unsigned int do_slm_cstates;
61 static unsigned int has_aperf;
62 static unsigned int has_epb;
63 static unsigned int units = 1000000000; /* Ghz etc */
64 static unsigned int genuine_intel;
65 static unsigned int has_invariant_tsc;
66 static unsigned int do_nehalem_platform_info;
67 static int do_smi;
68 static unsigned int show_pkg;
69 static unsigned int show_core;
70 static unsigned int show_cpu;
71 static unsigned int do_rapl;
72 static unsigned int do_dts;
73 static unsigned int do_ptm;
74 static unsigned int tcc_activation_temp;
75 static unsigned int tcc_activation_temp_override;
76 static double rapl_power_units, rapl_energy_units, rapl_time_units;
77 static double rapl_joule_counter_range;
79 #define RAPL_PKG (1 << 0)
80 /* 0x610 MSR_PKG_POWER_LIMIT */
81 /* 0x611 MSR_PKG_ENERGY_STATUS */
82 #define RAPL_PKG_PERF_STATUS (1 << 1)
83 /* 0x613 MSR_PKG_PERF_STATUS */
84 #define RAPL_PKG_POWER_INFO (1 << 2)
85 /* 0x614 MSR_PKG_POWER_INFO */
87 #define RAPL_DRAM (1 << 3)
88 /* 0x618 MSR_DRAM_POWER_LIMIT */
89 /* 0x619 MSR_DRAM_ENERGY_STATUS */
90 /* 0x61c MSR_DRAM_POWER_INFO */
91 #define RAPL_DRAM_PERF_STATUS (1 << 4)
92 /* 0x61b MSR_DRAM_PERF_STATUS */
94 #define RAPL_CORES (1 << 5)
95 /* 0x638 MSR_PP0_POWER_LIMIT */
96 /* 0x639 MSR_PP0_ENERGY_STATUS */
97 #define RAPL_CORE_POLICY (1 << 6)
98 /* 0x63a MSR_PP0_POLICY */
101 #define RAPL_GFX (1 << 7)
102 /* 0x640 MSR_PP1_POWER_LIMIT */
103 /* 0x641 MSR_PP1_ENERGY_STATUS */
104 /* 0x642 MSR_PP1_POLICY */
105 #define TJMAX_DEFAULT 100
107 int aperf_mperf_unstable;
108 int backwards_count;
109 char *progname;
111 cpu_set_t *cpu_present_set, *cpu_affinity_set;
112 size_t cpu_present_setsize, cpu_affinity_setsize;
114 struct thread_data {
115 unsigned long long tsc;
116 unsigned long long aperf;
117 unsigned long long mperf;
118 unsigned long long c1;
119 unsigned int smi_count;
120 unsigned int cpu_id;
121 unsigned int flags;
122 #define CPU_IS_FIRST_THREAD_IN_CORE 0x2
123 #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
124 } *thread_even, *thread_odd;
126 struct core_data {
127 unsigned long long c3;
128 unsigned long long c6;
129 unsigned long long c7;
130 unsigned int core_temp_c;
131 unsigned int core_id;
132 } *core_even, *core_odd;
134 struct pkg_data {
135 unsigned long long pc2;
136 unsigned long long pc3;
137 unsigned long long pc6;
138 unsigned long long pc7;
139 unsigned long long pc8;
140 unsigned long long pc9;
141 unsigned long long pc10;
142 unsigned int package_id;
143 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
144 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
145 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
146 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
147 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
148 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
149 unsigned int pkg_temp_c;
151 } *package_even, *package_odd;
153 #define ODD_COUNTERS thread_odd, core_odd, package_odd
154 #define EVEN_COUNTERS thread_even, core_even, package_even
155 static _Bool is_even = 1;
157 static _Bool allocated = 0;
158 static _Bool initialized = 0;
160 #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
161 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
162 topo.num_threads_per_core + \
163 (core_no) * topo.num_threads_per_core + (thread_no))
164 #define GET_CORE(core_base, core_no, pkg_no) \
165 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
166 #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
168 struct topo_params {
169 int num_packages;
170 int num_cpus;
171 int num_cores;
172 int max_cpu_num;
173 int num_cores_per_pkg;
174 int num_threads_per_core;
175 } topo;
177 struct timeval tv_even, tv_odd, tv_delta;
179 enum return_values {
180 OK = 0,
181 ERR_CPU_MIGRATE,
182 ERR_MSR_IA32_APERF,
183 ERR_MSR_IA32_MPERF,
184 ERR_MSR_SMI_COUNT,
185 ERR_MSR_CORE_C3_RESIDENCY,
186 ERR_MSR_CORE_C6_RESIDENCY,
187 ERR_MSR_CORE_C7_RESIDENCY,
188 ERR_MSR_IA32_THERM_STATUS,
189 ERR_MSR_PKG_C3_RESIDENCY,
190 ERR_MSR_PKG_C6_RESIDENCY,
191 ERR_MSR_PKG_C2_RESIDENCY,
192 ERR_MSR_PKG_C7_RESIDENCY,
193 ERR_MSR_PKG_C8_RESIDENCY,
194 ERR_MSR_PKG_C9_RESIDENCY,
195 ERR_MSR_PKG_C10_RESIDENCY,
196 ERR_MSR_PKG_ENERGY_STATUS,
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_CPU_NOT_PRESENT,
204 ERR_NO_MSR,
205 ERR_CANT_OPEN_FILE,
206 ERR_CANT_READ_NUMBER,
207 ERR_CANT_READ_PROC_STAT,
208 ERR_NO_INVARIANT_TSC,
209 ERR_NO_APERF,
210 ERR_CALLOC,
211 ERR_CPU_ALLOC,
212 ERR_NOT_ROOT,
213 };
215 #define __must_check __attribute__((warn_unused_result))
217 static int setup_all_buffers(void);
219 static int
220 cpu_is_not_present(int cpu)
221 {
222 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
223 }
224 /*
225 * run func(thread, core, package) in topology order
226 * skip non-present cpus
227 */
229 static int __must_check
230 for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
231 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
232 {
233 int retval, pkg_no, core_no, thread_no;
235 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
236 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
237 for (thread_no = 0; thread_no <
238 topo.num_threads_per_core; ++thread_no) {
239 struct thread_data *t;
240 struct core_data *c;
241 struct pkg_data *p;
243 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
245 if (cpu_is_not_present(t->cpu_id))
246 continue;
248 c = GET_CORE(core_base, core_no, pkg_no);
249 p = GET_PKG(pkg_base, pkg_no);
251 retval = func(t, c, p);
252 if (retval)
253 return retval;
254 }
255 }
256 }
257 return 0;
258 }
260 static int __must_check
261 cpu_migrate(int cpu)
262 {
263 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
264 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
265 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
266 return -ERR_CPU_MIGRATE;
267 else
268 return 0;
269 }
271 static int __must_check
272 get_msr(int cpu, off_t offset, unsigned long long *msr)
273 {
274 ssize_t retval;
275 char pathname[32];
276 int fd;
278 sprintf(pathname, "/dev/cpu/%d/msr", cpu);
279 fd = open(pathname, O_RDONLY);
280 if (fd < 0)
281 return -1;
283 retval = pread(fd, msr, sizeof *msr, offset);
284 close(fd);
286 if (retval != sizeof *msr) {
287 ERROR ("%s offset 0x%llx read failed\n", pathname, (unsigned long long)offset);
288 return -1;
289 }
291 return 0;
292 }
294 #define DELTA_WRAP32(new, old) \
295 if (new > old) { \
296 old = new - old; \
297 } else { \
298 old = 0x100000000 + new - old; \
299 }
301 static void
302 delta_package(struct pkg_data *new, struct pkg_data *old)
303 {
304 old->pc2 = new->pc2 - old->pc2;
305 old->pc3 = new->pc3 - old->pc3;
306 old->pc6 = new->pc6 - old->pc6;
307 old->pc7 = new->pc7 - old->pc7;
308 old->pc8 = new->pc8 - old->pc8;
309 old->pc9 = new->pc9 - old->pc9;
310 old->pc10 = new->pc10 - old->pc10;
311 old->pkg_temp_c = new->pkg_temp_c;
313 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
314 DELTA_WRAP32(new->energy_cores, old->energy_cores);
315 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
316 DELTA_WRAP32(new->energy_dram, old->energy_dram);
317 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
318 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
319 }
321 static void
322 delta_core(struct core_data *new, struct core_data *old)
323 {
324 old->c3 = new->c3 - old->c3;
325 old->c6 = new->c6 - old->c6;
326 old->c7 = new->c7 - old->c7;
327 old->core_temp_c = new->core_temp_c;
328 }
330 /*
331 * old = new - old
332 */
333 static int __must_check
334 delta_thread(struct thread_data *new, struct thread_data *old,
335 struct core_data *core_delta)
336 {
337 old->tsc = new->tsc - old->tsc;
339 /* check for TSC < 1 Mcycles over interval */
340 if (old->tsc < (1000 * 1000)) {
341 WARNING("Insanely slow TSC rate, TSC stops in idle?\n"
342 "You can disable all c-states by booting with \"idle=poll\"\n"
343 "or just the deep ones with \"processor.max_cstate=1\"");
344 return -1;
345 }
347 old->c1 = new->c1 - old->c1;
349 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
350 old->aperf = new->aperf - old->aperf;
351 old->mperf = new->mperf - old->mperf;
352 } else {
354 if (!aperf_mperf_unstable) {
355 WARNING("%s: APERF or MPERF went backwards *\n", progname);
356 WARNING("* Frequency results do not cover entire interval *\n");
357 WARNING("* fix this by running Linux-2.6.30 or later *\n");
359 aperf_mperf_unstable = 1;
360 }
361 /*
362 * mperf delta is likely a huge "positive" number
363 * can not use it for calculating c0 time
364 */
365 skip_c0 = 1;
366 skip_c1 = 1;
367 }
370 /*
371 * As counter collection is not atomic,
372 * it is possible for mperf's non-halted cycles + idle states
373 * to exceed TSC's all cycles: show c1 = 0% in that case.
374 */
375 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
376 old->c1 = 0;
377 else {
378 /* normal case, derive c1 */
379 old->c1 = old->tsc - old->mperf - core_delta->c3
380 - core_delta->c6 - core_delta->c7;
381 }
383 if (old->mperf == 0) {
384 WARNING("cpu%d MPERF 0!\n", old->cpu_id);
385 old->mperf = 1; /* divide by 0 protection */
386 }
388 if (do_smi)
389 old->smi_count = new->smi_count - old->smi_count;
391 return 0;
392 }
394 static int __must_check
395 delta_cpu(struct thread_data *t, struct core_data *c,
396 struct pkg_data *p, struct thread_data *t2,
397 struct core_data *c2, struct pkg_data *p2)
398 {
399 int ret;
401 /* calculate core delta only for 1st thread in core */
402 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
403 delta_core(c, c2);
405 /* always calculate thread delta */
406 ret = delta_thread(t, t2, c2); /* c2 is core delta */
407 if (ret != 0)
408 return ret;
410 /* calculate package delta only for 1st core in package */
411 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
412 delta_package(p, p2);
414 return 0;
415 }
417 static unsigned long long
418 rdtsc(void)
419 {
420 unsigned int low, high;
422 asm volatile("rdtsc" : "=a" (low), "=d" (high));
424 return low | ((unsigned long long)high) << 32;
425 }
428 /*
429 * get_counters(...)
430 * migrate to cpu
431 * acquire and record local counters for that cpu
432 */
433 static int __must_check
434 get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
435 {
436 int cpu = t->cpu_id;
437 unsigned long long msr;
439 if (cpu_migrate(cpu)) {
440 WARNING("Could not migrate to CPU %d\n", cpu);
441 return -ERR_CPU_MIGRATE;
442 }
444 t->tsc = rdtsc(); /* we are running on local CPU of interest */
446 if (has_aperf) {
447 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
448 return -ERR_MSR_IA32_APERF;
449 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
450 return -ERR_MSR_IA32_MPERF;
451 }
453 if (do_smi) {
454 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
455 return -ERR_MSR_SMI_COUNT;
456 t->smi_count = msr & 0xFFFFFFFF;
457 }
459 /* collect core counters only for 1st thread in core */
460 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
461 return 0;
463 if (do_nhm_cstates && !do_slm_cstates) {
464 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
465 return -ERR_MSR_CORE_C3_RESIDENCY;
466 }
468 if (do_nhm_cstates) {
469 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
470 return -ERR_MSR_CORE_C6_RESIDENCY;
471 }
473 if (do_snb_cstates)
474 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
475 return -ERR_MSR_CORE_C7_RESIDENCY;
477 if (do_dts) {
478 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
479 return -ERR_MSR_IA32_THERM_STATUS;
480 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
481 }
484 /* collect package counters only for 1st core in package */
485 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
486 return 0;
488 if (do_nhm_cstates && !do_slm_cstates) {
489 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
490 return -ERR_MSR_PKG_C3_RESIDENCY;
491 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
492 return -ERR_MSR_PKG_C6_RESIDENCY;
493 }
494 if (do_snb_cstates) {
495 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
496 return -ERR_MSR_PKG_C2_RESIDENCY;
497 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
498 return -ERR_MSR_PKG_C7_RESIDENCY;
499 }
500 if (do_c8_c9_c10) {
501 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
502 return -ERR_MSR_PKG_C8_RESIDENCY;
503 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
504 return -ERR_MSR_PKG_C9_RESIDENCY;
505 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
506 return -ERR_MSR_PKG_C10_RESIDENCY;
507 }
508 if (do_rapl & RAPL_PKG) {
509 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
510 return -ERR_MSR_PKG_ENERGY_STATUS;
511 p->energy_pkg = msr & 0xFFFFFFFF;
512 }
513 if (do_rapl & RAPL_CORES) {
514 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
515 return MSR_PP0_ENERGY_STATUS;
516 p->energy_cores = msr & 0xFFFFFFFF;
517 }
518 if (do_rapl & RAPL_DRAM) {
519 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
520 return -ERR_MSR_DRAM_ENERGY_STATUS;
521 p->energy_dram = msr & 0xFFFFFFFF;
522 }
523 if (do_rapl & RAPL_GFX) {
524 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
525 return -ERR_MSR_PP1_ENERGY_STATUS;
526 p->energy_gfx = msr & 0xFFFFFFFF;
527 }
528 if (do_rapl & RAPL_PKG_PERF_STATUS) {
529 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
530 return -ERR_MSR_PKG_PERF_STATUS;
531 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
532 }
533 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
534 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
535 return -ERR_MSR_DRAM_PERF_STATUS;
536 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
537 }
538 if (do_ptm) {
539 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
540 return -ERR_MSR_IA32_PACKAGE_THERM_STATUS;
541 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
542 }
543 return 0;
544 }
546 static void
547 free_all_buffers(void)
548 {
549 allocated = 0;
550 initialized = 0;
552 CPU_FREE(cpu_present_set);
553 cpu_present_set = NULL;
554 cpu_present_set = 0;
556 CPU_FREE(cpu_affinity_set);
557 cpu_affinity_set = NULL;
558 cpu_affinity_setsize = 0;
560 free(thread_even);
561 free(core_even);
562 free(package_even);
564 thread_even = NULL;
565 core_even = NULL;
566 package_even = NULL;
568 free(thread_odd);
569 free(core_odd);
570 free(package_odd);
572 thread_odd = NULL;
573 core_odd = NULL;
574 package_odd = NULL;
575 }
577 /*
578 * Parse a file containing a single int.
579 */
580 static int
581 parse_int_file(const char *fmt, ...)
582 {
583 va_list args;
584 char path[PATH_MAX];
585 FILE *filep;
586 int value;
588 va_start(args, fmt);
589 vsnprintf(path, sizeof(path), fmt, args);
590 va_end(args);
591 filep = fopen(path, "r");
592 if (!filep) {
593 ERROR("%s: open failed", path);
594 return -ERR_CANT_OPEN_FILE;
595 }
596 if (fscanf(filep, "%d", &value) != 1) {
597 ERROR("%s: failed to parse number from file", path);
598 return -ERR_CANT_READ_NUMBER;
599 }
600 fclose(filep);
601 return value;
602 }
604 /*
605 * cpu_is_first_sibling_in_core(cpu)
606 * return 1 if given CPU is 1st HT sibling in the core
607 */
608 static int
609 cpu_is_first_sibling_in_core(int cpu)
610 {
611 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
612 }
614 /*
615 * cpu_is_first_core_in_package(cpu)
616 * return 1 if given CPU is 1st core in package
617 */
618 static int
619 cpu_is_first_core_in_package(int cpu)
620 {
621 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
622 }
624 static int
625 get_physical_package_id(int cpu)
626 {
627 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
628 }
630 static int
631 get_core_id(int cpu)
632 {
633 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
634 }
636 static int
637 get_num_ht_siblings(int cpu)
638 {
639 char path[80];
640 FILE *filep;
641 int sib1, sib2;
642 int matches;
643 char character;
645 sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
646 filep = fopen(path, "r");
647 if (!filep) {
648 ERROR("%s: open failed", path);
649 return -ERR_CANT_OPEN_FILE;
650 }
651 /*
652 * file format:
653 * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4)
654 * otherwinse 1 sibling (self).
655 */
656 matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2);
658 fclose(filep);
660 if (matches == 3)
661 return 2;
662 else
663 return 1;
664 }
666 /*
667 * run func(thread, core, package) in topology order
668 * skip non-present cpus
669 */
672 static int __must_check
673 for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
674 struct pkg_data *, struct thread_data *, struct core_data *,
675 struct pkg_data *), struct thread_data *thread_base,
676 struct core_data *core_base, struct pkg_data *pkg_base,
677 struct thread_data *thread_base2, struct core_data *core_base2,
678 struct pkg_data *pkg_base2)
679 {
680 int retval, pkg_no, core_no, thread_no;
682 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
683 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
684 for (thread_no = 0; thread_no <
685 topo.num_threads_per_core; ++thread_no) {
686 struct thread_data *t, *t2;
687 struct core_data *c, *c2;
688 struct pkg_data *p, *p2;
690 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
692 if (cpu_is_not_present(t->cpu_id))
693 continue;
695 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
697 c = GET_CORE(core_base, core_no, pkg_no);
698 c2 = GET_CORE(core_base2, core_no, pkg_no);
700 p = GET_PKG(pkg_base, pkg_no);
701 p2 = GET_PKG(pkg_base2, pkg_no);
703 retval = func(t, c, p, t2, c2, p2);
704 if (retval)
705 return retval;
706 }
707 }
708 }
709 return 0;
710 }
712 /*
713 * run func(cpu) on every cpu in /proc/stat
714 * return max_cpu number
715 */
716 static int __must_check
717 for_all_proc_cpus(int (func)(int))
718 {
719 FILE *fp;
720 int cpu_num;
721 int retval;
723 fp = fopen(proc_stat, "r");
724 if (!fp) {
725 ERROR("%s: open failed", proc_stat);
726 return -ERR_CANT_OPEN_FILE;
727 }
729 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
730 if (retval != 0) {
731 ERROR("%s: failed to parse format", proc_stat);
732 return -ERR_CANT_READ_PROC_STAT;
733 }
735 while (1) {
736 retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
737 if (retval != 1)
738 break;
740 retval = func(cpu_num);
741 if (retval) {
742 fclose(fp);
743 return(retval);
744 }
745 }
746 fclose(fp);
747 return 0;
748 }
750 /*
751 * count_cpus()
752 * remember the last one seen, it will be the max
753 */
754 static int
755 count_cpus(int cpu)
756 {
757 if (topo.max_cpu_num < cpu)
758 topo.max_cpu_num = cpu;
760 topo.num_cpus += 1;
761 return 0;
762 }
763 static int
764 mark_cpu_present(int cpu)
765 {
766 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
767 return 0;
768 }
771 static void
772 turbostat_submit (const char *plugin_instance,
773 const char *type, const char *type_instance,
774 gauge_t value)
775 {
776 value_list_t vl = VALUE_LIST_INIT;
777 value_t v;
779 v.gauge = value;
780 vl.values = &v;
781 vl.values_len = 1;
782 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
783 sstrncpy (vl.plugin, PLUGIN_NAME, sizeof (vl.plugin));
784 if (plugin_instance != NULL)
785 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
786 sstrncpy (vl.type, type, sizeof (vl.type));
787 if (type_instance != NULL)
788 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
790 plugin_dispatch_values (&vl);
791 }
793 /*
794 * column formatting convention & formats
795 * package: "pk" 2 columns %2d
796 * core: "cor" 3 columns %3d
797 * CPU: "CPU" 3 columns %3d
798 * Pkg_W: %6.2
799 * Cor_W: %6.2
800 * GFX_W: %5.2
801 * RAM_W: %5.2
802 * GHz: "GHz" 3 columns %3.2
803 * TSC: "TSC" 3 columns %3.2
804 * SMI: "SMI" 4 columns %4d
805 * percentage " %pc3" %6.2
806 * Perf Status percentage: %5.2
807 * "CTMP" 4 columns %4d
808 */
809 #define NAME_LEN 12
810 static int
811 submit_counters(struct thread_data *t, struct core_data *c,
812 struct pkg_data *p)
813 {
814 char name[NAME_LEN];
815 double interval_float;
817 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
819 snprintf(name, NAME_LEN, "cpu%02d", t->cpu_id);
821 if (do_nhm_cstates) {
822 if (!skip_c0)
823 turbostat_submit(name, "percent", "c0", 100.0 * t->mperf/t->tsc);
824 if (!skip_c1)
825 turbostat_submit(name, "percent", "c1", 100.0 * t->c1/t->tsc);
826 }
828 /* GHz */
829 if (has_aperf && ((!aperf_mperf_unstable) || (!(t->aperf > t->tsc || t->mperf > t->tsc))))
830 turbostat_submit(NULL, "frequency", name, 1.0 * t->tsc / units * t->aperf / t->mperf / interval_float);
832 /* SMI */
833 if (do_smi)
834 turbostat_submit(NULL, "current", name, t->smi_count);
836 /* print per-core data only for 1st thread in core */
837 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
838 goto done;
840 snprintf(name, NAME_LEN, "core%02d", c->core_id);
842 if (do_nhm_cstates && !do_slm_cstates)
843 turbostat_submit(name, "percent", "c3", 100.0 * c->c3/t->tsc);
844 if (do_nhm_cstates)
845 turbostat_submit(name, "percent", "c6", 100.0 * c->c6/t->tsc);
846 if (do_snb_cstates)
847 turbostat_submit(name, "percent", "c7", 100.0 * c->c7/t->tsc);
849 if (do_dts)
850 turbostat_submit(NULL, "temperature", name, c->core_temp_c);
852 /* print per-package data only for 1st core in package */
853 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
854 goto done;
856 snprintf(name, NAME_LEN, "pkg%02d", p->package_id);
858 if (do_ptm)
859 turbostat_submit(NULL, "temperature", name, p->pkg_temp_c);
861 if (do_snb_cstates)
862 turbostat_submit(name, "percent", "pc2", 100.0 * p->pc2/t->tsc);
863 if (do_nhm_cstates && !do_slm_cstates)
864 turbostat_submit(name, "percent", "pc3", 100.0 * p->pc3/t->tsc);
865 if (do_nhm_cstates && !do_slm_cstates)
866 turbostat_submit(name, "percent", "pc6", 100.0 * p->pc6/t->tsc);
867 if (do_snb_cstates)
868 turbostat_submit(name, "percent", "pc7", 100.0 * p->pc7/t->tsc);
869 if (do_c8_c9_c10) {
870 turbostat_submit(name, "percent", "pc8", 100.0 * p->pc8/t->tsc);
871 turbostat_submit(name, "percent", "pc9", 100.0 * p->pc9/t->tsc);
872 turbostat_submit(name, "percent", "pc10", 100.0 * p->pc10/t->tsc);
873 }
875 if (do_rapl) {
876 if (do_rapl & RAPL_PKG)
877 turbostat_submit(name, "power", "Pkg_W", p->energy_pkg * rapl_energy_units / interval_float);
878 if (do_rapl & RAPL_CORES)
879 turbostat_submit(name, "power", "Cor_W", p->energy_cores * rapl_energy_units / interval_float);
880 if (do_rapl & RAPL_GFX)
881 turbostat_submit(name, "power", "GFX_W", p->energy_gfx * rapl_energy_units / interval_float);
882 if (do_rapl & RAPL_DRAM)
883 turbostat_submit(name, "power", "RAM_W", p->energy_dram * rapl_energy_units / interval_float);
884 }
885 done:
886 return 0;
887 }
889 static int
890 turbostat_read(user_data_t * not_used)
891 {
892 int ret;
894 if (!allocated) {
895 if ((ret = setup_all_buffers()) < 0)
896 return ret;
897 }
899 if (for_all_proc_cpus(cpu_is_not_present)) {
900 free_all_buffers();
901 if ((ret = setup_all_buffers()) < 0)
902 return ret;
903 if (for_all_proc_cpus(cpu_is_not_present))
904 return -ERR_CPU_NOT_PRESENT;
905 }
907 if (!initialized) {
908 if ((ret = for_all_cpus(get_counters, EVEN_COUNTERS)) < 0)
909 return ret;
910 gettimeofday(&tv_even, (struct timezone *)NULL);
911 is_even = 1;
912 initialized = 1;
913 return 0;
914 }
916 if (is_even) {
917 if ((ret = for_all_cpus(get_counters, ODD_COUNTERS)) < 0)
918 return ret;
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_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) < 0)
923 return ret;
924 if ((ret = for_all_cpus(submit_counters, EVEN_COUNTERS)) < 0)
925 return ret;
926 } else {
927 if ((ret = for_all_cpus(get_counters, EVEN_COUNTERS)) < 0)
928 return ret;
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_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) < 0)
933 return ret;
934 if ((ret = for_all_cpus(submit_counters, ODD_COUNTERS)) < 0)
935 return ret;
936 }
937 return 0;
938 }
940 static int __must_check
941 check_dev_msr()
942 {
943 struct stat sb;
945 if (stat("/dev/cpu/0/msr", &sb)) {
946 ERROR("no /dev/cpu/0/msr\n"
947 "Try \"# modprobe msr\"");
948 return -ERR_NO_MSR;
949 }
950 return 0;
951 }
953 static int __must_check
954 check_super_user()
955 {
956 if (getuid() != 0) {
957 ERROR("must be root");
958 return -ERR_NOT_ROOT;
959 }
960 return 0;
961 }
964 #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
965 #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
967 static double
968 get_tdp(unsigned int model)
969 {
970 unsigned long long msr;
972 if (do_rapl & RAPL_PKG_POWER_INFO)
973 if (!get_msr(0, MSR_PKG_POWER_INFO, &msr))
974 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
976 switch (model) {
977 case 0x37:
978 case 0x4D:
979 return 30.0;
980 default:
981 return 135.0;
982 }
983 }
986 /*
987 * rapl_probe()
988 *
989 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
990 */
991 static void
992 rapl_probe(unsigned int family, unsigned int model)
993 {
994 unsigned long long msr;
995 unsigned int time_unit;
996 double tdp;
998 if (!genuine_intel)
999 return;
1001 if (family != 6)
1002 return;
1004 switch (model) {
1005 case 0x2A:
1006 case 0x3A:
1007 case 0x3C: /* HSW */
1008 case 0x45: /* HSW */
1009 case 0x46: /* HSW */
1010 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
1011 break;
1012 case 0x3F: /* HSX */
1013 do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
1014 break;
1015 case 0x2D:
1016 case 0x3E:
1017 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_PKG_PERF_STATUS | RAPL_DRAM_PERF_STATUS | RAPL_PKG_POWER_INFO;
1018 break;
1019 case 0x37: /* BYT */
1020 case 0x4D: /* AVN */
1021 do_rapl = RAPL_PKG | RAPL_CORES ;
1022 break;
1023 default:
1024 return;
1025 }
1027 /* units on package 0, verify later other packages match */
1028 if (get_msr(0, MSR_RAPL_POWER_UNIT, &msr))
1029 return;
1031 rapl_power_units = 1.0 / (1 << (msr & 0xF));
1032 if (model == 0x37)
1033 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
1034 else
1035 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
1037 time_unit = msr >> 16 & 0xF;
1038 if (time_unit == 0)
1039 time_unit = 0xA;
1041 rapl_time_units = 1.0 / (1 << (time_unit));
1043 tdp = get_tdp(model);
1045 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
1046 // if (verbose)
1047 // fprintf(stderr, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
1049 return;
1050 }
1052 static int
1053 is_snb(unsigned int family, unsigned int model)
1054 {
1055 if (!genuine_intel)
1056 return 0;
1058 switch (model) {
1059 case 0x2A:
1060 case 0x2D:
1061 case 0x3A: /* IVB */
1062 case 0x3E: /* IVB Xeon */
1063 case 0x3C: /* HSW */
1064 case 0x3F: /* HSW */
1065 case 0x45: /* HSW */
1066 case 0x46: /* HSW */
1067 return 1;
1068 }
1069 return 0;
1070 }
1072 static int
1073 has_c8_c9_c10(unsigned int family, unsigned int model)
1074 {
1075 if (!genuine_intel)
1076 return 0;
1078 switch (model) {
1079 case 0x45:
1080 return 1;
1081 }
1082 return 0;
1083 }
1086 static int
1087 is_slm(unsigned int family, unsigned int model)
1088 {
1089 if (!genuine_intel)
1090 return 0;
1091 switch (model) {
1092 case 0x37: /* BYT */
1093 case 0x4D: /* AVN */
1094 return 1;
1095 }
1096 return 0;
1097 }
1099 /*
1100 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
1101 * the Thermal Control Circuit (TCC) activates.
1102 * This is usually equal to tjMax.
1103 *
1104 * Older processors do not have this MSR, so there we guess,
1105 * but also allow cmdline over-ride with -T.
1106 *
1107 * Several MSR temperature values are in units of degrees-C
1108 * below this value, including the Digital Thermal Sensor (DTS),
1109 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
1110 */
1111 static int __must_check
1112 set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1113 {
1114 unsigned long long msr;
1115 unsigned int target_c_local;
1116 int cpu;
1118 /* tcc_activation_temp is used only for dts or ptm */
1119 if (!(do_dts || do_ptm))
1120 return 0;
1122 /* this is a per-package concept */
1123 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1124 return 0;
1126 cpu = t->cpu_id;
1127 if (cpu_migrate(cpu)) {
1128 ERROR("Could not migrate to CPU %d\n", cpu);
1129 return -ERR_CPU_MIGRATE;
1130 }
1132 if (tcc_activation_temp_override != 0) {
1133 tcc_activation_temp = tcc_activation_temp_override;
1134 ERROR("cpu%d: Using cmdline TCC Target (%d C)\n",
1135 cpu, tcc_activation_temp);
1136 return 0;
1137 }
1139 /* Temperature Target MSR is Nehalem and newer only */
1140 if (!do_nehalem_platform_info)
1141 goto guess;
1143 if (get_msr(0, MSR_IA32_TEMPERATURE_TARGET, &msr))
1144 goto guess;
1146 target_c_local = (msr >> 16) & 0x7F;
1148 if (target_c_local < 85 || target_c_local > 127)
1149 goto guess;
1151 tcc_activation_temp = target_c_local;
1153 return 0;
1155 guess:
1156 tcc_activation_temp = TJMAX_DEFAULT;
1157 WARNING("cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
1158 cpu, tcc_activation_temp);
1160 return 0;
1161 }
1163 static int __must_check
1164 check_cpuid()
1165 {
1166 unsigned int eax, ebx, ecx, edx, max_level;
1167 unsigned int fms, family, model;
1169 eax = ebx = ecx = edx = 0;
1171 __get_cpuid(0, &max_level, &ebx, &ecx, &edx);
1173 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
1174 genuine_intel = 1;
1176 fms = 0;
1177 __get_cpuid(1, &fms, &ebx, &ecx, &edx);
1178 family = (fms >> 8) & 0xf;
1179 model = (fms >> 4) & 0xf;
1180 if (family == 6 || family == 0xf)
1181 model += ((fms >> 16) & 0xf) << 4;
1183 if (!(edx & (1 << 5))) {
1184 ERROR("CPUID: no MSR");
1185 return -ERR_NO_MSR;
1186 }
1188 /*
1189 * check max extended function levels of CPUID.
1190 * This is needed to check for invariant TSC.
1191 * This check is valid for both Intel and AMD.
1192 */
1193 ebx = ecx = edx = 0;
1194 __get_cpuid(0x80000000, &max_level, &ebx, &ecx, &edx);
1196 if (max_level < 0x80000007) {
1197 ERROR("CPUID: no invariant TSC (max_level 0x%x)", max_level);
1198 return -ERR_NO_INVARIANT_TSC;
1199 }
1201 /*
1202 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
1203 * this check is valid for both Intel and AMD
1204 */
1205 __get_cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
1206 has_invariant_tsc = edx & (1 << 8);
1208 if (!has_invariant_tsc) {
1209 ERROR("No invariant TSC");
1210 return -ERR_NO_INVARIANT_TSC;
1211 }
1213 /*
1214 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
1215 * this check is valid for both Intel and AMD
1216 */
1218 __get_cpuid(0x6, &eax, &ebx, &ecx, &edx);
1219 has_aperf = ecx & (1 << 0);
1220 do_dts = eax & (1 << 0);
1221 do_ptm = eax & (1 << 6);
1222 has_epb = ecx & (1 << 3);
1224 if (!has_aperf) {
1225 ERROR("No APERF");
1226 return -ERR_NO_APERF;
1227 }
1229 do_nehalem_platform_info = genuine_intel && has_invariant_tsc;
1230 do_nhm_cstates = genuine_intel; /* all Intel w/ non-stop TSC have NHM counters */
1231 do_smi = do_nhm_cstates;
1232 do_snb_cstates = is_snb(family, model);
1233 do_c8_c9_c10 = has_c8_c9_c10(family, model);
1234 do_slm_cstates = is_slm(family, model);
1236 rapl_probe(family, model);
1238 return 0;
1239 }
1243 static int __must_check
1244 topology_probe()
1245 {
1246 int i;
1247 int ret;
1248 int max_core_id = 0;
1249 int max_package_id = 0;
1250 int max_siblings = 0;
1251 struct cpu_topology {
1252 int core_id;
1253 int physical_package_id;
1254 } *cpus;
1256 /* Initialize num_cpus, max_cpu_num */
1257 topo.num_cpus = 0;
1258 topo.max_cpu_num = 0;
1259 ret = for_all_proc_cpus(count_cpus);
1260 if (ret < 0)
1261 return ret;
1262 if (topo.num_cpus > 1)
1263 show_cpu = 1;
1265 DEBUG("num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
1267 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
1268 if (cpus == NULL) {
1269 ERROR("calloc cpus");
1270 return -ERR_CALLOC;
1271 }
1273 /*
1274 * Allocate and initialize cpu_present_set
1275 */
1276 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
1277 if (cpu_present_set == NULL) {
1278 free(cpus);
1279 ERROR("CPU_ALLOC");
1280 return -ERR_CPU_ALLOC;
1281 }
1282 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
1283 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
1284 ret = for_all_proc_cpus(mark_cpu_present);
1285 if (ret < 0) {
1286 free(cpus);
1287 return ret;
1288 }
1290 /*
1291 * Allocate and initialize cpu_affinity_set
1292 */
1293 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
1294 if (cpu_affinity_set == NULL) {
1295 free(cpus);
1296 ERROR("CPU_ALLOC");
1297 return -ERR_CPU_ALLOC;
1298 }
1299 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
1300 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
1303 /*
1304 * For online cpus
1305 * find max_core_id, max_package_id
1306 */
1307 for (i = 0; i <= topo.max_cpu_num; ++i) {
1308 int siblings;
1310 if (cpu_is_not_present(i)) {
1311 //if (verbose > 1)
1312 fprintf(stderr, "cpu%d NOT PRESENT\n", i);
1313 continue;
1314 }
1315 cpus[i].core_id = get_core_id(i);
1316 if (cpus[i].core_id < 0)
1317 return cpus[i].core_id;
1318 if (cpus[i].core_id > max_core_id)
1319 max_core_id = cpus[i].core_id;
1321 cpus[i].physical_package_id = get_physical_package_id(i);
1322 if (cpus[i].physical_package_id < 0)
1323 return cpus[i].physical_package_id;
1324 if (cpus[i].physical_package_id > max_package_id)
1325 max_package_id = cpus[i].physical_package_id;
1327 siblings = get_num_ht_siblings(i);
1328 if (siblings < 0)
1329 return siblings;
1330 if (siblings > max_siblings)
1331 max_siblings = siblings;
1332 DEBUG("cpu %d pkg %d core %d\n",
1333 i, cpus[i].physical_package_id, cpus[i].core_id);
1334 }
1335 topo.num_cores_per_pkg = max_core_id + 1;
1336 DEBUG("max_core_id %d, sizing for %d cores per package\n",
1337 max_core_id, topo.num_cores_per_pkg);
1338 if (topo.num_cores_per_pkg > 1)
1339 show_core = 1;
1341 topo.num_packages = max_package_id + 1;
1342 DEBUG("max_package_id %d, sizing for %d packages\n",
1343 max_package_id, topo.num_packages);
1344 if (topo.num_packages > 1)
1345 show_pkg = 1;
1347 topo.num_threads_per_core = max_siblings;
1348 DEBUG("max_siblings %d\n", max_siblings);
1350 free(cpus);
1351 return 0;
1352 }
1354 static int
1355 allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
1356 {
1357 int i;
1359 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
1360 topo.num_packages, sizeof(struct thread_data));
1361 if (*t == NULL)
1362 goto error;
1364 for (i = 0; i < topo.num_threads_per_core *
1365 topo.num_cores_per_pkg * topo.num_packages; i++)
1366 (*t)[i].cpu_id = -1;
1368 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
1369 sizeof(struct core_data));
1370 if (*c == NULL)
1371 goto error;
1373 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
1374 (*c)[i].core_id = -1;
1376 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
1377 if (*p == NULL)
1378 goto error;
1380 for (i = 0; i < topo.num_packages; i++)
1381 (*p)[i].package_id = i;
1383 return 0;
1384 error:
1385 ERROR("calloc counters");
1386 return -ERR_CALLOC;
1387 }
1388 /*
1389 * init_counter()
1390 *
1391 * set cpu_id, core_num, pkg_num
1392 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
1393 *
1394 * increment topo.num_cores when 1st core in pkg seen
1395 */
1396 static int
1397 init_counter(struct thread_data *thread_base, struct core_data *core_base,
1398 struct pkg_data *pkg_base, int thread_num, int core_num,
1399 int pkg_num, int cpu_id)
1400 {
1401 int ret;
1402 struct thread_data *t;
1403 struct core_data *c;
1404 struct pkg_data *p;
1406 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
1407 c = GET_CORE(core_base, core_num, pkg_num);
1408 p = GET_PKG(pkg_base, pkg_num);
1410 t->cpu_id = cpu_id;
1411 if (thread_num == 0) {
1412 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
1413 if ((ret = cpu_is_first_core_in_package(cpu_id)) < 0) {
1414 return ret;
1415 } else if (ret != 0) {
1416 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
1417 }
1418 }
1420 c->core_id = core_num;
1421 p->package_id = pkg_num;
1423 return 0;
1424 }
1427 static int
1428 initialize_counters(int cpu_id)
1429 {
1430 int my_thread_id, my_core_id, my_package_id;
1431 int ret;
1433 my_package_id = get_physical_package_id(cpu_id);
1434 if (my_package_id < 0)
1435 return my_package_id;
1436 my_core_id = get_core_id(cpu_id);
1437 if (my_core_id < 0)
1438 return my_core_id;
1440 if ((ret = cpu_is_first_sibling_in_core(cpu_id)) < 0) {
1441 return ret;
1442 } else if (ret != 0) {
1443 my_thread_id = 0;
1444 topo.num_cores++;
1445 } else {
1446 my_thread_id = 1;
1447 }
1449 ret = init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
1450 if (ret < 0)
1451 return ret;
1452 ret = init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
1453 if (ret < 0)
1454 return ret;
1455 return 0;
1456 }
1458 #define DO_OR_GOTO_ERR(something) \
1459 do { \
1460 ret = (something); \
1461 if (ret < 0) \
1462 goto err; \
1463 } while (0)
1465 static int setup_all_buffers(void)
1466 {
1467 int ret;
1469 DO_OR_GOTO_ERR(topology_probe());
1470 DO_OR_GOTO_ERR(allocate_counters(&thread_even, &core_even, &package_even));
1471 DO_OR_GOTO_ERR(allocate_counters(&thread_odd, &core_odd, &package_odd));
1472 DO_OR_GOTO_ERR(for_all_proc_cpus(initialize_counters));
1474 allocated = 1;
1475 return 0;
1476 err:
1477 free_all_buffers();
1478 return ret;
1479 }
1481 static int
1482 turbostat_init(void)
1483 {
1484 int ret;
1485 struct timespec ts;
1487 DO_OR_GOTO_ERR(check_cpuid());
1488 DO_OR_GOTO_ERR(check_dev_msr());
1489 DO_OR_GOTO_ERR(check_super_user());
1490 DO_OR_GOTO_ERR(setup_all_buffers());
1491 DO_OR_GOTO_ERR(for_all_cpus(set_temperature_target, EVEN_COUNTERS));
1493 ts.tv_sec = interval_sec;
1494 ts.tv_nsec = 0;
1496 plugin_register_complex_read(NULL, PLUGIN_NAME, turbostat_read, &ts, NULL);
1498 return 0;
1499 err:
1500 free_all_buffers();
1501 return ret;
1502 }
1504 static const char *config_keys[] =
1505 {
1506 "Interval",
1507 };
1508 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
1510 static int
1511 turbostat_config(const char *key, const char *value)
1512 {
1513 if (strcasecmp("Interval", key) == 0)
1514 interval_sec = atoi(value);
1515 else
1516 return -1;
1517 return 0;
1518 }
1520 void module_register(void);
1521 void module_register(void)
1522 {
1523 plugin_register_init(PLUGIN_NAME, turbostat_init);
1524 plugin_register_config(PLUGIN_NAME, turbostat_config, config_keys, config_keys_num);
1525 }