diff mbox series

[ovs-dev,v6,3/5] stopwatch: Add API for retrieving calculated statistics

Message ID 20180307170115.29137-4-mmichels@redhat.com
State Superseded
Headers show
Series Stopwatch Library | expand

Commit Message

Mark Michelson March 7, 2018, 5:01 p.m. UTC
From: Jakub Sitnicki <jkbs@redhat.com>

Will be used for testing the module.

Signed-off-by: Jakub Sitnicki <jkbs@redhat.com>
Signed-off-by: Mark Michelson <mmichels@redhat.com>
---
 lib/stopwatch.c | 34 ++++++++++++++++++++++++++++++++++
 lib/stopwatch.h | 13 +++++++++++++
 2 files changed, 47 insertions(+)
diff mbox series

Patch

diff --git a/lib/stopwatch.c b/lib/stopwatch.c
index 9b7847bf5..4f36c4692 100644
--- a/lib/stopwatch.c
+++ b/lib/stopwatch.c
@@ -232,6 +232,40 @@  add_sample(struct stopwatch *sw, unsigned long long new_sample)
     calc_average(&sw->long_term, new_sample);
 }
 
+static bool
+performance_get_stats_protected(const char *name,
+                                struct performance_stats *stats)
+{
+    struct performance *perf;
+
+    perf = shash_find_data(&performances, name);
+    if (!perf) {
+        return false;
+    }
+
+    stats->count = perf->samples;
+    stats->unit = perf->units;
+    stats->max = perf->max;
+    stats->min = perf->min;
+    stats->pctl_95 = perf->pctl.percentile;
+    stats->ewma_50 = perf->short_term.average;
+    stats->ewma_1 = perf->long_term.average;
+
+    return true;
+}
+
+bool
+performance_get_stats(const char *name, struct performance_stats *stats)
+{
+    bool found = false;
+
+    ovs_mutex_lock(&performances_lock);
+    found = performance_get_stats_protected(name, stats);
+    ovs_mutex_unlock(&performances_lock);
+
+    return found;
+}
+
 static void
 stopwatch_print(struct stopwatch *sw, const char *name,
                   struct ds *s)
diff --git a/lib/stopwatch.h b/lib/stopwatch.h
index 61f814523..fac5de2c6 100644
--- a/lib/stopwatch.h
+++ b/lib/stopwatch.h
@@ -24,6 +24,16 @@  enum stopwatch_units {
     SW_NS,
 };
 
+struct stopwatch_stats {
+    unsigned long long count;    /* Total number of samples. */
+    enum stopwatch_units unit; /* Unit of following values. */
+    unsigned long long max;      /* Maximum value. */
+    unsigned long long min;      /* Minimum value. */
+    double pctl_95;              /* 95th percentile. */
+    double ewma_50;              /* Exponentially weighted moving average (alpha 0.50). */
+    double ewma_1;               /* Exponentially weighted moving average (alpha 0.01). */
+};
+
 /* Create a new stopwatch.
  * The "units" are not used for any calculations but are printed when
  * statistics are requested.
@@ -38,4 +48,7 @@  void stopwatch_start(const char *name, unsigned long long ts);
  */
 void stopwatch_stop(const char *name, unsigned long long ts);
 
+/* Retrieve statistics calculated from collected samples */
+bool stopwatch_get_stats(const char *name, struct stopwatch_stats *stats);
+
 #endif /* stopwatch.h */