25 #include <sys/types.h>
27 #include <sys/resource.h>
66 printf(
"%s: [-c cpu] [-v] "
67 "(-r | 'performance' | 'normal' | 'powersave' | n)\n",
72 #define MSR_IA32_ENERGY_PERF_BIAS 0x000001b0
74 #define BIAS_PERFORMANCE 0
75 #define BIAS_BALANCE 6
76 #define BIAS_POWERSAVE 15
84 while ((opt = getopt(argc, argv,
"+rvc:")) != -1) {
108 if (argc != optind + 1) {
109 printf(
"must supply -r or policy param\n");
113 if (!
strcmp(
"performance", argv[optind])) {
115 }
else if (!
strcmp(
"normal", argv[optind])) {
117 }
else if (!
strcmp(
"powersave", argv[optind])) {
123 if (endptr == argv[optind] ||
125 fprintf(stderr,
"invalid value: %s\n",
142 eax = ebx = ecx = edx = 0;
145 "=d" (edx) :
"a" (0));
147 if (ebx != 0x756e6547 || edx != 0x49656e69 || ecx != 0x6c65746e) {
149 fprintf(stderr,
"%.4s%.4s%.4s != GenuineIntel",
150 (
char *)&ebx, (
char *)&edx, (
char *)&ecx);
154 asm(
"cpuid" :
"=a" (fms),
"=c" (ecx),
"=d" (edx) :
"a" (1) :
"ebx");
155 family = (fms >> 8) & 0xf;
156 model = (fms >> 4) & 0xf;
157 stepping = fms & 0xf;
158 if (family == 6 || family == 0xf)
159 model += ((fms >> 16) & 0xf) << 4;
162 printf(
"CPUID %d levels family:model:stepping "
163 "0x%x:%x:%x (%d:%d:%d)\n", max_level,
164 family, model, stepping, family, model, stepping);
166 if (!(edx & (1 << 5))) {
168 printf(
"CPUID: no MSR\n");
176 asm(
"cpuid" :
"=a" (eax),
"=b" (ebx),
"=c" (
ecx),
"=d" (edx) :
"a" (6));
178 printf(
"CPUID.06H.ECX: 0x%x\n", ecx);
179 if (!(ecx & (1 << 3))) {
181 printf(
"CPUID: No MSR_IA32_ENERGY_PERF_BIAS\n");
189 unsigned long long msr;
194 sprintf(msr_path,
"/dev/cpu/%d/msr", cpu);
197 printf(
"Try \"# modprobe msr\"\n");
202 retval = pread(fd, &msr,
sizeof msr, offset);
204 if (retval !=
sizeof msr) {
205 printf(
"pread cpu%d 0x%x = %d\n", cpu, offset, retval);
214 unsigned long long old_msr;
219 sprintf(msr_path,
"/dev/cpu/%d/msr", cpu);
226 retval = pread(fd, &old_msr,
sizeof old_msr, offset);
227 if (retval !=
sizeof old_msr) {
229 printf(
"pread cpu%d 0x%x = %d\n", cpu, offset, retval);
233 retval = pwrite(fd, &new_msr,
sizeof new_msr, offset);
234 if (retval !=
sizeof new_msr) {
236 printf(
"pwrite cpu%d 0x%x = %d\n", cpu, offset, retval);
247 printf(
"cpu%d: 0x%016llx\n",
253 unsigned long long previous_msr;
258 printf(
"cpu%d msr0x%x 0x%016llx -> 0x%016llx\n",
279 retval = fscanf(fp,
"cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
281 perror(
"/proc/stat format");
289 "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n",
304 printf(
"x86_energy_perf_policy Nov 24, 2010"