benchmark-async-pummel.c
Go to the documentation of this file.
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 
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 
53 static void pummel(void* arg) {
55 
56  while (ACCESS_ONCE(const char*, handle->data) == running)
58 
59  /* Acknowledge that we've seen handle->data change. */
60  ACCESS_ONCE(const char*, handle->data) = stopped;
61 }
62 
63 
64 static int test_async_pummel(int nthreads) {
65  uv_thread_t* tids;
67  uint64_t time;
68  int i;
69 
70  tids = calloc(nthreads, sizeof(tids[0]));
71  ASSERT(tids != NULL);
72 
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 
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 
98  return 0;
99 }
100 
101 
102 BENCHMARK_IMPL(async_pummel_1) {
103  return test_async_pummel(1);
104 }
105 
106 
107 BENCHMARK_IMPL(async_pummel_2) {
108  return test_async_pummel(2);
109 }
110 
111 
112 BENCHMARK_IMPL(async_pummel_4) {
113  return test_async_pummel(4);
114 }
115 
116 
117 BENCHMARK_IMPL(async_pummel_8) {
118  return test_async_pummel(8);
119 }
task.h
callbacks
static unsigned int callbacks
Definition: benchmark-async-pummel.c:31
printf
_Use_decl_annotations_ int __cdecl printf(const char *_Format,...)
Definition: cs_driver.c:91
stopped
static const char stopped[]
Definition: benchmark-async-pummel.c:36
ASSERT
#define ASSERT(expr)
Definition: task.h:102
BENCHMARK_IMPL
BENCHMARK_IMPL(async_pummel_1)
Definition: benchmark-async-pummel.c:102
uv_thread_join
UV_EXTERN int uv_thread_join(uv_thread_t *tid)
Definition: libuv/src/unix/thread.c:271
uv_run
UV_EXTERN int uv_run(uv_loop_t *, uv_run_mode mode)
Definition: unix/core.c:361
uv_close
UV_EXTERN void uv_close(uv_handle_t *handle, uv_close_cb close_cb)
Definition: unix/core.c:112
uv_default_loop
UV_EXTERN uv_loop_t * uv_default_loop(void)
Definition: uv-common.c:733
uv_async_s
Definition: uv.h:834
done
static volatile int done
Definition: benchmark-async-pummel.c:32
UV_RUN_DEFAULT
@ UV_RUN_DEFAULT
Definition: uv.h:254
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
arg
Definition: cmdline.cc:40
nthreads
static unsigned int nthreads
Definition: threadpool.c:37
uv_thread_create
UV_EXTERN int uv_thread_create(uv_thread_t *tid, uv_thread_cb entry, void *arg)
Definition: libuv/src/unix/thread.c:209
running
static const char running[]
Definition: benchmark-async-pummel.c:34
uv_sleep
UV_EXTERN void uv_sleep(unsigned int msec)
Definition: unix/core.c:1521
uv.h
MAKE_VALGRIND_HAPPY
#define MAKE_VALGRIND_HAPPY()
Definition: task.h:229
async_cb
static void async_cb(uv_async_t *handle)
Definition: benchmark-async-pummel.c:39
testing::internal::fmt
GTEST_API_ const char * fmt
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1808
uv_async_init
UV_EXTERN int uv_async_init(uv_loop_t *, uv_async_t *async, uv_async_cb async_cb)
Definition: unix/async.c:44
ACCESS_ONCE
#define ACCESS_ONCE(type, var)
Definition: benchmark-async-pummel.c:29
pummel
static void pummel(void *arg)
Definition: benchmark-async-pummel.c:53
uv_hrtime
UV_EXTERN uint64_t uv_hrtime(void)
Definition: unix/core.c:107
NUM_PINGS
#define NUM_PINGS
Definition: benchmark-async-pummel.c:28
stop
static const char stop[]
Definition: benchmark-async-pummel.c:35
handle
static csh handle
Definition: test_arm_regression.c:16
test_async_pummel
static int test_async_pummel(int nthreads)
Definition: benchmark-async-pummel.c:64
uv_handle_s
Definition: uv.h:441
uv_thread_t
pthread_t uv_thread_t
Definition: unix.h:134
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
uv_async_send
UV_EXTERN int uv_async_send(uv_async_t *async)
Definition: unix/async.c:62


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:36