Skip to content

Commit

Permalink
rework buffer and memory profiles, added minimal memory profile
Browse files Browse the repository at this point in the history
  • Loading branch information
radkesvat committed Jun 11, 2024
1 parent aa767b2 commit 934dad4
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 23 deletions.
20 changes: 14 additions & 6 deletions core/core_settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
Expand Down Expand Up @@ -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:
Expand All @@ -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);
}
Expand Down
8 changes: 4 additions & 4 deletions ww/buffer_pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion ww/generic_pool.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 9 additions & 4 deletions ww/shiftbuffer.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "shiftbuffer.h"
#include "utils/mathutils.h"
#include "ww.h"
#include <assert.h> // for assert
#include <math.h> //cel,log2,pow
#include <stdint.h>
Expand Down Expand Up @@ -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
Expand All @@ -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)};
Expand Down Expand Up @@ -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)
{
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions ww/ww.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 6 additions & 5 deletions ww/ww.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 934dad4

Please sign in to comment.