Skip to content

Add USDT probes for Nexus background tasks #8567

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions nexus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ tokio-postgres = { workspace = true, features = ["with-serde_json-1"] }
tokio-util = { workspace = true, features = ["codec"] }
tough.workspace = true
tufaceous-artifact.workspace = true
usdt.workspace = true
uuid.workspace = true

nexus-auth.workspace = true
Expand Down
24 changes: 23 additions & 1 deletion nexus/src/app/background/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
//! None of this file is specific to Nexus or any of the specific background
//! tasks in Nexus, although the design is pretty bespoke for what Nexus needs.

use crate::app::background::probes;

use super::BackgroundTask;
use super::TaskName;
use assert_matches::assert_matches;
Expand Down Expand Up @@ -129,6 +131,7 @@ impl Driver {
name.clone(),
)]));
let task_exec = TaskExec::new(
&name,
taskdef.period,
taskdef.task_impl,
activator.clone(),
Expand Down Expand Up @@ -189,6 +192,7 @@ impl Driver {
/// If the task is currently running, it will be activated again when it
/// finishes.
pub(super) fn activate(&self, task: &TaskName) {
probes::background__task__activate!(|| task.as_str());
self.task_required(task).activator.activate();
}

Expand Down Expand Up @@ -234,6 +238,8 @@ pub struct TaskDefinition<'a, N: ToString, D: ToString> {
/// Encapsulates state needed by the background tokio task to manage activation
/// of the background task
struct TaskExec {
/// the name of this background task
name: String,
/// how often the background task should be activated
period: Duration,
/// impl of the background task
Expand All @@ -251,13 +257,23 @@ struct TaskExec {

impl TaskExec {
fn new(
name: impl ToString,
period: Duration,
imp: Box<dyn BackgroundTask>,
activation: Activator,
opctx: OpContext,
status_tx: watch::Sender<TaskStatus>,
) -> TaskExec {
TaskExec { period, imp, activation, opctx, status_tx, iteration: 0 }
let name = name.to_string();
TaskExec {
name,
period,
imp,
activation,
opctx,
status_tx,
iteration: 0,
}
}

/// Body of the tokio task that manages activation of this background task
Expand Down Expand Up @@ -318,10 +334,16 @@ impl TaskExec {
});

// Do it!
probes::background__task__activate__start!(|| {
(&self.name, self.iteration, format!("{reason:?}"))
});
let details = self.imp.activate(&self.opctx).await;
let details_str = serde_json::to_string(&details).unwrap_or_else(|e| {
format!("<<failed to serialize task status: {}>>", e)
});
probes::background__task__activate__done!(|| {
(&self.name, self.iteration, &details_str)
});

let elapsed = start_instant.elapsed();

Expand Down
22 changes: 22 additions & 0 deletions nexus/src/app/background/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,25 @@ impl TaskName {
&self.0
}
}

#[usdt::provider(provider = "nexus")]
mod probes {
/// Fires just before explicitly activating the named background task.
fn background__task__activate(task_name: &str) {}

/// Fires just before running the activate method of the named task.
fn background__task__activate__start(
task_name: &str,
iteration: u64,
reason: &str,
) {
}

/// Fires just after completing the task, with the result as JSON.
fn background__task__activate__done(
task_name: &str,
iteration: u64,
details: &str,
) {
}
}
27 changes: 27 additions & 0 deletions tools/dtrace/nexus/trace-bgtasks.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/sbin/dtrace -Zqs

nexus*:::background-task-activate-start
{
this->task_name = copyinstr(arg0);
iters[this->task_name] = arg1;
reasons[this->task_name] = copyinstr(arg2);
ts[this->task_name] = timestamp;
}

nexus*:::background-task-activate-done
/iters[this->task_name]/
{
this->task_name = copyinstr(arg0);
this->duration = timestamp - ts[this->task_name];
printf(
"task_name=\"%s\" iteration=%d reason=%s details=%s duration=%dus\n",
this->task_name,
iters[this->task_name],
reasons[this->task_name],
copyinstr(arg2),
this->duration / 1000
);
iters[this->task_name] = 0;
reasons[this->task_name] = 0;
ts[this->task_name] = 0;
}
Loading