From 0ebf7cbc5ed16c1480cc4b32dc7de188c60115b5 Mon Sep 17 00:00:00 2001 From: Victor Julien Date: Fri, 14 May 2010 21:46:25 +0200 Subject: [PATCH] Convert flow bucket lock from mutex to spinlock. Locks should be very short, so spinlocks should be faster. --- src/flow-hash.c | 14 +++++++------- src/flow-hash.h | 3 ++- src/flow.c | 36 ++++++++++++++++++------------------ 3 files changed, 27 insertions(+), 26 deletions(-) diff --git a/src/flow-hash.c b/src/flow-hash.c index 139daa28e7..6c909cdafb 100644 --- a/src/flow-hash.c +++ b/src/flow-hash.c @@ -97,7 +97,7 @@ Flow *FlowGetFlowFromHash (Packet *p) uint32_t key = FlowGetKey(p); /* get our hash bucket and lock it */ FlowBucket *fb = &flow_hash[key]; - SCMutexLock(&fb->m); + SCSpinLock(&fb->s); SCLogDebug("fb %p fb->f %p", fb, fb->f); @@ -110,7 +110,7 @@ Flow *FlowGetFlowFromHash (Packet *p) f = fb->f = FlowAlloc(); if (f == NULL) { - SCMutexUnlock(&fb->m); + SCSpinUnlock(&fb->s); return NULL; } } @@ -125,7 +125,7 @@ Flow *FlowGetFlowFromHash (Packet *p) f->flags |= FLOW_NEW_LIST; f->fb = fb; - SCMutexUnlock(&fb->m); + SCSpinUnlock(&fb->s); return f; } @@ -151,7 +151,7 @@ Flow *FlowGetFlowFromHash (Packet *p) f = fb->f = FlowAlloc(); if (f == NULL) { - SCMutexUnlock(&fb->m); + SCSpinUnlock(&fb->s); return NULL; } } @@ -167,7 +167,7 @@ Flow *FlowGetFlowFromHash (Packet *p) f->flags |= FLOW_NEW_LIST; f->fb = fb; - SCMutexUnlock(&fb->m); + SCSpinUnlock(&fb->s); return f; } @@ -185,7 +185,7 @@ Flow *FlowGetFlowFromHash (Packet *p) fb->f = f; /* found our flow */ - SCMutexUnlock(&fb->m); + SCSpinUnlock(&fb->s); return f; } @@ -196,7 +196,7 @@ Flow *FlowGetFlowFromHash (Packet *p) /* The 'root' flow was our flow, return it. * It's already locked. */ - SCMutexUnlock(&fb->m); + SCSpinUnlock(&fb->s); return f; } diff --git a/src/flow-hash.h b/src/flow-hash.h index 3a28e066e7..b1f79901fa 100644 --- a/src/flow-hash.h +++ b/src/flow-hash.h @@ -30,7 +30,8 @@ * to the list, the entire bucket is locked. */ typedef struct FlowBucket_ { Flow *f; - SCMutex m; +// SCMutex m; + SCSpinlock s; } FlowBucket; /* prototypes */ diff --git a/src/flow.c b/src/flow.c index 87ee049960..45a506d503 100644 --- a/src/flow.c +++ b/src/flow.c @@ -155,7 +155,7 @@ static int FlowPrune (FlowQueue *q, struct timeval *ts) /* unlock list */ SCMutexUnlock(&q->mutex_q); - if (SCMutexTrylock(&f->fb->m) != 0) { + if (SCSpinTrylock(&f->fb->s) != 0) { SCMutexUnlock(&f->m); SCLogDebug("cant lock 2"); return 0; @@ -210,7 +210,7 @@ static int FlowPrune (FlowQueue *q, struct timeval *ts) /* do the timeout check */ if ((int32_t)(f->lastts.tv_sec + timeout) >= ts->tv_sec) { - SCMutexUnlock(&f->fb->m); + SCSpinUnlock(&f->fb->s); SCMutexUnlock(&f->m); SCLogDebug("timeout check failed"); return 0; @@ -220,7 +220,7 @@ static int FlowPrune (FlowQueue *q, struct timeval *ts) * we are currently processing in one of the threads */ if (f->use_cnt > 0) { SCLogDebug("timed out but use_cnt > 0: %"PRIu16", %p, proto %"PRIu8"", f->use_cnt, f, f->proto); - SCMutexUnlock(&f->fb->m); + SCSpinUnlock(&f->fb->s); SCMutexUnlock(&f->m); SCLogDebug("it is in one of the threads"); return 0; @@ -237,7 +237,7 @@ static int FlowPrune (FlowQueue *q, struct timeval *ts) f->hnext = NULL; f->hprev = NULL; - SCMutexUnlock(&f->fb->m); + SCSpinUnlock(&f->fb->s); f->fb = NULL; FlowClearMemory (f, f->protomap); @@ -511,7 +511,7 @@ void FlowInitConfig (char quiet) memset(flow_hash, 0, flow_config.hash_size * sizeof(FlowBucket)); for (i = 0; i < flow_config.hash_size; i++) - SCMutexInit(&flow_hash[i].m, NULL); + SCSpinInit(&flow_hash[i].s, 0); flow_memuse += (flow_config.hash_size * sizeof(FlowBucket)); if (quiet == FALSE) @@ -620,7 +620,7 @@ void FlowShutdown(void) { if (flow_hash != NULL) { /* clean up flow mutexes */ for (u = 0; u < flow_config.hash_size; u++) { - SCMutexDestroy(&flow_hash[u].m); + SCSpinDestroy(&flow_hash[u].s); } SCFree(flow_hash); flow_hash = NULL; @@ -1295,7 +1295,7 @@ static int FlowTest03 (void) { memset(&ts, 0, sizeof(ts)); memset(&fb, 0, sizeof(FlowBucket)); - SCMutexInit(&fb.m, NULL); + SCSpinInit(&fb.s, 0); SCMutexInit(&f.m, NULL); TimeGet(&ts); @@ -1306,12 +1306,12 @@ static int FlowTest03 (void) { f.proto = IPPROTO_TCP; if (FlowTestPrune(&f, &ts) != 1) { - SCMutexDestroy(&fb.m); + SCSpinDestroy(&fb.s); SCMutexDestroy(&f.m); return 0; } - SCMutexDestroy(&fb.m); + SCSpinDestroy(&fb.s); SCMutexDestroy(&f.m); return 1; } @@ -1340,7 +1340,7 @@ static int FlowTest04 (void) { memset(&seg, 0, sizeof(TcpSegment)); memset(&client, 0, sizeof(TcpSegment)); - SCMutexInit(&fb.m, NULL); + SCSpinInit(&fb.s, 0); SCMutexInit(&f.m, NULL); TimeGet(&ts); @@ -1358,12 +1358,12 @@ static int FlowTest04 (void) { f.proto = IPPROTO_TCP; if (FlowTestPrune(&f, &ts) != 1) { - SCMutexDestroy(&fb.m); + SCSpinDestroy(&fb.s); SCMutexDestroy(&f.m); return 0; } - SCMutexDestroy(&fb.m); + SCSpinDestroy(&fb.s); SCMutexDestroy(&f.m); return 1; @@ -1388,7 +1388,7 @@ static int FlowTest05 (void) { memset(&ts, 0, sizeof(ts)); memset(&fb, 0, sizeof(FlowBucket)); - SCMutexInit(&fb.m, NULL); + SCSpinInit(&fb.s, 0); SCMutexInit(&f.m, NULL); TimeGet(&ts); @@ -1400,12 +1400,12 @@ static int FlowTest05 (void) { f.flags = FLOW_EMERGENCY; if (FlowTestPrune(&f, &ts) != 1) { - SCMutexDestroy(&fb.m); + SCSpinDestroy(&fb.s); SCMutexDestroy(&f.m); return 0; } - SCMutexDestroy(&fb.m); + SCSpinDestroy(&fb.s); SCMutexDestroy(&f.m); return 1; } @@ -1434,7 +1434,7 @@ static int FlowTest06 (void) { memset(&seg, 0, sizeof(TcpSegment)); memset(&client, 0, sizeof(TcpSegment)); - SCMutexInit(&fb.m, NULL); + SCSpinInit(&fb.s, 0); SCMutexInit(&f.m, NULL); TimeGet(&ts); @@ -1453,12 +1453,12 @@ static int FlowTest06 (void) { f.flags = FLOW_EMERGENCY; if (FlowTestPrune(&f, &ts) != 1) { - SCMutexDestroy(&fb.m); + SCSpinDestroy(&fb.s); SCMutexDestroy(&f.m); return 0; } - SCMutexDestroy(&fb.m); + SCSpinDestroy(&fb.s); SCMutexDestroy(&f.m); return 1;