]> git.proxmox.com Git - mirror_ovs.git/blobdiff - lib/coverage.c
netdev-offload-dpdk: Refactor action items freeing scheme.
[mirror_ovs.git] / lib / coverage.c
index 5a20d6ec3f482a862c053eab19f299075cf6abc7..a95b6aa255468217cd26d5c02fbd29a60b6fc2fa 100644 (file)
 #include "coverage.h"
 #include <inttypes.h>
 #include <stdlib.h>
-#include "dynamic-string.h"
+#include "openvswitch/dynamic-string.h"
 #include "hash.h"
 #include "svec.h"
 #include "timeval.h"
 #include "unixctl.h"
 #include "util.h"
-#include "vlog.h"
+#include "openvswitch/vlog.h"
 
 VLOG_DEFINE_THIS_MODULE(coverage);
 
@@ -44,6 +44,8 @@ static unsigned int idx_count = 0;
 static void coverage_read(struct svec *);
 static unsigned int coverage_array_sum(const unsigned int *arr,
                                        const unsigned int len);
+static bool coverage_read_counter(const char *name,
+                                  unsigned long long int *count);
 
 /* Registers a coverage counter with the coverage core */
 void
@@ -72,11 +74,32 @@ coverage_unixctl_show(struct unixctl_conn *conn, int argc OVS_UNUSED,
     svec_destroy(&lines);
 }
 
+static void
+coverage_unixctl_read_counter(struct unixctl_conn *conn, int argc OVS_UNUSED,
+                              const char *argv[], void *aux OVS_UNUSED)
+{
+    unsigned long long count;
+    char *reply;
+    bool ok;
+
+    ok = coverage_read_counter(argv[1], &count);
+    if (!ok) {
+        unixctl_command_reply_error(conn, "No such counter");
+        return;
+    }
+
+    reply = xasprintf("%llu\n", count);
+    unixctl_command_reply(conn, reply);
+    free(reply);
+}
+
 void
 coverage_init(void)
 {
     unixctl_command_register("coverage/show", "", 0, 0,
                              coverage_unixctl_show, NULL);
+    unixctl_command_register("coverage/read-counter", "COUNTER", 1, 1,
+                             coverage_unixctl_read_counter, NULL);
 }
 
 /* Sorts coverage counters in descending order by total, within equal
@@ -245,9 +268,14 @@ coverage_read(struct svec *lines)
 
 /* Runs approximately every COVERAGE_CLEAR_INTERVAL amount of time to
  * synchronize per-thread counters with global counters. Every thread maintains
- * a separate timer to ensure all counters are periodically aggregated. */
-void
-coverage_clear(void)
+ * a separate timer to ensure all counters are periodically aggregated.
+ *
+ * Uses 'ovs_mutex_trylock()' if 'trylock' is true.  This is to prevent
+ * multiple performance-critical threads contending over the 'coverage_mutex'.
+ *
+ * */
+static void
+coverage_clear__(bool trylock)
 {
     long long int now, *thread_time;
 
@@ -262,7 +290,15 @@ coverage_clear(void)
     if (now >= *thread_time) {
         size_t i;
 
-        ovs_mutex_lock(&coverage_mutex);
+        if (trylock) {
+            /* Returns if cannot acquire lock. */
+            if (ovs_mutex_trylock(&coverage_mutex)) {
+                return;
+            }
+        } else {
+            ovs_mutex_lock(&coverage_mutex);
+        }
+
         for (i = 0; i < n_coverage_counters; i++) {
             struct coverage_counter *c = coverage_counters[i];
             c->total += c->count();
@@ -272,6 +308,18 @@ coverage_clear(void)
     }
 }
 
+void
+coverage_clear(void)
+{
+    coverage_clear__(false);
+}
+
+void
+coverage_try_clear(void)
+{
+    coverage_clear__(true);
+}
+
 /* Runs approximately every COVERAGE_RUN_INTERVAL amount of time to update the
  * coverage counters' 'min' and 'hr' array.  'min' array is for cumulating
  * per second counts into per minute count.  'hr' array is for cumulating per
@@ -279,8 +327,6 @@ coverage_clear(void)
 void
 coverage_run(void)
 {
-    /* Defines the moving average array index variables. */
-    static unsigned int min_idx, hr_idx;
     struct coverage_counter **c = coverage_counters;
     long long int now;
 
@@ -330,8 +376,6 @@ coverage_run(void)
 
         /* Updates the global index variables. */
         idx_count = (idx_count + slots) % (MIN_AVG_LEN * HR_AVG_LEN);
-        min_idx = idx_count % MIN_AVG_LEN;
-        hr_idx  = idx_count / MIN_AVG_LEN;
         /* Updates the run time. */
         coverage_run_time = now + COVERAGE_RUN_INTERVAL;
     }
@@ -351,3 +395,22 @@ coverage_array_sum(const unsigned int *arr, const unsigned int len)
     ovs_mutex_unlock(&coverage_mutex);
     return sum;
 }
+
+static bool
+coverage_read_counter(const char *name, unsigned long long int *count)
+{
+    for (size_t i = 0; i < n_coverage_counters; i++) {
+        struct coverage_counter *c = coverage_counters[i];
+
+        if (!strcmp(c->name, name)) {
+            ovs_mutex_lock(&coverage_mutex);
+            c->total += c->count();
+            *count = c->total;
+            ovs_mutex_unlock(&coverage_mutex);
+
+            return true;
+        }
+    }
+
+    return false;
+}