cpupowerutils: lib - ConfigStyle bugfixes

Signed-off-by: Dominik Brodowski <linux@dominikbrodowski.net>
This commit is contained in:
Dominik Brodowski 2011-04-19 19:42:33 +02:00
parent 02af3cb5aa
commit 6c2b818551
4 changed files with 258 additions and 221 deletions

View File

@ -42,21 +42,25 @@ int cpufreq_get_hardware_limits(unsigned int cpu,
return sysfs_get_freq_hardware_limits(cpu, min, max); return sysfs_get_freq_hardware_limits(cpu, min, max);
} }
char * cpufreq_get_driver(unsigned int cpu) { char *cpufreq_get_driver(unsigned int cpu)
{
return sysfs_get_freq_driver(cpu); return sysfs_get_freq_driver(cpu);
} }
void cpufreq_put_driver(char * ptr) { void cpufreq_put_driver(char *ptr)
{
if (!ptr) if (!ptr)
return; return;
free(ptr); free(ptr);
} }
struct cpufreq_policy * cpufreq_get_policy(unsigned int cpu) { struct cpufreq_policy *cpufreq_get_policy(unsigned int cpu)
{
return sysfs_get_freq_policy(cpu); return sysfs_get_freq_policy(cpu);
} }
void cpufreq_put_policy(struct cpufreq_policy *policy) { void cpufreq_put_policy(struct cpufreq_policy *policy)
{
if ((!policy) || (!policy->governor)) if ((!policy) || (!policy->governor))
return; return;
@ -65,11 +69,14 @@ void cpufreq_put_policy(struct cpufreq_policy *policy) {
free(policy); free(policy);
} }
struct cpufreq_available_governors * cpufreq_get_available_governors(unsigned int cpu) { struct cpufreq_available_governors *cpufreq_get_available_governors(unsigned
int cpu)
{
return sysfs_get_freq_available_governors(cpu); return sysfs_get_freq_available_governors(cpu);
} }
void cpufreq_put_available_governors(struct cpufreq_available_governors *any) { void cpufreq_put_available_governors(struct cpufreq_available_governors *any)
{
struct cpufreq_available_governors *tmp, *next; struct cpufreq_available_governors *tmp, *next;
if (!any) if (!any)
@ -86,11 +93,14 @@ void cpufreq_put_available_governors(struct cpufreq_available_governors *any) {
} }
struct cpufreq_available_frequencies * cpufreq_get_available_frequencies(unsigned int cpu) { struct cpufreq_available_frequencies
*cpufreq_get_available_frequencies(unsigned int cpu)
{
return sysfs_get_available_frequencies(cpu); return sysfs_get_available_frequencies(cpu);
} }
void cpufreq_put_available_frequencies(struct cpufreq_available_frequencies *any) { void cpufreq_put_available_frequencies(struct cpufreq_available_frequencies
*any) {
struct cpufreq_available_frequencies *tmp, *next; struct cpufreq_available_frequencies *tmp, *next;
if (!any) if (!any)
@ -105,11 +115,13 @@ void cpufreq_put_available_frequencies(struct cpufreq_available_frequencies *any
} }
struct cpufreq_affected_cpus * cpufreq_get_affected_cpus(unsigned int cpu) { struct cpufreq_affected_cpus *cpufreq_get_affected_cpus(unsigned int cpu)
{
return sysfs_get_freq_affected_cpus(cpu); return sysfs_get_freq_affected_cpus(cpu);
} }
void cpufreq_put_affected_cpus(struct cpufreq_affected_cpus *any) { void cpufreq_put_affected_cpus(struct cpufreq_affected_cpus *any)
{
struct cpufreq_affected_cpus *tmp, *next; struct cpufreq_affected_cpus *tmp, *next;
if (!any) if (!any)
@ -124,16 +136,19 @@ void cpufreq_put_affected_cpus(struct cpufreq_affected_cpus *any) {
} }
struct cpufreq_affected_cpus * cpufreq_get_related_cpus(unsigned int cpu) { struct cpufreq_affected_cpus *cpufreq_get_related_cpus(unsigned int cpu)
{
return sysfs_get_freq_related_cpus(cpu); return sysfs_get_freq_related_cpus(cpu);
} }
void cpufreq_put_related_cpus(struct cpufreq_affected_cpus *any) { void cpufreq_put_related_cpus(struct cpufreq_affected_cpus *any)
{
cpufreq_put_affected_cpus(any); cpufreq_put_affected_cpus(any);
} }
int cpufreq_set_policy(unsigned int cpu, struct cpufreq_policy *policy) { int cpufreq_set_policy(unsigned int cpu, struct cpufreq_policy *policy)
{
if (!policy || !(policy->governor)) if (!policy || !(policy->governor))
return -EINVAL; return -EINVAL;
@ -141,35 +156,39 @@ int cpufreq_set_policy(unsigned int cpu, struct cpufreq_policy *policy) {
} }
int cpufreq_modify_policy_min(unsigned int cpu, unsigned long min_freq) { int cpufreq_modify_policy_min(unsigned int cpu, unsigned long min_freq)
{
return sysfs_modify_freq_policy_min(cpu, min_freq); return sysfs_modify_freq_policy_min(cpu, min_freq);
} }
int cpufreq_modify_policy_max(unsigned int cpu, unsigned long max_freq) { int cpufreq_modify_policy_max(unsigned int cpu, unsigned long max_freq)
{
return sysfs_modify_freq_policy_max(cpu, max_freq); return sysfs_modify_freq_policy_max(cpu, max_freq);
} }
int cpufreq_modify_policy_governor(unsigned int cpu, char *governor) { int cpufreq_modify_policy_governor(unsigned int cpu, char *governor)
{
if ((!governor) || (strlen(governor) > 19)) if ((!governor) || (strlen(governor) > 19))
return -EINVAL; return -EINVAL;
return sysfs_modify_freq_policy_governor(cpu, governor); return sysfs_modify_freq_policy_governor(cpu, governor);
} }
int cpufreq_set_frequency(unsigned int cpu, unsigned long target_frequency) { int cpufreq_set_frequency(unsigned int cpu, unsigned long target_frequency)
{
return sysfs_set_frequency(cpu, target_frequency); return sysfs_set_frequency(cpu, target_frequency);
} }
struct cpufreq_stats * cpufreq_get_stats(unsigned int cpu, unsigned long long *total_time) { struct cpufreq_stats *cpufreq_get_stats(unsigned int cpu,
struct cpufreq_stats *ret; unsigned long long *total_time)
{
ret = sysfs_get_freq_stats(cpu, total_time); return sysfs_get_freq_stats(cpu, total_time);
return (ret);
} }
void cpufreq_put_stats(struct cpufreq_stats *any) { void cpufreq_put_stats(struct cpufreq_stats *any)
{
struct cpufreq_stats *tmp, *next; struct cpufreq_stats *tmp, *next;
if (!any) if (!any)
@ -183,8 +202,7 @@ void cpufreq_put_stats(struct cpufreq_stats *any) {
} }
} }
unsigned long cpufreq_get_transitions(unsigned int cpu) { unsigned long cpufreq_get_transitions(unsigned int cpu)
unsigned long ret = sysfs_get_freq_transitions(cpu); {
return sysfs_get_freq_transitions(cpu);
return (ret);
} }

View File

@ -93,9 +93,9 @@ extern unsigned long cpufreq_get_transition_latency(unsigned int cpu);
* considerations by cpufreq policy notifiers in the kernel. * considerations by cpufreq policy notifiers in the kernel.
*/ */
extern int cpufreq_get_hardware_limits(unsigned int cpu, extern int cpufreq_get_hardware_limits(unsigned int cpu,
unsigned long *min, unsigned long *min,
unsigned long *max); unsigned long *max);
/* determine CPUfreq driver used /* determine CPUfreq driver used
@ -104,9 +104,9 @@ extern int cpufreq_get_hardware_limits(unsigned int cpu,
* to avoid memory leakage, please. * to avoid memory leakage, please.
*/ */
extern char * cpufreq_get_driver(unsigned int cpu); extern char *cpufreq_get_driver(unsigned int cpu);
extern void cpufreq_put_driver(char * ptr); extern void cpufreq_put_driver(char *ptr);
/* determine CPUfreq policy currently used /* determine CPUfreq policy currently used
@ -116,7 +116,7 @@ extern void cpufreq_put_driver(char * ptr);
*/ */
extern struct cpufreq_policy * cpufreq_get_policy(unsigned int cpu); extern struct cpufreq_policy *cpufreq_get_policy(unsigned int cpu);
extern void cpufreq_put_policy(struct cpufreq_policy *policy); extern void cpufreq_put_policy(struct cpufreq_policy *policy);
@ -129,41 +129,47 @@ extern void cpufreq_put_policy(struct cpufreq_policy *policy);
*/ */
extern struct cpufreq_available_governors * cpufreq_get_available_governors(unsigned int cpu); extern struct cpufreq_available_governors
*cpufreq_get_available_governors(unsigned int cpu);
extern void cpufreq_put_available_governors(struct cpufreq_available_governors *first); extern void cpufreq_put_available_governors(
struct cpufreq_available_governors *first);
/* determine CPU frequency states available /* determine CPU frequency states available
* *
* only present on _some_ ->target() cpufreq drivers. For information purposes * Only present on _some_ ->target() cpufreq drivers. For information purposes
* only. Please free allocated memory by calling cpufreq_put_available_frequencies * only. Please free allocated memory by calling
* after use. * cpufreq_put_available_frequencies after use.
*/ */
extern struct cpufreq_available_frequencies * cpufreq_get_available_frequencies(unsigned int cpu); extern struct cpufreq_available_frequencies
*cpufreq_get_available_frequencies(unsigned int cpu);
extern void cpufreq_put_available_frequencies(struct cpufreq_available_frequencies *first); extern void cpufreq_put_available_frequencies(
struct cpufreq_available_frequencies *first);
/* determine affected CPUs /* determine affected CPUs
* *
* Remember to call cpufreq_put_affected_cpus when no longer needed * Remember to call cpufreq_put_affected_cpus when no longer needed
* to avoid memory leakage, please. * to avoid memory leakage, please.
*/ */
extern struct cpufreq_affected_cpus * cpufreq_get_affected_cpus(unsigned int cpu); extern struct cpufreq_affected_cpus *cpufreq_get_affected_cpus(unsigned
int cpu);
extern void cpufreq_put_affected_cpus(struct cpufreq_affected_cpus *first); extern void cpufreq_put_affected_cpus(struct cpufreq_affected_cpus *first);
/* determine related CPUs /* determine related CPUs
* *
* Remember to call cpufreq_put_related_cpus when no longer needed * Remember to call cpufreq_put_related_cpus when no longer needed
* to avoid memory leakage, please. * to avoid memory leakage, please.
*/ */
extern struct cpufreq_affected_cpus * cpufreq_get_related_cpus(unsigned int cpu); extern struct cpufreq_affected_cpus *cpufreq_get_related_cpus(unsigned
int cpu);
extern void cpufreq_put_related_cpus(struct cpufreq_affected_cpus *first); extern void cpufreq_put_related_cpus(struct cpufreq_affected_cpus *first);
@ -173,15 +179,16 @@ extern void cpufreq_put_related_cpus(struct cpufreq_affected_cpus *first);
* This is not available in all kernel versions or configurations. * This is not available in all kernel versions or configurations.
*/ */
extern struct cpufreq_stats * cpufreq_get_stats(unsigned int cpu, unsigned long long *total_time); extern struct cpufreq_stats *cpufreq_get_stats(unsigned int cpu,
unsigned long long *total_time);
extern void cpufreq_put_stats(struct cpufreq_stats *stats); extern void cpufreq_put_stats(struct cpufreq_stats *stats);
extern unsigned long cpufreq_get_transitions(unsigned int cpu); extern unsigned long cpufreq_get_transitions(unsigned int cpu);
/* set new cpufreq policy /* set new cpufreq policy
* *
* Tries to set the passed policy as new policy as close as possible, * Tries to set the passed policy as new policy as close as possible,
* but results may differ depending e.g. on governors being available. * but results may differ depending e.g. on governors being available.
*/ */
@ -189,7 +196,7 @@ extern unsigned long cpufreq_get_transitions(unsigned int cpu);
extern int cpufreq_set_policy(unsigned int cpu, struct cpufreq_policy *policy); extern int cpufreq_set_policy(unsigned int cpu, struct cpufreq_policy *policy);
/* modify a policy by only changing min/max freq or governor /* modify a policy by only changing min/max freq or governor
* *
* Does not check whether result is what was intended. * Does not check whether result is what was intended.
*/ */
@ -202,11 +209,12 @@ extern int cpufreq_modify_policy_governor(unsigned int cpu, char *governor);
/* set a specific frequency /* set a specific frequency
* *
* Does only work if userspace governor can be used and no external * Does only work if userspace governor can be used and no external
* interference (other calls to this function or to set/modify_policy) * interference (other calls to this function or to set/modify_policy)
* occurs. Also does not work on ->range() cpufreq drivers. * occurs. Also does not work on ->range() cpufreq drivers.
*/ */
extern int cpufreq_set_frequency(unsigned int cpu, unsigned long target_frequency); extern int cpufreq_set_frequency(unsigned int cpu,
unsigned long target_frequency);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -24,14 +24,14 @@
static unsigned int sysfs_read_file(const char *path, char *buf, size_t buflen) static unsigned int sysfs_read_file(const char *path, char *buf, size_t buflen)
{ {
int fd; int fd;
size_t numread; ssize_t numread;
if ( ( fd = open(path, O_RDONLY) ) == -1 ) fd = open(path, O_RDONLY);
if (fd == -1)
return 0; return 0;
numread = read(fd, buf, buflen - 1); numread = read(fd, buf, buflen - 1);
if ( numread < 1 ) if (numread < 1) {
{
close(fd); close(fd);
return 0; return 0;
} }
@ -39,7 +39,7 @@ static unsigned int sysfs_read_file(const char *path, char *buf, size_t buflen)
buf[numread] = '\0'; buf[numread] = '\0';
close(fd); close(fd);
return numread; return (unsigned int) numread;
} }
@ -65,24 +65,24 @@ static unsigned int sysfs_cpufreq_write_file(unsigned int cpu,
{ {
char path[SYSFS_PATH_MAX]; char path[SYSFS_PATH_MAX];
int fd; int fd;
size_t numwrite; ssize_t numwrite;
snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/cpufreq/%s", snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/cpufreq/%s",
cpu, fname); cpu, fname);
if ( ( fd = open(path, O_WRONLY) ) == -1 ) fd = open(path, O_WRONLY);
if (fd == -1)
return 0; return 0;
numwrite = write(fd, value, len); numwrite = write(fd, value, len);
if ( numwrite < 1 ) if (numwrite < 1) {
{
close(fd); close(fd);
return 0; return 0;
} }
close(fd); close(fd);
return numwrite; return (unsigned int) numwrite;
} }
/* read access to files which contain one numeric value */ /* read access to files which contain one numeric value */
@ -114,21 +114,23 @@ static const char *cpufreq_value_files[MAX_CPUFREQ_VALUE_READ_FILES] = {
static unsigned long sysfs_cpufreq_get_one_value(unsigned int cpu, static unsigned long sysfs_cpufreq_get_one_value(unsigned int cpu,
enum cpufreq_value which) enum cpufreq_value which)
{ {
unsigned long value; unsigned long value;
unsigned int len; unsigned int len;
char linebuf[MAX_LINE_LEN]; char linebuf[MAX_LINE_LEN];
char *endp; char *endp;
if ( which >= MAX_CPUFREQ_VALUE_READ_FILES ) if (which >= MAX_CPUFREQ_VALUE_READ_FILES)
return 0; return 0;
if ( ( len = sysfs_cpufreq_read_file(cpu, cpufreq_value_files[which], len = sysfs_cpufreq_read_file(cpu, cpufreq_value_files[which],
linebuf, sizeof(linebuf))) == 0 ) linebuf, sizeof(linebuf));
if (len == 0)
return 0; return 0;
value = strtoul(linebuf, &endp, 0); value = strtoul(linebuf, &endp, 0);
if ( endp == linebuf || errno == ERANGE ) if (endp == linebuf || errno == ERANGE)
return 0; return 0;
return value; return value;
@ -148,7 +150,7 @@ static const char *cpufreq_string_files[MAX_CPUFREQ_STRING_FILES] = {
}; };
static char * sysfs_cpufreq_get_one_string(unsigned int cpu, static char *sysfs_cpufreq_get_one_string(unsigned int cpu,
enum cpufreq_string which) enum cpufreq_string which)
{ {
char linebuf[MAX_LINE_LEN]; char linebuf[MAX_LINE_LEN];
@ -158,11 +160,13 @@ static char * sysfs_cpufreq_get_one_string(unsigned int cpu,
if (which >= MAX_CPUFREQ_STRING_FILES) if (which >= MAX_CPUFREQ_STRING_FILES)
return NULL; return NULL;
if ( ( len = sysfs_cpufreq_read_file(cpu, cpufreq_string_files[which], len = sysfs_cpufreq_read_file(cpu, cpufreq_string_files[which],
linebuf, sizeof(linebuf))) == 0 ) linebuf, sizeof(linebuf));
if (len == 0)
return NULL; return NULL;
if ( ( result = strdup(linebuf) ) == NULL ) result = strdup(linebuf);
if (result == NULL)
return NULL; return NULL;
if (result[strlen(result) - 1] == '\n') if (result[strlen(result) - 1] == '\n')
@ -195,8 +199,8 @@ static int sysfs_cpufreq_write_one_value(unsigned int cpu,
if (which >= MAX_CPUFREQ_WRITE_FILES) if (which >= MAX_CPUFREQ_WRITE_FILES)
return 0; return 0;
if ( sysfs_cpufreq_write_file(cpu, cpufreq_write_files[which], if (sysfs_cpufreq_write_file(cpu, cpufreq_write_files[which],
new_value, len) != len ) new_value, len) != len)
return -ENODEV; return -ENODEV;
return 0; return 0;
@ -235,11 +239,13 @@ int sysfs_get_freq_hardware_limits(unsigned int cpu,
return 0; return 0;
} }
char * sysfs_get_freq_driver(unsigned int cpu) { char *sysfs_get_freq_driver(unsigned int cpu)
{
return sysfs_cpufreq_get_one_string(cpu, SCALING_DRIVER); return sysfs_cpufreq_get_one_string(cpu, SCALING_DRIVER);
} }
struct cpufreq_policy * sysfs_get_freq_policy(unsigned int cpu) { struct cpufreq_policy *sysfs_get_freq_policy(unsigned int cpu)
{
struct cpufreq_policy *policy; struct cpufreq_policy *policy;
policy = malloc(sizeof(struct cpufreq_policy)); policy = malloc(sizeof(struct cpufreq_policy));
@ -270,27 +276,24 @@ sysfs_get_freq_available_governors(unsigned int cpu) {
unsigned int pos, i; unsigned int pos, i;
unsigned int len; unsigned int len;
if ( ( len = sysfs_cpufreq_read_file(cpu, "scaling_available_governors", len = sysfs_cpufreq_read_file(cpu, "scaling_available_governors",
linebuf, sizeof(linebuf))) == 0 ) linebuf, sizeof(linebuf));
{ if (len == 0)
return NULL; return NULL;
}
pos = 0; pos = 0;
for ( i = 0; i < len; i++ ) for (i = 0; i < len; i++) {
{ if (linebuf[i] == ' ' || linebuf[i] == '\n') {
if ( linebuf[i] == ' ' || linebuf[i] == '\n' ) if (i - pos < 2)
{
if ( i - pos < 2 )
continue; continue;
if ( current ) { if (current) {
current->next = malloc(sizeof *current ); current->next = malloc(sizeof(*current));
if ( ! current->next ) if (!current->next)
goto error_out; goto error_out;
current = current->next; current = current->next;
} else { } else {
first = malloc( sizeof *first ); first = malloc(sizeof(*first));
if ( ! first ) if (!first)
goto error_out; goto error_out;
current = first; current = first;
} }
@ -298,10 +301,10 @@ sysfs_get_freq_available_governors(unsigned int cpu) {
current->next = NULL; current->next = NULL;
current->governor = malloc(i - pos + 1); current->governor = malloc(i - pos + 1);
if ( ! current->governor ) if (!current->governor)
goto error_out; goto error_out;
memcpy( current->governor, linebuf + pos, i - pos); memcpy(current->governor, linebuf + pos, i - pos);
current->governor[i - pos] = '\0'; current->governor[i - pos] = '\0';
pos = i + 1; pos = i + 1;
} }
@ -310,11 +313,11 @@ sysfs_get_freq_available_governors(unsigned int cpu) {
return first; return first;
error_out: error_out:
while ( first ) { while (first) {
current = first->next; current = first->next;
if ( first->governor ) if (first->governor)
free( first->governor ); free(first->governor);
free( first ); free(first);
first = current; first = current;
} }
return NULL; return NULL;
@ -330,30 +333,26 @@ sysfs_get_available_frequencies(unsigned int cpu) {
unsigned int pos, i; unsigned int pos, i;
unsigned int len; unsigned int len;
if ( ( len = sysfs_cpufreq_read_file(cpu, len = sysfs_cpufreq_read_file(cpu, "scaling_available_frequencies",
"scaling_available_frequencies", linebuf, sizeof(linebuf));
linebuf, sizeof(linebuf))) == 0 ) if (len == 0)
{
return NULL; return NULL;
}
pos = 0; pos = 0;
for ( i = 0; i < len; i++ ) for (i = 0; i < len; i++) {
{ if (linebuf[i] == ' ' || linebuf[i] == '\n') {
if ( linebuf[i] == ' ' || linebuf[i] == '\n' ) if (i - pos < 2)
{
if ( i - pos < 2 )
continue; continue;
if ( i - pos >= SYSFS_PATH_MAX ) if (i - pos >= SYSFS_PATH_MAX)
goto error_out; goto error_out;
if ( current ) { if (current) {
current->next = malloc(sizeof *current ); current->next = malloc(sizeof(*current));
if ( ! current->next ) if (!current->next)
goto error_out; goto error_out;
current = current->next; current = current->next;
} else { } else {
first = malloc(sizeof *first ); first = malloc(sizeof(*first));
if ( ! first ) if (!first)
goto error_out; goto error_out;
current = first; current = first;
} }
@ -362,66 +361,7 @@ sysfs_get_available_frequencies(unsigned int cpu) {
memcpy(one_value, linebuf + pos, i - pos); memcpy(one_value, linebuf + pos, i - pos);
one_value[i - pos] = '\0'; one_value[i - pos] = '\0';
if ( sscanf(one_value, "%lu", &current->frequency) != 1 ) if (sscanf(one_value, "%lu", &current->frequency) != 1)
goto error_out;
pos = i + 1;
}
}
return first;
error_out:
while ( first ) {
current = first->next;
free(first);
first = current;
}
return NULL;
}
static struct cpufreq_affected_cpus * sysfs_get_cpu_list(unsigned int cpu,
const char *file) {
struct cpufreq_affected_cpus *first = NULL;
struct cpufreq_affected_cpus *current = NULL;
char one_value[SYSFS_PATH_MAX];
char linebuf[MAX_LINE_LEN];
unsigned int pos, i;
unsigned int len;
if ( ( len = sysfs_cpufreq_read_file(cpu, file, linebuf,
sizeof(linebuf))) == 0 )
{
return NULL;
}
pos = 0;
for ( i = 0; i < len; i++ )
{
if ( i == len || linebuf[i] == ' ' || linebuf[i] == '\n' )
{
if ( i - pos < 1 )
continue;
if ( i - pos >= SYSFS_PATH_MAX )
goto error_out;
if ( current ) {
current->next = malloc(sizeof *current);
if ( ! current->next )
goto error_out;
current = current->next;
} else {
first = malloc(sizeof *first);
if ( ! first )
goto error_out;
current = first;
}
current->first = first;
current->next = NULL;
memcpy(one_value, linebuf + pos, i - pos);
one_value[i - pos] = '\0';
if ( sscanf(one_value, "%u", &current->cpu) != 1 )
goto error_out; goto error_out;
pos = i + 1; pos = i + 1;
@ -439,44 +379,35 @@ static struct cpufreq_affected_cpus * sysfs_get_cpu_list(unsigned int cpu,
return NULL; return NULL;
} }
struct cpufreq_affected_cpus * sysfs_get_freq_affected_cpus(unsigned int cpu) { static struct cpufreq_affected_cpus *sysfs_get_cpu_list(unsigned int cpu,
return sysfs_get_cpu_list(cpu, "affected_cpus"); const char *file)
} {
struct cpufreq_affected_cpus *first = NULL;
struct cpufreq_affected_cpus * sysfs_get_freq_related_cpus(unsigned int cpu) { struct cpufreq_affected_cpus *current = NULL;
return sysfs_get_cpu_list(cpu, "related_cpus");
}
struct cpufreq_stats * sysfs_get_freq_stats(unsigned int cpu, unsigned long long *total_time) {
struct cpufreq_stats *first = NULL;
struct cpufreq_stats *current = NULL;
char one_value[SYSFS_PATH_MAX]; char one_value[SYSFS_PATH_MAX];
char linebuf[MAX_LINE_LEN]; char linebuf[MAX_LINE_LEN];
unsigned int pos, i; unsigned int pos, i;
unsigned int len; unsigned int len;
if ( ( len = sysfs_cpufreq_read_file(cpu, "stats/time_in_state", len = sysfs_cpufreq_read_file(cpu, file, linebuf, sizeof(linebuf));
linebuf, sizeof(linebuf))) == 0 ) if (len == 0)
return NULL; return NULL;
*total_time = 0;
pos = 0; pos = 0;
for ( i = 0; i < len; i++ ) for (i = 0; i < len; i++) {
{ if (i == len || linebuf[i] == ' ' || linebuf[i] == '\n') {
if ( i == strlen(linebuf) || linebuf[i] == '\n' ) if (i - pos < 1)
{
if ( i - pos < 2 )
continue; continue;
if ( (i - pos) >= SYSFS_PATH_MAX ) if (i - pos >= SYSFS_PATH_MAX)
goto error_out; goto error_out;
if ( current ) { if (current) {
current->next = malloc(sizeof *current ); current->next = malloc(sizeof(*current));
if ( ! current->next ) if (!current->next)
goto error_out; goto error_out;
current = current->next; current = current->next;
} else { } else {
first = malloc(sizeof *first ); first = malloc(sizeof(*first));
if ( ! first ) if (!first)
goto error_out; goto error_out;
current = first; current = first;
} }
@ -485,7 +416,76 @@ struct cpufreq_stats * sysfs_get_freq_stats(unsigned int cpu, unsigned long long
memcpy(one_value, linebuf + pos, i - pos); memcpy(one_value, linebuf + pos, i - pos);
one_value[i - pos] = '\0'; one_value[i - pos] = '\0';
if ( sscanf(one_value, "%lu %llu", &current->frequency, &current->time_in_state) != 2 )
if (sscanf(one_value, "%u", &current->cpu) != 1)
goto error_out;
pos = i + 1;
}
}
return first;
error_out:
while (first) {
current = first->next;
free(first);
first = current;
}
return NULL;
}
struct cpufreq_affected_cpus *sysfs_get_freq_affected_cpus(unsigned int cpu)
{
return sysfs_get_cpu_list(cpu, "affected_cpus");
}
struct cpufreq_affected_cpus *sysfs_get_freq_related_cpus(unsigned int cpu)
{
return sysfs_get_cpu_list(cpu, "related_cpus");
}
struct cpufreq_stats *sysfs_get_freq_stats(unsigned int cpu,
unsigned long long *total_time) {
struct cpufreq_stats *first = NULL;
struct cpufreq_stats *current = NULL;
char one_value[SYSFS_PATH_MAX];
char linebuf[MAX_LINE_LEN];
unsigned int pos, i;
unsigned int len;
len = sysfs_cpufreq_read_file(cpu, "stats/time_in_state",
linebuf, sizeof(linebuf));
if (len == 0)
return NULL;
*total_time = 0;
pos = 0;
for (i = 0; i < len; i++) {
if (i == strlen(linebuf) || linebuf[i] == '\n') {
if (i - pos < 2)
continue;
if ((i - pos) >= SYSFS_PATH_MAX)
goto error_out;
if (current) {
current->next = malloc(sizeof(*current));
if (!current->next)
goto error_out;
current = current->next;
} else {
first = malloc(sizeof(*first));
if (!first)
goto error_out;
current = first;
}
current->first = first;
current->next = NULL;
memcpy(one_value, linebuf + pos, i - pos);
one_value[i - pos] = '\0';
if (sscanf(one_value, "%lu %llu",
&current->frequency,
&current->time_in_state) != 2)
goto error_out; goto error_out;
*total_time = *total_time + current->time_in_state; *total_time = *total_time + current->time_in_state;
@ -496,7 +496,7 @@ struct cpufreq_stats * sysfs_get_freq_stats(unsigned int cpu, unsigned long long
return first; return first;
error_out: error_out:
while ( first ) { while (first) {
current = first->next; current = first->next;
free(first); free(first);
first = current; first = current;
@ -511,29 +511,29 @@ unsigned long sysfs_get_freq_transitions(unsigned int cpu)
static int verify_gov(char *new_gov, char *passed_gov) static int verify_gov(char *new_gov, char *passed_gov)
{ {
unsigned int i, j=0; unsigned int i, j = 0;
if (!passed_gov || (strlen(passed_gov) > 19)) if (!passed_gov || (strlen(passed_gov) > 19))
return -EINVAL; return -EINVAL;
strncpy(new_gov, passed_gov, 20); strncpy(new_gov, passed_gov, 20);
for (i=0;i<20;i++) { for (i = 0; i < 20; i++) {
if (j) { if (j) {
new_gov[i] = '\0'; new_gov[i] = '\0';
continue; continue;
} }
if ((new_gov[i] >= 'a') && (new_gov[i] <= 'z')) { if ((new_gov[i] >= 'a') && (new_gov[i] <= 'z'))
continue; continue;
}
if ((new_gov[i] >= 'A') && (new_gov[i] <= 'Z')) { if ((new_gov[i] >= 'A') && (new_gov[i] <= 'Z'))
continue; continue;
}
if (new_gov[i] == '-') { if (new_gov[i] == '-')
continue; continue;
}
if (new_gov[i] == '_') { if (new_gov[i] == '_')
continue; continue;
}
if (new_gov[i] == '\0') { if (new_gov[i] == '\0') {
j = 1; j = 1;
continue; continue;
@ -627,7 +627,8 @@ int sysfs_set_freq_policy(unsigned int cpu, struct cpufreq_policy *policy)
gov, strlen(gov)); gov, strlen(gov));
} }
int sysfs_set_frequency(unsigned int cpu, unsigned long target_frequency) { int sysfs_set_frequency(unsigned int cpu, unsigned long target_frequency)
{
struct cpufreq_policy *pol = sysfs_get_freq_policy(cpu); struct cpufreq_policy *pol = sysfs_get_freq_policy(cpu);
char userspace_gov[] = "userspace"; char userspace_gov[] = "userspace";
char freq[SYSFS_PATH_MAX]; char freq[SYSFS_PATH_MAX];
@ -640,7 +641,7 @@ int sysfs_set_frequency(unsigned int cpu, unsigned long target_frequency) {
ret = sysfs_modify_freq_policy_governor(cpu, userspace_gov); ret = sysfs_modify_freq_policy_governor(cpu, userspace_gov);
if (ret) { if (ret) {
cpufreq_put_policy(pol); cpufreq_put_policy(pol);
return (ret); return ret;
} }
} }
@ -662,7 +663,7 @@ int sysfs_cpu_exists(unsigned int cpu)
snprintf(file, SYSFS_PATH_MAX, PATH_TO_CPU "cpu%u/", cpu); snprintf(file, SYSFS_PATH_MAX, PATH_TO_CPU "cpu%u/", cpu);
if ( stat(file, &statbuf) != 0 ) if (stat(file, &statbuf) != 0)
return -ENOSYS; return -ENOSYS;
return S_ISDIR(statbuf.st_mode) ? 0 : -ENOSYS; return S_ISDIR(statbuf.st_mode) ? 0 : -ENOSYS;

View File

@ -5,17 +5,27 @@ extern unsigned int sysfs_cpu_exists(unsigned int cpu);
extern unsigned long sysfs_get_freq_kernel(unsigned int cpu); extern unsigned long sysfs_get_freq_kernel(unsigned int cpu);
extern unsigned long sysfs_get_freq_hardware(unsigned int cpu); extern unsigned long sysfs_get_freq_hardware(unsigned int cpu);
extern unsigned long sysfs_get_freq_transition_latency(unsigned int cpu); extern unsigned long sysfs_get_freq_transition_latency(unsigned int cpu);
extern int sysfs_get_freq_hardware_limits(unsigned int cpu, unsigned long *min, unsigned long *max); extern int sysfs_get_freq_hardware_limits(unsigned int cpu,
extern char * sysfs_get_freq_driver(unsigned int cpu); unsigned long *min, unsigned long *max);
extern struct cpufreq_policy * sysfs_get_freq_policy(unsigned int cpu); extern char *sysfs_get_freq_driver(unsigned int cpu);
extern struct cpufreq_available_governors * sysfs_get_freq_available_governors(unsigned int cpu); extern struct cpufreq_policy *sysfs_get_freq_policy(unsigned int cpu);
extern struct cpufreq_available_frequencies * sysfs_get_available_frequencies(unsigned int cpu); extern struct cpufreq_available_governors *sysfs_get_freq_available_governors(
extern struct cpufreq_affected_cpus * sysfs_get_freq_affected_cpus(unsigned int cpu); unsigned int cpu);
extern struct cpufreq_affected_cpus * sysfs_get_freq_related_cpus(unsigned int cpu); extern struct cpufreq_available_frequencies *sysfs_get_available_frequencies(
extern struct cpufreq_stats * sysfs_get_freq_stats(unsigned int cpu, unsigned long long *total_time); unsigned int cpu);
extern struct cpufreq_affected_cpus *sysfs_get_freq_affected_cpus(
unsigned int cpu);
extern struct cpufreq_affected_cpus *sysfs_get_freq_related_cpus(
unsigned int cpu);
extern struct cpufreq_stats *sysfs_get_freq_stats(unsigned int cpu,
unsigned long long *total_time);
extern unsigned long sysfs_get_freq_transitions(unsigned int cpu); extern unsigned long sysfs_get_freq_transitions(unsigned int cpu);
extern int sysfs_set_freq_policy(unsigned int cpu, struct cpufreq_policy *policy); extern int sysfs_set_freq_policy(unsigned int cpu,
extern int sysfs_modify_freq_policy_min(unsigned int cpu, unsigned long min_freq); struct cpufreq_policy *policy);
extern int sysfs_modify_freq_policy_max(unsigned int cpu, unsigned long max_freq); extern int sysfs_modify_freq_policy_min(unsigned int cpu,
unsigned long min_freq);
extern int sysfs_modify_freq_policy_max(unsigned int cpu,
unsigned long max_freq);
extern int sysfs_modify_freq_policy_governor(unsigned int cpu, char *governor); extern int sysfs_modify_freq_policy_governor(unsigned int cpu, char *governor);
extern int sysfs_set_frequency(unsigned int cpu, unsigned long target_frequency); extern int sysfs_set_frequency(unsigned int cpu,
unsigned long target_frequency);