benchmark-spawn.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 /* This benchmark spawns itself 1000 times. */
23 
24 #include "task.h"
25 #include "uv.h"
26 
27 static uv_loop_t* loop;
28 
29 static int N = 1000;
30 static int done;
31 
34 static char exepath[1024];
35 static size_t exepath_size = 1024;
36 static char* args[3];
37 static uv_pipe_t out;
38 
39 #define OUTPUT_SIZE 1024
40 static char output[OUTPUT_SIZE];
41 static int output_used;
42 
43 static int process_open;
44 static int pipe_open;
45 
46 
47 static void spawn(void);
48 
49 
50 static void maybe_spawn(void) {
51  if (process_open == 0 && pipe_open == 0) {
52  done++;
53  if (done < N) {
54  spawn();
55  }
56  }
57 }
58 
59 
61  ASSERT(process_open == 1);
62  process_open = 0;
63  maybe_spawn();
64 }
65 
66 
68  int64_t exit_status,
69  int term_signal) {
70  ASSERT(exit_status == 42);
71  ASSERT(term_signal == 0);
73 }
74 
75 
77  size_t suggested_size,
78  uv_buf_t* buf) {
79  buf->base = output + output_used;
80  buf->len = OUTPUT_SIZE - output_used;
81 }
82 
83 
84 static void pipe_close_cb(uv_handle_t* pipe) {
85  ASSERT(pipe_open == 1);
86  pipe_open = 0;
87  maybe_spawn();
88 }
89 
90 
91 static void on_read(uv_stream_t* pipe, ssize_t nread, const uv_buf_t* buf) {
92  if (nread > 0) {
93  ASSERT(pipe_open == 1);
94  output_used += nread;
95  } else if (nread < 0) {
96  if (nread == UV_EOF) {
98  }
99  }
100 }
101 
102 
103 static void spawn(void) {
104  uv_stdio_container_t stdio[2];
105  int r;
106 
107  ASSERT(process_open == 0);
108  ASSERT(pipe_open == 0);
109 
110  args[0] = exepath;
111  args[1] = "spawn_helper";
112  args[2] = NULL;
113  options.file = exepath;
114  options.args = args;
116 
117  uv_pipe_init(loop, &out, 0);
118 
119  options.stdio = stdio;
120  options.stdio_count = 2;
124 
125  r = uv_spawn(loop, &process, &options);
126  ASSERT(r == 0);
127 
128  process_open = 1;
129  pipe_open = 1;
130  output_used = 0;
131 
133  ASSERT(r == 0);
134 }
135 
136 
138  int r;
139  static int64_t start_time, end_time;
140 
141  loop = uv_default_loop();
142 
144  ASSERT(r == 0);
145  exepath[exepath_size] = '\0';
146 
149 
150  spawn();
151 
153  ASSERT(r == 0);
154 
156  end_time = uv_now(loop);
157 
158  fprintf(stderr, "spawn: %.0f spawns/s\n",
159  (double) N / (double) (end_time - start_time) * 1000.0);
160  fflush(stderr);
161 
163  return 0;
164 }
uv_process_options_s
Definition: uv.h:940
uv_process_s
Definition: uv.h:1037
on_alloc
static void on_alloc(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf)
Definition: benchmark-spawn.c:76
task.h
OUTPUT_SIZE
#define OUTPUT_SIZE
Definition: benchmark-spawn.c:39
uv_pipe_init
UV_EXTERN int uv_pipe_init(uv_loop_t *, uv_pipe_t *handle, int ipc)
Definition: unix/pipe.c:33
exit_cb
static void exit_cb(uv_process_t *process, int64_t exit_status, int term_signal)
Definition: benchmark-spawn.c:67
uv_now
UV_EXTERN uint64_t uv_now(const uv_loop_t *)
Definition: uv-common.c:537
out
static uv_pipe_t out
Definition: benchmark-spawn.c:37
UV_IGNORE
@ UV_IGNORE
Definition: uv.h:911
spawn
static void spawn(void)
Definition: benchmark-spawn.c:103
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
end_time
static int64_t end_time
Definition: benchmark-getaddrinfo.c:38
uv_process_options_s::exit_cb
uv_exit_cb exit_cb
Definition: uv.h:941
ASSERT
#define ASSERT(expr)
Definition: task.h:102
start_time
static int64_t start_time
Definition: benchmark-getaddrinfo.c:37
uv_run
UV_EXTERN int uv_run(uv_loop_t *, uv_run_mode mode)
Definition: unix/core.c:361
pipe_close_cb
static void pipe_close_cb(uv_handle_t *pipe)
Definition: benchmark-spawn.c:84
python_utils.port_server.stderr
stderr
Definition: port_server.py:51
uv_close
UV_EXTERN void uv_close(uv_handle_t *handle, uv_close_cb close_cb)
Definition: unix/core.c:112
uv_stream_s
Definition: uv.h:491
exepath
static char exepath[1024]
Definition: benchmark-spawn.c:34
uv_process_options_s::file
const char * file
Definition: uv.h:942
process_open
static int process_open
Definition: benchmark-spawn.c:43
UV_WRITABLE_PIPE
@ UV_WRITABLE_PIPE
Definition: uv.h:922
output
static char output[OUTPUT_SIZE]
Definition: benchmark-spawn.c:40
uv_update_time
UV_EXTERN void uv_update_time(uv_loop_t *)
Definition: unix/core.c:413
uv_default_loop
UV_EXTERN uv_loop_t * uv_default_loop(void)
Definition: uv-common.c:733
ssize_t
intptr_t ssize_t
Definition: win.h:27
process
static uv_process_t process
Definition: benchmark-spawn.c:32
uv_stdio_container_s::flags
uv_stdio_flags flags
Definition: uv.h:932
uv_process_options_s::stdio
uv_stdio_container_t * stdio
Definition: uv.h:975
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
uv_process_options_s::args
char ** args
Definition: uv.h:949
UV_RUN_DEFAULT
@ UV_RUN_DEFAULT
Definition: uv.h:254
uv_process_options_s::stdio_count
int stdio_count
Definition: uv.h:974
uv_read_start
UV_EXTERN int uv_read_start(uv_stream_t *, uv_alloc_cb alloc_cb, uv_read_cb read_cb)
Definition: unix/stream.c:1555
uv_stdio_container_s
Definition: uv.h:931
uv_spawn
UV_EXTERN int uv_spawn(uv_loop_t *loop, uv_process_t *handle, const uv_process_options_t *options)
Definition: unix/process.c:408
uv_exepath
UV_EXTERN int uv_exepath(char *buffer, size_t *size)
Definition: aix-common.c:79
uv_stdio_container_s::stream
uv_stream_t * stream
Definition: uv.h:935
N
static int N
Definition: benchmark-spawn.c:29
uv.h
process_close_cb
static void process_close_cb(uv_handle_t *handle)
Definition: benchmark-spawn.c:60
MAKE_VALGRIND_HAPPY
#define MAKE_VALGRIND_HAPPY()
Definition: task.h:229
exepath_size
static size_t exepath_size
Definition: benchmark-spawn.c:35
uv_buf_t
Definition: unix.h:121
done
static int done
Definition: benchmark-spawn.c:30
maybe_spawn
static void maybe_spawn(void)
Definition: benchmark-spawn.c:50
loop
static uv_loop_t * loop
Definition: benchmark-spawn.c:27
fix_build_deps.r
r
Definition: fix_build_deps.py:491
args
static char * args[3]
Definition: benchmark-spawn.c:36
uv_pipe_s
Definition: uv.h:757
UV_CREATE_PIPE
@ UV_CREATE_PIPE
Definition: uv.h:912
options
static uv_process_options_t options
Definition: benchmark-spawn.c:33
BENCHMARK_IMPL
BENCHMARK_IMPL(spawn)
Definition: benchmark-spawn.c:137
uv_stdio_container_s::data
union uv_stdio_container_s::@399 data
handle
static csh handle
Definition: test_arm_regression.c:16
output_used
static int output_used
Definition: benchmark-spawn.c:41
uv_handle_s
Definition: uv.h:441
uv_loop_s
Definition: uv.h:1767
on_read
static void on_read(uv_stream_t *pipe, ssize_t nread, const uv_buf_t *buf)
Definition: benchmark-spawn.c:91
pipe_open
static int pipe_open
Definition: benchmark-spawn.c:44


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