Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

cpupowerutils: lib - ConfigStyle bugfixes

Signed-off-by: Dominik Brodowski <linux@dominikbrodowski.net>

+258 -221
+45 -27
tools/power/cpupower/lib/cpufreq.c
··· 42 42 return sysfs_get_freq_hardware_limits(cpu, min, max); 43 43 } 44 44 45 - char * cpufreq_get_driver(unsigned int cpu) { 45 + char *cpufreq_get_driver(unsigned int cpu) 46 + { 46 47 return sysfs_get_freq_driver(cpu); 47 48 } 48 49 49 - void cpufreq_put_driver(char * ptr) { 50 + void cpufreq_put_driver(char *ptr) 51 + { 50 52 if (!ptr) 51 53 return; 52 54 free(ptr); 53 55 } 54 56 55 - struct cpufreq_policy * cpufreq_get_policy(unsigned int cpu) { 57 + struct cpufreq_policy *cpufreq_get_policy(unsigned int cpu) 58 + { 56 59 return sysfs_get_freq_policy(cpu); 57 60 } 58 61 59 - void cpufreq_put_policy(struct cpufreq_policy *policy) { 62 + void cpufreq_put_policy(struct cpufreq_policy *policy) 63 + { 60 64 if ((!policy) || (!policy->governor)) 61 65 return; 62 66 ··· 69 65 free(policy); 70 66 } 71 67 72 - struct cpufreq_available_governors * cpufreq_get_available_governors(unsigned int cpu) { 68 + struct cpufreq_available_governors *cpufreq_get_available_governors(unsigned 69 + int cpu) 70 + { 73 71 return sysfs_get_freq_available_governors(cpu); 74 72 } 75 73 76 - void cpufreq_put_available_governors(struct cpufreq_available_governors *any) { 74 + void cpufreq_put_available_governors(struct cpufreq_available_governors *any) 75 + { 77 76 struct cpufreq_available_governors *tmp, *next; 78 77 79 78 if (!any) ··· 93 86 } 94 87 95 88 96 - struct cpufreq_available_frequencies * cpufreq_get_available_frequencies(unsigned int cpu) { 89 + struct cpufreq_available_frequencies 90 + *cpufreq_get_available_frequencies(unsigned int cpu) 91 + { 97 92 return sysfs_get_available_frequencies(cpu); 98 93 } 99 94 100 - void cpufreq_put_available_frequencies(struct cpufreq_available_frequencies *any) { 95 + void cpufreq_put_available_frequencies(struct cpufreq_available_frequencies 96 + *any) { 101 97 struct cpufreq_available_frequencies *tmp, *next; 102 98 103 99 if (!any) ··· 115 105 } 116 106 117 107 118 - struct cpufreq_affected_cpus * cpufreq_get_affected_cpus(unsigned int cpu) { 108 + struct cpufreq_affected_cpus *cpufreq_get_affected_cpus(unsigned int cpu) 109 + { 119 110 return sysfs_get_freq_affected_cpus(cpu); 120 111 } 121 112 122 - void cpufreq_put_affected_cpus(struct cpufreq_affected_cpus *any) { 113 + void cpufreq_put_affected_cpus(struct cpufreq_affected_cpus *any) 114 + { 123 115 struct cpufreq_affected_cpus *tmp, *next; 124 116 125 117 if (!any) ··· 136 124 } 137 125 138 126 139 - struct cpufreq_affected_cpus * cpufreq_get_related_cpus(unsigned int cpu) { 127 + struct cpufreq_affected_cpus *cpufreq_get_related_cpus(unsigned int cpu) 128 + { 140 129 return sysfs_get_freq_related_cpus(cpu); 141 130 } 142 131 143 - void cpufreq_put_related_cpus(struct cpufreq_affected_cpus *any) { 132 + void cpufreq_put_related_cpus(struct cpufreq_affected_cpus *any) 133 + { 144 134 cpufreq_put_affected_cpus(any); 145 135 } 146 136 147 137 148 - int cpufreq_set_policy(unsigned int cpu, struct cpufreq_policy *policy) { 138 + int cpufreq_set_policy(unsigned int cpu, struct cpufreq_policy *policy) 139 + { 149 140 if (!policy || !(policy->governor)) 150 141 return -EINVAL; 151 142 ··· 156 141 } 157 142 158 143 159 - int cpufreq_modify_policy_min(unsigned int cpu, unsigned long min_freq) { 144 + int cpufreq_modify_policy_min(unsigned int cpu, unsigned long min_freq) 145 + { 160 146 return sysfs_modify_freq_policy_min(cpu, min_freq); 161 147 } 162 148 163 149 164 - int cpufreq_modify_policy_max(unsigned int cpu, unsigned long max_freq) { 150 + int cpufreq_modify_policy_max(unsigned int cpu, unsigned long max_freq) 151 + { 165 152 return sysfs_modify_freq_policy_max(cpu, max_freq); 166 153 } 167 154 168 155 169 - int cpufreq_modify_policy_governor(unsigned int cpu, char *governor) { 156 + int cpufreq_modify_policy_governor(unsigned int cpu, char *governor) 157 + { 170 158 if ((!governor) || (strlen(governor) > 19)) 171 159 return -EINVAL; 172 160 173 161 return sysfs_modify_freq_policy_governor(cpu, governor); 174 162 } 175 163 176 - int cpufreq_set_frequency(unsigned int cpu, unsigned long target_frequency) { 164 + int cpufreq_set_frequency(unsigned int cpu, unsigned long target_frequency) 165 + { 177 166 return sysfs_set_frequency(cpu, target_frequency); 178 167 } 179 168 180 - struct cpufreq_stats * cpufreq_get_stats(unsigned int cpu, unsigned long long *total_time) { 181 - struct cpufreq_stats *ret; 182 - 183 - ret = sysfs_get_freq_stats(cpu, total_time); 184 - return (ret); 169 + struct cpufreq_stats *cpufreq_get_stats(unsigned int cpu, 170 + unsigned long long *total_time) 171 + { 172 + return sysfs_get_freq_stats(cpu, total_time); 185 173 } 186 174 187 - void cpufreq_put_stats(struct cpufreq_stats *any) { 175 + void cpufreq_put_stats(struct cpufreq_stats *any) 176 + { 188 177 struct cpufreq_stats *tmp, *next; 189 178 190 179 if (!any) ··· 202 183 } 203 184 } 204 185 205 - unsigned long cpufreq_get_transitions(unsigned int cpu) { 206 - unsigned long ret = sysfs_get_freq_transitions(cpu); 207 - 208 - return (ret); 186 + unsigned long cpufreq_get_transitions(unsigned int cpu) 187 + { 188 + return sysfs_get_freq_transitions(cpu); 209 189 }
+31 -23
tools/power/cpupower/lib/cpufreq.h
··· 93 93 * considerations by cpufreq policy notifiers in the kernel. 94 94 */ 95 95 96 - extern int cpufreq_get_hardware_limits(unsigned int cpu, 97 - unsigned long *min, 98 - unsigned long *max); 96 + extern int cpufreq_get_hardware_limits(unsigned int cpu, 97 + unsigned long *min, 98 + unsigned long *max); 99 99 100 100 101 101 /* determine CPUfreq driver used ··· 104 104 * to avoid memory leakage, please. 105 105 */ 106 106 107 - extern char * cpufreq_get_driver(unsigned int cpu); 107 + extern char *cpufreq_get_driver(unsigned int cpu); 108 108 109 - extern void cpufreq_put_driver(char * ptr); 109 + extern void cpufreq_put_driver(char *ptr); 110 110 111 111 112 112 /* determine CPUfreq policy currently used ··· 116 116 */ 117 117 118 118 119 - extern struct cpufreq_policy * cpufreq_get_policy(unsigned int cpu); 119 + extern struct cpufreq_policy *cpufreq_get_policy(unsigned int cpu); 120 120 121 121 extern void cpufreq_put_policy(struct cpufreq_policy *policy); 122 122 ··· 129 129 */ 130 130 131 131 132 - extern struct cpufreq_available_governors * cpufreq_get_available_governors(unsigned int cpu); 132 + extern struct cpufreq_available_governors 133 + *cpufreq_get_available_governors(unsigned int cpu); 133 134 134 - extern void cpufreq_put_available_governors(struct cpufreq_available_governors *first); 135 + extern void cpufreq_put_available_governors( 136 + struct cpufreq_available_governors *first); 135 137 136 138 137 139 /* determine CPU frequency states available 138 140 * 139 - * only present on _some_ ->target() cpufreq drivers. For information purposes 140 - * only. Please free allocated memory by calling cpufreq_put_available_frequencies 141 - * after use. 141 + * Only present on _some_ ->target() cpufreq drivers. For information purposes 142 + * only. Please free allocated memory by calling 143 + * cpufreq_put_available_frequencies after use. 142 144 */ 143 145 144 - extern struct cpufreq_available_frequencies * cpufreq_get_available_frequencies(unsigned int cpu); 146 + extern struct cpufreq_available_frequencies 147 + *cpufreq_get_available_frequencies(unsigned int cpu); 145 148 146 - extern void cpufreq_put_available_frequencies(struct cpufreq_available_frequencies *first); 149 + extern void cpufreq_put_available_frequencies( 150 + struct cpufreq_available_frequencies *first); 147 151 148 152 149 - /* determine affected CPUs 153 + /* determine affected CPUs 150 154 * 151 155 * Remember to call cpufreq_put_affected_cpus when no longer needed 152 156 * to avoid memory leakage, please. 153 157 */ 154 158 155 - extern struct cpufreq_affected_cpus * cpufreq_get_affected_cpus(unsigned int cpu); 159 + extern struct cpufreq_affected_cpus *cpufreq_get_affected_cpus(unsigned 160 + int cpu); 156 161 157 162 extern void cpufreq_put_affected_cpus(struct cpufreq_affected_cpus *first); 158 163 159 164 160 - /* determine related CPUs 165 + /* determine related CPUs 161 166 * 162 167 * Remember to call cpufreq_put_related_cpus when no longer needed 163 168 * to avoid memory leakage, please. 164 169 */ 165 170 166 - extern struct cpufreq_affected_cpus * cpufreq_get_related_cpus(unsigned int cpu); 171 + extern struct cpufreq_affected_cpus *cpufreq_get_related_cpus(unsigned 172 + int cpu); 167 173 168 174 extern void cpufreq_put_related_cpus(struct cpufreq_affected_cpus *first); 169 175 ··· 179 173 * This is not available in all kernel versions or configurations. 180 174 */ 181 175 182 - extern struct cpufreq_stats * cpufreq_get_stats(unsigned int cpu, unsigned long long *total_time); 176 + extern struct cpufreq_stats *cpufreq_get_stats(unsigned int cpu, 177 + unsigned long long *total_time); 183 178 184 179 extern void cpufreq_put_stats(struct cpufreq_stats *stats); 185 180 186 181 extern unsigned long cpufreq_get_transitions(unsigned int cpu); 187 182 188 183 189 - /* set new cpufreq policy 190 - * 184 + /* set new cpufreq policy 185 + * 191 186 * Tries to set the passed policy as new policy as close as possible, 192 187 * but results may differ depending e.g. on governors being available. 193 188 */ ··· 196 189 extern int cpufreq_set_policy(unsigned int cpu, struct cpufreq_policy *policy); 197 190 198 191 199 - /* modify a policy by only changing min/max freq or governor 192 + /* modify a policy by only changing min/max freq or governor 200 193 * 201 194 * Does not check whether result is what was intended. 202 195 */ ··· 209 202 /* set a specific frequency 210 203 * 211 204 * Does only work if userspace governor can be used and no external 212 - * interference (other calls to this function or to set/modify_policy) 205 + * interference (other calls to this function or to set/modify_policy) 213 206 * occurs. Also does not work on ->range() cpufreq drivers. 214 207 */ 215 208 216 - extern int cpufreq_set_frequency(unsigned int cpu, unsigned long target_frequency); 209 + extern int cpufreq_set_frequency(unsigned int cpu, 210 + unsigned long target_frequency); 217 211 218 212 #ifdef __cplusplus 219 213 }
+160 -159
tools/power/cpupower/lib/sysfs.c
··· 24 24 static unsigned int sysfs_read_file(const char *path, char *buf, size_t buflen) 25 25 { 26 26 int fd; 27 - size_t numread; 27 + ssize_t numread; 28 28 29 - if ( ( fd = open(path, O_RDONLY) ) == -1 ) 29 + fd = open(path, O_RDONLY); 30 + if (fd == -1) 30 31 return 0; 31 32 32 33 numread = read(fd, buf, buflen - 1); 33 - if ( numread < 1 ) 34 - { 34 + if (numread < 1) { 35 35 close(fd); 36 36 return 0; 37 37 } ··· 39 39 buf[numread] = '\0'; 40 40 close(fd); 41 41 42 - return numread; 42 + return (unsigned int) numread; 43 43 } 44 44 45 45 ··· 65 65 { 66 66 char path[SYSFS_PATH_MAX]; 67 67 int fd; 68 - size_t numwrite; 68 + ssize_t numwrite; 69 69 70 70 snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/cpufreq/%s", 71 71 cpu, fname); 72 72 73 - if ( ( fd = open(path, O_WRONLY) ) == -1 ) 73 + fd = open(path, O_WRONLY); 74 + if (fd == -1) 74 75 return 0; 75 76 76 77 numwrite = write(fd, value, len); 77 - if ( numwrite < 1 ) 78 - { 78 + if (numwrite < 1) { 79 79 close(fd); 80 80 return 0; 81 81 } 82 82 83 83 close(fd); 84 84 85 - return numwrite; 85 + return (unsigned int) numwrite; 86 86 } 87 87 88 88 /* read access to files which contain one numeric value */ ··· 114 114 static unsigned long sysfs_cpufreq_get_one_value(unsigned int cpu, 115 115 enum cpufreq_value which) 116 116 { 117 - unsigned long value; 117 + unsigned long value; 118 118 unsigned int len; 119 119 char linebuf[MAX_LINE_LEN]; 120 120 char *endp; 121 121 122 - if ( which >= MAX_CPUFREQ_VALUE_READ_FILES ) 122 + if (which >= MAX_CPUFREQ_VALUE_READ_FILES) 123 123 return 0; 124 124 125 - if ( ( len = sysfs_cpufreq_read_file(cpu, cpufreq_value_files[which], 126 - linebuf, sizeof(linebuf))) == 0 ) 125 + len = sysfs_cpufreq_read_file(cpu, cpufreq_value_files[which], 126 + linebuf, sizeof(linebuf)); 127 + 128 + if (len == 0) 127 129 return 0; 128 130 129 131 value = strtoul(linebuf, &endp, 0); 130 132 131 - if ( endp == linebuf || errno == ERANGE ) 133 + if (endp == linebuf || errno == ERANGE) 132 134 return 0; 133 135 134 136 return value; ··· 150 148 }; 151 149 152 150 153 - static char * sysfs_cpufreq_get_one_string(unsigned int cpu, 151 + static char *sysfs_cpufreq_get_one_string(unsigned int cpu, 154 152 enum cpufreq_string which) 155 153 { 156 154 char linebuf[MAX_LINE_LEN]; ··· 160 158 if (which >= MAX_CPUFREQ_STRING_FILES) 161 159 return NULL; 162 160 163 - if ( ( len = sysfs_cpufreq_read_file(cpu, cpufreq_string_files[which], 164 - linebuf, sizeof(linebuf))) == 0 ) 161 + len = sysfs_cpufreq_read_file(cpu, cpufreq_string_files[which], 162 + linebuf, sizeof(linebuf)); 163 + if (len == 0) 165 164 return NULL; 166 165 167 - if ( ( result = strdup(linebuf) ) == NULL ) 166 + result = strdup(linebuf); 167 + if (result == NULL) 168 168 return NULL; 169 169 170 170 if (result[strlen(result) - 1] == '\n') ··· 199 195 if (which >= MAX_CPUFREQ_WRITE_FILES) 200 196 return 0; 201 197 202 - if ( sysfs_cpufreq_write_file(cpu, cpufreq_write_files[which], 203 - new_value, len) != len ) 198 + if (sysfs_cpufreq_write_file(cpu, cpufreq_write_files[which], 199 + new_value, len) != len) 204 200 return -ENODEV; 205 201 206 202 return 0; ··· 239 235 return 0; 240 236 } 241 237 242 - char * sysfs_get_freq_driver(unsigned int cpu) { 238 + char *sysfs_get_freq_driver(unsigned int cpu) 239 + { 243 240 return sysfs_cpufreq_get_one_string(cpu, SCALING_DRIVER); 244 241 } 245 242 246 - struct cpufreq_policy * sysfs_get_freq_policy(unsigned int cpu) { 243 + struct cpufreq_policy *sysfs_get_freq_policy(unsigned int cpu) 244 + { 247 245 struct cpufreq_policy *policy; 248 246 249 247 policy = malloc(sizeof(struct cpufreq_policy)); ··· 276 270 unsigned int pos, i; 277 271 unsigned int len; 278 272 279 - if ( ( len = sysfs_cpufreq_read_file(cpu, "scaling_available_governors", 280 - linebuf, sizeof(linebuf))) == 0 ) 281 - { 273 + len = sysfs_cpufreq_read_file(cpu, "scaling_available_governors", 274 + linebuf, sizeof(linebuf)); 275 + if (len == 0) 282 276 return NULL; 283 - } 284 277 285 278 pos = 0; 286 - for ( i = 0; i < len; i++ ) 287 - { 288 - if ( linebuf[i] == ' ' || linebuf[i] == '\n' ) 289 - { 290 - if ( i - pos < 2 ) 279 + for (i = 0; i < len; i++) { 280 + if (linebuf[i] == ' ' || linebuf[i] == '\n') { 281 + if (i - pos < 2) 291 282 continue; 292 - if ( current ) { 293 - current->next = malloc(sizeof *current ); 294 - if ( ! current->next ) 283 + if (current) { 284 + current->next = malloc(sizeof(*current)); 285 + if (!current->next) 295 286 goto error_out; 296 287 current = current->next; 297 288 } else { 298 - first = malloc( sizeof *first ); 299 - if ( ! first ) 289 + first = malloc(sizeof(*first)); 290 + if (!first) 300 291 goto error_out; 301 292 current = first; 302 293 } ··· 301 298 current->next = NULL; 302 299 303 300 current->governor = malloc(i - pos + 1); 304 - if ( ! current->governor ) 301 + if (!current->governor) 305 302 goto error_out; 306 303 307 - memcpy( current->governor, linebuf + pos, i - pos); 304 + memcpy(current->governor, linebuf + pos, i - pos); 308 305 current->governor[i - pos] = '\0'; 309 306 pos = i + 1; 310 307 } ··· 313 310 return first; 314 311 315 312 error_out: 316 - while ( first ) { 313 + while (first) { 317 314 current = first->next; 318 - if ( first->governor ) 319 - free( first->governor ); 320 - free( first ); 315 + if (first->governor) 316 + free(first->governor); 317 + free(first); 321 318 first = current; 322 319 } 323 320 return NULL; ··· 333 330 unsigned int pos, i; 334 331 unsigned int len; 335 332 336 - if ( ( len = sysfs_cpufreq_read_file(cpu, 337 - "scaling_available_frequencies", 338 - linebuf, sizeof(linebuf))) == 0 ) 339 - { 333 + len = sysfs_cpufreq_read_file(cpu, "scaling_available_frequencies", 334 + linebuf, sizeof(linebuf)); 335 + if (len == 0) 340 336 return NULL; 341 - } 342 337 343 338 pos = 0; 344 - for ( i = 0; i < len; i++ ) 345 - { 346 - if ( linebuf[i] == ' ' || linebuf[i] == '\n' ) 347 - { 348 - if ( i - pos < 2 ) 339 + for (i = 0; i < len; i++) { 340 + if (linebuf[i] == ' ' || linebuf[i] == '\n') { 341 + if (i - pos < 2) 349 342 continue; 350 - if ( i - pos >= SYSFS_PATH_MAX ) 343 + if (i - pos >= SYSFS_PATH_MAX) 351 344 goto error_out; 352 - if ( current ) { 353 - current->next = malloc(sizeof *current ); 354 - if ( ! current->next ) 345 + if (current) { 346 + current->next = malloc(sizeof(*current)); 347 + if (!current->next) 355 348 goto error_out; 356 349 current = current->next; 357 350 } else { 358 - first = malloc(sizeof *first ); 359 - if ( ! first ) 351 + first = malloc(sizeof(*first)); 352 + if (!first) 360 353 goto error_out; 361 354 current = first; 362 355 } ··· 361 362 362 363 memcpy(one_value, linebuf + pos, i - pos); 363 364 one_value[i - pos] = '\0'; 364 - if ( sscanf(one_value, "%lu", &current->frequency) != 1 ) 365 - goto error_out; 366 - 367 - pos = i + 1; 368 - } 369 - } 370 - 371 - return first; 372 - 373 - error_out: 374 - while ( first ) { 375 - current = first->next; 376 - free(first); 377 - first = current; 378 - } 379 - return NULL; 380 - } 381 - 382 - static struct cpufreq_affected_cpus * sysfs_get_cpu_list(unsigned int cpu, 383 - const char *file) { 384 - struct cpufreq_affected_cpus *first = NULL; 385 - struct cpufreq_affected_cpus *current = NULL; 386 - char one_value[SYSFS_PATH_MAX]; 387 - char linebuf[MAX_LINE_LEN]; 388 - unsigned int pos, i; 389 - unsigned int len; 390 - 391 - if ( ( len = sysfs_cpufreq_read_file(cpu, file, linebuf, 392 - sizeof(linebuf))) == 0 ) 393 - { 394 - return NULL; 395 - } 396 - 397 - pos = 0; 398 - for ( i = 0; i < len; i++ ) 399 - { 400 - if ( i == len || linebuf[i] == ' ' || linebuf[i] == '\n' ) 401 - { 402 - if ( i - pos < 1 ) 403 - continue; 404 - if ( i - pos >= SYSFS_PATH_MAX ) 405 - goto error_out; 406 - if ( current ) { 407 - current->next = malloc(sizeof *current); 408 - if ( ! current->next ) 409 - goto error_out; 410 - current = current->next; 411 - } else { 412 - first = malloc(sizeof *first); 413 - if ( ! first ) 414 - goto error_out; 415 - current = first; 416 - } 417 - current->first = first; 418 - current->next = NULL; 419 - 420 - memcpy(one_value, linebuf + pos, i - pos); 421 - one_value[i - pos] = '\0'; 422 - 423 - if ( sscanf(one_value, "%u", &current->cpu) != 1 ) 365 + if (sscanf(one_value, "%lu", &current->frequency) != 1) 424 366 goto error_out; 425 367 426 368 pos = i + 1; ··· 379 439 return NULL; 380 440 } 381 441 382 - struct cpufreq_affected_cpus * sysfs_get_freq_affected_cpus(unsigned int cpu) { 383 - return sysfs_get_cpu_list(cpu, "affected_cpus"); 384 - } 385 - 386 - struct cpufreq_affected_cpus * sysfs_get_freq_related_cpus(unsigned int cpu) { 387 - return sysfs_get_cpu_list(cpu, "related_cpus"); 388 - } 389 - 390 - struct cpufreq_stats * sysfs_get_freq_stats(unsigned int cpu, unsigned long long *total_time) { 391 - struct cpufreq_stats *first = NULL; 392 - struct cpufreq_stats *current = NULL; 442 + static struct cpufreq_affected_cpus *sysfs_get_cpu_list(unsigned int cpu, 443 + const char *file) 444 + { 445 + struct cpufreq_affected_cpus *first = NULL; 446 + struct cpufreq_affected_cpus *current = NULL; 393 447 char one_value[SYSFS_PATH_MAX]; 394 448 char linebuf[MAX_LINE_LEN]; 395 449 unsigned int pos, i; 396 450 unsigned int len; 397 451 398 - if ( ( len = sysfs_cpufreq_read_file(cpu, "stats/time_in_state", 399 - linebuf, sizeof(linebuf))) == 0 ) 452 + len = sysfs_cpufreq_read_file(cpu, file, linebuf, sizeof(linebuf)); 453 + if (len == 0) 400 454 return NULL; 401 455 402 - *total_time = 0; 403 456 pos = 0; 404 - for ( i = 0; i < len; i++ ) 405 - { 406 - if ( i == strlen(linebuf) || linebuf[i] == '\n' ) 407 - { 408 - if ( i - pos < 2 ) 457 + for (i = 0; i < len; i++) { 458 + if (i == len || linebuf[i] == ' ' || linebuf[i] == '\n') { 459 + if (i - pos < 1) 409 460 continue; 410 - if ( (i - pos) >= SYSFS_PATH_MAX ) 461 + if (i - pos >= SYSFS_PATH_MAX) 411 462 goto error_out; 412 - if ( current ) { 413 - current->next = malloc(sizeof *current ); 414 - if ( ! current->next ) 463 + if (current) { 464 + current->next = malloc(sizeof(*current)); 465 + if (!current->next) 415 466 goto error_out; 416 467 current = current->next; 417 468 } else { 418 - first = malloc(sizeof *first ); 419 - if ( ! first ) 469 + first = malloc(sizeof(*first)); 470 + if (!first) 420 471 goto error_out; 421 472 current = first; 422 473 } ··· 416 485 417 486 memcpy(one_value, linebuf + pos, i - pos); 418 487 one_value[i - pos] = '\0'; 419 - if ( sscanf(one_value, "%lu %llu", &current->frequency, &current->time_in_state) != 2 ) 488 + 489 + if (sscanf(one_value, "%u", &current->cpu) != 1) 490 + goto error_out; 491 + 492 + pos = i + 1; 493 + } 494 + } 495 + 496 + return first; 497 + 498 + error_out: 499 + while (first) { 500 + current = first->next; 501 + free(first); 502 + first = current; 503 + } 504 + return NULL; 505 + } 506 + 507 + struct cpufreq_affected_cpus *sysfs_get_freq_affected_cpus(unsigned int cpu) 508 + { 509 + return sysfs_get_cpu_list(cpu, "affected_cpus"); 510 + } 511 + 512 + struct cpufreq_affected_cpus *sysfs_get_freq_related_cpus(unsigned int cpu) 513 + { 514 + return sysfs_get_cpu_list(cpu, "related_cpus"); 515 + } 516 + 517 + struct cpufreq_stats *sysfs_get_freq_stats(unsigned int cpu, 518 + unsigned long long *total_time) { 519 + struct cpufreq_stats *first = NULL; 520 + struct cpufreq_stats *current = NULL; 521 + char one_value[SYSFS_PATH_MAX]; 522 + char linebuf[MAX_LINE_LEN]; 523 + unsigned int pos, i; 524 + unsigned int len; 525 + 526 + len = sysfs_cpufreq_read_file(cpu, "stats/time_in_state", 527 + linebuf, sizeof(linebuf)); 528 + if (len == 0) 529 + return NULL; 530 + 531 + *total_time = 0; 532 + pos = 0; 533 + for (i = 0; i < len; i++) { 534 + if (i == strlen(linebuf) || linebuf[i] == '\n') { 535 + if (i - pos < 2) 536 + continue; 537 + if ((i - pos) >= SYSFS_PATH_MAX) 538 + goto error_out; 539 + if (current) { 540 + current->next = malloc(sizeof(*current)); 541 + if (!current->next) 542 + goto error_out; 543 + current = current->next; 544 + } else { 545 + first = malloc(sizeof(*first)); 546 + if (!first) 547 + goto error_out; 548 + current = first; 549 + } 550 + current->first = first; 551 + current->next = NULL; 552 + 553 + memcpy(one_value, linebuf + pos, i - pos); 554 + one_value[i - pos] = '\0'; 555 + if (sscanf(one_value, "%lu %llu", 556 + &current->frequency, 557 + &current->time_in_state) != 2) 420 558 goto error_out; 421 559 422 560 *total_time = *total_time + current->time_in_state; ··· 496 496 return first; 497 497 498 498 error_out: 499 - while ( first ) { 499 + while (first) { 500 500 current = first->next; 501 501 free(first); 502 502 first = current; ··· 511 511 512 512 static int verify_gov(char *new_gov, char *passed_gov) 513 513 { 514 - unsigned int i, j=0; 514 + unsigned int i, j = 0; 515 515 516 516 if (!passed_gov || (strlen(passed_gov) > 19)) 517 517 return -EINVAL; 518 518 519 519 strncpy(new_gov, passed_gov, 20); 520 - for (i=0;i<20;i++) { 520 + for (i = 0; i < 20; i++) { 521 521 if (j) { 522 522 new_gov[i] = '\0'; 523 523 continue; 524 524 } 525 - if ((new_gov[i] >= 'a') && (new_gov[i] <= 'z')) { 525 + if ((new_gov[i] >= 'a') && (new_gov[i] <= 'z')) 526 526 continue; 527 - } 528 - if ((new_gov[i] >= 'A') && (new_gov[i] <= 'Z')) { 527 + 528 + if ((new_gov[i] >= 'A') && (new_gov[i] <= 'Z')) 529 529 continue; 530 - } 531 - if (new_gov[i] == '-') { 530 + 531 + if (new_gov[i] == '-') 532 532 continue; 533 - } 534 - if (new_gov[i] == '_') { 533 + 534 + if (new_gov[i] == '_') 535 535 continue; 536 - } 536 + 537 537 if (new_gov[i] == '\0') { 538 538 j = 1; 539 539 continue; ··· 627 627 gov, strlen(gov)); 628 628 } 629 629 630 - int sysfs_set_frequency(unsigned int cpu, unsigned long target_frequency) { 630 + int sysfs_set_frequency(unsigned int cpu, unsigned long target_frequency) 631 + { 631 632 struct cpufreq_policy *pol = sysfs_get_freq_policy(cpu); 632 633 char userspace_gov[] = "userspace"; 633 634 char freq[SYSFS_PATH_MAX]; ··· 641 640 ret = sysfs_modify_freq_policy_governor(cpu, userspace_gov); 642 641 if (ret) { 643 642 cpufreq_put_policy(pol); 644 - return (ret); 643 + return ret; 645 644 } 646 645 } 647 646 ··· 663 662 664 663 snprintf(file, SYSFS_PATH_MAX, PATH_TO_CPU "cpu%u/", cpu); 665 664 666 - if ( stat(file, &statbuf) != 0 ) 665 + if (stat(file, &statbuf) != 0) 667 666 return -ENOSYS; 668 667 669 668 return S_ISDIR(statbuf.st_mode) ? 0 : -ENOSYS;
+22 -12
tools/power/cpupower/lib/sysfs.h
··· 5 5 extern unsigned long sysfs_get_freq_kernel(unsigned int cpu); 6 6 extern unsigned long sysfs_get_freq_hardware(unsigned int cpu); 7 7 extern unsigned long sysfs_get_freq_transition_latency(unsigned int cpu); 8 - extern int sysfs_get_freq_hardware_limits(unsigned int cpu, unsigned long *min, unsigned long *max); 9 - extern char * sysfs_get_freq_driver(unsigned int cpu); 10 - extern struct cpufreq_policy * sysfs_get_freq_policy(unsigned int cpu); 11 - extern struct cpufreq_available_governors * sysfs_get_freq_available_governors(unsigned int cpu); 12 - extern struct cpufreq_available_frequencies * sysfs_get_available_frequencies(unsigned int cpu); 13 - extern struct cpufreq_affected_cpus * sysfs_get_freq_affected_cpus(unsigned int cpu); 14 - extern struct cpufreq_affected_cpus * sysfs_get_freq_related_cpus(unsigned int cpu); 15 - extern struct cpufreq_stats * sysfs_get_freq_stats(unsigned int cpu, unsigned long long *total_time); 8 + extern int sysfs_get_freq_hardware_limits(unsigned int cpu, 9 + unsigned long *min, unsigned long *max); 10 + extern char *sysfs_get_freq_driver(unsigned int cpu); 11 + extern struct cpufreq_policy *sysfs_get_freq_policy(unsigned int cpu); 12 + extern struct cpufreq_available_governors *sysfs_get_freq_available_governors( 13 + unsigned int cpu); 14 + extern struct cpufreq_available_frequencies *sysfs_get_available_frequencies( 15 + unsigned int cpu); 16 + extern struct cpufreq_affected_cpus *sysfs_get_freq_affected_cpus( 17 + unsigned int cpu); 18 + extern struct cpufreq_affected_cpus *sysfs_get_freq_related_cpus( 19 + unsigned int cpu); 20 + extern struct cpufreq_stats *sysfs_get_freq_stats(unsigned int cpu, 21 + unsigned long long *total_time); 16 22 extern unsigned long sysfs_get_freq_transitions(unsigned int cpu); 17 - extern int sysfs_set_freq_policy(unsigned int cpu, struct cpufreq_policy *policy); 18 - extern int sysfs_modify_freq_policy_min(unsigned int cpu, unsigned long min_freq); 19 - extern int sysfs_modify_freq_policy_max(unsigned int cpu, unsigned long max_freq); 23 + extern int sysfs_set_freq_policy(unsigned int cpu, 24 + struct cpufreq_policy *policy); 25 + extern int sysfs_modify_freq_policy_min(unsigned int cpu, 26 + unsigned long min_freq); 27 + extern int sysfs_modify_freq_policy_max(unsigned int cpu, 28 + unsigned long max_freq); 20 29 extern int sysfs_modify_freq_policy_governor(unsigned int cpu, char *governor); 21 - extern int sysfs_set_frequency(unsigned int cpu, unsigned long target_frequency); 30 + extern int sysfs_set_frequency(unsigned int cpu, 31 + unsigned long target_frequency);