aboutsummaryrefslogtreecommitdiff
path: root/idlestat.c
diff options
context:
space:
mode:
authorTuukka Tikkanen <tuukka.tikkanen@linaro.org>2014-12-03 12:26:17 +0200
committerTuukka Tikkanen <tuukka.tikkanen@linaro.org>2014-12-05 09:52:39 +0200
commit27f2342b422a3284a07cc541d780192dfab483a1 (patch)
tree00fa28915060b30b6b47aea8463b34f9bbf0f801 /idlestat.c
parent4fea09fd20ec00a57bca4e686b30c17f506c2312 (diff)
idlestat: Make ptrerror return a non-NULL pointer
Some functions such as inter() and intersection() use NULL return value to indicate both errors (e.g. out of memory) and a result (empty intersection). Make ptrerror() return a unique non-NULL pointer value so that the error and NULL result can be told apart. Previously the parameter to ptrerror() was unconditionally interpreted as a pointer to string to be passed to perror(). Now the function checks the value and calls perror only if the parameter value is not NULL. A new function, is_err(), has been added to check if any pointer value is equal to ptrerror() return value. error(), ptrerror() and is_err() have been moved to utils.c. The functions that used ptrerror() before this patch have been modified to consistently return ptrerror() instead of NULL in cases of errors (affects the return statements where no call to perror() is desired). Likewise the callers now use is_err() instead of checking for NULL pointer. Signed-off-by: Tuukka Tikkanen <tuukka.tikkanen@linaro.org>
Diffstat (limited to 'idlestat.c')
-rw-r--r--idlestat.c33
1 files changed, 13 insertions, 20 deletions
diff --git a/idlestat.c b/idlestat.c
index 7e3ea38..016d176 100644
--- a/idlestat.c
+++ b/idlestat.c
@@ -79,18 +79,6 @@ static inline int bad_filename(const char *filename)
return 0;
}
-static inline int error(const char *str)
-{
- perror(str);
- return -1;
-}
-
-static inline void *ptrerror(const char *str)
-{
- perror(str);
- return NULL;
-}
-
#define TRACE_TS_FORMAT "%*[^:]:%lf"
static int get_trace_ts(double *ts)
@@ -383,13 +371,15 @@ int cpuidle_get_target_residency(int cpu, int state)
* structs to maintain statistics of C-state transitions
* @nrcpus: number of CPUs
*
- * Return: per-CPU array of structs (success) or NULL (error)
+ * Return: per-CPU array of structs (success) or ptrerror() (error)
*/
static struct cpuidle_cstates *build_cstate_info(int nrcpus)
{
int cpu;
struct cpuidle_cstates *cstates;
+ assert(nrcpus > 0);
+
cstates = calloc(nrcpus, sizeof(*cstates));
if (!cstates)
return ptrerror("calloc in build_cstate_info");
@@ -536,13 +526,15 @@ static int alloc_pstate(struct cpufreq_pstates *pstates, unsigned int freq)
* @f: the file handle of the idlestat trace file
* @nrcpus: number of CPUs
*
- * Return: per-CPU array of structs (success) or NULL (error)
+ * Return: per-CPU array of structs (success) or ptrerror() (error)
*/
static struct cpuidle_cstates *load_and_build_cstate_info(FILE* f, int nrcpus)
{
int cpu;
struct cpuidle_cstates *cstates;
+ assert(nrcpus > 0);
+
cstates = calloc(nrcpus, sizeof(*cstates));
if (!cstates)
return ptrerror("calloc in load_and_build_cstate_info");
@@ -564,7 +556,7 @@ static struct cpuidle_cstates *load_and_build_cstate_info(FILE* f, int nrcpus)
"Expected: cpuid %d:\n"
"Read: %s",
__FUNCTION__, cpu, buffer);
- return NULL;
+ return ptrerror(NULL);
}
for (i = 0; i < MAXCSTATE; i++) {
@@ -931,6 +923,7 @@ void output_cstate_info(FILE *f, int nrcpus) {
int i, j;
cstates = build_cstate_info(nrcpus);
+ assert(!is_err(cstates));
for (i=0; i < nrcpus; i++) {
fprintf(f, "cpuid %d:\n", i);
@@ -992,7 +985,7 @@ struct cpuidle_datas *idlestat_load(const char *filename)
if (!f) {
fprintf(stderr, "%s: failed to open '%s': %m\n", __func__,
filename);
- return NULL;
+ return ptrerror(NULL);
}
/* version line */
@@ -1017,7 +1010,7 @@ struct cpuidle_datas *idlestat_load(const char *filename)
fprintf(stderr, "%s: unrecognized import format in '%s'\n",
__func__, filename);
fclose(f);
- return NULL;
+ return ptrerror(NULL);
}
if (!nrcpus) {
@@ -1041,10 +1034,10 @@ struct cpuidle_datas *idlestat_load(const char *filename)
datas->cstates = load_and_build_cstate_info(f, nrcpus);
else
datas->cstates = build_cstate_info(nrcpus);
- if (!datas->cstates) {
+ if (is_err(datas->cstates)) {
free(datas);
fclose(f);
- return NULL;
+ return ptrerror(NULL);
}
datas->pstates = build_pstate_info(nrcpus);
@@ -1660,7 +1653,7 @@ int main(int argc, char *argv[], char *const envp[])
/* Load the idle states information */
datas = idlestat_load(options.filename);
- if (!datas)
+ if (is_err(datas))
return 1;
/* Compute cluster idle intersection between cpus belonging to