Skip to content

Commit 8941592

Browse files
tracing: add portable-atomic support to other tracing crates (#3246)
This PR expands [`portable-atomic`](https://docs.rs/portable-atomic/) utilisation within Tracing, improving platform support without breaking the existing public API. ## Motivation Since #3199 was merged, it's now possible to bring more tracing crates to atomically challenged platforms through `portable-atomic`. Additionally, CI is not currently setup to ensure this feature behaves as expected (allowing compilation on platforms with incomplete atomic support). ## Solution - Added `portable-atomic` support to: - `tracing` - `tracing-futures` - `tracing-serde` - `tracing-subscriber` - Added `no_std` support to: - `tracing-macros` - `tracing-futures` - Added CI task to catch regressions in `portable-atomic` _and_ `no_std` support. ## Notes - A `critical-section` feature is also added to make CI testing and usage of `tracing`/etc. on atomically challenged platforms simpler. - No additional dependencies are included in this PR, optional or otherwise. Instances of including `portable-atomic` as a dependency only occur when it would have already been included transitively via `tracing-core`'s `portable-atomic-util` dependency. - I checked all instances of replacing `core::sync::atomic` with `portable-atomic` to ensure the public API was unaffected. As such, this is not a breaking change, since it simply adds `portable-atomic` and `critical-section` features.
1 parent 7a3ddfb commit 8941592

File tree

14 files changed

+92
-25
lines changed

14 files changed

+92
-25
lines changed

Diff for: .github/workflows/CI.yml

+24
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,30 @@ jobs:
189189
esac
190190
shell: bash
191191

192+
check-portable-atomic:
193+
# Run `cargo check` on platforms without full atomic support
194+
name: cargo check (${{ matrix.target }} with portable-atomic)
195+
needs: check
196+
strategy:
197+
matrix:
198+
target: ["thumbv6m-none-eabi"]
199+
runs-on: ubuntu-latest
200+
steps:
201+
- uses: actions/checkout@v4
202+
- uses: dtolnay/rust-toolchain@stable
203+
with:
204+
targets: ${{ matrix.target }}
205+
- name: Check tracing-core
206+
run: cargo check --target ${{ matrix.target }} -p tracing-core --no-default-features --features alloc,portable-atomic,critical-section
207+
- name: Check tracing-serde
208+
run: cargo check --target ${{ matrix.target }} -p tracing-serde --no-default-features --features portable-atomic,critical-section
209+
- name: Check tracing
210+
run: cargo check --target ${{ matrix.target }} -p tracing --no-default-features --features attributes,portable-atomic,critical-section
211+
- name: Check tracing-subscriber
212+
run: cargo check --target ${{ matrix.target }} -p tracing-subscriber --no-default-features --features alloc,portable-atomic,critical-section
213+
- name: Check tracing-futures
214+
run: cargo check --target ${{ matrix.target }} -p tracing-futures --no-default-features --features portable-atomic,critical-section
215+
192216
### test jobs #############################################################
193217

194218
test:

Diff for: tracing-core/Cargo.toml

+4-2
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,17 @@ rust-version = "1.63.0"
2727
[features]
2828
default = ["std"]
2929
alloc = ["portable-atomic-util?/alloc"]
30-
std = ["once_cell", "alloc", "portable-atomic-util?/std"]
31-
portable-atomic = ["dep:portable-atomic-util", "once_cell?/portable-atomic"]
30+
std = ["once_cell", "alloc", "portable-atomic?/std", "portable-atomic-util?/std"]
31+
portable-atomic = ["dep:portable-atomic", "dep:portable-atomic-util", "once_cell?/portable-atomic"]
32+
critical-section = ["portable-atomic?/critical-section", "once_cell?/critical-section"]
3233

3334
[badges]
3435
maintenance = { status = "actively-developed" }
3536

3637
[dependencies]
3738
once_cell = { version = "1.13.0", optional = true }
3839
portable-atomic-util = { version = "0.2.4", default-features = false, optional = true }
40+
portable-atomic = { version = "1", default-features = false, optional = true }
3941

4042
[package.metadata.docs.rs]
4143
all-features = true

Diff for: tracing-core/src/callsite.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,15 @@ use core::{
8989
fmt,
9090
hash::{Hash, Hasher},
9191
ptr,
92-
sync::atomic::{AtomicPtr, Ordering},
92+
sync::atomic::Ordering,
9393
};
9494

95+
#[cfg(feature = "portable-atomic")]
96+
use portable_atomic::AtomicPtr;
97+
98+
#[cfg(not(feature = "portable-atomic"))]
99+
use core::sync::atomic::AtomicPtr;
100+
95101
type Callsites = LinkedList;
96102

97103
/// Trait implemented by callsites.

Diff for: tracing-core/src/dispatch.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,13 @@ use crate::{
140140
span, Event, LevelFilter, Metadata,
141141
};
142142

143-
use core::{
144-
any::Any,
145-
fmt,
146-
sync::atomic::{AtomicBool, AtomicUsize, Ordering},
147-
};
143+
use core::{any::Any, fmt, sync::atomic::Ordering};
144+
145+
#[cfg(feature = "portable-atomic")]
146+
use portable_atomic::{AtomicBool, AtomicUsize};
147+
148+
#[cfg(not(feature = "portable-atomic"))]
149+
use core::sync::atomic::{AtomicBool, AtomicUsize};
148150

149151
#[cfg(feature = "std")]
150152
use std::{

Diff for: tracing-core/src/metadata.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
//! Metadata describing trace data.
22
use super::{callsite, field};
3-
use core::{
4-
cmp, fmt,
5-
str::FromStr,
6-
sync::atomic::{AtomicUsize, Ordering},
7-
};
3+
use core::{cmp, fmt, str::FromStr, sync::atomic::Ordering};
4+
5+
#[cfg(feature = "portable-atomic")]
6+
use portable_atomic::AtomicUsize;
7+
8+
#[cfg(not(feature = "portable-atomic"))]
9+
use core::sync::atomic::AtomicUsize;
810

911
/// Metadata describing a [span] or [event].
1012
///

Diff for: tracing-core/src/spin/once.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
use core::cell::UnsafeCell;
22
use core::fmt;
33
use core::hint::spin_loop;
4-
use core::sync::atomic::{AtomicUsize, Ordering};
4+
use core::sync::atomic::Ordering;
5+
6+
#[cfg(feature = "portable-atomic")]
7+
use portable_atomic::AtomicUsize;
8+
9+
#[cfg(not(feature = "portable-atomic"))]
10+
use core::sync::atomic::AtomicUsize;
511

612
/// A synchronization primitive which can be used to run a one-time global
713
/// initialization. Unlike its std equivalent, this is generalized so that the

Diff for: tracing-futures/Cargo.toml

+5-3
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ default = ["std-future", "std"]
2323
futures-01 = ["futures_01", "std"]
2424
futures-03 = ["std-future", "futures", "futures-task", "std"]
2525
std-future = ["pin-project-lite"]
26-
tokio = ["tokio_01"]
26+
tokio = ["tokio_01", "dep:tokio-threadpool", "dep:mio"]
2727
std = ["tracing/std"]
28+
portable-atomic = ["tracing/portable-atomic"]
29+
critical-section = ["tracing/critical-section"]
2830

2931
[dependencies]
3032
futures_01 = { package = "futures", version = "0.1.31", optional = true }
@@ -36,8 +38,8 @@ tokio-executor = { version = "0.1.10", optional = true }
3638
tokio_01 = { package = "tokio", version = "0.1.22", optional = true }
3739

3840
# Fix minimal-versions
39-
tokio-threadpool = "0.1.18"
40-
mio = "0.6.23"
41+
tokio-threadpool = { version = "0.1.18", optional = true }
42+
mio = { version = "0.6.23", optional = true }
4143

4244
[dev-dependencies]
4345
futures = "0.3.21"

Diff for: tracing-macros/Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ license = "MIT"
1818
rust-version = "1.63.0"
1919

2020
[dependencies]
21-
tracing = { path = "../tracing", version = "0.2", default-features = false, features = ["std"] }
21+
tracing = { path = "../tracing", version = "0.2", default-features = false }
2222

2323
[dev-dependencies]
2424
tracing-subscriber = { path = "../tracing-subscriber", version = "0.3" }
25+
tracing = { path = "../tracing", version = "0.2", default-features = false, features = ["std"] }
2526

2627
[badges]
2728
maintenance = { status = "experimental" }

Diff for: tracing-macros/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![no_std]
2+
13
#[doc(hidden)]
24
pub use tracing;
35

Diff for: tracing-serde/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ rust-version = "1.63.0"
2121
[features]
2222
default = ["std"]
2323
std = ["serde/std", "tracing-core/std"]
24+
portable-atomic = ["tracing-core/portable-atomic"]
25+
critical-section = ["tracing-core/critical-section"]
2426

2527
[dependencies]
2628
serde = { version = "1.0.139", default-features = false, features = ["alloc"] }

Diff for: tracing-subscriber/Cargo.toml

+6-1
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ rust-version = "1.63.0"
2525
[features]
2626

2727
default = ["smallvec", "fmt", "ansi", "tracing-log", "std"]
28-
alloc = ["tracing-core/alloc"]
28+
alloc = ["tracing-core/alloc", "portable-atomic-util?/alloc"]
2929
std = ["alloc", "tracing-core/std"]
3030
env-filter = ["matchers", "regex", "once_cell", "tracing", "std", "thread_local"]
3131
fmt = ["registry", "std"]
3232
ansi = ["fmt", "nu-ansi-term"]
3333
registry = ["sharded-slab", "thread_local", "std"]
3434
json = ["tracing-serde", "serde", "serde_json"]
35+
portable-atomic = ["dep:portable-atomic-util", "tracing-core/portable-atomic", "tracing?/portable-atomic", "tracing-serde?/portable-atomic"]
36+
critical-section = ["tracing-core/critical-section", "tracing?/critical-section", "tracing-serde?/critical-section"]
3537

3638
# Enables support for local time when using the `time` crate timestamp
3739
# formatters.
@@ -66,6 +68,9 @@ chrono = { version = "0.4.26", default-features = false, features = ["clock", "s
6668
sharded-slab = { version = "0.1.4", optional = true }
6769
thread_local = { version = "1.1.4", optional = true }
6870

71+
# portable-atomic
72+
portable-atomic-util = { version = "0.2.4", default-features = false, optional = true }
73+
6974
[dev-dependencies]
7075
tracing = { path = "../tracing", version = "0.2" }
7176
tracing-mock = { path = "../tracing-mock", features = ["tracing-subscriber"] }

Diff for: tracing-subscriber/src/registry/mod.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,13 @@ pub struct Scope<'a, R> {
233233
feature! {
234234
#![any(feature = "alloc", feature = "std")]
235235

236-
use alloc::{
237-
boxed::Box,
238-
sync::Arc
239-
};
236+
use alloc::boxed::Box;
237+
238+
#[cfg(feature = "portable-atomic")]
239+
use portable_atomic_util::Arc;
240+
241+
#[cfg(not(feature = "portable-atomic"))]
242+
use alloc::sync::Arc;
240243

241244
#[cfg(not(feature = "smallvec"))]
242245
use alloc::vec::{self, Vec};

Diff for: tracing/Cargo.toml

+4-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ tracing-core = { path = "../tracing-core", version = "0.2", default-features = f
3232
log = { version = "0.4.17", optional = true }
3333
tracing-attributes = { path = "../tracing-attributes", version = "0.2", optional = true }
3434
pin-project-lite = "0.2.9"
35+
portable-atomic = { version = "1", default-features = false, optional = true }
3536

3637
[dev-dependencies]
3738
criterion = { version = "0.3.6", default-features = false }
@@ -60,9 +61,11 @@ release_max_level_debug = []
6061
release_max_level_trace = []
6162

6263
alloc = ["tracing-core/alloc"]
63-
std = ["tracing-core/std", "alloc"]
64+
std = ["tracing-core/std", "alloc", "portable-atomic?/std"]
6465
log-always = ["log"]
6566
attributes = ["tracing-attributes"]
67+
portable-atomic = ["dep:portable-atomic", "tracing-core/portable-atomic"]
68+
critical-section = ["tracing-core/critical-section", "portable-atomic?/critical-section"]
6669

6770
[[bench]]
6871
name = "baseline"

Diff for: tracing/src/lib.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1003,7 +1003,14 @@ pub mod __macro_support {
10031003
pub use crate::callsite::{Callsite, Registration};
10041004
use crate::{collect::Interest, Metadata};
10051005
use core::fmt;
1006-
use core::sync::atomic::{AtomicU8, Ordering};
1006+
use core::sync::atomic::Ordering;
1007+
1008+
#[cfg(feature = "portable-atomic")]
1009+
use portable_atomic::AtomicU8;
1010+
1011+
#[cfg(not(feature = "portable-atomic"))]
1012+
use core::sync::atomic::AtomicU8;
1013+
10071014
// Re-export the `core` functions that are used in macros. This allows
10081015
// a crate to be named `core` and avoid name clashes.
10091016
// See here: https://github.com/tokio-rs/tracing/issues/2761

0 commit comments

Comments
 (0)