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 "../../../platform/fd_sys_util.h"
8 : #include "../../../../disco/topo/fd_topob.h"
9 : #include "../../../../disco/topo/fd_cpu_topo.h"
10 : #include "../../../../disco/net/fd_net_tile.h"
11 :
12 : #include <errno.h>
13 : #include <stdlib.h>
14 : #include <unistd.h>
15 : #include <sched.h>
16 : #include <fcntl.h>
17 : #include <pthread.h>
18 : #include <signal.h>
19 : #include <linux/capability.h>
20 : #include <linux/futex.h>
21 : #include <sys/syscall.h>
22 : #include <sys/wait.h>
23 : #include <sys/socket.h>
24 : #include <arpa/inet.h>
25 :
26 : extern fd_topo_obj_callbacks_t * CALLBACKS[];
27 :
28 : fd_topo_run_tile_t
29 : fdctl_tile_run( fd_topo_tile_t const * tile );
30 :
31 : void
32 : update_config_for_dev( config_t * config );
33 :
34 : int
35 0 : bench_transaction_mode( char const * name ) {
36 0 : if( !strcmp( name, "" ) ) return BENCHG_TRANSACTION_MODE_NOOP;
37 0 : if( !strcmp( name, "noop" ) ) return BENCHG_TRANSACTION_MODE_NOOP;
38 0 : if( !strcmp( name, "sol-transfer" ) ) return BENCHG_TRANSACTION_MODE_SOL_TRANSFER;
39 0 : if( !strcmp( name, "ptoken-transfer" ) ) return BENCHG_TRANSACTION_MODE_PTOKEN_TRANSFER;
40 0 : return -1;
41 0 : }
42 :
43 : void
44 : bench_cmd_args( int * pargc,
45 : char *** pargv,
46 0 : args_t * args ) {
47 0 : args->load.no_quic = fd_env_strip_cmdline_contains( pargc, pargv, "--no-quic" );
48 0 : args->load.no_watch = fd_env_strip_cmdline_contains( pargc, pargv, "--no-watch" );
49 0 : args->load.duration = fd_env_strip_cmdline_ulong( pargc, pargv, "--duration", NULL, 0UL );
50 0 : }
51 :
52 : void
53 : add_bench_topo( fd_topo_t * topo,
54 : char const * affinity,
55 : ulong benchg_tile_cnt,
56 : ulong benchs_tile_cnt,
57 : ulong accounts_cnt,
58 : int transaction_mode,
59 : float contending_fraction,
60 : float cu_price_spread,
61 : ulong conn_cnt,
62 : ushort send_to_port,
63 : uint send_to_ip_addr,
64 : ushort rpc_port,
65 0 : uint rpc_ip_addr ) {
66 :
67 0 : fd_topob_wksp( topo, "bench" );
68 0 : fd_topob_link( topo, "bencho_out", "bench", 128UL, 64UL, 1UL );
69 0 : for( ulong i=0UL; i<benchg_tile_cnt; i++ ) fd_topob_link( topo, "benchg_s", "bench", 65536UL, FD_TXN_MTU, 1UL );
70 :
71 0 : int is_bench_auto_affinity = !strcmp( affinity, "auto" );
72 :
73 0 : ushort parsed_tile_to_cpu[ FD_TILE_MAX ];
74 0 : for( ulong i=0UL; i<FD_TILE_MAX; i++ ) parsed_tile_to_cpu[ i ] = USHORT_MAX;
75 :
76 0 : fd_topo_cpus_t cpus[1];
77 0 : fd_topo_cpus_init( cpus );
78 :
79 0 : ulong affinity_tile_cnt = 0UL;
80 0 : if( FD_LIKELY( !is_bench_auto_affinity ) ) affinity_tile_cnt = fd_topob_parse_affinity_cstr( affinity, parsed_tile_to_cpu, 0, 1 );
81 :
82 0 : ulong tile_to_cpu[ FD_TILE_MAX ] = {0};
83 0 : for( ulong i=0UL; i<affinity_tile_cnt; i++ ) {
84 0 : ushort cpu_idx = (ushort)( parsed_tile_to_cpu[ i ] & ~FD_TOPOB_CPU_SHARED );
85 0 : if( FD_UNLIKELY( parsed_tile_to_cpu[ i ]!=USHORT_MAX && cpu_idx>=cpus->cpu_cnt ) )
86 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 "
87 0 : "only has %lu CPUs. You should either change the CPU allocations in the affinity string, or increase the number of CPUs "
88 0 : "in the system.",
89 0 : cpu_idx, cpus->cpu_cnt ));
90 0 : tile_to_cpu[ i ] = fd_ulong_if( parsed_tile_to_cpu[ i ]==USHORT_MAX, ULONG_MAX, (ulong)parsed_tile_to_cpu[ i ] );
91 0 : }
92 0 : if( FD_LIKELY( !is_bench_auto_affinity ) ) {
93 0 : if( FD_UNLIKELY( affinity_tile_cnt<benchg_tile_cnt+1UL+benchs_tile_cnt ) )
94 0 : FD_LOG_ERR(( "The benchmark topology you are using has %lu bench tiles, but the CPU affinity specified "
95 0 : "in the [development.bench.affinity] only provides for %lu cores. ",
96 0 : benchg_tile_cnt+1UL+benchs_tile_cnt, affinity_tile_cnt ));
97 0 : else if( FD_UNLIKELY( affinity_tile_cnt>benchg_tile_cnt+1UL+benchs_tile_cnt ) )
98 0 : FD_LOG_WARNING(( "The benchmark topology you are using has %lu bench tiles, but the CPU affinity specified "
99 0 : "in the [development.bench.affinity] provides for %lu cores. The extra cores will be unused.",
100 0 : benchg_tile_cnt+1UL+benchs_tile_cnt, affinity_tile_cnt ));
101 0 : }
102 0 : fd_topo_tile_t * bencho = fd_topob_tile( topo, "bencho", "bench", "bench", tile_to_cpu[ 0 ], 0, 0, 0 );
103 0 : bencho->bencho.rpc_port = rpc_port;
104 0 : bencho->bencho.rpc_ip_addr = rpc_ip_addr;
105 0 : for( ulong i=0UL; i<benchg_tile_cnt; i++ ) {
106 0 : fd_topo_tile_t * benchg = fd_topob_tile( topo, "benchg", "bench", "bench", tile_to_cpu[ i+1UL ], 0, 0, 0 );
107 0 : benchg->benchg.accounts_cnt = accounts_cnt;
108 0 : benchg->benchg.mode = transaction_mode;
109 0 : benchg->benchg.contending_fraction = contending_fraction;
110 0 : benchg->benchg.cu_price_spread = cu_price_spread;
111 0 : }
112 0 : for( ulong i=0UL; i<benchs_tile_cnt; i++ ) {
113 0 : fd_topo_tile_t * benchs = fd_topob_tile( topo, "benchs", "bench", "bench", tile_to_cpu[ benchg_tile_cnt+1UL+i ], 0, 0, 0 );
114 0 : benchs->benchs.send_to_ip_addr = send_to_ip_addr;
115 0 : benchs->benchs.send_to_port = send_to_port;
116 0 : benchs->benchs.conn_cnt = conn_cnt;
117 0 : }
118 :
119 0 : fd_topob_tile_out( topo, "bencho", 0UL, "bencho_out", 0UL );
120 0 : if( FD_LIKELY( fd_topo_find_link( topo, "replay_slot", 0UL )!=ULONG_MAX ) ) {
121 0 : fd_topob_tile_in( topo, "bencho", 0UL, "metric_in", "replay_slot", 0UL, FD_TOPOB_RELIABLE, FD_TOPOB_POLLED ); /* fseq in a wksp replay maps */
122 0 : }
123 0 : for( ulong i=0UL; i<benchg_tile_cnt; i++ ) {
124 0 : fd_topob_tile_in( topo, "benchg", i, "bench", "bencho_out", 0, 1, 1 );
125 0 : fd_topob_tile_out( topo, "benchg", i, "benchg_s", i );
126 0 : }
127 0 : for( ulong i=0UL; i<benchg_tile_cnt; i++ ) {
128 0 : for( ulong j=0UL; j<benchs_tile_cnt; j++ ) {
129 0 : fd_topob_tile_in( topo, "benchs", j, "bench", "benchg_s", i, 1, 1 );
130 0 : }
131 0 : }
132 :
133 : /* This will blow away previous auto topology layouts and recompute an auto topology. */
134 0 : if( FD_UNLIKELY( is_bench_auto_affinity ) ) fd_topob_auto_layout( topo );
135 0 : fd_topob_sleep_finish( topo );
136 0 : fd_topob_finish( topo, CALLBACKS );
137 0 : }
138 :
139 : void
140 : fd_topo_initialize( config_t * config );
141 :
142 : void
143 0 : bench_topo( config_t * config ) {
144 0 : config->tiles.rpc.delay_startup = 0;
145 :
146 0 : fd_topo_initialize( config );
147 :
148 0 : ushort rpc_port;
149 0 : uint rpc_ip_addr;
150 0 : if( FD_UNLIKELY( !config->tiles.rpc.enabled ) ) FD_LOG_ERR(( "RPC tile must be enabled to run bench" ));
151 0 : rpc_port = config->tiles.rpc.rpc_listen_port;
152 0 : if( FD_UNLIKELY( !fd_cstr_to_ip4_addr( config->tiles.rpc.rpc_listen_address, &rpc_ip_addr ) ) )
153 0 : FD_LOG_ERR(( "failed to parse rpc listen address `%s`", config->tiles.rpc.rpc_listen_address ));
154 :
155 0 : int is_auto_affinity = !strcmp( config->layout.affinity, "auto" );
156 0 : int is_bench_auto_affinity = !strcmp( config->development.bench.affinity, "auto" );
157 :
158 0 : if( FD_UNLIKELY( is_auto_affinity != is_bench_auto_affinity ) ) {
159 0 : FD_LOG_ERR(( "The CPU affinity string in the configuration file under [layout.affinity] and [development.bench.affinity] must all be set to 'auto' or all be set to a specific CPU affinity string." ));
160 0 : }
161 :
162 0 : int transaction_mode = bench_transaction_mode( config->development.bench.transaction_mode );
163 0 : if( FD_UNLIKELY( transaction_mode<0 ) )
164 0 : FD_LOG_ERR(( "unknown [development.bench.transaction_mode] `%s`", config->development.bench.transaction_mode ));
165 :
166 0 : add_bench_topo( &config->topo,
167 0 : config->development.bench.affinity,
168 0 : config->development.bench.benchg_tile_count,
169 0 : config->development.bench.benchs_tile_count,
170 0 : config->development.genesis.fund_initial_accounts,
171 0 : transaction_mode, 0.0f, 0.0f,
172 0 : config->layout.quic_tile_count,
173 0 : config->tiles.quic.quic_transaction_listen_port,
174 0 : config->net.ip_addr,
175 0 : rpc_port,
176 0 : rpc_ip_addr );
177 0 : }
178 :
179 : static pid_t bench_pid;
180 :
181 : static void
182 0 : bench_signal( int sig ) {
183 0 : kill( bench_pid, sig );
184 0 : }
185 :
186 : void
187 : bench_cmd_fn( args_t * args,
188 0 : config_t * config ) {
189 :
190 0 : if( args->load.no_quic ) {
191 0 : ushort port = config->tiles.quic.regular_transaction_listen_port;
192 0 : ulong benchs_tile_cnt = fd_topo_tile_name_cnt( &config->topo, "benchs" );
193 0 : for( ulong i=0UL; i<benchs_tile_cnt; i++ ) {
194 0 : fd_topo_tile_t * benchs = &config->topo.tiles[ fd_topo_find_tile( &config->topo, "benchs", i ) ];
195 0 : benchs->benchs.no_quic = 1;
196 0 : benchs->benchs.send_to_port = port;
197 0 : }
198 0 : }
199 :
200 0 : config->topo.tiles[ fd_topo_find_tile( &config->topo, "bencho", 0UL ) ].bencho.duration_s = args->load.duration;
201 :
202 : /* Bench boots a fresh genesis each run, so a vote history left by an
203 : earlier run would hold votor back from leading. */
204 0 : ulong votor_idx = fd_topo_find_tile( &config->topo, "votor", 0UL );
205 0 : if( FD_LIKELY( votor_idx!=ULONG_MAX ) ) config->topo.tiles[ votor_idx ].votor.vote_history_path[ 0 ] = '\0';
206 :
207 0 : args_t configure_args = {
208 0 : .configure.command = CONFIGURE_CMD_INIT,
209 0 : };
210 :
211 : /* FD_BACKTEST_SHARED_HOST skips the host-global stages (irq-affinity,
212 : irq-balance, kworkers) so another instance on the host keeps its setup. */
213 0 : int shared_host = !!getenv( "FD_BACKTEST_SHARED_HOST" );
214 0 : ulong stage_cnt = 0UL;
215 0 : for( ulong i=0UL; STAGES[ i ]; i++ ) {
216 0 : if( shared_host && ( STAGES[ i ]==&fd_cfg_stage_irq_affinity || STAGES[ i ]==&fd_cfg_stage_irq_balance || STAGES[ i ]==&fd_cfg_stage_kworkers ) ) continue;
217 0 : configure_args.configure.stages[ stage_cnt++ ] = STAGES[ i ];
218 0 : }
219 0 : configure_cmd_fn( &configure_args, config );
220 :
221 : /* Everything past configure runs in a child: the tiles join the
222 : cpuset cgroup and drop privileges, so only a root parent outside
223 : it can dissolve the partition once they exit (or fail to boot) */
224 0 : bench_pid = fork();
225 0 : if( FD_UNLIKELY( bench_pid<0 ) ) FD_LOG_ERR(( "fork() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
226 0 : if( FD_LIKELY( bench_pid ) ) {
227 0 : struct sigaction sa = { .sa_handler = bench_signal };
228 0 : if( FD_UNLIKELY( sigaction( SIGINT, &sa, NULL ) || sigaction( SIGTERM, &sa, NULL ) ) )
229 0 : FD_LOG_ERR(( "sigaction() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
230 :
231 0 : int wstatus;
232 0 : while( FD_UNLIKELY( -1==waitpid( bench_pid, &wstatus, 0 ) ) ) {
233 0 : if( FD_UNLIKELY( errno!=EINTR ) ) FD_LOG_ERR(( "waitpid() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
234 0 : }
235 :
236 0 : configure_args = (args_t){ .configure.command = CONFIGURE_CMD_FINI, .configure.stages = { &fd_cfg_stage_cpuset } };
237 0 : configure_cmd_fn( &configure_args, config );
238 0 : fd_sys_util_exit_group( WIFSIGNALED( wstatus ) ? 128+WTERMSIG( wstatus ) : WEXITSTATUS( wstatus ) );
239 0 : }
240 :
241 0 : update_config_for_dev( config );
242 :
243 0 : run_firedancer_init( config, 1, 1 );
244 :
245 0 : if( 0==strcmp( config->net.provider, "xdp" ) ) {
246 0 : fd_topo_install_xdp_simple( &config->topo, config->net.bind_address_parsed );
247 0 : }
248 :
249 0 : initialize_accdb_fd( config );
250 0 : initialize_epoch_credits_fd( config );
251 0 : initialize_cost_tracker_fd( config );
252 0 : initialize_vote_stakes_fd( config );
253 0 : initialize_stake_delegations_fd( config );
254 0 : initialize_txncache_fd( config );
255 0 : initialize_store_fds( config );
256 0 : initialize_snapshot_fds( config );
257 :
258 0 : fd_topo_join_workspaces( &config->topo, FD_SHMEM_JOIN_MODE_READ_WRITE, FD_TOPO_CORE_DUMP_LEVEL_DISABLED );
259 0 : if( 0==strcmp( config->net.provider, "mlx5" ) ) {
260 0 : fd_topo_install_mlx5( &config->topo, NULL );
261 0 : }
262 0 : if( 0==strcmp( config->net.provider, "iavf" ) ) {
263 0 : fd_topo_install_iavf( &config->topo, NULL );
264 0 : }
265 :
266 0 : if( !args->load.no_watch ) {
267 : /* watch incompatible with sandbox */
268 0 : config->development.sandbox = 0;
269 0 : config->development.no_clone = 1;
270 :
271 0 : int pipefd[2];
272 0 : if( FD_UNLIKELY( pipe2( pipefd, O_NONBLOCK ) ) ) FD_LOG_ERR(( "pipe2() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
273 :
274 0 : args_t watch_args;
275 0 : watch_args.watch.drain_output_fd = pipefd[0];
276 0 : watch_args.watch.full = 1;
277 0 : if( FD_UNLIKELY( -1==dup2( pipefd[ 1 ], STDERR_FILENO ) ) ) FD_LOG_ERR(( "dup2() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
278 :
279 : /* FIXME allow running sandboxed/multiprocess */
280 0 : fd_topo_run_single_process( &config->topo, config->uid, config->gid, fdctl_tile_run );
281 0 : watch_cmd_fn( &watch_args, config );
282 0 : } else {
283 : /* FIXME allow running sandboxed/multiprocess */
284 0 : fd_topo_run_single_process( &config->topo, config->uid, config->gid, fdctl_tile_run );
285 0 : }
286 0 : }
|