mirror of
https://github.com/mozilla-services/syncstorage-rs.git
synced 2025-08-06 20:06:57 +02:00
Merge pull request #1692 from mozilla-services/fix/threadpool-metrics-STOR-201
Some checks are pending
Glean probe-scraper / glean-probe-scraper (push) Waiting to run
Some checks are pending
Glean probe-scraper / glean-probe-scraper (push) Waiting to run
fix: avoid underflow of the queued_tasks metric
This commit is contained in:
commit
1d355ec7b5
@ -130,18 +130,18 @@ impl BlockingThreadpool {
|
|||||||
T: Send + 'static,
|
T: Send + 'static,
|
||||||
E: fmt::Debug + Send + InternalError + 'static,
|
E: fmt::Debug + Send + InternalError + 'static,
|
||||||
{
|
{
|
||||||
self.spawned_tasks.fetch_add(1, Ordering::SeqCst);
|
self.spawned_tasks.fetch_add(1, Ordering::Relaxed);
|
||||||
// Ensure the counter's always decremented (whether the task completed,
|
// Ensure the counter's always decremented (whether the task completed,
|
||||||
// was cancelled or panicked)
|
// was cancelled or panicked)
|
||||||
scopeguard::defer! {
|
scopeguard::defer! {
|
||||||
self.spawned_tasks.fetch_sub(1, Ordering::SeqCst);
|
self.spawned_tasks.fetch_sub(1, Ordering::Relaxed);
|
||||||
}
|
}
|
||||||
|
|
||||||
let active_threads = Arc::clone(&self.active_threads);
|
let active_threads = Arc::clone(&self.active_threads);
|
||||||
let f_with_metrics = move || {
|
let f_with_metrics = move || {
|
||||||
active_threads.fetch_add(1, Ordering::SeqCst);
|
active_threads.fetch_add(1, Ordering::Relaxed);
|
||||||
scopeguard::defer! {
|
scopeguard::defer! {
|
||||||
active_threads.fetch_sub(1, Ordering::SeqCst);
|
active_threads.fetch_sub(1, Ordering::Relaxed);
|
||||||
}
|
}
|
||||||
f()
|
f()
|
||||||
};
|
};
|
||||||
@ -154,11 +154,14 @@ impl BlockingThreadpool {
|
|||||||
|
|
||||||
/// Return the pool's current metrics
|
/// Return the pool's current metrics
|
||||||
pub fn metrics(&self) -> BlockingThreadpoolMetrics {
|
pub fn metrics(&self) -> BlockingThreadpoolMetrics {
|
||||||
// active_threads is decremented on a separate thread so we need a
|
let spawned_tasks = self.spawned_tasks.load(Ordering::Relaxed);
|
||||||
// strong Ordering to ensure it's in sync w/ spawned_tasks (otherwise
|
// active_threads is decremented on a separate thread so there's no
|
||||||
// it could underflow queued_tasks)
|
// Drop order guarantee of spawned_tasks decrementing before it does:
|
||||||
let spawned_tasks = self.spawned_tasks.load(Ordering::SeqCst);
|
// catch the case where active_threads is larger
|
||||||
let active_threads = self.active_threads.load(Ordering::SeqCst);
|
let active_threads = self
|
||||||
|
.active_threads
|
||||||
|
.load(Ordering::Relaxed)
|
||||||
|
.min(spawned_tasks);
|
||||||
BlockingThreadpoolMetrics {
|
BlockingThreadpoolMetrics {
|
||||||
queued_tasks: spawned_tasks - active_threads,
|
queued_tasks: spawned_tasks - active_threads,
|
||||||
active_threads,
|
active_threads,
|
||||||
|
Loading…
Reference in New Issue
Block a user