diff options
Diffstat (limited to 'tools/power/cpupower/utils/helpers/sysfs.c')
-rw-r--r-- | tools/power/cpupower/utils/helpers/sysfs.c | 112 |
1 files changed, 60 insertions, 52 deletions
diff --git a/tools/power/cpupower/utils/helpers/sysfs.c b/tools/power/cpupower/utils/helpers/sysfs.c index 0c534e79652b..55e2466674c6 100644 --- a/tools/power/cpupower/utils/helpers/sysfs.c +++ b/tools/power/cpupower/utils/helpers/sysfs.c | |||
@@ -19,14 +19,14 @@ | |||
19 | unsigned int sysfs_read_file(const char *path, char *buf, size_t buflen) | 19 | unsigned int sysfs_read_file(const char *path, char *buf, size_t buflen) |
20 | { | 20 | { |
21 | int fd; | 21 | int fd; |
22 | size_t numread; | 22 | ssize_t numread; |
23 | 23 | ||
24 | if ( ( fd = open(path, O_RDONLY) ) == -1 ) | 24 | fd = open(path, O_RDONLY); |
25 | if (fd == -1) | ||
25 | return 0; | 26 | return 0; |
26 | 27 | ||
27 | numread = read(fd, buf, buflen - 1); | 28 | numread = read(fd, buf, buflen - 1); |
28 | if ( numread < 1 ) | 29 | if (numread < 1) { |
29 | { | ||
30 | close(fd); | 30 | close(fd); |
31 | return 0; | 31 | return 0; |
32 | } | 32 | } |
@@ -34,26 +34,26 @@ unsigned int sysfs_read_file(const char *path, char *buf, size_t buflen) | |||
34 | buf[numread] = '\0'; | 34 | buf[numread] = '\0'; |
35 | close(fd); | 35 | close(fd); |
36 | 36 | ||
37 | return numread; | 37 | return (unsigned int) numread; |
38 | } | 38 | } |
39 | 39 | ||
40 | static unsigned int sysfs_write_file(const char *path, | 40 | static unsigned int sysfs_write_file(const char *path, |
41 | const char *value, size_t len) | 41 | const char *value, size_t len) |
42 | { | 42 | { |
43 | int fd; | 43 | int fd; |
44 | size_t numwrite; | 44 | ssize_t numwrite; |
45 | 45 | ||
46 | if ( ( fd = open(path, O_WRONLY) ) == -1 ) | 46 | fd = open(path, O_WRONLY); |
47 | if (fd == -1) | ||
47 | return 0; | 48 | return 0; |
48 | 49 | ||
49 | numwrite = write(fd, value, len); | 50 | numwrite = write(fd, value, len); |
50 | if ( numwrite < 1 ) | 51 | if (numwrite < 1) { |
51 | { | ||
52 | close(fd); | 52 | close(fd); |
53 | return 0; | 53 | return 0; |
54 | } | 54 | } |
55 | close(fd); | 55 | close(fd); |
56 | return numwrite; | 56 | return (unsigned int) numwrite; |
57 | } | 57 | } |
58 | 58 | ||
59 | /* CPUidle idlestate specific /sys/devices/system/cpu/cpuX/cpuidle/ access */ | 59 | /* CPUidle idlestate specific /sys/devices/system/cpu/cpuX/cpuidle/ access */ |
@@ -69,17 +69,17 @@ unsigned int sysfs_idlestate_read_file(unsigned int cpu, unsigned int idlestate, | |||
69 | { | 69 | { |
70 | char path[SYSFS_PATH_MAX]; | 70 | char path[SYSFS_PATH_MAX]; |
71 | int fd; | 71 | int fd; |
72 | size_t numread; | 72 | ssize_t numread; |
73 | 73 | ||
74 | snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/cpuidle/state%u/%s", | 74 | snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/cpuidle/state%u/%s", |
75 | cpu, idlestate, fname); | 75 | cpu, idlestate, fname); |
76 | 76 | ||
77 | if ( ( fd = open(path, O_RDONLY) ) == -1 ) | 77 | fd = open(path, O_RDONLY); |
78 | if (fd == -1) | ||
78 | return 0; | 79 | return 0; |
79 | 80 | ||
80 | numread = read(fd, buf, buflen - 1); | 81 | numread = read(fd, buf, buflen - 1); |
81 | if ( numread < 1 ) | 82 | if (numread < 1) { |
82 | { | ||
83 | close(fd); | 83 | close(fd); |
84 | return 0; | 84 | return 0; |
85 | } | 85 | } |
@@ -87,7 +87,7 @@ unsigned int sysfs_idlestate_read_file(unsigned int cpu, unsigned int idlestate, | |||
87 | buf[numread] = '\0'; | 87 | buf[numread] = '\0'; |
88 | close(fd); | 88 | close(fd); |
89 | 89 | ||
90 | return numread; | 90 | return (unsigned int) numread; |
91 | } | 91 | } |
92 | 92 | ||
93 | /* read access to files which contain one numeric value */ | 93 | /* read access to files which contain one numeric value */ |
@@ -116,19 +116,18 @@ static unsigned long long sysfs_idlestate_get_one_value(unsigned int cpu, | |||
116 | char linebuf[MAX_LINE_LEN]; | 116 | char linebuf[MAX_LINE_LEN]; |
117 | char *endp; | 117 | char *endp; |
118 | 118 | ||
119 | if ( which >= MAX_IDLESTATE_VALUE_FILES ) | 119 | if (which >= MAX_IDLESTATE_VALUE_FILES) |
120 | return 0; | 120 | return 0; |
121 | 121 | ||
122 | if ( ( len = sysfs_idlestate_read_file(cpu, idlestate, | 122 | len = sysfs_idlestate_read_file(cpu, idlestate, |
123 | idlestate_value_files[which], | 123 | idlestate_value_files[which], |
124 | linebuf, sizeof(linebuf))) == 0 ) | 124 | linebuf, sizeof(linebuf)); |
125 | { | 125 | if (len == 0) |
126 | return 0; | 126 | return 0; |
127 | } | ||
128 | 127 | ||
129 | value = strtoull(linebuf, &endp, 0); | 128 | value = strtoull(linebuf, &endp, 0); |
130 | 129 | ||
131 | if ( endp == linebuf || errno == ERANGE ) | 130 | if (endp == linebuf || errno == ERANGE) |
132 | return 0; | 131 | return 0; |
133 | 132 | ||
134 | return value; | 133 | return value; |
@@ -148,9 +147,9 @@ static const char *idlestate_string_files[MAX_IDLESTATE_STRING_FILES] = { | |||
148 | }; | 147 | }; |
149 | 148 | ||
150 | 149 | ||
151 | static char * sysfs_idlestate_get_one_string(unsigned int cpu, | 150 | static char *sysfs_idlestate_get_one_string(unsigned int cpu, |
152 | unsigned int idlestate, | 151 | unsigned int idlestate, |
153 | enum idlestate_string which) | 152 | enum idlestate_string which) |
154 | { | 153 | { |
155 | char linebuf[MAX_LINE_LEN]; | 154 | char linebuf[MAX_LINE_LEN]; |
156 | char *result; | 155 | char *result; |
@@ -159,12 +158,14 @@ static char * sysfs_idlestate_get_one_string(unsigned int cpu, | |||
159 | if (which >= MAX_IDLESTATE_STRING_FILES) | 158 | if (which >= MAX_IDLESTATE_STRING_FILES) |
160 | return NULL; | 159 | return NULL; |
161 | 160 | ||
162 | if ( ( len = sysfs_idlestate_read_file(cpu, idlestate, | 161 | len = sysfs_idlestate_read_file(cpu, idlestate, |
163 | idlestate_string_files[which], | 162 | idlestate_string_files[which], |
164 | linebuf, sizeof(linebuf))) == 0 ) | 163 | linebuf, sizeof(linebuf)); |
164 | if (len == 0) | ||
165 | return NULL; | 165 | return NULL; |
166 | 166 | ||
167 | if ( ( result = strdup(linebuf) ) == NULL ) | 167 | result = strdup(linebuf); |
168 | if (result == NULL) | ||
168 | return NULL; | 169 | return NULL; |
169 | 170 | ||
170 | if (result[strlen(result) - 1] == '\n') | 171 | if (result[strlen(result) - 1] == '\n') |
@@ -173,27 +174,30 @@ static char * sysfs_idlestate_get_one_string(unsigned int cpu, | |||
173 | return result; | 174 | return result; |
174 | } | 175 | } |
175 | 176 | ||
176 | unsigned long sysfs_get_idlestate_latency(unsigned int cpu, unsigned int idlestate) | 177 | unsigned long sysfs_get_idlestate_latency(unsigned int cpu, |
178 | unsigned int idlestate) | ||
177 | { | 179 | { |
178 | return sysfs_idlestate_get_one_value(cpu, idlestate, IDLESTATE_LATENCY); | 180 | return sysfs_idlestate_get_one_value(cpu, idlestate, IDLESTATE_LATENCY); |
179 | } | 181 | } |
180 | 182 | ||
181 | unsigned long sysfs_get_idlestate_usage(unsigned int cpu, unsigned int idlestate) | 183 | unsigned long sysfs_get_idlestate_usage(unsigned int cpu, |
184 | unsigned int idlestate) | ||
182 | { | 185 | { |
183 | return sysfs_idlestate_get_one_value(cpu, idlestate, IDLESTATE_USAGE); | 186 | return sysfs_idlestate_get_one_value(cpu, idlestate, IDLESTATE_USAGE); |
184 | } | 187 | } |
185 | 188 | ||
186 | unsigned long long sysfs_get_idlestate_time(unsigned int cpu, unsigned int idlestate) | 189 | unsigned long long sysfs_get_idlestate_time(unsigned int cpu, |
190 | unsigned int idlestate) | ||
187 | { | 191 | { |
188 | return sysfs_idlestate_get_one_value(cpu, idlestate, IDLESTATE_TIME); | 192 | return sysfs_idlestate_get_one_value(cpu, idlestate, IDLESTATE_TIME); |
189 | } | 193 | } |
190 | 194 | ||
191 | char * sysfs_get_idlestate_name(unsigned int cpu, unsigned int idlestate) | 195 | char *sysfs_get_idlestate_name(unsigned int cpu, unsigned int idlestate) |
192 | { | 196 | { |
193 | return sysfs_idlestate_get_one_string(cpu, idlestate, IDLESTATE_NAME); | 197 | return sysfs_idlestate_get_one_string(cpu, idlestate, IDLESTATE_NAME); |
194 | } | 198 | } |
195 | 199 | ||
196 | char * sysfs_get_idlestate_desc(unsigned int cpu, unsigned int idlestate) | 200 | char *sysfs_get_idlestate_desc(unsigned int cpu, unsigned int idlestate) |
197 | { | 201 | { |
198 | return sysfs_idlestate_get_one_string(cpu, idlestate, IDLESTATE_DESC); | 202 | return sysfs_idlestate_get_one_string(cpu, idlestate, IDLESTATE_DESC); |
199 | } | 203 | } |
@@ -211,14 +215,14 @@ int sysfs_get_idlestate_count(unsigned int cpu) | |||
211 | 215 | ||
212 | 216 | ||
213 | snprintf(file, SYSFS_PATH_MAX, PATH_TO_CPU "cpuidle"); | 217 | snprintf(file, SYSFS_PATH_MAX, PATH_TO_CPU "cpuidle"); |
214 | if ( stat(file, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) | 218 | if (stat(file, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) |
215 | return -ENODEV; | 219 | return -ENODEV; |
216 | 220 | ||
217 | snprintf(file, SYSFS_PATH_MAX, PATH_TO_CPU "cpu%u/cpuidle/state0", cpu); | 221 | snprintf(file, SYSFS_PATH_MAX, PATH_TO_CPU "cpu%u/cpuidle/state0", cpu); |
218 | if ( stat(file, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) | 222 | if (stat(file, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) |
219 | return 0; | 223 | return 0; |
220 | 224 | ||
221 | while(stat(file, &statbuf) == 0 && S_ISDIR(statbuf.st_mode)) { | 225 | while (stat(file, &statbuf) == 0 && S_ISDIR(statbuf.st_mode)) { |
222 | snprintf(file, SYSFS_PATH_MAX, PATH_TO_CPU | 226 | snprintf(file, SYSFS_PATH_MAX, PATH_TO_CPU |
223 | "cpu%u/cpuidle/state%d", cpu, idlestates); | 227 | "cpu%u/cpuidle/state%d", cpu, idlestates); |
224 | idlestates++; | 228 | idlestates++; |
@@ -261,7 +265,7 @@ static const char *cpuidle_string_files[MAX_CPUIDLE_STRING_FILES] = { | |||
261 | }; | 265 | }; |
262 | 266 | ||
263 | 267 | ||
264 | static char * sysfs_cpuidle_get_one_string(enum cpuidle_string which) | 268 | static char *sysfs_cpuidle_get_one_string(enum cpuidle_string which) |
265 | { | 269 | { |
266 | char linebuf[MAX_LINE_LEN]; | 270 | char linebuf[MAX_LINE_LEN]; |
267 | char *result; | 271 | char *result; |
@@ -270,11 +274,13 @@ static char * sysfs_cpuidle_get_one_string(enum cpuidle_string which) | |||
270 | if (which >= MAX_CPUIDLE_STRING_FILES) | 274 | if (which >= MAX_CPUIDLE_STRING_FILES) |
271 | return NULL; | 275 | return NULL; |
272 | 276 | ||
273 | if ( ( len = sysfs_cpuidle_read_file(cpuidle_string_files[which], | 277 | len = sysfs_cpuidle_read_file(cpuidle_string_files[which], |
274 | linebuf, sizeof(linebuf))) == 0 ) | 278 | linebuf, sizeof(linebuf)); |
279 | if (len == 0) | ||
275 | return NULL; | 280 | return NULL; |
276 | 281 | ||
277 | if ( ( result = strdup(linebuf) ) == NULL ) | 282 | result = strdup(linebuf); |
283 | if (result == NULL) | ||
278 | return NULL; | 284 | return NULL; |
279 | 285 | ||
280 | if (result[strlen(result) - 1] == '\n') | 286 | if (result[strlen(result) - 1] == '\n') |
@@ -283,7 +289,7 @@ static char * sysfs_cpuidle_get_one_string(enum cpuidle_string which) | |||
283 | return result; | 289 | return result; |
284 | } | 290 | } |
285 | 291 | ||
286 | char * sysfs_get_cpuidle_governor(void) | 292 | char *sysfs_get_cpuidle_governor(void) |
287 | { | 293 | { |
288 | char *tmp = sysfs_cpuidle_get_one_string(CPUIDLE_GOVERNOR_RO); | 294 | char *tmp = sysfs_cpuidle_get_one_string(CPUIDLE_GOVERNOR_RO); |
289 | if (!tmp) | 295 | if (!tmp) |
@@ -292,7 +298,7 @@ char * sysfs_get_cpuidle_governor(void) | |||
292 | return tmp; | 298 | return tmp; |
293 | } | 299 | } |
294 | 300 | ||
295 | char * sysfs_get_cpuidle_driver(void) | 301 | char *sysfs_get_cpuidle_driver(void) |
296 | { | 302 | { |
297 | return sysfs_cpuidle_get_one_string(CPUIDLE_DRIVER); | 303 | return sysfs_cpuidle_get_one_string(CPUIDLE_DRIVER); |
298 | } | 304 | } |
@@ -304,9 +310,9 @@ char * sysfs_get_cpuidle_driver(void) | |||
304 | * | 310 | * |
305 | * Returns negative value on failure | 311 | * Returns negative value on failure |
306 | */ | 312 | */ |
307 | int sysfs_get_sched(const char* smt_mc) | 313 | int sysfs_get_sched(const char *smt_mc) |
308 | { | 314 | { |
309 | unsigned long value; | 315 | unsigned long value; |
310 | char linebuf[MAX_LINE_LEN]; | 316 | char linebuf[MAX_LINE_LEN]; |
311 | char *endp; | 317 | char *endp; |
312 | char path[SYSFS_PATH_MAX]; | 318 | char path[SYSFS_PATH_MAX]; |
@@ -314,11 +320,12 @@ int sysfs_get_sched(const char* smt_mc) | |||
314 | if (strcmp("mc", smt_mc) && strcmp("smt", smt_mc)) | 320 | if (strcmp("mc", smt_mc) && strcmp("smt", smt_mc)) |
315 | return -EINVAL; | 321 | return -EINVAL; |
316 | 322 | ||
317 | snprintf(path, sizeof(path), PATH_TO_CPU "sched_%s_power_savings", smt_mc); | 323 | snprintf(path, sizeof(path), |
318 | if (sysfs_read_file(path, linebuf, MAX_LINE_LEN) == 0 ) | 324 | PATH_TO_CPU "sched_%s_power_savings", smt_mc); |
325 | if (sysfs_read_file(path, linebuf, MAX_LINE_LEN) == 0) | ||
319 | return -1; | 326 | return -1; |
320 | value = strtoul(linebuf, &endp, 0); | 327 | value = strtoul(linebuf, &endp, 0); |
321 | if ( endp == linebuf || errno == ERANGE ) | 328 | if (endp == linebuf || errno == ERANGE) |
322 | return -1; | 329 | return -1; |
323 | return value; | 330 | return value; |
324 | } | 331 | } |
@@ -329,7 +336,7 @@ int sysfs_get_sched(const char* smt_mc) | |||
329 | * | 336 | * |
330 | * Returns negative value on failure | 337 | * Returns negative value on failure |
331 | */ | 338 | */ |
332 | int sysfs_set_sched(const char* smt_mc, int val) | 339 | int sysfs_set_sched(const char *smt_mc, int val) |
333 | { | 340 | { |
334 | char linebuf[MAX_LINE_LEN]; | 341 | char linebuf[MAX_LINE_LEN]; |
335 | char path[SYSFS_PATH_MAX]; | 342 | char path[SYSFS_PATH_MAX]; |
@@ -338,13 +345,14 @@ int sysfs_set_sched(const char* smt_mc, int val) | |||
338 | if (strcmp("mc", smt_mc) && strcmp("smt", smt_mc)) | 345 | if (strcmp("mc", smt_mc) && strcmp("smt", smt_mc)) |
339 | return -EINVAL; | 346 | return -EINVAL; |
340 | 347 | ||
341 | snprintf(path, sizeof(path), PATH_TO_CPU "sched_%s_power_savings", smt_mc); | 348 | snprintf(path, sizeof(path), |
349 | PATH_TO_CPU "sched_%s_power_savings", smt_mc); | ||
342 | sprintf(linebuf, "%d", val); | 350 | sprintf(linebuf, "%d", val); |
343 | 351 | ||
344 | if ( stat(path, &statbuf) != 0 ) | 352 | if (stat(path, &statbuf) != 0) |
345 | return -ENODEV; | 353 | return -ENODEV; |
346 | 354 | ||
347 | if (sysfs_write_file(path, linebuf, MAX_LINE_LEN) == 0 ) | 355 | if (sysfs_write_file(path, linebuf, MAX_LINE_LEN) == 0) |
348 | return -1; | 356 | return -1; |
349 | return 0; | 357 | return 0; |
350 | } | 358 | } |