LCOV - code coverage report
Current view: top level - app/fdctl/run/tiles - fd_cswtch.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 9 133 6.8 %
Date: 2025-01-08 12:08:44 Functions: 2 7 28.6 %

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

Generated by: LCOV version 1.14