summaryrefslogtreecommitdiff
path: root/lib/lvgl/src/misc/lv_profiler_builtin.c
blob: ba278ae944eb5073708d15f70615ab17f4642583 (plain)
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
/**
 * @file lv_profiler_builtin.c
 *
 */

/*********************
 *      INCLUDES
 *********************/

#include "lv_profiler_builtin.h"
#include "../lvgl.h"
#include "../core/lv_global.h"

/*********************
 *      DEFINES
 *********************/

#if LV_USE_PROFILER && LV_USE_PROFILER_BUILTIN

#define profiler_ctx LV_GLOBAL_DEFAULT()->profiler_context

#define LV_PROFILER_STR_MAX_LEN 128
#define LV_PROFILER_TICK_PER_SEC_MAX 1000000

#if LV_USE_OS
    #define LV_PROFILER_MULTEX_INIT   lv_mutex_init(&profiler_ctx->mutex)
    #define LV_PROFILER_MULTEX_DEINIT lv_mutex_delete(&profiler_ctx->mutex)
    #define LV_PROFILER_MULTEX_LOCK   lv_mutex_lock(&profiler_ctx->mutex)
    #define LV_PROFILER_MULTEX_UNLOCK lv_mutex_unlock(&profiler_ctx->mutex)
#else
    #define LV_PROFILER_MULTEX_INIT
    #define LV_PROFILER_MULTEX_DEINIT
    #define LV_PROFILER_MULTEX_LOCK
    #define LV_PROFILER_MULTEX_UNLOCK
#endif

/**********************
 *      TYPEDEFS
 **********************/

/**
 * @brief Structure representing a built-in profiler item in LVGL
 */
typedef struct {
    char tag;          /**< The tag of the profiler item */
    uint32_t tick;     /**< The tick value of the profiler item */
    const char * func; /**< A pointer to the function associated with the profiler item */
#if LV_USE_OS
    int tid;           /**< The thread ID of the profiler item */
    int cpu;         /**< The CPU ID of the profiler item */
#endif
} lv_profiler_builtin_item_t;

/**
 * @brief Structure representing a context for the LVGL built-in profiler
 */
typedef struct _lv_profiler_builtin_ctx_t {
    lv_profiler_builtin_item_t * item_arr; /**< Pointer to an array of profiler items */
    uint32_t item_num;                     /**< Number of profiler items in the array */
    uint32_t cur_index;                    /**< Index of the current profiler item */
    lv_profiler_builtin_config_t config;   /**< Configuration for the built-in profiler */
    bool enable;                           /**< Whether the built-in profiler is enabled */
#if LV_USE_OS
    lv_mutex_t mutex;                      /**< Mutex to protect the built-in profiler */
#endif
} lv_profiler_builtin_ctx_t;

/**********************
 *  STATIC PROTOTYPES
 **********************/

static void default_flush_cb(const char * buf);
static int default_tid_get_cb(void);
static int default_cpu_get_cb(void);
static void flush_no_lock(void);

/**********************
 *  STATIC VARIABLES
 **********************/

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
 **********************/

void lv_profiler_builtin_config_init(lv_profiler_builtin_config_t * config)
{
    LV_ASSERT_NULL(config);
    lv_memzero(config, sizeof(lv_profiler_builtin_config_t));
    config->buf_size = LV_PROFILER_BUILTIN_BUF_SIZE;
    config->tick_per_sec = 1000;
    config->tick_get_cb = lv_tick_get;
    config->flush_cb = default_flush_cb;
    config->tid_get_cb = default_tid_get_cb;
    config->cpu_get_cb = default_cpu_get_cb;
}

