1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
|
/* Copyright 2016 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include "platform.h"
#include "nsync.h"
#include "smprintf.h"
#include "testing.h"
#include "closure.h"
NSYNC_CPP_USING_
/* The benchmarks in this file use various mechanisms to
ping-pong back and forth between two threads as they count i from
0 to limit.
The data structure contains multiple synchronization primitives,
but each benchmark uses only those it needs.
The setting of GOMAXPROCS, and the exact choices of the thread scheduler can
have great effect on the timings. */
typedef struct ping_pong_s {
nsync_mu mu;
nsync_cv cv[2];
pthread_mutex_t mutex;
pthread_rwlock_t rwmutex;
pthread_cond_t cond[2];
pthread_cond_t done_cond;
int outstanding;
int i;
int limit;
} ping_pong;
static void ping_pong_init (ping_pong *pp, int limit) {
memset ((void *) pp, 0, sizeof (*pp));
pthread_mutex_init (&pp->mutex, NULL);
pthread_rwlock_init (&pp->rwmutex, NULL);
pthread_cond_init (&pp->cond[0], NULL);
pthread_cond_init (&pp->cond[1], NULL);
pthread_cond_init (&pp->done_cond, NULL);
pp->outstanding = 2;
pp->limit = limit;
}
static void ping_pong_done (ping_pong *pp) {
pthread_mutex_lock (&pp->mutex);
pp->outstanding--;
if (pp->outstanding == 0) {
pthread_cond_broadcast (&pp->done_cond);
}
pthread_mutex_unlock (&pp->mutex);
}
static void ping_pong_destroy (ping_pong *pp) {
pthread_mutex_lock (&pp->mutex);
while (pp->outstanding != 0) {
pthread_cond_wait (&pp->done_cond, &pp->mutex);
}
pthread_mutex_unlock (&pp->mutex);
pthread_mutex_destroy (&pp->mutex);
pthread_rwlock_destroy (&pp->rwmutex);
pthread_cond_destroy (&pp->cond[0]);
pthread_cond_destroy (&pp->cond[1]);
pthread_cond_destroy (&pp->done_cond);
}
/* --------------------------------------- */
CLOSURE_DECL_BODY2 (ping_pong, ping_pong *, int)
/* void pthread_mutex_lock */
static void void_pthread_mutex_lock (void *mu) {
pthread_mutex_lock ((pthread_mutex_t *) mu);
}
/* void pthread_mutex_unlock */
static void void_pthread_mutex_unlock (void *mu) {
pthread_mutex_unlock ((pthread_mutex_t *) mu);
}
/* Run by each thread in benchmark_ping_pong_mutex_cv(). */
static void mutex_cv_ping_pong (ping_pong *pp, int parity) {
pthread_mutex_lock (&pp->mutex);
while (pp->i < pp->limit) {
while ((pp->i & 1) == parity) {
nsync_cv_wait_with_deadline_generic (&pp->cv[parity], &pp->mutex,
&void_pthread_mutex_lock,
&void_pthread_mutex_unlock,
nsync_time_no_deadline, NULL);
}
pp->i++;
nsync_cv_signal (&pp->cv[1-parity]);
}
pthread_mutex_unlock (&pp->mutex);
ping_pong_done (pp);
}
/* Measure the wakeup speed of pthread_mutex_t/nsync_cv used to ping-pong back and
forth between two threads. */
static void benchmark_ping_pong_mutex_cv (testing t) {
ping_pong pp;
ping_pong_init (&pp, testing_n (t));
closure_fork (closure_ping_pong (&mutex_cv_ping_pong, &pp, 0));
mutex_cv_ping_pong (&pp, 1);
ping_pong_destroy (&pp);
}
/* --------------------------------------- */
/* Run by each thread in benchmark_ping_pong_mu_cv(). */
static void mu_cv_ping_pong (ping_pong *pp, int parity) {
nsync_mu_lock (&pp->mu);
while (pp->i < pp->limit) {
while ((pp->i & 1) == parity) {
nsync_cv_wait (&pp->cv[parity], &pp->mu);
}
pp->i++;
nsync_cv_signal (&pp->cv[1-parity]);
}
nsync_mu_unlock (&pp->mu);
ping_pong_done (pp);
}
/* Measure the wakeup speed of nsync_mu/nsync_cv used to
ping-pong back and forth between two threads. */
static void benchmark_ping_pong_mu_cv (testing t) {
ping_pong pp;
ping_pong_init (&pp, testing_n (t));
closure_fork (closure_ping_pong (&mu_cv_ping_pong, &pp, 0));
mu_cv_ping_pong (&pp, 1);
ping_pong_destroy (&pp);
}
/* --------------------------------------- */
/* Run by each thread in benchmark_ping_pong_mu_cv_unexpired_deadline(). */
static void mu_cv_unexpired_deadline_ping_pong (ping_pong *pp, int parity) {
nsync_time deadline_in1hour;
deadline_in1hour = nsync_time_add (nsync_time_now (), nsync_time_ms (3600000));
nsync_mu_lock (&pp->mu);
while (pp->i < pp->limit) {
while ((pp->i & 1) == parity) {
nsync_cv_wait_with_deadline (&pp->cv[parity], &pp->mu,
deadline_in1hour, NULL);
}
pp->i++;
nsync_cv_signal (&pp->cv[1 - parity]);
}
nsync_mu_unlock (&pp->mu);
ping_pong_done (pp);
}
/* Measure the wakeup speed of nsync_mu/nsync_cv used to ping-pong back and forth
between two threads, with an unexpired deadline pending. */
static void benchmark_ping_pong_mu_cv_unexpired_deadline (testing t) {
ping_pong pp;
ping_pong_init (&pp, testing_n (t));
closure_fork (closure_ping_pong (&mu_cv_unexpired_deadline_ping_pong, &pp, 0));
mu_cv_unexpired_deadline_ping_pong (&pp, 1);
ping_pong_destroy (&pp);
}
/* --------------------------------------- */
/* even_ping_pong and odd_ping_pong are wait conditions used by mu_ping_pong. */
static int even_ping_pong (const void *v) {
return ((((const ping_pong *) v)->i & 1) == 0);
}
static int odd_ping_pong (const void *v) {
return ((((const ping_pong *) v)->i & 1) == 1);
}
typedef int (*condition_func) (const void *v);
static const condition_func condition[] = { &even_ping_pong, &odd_ping_pong };
/* Run by each thread in benchmark_ping_pong_mu_unexpired_deadline(). */
static void mu_unexpired_deadline_ping_pong (ping_pong *pp, int parity) {
nsync_time deadline_in1hour;
deadline_in1hour = nsync_time_add (nsync_time_now (), nsync_time_ms (3600000));
nsync_mu_lock (&pp->mu);
while (pp->i < pp->limit) {
nsync_mu_wait_with_deadline (&pp->mu, condition[parity], pp, NULL,
deadline_in1hour, NULL);
pp->i++;
}
nsync_mu_unlock (&pp->mu);
ping_pong_done (pp);
}
/* Measure the wakeup speed of nsync_mu's wait_with_deadline() primitive used to
ping-pong back and forth between two threads with an unexpired deadline
pending. */
static void benchmark_ping_pong_mu_unexpired_deadline (testing t) {
ping_pong pp;
ping_pong_init (&pp, testing_n (t));
closure_fork (closure_ping_pong (&mu_unexpired_deadline_ping_pong, &pp, 0));
mu_unexpired_deadline_ping_pong (&pp, 1);
ping_pong_destroy (&pp);
}
/* --------------------------------------- */
/* Run by each thread in benchmark_ping_pong_mutex_cond_unexpired_deadline(). */
static void mutex_cond_unexpired_deadline_ping_pong (ping_pong *pp, int parity) {
struct timespec ts;
clock_gettime (CLOCK_REALTIME, &ts);
ts.tv_sec += 3600;
pthread_mutex_lock (&pp->mutex);
while (pp->i < pp->limit) {
while ((pp->i & 1) == parity) {
pthread_cond_timedwait (&pp->cond[parity], &pp->mutex, &ts);
}
pp->i++;
pthread_cond_signal (&pp->cond[1-parity]);
}
pthread_mutex_unlock (&pp->mutex);
ping_pong_done (pp);
}
/* Measure the wakeup speed of pthread_mutex_t/pthread_cond_t used to ping-pong
back and forth between two threads. */
static void benchmark_ping_pong_mutex_cond_unexpired_deadline (testing t) {
ping_pong pp;
ping_pong_init (&pp, testing_n (t));
closure_fork (closure_ping_pong (&mutex_cond_unexpired_deadline_ping_pong, &pp, 0));
mutex_cond_unexpired_deadline_ping_pong (&pp, 1);
ping_pong_destroy (&pp);
}
/* --------------------------------------- */
/* Run by each thread in benchmark_ping_pong_mutex_cond(). */
static void mutex_cond_ping_pong (ping_pong *pp, int parity) {
pthread_mutex_lock (&pp->mutex);
while (pp->i < pp->limit) {
while ((pp->i & 1) == parity) {
pthread_cond_wait (&pp->cond[parity], &pp->mutex);
}
pp->i++;
pthread_cond_signal (&pp->cond[1-parity]);
}
pthread_mutex_unlock (&pp->mutex);
ping_pong_done (pp);
}
/* Measure the wakeup speed of pthread_mutex_t/pthread_cond_t used to ping-pong
back and forth between two threads. */
static void benchmark_ping_pong_mutex_cond (testing t) {
ping_pong pp;
ping_pong_init (&pp, testing_n (t));
closure_fork (closure_ping_pong (&mutex_cond_ping_pong, &pp, 0));
mutex_cond_ping_pong (&pp, 1);
ping_pong_destroy (&pp);
}
/* --------------------------------------- */
/* Run by each thread in benchmark_ping_pong_mu(). */
static void mu_ping_pong (ping_pong *pp, int parity) {
nsync_mu_lock (&pp->mu);
while (pp->i < pp->limit) {
nsync_mu_wait (&pp->mu, condition[parity], pp, NULL);
pp->i++;
}
nsync_mu_unlock (&pp->mu);
ping_pong_done (pp);
}
/* Measure the wakeup speed of nsync_mu's conditional
critical sections, used to ping-pong back and forth between two threads. */
static void benchmark_ping_pong_mu (testing t) {
ping_pong pp;
ping_pong_init (&pp, testing_n (t));
closure_fork (closure_ping_pong (&mu_ping_pong, &pp, 0));
mu_ping_pong (&pp, 1);
ping_pong_destroy (&pp);
}
/* --------------------------------------- */
/* void pthread_rwlock_wrlock */
static void void_pthread_rwlock_wrlock (void *mu) {
pthread_rwlock_wrlock ((pthread_rwlock_t *) mu);
}
/* void pthread_rwlock_unlock */
static void void_pthread_rwlock_unlock (void *mu) {
pthread_rwlock_unlock ((pthread_rwlock_t *) mu);
}
/* Run by each thread in benchmark_ping_pong_rwmutex_cv(). */
static void rw_mutex_cv_ping_pong (ping_pong *pp, int parity) {
pthread_rwlock_wrlock (&pp->rwmutex);
while (pp->i < pp->limit) {
while ((pp->i & 1) == parity) {
nsync_cv_wait_with_deadline_generic (&pp->cv[parity], &pp->rwmutex,
&void_pthread_rwlock_wrlock,
&void_pthread_rwlock_unlock,
nsync_time_no_deadline, NULL);
}
pp->i++;
nsync_cv_signal (&pp->cv[1-parity]);
}
pthread_rwlock_unlock (&pp->rwmutex);
ping_pong_done (pp);
}
/* Measure the wakeup speed of pthread_rwlock_t/nsync_cv used to ping-pong back and
forth between two threads. */
static void benchmark_ping_pong_rwmutex_cv (testing t) {
ping_pong pp;
ping_pong_init (&pp, testing_n (t));
closure_fork (closure_ping_pong (&rw_mutex_cv_ping_pong, &pp, 0));
rw_mutex_cv_ping_pong (&pp, 1);
ping_pong_destroy (&pp);
}
/* --------------------------------------- */
/* Run by each thread in benchmark_ping_pong_wait_n_cv(). */
static void wait_n_cv_ping_pong (ping_pong *pp, int parity) {
struct nsync_waitable_s waitable;
struct nsync_waitable_s *pwaitable = &waitable;
waitable.v = &pp->cv[parity];
waitable.funcs = &nsync_cv_waitable_funcs;
nsync_mu_lock (&pp->mu);
while (pp->i < pp->limit) {
while ((pp->i & 1) == parity) {
nsync_wait_n (&pp->mu, (void (*) (void *)) &nsync_mu_lock,
(void (*) (void *)) &nsync_mu_unlock,
nsync_time_no_deadline, 1, &pwaitable);
}
pp->i++;
nsync_cv_signal (&pp->cv[1 - parity]);
}
nsync_mu_unlock (&pp->mu);
ping_pong_done (pp);
}
/* Measure the wakeup speed of nsync_mu/nsync_cv using nsync_wait_n, used to
ping-pong back and forth between two threads. */
static void benchmark_ping_pong_wait_n_cv (testing t) {
ping_pong pp;
ping_pong_init (&pp, testing_n (t));
closure_fork (closure_ping_pong (&wait_n_cv_ping_pong, &pp, 0));
wait_n_cv_ping_pong (&pp, 1);
ping_pong_destroy (&pp);
}
/* --------------------------------------- */
int main (int argc, char *argv[]) {
testing_base tb = testing_new (argc, argv, 0);
BENCHMARK_RUN (tb, benchmark_ping_pong_mu);
BENCHMARK_RUN (tb, benchmark_ping_pong_mu_unexpired_deadline);
BENCHMARK_RUN (tb, benchmark_ping_pong_mu_cv);
BENCHMARK_RUN (tb, benchmark_ping_pong_mu_cv_unexpired_deadline);
BENCHMARK_RUN (tb, benchmark_ping_pong_mutex_cond);
BENCHMARK_RUN (tb, benchmark_ping_pong_mutex_cond_unexpired_deadline);
BENCHMARK_RUN (tb, benchmark_ping_pong_mutex_cv);
BENCHMARK_RUN (tb, benchmark_ping_pong_rwmutex_cv);
BENCHMARK_RUN (tb, benchmark_ping_pong_wait_n_cv);
return (testing_base_exit (tb));
}
|