From ca48b8c2623ff899159d040ac7c78a90466953a4 Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Sat, 17 Aug 2024 22:02:37 +0300 Subject: [PATCH] misc/common: Fix profiling macro. Now it can be called multiple times in the same function and can be nested. --- src/omv/common/omv_common.h | 9 ++++----- src/omv/imlib/draw.c | 4 ++-- src/omv/imlib/jpegd.c | 4 ++-- src/omv/imlib/jpege.c | 4 ++-- src/omv/imlib/png.c | 8 ++++---- src/omv/modules/py_ml.c | 5 +++++ src/omv/ports/stm32/jpeg.c | 8 ++++---- src/omv/ports/stm32/omv_gpu.c | 4 ++-- 8 files changed, 25 insertions(+), 21 deletions(-) diff --git a/src/omv/common/omv_common.h b/src/omv/common/omv_common.h index 2c07b5065..6349d6866 100644 --- a/src/omv/common/omv_common.h +++ b/src/omv/common/omv_common.h @@ -51,13 +51,12 @@ #include #include "py/mphal.h" #define OMV_CONCATENATE_DETAIL(x, y) x##y -#define OMV_CONCATENATE(x, y) OMV_CONCATENATE_DETAIL(x, y) - -#define OMV_PROFILE_START(F) mp_uint_t OMV_CONCATENATE(F, _ticks_start_) = mp_hal_ticks_us() -#define OMV_PROFILE_END(F) printf("%s %u us\n", F, mp_hal_ticks_us() - OMV_CONCATENATE(F, _ticks_start_)) +#define OMV_CONCATENATE(x, y) OMV_CONCATENATE_DETAIL(x, y) +#define OMV_PROFILE_START(F) mp_uint_t OMV_CONCATENATE(_ticks_start_, F) = mp_hal_ticks_us() +#define OMV_PROFILE_PRINT(F) printf("%s:%s %u us\n", __FUNCTION__, #F, mp_hal_ticks_us() - OMV_CONCATENATE(_ticks_start_, F)) #else #define OMV_PROFILE_START(F) -#define OMV_PROFILE_END(F) +#define OMV_PROFILE_PRINT(F) #endif #endif //__OMV_COMMON_H__ diff --git a/src/omv/imlib/draw.c b/src/omv/imlib/draw.c index d20254cdf..1ef7bdc6a 100644 --- a/src/omv/imlib/draw.c +++ b/src/omv/imlib/draw.c @@ -2843,7 +2843,7 @@ void imlib_draw_image(image_t *dst_img, imlib_draw_row_callback_t callback, void *callback_arg, void *dst_row_override) { - OMV_PROFILE_START(__FUNCTION__); + OMV_PROFILE_START(); int dst_delta_x = 1; // positive direction if (x_scale < 0.f) { // flip X @@ -5518,7 +5518,7 @@ exit_cleanup: if (&new_src_img == src_img) { fb_free(); } - OMV_PROFILE_END(__FUNCTION__); + OMV_PROFILE_PRINT(); } #ifdef IMLIB_ENABLE_FLOOD_FILL diff --git a/src/omv/imlib/jpegd.c b/src/omv/imlib/jpegd.c index 7e50cce31..3ae6c612e 100644 --- a/src/omv/imlib/jpegd.c +++ b/src/omv/imlib/jpegd.c @@ -2841,7 +2841,7 @@ static int DecodeJPEG(JPEGIMAGE *pJPEG) { } void jpeg_decompress(image_t *dst, image_t *src) { - OMV_PROFILE_START(__FUNCTION__); + OMV_PROFILE_START(); JPEGIMAGE jpg; // Supports decoding baseline JPEGs only. @@ -2882,6 +2882,6 @@ void jpeg_decompress(image_t *dst, image_t *src) { mp_raise_msg(&mp_type_OSError, MP_ERROR_TEXT("JPEG decoder failed.")); } - OMV_PROFILE_END(__FUNCTION__); + OMV_PROFILE_PRINT(); } #endif diff --git a/src/omv/imlib/jpege.c b/src/omv/imlib/jpege.c index fcd75dc6b..11ae10a73 100644 --- a/src/omv/imlib/jpege.c +++ b/src/omv/imlib/jpege.c @@ -1567,7 +1567,7 @@ static void jpeg_write_headers(jpeg_buf_t *jpeg_buf, int w, int h, int bpp, jpeg } bool jpeg_compress(image_t *src, image_t *dst, int quality, bool realloc, jpeg_subsampling_t subsampling) { - OMV_PROFILE_START(__FUNCTION__); + OMV_PROFILE_START(); if (!dst->data) { uint32_t size = 0; @@ -1898,7 +1898,7 @@ bool jpeg_compress(image_t *src, image_t *dst, int quality, bool realloc, jpeg_s dst->size = jpeg_buf.idx; dst->data = jpeg_buf.buf; - OMV_PROFILE_END(__FUNCTION__); + OMV_PROFILE_PRINT(); return false; } diff --git a/src/omv/imlib/png.c b/src/omv/imlib/png.c index f71555328..6da505e57 100644 --- a/src/omv/imlib/png.c +++ b/src/omv/imlib/png.c @@ -105,7 +105,7 @@ unsigned lodepng_convert_cb(unsigned char *out, const unsigned char *in, #if defined(IMLIB_ENABLE_PNG_ENCODER) bool png_compress(image_t *src, image_t *dst) { - OMV_PROFILE_START(__FUNCTION__); + OMV_PROFILE_START(); if (src->is_compressed) { return true; @@ -177,14 +177,14 @@ bool png_compress(image_t *src, image_t *dst) { // free fb_alloc() memory used for umm_init_x(). fb_free(); // umm_init_x(); } - OMV_PROFILE_END(__FUNCTION__); + OMV_PROFILE_PRINT(); return false; } #endif // IMLIB_ENABLE_PNG_ENCODER #if defined(IMLIB_ENABLE_PNG_DECODER) void png_decompress(image_t *dst, image_t *src) { - OMV_PROFILE_START(__FUNCTION__); + OMV_PROFILE_START(); umm_init_x(fb_avail()); LodePNGState state; @@ -226,7 +226,7 @@ void png_decompress(image_t *dst, image_t *src) { // free fb_alloc() memory used for umm_init_x(). fb_free(); // umm_init_x(); - OMV_PROFILE_END(__FUNCTION__); + OMV_PROFILE_PRINT(); } #endif // IMLIB_ENABLE_PNG_DECODER #endif // IMLIB_ENABLE_PNG_ENCODER || IMLIB_ENABLE_PNG_DECODER diff --git a/src/omv/modules/py_ml.c b/src/omv/modules/py_ml.c index d6c2ae8b9..4cd2d3cf4 100644 --- a/src/omv/modules/py_ml.c +++ b/src/omv/modules/py_ml.c @@ -226,8 +226,13 @@ static mp_obj_t py_ml_model_predict(uint n_args, const mp_obj_t *pos_args, mp_ma mp_raise_msg(&mp_type_ValueError, MP_ERROR_TEXT("Unsupported input type. Expected a list")); } + OMV_PROFILE_START(preprocess); py_ml_process_input(model, pos_args[1]); + OMV_PROFILE_PRINT(preprocess); + + OMV_PROFILE_START(inference); ml_backend_run_inference(model); + OMV_PROFILE_PRINT(inference); mp_obj_t output = py_ml_process_output(model); diff --git a/src/omv/ports/stm32/jpeg.c b/src/omv/ports/stm32/jpeg.c index a1bf294f4..c05a5dab0 100644 --- a/src/omv/ports/stm32/jpeg.c +++ b/src/omv/ports/stm32/jpeg.c @@ -106,7 +106,7 @@ static void jpeg_compress_data_ready(JPEG_HandleTypeDef *hjpeg, uint8_t *pDataOu } bool jpeg_compress(image_t *src, image_t *dst, int quality, bool realloc, jpeg_subsampling_t subsampling) { - OMV_PROFILE_START(__FUNCTION__); + OMV_PROFILE_START(); HAL_JPEG_RegisterGetDataCallback(&JPEG_state.jpeg_descr, jpeg_compress_get_data); HAL_JPEG_RegisterDataReadyCallback(&JPEG_state.jpeg_descr, jpeg_compress_data_ready); @@ -365,7 +365,7 @@ exit_cleanup: fb_free(); // mcu_row_buffer (after DMA is aborted) - OMV_PROFILE_END(__FUNCTION__); + OMV_PROFILE_PRINT(); return jpeg_overflow; } @@ -397,7 +397,7 @@ static void jpeg_decompress_data_ready(JPEG_HandleTypeDef *hjpeg, uint8_t *pData } void jpeg_decompress(image_t *dst, image_t *src) { - OMV_PROFILE_START(__FUNCTION__); + OMV_PROFILE_START(); // Verify the jpeg image is not a non-baseline jpeg image and check that is has // valid headers up to the start-of-scan header (which cannot be trivially walked). @@ -752,7 +752,7 @@ exit_cleanup: fb_free(); // JPEG_state.jpeg_descr.pJpegInBuffPtr (after DMA is aborted) } - OMV_PROFILE_END(__FUNCTION__); + OMV_PROFILE_PRINT(); } void imlib_hardware_jpeg_init() { diff --git a/src/omv/ports/stm32/omv_gpu.c b/src/omv/ports/stm32/omv_gpu.c index 2729d19c8..c767867c4 100644 --- a/src/omv/ports/stm32/omv_gpu.c +++ b/src/omv/ports/stm32/omv_gpu.c @@ -32,7 +32,7 @@ int omv_gpu_draw_image(image_t *src_img, const uint16_t *color_palette, const uint8_t *alpha_palette, image_hint_t hint) { - OMV_PROFILE_START(__FUNCTION__); + OMV_PROFILE_START(); // DMA2D can only draw on RGB565 buffers and the destination/source buffers must be accessible by DMA. if ((dst_img->pixfmt != PIXFORMAT_RGB565) || (!DMA_BUFFER(dst_img->data)) || (!DMA_BUFFER(src_img->data))) { @@ -211,7 +211,7 @@ int omv_gpu_draw_image(image_t *src_img, fb_free(); // clut } - OMV_PROFILE_END(__FUNCTION__); + OMV_PROFILE_PRINT(); return 0; } #endif // (OMV_GPU_ENABLE == 1)