1e66f31c5Sopenharmony_ci/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2e66f31c5Sopenharmony_ci *
3e66f31c5Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy
4e66f31c5Sopenharmony_ci * of this software and associated documentation files (the "Software"), to
5e66f31c5Sopenharmony_ci * deal in the Software without restriction, including without limitation the
6e66f31c5Sopenharmony_ci * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7e66f31c5Sopenharmony_ci * sell copies of the Software, and to permit persons to whom the Software is
8e66f31c5Sopenharmony_ci * furnished to do so, subject to the following conditions:
9e66f31c5Sopenharmony_ci *
10e66f31c5Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
11e66f31c5Sopenharmony_ci * all copies or substantial portions of the Software.
12e66f31c5Sopenharmony_ci *
13e66f31c5Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14e66f31c5Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15e66f31c5Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16e66f31c5Sopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17e66f31c5Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18e66f31c5Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19e66f31c5Sopenharmony_ci * IN THE SOFTWARE.
20e66f31c5Sopenharmony_ci */
21e66f31c5Sopenharmony_ci
22e66f31c5Sopenharmony_ci#include "task.h"
23e66f31c5Sopenharmony_ci#include "uv.h"
24e66f31c5Sopenharmony_ci
25e66f31c5Sopenharmony_ci#include <stdio.h>
26e66f31c5Sopenharmony_ci#include <stdlib.h>
27e66f31c5Sopenharmony_ci
28e66f31c5Sopenharmony_ci#define NUM_PINGS (1000 * 1000)
29e66f31c5Sopenharmony_ci
30e66f31c5Sopenharmony_cistruct ctx {
31e66f31c5Sopenharmony_ci  uv_loop_t loop;
32e66f31c5Sopenharmony_ci  uv_thread_t thread;
33e66f31c5Sopenharmony_ci  uv_async_t main_async;    /* wake up main thread */
34e66f31c5Sopenharmony_ci  uv_async_t worker_async;  /* wake up worker */
35e66f31c5Sopenharmony_ci  unsigned int nthreads;
36e66f31c5Sopenharmony_ci  unsigned int main_sent;
37e66f31c5Sopenharmony_ci  unsigned int main_seen;
38e66f31c5Sopenharmony_ci  unsigned int worker_sent;
39e66f31c5Sopenharmony_ci  unsigned int worker_seen;
40e66f31c5Sopenharmony_ci};
41e66f31c5Sopenharmony_ci
42e66f31c5Sopenharmony_ci
43e66f31c5Sopenharmony_cistatic void worker_async_cb(uv_async_t* handle) {
44e66f31c5Sopenharmony_ci  struct ctx* ctx = container_of(handle, struct ctx, worker_async);
45e66f31c5Sopenharmony_ci
46e66f31c5Sopenharmony_ci  ASSERT_OK(uv_async_send(&ctx->main_async));
47e66f31c5Sopenharmony_ci  ctx->worker_sent++;
48e66f31c5Sopenharmony_ci  ctx->worker_seen++;
49e66f31c5Sopenharmony_ci
50e66f31c5Sopenharmony_ci  if (ctx->worker_sent >= NUM_PINGS)
51e66f31c5Sopenharmony_ci    uv_close((uv_handle_t*) &ctx->worker_async, NULL);
52e66f31c5Sopenharmony_ci}
53e66f31c5Sopenharmony_ci
54e66f31c5Sopenharmony_ci
55e66f31c5Sopenharmony_cistatic void main_async_cb(uv_async_t* handle) {
56e66f31c5Sopenharmony_ci  struct ctx* ctx = container_of(handle, struct ctx, main_async);
57e66f31c5Sopenharmony_ci
58e66f31c5Sopenharmony_ci  ASSERT_OK(uv_async_send(&ctx->worker_async));
59e66f31c5Sopenharmony_ci  ctx->main_sent++;
60e66f31c5Sopenharmony_ci  ctx->main_seen++;
61e66f31c5Sopenharmony_ci
62e66f31c5Sopenharmony_ci  if (ctx->main_sent >= NUM_PINGS)
63e66f31c5Sopenharmony_ci    uv_close((uv_handle_t*) &ctx->main_async, NULL);
64e66f31c5Sopenharmony_ci}
65e66f31c5Sopenharmony_ci
66e66f31c5Sopenharmony_ci
67e66f31c5Sopenharmony_cistatic void worker(void* arg) {
68e66f31c5Sopenharmony_ci  struct ctx* ctx = arg;
69e66f31c5Sopenharmony_ci  ASSERT_OK(uv_async_send(&ctx->main_async));
70e66f31c5Sopenharmony_ci  ASSERT_OK(uv_run(&ctx->loop, UV_RUN_DEFAULT));
71e66f31c5Sopenharmony_ci  uv_loop_close(&ctx->loop);
72e66f31c5Sopenharmony_ci}
73e66f31c5Sopenharmony_ci
74e66f31c5Sopenharmony_ci
75e66f31c5Sopenharmony_cistatic int test_async(int nthreads) {
76e66f31c5Sopenharmony_ci  char fmtbuf[32];
77e66f31c5Sopenharmony_ci  struct ctx* threads;
78e66f31c5Sopenharmony_ci  struct ctx* ctx;
79e66f31c5Sopenharmony_ci  uint64_t time;
80e66f31c5Sopenharmony_ci  int i;
81e66f31c5Sopenharmony_ci
82e66f31c5Sopenharmony_ci  threads = calloc(nthreads, sizeof(threads[0]));
83e66f31c5Sopenharmony_ci  ASSERT_NOT_NULL(threads);
84e66f31c5Sopenharmony_ci
85e66f31c5Sopenharmony_ci  for (i = 0; i < nthreads; i++) {
86e66f31c5Sopenharmony_ci    ctx = threads + i;
87e66f31c5Sopenharmony_ci    ctx->nthreads = nthreads;
88e66f31c5Sopenharmony_ci    ASSERT_OK(uv_loop_init(&ctx->loop));
89e66f31c5Sopenharmony_ci    ASSERT_OK(uv_async_init(&ctx->loop, &ctx->worker_async, worker_async_cb));
90e66f31c5Sopenharmony_ci    ASSERT_OK(uv_async_init(uv_default_loop(),
91e66f31c5Sopenharmony_ci                            &ctx->main_async,
92e66f31c5Sopenharmony_ci                            main_async_cb));
93e66f31c5Sopenharmony_ci    ASSERT_OK(uv_thread_create(&ctx->thread, worker, ctx));
94e66f31c5Sopenharmony_ci  }
95e66f31c5Sopenharmony_ci
96e66f31c5Sopenharmony_ci  time = uv_hrtime();
97e66f31c5Sopenharmony_ci
98e66f31c5Sopenharmony_ci  ASSERT_OK(uv_run(uv_default_loop(), UV_RUN_DEFAULT));
99e66f31c5Sopenharmony_ci
100e66f31c5Sopenharmony_ci  for (i = 0; i < nthreads; i++)
101e66f31c5Sopenharmony_ci    ASSERT_OK(uv_thread_join(&threads[i].thread));
102e66f31c5Sopenharmony_ci
103e66f31c5Sopenharmony_ci  time = uv_hrtime() - time;
104e66f31c5Sopenharmony_ci
105e66f31c5Sopenharmony_ci  for (i = 0; i < nthreads; i++) {
106e66f31c5Sopenharmony_ci    ctx = threads + i;
107e66f31c5Sopenharmony_ci    ASSERT_EQ(ctx->worker_sent, NUM_PINGS);
108e66f31c5Sopenharmony_ci    ASSERT_EQ(ctx->worker_seen, NUM_PINGS);
109e66f31c5Sopenharmony_ci    ASSERT_EQ(ctx->main_sent, (unsigned int) NUM_PINGS);
110e66f31c5Sopenharmony_ci    ASSERT_EQ(ctx->main_seen, (unsigned int) NUM_PINGS);
111e66f31c5Sopenharmony_ci  }
112e66f31c5Sopenharmony_ci
113e66f31c5Sopenharmony_ci  printf("async%d: %.2f sec (%s/sec)\n",
114e66f31c5Sopenharmony_ci         nthreads,
115e66f31c5Sopenharmony_ci         time / 1e9,
116e66f31c5Sopenharmony_ci         fmt(&fmtbuf, NUM_PINGS / (time / 1e9)));
117e66f31c5Sopenharmony_ci
118e66f31c5Sopenharmony_ci  free(threads);
119e66f31c5Sopenharmony_ci
120e66f31c5Sopenharmony_ci  MAKE_VALGRIND_HAPPY(uv_default_loop());
121e66f31c5Sopenharmony_ci  return 0;
122e66f31c5Sopenharmony_ci}
123e66f31c5Sopenharmony_ci
124e66f31c5Sopenharmony_ci
125e66f31c5Sopenharmony_ciBENCHMARK_IMPL(async1) {
126e66f31c5Sopenharmony_ci  return test_async(1);
127e66f31c5Sopenharmony_ci}
128e66f31c5Sopenharmony_ci
129e66f31c5Sopenharmony_ci
130e66f31c5Sopenharmony_ciBENCHMARK_IMPL(async2) {
131e66f31c5Sopenharmony_ci  return test_async(2);
132e66f31c5Sopenharmony_ci}
133e66f31c5Sopenharmony_ci
134e66f31c5Sopenharmony_ci
135e66f31c5Sopenharmony_ciBENCHMARK_IMPL(async4) {
136e66f31c5Sopenharmony_ci  return test_async(4);
137e66f31c5Sopenharmony_ci}
138e66f31c5Sopenharmony_ci
139e66f31c5Sopenharmony_ci
140e66f31c5Sopenharmony_ciBENCHMARK_IMPL(async8) {
141e66f31c5Sopenharmony_ci  return test_async(8);
142e66f31c5Sopenharmony_ci}
143