• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
31 static unsigned int callbacks;
32 static volatile int done;
33 
34 static const char running[] = "running";
35 static const char stop[]    = "stop";
36 static const char stopped[] = "stopped";
37 
38 
async_cb(uv_async_t * handle)39 static 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 
pummel(void * arg)53 static 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 
test_async_pummel(int nthreads)64 static int test_async_pummel(int nthreads) {
65   uv_thread_t* tids;
66   uv_async_t handle;
67   uint64_t time;
68   int i;
69 
70   tids = calloc(nthreads, sizeof(tids[0]));
71   ASSERT_NOT_NULL(tids);
72 
73   ASSERT(0 == uv_async_init(uv_default_loop(), &handle, async_cb));
74   ACCESS_ONCE(const char*, handle.data) = running;
75 
76   for (i = 0; i < nthreads; i++)
77     ASSERT(0 == uv_thread_create(tids + i, pummel, &handle));
78 
79   time = uv_hrtime();
80 
81   ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
82 
83   time = uv_hrtime() - time;
84   done = 1;
85 
86   for (i = 0; i < nthreads; i++)
87     ASSERT(0 == uv_thread_join(tids + i));
88 
89   printf("async_pummel_%d: %s callbacks in %.2f seconds (%s/sec)\n",
90          nthreads,
91          fmt(callbacks),
92          time / 1e9,
93          fmt(callbacks / (time / 1e9)));
94 
95   free(tids);
96 
97   MAKE_VALGRIND_HAPPY();
98   return 0;
99 }
100 
101 
BENCHMARK_IMPL(async_pummel_1)102 BENCHMARK_IMPL(async_pummel_1) {
103   return test_async_pummel(1);
104 }
105 
106 
BENCHMARK_IMPL(async_pummel_2)107 BENCHMARK_IMPL(async_pummel_2) {
108   return test_async_pummel(2);
109 }
110 
111 
BENCHMARK_IMPL(async_pummel_4)112 BENCHMARK_IMPL(async_pummel_4) {
113   return test_async_pummel(4);
114 }
115 
116 
BENCHMARK_IMPL(async_pummel_8)117 BENCHMARK_IMPL(async_pummel_8) {
118   return test_async_pummel(8);
119 }
120