-
Notifications
You must be signed in to change notification settings - Fork 5
/
loadcpufreq.c
170 lines (128 loc) · 3.02 KB
/
loadcpufreq.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
// (C) Curaga 2011
// Under the GPLv3
#include <stdio.h>
#include <stdlib.h>
#include <sys/utsname.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#define bufsiz 256
static void load(const char *mod) {
pid_t pid = vfork();
if (pid == 0) {
execl("/sbin/modprobe", "/sbin/modprobe", "-b", mod, NULL);
}
}
static char *dump(const char *buf) {
char *tmp;
tmp = strchr(buf, '\n');
if (tmp) *tmp = '\0';
tmp = strchr(buf, ':') + 2;
return strdup(tmp);
}
static char *detect() {
char *name = calloc(160, sizeof(char));
if (!name) exit(1);
sprintf(name, "acpi-cpufreq");
FILE *cpuinfo = fopen("/proc/cpuinfo", "r");
if (!cpuinfo) exit(1);
char buf[bufsiz], *modelname = NULL, *vendorid = NULL;
char *tmp;
int modelid = 0, family = 0;
while (fgets(buf, bufsiz, cpuinfo) != NULL) {
if (strncmp("model name", buf, 10) == 0 && !modelname) {
modelname = dump(buf);
}
else if (strncmp("model", buf, 5) == 0 && !modelid) {
tmp = dump(buf);
modelid = atoi(tmp);
free(tmp);
}
else if (strncmp("vendor_id", buf, 9) == 0 && !vendorid) {
vendorid = dump(buf);
}
else if (strncmp("cpu family", buf, 10) == 0 && !family) {
tmp = dump(buf);
family = atoi(tmp);
free(tmp);
}
}
rewind(cpuinfo);
if (!modelname) modelname = calloc(1, sizeof(char));
if (!vendorid) vendorid = calloc(1, sizeof(char));
/* printf("model name %s\n", modelname);
printf("model %d\n", modelid);
printf("vendor id %s\n", vendorid);
printf("family %d\n", family);*/
if (strncmp("GenuineIntel", vendorid, 12) == 0) {
while (fgets(buf, bufsiz, cpuinfo) != NULL) {
if (strstr(buf, "est")) goto out;
}
if (family == 15 || strstr(modelname, " III ")) {
sprintf(name, "speedstep-ich");
}
} else if (strncmp("AuthenticAMD", vendorid, 12) == 0) {
switch(family) {
case 5:
sprintf(name, "powernow-k6");
break;
case 6:
sprintf(name, "powernow-k7");
break;
case 15:
case 16:
case 17:
case 20:
sprintf(name, "powernow-k8");
break;
}
} else if (strncmp("CentaurHauls", vendorid, 12) == 0) {
if (family == 6) {
switch (modelid) {
case 10:
break;
default:
sprintf(name, "longhaul");
break;
}
}
} else if (strncmp("GenuineTMx86", vendorid, 12) == 0) {
while (fgets(buf, bufsiz, cpuinfo) != NULL) {
if (strstr(buf, "longrun")) {
sprintf(name, "longrun");
break;
}
}
}
out:
fclose(cpuinfo);
free(modelname);
free(vendorid);
return name;
}
static void helpers(const char *ver) {
char fp[PATH_MAX];
snprintf(fp, PATH_MAX, "/lib/modules/%s/kernel/drivers/cpufreq", ver);
if (chdir(fp)) exit(1);
DIR *dir = opendir(".");
if (!dir) exit(1);
struct dirent *ent = readdir(dir);
while (ent) {
if (strncmp(ent->d_name, "cpufreq_", 8) == 0) {
// printf("Loading %s\n", ent->d_name);
load(ent->d_name);
}
ent = readdir(dir);
}
closedir(dir);
}
int main() {
struct utsname un;
if (uname(&un)) return 1;
helpers(un.release);
char *mod = detect();
// printf("Loading %s\n", mod);
load(mod);
free(mod);
return 0;
}