-
Notifications
You must be signed in to change notification settings - Fork 102
/
x86_energy_perf_policy.c
1560 lines (1282 loc) · 35.9 KB
/
x86_energy_perf_policy.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0-only
/*
* x86_energy_perf_policy -- set the energy versus performance
* policy preference bias on recent X86 processors.
*/
/*
* Copyright (c) 2010 - 2017 Intel Corporation.
* Len Brown <len.brown@intel.com>
*/
#define _GNU_SOURCE
#include MSRHEADER
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sched.h>
#include <sys/stat.h>
#include <sys/resource.h>
#include <getopt.h>
#include <err.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/time.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <cpuid.h>
#include <errno.h>
#define OPTARG_NORMAL (INT_MAX - 1)
#define OPTARG_POWER (INT_MAX - 2)
#define OPTARG_BALANCE_POWER (INT_MAX - 3)
#define OPTARG_BALANCE_PERFORMANCE (INT_MAX - 4)
#define OPTARG_PERFORMANCE (INT_MAX - 5)
struct msr_hwp_cap {
unsigned char highest;
unsigned char guaranteed;
unsigned char efficient;
unsigned char lowest;
};
struct msr_hwp_request {
unsigned char hwp_min;
unsigned char hwp_max;
unsigned char hwp_desired;
unsigned char hwp_epp;
unsigned int hwp_window;
unsigned char hwp_use_pkg;
} req_update;
unsigned int debug;
unsigned int verbose;
unsigned int force;
char *progname;
int base_cpu;
unsigned char update_epb;
unsigned long long new_epb;
unsigned char turbo_is_enabled;
unsigned char update_turbo;
unsigned char turbo_update_value;
unsigned char update_hwp_epp;
unsigned char update_hwp_min;
unsigned char update_hwp_max;
unsigned char update_hwp_desired;
unsigned char update_hwp_window;
unsigned char update_hwp_use_pkg;
unsigned char update_hwp_enable;
#define hwp_update_enabled() (update_hwp_enable | update_hwp_epp | update_hwp_max | update_hwp_min | update_hwp_desired | update_hwp_window | update_hwp_use_pkg)
int max_cpu_num;
int max_pkg_num;
#define MAX_PACKAGES 64
unsigned int first_cpu_in_pkg[MAX_PACKAGES];
unsigned long long pkg_present_set;
unsigned long long pkg_selected_set;
cpu_set_t *cpu_present_set;
cpu_set_t *cpu_selected_set;
int genuine_intel;
size_t cpu_setsize;
char *proc_stat = "/proc/stat";
unsigned int has_epb; /* MSR_IA32_ENERGY_PERF_BIAS */
unsigned int has_hwp; /* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */
/* IA32_HWP_REQUEST, IA32_HWP_STATUS */
unsigned int has_hwp_notify; /* IA32_HWP_INTERRUPT */
unsigned int has_hwp_activity_window; /* IA32_HWP_REQUEST[bits 41:32] */
unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */
unsigned int has_hwp_request_pkg; /* IA32_HWP_REQUEST_PKG */
unsigned int bdx_highest_ratio;
#define PATH_TO_CPU "/sys/devices/system/cpu/"
#define SYSFS_PATH_MAX 255
/*
* maintain compatibility with original implementation, but don't document it:
*/
void usage(void)
{
fprintf(stderr, "%s [options] [scope][field value]\n", progname);
fprintf(stderr, "scope: --cpu cpu-list [--hwp-use-pkg #] | --pkg pkg-list\n");
fprintf(stderr, "field: --all | --epb | --hwp-epp | --hwp-min | --hwp-max | --hwp-desired\n");
fprintf(stderr, "other: --hwp-enable | --turbo-enable (0 | 1) | --help | --force\n");
fprintf(stderr,
"value: ( # | \"normal\" | \"performance\" | \"balance-performance\" | \"balance-power\"| \"power\")\n");
fprintf(stderr, "--hwp-window usec\n");
fprintf(stderr, "Specify only Energy Performance BIAS (legacy usage):\n");
fprintf(stderr, "%s: [-c cpu] [-v] (-r | policy-value )\n", progname);
exit(1);
}
/*
* If bdx_highest_ratio is set,
* then we must translate between MSR format and simple ratio
* used on the cmdline.
*/
int ratio_2_msr_perf(int ratio)
{
int msr_perf;
if (!bdx_highest_ratio)
return ratio;
msr_perf = ratio * 255 / bdx_highest_ratio;
if (debug)
fprintf(stderr, "%d = ratio_to_msr_perf(%d)\n", msr_perf, ratio);
return msr_perf;
}
int msr_perf_2_ratio(int msr_perf)
{
int ratio;
double d;
if (!bdx_highest_ratio)
return msr_perf;
d = (double)msr_perf * (double) bdx_highest_ratio / 255.0;
d = d + 0.5; /* round */
ratio = (int)d;
if (debug)
fprintf(stderr, "%d = msr_perf_ratio(%d) {%f}\n", ratio, msr_perf, d);
return ratio;
}
int parse_cmdline_epb(int i)
{
if (!has_epb)
errx(1, "EPB not enabled on this platform");
update_epb = 1;
switch (i) {
case OPTARG_POWER:
return ENERGY_PERF_BIAS_POWERSAVE;
case OPTARG_BALANCE_POWER:
return ENERGY_PERF_BIAS_BALANCE_POWERSAVE;
case OPTARG_NORMAL:
return ENERGY_PERF_BIAS_NORMAL;
case OPTARG_BALANCE_PERFORMANCE:
return ENERGY_PERF_BIAS_BALANCE_PERFORMANCE;
case OPTARG_PERFORMANCE:
return ENERGY_PERF_BIAS_PERFORMANCE;
}
if (i < 0 || i > ENERGY_PERF_BIAS_POWERSAVE)
errx(1, "--epb must be from 0 to 15");
return i;
}
#define HWP_CAP_LOWEST 0
#define HWP_CAP_HIGHEST 255
/*
* "performance" changes hwp_min to cap.highest
* All others leave it at cap.lowest
*/
int parse_cmdline_hwp_min(int i)
{
update_hwp_min = 1;
switch (i) {
case OPTARG_POWER:
case OPTARG_BALANCE_POWER:
case OPTARG_NORMAL:
case OPTARG_BALANCE_PERFORMANCE:
return HWP_CAP_LOWEST;
case OPTARG_PERFORMANCE:
return HWP_CAP_HIGHEST;
}
return i;
}
/*
* "power" changes hwp_max to cap.lowest
* All others leave it at cap.highest
*/
int parse_cmdline_hwp_max(int i)
{
update_hwp_max = 1;
switch (i) {
case OPTARG_POWER:
return HWP_CAP_LOWEST;
case OPTARG_NORMAL:
case OPTARG_BALANCE_POWER:
case OPTARG_BALANCE_PERFORMANCE:
case OPTARG_PERFORMANCE:
return HWP_CAP_HIGHEST;
}
return i;
}
/*
* for --hwp-des, all strings leave it in autonomous mode
* If you want to change it, you need to explicitly pick a value
*/
int parse_cmdline_hwp_desired(int i)
{
update_hwp_desired = 1;
switch (i) {
case OPTARG_POWER:
case OPTARG_BALANCE_POWER:
case OPTARG_BALANCE_PERFORMANCE:
case OPTARG_NORMAL:
case OPTARG_PERFORMANCE:
return 0; /* autonomous */
}
return i;
}
int parse_cmdline_hwp_window(int i)
{
unsigned int exponent;
update_hwp_window = 1;
switch (i) {
case OPTARG_POWER:
case OPTARG_BALANCE_POWER:
case OPTARG_NORMAL:
case OPTARG_BALANCE_PERFORMANCE:
case OPTARG_PERFORMANCE:
return 0;
}
if (i < 0 || i > 1270000000) {
fprintf(stderr, "--hwp-window: 0 for auto; 1 - 1270000000 usec for window duration\n");
usage();
}
for (exponent = 0; ; ++exponent) {
if (debug)
printf("%d 10^%d\n", i, exponent);
if (i <= 127)
break;
i = i / 10;
}
if (debug)
fprintf(stderr, "%d*10^%d: 0x%x\n", i, exponent, (exponent << 7) | i);
return (exponent << 7) | i;
}
int parse_cmdline_hwp_epp(int i)
{
update_hwp_epp = 1;
switch (i) {
case OPTARG_POWER:
return HWP_EPP_POWERSAVE;
case OPTARG_BALANCE_POWER:
return HWP_EPP_BALANCE_POWERSAVE;
case OPTARG_NORMAL:
case OPTARG_BALANCE_PERFORMANCE:
return HWP_EPP_BALANCE_PERFORMANCE;
case OPTARG_PERFORMANCE:
return HWP_EPP_PERFORMANCE;
}
if (i < 0 || i > 0xff) {
fprintf(stderr, "--hwp-epp must be from 0 to 0xff\n");
usage();
}
return i;
}
int parse_cmdline_turbo(int i)
{
update_turbo = 1;
switch (i) {
case OPTARG_POWER:
return 0;
case OPTARG_NORMAL:
case OPTARG_BALANCE_POWER:
case OPTARG_BALANCE_PERFORMANCE:
case OPTARG_PERFORMANCE:
return 1;
}
if (i < 0 || i > 1) {
fprintf(stderr, "--turbo-enable: 1 to enable, 0 to disable\n");
usage();
}
return i;
}
int parse_optarg_string(char *s)
{
int i;
char *endptr;
if (!strncmp(s, "default", 7))
return OPTARG_NORMAL;
if (!strncmp(s, "normal", 6))
return OPTARG_NORMAL;
if (!strncmp(s, "power", 9))
return OPTARG_POWER;
if (!strncmp(s, "balance-power", 17))
return OPTARG_BALANCE_POWER;
if (!strncmp(s, "balance-performance", 19))
return OPTARG_BALANCE_PERFORMANCE;
if (!strncmp(s, "performance", 11))
return OPTARG_PERFORMANCE;
i = strtol(s, &endptr, 0);
if (s == endptr) {
fprintf(stderr, "no digits in \"%s\"\n", s);
usage();
}
if (i == LONG_MIN || i == LONG_MAX)
errx(-1, "%s", s);
if (i > 0xFF)
errx(-1, "%d (0x%x) must be < 256", i, i);
if (i < 0)
errx(-1, "%d (0x%x) must be >= 0", i, i);
return i;
}
void parse_cmdline_all(char *s)
{
force++;
update_hwp_enable = 1;
req_update.hwp_min = parse_cmdline_hwp_min(parse_optarg_string(s));
req_update.hwp_max = parse_cmdline_hwp_max(parse_optarg_string(s));
req_update.hwp_epp = parse_cmdline_hwp_epp(parse_optarg_string(s));
if (has_epb)
new_epb = parse_cmdline_epb(parse_optarg_string(s));
turbo_update_value = parse_cmdline_turbo(parse_optarg_string(s));
req_update.hwp_desired = parse_cmdline_hwp_desired(parse_optarg_string(s));
req_update.hwp_window = parse_cmdline_hwp_window(parse_optarg_string(s));
}
void validate_cpu_selected_set(void)
{
int cpu;
if (CPU_COUNT_S(cpu_setsize, cpu_selected_set) == 0)
errx(0, "no CPUs requested");
for (cpu = 0; cpu <= max_cpu_num; ++cpu) {
if (CPU_ISSET_S(cpu, cpu_setsize, cpu_selected_set))
if (!CPU_ISSET_S(cpu, cpu_setsize, cpu_present_set))
errx(1, "Requested cpu% is not present", cpu);
}
}
void parse_cmdline_cpu(char *s)
{
char *startp, *endp;
int cpu = 0;
if (pkg_selected_set) {
usage();
errx(1, "--cpu | --pkg");
}
cpu_selected_set = CPU_ALLOC((max_cpu_num + 1));
if (cpu_selected_set == NULL)
err(1, "cpu_selected_set");
CPU_ZERO_S(cpu_setsize, cpu_selected_set);
for (startp = s; startp && *startp;) {
if (*startp == ',') {
startp++;
continue;
}
if (*startp == '-') {
int end_cpu;
startp++;
end_cpu = strtol(startp, &endp, 10);
if (startp == endp)
continue;
while (cpu <= end_cpu) {
if (cpu > max_cpu_num)
errx(1, "Requested cpu%d exceeds max cpu%d", cpu, max_cpu_num);
CPU_SET_S(cpu, cpu_setsize, cpu_selected_set);
cpu++;
}
startp = endp;
continue;
}
if (strncmp(startp, "all", 3) == 0) {
for (cpu = 0; cpu <= max_cpu_num; cpu += 1) {
if (CPU_ISSET_S(cpu, cpu_setsize, cpu_present_set))
CPU_SET_S(cpu, cpu_setsize, cpu_selected_set);
}
startp += 3;
if (*startp == 0)
break;
}
/* "--cpu even" is not documented */
if (strncmp(startp, "even", 4) == 0) {
for (cpu = 0; cpu <= max_cpu_num; cpu += 2) {
if (CPU_ISSET_S(cpu, cpu_setsize, cpu_present_set))
CPU_SET_S(cpu, cpu_setsize, cpu_selected_set);
}
startp += 4;
if (*startp == 0)
break;
}
/* "--cpu odd" is not documented */
if (strncmp(startp, "odd", 3) == 0) {
for (cpu = 1; cpu <= max_cpu_num; cpu += 2) {
if (CPU_ISSET_S(cpu, cpu_setsize, cpu_present_set))
CPU_SET_S(cpu, cpu_setsize, cpu_selected_set);
}
startp += 3;
if (*startp == 0)
break;
}
cpu = strtol(startp, &endp, 10);
if (startp == endp)
errx(1, "--cpu cpu-set: confused by '%s'", startp);
if (cpu > max_cpu_num)
errx(1, "Requested cpu%d exceeds max cpu%d", cpu, max_cpu_num);
CPU_SET_S(cpu, cpu_setsize, cpu_selected_set);
startp = endp;
}
validate_cpu_selected_set();
}
void parse_cmdline_pkg(char *s)
{
char *startp, *endp;
int pkg = 0;
if (cpu_selected_set) {
usage();
errx(1, "--pkg | --cpu");
}
pkg_selected_set = 0;
for (startp = s; startp && *startp;) {
if (*startp == ',') {
startp++;
continue;
}
if (*startp == '-') {
int end_pkg;
startp++;
end_pkg = strtol(startp, &endp, 10);
if (startp == endp)
continue;
while (pkg <= end_pkg) {
if (pkg > max_pkg_num)
errx(1, "Requested pkg%d exceeds max pkg%d", pkg, max_pkg_num);
pkg_selected_set |= 1 << pkg;
pkg++;
}
startp = endp;
continue;
}
if (strncmp(startp, "all", 3) == 0) {
pkg_selected_set = pkg_present_set;
return;
}
pkg = strtol(startp, &endp, 10);
if (pkg > max_pkg_num)
errx(1, "Requested pkg%d Exceeds max pkg%d", pkg, max_pkg_num);
pkg_selected_set |= 1 << pkg;
startp = endp;
}
}
void for_packages(unsigned long long pkg_set, int (func)(int))
{
int pkg_num;
for (pkg_num = 0; pkg_num <= max_pkg_num; ++pkg_num) {
if (pkg_set & (1UL << pkg_num))
func(pkg_num);
}
}
void print_version(void)
{
printf("x86_energy_perf_policy 17.05.11 (C) Len Brown <len.brown@intel.com>\n");
}
void cmdline(int argc, char **argv)
{
int opt;
int option_index = 0;
static struct option long_options[] = {
{"all", required_argument, 0, 'a'},
{"cpu", required_argument, 0, 'c'},
{"pkg", required_argument, 0, 'p'},
{"debug", no_argument, 0, 'd'},
{"hwp-desired", required_argument, 0, 'D'},
{"epb", required_argument, 0, 'B'},
{"force", no_argument, 0, 'f'},
{"hwp-enable", no_argument, 0, 'e'},
{"help", no_argument, 0, 'h'},
{"hwp-epp", required_argument, 0, 'P'},
{"hwp-min", required_argument, 0, 'm'},
{"hwp-max", required_argument, 0, 'M'},
{"read", no_argument, 0, 'r'},
{"turbo-enable", required_argument, 0, 't'},
{"hwp-use-pkg", required_argument, 0, 'u'},
{"version", no_argument, 0, 'v'},
{"hwp-window", required_argument, 0, 'w'},
{0, 0, 0, 0 }
};
progname = argv[0];
while ((opt = getopt_long_only(argc, argv, "+a:c:dD:E:e:f:m:M:rt:u:vw:",
long_options, &option_index)) != -1) {
switch (opt) {
case 'a':
parse_cmdline_all(optarg);
break;
case 'B':
new_epb = parse_cmdline_epb(parse_optarg_string(optarg));
break;
case 'c':
parse_cmdline_cpu(optarg);
break;
case 'e':
update_hwp_enable = 1;
break;
case 'h':
usage();
break;
case 'd':
debug++;
verbose++;
break;
case 'f':
force++;
break;
case 'D':
req_update.hwp_desired = parse_cmdline_hwp_desired(parse_optarg_string(optarg));
break;
case 'm':
req_update.hwp_min = parse_cmdline_hwp_min(parse_optarg_string(optarg));
break;
case 'M':
req_update.hwp_max = parse_cmdline_hwp_max(parse_optarg_string(optarg));
break;
case 'p':
parse_cmdline_pkg(optarg);
break;
case 'P':
req_update.hwp_epp = parse_cmdline_hwp_epp(parse_optarg_string(optarg));
break;
case 'r':
/* v1 used -r to specify read-only mode, now the default */
break;
case 't':
turbo_update_value = parse_cmdline_turbo(parse_optarg_string(optarg));
break;
case 'u':
update_hwp_use_pkg++;
if (atoi(optarg) == 0)
req_update.hwp_use_pkg = 0;
else
req_update.hwp_use_pkg = 1;
break;
case 'v':
print_version();
exit(0);
break;
case 'w':
req_update.hwp_window = parse_cmdline_hwp_window(parse_optarg_string(optarg));
break;
default:
usage();
}
}
/*
* v1 allowed "performance"|"normal"|"power" with no policy specifier
* to update BIAS. Continue to support that, even though no longer documented.
*/
if (argc == optind + 1)
new_epb = parse_cmdline_epb(parse_optarg_string(argv[optind]));
if (argc > optind + 1) {
fprintf(stderr, "stray parameter '%s'\n", argv[optind + 1]);
usage();
}
}
/*
* Open a file, and exit on failure
*/
FILE *fopen_or_die(const char *path, const char *mode)
{
FILE *filep = fopen(path, "r");
if (!filep)
err(1, "%s: open failed", path);
return filep;
}
void err_on_hypervisor(void)
{
FILE *cpuinfo;
char *flags, *hypervisor;
char *buffer;
/* On VMs /proc/cpuinfo contains a "flags" entry for hypervisor */
cpuinfo = fopen_or_die("/proc/cpuinfo", "ro");
buffer = malloc(4096);
if (!buffer) {
fclose(cpuinfo);
err(-ENOMEM, "buffer malloc fail");
}
if (!fread(buffer, 1024, 1, cpuinfo)) {
fclose(cpuinfo);
free(buffer);
err(1, "Reading /proc/cpuinfo failed");
}
flags = strstr(buffer, "flags");
rewind(cpuinfo);
fseek(cpuinfo, flags - buffer, SEEK_SET);
if (!fgets(buffer, 4096, cpuinfo)) {
fclose(cpuinfo);
free(buffer);
err(1, "Reading /proc/cpuinfo failed");
}
fclose(cpuinfo);
hypervisor = strstr(buffer, "hypervisor");
free(buffer);
if (hypervisor)
err(-1,
"not supported on this virtual machine");
}
int get_msr(int cpu, int offset, unsigned long long *msr)
{
int retval;
char pathname[32];
int fd;
sprintf(pathname, "/dev/cpu/%d/msr", cpu);
fd = open(pathname, O_RDONLY);
if (fd < 0)
err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname);
retval = pread(fd, msr, sizeof(*msr), offset);
if (retval != sizeof(*msr)) {
err_on_hypervisor();
err(-1, "%s offset 0x%llx read failed", pathname, (unsigned long long)offset);
}
if (debug > 1)
fprintf(stderr, "get_msr(cpu%d, 0x%X, 0x%llX)\n", cpu, offset, *msr);
close(fd);
return 0;
}
int put_msr(int cpu, int offset, unsigned long long new_msr)
{
char pathname[32];
int retval;
int fd;
sprintf(pathname, "/dev/cpu/%d/msr", cpu);
fd = open(pathname, O_RDWR);
if (fd < 0)
err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname);
retval = pwrite(fd, &new_msr, sizeof(new_msr), offset);
if (retval != sizeof(new_msr))
err(-2, "pwrite(cpu%d, offset 0x%x, 0x%llx) = %d", cpu, offset, new_msr, retval);
close(fd);
if (debug > 1)
fprintf(stderr, "put_msr(cpu%d, 0x%X, 0x%llX)\n", cpu, offset, new_msr);
return 0;
}
static unsigned int read_sysfs(const char *path, char *buf, size_t buflen)
{
ssize_t numread;
int fd;
fd = open(path, O_RDONLY);
if (fd == -1)
return 0;
numread = read(fd, buf, buflen - 1);
if (numread < 1) {
close(fd);
return 0;
}
buf[numread] = '\0';
close(fd);
return (unsigned int) numread;
}
static unsigned int write_sysfs(const char *path, char *buf, size_t buflen)
{
ssize_t numwritten;
int fd;
fd = open(path, O_WRONLY);
if (fd == -1)
return 0;
numwritten = write(fd, buf, buflen - 1);
if (numwritten < 1) {
perror("write failed\n");
close(fd);
return -1;
}
close(fd);
return (unsigned int) numwritten;
}
void print_hwp_cap(int cpu, struct msr_hwp_cap *cap, char *str)
{
if (cpu != -1)
printf("cpu%d: ", cpu);
printf("HWP_CAP: low %d eff %d guar %d high %d\n",
cap->lowest, cap->efficient, cap->guaranteed, cap->highest);
}
void read_hwp_cap(int cpu, struct msr_hwp_cap *cap, unsigned int msr_offset)
{
unsigned long long msr;
get_msr(cpu, msr_offset, &msr);
cap->highest = msr_perf_2_ratio(HWP_HIGHEST_PERF(msr));
cap->guaranteed = msr_perf_2_ratio(HWP_GUARANTEED_PERF(msr));
cap->efficient = msr_perf_2_ratio(HWP_MOSTEFFICIENT_PERF(msr));
cap->lowest = msr_perf_2_ratio(HWP_LOWEST_PERF(msr));
}
void print_hwp_request(int cpu, struct msr_hwp_request *h, char *str)
{
if (cpu != -1)
printf("cpu%d: ", cpu);
if (str)
printf("%s", str);
printf("HWP_REQ: min %d max %d des %d epp %d window 0x%x (%d*10^%dus) use_pkg %d\n",
h->hwp_min, h->hwp_max, h->hwp_desired, h->hwp_epp,
h->hwp_window, h->hwp_window & 0x7F, (h->hwp_window >> 7) & 0x7, h->hwp_use_pkg);
}
void print_hwp_request_pkg(int pkg, struct msr_hwp_request *h, char *str)
{
printf("pkg%d: ", pkg);
if (str)
printf("%s", str);
printf("HWP_REQ_PKG: min %d max %d des %d epp %d window 0x%x (%d*10^%dus)\n",
h->hwp_min, h->hwp_max, h->hwp_desired, h->hwp_epp,
h->hwp_window, h->hwp_window & 0x7F, (h->hwp_window >> 7) & 0x7);
}
void read_hwp_request(int cpu, struct msr_hwp_request *hwp_req, unsigned int msr_offset)
{
unsigned long long msr;
get_msr(cpu, msr_offset, &msr);
hwp_req->hwp_min = msr_perf_2_ratio((((msr) >> 0) & 0xff));
hwp_req->hwp_max = msr_perf_2_ratio((((msr) >> 8) & 0xff));
hwp_req->hwp_desired = msr_perf_2_ratio((((msr) >> 16) & 0xff));
hwp_req->hwp_epp = (((msr) >> 24) & 0xff);
hwp_req->hwp_window = (((msr) >> 32) & 0x3ff);
hwp_req->hwp_use_pkg = (((msr) >> 42) & 0x1);
}
void write_hwp_request(int cpu, struct msr_hwp_request *hwp_req, unsigned int msr_offset)
{
unsigned long long msr = 0;
if (debug > 1)
printf("cpu%d: requesting min %d max %d des %d epp %d window 0x%0x use_pkg %d\n",
cpu, hwp_req->hwp_min, hwp_req->hwp_max,
hwp_req->hwp_desired, hwp_req->hwp_epp,
hwp_req->hwp_window, hwp_req->hwp_use_pkg);
msr |= HWP_MIN_PERF(ratio_2_msr_perf(hwp_req->hwp_min));
msr |= HWP_MAX_PERF(ratio_2_msr_perf(hwp_req->hwp_max));
msr |= HWP_DESIRED_PERF(ratio_2_msr_perf(hwp_req->hwp_desired));
msr |= HWP_ENERGY_PERF_PREFERENCE(hwp_req->hwp_epp);
msr |= HWP_ACTIVITY_WINDOW(hwp_req->hwp_window);
msr |= HWP_PACKAGE_CONTROL(hwp_req->hwp_use_pkg);
put_msr(cpu, msr_offset, msr);
}
static int get_epb(int cpu)
{
char path[SYSFS_PATH_MAX];
char linebuf[3];
char *endp;
long val;
if (!has_epb)
return -1;
snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/power/energy_perf_bias", cpu);
if (!read_sysfs(path, linebuf, 3))
return -1;
val = strtol(linebuf, &endp, 0);
if (endp == linebuf || errno == ERANGE)
return -1;
return (int)val;
}
static int set_epb(int cpu, int val)
{
char path[SYSFS_PATH_MAX];
char linebuf[3];
char *endp;
int ret;
if (!has_epb)
return -1;
snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/power/energy_perf_bias", cpu);
snprintf(linebuf, sizeof(linebuf), "%d", val);
ret = write_sysfs(path, linebuf, 3);
if (ret <= 0)
return -1;
val = strtol(linebuf, &endp, 0);
if (endp == linebuf || errno == ERANGE)
return -1;
return (int)val;
}
int print_cpu_msrs(int cpu)
{
struct msr_hwp_request req;
struct msr_hwp_cap cap;
int epb;
epb = get_epb(cpu);
if (epb >= 0)
printf("cpu%d: EPB %u\n", cpu, (unsigned int) epb);
if (!has_hwp)
return 0;
read_hwp_request(cpu, &req, MSR_HWP_REQUEST);
print_hwp_request(cpu, &req, "");
read_hwp_cap(cpu, &cap, MSR_HWP_CAPABILITIES);
print_hwp_cap(cpu, &cap, "");
return 0;
}
int print_pkg_msrs(int pkg)
{
struct msr_hwp_request req;
unsigned long long msr;
if (!has_hwp)
return 0;
read_hwp_request(first_cpu_in_pkg[pkg], &req, MSR_HWP_REQUEST_PKG);
print_hwp_request_pkg(pkg, &req, "");
if (has_hwp_notify) {
get_msr(first_cpu_in_pkg[pkg], MSR_HWP_INTERRUPT, &msr);
fprintf(stderr,
"pkg%d: MSR_HWP_INTERRUPT: 0x%08llx (Excursion_Min-%sabled, Guaranteed_Perf_Change-%sabled)\n",
pkg, msr,
((msr) & 0x2) ? "EN" : "Dis",
((msr) & 0x1) ? "EN" : "Dis");
}
get_msr(first_cpu_in_pkg[pkg], MSR_HWP_STATUS, &msr);
fprintf(stderr,
"pkg%d: MSR_HWP_STATUS: 0x%08llx (%sExcursion_Min, %sGuaranteed_Perf_Change)\n",
pkg, msr,
((msr) & 0x4) ? "" : "No-",
((msr) & 0x1) ? "" : "No-");
return 0;
}
/*
* Assumption: All HWP systems have 100 MHz bus clock
*/
int ratio_2_sysfs_khz(int ratio)
{
int bclk_khz = 100 * 1000; /* 100,000 KHz = 100 MHz */
return ratio * bclk_khz;
}
/*
* If HWP is enabled and cpufreq sysfs attribtes are present,
* then update sysfs, so that it will not become
* stale when we write to MSRs.
* (intel_pstate's max_perf_pct and min_perf_pct will follow cpufreq,
* so we don't have to touch that.)
*/
void update_cpufreq_scaling_freq(int is_max, int cpu, unsigned int ratio)
{
char pathname[64];
FILE *fp;
int retval;
int khz;
sprintf(pathname, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_%s_freq",
cpu, is_max ? "max" : "min");
fp = fopen(pathname, "w");
if (!fp) {
if (debug)
perror(pathname);
return;
}
khz = ratio_2_sysfs_khz(ratio);
retval = fprintf(fp, "%d", khz);
if (retval < 0)
if (debug)
perror("fprintf");
if (debug)
printf("echo %d > %s\n", khz, pathname);
fclose(fp);
}
/*
* We update all sysfs before updating any MSRs because of
* bugs in cpufreq/intel_pstate where the sysfs writes
* for a CPU may change the min/max values on other CPUS.
*/
int update_sysfs(int cpu)
{
if (!has_hwp)
return 0;
if (!hwp_update_enabled())
return 0;