ofproto-dpif: Guard rule statistics with a mutex.
authorEthan Jackson <ethan@nicira.com>
Sat, 3 Aug 2013 20:13:26 +0000 (13:13 -0700)
committerEthan Jackson <ethan@nicira.com>
Mon, 5 Aug 2013 19:40:14 +0000 (12:40 -0700)
Signed-off-by: Ethan Jackson <ethan@nicira.com>
Acked-by: Ben Pfaff <blp@nicira.com>
ofproto/ofproto-dpif.c
ofproto/ofproto-dpif.h

index d9ff182..79601a3 100644 (file)
@@ -4864,9 +4864,11 @@ push_all_stats(void)
 void
 rule_credit_stats(struct rule_dpif *rule, const struct dpif_flow_stats *stats)
 {
+    ovs_mutex_lock(&rule->stats_mutex);
     rule->packet_count += stats->n_packets;
     rule->byte_count += stats->n_bytes;
     ofproto_rule_update_used(&rule->up, stats->used);
+    ovs_mutex_unlock(&rule->stats_mutex);
 }
 \f
 /* Subfacets. */
@@ -5224,16 +5226,21 @@ static enum ofperr
 rule_construct(struct rule *rule_)
 {
     struct rule_dpif *rule = rule_dpif_cast(rule_);
+    ovs_mutex_init(&rule->stats_mutex, PTHREAD_MUTEX_NORMAL);
+    ovs_mutex_lock(&rule->stats_mutex);
     rule->packet_count = 0;
     rule->byte_count = 0;
+    ovs_mutex_unlock(&rule->stats_mutex);
     complete_operation(rule);
     return 0;
 }
 
 static void
-rule_destruct(struct rule *rule)
+rule_destruct(struct rule *rule_)
 {
-    complete_operation(rule_dpif_cast(rule));
+    struct rule_dpif *rule = rule_dpif_cast(rule_);
+    complete_operation(rule);
+    ovs_mutex_destroy(&rule->stats_mutex);
 }
 
 static void
@@ -5249,8 +5256,10 @@ rule_get_stats(struct rule *rule_, uint64_t *packets, uint64_t *bytes)
 
     /* Start from historical data for 'rule' itself that are no longer tracked
      * in facets.  This counts, for example, facets that have expired. */
+    ovs_mutex_lock(&rule->stats_mutex);
     *packets = rule->packet_count;
     *bytes = rule->byte_count;
+    ovs_mutex_unlock(&rule->stats_mutex);
 }
 
 static void
index 0c6af62..18b1829 100644 (file)
@@ -19,6 +19,7 @@
 
 #include "hmapx.h"
 #include "ofproto/ofproto-provider.h"
+#include "ovs-thread.h"
 #include "timer.h"
 #include "util.h"
 
@@ -42,8 +43,9 @@ struct rule_dpif {
      *     packet_count or byte_count member or that can be obtained from the
      *     datapath by, e.g., dpif_flow_get() for any subfacet.
      */
-    uint64_t packet_count;       /* Number of packets received. */
-    uint64_t byte_count;         /* Number of bytes received. */
+    struct ovs_mutex stats_mutex;
+    uint64_t packet_count OVS_GUARDED;  /* Number of packets received. */
+    uint64_t byte_count OVS_GUARDED;    /* Number of bytes received. */
 };
 
 static inline struct rule_dpif *rule_dpif_cast(const struct rule *rule)