12 #include <sys/types.h>
19 #define PATH_TO_CPU "/sys/devices/system/cpu/"
20 #define MAX_LINE_LEN 4096
21 #define SYSFS_PATH_MAX 255
24 static unsigned int sysfs_read_file(
const char *
path,
char *
buf,
size_t buflen)
33 numread =
read(fd, buf, buflen - 1);
42 return (
unsigned int) numread;
50 static unsigned int sysfs_cpufreq_read_file(
unsigned int cpu,
const char *
fname,
57 return sysfs_read_file(path, buf, buflen);
62 static unsigned int sysfs_cpufreq_write_file(
unsigned int cpu,
64 const char *
value,
size_t len)
77 numwrite =
write(fd, value, len);
85 return (
unsigned int) numwrite;
114 static unsigned long sysfs_cpufreq_get_one_value(
unsigned int cpu,
125 len = sysfs_cpufreq_read_file(cpu, cpufreq_value_files[which],
126 linebuf,
sizeof(linebuf));
131 value = strtoul(linebuf, &endp, 0);
133 if (endp == linebuf || errno ==
ERANGE)
153 static char *sysfs_cpufreq_get_one_string(
unsigned int cpu,
163 len = sysfs_cpufreq_read_file(cpu, cpufreq_string_files[which],
164 linebuf,
sizeof(linebuf));
168 result = strdup(linebuf);
172 if (result[
strlen(result) - 1] ==
'\n')
173 result[
strlen(result) - 1] =
'\0';
195 static int sysfs_cpufreq_write_one_value(
unsigned int cpu,
197 const char *new_value,
size_t len)
202 if (sysfs_cpufreq_write_file(cpu, cpufreq_write_files[which],
203 new_value, len) != len)
228 if ((!min) || (!max))
262 if ((!policy->
min) || (!policy->
max)) {
279 len = sysfs_cpufreq_read_file(cpu,
"scaling_available_governors",
280 linebuf,
sizeof(linebuf));
285 for (i = 0; i < len; i++) {
286 if (linebuf[i] ==
' ' || linebuf[i] ==
'\n') {
293 current = current->
next;
295 first =
malloc(
sizeof(*first));
317 current = first->
next;
336 len = sysfs_cpufreq_read_file(cpu,
"scaling_available_frequencies",
337 linebuf,
sizeof(linebuf));
342 for (i = 0; i < len; i++) {
343 if (linebuf[i] ==
' ' || linebuf[i] ==
'\n') {
352 current = current->
next;
354 first =
malloc(
sizeof(*first));
362 memcpy(one_value, linebuf + pos, i - pos);
363 one_value[i -
pos] =
'\0';
375 current = first->
next;
392 len = sysfs_cpufreq_read_file(cpu, file, linebuf,
sizeof(linebuf));
397 for (i = 0; i < len; i++) {
398 if (i == len || linebuf[i] ==
' ' || linebuf[i] ==
'\n') {
407 current = current->
next;
409 first =
malloc(
sizeof(*first));
417 memcpy(one_value, linebuf + pos, i - pos);
418 one_value[i -
pos] =
'\0';
420 if (
sscanf(one_value,
"%u", ¤t->
cpu) != 1)
431 current = first->
next;
440 return sysfs_get_cpu_list(cpu,
"affected_cpus");
445 return sysfs_get_cpu_list(cpu,
"related_cpus");
449 unsigned long long *total_time) {
457 len = sysfs_cpufreq_read_file(cpu,
"stats/time_in_state",
458 linebuf,
sizeof(linebuf));
464 for (i = 0; i < len; i++) {
465 if (i ==
strlen(linebuf) || linebuf[i] ==
'\n') {
474 current = current->
next;
476 first =
malloc(
sizeof(*first));
484 memcpy(one_value, linebuf + pos, i - pos);
485 one_value[i -
pos] =
'\0';
486 if (
sscanf(one_value,
"%lu %llu",
500 current = first->
next;
512 static int verify_gov(
char *new_gov,
char *passed_gov)
514 unsigned int i,
j = 0;
516 if (!passed_gov || (
strlen(passed_gov) > 19))
519 strncpy(new_gov, passed_gov, 20);
520 for (i = 0; i < 20; i++) {
525 if ((new_gov[i] >=
'a') && (new_gov[i] <=
'z'))
528 if ((new_gov[i] >=
'A') && (new_gov[i] <=
'Z'))
531 if (new_gov[i] ==
'-')
534 if (new_gov[i] ==
'_')
537 if (new_gov[i] ==
'\0') {
554 if (verify_gov(new_gov, governor))
558 new_gov,
strlen(new_gov));
589 unsigned long old_min;
595 if (policy->
max < policy->
min)
598 if (verify_gov(gov, policy->
governor))
605 write_max_first = (old_min && (policy->
max < old_min) ? 0 : 1);
607 if (write_max_first) {
619 if (!write_max_first) {
633 char userspace_gov[] =
"userspace";
666 if (
stat(file, &statbuf) != 0)