Skip to content

Commit 5ce51ba

Browse files
author
Allen Short
committed
External memory-usage tracking
1 parent e653118 commit 5ce51ba

File tree

8 files changed

+67
-25
lines changed

8 files changed

+67
-25
lines changed

docs/sandbox.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
* **output_limit** - the largest output string an input or analysis plugin can inject into the host (bytes, default 64KiB)
66
* **memory_limit** - the maximum amount of memory a plugin can use before being terminated (bytes, default 8MiB)
7+
* **external_memory_limit** - the maximum amount of memory external libraries (exposed to the sandbox as userdata) can use before plugin is terminated; set to 0 for unlimited (bytes, default unlimited)
78
* **instruction_limit** - the maximum number of Lua instructions a plugin can execute in a single API function call (count, default 1MM)
89
* **path** - The path used by require to search for a Lua loader. See [package loaders](http://www.lua.org/manual/5.1/manual.html#pdf-package.loaders)
910
for the path syntax. By default no paths are set in the sandbox and everything has been moved to a sandbox configuration table.

include/luasandbox.h

+14-12
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,16 @@
2727

2828
#define LSB_ERROR_SIZE 256
2929

30-
#define LSB_SHUTTING_DOWN "shutting down"
31-
#define LSB_CONFIG_TABLE "lsb_config"
32-
#define LSB_THIS_PTR "lsb_this_ptr"
33-
#define LSB_MEMORY_LIMIT "memory_limit"
34-
#define LSB_INSTRUCTION_LIMIT "instruction_limit"
35-
#define LSB_OUTPUT_LIMIT "output_limit"
36-
#define LSB_LUA_PATH "path"
37-
#define LSB_LUA_CPATH "cpath"
38-
#define LSB_NIL_ERROR "<nil error message>"
30+
#define LSB_SHUTTING_DOWN "shutting down"
31+
#define LSB_CONFIG_TABLE "lsb_config"
32+
#define LSB_THIS_PTR "lsb_this_ptr"
33+
#define LSB_MEMORY_LIMIT "memory_limit"
34+
#define LSB_EXTERNAL_MEMORY_LIMIT "external_memory_limit"
35+
#define LSB_INSTRUCTION_LIMIT "instruction_limit"
36+
#define LSB_OUTPUT_LIMIT "output_limit"
37+
#define LSB_LUA_PATH "path"
38+
#define LSB_LUA_CPATH "cpath"
39+
#define LSB_NIL_ERROR "<nil error message>"
3940

4041
typedef enum {
4142
LSB_UNKNOWN = 0,
@@ -53,9 +54,10 @@ typedef enum {
5354
} lsb_usage_stat;
5455

5556
typedef enum {
56-
LSB_UT_MEMORY = 0,
57-
LSB_UT_INSTRUCTION = 1,
58-
LSB_UT_OUTPUT = 2,
57+
LSB_UT_MEMORY = 0,
58+
LSB_UT_INSTRUCTION = 1,
59+
LSB_UT_OUTPUT = 2,
60+
LSB_UT_EXTERNAL_MEMORY = 3,
5961

6062
LSB_UT_MAX
6163
} lsb_usage_type;

include/luasandbox/heka/sandbox.h

+12
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ typedef struct lsb_heka_sandbox lsb_heka_sandbox;
5454
typedef struct lsb_heka_stats {
5555
unsigned long long mem_cur;
5656
unsigned long long mem_max;
57+
unsigned long long ext_mem_cur;
58+
unsigned long long ext_mem_max;
5759
unsigned long long ins_max;
5860
unsigned long long out_max;
5961
unsigned long long im_cnt;
@@ -338,6 +340,16 @@ LSB_HEKA_EXPORT const char* lsb_heka_get_lua_file(lsb_heka_sandbox *hsb);
338340
*/
339341
LSB_HEKA_EXPORT lsb_heka_stats lsb_heka_get_stats(lsb_heka_sandbox *hsb);
340342

343+
/**
344+
* Retrieve the limit on external memory allocation by native modules, or 0 if none is set.
345+
* @param hsb Heka sandbox
346+
*
347+
* @return size_t Memory allocation limit
348+
*/
349+
LSB_HEKA_EXPORT size_t lsb_heka_get_ext_memory_limit(lsb_heka_sandbox *hsb);
350+
351+
LSB_HEKA_EXPORT void lsb_heka_adjust_ext_memory_usage(lsb_heka_sandbox *hsb, int sizechange);
352+
341353
/**
342354
* Queries the state of the sandbox.
343355
*

src/heka/sandbox.c

+31-13
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include <errno.h>
1212
#include <math.h>
13+
#include <stdio.h>
1314
#include <stdlib.h>
1415
#include <string.h>
1516

@@ -1058,24 +1059,41 @@ const char* lsb_heka_get_lua_file(lsb_heka_sandbox *hsb)
10581059

10591060
lsb_heka_stats lsb_heka_get_stats(lsb_heka_sandbox *hsb)
10601061
{
1061-
if (!hsb) return (struct lsb_heka_stats){ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1062+
if (!hsb) return (struct lsb_heka_stats){ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
10621063

10631064
return (struct lsb_heka_stats){
1064-
.mem_cur = lsb_usage(hsb->lsb, LSB_UT_MEMORY, LSB_US_CURRENT),
1065-
.mem_max = lsb_usage(hsb->lsb, LSB_UT_MEMORY, LSB_US_MAXIMUM),
1066-
.out_max = lsb_usage(hsb->lsb, LSB_UT_OUTPUT, LSB_US_MAXIMUM),
1067-
.ins_max = lsb_usage(hsb->lsb, LSB_UT_INSTRUCTION, LSB_US_MAXIMUM),
1068-
.im_cnt = hsb->stats.im_cnt,
1069-
.im_bytes = hsb->stats.im_bytes,
1070-
.pm_cnt = hsb->stats.pm_cnt,
1071-
.pm_failures = hsb->stats.pm_failures,
1072-
.pm_avg = hsb->stats.pm.mean,
1073-
.pm_sd = lsb_sd_running_stats(&hsb->stats.pm),
1074-
.te_avg = hsb->stats.te.mean,
1075-
.te_sd = lsb_sd_running_stats(&hsb->stats.te)
1065+
.mem_cur = lsb_usage(hsb->lsb, LSB_UT_MEMORY, LSB_US_CURRENT),
1066+
.mem_max = lsb_usage(hsb->lsb, LSB_UT_MEMORY, LSB_US_MAXIMUM),
1067+
.ext_mem_cur = lsb_usage(hsb->lsb, LSB_UT_EXTERNAL_MEMORY, LSB_US_CURRENT),
1068+
.ext_mem_max = lsb_usage(hsb->lsb, LSB_UT_EXTERNAL_MEMORY, LSB_US_MAXIMUM),
1069+
.out_max = lsb_usage(hsb->lsb, LSB_UT_OUTPUT, LSB_US_MAXIMUM),
1070+
.ins_max = lsb_usage(hsb->lsb, LSB_UT_INSTRUCTION, LSB_US_MAXIMUM),
1071+
.im_cnt = hsb->stats.im_cnt,
1072+
.im_bytes = hsb->stats.im_bytes,
1073+
.pm_cnt = hsb->stats.pm_cnt,
1074+
.pm_failures = hsb->stats.pm_failures,
1075+
.pm_avg = hsb->stats.pm.mean,
1076+
.pm_sd = lsb_sd_running_stats(&hsb->stats.pm),
1077+
.te_avg = hsb->stats.te.mean,
1078+
.te_sd = lsb_sd_running_stats(&hsb->stats.te)
10761079
};
10771080
}
10781081

1082+
size_t lsb_heka_get_ext_memory_limit(lsb_heka_sandbox *hsb)
1083+
{
1084+
return hsb ? lsb_usage(hsb->lsb, LSB_UT_EXTERNAL_MEMORY, LSB_US_LIMIT) : 0;
1085+
}
1086+
1087+
void lsb_heka_adjust_ext_memory_usage(lsb_heka_sandbox *hsb, int sizechange)
1088+
{
1089+
size_t oldsize = hsb->lsb->usage[LSB_UT_EXTERNAL_MEMORY][LSB_US_CURRENT];
1090+
size_t newsize = hsb->lsb->usage[LSB_UT_EXTERNAL_MEMORY][LSB_US_CURRENT] + sizechange;
1091+
1092+
if (newsize > hsb->lsb->usage[LSB_UT_EXTERNAL_MEMORY][LSB_US_MAXIMUM]) {
1093+
hsb->lsb->usage[LSB_UT_EXTERNAL_MEMORY][LSB_US_MAXIMUM] = newsize;
1094+
}
1095+
hsb->lsb->usage[LSB_UT_EXTERNAL_MEMORY][LSB_US_CURRENT] = newsize;
1096+
}
10791097

10801098
bool lsb_heka_is_running(lsb_heka_sandbox *hsb)
10811099
{

src/heka/test/test_heka_sandbox.c

+2
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,8 @@ static char* test_timer_event()
350350
lsb_heka_stats stats = lsb_heka_get_stats(hsb);
351351
mu_assert(0 < stats.mem_cur, "received %llu", stats.mem_cur);
352352
mu_assert(0 < stats.mem_max, "received %llu", stats.mem_max);
353+
mu_assert(0 == stats.ext_mem_cur, "received %llu", stats.ext_mem_cur);
354+
mu_assert(0 == stats.ext_mem_max, "received %llu", stats.ext_mem_max);
353355
mu_assert(0 == stats.out_max, "received %llu", stats.out_max);
354356
mu_assert(0 < stats.ins_max, "received %llu", stats.ins_max);
355357
mu_assert(0 == stats.pm_cnt, "received %llu", stats.pm_cnt);

src/luasandbox.c

+5
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,9 @@ static lua_State* load_sandbox_config(const char *cfg, lsb_logger *logger)
297297
ret = check_int(L, LUA_GLOBALSINDEX, LSB_MEMORY_LIMIT, 8 * 1024 * 1024);
298298
if (ret) goto cleanup;
299299

300+
ret = check_int(L, LUA_GLOBALSINDEX, LSB_EXTERNAL_MEMORY_LIMIT, 0);
301+
if (ret) goto cleanup;
302+
300303
ret = check_int(L, LUA_GLOBALSINDEX, LSB_INSTRUCTION_LIMIT, 1000000);
301304
if (ret) goto cleanup;
302305

@@ -477,6 +480,7 @@ lsb_lua_sandbox* lsb_create(void *parent,
477480
lua_pop(lua_cfg, 2);
478481
lua_close(lua_cfg);
479482
size_t ml = get_int(lsb->lua, -1, "memory_limit");
483+
size_t eml = get_int(lsb->lua, -1, "external_memory_limit");
480484
size_t il = get_int(lsb->lua, -1, "instruction_limit");
481485
size_t ol = get_int(lsb->lua, -1, "output_limit");
482486
int log_level = get_int(lsb->lua, -1, "log_level");
@@ -492,6 +496,7 @@ lsb_lua_sandbox* lsb_create(void *parent,
492496

493497
lsb->parent = parent;
494498
lsb->usage[LSB_UT_MEMORY][LSB_US_LIMIT] = ml;
499+
lsb->usage[LSB_UT_EXTERNAL_MEMORY][LSB_US_LIMIT] = eml;
495500
lsb->usage[LSB_UT_INSTRUCTION][LSB_US_LIMIT] = il;
496501
lsb->usage[LSB_UT_OUTPUT][LSB_US_LIMIT] = ol;
497502
lsb->state = LSB_UNKNOWN;

src/test/lua/read_config.lua

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
assert(type(read_config) == "function")
22
assert(read_config("memory_limit") == 65765)
3+
assert(read_config("external_memory_limit") == 1610612736)
34
assert(read_config("instruction_limit") == 1000)
45
assert(read_config("output_limit") == 1024)
56

src/test/test_generic_sandbox.c

+1
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ static char* test_create_error()
223223
static char* test_read_config()
224224
{
225225
const char *cfg = "memory_limit = 65765\n"
226+
"external_memory_limit = 1610612736\n"
226227
"instruction_limit = 1000\n"
227228
"output_limit = 1024\n"
228229
"array = {'foo', 99}\n"

0 commit comments

Comments
 (0)