Skip to content

Commit

Permalink
can discover which future was executed before task halted
Browse files Browse the repository at this point in the history
  • Loading branch information
dshulyak committed Dec 20, 2024
1 parent 0ae2da9 commit 41e63d7
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 3 deletions.
57 changes: 57 additions & 0 deletions e2e/examples/deadlock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//! Note that this example will not finish by itself and almost guaranteed to deadlock.
use std::sync::Arc;

use tokio::sync::Mutex;
use tracing::instrument;

#[tokio::main]
async fn main() {
tracing_stacks::init();

let state = Arc::new(State {
first: Mutex::new(0),
second: Mutex::new(0),
});
let handle1 = tokio::spawn({
let state = state.clone();
async move {
loop {
state.increment_in_order().await;
}
}
});
let handle2 = tokio::spawn({
let state = state.clone();
async move {
loop {
state.increment_out_of_order().await;
}
}
});
handle1.await.expect("no error");
handle2.await.expect("no error");
}

struct State {
first: Mutex<u64>,
second: Mutex<u64>,
}

impl State {
#[instrument(skip(self))]
async fn increment_in_order(&self) {
let mut first = self.first.lock().await;
let mut second = self.second.lock().await;
*first += 1;
*second += 1;
}

#[instrument(skip(self))]
async fn increment_out_of_order(&self) {
let mut second = self.second.lock().await;
let mut first = self.first.lock().await;
*first += 1;
*second += 1;
}
}
9 changes: 9 additions & 0 deletions stacks/src/bpf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pub(crate) enum Program {
Block(Block),
Vfs(Vfs),
Net(Net),
Usdt(Usdt),
}

impl From<Profile> for Program {
Expand Down Expand Up @@ -106,6 +107,7 @@ impl Display for Program {
Program::Block(block) => write!(f, "{}", block),
Program::Vfs(vfs) => write!(f, "{}", vfs),
Program::Net(net) => write!(f, "{}", net),
Program::Usdt(usdt) => write!(f, "{}", usdt),
}
}
}
Expand All @@ -122,6 +124,7 @@ impl TryFrom<&str> for Program {
Some("block") => Ok(Program::Block(parts.try_into()?)),
Some("vfs") => Ok(Program::Vfs(parts.try_into()?)),
Some("net") => Ok(Program::Net(parts.try_into()?)),
Some("usdt") => Ok(Program::Usdt(parts.try_into()?)),
Some(program) => anyhow::bail!("invalid program {}", program),
None => anyhow::bail!("empty program"),
}
Expand Down Expand Up @@ -261,6 +264,12 @@ impl Programs {
}
self.net = Some(net);
}
Program::Usdt(usdt) => {
if self.usdt.is_some() {
anyhow::bail!("duplicate usdt. {} and {}", self.usdt.as_ref().unwrap(), usdt);
}
self.usdt = Some(usdt);
}
}
Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions stacks/src/bpf/stacks.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -489,13 +489,13 @@ int BPF_USDT(stacks_tracing_exit, u64 span_id)
event->pid = pid;
event->cpu_id = bpf_get_smp_processor_id();
event->span_id = span_id;
if (cfg.switch_ustack)
if (cfg.usdt_ustack)
{
event->ustack = bpf_get_stackid(ctx, &stackmap, BPF_F_USER_STACK | BPF_F_FAST_STACK_CMP | BPF_F_REUSE_STACKID);
} else {
event->ustack = -1;
}
if (cfg.switch_kstack)
if (cfg.usdt_kstack)
{
event->kstack = bpf_get_stackid(ctx, &stackmap, BPF_F_FAST_STACK_CMP | BPF_F_REUSE_STACKID);
} else {
Expand Down
2 changes: 1 addition & 1 deletion stacksexport/sql/traceview/trace_exit_and_close.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ select
duration / 1000 as duration,
tgid as pid,
pid as tid,
command as name,
trace_name as name,
cpu,
ustack as end_stack
from stacks
Expand Down

0 comments on commit 41e63d7

Please sign in to comment.