vlog: Make vlog_should_drop() thread-safe.
authorBen Pfaff <blp@nicira.com>
Tue, 9 Apr 2013 18:18:01 +0000 (11:18 -0700)
committerBen Pfaff <blp@nicira.com>
Fri, 12 Jul 2013 21:31:35 +0000 (14:31 -0700)
Signed-off-by: Ben Pfaff <blp@nicira.com>
Acked-by: Ed Maste <emaste@freebsd.org>
lib/vlog.c
lib/vlog.h

index bbc7c55..f6f1676 100644 (file)
@@ -907,6 +907,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) {
@@ -914,21 +915,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;
 }
 
index c7ab206..b2e1d49 100644 (file)
@@ -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. */