• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Rockchip Electronics Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef __MPP_TIME_H__
17 #define __MPP_TIME_H__
18 
19 #include "rk_type.h"
20 #include "mpp_thread.h"
21 
22 #if defined(_WIN32) && !defined(__MINGW32CE__)
23 #include <windows.h>
24 #define msleep                  Sleep
25 #define sleep(x)                Sleep((x)*1000)
26 #else
27 #include <unistd.h>
28 #define msleep(x)               usleep((x)*1000)
29 #endif
30 
31 typedef void* MppClock;
32 typedef void* MppTimer;
33 typedef void* MppStopwatch;
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
39 RK_S64 mpp_time(void);
40 void mpp_time_diff(RK_S64 start, RK_S64 end, RK_S64 limit, const char *fmt);
41 
42 /*
43  * Clock create / destroy / enable / disable function
44  * Note when clock is create it is default disabled user need to call enable
45  * fucntion with enable = 1 to enable the clock.
46  * User can use enable function with enable = 0 to disable the clock.
47  */
48 MppClock mpp_clock_get(const char *name);
49 void mpp_clock_put(MppClock clock);
50 void mpp_clock_enable(MppClock clock, RK_U32 enable);
51 
52 /*
53  * Clock basic operation function
54  * start : let clock start timing counter
55  * pause : let clock pause and return the diff to start time
56  * reset : let clock counter to all zero
57  */
58 RK_S64 mpp_clock_start(MppClock clock);
59 RK_S64 mpp_clock_pause(MppClock clock);
60 RK_S64 mpp_clock_reset(MppClock clock);
61 
62 /*
63  * These clock helper function can only be call when clock is paused:
64  * mpp_clock_get_sum    - Return clock sum up total value
65  * mpp_clock_get_count  - Return clock sum up counter value
66  * mpp_clock_get_name   - Return clock name
67  */
68 RK_S64 mpp_clock_get_sum(MppClock clock);
69 RK_S64 mpp_clock_get_count(MppClock clock);
70 const char *mpp_clock_get_name(MppClock clock);
71 
72 /*
73  * MppTimer is for timer with callback function
74  * It will provide the ability to repeat doing something until it is
75  * disalble or put.
76  *
77  * Timer work flow
78  *
79  * 1. mpp_timer_get
80  * 2. mpp_timer_set_callback
81  * 3. mpp_timer_set_timing(initial, interval)
82  * 4. mpp_timer_set_enable(initial, 1)
83  *    ... running ...
84  * 5. mpp_timer_set_enable(initial, 0)
85  * 6. mpp_timer_put
86  */
87 MppTimer mpp_timer_get(const char *name);
88 void mpp_timer_set_callback(MppTimer timer, MppThreadFunc func, void *ctx);
89 void mpp_timer_set_timing(MppTimer timer, RK_S32 initial, RK_S32 interval);
90 void mpp_timer_set_enable(MppTimer timer, RK_S32 enable);
91 void mpp_timer_put(MppTimer timer);
92 
93 /*
94  * MppStopwatch is for timer to record event and time
95  *
96  * Stopwatch work flow
97  *
98  * 1. mpp_stopwatch_get
99  * 2. mpp_stopwatch_setup(max_count, show_on_exit)
100  * 3. mpp_stopwatch_record(event)
101  *    ... running ...
102  * 4. mpp_stopwatch_record(event)
103  * 5. mpp_stopwatch_put (show events and time)
104  */
105 MppStopwatch mpp_stopwatch_get(const char *name);
106 void mpp_stopwatch_set_show_on_exit(MppStopwatch stopwatch, RK_S32 show_on_exit);
107 void mpp_stopwatch_record(MppStopwatch stopwatch, const char *event);
108 void mpp_stopwatch_put(MppStopwatch timer);
109 RK_S64 mpp_stopwatch_elapsed_time(MppStopwatch stopwatch);
110 
111 #ifdef __cplusplus
112 }
113 #endif
114 
115 #ifdef __cplusplus
116 class AutoTiming {
117 public:
118     AutoTiming(const char *name = __FUNCTION__);
119     ~AutoTiming();
120 private:
121     const char  *mName;
122     RK_S64      mStart;
123     RK_S64      mEnd;
124 
125     AutoTiming(const AutoTiming &);
126     AutoTiming &operator = (const AutoTiming&);
127 };
128 
129 #endif
130 
131 #define AUTO_TIMER_STRING(name, cnt)        name ## cnt
132 #define AUTO_TIMER_NAME_STRING(name, cnt)   AUTO_TIMER_STRING(name, cnt)
133 #define AUTO_TIMER_NAME(name)               AUTO_TIMER_NAME_STRING(name, __COUNTER__)
134 #define AUTO_TIMING()                       AutoTiming AUTO_TIMER_NAME(auto_timing)(__FUNCTION__)
135 
136 #endif /* __MPP_TIME_H__ */
137