time-utils.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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;
  93. c = strchr(str, '%');
  94. if (c)
  95. *c = '\0';
  96. else
  97. return -1;
  98. *pcnt = atof(str) / 100.0;
  99. return 0;
  100. }
  101. static int percent_slash_split(char *str, struct perf_time_interval *ptime,
  102. u64 start, u64 end)
  103. {
  104. char *p, *end_str;
  105. double pcnt, start_pcnt, end_pcnt;
  106. u64 total = end - start;
  107. int i;
  108. /*
  109. * Example:
  110. * 10%/2: select the second 10% slice and the third 10% slice
  111. */
  112. /* We can modify this string since the original one is copied */
  113. p = strchr(str, '/');
  114. if (!p)
  115. return -1;
  116. *p = '\0';
  117. if (parse_percent(&pcnt, str) < 0)
  118. return -1;
  119. p++;
  120. i = (int)strtol(p, &end_str, 10);
  121. if (*end_str)
  122. return -1;
  123. if (pcnt <= 0.0)
  124. return -1;
  125. start_pcnt = pcnt * (i - 1);
  126. end_pcnt = pcnt * i;
  127. if (start_pcnt < 0.0 || start_pcnt > 1.0 ||
  128. end_pcnt < 0.0 || end_pcnt > 1.0) {
  129. return -1;
  130. }
  131. ptime->start = start + round(start_pcnt * total);
  132. ptime->end = start + round(end_pcnt * total);
  133. return 0;
  134. }
  135. static int percent_dash_split(char *str, struct perf_time_interval *ptime,
  136. u64 start, u64 end)
  137. {
  138. char *start_str = NULL, *end_str;
  139. double start_pcnt, end_pcnt;
  140. u64 total = end - start;
  141. int ret;
  142. /*
  143. * Example: 0%-10%
  144. */
  145. ret = split_start_end(&start_str, &end_str, str, '-');
  146. if (ret || !start_str)
  147. return ret;
  148. if ((parse_percent(&start_pcnt, start_str) != 0) ||
  149. (parse_percent(&end_pcnt, end_str) != 0)) {
  150. free(start_str);
  151. return -1;
  152. }
  153. free(start_str);
  154. if (start_pcnt < 0.0 || start_pcnt > 1.0 ||
  155. end_pcnt < 0.0 || end_pcnt > 1.0 ||
  156. start_pcnt > end_pcnt) {
  157. return -1;
  158. }
  159. ptime->start = start + round(start_pcnt * total);
  160. ptime->end = start + round(end_pcnt * total);
  161. return 0;
  162. }
  163. typedef int (*time_pecent_split)(char *, struct perf_time_interval *,
  164. u64 start, u64 end);
  165. static int percent_comma_split(struct perf_time_interval *ptime_buf, int num,
  166. const char *ostr, u64 start, u64 end,
  167. time_pecent_split func)
  168. {
  169. char *str, *p1, *p2;
  170. int len, ret, i = 0;
  171. str = strdup(ostr);
  172. if (str == NULL)
  173. return -ENOMEM;
  174. len = strlen(str);
  175. p1 = str;
  176. while (p1 < str + len) {
  177. if (i >= num) {
  178. free(str);
  179. return -1;
  180. }
  181. p2 = strchr(p1, ',');
  182. if (p2)
  183. *p2 = '\0';
  184. ret = (func)(p1, &ptime_buf[i], start, end);
  185. if (ret < 0) {
  186. free(str);
  187. return -1;
  188. }
  189. pr_debug("start time %d: %" PRIu64 ", ", i, ptime_buf[i].start);
  190. pr_debug("end time %d: %" PRIu64 "\n", i, ptime_buf[i].end);
  191. i++;
  192. if (p2)
  193. p1 = p2 + 1;
  194. else
  195. break;
  196. }
  197. free(str);
  198. return i;
  199. }
  200. int perf_time__percent_parse_str(struct perf_time_interval *ptime_buf, int num,
  201. const char *ostr, u64 start, u64 end)
  202. {
  203. char *c;
  204. /*
  205. * ostr example:
  206. * 10%/2,10%/3: select the second 10% slice and the third 10% slice
  207. * 0%-10%,30%-40%: multiple time range
  208. */
  209. memset(ptime_buf, 0, sizeof(*ptime_buf) * num);
  210. c = strchr(ostr, '/');
  211. if (c) {
  212. return percent_comma_split(ptime_buf, num, ostr, start,
  213. end, percent_slash_split);
  214. }
  215. c = strchr(ostr, '-');
  216. if (c) {
  217. return percent_comma_split(ptime_buf, num, ostr, start,
  218. end, percent_dash_split);
  219. }
  220. return -1;
  221. }
  222. bool perf_time__skip_sample(struct perf_time_interval *ptime, u64 timestamp)
  223. {
  224. /* if time is not set don't drop sample */
  225. if (timestamp == 0)
  226. return false;
  227. /* otherwise compare sample time to time window */
  228. if ((ptime->start && timestamp < ptime->start) ||
  229. (ptime->end && timestamp > ptime->end)) {
  230. return true;
  231. }
  232. return false;
  233. }
  234. bool perf_time__ranges_skip_sample(struct perf_time_interval *ptime_buf,
  235. int num, u64 timestamp)
  236. {
  237. struct perf_time_interval *ptime;
  238. int i;
  239. if ((timestamp == 0) || (num == 0))
  240. return false;
  241. if (num == 1)
  242. return perf_time__skip_sample(&ptime_buf[0], timestamp);
  243. /*
  244. * start/end of multiple time ranges must be valid.
  245. */
  246. for (i = 0; i < num; i++) {
  247. ptime = &ptime_buf[i];
  248. if (timestamp >= ptime->start &&
  249. ((timestamp < ptime->end && i < num - 1) ||
  250. (timestamp <= ptime->end && i == num - 1))) {
  251. break;
  252. }
  253. }
  254. return (i == num) ? true : false;
  255. }
  256. int timestamp__scnprintf_usec(u64 timestamp, char *buf, size_t sz)
  257. {
  258. u64 sec = timestamp / NSEC_PER_SEC;
  259. u64 usec = (timestamp % NSEC_PER_SEC) / NSEC_PER_USEC;
  260. return scnprintf(buf, sz, "%"PRIu64".%06"PRIu64, sec, usec);
  261. }
  262. int fetch_current_timestamp(char *buf, size_t sz)
  263. {
  264. struct timeval tv;
  265. struct tm tm;
  266. char dt[32];
  267. if (gettimeofday(&tv, NULL) || !localtime_r(&tv.tv_sec, &tm))
  268. return -1;
  269. if (!strftime(dt, sizeof(dt), "%Y%m%d%H%M%S", &tm))
  270. return -1;
  271. scnprintf(buf, sz, "%s%02u", dt, (unsigned)tv.tv_usec / 10000);
  272. return 0;
  273. }