-
Notifications
You must be signed in to change notification settings - Fork 19
feat(sidecar)!: Add stats computation via SHM #1821
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
bwoebi
wants to merge
12
commits into
main
Choose a base branch
from
bob/shm-stats-computation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
fc1ab19
Add stats computation via SHM
bwoebi 1274931
clippy
bwoebi 95ae27b
PR comments
bwoebi 01d04e8
Fix ftruncate() causing SIGBUS on /dev/shm full
bwoebi 7a89da5
Add tag filters to /info
bwoebi 846efa7
Fixup after rebase
bwoebi b60fd46
Actually reuse the exporter
bwoebi c179590
fix compile
bwoebi 7365de0
Stats exporter in README
bwoebi 205a42e
Fix data races with concentrator shutdown
bwoebi ddece1d
Avoid TracerHeaderMap
bwoebi 93f6b78
Revert TracerHeaderMap changes
bwoebi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| // Copyright 2021-Present Datadog, Inc. https://www.datadoghq.com/ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| //! Lock-free `Option<T>` with atomic take, valid for any `T` where | ||
| //! `size_of::<Option<T>>() <= 8`. | ||
|
|
||
| use std::cell::UnsafeCell; | ||
| use std::mem::{self, MaybeUninit}; | ||
| use std::ptr; | ||
| use std::sync::atomic::{AtomicU16, AtomicU32, AtomicU64, AtomicU8, Ordering}; | ||
|
|
||
| /// An `Option<T>` that supports lock-free atomic take. | ||
| /// | ||
| /// # Constraints | ||
| /// `size_of::<Option<T>>()` must be `<= 8`. Enforced by a `debug_assert` in | ||
| /// `From<Option<T>>`). This holds for niche-optimised types (`NonNull<T>`, | ||
| /// `Box<T>`, …) and for any `Option<T>` that fits in a single machine word. | ||
| /// | ||
| /// # Storage | ||
| /// The option is stored in a `UnsafeCell<Option<T>>`, giving it exactly the size | ||
| /// and alignment of `Option<T>` itself. `take()` picks the narrowest atomic that | ||
| /// covers `size_of::<Option<T>>()` bytes — `AtomicU8` for 1-byte options up to | ||
| /// `AtomicU64` for 5–8 byte options. The atomic cast is valid because | ||
| /// `align_of::<AtomicUN>() == align_of::<uN>() <= align_of::<Option<T>>()`. | ||
| /// | ||
| /// # None sentinel | ||
| /// The "none" bit-pattern is computed by value (`Option::<T>::None`) rather than | ||
| /// assumed to be zero, so the implementation is correct for both niche-optimised | ||
| /// types and discriminant-based options. | ||
| /// | ||
| /// `UnsafeCell` provides the interior-mutability aliasing permission required by | ||
| /// Rust's memory model when mutating through a shared reference. | ||
| pub struct AtomicOption<T>(UnsafeCell<Option<T>>); | ||
|
|
||
| impl<T> AtomicOption<T> { | ||
| /// Encode `val` as a `u64`, transferring ownership into the bit representation. | ||
| const fn encode(val: Option<T>) -> u64 { | ||
| let mut bits = 0u64; | ||
| unsafe { | ||
| ptr::copy_nonoverlapping( | ||
| ptr::from_ref(&val).cast::<u8>(), | ||
| ptr::from_mut(&mut bits).cast::<u8>(), | ||
| size_of::<Option<T>>(), | ||
| ); | ||
| mem::forget(val); | ||
| } | ||
| bits | ||
| } | ||
|
|
||
| /// Atomically swap the storage with `new_bits`, returning the old bits. | ||
| #[inline] | ||
| fn atomic_swap(&self, new_bits: u64) -> u64 { | ||
| unsafe { | ||
| let ptr = self.0.get(); | ||
| match size_of::<Option<T>>() { | ||
| 1 => (*(ptr as *const AtomicU8)).swap(new_bits as u8, Ordering::AcqRel) as u64, | ||
| 2 => (*(ptr as *const AtomicU16)).swap(new_bits as u16, Ordering::AcqRel) as u64, | ||
| 3 | 4 => { | ||
| (*(ptr as *const AtomicU32)).swap(new_bits as u32, Ordering::AcqRel) as u64 | ||
| } | ||
| _ => (*(ptr as *const AtomicU64)).swap(new_bits, Ordering::AcqRel), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Reconstruct an `Option<T>` from its `u64` bit representation. | ||
| /// | ||
| /// # Safety | ||
| /// `bits` must hold a valid `Option<T>` bit-pattern in its low | ||
| /// `size_of::<Option<T>>()` bytes, as produced by a previous `encode`. | ||
| const unsafe fn decode(bits: u64) -> Option<T> { | ||
| let mut result = MaybeUninit::<Option<T>>::uninit(); | ||
| ptr::copy_nonoverlapping( | ||
| ptr::from_ref(&bits).cast::<u8>(), | ||
| result.as_mut_ptr().cast::<u8>(), | ||
| size_of::<Option<T>>(), | ||
| ); | ||
| result.assume_init() | ||
| } | ||
|
|
||
| /// Atomically replace the stored value with `None` and return what was there. | ||
| /// Returns `None` if the value was already taken. | ||
| pub fn take(&self) -> Option<T> { | ||
| let old = self.atomic_swap(Self::encode(None)); | ||
| // SAFETY: `old` holds a valid `Option<T>` bit-pattern. | ||
| unsafe { Self::decode(old) } | ||
| } | ||
|
|
||
| /// Atomically store `val`, dropping any previous value. | ||
| pub fn set(&self, val: Option<T>) -> Option<T> { | ||
| let old = self.atomic_swap(Self::encode(val)); | ||
| unsafe { Self::decode(old) } | ||
| } | ||
|
|
||
| /// Atomically store `Some(val)`, returning the previous value. | ||
| pub fn replace(&self, val: T) -> Option<T> { | ||
| self.set(Some(val)) | ||
| } | ||
|
|
||
| /// Borrow the current value without taking it. | ||
| /// | ||
| /// # Safety | ||
| /// Must not be called concurrently with [`take`], [`set`], or [`replace`]. | ||
| pub unsafe fn as_option(&self) -> &Option<T> { | ||
| &*self.0.get() | ||
| } | ||
| } | ||
|
|
||
| impl<T> From<Option<T>> for AtomicOption<T> { | ||
| fn from(val: Option<T>) -> Self { | ||
| // we may raise this to 16 once AtomicU128 becomes stable | ||
| debug_assert!( | ||
| size_of::<Option<T>>() <= size_of::<u64>(), | ||
| "AtomicOption requires size_of::<Option<T>>() <= 8, got {}", | ||
| size_of::<Option<T>>() | ||
| ); | ||
| Self(UnsafeCell::new(val)) | ||
| } | ||
| } | ||
|
|
||
| // `AtomicOption<T>` is `Send`/`Sync` when `T: Send` — same contract as `Mutex<Option<T>>`. | ||
| unsafe impl<T: Send> Send for AtomicOption<T> {} | ||
| unsafe impl<T: Send> Sync for AtomicOption<T> {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have a strong opinion but this should probably be discussed in #libdatadog
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We've done that in the past already for cases where our MSRV did not support APIs which became the only solution to satisfy a clippy lint in future. It misses a comment though, when we'll be able to drop this.