mpp_time.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. /*
  2. * Copyright 2015 Rockchip Electronics Co. LTD
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #define MODULE_TAG "mpp_time"
  17. #include <errno.h>
  18. #include <string.h>
  19. #include <sys/timerfd.h>
  20. #include <sys/epoll.h>
  21. #include "mpp_mem.h"
  22. #include "mpp_time.h"
  23. #include "mpp_debug.h"
  24. #include "mpp_common.h"
  25. #include "mpp_thread.h"
  26. #if _WIN32
  27. #include <sys/types.h>
  28. #include <sys/timeb.h>
  29. RK_S64 mpp_time()
  30. {
  31. struct timeb tb;
  32. ftime(&tb);
  33. return ((RK_S64)tb.time * 1000 + (RK_S64)tb.millitm) * 1000;
  34. }
  35. #else
  36. #include <time.h>
  37. RK_S64 mpp_time()
  38. {
  39. struct timespec time = {0, 0};
  40. clock_gettime(CLOCK_MONOTONIC, &time);
  41. return (RK_S64)time.tv_sec * 1000000 + (RK_S64)time.tv_nsec / 1000;
  42. }
  43. #endif
  44. void mpp_time_diff(RK_S64 start, RK_S64 end, RK_S64 limit, const char *fmt)
  45. {
  46. RK_S64 diff = end - start;
  47. if (diff >= limit)
  48. mpp_dbg(MPP_DBG_TIMING, "%s timing %lld us\n", fmt, diff);
  49. }
  50. typedef struct MppClockImpl_t {
  51. const char *check;
  52. char name[16];
  53. RK_U32 enable;
  54. RK_S64 base;
  55. RK_S64 time;
  56. RK_S64 sum;
  57. RK_S64 count;
  58. } MppClockImpl;
  59. static const char *clock_name = "mpp_clock";
  60. MPP_RET check_is_mpp_clock(void *clock)
  61. {
  62. if (clock && ((MppClockImpl*)clock)->check == clock_name)
  63. return MPP_OK;
  64. mpp_err_f("pointer %p failed on check\n", clock);
  65. mpp_abort();
  66. return MPP_NOK;
  67. }
  68. MppClock mpp_clock_get(const char *name)
  69. {
  70. MppClockImpl *impl = mpp_calloc(MppClockImpl, 1);
  71. if (impl) {
  72. impl->check = clock_name;
  73. snprintf(impl->name, sizeof(impl->name) - 1, name, NULL);
  74. } else
  75. mpp_err_f("malloc failed\n");
  76. return impl;
  77. }
  78. void mpp_clock_put(MppClock clock)
  79. {
  80. if (NULL == clock || check_is_mpp_clock(clock)) {
  81. mpp_err_f("invalid clock %p\n", clock);
  82. return ;
  83. }
  84. mpp_free(clock);
  85. }
  86. void mpp_clock_enable(MppClock clock, RK_U32 enable)
  87. {
  88. if (NULL == clock || check_is_mpp_clock(clock)) {
  89. mpp_err_f("invalid clock %p\n", clock);
  90. } else {
  91. MppClockImpl *p = (MppClockImpl *)clock;
  92. p->enable = (enable) ? (1) : (0);
  93. }
  94. }
  95. RK_S64 mpp_clock_start(MppClock clock)
  96. {
  97. if (NULL == clock || check_is_mpp_clock(clock)) {
  98. mpp_err_f("invalid clock %p\n", clock);
  99. return 0;
  100. }
  101. MppClockImpl *p = (MppClockImpl *)clock;
  102. if (!p->enable)
  103. return 0;
  104. p->base = mpp_time();
  105. p->time = 0;
  106. return p->base;
  107. }
  108. RK_S64 mpp_clock_pause(MppClock clock)
  109. {
  110. if (NULL == clock || check_is_mpp_clock(clock)) {
  111. mpp_err_f("invalid clock %p\n", clock);
  112. return 0;
  113. }
  114. MppClockImpl *p = (MppClockImpl *)clock;
  115. if (!p->enable)
  116. return 0;
  117. RK_S64 time = mpp_time();
  118. if (!p->time) {
  119. // first pause after start
  120. p->sum += time - p->base;
  121. p->count++;
  122. }
  123. p->time = time;
  124. return p->time - p->base;
  125. }
  126. RK_S64 mpp_clock_reset(MppClock clock)
  127. {
  128. if (NULL == clock || check_is_mpp_clock(clock)) {
  129. mpp_err_f("invalid clock %p\n", clock);
  130. } else {
  131. MppClockImpl *p = (MppClockImpl *)clock;
  132. p->base = 0;
  133. p->time = 0;
  134. p->sum = 0;
  135. p->count = 0;
  136. }
  137. return 0;
  138. }
  139. RK_S64 mpp_clock_get_sum(MppClock clock)
  140. {
  141. if (NULL == clock || check_is_mpp_clock(clock)) {
  142. mpp_err_f("invalid clock %p\n", clock);
  143. return 0;
  144. }
  145. MppClockImpl *p = (MppClockImpl *)clock;
  146. return (p->enable) ? (p->sum) : (0);
  147. }
  148. RK_S64 mpp_clock_get_count(MppClock clock)
  149. {
  150. if (NULL == clock || check_is_mpp_clock(clock)) {
  151. mpp_err_f("invalid clock %p\n", clock);
  152. return 0;
  153. }
  154. MppClockImpl *p = (MppClockImpl *)clock;
  155. return (p->enable) ? (p->count) : (0);
  156. }
  157. const char *mpp_clock_get_name(MppClock clock)
  158. {
  159. if (NULL == clock || check_is_mpp_clock(clock)) {
  160. mpp_err_f("invalid clock %p\n", clock);
  161. return NULL;
  162. }
  163. MppClockImpl *p = (MppClockImpl *)clock;
  164. return p->name;
  165. }
  166. typedef struct MppTimerImpl_t {
  167. const char *check;
  168. char name[16];
  169. RK_S32 enabled;
  170. RK_S32 initial;
  171. RK_S32 interval;
  172. RK_S32 timer_fd;
  173. RK_S32 epoll_fd;
  174. MppThread *thd;
  175. MppThreadFunc func;
  176. void *ctx;
  177. } MppTimerImpl;
  178. static const char *timer_name = "mpp_timer";
  179. MPP_RET check_is_mpp_timer(void *timer)
  180. {
  181. if (timer && ((MppTimerImpl*)timer)->check == timer_name)
  182. return MPP_OK;
  183. mpp_err_f("pointer %p failed on check\n", timer);
  184. mpp_abort();
  185. return MPP_NOK;
  186. }
  187. static void *mpp_timer_thread(void *ctx)
  188. {
  189. struct itimerspec ts;
  190. RK_S32 ret = 0;
  191. MppTimerImpl *impl = (MppTimerImpl *)ctx;
  192. MppThread *thd = impl->thd;
  193. RK_S32 timer_fd = impl->timer_fd;
  194. // first expire time
  195. ts.it_value.tv_sec = impl->initial / 1000;
  196. ts.it_value.tv_nsec = (impl->initial % 1000) * 1000;
  197. // last expire time
  198. ts.it_interval.tv_sec = impl->interval / 1000;
  199. ts.it_interval.tv_nsec = (impl->interval % 1000) * 1000 * 1000;
  200. ret = timerfd_settime(timer_fd, 0, &ts, NULL);
  201. if (ret < 0) {
  202. mpp_err("timerfd_settime error, Error:[%d:%s]", errno, strerror(errno));
  203. return NULL;
  204. }
  205. while (1) {
  206. if (MPP_THREAD_RUNNING != thd->get_status())
  207. break;
  208. struct epoll_event events;
  209. memset(&events, 0, sizeof(events));
  210. /* wait epoll event */
  211. RK_S32 fd_cnt = epoll_wait(impl->epoll_fd, &events, 1, 500);
  212. if (fd_cnt && (events.events & EPOLLIN) && (events.data.fd == timer_fd)) {
  213. RK_U64 exp = 0;
  214. ssize_t cnt = read(timer_fd, &exp, sizeof(exp));
  215. mpp_assert(cnt == sizeof(exp));
  216. impl->func(impl->ctx);
  217. }
  218. }
  219. return NULL;
  220. }
  221. MppTimer mpp_timer_get(const char *name)
  222. {
  223. RK_S32 timer_fd = -1;
  224. RK_S32 epoll_fd = -1;
  225. MppTimerImpl *impl = NULL;
  226. do {
  227. struct epoll_event event;
  228. impl = mpp_calloc(MppTimerImpl, 1);
  229. if (NULL == impl) {
  230. mpp_err_f("malloc failed\n");
  231. break;
  232. }
  233. timer_fd = timerfd_create(CLOCK_REALTIME, 0);
  234. if (timer_fd < 0)
  235. break;
  236. epoll_fd = epoll_create(1);
  237. if (epoll_fd < 0)
  238. break;
  239. memset(&event, 0, sizeof(event));
  240. event.data.fd = timer_fd;
  241. event.events = EPOLLIN | EPOLLET;
  242. if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, timer_fd, &event) < 0)
  243. break;
  244. impl->timer_fd = timer_fd;
  245. impl->epoll_fd = epoll_fd;
  246. /* default 1 second (1000ms) looper */
  247. impl->initial = 1000;
  248. impl->interval = 1000;
  249. impl->check = timer_name;
  250. snprintf(impl->name, sizeof(impl->name) - 1, name, NULL);
  251. return impl;
  252. } while (0);
  253. mpp_err_f("failed to create timer\n");
  254. if (impl) {
  255. mpp_free(impl);
  256. impl = NULL;
  257. }
  258. if (timer_fd >= 0) {
  259. close(timer_fd);
  260. timer_fd = -1;
  261. }
  262. if (epoll_fd >= 0) {
  263. close(epoll_fd);
  264. epoll_fd = -1;
  265. }
  266. return NULL;
  267. }
  268. void mpp_timer_set_callback(MppTimer timer, MppThreadFunc func, void *ctx)
  269. {
  270. if (NULL == timer || check_is_mpp_timer(timer)) {
  271. mpp_err_f("invalid timer %p\n", timer);
  272. return ;
  273. }
  274. if (NULL == func) {
  275. mpp_err_f("invalid NULL callback\n");
  276. return ;
  277. }
  278. MppTimerImpl *impl = (MppTimerImpl *)timer;
  279. impl->func = func;
  280. impl->ctx = ctx;
  281. }
  282. void mpp_timer_set_timing(MppTimer timer, RK_S32 initial, RK_S32 interval)
  283. {
  284. if (NULL == timer || check_is_mpp_timer(timer)) {
  285. mpp_err_f("invalid timer %p\n", timer);
  286. return ;
  287. }
  288. MppTimerImpl *impl = (MppTimerImpl *)timer;
  289. impl->initial = initial;
  290. impl->interval = interval;
  291. }
  292. void mpp_timer_set_enable(MppTimer timer, RK_S32 enable)
  293. {
  294. if (NULL == timer || check_is_mpp_timer(timer)) {
  295. mpp_err_f("invalid timer %p\n", timer);
  296. return ;
  297. }
  298. MppTimerImpl *impl = (MppTimerImpl *)timer;
  299. if (NULL == impl->func || impl->initial < 0 || impl->interval < 0) {
  300. mpp_err_f("invalid func %p initial %d interval %d\n",
  301. impl->func, impl->initial, impl->interval);
  302. return ;
  303. }
  304. if (enable) {
  305. if (!impl->enabled && NULL == impl->thd) {
  306. MppThread *thd = new MppThread(mpp_timer_thread, impl, impl->name);
  307. if (thd) {
  308. impl->thd = thd;
  309. impl->enabled = 1;
  310. thd->start();
  311. }
  312. }
  313. } else {
  314. if (impl->enabled && impl->thd) {
  315. impl->thd->stop();
  316. impl->enabled = 0;
  317. }
  318. }
  319. }
  320. void mpp_timer_put(MppTimer timer)
  321. {
  322. if (NULL == timer || check_is_mpp_timer(timer)) {
  323. mpp_err_f("invalid timer %p\n", timer);
  324. return ;
  325. }
  326. MppTimerImpl *impl = (MppTimerImpl *)timer;
  327. if (impl->enabled)
  328. mpp_timer_set_enable(timer, 0);
  329. if (impl->timer_fd >= 0) {
  330. close(impl->timer_fd);
  331. impl->timer_fd = -1;
  332. }
  333. if (impl->epoll_fd >= 0) {
  334. close(impl->epoll_fd);
  335. impl->epoll_fd = -1;
  336. }
  337. if (impl->thd) {
  338. delete impl->thd;
  339. impl->thd = NULL;
  340. }
  341. if (impl) {
  342. mpp_free(impl);
  343. impl = NULL;
  344. }
  345. }
  346. AutoTiming::AutoTiming(const char *name)
  347. {
  348. mStart = mpp_time();
  349. mName = name;
  350. }
  351. AutoTiming::~AutoTiming()
  352. {
  353. mEnd = mpp_time();
  354. mpp_log("%s timing %lld us\n", mName, mEnd - mStart);
  355. }
  356. #define STOPWATCH_TRACE_STR_LEN 64
  357. typedef struct MppStopwatchNode_t {
  358. char event[STOPWATCH_TRACE_STR_LEN];
  359. RK_S64 time;
  360. } MppStopwatchNode;
  361. typedef struct MppStopwatchImpl_t {
  362. const char *check;
  363. char name[STOPWATCH_TRACE_STR_LEN];
  364. RK_S32 max_count;
  365. RK_S32 filled_count;
  366. RK_S32 show_on_exit;
  367. RK_S32 log_len;
  368. RK_S64 time_elipsed;
  369. MppStopwatchNode *nodes;
  370. } MppStopwatchImpl;
  371. static const char *stopwatch_name = "mpp_stopwatch";
  372. MPP_RET check_is_mpp_stopwatch(void *stopwatch)
  373. {
  374. if (stopwatch && ((MppStopwatchImpl*)stopwatch)->check == stopwatch_name)
  375. return MPP_OK;
  376. mpp_err_f("pointer %p failed on check\n", stopwatch);
  377. mpp_abort();
  378. return MPP_NOK;
  379. }
  380. MppStopwatch mpp_stopwatch_get(const char *name)
  381. {
  382. MppStopwatchImpl *impl = mpp_calloc(MppStopwatchImpl, 1);
  383. MppStopwatchNode *nodes = mpp_calloc(MppStopwatchNode, 8);
  384. if (impl && nodes) {
  385. impl->check = stopwatch_name;
  386. snprintf(impl->name, sizeof(impl->name) - 1, name, NULL);
  387. impl->nodes = nodes;
  388. impl->max_count = 8;
  389. } else {
  390. mpp_err_f("malloc failed\n");
  391. MPP_FREE(impl);
  392. MPP_FREE(nodes);
  393. }
  394. return impl;
  395. }
  396. void mpp_stopwatch_set_show_on_exit(MppStopwatch stopwatch, RK_S32 show_on_exit)
  397. {
  398. if (NULL == stopwatch || check_is_mpp_stopwatch(stopwatch)) {
  399. mpp_err_f("invalid stopwatch %p\n", stopwatch);
  400. return ;
  401. }
  402. MppStopwatchImpl *impl = (MppStopwatchImpl *)stopwatch;
  403. impl->show_on_exit = show_on_exit;
  404. }
  405. void mpp_stopwatch_record(MppStopwatch stopwatch, const char *event)
  406. {
  407. /* do not print noisy log */
  408. if (NULL == stopwatch)
  409. return ;
  410. if (check_is_mpp_stopwatch(stopwatch)) {
  411. mpp_err_f("invalid stopwatch %p on %s\n", stopwatch, event);
  412. return ;
  413. }
  414. MppStopwatchImpl *impl = (MppStopwatchImpl *)stopwatch;
  415. if (impl->filled_count >= impl->max_count) {
  416. RK_S32 max_count = impl->max_count * 2;
  417. MppStopwatchNode *nodes = mpp_realloc(impl->nodes, MppStopwatchNode,
  418. max_count);
  419. if (nodes) {
  420. impl->nodes = nodes;
  421. impl->max_count = max_count;
  422. }
  423. }
  424. if (impl->filled_count < impl->max_count) {
  425. MppStopwatchNode *node = impl->nodes + impl->filled_count;
  426. node->time = mpp_time();
  427. if (event) {
  428. RK_S32 len = snprintf(node->event, sizeof(node->event) - 1,
  429. "%s", event);
  430. if (len > impl->log_len)
  431. impl->log_len = len;
  432. }
  433. impl->filled_count++;
  434. }
  435. }
  436. void mpp_stopwatch_put(MppStopwatch stopwatch)
  437. {
  438. if (NULL == stopwatch || check_is_mpp_stopwatch(stopwatch)) {
  439. mpp_err_f("invalid stopwatch %p\n", stopwatch);
  440. return ;
  441. }
  442. MppStopwatchImpl *impl = (MppStopwatchImpl *)stopwatch;
  443. if (impl->show_on_exit && impl->nodes && impl->filled_count) {
  444. MppStopwatchNode *node = impl->nodes;
  445. RK_S64 last_time = node->time;
  446. RK_S32 i;
  447. char fmt[32];
  448. snprintf(fmt, sizeof(fmt) - 1, "%%s %%-%ds: %%6.2f\n", impl->log_len);
  449. node++;
  450. for (i = 1; i < impl->filled_count; i++) {
  451. mpp_log(fmt, impl->name, node->event,
  452. (float)(node->time - last_time) / 1000);
  453. last_time = node->time;
  454. node++;
  455. }
  456. }
  457. MPP_FREE(impl->nodes);
  458. MPP_FREE(impl);
  459. }
  460. RK_S64 mpp_stopwatch_elapsed_time(MppStopwatch stopwatch)
  461. {
  462. if (NULL == stopwatch || check_is_mpp_stopwatch(stopwatch)) {
  463. mpp_err_f("invalid stopwatch %p\n", stopwatch);
  464. return 0;
  465. }
  466. MppStopwatchImpl *impl = (MppStopwatchImpl *)stopwatch;
  467. if (impl->filled_count < 2)
  468. return 0;
  469. RK_S64 base_time = impl->nodes[0].time;
  470. RK_S64 curr_time = impl->nodes[impl->filled_count - 1].time;
  471. RK_S64 elapsed_time = curr_time - base_time;
  472. return elapsed_time;
  473. }