]> git.karo-electronics.de Git - karo-tx-linux.git/blob - tools/power/x86/turbostat/turbostat.c
tools/power turbostat: enable turbostat to support Knights Landing (KNL)
[karo-tx-linux.git] / tools / power / x86 / turbostat / turbostat.c
1 /*
2  * turbostat -- show CPU frequency and C-state residency
3  * on modern Intel turbo-capable processors.
4  *
5  * Copyright (c) 2013 Intel Corporation.
6  * Len Brown <len.brown@intel.com>
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms and conditions of the GNU General Public License,
10  * version 2, as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #define _GNU_SOURCE
23 #include MSRHEADER
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <err.h>
27 #include <unistd.h>
28 #include <sys/types.h>
29 #include <sys/wait.h>
30 #include <sys/stat.h>
31 #include <sys/resource.h>
32 #include <fcntl.h>
33 #include <signal.h>
34 #include <sys/time.h>
35 #include <stdlib.h>
36 #include <getopt.h>
37 #include <dirent.h>
38 #include <string.h>
39 #include <ctype.h>
40 #include <sched.h>
41 #include <cpuid.h>
42 #include <linux/capability.h>
43 #include <errno.h>
44
45 char *proc_stat = "/proc/stat";
46 unsigned int interval_sec = 5;
47 unsigned int debug;
48 unsigned int rapl_joules;
49 unsigned int summary_only;
50 unsigned int dump_only;
51 unsigned int skip_c0;
52 unsigned int skip_c1;
53 unsigned int do_nhm_cstates;
54 unsigned int do_snb_cstates;
55 unsigned int do_knl_cstates;
56 unsigned int do_pc2;
57 unsigned int do_pc3;
58 unsigned int do_pc6;
59 unsigned int do_pc7;
60 unsigned int do_c8_c9_c10;
61 unsigned int do_skl_residency;
62 unsigned int do_slm_cstates;
63 unsigned int use_c1_residency_msr;
64 unsigned int has_aperf;
65 unsigned int has_epb;
66 unsigned int units = 1000000;   /* MHz etc */
67 unsigned int genuine_intel;
68 unsigned int has_invariant_tsc;
69 unsigned int do_nhm_platform_info;
70 unsigned int extra_msr_offset32;
71 unsigned int extra_msr_offset64;
72 unsigned int extra_delta_offset32;
73 unsigned int extra_delta_offset64;
74 int do_smi;
75 double bclk;
76 unsigned int show_pkg;
77 unsigned int show_core;
78 unsigned int show_cpu;
79 unsigned int show_pkg_only;
80 unsigned int show_core_only;
81 char *output_buffer, *outp;
82 unsigned int do_rapl;
83 unsigned int do_dts;
84 unsigned int do_ptm;
85 unsigned int tcc_activation_temp;
86 unsigned int tcc_activation_temp_override;
87 double rapl_power_units, rapl_time_units;
88 double rapl_dram_energy_units, rapl_energy_units;
89 double rapl_joule_counter_range;
90 unsigned int do_core_perf_limit_reasons;
91 unsigned int do_gfx_perf_limit_reasons;
92 unsigned int do_ring_perf_limit_reasons;
93 unsigned int crystal_hz;
94 unsigned long long tsc_hz;
95
96 #define RAPL_PKG                (1 << 0)
97                                         /* 0x610 MSR_PKG_POWER_LIMIT */
98                                         /* 0x611 MSR_PKG_ENERGY_STATUS */
99 #define RAPL_PKG_PERF_STATUS    (1 << 1)
100                                         /* 0x613 MSR_PKG_PERF_STATUS */
101 #define RAPL_PKG_POWER_INFO     (1 << 2)
102                                         /* 0x614 MSR_PKG_POWER_INFO */
103
104 #define RAPL_DRAM               (1 << 3)
105                                         /* 0x618 MSR_DRAM_POWER_LIMIT */
106                                         /* 0x619 MSR_DRAM_ENERGY_STATUS */
107 #define RAPL_DRAM_PERF_STATUS   (1 << 4)
108                                         /* 0x61b MSR_DRAM_PERF_STATUS */
109 #define RAPL_DRAM_POWER_INFO    (1 << 5)
110                                         /* 0x61c MSR_DRAM_POWER_INFO */
111
112 #define RAPL_CORES              (1 << 6)
113                                         /* 0x638 MSR_PP0_POWER_LIMIT */
114                                         /* 0x639 MSR_PP0_ENERGY_STATUS */
115 #define RAPL_CORE_POLICY        (1 << 7)
116                                         /* 0x63a MSR_PP0_POLICY */
117
118 #define RAPL_GFX                (1 << 8)
119                                         /* 0x640 MSR_PP1_POWER_LIMIT */
120                                         /* 0x641 MSR_PP1_ENERGY_STATUS */
121                                         /* 0x642 MSR_PP1_POLICY */
122 #define TJMAX_DEFAULT   100
123
124 #define MAX(a, b) ((a) > (b) ? (a) : (b))
125
126 int aperf_mperf_unstable;
127 int backwards_count;
128 char *progname;
129
130 cpu_set_t *cpu_present_set, *cpu_affinity_set;
131 size_t cpu_present_setsize, cpu_affinity_setsize;
132
133 struct thread_data {
134         unsigned long long tsc;
135         unsigned long long aperf;
136         unsigned long long mperf;
137         unsigned long long c1;
138         unsigned long long extra_msr64;
139         unsigned long long extra_delta64;
140         unsigned long long extra_msr32;
141         unsigned long long extra_delta32;
142         unsigned int smi_count;
143         unsigned int cpu_id;
144         unsigned int flags;
145 #define CPU_IS_FIRST_THREAD_IN_CORE     0x2
146 #define CPU_IS_FIRST_CORE_IN_PACKAGE    0x4
147 } *thread_even, *thread_odd;
148
149 struct core_data {
150         unsigned long long c3;
151         unsigned long long c6;
152         unsigned long long c7;
153         unsigned int core_temp_c;
154         unsigned int core_id;
155 } *core_even, *core_odd;
156
157 struct pkg_data {
158         unsigned long long pc2;
159         unsigned long long pc3;
160         unsigned long long pc6;
161         unsigned long long pc7;
162         unsigned long long pc8;
163         unsigned long long pc9;
164         unsigned long long pc10;
165         unsigned long long pkg_wtd_core_c0;
166         unsigned long long pkg_any_core_c0;
167         unsigned long long pkg_any_gfxe_c0;
168         unsigned long long pkg_both_core_gfxe_c0;
169         unsigned int package_id;
170         unsigned int energy_pkg;        /* MSR_PKG_ENERGY_STATUS */
171         unsigned int energy_dram;       /* MSR_DRAM_ENERGY_STATUS */
172         unsigned int energy_cores;      /* MSR_PP0_ENERGY_STATUS */
173         unsigned int energy_gfx;        /* MSR_PP1_ENERGY_STATUS */
174         unsigned int rapl_pkg_perf_status;      /* MSR_PKG_PERF_STATUS */
175         unsigned int rapl_dram_perf_status;     /* MSR_DRAM_PERF_STATUS */
176         unsigned int pkg_temp_c;
177
178 } *package_even, *package_odd;
179
180 #define ODD_COUNTERS thread_odd, core_odd, package_odd
181 #define EVEN_COUNTERS thread_even, core_even, package_even
182
183 #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
184         (thread_base + (pkg_no) * topo.num_cores_per_pkg * \
185                 topo.num_threads_per_core + \
186                 (core_no) * topo.num_threads_per_core + (thread_no))
187 #define GET_CORE(core_base, core_no, pkg_no) \
188         (core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
189 #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
190
191 struct system_summary {
192         struct thread_data threads;
193         struct core_data cores;
194         struct pkg_data packages;
195 } sum, average;
196
197
198 struct topo_params {
199         int num_packages;
200         int num_cpus;
201         int num_cores;
202         int max_cpu_num;
203         int num_cores_per_pkg;
204         int num_threads_per_core;
205 } topo;
206
207 struct timeval tv_even, tv_odd, tv_delta;
208
209 void setup_all_buffers(void);
210
211 int cpu_is_not_present(int cpu)
212 {
213         return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
214 }
215 /*
216  * run func(thread, core, package) in topology order
217  * skip non-present cpus
218  */
219
220 int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
221         struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
222 {
223         int retval, pkg_no, core_no, thread_no;
224
225         for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
226                 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
227                         for (thread_no = 0; thread_no <
228                                 topo.num_threads_per_core; ++thread_no) {
229                                 struct thread_data *t;
230                                 struct core_data *c;
231                                 struct pkg_data *p;
232
233                                 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
234
235                                 if (cpu_is_not_present(t->cpu_id))
236                                         continue;
237
238                                 c = GET_CORE(core_base, core_no, pkg_no);
239                                 p = GET_PKG(pkg_base, pkg_no);
240
241                                 retval = func(t, c, p);
242                                 if (retval)
243                                         return retval;
244                         }
245                 }
246         }
247         return 0;
248 }
249
250 int cpu_migrate(int cpu)
251 {
252         CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
253         CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
254         if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
255                 return -1;
256         else
257                 return 0;
258 }
259
260 int get_msr(int cpu, off_t offset, unsigned long long *msr)
261 {
262         ssize_t retval;
263         char pathname[32];
264         int fd;
265
266         sprintf(pathname, "/dev/cpu/%d/msr", cpu);
267         fd = open(pathname, O_RDONLY);
268         if (fd < 0)
269                 err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname);
270
271         retval = pread(fd, msr, sizeof *msr, offset);
272         close(fd);
273
274         if (retval != sizeof *msr)
275                 err(-1, "%s offset 0x%llx read failed", pathname, (unsigned long long)offset);
276
277         return 0;
278 }
279
280 /*
281  * Example Format w/ field column widths:
282  *
283  *  Package    Core     CPU Avg_MHz Bzy_MHz TSC_MHz     SMI   %Busy CPU_%c1 CPU_%c3 CPU_%c6 CPU_%c7 CoreTmp  PkgTmp Pkg%pc2 Pkg%pc3 Pkg%pc6 Pkg%pc7 PkgWatt CorWatt GFXWatt
284  * 123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678
285  */
286
287 void print_header(void)
288 {
289         if (show_pkg)
290                 outp += sprintf(outp, " Package");
291         if (show_core)
292                 outp += sprintf(outp, "    Core");
293         if (show_cpu)
294                 outp += sprintf(outp, "     CPU");
295         if (has_aperf)
296                 outp += sprintf(outp, " Avg_MHz");
297         if (has_aperf)
298                 outp += sprintf(outp, "   %%Busy");
299         if (has_aperf)
300                 outp += sprintf(outp, " Bzy_MHz");
301         outp += sprintf(outp, " TSC_MHz");
302
303         if (extra_delta_offset32)
304                 outp += sprintf(outp, "  count 0x%03X", extra_delta_offset32);
305         if (extra_delta_offset64)
306                 outp += sprintf(outp, "  COUNT 0x%03X", extra_delta_offset64);
307         if (extra_msr_offset32)
308                 outp += sprintf(outp, "   MSR 0x%03X", extra_msr_offset32);
309         if (extra_msr_offset64)
310                 outp += sprintf(outp, "           MSR 0x%03X", extra_msr_offset64);
311
312         if (!debug)
313                 goto done;
314
315         if (do_smi)
316                 outp += sprintf(outp, "     SMI");
317
318         if (do_nhm_cstates)
319                 outp += sprintf(outp, "  CPU%%c1");
320         if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates)
321                 outp += sprintf(outp, "  CPU%%c3");
322         if (do_nhm_cstates)
323                 outp += sprintf(outp, "  CPU%%c6");
324         if (do_snb_cstates)
325                 outp += sprintf(outp, "  CPU%%c7");
326
327         if (do_dts)
328                 outp += sprintf(outp, " CoreTmp");
329         if (do_ptm)
330                 outp += sprintf(outp, "  PkgTmp");
331
332         if (do_skl_residency) {
333                 outp += sprintf(outp, " Totl%%C0");
334                 outp += sprintf(outp, "  Any%%C0");
335                 outp += sprintf(outp, "  GFX%%C0");
336                 outp += sprintf(outp, " CPUGFX%%");
337         }
338
339         if (do_pc2)
340                 outp += sprintf(outp, " Pkg%%pc2");
341         if (do_pc3)
342                 outp += sprintf(outp, " Pkg%%pc3");
343         if (do_pc6)
344                 outp += sprintf(outp, " Pkg%%pc6");
345         if (do_pc7)
346                 outp += sprintf(outp, " Pkg%%pc7");
347         if (do_c8_c9_c10) {
348                 outp += sprintf(outp, " Pkg%%pc8");
349                 outp += sprintf(outp, " Pkg%%pc9");
350                 outp += sprintf(outp, " Pk%%pc10");
351         }
352
353         if (do_rapl && !rapl_joules) {
354                 if (do_rapl & RAPL_PKG)
355                         outp += sprintf(outp, " PkgWatt");
356                 if (do_rapl & RAPL_CORES)
357                         outp += sprintf(outp, " CorWatt");
358                 if (do_rapl & RAPL_GFX)
359                         outp += sprintf(outp, " GFXWatt");
360                 if (do_rapl & RAPL_DRAM)
361                         outp += sprintf(outp, " RAMWatt");
362                 if (do_rapl & RAPL_PKG_PERF_STATUS)
363                         outp += sprintf(outp, "   PKG_%%");
364                 if (do_rapl & RAPL_DRAM_PERF_STATUS)
365                         outp += sprintf(outp, "   RAM_%%");
366         } else if (do_rapl && rapl_joules) {
367                 if (do_rapl & RAPL_PKG)
368                         outp += sprintf(outp, "   Pkg_J");
369                 if (do_rapl & RAPL_CORES)
370                         outp += sprintf(outp, "   Cor_J");
371                 if (do_rapl & RAPL_GFX)
372                         outp += sprintf(outp, "   GFX_J");
373                 if (do_rapl & RAPL_DRAM)
374                         outp += sprintf(outp, "   RAM_W");
375                 if (do_rapl & RAPL_PKG_PERF_STATUS)
376                         outp += sprintf(outp, "   PKG_%%");
377                 if (do_rapl & RAPL_DRAM_PERF_STATUS)
378                         outp += sprintf(outp, "   RAM_%%");
379                 outp += sprintf(outp, "   time");
380
381         }
382     done:
383         outp += sprintf(outp, "\n");
384 }
385
386 int dump_counters(struct thread_data *t, struct core_data *c,
387         struct pkg_data *p)
388 {
389         outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
390
391         if (t) {
392                 outp += sprintf(outp, "CPU: %d flags 0x%x\n",
393                         t->cpu_id, t->flags);
394                 outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
395                 outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
396                 outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
397                 outp += sprintf(outp, "c1: %016llX\n", t->c1);
398                 outp += sprintf(outp, "msr0x%x: %08llX\n",
399                         extra_delta_offset32, t->extra_delta32);
400                 outp += sprintf(outp, "msr0x%x: %016llX\n",
401                         extra_delta_offset64, t->extra_delta64);
402                 outp += sprintf(outp, "msr0x%x: %08llX\n",
403                         extra_msr_offset32, t->extra_msr32);
404                 outp += sprintf(outp, "msr0x%x: %016llX\n",
405                         extra_msr_offset64, t->extra_msr64);
406                 if (do_smi)
407                         outp += sprintf(outp, "SMI: %08X\n", t->smi_count);
408         }
409
410         if (c) {
411                 outp += sprintf(outp, "core: %d\n", c->core_id);
412                 outp += sprintf(outp, "c3: %016llX\n", c->c3);
413                 outp += sprintf(outp, "c6: %016llX\n", c->c6);
414                 outp += sprintf(outp, "c7: %016llX\n", c->c7);
415                 outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
416         }
417
418         if (p) {
419                 outp += sprintf(outp, "package: %d\n", p->package_id);
420
421                 outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
422                 outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
423                 outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
424                 outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
425
426                 outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
427                 if (do_pc3)
428                         outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
429                 if (do_pc6)
430                         outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
431                 if (do_pc7)
432                         outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
433                 outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
434                 outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
435                 outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
436                 outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
437                 outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
438                 outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
439                 outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
440                 outp += sprintf(outp, "Throttle PKG: %0X\n",
441                         p->rapl_pkg_perf_status);
442                 outp += sprintf(outp, "Throttle RAM: %0X\n",
443                         p->rapl_dram_perf_status);
444                 outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
445         }
446
447         outp += sprintf(outp, "\n");
448
449         return 0;
450 }
451
452 /*
453  * column formatting convention & formats
454  */
455 int format_counters(struct thread_data *t, struct core_data *c,
456         struct pkg_data *p)
457 {
458         double interval_float;
459         char *fmt8;
460
461          /* if showing only 1st thread in core and this isn't one, bail out */
462         if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
463                 return 0;
464
465          /* if showing only 1st thread in pkg and this isn't one, bail out */
466         if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
467                 return 0;
468
469         interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
470
471         /* topo columns, print blanks on 1st (average) line */
472         if (t == &average.threads) {
473                 if (show_pkg)
474                         outp += sprintf(outp, "       -");
475                 if (show_core)
476                         outp += sprintf(outp, "       -");
477                 if (show_cpu)
478                         outp += sprintf(outp, "       -");
479         } else {
480                 if (show_pkg) {
481                         if (p)
482                                 outp += sprintf(outp, "%8d", p->package_id);
483                         else
484                                 outp += sprintf(outp, "       -");
485                 }
486                 if (show_core) {
487                         if (c)
488                                 outp += sprintf(outp, "%8d", c->core_id);
489                         else
490                                 outp += sprintf(outp, "       -");
491                 }
492                 if (show_cpu)
493                         outp += sprintf(outp, "%8d", t->cpu_id);
494         }
495
496         /* Avg_MHz */
497         if (has_aperf)
498                 outp += sprintf(outp, "%8.0f",
499                         1.0 / units * t->aperf / interval_float);
500
501         /* %Busy */
502         if (has_aperf) {
503                 if (!skip_c0)
504                         outp += sprintf(outp, "%8.2f", 100.0 * t->mperf/t->tsc);
505                 else
506                         outp += sprintf(outp, "********");
507         }
508
509         /* Bzy_MHz */
510         if (has_aperf)
511                 outp += sprintf(outp, "%8.0f",
512                         1.0 * t->tsc / units * t->aperf / t->mperf / interval_float);
513
514         /* TSC_MHz */
515         outp += sprintf(outp, "%8.0f", 1.0 * t->tsc/units/interval_float);
516
517         /* delta */
518         if (extra_delta_offset32)
519                 outp += sprintf(outp, "  %11llu", t->extra_delta32);
520
521         /* DELTA */
522         if (extra_delta_offset64)
523                 outp += sprintf(outp, "  %11llu", t->extra_delta64);
524         /* msr */
525         if (extra_msr_offset32)
526                 outp += sprintf(outp, "  0x%08llx", t->extra_msr32);
527
528         /* MSR */
529         if (extra_msr_offset64)
530                 outp += sprintf(outp, "  0x%016llx", t->extra_msr64);
531
532         if (!debug)
533                 goto done;
534
535         /* SMI */
536         if (do_smi)
537                 outp += sprintf(outp, "%8d", t->smi_count);
538
539         if (do_nhm_cstates) {
540                 if (!skip_c1)
541                         outp += sprintf(outp, "%8.2f", 100.0 * t->c1/t->tsc);
542                 else
543                         outp += sprintf(outp, "********");
544         }
545
546         /* print per-core data only for 1st thread in core */
547         if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
548                 goto done;
549
550         if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates)
551                 outp += sprintf(outp, "%8.2f", 100.0 * c->c3/t->tsc);
552         if (do_nhm_cstates)
553                 outp += sprintf(outp, "%8.2f", 100.0 * c->c6/t->tsc);
554         if (do_snb_cstates)
555                 outp += sprintf(outp, "%8.2f", 100.0 * c->c7/t->tsc);
556
557         if (do_dts)
558                 outp += sprintf(outp, "%8d", c->core_temp_c);
559
560         /* print per-package data only for 1st core in package */
561         if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
562                 goto done;
563
564         /* PkgTmp */
565         if (do_ptm)
566                 outp += sprintf(outp, "%8d", p->pkg_temp_c);
567
568         /* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
569         if (do_skl_residency) {
570                 outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_wtd_core_c0/t->tsc);
571                 outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_any_core_c0/t->tsc);
572                 outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_any_gfxe_c0/t->tsc);
573                 outp += sprintf(outp, "%8.2f", 100.0 * p->pkg_both_core_gfxe_c0/t->tsc);
574         }
575
576         if (do_pc2)
577                 outp += sprintf(outp, "%8.2f", 100.0 * p->pc2/t->tsc);
578         if (do_pc3)
579                 outp += sprintf(outp, "%8.2f", 100.0 * p->pc3/t->tsc);
580         if (do_pc6)
581                 outp += sprintf(outp, "%8.2f", 100.0 * p->pc6/t->tsc);
582         if (do_pc7)
583                 outp += sprintf(outp, "%8.2f", 100.0 * p->pc7/t->tsc);
584         if (do_c8_c9_c10) {
585                 outp += sprintf(outp, "%8.2f", 100.0 * p->pc8/t->tsc);
586                 outp += sprintf(outp, "%8.2f", 100.0 * p->pc9/t->tsc);
587                 outp += sprintf(outp, "%8.2f", 100.0 * p->pc10/t->tsc);
588         }
589
590         /*
591          * If measurement interval exceeds minimum RAPL Joule Counter range,
592          * indicate that results are suspect by printing "**" in fraction place.
593          */
594         if (interval_float < rapl_joule_counter_range)
595                 fmt8 = "%8.2f";
596         else
597                 fmt8 = " %6.0f**";
598
599         if (do_rapl && !rapl_joules) {
600                 if (do_rapl & RAPL_PKG)
601                         outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units / interval_float);
602                 if (do_rapl & RAPL_CORES)
603                         outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units / interval_float);
604                 if (do_rapl & RAPL_GFX)
605                         outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units / interval_float);
606                 if (do_rapl & RAPL_DRAM)
607                         outp += sprintf(outp, fmt8, p->energy_dram * rapl_dram_energy_units / interval_float);
608                 if (do_rapl & RAPL_PKG_PERF_STATUS)
609                         outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
610                 if (do_rapl & RAPL_DRAM_PERF_STATUS)
611                         outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
612         } else if (do_rapl && rapl_joules) {
613                 if (do_rapl & RAPL_PKG)
614                         outp += sprintf(outp, fmt8,
615                                         p->energy_pkg * rapl_energy_units);
616                 if (do_rapl & RAPL_CORES)
617                         outp += sprintf(outp, fmt8,
618                                         p->energy_cores * rapl_energy_units);
619                 if (do_rapl & RAPL_GFX)
620                         outp += sprintf(outp, fmt8,
621                                         p->energy_gfx * rapl_energy_units);
622                 if (do_rapl & RAPL_DRAM)
623                         outp += sprintf(outp, fmt8,
624                                         p->energy_dram * rapl_dram_energy_units);
625                 if (do_rapl & RAPL_PKG_PERF_STATUS)
626                         outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
627                 if (do_rapl & RAPL_DRAM_PERF_STATUS)
628                         outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
629
630                 outp += sprintf(outp, fmt8, interval_float);
631         }
632 done:
633         outp += sprintf(outp, "\n");
634
635         return 0;
636 }
637
638 void flush_stdout()
639 {
640         fputs(output_buffer, stdout);
641         fflush(stdout);
642         outp = output_buffer;
643 }
644 void flush_stderr()
645 {
646         fputs(output_buffer, stderr);
647         outp = output_buffer;
648 }
649 void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
650 {
651         static int printed;
652
653         if (!printed || !summary_only)
654                 print_header();
655
656         if (topo.num_cpus > 1)
657                 format_counters(&average.threads, &average.cores,
658                         &average.packages);
659
660         printed = 1;
661
662         if (summary_only)
663                 return;
664
665         for_all_cpus(format_counters, t, c, p);
666 }
667
668 #define DELTA_WRAP32(new, old)                  \
669         if (new > old) {                        \
670                 old = new - old;                \
671         } else {                                \
672                 old = 0x100000000 + new - old;  \
673         }
674
675 void
676 delta_package(struct pkg_data *new, struct pkg_data *old)
677 {
678
679         if (do_skl_residency) {
680                 old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
681                 old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
682                 old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
683                 old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0;
684         }
685         old->pc2 = new->pc2 - old->pc2;
686         if (do_pc3)
687                 old->pc3 = new->pc3 - old->pc3;
688         if (do_pc6)
689                 old->pc6 = new->pc6 - old->pc6;
690         if (do_pc7)
691                 old->pc7 = new->pc7 - old->pc7;
692         old->pc8 = new->pc8 - old->pc8;
693         old->pc9 = new->pc9 - old->pc9;
694         old->pc10 = new->pc10 - old->pc10;
695         old->pkg_temp_c = new->pkg_temp_c;
696
697         DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
698         DELTA_WRAP32(new->energy_cores, old->energy_cores);
699         DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
700         DELTA_WRAP32(new->energy_dram, old->energy_dram);
701         DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
702         DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
703 }
704
705 void
706 delta_core(struct core_data *new, struct core_data *old)
707 {
708         old->c3 = new->c3 - old->c3;
709         old->c6 = new->c6 - old->c6;
710         old->c7 = new->c7 - old->c7;
711         old->core_temp_c = new->core_temp_c;
712 }
713
714 /*
715  * old = new - old
716  */
717 void
718 delta_thread(struct thread_data *new, struct thread_data *old,
719         struct core_data *core_delta)
720 {
721         old->tsc = new->tsc - old->tsc;
722
723         /* check for TSC < 1 Mcycles over interval */
724         if (old->tsc < (1000 * 1000))
725                 errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
726                      "You can disable all c-states by booting with \"idle=poll\"\n"
727                      "or just the deep ones with \"processor.max_cstate=1\"");
728
729         old->c1 = new->c1 - old->c1;
730
731         if (has_aperf) {
732                 if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
733                         old->aperf = new->aperf - old->aperf;
734                         old->mperf = new->mperf - old->mperf;
735                 } else {
736
737                         if (!aperf_mperf_unstable) {
738                                 fprintf(stderr, "%s: APERF or MPERF went backwards *\n", progname);
739                                 fprintf(stderr, "* Frequency results do not cover entire interval *\n");
740                                 fprintf(stderr, "* fix this by running Linux-2.6.30 or later *\n");
741
742                                 aperf_mperf_unstable = 1;
743                         }
744                         /*
745                          * mperf delta is likely a huge "positive" number
746                          * can not use it for calculating c0 time
747                          */
748                         skip_c0 = 1;
749                         skip_c1 = 1;
750                 }
751         }
752
753
754         if (use_c1_residency_msr) {
755                 /*
756                  * Some models have a dedicated C1 residency MSR,
757                  * which should be more accurate than the derivation below.
758                  */
759         } else {
760                 /*
761                  * As counter collection is not atomic,
762                  * it is possible for mperf's non-halted cycles + idle states
763                  * to exceed TSC's all cycles: show c1 = 0% in that case.
764                  */
765                 if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
766                         old->c1 = 0;
767                 else {
768                         /* normal case, derive c1 */
769                         old->c1 = old->tsc - old->mperf - core_delta->c3
770                                 - core_delta->c6 - core_delta->c7;
771                 }
772         }
773
774         if (old->mperf == 0) {
775                 if (debug > 1) fprintf(stderr, "cpu%d MPERF 0!\n", old->cpu_id);
776                 old->mperf = 1; /* divide by 0 protection */
777         }
778
779         old->extra_delta32 = new->extra_delta32 - old->extra_delta32;
780         old->extra_delta32 &= 0xFFFFFFFF;
781
782         old->extra_delta64 = new->extra_delta64 - old->extra_delta64;
783
784         /*
785          * Extra MSR is just a snapshot, simply copy latest w/o subtracting
786          */
787         old->extra_msr32 = new->extra_msr32;
788         old->extra_msr64 = new->extra_msr64;
789
790         if (do_smi)
791                 old->smi_count = new->smi_count - old->smi_count;
792 }
793
794 int delta_cpu(struct thread_data *t, struct core_data *c,
795         struct pkg_data *p, struct thread_data *t2,
796         struct core_data *c2, struct pkg_data *p2)
797 {
798         /* calculate core delta only for 1st thread in core */
799         if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
800                 delta_core(c, c2);
801
802         /* always calculate thread delta */
803         delta_thread(t, t2, c2);        /* c2 is core delta */
804
805         /* calculate package delta only for 1st core in package */
806         if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
807                 delta_package(p, p2);
808
809         return 0;
810 }
811
812 void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
813 {
814         t->tsc = 0;
815         t->aperf = 0;
816         t->mperf = 0;
817         t->c1 = 0;
818
819         t->smi_count = 0;
820         t->extra_delta32 = 0;
821         t->extra_delta64 = 0;
822
823         /* tells format_counters to dump all fields from this set */
824         t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
825
826         c->c3 = 0;
827         c->c6 = 0;
828         c->c7 = 0;
829         c->core_temp_c = 0;
830
831         p->pkg_wtd_core_c0 = 0;
832         p->pkg_any_core_c0 = 0;
833         p->pkg_any_gfxe_c0 = 0;
834         p->pkg_both_core_gfxe_c0 = 0;
835
836         p->pc2 = 0;
837         if (do_pc3)
838                 p->pc3 = 0;
839         if (do_pc6)
840                 p->pc6 = 0;
841         if (do_pc7)
842                 p->pc7 = 0;
843         p->pc8 = 0;
844         p->pc9 = 0;
845         p->pc10 = 0;
846
847         p->energy_pkg = 0;
848         p->energy_dram = 0;
849         p->energy_cores = 0;
850         p->energy_gfx = 0;
851         p->rapl_pkg_perf_status = 0;
852         p->rapl_dram_perf_status = 0;
853         p->pkg_temp_c = 0;
854 }
855 int sum_counters(struct thread_data *t, struct core_data *c,
856         struct pkg_data *p)
857 {
858         average.threads.tsc += t->tsc;
859         average.threads.aperf += t->aperf;
860         average.threads.mperf += t->mperf;
861         average.threads.c1 += t->c1;
862
863         average.threads.extra_delta32 += t->extra_delta32;
864         average.threads.extra_delta64 += t->extra_delta64;
865
866         /* sum per-core values only for 1st thread in core */
867         if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
868                 return 0;
869
870         average.cores.c3 += c->c3;
871         average.cores.c6 += c->c6;
872         average.cores.c7 += c->c7;
873
874         average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
875
876         /* sum per-pkg values only for 1st core in pkg */
877         if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
878                 return 0;
879
880         if (do_skl_residency) {
881                 average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
882                 average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
883                 average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
884                 average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
885         }
886
887         average.packages.pc2 += p->pc2;
888         if (do_pc3)
889                 average.packages.pc3 += p->pc3;
890         if (do_pc6)
891                 average.packages.pc6 += p->pc6;
892         if (do_pc7)
893                 average.packages.pc7 += p->pc7;
894         average.packages.pc8 += p->pc8;
895         average.packages.pc9 += p->pc9;
896         average.packages.pc10 += p->pc10;
897
898         average.packages.energy_pkg += p->energy_pkg;
899         average.packages.energy_dram += p->energy_dram;
900         average.packages.energy_cores += p->energy_cores;
901         average.packages.energy_gfx += p->energy_gfx;
902
903         average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
904
905         average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
906         average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
907         return 0;
908 }
909 /*
910  * sum the counters for all cpus in the system
911  * compute the weighted average
912  */
913 void compute_average(struct thread_data *t, struct core_data *c,
914         struct pkg_data *p)
915 {
916         clear_counters(&average.threads, &average.cores, &average.packages);
917
918         for_all_cpus(sum_counters, t, c, p);
919
920         average.threads.tsc /= topo.num_cpus;
921         average.threads.aperf /= topo.num_cpus;
922         average.threads.mperf /= topo.num_cpus;
923         average.threads.c1 /= topo.num_cpus;
924
925         average.threads.extra_delta32 /= topo.num_cpus;
926         average.threads.extra_delta32 &= 0xFFFFFFFF;
927
928         average.threads.extra_delta64 /= topo.num_cpus;
929
930         average.cores.c3 /= topo.num_cores;
931         average.cores.c6 /= topo.num_cores;
932         average.cores.c7 /= topo.num_cores;
933
934         if (do_skl_residency) {
935                 average.packages.pkg_wtd_core_c0 /= topo.num_packages;
936                 average.packages.pkg_any_core_c0 /= topo.num_packages;
937                 average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
938                 average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
939         }
940
941         average.packages.pc2 /= topo.num_packages;
942         if (do_pc3)
943                 average.packages.pc3 /= topo.num_packages;
944         if (do_pc6)
945                 average.packages.pc6 /= topo.num_packages;
946         if (do_pc7)
947                 average.packages.pc7 /= topo.num_packages;
948
949         average.packages.pc8 /= topo.num_packages;
950         average.packages.pc9 /= topo.num_packages;
951         average.packages.pc10 /= topo.num_packages;
952 }
953
954 static unsigned long long rdtsc(void)
955 {
956         unsigned int low, high;
957
958         asm volatile("rdtsc" : "=a" (low), "=d" (high));
959
960         return low | ((unsigned long long)high) << 32;
961 }
962
963
964 /*
965  * get_counters(...)
966  * migrate to cpu
967  * acquire and record local counters for that cpu
968  */
969 int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
970 {
971         int cpu = t->cpu_id;
972         unsigned long long msr;
973
974         if (cpu_migrate(cpu)) {
975                 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
976                 return -1;
977         }
978
979         t->tsc = rdtsc();       /* we are running on local CPU of interest */
980
981         if (has_aperf) {
982                 if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
983                         return -3;
984                 if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
985                         return -4;
986         }
987
988         if (do_smi) {
989                 if (get_msr(cpu, MSR_SMI_COUNT, &msr))
990                         return -5;
991                 t->smi_count = msr & 0xFFFFFFFF;
992         }
993         if (extra_delta_offset32) {
994                 if (get_msr(cpu, extra_delta_offset32, &msr))
995                         return -5;
996                 t->extra_delta32 = msr & 0xFFFFFFFF;
997         }
998
999         if (extra_delta_offset64)
1000                 if (get_msr(cpu, extra_delta_offset64, &t->extra_delta64))
1001                         return -5;
1002
1003         if (extra_msr_offset32) {
1004                 if (get_msr(cpu, extra_msr_offset32, &msr))
1005                         return -5;
1006                 t->extra_msr32 = msr & 0xFFFFFFFF;
1007         }
1008
1009         if (extra_msr_offset64)
1010                 if (get_msr(cpu, extra_msr_offset64, &t->extra_msr64))
1011                         return -5;
1012
1013         if (use_c1_residency_msr) {
1014                 if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1015                         return -6;
1016         }
1017
1018         /* collect core counters only for 1st thread in core */
1019         if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1020                 return 0;
1021
1022         if (do_nhm_cstates && !do_slm_cstates && !do_knl_cstates) {
1023                 if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1024                         return -6;
1025         }
1026
1027         if (do_nhm_cstates && !do_knl_cstates) {
1028                 if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1029                         return -7;
1030         } else if (do_knl_cstates) {
1031                 if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1032                         return -7;
1033         }
1034
1035         if (do_snb_cstates)
1036                 if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1037                         return -8;
1038
1039         if (do_dts) {
1040                 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1041                         return -9;
1042                 c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1043         }
1044
1045
1046         /* collect package counters only for 1st core in package */
1047         if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1048                 return 0;
1049
1050         if (do_skl_residency) {
1051                 if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
1052                         return -10;
1053                 if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
1054                         return -11;
1055                 if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
1056                         return -12;
1057                 if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
1058                         return -13;
1059         }
1060         if (do_pc3)
1061                 if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1062                         return -9;
1063         if (do_pc6)
1064                 if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1065                         return -10;
1066         if (do_pc2)
1067                 if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1068                         return -11;
1069         if (do_pc7)
1070                 if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1071                         return -12;
1072         if (do_c8_c9_c10) {
1073                 if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1074                         return -13;
1075                 if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1076                         return -13;
1077                 if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1078                         return -13;
1079         }
1080         if (do_rapl & RAPL_PKG) {
1081                 if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1082                         return -13;
1083                 p->energy_pkg = msr & 0xFFFFFFFF;
1084         }
1085         if (do_rapl & RAPL_CORES) {
1086                 if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1087                         return -14;
1088                 p->energy_cores = msr & 0xFFFFFFFF;
1089         }
1090         if (do_rapl & RAPL_DRAM) {
1091                 if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1092                         return -15;
1093                 p->energy_dram = msr & 0xFFFFFFFF;
1094         }
1095         if (do_rapl & RAPL_GFX) {
1096                 if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1097                         return -16;
1098                 p->energy_gfx = msr & 0xFFFFFFFF;
1099         }
1100         if (do_rapl & RAPL_PKG_PERF_STATUS) {
1101                 if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1102                         return -16;
1103                 p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1104         }
1105         if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1106                 if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1107                         return -16;
1108                 p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1109         }
1110         if (do_ptm) {
1111                 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1112                         return -17;
1113                 p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1114         }
1115         return 0;
1116 }
1117
1118 /*
1119  * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1120  * If you change the values, note they are used both in comparisons
1121  * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1122  */
1123
1124 #define PCLUKN 0 /* Unknown */
1125 #define PCLRSV 1 /* Reserved */
1126 #define PCL__0 2 /* PC0 */
1127 #define PCL__1 3 /* PC1 */
1128 #define PCL__2 4 /* PC2 */
1129 #define PCL__3 5 /* PC3 */
1130 #define PCL__4 6 /* PC4 */
1131 #define PCL__6 7 /* PC6 */
1132 #define PCL_6N 8 /* PC6 No Retention */
1133 #define PCL_6R 9 /* PC6 Retention */
1134 #define PCL__7 10 /* PC7 */
1135 #define PCL_7S 11 /* PC7 Shrink */
1136 #define PCL__8 12 /* PC8 */
1137 #define PCL__9 13 /* PC9 */
1138 #define PCLUNL 14 /* Unlimited */
1139
1140 int pkg_cstate_limit = PCLUKN;
1141 char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
1142         "pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"};
1143
1144 int nhm_pkg_cstate_limits[16] = {PCL__0, PCL__1, PCL__3, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1145 int snb_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL_6N, PCL_6R, PCL__7, PCL_7S, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1146 int hsw_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1147 int slv_pkg_cstate_limits[16] = {PCL__0, PCL__1, PCLRSV, PCLRSV, PCL__4, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1148 int amt_pkg_cstate_limits[16] = {PCL__0, PCL__1, PCL__2, PCLRSV, PCLRSV, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1149 int phi_pkg_cstate_limits[16] = {PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1150
1151 static void
1152 dump_nhm_platform_info(void)
1153 {
1154         unsigned long long msr;
1155         unsigned int ratio;
1156
1157         get_msr(0, MSR_NHM_PLATFORM_INFO, &msr);
1158
1159         fprintf(stderr, "cpu0: MSR_NHM_PLATFORM_INFO: 0x%08llx\n", msr);
1160
1161         ratio = (msr >> 40) & 0xFF;
1162         fprintf(stderr, "%d * %.0f = %.0f MHz max efficiency frequency\n",
1163                 ratio, bclk, ratio * bclk);
1164
1165         ratio = (msr >> 8) & 0xFF;
1166         fprintf(stderr, "%d * %.0f = %.0f MHz base frequency\n",
1167                 ratio, bclk, ratio * bclk);
1168
1169         get_msr(0, MSR_IA32_POWER_CTL, &msr);
1170         fprintf(stderr, "cpu0: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
1171                 msr, msr & 0x2 ? "EN" : "DIS");
1172
1173         return;
1174 }
1175
1176 static void
1177 dump_hsw_turbo_ratio_limits(void)
1178 {
1179         unsigned long long msr;
1180         unsigned int ratio;
1181
1182         get_msr(0, MSR_TURBO_RATIO_LIMIT2, &msr);
1183
1184         fprintf(stderr, "cpu0: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", msr);
1185
1186         ratio = (msr >> 8) & 0xFF;
1187         if (ratio)
1188                 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 18 active cores\n",
1189                         ratio, bclk, ratio * bclk);
1190
1191         ratio = (msr >> 0) & 0xFF;
1192         if (ratio)
1193                 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 17 active cores\n",
1194                         ratio, bclk, ratio * bclk);
1195         return;
1196 }
1197
1198 static void
1199 dump_ivt_turbo_ratio_limits(void)
1200 {
1201         unsigned long long msr;
1202         unsigned int ratio;
1203
1204         get_msr(0, MSR_TURBO_RATIO_LIMIT1, &msr);
1205
1206         fprintf(stderr, "cpu0: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", msr);
1207
1208         ratio = (msr >> 56) & 0xFF;
1209         if (ratio)
1210                 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 16 active cores\n",
1211                         ratio, bclk, ratio * bclk);
1212
1213         ratio = (msr >> 48) & 0xFF;
1214         if (ratio)
1215                 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 15 active cores\n",
1216                         ratio, bclk, ratio * bclk);
1217
1218         ratio = (msr >> 40) & 0xFF;
1219         if (ratio)
1220                 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 14 active cores\n",
1221                         ratio, bclk, ratio * bclk);
1222
1223         ratio = (msr >> 32) & 0xFF;
1224         if (ratio)
1225                 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 13 active cores\n",
1226                         ratio, bclk, ratio * bclk);
1227
1228         ratio = (msr >> 24) & 0xFF;
1229         if (ratio)
1230                 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 12 active cores\n",
1231                         ratio, bclk, ratio * bclk);
1232
1233         ratio = (msr >> 16) & 0xFF;
1234         if (ratio)
1235                 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 11 active cores\n",
1236                         ratio, bclk, ratio * bclk);
1237
1238         ratio = (msr >> 8) & 0xFF;
1239         if (ratio)
1240                 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 10 active cores\n",
1241                         ratio, bclk, ratio * bclk);
1242
1243         ratio = (msr >> 0) & 0xFF;
1244         if (ratio)
1245                 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 9 active cores\n",
1246                         ratio, bclk, ratio * bclk);
1247         return;
1248 }
1249
1250 static void
1251 dump_nhm_turbo_ratio_limits(void)
1252 {
1253         unsigned long long msr;
1254         unsigned int ratio;
1255
1256         get_msr(0, MSR_TURBO_RATIO_LIMIT, &msr);
1257
1258         fprintf(stderr, "cpu0: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", msr);
1259
1260         ratio = (msr >> 56) & 0xFF;
1261         if (ratio)
1262                 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 8 active cores\n",
1263                         ratio, bclk, ratio * bclk);
1264
1265         ratio = (msr >> 48) & 0xFF;
1266         if (ratio)
1267                 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 7 active cores\n",
1268                         ratio, bclk, ratio * bclk);
1269
1270         ratio = (msr >> 40) & 0xFF;
1271         if (ratio)
1272                 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 6 active cores\n",
1273                         ratio, bclk, ratio * bclk);
1274
1275         ratio = (msr >> 32) & 0xFF;
1276         if (ratio)
1277                 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 5 active cores\n",
1278                         ratio, bclk, ratio * bclk);
1279
1280         ratio = (msr >> 24) & 0xFF;
1281         if (ratio)
1282                 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 4 active cores\n",
1283                         ratio, bclk, ratio * bclk);
1284
1285         ratio = (msr >> 16) & 0xFF;
1286         if (ratio)
1287                 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 3 active cores\n",
1288                         ratio, bclk, ratio * bclk);
1289
1290         ratio = (msr >> 8) & 0xFF;
1291         if (ratio)
1292                 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 2 active cores\n",
1293                         ratio, bclk, ratio * bclk);
1294
1295         ratio = (msr >> 0) & 0xFF;
1296         if (ratio)
1297                 fprintf(stderr, "%d * %.0f = %.0f MHz max turbo 1 active cores\n",
1298                         ratio, bclk, ratio * bclk);
1299         return;
1300 }
1301
1302 static void
1303 dump_knl_turbo_ratio_limits(void)
1304 {
1305         int cores;
1306         unsigned int ratio;
1307         unsigned long long msr;
1308         int delta_cores;
1309         int delta_ratio;
1310         int i;
1311
1312         get_msr(0, MSR_NHM_TURBO_RATIO_LIMIT, &msr);
1313
1314         fprintf(stderr, "cpu0: MSR_NHM_TURBO_RATIO_LIMIT: 0x%08llx\n",
1315         msr);
1316
1317         /**
1318          * Turbo encoding in KNL is as follows:
1319          * [7:0] -- Base value of number of active cores of bucket 1.
1320          * [15:8] -- Base value of freq ratio of bucket 1.
1321          * [20:16] -- +ve delta of number of active cores of bucket 2.
1322          * i.e. active cores of bucket 2 =
1323          * active cores of bucket 1 + delta
1324          * [23:21] -- Negative delta of freq ratio of bucket 2.
1325          * i.e. freq ratio of bucket 2 =
1326          * freq ratio of bucket 1 - delta
1327          * [28:24]-- +ve delta of number of active cores of bucket 3.
1328          * [31:29]-- -ve delta of freq ratio of bucket 3.
1329          * [36:32]-- +ve delta of number of active cores of bucket 4.
1330          * [39:37]-- -ve delta of freq ratio of bucket 4.
1331          * [44:40]-- +ve delta of number of active cores of bucket 5.
1332          * [47:45]-- -ve delta of freq ratio of bucket 5.
1333          * [52:48]-- +ve delta of number of active cores of bucket 6.
1334          * [55:53]-- -ve delta of freq ratio of bucket 6.
1335          * [60:56]-- +ve delta of number of active cores of bucket 7.
1336          * [63:61]-- -ve delta of freq ratio of bucket 7.
1337          */
1338         cores = msr & 0xFF;
1339         ratio = (msr >> 8) && 0xFF;
1340         if (ratio > 0)
1341                 fprintf(stderr,
1342                         "%d * %.0f = %.0f MHz max turbo %d active cores\n",
1343                         ratio, bclk, ratio * bclk, cores);
1344
1345         for (i = 16; i < 64; i = i + 8) {
1346                 delta_cores = (msr >> i) & 0x1F;
1347                 delta_ratio = (msr >> (i + 5)) && 0x7;
1348                 if (!delta_cores || !delta_ratio)
1349                         return;
1350                 cores = cores + delta_cores;
1351                 ratio = ratio - delta_ratio;
1352
1353                 /** -ve ratios will make successive ratio calculations
1354                  * negative. Hence return instead of carrying on.
1355                  */
1356                 if (ratio > 0)
1357                         fprintf(stderr,
1358                                 "%d * %.0f = %.0f MHz max turbo %d active cores\n",
1359                                 ratio, bclk, ratio * bclk, cores);
1360         }
1361 }
1362
1363 static void
1364 dump_nhm_cst_cfg(void)
1365 {
1366         unsigned long long msr;
1367
1368         get_msr(0, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
1369
1370 #define SNB_C1_AUTO_UNDEMOTE              (1UL << 27)
1371 #define SNB_C3_AUTO_UNDEMOTE              (1UL << 28)
1372
1373         fprintf(stderr, "cpu0: MSR_NHM_SNB_PKG_CST_CFG_CTL: 0x%08llx", msr);
1374
1375         fprintf(stderr, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: %s)\n",
1376                 (msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
1377                 (msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
1378                 (msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
1379                 (msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
1380                 (msr & (1 << 15)) ? "" : "UN",
1381                 (unsigned int)msr & 7,
1382                 pkg_cstate_limit_strings[pkg_cstate_limit]);
1383         return;
1384 }
1385
1386 void free_all_buffers(void)
1387 {
1388         CPU_FREE(cpu_present_set);
1389         cpu_present_set = NULL;
1390         cpu_present_set = 0;
1391
1392         CPU_FREE(cpu_affinity_set);
1393         cpu_affinity_set = NULL;
1394         cpu_affinity_setsize = 0;
1395
1396         free(thread_even);
1397         free(core_even);
1398         free(package_even);
1399
1400         thread_even = NULL;
1401         core_even = NULL;
1402         package_even = NULL;
1403
1404         free(thread_odd);
1405         free(core_odd);
1406         free(package_odd);
1407
1408         thread_odd = NULL;
1409         core_odd = NULL;
1410         package_odd = NULL;
1411
1412         free(output_buffer);
1413         output_buffer = NULL;
1414         outp = NULL;
1415 }
1416
1417 /*
1418  * Open a file, and exit on failure
1419  */
1420 FILE *fopen_or_die(const char *path, const char *mode)
1421 {
1422         FILE *filep = fopen(path, "r");
1423         if (!filep)
1424                 err(1, "%s: open failed", path);
1425         return filep;
1426 }
1427
1428 /*
1429  * Parse a file containing a single int.
1430  */
1431 int parse_int_file(const char *fmt, ...)
1432 {
1433         va_list args;
1434         char path[PATH_MAX];
1435         FILE *filep;
1436         int value;
1437
1438         va_start(args, fmt);
1439         vsnprintf(path, sizeof(path), fmt, args);
1440         va_end(args);
1441         filep = fopen_or_die(path, "r");
1442         if (fscanf(filep, "%d", &value) != 1)
1443                 err(1, "%s: failed to parse number from file", path);
1444         fclose(filep);
1445         return value;
1446 }
1447
1448 /*
1449  * get_cpu_position_in_core(cpu)
1450  * return the position of the CPU among its HT siblings in the core
1451  * return -1 if the sibling is not in list
1452  */
1453 int get_cpu_position_in_core(int cpu)
1454 {
1455         char path[64];
1456         FILE *filep;
1457         int this_cpu;
1458         char character;
1459         int i;
1460
1461         sprintf(path,
1462                 "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list",
1463                 cpu);
1464         filep = fopen(path, "r");
1465         if (filep == NULL) {
1466                 perror(path);
1467                 exit(1);
1468         }
1469
1470         for (i = 0; i < topo.num_threads_per_core; i++) {
1471                 fscanf(filep, "%d", &this_cpu);
1472                 if (this_cpu == cpu) {
1473                         fclose(filep);
1474                         return i;
1475                 }
1476
1477                 /* Account for no separator after last thread*/
1478                 if (i != (topo.num_threads_per_core - 1))
1479                         fscanf(filep, "%c", &character);
1480         }
1481
1482         fclose(filep);
1483         return -1;
1484 }
1485
1486 /*
1487  * cpu_is_first_core_in_package(cpu)
1488  * return 1 if given CPU is 1st core in package
1489  */
1490 int cpu_is_first_core_in_package(int cpu)
1491 {
1492         return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
1493 }
1494
1495 int get_physical_package_id(int cpu)
1496 {
1497         return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
1498 }
1499
1500 int get_core_id(int cpu)
1501 {
1502         return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
1503 }
1504
1505 int get_num_ht_siblings(int cpu)
1506 {
1507         char path[80];
1508         FILE *filep;
1509         int sib1;
1510         int matches = 0;
1511         char character;
1512         char str[100];
1513         char *ch;
1514
1515         sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
1516         filep = fopen_or_die(path, "r");
1517
1518         /*
1519          * file format:
1520          * A ',' separated or '-' separated set of numbers
1521          * (eg 1-2 or 1,3,4,5)
1522          */
1523         fscanf(filep, "%d%c\n", &sib1, &character);
1524         fseek(filep, 0, SEEK_SET);
1525         fgets(str, 100, filep);
1526         ch = strchr(str, character);
1527         while (ch != NULL) {
1528                 matches++;
1529                 ch = strchr(ch+1, character);
1530         }
1531
1532         fclose(filep);
1533         return matches+1;
1534 }
1535
1536 /*
1537  * run func(thread, core, package) in topology order
1538  * skip non-present cpus
1539  */
1540
1541 int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
1542         struct pkg_data *, struct thread_data *, struct core_data *,
1543         struct pkg_data *), struct thread_data *thread_base,
1544         struct core_data *core_base, struct pkg_data *pkg_base,
1545         struct thread_data *thread_base2, struct core_data *core_base2,
1546         struct pkg_data *pkg_base2)
1547 {
1548         int retval, pkg_no, core_no, thread_no;
1549
1550         for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
1551                 for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
1552                         for (thread_no = 0; thread_no <
1553                                 topo.num_threads_per_core; ++thread_no) {
1554                                 struct thread_data *t, *t2;
1555                                 struct core_data *c, *c2;
1556                                 struct pkg_data *p, *p2;
1557
1558                                 t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
1559
1560                                 if (cpu_is_not_present(t->cpu_id))
1561                                         continue;
1562
1563                                 t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
1564
1565                                 c = GET_CORE(core_base, core_no, pkg_no);
1566                                 c2 = GET_CORE(core_base2, core_no, pkg_no);
1567
1568                                 p = GET_PKG(pkg_base, pkg_no);
1569                                 p2 = GET_PKG(pkg_base2, pkg_no);
1570
1571                                 retval = func(t, c, p, t2, c2, p2);
1572                                 if (retval)
1573                                         return retval;
1574                         }
1575                 }
1576         }
1577         return 0;
1578 }
1579
1580 /*
1581  * run func(cpu) on every cpu in /proc/stat
1582  * return max_cpu number
1583  */
1584 int for_all_proc_cpus(int (func)(int))
1585 {
1586         FILE *fp;
1587         int cpu_num;
1588         int retval;
1589
1590         fp = fopen_or_die(proc_stat, "r");
1591
1592         retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
1593         if (retval != 0)
1594                 err(1, "%s: failed to parse format", proc_stat);
1595
1596         while (1) {
1597                 retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
1598                 if (retval != 1)
1599                         break;
1600
1601                 retval = func(cpu_num);
1602                 if (retval) {
1603                         fclose(fp);
1604                         return(retval);
1605                 }
1606         }
1607         fclose(fp);
1608         return 0;
1609 }
1610
1611 void re_initialize(void)
1612 {
1613         free_all_buffers();
1614         setup_all_buffers();
1615         printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
1616 }
1617
1618
1619 /*
1620  * count_cpus()
1621  * remember the last one seen, it will be the max
1622  */
1623 int count_cpus(int cpu)
1624 {
1625         if (topo.max_cpu_num < cpu)
1626                 topo.max_cpu_num = cpu;
1627
1628         topo.num_cpus += 1;
1629         return 0;
1630 }
1631 int mark_cpu_present(int cpu)
1632 {
1633         CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
1634         return 0;
1635 }
1636
1637 void turbostat_loop()
1638 {
1639         int retval;
1640         int restarted = 0;
1641
1642 restart:
1643         restarted++;
1644
1645         retval = for_all_cpus(get_counters, EVEN_COUNTERS);
1646         if (retval < -1) {
1647                 exit(retval);
1648         } else if (retval == -1) {
1649                 if (restarted > 1) {
1650                         exit(retval);
1651                 }
1652                 re_initialize();
1653                 goto restart;
1654         }
1655         restarted = 0;
1656         gettimeofday(&tv_even, (struct timezone *)NULL);
1657
1658         while (1) {
1659                 if (for_all_proc_cpus(cpu_is_not_present)) {
1660                         re_initialize();
1661                         goto restart;
1662                 }
1663                 sleep(interval_sec);
1664                 retval = for_all_cpus(get_counters, ODD_COUNTERS);
1665                 if (retval < -1) {
1666                         exit(retval);
1667                 } else if (retval == -1) {
1668                         re_initialize();
1669                         goto restart;
1670                 }
1671                 gettimeofday(&tv_odd, (struct timezone *)NULL);
1672                 timersub(&tv_odd, &tv_even, &tv_delta);
1673                 for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
1674                 compute_average(EVEN_COUNTERS);
1675                 format_all_counters(EVEN_COUNTERS);
1676                 flush_stdout();
1677                 sleep(interval_sec);
1678                 retval = for_all_cpus(get_counters, EVEN_COUNTERS);
1679                 if (retval < -1) {
1680                         exit(retval);
1681                 } else if (retval == -1) {
1682                         re_initialize();
1683                         goto restart;
1684                 }
1685                 gettimeofday(&tv_even, (struct timezone *)NULL);
1686                 timersub(&tv_even, &tv_odd, &tv_delta);
1687                 for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS);
1688                 compute_average(ODD_COUNTERS);
1689                 format_all_counters(ODD_COUNTERS);
1690                 flush_stdout();
1691         }
1692 }
1693
1694 void check_dev_msr()
1695 {
1696         struct stat sb;
1697
1698         if (stat("/dev/cpu/0/msr", &sb))
1699                 if (system("/sbin/modprobe msr > /dev/null 2>&1"))
1700                         err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
1701 }
1702
1703 void check_permissions()
1704 {
1705         struct __user_cap_header_struct cap_header_data;
1706         cap_user_header_t cap_header = &cap_header_data;
1707         struct __user_cap_data_struct cap_data_data;
1708         cap_user_data_t cap_data = &cap_data_data;
1709         extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
1710         int do_exit = 0;
1711
1712         /* check for CAP_SYS_RAWIO */
1713         cap_header->pid = getpid();
1714         cap_header->version = _LINUX_CAPABILITY_VERSION;
1715         if (capget(cap_header, cap_data) < 0)
1716                 err(-6, "capget(2) failed");
1717
1718         if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
1719                 do_exit++;
1720                 warnx("capget(CAP_SYS_RAWIO) failed,"
1721                         " try \"# setcap cap_sys_rawio=ep %s\"", progname);
1722         }
1723
1724         /* test file permissions */
1725         if (euidaccess("/dev/cpu/0/msr", R_OK)) {
1726                 do_exit++;
1727                 warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
1728         }
1729
1730         /* if all else fails, thell them to be root */
1731         if (do_exit)
1732                 if (getuid() != 0)
1733                         warnx("... or simply run as root");
1734
1735         if (do_exit)
1736                 exit(-6);
1737 }
1738
1739 /*
1740  * NHM adds support for additional MSRs:
1741  *
1742  * MSR_SMI_COUNT                   0x00000034
1743  *
1744  * MSR_NHM_PLATFORM_INFO           0x000000ce
1745  * MSR_NHM_SNB_PKG_CST_CFG_CTL     0x000000e2
1746  *
1747  * MSR_PKG_C3_RESIDENCY            0x000003f8
1748  * MSR_PKG_C6_RESIDENCY            0x000003f9
1749  * MSR_CORE_C3_RESIDENCY           0x000003fc
1750  * MSR_CORE_C6_RESIDENCY           0x000003fd
1751  *
1752  * Side effect:
1753  * sets global pkg_cstate_limit to decode MSR_NHM_SNB_PKG_CST_CFG_CTL
1754  */
1755 int probe_nhm_msrs(unsigned int family, unsigned int model)
1756 {
1757         unsigned long long msr;
1758         int *pkg_cstate_limits;
1759
1760         if (!genuine_intel)
1761                 return 0;
1762
1763         if (family != 6)
1764                 return 0;
1765
1766         switch (model) {
1767         case 0x1A:      /* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
1768         case 0x1E:      /* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
1769         case 0x1F:      /* Core i7 and i5 Processor - Nehalem */
1770         case 0x25:      /* Westmere Client - Clarkdale, Arrandale */
1771         case 0x2C:      /* Westmere EP - Gulftown */
1772         case 0x2E:      /* Nehalem-EX Xeon - Beckton */
1773         case 0x2F:      /* Westmere-EX Xeon - Eagleton */
1774                 pkg_cstate_limits = nhm_pkg_cstate_limits;
1775                 break;
1776         case 0x2A:      /* SNB */
1777         case 0x2D:      /* SNB Xeon */
1778         case 0x3A:      /* IVB */
1779         case 0x3E:      /* IVB Xeon */
1780                 pkg_cstate_limits = snb_pkg_cstate_limits;
1781                 break;
1782         case 0x3C:      /* HSW */
1783         case 0x3F:      /* HSX */
1784         case 0x45:      /* HSW */
1785         case 0x46:      /* HSW */
1786         case 0x3D:      /* BDW */
1787         case 0x47:      /* BDW */
1788         case 0x4F:      /* BDX */
1789         case 0x56:      /* BDX-DE */
1790         case 0x4E:      /* SKL */
1791         case 0x5E:      /* SKL */
1792                 pkg_cstate_limits = hsw_pkg_cstate_limits;
1793                 break;
1794         case 0x37:      /* BYT */
1795         case 0x4D:      /* AVN */
1796                 pkg_cstate_limits = slv_pkg_cstate_limits;
1797                 break;
1798         case 0x4C:      /* AMT */
1799                 pkg_cstate_limits = amt_pkg_cstate_limits;
1800                 break;
1801         case 0x57:      /* PHI */
1802                 pkg_cstate_limits = phi_pkg_cstate_limits;
1803                 break;
1804         default:
1805                 return 0;
1806         }
1807         get_msr(0, MSR_NHM_SNB_PKG_CST_CFG_CTL, &msr);
1808
1809         pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
1810
1811         return 1;
1812 }
1813 int has_nhm_turbo_ratio_limit(unsigned int family, unsigned int model)
1814 {
1815         switch (model) {
1816         /* Nehalem compatible, but do not include turbo-ratio limit support */
1817         case 0x2E:      /* Nehalem-EX Xeon - Beckton */
1818         case 0x2F:      /* Westmere-EX Xeon - Eagleton */
1819                 return 0;
1820         default:
1821                 return 1;
1822         }
1823 }
1824 int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
1825 {
1826         if (!genuine_intel)
1827                 return 0;
1828
1829         if (family != 6)
1830                 return 0;
1831
1832         switch (model) {
1833         case 0x3E:      /* IVB Xeon */
1834         case 0x3F:      /* HSW Xeon */
1835                 return 1;
1836         default:
1837                 return 0;
1838         }
1839 }
1840 int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
1841 {
1842         if (!genuine_intel)
1843                 return 0;
1844
1845         if (family != 6)
1846                 return 0;
1847
1848         switch (model) {
1849         case 0x3F:      /* HSW Xeon */
1850                 return 1;
1851         default:
1852                 return 0;
1853         }
1854 }
1855
1856 int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
1857 {
1858         if (!genuine_intel)
1859                 return 0;
1860
1861         if (family != 6)
1862                 return 0;
1863
1864         switch (model) {
1865         case 0x57:      /* Knights Landing */
1866                 return 1;
1867         default:
1868                 return 0;
1869         }
1870 }
1871 static void
1872 dump_cstate_pstate_config_info(family, model)
1873 {
1874         if (!do_nhm_platform_info)
1875                 return;
1876
1877         dump_nhm_platform_info();
1878
1879         if (has_hsw_turbo_ratio_limit(family, model))
1880                 dump_hsw_turbo_ratio_limits();
1881
1882         if (has_ivt_turbo_ratio_limit(family, model))
1883                 dump_ivt_turbo_ratio_limits();
1884
1885         if (has_nhm_turbo_ratio_limit(family, model))
1886                 dump_nhm_turbo_ratio_limits();
1887
1888         if (has_knl_turbo_ratio_limit(family, model))
1889                 dump_knl_turbo_ratio_limits();
1890
1891         dump_nhm_cst_cfg();
1892 }
1893
1894
1895 /*
1896  * print_epb()
1897  * Decode the ENERGY_PERF_BIAS MSR
1898  */
1899 int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1900 {
1901         unsigned long long msr;
1902         char *epb_string;
1903         int cpu;
1904
1905         if (!has_epb)
1906                 return 0;
1907
1908         cpu = t->cpu_id;
1909
1910         /* EPB is per-package */
1911         if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1912                 return 0;
1913
1914         if (cpu_migrate(cpu)) {
1915                 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1916                 return -1;
1917         }
1918
1919         if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
1920                 return 0;
1921
1922         switch (msr & 0x7) {
1923         case ENERGY_PERF_BIAS_PERFORMANCE:
1924                 epb_string = "performance";
1925                 break;
1926         case ENERGY_PERF_BIAS_NORMAL:
1927                 epb_string = "balanced";
1928                 break;
1929         case ENERGY_PERF_BIAS_POWERSAVE:
1930                 epb_string = "powersave";
1931                 break;
1932         default:
1933                 epb_string = "custom";
1934                 break;
1935         }
1936         fprintf(stderr, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
1937
1938         return 0;
1939 }
1940
1941 /*
1942  * print_perf_limit()
1943  */
1944 int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1945 {
1946         unsigned long long msr;
1947         int cpu;
1948
1949         cpu = t->cpu_id;
1950
1951         /* per-package */
1952         if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1953                 return 0;
1954
1955         if (cpu_migrate(cpu)) {
1956                 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
1957                 return -1;
1958         }
1959
1960         if (do_core_perf_limit_reasons) {
1961                 get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
1962                 fprintf(stderr, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
1963                 fprintf(stderr, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
1964                         (msr & 1 << 15) ? "bit15, " : "",
1965                         (msr & 1 << 14) ? "bit14, " : "",
1966                         (msr & 1 << 13) ? "Transitions, " : "",
1967                         (msr & 1 << 12) ? "MultiCoreTurbo, " : "",
1968                         (msr & 1 << 11) ? "PkgPwrL2, " : "",
1969                         (msr & 1 << 10) ? "PkgPwrL1, " : "",
1970                         (msr & 1 << 9) ? "CorePwr, " : "",
1971                         (msr & 1 << 8) ? "Amps, " : "",
1972                         (msr & 1 << 6) ? "VR-Therm, " : "",
1973                         (msr & 1 << 5) ? "Auto-HWP, " : "",
1974                         (msr & 1 << 4) ? "Graphics, " : "",
1975                         (msr & 1 << 2) ? "bit2, " : "",
1976                         (msr & 1 << 1) ? "ThermStatus, " : "",
1977                         (msr & 1 << 0) ? "PROCHOT, " : "");
1978                 fprintf(stderr, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n",
1979                         (msr & 1 << 31) ? "bit31, " : "",
1980                         (msr & 1 << 30) ? "bit30, " : "",
1981                         (msr & 1 << 29) ? "Transitions, " : "",
1982                         (msr & 1 << 28) ? "MultiCoreTurbo, " : "",
1983                         (msr & 1 << 27) ? "PkgPwrL2, " : "",
1984                         (msr & 1 << 26) ? "PkgPwrL1, " : "",
1985                         (msr & 1 << 25) ? "CorePwr, " : "",
1986                         (msr & 1 << 24) ? "Amps, " : "",
1987                         (msr & 1 << 22) ? "VR-Therm, " : "",
1988                         (msr & 1 << 21) ? "Auto-HWP, " : "",
1989                         (msr & 1 << 20) ? "Graphics, " : "",
1990                         (msr & 1 << 18) ? "bit18, " : "",
1991                         (msr & 1 << 17) ? "ThermStatus, " : "",
1992                         (msr & 1 << 16) ? "PROCHOT, " : "");
1993
1994         }
1995         if (do_gfx_perf_limit_reasons) {
1996                 get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
1997                 fprintf(stderr, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
1998                 fprintf(stderr, " (Active: %s%s%s%s%s%s%s%s)",
1999                         (msr & 1 << 0) ? "PROCHOT, " : "",
2000                         (msr & 1 << 1) ? "ThermStatus, " : "",
2001                         (msr & 1 << 4) ? "Graphics, " : "",
2002                         (msr & 1 << 6) ? "VR-Therm, " : "",
2003                         (msr & 1 << 8) ? "Amps, " : "",
2004                         (msr & 1 << 9) ? "GFXPwr, " : "",
2005                         (msr & 1 << 10) ? "PkgPwrL1, " : "",
2006                         (msr & 1 << 11) ? "PkgPwrL2, " : "");
2007                 fprintf(stderr, " (Logged: %s%s%s%s%s%s%s%s)\n",
2008                         (msr & 1 << 16) ? "PROCHOT, " : "",
2009                         (msr & 1 << 17) ? "ThermStatus, " : "",
2010                         (msr & 1 << 20) ? "Graphics, " : "",
2011                         (msr & 1 << 22) ? "VR-Therm, " : "",
2012                         (msr & 1 << 24) ? "Amps, " : "",
2013                         (msr & 1 << 25) ? "GFXPwr, " : "",
2014                         (msr & 1 << 26) ? "PkgPwrL1, " : "",
2015                         (msr & 1 << 27) ? "PkgPwrL2, " : "");
2016         }
2017         if (do_ring_perf_limit_reasons) {
2018                 get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
2019                 fprintf(stderr, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
2020                 fprintf(stderr, " (Active: %s%s%s%s%s%s)",
2021                         (msr & 1 << 0) ? "PROCHOT, " : "",
2022                         (msr & 1 << 1) ? "ThermStatus, " : "",
2023                         (msr & 1 << 6) ? "VR-Therm, " : "",
2024                         (msr & 1 << 8) ? "Amps, " : "",
2025                         (msr & 1 << 10) ? "PkgPwrL1, " : "",
2026                         (msr & 1 << 11) ? "PkgPwrL2, " : "");
2027                 fprintf(stderr, " (Logged: %s%s%s%s%s%s)\n",
2028                         (msr & 1 << 16) ? "PROCHOT, " : "",
2029                         (msr & 1 << 17) ? "ThermStatus, " : "",
2030                         (msr & 1 << 22) ? "VR-Therm, " : "",
2031                         (msr & 1 << 24) ? "Amps, " : "",
2032                         (msr & 1 << 26) ? "PkgPwrL1, " : "",
2033                         (msr & 1 << 27) ? "PkgPwrL2, " : "");
2034         }
2035         return 0;
2036 }
2037
2038 #define RAPL_POWER_GRANULARITY  0x7FFF  /* 15 bit power granularity */
2039 #define RAPL_TIME_GRANULARITY   0x3F /* 6 bit time granularity */
2040
2041 double get_tdp(model)
2042 {
2043         unsigned long long msr;
2044
2045         if (do_rapl & RAPL_PKG_POWER_INFO)
2046                 if (!get_msr(0, MSR_PKG_POWER_INFO, &msr))
2047                         return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
2048
2049         switch (model) {
2050         case 0x37:
2051         case 0x4D:
2052                 return 30.0;
2053         default:
2054                 return 135.0;
2055         }
2056 }
2057
2058 /*
2059  * rapl_dram_energy_units_probe()
2060  * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
2061  */
2062 static double
2063 rapl_dram_energy_units_probe(int  model, double rapl_energy_units)
2064 {
2065         /* only called for genuine_intel, family 6 */
2066
2067         switch (model) {
2068         case 0x3F:      /* HSX */
2069         case 0x4F:      /* BDX */
2070         case 0x56:      /* BDX-DE */
2071         case 0x57:      /* KNL */
2072                 return (rapl_dram_energy_units = 15.3 / 1000000);
2073         default:
2074                 return (rapl_energy_units);
2075         }
2076 }
2077
2078
2079 /*
2080  * rapl_probe()
2081  *
2082  * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
2083  */
2084 void rapl_probe(unsigned int family, unsigned int model)
2085 {
2086         unsigned long long msr;
2087         unsigned int time_unit;
2088         double tdp;
2089
2090         if (!genuine_intel)
2091                 return;
2092
2093         if (family != 6)
2094                 return;
2095
2096         switch (model) {
2097         case 0x2A:
2098         case 0x3A:
2099         case 0x3C:      /* HSW */
2100         case 0x45:      /* HSW */
2101         case 0x46:      /* HSW */
2102         case 0x3D:      /* BDW */
2103         case 0x47:      /* BDW */
2104                 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
2105                 break;
2106         case 0x4E:      /* SKL */
2107         case 0x5E:      /* SKL */
2108                 do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
2109                 break;
2110         case 0x3F:      /* HSX */
2111         case 0x4F:      /* BDX */
2112         case 0x56:      /* BDX-DE */
2113         case 0x57:      /* KNL */
2114                 do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
2115                 break;
2116         case 0x2D:
2117         case 0x3E:
2118                 do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_PKG_PERF_STATUS | RAPL_DRAM_PERF_STATUS | RAPL_PKG_POWER_INFO;
2119                 break;
2120         case 0x37:      /* BYT */
2121         case 0x4D:      /* AVN */
2122                 do_rapl = RAPL_PKG | RAPL_CORES ;
2123                 break;
2124         default:
2125                 return;
2126         }
2127
2128         /* units on package 0, verify later other packages match */
2129         if (get_msr(0, MSR_RAPL_POWER_UNIT, &msr))
2130                 return;
2131
2132         rapl_power_units = 1.0 / (1 << (msr & 0xF));
2133         if (model == 0x37)
2134                 rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
2135         else
2136                 rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
2137
2138         rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
2139
2140         time_unit = msr >> 16 & 0xF;
2141         if (time_unit == 0)
2142                 time_unit = 0xA;
2143
2144         rapl_time_units = 1.0 / (1 << (time_unit));
2145
2146         tdp = get_tdp(model);
2147
2148         rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
2149         if (debug)
2150                 fprintf(stderr, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
2151
2152         return;
2153 }
2154
2155 void perf_limit_reasons_probe(family, model)
2156 {
2157         if (!genuine_intel)
2158                 return;
2159
2160         if (family != 6)
2161                 return;
2162
2163         switch (model) {
2164         case 0x3C:      /* HSW */
2165         case 0x45:      /* HSW */
2166         case 0x46:      /* HSW */
2167                 do_gfx_perf_limit_reasons = 1;
2168         case 0x3F:      /* HSX */
2169                 do_core_perf_limit_reasons = 1;
2170                 do_ring_perf_limit_reasons = 1;
2171         default:
2172                 return;
2173         }
2174 }
2175
2176 int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2177 {
2178         unsigned long long msr;
2179         unsigned int dts;
2180         int cpu;
2181
2182         if (!(do_dts || do_ptm))
2183                 return 0;
2184
2185         cpu = t->cpu_id;
2186
2187         /* DTS is per-core, no need to print for each thread */
2188         if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE)) 
2189                 return 0;
2190
2191         if (cpu_migrate(cpu)) {
2192                 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
2193                 return -1;
2194         }
2195
2196         if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
2197                 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
2198                         return 0;
2199
2200                 dts = (msr >> 16) & 0x7F;
2201                 fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
2202                         cpu, msr, tcc_activation_temp - dts);
2203
2204 #ifdef  THERM_DEBUG
2205                 if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
2206                         return 0;
2207
2208                 dts = (msr >> 16) & 0x7F;
2209                 dts2 = (msr >> 8) & 0x7F;
2210                 fprintf(stderr, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
2211                         cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
2212 #endif
2213         }
2214
2215
2216         if (do_dts) {
2217                 unsigned int resolution;
2218
2219                 if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
2220                         return 0;
2221
2222                 dts = (msr >> 16) & 0x7F;
2223                 resolution = (msr >> 27) & 0xF;
2224                 fprintf(stderr, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
2225                         cpu, msr, tcc_activation_temp - dts, resolution);
2226
2227 #ifdef THERM_DEBUG
2228                 if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
2229                         return 0;
2230
2231                 dts = (msr >> 16) & 0x7F;
2232                 dts2 = (msr >> 8) & 0x7F;
2233                 fprintf(stderr, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
2234                         cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
2235 #endif
2236         }
2237
2238         return 0;
2239 }
2240         
2241 void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
2242 {
2243         fprintf(stderr, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
2244                 cpu, label,
2245                 ((msr >> 15) & 1) ? "EN" : "DIS",
2246                 ((msr >> 0) & 0x7FFF) * rapl_power_units,
2247                 (1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
2248                 (((msr >> 16) & 1) ? "EN" : "DIS"));
2249
2250         return;
2251 }
2252
2253 int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2254 {
2255         unsigned long long msr;
2256         int cpu;
2257
2258         if (!do_rapl)
2259                 return 0;
2260
2261         /* RAPL counters are per package, so print only for 1st thread/package */
2262         if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2263                 return 0;
2264
2265         cpu = t->cpu_id;
2266         if (cpu_migrate(cpu)) {
2267                 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
2268                 return -1;
2269         }
2270
2271         if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
2272                 return -1;
2273
2274         if (debug) {
2275                 fprintf(stderr, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx "
2276                         "(%f Watts, %f Joules, %f sec.)\n", cpu, msr,
2277                         rapl_power_units, rapl_energy_units, rapl_time_units);
2278         }
2279         if (do_rapl & RAPL_PKG_POWER_INFO) {
2280
2281                 if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
2282                         return -5;
2283
2284
2285                 fprintf(stderr, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
2286                         cpu, msr,
2287                         ((msr >>  0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2288                         ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2289                         ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2290                         ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
2291
2292         }
2293         if (do_rapl & RAPL_PKG) {
2294
2295                 if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
2296                         return -9;
2297
2298                 fprintf(stderr, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
2299                         cpu, msr, (msr >> 63) & 1 ? "": "UN");
2300
2301                 print_power_limit_msr(cpu, msr, "PKG Limit #1");
2302                 fprintf(stderr, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
2303                         cpu,
2304                         ((msr >> 47) & 1) ? "EN" : "DIS",
2305                         ((msr >> 32) & 0x7FFF) * rapl_power_units,
2306                         (1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
2307                         ((msr >> 48) & 1) ? "EN" : "DIS");
2308         }
2309
2310         if (do_rapl & RAPL_DRAM_POWER_INFO) {
2311                 if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
2312                         return -6;
2313
2314                 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
2315                         cpu, msr,
2316                         ((msr >>  0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2317                         ((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2318                         ((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
2319                         ((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
2320         }
2321         if (do_rapl & RAPL_DRAM) {
2322                 if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
2323                         return -9;
2324                 fprintf(stderr, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
2325                                 cpu, msr, (msr >> 31) & 1 ? "": "UN");
2326
2327                 print_power_limit_msr(cpu, msr, "DRAM Limit");
2328         }
2329         if (do_rapl & RAPL_CORE_POLICY) {
2330                 if (debug) {
2331                         if (get_msr(cpu, MSR_PP0_POLICY, &msr))
2332                                 return -7;
2333
2334                         fprintf(stderr, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
2335                 }
2336         }
2337         if (do_rapl & RAPL_CORES) {
2338                 if (debug) {
2339
2340                         if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
2341                                 return -9;
2342                         fprintf(stderr, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
2343                                         cpu, msr, (msr >> 31) & 1 ? "": "UN");
2344                         print_power_limit_msr(cpu, msr, "Cores Limit");
2345                 }
2346         }
2347         if (do_rapl & RAPL_GFX) {
2348                 if (debug) {
2349                         if (get_msr(cpu, MSR_PP1_POLICY, &msr))
2350                                 return -8;
2351
2352                         fprintf(stderr, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
2353
2354                         if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
2355                                 return -9;
2356                         fprintf(stderr, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
2357                                         cpu, msr, (msr >> 31) & 1 ? "": "UN");
2358                         print_power_limit_msr(cpu, msr, "GFX Limit");
2359                 }
2360         }
2361         return 0;
2362 }
2363
2364 /*
2365  * SNB adds support for additional MSRs:
2366  *
2367  * MSR_PKG_C7_RESIDENCY            0x000003fa
2368  * MSR_CORE_C7_RESIDENCY           0x000003fe
2369  * MSR_PKG_C2_RESIDENCY            0x0000060d
2370  */
2371
2372 int has_snb_msrs(unsigned int family, unsigned int model)
2373 {
2374         if (!genuine_intel)
2375                 return 0;
2376
2377         switch (model) {
2378         case 0x2A:
2379         case 0x2D:
2380         case 0x3A:      /* IVB */
2381         case 0x3E:      /* IVB Xeon */
2382         case 0x3C:      /* HSW */
2383         case 0x3F:      /* HSW */
2384         case 0x45:      /* HSW */
2385         case 0x46:      /* HSW */
2386         case 0x3D:      /* BDW */
2387         case 0x47:      /* BDW */
2388         case 0x4F:      /* BDX */
2389         case 0x56:      /* BDX-DE */
2390         case 0x4E:      /* SKL */
2391         case 0x5E:      /* SKL */
2392                 return 1;
2393         }
2394         return 0;
2395 }
2396
2397 /*
2398  * HSW adds support for additional MSRs:
2399  *
2400  * MSR_PKG_C8_RESIDENCY            0x00000630
2401  * MSR_PKG_C9_RESIDENCY            0x00000631
2402  * MSR_PKG_C10_RESIDENCY           0x00000632
2403  */
2404 int has_hsw_msrs(unsigned int family, unsigned int model)
2405 {
2406         if (!genuine_intel)
2407                 return 0;
2408
2409         switch (model) {
2410         case 0x45:      /* HSW */
2411         case 0x3D:      /* BDW */
2412         case 0x4E:      /* SKL */
2413         case 0x5E:      /* SKL */
2414                 return 1;
2415         }
2416         return 0;
2417 }
2418
2419 /*
2420  * SKL adds support for additional MSRS:
2421  *
2422  * MSR_PKG_WEIGHTED_CORE_C0_RES    0x00000658
2423  * MSR_PKG_ANY_CORE_C0_RES         0x00000659
2424  * MSR_PKG_ANY_GFXE_C0_RES         0x0000065A
2425  * MSR_PKG_BOTH_CORE_GFXE_C0_RES   0x0000065B
2426  */
2427 int has_skl_msrs(unsigned int family, unsigned int model)
2428 {
2429         if (!genuine_intel)
2430                 return 0;
2431
2432         switch (model) {
2433         case 0x4E:      /* SKL */
2434         case 0x5E:      /* SKL */
2435                 return 1;
2436         }
2437         return 0;
2438 }
2439
2440
2441
2442 int is_slm(unsigned int family, unsigned int model)
2443 {
2444         if (!genuine_intel)
2445                 return 0;
2446         switch (model) {
2447         case 0x37:      /* BYT */
2448         case 0x4D:      /* AVN */
2449                 return 1;
2450         }
2451         return 0;
2452 }
2453
2454 int is_knl(unsigned int family, unsigned int model)
2455 {
2456         if (!genuine_intel)
2457                 return 0;
2458         switch (model) {
2459         case 0x57:      /* KNL */
2460                 return 1;
2461         }
2462         return 0;
2463 }
2464
2465 #define SLM_BCLK_FREQS 5
2466 double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
2467
2468 double slm_bclk(void)
2469 {
2470         unsigned long long msr = 3;
2471         unsigned int i;
2472         double freq;
2473
2474         if (get_msr(0, MSR_FSB_FREQ, &msr))
2475                 fprintf(stderr, "SLM BCLK: unknown\n");
2476
2477         i = msr & 0xf;
2478         if (i >= SLM_BCLK_FREQS) {
2479                 fprintf(stderr, "SLM BCLK[%d] invalid\n", i);
2480                 msr = 3;
2481         }
2482         freq = slm_freq_table[i];
2483
2484         fprintf(stderr, "SLM BCLK: %.1f Mhz\n", freq);
2485
2486         return freq;
2487 }
2488
2489 double discover_bclk(unsigned int family, unsigned int model)
2490 {
2491         if (has_snb_msrs(family, model))
2492                 return 100.00;
2493         else if (is_slm(family, model))
2494                 return slm_bclk();
2495         else
2496                 return 133.33;
2497 }
2498
2499 /*
2500  * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
2501  * the Thermal Control Circuit (TCC) activates.
2502  * This is usually equal to tjMax.
2503  *
2504  * Older processors do not have this MSR, so there we guess,
2505  * but also allow cmdline over-ride with -T.
2506  *
2507  * Several MSR temperature values are in units of degrees-C
2508  * below this value, including the Digital Thermal Sensor (DTS),
2509  * Package Thermal Management Sensor (PTM), and thermal event thresholds.
2510  */
2511 int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2512 {
2513         unsigned long long msr;
2514         unsigned int target_c_local;
2515         int cpu;
2516
2517         /* tcc_activation_temp is used only for dts or ptm */
2518         if (!(do_dts || do_ptm))
2519                 return 0;
2520
2521         /* this is a per-package concept */
2522         if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2523                 return 0;
2524
2525         cpu = t->cpu_id;
2526         if (cpu_migrate(cpu)) {
2527                 fprintf(stderr, "Could not migrate to CPU %d\n", cpu);
2528                 return -1;
2529         }
2530
2531         if (tcc_activation_temp_override != 0) {
2532                 tcc_activation_temp = tcc_activation_temp_override;
2533                 fprintf(stderr, "cpu%d: Using cmdline TCC Target (%d C)\n",
2534                         cpu, tcc_activation_temp);
2535                 return 0;
2536         }
2537
2538         /* Temperature Target MSR is Nehalem and newer only */
2539         if (!do_nhm_platform_info)
2540                 goto guess;
2541
2542         if (get_msr(0, MSR_IA32_TEMPERATURE_TARGET, &msr))
2543                 goto guess;
2544
2545         target_c_local = (msr >> 16) & 0xFF;
2546
2547         if (debug)
2548                 fprintf(stderr, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
2549                         cpu, msr, target_c_local);
2550
2551         if (!target_c_local)
2552                 goto guess;
2553
2554         tcc_activation_temp = target_c_local;
2555
2556         return 0;
2557
2558 guess:
2559         tcc_activation_temp = TJMAX_DEFAULT;
2560         fprintf(stderr, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
2561                 cpu, tcc_activation_temp);
2562
2563         return 0;
2564 }
2565 void process_cpuid()
2566 {
2567         unsigned int eax, ebx, ecx, edx, max_level;
2568         unsigned int fms, family, model, stepping;
2569
2570         eax = ebx = ecx = edx = 0;
2571
2572         __get_cpuid(0, &max_level, &ebx, &ecx, &edx);
2573
2574         if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
2575                 genuine_intel = 1;
2576
2577         if (debug)
2578                 fprintf(stderr, "CPUID(0): %.4s%.4s%.4s ",
2579                         (char *)&ebx, (char *)&edx, (char *)&ecx);
2580
2581         __get_cpuid(1, &fms, &ebx, &ecx, &edx);
2582         family = (fms >> 8) & 0xf;
2583         model = (fms >> 4) & 0xf;
2584         stepping = fms & 0xf;
2585         if (family == 6 || family == 0xf)
2586                 model += ((fms >> 16) & 0xf) << 4;
2587
2588         if (debug)
2589                 fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
2590                         max_level, family, model, stepping, family, model, stepping);
2591
2592         if (!(edx & (1 << 5)))
2593                 errx(1, "CPUID: no MSR");
2594
2595         /*
2596          * check max extended function levels of CPUID.
2597          * This is needed to check for invariant TSC.
2598          * This check is valid for both Intel and AMD.
2599          */
2600         ebx = ecx = edx = 0;
2601         __get_cpuid(0x80000000, &max_level, &ebx, &ecx, &edx);
2602
2603         if (max_level >= 0x80000007) {
2604
2605                 /*
2606                  * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
2607                  * this check is valid for both Intel and AMD
2608                  */
2609                 __get_cpuid(0x80000007, &eax, &ebx, &ecx, &edx);
2610                 has_invariant_tsc = edx & (1 << 8);
2611         }
2612
2613         /*
2614          * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
2615          * this check is valid for both Intel and AMD
2616          */
2617
2618         __get_cpuid(0x6, &eax, &ebx, &ecx, &edx);
2619         has_aperf = ecx & (1 << 0);
2620         do_dts = eax & (1 << 0);
2621         do_ptm = eax & (1 << 6);
2622         has_epb = ecx & (1 << 3);
2623
2624         if (debug)
2625                 fprintf(stderr, "CPUID(6): %sAPERF, %sDTS, %sPTM, %sEPB\n",
2626                         has_aperf ? "" : "No ",
2627                         do_dts ? "" : "No ",
2628                         do_ptm ? "" : "No ",
2629                         has_epb ? "" : "No ");
2630
2631         if (max_level > 0x15) {
2632                 unsigned int eax_crystal;
2633                 unsigned int ebx_tsc;
2634
2635                 /*
2636                  * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
2637                  */
2638                 eax_crystal = ebx_tsc = crystal_hz = edx = 0;
2639                 __get_cpuid(0x15, &eax_crystal, &ebx_tsc, &crystal_hz, &edx);
2640
2641                 if (ebx_tsc != 0) {
2642
2643                         if (debug && (ebx != 0))
2644                                 fprintf(stderr, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
2645                                         eax_crystal, ebx_tsc, crystal_hz);
2646
2647                         if (crystal_hz == 0)
2648                                 switch(model) {
2649                                 case 0x4E:      /* SKL */
2650                                 case 0x5E:      /* SKL */
2651                                         crystal_hz = 24000000;  /* 24 MHz */
2652                                         break;
2653                                 default:
2654                                         crystal_hz = 0;
2655                         }
2656
2657                         if (crystal_hz) {
2658                                 tsc_hz =  (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
2659                                 if (debug)
2660                                         fprintf(stderr, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
2661                                                 tsc_hz / 1000000, crystal_hz, ebx_tsc,  eax_crystal);
2662                         }
2663                 }
2664         }
2665
2666         do_nhm_platform_info = do_nhm_cstates = do_smi = probe_nhm_msrs(family, model);
2667         do_snb_cstates = has_snb_msrs(family, model);
2668         do_pc2 = do_snb_cstates && (pkg_cstate_limit >= PCL__2);
2669         do_pc3 = (pkg_cstate_limit >= PCL__3);
2670         do_pc6 = (pkg_cstate_limit >= PCL__6);
2671         do_pc7 = do_snb_cstates && (pkg_cstate_limit >= PCL__7);
2672         do_c8_c9_c10 = has_hsw_msrs(family, model);
2673         do_skl_residency = has_skl_msrs(family, model);
2674         do_slm_cstates = is_slm(family, model);
2675         do_knl_cstates  = is_knl(family, model);
2676         bclk = discover_bclk(family, model);
2677
2678         rapl_probe(family, model);
2679         perf_limit_reasons_probe(family, model);
2680
2681         if (debug)
2682                 dump_cstate_pstate_config_info();
2683
2684         return;
2685 }
2686
2687 void help()
2688 {
2689         fprintf(stderr,
2690         "Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
2691         "\n"
2692         "Turbostat forks the specified COMMAND and prints statistics\n"
2693         "when COMMAND completes.\n"
2694         "If no COMMAND is specified, turbostat wakes every 5-seconds\n"
2695         "to print statistics, until interrupted.\n"
2696         "--debug        run in \"debug\" mode\n"
2697         "--interval sec Override default 5-second measurement interval\n"
2698         "--help         print this help message\n"
2699         "--counter msr  print 32-bit counter at address \"msr\"\n"
2700         "--Counter msr  print 64-bit Counter at address \"msr\"\n"
2701         "--msr msr      print 32-bit value at address \"msr\"\n"
2702         "--MSR msr      print 64-bit Value at address \"msr\"\n"
2703         "--version      print version information\n"
2704         "\n"
2705         "For more help, run \"man turbostat\"\n");
2706 }
2707
2708
2709 /*
2710  * in /dev/cpu/ return success for names that are numbers
2711  * ie. filter out ".", "..", "microcode".
2712  */
2713 int dir_filter(const struct dirent *dirp)
2714 {
2715         if (isdigit(dirp->d_name[0]))
2716                 return 1;
2717         else
2718                 return 0;
2719 }
2720
2721 int open_dev_cpu_msr(int dummy1)
2722 {
2723         return 0;
2724 }
2725
2726 void topology_probe()
2727 {
2728         int i;
2729         int max_core_id = 0;
2730         int max_package_id = 0;
2731         int max_siblings = 0;
2732         struct cpu_topology {
2733                 int core_id;
2734                 int physical_package_id;
2735         } *cpus;
2736
2737         /* Initialize num_cpus, max_cpu_num */
2738         topo.num_cpus = 0;
2739         topo.max_cpu_num = 0;
2740         for_all_proc_cpus(count_cpus);
2741         if (!summary_only && topo.num_cpus > 1)
2742                 show_cpu = 1;
2743
2744         if (debug > 1)
2745                 fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
2746
2747         cpus = calloc(1, (topo.max_cpu_num  + 1) * sizeof(struct cpu_topology));
2748         if (cpus == NULL)
2749                 err(1, "calloc cpus");
2750
2751         /*
2752          * Allocate and initialize cpu_present_set
2753          */
2754         cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
2755         if (cpu_present_set == NULL)
2756                 err(3, "CPU_ALLOC");
2757         cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2758         CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
2759         for_all_proc_cpus(mark_cpu_present);
2760
2761         /*
2762          * Allocate and initialize cpu_affinity_set
2763          */
2764         cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
2765         if (cpu_affinity_set == NULL)
2766                 err(3, "CPU_ALLOC");
2767         cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
2768         CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
2769
2770
2771         /*
2772          * For online cpus
2773          * find max_core_id, max_package_id
2774          */
2775         for (i = 0; i <= topo.max_cpu_num; ++i) {
2776                 int siblings;
2777
2778                 if (cpu_is_not_present(i)) {
2779                         if (debug > 1)
2780                                 fprintf(stderr, "cpu%d NOT PRESENT\n", i);
2781                         continue;
2782                 }
2783                 cpus[i].core_id = get_core_id(i);
2784                 if (cpus[i].core_id > max_core_id)
2785                         max_core_id = cpus[i].core_id;
2786
2787                 cpus[i].physical_package_id = get_physical_package_id(i);
2788                 if (cpus[i].physical_package_id > max_package_id)
2789                         max_package_id = cpus[i].physical_package_id;
2790
2791                 siblings = get_num_ht_siblings(i);
2792                 if (siblings > max_siblings)
2793                         max_siblings = siblings;
2794                 if (debug > 1)
2795                         fprintf(stderr, "cpu %d pkg %d core %d\n",
2796                                 i, cpus[i].physical_package_id, cpus[i].core_id);
2797         }
2798         topo.num_cores_per_pkg = max_core_id + 1;
2799         if (debug > 1)
2800                 fprintf(stderr, "max_core_id %d, sizing for %d cores per package\n",
2801                         max_core_id, topo.num_cores_per_pkg);
2802         if (debug && !summary_only && topo.num_cores_per_pkg > 1)
2803                 show_core = 1;
2804
2805         topo.num_packages = max_package_id + 1;
2806         if (debug > 1)
2807                 fprintf(stderr, "max_package_id %d, sizing for %d packages\n",
2808                         max_package_id, topo.num_packages);
2809         if (debug && !summary_only && topo.num_packages > 1)
2810                 show_pkg = 1;
2811
2812         topo.num_threads_per_core = max_siblings;
2813         if (debug > 1)
2814                 fprintf(stderr, "max_siblings %d\n", max_siblings);
2815
2816         free(cpus);
2817 }
2818
2819 void
2820 allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
2821 {
2822         int i;
2823
2824         *t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
2825                 topo.num_packages, sizeof(struct thread_data));
2826         if (*t == NULL)
2827                 goto error;
2828
2829         for (i = 0; i < topo.num_threads_per_core *
2830                 topo.num_cores_per_pkg * topo.num_packages; i++)
2831                 (*t)[i].cpu_id = -1;
2832
2833         *c = calloc(topo.num_cores_per_pkg * topo.num_packages,
2834                 sizeof(struct core_data));
2835         if (*c == NULL)
2836                 goto error;
2837
2838         for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
2839                 (*c)[i].core_id = -1;
2840
2841         *p = calloc(topo.num_packages, sizeof(struct pkg_data));
2842         if (*p == NULL)
2843                 goto error;
2844
2845         for (i = 0; i < topo.num_packages; i++)
2846                 (*p)[i].package_id = i;
2847
2848         return;
2849 error:
2850         err(1, "calloc counters");
2851 }
2852 /*
2853  * init_counter()
2854  *
2855  * set cpu_id, core_num, pkg_num
2856  * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
2857  *
2858  * increment topo.num_cores when 1st core in pkg seen
2859  */
2860 void init_counter(struct thread_data *thread_base, struct core_data *core_base,
2861         struct pkg_data *pkg_base, int thread_num, int core_num,
2862         int pkg_num, int cpu_id)
2863 {
2864         struct thread_data *t;
2865         struct core_data *c;
2866         struct pkg_data *p;
2867
2868         t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
2869         c = GET_CORE(core_base, core_num, pkg_num);
2870         p = GET_PKG(pkg_base, pkg_num);
2871
2872         t->cpu_id = cpu_id;
2873         if (thread_num == 0) {
2874                 t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
2875                 if (cpu_is_first_core_in_package(cpu_id))
2876                         t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
2877         }
2878
2879         c->core_id = core_num;
2880         p->package_id = pkg_num;
2881 }
2882
2883
2884 int initialize_counters(int cpu_id)
2885 {
2886         int my_thread_id, my_core_id, my_package_id;
2887
2888         my_package_id = get_physical_package_id(cpu_id);
2889         my_core_id = get_core_id(cpu_id);
2890         my_thread_id = get_cpu_position_in_core(cpu_id);
2891         if (!my_thread_id)
2892                 topo.num_cores++;
2893
2894         init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2895         init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
2896         return 0;
2897 }
2898
2899 void allocate_output_buffer()
2900 {
2901         output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
2902         outp = output_buffer;
2903         if (outp == NULL)
2904                 err(-1, "calloc output buffer");
2905 }
2906
2907 void setup_all_buffers(void)
2908 {
2909         topology_probe();
2910         allocate_counters(&thread_even, &core_even, &package_even);
2911         allocate_counters(&thread_odd, &core_odd, &package_odd);
2912         allocate_output_buffer();
2913         for_all_proc_cpus(initialize_counters);
2914 }
2915
2916 void turbostat_init()
2917 {
2918         check_dev_msr();
2919         check_permissions();
2920         process_cpuid();
2921
2922         setup_all_buffers();
2923
2924         if (debug)
2925                 for_all_cpus(print_epb, ODD_COUNTERS);
2926
2927         if (debug)
2928                 for_all_cpus(print_perf_limit, ODD_COUNTERS);
2929
2930         if (debug)
2931                 for_all_cpus(print_rapl, ODD_COUNTERS);
2932
2933         for_all_cpus(set_temperature_target, ODD_COUNTERS);
2934
2935         if (debug)
2936                 for_all_cpus(print_thermal, ODD_COUNTERS);
2937 }
2938
2939 int fork_it(char **argv)
2940 {
2941         pid_t child_pid;
2942         int status;
2943
2944         status = for_all_cpus(get_counters, EVEN_COUNTERS);
2945         if (status)
2946                 exit(status);
2947         /* clear affinity side-effect of get_counters() */
2948         sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
2949         gettimeofday(&tv_even, (struct timezone *)NULL);
2950
2951         child_pid = fork();
2952         if (!child_pid) {
2953                 /* child */
2954                 execvp(argv[0], argv);
2955         } else {
2956
2957                 /* parent */
2958                 if (child_pid == -1)
2959                         err(1, "fork");
2960
2961                 signal(SIGINT, SIG_IGN);
2962                 signal(SIGQUIT, SIG_IGN);
2963                 if (waitpid(child_pid, &status, 0) == -1)
2964                         err(status, "waitpid");
2965         }
2966         /*
2967          * n.b. fork_it() does not check for errors from for_all_cpus()
2968          * because re-starting is problematic when forking
2969          */
2970         for_all_cpus(get_counters, ODD_COUNTERS);
2971         gettimeofday(&tv_odd, (struct timezone *)NULL);
2972         timersub(&tv_odd, &tv_even, &tv_delta);
2973         for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS);
2974         compute_average(EVEN_COUNTERS);
2975         format_all_counters(EVEN_COUNTERS);
2976         flush_stderr();
2977
2978         fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
2979
2980         return status;
2981 }
2982
2983 int get_and_dump_counters(void)
2984 {
2985         int status;
2986
2987         status = for_all_cpus(get_counters, ODD_COUNTERS);
2988         if (status)
2989                 return status;
2990
2991         status = for_all_cpus(dump_counters, ODD_COUNTERS);
2992         if (status)
2993                 return status;
2994
2995         flush_stdout();
2996
2997         return status;
2998 }
2999
3000 void print_version() {
3001         fprintf(stderr, "turbostat version 4.5 2 Apr, 2015"
3002                 " - Len Brown <lenb@kernel.org>\n");
3003 }
3004
3005 void cmdline(int argc, char **argv)
3006 {
3007         int opt;
3008         int option_index = 0;
3009         static struct option long_options[] = {
3010                 {"Counter",     required_argument,      0, 'C'},
3011                 {"counter",     required_argument,      0, 'c'},
3012                 {"Dump",        no_argument,            0, 'D'},
3013                 {"debug",       no_argument,            0, 'd'},
3014                 {"interval",    required_argument,      0, 'i'},
3015                 {"help",        no_argument,            0, 'h'},
3016                 {"Joules",      no_argument,            0, 'J'},
3017                 {"MSR",         required_argument,      0, 'M'},
3018                 {"msr",         required_argument,      0, 'm'},
3019                 {"Package",     no_argument,            0, 'p'},
3020                 {"processor",   no_argument,            0, 'p'},
3021                 {"Summary",     no_argument,            0, 'S'},
3022                 {"TCC",         required_argument,      0, 'T'},
3023                 {"version",     no_argument,            0, 'v' },
3024                 {0,             0,                      0,  0 }
3025         };
3026
3027         progname = argv[0];
3028
3029         while ((opt = getopt_long_only(argc, argv, "C:c:Ddhi:JM:m:PpST:v",
3030                                 long_options, &option_index)) != -1) {
3031                 switch (opt) {
3032                 case 'C':
3033                         sscanf(optarg, "%x", &extra_delta_offset64);
3034                         break;
3035                 case 'c':
3036                         sscanf(optarg, "%x", &extra_delta_offset32);
3037                         break;
3038                 case 'D':
3039                         dump_only++;
3040                         break;
3041                 case 'd':
3042                         debug++;
3043                         break;
3044                 case 'h':
3045                 default:
3046                         help();
3047                         exit(1);
3048                 case 'i':
3049                         interval_sec = atoi(optarg);
3050                         break;
3051                 case 'J':
3052                         rapl_joules++;
3053                         break;
3054                 case 'M':
3055                         sscanf(optarg, "%x", &extra_msr_offset64);
3056                         break;
3057                 case 'm':
3058                         sscanf(optarg, "%x", &extra_msr_offset32);
3059                         break;
3060                 case 'P':
3061                         show_pkg_only++;
3062                         break;
3063                 case 'p':
3064                         show_core_only++;
3065                         break;
3066                 case 'S':
3067                         summary_only++;
3068                         break;
3069                 case 'T':
3070                         tcc_activation_temp_override = atoi(optarg);
3071                         break;
3072                 case 'v':
3073                         print_version();
3074                         exit(0);
3075                         break;
3076                 }
3077         }
3078 }
3079
3080 int main(int argc, char **argv)
3081 {
3082         cmdline(argc, argv);
3083
3084         if (debug)
3085                 print_version();
3086
3087         turbostat_init();
3088
3089         /* dump counters and exit */
3090         if (dump_only)
3091                 return get_and_dump_counters();
3092
3093         /*
3094          * if any params left, it must be a command to fork
3095          */
3096         if (argc - optind)
3097                 return fork_it(argv + optind);
3098         else
3099                 turbostat_loop();
3100
3101         return 0;
3102 }