-
Notifications
You must be signed in to change notification settings - Fork 21
feat(library-config): add macOS and Windows process context #2238
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
cataphract
wants to merge
4
commits into
glopes/otel-process-ctx-linux-update
Choose a base branch
from
glopes/otel-process-ctx-macos-windows
base: glopes/otel-process-ctx-linux-update
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
4 commits
Select commit
Hold shift + click to select a range
8b0fcb9
feat(library-config): add macOS and Windows process context
cataphract 17e4297
Force otel_process_ctx_v2 exported on win via .drectve
cataphract 954186b
Detect unpublished data on macos after fork
cataphract 174e3b4
refactor: adjust SAFETY comments and a bit of style
morrisonlevi 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
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
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,26 @@ | ||
| // Copyright 2026-Present Datadog, Inc. https://www.datadoghq.com/ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| use portable_atomic::AtomicU128; | ||
|
|
||
| #[cfg(not(any(target_arch = "aarch64", target_arch = "x86_64")))] | ||
| compile_error!("OTel process context only supports aarch64 and x86_64 on macOS"); | ||
| #[cfg(not(target_endian = "little"))] | ||
| compile_error!("OTel process context requires a little-endian macOS target"); | ||
|
|
||
| pub(super) type AtomicPublishedHeader = AtomicU128; | ||
|
|
||
| const _: () = { | ||
| // Both supported macOS architectures have native 128-bit atomics. Keep this assertion so a | ||
| // target change cannot silently select portable-atomic's software-lock fallback. | ||
| assert!(AtomicPublishedHeader::is_always_lock_free()); | ||
| assert!(size_of::<AtomicPublishedHeader>() == size_of::<u128>()); | ||
| assert!(align_of::<AtomicPublishedHeader>() == 16); | ||
| assert!(size_of::<usize>() == size_of::<u64>()); | ||
| }; | ||
|
|
||
| // The low 64 bits contain the header address and the next 32 bits contain the publisher PID. | ||
| // Keeping them in one value lets readers observe both through a single atomic load. | ||
| #[cfg(feature = "process-context-reader")] | ||
| pub(super) const HEADER_ADDRESS_MASK: u128 = u64::MAX as u128; | ||
| pub(super) const PUBLISHER_PID_SHIFT: u32 = u64::BITS; |
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 |
|---|---|---|
|
|
@@ -174,6 +174,64 @@ fn create_pipe() -> io::Result<(OwnedFd, OwnedFd, usize)> { | |
| Ok((read_fd, write_fd, capacity as usize)) | ||
| } | ||
|
|
||
| #[cfg(target_os = "macos")] | ||
| fn create_pipe() -> io::Result<(OwnedFd, OwnedFd, usize)> { | ||
| let mut fds = [0; 2]; | ||
| // SAFETY: fds points to space for the two descriptors returned by pipe. | ||
| if unsafe { libc::pipe(fds.as_mut_ptr()) } != 0 { | ||
| return Err(last_error("failed to create process context copy pipe")); | ||
| } | ||
|
|
||
| // SAFETY: pipe initialized both descriptors and ownership is transferred exactly once. | ||
| let (read_fd, write_fd) = | ||
| unsafe { (OwnedFd::from_raw_fd(fds[0]), OwnedFd::from_raw_fd(fds[1])) }; | ||
| configure_fd(&read_fd)?; | ||
| configure_fd(&write_fd)?; | ||
|
|
||
| // POSIX guarantees that an empty pipe accepts at least PIPE_BUF bytes without blocking. | ||
| // SAFETY: write_fd is a valid pipe descriptor. | ||
| let chunk_size = unsafe { libc::fpathconf(write_fd.as_raw_fd(), libc::_PC_PIPE_BUF) }; | ||
| if chunk_size <= 0 { | ||
| return Err(last_error( | ||
| "failed to query process context copy pipe capacity", | ||
| )); | ||
| } | ||
|
|
||
| Ok((read_fd, write_fd, chunk_size as usize)) | ||
| } | ||
|
|
||
| #[cfg(target_os = "macos")] | ||
| fn configure_fd(fd: &OwnedFd) -> io::Result<()> { | ||
|
Comment on lines
+203
to
+204
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function is okay but I think making a |
||
| // SAFETY: fd is a valid descriptor. | ||
| let status = unsafe { libc::fcntl(fd.as_raw_fd(), libc::F_GETFL) }; | ||
| if status < 0 { | ||
| return Err(last_error( | ||
| "failed to query process context copy pipe status flags", | ||
| )); | ||
| } | ||
| // SAFETY: fd is valid and F_SETFL accepts the status flags returned above. | ||
| if unsafe { libc::fcntl(fd.as_raw_fd(), libc::F_SETFL, status | libc::O_NONBLOCK) } < 0 { | ||
| return Err(last_error( | ||
| "failed to make process context copy pipe non-blocking", | ||
| )); | ||
| } | ||
|
|
||
| // SAFETY: fd is a valid descriptor. | ||
| let descriptor = unsafe { libc::fcntl(fd.as_raw_fd(), libc::F_GETFD) }; | ||
| if descriptor < 0 { | ||
| return Err(last_error( | ||
| "failed to query process context copy pipe descriptor flags", | ||
| )); | ||
| } | ||
| // SAFETY: fd is valid and F_SETFD accepts the descriptor flags returned above. | ||
| if unsafe { libc::fcntl(fd.as_raw_fd(), libc::F_SETFD, descriptor | libc::FD_CLOEXEC) } < 0 { | ||
| return Err(last_error( | ||
| "failed to mark process context copy pipe close-on-exec", | ||
| )); | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn last_error(context: &'static str) -> io::Error { | ||
| let err = io::Error::last_os_error(); | ||
| io::Error::new(err.kind(), format!("{context}: {err}")) | ||
|
|
||
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.
Making sure I understand here:
F_GETPIPE_SZ) because we have it (Linux extension)._PC_PIPE_BUF, as we do not have the full capacity of the pipe on this platform.The practical effect is that on Linux, if we have larger contexts, we can copy it all in one go, whereas on macOS we'll have to loop with syscalls. Yes?