From ccf22cf563114e723aff72ab3fc30053bfbd9e3c Mon Sep 17 00:00:00 2001 From: Victor Julien Date: Sun, 16 May 2010 18:41:18 +0200 Subject: [PATCH] Move to different way of enforcing max_pending. Should require less locks. --- src/source-pcap-file.c | 6 ------ src/suricata.c | 42 ++++++++++-------------------------------- src/tmqh-packetpool.c | 20 ++------------------ 3 files changed, 12 insertions(+), 56 deletions(-) diff --git a/src/source-pcap-file.c b/src/source-pcap-file.c index 5e42539a5c..05a66eeba5 100644 --- a/src/source-pcap-file.c +++ b/src/source-pcap-file.c @@ -103,12 +103,6 @@ void PcapFileCallback(char *user, struct pcap_pkthdr *h, u_char *pkt) { PcapFileThreadVars *ptv = (PcapFileThreadVars *)user; - SCMutexLock(&mutex_pending); - if (pending > max_pending_packets) { - SCondWait(&cond_pending, &mutex_pending); - } - SCMutexUnlock(&mutex_pending); - Packet *p = ptv->in_p; p->ts.tv_sec = h->ts.tv_sec; diff --git a/src/suricata.c b/src/suricata.c index 27c8fa64e6..ebe037fbf4 100644 --- a/src/suricata.c +++ b/src/suricata.c @@ -219,12 +219,13 @@ Packet *SetupPktWait (void) Packet *SetupPkt (void) { Packet *p = NULL; - int r = 0; - - r = SCMutexLock(&packet_q.mutex_q); - p = PacketDequeue(&packet_q); - r = SCMutexUnlock(&packet_q.mutex_q); + SCMutexLock(&packet_q.mutex_q); + while ((p = PacketDequeue(&packet_q)) == NULL) { + SCondWait(&packet_q.cond_q, &packet_q.mutex_q); + } + SCMutexUnlock(&packet_q.mutex_q); +/* if (p == NULL) { TmqDebugList(); @@ -240,7 +241,7 @@ Packet *SetupPkt (void) SCLogDebug("allocated a new packet..."); } - +*/ /* reset the packet csum fields */ RESET_PACKET_CSUMS(p); @@ -280,29 +281,6 @@ Packet *TunnelPktSetup(ThreadVars *t, DecodeThreadVars *dtv, Packet *parent, uin /* get us a packet */ Packet *p = SetupPkt(); - int r = 0; -#if 0 - do { - r = SCMutexLock(&packet_q.mutex_q); - p = PacketDequeue(&packet_q); - SCMutexUnlock(&packet_q.mutex_q); - - if (p == NULL) { - //TmqDebugList(); - usleep(1000); /* sleep 1ms */ - - /* XXX check for recv'd signals, so - * we can exit on signals received */ - } - } while (p == NULL); -#endif - r = SCMutexLock(&mutex_pending); - pending++; -#ifdef DBG_PERF - if (pending > dbg_maxpending) - dbg_maxpending = pending; -#endif /* DBG_PERF */ - SCMutexUnlock(&mutex_pending); /* set the root ptr to the lowest layer */ if (parent->root != NULL) @@ -1043,10 +1021,10 @@ int main(int argc, char **argv) if (sigflags & SURICATA_SIGTERM || sigflags & SURICATA_KILL) break; - SCMutexLock(&mutex_pending); - if (pending == 0) + SCMutexLock(&packet_q.mutex_q); + if (packet_q.len == max_pending_packets) done = 1; - SCMutexUnlock(&mutex_pending); + SCMutexUnlock(&packet_q.mutex_q); if (done == 0) { usleep(100); diff --git a/src/tmqh-packetpool.c b/src/tmqh-packetpool.c index 94e6baba13..8ef5cf8d39 100644 --- a/src/tmqh-packetpool.c +++ b/src/tmqh-packetpool.c @@ -147,6 +147,7 @@ void TmqhOutputPacketpool(ThreadVars *t, Packet *p) SCMutexLock(&q->mutex_q); PacketEnqueue(q, p->root); + SCCondSignal(&q->cond_q); SCMutexUnlock(&q->mutex_q); } @@ -154,25 +155,8 @@ void TmqhOutputPacketpool(ThreadVars *t, Packet *p) SCMutexLock(&q->mutex_q); PacketEnqueue(q, p); + SCCondSignal(&q->cond_q); SCMutexUnlock(&q->mutex_q); - - SCMutexLock(&mutex_pending); - //printf("TmqhOutputPacketpool: pending %" PRIu32 "\n", pending); - if (pending > 0) { - pending--; - if (proot) { - if (pending > 0) { - pending--; - } else { - printf("TmqhOutputPacketpool: warning, trying to subtract from 0 pending counter (tunnel root).\n"); - } - } - } else { - printf("TmqhOutputPacketpool: warning, trying to subtract from 0 pending counter.\n"); - } - if (pending <= max_pending_packets) - SCCondSignal(&cond_pending); - SCMutexUnlock(&mutex_pending); } /**