1/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to
5 * deal in the Software without restriction, including without limitation the
6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 * sell copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19 * IN THE SOFTWARE.
20 */
21
22#include "task.h"
23#include "uv.h"
24
25#include <stdio.h>
26#include <stdlib.h>
27
28#define NUM_PINGS               (1000 * 1000)
29#define ACCESS_ONCE(type, var)  (*(volatile type*) &(var))
30
31static unsigned int callbacks;
32static volatile int done;
33
34static const char running[] = "running";
35static const char stop[]    = "stop";
36static const char stopped[] = "stopped";
37
38
39static void async_cb(uv_async_t* handle) {
40  if (++callbacks == NUM_PINGS) {
41    /* Tell the pummel thread to stop. */
42    ACCESS_ONCE(const char*, handle->data) = stop;
43
44    /* Wait for the pummel thread to acknowledge that it has stoppped. */
45    while (ACCESS_ONCE(const char*, handle->data) != stopped)
46      uv_sleep(0);
47
48    uv_close((uv_handle_t*) handle, NULL);
49  }
50}
51
52
53static void pummel(void* arg) {
54  uv_async_t* handle = (uv_async_t*) arg;
55
56  while (ACCESS_ONCE(const char*, handle->data) == running)
57    uv_async_send(handle);
58
59  /* Acknowledge that we've seen handle->data change. */
60  ACCESS_ONCE(const char*, handle->data) = stopped;
61}
62
63
64static int test_async_pummel(int nthreads) {
65  char fmtbuf[2][32];
66  uv_thread_t* tids;
67  uv_async_t handle;
68  uint64_t time;
69  int i;
70
71  tids = calloc(nthreads, sizeof(tids[0]));
72  ASSERT_NOT_NULL(tids);
73
74  ASSERT_OK(uv_async_init(uv_default_loop(), &handle, async_cb));
75  ACCESS_ONCE(const char*, handle.data) = running;
76
77  for (i = 0; i < nthreads; i++)
78    ASSERT_OK(uv_thread_create(tids + i, pummel, &handle));
79
80  time = uv_hrtime();
81
82  ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));
83
84  time = uv_hrtime() - time;
85  done = 1;
86
87  for (i = 0; i < nthreads; i++)
88    ASSERT_OK(uv_thread_join(tids + i));
89
90  printf("async_pummel_%d: %s callbacks in %.2f seconds (%s/sec)\n",
91         nthreads,
92         fmt(&fmtbuf[0], callbacks),
93         time / 1e9,
94         fmt(&fmtbuf[1], callbacks / (time / 1e9)));
95
96  free(tids);
97
98  MAKE_VALGRIND_HAPPY(uv_default_loop());
99  return 0;
100}
101
102
103BENCHMARK_IMPL(async_pummel_1) {
104  return test_async_pummel(1);
105}
106
107
108BENCHMARK_IMPL(async_pummel_2) {
109  return test_async_pummel(2);
110}
111
112
113BENCHMARK_IMPL(async_pummel_4) {
114  return test_async_pummel(4);
115}
116
117
118BENCHMARK_IMPL(async_pummel_8) {
119  return test_async_pummel(8);
120}
121