1 // SPDX-License-Identifier: GPL-2.0
5 #include <linux/time64.h>
12 #include "time-utils.h"
14 int parse_nsec_time(const char *str, u64 *ptime)
16 u64 time_sec, time_nsec;
19 time_sec = strtoul(str, &end, 10);
20 if (*end != '.' && *end != '\0')
27 if (strlen(++end) > 9)
30 strncpy(nsec_buf, end, 9);
33 /* make it nsec precision */
34 for (i = strlen(nsec_buf); i < 9; i++)
37 time_nsec = strtoul(nsec_buf, &end, 10);
43 *ptime = time_sec * NSEC_PER_SEC + time_nsec;
47 static int parse_timestr_sec_nsec(struct perf_time_interval *ptime,
48 char *start_str, char *end_str)
50 if (start_str && (*start_str != '\0') &&
51 (parse_nsec_time(start_str, &ptime->start) != 0)) {
55 if (end_str && (*end_str != '\0') &&
56 (parse_nsec_time(end_str, &ptime->end) != 0)) {
63 int perf_time__parse_str(struct perf_time_interval *ptime, const char *ostr)
65 char *start_str, *end_str;
69 if (ostr == NULL || *ostr == '\0')
72 /* copy original string because we need to modify it */
80 /* str has the format: <start>,<stop>
81 * variations: <start>,
86 d = strchr(start_str, ',');
93 rc = parse_timestr_sec_nsec(ptime, start_str, end_str);
97 /* make sure end time is after start time if it was given */
98 if (rc == 0 && ptime->end && ptime->end < ptime->start)
101 pr_debug("start time %" PRIu64 ", ", ptime->start);
102 pr_debug("end time %" PRIu64 "\n", ptime->end);
107 bool perf_time__skip_sample(struct perf_time_interval *ptime, u64 timestamp)
109 /* if time is not set don't drop sample */
113 /* otherwise compare sample time to time window */
114 if ((ptime->start && timestamp < ptime->start) ||
115 (ptime->end && timestamp > ptime->end)) {
122 int timestamp__scnprintf_usec(u64 timestamp, char *buf, size_t sz)
124 u64 sec = timestamp / NSEC_PER_SEC;
125 u64 usec = (timestamp % NSEC_PER_SEC) / NSEC_PER_USEC;
127 return scnprintf(buf, sz, "%"PRIu64".%06"PRIu64, sec, usec);
130 int fetch_current_timestamp(char *buf, size_t sz)
136 if (gettimeofday(&tv, NULL) || !localtime_r(&tv.tv_sec, &tm))
139 if (!strftime(dt, sizeof(dt), "%Y%m%d%H%M%S", &tm))
142 scnprintf(buf, sz, "%s%02u", dt, (unsigned)tv.tv_usec / 10000);