LCOV - code coverage report
Current view: top level - app/shared/commands/monitor - monitor.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 431 0.0 %
Date: 2025-07-01 05:00:49 Functions: 0 12 0.0 %

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

Generated by: LCOV version 1.14