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
|
#include <fcntl.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include "debug.h"
#include "sysfs.h"
#define DIR_BUF_SIZE 1024
// Validate if we found a file named cpu[0-9][0-9
int cpufreq_is_cpu(char *name)
{
if (strncmp(name,"cpu",3)!=0) return 0;
if ((name[3]<'0') || (name[3]>'9')) return 0;
if (name[4]==0) return 1;
if ((name[4]<'0') || (name[4]>'9')) return 0;
if (name[5]==0) return 1;
return 0;
}
// Set a cpufreq parameter to a value
int cpufreq_set_str(char *parameter,char *value,int checkdelay)
{
int fd;
char sysfs_file[128];
int done=0;
DEBUG1_CPUFREQ("Set %s to %s\n",parameter,value)
char buf[DIR_BUF_SIZE];
long nread;
struct linux_dirent {
unsigned long d_ino;
off_t d_off;
unsigned short d_reclen;
char d_name[];
};
struct linux_dirent *d;
size_t bpos;
// Open the CPU directory in sysfs
fd=open("/sys/devices/system/cpu",O_RDONLY|O_DIRECTORY);
if (fd==-1)
{
DEBUG1_CPUFREQ("Unable to find CPU's\n");
return -1;
}
while(1)
{
// Iterate over SYS_getdents calls
nread=syscall(SYS_getdents,fd,buf,DIR_BUF_SIZE);
// If we can't read return an error
if (nread==-1)
{
DEBUG1_CPUFREQ("Unable to find CPU's\n");
close(fd);
return -1;
}
// If there are no entries left we're done
if (nread==0) break;
// Iterate over returned file info
for(bpos=0;bpos<nread;)
{
d=(struct linux_dirent *)(buf+bpos);
// If it's a CPU, set our parameter
if (cpufreq_is_cpu(d->d_name))
{
DEBUG2_CPUFREQ("Found CPU %s\n",d->d_name);
snprintf(sysfs_file,128,"/sys/devices/system/cpu/%s/cpufreq/%s",d->d_name,parameter);
if (sysfs_write_str(sysfs_file,value,checkdelay)!=0)
{
DEBUG1_CPUFREQ("Failed to set %s\n",d->d_name);
return -1;
}
done++;
}
bpos+=d->d_reclen;
}
}
close(fd);
return done;
}
// This is just a wrapper around cpufreq_set_str with long int to string
// convertion
int cpufreq_set_long_int(char *parameter,long int value,int checkdelay)
{
char buf[255];
snprintf(buf,255,"%ld",value);
return cpufreq_set_str(parameter,buf,checkdelay);
}
|