diff options
Diffstat (limited to 'tools/perf/util/util.c')
-rw-r--r-- | tools/perf/util/util.c | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/tools/perf/util/util.c b/tools/perf/util/util.c index 59d868add275..6d17b18e915d 100644 --- a/tools/perf/util/util.c +++ b/tools/perf/util/util.c | |||
@@ -269,3 +269,95 @@ void perf_debugfs_set_path(const char *mntpt) | |||
269 | snprintf(debugfs_mountpoint, strlen(debugfs_mountpoint), "%s", mntpt); | 269 | snprintf(debugfs_mountpoint, strlen(debugfs_mountpoint), "%s", mntpt); |
270 | set_tracing_events_path(mntpt); | 270 | set_tracing_events_path(mntpt); |
271 | } | 271 | } |
272 | |||
273 | static const char *find_debugfs(void) | ||
274 | { | ||
275 | const char *path = perf_debugfs_mount(NULL); | ||
276 | |||
277 | if (!path) | ||
278 | fprintf(stderr, "Your kernel does not support the debugfs filesystem"); | ||
279 | |||
280 | return path; | ||
281 | } | ||
282 | |||
283 | /* | ||
284 | * Finds the path to the debugfs/tracing | ||
285 | * Allocates the string and stores it. | ||
286 | */ | ||
287 | const char *find_tracing_dir(void) | ||
288 | { | ||
289 | static char *tracing; | ||
290 | static int tracing_found; | ||
291 | const char *debugfs; | ||
292 | |||
293 | if (tracing_found) | ||
294 | return tracing; | ||
295 | |||
296 | debugfs = find_debugfs(); | ||
297 | if (!debugfs) | ||
298 | return NULL; | ||
299 | |||
300 | tracing = malloc(strlen(debugfs) + 9); | ||
301 | if (!tracing) | ||
302 | return NULL; | ||
303 | |||
304 | sprintf(tracing, "%s/tracing", debugfs); | ||
305 | |||
306 | tracing_found = 1; | ||
307 | return tracing; | ||
308 | } | ||
309 | |||
310 | char *get_tracing_file(const char *name) | ||
311 | { | ||
312 | const char *tracing; | ||
313 | char *file; | ||
314 | |||
315 | tracing = find_tracing_dir(); | ||
316 | if (!tracing) | ||
317 | return NULL; | ||
318 | |||
319 | file = malloc(strlen(tracing) + strlen(name) + 2); | ||
320 | if (!file) | ||
321 | return NULL; | ||
322 | |||
323 | sprintf(file, "%s/%s", tracing, name); | ||
324 | return file; | ||
325 | } | ||
326 | |||
327 | void put_tracing_file(char *file) | ||
328 | { | ||
329 | free(file); | ||
330 | } | ||
331 | |||
332 | int parse_nsec_time(const char *str, u64 *ptime) | ||
333 | { | ||
334 | u64 time_sec, time_nsec; | ||
335 | char *end; | ||
336 | |||
337 | time_sec = strtoul(str, &end, 10); | ||
338 | if (*end != '.' && *end != '\0') | ||
339 | return -1; | ||
340 | |||
341 | if (*end == '.') { | ||
342 | int i; | ||
343 | char nsec_buf[10]; | ||
344 | |||
345 | if (strlen(++end) > 9) | ||
346 | return -1; | ||
347 | |||
348 | strncpy(nsec_buf, end, 9); | ||
349 | nsec_buf[9] = '\0'; | ||
350 | |||
351 | /* make it nsec precision */ | ||
352 | for (i = strlen(nsec_buf); i < 9; i++) | ||
353 | nsec_buf[i] = '0'; | ||
354 | |||
355 | time_nsec = strtoul(nsec_buf, &end, 10); | ||
356 | if (*end != '\0') | ||
357 | return -1; | ||
358 | } else | ||
359 | time_nsec = 0; | ||
360 | |||
361 | *ptime = time_sec * NSEC_PER_SEC + time_nsec; | ||
362 | return 0; | ||
363 | } | ||