diff options
author | Josh Triplett <josh@joshtriplett.org> | 2013-08-20 20:20:18 -0400 |
---|---|---|
committer | Len Brown <len.brown@intel.com> | 2014-01-18 22:34:10 -0500 |
commit | b2c95d90a799c9885661d431034b7ca733cc2660 (patch) | |
tree | 4ff18cce19b0175de18b0dc21649fe994127e927 /tools | |
parent | 57a42a34d126f2fe5d1f2f120c5f7a31ec65cd31 (diff) |
turbostat: Clean up error handling; disambiguate error messages; use err and errx
Most of turbostat's error handling consists of printing an error (often
including an errno) and exiting. Since perror doesn't support a format
string, those error messages are often ambiguous, such as just showing a
file path, which doesn't uniquely identify which call failed.
turbostat already uses _GNU_SOURCE, so switch to the err and errx
functions from err.h, which take a format string.
Signed-off-by: Josh Triplett <josh@joshtriplett.org>
Signed-off-by: Len Brown <len.brown@intel.com>
Diffstat (limited to 'tools')
-rw-r--r-- | tools/power/x86/turbostat/turbostat.c | 107 |
1 files changed, 38 insertions, 69 deletions
diff --git a/tools/power/x86/turbostat/turbostat.c b/tools/power/x86/turbostat/turbostat.c index de634c8228b4..c8cce5dcc823 100644 --- a/tools/power/x86/turbostat/turbostat.c +++ b/tools/power/x86/turbostat/turbostat.c | |||
@@ -23,6 +23,7 @@ | |||
23 | #include MSRHEADER | 23 | #include MSRHEADER |
24 | #include <stdarg.h> | 24 | #include <stdarg.h> |
25 | #include <stdio.h> | 25 | #include <stdio.h> |
26 | #include <err.h> | ||
26 | #include <unistd.h> | 27 | #include <unistd.h> |
27 | #include <sys/types.h> | 28 | #include <sys/types.h> |
28 | #include <sys/wait.h> | 29 | #include <sys/wait.h> |
@@ -624,12 +625,10 @@ delta_thread(struct thread_data *new, struct thread_data *old, | |||
624 | old->tsc = new->tsc - old->tsc; | 625 | old->tsc = new->tsc - old->tsc; |
625 | 626 | ||
626 | /* check for TSC < 1 Mcycles over interval */ | 627 | /* check for TSC < 1 Mcycles over interval */ |
627 | if (old->tsc < (1000 * 1000)) { | 628 | if (old->tsc < (1000 * 1000)) |
628 | fprintf(stderr, "Insanely slow TSC rate, TSC stops in idle?\n"); | 629 | errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n" |
629 | fprintf(stderr, "You can disable all c-states by booting with \"idle=poll\"\n"); | 630 | "You can disable all c-states by booting with \"idle=poll\"\n" |
630 | fprintf(stderr, "or just the deep ones with \"processor.max_cstate=1\"\n"); | 631 | "or just the deep ones with \"processor.max_cstate=1\""); |
631 | exit(-3); | ||
632 | } | ||
633 | 632 | ||
634 | old->c1 = new->c1 - old->c1; | 633 | old->c1 = new->c1 - old->c1; |
635 | 634 | ||
@@ -1180,10 +1179,8 @@ void free_all_buffers(void) | |||
1180 | FILE *fopen_or_die(const char *path, const char *mode) | 1179 | FILE *fopen_or_die(const char *path, const char *mode) |
1181 | { | 1180 | { |
1182 | FILE *filep = fopen(path, "r"); | 1181 | FILE *filep = fopen(path, "r"); |
1183 | if (!filep) { | 1182 | if (!filep) |
1184 | perror(path); | 1183 | err(1, "%s: open failed", path); |
1185 | exit(1); | ||
1186 | } | ||
1187 | return filep; | 1184 | return filep; |
1188 | } | 1185 | } |
1189 | 1186 | ||
@@ -1201,10 +1198,8 @@ int parse_int_file(const char *fmt, ...) | |||
1201 | vsnprintf(path, sizeof(path), fmt, args); | 1198 | vsnprintf(path, sizeof(path), fmt, args); |
1202 | va_end(args); | 1199 | va_end(args); |
1203 | filep = fopen_or_die(path, "r"); | 1200 | filep = fopen_or_die(path, "r"); |
1204 | if (fscanf(filep, "%d", &value) != 1) { | 1201 | if (fscanf(filep, "%d", &value) != 1) |
1205 | perror(path); | 1202 | err(1, "%s: failed to parse number from file", path); |
1206 | exit(1); | ||
1207 | } | ||
1208 | fclose(filep); | 1203 | fclose(filep); |
1209 | return value; | 1204 | return value; |
1210 | } | 1205 | } |
@@ -1319,10 +1314,8 @@ int for_all_proc_cpus(int (func)(int)) | |||
1319 | fp = fopen_or_die(proc_stat, "r"); | 1314 | fp = fopen_or_die(proc_stat, "r"); |
1320 | 1315 | ||
1321 | retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n"); | 1316 | retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n"); |
1322 | if (retval != 0) { | 1317 | if (retval != 0) |
1323 | perror("/proc/stat format"); | 1318 | err(1, "%s: failed to parse format", proc_stat); |
1324 | exit(1); | ||
1325 | } | ||
1326 | 1319 | ||
1327 | while (1) { | 1320 | while (1) { |
1328 | retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num); | 1321 | retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num); |
@@ -1426,19 +1419,15 @@ void check_dev_msr() | |||
1426 | { | 1419 | { |
1427 | struct stat sb; | 1420 | struct stat sb; |
1428 | 1421 | ||
1429 | if (stat("/dev/cpu/0/msr", &sb)) { | 1422 | if (stat("/dev/cpu/0/msr", &sb)) |
1430 | fprintf(stderr, "no /dev/cpu/0/msr\n"); | 1423 | err(-5, "no /dev/cpu/0/msr\n" |
1431 | fprintf(stderr, "Try \"# modprobe msr\"\n"); | 1424 | "Try \"# modprobe msr\""); |
1432 | exit(-5); | ||
1433 | } | ||
1434 | } | 1425 | } |
1435 | 1426 | ||
1436 | void check_super_user() | 1427 | void check_super_user() |
1437 | { | 1428 | { |
1438 | if (getuid() != 0) { | 1429 | if (getuid() != 0) |
1439 | fprintf(stderr, "must be root\n"); | 1430 | errx(-6, "must be root"); |
1440 | exit(-6); | ||
1441 | } | ||
1442 | } | 1431 | } |
1443 | 1432 | ||
1444 | int has_nehalem_turbo_ratio_limit(unsigned int family, unsigned int model) | 1433 | int has_nehalem_turbo_ratio_limit(unsigned int family, unsigned int model) |
@@ -1979,10 +1968,8 @@ void check_cpuid() | |||
1979 | fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n", | 1968 | fprintf(stderr, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n", |
1980 | max_level, family, model, stepping, family, model, stepping); | 1969 | max_level, family, model, stepping, family, model, stepping); |
1981 | 1970 | ||
1982 | if (!(edx & (1 << 5))) { | 1971 | if (!(edx & (1 << 5))) |
1983 | fprintf(stderr, "CPUID: no MSR\n"); | 1972 | errx(1, "CPUID: no MSR"); |
1984 | exit(1); | ||
1985 | } | ||
1986 | 1973 | ||
1987 | /* | 1974 | /* |
1988 | * check max extended function levels of CPUID. | 1975 | * check max extended function levels of CPUID. |
@@ -1992,10 +1979,8 @@ void check_cpuid() | |||
1992 | ebx = ecx = edx = 0; | 1979 | ebx = ecx = edx = 0; |
1993 | __get_cpuid(0x80000000, &max_level, &ebx, &ecx, &edx); | 1980 | __get_cpuid(0x80000000, &max_level, &ebx, &ecx, &edx); |
1994 | 1981 | ||
1995 | if (max_level < 0x80000007) { | 1982 | if (max_level < 0x80000007) |
1996 | fprintf(stderr, "CPUID: no invariant TSC (max_level 0x%x)\n", max_level); | 1983 | errx(1, "CPUID: no invariant TSC (max_level 0x%x)", max_level); |
1997 | exit(1); | ||
1998 | } | ||
1999 | 1984 | ||
2000 | /* | 1985 | /* |
2001 | * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8 | 1986 | * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8 |
@@ -2004,10 +1989,8 @@ void check_cpuid() | |||
2004 | __get_cpuid(0x80000007, &eax, &ebx, &ecx, &edx); | 1989 | __get_cpuid(0x80000007, &eax, &ebx, &ecx, &edx); |
2005 | has_invariant_tsc = edx & (1 << 8); | 1990 | has_invariant_tsc = edx & (1 << 8); |
2006 | 1991 | ||
2007 | if (!has_invariant_tsc) { | 1992 | if (!has_invariant_tsc) |
2008 | fprintf(stderr, "No invariant TSC\n"); | 1993 | errx(1, "No invariant TSC"); |
2009 | exit(1); | ||
2010 | } | ||
2011 | 1994 | ||
2012 | /* | 1995 | /* |
2013 | * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0 | 1996 | * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0 |
@@ -2028,7 +2011,7 @@ void check_cpuid() | |||
2028 | has_epb ? ", EPB": ""); | 2011 | has_epb ? ", EPB": ""); |
2029 | 2012 | ||
2030 | if (!has_aperf) | 2013 | if (!has_aperf) |
2031 | exit(-1); | 2014 | errx(-1, "No APERF"); |
2032 | 2015 | ||
2033 | do_nehalem_platform_info = genuine_intel && has_invariant_tsc; | 2016 | do_nehalem_platform_info = genuine_intel && has_invariant_tsc; |
2034 | do_nhm_cstates = genuine_intel; /* all Intel w/ non-stop TSC have NHM counters */ | 2017 | do_nhm_cstates = genuine_intel; /* all Intel w/ non-stop TSC have NHM counters */ |
@@ -2048,9 +2031,8 @@ void check_cpuid() | |||
2048 | 2031 | ||
2049 | void usage() | 2032 | void usage() |
2050 | { | 2033 | { |
2051 | fprintf(stderr, "%s: [-v][-R][-T][-p|-P|-S][-c MSR# | -s]][-C MSR#][-m MSR#][-M MSR#][-i interval_sec | command ...]\n", | 2034 | errx(1, "%s: [-v][-R][-T][-p|-P|-S][-c MSR# | -s]][-C MSR#][-m MSR#][-M MSR#][-i interval_sec | command ...]\n", |
2052 | progname); | 2035 | progname); |
2053 | exit(1); | ||
2054 | } | 2036 | } |
2055 | 2037 | ||
2056 | 2038 | ||
@@ -2093,19 +2075,15 @@ void topology_probe() | |||
2093 | fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num); | 2075 | fprintf(stderr, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num); |
2094 | 2076 | ||
2095 | cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology)); | 2077 | cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology)); |
2096 | if (cpus == NULL) { | 2078 | if (cpus == NULL) |
2097 | perror("calloc cpus"); | 2079 | err(1, "calloc cpus"); |
2098 | exit(1); | ||
2099 | } | ||
2100 | 2080 | ||
2101 | /* | 2081 | /* |
2102 | * Allocate and initialize cpu_present_set | 2082 | * Allocate and initialize cpu_present_set |
2103 | */ | 2083 | */ |
2104 | cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1)); | 2084 | cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1)); |
2105 | if (cpu_present_set == NULL) { | 2085 | if (cpu_present_set == NULL) |
2106 | perror("CPU_ALLOC"); | 2086 | err(3, "CPU_ALLOC"); |
2107 | exit(3); | ||
2108 | } | ||
2109 | cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); | 2087 | cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); |
2110 | CPU_ZERO_S(cpu_present_setsize, cpu_present_set); | 2088 | CPU_ZERO_S(cpu_present_setsize, cpu_present_set); |
2111 | for_all_proc_cpus(mark_cpu_present); | 2089 | for_all_proc_cpus(mark_cpu_present); |
@@ -2114,10 +2092,8 @@ void topology_probe() | |||
2114 | * Allocate and initialize cpu_affinity_set | 2092 | * Allocate and initialize cpu_affinity_set |
2115 | */ | 2093 | */ |
2116 | cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1)); | 2094 | cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1)); |
2117 | if (cpu_affinity_set == NULL) { | 2095 | if (cpu_affinity_set == NULL) |
2118 | perror("CPU_ALLOC"); | 2096 | err(3, "CPU_ALLOC"); |
2119 | exit(3); | ||
2120 | } | ||
2121 | cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); | 2097 | cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1)); |
2122 | CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); | 2098 | CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set); |
2123 | 2099 | ||
@@ -2201,8 +2177,7 @@ allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data | |||
2201 | 2177 | ||
2202 | return; | 2178 | return; |
2203 | error: | 2179 | error: |
2204 | perror("calloc counters"); | 2180 | err(1, "calloc counters"); |
2205 | exit(1); | ||
2206 | } | 2181 | } |
2207 | /* | 2182 | /* |
2208 | * init_counter() | 2183 | * init_counter() |
@@ -2259,10 +2234,8 @@ void allocate_output_buffer() | |||
2259 | { | 2234 | { |
2260 | output_buffer = calloc(1, (1 + topo.num_cpus) * 256); | 2235 | output_buffer = calloc(1, (1 + topo.num_cpus) * 256); |
2261 | outp = output_buffer; | 2236 | outp = output_buffer; |
2262 | if (outp == NULL) { | 2237 | if (outp == NULL) |
2263 | perror("calloc"); | 2238 | err(-1, "calloc output buffer"); |
2264 | exit(-1); | ||
2265 | } | ||
2266 | } | 2239 | } |
2267 | 2240 | ||
2268 | void setup_all_buffers(void) | 2241 | void setup_all_buffers(void) |
@@ -2316,17 +2289,13 @@ int fork_it(char **argv) | |||
2316 | } else { | 2289 | } else { |
2317 | 2290 | ||
2318 | /* parent */ | 2291 | /* parent */ |
2319 | if (child_pid == -1) { | 2292 | if (child_pid == -1) |
2320 | perror("fork"); | 2293 | err(1, "fork"); |
2321 | exit(1); | ||
2322 | } | ||
2323 | 2294 | ||
2324 | signal(SIGINT, SIG_IGN); | 2295 | signal(SIGINT, SIG_IGN); |
2325 | signal(SIGQUIT, SIG_IGN); | 2296 | signal(SIGQUIT, SIG_IGN); |
2326 | if (waitpid(child_pid, &status, 0) == -1) { | 2297 | if (waitpid(child_pid, &status, 0) == -1) |
2327 | perror("wait"); | 2298 | err(status, "waitpid"); |
2328 | exit(status); | ||
2329 | } | ||
2330 | } | 2299 | } |
2331 | /* | 2300 | /* |
2332 | * n.b. fork_it() does not check for errors from for_all_cpus() | 2301 | * n.b. fork_it() does not check for errors from for_all_cpus() |