Enhancements on wasm function execution time statistic (#2985)

Enhance the statistic of wasm function execution time, or the performance
profiling feature:
- Add os_time_thread_cputime_us() to get the cputime of a thread,
  and use it to calculate the execution time of a wasm function
- Support the statistic of the children execution time of a function,
  and dump it in wasm_runtime_dump_perf_profiling
- Expose two APIs:
  wasm_runtime_sum_wasm_exec_time
  wasm_runtime_get_wasm_func_exec_time

And rename os_time_get_boot_microsecond to os_time_get_boot_us.
This commit is contained in:
liang.he
2024-01-17 09:51:54 +08:00
committed by GitHub
parent 25ccc9f2d5
commit 5c8b8a17a6
24 changed files with 336 additions and 86 deletions

View File

@ -6,7 +6,14 @@
#include "platform_api_vmcore.h"
uint64
os_time_get_boot_microsecond()
os_time_get_boot_us()
{
return (uint64)aos_now_ms() * 1000;
}
uint64
os_time_thread_cputime_us(void)
{
/* FIXME if u know the right api */
return os_time_get_boot_us();
}

View File

@ -6,8 +6,15 @@
#include "platform_api_vmcore.h"
uint64
os_time_get_boot_microsecond()
os_time_get_boot_us()
{
TickType_t ticks = xTaskGetTickCount();
return (uint64)1000 * 1000 / configTICK_RATE_HZ * ticks;
}
uint64
os_time_thread_cputime_us(void)
{
/* FIXME if u know the right api */
return os_time_get_boot_us();
}

View File

@ -6,7 +6,7 @@
#include "platform_api_vmcore.h"
uint64
os_time_get_boot_microsecond()
os_time_get_boot_us()
{
struct timespec ts;
if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
@ -15,3 +15,14 @@ os_time_get_boot_microsecond()
return ((uint64)ts.tv_sec) * 1000 * 1000 + ((uint64)ts.tv_nsec) / 1000;
}
uint64
os_time_thread_cputime_us()
{
struct timespec ts;
if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts) != 0) {
return 0;
}
return ((uint64)ts.tv_sec) * 1000 * 1000 + ((uint64)ts.tv_nsec) / 1000;
}

View File

@ -36,11 +36,18 @@ os_vprintf(const char *format, va_list ap)
}
uint64
os_time_get_boot_microsecond(void)
os_time_get_boot_us(void)
{
return (uint64)esp_timer_get_time();
}
uint64
os_time_thread_cputime_us(void)
{
/* FIXME if u know the right api */
return os_time_get_boot_us();
}
uint8 *
os_thread_get_stack_boundary(void)
{

View File

@ -64,7 +64,13 @@ os_vprintf(const char *format, va_list ap);
* Get microseconds after boot.
*/
uint64
os_time_get_boot_microsecond(void);
os_time_get_boot_us(void);
/**
* Get thread-specific CPU-time clock in microseconds
*/
uint64
os_time_thread_cputime_us(void);
/**
* Get current thread id.

View File

@ -26,7 +26,7 @@ ocall_clock_nanosleep(int *p_ret, unsigned clock_id, int flags,
const void *rem_buf, unsigned int rem_buf_size);
uint64
os_time_get_boot_microsecond()
os_time_get_boot_us()
{
#ifndef SGX_DISABLE_WASI
struct timespec ts;
@ -40,6 +40,21 @@ os_time_get_boot_microsecond()
#endif
}
uint64
os_time_thread_cputime_us(void)
{
#ifndef SGX_DISABLE_WASI
struct timespec ts;
if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts) != 0) {
return 0;
}
return ((uint64)ts.tv_sec) * 1000 * 1000 + ((uint64)ts.tv_nsec) / 1000;
#else
return 0;
#endif
}
#ifndef SGX_DISABLE_WASI
int

View File

@ -10,25 +10,32 @@
#if IS_USED(MODULE_ZTIMER64_USEC)
uint64
os_time_get_boot_microsecond()
os_time_get_boot_us()
{
return ztimer64_now(ZTIMER64_USEC);
}
#elif IS_USED(MODULE_ZTIMER64_MSEC)
uint64
os_time_get_boot_microsecond()
os_time_get_boot_us()
{
return ztimer64_now(ZTIMER64_MSEC) * 1000;
}
#else
#ifdef __GNUC__
__attribute__((weak)) uint64
os_time_get_boot_microsecond();
os_time_get_boot_us();
#endif
uint64
os_time_get_boot_microsecond()
os_time_get_boot_us()
{
static uint64_t times;
return ++times;
}
#endif
uint64
os_time_thread_cputime_us(void)
{
/* FIXME if u know the right api */
return os_time_get_boot_us();
}

View File

@ -120,13 +120,20 @@ os_vprintf(const char *format, va_list ap)
}
uint64
os_time_get_boot_microsecond(void)
os_time_get_boot_us(void)
{
uint64 ret = rt_tick_get() * 1000;
ret /= RT_TICK_PER_SECOND;
return ret;
}
uint64
os_time_thread_cputime_us(void)
{
/* FIXME if u know the right api */
return os_time_get_boot_us();
}
korp_tid
os_self_thread(void)
{

View File

@ -6,7 +6,7 @@
#include "platform_api_vmcore.h"
uint64
os_time_get_boot_microsecond()
os_time_get_boot_us()
{
struct timespec ts;
#if defined(__MINGW32__)
@ -18,3 +18,10 @@ os_time_get_boot_microsecond()
return ((uint64)ts.tv_sec) * 1000 * 1000 + ((uint64)ts.tv_nsec) / 1000;
}
uint64
os_time_thread_cputime_us(void)
{
/* FIXME if u know the right api */
return os_time_get_boot_us();
}

View File

@ -6,7 +6,14 @@
#include "platform_api_vmcore.h"
uint64
os_time_get_boot_microsecond()
os_time_get_boot_us()
{
return k_uptime_get() * 1000;
}
uint64
os_time_thread_cputime_us(void)
{
/* FIXME if u know the right api */
return os_time_get_boot_us();
}

View File

@ -31,7 +31,7 @@ bh_log(LogLevel log_level, const char *file, int line, const char *fmt, ...)
self = os_self_thread();
usec = os_time_get_boot_microsecond();
usec = os_time_get_boot_us();
t = (uint32)(usec / 1000000) % (24 * 60 * 60);
h = t / (60 * 60);
t = t % (60 * 60);

View File

@ -38,7 +38,7 @@ struct _timer_ctx {
uint64
bh_get_tick_ms()
{
return os_time_get_boot_microsecond() / 1000;
return os_time_get_boot_us() / 1000;
}
uint32