void lv_profiler_builtin_init(const lv_profiler_builtin_config_t * config)
{
    LV_ASSERT_NULL(config);
    LV_ASSERT_NULL(config->tick_get_cb);

    uint32_t num = config->buf_size / sizeof(lv_profiler_builtin_item_t);
    if(num == 0) {
        LV_LOG_WARN("buf_size must > %d", (int)sizeof(lv_profiler_builtin_item_t));
        return;
    }

    if(config->tick_per_sec == 0 || config->tick_per_sec > LV_PROFILER_TICK_PER_SEC_MAX) {
        LV_LOG_WARN("tick_per_sec range must be between 1~%d", LV_PROFILER_TICK_PER_SEC_MAX);
        return;
    }

    /*Free the old item_arr memory*/
    if(profiler_ctx) {
        lv_profiler_builtin_uninit();
    }

    profiler_ctx = lv_malloc_zeroed(sizeof(lv_profiler_builtin_ctx_t));
    LV_ASSERT_MALLOC(profiler_ctx);

    profiler_ctx->item_arr = lv_malloc(num * sizeof(lv_profiler_builtin_item_t));
    LV_ASSERT_MALLOC(profiler_ctx->item_arr);
    if(profiler_ctx->item_arr == NULL) {
        lv_free(profiler_ctx);
        profiler_ctx = NULL;
        LV_LOG_ERROR("malloc failed for item_arr");
        return;
    }

    LV_PROFILER_MULTEX_INIT;
    profiler_ctx->item_num = num;
    profiler_ctx->config = *config;

    if(profiler_ctx->config.flush_cb) {
        /* add profiler header for perfetto */
        profiler_ctx->config.flush_cb("# tracer: nop\n");
        profiler_ctx->config.flush_cb("#\n");
    }

    lv_profiler_builtin_set_enable(true);

    LV_LOG_INFO("init OK, item_num = %d", (int)num);
}

void lv_profiler_builtin_uninit(void)
{
    LV_ASSERT_NULL(profiler_ctx);
    LV_PROFILER_MULTEX_DEINIT;
    lv_free(profiler_ctx->item_arr);
    lv_free(profiler_ctx);
    profiler_ctx = NULL;
}

void lv_profiler_builtin_set_enable(bool enable)
{
    if(!profiler_ctx) {
        return;
    }

    profiler_ctx->enable = enable;
}

void lv_profiler_builtin_flush(void)
{
    LV_ASSERT_NULL(profiler_ctx);

    LV_PROFILER_MULTEX_LOCK;
    flush_no_lock();
    LV_PROFILER_MULTEX_UNLOCK;
}

void lv_profiler_builtin_write(const char * func, char tag)
{
    LV_ASSERT_NULL(profiler_ctx);
    LV_ASSERT_NULL(func);

    if(!profiler_ctx->enable) {
        return;
    }

    LV_PROFILER_MULTEX_LOCK;

    if(profiler_ctx->cur_index >= profiler_ctx->item_num) {
        flush_no_lock();
        profiler_ctx->cur_index = 0;
    }

    lv_profiler_builtin_item_t * item = &profiler_ctx->item_arr[profiler_ctx->cur_index];
    item->func = func;
    item->tag = tag;
    item->tick = profiler_ctx->config.tick_get_cb();

#if LV_USE_OS
    item->tid = profiler_ctx->config.tid_get_cb();
    item->cpu = profiler_ctx->config.cpu_get_cb();
#endif

    profiler_ctx->cur_index++;

    LV_PROFILER_MULTEX_UNLOCK;
}

/**********************
 *   STATIC FUNCTIONS
 **********************/

static void default_flush_cb(const char * buf)
{
    LV_LOG("%s", buf);
}

static int default_tid_get_cb(void)
{
    return 1;
}

static int default_cpu_get_cb(void)
{
    return 0;
}

static void flush_no_lock(void)
{
    if(!profiler_ctx->config.flush_cb) {
        LV_LOG_WARN("flush_cb is not registered");
        return;
    }

    uint32_t cur = 0;
    char buf[LV_PROFILER_STR_MAX_LEN];
    uint32_t tick_per_sec = profiler_ctx->config.tick_per_sec;
    while(cur < profiler_ctx->cur_index) {
        lv_profiler_builtin_item_t * item = &profiler_ctx->item_arr[cur++];
        uint32_t sec = item->tick / tick_per_sec;
        uint32_t usec = (item->tick % tick_per_sec) * (LV_PROFILER_TICK_PER_SEC_MAX / tick_per_sec);

#if LV_USE_OS
        lv_snprintf(buf, sizeof(buf),
                    "   LVGL-%d [%d] %" LV_PRIu32 ".%06" LV_PRIu32 ": tracing_mark_write: %c|1|%s\n",
                    item->tid,
                    item->cpu,
                    sec,
                    usec,
                    item->tag,
                    item->func);
#else
        lv_snprintf(buf, sizeof(buf),
                    "   LVGL-1 [0] %" LV_PRIu32 ".%06" LV_PRIu32 ": tracing_mark_write: %c|1|%s\n",
                    sec,
                    usec,
                    item->tag,
                    item->func);
#endif
        profiler_ctx->config.flush_cb(buf);
    }
}

#endif /*LV_USE_PROFILER_BUILTIN*/