diff options
author | Josh Triplett <josh@joshtriplett.org> | 2013-08-20 20:20:16 -0400 |
---|---|---|
committer | Len Brown <len.brown@intel.com> | 2014-01-18 22:34:09 -0500 |
commit | 95aebc44e73b05d4e95774b983a63909de638808 (patch) | |
tree | 90b0ae555877d88f166d651fb938c58b24d301ed | |
parent | 74823419761c11830ea1819365f82cf3d48795cb (diff) |
turbostat: Add a helper to parse a single int out of a file
Many different chunks of code in turbostat open a file, parse a single
int out of it, and close it. Factor that out into a common function.
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.c | 81 |
1 files changed, 24 insertions, 57 deletions
diff --git a/tools/power/x86/turbostat/turbostat.c b/tools/power/x86/turbostat/turbostat.c index 4f7b88b035c3..f7b5d6f83d28 100644 --- a/tools/power/x86/turbostat/turbostat.c +++ b/tools/power/x86/turbostat/turbostat.c | |||
@@ -21,6 +21,7 @@ | |||
21 | 21 | ||
22 | #define _GNU_SOURCE | 22 | #define _GNU_SOURCE |
23 | #include MSRHEADER | 23 | #include MSRHEADER |
24 | #include <stdarg.h> | ||
24 | #include <stdio.h> | 25 | #include <stdio.h> |
25 | #include <unistd.h> | 26 | #include <unistd.h> |
26 | #include <sys/types.h> | 27 | #include <sys/types.h> |
@@ -1174,27 +1175,38 @@ void free_all_buffers(void) | |||
1174 | } | 1175 | } |
1175 | 1176 | ||
1176 | /* | 1177 | /* |
1177 | * cpu_is_first_sibling_in_core(cpu) | 1178 | * Parse a file containing a single int. |
1178 | * return 1 if given CPU is 1st HT sibling in the core | ||
1179 | */ | 1179 | */ |
1180 | int cpu_is_first_sibling_in_core(int cpu) | 1180 | int parse_int_file(const char *fmt, ...) |
1181 | { | 1181 | { |
1182 | char path[64]; | 1182 | va_list args; |
1183 | char path[PATH_MAX]; | ||
1183 | FILE *filep; | 1184 | FILE *filep; |
1184 | int first_cpu; | 1185 | int value; |
1185 | 1186 | ||
1186 | sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu); | 1187 | va_start(args, fmt); |
1188 | vsnprintf(path, sizeof(path), fmt, args); | ||
1189 | va_end(args); | ||
1187 | filep = fopen(path, "r"); | 1190 | filep = fopen(path, "r"); |
1188 | if (filep == NULL) { | 1191 | if (!filep) { |
1189 | perror(path); | 1192 | perror(path); |
1190 | exit(1); | 1193 | exit(1); |
1191 | } | 1194 | } |
1192 | if (fscanf(filep, "%d", &first_cpu) != 1) { | 1195 | if (fscanf(filep, "%d", &value) != 1) { |
1193 | perror(path); | 1196 | perror(path); |
1194 | exit(1); | 1197 | exit(1); |
1195 | } | 1198 | } |
1196 | fclose(filep); | 1199 | fclose(filep); |
1197 | return (cpu == first_cpu); | 1200 | return value; |
1201 | } | ||
1202 | |||
1203 | /* | ||
1204 | * cpu_is_first_sibling_in_core(cpu) | ||
1205 | * return 1 if given CPU is 1st HT sibling in the core | ||
1206 | */ | ||
1207 | int cpu_is_first_sibling_in_core(int cpu) | ||
1208 | { | ||
1209 | return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu); | ||
1198 | } | 1210 | } |
1199 | 1211 | ||
1200 | /* | 1212 | /* |
@@ -1203,62 +1215,17 @@ int cpu_is_first_sibling_in_core(int cpu) | |||
1203 | */ | 1215 | */ |
1204 | int cpu_is_first_core_in_package(int cpu) | 1216 | int cpu_is_first_core_in_package(int cpu) |
1205 | { | 1217 | { |
1206 | char path[64]; | 1218 | return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu); |
1207 | FILE *filep; | ||
1208 | int first_cpu; | ||
1209 | |||
1210 | sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu); | ||
1211 | filep = fopen(path, "r"); | ||
1212 | if (filep == NULL) { | ||
1213 | perror(path); | ||
1214 | exit(1); | ||
1215 | } | ||
1216 | if (fscanf(filep, "%d", &first_cpu) != 1) { | ||
1217 | perror(path); | ||
1218 | exit(1); | ||
1219 | } | ||
1220 | fclose(filep); | ||
1221 | return (cpu == first_cpu); | ||
1222 | } | 1219 | } |
1223 | 1220 | ||
1224 | int get_physical_package_id(int cpu) | 1221 | int get_physical_package_id(int cpu) |
1225 | { | 1222 | { |
1226 | char path[80]; | 1223 | return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu); |
1227 | FILE *filep; | ||
1228 | int pkg; | ||
1229 | |||
1230 | sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu); | ||
1231 | filep = fopen(path, "r"); | ||
1232 | if (filep == NULL) { | ||
1233 | perror(path); | ||
1234 | exit(1); | ||
1235 | } | ||
1236 | if (fscanf(filep, "%d", &pkg) != 1) { | ||
1237 | perror(path); | ||
1238 | exit(1); | ||
1239 | } | ||
1240 | fclose(filep); | ||
1241 | return pkg; | ||
1242 | } | 1224 | } |
1243 | 1225 | ||
1244 | int get_core_id(int cpu) | 1226 | int get_core_id(int cpu) |
1245 | { | 1227 | { |
1246 | char path[80]; | 1228 | return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu); |
1247 | FILE *filep; | ||
1248 | int core; | ||
1249 | |||
1250 | sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/core_id", cpu); | ||
1251 | filep = fopen(path, "r"); | ||
1252 | if (filep == NULL) { | ||
1253 | perror(path); | ||
1254 | exit(1); | ||
1255 | } | ||
1256 | if (fscanf(filep, "%d", &core) != 1) { | ||
1257 | perror(path); | ||
1258 | exit(1); | ||
1259 | } | ||
1260 | fclose(filep); | ||
1261 | return core; | ||
1262 | } | 1229 | } |
1263 | 1230 | ||
1264 | int get_num_ht_siblings(int cpu) | 1231 | int get_num_ht_siblings(int cpu) |