time-utils.c 11 KB

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