perf util: Add parse_nsec_time() function
authorNamhyung Kim <namhyung.kim@lge.com>
Tue, 4 Jun 2013 01:50:29 +0000 (10:50 +0900)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Wed, 7 Aug 2013 20:35:26 +0000 (17:35 -0300)
The parse_nsec_time() function is for parsing a string of time into
64-bit nsec value.  It's a preparation of time filtering in some of perf
commands.

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Tested-by: David Ahern <dsahern@gmail.com>
Acked-by: David Ahern <dsahern@gmail.com>
Cc: Andi Kleen <andi@firstfloor.org>
Cc: David Ahern <dsahern@gmail.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Peter Zijlstra <a.p.zijlstra@chello.nl>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lkml.kernel.org/r/1370310629-9642-1-git-send-email-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/util/util.c
tools/perf/util/util.h

index 9a0658405760e1158d1ca1f51ead1472b651058c..6d17b18e915d56b47e02d16c4c27cae2a85764db 100644 (file)
@@ -328,3 +328,36 @@ void put_tracing_file(char *file)
 {
        free(file);
 }
+
+int parse_nsec_time(const char *str, u64 *ptime)
+{
+       u64 time_sec, time_nsec;
+       char *end;
+
+       time_sec = strtoul(str, &end, 10);
+       if (*end != '.' && *end != '\0')
+               return -1;
+
+       if (*end == '.') {
+               int i;
+               char nsec_buf[10];
+
+               if (strlen(++end) > 9)
+                       return -1;
+
+               strncpy(nsec_buf, end, 9);
+               nsec_buf[9] = '\0';
+
+               /* make it nsec precision */
+               for (i = strlen(nsec_buf); i < 9; i++)
+                       nsec_buf[i] = '0';
+
+               time_nsec = strtoul(nsec_buf, &end, 10);
+               if (*end != '\0')
+                       return -1;
+       } else
+               time_nsec = 0;
+
+       *ptime = time_sec * NSEC_PER_SEC + time_nsec;
+       return 0;
+}
index cc1574edcd9abbe2554c5f3d986af7427c255485..a53535949043f32654a181c3728a421c52baf276 100644 (file)
@@ -208,6 +208,8 @@ static inline int has_extension(const char *filename, const char *ext)
 #define NSEC_PER_MSEC  1000000L
 #endif
 
+int parse_nsec_time(const char *str, u64 *ptime);
+
 extern unsigned char sane_ctype[256];
 #define GIT_SPACE              0x01
 #define GIT_DIGIT              0x02