Skip to content

Commit 15a5128

Browse files
committed
chore(crashtracker): use weaker mem ordering for OP_COUTERS
1 parent 6a02f01 commit 15a5128

3 files changed

Lines changed: 14 additions & 13 deletions

File tree

libdd-crashtracker-ffi/src/collector/counters.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use libdd_common_ffi::{wrap_with_void_ffi_result, VoidResult};
1717
#[must_use]
1818
#[named]
1919
pub unsafe extern "C" fn ddog_crasht_reset_counters() -> VoidResult {
20-
wrap_with_void_ffi_result!({ libdd_crashtracker::reset_counters()? })
20+
wrap_with_void_ffi_result!({ libdd_crashtracker::reset_counters() })
2121
}
2222

2323
#[no_mangle]

libdd-crashtracker/src/collector/api.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub fn on_fork(
5252
) -> anyhow::Result<()> {
5353
clear_spans()?;
5454
clear_traces()?;
55-
reset_counters()?;
55+
reset_counters();
5656
// Leave the old signal handler in place: they are unaffected by fork.
5757
// https://man7.org/linux/man-pages/man2/sigaction.2.html
5858
// The altstack (if any) is similarly unaffected by fork:

libdd-crashtracker/src/collector/counters.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright 2023-Present Datadog, Inc. https://www.datadoghq.com/
22
// SPDX-License-Identifier: Apache-2.0
33

4-
use std::sync::atomic::{AtomicI64, Ordering::SeqCst};
4+
use std::sync::atomic::{AtomicI64, Ordering};
55
use thiserror::Error;
66

77
#[cfg(unix)]
@@ -48,7 +48,6 @@ impl OpTypes {
4848
#[allow(clippy::declare_interior_mutable_const)]
4949
const ATOMIC_ZERO: AtomicI64 = AtomicI64::new(0);
5050

51-
// TODO: Is this
5251
static OP_COUNTERS: [AtomicI64; OpTypes::SIZE as usize] = [ATOMIC_ZERO; OpTypes::SIZE as usize];
5352

5453
/// Track that an operation (of type op) has begun.
@@ -58,9 +57,7 @@ static OP_COUNTERS: [AtomicI64; OpTypes::SIZE as usize] = [ATOMIC_ZERO; OpTypes:
5857
/// ATOMICITY:
5958
/// This function is atomic.
6059
pub fn begin_op(op: OpTypes) -> Result<(), CounterError> {
61-
// TODO: I'm making everything SeqCst for now. Could possibly gain some
62-
// performance by using a weaker ordering.
63-
let old = OP_COUNTERS[op as usize].fetch_add(1, SeqCst);
60+
let old = OP_COUNTERS[op as usize].fetch_add(1, Ordering::Relaxed);
6461
if old == i64::MAX - 1 {
6562
return Err(CounterError::CounterOverflow(op));
6663
}
@@ -72,7 +69,7 @@ pub fn begin_op(op: OpTypes) -> Result<(), CounterError> {
7269
/// PRECONDITIONS: This function assumes that the crash-tracker is initialized.
7370
/// ATOMICITY: This function is atomic.
7471
pub fn end_op(op: OpTypes) -> Result<(), CounterError> {
75-
let old = OP_COUNTERS[op as usize].fetch_sub(1, SeqCst);
72+
let old = OP_COUNTERS[op as usize].fetch_sub(1, Ordering::Relaxed);
7673
if old <= 0 {
7774
return Err(CounterError::OperationNotStarted(op));
7875
}
@@ -103,7 +100,12 @@ pub fn emit_counters(w: &mut impl Write) -> Result<(), CounterError> {
103100

104101
writeln!(w, "{DD_CRASHTRACK_BEGIN_COUNTERS}")?;
105102
for (i, c) in OP_COUNTERS.iter().enumerate() {
106-
writeln!(w, "{{\"{}\": {}}}", OpTypes::name(i)?, c.load(SeqCst))?;
103+
writeln!(
104+
w,
105+
"{{\"{}\": {}}}",
106+
OpTypes::name(i)?,
107+
c.load(Ordering::Relaxed)
108+
)?;
107109
}
108110
writeln!(w, "{DD_CRASHTRACK_END_COUNTERS}")?;
109111
w.flush()?;
@@ -113,14 +115,13 @@ pub fn emit_counters(w: &mut impl Write) -> Result<(), CounterError> {
113115
/// Resets all counters to 0.
114116
/// Expected to be used after a fork, to reset the counters on the child
115117
/// ATOMICITY:
116-
/// This is NOT ATOMIC.
118+
/// The reset of each individual counter is atomic, but the entire reset is NOT.
117119
/// Should only be used when no conflicting updates can occur,
118120
/// e.g. after a fork but before ops start on the child.
119-
pub fn reset_counters() -> Result<(), CounterError> {
121+
pub fn reset_counters() {
120122
for c in OP_COUNTERS.iter() {
121-
c.store(0, SeqCst);
123+
c.store(0, Ordering::Relaxed);
122124
}
123-
Ok(())
124125
}
125126

126127
#[derive(Debug, Error)]

0 commit comments

Comments
 (0)