Line data Source code
1 : #define _GNU_SOURCE
2 : #include "bench.h"
3 : #include "../../../shared/commands/configure/configure.h"
4 : #include "../../../shared/commands/run/run.h"
5 :
6 : #include "../../../shared/commands/watch/watch.h"
7 : #include "../../../../disco/topo/fd_topob.h"
8 : #include "../../../../disco/topo/fd_cpu_topo.h"
9 : #include "../../../../disco/net/fd_net_tile.h"
10 : #include "../../../../util/tile/fd_tile_private.h"
11 :
12 : #include <errno.h>
13 : #include <unistd.h>
14 : #include <sched.h>
15 : #include <fcntl.h>
16 : #include <pthread.h>
17 : #include <linux/capability.h>
18 : #include <linux/futex.h>
19 : #include <sys/syscall.h>
20 : #include <sys/wait.h>
21 : #include <sys/socket.h>
22 : #include <arpa/inet.h>
23 :
24 : extern fd_topo_obj_callbacks_t * CALLBACKS[];
25 :
26 : fd_topo_run_tile_t
27 : fdctl_tile_run( fd_topo_tile_t const * tile );
28 :
29 : void
30 : update_config_for_dev( config_t * config );
31 :
32 : void
33 : bench_cmd_args( int * pargc,
34 : char *** pargv,
35 0 : args_t * args ) {
36 0 : args->load.no_quic = fd_env_strip_cmdline_contains( pargc, pargv, "--no-quic" );
37 0 : args->load.no_watch = fd_env_strip_cmdline_contains( pargc, pargv, "--no-watch" );
38 0 : }
39 :
40 : void
41 : add_bench_topo( fd_topo_t * topo,
42 : char const * affinity,
43 : ulong benchg_tile_cnt,
44 : ulong benchs_tile_cnt,
45 : ulong accounts_cnt,
46 : int transaction_mode,
47 : float contending_fraction,
48 : float cu_price_spread,
49 : ulong conn_cnt,
50 : ushort send_to_port,
51 : uint send_to_ip_addr,
52 : ushort rpc_port,
53 : uint rpc_ip_addr,
54 0 : int reserve_agave_cores ) {
55 :
56 0 : fd_topob_wksp( topo, "bench" );
57 0 : fd_topob_link( topo, "bencho_out", "bench", 128UL, 64UL, 1UL );
58 0 : for( ulong i=0UL; i<benchg_tile_cnt; i++ ) fd_topob_link( topo, "benchg_s", "bench", 65536UL, FD_TXN_MTU, 1UL );
59 :
60 0 : int is_bench_auto_affinity = !strcmp( affinity, "auto" );
61 :
62 0 : ushort parsed_tile_to_cpu[ FD_TILE_MAX ];
63 0 : for( ulong i=0UL; i<FD_TILE_MAX; i++ ) parsed_tile_to_cpu[ i ] = USHORT_MAX;
64 :
65 0 : fd_topo_cpus_t cpus[1];
66 0 : fd_topo_cpus_init( cpus );
67 :
68 0 : ulong affinity_tile_cnt = 0UL;
69 0 : if( FD_LIKELY( !is_bench_auto_affinity ) ) affinity_tile_cnt = fd_tile_private_cpus_parse( affinity, parsed_tile_to_cpu );
70 :
71 0 : ulong tile_to_cpu[ FD_TILE_MAX ] = {0};
72 0 : for( ulong i=0UL; i<affinity_tile_cnt; i++ ) {
73 0 : if( FD_UNLIKELY( parsed_tile_to_cpu[ i ]!=USHORT_MAX && parsed_tile_to_cpu[ i ]>=cpus->cpu_cnt ) )
74 0 : FD_LOG_ERR(( "The CPU affinity string in the configuration file under [development.bench.affinity] specifies a CPU index of %hu, but the system "
75 0 : "only has %lu CPUs. You should either change the CPU allocations in the affinity string, or increase the number of CPUs "
76 0 : "in the system.",
77 0 : parsed_tile_to_cpu[ i ], cpus->cpu_cnt ));
78 0 : tile_to_cpu[ i ] = fd_ulong_if( parsed_tile_to_cpu[ i ]==USHORT_MAX, ULONG_MAX, (ulong)parsed_tile_to_cpu[ i ] );
79 0 : }
80 0 : if( FD_LIKELY( !is_bench_auto_affinity ) ) {
81 0 : if( FD_UNLIKELY( affinity_tile_cnt<benchg_tile_cnt+1UL+benchs_tile_cnt ) )
82 0 : FD_LOG_ERR(( "The benchmark topology you are using has %lu bench tiles, but the CPU affinity specified "
83 0 : "in the [development.bench.affinity] only provides for %lu cores. ",
84 0 : benchg_tile_cnt+1UL+benchs_tile_cnt, affinity_tile_cnt ));
85 0 : else if( FD_UNLIKELY( affinity_tile_cnt>benchg_tile_cnt+1UL+benchs_tile_cnt ) )
86 0 : FD_LOG_WARNING(( "The benchmark topology you are using has %lu bench tiles, but the CPU affinity specified "
87 0 : "in the [development.bench.affinity] provides for %lu cores. The extra cores will be unused.",
88 0 : benchg_tile_cnt+1UL+benchs_tile_cnt, affinity_tile_cnt ));
89 0 : }
90 0 : fd_topo_tile_t * bencho = fd_topob_tile( topo, "bencho", "bench", "bench", tile_to_cpu[ 0 ], 0, 0, 0 );
91 0 : bencho->bencho.rpc_port = rpc_port;
92 0 : bencho->bencho.rpc_ip_addr = rpc_ip_addr;
93 0 : for( ulong i=0UL; i<benchg_tile_cnt; i++ ) {
94 0 : fd_topo_tile_t * benchg = fd_topob_tile( topo, "benchg", "bench", "bench", tile_to_cpu[ i+1UL ], 0, 0, 0 );
95 0 : benchg->benchg.accounts_cnt = accounts_cnt;
96 0 : benchg->benchg.mode = transaction_mode;
97 0 : benchg->benchg.contending_fraction = contending_fraction;
98 0 : benchg->benchg.cu_price_spread = cu_price_spread;
99 0 : }
100 0 : for( ulong i=0UL; i<benchs_tile_cnt; i++ ) {
101 0 : fd_topo_tile_t * benchs = fd_topob_tile( topo, "benchs", "bench", "bench", tile_to_cpu[ benchg_tile_cnt+1UL+i ], 0, 0, 0 );
102 0 : benchs->benchs.send_to_ip_addr = send_to_ip_addr;
103 0 : benchs->benchs.send_to_port = send_to_port;
104 0 : benchs->benchs.conn_cnt = conn_cnt;
105 0 : }
106 :
107 0 : fd_topob_tile_out( topo, "bencho", 0UL, "bencho_out", 0UL );
108 0 : for( ulong i=0UL; i<benchg_tile_cnt; i++ ) {
109 0 : fd_topob_tile_in( topo, "benchg", i, "bench", "bencho_out", 0, 1, 1 );
110 0 : fd_topob_tile_out( topo, "benchg", i, "benchg_s", i );
111 0 : }
112 0 : for( ulong i=0UL; i<benchg_tile_cnt; i++ ) {
113 0 : for( ulong j=0UL; j<benchs_tile_cnt; j++ ) {
114 0 : fd_topob_tile_in( topo, "benchs", j, "bench", "benchg_s", i, 1, 1 );
115 0 : }
116 0 : }
117 :
118 : /* This will blow away previous auto topology layouts and recompute an auto topology. */
119 0 : if( FD_UNLIKELY( is_bench_auto_affinity ) ) fd_topob_auto_layout( topo, reserve_agave_cores );
120 0 : fd_topob_finish( topo, CALLBACKS );
121 0 : }
122 :
123 : void
124 : fd_topo_initialize( config_t * config );
125 :
126 : void
127 0 : bench_topo( config_t * config ) {
128 0 : config->tiles.rpc.delay_startup = 0;
129 :
130 0 : fd_topo_initialize( config );
131 :
132 0 : ushort rpc_port;
133 0 : uint rpc_ip_addr;
134 0 : if( FD_UNLIKELY( !config->is_firedancer ) ) {
135 0 : config->frankendancer.rpc.port = fd_ushort_if( config->frankendancer.rpc.port, config->frankendancer.rpc.port, 8899 );
136 0 : config->frankendancer.rpc.full_api = 1;
137 0 : rpc_port = config->frankendancer.rpc.port;
138 0 : rpc_ip_addr = config->net.ip_addr;
139 0 : } else {
140 0 : if( FD_UNLIKELY( !config->tiles.rpc.enabled ) ) FD_LOG_ERR(( "RPC tile must be enabled to run bench" ));
141 0 : rpc_port = config->tiles.rpc.rpc_listen_port;
142 0 : if( FD_UNLIKELY( !fd_cstr_to_ip4_addr( config->tiles.rpc.rpc_listen_address, &rpc_ip_addr ) ) )
143 0 : FD_LOG_ERR(( "failed to parse rpc listen address `%s`", config->tiles.rpc.rpc_listen_address ));
144 0 : }
145 :
146 0 : int is_auto_affinity = !strcmp( config->layout.affinity, "auto" );
147 0 : int is_agave_auto_affinity;
148 0 : if( FD_UNLIKELY( config->is_firedancer ) ) {
149 0 : is_agave_auto_affinity = is_auto_affinity;
150 0 : } else {
151 0 : is_agave_auto_affinity = !strcmp( config->frankendancer.layout.agave_affinity, "auto" );
152 0 : }
153 0 : int is_bench_auto_affinity = !strcmp( config->development.bench.affinity, "auto" );
154 :
155 0 : if( FD_UNLIKELY( is_auto_affinity != is_agave_auto_affinity ||
156 0 : is_auto_affinity != is_bench_auto_affinity ) ) {
157 0 : FD_LOG_ERR(( "The CPU affinity string in the configuration file under [layout.affinity], [layout.agave_affinity], and [development.bench.affinity] must all be set to 'auto' or all be set to a specific CPU affinity string." ));
158 0 : }
159 :
160 0 : add_bench_topo( &config->topo,
161 0 : config->development.bench.affinity,
162 0 : config->development.bench.benchg_tile_count,
163 0 : config->development.bench.benchs_tile_count,
164 0 : config->development.genesis.fund_initial_accounts,
165 0 : 0, 0.0f, 0.0f,
166 0 : config->layout.quic_tile_count,
167 0 : config->tiles.quic.quic_transaction_listen_port,
168 0 : config->net.ip_addr,
169 0 : rpc_port,
170 0 : rpc_ip_addr,
171 0 : !config->is_firedancer );
172 0 : }
173 :
174 : void
175 : bench_cmd_fn( args_t * args,
176 0 : config_t * config ) {
177 :
178 0 : if( args->load.no_quic ) {
179 0 : ushort port = config->tiles.quic.regular_transaction_listen_port;
180 0 : ulong benchs_tile_cnt = fd_topo_tile_name_cnt( &config->topo, "benchs" );
181 0 : for( ulong i=0UL; i<benchs_tile_cnt; i++ ) {
182 0 : fd_topo_tile_t * benchs = &config->topo.tiles[ fd_topo_find_tile( &config->topo, "benchs", i ) ];
183 0 : benchs->benchs.no_quic = 1;
184 0 : benchs->benchs.send_to_port = port;
185 0 : }
186 0 : }
187 :
188 0 : args_t configure_args = {
189 0 : .configure.command = CONFIGURE_CMD_INIT,
190 0 : };
191 :
192 0 : for( ulong i=0UL; STAGES[ i ]; i++ )
193 0 : configure_args.configure.stages[ i ] = STAGES[ i ];
194 0 : configure_cmd_fn( &configure_args, config );
195 :
196 0 : update_config_for_dev( config );
197 :
198 0 : run_firedancer_init( config, 1, 1 );
199 :
200 0 : if( 0==strcmp( config->net.provider, "xdp" ) ) {
201 0 : fd_topo_install_xdp_simple( &config->topo, config->net.bind_address_parsed );
202 0 : }
203 :
204 0 : fd_topo_join_workspaces( &config->topo, FD_SHMEM_JOIN_MODE_READ_WRITE, FD_TOPO_CORE_DUMP_LEVEL_DISABLED );
205 :
206 0 : if( !args->load.no_watch ) {
207 : /* watch incompatible with sandbox */
208 0 : config->development.sandbox = 0;
209 0 : config->development.no_clone = 1;
210 :
211 0 : int pipefd[2];
212 0 : if( FD_UNLIKELY( pipe2( pipefd, O_NONBLOCK ) ) ) FD_LOG_ERR(( "pipe2() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
213 :
214 0 : args_t watch_args;
215 0 : watch_args.watch.drain_output_fd = pipefd[0];
216 0 : if( FD_UNLIKELY( -1==dup2( pipefd[ 1 ], STDERR_FILENO ) ) ) FD_LOG_ERR(( "dup2() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
217 :
218 : /* FIXME allow running sandboxed/multiprocess */
219 0 : fd_topo_run_single_process( &config->topo, 2, config->uid, config->gid, fdctl_tile_run );
220 0 : watch_cmd_fn( &watch_args, config );
221 0 : } else {
222 : /* FIXME allow running sandboxed/multiprocess */
223 0 : fd_topo_run_single_process( &config->topo, 2, config->uid, config->gid, fdctl_tile_run );
224 0 : }
225 0 : }
|