]> asedeno.scripts.mit.edu Git - linux.git/blob - tools/perf/util/time-utils.c
perf time-utils: Factor out set_percent_time()
[linux.git] / tools / perf / util / time-utils.c
1 // SPDX-License-Identifier: GPL-2.0
2 #include <stdlib.h>
3 #include <string.h>
4 #include <sys/time.h>
5 #include <linux/time64.h>
6 #include <time.h>
7 #include <errno.h>
8 #include <inttypes.h>
9 #include <math.h>
10
11 #include "perf.h"
12 #include "debug.h"
13 #include "time-utils.h"
14 #include "session.h"
15 #include "evlist.h"
16
17 int parse_nsec_time(const char *str, u64 *ptime)
18 {
19         u64 time_sec, time_nsec;
20         char *end;
21
22         time_sec = strtoul(str, &end, 10);
23         if (*end != '.' && *end != '\0')
24                 return -1;
25
26         if (*end == '.') {
27                 int i;
28                 char nsec_buf[10];
29
30                 if (strlen(++end) > 9)
31                         return -1;
32
33                 strncpy(nsec_buf, end, 9);
34                 nsec_buf[9] = '\0';
35
36                 /* make it nsec precision */
37                 for (i = strlen(nsec_buf); i < 9; i++)
38                         nsec_buf[i] = '0';
39
40                 time_nsec = strtoul(nsec_buf, &end, 10);
41                 if (*end != '\0')
42                         return -1;
43         } else
44                 time_nsec = 0;
45
46         *ptime = time_sec * NSEC_PER_SEC + time_nsec;
47         return 0;
48 }
49
50 static int parse_timestr_sec_nsec(struct perf_time_interval *ptime,
51                                   char *start_str, char *end_str)
52 {
53         if (start_str && (*start_str != '\0') &&
54             (parse_nsec_time(start_str, &ptime->start) != 0)) {
55                 return -1;
56         }
57
58         if (end_str && (*end_str != '\0') &&
59             (parse_nsec_time(end_str, &ptime->end) != 0)) {
60                 return -1;
61         }
62
63         return 0;
64 }
65
66 static int split_start_end(char **start, char **end, const char *ostr, char ch)
67 {
68         char *start_str, *end_str;
69         char *d, *str;
70
71         if (ostr == NULL || *ostr == '\0')
72                 return 0;
73
74         /* copy original string because we need to modify it */
75         str = strdup(ostr);
76         if (str == NULL)
77                 return -ENOMEM;
78
79         start_str = str;
80         d = strchr(start_str, ch);
81         if (d) {
82                 *d = '\0';
83                 ++d;
84         }
85         end_str = d;
86
87         *start = start_str;
88         *end = end_str;
89
90         return 0;
91 }
92
93 int perf_time__parse_str(struct perf_time_interval *ptime, const char *ostr)
94 {
95         char *start_str = NULL, *end_str;
96         int rc;
97
98         rc = split_start_end(&start_str, &end_str, ostr, ',');
99         if (rc || !start_str)
100                 return rc;
101
102         ptime->start = 0;
103         ptime->end = 0;
104
105         rc = parse_timestr_sec_nsec(ptime, start_str, end_str);
106
107         free(start_str);
108
109         /* make sure end time is after start time if it was given */
110         if (rc == 0 && ptime->end && ptime->end < ptime->start)
111                 return -EINVAL;
112
113         pr_debug("start time %" PRIu64 ", ", ptime->start);
114         pr_debug("end time %" PRIu64 "\n", ptime->end);
115
116         return rc;
117 }
118
119 static int parse_percent(double *pcnt, char *str)
120 {
121         char *c, *endptr;
122         double d;
123
124         c = strchr(str, '%');
125         if (c)
126                 *c = '\0';
127         else
128                 return -1;
129
130         d = strtod(str, &endptr);
131         if (endptr != str + strlen(str))
132                 return -1;
133
134         *pcnt = d / 100.0;
135         return 0;
136 }
137
138 static int set_percent_time(struct perf_time_interval *ptime, double start_pcnt,
139                             double end_pcnt, u64 start, u64 end)
140 {
141         u64 total = end - start;
142
143         if (start_pcnt < 0.0 || start_pcnt > 1.0 ||
144             end_pcnt < 0.0 || end_pcnt > 1.0) {
145                 return -1;
146         }
147
148         ptime->start = start + round(start_pcnt * total);
149         ptime->end = start + round(end_pcnt * total);
150
151         return 0;
152 }
153
154 static int percent_slash_split(char *str, struct perf_time_interval *ptime,
155                                u64 start, u64 end)
156 {
157         char *p, *end_str;
158         double pcnt, start_pcnt, end_pcnt;
159         int i;
160
161         /*
162          * Example:
163          * 10%/2: select the second 10% slice and the third 10% slice
164          */
165
166         /* We can modify this string since the original one is copied */
167         p = strchr(str, '/');
168         if (!p)
169                 return -1;
170
171         *p = '\0';
172         if (parse_percent(&pcnt, str) < 0)
173                 return -1;
174
175         p++;
176         i = (int)strtol(p, &end_str, 10);
177         if (*end_str)
178                 return -1;
179
180         if (pcnt <= 0.0)
181                 return -1;
182
183         start_pcnt = pcnt * (i - 1);
184         end_pcnt = pcnt * i;
185
186         return set_percent_time(ptime, start_pcnt, end_pcnt, start, end);
187 }
188
189 static int percent_dash_split(char *str, struct perf_time_interval *ptime,
190                               u64 start, u64 end)
191 {
192         char *start_str = NULL, *end_str;
193         double start_pcnt, end_pcnt;
194         int ret;
195
196         /*
197          * Example: 0%-10%
198          */
199
200         ret = split_start_end(&start_str, &end_str, str, '-');
201         if (ret || !start_str)
202                 return ret;
203
204         if ((parse_percent(&start_pcnt, start_str) != 0) ||
205             (parse_percent(&end_pcnt, end_str) != 0)) {
206                 free(start_str);
207                 return -1;
208         }
209
210         free(start_str);
211
212         return set_percent_time(ptime, start_pcnt, end_pcnt, start, end);
213 }
214
215 typedef int (*time_pecent_split)(char *, struct perf_time_interval *,
216                                  u64 start, u64 end);
217
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)
221 {
222         char *str, *p1, *p2;
223         int len, ret, i = 0;
224
225         str = strdup(ostr);
226         if (str == NULL)
227                 return -ENOMEM;
228
229         len = strlen(str);
230         p1 = str;
231
232         while (p1 < str + len) {
233                 if (i >= num) {
234                         free(str);
235                         return -1;
236                 }
237
238                 p2 = strchr(p1, ',');
239                 if (p2)
240                         *p2 = '\0';
241
242                 ret = (func)(p1, &ptime_buf[i], start, end);
243                 if (ret < 0) {
244                         free(str);
245                         return -1;
246                 }
247
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);
250
251                 i++;
252
253                 if (p2)
254                         p1 = p2 + 1;
255                 else
256                         break;
257         }
258
259         free(str);
260         return i;
261 }
262
263 static int one_percent_convert(struct perf_time_interval *ptime_buf,
264                                const char *ostr, u64 start, u64 end, char *c)
265 {
266         char *str;
267         int len = strlen(ostr), ret;
268
269         /*
270          * c points to '%'.
271          * '%' should be the last character
272          */
273         if (ostr + len - 1 != c)
274                 return -1;
275
276         /*
277          * Construct a string like "xx%/1"
278          */
279         str = malloc(len + 3);
280         if (str == NULL)
281                 return -ENOMEM;
282
283         memcpy(str, ostr, len);
284         strcpy(str + len, "/1");
285
286         ret = percent_slash_split(str, ptime_buf, start, end);
287         if (ret == 0)
288                 ret = 1;
289
290         free(str);
291         return ret;
292 }
293
294 int perf_time__percent_parse_str(struct perf_time_interval *ptime_buf, int num,
295                                  const char *ostr, u64 start, u64 end)
296 {
297         char *c;
298
299         /*
300          * ostr example:
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
304          */
305
306         memset(ptime_buf, 0, sizeof(*ptime_buf) * num);
307
308         c = strchr(ostr, '/');
309         if (c) {
310                 return percent_comma_split(ptime_buf, num, ostr, start,
311                                            end, percent_slash_split);
312         }
313
314         c = strchr(ostr, '-');
315         if (c) {
316                 return percent_comma_split(ptime_buf, num, ostr, start,
317                                            end, percent_dash_split);
318         }
319
320         c = strchr(ostr, '%');
321         if (c)
322                 return one_percent_convert(ptime_buf, ostr, start, end, c);
323
324         return -1;
325 }
326
327 struct perf_time_interval *perf_time__range_alloc(const char *ostr, int *size)
328 {
329         const char *p1, *p2;
330         int i = 1;
331         struct perf_time_interval *ptime;
332
333         /*
334          * At least allocate one time range.
335          */
336         if (!ostr)
337                 goto alloc;
338
339         p1 = ostr;
340         while (p1 < ostr + strlen(ostr)) {
341                 p2 = strchr(p1, ',');
342                 if (!p2)
343                         break;
344
345                 p1 = p2 + 1;
346                 i++;
347         }
348
349 alloc:
350         *size = i;
351         ptime = calloc(i, sizeof(*ptime));
352         return ptime;
353 }
354
355 bool perf_time__skip_sample(struct perf_time_interval *ptime, u64 timestamp)
356 {
357         /* if time is not set don't drop sample */
358         if (timestamp == 0)
359                 return false;
360
361         /* otherwise compare sample time to time window */
362         if ((ptime->start && timestamp < ptime->start) ||
363             (ptime->end && timestamp > ptime->end)) {
364                 return true;
365         }
366
367         return false;
368 }
369
370 bool perf_time__ranges_skip_sample(struct perf_time_interval *ptime_buf,
371                                    int num, u64 timestamp)
372 {
373         struct perf_time_interval *ptime;
374         int i;
375
376         if ((!ptime_buf) || (timestamp == 0) || (num == 0))
377                 return false;
378
379         if (num == 1)
380                 return perf_time__skip_sample(&ptime_buf[0], timestamp);
381
382         /*
383          * start/end of multiple time ranges must be valid.
384          */
385         for (i = 0; i < num; i++) {
386                 ptime = &ptime_buf[i];
387
388                 if (timestamp >= ptime->start &&
389                     (timestamp <= ptime->end || !ptime->end)) {
390                         return false;
391                 }
392         }
393
394         return true;
395 }
396
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)
401 {
402         struct perf_time_interval *ptime_range;
403         int size, num, ret;
404
405         ptime_range = perf_time__range_alloc(time_str, &size);
406         if (!ptime_range)
407                 return -ENOMEM;
408
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");
415                         ret = -EINVAL;
416                         goto error;
417                 }
418
419                 num = perf_time__percent_parse_str(
420                                 ptime_range, size,
421                                 time_str,
422                                 session->evlist->first_sample_time,
423                                 session->evlist->last_sample_time);
424
425                 if (num < 0) {
426                         pr_err("Invalid time string\n");
427                         ret = -EINVAL;
428                         goto error;
429                 }
430         } else {
431                 num = 1;
432         }
433
434         *range_size = size;
435         *range_num = num;
436         *ranges = ptime_range;
437         return 0;
438
439 error:
440         free(ptime_range);
441         return ret;
442 }
443
444 int timestamp__scnprintf_usec(u64 timestamp, char *buf, size_t sz)
445 {
446         u64  sec = timestamp / NSEC_PER_SEC;
447         u64 usec = (timestamp % NSEC_PER_SEC) / NSEC_PER_USEC;
448
449         return scnprintf(buf, sz, "%"PRIu64".%06"PRIu64, sec, usec);
450 }
451
452 int timestamp__scnprintf_nsec(u64 timestamp, char *buf, size_t sz)
453 {
454         u64 sec  = timestamp / NSEC_PER_SEC,
455             nsec = timestamp % NSEC_PER_SEC;
456
457         return scnprintf(buf, sz, "%" PRIu64 ".%09" PRIu64, sec, nsec);
458 }
459
460 int fetch_current_timestamp(char *buf, size_t sz)
461 {
462         struct timeval tv;
463         struct tm tm;
464         char dt[32];
465
466         if (gettimeofday(&tv, NULL) || !localtime_r(&tv.tv_sec, &tm))
467                 return -1;
468
469         if (!strftime(dt, sizeof(dt), "%Y%m%d%H%M%S", &tm))
470                 return -1;
471
472         scnprintf(buf, sz, "%s%02u", dt, (unsigned)tv.tv_usec / 10000);
473
474         return 0;
475 }