time-utils.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. #include "perf.h"
  11. #include "debug.h"
  12. #include "time-utils.h"
  13. int parse_nsec_time(const char *str, u64 *ptime)
  14. {
  15. u64 time_sec, time_nsec;
  16. char *end;
  17. time_sec = strtoul(str, &end, 10);
  18. if (*end != '.' && *end != '\0')
  19. return -1;
  20. if (*end == '.') {
  21. int i;
  22. char nsec_buf[10];
  23. if (strlen(++end) > 9)
  24. return -1;
  25. strncpy(nsec_buf, end, 9);
  26. nsec_buf[9] = '\0';
  27. /* make it nsec precision */
  28. for (i = strlen(nsec_buf); i < 9; i++)
  29. nsec_buf[i] = '0';
  30. time_nsec = strtoul(nsec_buf, &end, 10);
  31. if (*end != '\0')
  32. return -1;
  33. } else
  34. time_nsec = 0;
  35. *ptime = time_sec * NSEC_PER_SEC + time_nsec;
  36. return 0;
  37. }
  38. static int parse_timestr_sec_nsec(struct perf_time_interval *ptime,
  39. char *start_str, char *end_str)
  40. {
  41. if (start_str && (*start_str != '\0') &&
  42. (parse_nsec_time(start_str, &ptime->start) != 0)) {
  43. return -1;
  44. }
  45. if (end_str && (*end_str != '\0') &&
  46. (parse_nsec_time(end_str, &ptime->end) != 0)) {
  47. return -1;
  48. }
  49. return 0;
  50. }
  51. static int split_start_end(char **start, char **end, const char *ostr, char ch)
  52. {
  53. char *start_str, *end_str;
  54. char *d, *str;
  55. if (ostr == NULL || *ostr == '\0')
  56. return 0;
  57. /* copy original string because we need to modify it */
  58. str = strdup(ostr);
  59. if (str == NULL)
  60. return -ENOMEM;
  61. start_str = str;
  62. d = strchr(start_str, ch);
  63. if (d) {
  64. *d = '\0';
  65. ++d;
  66. }
  67. end_str = d;
  68. *start = start_str;
  69. *end = end_str;
  70. return 0;
  71. }
  72. int perf_time__parse_str(struct perf_time_interval *ptime, const char *ostr)
  73. {
  74. char *start_str = NULL, *end_str;
  75. int rc;
  76. rc = split_start_end(&start_str, &end_str, ostr, ',');
  77. if (rc || !start_str)
  78. return rc;
  79. ptime->start = 0;
  80. ptime->end = 0;
  81. rc = parse_timestr_sec_nsec(ptime, start_str, end_str);
  82. free(start_str);
  83. /* make sure end time is after start time if it was given */
  84. if (rc == 0 && ptime->end && ptime->end < ptime->start)
  85. return -EINVAL;
  86. pr_debug("start time %" PRIu64 ", ", ptime->start);
  87. pr_debug("end time %" PRIu64 "\n", ptime->end);
  88. return rc;
  89. }
  90. static int parse_percent(double *pcnt, char *str)
  91. {
  92. char *c, *endptr;
  93. double d;
  94. c = strchr(str, '%');
  95. if (c)
  96. *c = '\0';
  97. else
  98. return -1;
  99. d = strtod(str, &endptr);
  100. if (endptr != str + strlen(str))
  101. return -1;
  102. *pcnt = d / 100.0;
  103. return 0;
  104. }
  105. static int percent_slash_split(char *str, struct perf_time_interval *ptime,
  106. u64 start, u64 end)
  107. {
  108. char *p, *end_str;
  109. double pcnt, start_pcnt, end_pcnt;
  110. u64 total = end - start;
  111. int i;
  112. /*
  113. * Example:
  114. * 10%/2: select the second 10% slice and the third 10% slice
  115. */
  116. /* We can modify this string since the original one is copied */
  117. p = strchr(str, '/');
  118. if (!p)
  119. return -1;
  120. *p = '\0';
  121. if (parse_percent(&pcnt, str) < 0)
  122. return -1;
  123. p++;
  124. i = (int)strtol(p, &end_str, 10);
  125. if (*end_str)
  126. return -1;
  127. if (pcnt <= 0.0)
  128. return -1;
  129. start_pcnt = pcnt * (i - 1);
  130. end_pcnt = pcnt * i;
  131. if (start_pcnt < 0.0 || start_pcnt > 1.0 ||
  132. end_pcnt < 0.0 || end_pcnt > 1.0) {
  133. return -1;
  134. }
  135. ptime->start = start + round(start_pcnt * total);
  136. ptime->end = start + round(end_pcnt * total);
  137. return 0;
  138. }
  139. static int percent_dash_split(char *str, struct perf_time_interval *ptime,
  140. u64 start, u64 end)
  141. {
  142. char *start_str = NULL, *end_str;
  143. double start_pcnt, end_pcnt;
  144. u64 total = end - start;
  145. int ret;
  146. /*
  147. * Example: 0%-10%
  148. */
  149. ret = split_start_end(&start_str, &end_str, str, '-');
  150. if (ret || !start_str)
  151. return ret;
  152. if ((parse_percent(&start_pcnt, start_str) != 0) ||
  153. (parse_percent(&end_pcnt, end_str) != 0)) {
  154. free(start_str);
  155. return -1;
  156. }
  157. free(start_str);
  158. if (start_pcnt < 0.0 || start_pcnt > 1.0 ||
  159. end_pcnt < 0.0 || end_pcnt > 1.0 ||
  160. start_pcnt > end_pcnt) {
  161. return -1;
  162. }
  163. ptime->start = start + round(start_pcnt * total);
  164. ptime->end = start + round(end_pcnt * total);
  165. return 0;
  166. }
  167. typedef int (*time_pecent_split)(char *, struct perf_time_interval *,
  168. u64 start, u64 end);
  169. static int percent_comma_split(struct perf_time_interval *ptime_buf, int num,
  170. const char *ostr, u64 start, u64 end,
  171. time_pecent_split func)
  172. {
  173. char *str, *p1, *p2;
  174. int len, ret, i = 0;
  175. str = strdup(ostr);
  176. if (str == NULL)
  177. return -ENOMEM;
  178. len = strlen(str);
  179. p1 = str;
  180. while (p1 < str + len) {
  181. if (i >= num) {
  182. free(str);
  183. return -1;
  184. }
  185. p2 = strchr(p1, ',');
  186. if (p2)
  187. *p2 = '\0';
  188. ret = (func)(p1, &ptime_buf[i], start, end);
  189. if (ret < 0) {
  190. free(str);
  191. return -1;
  192. }
  193. pr_debug("start time %d: %" PRIu64 ", ", i, ptime_buf[i].start);
  194. pr_debug("end time %d: %" PRIu64 "\n", i, ptime_buf[i].end);
  195. i++;
  196. if (p2)
  197. p1 = p2 + 1;
  198. else
  199. break;
  200. }
  201. free(str);
  202. return i;
  203. }
  204. static int one_percent_convert(struct perf_time_interval *ptime_buf,
  205. const char *ostr, u64 start, u64 end, char *c)
  206. {
  207. char *str;
  208. int len = strlen(ostr), ret;
  209. /*
  210. * c points to '%'.
  211. * '%' should be the last character
  212. */
  213. if (ostr + len - 1 != c)
  214. return -1;
  215. /*
  216. * Construct a string like "xx%/1"
  217. */
  218. str = malloc(len + 3);
  219. if (str == NULL)
  220. return -ENOMEM;
  221. memcpy(str, ostr, len);
  222. strcpy(str + len, "/1");
  223. ret = percent_slash_split(str, ptime_buf, start, end);
  224. if (ret == 0)
  225. ret = 1;
  226. free(str);
  227. return ret;
  228. }
  229. int perf_time__percent_parse_str(struct perf_time_interval *ptime_buf, int num,
  230. const char *ostr, u64 start, u64 end)
  231. {
  232. char *c;
  233. /*
  234. * ostr example:
  235. * 10%/2,10%/3: select the second 10% slice and the third 10% slice
  236. * 0%-10%,30%-40%: multiple time range
  237. * 50%: just one percent
  238. */
  239. memset(ptime_buf, 0, sizeof(*ptime_buf) * num);
  240. c = strchr(ostr, '/');
  241. if (c) {
  242. return percent_comma_split(ptime_buf, num, ostr, start,
  243. end, percent_slash_split);
  244. }
  245. c = strchr(ostr, '-');
  246. if (c) {
  247. return percent_comma_split(ptime_buf, num, ostr, start,
  248. end, percent_dash_split);
  249. }
  250. c = strchr(ostr, '%');
  251. if (c)
  252. return one_percent_convert(ptime_buf, ostr, start, end, c);
  253. return -1;
  254. }
  255. struct perf_time_interval *perf_time__range_alloc(const char *ostr, int *size)
  256. {
  257. const char *p1, *p2;
  258. int i = 1;
  259. struct perf_time_interval *ptime;
  260. /*
  261. * At least allocate one time range.
  262. */
  263. if (!ostr)
  264. goto alloc;
  265. p1 = ostr;
  266. while (p1 < ostr + strlen(ostr)) {
  267. p2 = strchr(p1, ',');
  268. if (!p2)
  269. break;
  270. p1 = p2 + 1;
  271. i++;
  272. }
  273. alloc:
  274. *size = i;
  275. ptime = calloc(i, sizeof(*ptime));
  276. return ptime;
  277. }
  278. bool perf_time__skip_sample(struct perf_time_interval *ptime, u64 timestamp)
  279. {
  280. /* if time is not set don't drop sample */
  281. if (timestamp == 0)
  282. return false;
  283. /* otherwise compare sample time to time window */
  284. if ((ptime->start && timestamp < ptime->start) ||
  285. (ptime->end && timestamp > ptime->end)) {
  286. return true;
  287. }
  288. return false;
  289. }
  290. bool perf_time__ranges_skip_sample(struct perf_time_interval *ptime_buf,
  291. int num, u64 timestamp)
  292. {
  293. struct perf_time_interval *ptime;
  294. int i;
  295. if ((timestamp == 0) || (num == 0))
  296. return false;
  297. if (num == 1)
  298. return perf_time__skip_sample(&ptime_buf[0], timestamp);
  299. /*
  300. * start/end of multiple time ranges must be valid.
  301. */
  302. for (i = 0; i < num; i++) {
  303. ptime = &ptime_buf[i];
  304. if (timestamp >= ptime->start &&
  305. ((timestamp < ptime->end && i < num - 1) ||
  306. (timestamp <= ptime->end && i == num - 1))) {
  307. break;
  308. }
  309. }
  310. return (i == num) ? true : false;
  311. }
  312. int timestamp__scnprintf_usec(u64 timestamp, char *buf, size_t sz)
  313. {
  314. u64 sec = timestamp / NSEC_PER_SEC;
  315. u64 usec = (timestamp % NSEC_PER_SEC) / NSEC_PER_USEC;
  316. return scnprintf(buf, sz, "%"PRIu64".%06"PRIu64, sec, usec);
  317. }
  318. int fetch_current_timestamp(char *buf, size_t sz)
  319. {
  320. struct timeval tv;
  321. struct tm tm;
  322. char dt[32];
  323. if (gettimeofday(&tv, NULL) || !localtime_r(&tv.tv_sec, &tm))
  324. return -1;
  325. if (!strftime(dt, sizeof(dt), "%Y%m%d%H%M%S", &tm))
  326. return -1;
  327. scnprintf(buf, sz, "%s%02u", dt, (unsigned)tv.tv_usec / 10000);
  328. return 0;
  329. }