From 0e124db81f6374ce37b3e73b066d33f534c96327 Mon Sep 17 00:00:00 2001 From: Jakub Sitnicki Date: Wed, 28 Mar 2018 16:35:09 -0500 Subject: [PATCH] stopwatch: Add API for waiting until samples have been processed Will be used for testing the module. Signed-off-by: Jakub Sitnicki Signed-off-by: Mark Michelson Signed-off-by: Ben Pfaff --- lib/stopwatch.c | 34 ++++++++++++++++++++++++++-------- lib/stopwatch.h | 3 +++ 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/lib/stopwatch.c b/lib/stopwatch.c index 7f036e894c1..4cce7d9115d 100644 --- a/lib/stopwatch.c +++ b/lib/stopwatch.c @@ -66,6 +66,7 @@ struct stopwatch { enum stopwatch_op { OP_START_SAMPLE, OP_END_SAMPLE, + OP_SYNC, OP_RESET, OP_SHUTDOWN, }; @@ -78,6 +79,7 @@ struct stopwatch_packet { static struct shash stopwatches = SHASH_INITIALIZER(&stopwatches); static struct ovs_mutex stopwatches_lock = OVS_MUTEX_INITIALIZER; +static pthread_cond_t stopwatches_sync = PTHREAD_COND_INITIALIZER; static int stopwatch_pipe[2]; static pthread_t stopwatch_thread_id; @@ -233,12 +235,12 @@ add_sample(struct stopwatch *sw, unsigned long long new_sample) } static bool -performance_get_stats_protected(const char *name, - struct performance_stats *stats) +stopwatch_get_stats_protected(const char *name, + struct stopwatch_stats *stats) { - struct performance *perf; + struct stopwatch *perf; - perf = shash_find_data(&performances, name); + perf = shash_find_data(&stopwatches, name); if (!perf) { return false; } @@ -255,13 +257,13 @@ performance_get_stats_protected(const char *name, } bool -performance_get_stats(const char *name, struct performance_stats *stats) +stopwatch_get_stats(const char *name, struct stopwatch_stats *stats) { bool found = false; - ovs_mutex_lock(&performances_lock); - found = performance_get_stats_protected(name, stats); - ovs_mutex_unlock(&performances_lock); + ovs_mutex_lock(&stopwatches_lock); + found = stopwatch_get_stats_protected(name, stats); + ovs_mutex_unlock(&stopwatches_lock); return found; } @@ -413,6 +415,9 @@ stopwatch_thread(void *ign OVS_UNUSED) case OP_END_SAMPLE: stopwatch_end_sample_protected(&pkt); break; + case OP_SYNC: + xpthread_cond_signal(&stopwatches_sync); + break; case OP_RESET: stopwatch_reset_protected(&pkt); break; @@ -515,3 +520,16 @@ stopwatch_stop(const char *name, unsigned long long ts) ovs_strlcpy(pkt.name, name, sizeof(pkt.name)); write(stopwatch_pipe[1], &pkt, sizeof(pkt)); } + +void +stopwatch_sync(void) +{ + struct stopwatch_packet pkt = { + .op = OP_SYNC, + }; + + ovs_mutex_lock(&stopwatches_lock); + write(stopwatch_pipe[1], &pkt, sizeof(pkt)); + ovs_mutex_cond_wait(&stopwatches_sync, &stopwatches_lock); + ovs_mutex_unlock(&stopwatches_lock); +} diff --git a/lib/stopwatch.h b/lib/stopwatch.h index fac5de2c62d..6ee7291d923 100644 --- a/lib/stopwatch.h +++ b/lib/stopwatch.h @@ -51,4 +51,7 @@ 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); +/* Block until all enqueued samples have been processed. */ +void stopwatch_sync(void); + #endif /* stopwatch.h */