From 8b57545540f5c843a873c6c86dfe5ea44e18587c Mon Sep 17 00:00:00 2001 From: Victor Julien Date: Wed, 22 May 2024 15:29:13 +0200 Subject: [PATCH] defrag: turn hash row into single linked list --- src/defrag-hash.c | 14 ++----------- src/defrag-hash.h | 1 - src/defrag-timeout.c | 47 ++++++++++++++++++++++---------------------- src/defrag.h | 3 +-- 4 files changed, 27 insertions(+), 38 deletions(-) diff --git a/src/defrag-hash.c b/src/defrag-hash.c index b342abb237..6f1b59ff7f 100644 --- a/src/defrag-hash.c +++ b/src/defrag-hash.c @@ -564,7 +564,6 @@ DefragTracker *DefragGetTrackerFromHash(ThreadVars *tv, DecodeThreadVars *dtv, P /* tracker is locked */ hb->head = dt; - hb->tail = dt; /* got one, now lock, initialize and return */ DefragTrackerInit(dt,p); @@ -591,8 +590,7 @@ DefragTracker *DefragGetTrackerFromHash(ThreadVars *tv, DecodeThreadVars *dtv, P DRLOCK_UNLOCK(hb); return NULL; } - prev->hnext = hb->tail = dt; - dt->hprev = prev; + prev->hnext = dt; /* tracker is locked */ @@ -702,17 +700,9 @@ static DefragTracker *DefragTrackerGetUsedDefragTracker(void) } /* remove from the hash */ - if (dt->hprev != NULL) - dt->hprev->hnext = dt->hnext; - if (dt->hnext != NULL) - dt->hnext->hprev = dt->hprev; - if (hb->head == dt) - hb->head = dt->hnext; - if (hb->tail == dt) - hb->tail = dt->hprev; + hb->head = dt->hnext; dt->hnext = NULL; - dt->hprev = NULL; DRLOCK_UNLOCK(hb); DefragTrackerClearMemory(dt); diff --git a/src/defrag-hash.h b/src/defrag-hash.h index 5e7499f31c..299967a492 100644 --- a/src/defrag-hash.h +++ b/src/defrag-hash.h @@ -60,7 +60,6 @@ typedef struct DefragTrackerHashRow_ { DRLOCK_TYPE lock; DefragTracker *head; - DefragTracker *tail; } DefragTrackerHashRow; /** defrag tracker hash table */ diff --git a/src/defrag-timeout.c b/src/defrag-timeout.c index 44e57956e0..dff754ea26 100644 --- a/src/defrag-timeout.c +++ b/src/defrag-timeout.c @@ -67,8 +67,10 @@ static uint32_t DefragTrackerHashRowTimeout( { uint32_t cnt = 0; + DefragTracker *prev_dt = NULL; do { if (SCMutexTrylock(&dt->lock) != 0) { + prev_dt = dt; dt = dt->hnext; continue; } @@ -77,34 +79,33 @@ static uint32_t DefragTrackerHashRowTimeout( /* check if the tracker is fully timed out and * ready to be discarded. */ - if (DefragTrackerTimedOut(dt, ts) == 1) { - /* remove from the hash */ - if (dt->hprev != NULL) - dt->hprev->hnext = dt->hnext; - if (dt->hnext != NULL) - dt->hnext->hprev = dt->hprev; - if (hb->head == dt) - hb->head = dt->hnext; - if (hb->tail == dt) - hb->tail = dt->hprev; - - dt->hnext = NULL; - dt->hprev = NULL; - - DefragTrackerClearMemory(dt); - - /* no one is referring to this tracker, use_cnt 0, removed from hash - * so we can unlock it and move it back to the spare queue. */ + if (DefragTrackerTimedOut(dt, ts) == 0) { + prev_dt = dt; SCMutexUnlock(&dt->lock); + dt = next_dt; + continue; + } - /* move to spare list */ - DefragTrackerMoveToSpare(dt); - - cnt++; + /* remove from the hash */ + if (prev_dt != NULL) { + prev_dt->hnext = dt->hnext; } else { - SCMutexUnlock(&dt->lock); + hb->head = dt->hnext; } + dt->hnext = NULL; + + DefragTrackerClearMemory(dt); + + /* no one is referring to this tracker, use_cnt 0, removed from hash + * so we can unlock it and move it back to the spare queue. */ + SCMutexUnlock(&dt->lock); + + /* move to spare list */ + DefragTrackerMoveToSpare(dt); + + cnt++; + dt = next_dt; } while (dt != NULL); diff --git a/src/defrag.h b/src/defrag.h index b6f3338b94..3fb65aeed8 100644 --- a/src/defrag.h +++ b/src/defrag.h @@ -115,9 +115,8 @@ typedef struct DefragTracker_ { struct IP_FRAGMENTS fragment_tree; - /** hash pointers, protected by hash row mutex/spin */ + /** hash pointer, protected by hash row mutex/spin */ struct DefragTracker_ *hnext; - struct DefragTracker_ *hprev; /** list pointers, protected by tracker-queue mutex/spin */ struct DefragTracker_ *lnext;