luat_lib_profiler.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include "luat_base.h"
  2. #include "luat_malloc.h"
  3. #include "luat_malloc.h"
  4. #include "luat_timer.h"
  5. #include "luat_profiler.h"
  6. #define LUAT_LOG_TAG "PRO"
  7. #include "luat_log.h"
  8. int luat_profiler_memdebug;
  9. luat_profiler_mem_t profiler_memregs[LUAT_PROFILER_MEMDEBUG_ADDR_COUNT];
  10. static int l_profiler_start(lua_State *L) {
  11. (void)L;
  12. luat_profiler_start();
  13. return 0;
  14. }
  15. static int l_profiler_stop(lua_State *L) {
  16. (void)L;
  17. luat_profiler_stop();
  18. return 0;
  19. }
  20. static int l_profiler_print(lua_State *L) {
  21. (void)L;
  22. luat_profiler_print();
  23. return 0;
  24. }
  25. static int l_profiler_memdebug(lua_State *L) {
  26. luat_profiler_memdebug = lua_toboolean(L, 1);
  27. LLOGD("memdebug %d", luat_profiler_memdebug);
  28. size_t total;
  29. size_t used;
  30. size_t max_used;
  31. luat_meminfo_luavm(&total, &used, &max_used);
  32. LLOGD("mem.lua %d %d %d", total, used, max_used);
  33. luat_meminfo_sys(&total, &used, &max_used);
  34. LLOGD("mem.sys %d %d %d", total, used, max_used);
  35. if (luat_profiler_memdebug == 0) {
  36. for (size_t i = 0; i < LUAT_PROFILER_MEMDEBUG_ADDR_COUNT; i++)
  37. {
  38. if (profiler_memregs[i].addr) {
  39. LLOGD("leak %08X %d", profiler_memregs[i].addr, profiler_memregs[i].len);
  40. }
  41. }
  42. }
  43. return 0;
  44. }
  45. #include "rotable2.h"
  46. static const rotable_Reg_t reg_profiler[] =
  47. {
  48. { "start" , ROREG_FUNC(l_profiler_start)},
  49. { "stop" , ROREG_FUNC(l_profiler_stop)},
  50. { "print", ROREG_FUNC(l_profiler_print)},
  51. { "memdebug", ROREG_FUNC(l_profiler_memdebug)},
  52. { NULL, ROREG_INT(0)}
  53. };
  54. LUAMOD_API int luaopen_profiler( lua_State *L ) {
  55. luat_newlib2(L, reg_profiler);
  56. return 1;
  57. }