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>
This commit is contained in:
parent
95aebc44e7
commit
57a42a34d1
|
@ -1174,6 +1174,19 @@ void free_all_buffers(void)
|
||||||
outp = NULL;
|
outp = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* 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.
|
* Parse a file containing a single int.
|
||||||
*/
|
*/
|
||||||
|
@ -1187,11 +1200,7 @@ int parse_int_file(const char *fmt, ...)
|
||||||
va_start(args, fmt);
|
va_start(args, fmt);
|
||||||
vsnprintf(path, sizeof(path), fmt, args);
|
vsnprintf(path, sizeof(path), fmt, args);
|
||||||
va_end(args);
|
va_end(args);
|
||||||
filep = fopen(path, "r");
|
filep = fopen_or_die(path, "r");
|
||||||
if (!filep) {
|
|
||||||
perror(path);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
if (fscanf(filep, "%d", &value) != 1) {
|
if (fscanf(filep, "%d", &value) != 1) {
|
||||||
perror(path);
|
perror(path);
|
||||||
exit(1);
|
exit(1);
|
||||||
|
@ -1237,11 +1246,7 @@ int get_num_ht_siblings(int cpu)
|
||||||
char character;
|
char character;
|
||||||
|
|
||||||
sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
|
sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
|
||||||
filep = fopen(path, "r");
|
filep = fopen_or_die(path, "r");
|
||||||
if (filep == NULL) {
|
|
||||||
perror(path);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
/*
|
/*
|
||||||
* file format:
|
* file format:
|
||||||
* if a pair of number with a character between: 2 siblings (eg. 1-2, or 1,4)
|
* 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 cpu_num;
|
||||||
int retval;
|
int retval;
|
||||||
|
|
||||||
fp = fopen(proc_stat, "r");
|
fp = fopen_or_die(proc_stat, "r");
|
||||||
if (fp == NULL) {
|
|
||||||
perror(proc_stat);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
|
retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
|
||||||
if (retval != 0) {
|
if (retval != 0) {
|
||||||
|
|
Loading…
Reference in New Issue