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