Skip to content

Commit

Permalink
Micro-benchmark for waking condvar on which no-one is waiting
Browse files Browse the repository at this point in the history
This patch adds to tst-condvar two benchmark for measuring
condvar::wake_all() on a condvar that nobody is waiting on.

The first benchmark does these wakes from a single thread, measuring
26ns before commit 3509b19, and
only 3ns after it.

The second benchmark does wake_all() loops from two threads on two
different CPUs. Before the aforementioned commit, this frequently
involved a contented mutex and context switches, with as much as
30,000 ns delay. After that commit, this benchmark measures 3ns,
the same as the single-threaded benchmark.
  • Loading branch information
nyh committed Jul 18, 2013
1 parent 3509b19 commit 0080ee6
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions tests/tst-condvar.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#include <atomic>

#include "sched.hh"
#include "debug.hh"
#include <drivers/clock.hh>

#include <osv/condvar.h>

Expand Down Expand Up @@ -131,6 +134,41 @@ int main(int argc, char **argv)
}
assert_idle(&cond);

debug("Measuring unwaited wake_all (one thread): ");
int iterations = 100000000;
condvar cv;
auto start = nanotime();
for (int i = 0; i < iterations; i++) {
cv.wake_all();
}
auto end = nanotime();
debug ("%d ns\n", (end-start)/iterations);


debug("Measuring unwaited wake_all (two threads): ");
iterations = 100000000;
unsigned int nthreads = 2;
assert(sched::cpus.size() >= nthreads);
sched::thread *threads2[nthreads];
std::atomic<u64> time(0);
for(unsigned int i = 0; i < nthreads; i++) {
threads2[i]= new sched::thread([iterations, &cv, &time] {
auto start = nanotime();
for (int j = 0; j < iterations; j++) {
cv.wake_all();
}
auto end = nanotime();
time += (end-start);
}, sched::thread::attr(sched::cpus[i]));
threads2[i]->start();
}
for(unsigned int i = 0; i < nthreads; i++) {
threads2[i]->join();
delete threads2[i];
}
debug ("%d ns\n", time/iterations/nthreads);


debug("condvar tests succeeded\n");
return 0;
}

0 comments on commit 0080ee6

Please sign in to comment.