aboutsummaryrefslogtreecommitdiffstats
path: root/tools/perf/util/time-utils.c
diff options
context:
space:
mode:
authorDavid Ahern <dsa@cumulusnetworks.com>2016-11-29 12:15:41 -0500
committerArnaldo Carvalho de Melo <acme@redhat.com>2016-12-01 11:02:32 -0500
commitfdf9dc4b34f5f40919370c4601eccfd0db726aa5 (patch)
tree035673547bf98ad5bf0208421683e05c593c6070 /tools/perf/util/time-utils.c
parent64eff7d9c4469b7e24fb7e5416a67ee5959c3f76 (diff)
perf tools: Add time-based utility functions
Add function to parse a user time string of the form <start>,<stop> where start and stop are time in sec.nsec format. Both start and stop times are optional. Add function to determine if a sample time is within a given time time window of interest. Signed-off-by: David Ahern <dsahern@gmail.com> Acked-by: Namhyung Kim <namhyung@kernel.org> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Link: http://lkml.kernel.org/r/1480439746-42695-2-git-send-email-dsahern@gmail.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'tools/perf/util/time-utils.c')
-rw-r--r--tools/perf/util/time-utils.c85
1 files changed, 85 insertions, 0 deletions
diff --git a/tools/perf/util/time-utils.c b/tools/perf/util/time-utils.c
new file mode 100644
index 000000000000..0443b2afd0cf
--- /dev/null
+++ b/tools/perf/util/time-utils.c
@@ -0,0 +1,85 @@
1#include <string.h>
2#include <sys/time.h>
3#include <time.h>
4#include <errno.h>
5#include <inttypes.h>
6
7#include "perf.h"
8#include "debug.h"
9#include "time-utils.h"
10#include "util.h"
11
12static int parse_timestr_sec_nsec(struct perf_time_interval *ptime,
13 char *start_str, char *end_str)
14{
15 if (start_str && (*start_str != '\0') &&
16 (parse_nsec_time(start_str, &ptime->start) != 0)) {
17 return -1;
18 }
19
20 if (end_str && (*end_str != '\0') &&
21 (parse_nsec_time(end_str, &ptime->end) != 0)) {
22 return -1;
23 }
24
25 return 0;
26}
27
28int perf_time__parse_str(struct perf_time_interval *ptime, const char *ostr)
29{
30 char *start_str, *end_str;
31 char *d, *str;
32 int rc = 0;
33
34 if (ostr == NULL || *ostr == '\0')
35 return 0;
36
37 /* copy original string because we need to modify it */
38 str = strdup(ostr);
39 if (str == NULL)
40 return -ENOMEM;
41
42 ptime->start = 0;
43 ptime->end = 0;
44
45 /* str has the format: <start>,<stop>
46 * variations: <start>,
47 * ,<stop>
48 * ,
49 */
50 start_str = str;
51 d = strchr(start_str, ',');
52 if (d) {
53 *d = '\0';
54 ++d;
55 }
56 end_str = d;
57
58 rc = parse_timestr_sec_nsec(ptime, start_str, end_str);
59
60 free(str);
61
62 /* make sure end time is after start time if it was given */
63 if (rc == 0 && ptime->end && ptime->end < ptime->start)
64 return -EINVAL;
65
66 pr_debug("start time %" PRIu64 ", ", ptime->start);
67 pr_debug("end time %" PRIu64 "\n", ptime->end);
68
69 return rc;
70}
71
72bool perf_time__skip_sample(struct perf_time_interval *ptime, u64 timestamp)
73{
74 /* if time is not set don't drop sample */
75 if (timestamp == 0)
76 return false;
77
78 /* otherwise compare sample time to time window */
79 if ((ptime->start && timestamp < ptime->start) ||
80 (ptime->end && timestamp > ptime->end)) {
81 return true;
82 }
83
84 return false;
85}