ca8551a999e8a1a5c335e2d0fec8e33127a8ab34
1 /*
2 * turbostat -- Log CPU frequency and C-state residency
3 * on modern Intel turbo-capable processors for collectd.
4 *
5 * Based on the 'turbostat' tool of the Linux kernel, found at
6 * linux/tools/power/x86/turbostat/turbostat.c:
7 * ----
8 * Copyright (c) 2013 Intel Corporation.
9 * Len Brown <len.brown@intel.com>
10 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms and conditions of the GNU General Public License,
13 * version 2, as published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * more details.
19 *
20 * You should have received a copy of the GNU General Public License along with
21 * this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
23 * ----
24 * Ported to collectd by Vincent Brillault <git@lerya.net>
25 */
27 /*
28 * _GNU_SOURCE is required because of the following functions:
29 * - CPU_ISSET_S
30 * - CPU_ZERO_S
31 * - CPU_SET_S
32 * - CPU_FREE
33 * - CPU_ALLOC
34 * - CPU_ALLOC_SIZE
35 */
36 #define _GNU_SOURCE
38 #include <asm/msr-index.h>
39 #include <stdarg.h>
40 #include <stdio.h>
41 #include <err.h>
42 #include <unistd.h>
43 #include <sys/types.h>
44 #include <sys/wait.h>
45 #include <sys/stat.h>
46 #include <sys/resource.h>
47 #include <fcntl.h>
48 #include <signal.h>
49 #include <sys/time.h>
50 #include <stdlib.h>
51 #include <dirent.h>
52 #include <string.h>
53 #include <ctype.h>
54 #include <sched.h>
55 #include <cpuid.h>
57 #include "collectd.h"
58 #include "common.h"
59 #include "plugin.h"
61 #define PLUGIN_NAME "turbostat"
63 static const char *proc_stat = "/proc/stat";
64 static unsigned int skip_c0;
65 static unsigned int skip_c1;
66 static unsigned int do_nhm_cstates;
67 static unsigned int do_snb_cstates;
68 static unsigned int do_c8_c9_c10;
69 static unsigned int do_slm_cstates;
70 static unsigned int has_aperf;
71 static unsigned int has_epb;
72 static unsigned int genuine_intel;
73 static unsigned int do_nehalem_platform_info;
74 static int do_smi;
75 static unsigned int do_rapl;
76 static unsigned int do_dts;
77 static unsigned int do_ptm;
78 static unsigned int tcc_activation_temp;
79 static unsigned int tcc_activation_temp_override;
80 static double rapl_power_units, rapl_energy_units, rapl_time_units;
81 static double rapl_joule_counter_range;
83 #define RAPL_PKG (1 << 0)
84 /* 0x610 MSR_PKG_POWER_LIMIT */
85 /* 0x611 MSR_PKG_ENERGY_STATUS */
86 #define RAPL_PKG_PERF_STATUS (1 << 1)
87 /* 0x613 MSR_PKG_PERF_STATUS */
88 #define RAPL_PKG_POWER_INFO (1 << 2)
89 /* 0x614 MSR_PKG_POWER_INFO */
91 #define RAPL_DRAM (1 << 3)
92 /* 0x618 MSR_DRAM_POWER_LIMIT */
93 /* 0x619 MSR_DRAM_ENERGY_STATUS */
94 /* 0x61c MSR_DRAM_POWER_INFO */
95 #define RAPL_DRAM_PERF_STATUS (1 << 4)
96 /* 0x61b MSR_DRAM_PERF_STATUS */
98 #define RAPL_CORES (1 << 5)
99 /* 0x638 MSR_PP0_POWER_LIMIT */
100 /* 0x639 MSR_PP0_ENERGY_STATUS */
101 #define RAPL_CORE_POLICY (1 << 6)
102 /* 0x63a MSR_PP0_POLICY */
105 #define RAPL_GFX (1 << 7)
106 /* 0x640 MSR_PP1_POWER_LIMIT */
107 /* 0x641 MSR_PP1_ENERGY_STATUS */
108 /* 0x642 MSR_PP1_POLICY */
109 #define TJMAX_DEFAULT 100
111 int aperf_mperf_unstable;
112 int backwards_count;
113 char *progname;
115 cpu_set_t *cpu_present_set, *cpu_affinity_set;
116 size_t cpu_present_setsize, cpu_affinity_setsize;
118 struct thread_data {
119 unsigned long long tsc;
120 unsigned long long aperf;
121 unsigned long long mperf;
122 unsigned long long c1;
123 unsigned int smi_count;
124 unsigned int cpu_id;
125 unsigned int flags;
126 #define CPU_IS_FIRST_THREAD_IN_CORE 0x2
127 #define CPU_IS_FIRST_CORE_IN_PACKAGE 0x4
128 } *thread_even, *thread_odd;
130 struct core_data {
131 unsigned long long c3;
132 unsigned long long c6;
133 unsigned long long c7;
134 unsigned int core_temp_c;
135 unsigned int core_id;
136 } *core_even, *core_odd;
138 struct pkg_data {
139 unsigned long long pc2;
140 unsigned long long pc3;
141 unsigned long long pc6;
142 unsigned long long pc7;
143 unsigned long long pc8;
144 unsigned long long pc9;
145 unsigned long long pc10;
146 unsigned int package_id;
147 unsigned int energy_pkg; /* MSR_PKG_ENERGY_STATUS */
148 unsigned int energy_dram; /* MSR_DRAM_ENERGY_STATUS */
149 unsigned int energy_cores; /* MSR_PP0_ENERGY_STATUS */
150 unsigned int energy_gfx; /* MSR_PP1_ENERGY_STATUS */
151 unsigned int rapl_pkg_perf_status; /* MSR_PKG_PERF_STATUS */
152 unsigned int rapl_dram_perf_status; /* MSR_DRAM_PERF_STATUS */
153 unsigned int pkg_temp_c;
155 } *package_even, *package_odd;
157 #define ODD_COUNTERS thread_odd, core_odd, package_odd
158 #define EVEN_COUNTERS thread_even, core_even, package_even
159 static _Bool is_even = 1;
161 static _Bool allocated = 0;
162 static _Bool initialized = 0;
164 #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
165 (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
166 topo.num_threads_per_core + \
167 (core_no) * topo.num_threads_per_core + (thread_no))
168 #define GET_CORE(core_base, core_no, pkg_no) \
169 (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
170 #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
172 struct topo_params {
173 int num_packages;
174 int num_cpus;
175 int num_cores;
176 int max_cpu_num;
177 int num_cores_per_pkg;
178 int num_threads_per_core;
179 } topo;
181 struct timeval tv_even, tv_odd, tv_delta;
183 enum return_values {
184 OK = 0,
185 ERR_CPU_MIGRATE,
186 ERR_MSR_IA32_APERF,
187 ERR_MSR_IA32_MPERF,
188 ERR_MSR_SMI_COUNT,
189 ERR_MSR_CORE_C3_RESIDENCY,
190 ERR_MSR_CORE_C6_RESIDENCY,
191 ERR_MSR_CORE_C7_RESIDENCY,
192 ERR_MSR_IA32_THERM_STATUS,
193 ERR_MSR_PKG_C3_RESIDENCY,
194 ERR_MSR_PKG_C6_RESIDENCY,
195 ERR_MSR_PKG_C2_RESIDENCY,
196 ERR_MSR_PKG_C7_RESIDENCY,
197 ERR_MSR_PKG_C8_RESIDENCY,
198 ERR_MSR_PKG_C9_RESIDENCY,
199 ERR_MSR_PKG_C10_RESIDENCY,
200 ERR_MSR_PKG_ENERGY_STATUS,
201 ERR_MSR_PP0_ENERGY_STATUS,
202 ERR_MSR_DRAM_ENERGY_STATUS,
203 ERR_MSR_PP1_ENERGY_STATUS,
204 ERR_MSR_PKG_PERF_STATUS,
205 ERR_MSR_DRAM_PERF_STATUS,
206 ERR_MSR_IA32_PACKAGE_THERM_STATUS,
207 ERR_CPU_NOT_PRESENT,
208 ERR_NO_MSR,
209 ERR_CANT_OPEN_FILE,
210 ERR_CANT_READ_NUMBER,
211 ERR_CANT_READ_PROC_STAT,
212 ERR_NO_INVARIANT_TSC,
213 ERR_NO_APERF,
214 ERR_CALLOC,
215 ERR_CPU_ALLOC,
216 ERR_NOT_ROOT,
217 };
219 static int setup_all_buffers(void);
221 static int
222 cpu_is_not_present(int cpu)
223 {
224 return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
225 }
226 /*
227 * run func(thread, core, package) in topology order
228 * skip non-present cpus
229 */
231 static int __attribute__((warn_unused_result))
232 for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
233 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
234 {
235 int retval, pkg_no, core_no, thread_no;
237 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
238 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
239 for (thread_no = 0; thread_no <
240 topo.num_threads_per_core; ++thread_no) {
241 struct thread_data *t;
242 struct core_data *c;
243 struct pkg_data *p;
245 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
247 if (cpu_is_not_present(t->cpu_id))
248 continue;
250 c = GET_CORE(core_base, core_no, pkg_no);
251 p = GET_PKG(pkg_base, pkg_no);
253 retval = func(t, c, p);
254 if (retval)
255 return retval;
256 }
257 }
258 }
259 return 0;
260 }
262 static int __attribute__((warn_unused_result))
263 cpu_migrate(int cpu)
264 {
265 CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
266 CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
267 if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
268 return -ERR_CPU_MIGRATE;
269 else
270 return 0;
271 }
273 static int __attribute__((warn_unused_result))
274 get_msr(int cpu, off_t offset, unsigned long long *msr)
275 {
276 ssize_t retval;
277 char pathname[32];
278 int fd;
280 ssnprintf(pathname, 32, "/dev/cpu/%d/msr", cpu);
281 fd = open(pathname, O_RDONLY);
282 if (fd < 0)
283 return -1;
285 retval = pread(fd, msr, sizeof *msr, offset);
286 close(fd);
288 if (retval != sizeof *msr) {
289 ERROR ("%s offset 0x%llx read failed\n", pathname, (unsigned long long)offset);
290 return -1;
291 }
293 return 0;
294 }
296 #define DELTA_WRAP32(new, old) \
297 if (new > old) { \
298 old = new - old; \
299 } else { \
300 old = 0x100000000 + new - old; \
301 }
303 static void
304 delta_package(struct pkg_data *new, struct pkg_data *old)
305 {
306 old->pc2 = new->pc2 - old->pc2;
307 old->pc3 = new->pc3 - old->pc3;
308 old->pc6 = new->pc6 - old->pc6;
309 old->pc7 = new->pc7 - old->pc7;
310 old->pc8 = new->pc8 - old->pc8;
311 old->pc9 = new->pc9 - old->pc9;
312 old->pc10 = new->pc10 - old->pc10;
313 old->pkg_temp_c = new->pkg_temp_c;
315 DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
316 DELTA_WRAP32(new->energy_cores, old->energy_cores);
317 DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
318 DELTA_WRAP32(new->energy_dram, old->energy_dram);
319 DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
320 DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
321 }
323 static void
324 delta_core(struct core_data *new, struct core_data *old)
325 {
326 old->c3 = new->c3 - old->c3;
327 old->c6 = new->c6 - old->c6;
328 old->c7 = new->c7 - old->c7;
329 old->core_temp_c = new->core_temp_c;
330 }
332 /*
333 * old = new - old
334 */
335 static int __attribute__((warn_unused_result))
336 delta_thread(struct thread_data *new, struct thread_data *old,
337 struct core_data *core_delta)
338 {
339 old->tsc = new->tsc - old->tsc;
341 /* check for TSC < 1 Mcycles over interval */
342 if (old->tsc < (1000 * 1000)) {
343 WARNING("Insanely slow TSC rate, TSC stops in idle?\n"
344 "You can disable all c-states by booting with \"idle=poll\"\n"
345 "or just the deep ones with \"processor.max_cstate=1\"");
346 return -1;
347 }
349 old->c1 = new->c1 - old->c1;
351 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
352 old->aperf = new->aperf - old->aperf;
353 old->mperf = new->mperf - old->mperf;
354 } else {
356 if (!aperf_mperf_unstable) {
357 WARNING("%s: APERF or MPERF went backwards *\n", progname);
358 WARNING("* Frequency results do not cover entire interval *\n");
359 WARNING("* fix this by running Linux-2.6.30 or later *\n");
361 aperf_mperf_unstable = 1;
362 }
363 /*
364 * mperf delta is likely a huge "positive" number
365 * can not use it for calculating c0 time
366 */
367 skip_c0 = 1;
368 skip_c1 = 1;
369 }
372 /*
373 * As counter collection is not atomic,
374 * it is possible for mperf's non-halted cycles + idle states
375 * to exceed TSC's all cycles: show c1 = 0% in that case.
376 */
377 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
378 old->c1 = 0;
379 else {
380 /* normal case, derive c1 */
381 old->c1 = old->tsc - old->mperf - core_delta->c3
382 - core_delta->c6 - core_delta->c7;
383 }
385 if (old->mperf == 0) {
386 WARNING("cpu%d MPERF 0!\n", old->cpu_id);
387 old->mperf = 1; /* divide by 0 protection */
388 }
390 if (do_smi)
391 old->smi_count = new->smi_count - old->smi_count;
393 return 0;
394 }
396 static int __attribute__((warn_unused_result))
397 delta_cpu(struct thread_data *t, struct core_data *c,
398 struct pkg_data *p, struct thread_data *t2,
399 struct core_data *c2, struct pkg_data *p2)
400 {
401 int ret;
403 /* calculate core delta only for 1st thread in core */
404 if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
405 delta_core(c, c2);
407 /* always calculate thread delta */
408 ret = delta_thread(t, t2, c2); /* c2 is core delta */
409 if (ret != 0)
410 return ret;
412 /* calculate package delta only for 1st core in package */
413 if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
414 delta_package(p, p2);
416 return 0;
417 }
419 static unsigned long long
420 rdtsc(void)
421 {
422 unsigned int low, high;
424 asm volatile("rdtsc" : "=a" (low), "=d" (high));
426 return low | ((unsigned long long)high) << 32;
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;
441 if (cpu_migrate(cpu)) {
442 WARNING("Could not migrate to CPU %d\n", cpu);
443 return -ERR_CPU_MIGRATE;
444 }
446 t->tsc = rdtsc(); /* we are running on local CPU of interest */
448 if (has_aperf) {
449 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
450 return -ERR_MSR_IA32_APERF;
451 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
452 return -ERR_MSR_IA32_MPERF;
453 }
455 if (do_smi) {
456 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
457 return -ERR_MSR_SMI_COUNT;
458 t->smi_count = msr & 0xFFFFFFFF;
459 }
461 /* collect core counters only for 1st thread in core */
462 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
463 return 0;
465 if (do_nhm_cstates && !do_slm_cstates) {
466 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
467 return -ERR_MSR_CORE_C3_RESIDENCY;
468 }
470 if (do_nhm_cstates) {
471 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
472 return -ERR_MSR_CORE_C6_RESIDENCY;
473 }
475 if (do_snb_cstates)
476 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
477 return -ERR_MSR_CORE_C7_RESIDENCY;
479 if (do_dts) {
480 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
481 return -ERR_MSR_IA32_THERM_STATUS;
482 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
483 }
486 /* collect package counters only for 1st core in package */
487 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
488 return 0;
490 if (do_nhm_cstates && !do_slm_cstates) {
491 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
492 return -ERR_MSR_PKG_C3_RESIDENCY;
493 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
494 return -ERR_MSR_PKG_C6_RESIDENCY;
495 }
496 if (do_snb_cstates) {
497 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
498 return -ERR_MSR_PKG_C2_RESIDENCY;
499 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
500 return -ERR_MSR_PKG_C7_RESIDENCY;
501 }
502 if (do_c8_c9_c10) {
503 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
504 return -ERR_MSR_PKG_C8_RESIDENCY;
505 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
506 return -ERR_MSR_PKG_C9_RESIDENCY;
507 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
508 return -ERR_MSR_PKG_C10_RESIDENCY;
509 }
510 if (do_rapl & RAPL_PKG) {
511 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
512 return -ERR_MSR_PKG_ENERGY_STATUS;
513 p->energy_pkg = msr & 0xFFFFFFFF;
514 }
515 if (do_rapl & RAPL_CORES) {
516 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
517 return MSR_PP0_ENERGY_STATUS;
518 p->energy_cores = msr & 0xFFFFFFFF;
519 }
520 if (do_rapl & RAPL_DRAM) {
521 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
522 return -ERR_MSR_DRAM_ENERGY_STATUS;
523 p->energy_dram = msr & 0xFFFFFFFF;
524 }
525 if (do_rapl & RAPL_GFX) {
526 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
527 return -ERR_MSR_PP1_ENERGY_STATUS;
528 p->energy_gfx = msr & 0xFFFFFFFF;
529 }
530 if (do_rapl & RAPL_PKG_PERF_STATUS) {
531 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
532 return -ERR_MSR_PKG_PERF_STATUS;
533 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
534 }
535 if (do_rapl & RAPL_DRAM_PERF_STATUS) {
536 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
537 return -ERR_MSR_DRAM_PERF_STATUS;
538 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
539 }
540 if (do_ptm) {
541 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
542 return -ERR_MSR_IA32_PACKAGE_THERM_STATUS;
543 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
544 }
545 return 0;
546 }
548 static void
549 free_all_buffers(void)
550 {
551 allocated = 0;
552 initialized = 0;
554 CPU_FREE(cpu_present_set);
555 cpu_present_set = NULL;
556 cpu_present_set = 0;
558 CPU_FREE(cpu_affinity_set);
559 cpu_affinity_set = NULL;
560 cpu_affinity_setsize = 0;
562 free(thread_even);
563 free(core_even);
564 free(package_even);
566 thread_even = NULL;
567 core_even = NULL;
568 package_even = NULL;
570 free(thread_odd);
571 free(core_odd);
572 free(package_odd);
574 thread_odd = NULL;
575 core_odd = NULL;
576 package_odd = 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, 80, "/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 /*
669 * run func(thread, core, package) in topology order
670 * skip non-present cpus
671 */
674 static int __attribute__((warn_unused_result))
675 for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
676 struct pkg_data *, struct thread_data *, struct core_data *,
677 struct pkg_data *), struct thread_data *thread_base,
678 struct core_data *core_base, struct pkg_data *pkg_base,
679 struct thread_data *thread_base2, struct core_data *core_base2,
680 struct pkg_data *pkg_base2)
681 {
682 int retval, pkg_no, core_no, thread_no;
684 for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
685 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
686 for (thread_no = 0; thread_no <
687 topo.num_threads_per_core; ++thread_no) {
688 struct thread_data *t, *t2;
689 struct core_data *c, *c2;
690 struct pkg_data *p, *p2;
692 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
694 if (cpu_is_not_present(t->cpu_id))
695 continue;
697 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
699 c = GET_CORE(core_base, core_no, pkg_no);
700 c2 = GET_CORE(core_base2, core_no, pkg_no);
702 p = GET_PKG(pkg_base, pkg_no);
703 p2 = GET_PKG(pkg_base2, pkg_no);
705 retval = func(t, c, p, t2, c2, p2);
706 if (retval)
707 return retval;
708 }
709 }
710 }
711 return 0;
712 }
714 /*
715 * run func(cpu) on every cpu in /proc/stat
716 * return max_cpu number
717 */
718 static int __attribute__((warn_unused_result))
719 for_all_proc_cpus(int (func)(int))
720 {
721 FILE *fp;
722 int cpu_num;
723 int retval;
725 fp = fopen(proc_stat, "r");
726 if (!fp) {
727 ERROR("%s: open failed", proc_stat);
728 return -ERR_CANT_OPEN_FILE;
729 }
731 retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
732 if (retval != 0) {
733 ERROR("%s: failed to parse format", proc_stat);
734 return -ERR_CANT_READ_PROC_STAT;
735 }
737 while (1) {
738 retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
739 if (retval != 1)
740 break;
742 retval = func(cpu_num);
743 if (retval) {
744 fclose(fp);
745 return(retval);
746 }
747 }
748 fclose(fp);
749 return 0;
750 }
752 /*
753 * count_cpus()
754 * remember the last one seen, it will be the max
755 */
756 static int
757 count_cpus(int cpu)
758 {
759 if (topo.max_cpu_num < cpu)
760 topo.max_cpu_num = cpu;
762 topo.num_cpus += 1;
763 return 0;
764 }
765 static int
766 mark_cpu_present(int cpu)
767 {
768 CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
769 return 0;
770 }
773 static void
774 turbostat_submit (const char *plugin_instance,
775 const char *type, const char *type_instance,
776 gauge_t value)
777 {
778 value_list_t vl = VALUE_LIST_INIT;
779 value_t v;
781 v.gauge = value;
782 vl.values = &v;
783 vl.values_len = 1;
784 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
785 sstrncpy (vl.plugin, PLUGIN_NAME, sizeof (vl.plugin));
786 if (plugin_instance != NULL)
787 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
788 sstrncpy (vl.type, type, sizeof (vl.type));
789 if (type_instance != NULL)
790 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
792 plugin_dispatch_values (&vl);
793 }
795 /*
796 * column formatting convention & formats
797 * package: "pk" 2 columns %2d
798 * core: "cor" 3 columns %3d
799 * CPU: "CPU" 3 columns %3d
800 * Pkg_W: %6.2
801 * Cor_W: %6.2
802 * GFX_W: %5.2
803 * RAM_W: %5.2
804 * GHz: "GHz" 3 columns %3.2
805 * TSC: "TSC" 3 columns %3.2
806 * SMI: "SMI" 4 columns %4d
807 * percentage " %pc3" %6.2
808 * Perf Status percentage: %5.2
809 * "CTMP" 4 columns %4d
810 */
811 #define NAME_LEN 12
812 static int
813 submit_counters(struct thread_data *t, struct core_data *c,
814 struct pkg_data *p)
815 {
816 char name[NAME_LEN];
817 double interval_float;
819 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
821 snprintf(name, NAME_LEN, "cpu%02d", t->cpu_id);
823 if (do_nhm_cstates) {
824 if (!skip_c0)
825 turbostat_submit(name, "percent", "c0", 100.0 * t->mperf/t->tsc);
826 if (!skip_c1)
827 turbostat_submit(name, "percent", "c1", 100.0 * t->c1/t->tsc);
828 }
830 /* GHz */
831 if (has_aperf && ((!aperf_mperf_unstable) || (!(t->aperf > t->tsc || t->mperf > t->tsc))))
832 turbostat_submit(NULL, "frequency", name, 1.0 * t->tsc / 1000000000 * t->aperf / t->mperf / interval_float);
834 /* SMI */
835 if (do_smi)
836 turbostat_submit(NULL, "current", name, t->smi_count);
838 /* print per-core data only for 1st thread in core */
839 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
840 goto done;
842 snprintf(name, NAME_LEN, "core%02d", c->core_id);
844 if (do_nhm_cstates && !do_slm_cstates)
845 turbostat_submit(name, "percent", "c3", 100.0 * c->c3/t->tsc);
846 if (do_nhm_cstates)
847 turbostat_submit(name, "percent", "c6", 100.0 * c->c6/t->tsc);
848 if (do_snb_cstates)
849 turbostat_submit(name, "percent", "c7", 100.0 * c->c7/t->tsc);
851 if (do_dts)
852 turbostat_submit(NULL, "temperature", name, c->core_temp_c);
854 /* print per-package data only for 1st core in package */
855 if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
856 goto done;
858 snprintf(name, NAME_LEN, "pkg%02d", p->package_id);
860 if (do_ptm)
861 turbostat_submit(NULL, "temperature", name, p->pkg_temp_c);
863 if (do_snb_cstates)
864 turbostat_submit(name, "percent", "pc2", 100.0 * p->pc2/t->tsc);
865 if (do_nhm_cstates && !do_slm_cstates)
866 turbostat_submit(name, "percent", "pc3", 100.0 * p->pc3/t->tsc);
867 if (do_nhm_cstates && !do_slm_cstates)
868 turbostat_submit(name, "percent", "pc6", 100.0 * p->pc6/t->tsc);
869 if (do_snb_cstates)
870 turbostat_submit(name, "percent", "pc7", 100.0 * p->pc7/t->tsc);
871 if (do_c8_c9_c10) {
872 turbostat_submit(name, "percent", "pc8", 100.0 * p->pc8/t->tsc);
873 turbostat_submit(name, "percent", "pc9", 100.0 * p->pc9/t->tsc);
874 turbostat_submit(name, "percent", "pc10", 100.0 * p->pc10/t->tsc);
875 }
877 if (do_rapl) {
878 if (do_rapl & RAPL_PKG)
879 turbostat_submit(name, "power", "Pkg_W", p->energy_pkg * rapl_energy_units / interval_float);
880 if (do_rapl & RAPL_CORES)
881 turbostat_submit(name, "power", "Cor_W", p->energy_cores * rapl_energy_units / interval_float);
882 if (do_rapl & RAPL_GFX)
883 turbostat_submit(name, "power", "GFX_W", p->energy_gfx * rapl_energy_units / interval_float);
884 if (do_rapl & RAPL_DRAM)
885 turbostat_submit(name, "power", "RAM_W", p->energy_dram * rapl_energy_units / interval_float);
886 }
887 done:
888 return 0;
889 }
891 static int
892 turbostat_read(user_data_t * not_used)
893 {
894 int ret;
896 if (!allocated) {
897 if ((ret = setup_all_buffers()) < 0)
898 return ret;
899 }
901 if (for_all_proc_cpus(cpu_is_not_present)) {
902 free_all_buffers();
903 if ((ret = setup_all_buffers()) < 0)
904 return ret;
905 if (for_all_proc_cpus(cpu_is_not_present))
906 return -ERR_CPU_NOT_PRESENT;
907 }
909 if (!initialized) {
910 if ((ret = for_all_cpus(get_counters, EVEN_COUNTERS)) < 0)
911 return ret;
912 gettimeofday(&tv_even, (struct timezone *)NULL);
913 is_even = 1;
914 initialized = 1;
915 return 0;
916 }
918 if (is_even) {
919 if ((ret = for_all_cpus(get_counters, ODD_COUNTERS)) < 0)
920 return ret;
921 gettimeofday(&tv_odd, (struct timezone *)NULL);
922 is_even = 0;
923 timersub(&tv_odd, &tv_even, &tv_delta);
924 if ((ret = for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) < 0)
925 return ret;
926 if ((ret = for_all_cpus(submit_counters, EVEN_COUNTERS)) < 0)
927 return ret;
928 } else {
929 if ((ret = for_all_cpus(get_counters, EVEN_COUNTERS)) < 0)
930 return ret;
931 gettimeofday(&tv_even, (struct timezone *)NULL);
932 is_even = 1;
933 timersub(&tv_even, &tv_odd, &tv_delta);
934 if ((ret = for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) < 0)
935 return ret;
936 if ((ret = for_all_cpus(submit_counters, ODD_COUNTERS)) < 0)
937 return ret;
938 }
939 return 0;
940 }
942 static int __attribute__((warn_unused_result))
943 check_dev_msr()
944 {
945 struct stat sb;
947 if (stat("/dev/cpu/0/msr", &sb)) {
948 ERROR("no /dev/cpu/0/msr\n"
949 "Try \"# modprobe msr\"");
950 return -ERR_NO_MSR;
951 }
952 return 0;
953 }
955 static int __attribute__((warn_unused_result))
956 check_super_user()
957 {
958 if (getuid() != 0) {
959 ERROR("must be root");
960 return -ERR_NOT_ROOT;
961 }
962 return 0;
963 }
966 #define RAPL_POWER_GRANULARITY 0x7FFF /* 15 bit power granularity */
967 #define RAPL_TIME_GRANULARITY 0x3F /* 6 bit time granularity */
969 static double
970 get_tdp(unsigned int model)
971 {
972 unsigned long long msr;
974 if (do_rapl & RAPL_PKG_POWER_INFO)
975 if (!get_msr(0, MSR_PKG_POWER_INFO, &msr))
976 return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
978 switch (model) {
979 case 0x37:
980 case 0x4D:
981 return 30.0;
982 default:
983 return 135.0;
984 }
985 }
988 /*
989 * rapl_probe()
990 *
991 * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
992 */
993 static void
994 rapl_probe(unsigned int family, unsigned int model)
995 {
996 unsigned long long msr;
997 unsigned int time_unit;
998 double tdp;
1000 if (!genuine_intel)
1001 return;
1003 if (family != 6)
1004 return;
1006 switch (model) {
1007 case 0x2A:
1008 case 0x3A:
1009 case 0x3C: /* HSW */
1010 case 0x45: /* HSW */
1011 case 0x46: /* HSW */
1012 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
1013 break;
1014 case 0x3F: /* HSX */
1015 do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
1016 break;
1017 case 0x2D:
1018 case 0x3E:
1019 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_PKG_PERF_STATUS | RAPL_DRAM_PERF_STATUS | RAPL_PKG_POWER_INFO;
1020 break;
1021 case 0x37: /* BYT */
1022 case 0x4D: /* AVN */
1023 do_rapl = RAPL_PKG | RAPL_CORES ;
1024 break;
1025 default:
1026 return;
1027 }
1029 /* units on package 0, verify later other packages match */
1030 if (get_msr(0, MSR_RAPL_POWER_UNIT, &msr))
1031 return;
1033 rapl_power_units = 1.0 / (1 << (msr & 0xF));
1034 if (model == 0x37)
1035 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
1036 else
1037 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
1039 time_unit = msr >> 16 & 0xF;
1040 if (time_unit == 0)
1041 time_unit = 0xA;
1043 rapl_time_units = 1.0 / (1 << (time_unit));
1045 tdp = get_tdp(model);
1047 rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
1048 // if (verbose)
1049 // fprintf(stderr, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
1051 return;
1052 }
1054 static int
1055 is_snb(unsigned int family, unsigned int model)
1056 {
1057 if (!genuine_intel)
1058 return 0;
1060 switch (model) {
1061 case 0x2A:
1062 case 0x2D:
1063 case 0x3A: /* IVB */
1064 case 0x3E: /* IVB Xeon */
1065 case 0x3C: /* HSW */
1066 case 0x3F: /* HSW */
1067 case 0x45: /* HSW */
1068 case 0x46: /* HSW */
1069 return 1;
1070 }
1071 return 0;
1072 }
1074 static int
1075 has_c8_c9_c10(unsigned int family, unsigned int model)
1076 {
1077 if (!genuine_intel)
1078 return 0;
1080 switch (model) {
1081 case 0x45:
1082 return 1;
1083 }
1084 return 0;
1085 }
1088 static int
1089 is_slm(unsigned int family, unsigned int model)
1090 {
1091 if (!genuine_intel)
1092 return 0;
1093 switch (model) {
1094 case 0x37: /* BYT */
1095 case 0x4D: /* AVN */
1096 return 1;
1097 }
1098 return 0;
1099 }
1101 /*
1102 * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
1103 * the Thermal Control Circuit (TCC) activates.
1104 * This is usually equal to tjMax.
1105 *
1106 * Older processors do not have this MSR, so there we guess,
1107 * but also allow cmdline over-ride with -T.
1108 *
1109 * Several MSR temperature values are in units of degrees-C
1110 * below this value, including the Digital Thermal Sensor (DTS),
1111 * Package Thermal Management Sensor (PTM), and thermal event thresholds.
1112 */
1113 static int __attribute__((warn_unused_result))
1114 set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1115 {
1116 unsigned long long msr;
1117 unsigned int target_c_local;
1118 int cpu;
1120 /* tcc_activation_temp is used only for dts or ptm */
1121 if (!(do_dts || do_ptm))
1122 return 0;
1124 /* this is a per-package concept */
1125 if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1126 return 0;
1128 cpu = t->cpu_id;
1129 if (cpu_migrate(cpu)) {
1130 ERROR("Could not migrate to CPU %d\n", cpu);
1131 return -ERR_CPU_MIGRATE;
1132 }
1134 if (tcc_activation_temp_override != 0) {
1135 tcc_activation_temp = tcc_activation_temp_override;
1136 ERROR("cpu%d: Using cmdline TCC Target (%d C)\n",
1137 cpu, tcc_activation_temp);
1138 return 0;
1139 }
1141 /* Temperature Target MSR is Nehalem and newer only */
1142 if (!do_nehalem_platform_info)
1143 goto guess;
1145 if (get_msr(0, MSR_IA32_TEMPERATURE_TARGET, &msr))
1146 goto guess;
1148 target_c_local = (msr >> 16) & 0x7F;
1150 if (target_c_local < 85 || target_c_local > 127)
1151 goto guess;
1153 tcc_activation_temp = target_c_local;
1155 return 0;
1157 guess:
1158 tcc_activation_temp = TJMAX_DEFAULT;
1159 WARNING("cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
1160 cpu, tcc_activation_temp);
1162 return 0;
1163 }
1165 static int __attribute__((warn_unused_result))
1166 check_cpuid()
1167 {
1168 unsigned int eax, ebx, ecx, edx, max_level;
1169 unsigned int fms, family, model;
1171 eax = ebx = ecx = edx = 0;
1173 __get_cpuid(0, &max_level, &ebx, &ecx, &edx);
1175 if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
1176 genuine_intel = 1;
1178 fms = 0;
1179 __get_cpuid(1, &fms, &ebx, &ecx, &edx);
1180 family = (fms >> 8) & 0xf;
1181 model = (fms >> 4) & 0xf;
1182 if (family == 6 || family == 0xf)
1183 model += ((fms >> 16) & 0xf) << 4;
1185 if (!(edx & (1 << 5))) {
1186 ERROR("CPUID: no MSR");
1187 return -ERR_NO_MSR;
1188 }
1190 /*
1191 * check max extended function levels of CPUID.
1192 * This is needed to check for invariant TSC.
1193 * This check is valid for both Intel and AMD.
1194 */
1195 ebx = ecx = edx = 0;
1196 __get_cpuid(0x80000000, &max_level, &ebx, &ecx, &edx);
1198 if (max_level < 0x80000007) {
1199 ERROR("CPUID: no invariant TSC (max_level 0x%x)", max_level);
1200 return -ERR_NO_INVARIANT_TSC;
1201 }
1203 /*
1204 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
1205 * this check is valid for both Intel and AMD
1206 */
1207 __get_cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
1208 if (!(edx & (1 << 8))) {
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;
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 __attribute__((warn_unused_result))
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;
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);
1337 topo.num_packages = max_package_id + 1;
1338 DEBUG("max_package_id %d, sizing for %d packages\n",
1339 max_package_id, topo.num_packages);
1341 topo.num_threads_per_core = max_siblings;
1342 DEBUG("max_siblings %d\n", max_siblings);
1344 free(cpus);
1345 return 0;
1346 }
1348 static int
1349 allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
1350 {
1351 int i;
1353 *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
1354 topo.num_packages, sizeof(struct thread_data));
1355 if (*t == NULL)
1356 goto error;
1358 for (i = 0; i < topo.num_threads_per_core *
1359 topo.num_cores_per_pkg * topo.num_packages; i++)
1360 (*t)[i].cpu_id = -1;
1362 *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
1363 sizeof(struct core_data));
1364 if (*c == NULL)
1365 goto error;
1367 for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
1368 (*c)[i].core_id = -1;
1370 *p = calloc(topo.num_packages, sizeof(struct pkg_data));
1371 if (*p == NULL)
1372 goto error;
1374 for (i = 0; i < topo.num_packages; i++)
1375 (*p)[i].package_id = i;
1377 return 0;
1378 error:
1379 ERROR("calloc counters");
1380 return -ERR_CALLOC;
1381 }
1382 /*
1383 * init_counter()
1384 *
1385 * set cpu_id, core_num, pkg_num
1386 * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
1387 *
1388 * increment topo.num_cores when 1st core in pkg seen
1389 */
1390 static int
1391 init_counter(struct thread_data *thread_base, struct core_data *core_base,
1392 struct pkg_data *pkg_base, int thread_num, int core_num,
1393 int pkg_num, int cpu_id)
1394 {
1395 int ret;
1396 struct thread_data *t;
1397 struct core_data *c;
1398 struct pkg_data *p;
1400 t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
1401 c = GET_CORE(core_base, core_num, pkg_num);
1402 p = GET_PKG(pkg_base, pkg_num);
1404 t->cpu_id = cpu_id;
1405 if (thread_num == 0) {
1406 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
1407 if ((ret = cpu_is_first_core_in_package(cpu_id)) < 0) {
1408 return ret;
1409 } else if (ret != 0) {
1410 t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
1411 }
1412 }
1414 c->core_id = core_num;
1415 p->package_id = pkg_num;
1417 return 0;
1418 }
1421 static int
1422 initialize_counters(int cpu_id)
1423 {
1424 int my_thread_id, my_core_id, my_package_id;
1425 int ret;
1427 my_package_id = get_physical_package_id(cpu_id);
1428 if (my_package_id < 0)
1429 return my_package_id;
1430 my_core_id = get_core_id(cpu_id);
1431 if (my_core_id < 0)
1432 return my_core_id;
1434 if ((ret = cpu_is_first_sibling_in_core(cpu_id)) < 0) {
1435 return ret;
1436 } else if (ret != 0) {
1437 my_thread_id = 0;
1438 topo.num_cores++;
1439 } else {
1440 my_thread_id = 1;
1441 }
1443 ret = init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
1444 if (ret < 0)
1445 return ret;
1446 ret = init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
1447 if (ret < 0)
1448 return ret;
1449 return 0;
1450 }
1452 #define DO_OR_GOTO_ERR(something) \
1453 do { \
1454 ret = (something); \
1455 if (ret < 0) \
1456 goto err; \
1457 } while (0)
1459 static int setup_all_buffers(void)
1460 {
1461 int ret;
1463 DO_OR_GOTO_ERR(topology_probe());
1464 DO_OR_GOTO_ERR(allocate_counters(&thread_even, &core_even, &package_even));
1465 DO_OR_GOTO_ERR(allocate_counters(&thread_odd, &core_odd, &package_odd));
1466 DO_OR_GOTO_ERR(for_all_proc_cpus(initialize_counters));
1468 allocated = 1;
1469 return 0;
1470 err:
1471 free_all_buffers();
1472 return ret;
1473 }
1475 static int
1476 turbostat_init(void)
1477 {
1478 int ret;
1480 DO_OR_GOTO_ERR(check_cpuid());
1481 DO_OR_GOTO_ERR(check_dev_msr());
1482 DO_OR_GOTO_ERR(check_super_user());
1483 DO_OR_GOTO_ERR(setup_all_buffers());
1484 DO_OR_GOTO_ERR(for_all_cpus(set_temperature_target, EVEN_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 void module_register(void);
1495 void module_register(void)
1496 {
1497 plugin_register_init(PLUGIN_NAME, turbostat_init);
1498 }