-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(util): add cache padding inhibitor (#192)
@jamesmunns' PR #161 added a feature for disabling cache padding in `cordyceps`. however, `mycelium-util` and `maitake` also cache pad some values. this branch adds similar feature flags to those crates. in `mycelium-util`, the flag controls the public `CachePadded` type, which is used both internally and in `maitake`. in `maiktake`, the feature just forwards to both `mycelium-util` and `cordyceps`' feature flags. Signed-off-by: Eliza Weisman <[email protected]>
- Loading branch information
Showing
7 changed files
with
137 additions
and
50 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
|
@@ -4,11 +4,14 @@ version = "0.1.0" | |
authors = ["Eliza Weisman <[email protected]>"] | ||
edition = "2018" | ||
rust-version = "1.61.0" | ||
readme = "README.md" | ||
|
||
# See more keys and their definitions at | ||
# https://doc.rust-lang.org/cargo/reference/manifest.html | ||
[features] | ||
default = [] | ||
alloc = ["cordyceps/alloc"] | ||
no-cache-pad = ["cordyceps/no-cache-pad"] | ||
|
||
[dependencies] | ||
tracing = { git = "https://github.com/tokio-rs/tracing", default_features = false, features = ["attributes"] } | ||
|
This file contains 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,19 @@ | ||
# mycelium-util | ||
|
||
a "standard library for programming in the [mycelium] kernel and related | ||
libraries. | ||
|
||
## features | ||
|
||
The following features are available (this list is incomplete; you can help by [expanding it].) | ||
|
||
[expanding it]: https://github.com/hawkw/mycelium/edit/main/util/README.md | ||
|
||
| Feature | Default | Explanation | | ||
| :--- | :--- | :--- | | ||
| `no-cache-pad` | `false` | Inhibits cache padding for the [`CachePadded`] struct. When this feature is NOT enabled, the size will be determined based on target platform. | | ||
| `alloc` | `false` | Enables [`liballoc`] dependency | | ||
|
||
[mycelium]: https://mycelium.elizas.website | ||
[`CachePadded`]: https://mycelium.elizas.website/mycelium_util/sync/struct.cachepadded | ||
[`liballoc`]: https://doc.rust-lang.org/alloc/ |
This file contains 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 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 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,108 @@ | ||
use core::{ | ||
fmt, | ||
ops::{Deref, DerefMut}, | ||
}; | ||
|
||
pub use self::inner::CachePadded; | ||
|
||
/// When configured not to pad to cache alignment, just provide a no-op wrapper struct | ||
/// This feature is useful for platforms with no data cache, such as many Cortex-M | ||
/// targets. | ||
#[cfg(feature = "no-cache-pad")] | ||
mod inner { | ||
/// Aligns the wrapped value to the size of a cache line. | ||
/// | ||
/// This is used to avoid [false sharing] for values that may be | ||
/// accessed concurrently. | ||
/// | ||
/// # Size/Alignment | ||
/// | ||
/// The size and alignment of this type depends on the target architecture, | ||
/// and on whether or not the `no-cache-pad` feature flag is enabled. | ||
/// | ||
/// When the `no-cache-pad` crate feature flag is enabled, this is simply a | ||
/// no-op wrapper struct. This is intended for use on useful for platforms | ||
/// with no data cache, such as many Cortex-M targets. | ||
/// | ||
/// In other cases, this type is always aligned to the size of a cache line, | ||
/// based on the target architecture. On `x86_64`/`aarch64`, a cache line is | ||
/// 128 bytes. On all other targets, a cache line is assumed to 64 bytes | ||
/// long. This type's size will always be a multiple of the cache line size; | ||
/// if the wrapped type is longer than the alignment of a cache line, then | ||
/// this type will be padded to multiple cache lines. | ||
/// | ||
/// [false sharing]: https://en.wikipedia.org/wiki/False_sharing | ||
#[derive(Clone, Copy, Default, Hash, PartialEq, Eq)] | ||
pub struct CachePadded<T>(pub(super) T); | ||
} | ||
|
||
/// When not inhibited, determine cache alignment based on target architecture. | ||
/// Align to 128 bytes on 64-bit x86/ARM targets, otherwise align to 64 bytes. | ||
#[cfg(not(feature = "no-cache-pad"))] | ||
mod inner { | ||
/// Aligns the wrapped value to the size of a cache line. | ||
/// | ||
/// This is used to avoid [false sharing] for values that may be | ||
/// accessed concurrently. | ||
/// | ||
/// # Size/Alignment | ||
/// | ||
/// The size and alignment of this type depends on the target architecture, | ||
/// and on whether or not the `no-cache-pad` feature flag is enabled. | ||
/// | ||
/// When the `no-cache-pad` crate feature flag is enabled, this is simply a | ||
/// no-op wrapper struct. This is intended for use on useful for platforms | ||
/// with no data cache, such as many Cortex-M targets. | ||
/// | ||
/// In other cases, this type is always aligned to the size of a cache line, | ||
/// based on the target architecture. On `x86_64`/`aarch64`, a cache line is | ||
/// 128 bytes. On all other targets, a cache line is assumed to 64 bytes | ||
/// long. This type's size will always be a multiple of the cache line size; | ||
/// if the wrapped type is longer than the alignment of a cache line, then | ||
/// this type will be padded to multiple cache lines. | ||
/// | ||
/// [false sharing]: https://en.wikipedia.org/wiki/False_sharing | ||
#[cfg_attr(any(target_arch = "x86_64", target_arch = "aarch64"), repr(align(128)))] | ||
#[cfg_attr( | ||
not(any(target_arch = "x86_64", target_arch = "aarch64")), | ||
repr(align(64)) | ||
)] | ||
#[derive(Clone, Copy, Default, Hash, PartialEq, Eq)] | ||
pub struct CachePadded<T>(pub(super) T); | ||
} | ||
|
||
// === impl CachePadded === | ||
|
||
impl<T> CachePadded<T> { | ||
/// Pads `value` to the length of a cache line. | ||
pub const fn new(value: T) -> Self { | ||
Self(value) | ||
} | ||
|
||
/// Unwraps the inner value and returns it. | ||
pub fn into_inner(self) -> T { | ||
self.0 | ||
} | ||
} | ||
|
||
impl<T> Deref for CachePadded<T> { | ||
type Target = T; | ||
|
||
#[inline] | ||
fn deref(&self) -> &T { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl<T> DerefMut for CachePadded<T> { | ||
#[inline] | ||
fn deref_mut(&mut self) -> &mut T { | ||
&mut self.0 | ||
} | ||
} | ||
|
||
impl<T: fmt::Debug> fmt::Debug for CachePadded<T> { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
self.0.fmt(f) | ||
} | ||
} |