|
| 1 | +#include <stdbool.h> |
| 2 | +#include <stdint.h> |
| 3 | +#include <sys/time.h> |
| 4 | +#include <unistd.h> |
| 5 | + |
| 6 | +#include "bench.h" |
| 7 | + |
| 8 | +static void barrier_init(barrier_t *b, int n) |
| 9 | +{ |
| 10 | + pthread_cond_init(&b->complete, NULL); |
| 11 | + pthread_mutex_init(&b->mutex, NULL); |
| 12 | + b->count = n; |
| 13 | + b->crossing = 0; |
| 14 | +} |
| 15 | + |
| 16 | +static void barrier_cross(barrier_t *b) |
| 17 | +{ |
| 18 | + pthread_mutex_lock(&b->mutex); |
| 19 | + b->crossing++; |
| 20 | + if (b->crossing < b->count) |
| 21 | + pthread_cond_wait(&b->complete, &b->mutex); |
| 22 | + else { |
| 23 | + pthread_cond_broadcast(&b->complete); |
| 24 | + b->crossing = 0; |
| 25 | + } |
| 26 | + pthread_mutex_unlock(&b->mutex); |
| 27 | +} |
| 28 | + |
| 29 | +#define DEFAULT_DURATION 1000 |
| 30 | +#define DEFAULT_NTHREADS 64 |
| 31 | +#define DEFAULT_ISIZE 256 |
| 32 | +#define DEFAULT_VRANGE 512 |
| 33 | + |
| 34 | +static volatile bool should_stop = false; |
| 35 | + |
| 36 | +static void *bench_thread(void *data) |
| 37 | +{ |
| 38 | + pthread_data_t *d = (pthread_data_t *) data; |
| 39 | + |
| 40 | + barrier_cross(d->barrier); |
| 41 | + while (!should_stop) { |
| 42 | + int from = rand_r(&d->seed) & 0x1; |
| 43 | + int key = rand_r(&d->seed) % d->range; |
| 44 | + list_move(key, d, from); |
| 45 | + d->n_move++; |
| 46 | + } |
| 47 | + |
| 48 | + return NULL; |
| 49 | +} |
| 50 | + |
| 51 | +int main(void) |
| 52 | +{ |
| 53 | + int duration = DEFAULT_DURATION; |
| 54 | + int n_threads = DEFAULT_NTHREADS; |
| 55 | + int init_size = DEFAULT_ISIZE; |
| 56 | + int value_range = DEFAULT_VRANGE; |
| 57 | + |
| 58 | + printf("List move benchmark\n"); |
| 59 | + printf("Test time: %d\n", duration); |
| 60 | + printf("Thread number: %d\n", n_threads); |
| 61 | + printf("Initial size: %d\n", init_size); |
| 62 | + printf("Value range: %d\n", value_range); |
| 63 | + |
| 64 | + struct timespec timeout = {.tv_sec = duration / 1000, |
| 65 | + .tv_nsec = (duration % 1000) * 1000000}; |
| 66 | + |
| 67 | + pthread_t *threads; |
| 68 | + if (!(threads = malloc(n_threads * sizeof(pthread_t)))) { |
| 69 | + printf("failed to malloc pthread_t\n"); |
| 70 | + goto out; |
| 71 | + } |
| 72 | + |
| 73 | + pthread_data_t **data; |
| 74 | + if (!(data = malloc(n_threads * sizeof(pthread_data_t *)))) { |
| 75 | + printf("failed to malloc pthread_data_t\n"); |
| 76 | + goto out; |
| 77 | + } |
| 78 | + for (int i = 0; i < n_threads; i++) { |
| 79 | + if ((data[i] = alloc_pthread_data()) == NULL) { |
| 80 | + printf("failed to malloc pthread_data_t %d\n", i); |
| 81 | + goto out; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + srand(getpid() ^ (uintptr_t) main); |
| 86 | + |
| 87 | + void *list; |
| 88 | + if (!(list = list_global_init(init_size, value_range))) { |
| 89 | + printf("failed to do list_global_init\n"); |
| 90 | + goto out; |
| 91 | + } |
| 92 | + |
| 93 | + barrier_t barrier; |
| 94 | + pthread_attr_t attr; |
| 95 | + barrier_init(&barrier, n_threads + 1); |
| 96 | + pthread_attr_init(&attr); |
| 97 | + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); |
| 98 | + for (int i = 0; i < n_threads; i++) { |
| 99 | + data[i]->id = i; |
| 100 | + data[i]->n_move = 0; |
| 101 | + data[i]->range = value_range; |
| 102 | + data[i]->seed = rand(); |
| 103 | + data[i]->barrier = &barrier; |
| 104 | + data[i]->list = list; |
| 105 | + if (list_thread_init(data[i])) { |
| 106 | + printf("Failed to do list_thread_init\n"); |
| 107 | + goto out; |
| 108 | + } |
| 109 | + if (pthread_create(&threads[i], &attr, bench_thread, |
| 110 | + (void *) (data[i])) != 0) { |
| 111 | + printf("Failed to create thread %d\n", i); |
| 112 | + goto out; |
| 113 | + } |
| 114 | + } |
| 115 | + pthread_attr_destroy(&attr); |
| 116 | + |
| 117 | + barrier_cross(&barrier); |
| 118 | + |
| 119 | + struct timeval start, end; |
| 120 | + gettimeofday(&start, NULL); |
| 121 | + nanosleep(&timeout, NULL); |
| 122 | + should_stop = true; |
| 123 | + gettimeofday(&end, NULL); |
| 124 | + |
| 125 | + for (int i = 0; i < n_threads; i++) { |
| 126 | + if (pthread_join(threads[i], NULL) != 0) { |
| 127 | + printf("Failed to join child thread %d\n", i); |
| 128 | + goto out; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + duration = (end.tv_sec * 1000 + end.tv_usec / 1000) - |
| 133 | + (start.tv_sec * 1000 + start.tv_usec / 1000); |
| 134 | + unsigned long n_move = 0; |
| 135 | + for (int i = 0; i < n_threads; i++) |
| 136 | + n_move += (data[i]->n_move); |
| 137 | + |
| 138 | + printf("\tduration: %d ms\n", duration); |
| 139 | + printf("\tops/second %lu (%f/s)\n", n_move, |
| 140 | + n_move * (1000.0) / duration); |
| 141 | + |
| 142 | + for (int i = 0; i < n_threads; i++) |
| 143 | + free_pthread_data(data[i]); |
| 144 | + list_global_exit(list); |
| 145 | + free(data); |
| 146 | + free(threads); |
| 147 | + |
| 148 | +out: |
| 149 | + return 0; |
| 150 | +} |
0 commit comments