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