feat(profiler): add posix porting (#8924)

This commit is contained in:
VIFEX
2025-09-23 10:20:28 +08:00
committed by GitHub
parent 7171f17fdb
commit 173cae5456
8 changed files with 161 additions and 40 deletions
+4
View File
@@ -1619,6 +1619,10 @@ menu "LVGL configuration"
bool "Enable built-in profiler by default" bool "Enable built-in profiler by default"
depends on LV_USE_PROFILER_BUILTIN depends on LV_USE_PROFILER_BUILTIN
default y default y
config LV_USE_PROFILER_BUILTIN_POSIX
bool "Enable POSIX profiler port"
depends on LV_USE_PROFILER_BUILTIN
default n
config LV_PROFILER_INCLUDE config LV_PROFILER_INCLUDE
string "Header to include for the profiler" string "Header to include for the profiler"
default "lvgl/src/misc/lv_profiler_builtin.h" default "lvgl/src/misc/lv_profiler_builtin.h"
+1 -40
View File
@@ -32,51 +32,12 @@ Configure profiler
To enable the profiler, set :c:macro:`LV_USE_PROFILER` in ``lv_conf.h`` and configure the following options: To enable the profiler, set :c:macro:`LV_USE_PROFILER` in ``lv_conf.h`` and configure the following options:
1. Enable the built-in profiler functionality by setting :c:macro:`LV_USE_PROFILER_BUILTIN`. 1. Enable the built-in profiler functionality by setting :c:macro:`LV_USE_PROFILER_BUILTIN`. If you have POSIX environment support, you can enable :c:macro:`LV_USE_PROFILER_BUILTIN_POSIX`.
2. Buffer configuration: Set the value of :c:macro:`LV_PROFILER_BUILTIN_BUF_SIZE` to configure the buffer size. A larger buffer can store more trace event information, reducing interference with rendering. However, it also results in higher memory consumption. 2. Buffer configuration: Set the value of :c:macro:`LV_PROFILER_BUILTIN_BUF_SIZE` to configure the buffer size. A larger buffer can store more trace event information, reducing interference with rendering. However, it also results in higher memory consumption.
3. Timestamp configuration: LVGL uses the :cpp:func:`lv_tick_get` function with a precision of 1ms by default to obtain timestamps when events occur. Therefore, it cannot accurately measure intervals below 1ms. If your system environment can provide higher precision (e.g., 1us), you can configure the profiler as follows: 3. Timestamp configuration: LVGL uses the :cpp:func:`lv_tick_get` function with a precision of 1ms by default to obtain timestamps when events occur. Therefore, it cannot accurately measure intervals below 1ms. If your system environment can provide higher precision (e.g., 1us), you can configure the profiler as follows:
- Recommended configuration in **UNIX** environments:
.. code-block:: c
#include <sys/syscall.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
static uint64_t my_get_tick_cb(void)
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ts.tv_sec * 1000000000 + ts.tv_nsec;
}
static int my_get_tid_cb(void)
{
return (int)syscall(SYS_gettid);
}
static int my_get_cpu_cb(void)
{
int cpu_id = 0;
syscall(SYS_getcpu, &cpu_id, NULL);
return cpu_id;
}
void my_profiler_init(void)
{
lv_profiler_builtin_config_t config;
lv_profiler_builtin_config_init(&config);
config.tick_per_sec = 1000000000; /* One second is equal to 1000000000 nanoseconds */
config.tick_get_cb = my_get_tick_cb;
config.tid_get_cb = my_get_tid_cb;
config.cpu_get_cb = my_get_cpu_cb;
lv_profiler_builtin_init(&config);
}
- Recommended configuration in **Arduino** environments: - Recommended configuration in **Arduino** environments:
.. code-block:: c .. code-block:: c
+1
View File
@@ -1094,6 +1094,7 @@
/** Default profiler trace buffer size */ /** Default profiler trace buffer size */
#define LV_PROFILER_BUILTIN_BUF_SIZE (16 * 1024) /**< [bytes] */ #define LV_PROFILER_BUILTIN_BUF_SIZE (16 * 1024) /**< [bytes] */
#define LV_PROFILER_BUILTIN_DEFAULT_ENABLE 1 #define LV_PROFILER_BUILTIN_DEFAULT_ENABLE 1
#define LV_USE_PROFILER_BUILTIN_POSIX 0 /**< Enable POSIX profiler port */
#endif #endif
/** Header to include for profiler */ /** Header to include for profiler */
+7
View File
@@ -3445,6 +3445,13 @@
#define LV_PROFILER_BUILTIN_DEFAULT_ENABLE 1 #define LV_PROFILER_BUILTIN_DEFAULT_ENABLE 1
#endif #endif
#endif #endif
#ifndef LV_USE_PROFILER_BUILTIN_POSIX
#ifdef CONFIG_LV_USE_PROFILER_BUILTIN_POSIX
#define LV_USE_PROFILER_BUILTIN_POSIX CONFIG_LV_USE_PROFILER_BUILTIN_POSIX
#else
#define LV_USE_PROFILER_BUILTIN_POSIX 0 /**< Enable POSIX profiler port */
#endif
#endif
#endif #endif
/** Header to include for profiler */ /** Header to include for profiler */
+4
View File
@@ -203,9 +203,13 @@ void lv_init(void)
#endif #endif
#if LV_USE_PROFILER && LV_USE_PROFILER_BUILTIN #if LV_USE_PROFILER && LV_USE_PROFILER_BUILTIN
#if LV_USE_PROFILER_BUILTIN_POSIX
lv_profiler_builtin_posix_init();
#else
lv_profiler_builtin_config_t profiler_config; lv_profiler_builtin_config_t profiler_config;
lv_profiler_builtin_config_init(&profiler_config); lv_profiler_builtin_config_init(&profiler_config);
lv_profiler_builtin_init(&profiler_config); lv_profiler_builtin_init(&profiler_config);
#endif
#endif #endif
lv_os_init(); lv_os_init();
+134
View File
@@ -0,0 +1,134 @@
/**
* @file lv_profiler_builtin_posix.c
*
*/
/*********************
* INCLUDES
*********************/
#include "lv_profiler_builtin_private.h"
#if LV_USE_PROFILER && LV_USE_PROFILER_BUILTIN && LV_USE_PROFILER_BUILTIN_POSIX
#if defined(_WIN32)
#include <windows.h>
#else
#include <pthread.h>
#endif
#include <stdio.h>
#include <time.h>
#if defined(__linux__)
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
#endif
/*********************
* DEFINES
*********************/
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
static uint64_t tick_get_cb(void);
static void flush_cb(const char * buf);
static int tid_get_cb(void);
static int cpu_get_cb(void);
/**********************
* STATIC VARIABLES
**********************/
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
void lv_profiler_builtin_posix_init(void)
{
lv_profiler_builtin_config_t config;
lv_profiler_builtin_config_init(&config);
/* One second is equal to 1000000000 nanoseconds */
config.tick_per_sec = 1000000000;
config.tick_get_cb = tick_get_cb;
config.flush_cb = flush_cb;
config.tid_get_cb = tid_get_cb;
config.cpu_get_cb = cpu_get_cb;
lv_profiler_builtin_init(&config);
}
/**********************
* STATIC FUNCTIONS
**********************/
static uint64_t tick_get_cb(void)
{
#if defined(_WIN32)
static LARGE_INTEGER frequency = {0};
LARGE_INTEGER counter;
if(frequency.QuadPart == 0) {
if(!QueryPerformanceFrequency(&frequency)) {
fprintf(stderr, "QueryPerformanceFrequency failed\n");
return 0;
}
}
if(!QueryPerformanceCounter(&counter)) {
fprintf(stderr, "QueryPerformanceCounter failed\n");
return 0;
}
/* Convert counter to nanoseconds */
return counter.QuadPart * 1000000000ULL / frequency.QuadPart;
#else
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (uint64_t)ts.tv_sec * 1000000000ULL + (uint64_t)ts.tv_nsec;
#endif
}
static void flush_cb(const char * buf)
{
printf("%s", buf);
}
static int tid_get_cb(void)
{
#if defined(__linux__)
return (int)syscall(SYS_gettid);
#elif defined(_WIN32)
return (int)GetCurrentThreadId();
#else
return (int)pthread_self();
#endif
}
static int cpu_get_cb(void)
{
#if defined(__linux__)
unsigned cpu;
int result = syscall(SYS_getcpu, &cpu, NULL, NULL);
if(result < 0) {
fprintf(stderr, "getcpu failed\n");
return -1;
}
return (int)cpu;
#else
return 0;
#endif
}
#endif
+9
View File
@@ -43,6 +43,15 @@ struct _lv_profiler_builtin_config_t {
* GLOBAL PROTOTYPES * GLOBAL PROTOTYPES
**********************/ **********************/
#if LV_USE_PROFILER_BUILTIN_POSIX
/**
* Initialize the built-in profiler with POSIX functions.
*/
void lv_profiler_builtin_posix_init(void);
#endif /* LV_USE_PROFILER_BUILTIN_POSIX */
/********************** /**********************
* MACROS * MACROS
**********************/ **********************/
+1
View File
@@ -108,6 +108,7 @@
#define LV_USE_SVG_DEBUG 1 #define LV_USE_SVG_DEBUG 1
#define LV_USE_PROFILER 1 #define LV_USE_PROFILER 1
#define LV_PROFILER_INCLUDE "lv_profiler_builtin.h" #define LV_PROFILER_INCLUDE "lv_profiler_builtin.h"
#define LV_USE_PROFILER_BUILTIN_POSIX 1
#define LV_USE_GRIDNAV 1 #define LV_USE_GRIDNAV 1
#define LV_USE_XML 1 #define LV_USE_XML 1
#define LV_USE_TRANSLATION 1 #define LV_USE_TRANSLATION 1