Tracing
This commit is contained in:
@@ -10,6 +10,7 @@ use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::time::Duration;
|
||||
|
||||
use super::uring::{IORING_OP_ASYNC_CANCEL, IoUring, IoUringCqe, IoUringSqe};
|
||||
use crate::trace_targets;
|
||||
|
||||
const WAKE_TARGET_TOKEN: u64 = 1;
|
||||
const TOKEN_KIND_SHIFT: u64 = 56;
|
||||
@@ -34,6 +35,13 @@ struct NotifierInner {
|
||||
|
||||
impl NotifierInner {
|
||||
fn notify(&self) -> io::Result<()> {
|
||||
#[cfg(debug_assertions)]
|
||||
tracing::trace!(
|
||||
target: trace_targets::DRIVER,
|
||||
event = "notify",
|
||||
ring_fd = self.ring_fd,
|
||||
"sending cross-thread driver wake"
|
||||
);
|
||||
if self.closed.load(Ordering::Acquire) {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::BrokenPipe,
|
||||
@@ -96,6 +104,12 @@ pub fn create() -> io::Result<(Driver, ThreadNotifier)> {
|
||||
/// emphasize driver construction.
|
||||
pub fn create_driver() -> io::Result<(Driver, ThreadNotifier)> {
|
||||
let ring = IoUring::new(64)?;
|
||||
tracing::debug!(
|
||||
target: trace_targets::DRIVER,
|
||||
event = "create_driver",
|
||||
ring_fd = ring.ring_fd(),
|
||||
"created runtime driver"
|
||||
);
|
||||
let notifier = Arc::new(NotifierInner {
|
||||
ring_fd: ring.ring_fd(),
|
||||
closed: AtomicBool::new(false),
|
||||
@@ -130,11 +144,27 @@ impl Driver {
|
||||
let saw_any = self
|
||||
.ring
|
||||
.drain_completions(|cqe| self.process_cqe(cqe, &mut ready));
|
||||
#[cfg(debug_assertions)]
|
||||
if saw_any {
|
||||
tracing::trace!(
|
||||
target: trace_targets::DRIVER,
|
||||
event = "poll_ready",
|
||||
timer_ready = ready.timer,
|
||||
wake_ready = ready.wake,
|
||||
"driver poll produced ready events"
|
||||
);
|
||||
}
|
||||
if saw_any { Ok(Some(ready)) } else { Ok(None) }
|
||||
}
|
||||
|
||||
/// Blocks until at least one completion is available.
|
||||
pub fn wait(&self) -> io::Result<()> {
|
||||
#[cfg(debug_assertions)]
|
||||
tracing::trace!(
|
||||
target: trace_targets::DRIVER,
|
||||
event = "wait",
|
||||
"waiting for driver completion"
|
||||
);
|
||||
self.ring.wait_for_cqe()
|
||||
}
|
||||
|
||||
@@ -142,6 +172,13 @@ impl Driver {
|
||||
///
|
||||
/// Passing `None` removes any active timer.
|
||||
pub fn rearm_timer(&self, deadline: Option<Duration>) -> io::Result<()> {
|
||||
#[cfg(debug_assertions)]
|
||||
tracing::trace!(
|
||||
target: trace_targets::TIMER,
|
||||
event = "rearm_timer",
|
||||
deadline_ns = deadline.map(|value| value.as_nanos() as u64),
|
||||
"rearming driver timer"
|
||||
);
|
||||
match (self.active_timer_token.get(), deadline) {
|
||||
(Some(active), Some(deadline)) => {
|
||||
self.ring.submit_timeout_update(active, deadline)?;
|
||||
@@ -168,6 +205,13 @@ impl Driver {
|
||||
on_complete: impl FnOnce(IoUringCqe) + Send + 'static,
|
||||
) -> io::Result<u64> {
|
||||
let token = self.next_token(CompletionKind::Operation);
|
||||
#[cfg(debug_assertions)]
|
||||
tracing::trace!(
|
||||
target: trace_targets::ASYNC,
|
||||
event = "submit_operation",
|
||||
token,
|
||||
"submitting async driver operation"
|
||||
);
|
||||
self.completions
|
||||
.borrow_mut()
|
||||
.insert(token, Box::new(on_complete));
|
||||
@@ -181,6 +225,13 @@ impl Driver {
|
||||
}
|
||||
|
||||
pub(crate) fn cancel_operation(&self, token: u64) -> io::Result<()> {
|
||||
#[cfg(debug_assertions)]
|
||||
tracing::trace!(
|
||||
target: trace_targets::ASYNC,
|
||||
event = "cancel_operation",
|
||||
token,
|
||||
"submitting async driver cancellation"
|
||||
);
|
||||
self.ring
|
||||
.submit_with_token(self.next_token(CompletionKind::OperationCancel), |sqe| {
|
||||
sqe.opcode = IORING_OP_ASYNC_CANCEL;
|
||||
@@ -216,6 +267,14 @@ impl Driver {
|
||||
}
|
||||
|
||||
fn process_cqe(&self, cqe: IoUringCqe, ready: &mut ReadyEvents) {
|
||||
#[cfg(debug_assertions)]
|
||||
tracing::trace!(
|
||||
target: trace_targets::DRIVER,
|
||||
event = "process_cqe",
|
||||
user_data = cqe.user_data,
|
||||
result = cqe.res,
|
||||
"processing io_uring completion"
|
||||
);
|
||||
if cqe.user_data == WAKE_TARGET_TOKEN {
|
||||
ready.wake = true;
|
||||
let wakes = cqe.res.max(1) as u64;
|
||||
@@ -256,6 +315,11 @@ impl Driver {
|
||||
|
||||
impl Drop for Driver {
|
||||
fn drop(&mut self) {
|
||||
tracing::debug!(
|
||||
target: trace_targets::DRIVER,
|
||||
event = "drop_driver",
|
||||
"dropping runtime driver"
|
||||
);
|
||||
self.notifier.closed.store(true, Ordering::Release);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user