5424a30587721b75327869037d81482561f2b1be
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 static int setup_all_buffers(void);
217 static int
218 cpu_is_not_present(int cpu)
219 {
220 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
221 }
222 /*
223 * run func(thread, core, package) in topology order
224 * skip non-present cpus
225 */
227 static int __attribute__((warn_unused_result))
228 for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
229 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
230 {
231 int retval, pkg_no, core_no, thread_no;
233 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
234 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
235 for (thread_no = 0; thread_no <
236 topo.num_threads_per_core; ++thread_no) {
237 struct thread_data *t;
238 struct core_data *c;
239 struct pkg_data *p;
241 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
243 if (cpu_is_not_present(t->cpu_id))
244 continue;
246 c = GET_CORE(core_base, core_no, pkg_no);
247 p = GET_PKG(pkg_base, pkg_no);
249 retval = func(t, c, p);
250 if (retval)
251 return retval;
252 }
253 }
254 }
255 return 0;
256 }
258 static int __attribute__((warn_unused_result))
259 cpu_migrate(int cpu)
260 {
261 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
262 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
263 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
264 return -ERR_CPU_MIGRATE;
265 else
266 return 0;
267 }
269 static int __attribute__((warn_unused_result))
270 get_msr(int cpu, off_t offset, unsigned long long *msr)
271 {
272 ssize_t retval;
273 char pathname[32];
274 int fd;
276 ssnprintf(pathname, 32, "/dev/cpu/%d/msr", cpu);
277 fd = open(pathname, O_RDONLY);
278 if (fd < 0)
279 return -1;
281 retval = pread(fd, msr, sizeof *msr, offset);
282 close(fd);
284 if (retval != sizeof *msr) {
285 ERROR ("%s offset 0x%llx read failed\n", pathname, (unsigned long long)offset);
286 return -1;
287 }
289 return 0;
290 }
292 #define DELTA_WRAP32(new, old) \
293 if (new > old) { \
294 old = new - old; \
295 } else { \
296 old = 0x100000000 + new - old; \
297 }
299 static void
300 delta_package(struct pkg_data *new, struct pkg_data *old)
301 {
302 old->pc2 = new->pc2 - old->pc2;
303 old->pc3 = new->pc3 - old->pc3;
304 old->pc6 = new->pc6 - old->pc6;
305 old->pc7 = new->pc7 - old->pc7;
306 old->pc8 = new->pc8 - old->pc8;
307 old->pc9 = new->pc9 - old->pc9;
308 old->pc10 = new->pc10 - old->pc10;
309 old->pkg_temp_c = new->pkg_temp_c;
311 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
312 DELTA_WRAP32(new->energy_cores, old->energy_cores);
313 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
314 DELTA_WRAP32(new->energy_dram, old->energy_dram);
315 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
316 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
317 }
319 static void
320 delta_core(struct core_data *new, struct core_data *old)
321 {
322 old->c3 = new->c3 - old->c3;
323 old->c6 = new->c6 - old->c6;
324 old->c7 = new->c7 - old->c7;
325 old->core_temp_c = new->core_temp_c;
326 }
328 /*
329 * old = new - old
330 */
331 static int __attribute__((warn_unused_result))
332 delta_thread(struct thread_data *new, struct thread_data *old,
333 struct core_data *core_delta)
334 {
335 old->tsc = new->tsc - old->tsc;
337 /* check for TSC < 1 Mcycles over interval */
338 if (old->tsc < (1000 * 1000)) {
339 WARNING("Insanely slow TSC rate, TSC stops in idle?\n"
340 "You can disable all c-states by booting with \"idle=poll\"\n"
341 "or just the deep ones with \"processor.max_cstate=1\"");
342 return -1;
343 }
345 old->c1 = new->c1 - old->c1;
347 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
348 old->aperf = new->aperf - old->aperf;
349 old->mperf = new->mperf - old->mperf;
350 } else {
352 if (!aperf_mperf_unstable) {
353 WARNING("%s: APERF or MPERF went backwards *\n", progname);
354 WARNING("* Frequency results do not cover entire interval *\n");
355 WARNING("* fix this by running Linux-2.6.30 or later *\n");
357 aperf_mperf_unstable = 1;
358 }
359 /*
360 * mperf delta is likely a huge "positive" number
361 * can not use it for calculating c0 time
362 */
363 skip_c0 = 1;
364 skip_c1 = 1;
365 }
368 /*
369 * As counter collection is not atomic,
370 * it is possible for mperf's non-halted cycles + idle states
371 * to exceed TSC's all cycles: show c1 = 0% in that case.
372 */
373 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
374 old->c1 = 0;
375 else {
376 /* normal case, derive c1 */
377 old->c1 = old->tsc - old->mperf - core_delta->c3
378 - core_delta->c6 - core_delta->c7;
379 }
381 if (old->mperf == 0) {
382 WARNING("cpu%d MPERF 0!\n", old->cpu_id);
383 old->mperf = 1; /* divide by 0 protection */
384 }
386 if (do_smi)
387 old->smi_count = new->smi_count - old->smi_count;
389 return 0;
390 }
392 static int __attribute__((warn_unused_result))
393 delta_cpu(struct thread_data *t, struct core_data *c,
394 struct pkg_data *p, struct thread_data *t2,
395 struct core_data *c2, struct pkg_data *p2)
396 {
397 int ret;
399 /* calculate core delta only for 1st thread in core */
400 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
401 delta_core(c, c2);
403 /* always calculate thread delta */
404 ret = delta_thread(t, t2, c2); /* c2 is core delta */
405 if (ret != 0)
406 return ret;
408 /* calculate package delta only for 1st core in package */
409 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
410 delta_package(p, p2);
412 return 0;
413 }
415 static unsigned long long
416 rdtsc(void)
417 {
418 unsigned int low, high;
420 asm volatile("rdtsc" : "=a" (low), "=d" (high));
422 return low | ((unsigned long long)high) << 32;
423 }
426 /*
427 * get_counters(...)
428 * migrate to cpu
429 * acquire and record local counters for that cpu
430 */
431 static int __attribute__((warn_unused_result))
432 get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
433 {
434 int cpu = t->cpu_id;
435 unsigned long long msr;
437 if (cpu_migrate(cpu)) {
438 WARNING("Could not migrate to CPU %d\n", cpu);
439 return -ERR_CPU_MIGRATE;
440 }
442 t->tsc = rdtsc(); /* we are running on local CPU of interest */
444 if (has_aperf) {
445 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
446 return -ERR_MSR_IA32_APERF;
447 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
448 return -ERR_MSR_IA32_MPERF;
449 }
451 if (do_smi) {
452 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
453 return -ERR_MSR_SMI_COUNT;
454 t->smi_count = msr & 0xFFFFFFFF;
455 }
457 /* collect core counters only for 1st thread in core */
458 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
459 return 0;
461 if (do_nhm_cstates && !do_slm_cstates) {
462 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
463 return -ERR_MSR_CORE_C3_RESIDENCY;
464 }
466 if (do_nhm_cstates) {
467 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
468 return -ERR_MSR_CORE_C6_RESIDENCY;
469 }
471 if (do_snb_cstates)
472 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
473 return -ERR_MSR_CORE_C7_RESIDENCY;
475 if (do_dts) {
476 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
477 return -ERR_MSR_IA32_THERM_STATUS;
478 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
479 }
482 /* collect package counters only for 1st core in package */
483 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
484 return 0;
486 if (do_nhm_cstates && !do_slm_cstates) {
487 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
488 return -ERR_MSR_PKG_C3_RESIDENCY;
489 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
490 return -ERR_MSR_PKG_C6_RESIDENCY;
491 }
492 if (do_snb_cstates) {
493 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
494 return -ERR_MSR_PKG_C2_RESIDENCY;
495 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
496 return -ERR_MSR_PKG_C7_RESIDENCY;
497 }
498 if (do_c8_c9_c10) {
499 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
500 return -ERR_MSR_PKG_C8_RESIDENCY;
501 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
502 return -ERR_MSR_PKG_C9_RESIDENCY;
503 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
504 return -ERR_MSR_PKG_C10_RESIDENCY;
505 }
506 if (do_rapl & RAPL_PKG) {
507 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
508 return -ERR_MSR_PKG_ENERGY_STATUS;
509 p->energy_pkg = msr & 0xFFFFFFFF;
510 }
511 if (do_rapl & RAPL_CORES) {
512 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
513 return MSR_PP0_ENERGY_STATUS;
514 p->energy_cores = msr & 0xFFFFFFFF;
515 }
516 if (do_rapl & RAPL_DRAM) {
517 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
518 return -ERR_MSR_DRAM_ENERGY_STATUS;
519 p->energy_dram = msr & 0xFFFFFFFF;
520 }
521 if (do_rapl & RAPL_GFX) {
522 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
523 return -ERR_MSR_PP1_ENERGY_STATUS;
524 p->energy_gfx = msr & 0xFFFFFFFF;
525 }
526 if (do_rapl & RAPL_PKG_PERF_STATUS) {
527 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
528 return -ERR_MSR_PKG_PERF_STATUS;
529 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
530 }
531 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
532 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
533 return -ERR_MSR_DRAM_PERF_STATUS;
534 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
535 }
536 if (do_ptm) {
537 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
538 return -ERR_MSR_IA32_PACKAGE_THERM_STATUS;
539 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
540 }
541 return 0;
542 }
544 static void
545 free_all_buffers(void)
546 {
547 allocated = 0;
548 initialized = 0;
550 CPU_FREE(cpu_present_set);
551 cpu_present_set = NULL;
552 cpu_present_set = 0;
554 CPU_FREE(cpu_affinity_set);
555 cpu_affinity_set = NULL;
556 cpu_affinity_setsize = 0;
558 free(thread_even);
559 free(core_even);
560 free(package_even);
562 thread_even = NULL;
563 core_even = NULL;
564 package_even = NULL;
566 free(thread_odd);
567 free(core_odd);
568 free(package_odd);
570 thread_odd = NULL;
571 core_odd = NULL;
572 package_odd = NULL;
573 }
575 /*
576 * Parse a file containing a single int.
577 */
578 static int __attribute__ ((format(printf,1,2)))
579 parse_int_file(const char *fmt, ...)
580 {
581 va_list args;
582 char path[PATH_MAX];
583 FILE *filep;
584 int value;
586 va_start(args, fmt);
587 vsnprintf(path, sizeof(path), fmt, args);
588 va_end(args);
589 filep = fopen(path, "r");
590 if (!filep) {
591 ERROR("%s: open failed", path);
592 return -ERR_CANT_OPEN_FILE;
593 }
594 if (fscanf(filep, "%d", &value) != 1) {
595 ERROR("%s: failed to parse number from file", path);
596 return -ERR_CANT_READ_NUMBER;
597 }
598 fclose(filep);
599 return value;
600 }
602 /*
603 * cpu_is_first_sibling_in_core(cpu)
604 * return 1 if given CPU is 1st HT sibling in the core
605 */
606 static int
607 cpu_is_first_sibling_in_core(int cpu)
608 {
609 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
610 }
612 /*
613 * cpu_is_first_core_in_package(cpu)
614 * return 1 if given CPU is 1st core in package
615 */
616 static int
617 cpu_is_first_core_in_package(int cpu)
618 {
619 return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
620 }
622 static int
623 get_physical_package_id(int cpu)
624 {
625 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
626 }
628 static int
629 get_core_id(int cpu)
630 {
631 return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
632 }
634 static int
635 get_num_ht_siblings(int cpu)
636 {
637 char path[80];
638 FILE *filep;
639 int sib1, sib2;
640 int matches;
641 char character;
643 ssnprintf(path, 80, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
644 filep = fopen(path, "r");
645 if (!filep) {
646 ERROR("%s: open failed", path);
647 return -ERR_CANT_OPEN_FILE;
648 }
649 /*
650 * file format:
651 * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4)
652 * otherwinse 1 sibling (self).
653 */
654 matches = fscanf(filep, "%d%c%d\n", &sib1, &character, &sib2);
656 fclose(filep);
658 if (matches == 3)
659 return 2;
660 else
661 return 1;
662 }
664 /*
665 * run func(thread, core, package) in topology order
666 * skip non-present cpus
667 */
670 static int __attribute__((warn_unused_result))
671 for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
672 struct pkg_data *, struct thread_data *, struct core_data *,
673 struct pkg_data *), struct thread_data *thread_base,
674 struct core_data *core_base, struct pkg_data *pkg_base,
675 struct thread_data *thread_base2, struct core_data *core_base2,
676 struct pkg_data *pkg_base2)
677 {
678 int retval, pkg_no, core_no, thread_no;
680 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
681 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
682 for (thread_no = 0; thread_no <
683 topo.num_threads_per_core; ++thread_no) {
684 struct thread_data *t, *t2;
685 struct core_data *c, *c2;
686 struct pkg_data *p, *p2;
688 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
690 if (cpu_is_not_present(t->cpu_id))
691 continue;
693 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
695 c = GET_CORE(core_base, core_no, pkg_no);
696 c2 = GET_CORE(core_base2, core_no, pkg_no);
698 p = GET_PKG(pkg_base, pkg_no);
699 p2 = GET_PKG(pkg_base2, pkg_no);
701 retval = func(t, c, p, t2, c2, p2);
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("%s: open failed", 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("%s: failed to parse format", 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,
810 struct pkg_data *p)
811 {
812 char name[NAME_LEN];
813 double interval_float;
815 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
817 snprintf(name, NAME_LEN, "cpu%02d", t->cpu_id);
819 if (do_nhm_cstates) {
820 if (!skip_c0)
821 turbostat_submit(name, "percent", "c0", 100.0 * t->mperf/t->tsc);
822 if (!skip_c1)
823 turbostat_submit(name, "percent", "c1", 100.0 * t->c1/t->tsc);
824 }
826 /* GHz */
827 if (has_aperf && ((!aperf_mperf_unstable) || (!(t->aperf > t->tsc || t->mperf > t->tsc))))
828 turbostat_submit(NULL, "frequency", name, 1.0 * t->tsc / units * t->aperf / t->mperf / interval_float);
830 /* SMI */
831 if (do_smi)
832 turbostat_submit(NULL, "current", name, t->smi_count);
834 /* print per-core data only for 1st thread in core */
835 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
836 goto done;
838 snprintf(name, NAME_LEN, "core%02d", c->core_id);
840 if (do_nhm_cstates && !do_slm_cstates)
841 turbostat_submit(name, "percent", "c3", 100.0 * c->c3/t->tsc);
842 if (do_nhm_cstates)
843 turbostat_submit(name, "percent", "c6", 100.0 * c->c6/t->tsc);
844 if (do_snb_cstates)
845 turbostat_submit(name, "percent", "c7", 100.0 * c->c7/t->tsc);
847 if (do_dts)
848 turbostat_submit(NULL, "temperature", name, c->core_temp_c);
850 /* print per-package data only for 1st core in package */
851 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
852 goto done;
854 snprintf(name, NAME_LEN, "pkg%02d", p->package_id);
856 if (do_ptm)
857 turbostat_submit(NULL, "temperature", name, p->pkg_temp_c);
859 if (do_snb_cstates)
860 turbostat_submit(name, "percent", "pc2", 100.0 * p->pc2/t->tsc);
861 if (do_nhm_cstates && !do_slm_cstates)
862 turbostat_submit(name, "percent", "pc3", 100.0 * p->pc3/t->tsc);
863 if (do_nhm_cstates && !do_slm_cstates)
864 turbostat_submit(name, "percent", "pc6", 100.0 * p->pc6/t->tsc);
865 if (do_snb_cstates)
866 turbostat_submit(name, "percent", "pc7", 100.0 * p->pc7/t->tsc);
867 if (do_c8_c9_c10) {
868 turbostat_submit(name, "percent", "pc8", 100.0 * p->pc8/t->tsc);
869 turbostat_submit(name, "percent", "pc9", 100.0 * p->pc9/t->tsc);
870 turbostat_submit(name, "percent", "pc10", 100.0 * p->pc10/t->tsc);
871 }
873 if (do_rapl) {
874 if (do_rapl & RAPL_PKG)
875 turbostat_submit(name, "power", "Pkg_W", p->energy_pkg * rapl_energy_units / interval_float);
876 if (do_rapl & RAPL_CORES)
877 turbostat_submit(name, "power", "Cor_W", p->energy_cores * rapl_energy_units / interval_float);
878 if (do_rapl & RAPL_GFX)
879 turbostat_submit(name, "power", "GFX_W", p->energy_gfx * rapl_energy_units / interval_float);
880 if (do_rapl & RAPL_DRAM)
881 turbostat_submit(name, "power", "RAM_W", p->energy_dram * rapl_energy_units / interval_float);
882 }
883 done:
884 return 0;
885 }
887 static int
888 turbostat_read(user_data_t * not_used)
889 {
890 int ret;
892 if (!allocated) {
893 if ((ret = setup_all_buffers()) < 0)
894 return ret;
895 }
897 if (for_all_proc_cpus(cpu_is_not_present)) {
898 free_all_buffers();
899 if ((ret = setup_all_buffers()) < 0)
900 return ret;
901 if (for_all_proc_cpus(cpu_is_not_present))
902 return -ERR_CPU_NOT_PRESENT;
903 }
905 if (!initialized) {
906 if ((ret = for_all_cpus(get_counters, EVEN_COUNTERS)) < 0)
907 return ret;
908 gettimeofday(&tv_even, (struct timezone *)NULL);
909 is_even = 1;
910 initialized = 1;
911 return 0;
912 }
914 if (is_even) {
915 if ((ret = for_all_cpus(get_counters, ODD_COUNTERS)) < 0)
916 return ret;
917 gettimeofday(&tv_odd, (struct timezone *)NULL);
918 is_even = 0;
919 timersub(&tv_odd, &tv_even, &tv_delta);
920 if ((ret = for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) < 0)
921 return ret;
922 if ((ret = for_all_cpus(submit_counters, EVEN_COUNTERS)) < 0)
923 return ret;
924 } else {
925 if ((ret = for_all_cpus(get_counters, EVEN_COUNTERS)) < 0)
926 return ret;
927 gettimeofday(&tv_even, (struct timezone *)NULL);
928 is_even = 1;
929 timersub(&tv_even, &tv_odd, &tv_delta);
930 if ((ret = for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) < 0)
931 return ret;
932 if ((ret = for_all_cpus(submit_counters, ODD_COUNTERS)) < 0)
933 return ret;
934 }
935 return 0;
936 }
938 static int __attribute__((warn_unused_result))
939 check_dev_msr()
940 {
941 struct stat sb;
943 if (stat("/dev/cpu/0/msr", &sb)) {
944 ERROR("no /dev/cpu/0/msr\n"
945 "Try \"# modprobe msr\"");
946 return -ERR_NO_MSR;
947 }
948 return 0;
949 }
951 static int __attribute__((warn_unused_result))
952 check_super_user()
953 {
954 if (getuid() != 0) {
955 ERROR("must be root");
956 return -ERR_NOT_ROOT;
957 }
958 return 0;
959 }
962 #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
963 #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
965 static double
966 get_tdp(unsigned int model)
967 {
968 unsigned long long msr;
970 if (do_rapl & RAPL_PKG_POWER_INFO)
971 if (!get_msr(0, MSR_PKG_POWER_INFO, &msr))
972 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
974 switch (model) {
975 case 0x37:
976 case 0x4D:
977 return 30.0;
978 default:
979 return 135.0;
980 }
981 }
984 /*
985 * rapl_probe()
986 *
987 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
988 */
989 static void
990 rapl_probe(unsigned int family, unsigned int model)
991 {
992 unsigned long long msr;
993 unsigned int time_unit;
994 double tdp;
996 if (!genuine_intel)
997 return;
999 if (family != 6)
1000 return;
1002 switch (model) {
1003 case 0x2A:
1004 case 0x3A:
1005 case 0x3C: /* HSW */
1006 case 0x45: /* HSW */
1007 case 0x46: /* HSW */
1008 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
1009 break;
1010 case 0x3F: /* HSX */
1011 do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
1012 break;
1013 case 0x2D:
1014 case 0x3E:
1015 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_PKG_PERF_STATUS | RAPL_DRAM_PERF_STATUS | RAPL_PKG_POWER_INFO;
1016 break;
1017 case 0x37: /* BYT */
1018 case 0x4D: /* AVN */
1019 do_rapl = RAPL_PKG | RAPL_CORES ;
1020 break;
1021 default:
1022 return;
1023 }
1025 /* units on package 0, verify later other packages match */
1026 if (get_msr(0, MSR_RAPL_POWER_UNIT, &msr))
1027 return;
1029 rapl_power_units = 1.0 / (1 << (msr & 0xF));
1030 if (model == 0x37)
1031 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
1032 else
1033 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
1035 time_unit = msr >> 16 & 0xF;
1036 if (time_unit == 0)
1037 time_unit = 0xA;
1039 rapl_time_units = 1.0 / (1 << (time_unit));
1041 tdp = get_tdp(model);
1043 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
1044 // if (verbose)
1045 // fprintf(stderr, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
1047 return;
1048 }
1050 static int
1051 is_snb(unsigned int family, unsigned int model)
1052 {
1053 if (!genuine_intel)
1054 return 0;
1056 switch (model) {
1057 case 0x2A:
1058 case 0x2D:
1059 case 0x3A: /* IVB */
1060 case 0x3E: /* IVB Xeon */
1061 case 0x3C: /* HSW */
1062 case 0x3F: /* HSW */
1063 case 0x45: /* HSW */
1064 case 0x46: /* HSW */
1065 return 1;
1066 }
1067 return 0;
1068 }
1070 static int
1071 has_c8_c9_c10(unsigned int family, unsigned int model)
1072 {
1073 if (!genuine_intel)
1074 return 0;
1076 switch (model) {
1077 case 0x45:
1078 return 1;
1079 }
1080 return 0;
1081 }
1084 static int
1085 is_slm(unsigned int family, unsigned int model)
1086 {
1087 if (!genuine_intel)
1088 return 0;
1089 switch (model) {
1090 case 0x37: /* BYT */
1091 case 0x4D: /* AVN */
1092 return 1;
1093 }
1094 return 0;
1095 }
1097 /*
1098 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
1099 * the Thermal Control Circuit (TCC) activates.
1100 * This is usually equal to tjMax.
1101 *
1102 * Older processors do not have this MSR, so there we guess,
1103 * but also allow cmdline over-ride with -T.
1104 *
1105 * Several MSR temperature values are in units of degrees-C
1106 * below this value, including the Digital Thermal Sensor (DTS),
1107 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
1108 */
1109 static int __attribute__((warn_unused_result))
1110 set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1111 {
1112 unsigned long long msr;
1113 unsigned int target_c_local;
1114 int cpu;
1116 /* tcc_activation_temp is used only for dts or ptm */
1117 if (!(do_dts || do_ptm))
1118 return 0;
1120 /* this is a per-package concept */
1121 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1122 return 0;
1124 cpu = t->cpu_id;
1125 if (cpu_migrate(cpu)) {
1126 ERROR("Could not migrate to CPU %d\n", cpu);
1127 return -ERR_CPU_MIGRATE;
1128 }
1130 if (tcc_activation_temp_override != 0) {
1131 tcc_activation_temp = tcc_activation_temp_override;
1132 ERROR("cpu%d: Using cmdline TCC Target (%d C)\n",
1133 cpu, tcc_activation_temp);
1134 return 0;
1135 }
1137 /* Temperature Target MSR is Nehalem and newer only */
1138 if (!do_nehalem_platform_info)
1139 goto guess;
1141 if (get_msr(0, MSR_IA32_TEMPERATURE_TARGET, &msr))
1142 goto guess;
1144 target_c_local = (msr >> 16) & 0x7F;
1146 if (target_c_local < 85 || target_c_local > 127)
1147 goto guess;
1149 tcc_activation_temp = target_c_local;
1151 return 0;
1153 guess:
1154 tcc_activation_temp = TJMAX_DEFAULT;
1155 WARNING("cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
1156 cpu, tcc_activation_temp);
1158 return 0;
1159 }
1161 static int __attribute__((warn_unused_result))
1162 check_cpuid()
1163 {
1164 unsigned int eax, ebx, ecx, edx, max_level;
1165 unsigned int fms, family, model;
1167 eax = ebx = ecx = edx = 0;
1169 __get_cpuid(0, &max_level, &ebx, &ecx, &edx);
1171 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
1172 genuine_intel = 1;
1174 fms = 0;
1175 __get_cpuid(1, &fms, &ebx, &ecx, &edx);
1176 family = (fms >> 8) & 0xf;
1177 model = (fms >> 4) & 0xf;
1178 if (family == 6 || family == 0xf)
1179 model += ((fms >> 16) & 0xf) << 4;
1181 if (!(edx & (1 << 5))) {
1182 ERROR("CPUID: no MSR");
1183 return -ERR_NO_MSR;
1184 }
1186 /*
1187 * check max extended function levels of CPUID.
1188 * This is needed to check for invariant TSC.
1189 * This check is valid for both Intel and AMD.
1190 */
1191 ebx = ecx = edx = 0;
1192 __get_cpuid(0x80000000, &max_level, &ebx, &ecx, &edx);
1194 if (max_level < 0x80000007) {
1195 ERROR("CPUID: no invariant TSC (max_level 0x%x)", max_level);
1196 return -ERR_NO_INVARIANT_TSC;
1197 }
1199 /*
1200 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
1201 * this check is valid for both Intel and AMD
1202 */
1203 __get_cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
1204 has_invariant_tsc = edx & (1 << 8);
1206 if (!has_invariant_tsc) {
1207 ERROR("No invariant TSC");
1208 return -ERR_NO_INVARIANT_TSC;
1209 }
1211 /*
1212 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
1213 * this check is valid for both Intel and AMD
1214 */
1216 __get_cpuid(0x6, &eax, &ebx, &ecx, &edx);
1217 has_aperf = ecx & (1 << 0);
1218 do_dts = eax & (1 << 0);
1219 do_ptm = eax & (1 << 6);
1220 has_epb = ecx & (1 << 3);
1222 if (!has_aperf) {
1223 ERROR("No APERF");
1224 return -ERR_NO_APERF;
1225 }
1227 do_nehalem_platform_info = genuine_intel && has_invariant_tsc;
1228 do_nhm_cstates = genuine_intel; /* all Intel w/ non-stop TSC have NHM counters */
1229 do_smi = do_nhm_cstates;
1230 do_snb_cstates = is_snb(family, model);
1231 do_c8_c9_c10 = has_c8_c9_c10(family, model);
1232 do_slm_cstates = is_slm(family, model);
1234 rapl_probe(family, model);
1236 return 0;
1237 }
1241 static int __attribute__((warn_unused_result))
1242 topology_probe()
1243 {
1244 int i;
1245 int ret;
1246 int max_core_id = 0;
1247 int max_package_id = 0;
1248 int max_siblings = 0;
1249 struct cpu_topology {
1250 int core_id;
1251 int physical_package_id;
1252 } *cpus;
1254 /* Initialize num_cpus, max_cpu_num */
1255 topo.num_cpus = 0;
1256 topo.max_cpu_num = 0;
1257 ret = for_all_proc_cpus(count_cpus);
1258 if (ret < 0)
1259 return ret;
1260 if (topo.num_cpus > 1)
1261 show_cpu = 1;
1263 DEBUG("num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
1265 cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
1266 if (cpus == NULL) {
1267 ERROR("calloc cpus");
1268 return -ERR_CALLOC;
1269 }
1271 /*
1272 * Allocate and initialize cpu_present_set
1273 */
1274 cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
1275 if (cpu_present_set == NULL) {
1276 free(cpus);
1277 ERROR("CPU_ALLOC");
1278 return -ERR_CPU_ALLOC;
1279 }
1280 cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
1281 CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
1282 ret = for_all_proc_cpus(mark_cpu_present);
1283 if (ret < 0) {
1284 free(cpus);
1285 return ret;
1286 }
1288 /*
1289 * Allocate and initialize cpu_affinity_set
1290 */
1291 cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
1292 if (cpu_affinity_set == NULL) {
1293 free(cpus);
1294 ERROR("CPU_ALLOC");
1295 return -ERR_CPU_ALLOC;
1296 }
1297 cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
1298 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
1301 /*
1302 * For online cpus
1303 * find max_core_id, max_package_id
1304 */
1305 for (i = 0; i <= topo.max_cpu_num; ++i) {
1306 int siblings;
1308 if (cpu_is_not_present(i)) {
1309 //if (verbose > 1)
1310 fprintf(stderr, "cpu%d NOT PRESENT\n", i);
1311 continue;
1312 }
1313 cpus[i].core_id = get_core_id(i);
1314 if (cpus[i].core_id < 0)
1315 return cpus[i].core_id;
1316 if (cpus[i].core_id > max_core_id)
1317 max_core_id = cpus[i].core_id;
1319 cpus[i].physical_package_id = get_physical_package_id(i);
1320 if (cpus[i].physical_package_id < 0)
1321 return cpus[i].physical_package_id;
1322 if (cpus[i].physical_package_id > max_package_id)
1323 max_package_id = cpus[i].physical_package_id;
1325 siblings = get_num_ht_siblings(i);
1326 if (siblings < 0)
1327 return siblings;
1328 if (siblings > max_siblings)
1329 max_siblings = siblings;
1330 DEBUG("cpu %d pkg %d core %d\n",
1331 i, cpus[i].physical_package_id, cpus[i].core_id);
1332 }
1333 topo.num_cores_per_pkg = max_core_id + 1;
1334 DEBUG("max_core_id %d, sizing for %d cores per package\n",
1335 max_core_id, topo.num_cores_per_pkg);
1336 if (topo.num_cores_per_pkg > 1)
1337 show_core = 1;
1339 topo.num_packages = max_package_id + 1;
1340 DEBUG("max_package_id %d, sizing for %d packages\n",
1341 max_package_id, topo.num_packages);
1342 if (topo.num_packages > 1)
1343 show_pkg = 1;
1345 topo.num_threads_per_core = max_siblings;
1346 DEBUG("max_siblings %d\n", max_siblings);
1348 free(cpus);
1349 return 0;
1350 }
1352 static int
1353 allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
1354 {
1355 int i;
1357 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
1358 topo.num_packages, sizeof(struct thread_data));
1359 if (*t == NULL)
1360 goto error;
1362 for (i = 0; i < topo.num_threads_per_core *
1363 topo.num_cores_per_pkg * topo.num_packages; i++)
1364 (*t)[i].cpu_id = -1;
1366 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
1367 sizeof(struct core_data));
1368 if (*c == NULL)
1369 goto error;
1371 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
1372 (*c)[i].core_id = -1;
1374 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
1375 if (*p == NULL)
1376 goto error;
1378 for (i = 0; i < topo.num_packages; i++)
1379 (*p)[i].package_id = i;
1381 return 0;
1382 error:
1383 ERROR("calloc counters");
1384 return -ERR_CALLOC;
1385 }
1386 /*
1387 * init_counter()
1388 *
1389 * set cpu_id, core_num, pkg_num
1390 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
1391 *
1392 * increment topo.num_cores when 1st core in pkg seen
1393 */
1394 static int
1395 init_counter(struct thread_data *thread_base, struct core_data *core_base,
1396 struct pkg_data *pkg_base, int thread_num, int core_num,
1397 int pkg_num, int cpu_id)
1398 {
1399 int ret;
1400 struct thread_data *t;
1401 struct core_data *c;
1402 struct pkg_data *p;
1404 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
1405 c = GET_CORE(core_base, core_num, pkg_num);
1406 p = GET_PKG(pkg_base, pkg_num);
1408 t->cpu_id = cpu_id;
1409 if (thread_num == 0) {
1410 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
1411 if ((ret = cpu_is_first_core_in_package(cpu_id)) < 0) {
1412 return ret;
1413 } else if (ret != 0) {
1414 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
1415 }
1416 }
1418 c->core_id = core_num;
1419 p->package_id = pkg_num;
1421 return 0;
1422 }
1425 static int
1426 initialize_counters(int cpu_id)
1427 {
1428 int my_thread_id, my_core_id, my_package_id;
1429 int ret;
1431 my_package_id = get_physical_package_id(cpu_id);
1432 if (my_package_id < 0)
1433 return my_package_id;
1434 my_core_id = get_core_id(cpu_id);
1435 if (my_core_id < 0)
1436 return my_core_id;
1438 if ((ret = cpu_is_first_sibling_in_core(cpu_id)) < 0) {
1439 return ret;
1440 } else if (ret != 0) {
1441 my_thread_id = 0;
1442 topo.num_cores++;
1443 } else {
1444 my_thread_id = 1;
1445 }
1447 ret = init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
1448 if (ret < 0)
1449 return ret;
1450 ret = init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
1451 if (ret < 0)
1452 return ret;
1453 return 0;
1454 }
1456 #define DO_OR_GOTO_ERR(something) \
1457 do { \
1458 ret = (something); \
1459 if (ret < 0) \
1460 goto err; \
1461 } while (0)
1463 static int setup_all_buffers(void)
1464 {
1465 int ret;
1467 DO_OR_GOTO_ERR(topology_probe());
1468 DO_OR_GOTO_ERR(allocate_counters(&thread_even, &core_even, &package_even));
1469 DO_OR_GOTO_ERR(allocate_counters(&thread_odd, &core_odd, &package_odd));
1470 DO_OR_GOTO_ERR(for_all_proc_cpus(initialize_counters));
1472 allocated = 1;
1473 return 0;
1474 err:
1475 free_all_buffers();
1476 return ret;
1477 }
1479 static int
1480 turbostat_init(void)
1481 {
1482 int ret;
1483 struct timespec ts;
1485 DO_OR_GOTO_ERR(check_cpuid());
1486 DO_OR_GOTO_ERR(check_dev_msr());
1487 DO_OR_GOTO_ERR(check_super_user());
1488 DO_OR_GOTO_ERR(setup_all_buffers());
1489 DO_OR_GOTO_ERR(for_all_cpus(set_temperature_target, EVEN_COUNTERS));
1491 ts.tv_sec = interval_sec;
1492 ts.tv_nsec = 0;
1494 plugin_register_complex_read(NULL, PLUGIN_NAME, turbostat_read, &ts, NULL);
1496 return 0;
1497 err:
1498 free_all_buffers();
1499 return ret;
1500 }
1502 static const char *config_keys[] =
1503 {
1504 "Interval",
1505 };
1506 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
1508 static int
1509 turbostat_config(const char *key, const char *value)
1510 {
1511 if (strcasecmp("Interval", key) == 0)
1512 interval_sec = atoi(value);
1513 else
1514 return -1;
1515 return 0;
1516 }
1518 void module_register(void);
1519 void module_register(void)
1520 {
1521 plugin_register_init(PLUGIN_NAME, turbostat_init);
1522 plugin_register_config(PLUGIN_NAME, turbostat_config, config_keys, config_keys_num);
1523 }