|
| 1 | +/* |
| 2 | + * Copyright (c) 2015, Charlie Curtsinger and Emery Berger, |
| 3 | + * University of Massachusetts Amherst |
| 4 | + * This file is part of the Coz project. See LICENSE.md file at the top-level |
| 5 | + * directory of this distribution and at http://github.com/plasma-umass/coz. |
| 6 | + */ |
| 7 | + |
| 8 | +// Like benchmarks/toy, but the main thread joins its workers through a |
| 9 | +// semaphore rather than pthread_join. |
| 10 | +// |
| 11 | +// This is the regression test for semaphore interposition. A thread blocked on |
| 12 | +// a semaphore is not running, so it must not be charged for virtual delays |
| 13 | +// inserted while it slept. Before libcoz wrapped sem_wait/semaphore_wait, the |
| 14 | +// main thread -- the one that visits the progress point -- paid all of them on |
| 15 | +// wake-up, and the profile came out with a slope near zero or negative |
| 16 | +// (measured: +0.13 with R^2 0.01, and -0.57 with R^2 0.18) instead of the ~1.0 |
| 17 | +// this program should show. |
| 18 | +// |
| 19 | +// Both loops inline the same xorshift, so the expected result is a single hot |
| 20 | +// line with a slope near 1.0: removing that work removes the program. |
| 21 | +#include <coz.h> |
| 22 | +#include <pthread.h> |
| 23 | +#include <stdio.h> |
| 24 | +#include <stdint.h> |
| 25 | + |
| 26 | +#ifdef __APPLE__ |
| 27 | +#include <dispatch/dispatch.h> |
| 28 | +static dispatch_semaphore_t done; |
| 29 | +static void sem_setup() { done = dispatch_semaphore_create(0); } |
| 30 | +static void sem_wait_one() { dispatch_semaphore_wait(done, DISPATCH_TIME_FOREVER); } |
| 31 | +static void sem_signal() { dispatch_semaphore_signal(done); } |
| 32 | +#else |
| 33 | +#include <semaphore.h> |
| 34 | +static sem_t done; |
| 35 | +static void sem_setup() { sem_init(&done, 0, 0); } |
| 36 | +static void sem_wait_one() { sem_wait(&done); } |
| 37 | +static void sem_signal() { sem_post(&done); } |
| 38 | +#endif |
| 39 | + |
| 40 | +static const uint64_t kIterations = 40000000ULL; |
| 41 | +static volatile uint64_t slow_sink, fast_sink; |
| 42 | + |
| 43 | +static uint64_t xorshift(uint64_t v) { |
| 44 | + v ^= v << 13; v ^= v >> 7; v ^= v << 17; return v; |
| 45 | +} |
| 46 | + |
| 47 | +static void* slow_work(void*) { |
| 48 | + uint64_t acc = 0x9E3779B97F4A7C15ULL; |
| 49 | + for (uint64_t i = 0; i < kIterations; i++) acc = xorshift(acc); |
| 50 | + slow_sink = acc; |
| 51 | + sem_signal(); |
| 52 | + return nullptr; |
| 53 | +} |
| 54 | + |
| 55 | +static void* fast_work(void*) { |
| 56 | + uint64_t acc = 0x9E3779B97F4A7C15ULL; |
| 57 | + for (uint64_t i = 0; i < kIterations / 2; i++) acc = xorshift(acc); |
| 58 | + fast_sink = acc; |
| 59 | + sem_signal(); |
| 60 | + return nullptr; |
| 61 | +} |
| 62 | + |
| 63 | +int main() { |
| 64 | + sem_setup(); |
| 65 | + printf("Starting.\n"); |
| 66 | + for (int round = 0; round < 100; round++) { |
| 67 | + pthread_t a, b; |
| 68 | + pthread_create(&a, nullptr, slow_work, nullptr); |
| 69 | + pthread_create(&b, nullptr, fast_work, nullptr); |
| 70 | + sem_wait_one(); // main blocks here -- invisible to coz without the wrappers |
| 71 | + sem_wait_one(); |
| 72 | + pthread_detach(a); |
| 73 | + pthread_detach(b); |
| 74 | + COZ_PROGRESS; |
| 75 | + printf("."); fflush(stdout); |
| 76 | + } |
| 77 | + printf("\nDone. %llu %llu\n", (unsigned long long)slow_sink, (unsigned long long)fast_sink); |
| 78 | +} |
0 commit comments