From 934dad43c00634cbb54adb82ef10a37656e100e0 Mon Sep 17 00:00:00 2001 From: Radkesvat <134321679+radkesvat@users.noreply.github.com> Date: Tue, 11 Jun 2024 16:44:43 +0000 Subject: [PATCH] rework buffer and memory profiles, added minimal memory profile --- core/core_settings.c | 20 ++++++++++++++------ ww/buffer_pool.c | 8 ++++---- ww/generic_pool.c | 2 +- ww/shiftbuffer.c | 13 +++++++++---- ww/ww.c | 6 +++--- ww/ww.h | 11 ++++++----- 6 files changed, 37 insertions(+), 23 deletions(-) diff --git a/core/core_settings.c b/core/core_settings.c index ced564d3..e9dabafe 100644 --- a/core/core_settings.c +++ b/core/core_settings.c @@ -22,8 +22,9 @@ enum settings_ram_profiles { kRamProfileServer = kRamProfileL2Memory, - kRamProfileClientGeneric = kRamProfileS1Memory, - kRamProfileClientLarger = kRamProfileM2Memory + kRamProfileClientGeneric = kRamProfileS2Memory, + kRamProfileClientLarger = kRamProfileM2Memory, + kRamProfileMinimal = kRamProfileS1Memory, }; #define DEFAULT_LIBS_PATH "libs/" @@ -232,15 +233,18 @@ static void parseMiscPartOfJson(cJSON *misc_obj) settings->ram_profile = kRamProfileS1Memory; break; case 2: - settings->ram_profile = kRamProfileM1Memory; + settings->ram_profile = kRamProfileS2Memory; break; case 3: - settings->ram_profile = kRamProfileM2Memory; + settings->ram_profile = kRamProfileM1Memory; break; case 4: - settings->ram_profile = kRamProfileL1Memory; + settings->ram_profile = kRamProfileM2Memory; break; case 5: + settings->ram_profile = kRamProfileL1Memory; + break; + case 6: settings->ram_profile = kRamProfileL2Memory; break; default: @@ -267,11 +271,15 @@ static void parseMiscPartOfJson(cJSON *misc_obj) { settings->ram_profile = kRamProfileClientLarger; } + else if (0 == strcmp(string_ram_profile, "ultralow") || 0 == strcmp(string_ram_profile, "minimal")) + { + settings->ram_profile = kRamProfileMinimal; + } if (settings->ram_profile <= 0) { fprintf(stderr, "CoreSettings: ram-profile can hold \"server\" or \"client\" " - "or \"client-larger\"\n"); + "or \"client-larger\" or \"ultralow\" \n"); exit(1); } diff --git a/ww/buffer_pool.c b/ww/buffer_pool.c index 41238f40..d05a3da7 100644 --- a/ww/buffer_pool.c +++ b/ww/buffer_pool.c @@ -19,14 +19,14 @@ #define MEMORY_PROFILE_SELECTED ram_profile #define BASE_READ_BUFSIZE (1U << 13) // 8k -#define BUFFERPOOL_SMALL_CONTAINER_LEN ((unsigned long) ((8 * MEMORY_PROFILE_SMALL))) -#define BUFFERPOOL_CONTAINER_LEN ((unsigned long) ((8 * MEMORY_PROFILE_SELECTED))) +#define BUFFERPOOL_SMALL_CONTAINER_LEN ((unsigned long) ((MEMORY_PROFILE_SMALL))) +#define BUFFERPOOL_CONTAINER_LEN ((unsigned long) ((MEMORY_PROFILE_SELECTED))) #define BUFFER_SIZE_MORE \ (((int) (MEMORY_PROFILE_SELECTED / 16)) > 1 ? (((int) (MEMORY_PROFILE_SELECTED / 16)) - 1) : (0)) -#define BUFFER_SIZE (1U << 15) // 32k (same as nginx file streaming) +#define BUFFER_SIZE (ram_profile >= kRamProfileS2Memory ? (1U << 15) : (1U << 12)) // 32k (same as nginx file streaming) // #define BUFFER_SIZE (BASE_READ_BUFSIZE + (BASE_READ_BUFSIZE * BUFFER_SIZE_MORE)) // [8k,32k] @@ -138,7 +138,7 @@ shift_buffer_t *appendBufferMerge(buffer_pool_t *pool, shift_buffer_t *restrict static buffer_pool_t *allocBufferPool(unsigned long bufcount,unsigned int buffer_size) // NOLINT { // stop using pool if you want less, simply uncomment lines in popbuffer and reuseBuffer - assert(bufcount >= 4); + assert(bufcount >= 1); // half of the pool is used, other half is free at startup bufcount = 2 * bufcount; diff --git a/ww/generic_pool.c b/ww/generic_pool.c index ebfaeae2..426a14d5 100644 --- a/ww/generic_pool.c +++ b/ww/generic_pool.c @@ -2,7 +2,7 @@ #include "utils/mathutils.h" #include "ww.h" -#define GENERIC_POOL_DEFAULT_WIDTH ((unsigned long) ((16 * ram_profile))) +#define GENERIC_POOL_DEFAULT_WIDTH ((unsigned long) ((ram_profile))) extern void poolReCharge(generic_pool_t *pool); extern void poolShrink(generic_pool_t *pool); diff --git a/ww/shiftbuffer.c b/ww/shiftbuffer.c index 6aa87b5d..bc5cb0f6 100644 --- a/ww/shiftbuffer.c +++ b/ww/shiftbuffer.c @@ -1,5 +1,6 @@ #include "shiftbuffer.h" #include "utils/mathutils.h" +#include "ww.h" #include // for assert #include //cel,log2,pow #include @@ -29,6 +30,8 @@ extern void readUI8(shift_buffer_t *self, uint8_t *dest); extern void readUI16(shift_buffer_t *self, uint16_t *dest); extern void readUI64(shift_buffer_t *self, uint64_t *dest); +#define PREPADDING ((ram_profile >= kRamProfileS2Memory ? (1U << 11) : (1U << 8)) + 512) + void destroyShiftBuffer(shift_buffer_t *self) { // if its a shallow then the underlying buffer survives @@ -49,14 +52,14 @@ shift_buffer_t *newShiftBuffer(unsigned int pre_cap) pre_cap = (unsigned int) pow(2, ceil(log2((double) max(16, pre_cap)))); } - unsigned int real_cap = pre_cap * 2; + unsigned int real_cap = pre_cap + (PREPADDING * 2); shift_buffer_t *self = malloc(sizeof(shift_buffer_t)); // todo (optimize) i think refc and pbuf could be in 1 malloc *self = (shift_buffer_t){.calc_len = 0, ._offset = 0, - .curpos = pre_cap, + .curpos = PREPADDING, .full_cap = real_cap, .refc = malloc(sizeof(self->refc[0])), .pbuf = malloc(real_cap)}; @@ -88,11 +91,13 @@ shift_buffer_t *newShallowShiftBuffer(shift_buffer_t *owner) void reset(shift_buffer_t *self, unsigned int pre_cap) { assert(! isShallow(self)); + if (pre_cap != 0 && pre_cap % 16 != 0) { pre_cap = (unsigned int) pow(2, ceil(log2(((double) max(16, pre_cap))))); } - unsigned int real_cap = pre_cap * 2; + + unsigned int real_cap = pre_cap + (PREPADDING * 2); if (self->full_cap != real_cap) { @@ -102,7 +107,7 @@ void reset(shift_buffer_t *self, unsigned int pre_cap) } self->calc_len = 0; self->_offset = 0; - self->curpos = pre_cap; + self->curpos = PREPADDING; } void unShallow(shift_buffer_t *self) diff --git a/ww/ww.c b/ww/ww.c index c3960a2e..9145f0b1 100644 --- a/ww/ww.c +++ b/ww/ww.c @@ -176,10 +176,10 @@ void createWW(const ww_construction_data_t init_data) { buffer_pools[i] = createBufferPool(); context_pools[i] = - newGenericPoolWithSize((16 * 8) + ram_profile, allocContextPoolHandle, destroyContextPoolHandle); - line_pools[i] = newGenericPoolWithSize((16 * 4) + ram_profile, allocLinePoolHandle, destroyLinePoolHandle); + newGenericPoolWithSize((16) + ram_profile, allocContextPoolHandle, destroyContextPoolHandle); + line_pools[i] = newGenericPoolWithSize((8) + ram_profile, allocLinePoolHandle, destroyLinePoolHandle); pipeline_msg_pools[i] = - newGenericPoolWithSize((16 * 8) + ram_profile, allocPipeLineMsgPoolHandle, destroyPipeLineMsgPoolHandle); + newGenericPoolWithSize((8) + ram_profile, allocPipeLineMsgPoolHandle, destroyPipeLineMsgPoolHandle); } loops = (hloop_t **) malloc(sizeof(hloop_t *) * workers_count); diff --git a/ww/ww.h b/ww/ww.h index 1fbadad6..1d8b8434 100644 --- a/ww/ww.h +++ b/ww/ww.h @@ -61,11 +61,12 @@ typedef struct enum ram_profiles { kRamProfileInvalid = 0, // 0 is invalid memory multiplier - kRamProfileS1Memory = 1, // APPROX 2MB per thread - kRamProfileM1Memory = 16 * 1, // APPROX 8MB per thread - kRamProfileM2Memory = 16 * 2, // APPROX 16MB per thread - kRamProfileL1Memory = 16 * 3, // APPROX 28MB per thread - kRamProfileL2Memory = 16 * 4 // APPROX 36MB per thread + kRamProfileS1Memory = 1, + kRamProfileS2Memory = 8, + kRamProfileM1Memory = 8 * 16 * 1, + kRamProfileM2Memory = 8 * 16 * 2, + kRamProfileL1Memory = 8 * 16 * 3, + kRamProfileL2Memory = 8 * 16 * 4 }; typedef struct