-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththegov.c
151 lines (130 loc) · 3.38 KB
/
thegov.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
/* change governors on the fly */
#include <stdio.h>
#include <stdlib.h>
#include <string.h> /* for strtok() */
#include <unistd.h> /* for sysconf() */
#define MAX_GOVERNORS 5
#define VERSION "1.00"
char *f_current_gov_start = "/sys/devices/system/cpu/cpu";
char *f_current_gov_end = "/cpufreq/scaling_governor";
char *f_avail_govs = "/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors";
char *avail_govs[MAX_GOVERNORS] = {0};
void show_current_gov(int coreidx)
{
FILE *f;
char line[256] = {0};
char gov_name[256] = {0}; /* collect full name here */
char stridx[2] = {0}; /* strcat() needs at least 2 chars */
char c = coreidx + 48; /* convert int to ascii here */
stridx[0] = c;
strcat(gov_name, f_current_gov_start);
strcat(gov_name, stridx);
strcat(gov_name, f_current_gov_end);
//printf("gov_name: %s \n", gov_name);
f = fopen(gov_name, "r");
if (!f)
{
printf("<can't find current governor. cpufreq seems to be"
" disabled in kernel>\n");
exit(1);
}
if (fgets(line, sizeof(line), f) == NULL)
{
printf("<can't read list of available governors>\n");
exit(1);
}
fclose(f);
printf("[core %d governor:] %s",coreidx, line);
}
void set_current_gov(int coreidx, int govidx)
{
FILE *f;
char gov_name[256] = {0}; /* collect full name here */
char stridx[2] = {0}; /* strcat() needs at least 2 chars */
char c = coreidx + 48; /* convert int to ascii here */
stridx[0] = c;
strcat(gov_name, f_current_gov_start);
strcat(gov_name, stridx);
strcat(gov_name, f_current_gov_end);
f = fopen(gov_name, "w");
if (!f)
{
printf("<can't find current governor>\n");
exit(1);
}
fputs(avail_govs[govidx], f);
fclose(f);
}
int main()
{
FILE *f;
char line[256];
char *p_line;
int num_govs;
int num_cores;
int cnt = 0;
int i;
int idx; /* index which user enters from keyboard */
/* get number of cores at runtime */
num_cores = sysconf(_SC_NPROCESSORS_ONLN);
printf("[cores online :] %d \n", num_cores);
/* show governors for every core */
for (i = 0; i < num_cores; i++)
show_current_gov(i);
/* show available governors in system */
f = fopen(f_avail_govs, "r");
if (!f)
{
printf("<can't find list of available governors>\n");
exit(1);
}
if (fgets(line, sizeof(line), f) == NULL)
{
printf("<can't read list of available governors>\n");
exit(1);
}
fclose(f);
printf("[avail governors:] %s", line);
/* parse line of avail governors and store them in array */
p_line = line;
while ((avail_govs[cnt] = strtok(p_line, " ")) != NULL)
{
//printf("cnt: %d , word: %s\n", cnt, avail_govs[cnt]);
/* pass string to strtok only first time, then pass NULL */
p_line = NULL;
cnt++;
}
num_govs = cnt;
printf("---------- list of governors ---------- \n");
for (cnt = 0; cnt < num_govs; cnt++)
{
/* check governors list for sanity and drop 0xA */
if (*avail_govs[cnt] == 0xA)
{
num_govs--;
break;
}
printf("press [#%d] to select \"%s\" \n",
cnt+1, avail_govs[cnt]);
}
printf("press [#0] to exit\n");
/* get user input */
scanf("%d", &idx);
if (idx < 0 || idx > num_govs)
{
printf("<wrong value entered. exit>\n");
exit(1);
}
else if (idx == 0)
{
exit(0);
}
idx--; /* decrease entered value to use as array index */
/* switch governors for every core */
for (i = 0; i < num_cores; i++)
set_current_gov(i, idx);
/* show governors for every core */
for (i = 0; i < num_cores; i++)
show_current_gov(i);
return 0;
}