Skip to content

Commit

Permalink
DropHelper: fix num_tasks gauge (#15048)
Browse files Browse the repository at this point in the history
  • Loading branch information
msmouse authored Oct 22, 2024
1 parent 280e15b commit 114d361
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions crates/aptos-drop-helper/src/async_concurrent_dropper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl AsyncConcurrentDropper {
pub fn new(name: &'static str, max_tasks: usize, num_threads: usize) -> Self {
Self {
name,
num_tasks_tracker: Arc::new(NumTasksTracker::new(max_tasks)),
num_tasks_tracker: Arc::new(NumTasksTracker::new(name, max_tasks)),
thread_pool: ThreadPool::with_name(format!("{}_conc_dropper", name), num_threads),
}
}
Expand All @@ -49,8 +49,7 @@ impl AsyncConcurrentDropper {

fn schedule_drop_impl<V: Send + 'static>(&self, v: V, notif_sender_opt: Option<Sender<()>>) {
let _timer = TIMER.timer_with(&[self.name, "enqueue_drop"]);
let num_tasks = self.num_tasks_tracker.inc();
GAUGE.set_with(&[self.name, "num_tasks"], num_tasks as i64);
self.num_tasks_tracker.inc();

let name = self.name;
let num_tasks_tracker = self.num_tasks_tracker.clone();
Expand All @@ -70,32 +69,35 @@ impl AsyncConcurrentDropper {
}

struct NumTasksTracker {
name: &'static str,
lock: Mutex<usize>,
cvar: Condvar,
max_tasks: usize,
}

impl NumTasksTracker {
fn new(max_tasks: usize) -> Self {
fn new(name: &'static str, max_tasks: usize) -> Self {
Self {
name,
lock: Mutex::new(0),
cvar: Condvar::new(),
max_tasks,
}
}

fn inc(&self) -> usize {
fn inc(&self) {
let mut num_tasks = self.lock.lock();
while *num_tasks >= self.max_tasks {
num_tasks = self.cvar.wait(num_tasks).expect("lock poisoned.");
}
*num_tasks += 1;
*num_tasks
GAUGE.set_with(&[self.name, "num_tasks"], *num_tasks as i64);
}

fn dec(&self) {
let mut num_tasks = self.lock.lock();
*num_tasks -= 1;
GAUGE.set_with(&[self.name, "num_tasks"], *num_tasks as i64);
self.cvar.notify_all();
}

Expand Down

0 comments on commit 114d361

Please sign in to comment.