From 7fe2f6399a84760a9af8896ac152728250f82adb Mon Sep 17 00:00:00 2001 From: Dominik Brodowski Date: Wed, 30 Mar 2011 16:30:11 +0200 Subject: cpupowerutils - cpufrequtils extended with quite some features CPU power consumption vs performance tuning is no longer limited to CPU frequency switching anymore: deep sleep states, traditional dynamic frequency scaling and hidden turbo/boost frequencies are tied close together and depend on each other. The first two exist on different architectures like PPC, Itanium and ARM, the latter (so far) only on X86. On X86 the APU (CPU+GPU) will only run most efficiently if CPU and GPU has proper power management in place. Users and Developers want to have *one* tool to get an overview what their system supports and to monitor and debug CPU power management in detail. The tool should compile and work on as many architectures as possible. Once this tool stabilizes a bit, it is intended to replace the Intel-specific tools in tools/power/x86 Signed-off-by: Dominik Brodowski --- tools/power/cpupower/utils/helpers/bitmask.c | 290 +++++++++++++++++++++++++++ 1 file changed, 290 insertions(+) create mode 100644 tools/power/cpupower/utils/helpers/bitmask.c (limited to 'tools/power/cpupower/utils/helpers/bitmask.c') diff --git a/tools/power/cpupower/utils/helpers/bitmask.c b/tools/power/cpupower/utils/helpers/bitmask.c new file mode 100644 index 00000000000..60f4d69bb20 --- /dev/null +++ b/tools/power/cpupower/utils/helpers/bitmask.c @@ -0,0 +1,290 @@ +#include +#include +#include + +#include + +/* How many bits in an unsigned long */ +#define bitsperlong (8 * sizeof(unsigned long)) + +/* howmany(a,b) : how many elements of size b needed to hold all of a */ +#define howmany(x,y) (((x)+((y)-1))/(y)) + +/* How many longs in mask of n bits */ +#define longsperbits(n) howmany(n, bitsperlong) + +#define max(a,b) ((a) > (b) ? (a) : (b)) + +/* + * Allocate and free `struct bitmask *` + */ + +/* Allocate a new `struct bitmask` with a size of n bits */ +struct bitmask *bitmask_alloc(unsigned int n) +{ + struct bitmask *bmp; + + bmp = malloc(sizeof(*bmp)); + if (bmp == 0) + return 0; + bmp->size = n; + bmp->maskp = calloc(longsperbits(n), sizeof(unsigned long)); + if (bmp->maskp == 0) { + free(bmp); + return 0; + } + return bmp; +} + +/* Free `struct bitmask` */ +void bitmask_free(struct bitmask *bmp) +{ + if (bmp == 0) + return; + free(bmp->maskp); + bmp->maskp = (unsigned long *)0xdeadcdef; /* double free tripwire */ + free(bmp); +} + +/* + * The routines _getbit() and _setbit() are the only + * routines that actually understand the layout of bmp->maskp[]. + * + * On little endian architectures, this could simply be an array of + * bytes. But the kernel layout of bitmasks _is_ visible to userspace + * via the sched_(set/get)affinity calls in Linux 2.6, and on big + * endian architectures, it is painfully obvious that this is an + * array of unsigned longs. + */ + +/* Return the value (0 or 1) of bit n in bitmask bmp */ +static unsigned int _getbit(const struct bitmask *bmp, unsigned int n) +{ + if (n < bmp->size) + return (bmp->maskp[n/bitsperlong] >> (n % bitsperlong)) & 1; + else + return 0; +} + +/* Set bit n in bitmask bmp to value v (0 or 1) */ +static void _setbit(struct bitmask *bmp, unsigned int n, unsigned int v) +{ + if (n < bmp->size) { + if (v) + bmp->maskp[n/bitsperlong] |= 1UL << (n % bitsperlong); + else + bmp->maskp[n/bitsperlong] &= ~(1UL << (n % bitsperlong)); + } +} + +/* + * When parsing bitmask lists, only allow numbers, separated by one + * of the allowed next characters. + * + * The parameter 'sret' is the return from a sscanf "%u%c". It is + * -1 if the sscanf input string was empty. It is 0 if the first + * character in the sscanf input string was not a decimal number. + * It is 1 if the unsigned number matching the "%u" was the end of the + * input string. It is 2 if one or more additional characters followed + * the matched unsigned number. If it is 2, then 'nextc' is the first + * character following the number. The parameter 'ok_next_chars' + * is the nul-terminated list of allowed next characters. + * + * The mask term just scanned was ok if and only if either the numbers + * matching the %u were all of the input or if the next character in + * the input past the numbers was one of the allowed next characters. + */ +static int scan_was_ok(int sret, char nextc, const char *ok_next_chars) +{ + return sret == 1 || + (sret == 2 && strchr(ok_next_chars, nextc) != NULL); +} + +static const char *nexttoken(const char *q, int sep) +{ + if (q) + q = strchr(q, sep); + if (q) + q++; + return q; +} + +/* Set a single bit i in bitmask */ +struct bitmask *bitmask_setbit(struct bitmask *bmp, unsigned int i) +{ + _setbit(bmp, i, 1); + return bmp; +} + +/* Set all bits in bitmask: bmp = ~0 */ +struct bitmask *bitmask_setall(struct bitmask *bmp) +{ + unsigned int i; + for (i = 0; i < bmp->size; i++) + _setbit(bmp, i, 1); + return bmp; +} + +/* Clear all bits in bitmask: bmp = 0 */ +struct bitmask *bitmask_clearall(struct bitmask *bmp) +{ + unsigned int i; + for (i = 0; i < bmp->size; i++) + _setbit(bmp, i, 0); + return bmp; +} + +/* True if all bits are clear */ +int bitmask_isallclear(const struct bitmask *bmp) +{ + unsigned int i; + for (i = 0; i < bmp->size; i++) + if (_getbit(bmp, i)) + return 0; + return 1; +} + +/* True if specified bit i is set */ +int bitmask_isbitset(const struct bitmask *bmp, unsigned int i) +{ + return _getbit(bmp, i); +} + +/* Number of lowest set bit (min) */ +unsigned int bitmask_first(const struct bitmask *bmp) +{ + return bitmask_next(bmp, 0); +} + +/* Number of highest set bit (max) */ +unsigned int bitmask_last(const struct bitmask *bmp) +{ + unsigned int i; + unsigned int m = bmp->size; + for (i = 0; i < bmp->size; i++) + if (_getbit(bmp, i)) + m = i; + return m; +} + +/* Number of next set bit at or above given bit i */ +unsigned int bitmask_next(const struct bitmask *bmp, unsigned int i) +{ + unsigned int n; + for (n = i; n < bmp->size; n++) + if (_getbit(bmp, n)) + break; + return n; +} + +/* + * Parses a comma-separated list of numbers and ranges of numbers, + * with optional ':%u' strides modifying ranges, into provided bitmask. + * Some examples of input lists and their equivalent simple list: + * Input Equivalent to + * 0-3 0,1,2,3 + * 0-7:2 0,2,4,6 + * 1,3,5-7 1,3,5,6,7 + * 0-3:2,8-15:4 0,2,8,12 + */ +int bitmask_parselist(const char *buf, struct bitmask *bmp) +{ + const char *p, *q; + + bitmask_clearall(bmp); + + q = buf; + while (p = q, q = nexttoken(q, ','), p) { + unsigned int a; /* begin of range */ + unsigned int b; /* end of range */ + unsigned int s; /* stride */ + const char *c1, *c2; /* next tokens after '-' or ',' */ + char nextc; /* char after sscanf %u match */ + int sret; /* sscanf return (number of matches) */ + + sret = sscanf(p, "%u%c", &a, &nextc); + if (!scan_was_ok(sret, nextc, ",-")) + goto err; + b = a; + s = 1; + c1 = nexttoken(p, '-'); + c2 = nexttoken(p, ','); + if (c1 != NULL && (c2 == NULL || c1 < c2)) { + sret = sscanf(c1, "%u%c", &b, &nextc); + if (!scan_was_ok(sret, nextc, ",:")) + goto err; + c1 = nexttoken(c1, ':'); + if (c1 != NULL && (c2 == NULL || c1 < c2)) { + sret = sscanf(c1, "%u%c", &s, &nextc); + if (!scan_was_ok(sret, nextc, ",")) + goto err; + } + } + if (!(a <= b)) + goto err; + if (b >= bmp->size) + goto err; + while (a <= b) { + _setbit(bmp, a, 1); + a += s; + } + } + return 0; +err: + bitmask_clearall(bmp); + return -1; +} + +/* + * emit(buf, buflen, rbot, rtop, len) + * + * Helper routine for bitmask_displaylist(). Write decimal number + * or range to buf+len, suppressing output past buf+buflen, with optional + * comma-prefix. Return len of what would be written to buf, if it + * all fit. + */ + +static inline int emit(char *buf, int buflen, int rbot, int rtop, int len) +{ + if (len > 0) + len += snprintf(buf + len, max(buflen - len, 0), ","); + if (rbot == rtop) + len += snprintf(buf + len, max(buflen - len, 0), "%d", rbot); + else + len += snprintf(buf + len, max(buflen - len, 0), "%d-%d", rbot, rtop); + return len; +} + +/* + * Write decimal list representation of bmp to buf. + * + * Output format is a comma-separated list of decimal numbers and + * ranges. Consecutively set bits are shown as two hyphen-separated + * decimal numbers, the smallest and largest bit numbers set in + * the range. Output format is compatible with the format + * accepted as input by bitmap_parselist(). + * + * The return value is the number of characters which would be + * generated for the given input, excluding the trailing '\0', as + * per ISO C99. + */ + +int bitmask_displaylist(char *buf, int buflen, const struct bitmask *bmp) +{ + int len = 0; + /* current bit is 'cur', most recently seen range is [rbot, rtop] */ + unsigned int cur, rbot, rtop; + + if (buflen > 0) + *buf = 0; + rbot = cur = bitmask_first(bmp); + while (cur < bmp->size) { + rtop = cur; + cur = bitmask_next(bmp, cur+1); + if (cur >= bmp->size || cur > rtop + 1) { + len = emit(buf, buflen, rbot, rtop, len); + rbot = cur; + } + } + return len; +} -- cgit v1.2.3-70-g09d2 From 2cd005cac6d586b8ca324814a9c58ed0c08ffe40 Mon Sep 17 00:00:00 2001 From: Dominik Brodowski Date: Tue, 19 Apr 2011 20:16:05 +0200 Subject: cpupowerutils: helpers - ConfigStyle bugfixes Signed-off-by: Dominik Brodowski --- tools/power/cpupower/utils/helpers/amd.c | 8 +- tools/power/cpupower/utils/helpers/bitmask.c | 12 +-- tools/power/cpupower/utils/helpers/cpuid.c | 14 ++-- tools/power/cpupower/utils/helpers/helpers.h | 6 +- tools/power/cpupower/utils/helpers/misc.c | 3 +- tools/power/cpupower/utils/helpers/pci.c | 2 +- tools/power/cpupower/utils/helpers/sysfs.c | 112 ++++++++++++++------------ tools/power/cpupower/utils/helpers/sysfs.h | 23 +++--- tools/power/cpupower/utils/helpers/topology.c | 6 +- 9 files changed, 100 insertions(+), 86 deletions(-) (limited to 'tools/power/cpupower/utils/helpers/bitmask.c') diff --git a/tools/power/cpupower/utils/helpers/amd.c b/tools/power/cpupower/utils/helpers/amd.c index 5e44e31fc7f..87d5605bdda 100644 --- a/tools/power/cpupower/utils/helpers/amd.c +++ b/tools/power/cpupower/utils/helpers/amd.c @@ -53,8 +53,8 @@ static int get_cof(int family, union msr_pstate pstate) if (family == 0x11) t = 0x8; - return ((100 * (fid + t)) >> did); - } + return (100 * (fid + t)) >> did; +} /* Needs: * cpu -> the cpu that gets evaluated @@ -74,7 +74,7 @@ int decode_pstates(unsigned int cpu, unsigned int cpu_family, { int i, psmax, pscur; union msr_pstate pstate; - unsigned long long val; + unsigned long long val; /* Only read out frequencies from HW when CPU might be boostable to keep the code as short and clean as possible. @@ -95,7 +95,7 @@ int decode_pstates(unsigned int cpu, unsigned int cpu_family, pscur += boost_states; psmax += boost_states; - for (i=0; i<=psmax; i++) { + for (i = 0; i <= psmax; i++) { if (i >= MAX_HW_PSTATES) { fprintf(stderr, "HW pstates [%d] exceeding max [%d]\n", psmax, MAX_HW_PSTATES); diff --git a/tools/power/cpupower/utils/helpers/bitmask.c b/tools/power/cpupower/utils/helpers/bitmask.c index 60f4d69bb20..5c074c60f90 100644 --- a/tools/power/cpupower/utils/helpers/bitmask.c +++ b/tools/power/cpupower/utils/helpers/bitmask.c @@ -8,12 +8,12 @@ #define bitsperlong (8 * sizeof(unsigned long)) /* howmany(a,b) : how many elements of size b needed to hold all of a */ -#define howmany(x,y) (((x)+((y)-1))/(y)) +#define howmany(x, y) (((x)+((y)-1))/(y)) /* How many longs in mask of n bits */ #define longsperbits(n) howmany(n, bitsperlong) -#define max(a,b) ((a) > (b) ? (a) : (b)) +#define max(a, b) ((a) > (b) ? (a) : (b)) /* * Allocate and free `struct bitmask *` @@ -73,7 +73,8 @@ static void _setbit(struct bitmask *bmp, unsigned int n, unsigned int v) if (v) bmp->maskp[n/bitsperlong] |= 1UL << (n % bitsperlong); else - bmp->maskp[n/bitsperlong] &= ~(1UL << (n % bitsperlong)); + bmp->maskp[n/bitsperlong] &= + ~(1UL << (n % bitsperlong)); } } @@ -185,7 +186,7 @@ unsigned int bitmask_next(const struct bitmask *bmp, unsigned int i) * 0-3 0,1,2,3 * 0-7:2 0,2,4,6 * 1,3,5-7 1,3,5,6,7 - * 0-3:2,8-15:4 0,2,8,12 + * 0-3:2,8-15:4 0,2,8,12 */ int bitmask_parselist(const char *buf, struct bitmask *bmp) { @@ -251,7 +252,8 @@ static inline int emit(char *buf, int buflen, int rbot, int rtop, int len) if (rbot == rtop) len += snprintf(buf + len, max(buflen - len, 0), "%d", rbot); else - len += snprintf(buf + len, max(buflen - len, 0), "%d-%d", rbot, rtop); + len += snprintf(buf + len, max(buflen - len, 0), "%d-%d", + rbot, rtop); return len; } diff --git a/tools/power/cpupower/utils/helpers/cpuid.c b/tools/power/cpupower/utils/helpers/cpuid.c index 71021f3bb69..944b2c1659d 100644 --- a/tools/power/cpupower/utils/helpers/cpuid.c +++ b/tools/power/cpupower/utils/helpers/cpuid.c @@ -67,28 +67,26 @@ int get_cpu_info(unsigned int cpu, struct cpupower_cpu_info *cpu_info) continue; value[63 - 1] = '\0'; - if (!strncmp(value, "processor\t: ", 12)) { + if (!strncmp(value, "processor\t: ", 12)) sscanf(value, "processor\t: %u", &proc); - } + if (proc != cpu) continue; /* Get CPU vendor */ - if (!strncmp(value, "vendor_id", 9)) + if (!strncmp(value, "vendor_id", 9)) { for (x = 1; x < X86_VENDOR_MAX; x++) { if (strstr(value, cpu_vendor_table[x])) cpu_info->vendor = x; } /* Get CPU family, etc. */ - else if (!strncmp(value, "cpu family\t: ", 13)) { + } else if (!strncmp(value, "cpu family\t: ", 13)) { sscanf(value, "cpu family\t: %u", &cpu_info->family); - } - else if (!strncmp(value, "model\t\t: ", 9)) { + } else if (!strncmp(value, "model\t\t: ", 9)) { sscanf(value, "model\t\t: %u", &cpu_info->model); - } - else if (!strncmp(value, "stepping\t: ", 10)) { + } else if (!strncmp(value, "stepping\t: ", 10)) { sscanf(value, "stepping\t: %u", &cpu_info->stepping); diff --git a/tools/power/cpupower/utils/helpers/helpers.h b/tools/power/cpupower/utils/helpers/helpers.h index a487dadb4cf..048f065925c 100644 --- a/tools/power/cpupower/utils/helpers/helpers.h +++ b/tools/power/cpupower/utils/helpers/helpers.h @@ -20,7 +20,7 @@ #ifndef gettext_noop #define gettext_noop(String) String #endif -#define N_(String) gettext_noop (String) +#define N_(String) gettext_noop(String) /* Internationalization ****************************/ extern int run_as_root; @@ -39,11 +39,11 @@ extern int be_verbose; #define dprint(fmt, ...) { \ if (be_verbose) { \ fprintf(stderr, "%s: " fmt, \ - __FUNCTION__, ##__VA_ARGS__); \ + __func__, ##__VA_ARGS__); \ } \ } #else -static inline void dprint(const char *fmt, ...) { } +static inline void dprint(const char *fmt, ...) { } #endif extern int be_verbose; /* Global verbose (-v) stuff *********************************/ diff --git a/tools/power/cpupower/utils/helpers/misc.c b/tools/power/cpupower/utils/helpers/misc.c index c1566e93e0e..e8b3140cc6b 100644 --- a/tools/power/cpupower/utils/helpers/misc.c +++ b/tools/power/cpupower/utils/helpers/misc.c @@ -2,7 +2,8 @@ #include "helpers/helpers.h" -int cpufreq_has_boost_support(unsigned int cpu, int *support, int *active, int * states) +int cpufreq_has_boost_support(unsigned int cpu, int *support, int *active, + int *states) { struct cpupower_cpu_info cpu_info; int ret; diff --git a/tools/power/cpupower/utils/helpers/pci.c b/tools/power/cpupower/utils/helpers/pci.c index 8dcc9381371..cd2eb6fe41c 100644 --- a/tools/power/cpupower/utils/helpers/pci.c +++ b/tools/power/cpupower/utils/helpers/pci.c @@ -33,7 +33,7 @@ struct pci_dev *pci_acc_init(struct pci_access **pacc, int vendor_id, for (i = 0; dev_ids[i] != 0; i++) { filter_nb_link.device = dev_ids[i]; - for (device=(*pacc)->devices; device; device = device->next) { + for (device = (*pacc)->devices; device; device = device->next) { if (pci_filter_match(&filter_nb_link, device)) return device; } diff --git a/tools/power/cpupower/utils/helpers/sysfs.c b/tools/power/cpupower/utils/helpers/sysfs.c index 0c534e79652..55e2466674c 100644 --- a/tools/power/cpupower/utils/helpers/sysfs.c +++ b/tools/power/cpupower/utils/helpers/sysfs.c @@ -19,14 +19,14 @@ 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; } @@ -34,26 +34,26 @@ unsigned int sysfs_read_file(const char *path, char *buf, size_t buflen) buf[numread] = '\0'; close(fd); - return numread; + return (unsigned int) numread; } static unsigned int sysfs_write_file(const char *path, const char *value, size_t len) { int fd; - size_t numwrite; + ssize_t numwrite; - 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; } /* 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, { char path[SYSFS_PATH_MAX]; int fd; - size_t numread; + ssize_t numread; snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/cpuidle/state%u/%s", cpu, idlestate, fname); - 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; } @@ -87,7 +87,7 @@ unsigned int sysfs_idlestate_read_file(unsigned int cpu, unsigned int idlestate, buf[numread] = '\0'; close(fd); - return numread; + return (unsigned int) numread; } /* 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, char linebuf[MAX_LINE_LEN]; char *endp; - if ( which >= MAX_IDLESTATE_VALUE_FILES ) + if (which >= MAX_IDLESTATE_VALUE_FILES) return 0; - if ( ( len = sysfs_idlestate_read_file(cpu, idlestate, - idlestate_value_files[which], - linebuf, sizeof(linebuf))) == 0 ) - { + len = sysfs_idlestate_read_file(cpu, idlestate, + idlestate_value_files[which], + linebuf, sizeof(linebuf)); + if (len == 0) return 0; - } value = strtoull(linebuf, &endp, 0); - if ( endp == linebuf || errno == ERANGE ) + if (endp == linebuf || errno == ERANGE) return 0; return value; @@ -148,9 +147,9 @@ static const char *idlestate_string_files[MAX_IDLESTATE_STRING_FILES] = { }; -static char * sysfs_idlestate_get_one_string(unsigned int cpu, - unsigned int idlestate, - enum idlestate_string which) +static char *sysfs_idlestate_get_one_string(unsigned int cpu, + unsigned int idlestate, + enum idlestate_string which) { char linebuf[MAX_LINE_LEN]; char *result; @@ -159,12 +158,14 @@ static char * sysfs_idlestate_get_one_string(unsigned int cpu, if (which >= MAX_IDLESTATE_STRING_FILES) return NULL; - if ( ( len = sysfs_idlestate_read_file(cpu, idlestate, - idlestate_string_files[which], - linebuf, sizeof(linebuf))) == 0 ) + len = sysfs_idlestate_read_file(cpu, idlestate, + idlestate_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') @@ -173,27 +174,30 @@ static char * sysfs_idlestate_get_one_string(unsigned int cpu, return result; } -unsigned long sysfs_get_idlestate_latency(unsigned int cpu, unsigned int idlestate) +unsigned long sysfs_get_idlestate_latency(unsigned int cpu, + unsigned int idlestate) { return sysfs_idlestate_get_one_value(cpu, idlestate, IDLESTATE_LATENCY); } -unsigned long sysfs_get_idlestate_usage(unsigned int cpu, unsigned int idlestate) +unsigned long sysfs_get_idlestate_usage(unsigned int cpu, + unsigned int idlestate) { return sysfs_idlestate_get_one_value(cpu, idlestate, IDLESTATE_USAGE); } -unsigned long long sysfs_get_idlestate_time(unsigned int cpu, unsigned int idlestate) +unsigned long long sysfs_get_idlestate_time(unsigned int cpu, + unsigned int idlestate) { return sysfs_idlestate_get_one_value(cpu, idlestate, IDLESTATE_TIME); } -char * sysfs_get_idlestate_name(unsigned int cpu, unsigned int idlestate) +char *sysfs_get_idlestate_name(unsigned int cpu, unsigned int idlestate) { return sysfs_idlestate_get_one_string(cpu, idlestate, IDLESTATE_NAME); } -char * sysfs_get_idlestate_desc(unsigned int cpu, unsigned int idlestate) +char *sysfs_get_idlestate_desc(unsigned int cpu, unsigned int idlestate) { return sysfs_idlestate_get_one_string(cpu, idlestate, IDLESTATE_DESC); } @@ -211,14 +215,14 @@ int sysfs_get_idlestate_count(unsigned int cpu) snprintf(file, SYSFS_PATH_MAX, PATH_TO_CPU "cpuidle"); - if ( stat(file, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) + if (stat(file, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) return -ENODEV; snprintf(file, SYSFS_PATH_MAX, PATH_TO_CPU "cpu%u/cpuidle/state0", cpu); - if ( stat(file, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) + if (stat(file, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) return 0; - while(stat(file, &statbuf) == 0 && S_ISDIR(statbuf.st_mode)) { + while (stat(file, &statbuf) == 0 && S_ISDIR(statbuf.st_mode)) { snprintf(file, SYSFS_PATH_MAX, PATH_TO_CPU "cpu%u/cpuidle/state%d", cpu, idlestates); idlestates++; @@ -261,7 +265,7 @@ static const char *cpuidle_string_files[MAX_CPUIDLE_STRING_FILES] = { }; -static char * sysfs_cpuidle_get_one_string(enum cpuidle_string which) +static char *sysfs_cpuidle_get_one_string(enum cpuidle_string which) { char linebuf[MAX_LINE_LEN]; char *result; @@ -270,11 +274,13 @@ static char * sysfs_cpuidle_get_one_string(enum cpuidle_string which) if (which >= MAX_CPUIDLE_STRING_FILES) return NULL; - if ( ( len = sysfs_cpuidle_read_file(cpuidle_string_files[which], - linebuf, sizeof(linebuf))) == 0 ) + len = sysfs_cpuidle_read_file(cpuidle_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') @@ -283,7 +289,7 @@ static char * sysfs_cpuidle_get_one_string(enum cpuidle_string which) return result; } -char * sysfs_get_cpuidle_governor(void) +char *sysfs_get_cpuidle_governor(void) { char *tmp = sysfs_cpuidle_get_one_string(CPUIDLE_GOVERNOR_RO); if (!tmp) @@ -292,7 +298,7 @@ char * sysfs_get_cpuidle_governor(void) return tmp; } -char * sysfs_get_cpuidle_driver(void) +char *sysfs_get_cpuidle_driver(void) { return sysfs_cpuidle_get_one_string(CPUIDLE_DRIVER); } @@ -304,9 +310,9 @@ char * sysfs_get_cpuidle_driver(void) * * Returns negative value on failure */ -int sysfs_get_sched(const char* smt_mc) +int sysfs_get_sched(const char *smt_mc) { - unsigned long value; + unsigned long value; char linebuf[MAX_LINE_LEN]; char *endp; char path[SYSFS_PATH_MAX]; @@ -314,11 +320,12 @@ int sysfs_get_sched(const char* smt_mc) if (strcmp("mc", smt_mc) && strcmp("smt", smt_mc)) return -EINVAL; - snprintf(path, sizeof(path), PATH_TO_CPU "sched_%s_power_savings", smt_mc); - if (sysfs_read_file(path, linebuf, MAX_LINE_LEN) == 0 ) + snprintf(path, sizeof(path), + PATH_TO_CPU "sched_%s_power_savings", smt_mc); + if (sysfs_read_file(path, linebuf, MAX_LINE_LEN) == 0) return -1; value = strtoul(linebuf, &endp, 0); - if ( endp == linebuf || errno == ERANGE ) + if (endp == linebuf || errno == ERANGE) return -1; return value; } @@ -329,7 +336,7 @@ int sysfs_get_sched(const char* smt_mc) * * Returns negative value on failure */ -int sysfs_set_sched(const char* smt_mc, int val) +int sysfs_set_sched(const char *smt_mc, int val) { char linebuf[MAX_LINE_LEN]; char path[SYSFS_PATH_MAX]; @@ -338,13 +345,14 @@ int sysfs_set_sched(const char* smt_mc, int val) if (strcmp("mc", smt_mc) && strcmp("smt", smt_mc)) return -EINVAL; - snprintf(path, sizeof(path), PATH_TO_CPU "sched_%s_power_savings", smt_mc); + snprintf(path, sizeof(path), + PATH_TO_CPU "sched_%s_power_savings", smt_mc); sprintf(linebuf, "%d", val); - if ( stat(path, &statbuf) != 0 ) + if (stat(path, &statbuf) != 0) return -ENODEV; - if (sysfs_write_file(path, linebuf, MAX_LINE_LEN) == 0 ) + if (sysfs_write_file(path, linebuf, MAX_LINE_LEN) == 0) return -1; return 0; } diff --git a/tools/power/cpupower/utils/helpers/sysfs.h b/tools/power/cpupower/utils/helpers/sysfs.h index 5d02d2fc70e..f9373e09063 100644 --- a/tools/power/cpupower/utils/helpers/sysfs.h +++ b/tools/power/cpupower/utils/helpers/sysfs.h @@ -7,17 +7,22 @@ extern unsigned int sysfs_read_file(const char *path, char *buf, size_t buflen); -extern unsigned long sysfs_get_idlestate_latency(unsigned int cpu, unsigned int idlestate); -extern unsigned long sysfs_get_idlestate_usage(unsigned int cpu, unsigned int idlestate); -extern unsigned long long sysfs_get_idlestate_time(unsigned int cpu, unsigned int idlestate); -extern char * sysfs_get_idlestate_name(unsigned int cpu, unsigned int idlestate); -extern char * sysfs_get_idlestate_desc(unsigned int cpu, unsigned int idlestate); +extern unsigned long sysfs_get_idlestate_latency(unsigned int cpu, + unsigned int idlestate); +extern unsigned long sysfs_get_idlestate_usage(unsigned int cpu, + unsigned int idlestate); +extern unsigned long long sysfs_get_idlestate_time(unsigned int cpu, + unsigned int idlestate); +extern char *sysfs_get_idlestate_name(unsigned int cpu, + unsigned int idlestate); +extern char *sysfs_get_idlestate_desc(unsigned int cpu, + unsigned int idlestate); extern int sysfs_get_idlestate_count(unsigned int cpu); -extern char * sysfs_get_cpuidle_governor(void); -extern char * sysfs_get_cpuidle_driver(void); +extern char *sysfs_get_cpuidle_governor(void); +extern char *sysfs_get_cpuidle_driver(void); -extern int sysfs_get_sched(const char* smt_mc); -extern int sysfs_set_sched(const char* smt_mc, int val); +extern int sysfs_get_sched(const char *smt_mc); +extern int sysfs_set_sched(const char *smt_mc, int val); #endif /* __CPUPOWER_HELPERS_SYSFS_H__ */ diff --git a/tools/power/cpupower/utils/helpers/topology.c b/tools/power/cpupower/utils/helpers/topology.c index 5ad842b956b..385ee5c7570 100644 --- a/tools/power/cpupower/utils/helpers/topology.c +++ b/tools/power/cpupower/utils/helpers/topology.c @@ -22,17 +22,17 @@ /* returns -1 on failure, 0 on success */ int sysfs_topology_read_file(unsigned int cpu, const char *fname) { - unsigned long value; + unsigned long value; char linebuf[MAX_LINE_LEN]; char *endp; char path[SYSFS_PATH_MAX]; snprintf(path, sizeof(path), PATH_TO_CPU "cpu%u/topology/%s", cpu, fname); - if (sysfs_read_file(path, linebuf, MAX_LINE_LEN) == 0 ) + if (sysfs_read_file(path, linebuf, MAX_LINE_LEN) == 0) return -1; value = strtoul(linebuf, &endp, 0); - if ( endp == linebuf || errno == ERANGE ) + if (endp == linebuf || errno == ERANGE) return -1; return value; } -- cgit v1.2.3-70-g09d2