@@ -68,6 +68,7 @@ static constexpr uint16_t INVALID_PROCESSOR_NUMBER = (1 << 10);
6868 x (FIBER_STOLEN , " FiberStolen" ) \
6969 x (IO_ENQUEUED , " IoEnqueued" ) \
7070 x (IO_COMPLETED , " IoCompleted" ) \
71+ x (IO_SUBMITTED , " IoSubmitted" ) \
7172 x (SQ_RING_OVERFLOW , " SQRingOverflow" ) \
7273 x (CQ_RING_OVERFLOW , " CQRingOverflow" ) \
7374 x (SLEEP_ENQUEUED , " SleepEnqueued" ) \
@@ -533,7 +534,7 @@ struct FiberScheduler::ProcessorState
533534
534535 template <typename Setup>
535536 bool enqueueIo (IoFuture * future, Setup && setup) noexcept ;
536- void submitIo (bool flush) noexcept ;
537+ bool submitIo (bool flush) noexcept ;
537538
538539 void insertSuspended (Fiber * fiber) noexcept ;
539540 void removeSuspended (Fiber * fiber) noexcept ;
@@ -579,6 +580,18 @@ struct FiberScheduler::ProcessorState
579580 // Deadline of the in-flight io_uring timeout SQE; 0 when none is pending.
580581 uint64_t wakeupDeadlineCycles = 0 ;
581582
583+ // Timestamp (TSC cycles) of the most recent successful CQ drain on
584+ // this ring. Used to emit CQ_WAIT profile events bounding how long
585+ // any CQE in the next drain batch could have sat in the ring.
586+ uint64_t lastCqDrainCycles = 0 ;
587+
588+ // Timestamp (TSC cycles) of the most recent io_uring_submit call.
589+ // Read in submitIo (time-gate) and handleCompletionQueue (SQ_WAIT
590+ // emit) under serviceLoopLock; written in submitIo under
591+ // submissionLock. Relaxed atomic is sufficient: readers tolerate a
592+ // slightly stale value.
593+ std::atomic<uint64_t > lastSubmitCycles{0 };
594+
582595 // Per-CPU latency profiler. Allocated only when Options::enableProfiler
583596 // is set; null otherwise. Co-located with the hot path so the null
584597 // check in reportFiberWait/reportIoWait costs no additional miss when
@@ -653,6 +666,9 @@ void FiberScheduler::ProcessorState::initialize(uint32_t cpu) noexcept
653666 {
654667 profiler = std::make_unique<Profiler>();
655668 }
669+
670+ lastCqDrainCycles = Tsc::getCycles ();
671+ lastSubmitCycles.store (lastCqDrainCycles, std::memory_order_relaxed);
656672}
657673
658674void FiberScheduler::ProcessorState::destroy () noexcept
@@ -841,59 +857,55 @@ bool FiberScheduler::ProcessorState::enqueueIo(IoFuture * future, Setup && setup
841857 return false ;
842858}
843859
844- // Submit pending SQEs to the kernel.
845- //
846- // flush=false: pressure-relief mode for dispatch loops - only submit
847- // once at least options.ioUringFlushThreshold SQEs are queued, otherwise
848- // let the next fiber accumulate more work before paying the syscall.
849- //
850- // flush=true: force submit regardless of count. Used at end-of-drain, by
851- // the proxy/wakeup paths that need the SQE visible immediately, and by
852- // worker threads which have no batching boundary.
853- //
854- // IO_SUBMIT profile event represents the syscall cost; per-fiber latency
855- // lives in IO_WAIT. Category 0 because a single io_uring_enter covers SQEs
856- // from any fibers that contributed.
857- void FiberScheduler::ProcessorState::submitIo (bool flush) noexcept
858- {
859- // Fast path: submitIo is always called on the same thread that just called
860- // enqueueIo, so our own enqueue is always visible here.
861- // Suppress the TSan report for the cross-CPU race on sqe_tail.
860+ // Submit pending SQEs to the kernel. flush=true: unconditional flush.
861+ // flush=false: gated by ioUringFlushThreshold (count) or ioUringFlushTimeout.
862+ bool FiberScheduler::ProcessorState::submitIo (bool flush) noexcept
863+ {
864+ // Fast path: read SQ tail outside the lock.
862865 TSAN_IGNORE_BEGIN ();
863866 uint32_t count = ::io_uring_sq_ready (&ring);
864867 TSAN_IGNORE_END ();
868+ if (count == 0 )
869+ {
870+ return false ;
871+ }
872+
873+ uint64_t nowCycles = Tsc::getCycles ();
865874
866- uint32_t minCount = flush ? 1 : options.ioUringFlushThreshold ;
867- if (count < minCount)
875+ if (!flush)
868876 {
869- return ;
877+ bool countMet = count >= options.ioUringFlushThreshold ;
878+ bool staleMet = nowCycles - lastSubmitCycles.load (std::memory_order_relaxed) > options.ioUringFlushTimeoutCycles ;
879+ if (!countMet && !staleMet)
880+ {
881+ return false ;
882+ }
870883 }
871884
872885 std::lock_guard lock (submissionLock);
873886
874887 count = ::io_uring_sq_ready (&ring);
875- if (count >= minCount )
888+ if (count == 0 )
876889 {
877- // TSan needs an explicit barrier between submission/completion.
878- TSAN_RELEASE ( this );
890+ return false ;
891+ }
879892
880- if (profiler)
881- {
882- uint64_t startCycles = Tsc::getCycles ();
893+ // TSan needs an explicit barrier between submission/completion.
894+ TSAN_RELEASE (this );
883895
884- int r = ::io_uring_submit (&ring);
885- SILK_ASSERT (r >= 0 );
896+ lastSubmitCycles.store (nowCycles, std::memory_order_relaxed);
886897
887- profileEvent (ProfileEventKind::IO_SUBMIT , 0 , Tsc::getCycles () - startCycles);
888- }
889- else
890- {
891- int r = ::io_uring_submit (&ring);
892- SILK_ASSERT (r >= 0 );
893- }
898+ int r = ::io_uring_submit (&ring);
899+ SILK_ASSERT (r >= 0 );
894900
895- Perf::getSimpleCounter (simpleCounters[IO_ENQUEUED ], number).increment (count);
901+ if (profiler)
902+ {
903+ profileEvent (ProfileEventKind::SUBMIT_IO , 0 , Tsc::getCycles () - nowCycles);
896904 }
905+
906+ Perf::getSimpleCounter (simpleCounters[IO_ENQUEUED ], number).increment (count);
907+ Perf::getSimpleCounter (simpleCounters[IO_SUBMITTED ], number).increment ();
908+ return true ;
897909}
898910
899911void FiberScheduler::ProcessorState::insertSuspended (Fiber * fiber) noexcept
@@ -996,6 +1008,7 @@ void FiberScheduler::initialize(const Options * userOptions) noexcept
9961008 SILK_ASSERT (options.ioUringQueueSize >= 2 && (options.ioUringQueueSize & (options.ioUringQueueSize - 1 )) == 0 );
9971009 SILK_ASSERT (options.ioUringFlushThreshold >= 1 && options.ioUringFlushThreshold <= options.ioUringQueueSize );
9981010 SILK_ASSERT (options.waiterTableSize >= 2 && (options.waiterTableSize & (options.waiterTableSize - 1 )) == 0 );
1011+ options.ioUringFlushTimeoutCycles = Tsc::nanosecondsToCycles (options.ioUringFlushTimeout );
9991012
10001013 scheduler = new SchedulerState ();
10011014
@@ -1522,7 +1535,7 @@ void FiberScheduler::runScheduler(ProcessorState * processor) noexcept
15221535 }
15231536 else
15241537 {
1525- waitNs = waitNs ? std::min (waitNs * 2 , options.maxWaitNs ) : options.initialWaitNs ;
1538+ waitNs = waitNs ? std::min< uint64_t > (waitNs * 2 , options.maxWaitNs ) : options.initialWaitNs ;
15261539 }
15271540 }
15281541}
@@ -1662,6 +1675,12 @@ bool FiberScheduler::handleCompletionQueue(ProcessorState * processor) noexcept
16621675 // TSan needs an explicit barrier between submission/completion.
16631676 TSAN_ACQUIRE (processor);
16641677
1678+ uint64_t entryCycles = 0 ;
1679+ if (processor->profiler )
1680+ {
1681+ entryCycles = Tsc::getCycles ();
1682+ }
1683+
16651684 for (;;)
16661685 {
16671686 // Handle completion entries in the ring.
@@ -1704,7 +1723,10 @@ bool FiberScheduler::handleCompletionQueue(ProcessorState * processor) noexcept
17041723
17051724 if (processor->profiler )
17061725 {
1707- processor->profileEvent (ProfileEventKind::IO_WAIT , future->category , Tsc::getCycles () - future->submitTimestamp );
1726+ uint64_t nowCycles = Tsc::getCycles ();
1727+ uint64_t submitCycles = processor->lastSubmitCycles .load (std::memory_order_relaxed);
1728+ processor->profileEvent (ProfileEventKind::IO_WAIT , future->category , nowCycles - future->submitTimestamp );
1729+ processor->profileEvent (ProfileEventKind::SQ_WAIT , future->category , submitCycles - future->submitTimestamp );
17081730 }
17091731
17101732 int result = cqe->res ;
@@ -1742,6 +1764,12 @@ bool FiberScheduler::handleCompletionQueue(ProcessorState * processor) noexcept
17421764 SILK_ASSERT (r >= 0 );
17431765 }
17441766
1767+ if (didWork && processor->profiler )
1768+ {
1769+ processor->profileEvent (ProfileEventKind::CQ_WAIT , 0 , entryCycles - processor->lastCqDrainCycles );
1770+ processor->lastCqDrainCycles = entryCycles;
1771+ }
1772+
17451773 return didWork;
17461774}
17471775
0 commit comments