summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWilliam Wilgus <wilgus.william@gmail.com>2020-10-05 03:28:02 -0400
committerWilliam Wilgus <wilgus.william@gmail.com>2020-10-05 11:53:27 -0400
commitef34126913978c7cd6e5b0831f78ac8355f053f0 (patch)
tree4a719e60a52d19031ed485d8c9e9fd64e446ef97
parent74258fca311b2d7e9d834ab8607f2bd326f67807 (diff)
downloadrockbox-ef34126913978c7cd6e5b0831f78ac8355f053f0.tar.gz
rockbox-ef34126913978c7cd6e5b0831f78ac8355f053f0.zip
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
-rw-r--r--apps/plugins/lua/rocklib.c16
-rw-r--r--apps/plugins/lua/tlsf_helper.c55
-rw-r--r--apps/plugins/lua_scripts/memchk.lua32
3 files changed, 100 insertions, 3 deletions
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 @@
53 * 53 *
54 * ----------------------------- 54 * -----------------------------
55 */ 55 */
56extern size_t rock_get_allocated_bytes(void); /* tlsf_helper.c */
57extern size_t rock_get_unused_bytes(void);
56 58
57#define RB_WRAP(func) static int rock_##func(lua_State UNUSED_ATTR *L) 59#define RB_WRAP(func) static int rock_##func(lua_State UNUSED_ATTR *L)
58#define SIMPLE_VOID_WRAPPER(func) RB_WRAP(func) { (void)L; func(); return 0; } 60#define SIMPLE_VOID_WRAPPER(func) RB_WRAP(func) { (void)L; func(); return 0; }
@@ -931,6 +933,19 @@ RB_WRAP(show_logo)
931 return 0; 933 return 0;
932} 934}
933 935
936RB_WRAP(mem_stats)
937{
938 /* used, allocd, free = rb.mem_stats() */
939 /* note free is the high watermark */
940 size_t allocd = rock_get_allocated_bytes();
941 size_t free = rock_get_unused_bytes();
942
943 lua_pushinteger(L, allocd - free);
944 lua_pushinteger(L, allocd);
945 lua_pushinteger(L, free);
946 return 3;
947}
948
934#define RB_FUNC(func) {#func, rock_##func} 949#define RB_FUNC(func) {#func, rock_##func}
935#define RB_ALIAS(name, func) {name, rock_##func} 950#define RB_ALIAS(name, func) {name, rock_##func}
936static const luaL_Reg rocklib[] = 951static const luaL_Reg rocklib[] =
@@ -1015,6 +1030,7 @@ static const luaL_Reg rocklib[] =
1015 /* MISC */ 1030 /* MISC */
1016 RB_FUNC(restart_lua), 1031 RB_FUNC(restart_lua),
1017 RB_FUNC(show_logo), 1032 RB_FUNC(show_logo),
1033 RB_FUNC(mem_stats),
1018 1034
1019 {NULL, NULL} 1035 {NULL, NULL}
1020}; 1036};
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 @@
21#include "plugin.h" 21#include "plugin.h"
22#include <tlsf.h> 22#include <tlsf.h>
23#include "lua.h" 23#include "lua.h"
24static const unsigned int sentinel = 0xBA5EFAC7;
25#define SENTINEL(n) (sentinel ^ (n))
24 26
25void *get_new_area(size_t *size) 27static char *pluginbuf_ptr = NULL;
28static size_t pluginbuf_size = 0;
29static char *audiobuf_ptr = NULL;
30static size_t audiobuf_size = 0;
31
32static void set_sentinel(void* buf, size_t size)
33{
34 size_t i;
35 unsigned int *b = (int*) buf;
36 for(i = 0; i < size / sizeof(sentinel); i++)
37 *b++ = SENTINEL(i);
38}
39
40static size_t check_sentinel(void* buf, size_t size)
41{
42 const size_t sz = size / sizeof(sentinel);
43 size_t unused = 0;
44 size_t i;
45 unsigned int *b = (int*) buf;
46 for(i = 0; i < sz; i++)
47 if (b[i] == SENTINEL(i))
48 {
49 unused++;
50 while(++i < sz && b[i] == SENTINEL(i) && ++unused)
51 ;;
52 }
53 return unused * sizeof(sentinel);
54}
55
56size_t rock_get_allocated_bytes(void)
26{ 57{
27 static char *pluginbuf_ptr = NULL; 58 return pluginbuf_size + audiobuf_size;
28 static char *audiobuf_ptr = NULL; 59}
60
61size_t rock_get_unused_bytes(void)
62{
63 size_t unused = 0;
64 if (pluginbuf_size)
65 unused += check_sentinel(pluginbuf_ptr, pluginbuf_size);
66 if (audiobuf_size)
67 unused += check_sentinel(audiobuf_ptr, audiobuf_size);
68 return unused;
69}
29 70
71void *get_new_area(size_t *size)
72{
30 if (pluginbuf_ptr == NULL) 73 if (pluginbuf_ptr == NULL)
31 { 74 {
32 pluginbuf_ptr = rb->plugin_get_buffer(size); 75 pluginbuf_ptr = rb->plugin_get_buffer(size);
33 76
77 pluginbuf_size = *size;
78 set_sentinel(pluginbuf_ptr, pluginbuf_size);
79
34 /* kill tlsf signature if any */ 80 /* kill tlsf signature if any */
35 memset(pluginbuf_ptr, 0, 4); 81 memset(pluginbuf_ptr, 0, 4);
36 82
@@ -43,6 +89,9 @@ void *get_new_area(size_t *size)
43 /* grab audiobuffer */ 89 /* grab audiobuffer */
44 audiobuf_ptr = rb->plugin_get_audio_buffer(size); 90 audiobuf_ptr = rb->plugin_get_audio_buffer(size);
45 91
92 audiobuf_size = *size;
93 set_sentinel(audiobuf_ptr, audiobuf_size);
94
46 return audiobuf_ptr; 95 return audiobuf_ptr;
47 } 96 }
48 97
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 @@
1--[[
2 __________ __ ___.
3 Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 \/ \/ \/ \/ \/
8 $Id$
9 Example Lua Memory Use
10 Copyright (C) 2020 William Wilgus
11 This program is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License
13 as published by the Free Software Foundation; either version 2
14 of the License, or (at your option) any later version.
15 This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 KIND, either express or implied.
17]]--
18
19local used, allocd, free = rb.mem_stats()
20local lu = collectgarbage("count")
21local fmt = function(t, v) return string.format("%s: %d Kb\n", t, v /1024) end
22
23-- this is how lua recommends to concat strings rather than ..
24local s_t = {}
25s_t[1] = "rockbox:\n"
26s_t[2] = fmt("Used ", used)
27s_t[3] = fmt("Allocd ", allocd)
28s_t[4] = fmt("Free ", free)
29s_t[5] = "\nlua:\n"
30s_t[6] = fmt("Used", lu * 1024)
31s_t[7] = "\n\nNote that the rockbox used count is a high watermark"
32rb.splash_scroller(10 * rb.HZ, table.concat(s_t))