Skip to content

Commit

Permalink
Profiler: Add MicroPython module for accessing probe data.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gadgetoid committed Sep 19, 2023
1 parent 453e256 commit 853c0a7
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 1 deletion.
9 changes: 8 additions & 1 deletion micropython/modules/profiler/micropython.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ add_library(usermod_${MOD_NAME} INTERFACE)

target_sources(usermod_${MOD_NAME} INTERFACE
${CMAKE_CURRENT_LIST_DIR}/${MOD_NAME}.cpp
${CMAKE_CURRENT_LIST_DIR}/${MOD_NAME}.c
)

target_include_directories(usermod_${MOD_NAME} INTERFACE
Expand All @@ -14,4 +15,10 @@ target_compile_definitions(usermod_${MOD_NAME} INTERFACE
MODULE_${MOD_NAME_UPPER}_ENABLED=1
)

target_link_libraries(usermod INTERFACE usermod_${MOD_NAME})
target_link_libraries(usermod INTERFACE usermod_${MOD_NAME})

set_source_files_properties(
${CMAKE_CURRENT_LIST_DIR}/profiler.c
PROPERTIES COMPILE_FLAGS
"-Wno-discarded-qualifiers"
)
24 changes: 24 additions & 0 deletions micropython/modules/profiler/profiler.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "profiler.h"

STATIC MP_DEFINE_CONST_FUN_OBJ_0(profiler_get_probes_obj, profiler_get_probes);
STATIC MP_DEFINE_CONST_FUN_OBJ_0(profiler_reset_obj, profiler_reset);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(profiler_get_probe_obj, profiler_get_probe);

STATIC const mp_map_elem_t profiler_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_profiler) },
{ MP_ROM_QSTR(MP_QSTR_get_probes), MP_ROM_PTR(&profiler_get_probes_obj) },
{ MP_ROM_QSTR(MP_QSTR_get_probe), MP_ROM_PTR(&profiler_get_probe_obj) },
{ MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&profiler_reset_obj) },
};
STATIC MP_DEFINE_CONST_DICT(mp_module_profiler_globals, profiler_globals_table);

const mp_obj_module_t profiler_user_cmodule = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&mp_module_profiler_globals,
};

#if MICROPY_VERSION <= 70144
MP_REGISTER_MODULE(MP_QSTR_profiler, profiler_user_cmodule, MODULE_PROFILER_ENABLED);
#else
MP_REGISTER_MODULE(MP_QSTR_profiler, profiler_user_cmodule);
#endif
70 changes: 70 additions & 0 deletions micropython/modules/profiler/profiler.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "profiler.hpp"
#include <cstring>

namespace pimoroni {
void Probe::reset() {
Expand Down Expand Up @@ -35,6 +36,25 @@ namespace pimoroni {
return new_probe;
}

void reset() {
Probe *p = probes[0];
for(auto x = 0u; x < probe_count; x++) {
p->reset();
p++;
}
}

Probe* get_probe(const char *name) {
Probe *p = probes[0];
for(auto x = 0u; x < probe_count; x++) {
if (strncmp(name, p->name.data(), p->name.length()) == 0) {
return p;
}
p++;
}
return nullptr;
}

Probe** get_probes() {
return probes;
}
Expand All @@ -43,4 +63,54 @@ namespace pimoroni {
return probe_count;
}
}
}

extern "C" {
#include "profiler.h"

mp_obj_t profiler_reset() {
pimoroni::Profiler::reset();

return mp_const_none;
}

mp_obj_t profiler_get_probe(mp_obj_t name) {
if(mp_obj_is_str_or_bytes(name)) {
GET_STR_DATA_LEN(name, str, str_len);
pimoroni::Probe *probe = pimoroni::Profiler::get_probe((const char *)str);

mp_obj_t tuple_probe[3] = {
mp_obj_new_str(probe->name.data(), probe->name.length()),
mp_obj_new_int(probe->get_avg_runtime()),
mp_obj_new_int(probe->get_exec_count())
};

return mp_obj_new_tuple(3, tuple_probe);
}
return mp_const_none;
}

mp_obj_t profiler_get_probes() {
pimoroni::Probe* probe = pimoroni::Profiler::get_probes()[0];
uint16_t count = pimoroni::Profiler::count();

mp_obj_t list_probes[count];

for(auto x = 0u; x < count; x++){
//mp_printf(&mp_plat_print, "Probe: %s\n", probe->name.data());
//mp_printf(&mp_plat_print, " Avg (us): %lu\n", probe->get_avg_runtime());
//mp_printf(&mp_plat_print, " Runs: %lu\n", probe->get_exec_count());

mp_obj_t tuple_probe[3] = {
mp_obj_new_str(probe->name.data(), probe->name.length()),
mp_obj_new_int(probe->get_avg_runtime()),
mp_obj_new_int(probe->get_exec_count())
};

list_probes[x] = mp_obj_new_tuple(3, tuple_probe);
probe++;
}
return mp_obj_new_list(count, list_probes);
}

}
6 changes: 6 additions & 0 deletions micropython/modules/profiler/profiler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "py/runtime.h"
#include "py/objstr.h"

extern mp_obj_t profiler_get_probes();
extern mp_obj_t profiler_get_probe(mp_obj_t name);
extern mp_obj_t profiler_reset();

0 comments on commit 853c0a7

Please sign in to comment.