Line data Source code
1 : #include "../../../disco/tiles.h"
2 :
3 : #include "../rpc_client/fd_rpc_client.h"
4 : #include "../rpc_client/fd_rpc_client_private.h"
5 :
6 : #include "../../../util/net/fd_ip4.h"
7 :
8 : #include <linux/unistd.h>
9 :
10 : #define FD_BENCHO_STATE_INIT 0UL
11 0 : #define FD_BENCHO_STATE_WAIT 1UL
12 0 : #define FD_BENCHO_STATE_READY 2UL
13 0 : #define FD_BENCHO_STATE_SENT 3UL
14 :
15 0 : #define FD_BENCHO_RPC_INITIALIZE_TIMEOUT (30L * 1000L * 1000L * 1000L)
16 0 : #define FD_BENCHO_RPC_RESPONSE_TIMEOUT (5L * 1000L * 1000L * 1000L)
17 :
18 : typedef struct {
19 : long rpc_ready_deadline;
20 :
21 : long blockhash_request;
22 : ulong blockhash_state;
23 : long blockhash_deadline;
24 :
25 : int txncount_measured1;
26 : long txncount_request;
27 : ulong txncount_state;
28 : long txncount_nextprint;
29 : long txncount_deadline;
30 :
31 : ulong txncount_prev;
32 :
33 : fd_rpc_client_t rpc[ 1 ];
34 :
35 : fd_wksp_t * mem;
36 : ulong out_chunk0;
37 : ulong out_wmark;
38 : ulong out_chunk;
39 : } fd_bencho_ctx_t;
40 :
41 : FD_FN_CONST static inline ulong
42 0 : scratch_align( void ) {
43 0 : return alignof( fd_bencho_ctx_t );
44 0 : }
45 :
46 : FD_FN_PURE static inline ulong
47 0 : scratch_footprint( fd_topo_tile_t const * tile ) {
48 0 : (void)tile;
49 0 : ulong l = FD_LAYOUT_INIT;
50 0 : l = FD_LAYOUT_APPEND( l, alignof( fd_bencho_ctx_t ), sizeof( fd_bencho_ctx_t ) );
51 0 : return FD_LAYOUT_FINI( l, scratch_align() );
52 0 : }
53 :
54 : static int
55 : service_block_hash( fd_bencho_ctx_t * ctx,
56 0 : fd_stem_context_t * stem ) {
57 0 : int did_work = 0;
58 :
59 0 : if( FD_UNLIKELY( ctx->blockhash_state==FD_BENCHO_STATE_WAIT ) ) {
60 0 : if( FD_LIKELY( fd_log_wallclock()>=ctx->blockhash_deadline ) )
61 0 : ctx->blockhash_state = FD_BENCHO_STATE_READY;
62 0 : }
63 :
64 0 : if( FD_UNLIKELY( ctx->blockhash_state==FD_BENCHO_STATE_READY ) ) {
65 0 : ctx->blockhash_request = fd_rpc_client_request_latest_block_hash( ctx->rpc );
66 0 : if( FD_UNLIKELY( ctx->blockhash_request<0L ) ) FD_LOG_ERR(( "failed to send RPC request" ));
67 :
68 0 : ctx->blockhash_state = FD_BENCHO_STATE_SENT;
69 0 : ctx->blockhash_deadline = fd_log_wallclock() + FD_BENCHO_RPC_RESPONSE_TIMEOUT;
70 :
71 0 : did_work = 1;
72 0 : }
73 :
74 0 : if( FD_UNLIKELY( ctx->blockhash_state==FD_BENCHO_STATE_SENT ) ) {
75 0 : fd_rpc_client_response_t * response = fd_rpc_client_status( ctx->rpc, ctx->blockhash_request, 0 );
76 0 : if( FD_UNLIKELY( response->status==FD_RPC_CLIENT_PENDING ) ) {
77 0 : if( FD_UNLIKELY( fd_log_wallclock()>=ctx->blockhash_deadline ) )
78 0 : FD_LOG_ERR(( "timed out waiting for RPC server to respond" ));
79 0 : return did_work;
80 0 : }
81 :
82 0 : if( FD_UNLIKELY( fd_log_wallclock()<ctx->rpc_ready_deadline && response->status==FD_RPC_CLIENT_ERR_NETWORK ) ) {
83 : /* RPC server not yet responding, give it some more time... */
84 0 : ctx->blockhash_state = FD_BENCHO_STATE_WAIT;
85 0 : ctx->blockhash_deadline = fd_log_wallclock() + 100L * 1000L * 1000L; /* 100 millis to retry */
86 0 : fd_rpc_client_close( ctx->rpc, ctx->blockhash_request );
87 0 : return did_work;
88 0 : }
89 :
90 0 : if( FD_UNLIKELY( response->status!=FD_RPC_CLIENT_SUCCESS ) )
91 0 : FD_LOG_ERR(( "RPC server returned error %ld", response->status ));
92 :
93 0 : ctx->blockhash_state = FD_BENCHO_STATE_WAIT;
94 0 : ctx->blockhash_deadline = fd_log_wallclock() + 400L * 1000L * 1000L; /* 400 millis til we fetch new blockhash */
95 0 : fd_memcpy( fd_chunk_to_laddr( ctx->mem, ctx->out_chunk ), response->result.latest_block_hash.block_hash, 32 );
96 0 : fd_stem_publish( stem, 0UL, 0UL, ctx->out_chunk, 32UL, 0UL, 0UL, 0UL );
97 0 : ctx->out_chunk = fd_dcache_compact_next( ctx->out_chunk, 32, ctx->out_chunk0, ctx->out_wmark );
98 :
99 0 : fd_rpc_client_close( ctx->rpc, ctx->blockhash_request );
100 0 : if( FD_UNLIKELY( !ctx->txncount_nextprint ) ) {
101 0 : ctx->txncount_nextprint = fd_log_wallclock();
102 0 : }
103 :
104 0 : did_work = 1;
105 0 : }
106 :
107 0 : return did_work;
108 0 : }
109 :
110 : static int
111 0 : service_txn_count( fd_bencho_ctx_t * ctx ) {
112 0 : if( FD_UNLIKELY( !ctx->txncount_nextprint ) ) return 0;
113 :
114 0 : int did_work = 0;
115 :
116 0 : if( FD_UNLIKELY( ctx->txncount_state==FD_BENCHO_STATE_WAIT ) ) {
117 0 : if( FD_LIKELY( fd_log_wallclock()>=ctx->txncount_deadline ) )
118 0 : ctx->txncount_state = FD_BENCHO_STATE_READY;
119 0 : }
120 :
121 0 : if( FD_UNLIKELY( ctx->txncount_state==FD_BENCHO_STATE_READY ) ) {
122 0 : ctx->txncount_request = fd_rpc_client_request_transaction_count( ctx->rpc );
123 0 : if( FD_UNLIKELY( ctx->txncount_request<0L ) ) FD_LOG_ERR(( "failed to send RPC request" ));
124 :
125 0 : ctx->txncount_state = FD_BENCHO_STATE_SENT;
126 0 : ctx->txncount_deadline = fd_log_wallclock() + FD_BENCHO_RPC_RESPONSE_TIMEOUT;
127 :
128 0 : did_work = 1;
129 0 : }
130 :
131 0 : if( FD_UNLIKELY( ctx->txncount_state==FD_BENCHO_STATE_SENT ) ) {
132 0 : fd_rpc_client_response_t * response = fd_rpc_client_status( ctx->rpc, ctx->txncount_request, 0 );
133 0 : if( FD_UNLIKELY( response->status==FD_RPC_CLIENT_PENDING ) ) {
134 0 : if( FD_UNLIKELY( fd_log_wallclock()>=ctx->txncount_deadline ) )
135 0 : FD_LOG_ERR(( "timed out waiting for RPC server to respond" ));
136 0 : return did_work;
137 0 : }
138 :
139 0 : if( FD_UNLIKELY( response->status!=FD_RPC_CLIENT_SUCCESS ) )
140 0 : FD_LOG_ERR(( "RPC server returned error %ld", response->status ));
141 :
142 0 : ulong txns = response->result.transaction_count.transaction_count;
143 0 : if( FD_LIKELY( ctx->txncount_measured1 ) )
144 0 : FD_LOG_NOTICE(( "%lu txn/s", (ulong)((double)(txns - ctx->txncount_prev)/1.2 )));
145 0 : ctx->txncount_measured1 = 1;
146 0 : ctx->txncount_prev = txns;
147 0 : ctx->txncount_nextprint += 1200L * 1000L * 1000L; /* 1.2 seconds til we print again, multiple of slot duration to prevent jitter */
148 :
149 0 : fd_rpc_client_close( ctx->rpc, ctx->txncount_request );
150 0 : ctx->txncount_state = FD_BENCHO_STATE_WAIT;
151 0 : ctx->txncount_deadline = ctx->txncount_nextprint;
152 :
153 0 : did_work = 1;
154 0 : }
155 :
156 0 : return did_work;
157 0 : }
158 :
159 : static inline void
160 : after_credit( fd_bencho_ctx_t * ctx,
161 : fd_stem_context_t * stem,
162 : int * opt_poll_in,
163 0 : int * charge_busy ) {
164 0 : (void)opt_poll_in;
165 :
166 0 : int did_work_rpc = fd_rpc_client_service( ctx->rpc, 0 );
167 0 : int did_work_service_block_hash = service_block_hash( ctx, stem );
168 0 : int did_work_service_txn_count = service_txn_count( ctx );
169 :
170 0 : *charge_busy = did_work_rpc | did_work_service_block_hash | did_work_service_txn_count;
171 0 : }
172 :
173 : static void
174 : unprivileged_init( fd_topo_t * topo,
175 0 : fd_topo_tile_t * tile ) {
176 0 : void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
177 :
178 0 : FD_SCRATCH_ALLOC_INIT( l, scratch );
179 0 : fd_bencho_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_bencho_ctx_t ), sizeof( fd_bencho_ctx_t ) );
180 :
181 0 : ctx->mem = topo->workspaces[ topo->objs[ topo->links[ tile->out_link_id[ 0 ] ].dcache_obj_id ].wksp_id ].wksp;
182 0 : ctx->out_chunk0 = fd_dcache_compact_chunk0( ctx->mem, topo->links[ tile->out_link_id[ 0 ] ].dcache );
183 0 : ctx->out_wmark = fd_dcache_compact_wmark ( ctx->mem, topo->links[ tile->out_link_id[ 0 ] ].dcache, topo->links[ tile->out_link_id[ 0 ] ].mtu );
184 0 : ctx->out_chunk = ctx->out_chunk0;
185 :
186 0 : ctx->rpc_ready_deadline = fd_log_wallclock() + FD_BENCHO_RPC_INITIALIZE_TIMEOUT;
187 0 : ctx->blockhash_state = FD_BENCHO_STATE_READY;
188 0 : ctx->txncount_nextprint = 0;
189 0 : ctx->txncount_state = FD_BENCHO_STATE_READY;
190 0 : ctx->txncount_measured1 = 0;
191 :
192 0 : FD_LOG_NOTICE(( "connecting to RPC server " FD_IP4_ADDR_FMT ":%u", FD_IP4_ADDR_FMT_ARGS( tile->bencho.rpc_ip_addr ), tile->bencho.rpc_port ));
193 0 : FD_TEST( fd_rpc_client_join( fd_rpc_client_new( ctx->rpc, tile->bencho.rpc_ip_addr, tile->bencho.rpc_port ) ) );
194 :
195 0 : ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, 1UL );
196 0 : if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
197 0 : FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
198 0 : }
199 :
200 0 : #define STEM_BURST (1UL)
201 :
202 0 : #define STEM_CALLBACK_CONTEXT_TYPE fd_bencho_ctx_t
203 0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_bencho_ctx_t)
204 :
205 0 : #define STEM_CALLBACK_AFTER_CREDIT after_credit
206 :
207 : #include "../../../disco/stem/fd_stem.c"
208 :
209 : fd_topo_run_tile_t fd_tile_bencho = {
210 : .name = "bencho",
211 : .scratch_align = scratch_align,
212 : .scratch_footprint = scratch_footprint,
213 : .unprivileged_init = unprivileged_init,
214 : .run = stem_run,
215 : };
|