counters: use spinlock for thread sync

Sync should be short, so no value in getting scheduled away.
pull/14398/head
Victor Julien 11 months ago
parent 96cabab46b
commit 6c77c8f371

@ -120,20 +120,20 @@ bool StatsEnabled(void)
static void StatsPublicThreadContextInit(StatsPublicThreadContext *t) static void StatsPublicThreadContextInit(StatsPublicThreadContext *t)
{ {
SCMutexInit(&t->m, NULL); SCSpinInit(&t->lock, 0);
} }
static void StatsPublicThreadContextCleanup(StatsPublicThreadContext *t) static void StatsPublicThreadContextCleanup(StatsPublicThreadContext *t)
{ {
SCMutexLock(&t->m); SCSpinLock(&t->lock);
SCFree(t->copy_of_private); SCFree(t->copy_of_private);
SCFree(t->pc_array); SCFree(t->pc_array);
StatsReleaseCounters(t->head); StatsReleaseCounters(t->head);
t->head = NULL; t->head = NULL;
SC_ATOMIC_SET(t->sync_now, false); SC_ATOMIC_SET(t->sync_now, false);
t->curr_id = 0; t->curr_id = 0;
SCMutexUnlock(&t->m); SCSpinUnlock(&t->lock);
SCMutexDestroy(&t->m); SCSpinDestroy(&t->lock);
} }
/** /**
@ -735,11 +735,11 @@ static int StatsOutput(ThreadVars *tv)
memset(&thread_table_from_private, 0x00, max_id * sizeof(StatsLocalCounter)); memset(&thread_table_from_private, 0x00, max_id * sizeof(StatsLocalCounter));
/* copy private table to a local variable to loop it w/o lock */ /* copy private table to a local variable to loop it w/o lock */
SCMutexLock(&sts->ctx->m); SCSpinLock(&sts->ctx->lock);
uint16_t table_size = sts->ctx->curr_id + 1; uint16_t table_size = sts->ctx->curr_id + 1;
memcpy(&thread_table_from_private, sts->ctx->copy_of_private, memcpy(&thread_table_from_private, sts->ctx->copy_of_private,
table_size * sizeof(StatsLocalCounter)); table_size * sizeof(StatsLocalCounter));
SCMutexUnlock(&sts->ctx->m); SCSpinUnlock(&sts->ctx->lock);
/* loop counters and handle them. This includes the global counters, which /* loop counters and handle them. This includes the global counters, which
* access the StatsCounters but don't modify them. */ * access the StatsCounters but don't modify them. */
@ -1289,9 +1289,9 @@ static int StatsUpdateCounterArray(StatsPrivateThreadContext *pca, StatsPublicTh
/* copy the whole table under lock to the public section /* copy the whole table under lock to the public section
* and release the lock. The stats thread will copy it from * and release the lock. The stats thread will copy it from
* there. */ * there. */
SCMutexLock(&pctx->m); SCSpinLock(&pctx->lock);
memcpy(pctx->copy_of_private, pca->head, (pca->size + 1) * sizeof(StatsLocalCounter)); memcpy(pctx->copy_of_private, pca->head, (pca->size + 1) * sizeof(StatsLocalCounter));
SCMutexUnlock(&pctx->m); SCSpinUnlock(&pctx->lock);
} }
SC_ATOMIC_SET(pctx->sync_now, false); SC_ATOMIC_SET(pctx->sync_now, false);
return 1; return 1;

@ -102,8 +102,8 @@ typedef struct StatsPublicThreadContext_ {
StatsLocalCounter *copy_of_private; StatsLocalCounter *copy_of_private;
/* mutex to prevent simultaneous access during update_counter/output_stat */ /* lock to prevent simultaneous access during update_counter/output_stat */
SCMutex m; SCSpinlock lock;
} StatsPublicThreadContext; } StatsPublicThreadContext;
/** /**

@ -951,7 +951,7 @@ ThreadVars *TmThreadCreate(const char *name, const char *inq_name, const char *i
goto error; goto error;
SC_ATOMIC_INIT(tv->flags); SC_ATOMIC_INIT(tv->flags);
SCMutexInit(&tv->perf_public_ctx.m, NULL); SCSpinInit(&tv->perf_public_ctx.lock, 0);
strlcpy(tv->name, name, sizeof(tv->name)); strlcpy(tv->name, name, sizeof(tv->name));

Loading…
Cancel
Save