Line data Source code
1 : #include "../fdctl.h"
2 :
3 : #include "generated/monitor_seccomp.h"
4 : #include "helper.h"
5 : #include "../run/run.h"
6 : #include "../../../disco/tiles.h"
7 : #include "../../../disco/fd_disco.h"
8 :
9 : #include <stdio.h>
10 : #include <signal.h>
11 : #include <sys/syscall.h>
12 : #include <linux/capability.h>
13 :
14 : void
15 : monitor_cmd_args( int * pargc,
16 : char *** pargv,
17 0 : args_t * args ) {
18 0 : args->monitor.drain_output_fd = -1; /* only accessible to development commands, not the command line */
19 0 : args->monitor.dt_min = fd_env_strip_cmdline_long( pargc, pargv, "--dt-min", NULL, 6666667. );
20 0 : args->monitor.dt_max = fd_env_strip_cmdline_long( pargc, pargv, "--dt-max", NULL, 133333333. );
21 0 : args->monitor.duration = fd_env_strip_cmdline_long( pargc, pargv, "--duration", NULL, 0. );
22 0 : args->monitor.seed = fd_env_strip_cmdline_uint( pargc, pargv, "--seed", NULL, (uint)fd_tickcount() );
23 0 : args->monitor.ns_per_tic = 1./fd_tempo_tick_per_ns( NULL ); /* calibrate during init */
24 :
25 0 : args->monitor.with_bench = fd_env_strip_cmdline_contains( pargc, pargv, "--bench" );
26 0 : args->monitor.with_sankey = fd_env_strip_cmdline_contains( pargc, pargv, "--sankey" );
27 :
28 0 : if( FD_UNLIKELY( args->monitor.dt_min<0L ) ) FD_LOG_ERR(( "--dt-min should be positive" ));
29 0 : if( FD_UNLIKELY( args->monitor.dt_max<args->monitor.dt_min ) ) FD_LOG_ERR(( "--dt-max should be at least --dt-min" ));
30 0 : if( FD_UNLIKELY( args->monitor.duration<0L ) ) FD_LOG_ERR(( "--duration should be non-negative" ));
31 0 : }
32 :
33 : void
34 : monitor_cmd_perm( args_t * args,
35 : fd_caps_ctx_t * caps,
36 0 : config_t * const config ) {
37 0 : (void)args;
38 :
39 0 : ulong mlock_limit = fd_topo_mlock( &config->topo );
40 :
41 0 : fd_caps_check_resource( caps, "monitor", RLIMIT_MEMLOCK, mlock_limit, "call `rlimit(2)` to increase `RLIMIT_MEMLOCK` so all memory can be locked with `mlock(2)`" );
42 :
43 0 : if( fd_sandbox_requires_cap_sys_admin( config->uid, config->gid ) )
44 0 : fd_caps_check_capability( caps, "monitor", CAP_SYS_ADMIN, "call `unshare(2)` with `CLONE_NEWUSER` to sandbox the process in a user namespace" );
45 0 : if( FD_LIKELY( getuid() != config->uid ) )
46 0 : fd_caps_check_capability( caps, "monitor", CAP_SETUID, "call `setresuid(2)` to switch uid to the sanbox user" );
47 0 : if( FD_LIKELY( getgid() != config->gid ) )
48 0 : fd_caps_check_capability( caps, "monitor", CAP_SETGID, "call `setresgid(2)` to switch gid to the sandbox user" );
49 0 : }
50 :
51 : typedef struct {
52 : ulong pid;
53 : ulong heartbeat;
54 :
55 : ulong in_backp;
56 : ulong backp_cnt;
57 :
58 : ulong nvcsw;
59 : ulong nivcsw;
60 :
61 : ulong regime_ticks[9];
62 : } tile_snap_t;
63 :
64 : typedef struct {
65 : ulong mcache_seq;
66 :
67 : ulong fseq_seq;
68 :
69 : ulong fseq_diag_tot_cnt;
70 : ulong fseq_diag_tot_sz;
71 : ulong fseq_diag_filt_cnt;
72 : ulong fseq_diag_filt_sz;
73 : ulong fseq_diag_ovrnp_cnt;
74 : ulong fseq_diag_ovrnr_cnt;
75 : ulong fseq_diag_slow_cnt;
76 : } link_snap_t;
77 :
78 : static ulong
79 0 : tile_total_ticks( tile_snap_t * snap ) {
80 0 : ulong total = 0UL;
81 0 : for( ulong i=0UL; i<9UL; i++ ) total += snap->regime_ticks[ i ];
82 0 : return total;
83 0 : }
84 :
85 : static void
86 : tile_snap( tile_snap_t * snap_cur, /* Snapshot for each tile, indexed [0,tile_cnt) */
87 0 : fd_topo_t * topo ) {
88 0 : for( ulong tile_idx=0UL; tile_idx<topo->tile_cnt; tile_idx++ ) {
89 0 : tile_snap_t * snap = &snap_cur[ tile_idx ];
90 :
91 0 : fd_topo_tile_t * tile = &topo->tiles[ tile_idx ];
92 0 : snap->heartbeat = fd_metrics_tile( tile->metrics )[ FD_METRICS_GAUGE_STEM_HEARTBEAT_OFF ];
93 :
94 0 : fd_metrics_register( tile->metrics );
95 :
96 0 : FD_COMPILER_MFENCE();
97 0 : snap->pid = FD_MGAUGE_GET( TILE, PID );
98 0 : snap->nvcsw = FD_MCNT_GET( TILE, CONTEXT_SWITCH_VOLUNTARY_COUNT );
99 0 : snap->nivcsw = FD_MCNT_GET( TILE, CONTEXT_SWITCH_INVOLUNTARY_COUNT );
100 0 : snap->in_backp = FD_MGAUGE_GET( STEM, IN_BACKPRESSURE );
101 0 : snap->backp_cnt = FD_MCNT_GET( STEM, BACKPRESSURE_COUNT );
102 0 : for( ulong i=0UL; i<9UL; i++ ) {
103 0 : snap->regime_ticks[ i ] = fd_metrics_tl[ MIDX(COUNTER, STEM, REGIME_DURATION_NANOS)+i ];
104 0 : }
105 0 : FD_COMPILER_MFENCE();
106 0 : }
107 0 : }
108 :
109 : static ulong
110 : find_producer_out_idx( fd_topo_t * topo,
111 : fd_topo_tile_t * producer,
112 : fd_topo_tile_t * consumer,
113 0 : ulong consumer_in_idx ) {
114 : /* This finds all reliable consumers of the producers primary output,
115 : and then returns the position of the consumer (specified by tile
116 : and index of the in of that tile) in that list. The list ordering
117 : is not important, except that it matches the ordering of fseqs
118 : provided to the mux tile, so that metrics written for each link
119 : index are retrieved at the same index here.
120 :
121 : This is why we only count reliable links, because the mux tile only
122 : looks at and writes producer side diagnostics (is the link slow)
123 : for reliable links. */
124 :
125 0 : ulong reliable_cons_cnt = 0UL;
126 0 : for( ulong i=0UL; i<topo->tile_cnt; i++ ) {
127 0 : fd_topo_tile_t * consumer_tile = &topo->tiles[ i ];
128 0 : for( ulong j=0UL; j<consumer_tile->in_cnt; j++ ) {
129 0 : for( ulong k=0UL; k<producer->out_cnt; k++ ) {
130 0 : if( FD_UNLIKELY( consumer_tile->in_link_id[ j ]==producer->out_link_id[ k ] && consumer_tile->in_link_reliable[ j ] ) ) {
131 0 : if( FD_UNLIKELY( consumer==consumer_tile && consumer_in_idx==j ) ) return reliable_cons_cnt;
132 0 : reliable_cons_cnt++;
133 0 : }
134 0 : }
135 0 : }
136 0 : }
137 :
138 0 : return ULONG_MAX;
139 0 : }
140 :
141 : static void
142 : link_snap( link_snap_t * snap_cur,
143 0 : fd_topo_t * topo ) {
144 0 : ulong link_idx = 0UL;
145 0 : for( ulong tile_idx=0UL; tile_idx<topo->tile_cnt; tile_idx++ ) {
146 0 : for( ulong in_idx=0UL; in_idx<topo->tiles[ tile_idx ].in_cnt; in_idx++ ) {
147 0 : link_snap_t * snap = &snap_cur[ link_idx ];
148 0 : fd_frag_meta_t const * mcache = topo->links[ topo->tiles[ tile_idx ].in_link_id[ in_idx ] ].mcache;
149 0 : ulong const * seq = (ulong const *)fd_mcache_seq_laddr_const( mcache );
150 0 : snap->mcache_seq = fd_mcache_seq_query( seq );
151 :
152 0 : ulong const * fseq = topo->tiles[ tile_idx ].in_link_fseq[ in_idx ];
153 0 : snap->fseq_seq = fd_fseq_query( fseq );
154 :
155 0 : ulong const * in_metrics = NULL;
156 0 : if( FD_LIKELY( topo->tiles[ tile_idx ].in_link_poll[ in_idx ] ) ) {
157 0 : in_metrics = (ulong const *)fd_metrics_link_in( topo->tiles[ tile_idx ].metrics, in_idx );
158 0 : }
159 :
160 0 : fd_topo_link_t * link = &topo->links[ topo->tiles[ tile_idx ].in_link_id[ in_idx ] ];
161 0 : ulong producer_id = fd_topo_find_link_producer( topo, link );
162 0 : FD_TEST( producer_id!=ULONG_MAX );
163 0 : volatile ulong const * out_metrics = NULL;
164 0 : if( FD_LIKELY( topo->tiles[ tile_idx ].in_link_reliable[ in_idx ] ) ) {
165 0 : fd_topo_tile_t * producer = &topo->tiles[ producer_id ];
166 0 : ulong cons_idx = find_producer_out_idx( topo, producer, &topo->tiles[ tile_idx ], in_idx );
167 :
168 0 : out_metrics = fd_metrics_link_out( producer->metrics, cons_idx );
169 0 : }
170 0 : FD_COMPILER_MFENCE();
171 0 : if( FD_LIKELY( in_metrics ) ) {
172 0 : snap->fseq_diag_tot_cnt = in_metrics[ FD_METRICS_COUNTER_LINK_CONSUMED_COUNT_OFF ];
173 0 : snap->fseq_diag_tot_sz = in_metrics[ FD_METRICS_COUNTER_LINK_CONSUMED_SIZE_BYTES_OFF ];
174 0 : snap->fseq_diag_filt_cnt = in_metrics[ FD_METRICS_COUNTER_LINK_FILTERED_COUNT_OFF ];
175 0 : snap->fseq_diag_filt_sz = in_metrics[ FD_METRICS_COUNTER_LINK_FILTERED_SIZE_BYTES_OFF ];
176 0 : snap->fseq_diag_ovrnp_cnt = in_metrics[ FD_METRICS_COUNTER_LINK_OVERRUN_POLLING_COUNT_OFF ];
177 0 : snap->fseq_diag_ovrnr_cnt = in_metrics[ FD_METRICS_COUNTER_LINK_OVERRUN_READING_COUNT_OFF ];
178 0 : } else {
179 0 : snap->fseq_diag_tot_cnt = 0UL;
180 0 : snap->fseq_diag_tot_sz = 0UL;
181 0 : snap->fseq_diag_filt_cnt = 0UL;
182 0 : snap->fseq_diag_filt_sz = 0UL;
183 0 : snap->fseq_diag_ovrnp_cnt = 0UL;
184 0 : snap->fseq_diag_ovrnr_cnt = 0UL;
185 0 : }
186 :
187 0 : if( FD_LIKELY( out_metrics ) )
188 0 : snap->fseq_diag_slow_cnt = out_metrics[ FD_METRICS_COUNTER_LINK_SLOW_COUNT_OFF ];
189 0 : else
190 0 : snap->fseq_diag_slow_cnt = 0UL;
191 0 : FD_COMPILER_MFENCE();
192 0 : snap->fseq_diag_tot_cnt += snap->fseq_diag_filt_cnt;
193 0 : snap->fseq_diag_tot_sz += snap->fseq_diag_filt_sz;
194 0 : link_idx++;
195 0 : }
196 0 : }
197 0 : }
198 :
199 : /**********************************************************************/
200 :
201 0 : static void write_stdout( char * buf, ulong buf_sz ) {
202 0 : ulong written = 0;
203 0 : ulong total = buf_sz;
204 0 : while( written < total ) {
205 0 : long n = write( STDOUT_FILENO, buf + written, total - written );
206 0 : if( FD_UNLIKELY( n < 0 ) ) {
207 0 : if( errno == EINTR ) continue;
208 0 : FD_LOG_ERR(( "error writing to stdout (%i-%s)", errno, fd_io_strerror( errno ) ));
209 0 : }
210 0 : written += (ulong)n;
211 0 : }
212 0 : }
213 :
214 : static int stop1 = 0;
215 :
216 0 : #define FD_MONITOR_TEXT_BUF_SZ 131072
217 : char buffer[ FD_MONITOR_TEXT_BUF_SZ ];
218 : char buffer2[ FD_MONITOR_TEXT_BUF_SZ ];
219 :
220 : static void
221 : drain_to_buffer( char ** buf,
222 : ulong * buf_sz,
223 0 : int fd ) {
224 0 : while(1) {
225 0 : long nread = read( fd, buffer2, *buf_sz );
226 0 : if( FD_LIKELY( nread == -1 && errno == EAGAIN ) ) break; /* no data available */
227 0 : else if( FD_UNLIKELY( nread == -1 ) ) FD_LOG_ERR(( "read() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
228 :
229 0 : char * ptr = buffer2;
230 0 : char * next;
231 0 : while(( next = memchr( ptr, '\n', (ulong)nread - (ulong)(ptr - buffer2) ))) {
232 0 : ulong len = (ulong)(next - ptr);
233 0 : if( FD_UNLIKELY( *buf_sz < len ) ) {
234 0 : write_stdout( buffer, FD_MONITOR_TEXT_BUF_SZ - *buf_sz );
235 0 : *buf = buffer;
236 0 : *buf_sz = FD_MONITOR_TEXT_BUF_SZ;
237 0 : }
238 0 : fd_memcpy( *buf, ptr, len );
239 0 : *buf += len;
240 0 : *buf_sz -= len;
241 :
242 0 : if( FD_UNLIKELY( *buf_sz < sizeof(TEXT_NEWLINE)-1 ) ) {
243 0 : write_stdout( buffer, FD_MONITOR_TEXT_BUF_SZ - *buf_sz );
244 0 : *buf = buffer;
245 0 : *buf_sz = FD_MONITOR_TEXT_BUF_SZ;
246 0 : }
247 0 : fd_memcpy( *buf, TEXT_NEWLINE, sizeof(TEXT_NEWLINE)-1 );
248 0 : *buf += sizeof(TEXT_NEWLINE)-1;
249 0 : *buf_sz -= sizeof(TEXT_NEWLINE)-1;
250 :
251 0 : ptr = next + 1;
252 0 : }
253 0 : }
254 0 : }
255 :
256 : void
257 : run_monitor( config_t * const config,
258 : int drain_output_fd,
259 : int with_sankey,
260 : long dt_min,
261 : long dt_max,
262 : long duration,
263 : uint seed,
264 0 : double ns_per_tic ) {
265 0 : fd_topo_t * topo = &config->topo;
266 :
267 : /* Setup local objects used by this app */
268 0 : fd_rng_t _rng[1];
269 0 : fd_rng_t * rng = fd_rng_join( fd_rng_new( _rng, seed, 0UL ) );
270 :
271 0 : tile_snap_t * tile_snap_prv = (tile_snap_t *)fd_alloca( alignof(tile_snap_t), sizeof(tile_snap_t)*2UL*topo->tile_cnt );
272 0 : if( FD_UNLIKELY( !tile_snap_prv ) ) FD_LOG_ERR(( "fd_alloca failed" )); /* Paranoia */
273 0 : tile_snap_t * tile_snap_cur = tile_snap_prv + topo->tile_cnt;
274 :
275 0 : ulong link_cnt = 0UL;
276 0 : for( ulong tile_idx=0UL; tile_idx<topo->tile_cnt; tile_idx++ ) link_cnt += topo->tiles[ tile_idx ].in_cnt;
277 0 : link_snap_t * link_snap_prv = (link_snap_t *)fd_alloca( alignof(link_snap_t), sizeof(link_snap_t)*2UL*link_cnt );
278 0 : if( FD_UNLIKELY( !link_snap_prv ) ) FD_LOG_ERR(( "fd_alloca failed" )); /* Paranoia */
279 0 : link_snap_t * link_snap_cur = link_snap_prv + link_cnt;
280 :
281 : /* Get the initial reference diagnostic snapshot */
282 0 : tile_snap( tile_snap_prv, topo );
283 0 : link_snap( link_snap_prv, topo );
284 0 : long then; long tic; fd_tempo_observe_pair( &then, &tic );
285 :
286 : /* Monitor for duration ns. Note that for duration==0, this
287 : will still do exactly one pretty print. */
288 0 : FD_LOG_NOTICE(( "monitoring --dt-min %li ns, --dt-max %li ns, --duration %li ns, --seed %u", dt_min, dt_max, duration, seed ));
289 :
290 0 : long stop = then + duration;
291 0 : if( duration == 0 ) stop = LONG_MAX;
292 :
293 0 : #define PRINT( ... ) do { \
294 0 : int n = snprintf( buf, buf_sz, __VA_ARGS__ ); \
295 0 : if( FD_UNLIKELY( n<0 ) ) FD_LOG_ERR(( "snprintf failed" )); \
296 0 : if( FD_UNLIKELY( (ulong)n>=buf_sz ) ) FD_LOG_ERR(( "snprintf truncated" )); \
297 0 : buf += n; buf_sz -= (ulong)n; \
298 0 : } while(0)
299 :
300 0 : ulong line_count = 0;
301 0 : for(;;) {
302 : /* Wait a somewhat randomized amount and then make a diagnostic
303 : snapshot */
304 0 : fd_log_wait_until( then + dt_min + (long)fd_rng_ulong_roll( rng, 1UL+(ulong)(dt_max-dt_min) ) );
305 :
306 0 : tile_snap( tile_snap_cur, topo );
307 0 : link_snap( link_snap_cur, topo );
308 0 : long now; long toc; fd_tempo_observe_pair( &now, &toc );
309 :
310 : /* Pretty print a comparison between this diagnostic snapshot and
311 : the previous one. */
312 :
313 0 : char * buf = buffer;
314 0 : ulong buf_sz = FD_MONITOR_TEXT_BUF_SZ;
315 :
316 : /* move to beginning of line, n lines ago */
317 0 : PRINT( "\033[%luF", line_count );
318 :
319 : /* drain any firedancer log messages into the terminal */
320 0 : if( FD_UNLIKELY( drain_output_fd >= 0 ) ) drain_to_buffer( &buf, &buf_sz, drain_output_fd );
321 0 : if( FD_UNLIKELY( buf_sz < FD_MONITOR_TEXT_BUF_SZ / 2 ) ) {
322 : /* make sure there's enough space to print the whole monitor in one go */
323 0 : write_stdout( buffer, FD_MONITOR_TEXT_BUF_SZ - buf_sz );
324 0 : buf = buffer;
325 0 : buf_sz = FD_MONITOR_TEXT_BUF_SZ;
326 0 : }
327 :
328 0 : char * mon_start = buf;
329 0 : if( FD_UNLIKELY( drain_output_fd >= 0 ) ) PRINT( TEXT_NEWLINE );
330 :
331 0 : long dt = now-then;
332 :
333 0 : char now_cstr[ FD_LOG_WALLCLOCK_CSTR_BUF_SZ ];
334 0 : PRINT( "snapshot for %s" TEXT_NEWLINE, fd_log_wallclock_cstr( now, now_cstr ) );
335 0 : PRINT( " tile | pid | stale | heart | nivcsw | nvcsw | in backp | backp cnt | %% hkeep | %% wait | %% backp | %% finish" TEXT_NEWLINE );
336 0 : PRINT( "---------+---------+------------+-------+---------------------+---------------------+----------+---------------------+----------+----------+----------+----------" TEXT_NEWLINE );
337 0 : for( ulong tile_idx=0UL; tile_idx<topo->tile_cnt; tile_idx++ ) {
338 0 : tile_snap_t * prv = &tile_snap_prv[ tile_idx ];
339 0 : tile_snap_t * cur = &tile_snap_cur[ tile_idx ];
340 0 : PRINT( " %7s", topo->tiles[ tile_idx ].name );
341 0 : PRINT( " | %7lu", cur->pid );
342 0 : PRINT( " | " ); printf_stale ( &buf, &buf_sz, (long)(0.5+ns_per_tic*(double)(toc - (long)cur->heartbeat)), 1e8 /* 100 millis */ );
343 0 : PRINT( " | " ); printf_heart ( &buf, &buf_sz, (long)cur->heartbeat, (long)prv->heartbeat );
344 0 : PRINT( " | " ); printf_err_cnt ( &buf, &buf_sz, cur->nivcsw, prv->nivcsw );
345 0 : PRINT( " | " ); printf_err_cnt ( &buf, &buf_sz, cur->nvcsw, prv->nvcsw );
346 0 : PRINT( " | " ); printf_err_bool( &buf, &buf_sz, cur->in_backp, prv->in_backp );
347 0 : PRINT( " | " ); printf_err_cnt ( &buf, &buf_sz, cur->backp_cnt, prv->backp_cnt );
348 :
349 0 : ulong cur_hkeep_ticks = cur->regime_ticks[0]+cur->regime_ticks[1]+cur->regime_ticks[2];
350 0 : ulong prv_hkeep_ticks = prv->regime_ticks[0]+prv->regime_ticks[1]+prv->regime_ticks[2];
351 :
352 0 : ulong cur_wait_ticks = cur->regime_ticks[3]+cur->regime_ticks[6];
353 0 : ulong prv_wait_ticks = prv->regime_ticks[3]+prv->regime_ticks[6];
354 :
355 0 : ulong cur_backp_ticks = cur->regime_ticks[5];
356 0 : ulong prv_backp_ticks = prv->regime_ticks[5];
357 :
358 0 : ulong cur_processing_ticks = cur->regime_ticks[4]+cur->regime_ticks[7];
359 0 : ulong prv_processing_ticks = prv->regime_ticks[4]+prv->regime_ticks[7];
360 :
361 0 : PRINT( " | " ); printf_pct( &buf, &buf_sz, cur_hkeep_ticks, prv_hkeep_ticks, 0., tile_total_ticks( cur ), tile_total_ticks( prv ), DBL_MIN );
362 0 : PRINT( " | " ); printf_pct( &buf, &buf_sz, cur_wait_ticks, prv_wait_ticks, 0., tile_total_ticks( cur ), tile_total_ticks( prv ), DBL_MIN );
363 0 : PRINT( " | " ); printf_pct( &buf, &buf_sz, cur_backp_ticks, prv_backp_ticks, 0., tile_total_ticks( cur ), tile_total_ticks( prv ), DBL_MIN );
364 0 : PRINT( " | " ); printf_pct( &buf, &buf_sz, cur_processing_ticks, prv_processing_ticks, 0., tile_total_ticks( cur ), tile_total_ticks( prv ), DBL_MIN );
365 0 : PRINT( TEXT_NEWLINE );
366 0 : }
367 0 : PRINT( TEXT_NEWLINE );
368 0 : PRINT( " link | tot TPS | tot bps | uniq TPS | uniq bps | ha tr%% | uniq bw%% | filt tr%% | filt bw%% | ovrnp cnt | ovrnr cnt | slow cnt | tx seq" TEXT_NEWLINE );
369 0 : PRINT( "------------------+----------+----------+----------+----------+----------+----------+----------+----------+---------------------+---------------------+---------------------+-------------------" TEXT_NEWLINE );
370 :
371 0 : ulong link_idx = 0UL;
372 0 : for( ulong tile_idx=0UL; tile_idx<topo->tile_cnt; tile_idx++ ) {
373 0 : for( ulong in_idx=0UL; in_idx<topo->tiles[ tile_idx ].in_cnt; in_idx++ ) {
374 0 : link_snap_t * prv = &link_snap_prv[ link_idx ];
375 0 : link_snap_t * cur = &link_snap_cur[ link_idx ];
376 :
377 0 : fd_topo_link_t * link = &topo->links[ topo->tiles[ tile_idx ].in_link_id[ in_idx ] ];
378 0 : ulong producer_tile_id = fd_topo_find_link_producer( topo, link );
379 0 : FD_TEST( producer_tile_id != ULONG_MAX );
380 0 : char const * producer = topo->tiles[ producer_tile_id ].name;
381 0 : PRINT( " %7s->%-7s", producer, topo->tiles[ tile_idx ].name );
382 0 : ulong cur_raw_cnt = /* cur->cnc_diag_ha_filt_cnt + */ cur->fseq_diag_tot_cnt;
383 0 : ulong cur_raw_sz = /* cur->cnc_diag_ha_filt_sz + */ cur->fseq_diag_tot_sz;
384 0 : ulong prv_raw_cnt = /* prv->cnc_diag_ha_filt_cnt + */ prv->fseq_diag_tot_cnt;
385 0 : ulong prv_raw_sz = /* prv->cnc_diag_ha_filt_sz + */ prv->fseq_diag_tot_sz;
386 :
387 0 : PRINT( " | " ); printf_rate( &buf, &buf_sz, 1e9, 0., cur_raw_cnt, prv_raw_cnt, dt );
388 0 : PRINT( " | " ); printf_rate( &buf, &buf_sz, 8e9, 0., cur_raw_sz, prv_raw_sz, dt ); /* Assumes sz incl framing */
389 0 : PRINT( " | " ); printf_rate( &buf, &buf_sz, 1e9, 0., cur->fseq_diag_tot_cnt, prv->fseq_diag_tot_cnt, dt );
390 0 : PRINT( " | " ); printf_rate( &buf, &buf_sz, 8e9, 0., cur->fseq_diag_tot_sz, prv->fseq_diag_tot_sz, dt ); /* Assumes sz incl framing */
391 :
392 0 : PRINT( " | " ); printf_pct ( &buf, &buf_sz, cur->fseq_diag_tot_cnt, prv->fseq_diag_tot_cnt, 0.,
393 0 : cur_raw_cnt, prv_raw_cnt, DBL_MIN );
394 0 : PRINT( " | " ); printf_pct ( &buf, &buf_sz, cur->fseq_diag_tot_sz, prv->fseq_diag_tot_sz, 0.,
395 0 : cur_raw_sz, prv_raw_sz, DBL_MIN ); /* Assumes sz incl framing */
396 0 : PRINT( " | " ); printf_pct ( &buf, &buf_sz, cur->fseq_diag_filt_cnt, prv->fseq_diag_filt_cnt, 0.,
397 0 : cur->fseq_diag_tot_cnt, prv->fseq_diag_tot_cnt, DBL_MIN );
398 0 : PRINT( " | " ); printf_pct ( &buf, &buf_sz, cur->fseq_diag_filt_sz, prv->fseq_diag_filt_sz, 0.,
399 0 : cur->fseq_diag_tot_sz, prv->fseq_diag_tot_sz, DBL_MIN ); /* Assumes sz incl framing */
400 :
401 0 : PRINT( " | " ); printf_err_cnt( &buf, &buf_sz, cur->fseq_diag_ovrnp_cnt, prv->fseq_diag_ovrnp_cnt );
402 0 : PRINT( " | " ); printf_err_cnt( &buf, &buf_sz, cur->fseq_diag_ovrnr_cnt, prv->fseq_diag_ovrnr_cnt );
403 0 : PRINT( " | " ); printf_err_cnt( &buf, &buf_sz, cur->fseq_diag_slow_cnt, prv->fseq_diag_slow_cnt );
404 0 : PRINT( " | " ); printf_seq( &buf, &buf_sz, cur->mcache_seq, prv->mcache_seq );
405 0 : PRINT( TEXT_NEWLINE );
406 0 : link_idx++;
407 0 : }
408 0 : }
409 :
410 :
411 0 : if( FD_UNLIKELY( with_sankey ) ) {
412 : /* We only need to count from one of the benchs, since they both receive
413 : all of the transactions. */
414 0 : fd_topo_tile_t const * benchs = &topo->tiles[ fd_topo_find_tile( topo, "benchs", 0UL ) ];
415 0 : ulong fseq_sum = 0UL;
416 0 : for( ulong i=0UL; i<benchs->in_cnt; i++ ) {
417 0 : ulong const * fseq = benchs->in_link_fseq[ i ];
418 0 : fseq_sum += fd_fseq_query( fseq );
419 0 : }
420 :
421 0 : fd_topo_tile_t const * net = &topo->tiles[ fd_topo_find_tile( topo, "net", 0UL ) ];
422 0 : ulong net_sent = fd_mcache_seq_query( fd_mcache_seq_laddr( topo->links[ net->out_link_id[ 0 ] ].mcache ) );
423 0 : net_sent += fd_mcache_seq_query( fd_mcache_seq_laddr( topo->links[ net->out_link_id[ 1 ] ].mcache ) );
424 0 : net_sent = fseq_sum;
425 :
426 0 : ulong verify_failed = 0UL;
427 0 : ulong verify_sent = 0UL;
428 0 : ulong verify_overrun = 0UL;
429 0 : for( ulong i=0UL; i<config->layout.verify_tile_count; i++ ) {
430 0 : fd_topo_tile_t const * verify = &topo->tiles[ fd_topo_find_tile( topo, "verify", i ) ];
431 0 : verify_overrun += fd_metrics_link_in( verify->metrics, 0UL )[ FD_METRICS_COUNTER_LINK_OVERRUN_POLLING_FRAG_COUNT_OFF ] / config->layout.verify_tile_count;
432 0 : verify_failed += fd_metrics_link_in( verify->metrics, 0UL )[ FD_METRICS_COUNTER_LINK_FILTERED_COUNT_OFF ];
433 0 : verify_sent += fd_mcache_seq_query( fd_mcache_seq_laddr( topo->links[ verify->out_link_id[ 0 ] ].mcache ) );
434 0 : }
435 :
436 0 : fd_topo_tile_t const * dedup = &topo->tiles[ fd_topo_find_tile( topo, "dedup", 0UL ) ];
437 0 : ulong dedup_failed = 0UL;
438 0 : for( ulong i=0UL; i<config->layout.verify_tile_count; i++) {
439 0 : dedup_failed += fd_metrics_link_in( dedup->metrics, i )[ FD_METRICS_COUNTER_LINK_FILTERED_COUNT_OFF ];
440 0 : }
441 0 : ulong dedup_sent = fd_mcache_seq_query( fd_mcache_seq_laddr( topo->links[ dedup->out_link_id[ 0 ] ].mcache ) );
442 :
443 0 : fd_topo_tile_t const * pack = &topo->tiles[ fd_topo_find_tile( topo, "pack", 0UL ) ];
444 0 : volatile ulong * pack_metrics = fd_metrics_tile( pack->metrics );
445 0 : ulong pack_invalid = pack_metrics[ FD_METRICS_COUNTER_PACK_TRANSACTION_INSERTED_WRITE_SYSVAR_OFF ] +
446 0 : pack_metrics[ FD_METRICS_COUNTER_PACK_TRANSACTION_INSERTED_ESTIMATION_FAIL_OFF ] +
447 0 : pack_metrics[ FD_METRICS_COUNTER_PACK_TRANSACTION_INSERTED_TOO_LARGE_OFF ] +
448 0 : pack_metrics[ FD_METRICS_COUNTER_PACK_TRANSACTION_INSERTED_EXPIRED_OFF ] +
449 0 : pack_metrics[ FD_METRICS_COUNTER_PACK_TRANSACTION_INSERTED_ADDR_LUT_OFF ] +
450 0 : pack_metrics[ FD_METRICS_COUNTER_PACK_TRANSACTION_INSERTED_UNAFFORDABLE_OFF ] +
451 0 : pack_metrics[ FD_METRICS_COUNTER_PACK_TRANSACTION_INSERTED_DUPLICATE_OFF ] +
452 0 : pack_metrics[ FD_METRICS_COUNTER_PACK_TRANSACTION_INSERTED_PRIORITY_OFF ] +
453 0 : pack_metrics[ FD_METRICS_COUNTER_PACK_TRANSACTION_INSERTED_NONVOTE_REPLACE_OFF ] +
454 0 : pack_metrics[ FD_METRICS_COUNTER_PACK_TRANSACTION_INSERTED_VOTE_REPLACE_OFF ];
455 0 : ulong pack_overrun = pack_metrics[ FD_METRICS_COUNTER_PACK_TRANSACTION_DROPPED_FROM_EXTRA_OFF ];
456 0 : ulong pack_sent = pack_metrics[ FD_METRICS_HISTOGRAM_PACK_TOTAL_TRANSACTIONS_PER_MICROBLOCK_COUNT_OFF + FD_HISTF_BUCKET_CNT ];
457 :
458 0 : static ulong last_fseq_sum;
459 0 : static ulong last_net_sent;
460 0 : static ulong last_verify_overrun;
461 0 : static ulong last_verify_failed;
462 0 : static ulong last_verify_sent;
463 0 : static ulong last_dedup_failed;
464 0 : static ulong last_dedup_sent;
465 0 : static ulong last_pack_overrun;
466 0 : static ulong last_pack_invalid;
467 0 : static ulong last_pack_sent;
468 :
469 0 : PRINT( "TXNS SENT: %-10lu" TEXT_NEWLINE, fseq_sum );
470 0 : PRINT( "NET TXNS SENT: %-10lu %-5.2lf%% %-5.2lf%%" TEXT_NEWLINE, net_sent, 100.0 * (double)net_sent/(double)fseq_sum, 100.0 * (double)(net_sent - last_net_sent)/(double)(fseq_sum - last_fseq_sum) );
471 0 : PRINT( "VERIFY OVERRUN: %-10lu %-5.2lf%% %-5.2lf%%" TEXT_NEWLINE, verify_overrun, 100.0 * (double)verify_overrun/(double)net_sent, 100.0 * (double)(verify_overrun - last_verify_overrun)/(double)(net_sent - last_net_sent) );
472 0 : PRINT( "VERIFY FAILED: %-10lu %-5.2lf%% %-5.2lf%%" TEXT_NEWLINE, verify_failed, 100.0 * (double)verify_failed/(double)net_sent, 100.0 * (double)(verify_failed - last_verify_failed)/(double)(net_sent - last_net_sent) );
473 0 : PRINT( "VERIFY SENT: %-10lu %-5.2lf%% %-5.2lf%%" TEXT_NEWLINE, verify_sent, 100.0 * (double)verify_sent/(double)net_sent, 100.0 * (double)(verify_sent - last_verify_sent)/(double)(net_sent - last_net_sent) );
474 0 : PRINT( "DEDUP FAILED: %-10lu %-5.2lf%% %-5.2lf%%" TEXT_NEWLINE, dedup_failed, 100.0 * (double)dedup_failed/(double)verify_sent, 100.0 * (double)(dedup_failed - last_dedup_failed)/(double)(verify_sent - last_verify_sent) );
475 0 : PRINT( "DEDUP SENT: %-10lu %-5.2lf%% %-5.2lf%%" TEXT_NEWLINE, dedup_sent, 100.0 * (double)dedup_sent/(double)verify_sent, 100.0 * (double)(dedup_sent - last_dedup_sent)/(double)(verify_sent - last_verify_sent) );
476 0 : PRINT( "PACK OVERRUN: %-10lu %-5.2lf%% %-5.2lf%%" TEXT_NEWLINE, pack_overrun, 100.0 * (double)pack_overrun/(double)dedup_sent, 100.0 * (double)(pack_overrun - last_pack_overrun)/(double)(dedup_sent - last_dedup_sent) );
477 0 : PRINT( "PACK INVALID: %-10lu %-5.2lf%% %-5.2lf%%" TEXT_NEWLINE, pack_invalid, 100.0 * (double)pack_invalid/(double)dedup_sent, 100.0 * (double)(pack_invalid - last_pack_invalid)/(double)(dedup_sent - last_dedup_sent) );
478 0 : PRINT( "PACK SENT: %-10lu %-5.2lf%% %-5.2lf%%" TEXT_NEWLINE, pack_sent, 100.0 * (double)pack_sent/(double)dedup_sent, 100.0 * (double)(pack_sent - last_pack_sent)/(double)(dedup_sent - last_dedup_sent) );
479 :
480 0 : last_fseq_sum = fseq_sum;
481 0 : last_net_sent = net_sent;
482 0 : last_verify_overrun = verify_overrun;
483 0 : last_verify_failed = verify_failed;
484 0 : last_verify_sent = verify_sent;
485 0 : last_dedup_failed = dedup_failed;
486 0 : last_dedup_sent = dedup_sent;
487 0 : last_pack_overrun = pack_overrun;
488 0 : last_pack_invalid = pack_invalid;
489 0 : last_pack_sent = pack_sent;
490 0 : }
491 :
492 : /* write entire monitor output buffer */
493 0 : write_stdout( buffer, sizeof(buffer) - buf_sz );
494 :
495 0 : if( FD_UNLIKELY( stop1 || (now-stop)>=0L ) ) {
496 : /* Stop once we've been monitoring for duration ns */
497 0 : break;
498 0 : }
499 :
500 : /* Still more monitoring to do ... wind up for the next iteration by
501 : swapping the two snap arrays. */
502 0 : line_count = 0;
503 0 : for ( ulong i=(ulong)(mon_start-buffer); i<sizeof(buffer) - buf_sz; i++ ) {
504 0 : if( buffer[i] == '\n' ) line_count++;
505 0 : }
506 :
507 0 : then = now; tic = toc;
508 0 : tile_snap_t * tmp = tile_snap_prv; tile_snap_prv = tile_snap_cur; tile_snap_cur = tmp;
509 0 : link_snap_t * tmp2 = link_snap_prv; link_snap_prv = link_snap_cur; link_snap_cur = tmp2;
510 0 : }
511 0 : }
512 :
513 : static void
514 0 : signal1( int sig ) {
515 0 : (void)sig;
516 0 : exit_group( 0 );
517 0 : }
518 :
519 : void
520 : add_bench_topo( fd_topo_t * topo,
521 : char const * affinity,
522 : ulong benchg_tile_cnt,
523 : ulong benchs_tile_cnt,
524 : ulong accounts_cnt,
525 : int transaction_mode,
526 : float contending_fraction,
527 : float cu_price_spread,
528 : ulong conn_cnt,
529 : ushort send_to_port,
530 : uint send_to_ip_addr,
531 : ushort rpc_port,
532 : uint rpc_ip_addr,
533 : int no_quic );
534 :
535 : void
536 : monitor_cmd_fn( args_t * args,
537 0 : config_t * const config ) {
538 0 : if( FD_UNLIKELY( args->monitor.with_bench ) ) {
539 0 : add_bench_topo( &config->topo,
540 0 : config->development.bench.affinity,
541 0 : config->development.bench.benchg_tile_count,
542 0 : config->development.bench.benchs_tile_count,
543 0 : 0UL,
544 0 : 0,
545 0 : 0.0f,
546 0 : 0.0f,
547 0 : 0UL,
548 0 : 0,
549 0 : 0U,
550 0 : 0,
551 0 : 0U,
552 0 : 1 );
553 0 : }
554 :
555 0 : struct sigaction sa = {
556 0 : .sa_handler = signal1,
557 0 : .sa_flags = 0,
558 0 : };
559 0 : if( FD_UNLIKELY( sigaction( SIGTERM, &sa, NULL ) ) )
560 0 : FD_LOG_ERR(( "sigaction(SIGTERM) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
561 0 : if( FD_UNLIKELY( sigaction( SIGINT, &sa, NULL ) ) )
562 0 : FD_LOG_ERR(( "sigaction(SIGINT) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
563 :
564 0 : int allow_fds[ 4 ];
565 0 : ulong allow_fds_cnt = 0;
566 0 : allow_fds[ allow_fds_cnt++ ] = 1; /* stdout */
567 0 : allow_fds[ allow_fds_cnt++ ] = 2; /* stderr */
568 0 : if( FD_LIKELY( fd_log_private_logfile_fd()!=-1 && fd_log_private_logfile_fd()!=1 ) )
569 0 : allow_fds[ allow_fds_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
570 0 : if( FD_UNLIKELY( args->monitor.drain_output_fd!=-1 ) )
571 0 : allow_fds[ allow_fds_cnt++ ] = args->monitor.drain_output_fd; /* maybe we are interposing firedancer log output with the monitor */
572 :
573 0 : fd_topo_join_workspaces( &config->topo, FD_SHMEM_JOIN_MODE_READ_ONLY );
574 :
575 0 : struct sock_filter seccomp_filter[ 128UL ];
576 0 : uint drain_output_fd = args->monitor.drain_output_fd >= 0 ? (uint)args->monitor.drain_output_fd : (uint)-1;
577 0 : populate_sock_filter_policy_monitor( 128UL, seccomp_filter, (uint)fd_log_private_logfile_fd(), drain_output_fd );
578 :
579 0 : if( FD_UNLIKELY( close( STDIN_FILENO ) ) ) FD_LOG_ERR(( "close(0) failed (%i-%s)", errno, fd_io_strerror( errno ) ));
580 0 : if( FD_UNLIKELY( close( config->log.lock_fd ) ) ) FD_LOG_ERR(( "close() failed (%i-%s)", errno, fd_io_strerror( errno ) ));
581 :
582 0 : if( FD_LIKELY( config->development.sandbox ) ) {
583 0 : fd_sandbox_enter( config->uid,
584 0 : config->gid,
585 0 : 0,
586 0 : 1, /* Keep controlling terminal for main so it can receive Ctrl+C */
587 0 : 0UL,
588 0 : allow_fds_cnt,
589 0 : allow_fds,
590 0 : sock_filter_policy_monitor_instr_cnt,
591 0 : seccomp_filter );
592 0 : } else {
593 0 : fd_sandbox_switch_uid_gid( config->uid, config->gid );
594 0 : }
595 :
596 0 : fd_topo_fill( &config->topo );
597 :
598 0 : run_monitor( config,
599 0 : args->monitor.drain_output_fd,
600 0 : args->monitor.with_sankey,
601 0 : args->monitor.dt_min,
602 0 : args->monitor.dt_max,
603 0 : args->monitor.duration,
604 0 : args->monitor.seed,
605 0 : args->monitor.ns_per_tic );
606 :
607 0 : exit_group( 0 );
608 0 : }
|