Loading tools/power/cpupower/lib/cpufreq.c +45 −27 Original line number Diff line number Diff line Loading @@ -42,21 +42,25 @@ int cpufreq_get_hardware_limits(unsigned int cpu, 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); } void cpufreq_put_driver(char * ptr) { void cpufreq_put_driver(char *ptr) { if (!ptr) return; 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); } void cpufreq_put_policy(struct cpufreq_policy *policy) { void cpufreq_put_policy(struct cpufreq_policy *policy) { if ((!policy) || (!policy->governor)) return; Loading @@ -65,11 +69,14 @@ void cpufreq_put_policy(struct cpufreq_policy *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); } 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; if (!any) Loading @@ -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); } 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; if (!any) Loading @@ -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); } 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; if (!any) Loading @@ -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); } 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); } 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)) return -EINVAL; Loading @@ -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); } 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); } 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)) return -EINVAL; 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); } struct cpufreq_stats * cpufreq_get_stats(unsigned int cpu, unsigned long long *total_time) { struct cpufreq_stats *ret; ret = sysfs_get_freq_stats(cpu, total_time); return (ret); struct cpufreq_stats *cpufreq_get_stats(unsigned int cpu, unsigned long long *total_time) { return sysfs_get_freq_stats(cpu, total_time); } void cpufreq_put_stats(struct cpufreq_stats *any) { void cpufreq_put_stats(struct cpufreq_stats *any) { struct cpufreq_stats *tmp, *next; if (!any) Loading @@ -183,8 +202,7 @@ void cpufreq_put_stats(struct cpufreq_stats *any) { } } unsigned long cpufreq_get_transitions(unsigned int cpu) { unsigned long ret = sysfs_get_freq_transitions(cpu); return (ret); unsigned long cpufreq_get_transitions(unsigned int cpu) { return sysfs_get_freq_transitions(cpu); } tools/power/cpupower/lib/cpufreq.h +31 −23 Original line number Diff line number Diff line Loading @@ -129,21 +129,25 @@ 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 * * only present on _some_ ->target() cpufreq drivers. For information purposes * only. Please free allocated memory by calling cpufreq_put_available_frequencies * after use. * Only present on _some_ ->target() cpufreq drivers. For information purposes * only. Please free allocated memory by calling * 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 Loading @@ -152,7 +156,8 @@ extern void cpufreq_put_available_frequencies(struct cpufreq_available_frequenci * 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); Loading @@ -163,7 +168,8 @@ extern void cpufreq_put_affected_cpus(struct cpufreq_affected_cpus *first); * 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); Loading @@ -173,7 +179,8 @@ extern void cpufreq_put_related_cpus(struct cpufreq_affected_cpus *first); * 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); Loading Loading @@ -206,7 +213,8 @@ extern int cpufreq_modify_policy_governor(unsigned int cpu, char *governor); * 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 } Loading tools/power/cpupower/lib/sysfs.c +111 −110 Original line number Diff line number Diff line Loading @@ -24,14 +24,14 @@ static unsigned int sysfs_read_file(const char *path, char *buf, size_t buflen) { 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; numread = read(fd, buf, buflen - 1); if ( numread < 1 ) { if (numread < 1) { close(fd); return 0; } Loading @@ -39,7 +39,7 @@ static unsigned int sysfs_read_file(const char *path, char *buf, size_t buflen) buf[numread] = '\0'; close(fd); return numread; return (unsigned int) numread; } Loading @@ -65,24 +65,24 @@ static unsigned int sysfs_cpufreq_write_file(unsigned int cpu, { char path[SYSFS_PATH_MAX]; int fd; size_t numwrite; ssize_t numwrite; snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/cpufreq/%s", cpu, fname); if ( ( fd = open(path, O_WRONLY) ) == -1 ) fd = open(path, O_WRONLY); if (fd == -1) return 0; numwrite = write(fd, value, len); if ( numwrite < 1 ) { if (numwrite < 1) { close(fd); return 0; } close(fd); return numwrite; return (unsigned int) numwrite; } /* read access to files which contain one numeric value */ Loading Loading @@ -122,8 +122,10 @@ static unsigned long sysfs_cpufreq_get_one_value(unsigned int cpu, if (which >= MAX_CPUFREQ_VALUE_READ_FILES) return 0; if ( ( len = sysfs_cpufreq_read_file(cpu, cpufreq_value_files[which], linebuf, sizeof(linebuf))) == 0 ) len = sysfs_cpufreq_read_file(cpu, cpufreq_value_files[which], linebuf, sizeof(linebuf)); if (len == 0) return 0; value = strtoul(linebuf, &endp, 0); Loading Loading @@ -158,11 +160,13 @@ static char * sysfs_cpufreq_get_one_string(unsigned int cpu, if (which >= MAX_CPUFREQ_STRING_FILES) return NULL; if ( ( len = sysfs_cpufreq_read_file(cpu, cpufreq_string_files[which], linebuf, sizeof(linebuf))) == 0 ) len = sysfs_cpufreq_read_file(cpu, cpufreq_string_files[which], linebuf, sizeof(linebuf)); if (len == 0) return NULL; if ( ( result = strdup(linebuf) ) == NULL ) result = strdup(linebuf); if (result == NULL) return NULL; if (result[strlen(result) - 1] == '\n') Loading Loading @@ -235,11 +239,13 @@ int sysfs_get_freq_hardware_limits(unsigned int cpu, 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); } struct cpufreq_policy * sysfs_get_freq_policy(unsigned int cpu) { struct cpufreq_policy *sysfs_get_freq_policy(unsigned int cpu) { struct cpufreq_policy *policy; policy = malloc(sizeof(struct cpufreq_policy)); Loading Loading @@ -270,26 +276,23 @@ sysfs_get_freq_available_governors(unsigned int cpu) { unsigned int pos, i; unsigned int len; if ( ( len = sysfs_cpufreq_read_file(cpu, "scaling_available_governors", linebuf, sizeof(linebuf))) == 0 ) { len = sysfs_cpufreq_read_file(cpu, "scaling_available_governors", linebuf, sizeof(linebuf)); if (len == 0) return NULL; } pos = 0; for ( i = 0; i < len; i++ ) { if ( linebuf[i] == ' ' || linebuf[i] == '\n' ) { for (i = 0; i < len; i++) { if (linebuf[i] == ' ' || linebuf[i] == '\n') { if (i - pos < 2) continue; if (current) { current->next = malloc(sizeof *current ); current->next = malloc(sizeof(*current)); if (!current->next) goto error_out; current = current->next; } else { first = malloc( sizeof *first ); first = malloc(sizeof(*first)); if (!first) goto error_out; current = first; Loading Loading @@ -330,29 +333,25 @@ sysfs_get_available_frequencies(unsigned int cpu) { unsigned int pos, i; unsigned int len; if ( ( len = sysfs_cpufreq_read_file(cpu, "scaling_available_frequencies", linebuf, sizeof(linebuf))) == 0 ) { len = sysfs_cpufreq_read_file(cpu, "scaling_available_frequencies", linebuf, sizeof(linebuf)); if (len == 0) return NULL; } pos = 0; for ( i = 0; i < len; i++ ) { if ( linebuf[i] == ' ' || linebuf[i] == '\n' ) { for (i = 0; i < len; i++) { if (linebuf[i] == ' ' || linebuf[i] == '\n') { if (i - pos < 2) continue; if (i - pos >= SYSFS_PATH_MAX) goto error_out; if (current) { current->next = malloc(sizeof *current ); current->next = malloc(sizeof(*current)); if (!current->next) goto error_out; current = current->next; } else { first = malloc(sizeof *first ); first = malloc(sizeof(*first)); if (!first) goto error_out; current = first; Loading Loading @@ -381,7 +380,8 @@ sysfs_get_available_frequencies(unsigned int cpu) { } static struct cpufreq_affected_cpus *sysfs_get_cpu_list(unsigned int cpu, const char *file) { const char *file) { struct cpufreq_affected_cpus *first = NULL; struct cpufreq_affected_cpus *current = NULL; char one_value[SYSFS_PATH_MAX]; Loading @@ -389,28 +389,24 @@ static struct cpufreq_affected_cpus * sysfs_get_cpu_list(unsigned int cpu, unsigned int pos, i; unsigned int len; if ( ( len = sysfs_cpufreq_read_file(cpu, file, linebuf, sizeof(linebuf))) == 0 ) { len = sysfs_cpufreq_read_file(cpu, file, linebuf, sizeof(linebuf)); if (len == 0) return NULL; } pos = 0; for ( i = 0; i < len; i++ ) { if ( i == len || linebuf[i] == ' ' || linebuf[i] == '\n' ) { 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); current->next = malloc(sizeof(*current)); if (!current->next) goto error_out; current = current->next; } else { first = malloc(sizeof *first); first = malloc(sizeof(*first)); if (!first) goto error_out; current = first; Loading Loading @@ -439,15 +435,18 @@ static struct cpufreq_affected_cpus * sysfs_get_cpu_list(unsigned int cpu, return NULL; } struct cpufreq_affected_cpus * sysfs_get_freq_affected_cpus(unsigned int cpu) { 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) { 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 *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]; Loading @@ -455,27 +454,26 @@ struct cpufreq_stats * sysfs_get_freq_stats(unsigned int cpu, unsigned long long unsigned int pos, i; unsigned int len; if ( ( len = sysfs_cpufreq_read_file(cpu, "stats/time_in_state", linebuf, sizeof(linebuf))) == 0 ) 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' ) { 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 ); current->next = malloc(sizeof(*current)); if (!current->next) goto error_out; current = current->next; } else { first = malloc(sizeof *first ); first = malloc(sizeof(*first)); if (!first) goto error_out; current = first; Loading @@ -485,7 +483,9 @@ struct cpufreq_stats * sysfs_get_freq_stats(unsigned int cpu, unsigned long long memcpy(one_value, linebuf + pos, i - pos); one_value[i - pos] = '\0'; if ( sscanf(one_value, "%lu %llu", ¤t->frequency, ¤t->time_in_state) != 2 ) if (sscanf(one_value, "%lu %llu", ¤t->frequency, ¤t->time_in_state) != 2) goto error_out; *total_time = *total_time + current->time_in_state; Loading Loading @@ -522,18 +522,18 @@ static int verify_gov(char *new_gov, char *passed_gov) new_gov[i] = '\0'; continue; } if ((new_gov[i] >= 'a') && (new_gov[i] <= 'z')) { if ((new_gov[i] >= 'a') && (new_gov[i] <= 'z')) continue; } if ((new_gov[i] >= 'A') && (new_gov[i] <= 'Z')) { if ((new_gov[i] >= 'A') && (new_gov[i] <= 'Z')) continue; } if (new_gov[i] == '-') { if (new_gov[i] == '-') continue; } if (new_gov[i] == '_') { if (new_gov[i] == '_') continue; } if (new_gov[i] == '\0') { j = 1; continue; Loading Loading @@ -627,7 +627,8 @@ int sysfs_set_freq_policy(unsigned int cpu, struct cpufreq_policy *policy) 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); char userspace_gov[] = "userspace"; char freq[SYSFS_PATH_MAX]; Loading @@ -640,7 +641,7 @@ int sysfs_set_frequency(unsigned int cpu, unsigned long target_frequency) { ret = sysfs_modify_freq_policy_governor(cpu, userspace_gov); if (ret) { cpufreq_put_policy(pol); return (ret); return ret; } } Loading tools/power/cpupower/lib/sysfs.h +22 −12 Original line number Diff line number Diff line Loading @@ -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_hardware(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, unsigned long *min, unsigned long *max); extern char *sysfs_get_freq_driver(unsigned int cpu); extern struct cpufreq_policy *sysfs_get_freq_policy(unsigned int cpu); extern struct cpufreq_available_governors * sysfs_get_freq_available_governors(unsigned int cpu); extern struct cpufreq_available_frequencies * sysfs_get_available_frequencies(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 struct cpufreq_available_governors *sysfs_get_freq_available_governors( unsigned int cpu); extern struct cpufreq_available_frequencies *sysfs_get_available_frequencies( 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 int sysfs_set_freq_policy(unsigned int cpu, struct cpufreq_policy *policy); 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_set_freq_policy(unsigned int cpu, struct cpufreq_policy *policy); 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_set_frequency(unsigned int cpu, unsigned long target_frequency); extern int sysfs_set_frequency(unsigned int cpu, unsigned long target_frequency); Loading
tools/power/cpupower/lib/cpufreq.c +45 −27 Original line number Diff line number Diff line Loading @@ -42,21 +42,25 @@ int cpufreq_get_hardware_limits(unsigned int cpu, 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); } void cpufreq_put_driver(char * ptr) { void cpufreq_put_driver(char *ptr) { if (!ptr) return; 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); } void cpufreq_put_policy(struct cpufreq_policy *policy) { void cpufreq_put_policy(struct cpufreq_policy *policy) { if ((!policy) || (!policy->governor)) return; Loading @@ -65,11 +69,14 @@ void cpufreq_put_policy(struct cpufreq_policy *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); } 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; if (!any) Loading @@ -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); } 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; if (!any) Loading @@ -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); } 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; if (!any) Loading @@ -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); } 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); } 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)) return -EINVAL; Loading @@ -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); } 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); } 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)) return -EINVAL; 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); } struct cpufreq_stats * cpufreq_get_stats(unsigned int cpu, unsigned long long *total_time) { struct cpufreq_stats *ret; ret = sysfs_get_freq_stats(cpu, total_time); return (ret); struct cpufreq_stats *cpufreq_get_stats(unsigned int cpu, unsigned long long *total_time) { return sysfs_get_freq_stats(cpu, total_time); } void cpufreq_put_stats(struct cpufreq_stats *any) { void cpufreq_put_stats(struct cpufreq_stats *any) { struct cpufreq_stats *tmp, *next; if (!any) Loading @@ -183,8 +202,7 @@ void cpufreq_put_stats(struct cpufreq_stats *any) { } } unsigned long cpufreq_get_transitions(unsigned int cpu) { unsigned long ret = sysfs_get_freq_transitions(cpu); return (ret); unsigned long cpufreq_get_transitions(unsigned int cpu) { return sysfs_get_freq_transitions(cpu); }
tools/power/cpupower/lib/cpufreq.h +31 −23 Original line number Diff line number Diff line Loading @@ -129,21 +129,25 @@ 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 * * only present on _some_ ->target() cpufreq drivers. For information purposes * only. Please free allocated memory by calling cpufreq_put_available_frequencies * after use. * Only present on _some_ ->target() cpufreq drivers. For information purposes * only. Please free allocated memory by calling * 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 Loading @@ -152,7 +156,8 @@ extern void cpufreq_put_available_frequencies(struct cpufreq_available_frequenci * 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); Loading @@ -163,7 +168,8 @@ extern void cpufreq_put_affected_cpus(struct cpufreq_affected_cpus *first); * 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); Loading @@ -173,7 +179,8 @@ extern void cpufreq_put_related_cpus(struct cpufreq_affected_cpus *first); * 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); Loading Loading @@ -206,7 +213,8 @@ extern int cpufreq_modify_policy_governor(unsigned int cpu, char *governor); * 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 } Loading
tools/power/cpupower/lib/sysfs.c +111 −110 Original line number Diff line number Diff line Loading @@ -24,14 +24,14 @@ static unsigned int sysfs_read_file(const char *path, char *buf, size_t buflen) { 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; numread = read(fd, buf, buflen - 1); if ( numread < 1 ) { if (numread < 1) { close(fd); return 0; } Loading @@ -39,7 +39,7 @@ static unsigned int sysfs_read_file(const char *path, char *buf, size_t buflen) buf[numread] = '\0'; close(fd); return numread; return (unsigned int) numread; } Loading @@ -65,24 +65,24 @@ static unsigned int sysfs_cpufreq_write_file(unsigned int cpu, { char path[SYSFS_PATH_MAX]; int fd; size_t numwrite; ssize_t numwrite; snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/cpufreq/%s", cpu, fname); if ( ( fd = open(path, O_WRONLY) ) == -1 ) fd = open(path, O_WRONLY); if (fd == -1) return 0; numwrite = write(fd, value, len); if ( numwrite < 1 ) { if (numwrite < 1) { close(fd); return 0; } close(fd); return numwrite; return (unsigned int) numwrite; } /* read access to files which contain one numeric value */ Loading Loading @@ -122,8 +122,10 @@ static unsigned long sysfs_cpufreq_get_one_value(unsigned int cpu, if (which >= MAX_CPUFREQ_VALUE_READ_FILES) return 0; if ( ( len = sysfs_cpufreq_read_file(cpu, cpufreq_value_files[which], linebuf, sizeof(linebuf))) == 0 ) len = sysfs_cpufreq_read_file(cpu, cpufreq_value_files[which], linebuf, sizeof(linebuf)); if (len == 0) return 0; value = strtoul(linebuf, &endp, 0); Loading Loading @@ -158,11 +160,13 @@ static char * sysfs_cpufreq_get_one_string(unsigned int cpu, if (which >= MAX_CPUFREQ_STRING_FILES) return NULL; if ( ( len = sysfs_cpufreq_read_file(cpu, cpufreq_string_files[which], linebuf, sizeof(linebuf))) == 0 ) len = sysfs_cpufreq_read_file(cpu, cpufreq_string_files[which], linebuf, sizeof(linebuf)); if (len == 0) return NULL; if ( ( result = strdup(linebuf) ) == NULL ) result = strdup(linebuf); if (result == NULL) return NULL; if (result[strlen(result) - 1] == '\n') Loading Loading @@ -235,11 +239,13 @@ int sysfs_get_freq_hardware_limits(unsigned int cpu, 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); } struct cpufreq_policy * sysfs_get_freq_policy(unsigned int cpu) { struct cpufreq_policy *sysfs_get_freq_policy(unsigned int cpu) { struct cpufreq_policy *policy; policy = malloc(sizeof(struct cpufreq_policy)); Loading Loading @@ -270,26 +276,23 @@ sysfs_get_freq_available_governors(unsigned int cpu) { unsigned int pos, i; unsigned int len; if ( ( len = sysfs_cpufreq_read_file(cpu, "scaling_available_governors", linebuf, sizeof(linebuf))) == 0 ) { len = sysfs_cpufreq_read_file(cpu, "scaling_available_governors", linebuf, sizeof(linebuf)); if (len == 0) return NULL; } pos = 0; for ( i = 0; i < len; i++ ) { if ( linebuf[i] == ' ' || linebuf[i] == '\n' ) { for (i = 0; i < len; i++) { if (linebuf[i] == ' ' || linebuf[i] == '\n') { if (i - pos < 2) continue; if (current) { current->next = malloc(sizeof *current ); current->next = malloc(sizeof(*current)); if (!current->next) goto error_out; current = current->next; } else { first = malloc( sizeof *first ); first = malloc(sizeof(*first)); if (!first) goto error_out; current = first; Loading Loading @@ -330,29 +333,25 @@ sysfs_get_available_frequencies(unsigned int cpu) { unsigned int pos, i; unsigned int len; if ( ( len = sysfs_cpufreq_read_file(cpu, "scaling_available_frequencies", linebuf, sizeof(linebuf))) == 0 ) { len = sysfs_cpufreq_read_file(cpu, "scaling_available_frequencies", linebuf, sizeof(linebuf)); if (len == 0) return NULL; } pos = 0; for ( i = 0; i < len; i++ ) { if ( linebuf[i] == ' ' || linebuf[i] == '\n' ) { for (i = 0; i < len; i++) { if (linebuf[i] == ' ' || linebuf[i] == '\n') { if (i - pos < 2) continue; if (i - pos >= SYSFS_PATH_MAX) goto error_out; if (current) { current->next = malloc(sizeof *current ); current->next = malloc(sizeof(*current)); if (!current->next) goto error_out; current = current->next; } else { first = malloc(sizeof *first ); first = malloc(sizeof(*first)); if (!first) goto error_out; current = first; Loading Loading @@ -381,7 +380,8 @@ sysfs_get_available_frequencies(unsigned int cpu) { } static struct cpufreq_affected_cpus *sysfs_get_cpu_list(unsigned int cpu, const char *file) { const char *file) { struct cpufreq_affected_cpus *first = NULL; struct cpufreq_affected_cpus *current = NULL; char one_value[SYSFS_PATH_MAX]; Loading @@ -389,28 +389,24 @@ static struct cpufreq_affected_cpus * sysfs_get_cpu_list(unsigned int cpu, unsigned int pos, i; unsigned int len; if ( ( len = sysfs_cpufreq_read_file(cpu, file, linebuf, sizeof(linebuf))) == 0 ) { len = sysfs_cpufreq_read_file(cpu, file, linebuf, sizeof(linebuf)); if (len == 0) return NULL; } pos = 0; for ( i = 0; i < len; i++ ) { if ( i == len || linebuf[i] == ' ' || linebuf[i] == '\n' ) { 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); current->next = malloc(sizeof(*current)); if (!current->next) goto error_out; current = current->next; } else { first = malloc(sizeof *first); first = malloc(sizeof(*first)); if (!first) goto error_out; current = first; Loading Loading @@ -439,15 +435,18 @@ static struct cpufreq_affected_cpus * sysfs_get_cpu_list(unsigned int cpu, return NULL; } struct cpufreq_affected_cpus * sysfs_get_freq_affected_cpus(unsigned int cpu) { 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) { 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 *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]; Loading @@ -455,27 +454,26 @@ struct cpufreq_stats * sysfs_get_freq_stats(unsigned int cpu, unsigned long long unsigned int pos, i; unsigned int len; if ( ( len = sysfs_cpufreq_read_file(cpu, "stats/time_in_state", linebuf, sizeof(linebuf))) == 0 ) 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' ) { 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 ); current->next = malloc(sizeof(*current)); if (!current->next) goto error_out; current = current->next; } else { first = malloc(sizeof *first ); first = malloc(sizeof(*first)); if (!first) goto error_out; current = first; Loading @@ -485,7 +483,9 @@ struct cpufreq_stats * sysfs_get_freq_stats(unsigned int cpu, unsigned long long memcpy(one_value, linebuf + pos, i - pos); one_value[i - pos] = '\0'; if ( sscanf(one_value, "%lu %llu", ¤t->frequency, ¤t->time_in_state) != 2 ) if (sscanf(one_value, "%lu %llu", ¤t->frequency, ¤t->time_in_state) != 2) goto error_out; *total_time = *total_time + current->time_in_state; Loading Loading @@ -522,18 +522,18 @@ static int verify_gov(char *new_gov, char *passed_gov) new_gov[i] = '\0'; continue; } if ((new_gov[i] >= 'a') && (new_gov[i] <= 'z')) { if ((new_gov[i] >= 'a') && (new_gov[i] <= 'z')) continue; } if ((new_gov[i] >= 'A') && (new_gov[i] <= 'Z')) { if ((new_gov[i] >= 'A') && (new_gov[i] <= 'Z')) continue; } if (new_gov[i] == '-') { if (new_gov[i] == '-') continue; } if (new_gov[i] == '_') { if (new_gov[i] == '_') continue; } if (new_gov[i] == '\0') { j = 1; continue; Loading Loading @@ -627,7 +627,8 @@ int sysfs_set_freq_policy(unsigned int cpu, struct cpufreq_policy *policy) 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); char userspace_gov[] = "userspace"; char freq[SYSFS_PATH_MAX]; Loading @@ -640,7 +641,7 @@ int sysfs_set_frequency(unsigned int cpu, unsigned long target_frequency) { ret = sysfs_modify_freq_policy_governor(cpu, userspace_gov); if (ret) { cpufreq_put_policy(pol); return (ret); return ret; } } Loading
tools/power/cpupower/lib/sysfs.h +22 −12 Original line number Diff line number Diff line Loading @@ -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_hardware(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, unsigned long *min, unsigned long *max); extern char *sysfs_get_freq_driver(unsigned int cpu); extern struct cpufreq_policy *sysfs_get_freq_policy(unsigned int cpu); extern struct cpufreq_available_governors * sysfs_get_freq_available_governors(unsigned int cpu); extern struct cpufreq_available_frequencies * sysfs_get_available_frequencies(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 struct cpufreq_available_governors *sysfs_get_freq_available_governors( unsigned int cpu); extern struct cpufreq_available_frequencies *sysfs_get_available_frequencies( 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 int sysfs_set_freq_policy(unsigned int cpu, struct cpufreq_policy *policy); 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_set_freq_policy(unsigned int cpu, struct cpufreq_policy *policy); 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_set_frequency(unsigned int cpu, unsigned long target_frequency); extern int sysfs_set_frequency(unsigned int cpu, unsigned long target_frequency);