#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);
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
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
/* 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;
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();
}
}
+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
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;
/* 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;
}
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;
+}