aboutsummaryrefslogtreecommitdiffstats
path: root/tools/power/x86/turbostat/turbostat.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/power/x86/turbostat/turbostat.c')
-rw-r--r--tools/power/x86/turbostat/turbostat.c200
1 files changed, 98 insertions, 102 deletions
diff --git a/tools/power/x86/turbostat/turbostat.c b/tools/power/x86/turbostat/turbostat.c
index 4c6983de6fd9..362a0cb448db 100644
--- a/tools/power/x86/turbostat/turbostat.c
+++ b/tools/power/x86/turbostat/turbostat.c
@@ -72,7 +72,7 @@ int need_reinitialize;
72 72
73int num_cpus; 73int num_cpus;
74 74
75typedef struct per_cpu_counters { 75struct counters {
76 unsigned long long tsc; /* per thread */ 76 unsigned long long tsc; /* per thread */
77 unsigned long long aperf; /* per thread */ 77 unsigned long long aperf; /* per thread */
78 unsigned long long mperf; /* per thread */ 78 unsigned long long mperf; /* per thread */
@@ -88,13 +88,13 @@ typedef struct per_cpu_counters {
88 int pkg; 88 int pkg;
89 int core; 89 int core;
90 int cpu; 90 int cpu;
91 struct per_cpu_counters *next; 91 struct counters *next;
92} PCC; 92};
93 93
94PCC *pcc_even; 94struct counters *cnt_even;
95PCC *pcc_odd; 95struct counters *cnt_odd;
96PCC *pcc_delta; 96struct counters *cnt_delta;
97PCC *pcc_average; 97struct counters *cnt_average;
98struct timeval tv_even; 98struct timeval tv_even;
99struct timeval tv_odd; 99struct timeval tv_odd;
100struct timeval tv_delta; 100struct timeval tv_delta;
@@ -125,7 +125,7 @@ unsigned long long get_msr(int cpu, off_t offset)
125 return msr; 125 return msr;
126} 126}
127 127
128void print_header() 128void print_header(void)
129{ 129{
130 if (show_pkg) 130 if (show_pkg)
131 fprintf(stderr, "pkg "); 131 fprintf(stderr, "pkg ");
@@ -160,39 +160,39 @@ void print_header()
160 putc('\n', stderr); 160 putc('\n', stderr);
161} 161}
162 162
163void dump_pcc(PCC *pcc) 163void dump_cnt(struct counters *cnt)
164{ 164{
165 fprintf(stderr, "package: %d ", pcc->pkg); 165 fprintf(stderr, "package: %d ", cnt->pkg);
166 fprintf(stderr, "core:: %d ", pcc->core); 166 fprintf(stderr, "core:: %d ", cnt->core);
167 fprintf(stderr, "CPU: %d ", pcc->cpu); 167 fprintf(stderr, "CPU: %d ", cnt->cpu);
168 fprintf(stderr, "TSC: %016llX\n", pcc->tsc); 168 fprintf(stderr, "TSC: %016llX\n", cnt->tsc);
169 fprintf(stderr, "c3: %016llX\n", pcc->c3); 169 fprintf(stderr, "c3: %016llX\n", cnt->c3);
170 fprintf(stderr, "c6: %016llX\n", pcc->c6); 170 fprintf(stderr, "c6: %016llX\n", cnt->c6);
171 fprintf(stderr, "c7: %016llX\n", pcc->c7); 171 fprintf(stderr, "c7: %016llX\n", cnt->c7);
172 fprintf(stderr, "aperf: %016llX\n", pcc->aperf); 172 fprintf(stderr, "aperf: %016llX\n", cnt->aperf);
173 fprintf(stderr, "pc2: %016llX\n", pcc->pc2); 173 fprintf(stderr, "pc2: %016llX\n", cnt->pc2);
174 fprintf(stderr, "pc3: %016llX\n", pcc->pc3); 174 fprintf(stderr, "pc3: %016llX\n", cnt->pc3);
175 fprintf(stderr, "pc6: %016llX\n", pcc->pc6); 175 fprintf(stderr, "pc6: %016llX\n", cnt->pc6);
176 fprintf(stderr, "pc7: %016llX\n", pcc->pc7); 176 fprintf(stderr, "pc7: %016llX\n", cnt->pc7);
177 fprintf(stderr, "msr0x%x: %016llX\n", extra_msr_offset, pcc->extra_msr); 177 fprintf(stderr, "msr0x%x: %016llX\n", extra_msr_offset, cnt->extra_msr);
178} 178}
179 179
180void dump_list(PCC *pcc) 180void dump_list(struct counters *cnt)
181{ 181{
182 printf("dump_list 0x%p\n", pcc); 182 printf("dump_list 0x%p\n", cnt);
183 183
184 for (; pcc; pcc = pcc->next) 184 for (; cnt; cnt = cnt->next)
185 dump_pcc(pcc); 185 dump_cnt(cnt);
186} 186}
187 187
188void print_pcc(PCC *p) 188void print_cnt(struct counters *p)
189{ 189{
190 double interval_float; 190 double interval_float;
191 191
192 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0; 192 interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
193 193
194 /* topology columns, print blanks on 1st (average) line */ 194 /* topology columns, print blanks on 1st (average) line */
195 if (p == pcc_average) { 195 if (p == cnt_average) {
196 if (show_pkg) 196 if (show_pkg)
197 fprintf(stderr, " "); 197 fprintf(stderr, " ");
198 if (show_core) 198 if (show_core)
@@ -262,24 +262,24 @@ void print_pcc(PCC *p)
262 putc('\n', stderr); 262 putc('\n', stderr);
263} 263}
264 264
265void print_counters(PCC *cnt) 265void print_counters(struct counters *counters)
266{ 266{
267 PCC *pcc; 267 struct counters *cnt;
268 268
269 print_header(); 269 print_header();
270 270
271 if (num_cpus > 1) 271 if (num_cpus > 1)
272 print_pcc(pcc_average); 272 print_cnt(cnt_average);
273 273
274 for (pcc = cnt; pcc != NULL; pcc = pcc->next) 274 for (cnt = counters; cnt != NULL; cnt = cnt->next)
275 print_pcc(pcc); 275 print_cnt(cnt);
276 276
277} 277}
278 278
279#define SUBTRACT_COUNTER(after, before, delta) (delta = (after - before), (before > after)) 279#define SUBTRACT_COUNTER(after, before, delta) (delta = (after - before), (before > after))
280 280
281 281int compute_delta(struct counters *after,
282int compute_delta(PCC *after, PCC *before, PCC *delta) 282 struct counters *before, struct counters *delta)
283{ 283{
284 int errors = 0; 284 int errors = 0;
285 int perf_err = 0; 285 int perf_err = 0;
@@ -391,20 +391,20 @@ int compute_delta(PCC *after, PCC *before, PCC *delta)
391 delta->extra_msr = after->extra_msr; 391 delta->extra_msr = after->extra_msr;
392 if (errors) { 392 if (errors) {
393 fprintf(stderr, "ERROR cpu%d before:\n", before->cpu); 393 fprintf(stderr, "ERROR cpu%d before:\n", before->cpu);
394 dump_pcc(before); 394 dump_cnt(before);
395 fprintf(stderr, "ERROR cpu%d after:\n", before->cpu); 395 fprintf(stderr, "ERROR cpu%d after:\n", before->cpu);
396 dump_pcc(after); 396 dump_cnt(after);
397 errors = 0; 397 errors = 0;
398 } 398 }
399 } 399 }
400 return 0; 400 return 0;
401} 401}
402 402
403void compute_average(PCC *delta, PCC *avg) 403void compute_average(struct counters *delta, struct counters *avg)
404{ 404{
405 PCC *sum; 405 struct counters *sum;
406 406
407 sum = calloc(1, sizeof(PCC)); 407 sum = calloc(1, sizeof(struct counters));
408 if (sum == NULL) { 408 if (sum == NULL) {
409 perror("calloc sum"); 409 perror("calloc sum");
410 exit(1); 410 exit(1);
@@ -438,35 +438,34 @@ void compute_average(PCC *delta, PCC *avg)
438 free(sum); 438 free(sum);
439} 439}
440 440
441void get_counters(PCC *pcc) 441void get_counters(struct counters *cnt)
442{ 442{
443 for ( ; pcc; pcc = pcc->next) { 443 for ( ; cnt; cnt = cnt->next) {
444 pcc->tsc = get_msr(pcc->cpu, MSR_TSC); 444 cnt->tsc = get_msr(cnt->cpu, MSR_TSC);
445 if (do_nhm_cstates) 445 if (do_nhm_cstates)
446 pcc->c3 = get_msr(pcc->cpu, MSR_CORE_C3_RESIDENCY); 446 cnt->c3 = get_msr(cnt->cpu, MSR_CORE_C3_RESIDENCY);
447 if (do_nhm_cstates) 447 if (do_nhm_cstates)
448 pcc->c6 = get_msr(pcc->cpu, MSR_CORE_C6_RESIDENCY); 448 cnt->c6 = get_msr(cnt->cpu, MSR_CORE_C6_RESIDENCY);
449 if (do_snb_cstates) 449 if (do_snb_cstates)
450 pcc->c7 = get_msr(pcc->cpu, MSR_CORE_C7_RESIDENCY); 450 cnt->c7 = get_msr(cnt->cpu, MSR_CORE_C7_RESIDENCY);
451 if (has_aperf) 451 if (has_aperf)
452 pcc->aperf = get_msr(pcc->cpu, MSR_APERF); 452 cnt->aperf = get_msr(cnt->cpu, MSR_APERF);
453 if (has_aperf) 453 if (has_aperf)
454 pcc->mperf = get_msr(pcc->cpu, MSR_MPERF); 454 cnt->mperf = get_msr(cnt->cpu, MSR_MPERF);
455 if (do_snb_cstates) 455 if (do_snb_cstates)
456 pcc->pc2 = get_msr(pcc->cpu, MSR_PKG_C2_RESIDENCY); 456 cnt->pc2 = get_msr(cnt->cpu, MSR_PKG_C2_RESIDENCY);
457 if (do_nhm_cstates) 457 if (do_nhm_cstates)
458 pcc->pc3 = get_msr(pcc->cpu, MSR_PKG_C3_RESIDENCY); 458 cnt->pc3 = get_msr(cnt->cpu, MSR_PKG_C3_RESIDENCY);
459 if (do_nhm_cstates) 459 if (do_nhm_cstates)
460 pcc->pc6 = get_msr(pcc->cpu, MSR_PKG_C6_RESIDENCY); 460 cnt->pc6 = get_msr(cnt->cpu, MSR_PKG_C6_RESIDENCY);
461 if (do_snb_cstates) 461 if (do_snb_cstates)
462 pcc->pc7 = get_msr(pcc->cpu, MSR_PKG_C7_RESIDENCY); 462 cnt->pc7 = get_msr(cnt->cpu, MSR_PKG_C7_RESIDENCY);
463 if (extra_msr_offset) 463 if (extra_msr_offset)
464 pcc->extra_msr = get_msr(pcc->cpu, extra_msr_offset); 464 cnt->extra_msr = get_msr(cnt->cpu, extra_msr_offset);
465 } 465 }
466} 466}
467 467
468 468void print_nehalem_info(void)
469void print_nehalem_info()
470{ 469{
471 unsigned long long msr; 470 unsigned long long msr;
472 unsigned int ratio; 471 unsigned int ratio;
@@ -514,38 +513,38 @@ void print_nehalem_info()
514 513
515} 514}
516 515
517void free_counter_list(PCC *list) 516void free_counter_list(struct counters *list)
518{ 517{
519 PCC *p; 518 struct counters *p;
520 519
521 for (p = list; p; ) { 520 for (p = list; p; ) {
522 PCC *free_me; 521 struct counters *free_me;
523 522
524 free_me = p; 523 free_me = p;
525 p = p->next; 524 p = p->next;
526 free(free_me); 525 free(free_me);
527 } 526 }
528 return;
529} 527}
530 528
531void free_all_counters(void) 529void free_all_counters(void)
532{ 530{
533 free_counter_list(pcc_even); 531 free_counter_list(cnt_even);
534 pcc_even = NULL; 532 cnt_even = NULL;
535 533
536 free_counter_list(pcc_odd); 534 free_counter_list(cnt_odd);
537 pcc_odd = NULL; 535 cnt_odd = NULL;
538 536
539 free_counter_list(pcc_delta); 537 free_counter_list(cnt_delta);
540 pcc_delta = NULL; 538 cnt_delta = NULL;
541 539
542 free_counter_list(pcc_average); 540 free_counter_list(cnt_average);
543 pcc_average = NULL; 541 cnt_average = NULL;
544} 542}
545 543
546void insert_cpu_counters(PCC **list, PCC *new) 544void insert_counters(struct counters **list,
545 struct counters *new)
547{ 546{
548 PCC *prev; 547 struct counters *prev;
549 548
550 /* 549 /*
551 * list was empty 550 * list was empty
@@ -594,18 +593,16 @@ void insert_cpu_counters(PCC **list, PCC *new)
594 */ 593 */
595 new->next = prev->next; 594 new->next = prev->next;
596 prev->next = new; 595 prev->next = new;
597
598 return;
599} 596}
600 597
601void alloc_new_cpu_counters(int pkg, int core, int cpu) 598void alloc_new_counters(int pkg, int core, int cpu)
602{ 599{
603 PCC *new; 600 struct counters *new;
604 601
605 if (verbose > 1) 602 if (verbose > 1)
606 printf("pkg%d core%d, cpu%d\n", pkg, core, cpu); 603 printf("pkg%d core%d, cpu%d\n", pkg, core, cpu);
607 604
608 new = (PCC *)calloc(1, sizeof(PCC)); 605 new = (struct counters *)calloc(1, sizeof(struct counters));
609 if (new == NULL) { 606 if (new == NULL) {
610 perror("calloc"); 607 perror("calloc");
611 exit(1); 608 exit(1);
@@ -613,9 +610,10 @@ void alloc_new_cpu_counters(int pkg, int core, int cpu)
613 new->pkg = pkg; 610 new->pkg = pkg;
614 new->core = core; 611 new->core = core;
615 new->cpu = cpu; 612 new->cpu = cpu;
616 insert_cpu_counters(&pcc_odd, new); 613 insert_counters(&cnt_odd, new);
617 614
618 new = (PCC *)calloc(1, sizeof(PCC)); 615 new = (struct counters *)calloc(1,
616 sizeof(struct counters));
619 if (new == NULL) { 617 if (new == NULL) {
620 perror("calloc"); 618 perror("calloc");
621 exit(1); 619 exit(1);
@@ -623,9 +621,9 @@ void alloc_new_cpu_counters(int pkg, int core, int cpu)
623 new->pkg = pkg; 621 new->pkg = pkg;
624 new->core = core; 622 new->core = core;
625 new->cpu = cpu; 623 new->cpu = cpu;
626 insert_cpu_counters(&pcc_even, new); 624 insert_counters(&cnt_even, new);
627 625
628 new = (PCC *)calloc(1, sizeof(PCC)); 626 new = (struct counters *)calloc(1, sizeof(struct counters));
629 if (new == NULL) { 627 if (new == NULL) {
630 perror("calloc"); 628 perror("calloc");
631 exit(1); 629 exit(1);
@@ -633,9 +631,9 @@ void alloc_new_cpu_counters(int pkg, int core, int cpu)
633 new->pkg = pkg; 631 new->pkg = pkg;
634 new->core = core; 632 new->core = core;
635 new->cpu = cpu; 633 new->cpu = cpu;
636 insert_cpu_counters(&pcc_delta, new); 634 insert_counters(&cnt_delta, new);
637 635
638 new = (PCC *)calloc(1, sizeof(PCC)); 636 new = (struct counters *)calloc(1, sizeof(struct counters));
639 if (new == NULL) { 637 if (new == NULL) {
640 perror("calloc"); 638 perror("calloc");
641 exit(1); 639 exit(1);
@@ -643,7 +641,7 @@ void alloc_new_cpu_counters(int pkg, int core, int cpu)
643 new->pkg = pkg; 641 new->pkg = pkg;
644 new->core = core; 642 new->core = core;
645 new->cpu = cpu; 643 new->cpu = cpu;
646 pcc_average = new; 644 cnt_average = new;
647} 645}
648 646
649int get_physical_package_id(int cpu) 647int get_physical_package_id(int cpu)
@@ -719,7 +717,7 @@ void re_initialize(void)
719{ 717{
720 printf("turbostat: topology changed, re-initializing.\n"); 718 printf("turbostat: topology changed, re-initializing.\n");
721 free_all_counters(); 719 free_all_counters();
722 num_cpus = for_all_cpus(alloc_new_cpu_counters); 720 num_cpus = for_all_cpus(alloc_new_counters);
723 need_reinitialize = 0; 721 need_reinitialize = 0;
724 printf("num_cpus is now %d\n", num_cpus); 722 printf("num_cpus is now %d\n", num_cpus);
725} 723}
@@ -728,7 +726,7 @@ void dummy(int pkg, int core, int cpu) { return; }
728/* 726/*
729 * check to see if a cpu came on-line 727 * check to see if a cpu came on-line
730 */ 728 */
731void verify_num_cpus() 729void verify_num_cpus(void)
732{ 730{
733 int new_num_cpus; 731 int new_num_cpus;
734 732
@@ -740,14 +738,12 @@ void verify_num_cpus()
740 num_cpus, new_num_cpus); 738 num_cpus, new_num_cpus);
741 need_reinitialize = 1; 739 need_reinitialize = 1;
742 } 740 }
743
744 return;
745} 741}
746 742
747void turbostat_loop() 743void turbostat_loop()
748{ 744{
749restart: 745restart:
750 get_counters(pcc_even); 746 get_counters(cnt_even);
751 gettimeofday(&tv_even, (struct timezone *)NULL); 747 gettimeofday(&tv_even, (struct timezone *)NULL);
752 748
753 while (1) { 749 while (1) {
@@ -757,24 +753,24 @@ restart:
757 goto restart; 753 goto restart;
758 } 754 }
759 sleep(interval_sec); 755 sleep(interval_sec);
760 get_counters(pcc_odd); 756 get_counters(cnt_odd);
761 gettimeofday(&tv_odd, (struct timezone *)NULL); 757 gettimeofday(&tv_odd, (struct timezone *)NULL);
762 758
763 compute_delta(pcc_odd, pcc_even, pcc_delta); 759 compute_delta(cnt_odd, cnt_even, cnt_delta);
764 timersub(&tv_odd, &tv_even, &tv_delta); 760 timersub(&tv_odd, &tv_even, &tv_delta);
765 compute_average(pcc_delta, pcc_average); 761 compute_average(cnt_delta, cnt_average);
766 print_counters(pcc_delta); 762 print_counters(cnt_delta);
767 if (need_reinitialize) { 763 if (need_reinitialize) {
768 re_initialize(); 764 re_initialize();
769 goto restart; 765 goto restart;
770 } 766 }
771 sleep(interval_sec); 767 sleep(interval_sec);
772 get_counters(pcc_even); 768 get_counters(cnt_even);
773 gettimeofday(&tv_even, (struct timezone *)NULL); 769 gettimeofday(&tv_even, (struct timezone *)NULL);
774 compute_delta(pcc_even, pcc_odd, pcc_delta); 770 compute_delta(cnt_even, cnt_odd, cnt_delta);
775 timersub(&tv_even, &tv_odd, &tv_delta); 771 timersub(&tv_even, &tv_odd, &tv_delta);
776 compute_average(pcc_delta, pcc_average); 772 compute_average(cnt_delta, cnt_average);
777 print_counters(pcc_delta); 773 print_counters(cnt_delta);
778 } 774 }
779} 775}
780 776
@@ -892,7 +888,7 @@ void check_cpuid()
892 * this check is valid for both Intel and AMD 888 * this check is valid for both Intel and AMD
893 */ 889 */
894 asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000007)); 890 asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x80000007));
895 has_invariant_tsc = edx && (1 << 8); 891 has_invariant_tsc = edx & (1 << 8);
896 892
897 if (!has_invariant_tsc) { 893 if (!has_invariant_tsc) {
898 fprintf(stderr, "No invariant TSC\n"); 894 fprintf(stderr, "No invariant TSC\n");
@@ -905,7 +901,7 @@ void check_cpuid()
905 */ 901 */
906 902
907 asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x6)); 903 asm("cpuid" : "=a" (eax), "=b" (ebx), "=c" (ecx), "=d" (edx) : "a" (0x6));
908 has_aperf = ecx && (1 << 0); 904 has_aperf = ecx & (1 << 0);
909 if (!has_aperf) { 905 if (!has_aperf) {
910 fprintf(stderr, "No APERF MSR\n"); 906 fprintf(stderr, "No APERF MSR\n");
911 exit(1); 907 exit(1);
@@ -952,7 +948,7 @@ void turbostat_init()
952 check_dev_msr(); 948 check_dev_msr();
953 check_super_user(); 949 check_super_user();
954 950
955 num_cpus = for_all_cpus(alloc_new_cpu_counters); 951 num_cpus = for_all_cpus(alloc_new_counters);
956 952
957 if (verbose) 953 if (verbose)
958 print_nehalem_info(); 954 print_nehalem_info();
@@ -962,7 +958,7 @@ int fork_it(char **argv)
962{ 958{
963 int retval; 959 int retval;
964 pid_t child_pid; 960 pid_t child_pid;
965 get_counters(pcc_even); 961 get_counters(cnt_even);
966 gettimeofday(&tv_even, (struct timezone *)NULL); 962 gettimeofday(&tv_even, (struct timezone *)NULL);
967 963
968 child_pid = fork(); 964 child_pid = fork();
@@ -985,14 +981,14 @@ int fork_it(char **argv)
985 exit(1); 981 exit(1);
986 } 982 }
987 } 983 }
988 get_counters(pcc_odd); 984 get_counters(cnt_odd);
989 gettimeofday(&tv_odd, (struct timezone *)NULL); 985 gettimeofday(&tv_odd, (struct timezone *)NULL);
990 retval = compute_delta(pcc_odd, pcc_even, pcc_delta); 986 retval = compute_delta(cnt_odd, cnt_even, cnt_delta);
991 987
992 timersub(&tv_odd, &tv_even, &tv_delta); 988 timersub(&tv_odd, &tv_even, &tv_delta);
993 compute_average(pcc_delta, pcc_average); 989 compute_average(cnt_delta, cnt_average);
994 if (!retval) 990 if (!retval)
995 print_counters(pcc_delta); 991 print_counters(cnt_delta);
996 992
997 fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);; 993 fprintf(stderr, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);;
998 994