diff options
author | Josh Triplett <josh@joshtriplett.org> | 2013-08-20 20:20:17 -0400 |
---|---|---|
committer | Len Brown <len.brown@intel.com> | 2014-01-18 22:34:09 -0500 |
commit | 57a42a34d126f2fe5d1f2f120c5f7a31ec65cd31 (patch) | |
tree | d646c40a4e06e7737b12bcddbe4ac9796f748bbf /tools/power | |
parent | 95aebc44e73b05d4e95774b983a63909de638808 (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>
Diffstat (limited to 'tools/power')
-rw-r--r-- | tools/power/x86/turbostat/turbostat.c | 31 |
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) | |||
1175 | } | 1175 | } |
1176 | 1176 | ||
1177 | /* | 1177 | /* |
1178 | * Open a file, and exit on failure | ||
1179 | */ | ||
1180 | FILE *fopen_or_die(const char *path, const char *mode) | ||
1181 | { | ||
1182 | FILE *filep = fopen(path, "r"); | ||
1183 | if (!filep) { | ||
1184 | perror(path); | ||
1185 | exit(1); | ||
1186 | } | ||
1187 | return filep; | ||
1188 | } | ||
1189 | |||
1190 | /* | ||
1178 | * Parse a file containing a single int. | 1191 | * Parse a file containing a single int. |
1179 | */ | 1192 | */ |
1180 | int parse_int_file(const char *fmt, ...) | 1193 | int parse_int_file(const char *fmt, ...) |
@@ -1187,11 +1200,7 @@ int parse_int_file(const char *fmt, ...) | |||
1187 | va_start(args, fmt); | 1200 | va_start(args, fmt); |
1188 | vsnprintf(path, sizeof(path), fmt, args); | 1201 | vsnprintf(path, sizeof(path), fmt, args); |
1189 | va_end(args); | 1202 | va_end(args); |
1190 | filep = fopen(path, "r"); | 1203 | filep = fopen_or_die(path, "r"); |
1191 | if (!filep) { | ||
1192 | perror(path); | ||
1193 | exit(1); | ||
1194 | } | ||
1195 | if (fscanf(filep, "%d", &value) != 1) { | 1204 | if (fscanf(filep, "%d", &value) != 1) { |
1196 | perror(path); | 1205 | perror(path); |
1197 | exit(1); | 1206 | exit(1); |
@@ -1237,11 +1246,7 @@ int get_num_ht_siblings(int cpu) | |||
1237 | char character; | 1246 | char character; |
1238 | 1247 | ||
1239 | sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu); | 1248 | sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu); |
1240 | filep = fopen(path, "r"); | 1249 | filep = fopen_or_die(path, "r"); |
1241 | if (filep == NULL) { | ||
1242 | perror(path); | ||
1243 | exit(1); | ||
1244 | } | ||
1245 | /* | 1250 | /* |
1246 | * file format: | 1251 | * file format: |
1247 | * if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4) | 1252 | * 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)) | |||
1311 | int cpu_num; | 1316 | int cpu_num; |
1312 | int retval; | 1317 | int retval; |
1313 | 1318 | ||
1314 | fp = fopen(proc_stat, "r"); | 1319 | fp = fopen_or_die(proc_stat, "r"); |
1315 | if (fp == NULL) { | ||
1316 | perror(proc_stat); | ||
1317 | exit(1); | ||
1318 | } | ||
1319 | 1320 | ||
1320 | retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n"); | 1321 | retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n"); |
1321 | if (retval != 0) { | 1322 | if (retval != 0) { |