LCOV - code coverage report
Current view: top level - disco/sleep - fd_mwaitx_tile.c (source / functions) Hit Total Coverage
Test: cov.lcov Lines: 0 165 0.0 %
Date: 2026-11-26 02:16:43 Functions: 0 11 0.0 %

          Line data    Source code
       1             : /* The mwaitx tile converts doorbell rings into futex wakes: it naps in
       2             :    hardware wake-on-store on the doorbell cache line (umonitor/umwait
       3             :    on Intel, monitorx/mwaitx on AMD, pause spin fallback), and is the
       4             :    sole issuer of FUTEX_WAKE in the system (a tile parked in epoll on
       5             :    its own fds is woken through its eventfd instead).  It also services
       6             :    parked tiles' deadlines and runs the verifying sweep (seq_mirror vs
       7             :    seq_snap).  Lost-doorbell latency includes producer mirror updates
       8             :    (STEM_LAZY for stem producers), bounded by the park cap. */
       9             : 
      10             : #include "fd_sleep.h"
      11             : 
      12             : #include "../metrics/fd_metrics.h"
      13             : #include "../stem/fd_stem.h"
      14             : #include "../topo/fd_topo.h"
      15             : 
      16             : #if FD_HAS_X86
      17             : #include <cpuid.h>
      18             : #include <x86intrin.h>
      19             : #endif
      20             : 
      21             : #include <linux/futex.h>
      22             : 
      23             : #include "generated/fd_mwaitx_tile_seccomp.h"
      24             : 
      25           0 : #define MWAITX_NAP_TICKS (100000L) /* ~30us at 3GHz, Intel caps umwait residency anyway */
      26             : 
      27             : struct fd_mwaitx_tile {
      28             :   fd_sleep_t * sleep;
      29             : 
      30             :   ulong tile_cnt;
      31             : 
      32             :   int has_waitpkg;
      33             :   int has_mwaitx;
      34             : 
      35             :   /* polled in link ids per tile, for the verifying sweep */
      36             :   uint  in_cnt [ FD_SLEEP_TILE_MAX ];
      37             :   uint  in_link[ FD_SLEEP_TILE_MAX ][ FD_SLEEP_IN_MAX ];
      38             : 
      39             :   /* eventfd doorbell per tile, -1 for a futex waiter */
      40             :   int   eventfd[ FD_SLEEP_TILE_MAX ];
      41             : 
      42             :   long  next_sweep; /* tick of the next deadline/sweep pass */
      43             :   int   napped;     /* wakes go out on the stem iteration after a nap */
      44             : 
      45             :   ulong metrics_nap;
      46             :   ulong metrics_wake;
      47             :   ulong metrics_deadline;
      48             :   ulong metrics_sweep;
      49             : };
      50             : 
      51             : typedef struct fd_mwaitx_tile fd_mwaitx_tile_t;
      52             : 
      53             : FD_FN_CONST static inline ulong
      54           0 : scratch_align( void ) {
      55           0 :   return alignof(fd_mwaitx_tile_t);
      56           0 : }
      57             : 
      58             : FD_FN_PURE static inline ulong
      59           0 : scratch_footprint( fd_topo_tile_t const * tile ) {
      60           0 :   (void)tile;
      61           0 :   return sizeof(fd_mwaitx_tile_t);
      62           0 : }
      63             : 
      64             : static void
      65           0 : metrics_write( fd_mwaitx_tile_t * ctx        ) {
      66           0 :   FD_MCNT_SET( MWAITX, NAP_TAKEN,            ctx->metrics_nap                                           );
      67           0 :   FD_MCNT_SET( MWAITX, WAKE_ISSUED_DOORBELL, ctx->metrics_wake-ctx->metrics_deadline-ctx->metrics_sweep );
      68           0 :   FD_MCNT_SET( MWAITX, WAKE_ISSUED_DEADLINE, ctx->metrics_deadline                                      );
      69           0 :   FD_MCNT_SET( MWAITX, WAKE_ISSUED_SWEEP,    ctx->metrics_sweep                                         );
      70           0 : }
      71             : 
      72             : static inline int
      73           0 : doorbell_rung( ulong const * line ) {
      74           0 :   ulong any = 0UL;
      75           0 :   for( ulong w=0UL; w<FD_SLEEP_BITS_CNT; w++ ) any |= FD_VOLATILE_CONST( line[ w ] );
      76           0 :   return !!any;
      77           0 : }
      78             : 
      79             : #if FD_HAS_X86
      80             : 
      81             : __attribute__((target("waitpkg"))) static inline void
      82             : idle_umwait( ulong const * line,
      83           0 :              long          deadline_tsc ) {
      84           0 :   _umonitor( (void *)line );
      85           0 :   if( FD_UNLIKELY( doorbell_rung( line ) ) ) return;
      86           0 :   _umwait( 0U /* C0.2 */, (unsigned long long)deadline_tsc );
      87           0 : }
      88             : 
      89             : __attribute__((target("mwaitx"))) static inline void
      90             : idle_mwaitx( ulong const * line,
      91           0 :              long          nap_ticks ) {
      92           0 :   _mm_monitorx( (void *)line, 0U, 0U );
      93           0 :   if( FD_UNLIKELY( doorbell_rung( line ) ) ) return;
      94           0 :   _mm_mwaitx( 0x2U /* timer */, 0x0U /* C1 */, (unsigned)nap_ticks );
      95           0 : }
      96             : 
      97             : #endif
      98             : 
      99             : static inline void
     100             : wake( fd_mwaitx_tile_t * ctx,
     101             :       ulong              tid,
     102           0 :       int                cause ) {
     103           0 :   int eventfd = ctx->eventfd[ tid ];
     104           0 :   if( FD_UNLIKELY( eventfd>=0 ) ) fd_sleep_wake_eventfd( &ctx->sleep->tile[ tid ].word, eventfd, cause );
     105           0 :   else                            fd_sleep_wake_one    ( &ctx->sleep->tile[ tid ].word, cause );
     106           0 :   ctx->metrics_wake++;
     107           0 : }
     108             : 
     109             : static void
     110             : before_credit( fd_mwaitx_tile_t *   ctx,
     111             :                fd_stem_context_t * stem,
     112           0 :                int *               charge_busy ) {
     113             :   /* Nap and wake in separate stem iterations, so the nap is billed
     114             :      idle and only the wakes are billed busy. */
     115           0 :   if( FD_LIKELY( !ctx->napped ) ) {
     116           0 :     long deadline = stem->now+MWAITX_NAP_TICKS;
     117           0 : #if FD_HAS_X86
     118           0 :     if(      FD_LIKELY( ctx->has_waitpkg ) ) idle_umwait( ctx->sleep->doorbell, deadline );
     119           0 :     else if( FD_LIKELY( ctx->has_mwaitx  ) ) idle_mwaitx( ctx->sleep->doorbell, MWAITX_NAP_TICKS );
     120           0 :     else
     121           0 : #endif
     122           0 :     while( FD_LIKELY( fd_tickcount()<deadline ) ) {
     123           0 :       if( FD_UNLIKELY( doorbell_rung( ctx->sleep->doorbell ) ) ) break;
     124           0 :       FD_SPIN_PAUSE();
     125           0 :     }
     126             : 
     127           0 :     ctx->metrics_nap++;
     128           0 :     ctx->napped = 1;
     129           0 :     return;
     130           0 :   }
     131           0 :   ctx->napped = 0;
     132             : 
     133             :   /* Nap ended ... send wakes if any first for latency.  No need to wake
     134             :      if already woken, and snapshot the parked bits to avoid races. */
     135           0 :   for( ulong w=0UL; w<FD_SLEEP_BITS_CNT; w++ ) {
     136           0 :     ulong rung = FD_VOLATILE_CONST( ctx->sleep->doorbell[ w ] );
     137           0 :     if( FD_LIKELY( !rung ) ) continue;
     138             : 
     139           0 :     rung = __atomic_exchange_n( &ctx->sleep->doorbell[ w ], 0UL, __ATOMIC_ACQUIRE );
     140           0 :     while( rung ) {
     141           0 :       ulong tid = (w<<6) + (ulong)fd_ulong_find_lsb( rung );
     142           0 :       rung &= rung-1UL;
     143           0 :       if( FD_LIKELY( !FD_VOLATILE_CONST( ctx->sleep->tile[ tid ].word ) ) ) {
     144           0 :         wake( ctx, tid, FD_SLEEP_UNPARK_RING );
     145           0 :         *charge_busy = 1;
     146           0 :       }
     147           0 :     }
     148           0 :   }
     149             : 
     150             :   /* Every nap period, service tile deadlines, and sweep all parked
     151             :      tiles to catch any lost doorbells due to small unavoidable race
     152             :      windows in the read-then-park sequence.  A tile with nothing due
     153             :      parks untimed and relies on this pass for its cap, so every parked
     154             :      tile is visited every pass. */
     155             : 
     156           0 :   if( FD_LIKELY( stem->now<ctx->next_sweep ) ) return;
     157           0 :   ctx->next_sweep = stem->now+MWAITX_NAP_TICKS;
     158             : 
     159           0 :   for( ulong w=0UL; w<FD_SLEEP_BITS_CNT; w++ ) {
     160           0 :     ulong parked = FD_VOLATILE_CONST( ctx->sleep->parked_bits[ w ] );
     161           0 :     while( parked ) {
     162           0 :       ulong tid = (w<<6) + (ulong)fd_ulong_find_lsb( parked );
     163           0 :       parked &= parked-1UL;
     164           0 :       if( FD_UNLIKELY( FD_VOLATILE_CONST( ctx->sleep->tile[ tid ].word ) ) ) continue; /* already woken */
     165             : 
     166           0 :       int cause = -1;
     167           0 :       if( FD_UNLIKELY( (long)FD_VOLATILE_CONST( ctx->sleep->tile[ tid ].deadline )<=stem->now ) ) {
     168           0 :         cause = FD_SLEEP_UNPARK_DEADLINE;
     169           0 :         ctx->metrics_deadline++;
     170           0 :       } else if( FD_LIKELY( !(FD_VOLATILE_CONST( ctx->sleep->credit_bits[ w ] ) & (1UL<<(tid&63UL))) ) ) {
     171             :         /* Producer mirror is ahead of the parked tile's snapshot, a
     172             :            pending frag is available, the doorbell was lost or raced.
     173             :            A tile parked on backpressure (credit bit) cannot use a
     174             :            frag; only its deadline or a credit ring wakes it. */
     175           0 :         for( ulong i=0UL; i<(ulong)ctx->in_cnt[ tid ]; i++ ) {
     176           0 :           ulong mirror = __atomic_load_n( &ctx->sleep->seq_mirror[ ctx->in_link[ tid ][ i ] ], __ATOMIC_ACQUIRE );
     177           0 :           if( FD_UNLIKELY( fd_seq_lt( FD_VOLATILE_CONST( ctx->sleep->seq_snap[ tid ][ i ] ), mirror ) ) ) {
     178           0 :             cause = FD_SLEEP_UNPARK_RING;
     179           0 :             ctx->metrics_sweep++;
     180           0 :             break;
     181           0 :           }
     182           0 :         }
     183           0 :       }
     184             : 
     185           0 :       if( FD_UNLIKELY( cause>=0 ) ) {
     186           0 :         wake( ctx, tid, cause );
     187           0 :         *charge_busy = 1;
     188           0 :       }
     189           0 :     }
     190           0 :   }
     191           0 : }
     192             : 
     193             : static void
     194             : unprivileged_init( fd_topo_t const *      topo,
     195           0 :                    fd_topo_tile_t const * tile ) {
     196           0 :   void * scratch = fd_topo_obj_laddr( topo, tile->tile_obj_id );
     197             : 
     198           0 :   FD_SCRATCH_ALLOC_INIT( l, scratch );
     199           0 :   fd_mwaitx_tile_t * ctx = FD_SCRATCH_ALLOC_APPEND( l, alignof(fd_mwaitx_tile_t), sizeof(fd_mwaitx_tile_t) );
     200             : 
     201           0 :   ctx->sleep = fd_sleep_join( fd_topo_obj_laddr( topo, topo->sleep_obj_id ) );
     202           0 :   FD_TEST( ctx->sleep );
     203           0 :   ctx->tile_cnt = topo->tile_cnt;
     204             : 
     205           0 :   for( ulong i=0UL; i<FD_SLEEP_TILE_MAX; i++ ) ctx->in_cnt[ i ] = 0U;
     206           0 :   for( ulong i=0UL; i<FD_SLEEP_TILE_MAX; i++ ) ctx->eventfd[ i ] = -1;
     207           0 :   for( ulong i=0UL; i<topo->tile_cnt; i++ ) {
     208           0 :     fd_topo_tile_t const * t = &topo->tiles[ i ];
     209           0 :     if( FD_UNLIKELY( t->sleep_eventfd ) ) ctx->eventfd[ t->id ] = FD_SLEEP_EVENTFD( t->id );
     210           0 :     ulong polled = 0UL;
     211           0 :     for( ulong j=0UL; j<t->in_cnt; j++ ) {
     212           0 :       if( FD_UNLIKELY( !t->in_link_poll[ j ] ) ) continue;
     213           0 :       ctx->in_link[ t->id ][ polled++ ] = (uint)t->in_link_id[ j ];
     214           0 :     }
     215           0 :     ctx->in_cnt[ t->id ] = (uint)polled;
     216           0 :   }
     217             : 
     218           0 :   ctx->has_waitpkg = 0;
     219           0 :   ctx->has_mwaitx  = 0;
     220           0 : #if FD_HAS_X86
     221           0 :   uint eax, ebx, ecx, edx;
     222           0 :   if( FD_LIKELY( __get_cpuid_count( 7U, 0U, &eax, &ebx, &ecx, &edx ) ) ) ctx->has_waitpkg = !!(ecx & (1U<<5U));
     223           0 :   if( FD_LIKELY( __get_cpuid( 0x80000001U, &eax, &ebx, &ecx, &edx ) ) ) ctx->has_mwaitx  = !!(ecx & (1U<<29U));
     224           0 : #endif
     225             : 
     226           0 :   ctx->next_sweep       = 0L;
     227           0 :   ctx->napped           = 0;
     228           0 :   ctx->metrics_nap      = 0UL;
     229           0 :   ctx->metrics_wake     = 0UL;
     230           0 :   ctx->metrics_deadline = 0UL;
     231           0 :   ctx->metrics_sweep    = 0UL;
     232             : 
     233           0 :   if( FD_UNLIKELY( !ctx->has_waitpkg && !ctx->has_mwaitx ) ) FD_LOG_WARNING(( "cpu has neither waitpkg nor mwaitx, mwaitx tile will spin" ));
     234             : 
     235           0 :   ulong scratch_top = FD_SCRATCH_ALLOC_FINI( l, scratch_align() );
     236           0 :   if( FD_UNLIKELY( scratch_top > (ulong)scratch + scratch_footprint( tile ) ) )
     237           0 :     FD_LOG_ERR(( "scratch overflow %lu %lu %lu", scratch_top - (ulong)scratch - scratch_footprint( tile ), scratch_top, (ulong)scratch + scratch_footprint( tile ) ));
     238           0 : }
     239             : 
     240             : static ulong
     241             : populate_allowed_seccomp( fd_topo_t const *      topo,
     242             :                           fd_topo_tile_t const * tile,
     243             :                           ulong                  out_cnt,
     244           0 :                           struct sock_filter *   out ) {
     245           0 :   (void)tile;
     246             : 
     247           0 :   ulong id_lo = ULONG_MAX;
     248           0 :   ulong id_hi = 0UL;
     249           0 :   for( ulong i=0UL; i<topo->tile_cnt; i++ ) {
     250           0 :     if( FD_LIKELY( !topo->tiles[ i ].sleep_eventfd ) ) continue;
     251           0 :     id_lo = fd_ulong_min( id_lo, topo->tiles[ i ].id     );
     252           0 :     id_hi = fd_ulong_max( id_hi, topo->tiles[ i ].id+1UL );
     253           0 :   }
     254           0 :   if( FD_LIKELY( id_lo==ULONG_MAX ) ) id_lo = id_hi = 0UL;
     255             : 
     256           0 :   populate_sock_filter_policy_fd_mwaitx_tile( out_cnt, out, (uint)fd_log_private_logfile_fd(),
     257           0 :                                               (uint)FD_SLEEP_EVENTFD( id_lo ), (uint)FD_SLEEP_EVENTFD( id_hi ) );
     258           0 :   return sock_filter_policy_fd_mwaitx_tile_instr_cnt;
     259           0 : }
     260             : 
     261             : static ulong
     262             : populate_allowed_fds( fd_topo_t const *      topo,
     263             :                       fd_topo_tile_t const * tile,
     264             :                       ulong                  out_fds_cnt,
     265           0 :                       int *                  out_fds ) {
     266           0 :   (void)tile;
     267             : 
     268           0 :   if( FD_UNLIKELY( out_fds_cnt<2UL ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
     269             : 
     270           0 :   ulong out_cnt = 0UL;
     271           0 :   out_fds[ out_cnt++ ] = 2; /* stderr */
     272           0 :   if( FD_LIKELY( -1!=fd_log_private_logfile_fd() ) )
     273           0 :     out_fds[ out_cnt++ ] = fd_log_private_logfile_fd(); /* logfile */
     274           0 :   for( ulong i=0UL; i<topo->tile_cnt; i++ ) {
     275           0 :     if( FD_LIKELY( !topo->tiles[ i ].sleep_eventfd ) ) continue;
     276           0 :     if( FD_UNLIKELY( out_cnt>=out_fds_cnt ) ) FD_LOG_ERR(( "out_fds_cnt %lu", out_fds_cnt ));
     277           0 :     out_fds[ out_cnt++ ] = FD_SLEEP_EVENTFD( topo->tiles[ i ].id ); /* eventfd doorbell */
     278           0 :   }
     279           0 :   return out_cnt;
     280           0 : }
     281             : 
     282             : #define STEM_NEVER_PARK 1
     283           0 : #define STEM_BURST (1UL)
     284           0 : #define STEM_LAZY  ((long)10e6) /* 10ms */
     285             : 
     286           0 : #define STEM_CALLBACK_CONTEXT_TYPE  fd_mwaitx_tile_t
     287           0 : #define STEM_CALLBACK_CONTEXT_ALIGN alignof(fd_mwaitx_tile_t)
     288             : 
     289           0 : #define STEM_CALLBACK_BEFORE_CREDIT before_credit
     290           0 : #define STEM_CALLBACK_METRICS_WRITE metrics_write
     291             : 
     292             : #include "../stem/fd_stem.c"
     293             : 
     294             : fd_topo_run_tile_t fd_tile_mwaitx = {
     295             :   .name                     = "mwaitx",
     296             :   .populate_allowed_seccomp = populate_allowed_seccomp,
     297             :   .populate_allowed_fds     = populate_allowed_fds,
     298             :   .scratch_align            = scratch_align,
     299             :   .scratch_footprint        = scratch_footprint,
     300             :   .unprivileged_init        = unprivileged_init,
     301             :   .run                      = stem_run,
     302             : };

Generated by: LCOV version 1.14