LCOV - code coverage report
Current view: top level - disco/cswtch - fd_cswtch_tile.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 153 0.0 %
Date: 2025-08-05 05:04:49 Functions: 0 7 0.0 %

          Line data    Source code
       1             : #include "../metrics/fd_metrics.h"
       2             : #include "../stem/fd_stem.h"
       3             : #include "../topo/fd_topo.h"
       4             : 
       5             : #include <fcntl.h>
       6             : #include <errno.h>
       7             : #include <stdlib.h>
       8             : #include <sys/types.h> /* SEEK_SET */
       9             : #include <time.h>
      10             : #include <unistd.h>
      11             : 
      12             : #include "generated/fd_cswtch_tile_seccomp.h"
      13             : 
      14           0 : #define REPORT_INTERVAL_MILLIS (100L)
      15             : 
      16             : typedef struct {
      17             :   long next_report_nanos;
      18             : 
      19             :   ulong            tile_cnt;
      20             :   long             first_seen_died[ FD_TILE_MAX ];
      21             :   int              status_fds[ FD_TILE_MAX ];
      22             :   volatile ulong * metrics[ FD_TILE_MAX ];
      23             : } fd_cswtch_ctx_t;
      24             : 
      25             : FD_FN_CONST static inline ulong
      26           0 : scratch_align( void ) {
      27           0 :   return 128UL;
      28           0 : }
      29             : 
      30             : FD_FN_PURE static inline ulong
      31           0 : scratch_footprint( fd_topo_tile_t const * tile ) {
      32           0 :   (void)tile;
      33           0 :   ulong l = FD_LAYOUT_INIT;
      34           0 :   l = FD_LAYOUT_APPEND( l, alignof( fd_cswtch_ctx_t ), sizeof( fd_cswtch_ctx_t ) );
      35           0 :   return FD_LAYOUT_FINI( l, scratch_align() );
      36           0 : }
      37             : 
      38             : static void
      39             : before_credit( fd_cswtch_ctx_t *   ctx,
      40             :                fd_stem_context_t * stem,
      41           0 :                int *               charge_busy ) {
      42           0 :   (void)stem;
      43             : 
      44           0 :   long now = fd_log_wallclock();
      45           0 :   if( now<ctx->next_report_nanos ) {
      46           0 :     long diff = ctx->next_report_nanos - now;
      47           0 :     diff = fd_long_min( diff, 2e6 /* 2ms */ );
      48           0 :     struct timespec const ts = {
      49           0 :       .tv_sec  = diff / (long)1e9,
      50           0 :       .tv_nsec = diff % (long)1e9
      51           0 :     };
      52           0 :     clock_nanosleep( CLOCK_REALTIME, 0, &ts, NULL );
      53           0 :     return;
      54           0 :   }
      55           0 :   ctx->next_report_nanos += REPORT_INTERVAL_MILLIS*1000L*1000L;
      56             : 
      57           0 :   *charge_busy = 1;
      58             : 
      59           0 :   for( ulong i=0UL; i<ctx->tile_cnt; i++ ) {
      60           0 :     if( FD_UNLIKELY( -1==lseek( ctx->status_fds[ i ], 0, SEEK_SET ) ) ) FD_LOG_ERR(( "lseek failed (%i-%s)", errno, strerror( errno ) ));
      61             : 
      62           0 :     char contents[ 4096 ] = {0};
      63           0 :     ulong contents_len = 0UL;
      64             : 
      65           0 :     int process_died = 0;
      66           0 :     while( 1 ) {
      67           0 :       if( FD_UNLIKELY( contents_len>=sizeof( contents ) ) ) FD_LOG_ERR(( "contents overflow" ));
      68           0 :       long n = read( ctx->status_fds[ i ], contents + contents_len, sizeof( contents ) - contents_len );
      69           0 :       if( FD_UNLIKELY( -1==n ) ) {
      70           0 :         if( FD_UNLIKELY( errno==ESRCH ) ) {
      71           0 :           process_died = 1;
      72           0 :           break;
      73           0 :         }
      74           0 :         FD_LOG_ERR(( "read failed (%i-%s)", errno, strerror( errno ) ));
      75           0 :       }
      76           0 :       if( FD_LIKELY( 0==n ) ) break;
      77           0 :       contents_len += (ulong)n;
      78           0 :     }
      79             : 
      80           0 :     if( FD_UNLIKELY( process_died ) ) {
      81             :       /* The tile died, but it's a tile which is allowed to shutdown, so
      82             :          just stop updating metrics for it. */
      83           0 :       if( FD_UNLIKELY( ctx->metrics[ i ][ FD_METRICS_GAUGE_TILE_STATUS_OFF ] ) ) continue;
      84           0 :     }
      85             : 
      86             :     /* Supervisor is going to bring the whole process tree down if any
      87             :        of the target PIDs died, so we can ignore this and wait. */
      88           0 :     if( FD_UNLIKELY( process_died ) ) {
      89           0 :       if( FD_UNLIKELY( !ctx->first_seen_died[ i ] ) ) {
      90           0 :         ctx->first_seen_died[ i ] = now;
      91           0 :       } else if( FD_LIKELY( ctx->first_seen_died[ i ]==LONG_MAX ) ) {
      92             :         /* We already reported this, so we can ignore it. */
      93           0 :       } else if( FD_UNLIKELY( now-ctx->first_seen_died[ i ] < 10L*1000L*1000L*1000L ) ) {
      94             :         /* Wait 10 seconds for supervisor to kill us before reporting WARNING */
      95           0 :       } else {
      96           0 :         FD_LOG_WARNING(( "cannot get context switch metrics for dead tile idx %lu", i ));
      97           0 :         ctx->first_seen_died[ i ] = LONG_MAX;
      98           0 :       }
      99           0 :       continue;
     100           0 :     }
     101             : 
     102           0 :     int found_voluntary = 0;
     103           0 :     int found_involuntary = 0;
     104             : 
     105           0 :     char * line = contents;
     106           0 :     while( 1 ) {
     107           0 :       char * next_line = strchr( line, '\n' );
     108           0 :       if( FD_UNLIKELY( NULL==next_line ) ) break;
     109           0 :       *next_line = '\0';
     110             : 
     111           0 :       char * colon = strchr( line, ':' );
     112           0 :       if( FD_UNLIKELY( NULL==colon ) ) FD_LOG_ERR(( "no colon in line '%s'", line ));
     113             : 
     114           0 :       *colon = '\0';
     115           0 :       char * key = line;
     116           0 :       char * value = colon + 1;
     117             : 
     118           0 :       while( ' '==*value || '\t'==*value ) value++;
     119             : 
     120           0 :       if( FD_LIKELY( !strncmp( key, "voluntary_ctxt_switches", 23UL ) ) ) {
     121           0 :         char * endptr;
     122           0 :         ulong voluntary_ctxt_switches = strtoul( value, &endptr, 10 );
     123           0 :         if( FD_UNLIKELY( *endptr!='\0' || voluntary_ctxt_switches==ULONG_MAX ) ) FD_LOG_ERR(( "strtoul failed" ));
     124           0 :         ctx->metrics[ i ][ FD_METRICS_COUNTER_TILE_CONTEXT_SWITCH_VOLUNTARY_COUNT_OFF ] = voluntary_ctxt_switches;
     125           0 :         found_voluntary = 1;
     126           0 :       } else if( FD_LIKELY( !strncmp( key, "nonvoluntary_ctxt_switches", 26UL ) ) ) {
     127           0 :         char * endptr;
     128           0 :         ulong involuntary_ctxt_switches = strtoul( value, &endptr, 10 );
     129           0 :         if( FD_UNLIKELY( *endptr!='\0' || involuntary_ctxt_switches==ULONG_MAX ) ) FD_LOG_ERR(( "strtoul failed" ));
     130           0 :         ctx->metrics[ i ][ FD_METRICS_COUNTER_TILE_CONTEXT_SWITCH_INVOLUNTARY_COUNT_OFF ] = involuntary_ctxt_switches;
     131           0 :         found_involuntary = 1;
     132           0 :       }
     133             : 
     134           0 :       line = next_line + 1;
     135           0 :     }
     136             : 
     137           0 :     if( FD_UNLIKELY( !found_voluntary   ) ) FD_LOG_ERR(( "voluntary_ctxt_switches not found" ));
     138           0 :     if( FD_UNLIKELY( !found_involuntary ) ) FD_LOG_ERR(( "nonvoluntary_ctxt_switches not found" ));
     139           0 :   }
     140           0 : }
     141             : 
     142             : static void
     143             : privileged_init( fd_topo_t *      topo,
     144           0 :                  fd_topo_tile_t * tile ) {
     145           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     146             : 
     147           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     148           0 :   fd_cswtch_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_cswtch_ctx_t ), sizeof( fd_cswtch_ctx_t ) );
     149             : 
     150           0 :   FD_TEST( topo->tile_cnt<FD_TILE_MAX );
     151             : 
     152           0 :   ctx->tile_cnt = topo->tile_cnt;
     153           0 :   for( ulong i=0UL; i<topo->tile_cnt; i++ ) {
     154           0 :     ulong * metrics = fd_metrics_join( fd_topo_obj_laddr( topo, topo->tiles[ i ].metrics_obj_id ) );
     155             : 
     156           0 :     for(;;) {
     157           0 :       ulong pid, tid;
     158           0 :       if( FD_UNLIKELY( tile->id==i ) ) {
     159           0 :         pid = fd_sandbox_getpid();
     160           0 :         tid = fd_sandbox_gettid();
     161           0 :       } else {
     162           0 :         pid = fd_metrics_tile( metrics )[ FD_METRICS_GAUGE_TILE_PID_OFF ];
     163           0 :         tid = fd_metrics_tile( metrics )[ FD_METRICS_GAUGE_TILE_TID_OFF ];
     164           0 :         if( FD_UNLIKELY( !pid || !tid ) ) {
     165           0 :           FD_SPIN_PAUSE();
     166           0 :           continue;
     167           0 :         }
     168           0 :       }
     169             : 
     170           0 :       char path[ 64 ];
     171           0 :       FD_TEST( fd_cstr_printf_check( path, sizeof( path ), NULL, "/proc/%lu/task/%lu/status", pid, tid ) );
     172           0 :       ctx->status_fds[ i ] = open( path, O_RDONLY );
     173           0 :       ctx->metrics[ i ] = fd_metrics_tile( metrics );
     174           0 :       if( FD_UNLIKELY( -1==ctx->status_fds[ i ] ) ) FD_LOG_ERR(( "open failed (%i-%s)", errno, strerror( errno ) ));
     175           0 :       break;
     176           0 :     }
     177           0 :   }
     178           0 : }
     179             : 
     180             : static void
     181             : unprivileged_init( fd_topo_t *      topo,
     182           0 :                    fd_topo_tile_t * tile ) {
     183           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     184             : 
     185           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     186           0 :   fd_cswtch_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_cswtch_ctx_t ), sizeof( fd_cswtch_ctx_t ) );
     187             : 
     188           0 :   memset( ctx->first_seen_died, 0, sizeof( ctx->first_seen_died ) );
     189           0 :   ctx->next_report_nanos = fd_log_wallclock();
     190             : 
     191           0 :   ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, 1UL );
     192           0 :   if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
     193           0 :     FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
     194           0 : }
     195             : 
     196             : static ulong
     197             : populate_allowed_seccomp( fd_topo_t const *      topo,
     198             :                           fd_topo_tile_t const * tile,
     199             :                           ulong                  out_cnt,
     200           0 :                           struct sock_filter *   out ) {
     201           0 :   (void)topo;
     202           0 :   (void)tile;
     203             : 
     204           0 :   populate_sock_filter_policy_fd_cswtch_tile( out_cnt, out, (uint)fd_log_private_logfile_fd() );
     205           0 :   return sock_filter_policy_fd_cswtch_tile_instr_cnt;
     206           0 : }
     207             : 
     208             : static ulong
     209             : populate_allowed_fds( fd_topo_t const *      topo,
     210             :                       fd_topo_tile_t const * tile,
     211             :                       ulong                  out_fds_cnt,
     212           0 :                       int *                  out_fds ) {
     213           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     214             : 
     215           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     216           0 :   fd_cswtch_ctx_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof( fd_cswtch_ctx_t ), sizeof( fd_cswtch_ctx_t ) );
     217             : 
     218           0 :   if( FD_UNLIKELY( out_fds_cnt<2UL+ctx->tile_cnt ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
     219             : 
     220           0 :   ulong out_cnt = 0UL;
     221           0 :   out_fds[ out_cnt++ ] = 2; /* stderr */
     222           0 :   if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
     223           0 :     out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
     224           0 :   for( ulong i=0UL; i<ctx->tile_cnt; i++ )
     225           0 :     out_fds[ out_cnt++ ] = ctx->status_fds[ i ]; /* /proc/<pid>/task/<tid>/status descriptor */
     226           0 :   return out_cnt;
     227           0 : }
     228             : 
     229           0 : #define STEM_BURST (1UL)
     230           0 : #define STEM_LAZY  ((long)10e6) /* 10ms */
     231             : 
     232           0 : #define STEM_CALLBACK_CONTEXT_TYPE  fd_cswtch_ctx_t
     233           0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_cswtch_ctx_t)
     234             : 
     235           0 : #define STEM_CALLBACK_BEFORE_CREDIT before_credit
     236             : 
     237             : #include "../../disco/stem/fd_stem.c"
     238             : 
     239             : fd_topo_run_tile_t fd_tile_cswtch = {
     240             :   .name                     = "cswtch",
     241             :   .populate_allowed_seccomp = populate_allowed_seccomp,
     242             :   .populate_allowed_fds     = populate_allowed_fds,
     243             :   .scratch_align            = scratch_align,
     244             :   .scratch_footprint        = scratch_footprint,
     245             :   .privileged_init          = privileged_init,
     246             :   .unprivileged_init        = unprivileged_init,
     247             :   .run                      = stem_run,
     248             : };

Generated by: LCOV version 1.14