From ef34126913978c7cd6e5b0831f78ac8355f053f0 Mon Sep 17 00:00:00 2001 From: William Wilgus Date: Mon, 5 Oct 2020 03:28:02 -0400 Subject: lua add better memory stats lua gives you a memory used number that only reflects the current allocations if fact it doesn't even give you a way to get the amount of ram free rb.mem_stats() seeks to fill this gap by marking the memory allocated for lua with a sentinel value which can later be checked to get a high water mark of the ram used by lua and a pretty good idea of how much ram is available Also includes an example script usage: used, allocd, free = rb.mem_stats() Change-Id: Ia282869f989848324d7d88c7df4827fdbce4fb4e --- apps/plugins/lua/rocklib.c | 16 +++++++++++ apps/plugins/lua/tlsf_helper.c | 55 +++++++++++++++++++++++++++++++++++-- apps/plugins/lua_scripts/memchk.lua | 32 +++++++++++++++++++++ 3 files changed, 100 insertions(+), 3 deletions(-) create mode 100644 apps/plugins/lua_scripts/memchk.lua diff --git a/apps/plugins/lua/rocklib.c b/apps/plugins/lua/rocklib.c index 5f0143efbb..8921c0a4c3 100644 --- a/apps/plugins/lua/rocklib.c +++ b/apps/plugins/lua/rocklib.c @@ -53,6 +53,8 @@ * * ----------------------------- */ +extern size_t rock_get_allocated_bytes(void); /* tlsf_helper.c */ +extern size_t rock_get_unused_bytes(void); #define RB_WRAP(func) static int rock_##func(lua_State UNUSED_ATTR *L) #define SIMPLE_VOID_WRAPPER(func) RB_WRAP(func) { (void)L; func(); return 0; } @@ -931,6 +933,19 @@ RB_WRAP(show_logo) return 0; } +RB_WRAP(mem_stats) +{ + /* used, allocd, free = rb.mem_stats() */ + /* note free is the high watermark */ + size_t allocd = rock_get_allocated_bytes(); + size_t free = rock_get_unused_bytes(); + + lua_pushinteger(L, allocd - free); + lua_pushinteger(L, allocd); + lua_pushinteger(L, free); + return 3; +} + #define RB_FUNC(func) {#func, rock_##func} #define RB_ALIAS(name, func) {name, rock_##func} static const luaL_Reg rocklib[] = @@ -1015,6 +1030,7 @@ static const luaL_Reg rocklib[] = /* MISC */ RB_FUNC(restart_lua), RB_FUNC(show_logo), + RB_FUNC(mem_stats), {NULL, NULL} }; diff --git a/apps/plugins/lua/tlsf_helper.c b/apps/plugins/lua/tlsf_helper.c index 097d39c8e4..52ef269bcd 100644 --- a/apps/plugins/lua/tlsf_helper.c +++ b/apps/plugins/lua/tlsf_helper.c @@ -21,16 +21,62 @@ #include "plugin.h" #include #include "lua.h" +static const unsigned int sentinel = 0xBA5EFAC7; +#define SENTINEL(n) (sentinel ^ (n)) -void *get_new_area(size_t *size) +static char *pluginbuf_ptr = NULL; +static size_t pluginbuf_size = 0; +static char *audiobuf_ptr = NULL; +static size_t audiobuf_size = 0; + +static void set_sentinel(void* buf, size_t size) +{ + size_t i; + unsigned int *b = (int*) buf; + for(i = 0; i < size / sizeof(sentinel); i++) + *b++ = SENTINEL(i); +} + +static size_t check_sentinel(void* buf, size_t size) +{ + const size_t sz = size / sizeof(sentinel); + size_t unused = 0; + size_t i; + unsigned int *b = (int*) buf; + for(i = 0; i < sz; i++) + if (b[i] == SENTINEL(i)) + { + unused++; + while(++i < sz && b[i] == SENTINEL(i) && ++unused) + ;; + } + return unused * sizeof(sentinel); +} + +size_t rock_get_allocated_bytes(void) { - static char *pluginbuf_ptr = NULL; - static char *audiobuf_ptr = NULL; + return pluginbuf_size + audiobuf_size; +} + +size_t rock_get_unused_bytes(void) +{ + size_t unused = 0; + if (pluginbuf_size) + unused += check_sentinel(pluginbuf_ptr, pluginbuf_size); + if (audiobuf_size) + unused += check_sentinel(audiobuf_ptr, audiobuf_size); + return unused; +} +void *get_new_area(size_t *size) +{ if (pluginbuf_ptr == NULL) { pluginbuf_ptr = rb->plugin_get_buffer(size); + pluginbuf_size = *size; + set_sentinel(pluginbuf_ptr, pluginbuf_size); + /* kill tlsf signature if any */ memset(pluginbuf_ptr, 0, 4); @@ -43,6 +89,9 @@ void *get_new_area(size_t *size) /* grab audiobuffer */ audiobuf_ptr = rb->plugin_get_audio_buffer(size); + audiobuf_size = *size; + set_sentinel(audiobuf_ptr, audiobuf_size); + return audiobuf_ptr; } diff --git a/apps/plugins/lua_scripts/memchk.lua b/apps/plugins/lua_scripts/memchk.lua new file mode 100644 index 0000000000..f7619fe3dd --- /dev/null +++ b/apps/plugins/lua_scripts/memchk.lua @@ -0,0 +1,32 @@ +--[[ + __________ __ ___. + Open \______ \ ____ ____ | | _\_ |__ _______ ___ + Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + \/ \/ \/ \/ \/ + $Id$ + Example Lua Memory Use + Copyright (C) 2020 William Wilgus + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + KIND, either express or implied. +]]-- + +local used, allocd, free = rb.mem_stats() +local lu = collectgarbage("count") +local fmt = function(t, v) return string.format("%s: %d Kb\n", t, v /1024) end + +-- this is how lua recommends to concat strings rather than .. +local s_t = {} +s_t[1] = "rockbox:\n" +s_t[2] = fmt("Used ", used) +s_t[3] = fmt("Allocd ", allocd) +s_t[4] = fmt("Free ", free) +s_t[5] = "\nlua:\n" +s_t[6] = fmt("Used", lu * 1024) +s_t[7] = "\n\nNote that the rockbox used count is a high watermark" +rb.splash_scroller(10 * rb.HZ, table.concat(s_t)) -- cgit v1.2.3