summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJosh Triplett <josh@joshtriplett.org>2013-08-20 17:20:17 -0700
committerLen Brown <len.brown@intel.com>2014-01-18 22:34:09 -0500
commit57a42a34d126f2fe5d1f2f120c5f7a31ec65cd31 (patch)
treed646c40a4e06e7737b12bcddbe4ac9796f748bbf
parent95aebc44e73b05d4e95774b983a63909de638808 (diff)
turbostat: Factor out common function to open file and exit on failure
Several different functions in turbostat contain the same pattern of opening a file and exiting on failure. Factor out a common fopen_or_die function for that. Signed-off-by: Josh Triplett <josh@joshtriplett.org> Signed-off-by: Len Brown <len.brown@intel.com>
-rw-r--r--tools/power/x86/turbostat/turbostat.c31
1 files changed, 16 insertions, 15 deletions
diff --git a/tools/power/x86/turbostat/turbostat.c b/tools/power/x86/turbostat/turbostat.c
index f7b5d6f83d28..de634c8228b4 100644
--- a/tools/power/x86/turbostat/turbostat.c
+++ b/tools/power/x86/turbostat/turbostat.c
@@ -1175,6 +1175,19 @@ void free_all_buffers(void)
}
/*
+ * Open a file, and exit on failure
+ */
+FILE *fopen_or_die(const char *path, const char *mode)
+{
+ FILE *filep = fopen(path, "r");
+ if (!filep) {
+ perror(path);
+ exit(1);
+ }
+ return filep;
+}
+
+/*
* Parse a file containing a single int.
*/
int parse_int_file(const char *fmt, ...)
@@ -1187,11 +1200,7 @@ int parse_int_file(const char *fmt, ...)
va_start(args, fmt);
vsnprintf(path, sizeof(path), fmt, args);
va_end(args);
- filep = fopen(path, "r");
- if (!filep) {
- perror(path);
- exit(1);
- }
+ filep = fopen_or_die(path, "r");
if (fscanf(filep, "%d", &value) != 1) {
perror(path);
exit(1);
@@ -1237,11 +1246,7 @@ int get_num_ht_siblings(int cpu)
char character;
sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
- filep = fopen(path, "r");
- if (filep == NULL) {
- perror(path);
- exit(1);
- }
+ filep = fopen_or_die(path, "r");
/*
* file format:
* if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4)
@@ -1311,11 +1316,7 @@ int for_all_proc_cpus(int (func)(int))
int cpu_num;
int retval;
- fp = fopen(proc_stat, "r");
- if (fp == NULL) {
- perror(proc_stat);
- exit(1);
- }
+ fp = fopen_or_die(proc_stat, "r");
retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
if (retval != 0) {