1 // SPDX-License-Identifier: GPL-2.0
5 #include <linux/time64.h>
13 #include "time-utils.h"
17 int parse_nsec_time(const char *str, u64 *ptime)
19 u64 time_sec, time_nsec;
22 time_sec = strtoul(str, &end, 10);
23 if (*end != '.' && *end != '\0')
30 if (strlen(++end) > 9)
33 strncpy(nsec_buf, end, 9);
36 /* make it nsec precision */
37 for (i = strlen(nsec_buf); i < 9; i++)
40 time_nsec = strtoul(nsec_buf, &end, 10);
46 *ptime = time_sec * NSEC_PER_SEC + time_nsec;
50 static int parse_timestr_sec_nsec(struct perf_time_interval *ptime,
51 char *start_str, char *end_str)
53 if (start_str && (*start_str != '\0') &&
54 (parse_nsec_time(start_str, &ptime->start) != 0)) {
58 if (end_str && (*end_str != '\0') &&
59 (parse_nsec_time(end_str, &ptime->end) != 0)) {
66 static int split_start_end(char **start, char **end, const char *ostr, char ch)
68 char *start_str, *end_str;
71 if (ostr == NULL || *ostr == '\0')
74 /* copy original string because we need to modify it */
80 d = strchr(start_str, ch);
93 int perf_time__parse_str(struct perf_time_interval *ptime, const char *ostr)
95 char *start_str = NULL, *end_str;
98 rc = split_start_end(&start_str, &end_str, ostr, ',');
105 rc = parse_timestr_sec_nsec(ptime, start_str, end_str);
109 /* make sure end time is after start time if it was given */
110 if (rc == 0 && ptime->end && ptime->end < ptime->start)
113 pr_debug("start time %" PRIu64 ", ", ptime->start);
114 pr_debug("end time %" PRIu64 "\n", ptime->end);
119 static int parse_percent(double *pcnt, char *str)
124 c = strchr(str, '%');
130 d = strtod(str, &endptr);
131 if (endptr != str + strlen(str))
138 static int set_percent_time(struct perf_time_interval *ptime, double start_pcnt,
139 double end_pcnt, u64 start, u64 end)
141 u64 total = end - start;
143 if (start_pcnt < 0.0 || start_pcnt > 1.0 ||
144 end_pcnt < 0.0 || end_pcnt > 1.0) {
148 ptime->start = start + round(start_pcnt * total);
149 ptime->end = start + round(end_pcnt * total);
154 static int percent_slash_split(char *str, struct perf_time_interval *ptime,
158 double pcnt, start_pcnt, end_pcnt;
163 * 10%/2: select the second 10% slice and the third 10% slice
166 /* We can modify this string since the original one is copied */
167 p = strchr(str, '/');
172 if (parse_percent(&pcnt, str) < 0)
176 i = (int)strtol(p, &end_str, 10);
183 start_pcnt = pcnt * (i - 1);
186 return set_percent_time(ptime, start_pcnt, end_pcnt, start, end);
189 static int percent_dash_split(char *str, struct perf_time_interval *ptime,
192 char *start_str = NULL, *end_str;
193 double start_pcnt, end_pcnt;
200 ret = split_start_end(&start_str, &end_str, str, '-');
201 if (ret || !start_str)
204 if ((parse_percent(&start_pcnt, start_str) != 0) ||
205 (parse_percent(&end_pcnt, end_str) != 0)) {
212 return set_percent_time(ptime, start_pcnt, end_pcnt, start, end);
215 typedef int (*time_pecent_split)(char *, struct perf_time_interval *,
218 static int percent_comma_split(struct perf_time_interval *ptime_buf, int num,
219 const char *ostr, u64 start, u64 end,
220 time_pecent_split func)
232 while (p1 < str + len) {
238 p2 = strchr(p1, ',');
242 ret = (func)(p1, &ptime_buf[i], start, end);
248 pr_debug("start time %d: %" PRIu64 ", ", i, ptime_buf[i].start);
249 pr_debug("end time %d: %" PRIu64 "\n", i, ptime_buf[i].end);
263 static int one_percent_convert(struct perf_time_interval *ptime_buf,
264 const char *ostr, u64 start, u64 end, char *c)
267 int len = strlen(ostr), ret;
271 * '%' should be the last character
273 if (ostr + len - 1 != c)
277 * Construct a string like "xx%/1"
279 str = malloc(len + 3);
283 memcpy(str, ostr, len);
284 strcpy(str + len, "/1");
286 ret = percent_slash_split(str, ptime_buf, start, end);
294 int perf_time__percent_parse_str(struct perf_time_interval *ptime_buf, int num,
295 const char *ostr, u64 start, u64 end)
301 * 10%/2,10%/3: select the second 10% slice and the third 10% slice
302 * 0%-10%,30%-40%: multiple time range
303 * 50%: just one percent
306 memset(ptime_buf, 0, sizeof(*ptime_buf) * num);
308 c = strchr(ostr, '/');
310 return percent_comma_split(ptime_buf, num, ostr, start,
311 end, percent_slash_split);
314 c = strchr(ostr, '-');
316 return percent_comma_split(ptime_buf, num, ostr, start,
317 end, percent_dash_split);
320 c = strchr(ostr, '%');
322 return one_percent_convert(ptime_buf, ostr, start, end, c);
327 struct perf_time_interval *perf_time__range_alloc(const char *ostr, int *size)
331 struct perf_time_interval *ptime;
334 * At least allocate one time range.
340 while (p1 < ostr + strlen(ostr)) {
341 p2 = strchr(p1, ',');
351 ptime = calloc(i, sizeof(*ptime));
355 bool perf_time__skip_sample(struct perf_time_interval *ptime, u64 timestamp)
357 /* if time is not set don't drop sample */
361 /* otherwise compare sample time to time window */
362 if ((ptime->start && timestamp < ptime->start) ||
363 (ptime->end && timestamp > ptime->end)) {
370 bool perf_time__ranges_skip_sample(struct perf_time_interval *ptime_buf,
371 int num, u64 timestamp)
373 struct perf_time_interval *ptime;
376 if ((!ptime_buf) || (timestamp == 0) || (num == 0))
380 return perf_time__skip_sample(&ptime_buf[0], timestamp);
383 * start/end of multiple time ranges must be valid.
385 for (i = 0; i < num; i++) {
386 ptime = &ptime_buf[i];
388 if (timestamp >= ptime->start &&
389 (timestamp <= ptime->end || !ptime->end)) {
397 int perf_time__parse_for_ranges(const char *time_str,
398 struct perf_session *session,
399 struct perf_time_interval **ranges,
400 int *range_size, int *range_num)
402 struct perf_time_interval *ptime_range;
405 ptime_range = perf_time__range_alloc(time_str, &size);
409 if (perf_time__parse_str(ptime_range, time_str) != 0) {
410 if (session->evlist->first_sample_time == 0 &&
411 session->evlist->last_sample_time == 0) {
412 pr_err("HINT: no first/last sample time found in perf data.\n"
413 "Please use latest perf binary to execute 'perf record'\n"
414 "(if '--buildid-all' is enabled, please set '--timestamp-boundary').\n");
419 num = perf_time__percent_parse_str(
422 session->evlist->first_sample_time,
423 session->evlist->last_sample_time);
426 pr_err("Invalid time string\n");
436 *ranges = ptime_range;
444 int timestamp__scnprintf_usec(u64 timestamp, char *buf, size_t sz)
446 u64 sec = timestamp / NSEC_PER_SEC;
447 u64 usec = (timestamp % NSEC_PER_SEC) / NSEC_PER_USEC;
449 return scnprintf(buf, sz, "%"PRIu64".%06"PRIu64, sec, usec);
452 int timestamp__scnprintf_nsec(u64 timestamp, char *buf, size_t sz)
454 u64 sec = timestamp / NSEC_PER_SEC,
455 nsec = timestamp % NSEC_PER_SEC;
457 return scnprintf(buf, sz, "%" PRIu64 ".%09" PRIu64, sec, nsec);
460 int fetch_current_timestamp(char *buf, size_t sz)
466 if (gettimeofday(&tv, NULL) || !localtime_r(&tv.tv_sec, &tm))
469 if (!strftime(dt, sizeof(dt), "%Y%m%d%H%M%S", &tm))
472 scnprintf(buf, sz, "%s%02u", dt, (unsigned)tv.tv_usec / 10000);