thash/memcap: Use atomics for memcap

Issue: 845

Maintain the memcap as an atomic counter so changes through the
unix-socket interface can be supported.
pull/12117/head
Jeff Lucovsky 9 months ago committed by Victor Julien
parent f23fec0c64
commit 83bdcda932

@ -228,12 +228,15 @@ static int THashInitConfig(THashTableContext *ctx, const char *cnf_prefix)
GET_VAR(cnf_prefix, "memcap");
if ((ConfGet(varname, &conf_val)) == 1)
{
if (ParseSizeStringU64(conf_val, &ctx->config.memcap) < 0) {
uint64_t memcap;
if (ParseSizeStringU64(conf_val, &memcap) < 0) {
SCLogError("Error parsing %s "
"from conf file - %s. Killing engine",
varname, conf_val);
return -1;
}
SC_ATOMIC_INIT(ctx->config.memcap);
SC_ATOMIC_SET(ctx->config.memcap, memcap);
}
GET_VAR(cnf_prefix, "hash-size");
if ((ConfGet(varname, &conf_val)) == 1)
@ -261,7 +264,7 @@ static int THashInitConfig(THashTableContext *ctx, const char *cnf_prefix)
"Memcap: %" PRIu64 ", Hash table size %" PRIu64 ". Calculate "
"total hash size by multiplying \"hash-size\" with %" PRIuMAX ", "
"which is the hash bucket size.",
ctx->config.memcap, hash_size, (uintmax_t)sizeof(THashHashRow));
SC_ATOMIC_GET(ctx->config.memcap), hash_size, (uintmax_t)sizeof(THashHashRow));
return -1;
}
ctx->array = SCMallocAligned(ctx->config.hash_size * sizeof(THashHashRow), CLS);
@ -283,7 +286,7 @@ static int THashInitConfig(THashTableContext *ctx, const char *cnf_prefix)
SCLogError("preallocating data failed: "
"max thash memcap reached. Memcap %" PRIu64 ", "
"Memuse %" PRIu64 ".",
ctx->config.memcap,
SC_ATOMIC_GET(ctx->config.memcap),
((uint64_t)SC_ATOMIC_GET(ctx->memuse) + THASH_DATA_SIZE(ctx)));
return -1;
}
@ -323,12 +326,12 @@ THashTableContext *THashInit(const char *cnf_prefix, size_t data_size,
unless defined by the rule keyword */
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
// limit memcap size to default when fuzzing
ctx->config.memcap = THASH_DEFAULT_MEMCAP;
SC_ATOMIC_SET(ctx->config.memcap, THASH_DEFAULT_MEMCAP);
#else
if (memcap > 0) {
ctx->config.memcap = memcap;
SC_ATOMIC_SET(ctx->config.memcap, memcap);
} else {
ctx->config.memcap = reset_memcap ? UINT64_MAX : THASH_DEFAULT_MEMCAP;
SC_ATOMIC_SET(ctx->config.memcap, reset_memcap ? UINT64_MAX : THASH_DEFAULT_MEMCAP);
}
#endif
ctx->config.prealloc = THASH_DEFAULT_PREALLOC;
@ -349,8 +352,9 @@ THashTableContext *THashInit(const char *cnf_prefix, size_t data_size,
* */
void THashConsolidateMemcap(THashTableContext *ctx)
{
ctx->config.memcap = MAX(SC_ATOMIC_GET(ctx->memuse), ctx->config.memcap);
SCLogDebug("memcap after load set to: %" PRIu64, ctx->config.memcap);
SC_ATOMIC_SET(
ctx->config.memcap, MAX(SC_ATOMIC_GET(ctx->memuse), SC_ATOMIC_GET(ctx->config.memcap)));
SCLogDebug("memcap after load set to: %" PRIu64, SC_ATOMIC_GET(ctx->config.memcap));
}
/** \brief shutdown the flow engine
@ -598,7 +602,7 @@ static THashData *THashDataGetNew(THashTableContext *ctx, void *data)
SC_ATOMIC_SET(ctx->memcap_reached, true);
}
SCLogError("Adding data will exceed memcap: %" PRIu64 ", current memuse: %" PRIu64,
(ctx)->config.memcap, SC_ATOMIC_GET(ctx->memuse));
SC_ATOMIC_GET((ctx)->config.memcap), SC_ATOMIC_GET(ctx->memuse));
}
}
}

@ -122,7 +122,7 @@ typedef int (*THashOutputFunc)(void *output_ctx, const uint8_t *data, const uint
typedef int (*THashFormatFunc)(const void *in_data, char *output, size_t output_size);
typedef struct THashDataConfig_ {
uint64_t memcap;
SC_ATOMIC_DECLARE(uint64_t, memcap);
uint32_t hash_rand;
uint32_t hash_size;
uint32_t prealloc;
@ -161,8 +161,9 @@ typedef struct THashTableContext_ {
* \retval 1 it fits
* \retval 0 no fit
*/
#define THASH_CHECK_MEMCAP(ctx, size) \
((((uint64_t)SC_ATOMIC_GET((ctx)->memuse) + (uint64_t)(size)) <= (ctx)->config.memcap))
#define THASH_CHECK_MEMCAP(ctx, size) \
((((uint64_t)SC_ATOMIC_GET((ctx)->memuse) + (uint64_t)(size)) <= \
SC_ATOMIC_GET((ctx)->config.memcap)))
#define THashIncrUsecnt(h) \
(void)SC_ATOMIC_ADD((h)->use_cnt, 1)

Loading…
Cancel
Save