-
Notifications
You must be signed in to change notification settings - Fork 1
/
linux.c
79 lines (72 loc) · 1.97 KB
/
linux.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
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <x86intrin.h>
#define __USE_GNU
#include <unistd.h>
#include <sched.h>
void enable_speed_step(int cpu, int on)
{
static const uint64_t ss_bit = (uint64_t)1 << 32;
static const off_t perf_ctl_msr = 0x199;
int fd, status;
uint64_t val, xval;
char msrdev[256];
snprintf(msrdev, sizeof(msrdev), "/dev/cpu/%d/msr", cpu);
fd = open(msrdev, O_RDWR);
if (fd < 0) {
fprintf(stderr,
"Linux: MSR device not available, leaving speed step as it was!\n");
return;
}
if (pread(fd, &val, sizeof(val), perf_ctl_msr) != sizeof(val)) {
fprintf(stderr, "Linux: Unable to read MSR device register 0x%lx: %s\n",
perf_ctl_msr, strerror(errno));
return;
}
status = (val & ss_bit) ? 0 : 1;
if (status ^ (on != 0)) {
if (on)
val &= ~ss_bit;
else
val |= ss_bit;
if (pwrite(fd, &val, sizeof(val), perf_ctl_msr) != sizeof(val)) {
fprintf(stderr, "Linux: Unable to write MSR device: %s\n",
strerror(errno));
return;
}
if (pread(fd, &xval, sizeof(xval), perf_ctl_msr) != sizeof(xval)) {
fprintf(stderr, "Linux: Unable to read MSR device: %s\n",
strerror(errno));
return;
}
if (val != xval) {
fprintf(stderr,
"Linux: Unable to write MSR device. Value 0x%lx did not stick at MSR 0x%lx!\n",
val, perf_ctl_msr);
return;
}
}
close(fd);
return;
}
int setup(int core)
{
cpu_set_t my_set;
CPU_ZERO(&my_set);
CPU_SET(core, &my_set);
if (sched_setaffinity(0, sizeof(cpu_set_t), &my_set) < 0)
return -1;
/* https://stackoverflow.com/questions/22309041/rdpmc-in-user-mode-does-not-work-even-with-pce-set */
fprintf(stderr, "Linux: If you get a segfault, make sure rdpmc is allowed.\n"
"Linux: Set /sys/bus/event_source/devices/cpu/rdpmc = 2 on recent kernels (4.0), or 1 for older kernels.\n");
return 0;
}
const char *os_name(void)
{
return "Linux";
}