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