[ovs-dev] [threads 24/28] vlog: Make vlog_should_drop() thread-safe.

Ben Pfaff blp at nicira.com
Wed Jul 10 23:04:06 UTC 2013


Signed-off-by: Ben Pfaff <blp at nicira.com>
---
 lib/vlog.c |   14 ++++++++++----
 lib/vlog.h |    3 +++
 2 files changed, 13 insertions(+), 4 deletions(-)

diff --git a/lib/vlog.c b/lib/vlog.c
index b07fd91..5fa1c6c 100644
--- a/lib/vlog.c
+++ b/lib/vlog.c
@@ -917,6 +917,7 @@ vlog_should_drop(const struct vlog_module *module, enum vlog_level level,
         return true;
     }
 
+    xpthread_mutex_lock(&rl->mutex);
     if (!token_bucket_withdraw(&rl->token_bucket, VLOG_MSG_TOKENS)) {
         time_t now = time_now();
         if (!rl->n_dropped) {
@@ -924,21 +925,26 @@ vlog_should_drop(const struct vlog_module *module, enum vlog_level level,
         }
         rl->last_dropped = now;
         rl->n_dropped++;
+        xpthread_mutex_unlock(&rl->mutex);
         return true;
     }
 
-    if (rl->n_dropped) {
+    if (!rl->n_dropped) {
+        xpthread_mutex_unlock(&rl->mutex);
+    } else {
         time_t now = time_now();
+        unsigned int n_dropped = rl->n_dropped;
         unsigned int first_dropped_elapsed = now - rl->first_dropped;
         unsigned int last_dropped_elapsed = now - rl->last_dropped;
+        rl->n_dropped = 0;
+        xpthread_mutex_unlock(&rl->mutex);
 
         vlog(module, level,
              "Dropped %u log messages in last %u seconds (most recently, "
              "%u seconds ago) due to excessive rate",
-             rl->n_dropped, first_dropped_elapsed, last_dropped_elapsed);
-
-        rl->n_dropped = 0;
+             n_dropped, first_dropped_elapsed, last_dropped_elapsed);
     }
+
     return false;
 }
 
diff --git a/lib/vlog.h b/lib/vlog.h
index c7ab206..b2e1d49 100644
--- a/lib/vlog.h
+++ b/lib/vlog.h
@@ -22,6 +22,7 @@
 #include <stdbool.h>
 #include <time.h>
 #include "compiler.h"
+#include "ovs-thread.h"
 #include "sat-math.h"
 #include "token-bucket.h"
 #include "util.h"
@@ -94,6 +95,7 @@ struct vlog_rate_limit {
     time_t first_dropped;       /* Time first message was dropped. */
     time_t last_dropped;        /* Time of most recent message drop. */
     unsigned int n_dropped;     /* Number of messages dropped. */
+    pthread_mutex_t mutex;      /* Mutual exclusion for rate limit. */
 };
 
 /* Number of tokens to emit a message.  We add 'rate' tokens per millisecond,
@@ -108,6 +110,7 @@ struct vlog_rate_limit {
             0,                              /* first_dropped */         \
             0,                              /* last_dropped */          \
             0,                              /* n_dropped */             \
+            PTHREAD_ADAPTIVE_MUTEX_INITIALIZER /* mutex */              \
         }
 
 /* Configuring how each module logs messages. */
-- 
1.7.2.5




More information about the dev mailing list