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