Skip to content

Fix malloc_size_of for mac #93

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

Merged
merged 44 commits into from
May 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
25270be
Mem tools by including trait and specific implementation in the crate
cheme Dec 6, 2018
94bb293
Plug malloc_size_of as a patched copy. Documentation is done by
cheme Dec 7, 2018
197a27d
A few additional impl for parity-eth use
cheme Dec 7, 2018
2a0cc08
Allocators.
cheme Dec 7, 2018
418e03b
heapsize estimation for allocator without use_size fn.
cheme Dec 10, 2018
6105606
Init no_std compat.
cheme Dec 10, 2018
59b4d55
Remove conditianal test code.
cheme Dec 10, 2018
efadc31
Renaming of mem repo to crate name.
cheme Dec 11, 2018
b2f1708
Remove conditional mettering for now (will need some testing to check if
cheme Dec 11, 2018
fb127a7
tab
cheme Dec 11, 2018
d5aa899
Remove features get form malloc_size and simply delete code instead.
cheme Jan 2, 2019
fd9dc92
Update parity-util-mem/README.md
pepyakin Jan 2, 2019
3df427b
Missing windows import
cheme Jan 3, 2019
da88f97
Merge branch 'mem-tools2' of github.com:cheme/parity-common into mem-…
cheme Jan 3, 2019
ef9a5b6
Use self for winapi.
cheme Jan 3, 2019
1b0b2e3
Correct import.
cheme Jan 3, 2019
5340912
shallow size on Arc with malloc return -1 (max val), replacing with
cheme Jan 3, 2019
8ada814
use cfg_if to makes feature choice more explicit.
cheme Jan 7, 2019
61c60b0
Remove servo specific impls (unused).
cheme Jan 9, 2019
767dc90
`estimate_heapsize` in travis.
cheme Jan 9, 2019
9876b65
Merge branch 'master' into mem-tools2
cheme Jan 9, 2019
a8a9b0e
Merge branch 'master' into mem-tools2
cheme Jan 9, 2019
a31851e
Remove last serde and remove servo_arc reference.
cheme Jan 10, 2019
73e8c7e
Add missing version
cheme Jan 10, 2019
64135ac
Use malloc_size_of_derive from crates.
cheme Jan 12, 2019
0437f7e
Rem heapsize constraint and deprecate impl.
cheme Jan 14, 2019
81b2da1
Remove path reference
cheme Jan 14, 2019
6c4a5fd
Remove path for hashdb to avoid trait conflicts.
cheme Jan 14, 2019
99b8ea4
Merge branch 'master' into deprecate_heapsize
cheme Jan 16, 2019
dedef64
Merge branch 'master' into deprecate_heapsize
cheme Jan 22, 2019
a9ca6e1
Restore path in cargo
cheme Jan 22, 2019
c315173
Missing path
cheme Jan 22, 2019
71f7aa8
Added test
cheme Jan 22, 2019
b7bcbc8
fix test
cheme Jan 22, 2019
5e53bf3
Do not use prefixed on mac by default (same with other platform as we
cheme Jan 22, 2019
b667436
Have macos rely upon estimate if default allocator
cheme Jan 22, 2019
85a763e
Need full test for macos without jemalloc
cheme Jan 22, 2019
3682270
String in internal heap measured impl
cheme Jan 22, 2019
566389e
Exclude jmalloc-sys on windows build (ci related)
cheme Jan 22, 2019
08f9ed1
Proper windows checks in cargo.toml
cheme Jan 23, 2019
1856597
Removing 'prefixed_malloc' from condition.
cheme Jan 23, 2019
df84088
arc only for jemalloc-global, escape for windows ci
cheme Jan 23, 2019
233bd99
Merge branch 'master' into deprecate_heapsize
cheme Feb 22, 2019
1a174e3
Merge branch 'master' into deprecate_heapsize
cheme May 17, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ script:
- cd uint/ && cargo test --features=std,quickcheck --release && cd ..
- cd plain_hasher/ && cargo test --no-default-features && cd ..
- cd parity-util-mem/ && cargo test --features=estimate-heapsize && cd ..
- cd parity-util-mem/ && cargo test --features=jemalloc-global && cd ..
7 changes: 5 additions & 2 deletions parity-util-mem/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@ cfg-if = "0.1.6"
malloc_size_of_derive = "0.1.0"
dlmalloc = { version = "0.1", features = ["global"], optional = true }
wee_alloc = { version = "0.4", optional = true }
jemallocator = { version = "0.1", optional = true }

elastic-array = { version = "*", optional = true }
ethereum-types = { version = "*", optional = true }
parking_lot = { version = "*", optional = true }

[target."cfg(windows)".dependencies.winapi]
[target.'cfg(target_os = "windows")'.dependencies.winapi]
version = "0.3.4"

[target.'cfg(not(target_os = "windows"))'.dependencies.jemallocator]
version = "0.1"
optional = true

[features]
default = ["std", "ethereum-impls"]
std = []
Expand Down
150 changes: 90 additions & 60 deletions parity-util-mem/slim_malloc_size_of.patch
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
diff --git a/components/malloc_size_of/lib.rs b/components/malloc_size_of/lib.rs
index 778082b5f0..7f527c930e 100644
index 778082b5f0..e13745d6af 100644
--- a/components/malloc_size_of/lib.rs
+++ b/components/malloc_size_of/lib.rs
@@ -43,55 +43,39 @@
Expand Down Expand Up @@ -82,23 +82,67 @@ index 778082b5f0..7f527c930e 100644

/// Operations used when measuring heap usage of data structures.
pub struct MallocSizeOfOps {
@@ -216,6 +200,7 @@ pub trait MallocConditionalShallowSizeOf {
@@ -216,44 +200,62 @@ pub trait MallocConditionalShallowSizeOf {
fn conditional_shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize;
}

+#[cfg(not(feature = "estimate-heapsize"))]
impl MallocSizeOf for String {
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
unsafe { ops.malloc_size_of(self.as_ptr()) }
@@ -229,6 +214,7 @@ impl<'a, T: ?Sized> MallocSizeOf for &'a T {
-impl MallocSizeOf for String {
- fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
- unsafe { ops.malloc_size_of(self.as_ptr()) }
+#[cfg(not(any(
+ all(
+ target_os = "macos",
+ not(feature = "jemalloc-global"),
+ ),
+ feature = "estimate-heapsize"
+)))]
+pub mod inner_allocator_use {
+
+use super::*;
+
+impl<T: ?Sized> MallocShallowSizeOf for Box<T> {
+ fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
+ unsafe { ops.malloc_size_of(&**self) }
}
}

-impl<'a, T: ?Sized> MallocSizeOf for &'a T {
- fn size_of(&self, _ops: &mut MallocSizeOfOps) -> usize {
- // Zero makes sense for a non-owning reference.
- 0
+impl<T> MallocShallowSizeOf for Vec<T> {
+ fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
+ unsafe { ops.malloc_size_of(self.as_ptr()) }
}
}

-impl<T: ?Sized> MallocShallowSizeOf for Box<T> {
- fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
- unsafe { ops.malloc_size_of(&**self) }
+// currently this seems only fine with jemalloc
+#[cfg(feature = "std")]
+#[cfg(any(target_os = "macos", target_os = "ios", target_os = "android", feature = "jemalloc-global"))]
+impl<T> MallocUnconditionalShallowSizeOf for Arc<T> {
+ fn unconditional_shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
+ unsafe { ops.malloc_size_of(arc_ptr(self)) }
}
}

+#[cfg(not(feature = "estimate-heapsize"))]
impl<T: ?Sized> MallocShallowSizeOf for Box<T> {
fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
unsafe { ops.malloc_size_of(&**self) }
@@ -241,24 +227,6 @@ impl<T: MallocSizeOf + ?Sized> MallocSizeOf for Box<T> {
-impl<T: MallocSizeOf + ?Sized> MallocSizeOf for Box<T> {
+#[cfg(feature = "std")]
+#[cfg(not(any(target_os = "macos", target_os = "ios", target_os = "android", feature = "jemalloc-global")))]
+impl<T> MallocUnconditionalShallowSizeOf for Arc<T> {
+ fn unconditional_shallow_size_of(&self, _ops: &mut MallocSizeOfOps) -> usize {
+ size_of::<T>()
+ }
+}
+
+}
+
+impl MallocSizeOf for String {
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
- self.shallow_size_of(ops) + (**self).size_of(ops)
+ unsafe { ops.malloc_size_of(self.as_ptr()) }
}
}

Expand All @@ -111,27 +155,27 @@ index 778082b5f0..7f527c930e 100644
- n += ops.malloc_size_of(&**self);
- }
- n
- }
-}
-
+impl<'a, T: ?Sized> MallocSizeOf for &'a T {
+ fn size_of(&self, _ops: &mut MallocSizeOfOps) -> usize {
+ // Zero makes sense for a non-owning reference.
+ 0
}
}

-impl<T: MallocSizeOf> MallocSizeOf for thin_slice::ThinBoxedSlice<T> {
- fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
- self.shallow_size_of(ops) + (**self).size_of(ops)
- }
-}
-
impl MallocSizeOf for () {
fn size_of(&self, _ops: &mut MallocSizeOfOps) -> usize {
0
@@ -329,6 +297,7 @@ impl<T: MallocSizeOf> MallocSizeOf for std::cell::RefCell<T> {
+impl<T: MallocSizeOf + ?Sized> MallocSizeOf for Box<T> {
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
self.shallow_size_of(ops) + (**self).size_of(ops)
}
@@ -329,6 +331,7 @@ impl<T: MallocSizeOf> MallocSizeOf for std::cell::RefCell<T> {
}
}

+#[cfg(feature = "std")]
impl<'a, B: ?Sized + ToOwned> MallocSizeOf for std::borrow::Cow<'a, B>
where
B::Owned: MallocSizeOf,
@@ -351,24 +320,7 @@ impl<T: MallocSizeOf> MallocSizeOf for [T] {
@@ -351,30 +354,6 @@ impl<T: MallocSizeOf> MallocSizeOf for [T] {
}
}

Expand All @@ -153,11 +197,16 @@ index 778082b5f0..7f527c930e 100644
- }
-}
-
+#[cfg(not(feature = "estimate-heapsize"))]
impl<T> MallocShallowSizeOf for Vec<T> {
fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
unsafe { ops.malloc_size_of(self.as_ptr()) }
@@ -412,30 +364,7 @@ impl<T: MallocSizeOf> MallocSizeOf for std::collections::VecDeque<T> {
-impl<T> MallocShallowSizeOf for Vec<T> {
- fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
- unsafe { ops.malloc_size_of(self.as_ptr()) }
- }
-}
-
impl<T: MallocSizeOf> MallocSizeOf for Vec<T> {
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
let mut n = self.shallow_size_of(ops);
@@ -412,30 +391,7 @@ impl<T: MallocSizeOf> MallocSizeOf for std::collections::VecDeque<T> {
}
}

Expand Down Expand Up @@ -189,15 +238,15 @@ index 778082b5f0..7f527c930e 100644
impl<T, S> MallocShallowSizeOf for std::collections::HashSet<T, S>
where
T: Eq + Hash,
@@ -457,6 +386,7 @@ where
@@ -457,6 +413,7 @@ where
}
}

+#[cfg(feature = "std")]
impl<T, S> MallocSizeOf for std::collections::HashSet<T, S>
where
T: Eq + Hash + MallocSizeOf,
@@ -471,59 +401,7 @@ where
@@ -471,59 +428,7 @@ where
}
}

Expand Down Expand Up @@ -258,15 +307,15 @@ index 778082b5f0..7f527c930e 100644
impl<K, V, S> MallocShallowSizeOf for std::collections::HashMap<K, V, S>
where
K: Eq + Hash,
@@ -541,6 +419,7 @@ where
@@ -541,6 +446,7 @@ where
}
}

+#[cfg(feature = "std")]
impl<K, V, S> MallocSizeOf for std::collections::HashMap<K, V, S>
where
K: Eq + Hash + MallocSizeOf,
@@ -587,62 +466,6 @@ where
@@ -587,62 +493,6 @@ where
}
}

Expand Down Expand Up @@ -329,40 +378,21 @@ index 778082b5f0..7f527c930e 100644
// PhantomData is always 0.
impl<T> MallocSizeOf for std::marker::PhantomData<T> {
fn size_of(&self, _ops: &mut MallocSizeOfOps) -> usize {
@@ -657,21 +480,43 @@ impl<T> MallocSizeOf for std::marker::PhantomData<T> {
@@ -657,21 +507,22 @@ impl<T> MallocSizeOf for std::marker::PhantomData<T> {
//impl<T> !MallocSizeOf for Arc<T> { }
//impl<T> !MallocShallowSizeOf for Arc<T> { }

-impl<T> MallocUnconditionalShallowSizeOf for servo_arc::Arc<T> {
- fn unconditional_shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
- unsafe { ops.malloc_size_of(self.heap_ptr()) }
- }
+#[cfg(feature = "std")]
+fn arc_ptr<T>(s: &Arc<T>) -> * const T {
+ &(**s) as *const T
+}
+
+
+// currently this seems only fine with jemalloc
+#[cfg(feature = "std")]
+#[cfg(not(feature = "estimate-heapsize"))]
+#[cfg(any(prefixed_jemalloc, target_os = "macos", target_os = "ios", target_os = "android", feature = "jemalloc-global"))]
+impl<T> MallocUnconditionalShallowSizeOf for Arc<T> {
fn unconditional_shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
- unsafe { ops.malloc_size_of(self.heap_ptr()) }
+ unsafe { ops.malloc_size_of(arc_ptr(self)) }
}
}

-impl<T: MallocSizeOf> MallocUnconditionalSizeOf for servo_arc::Arc<T> {
+#[cfg(feature = "std")]
+#[cfg(not(feature = "estimate-heapsize"))]
+#[cfg(not(any(prefixed_jemalloc, target_os = "macos", target_os = "ios", target_os = "android", feature = "jemalloc-global")))]
+impl<T> MallocUnconditionalShallowSizeOf for Arc<T> {
+ fn unconditional_shallow_size_of(&self, _ops: &mut MallocSizeOfOps) -> usize {
+ size_of::<T>()
+ }
+}
+
+
+#[cfg(feature = "std")]
+impl<T: MallocSizeOf> MallocUnconditionalSizeOf for Arc<T> {
fn unconditional_size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
self.unconditional_shallow_size_of(ops) + (**self).size_of(ops)
Expand All @@ -378,7 +408,7 @@ index 778082b5f0..7f527c930e 100644
0
} else {
self.unconditional_shallow_size_of(ops)
@@ -679,9 +524,10 @@ impl<T> MallocConditionalShallowSizeOf for servo_arc::Arc<T> {
@@ -679,9 +530,10 @@ impl<T> MallocConditionalShallowSizeOf for servo_arc::Arc<T> {
}
}

Expand All @@ -391,7 +421,7 @@ index 778082b5f0..7f527c930e 100644
0
} else {
self.unconditional_size_of(ops)
@@ -695,203 +541,13 @@ impl<T: MallocSizeOf> MallocConditionalSizeOf for servo_arc::Arc<T> {
@@ -695,203 +547,13 @@ impl<T: MallocSizeOf> MallocConditionalSizeOf for servo_arc::Arc<T> {
/// If a mutex is stored inside of an Arc value as a member of a data type that is being measured,
/// the Arc will not be automatically measured so there is no risk of overcounting the mutex's
/// contents.
Expand Down Expand Up @@ -596,7 +626,7 @@ index 778082b5f0..7f527c930e 100644
#[macro_export]
macro_rules! malloc_size_of_is_0(
($($ty:ty),+) => (
@@ -929,117 +585,6 @@ malloc_size_of_is_0!(Range<u8>, Range<u16>, Range<u32>, Range<u64>, Range<usize>
@@ -929,117 +591,6 @@ malloc_size_of_is_0!(Range<u8>, Range<u16>, Range<u32>, Range<u64>, Range<usize>
malloc_size_of_is_0!(Range<i8>, Range<i16>, Range<i32>, Range<i64>, Range<isize>);
malloc_size_of_is_0!(Range<f32>, Range<f64>);

Expand Down
16 changes: 12 additions & 4 deletions parity-util-mem/src/allocators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
//! - jemalloc: use jemallocator crate
//! - weealloc: default to `estimate_size`
//! - dlmalloc: default to `estimate_size`
//! - arch x86/macos:
//! - no features: use default alloc, requires using `estimate_size`
//! - jemalloc: use jemallocator crate
//! - weealloc: default to `estimate_size`
//! - dlmalloc: default to `estimate_size`
//! - arch wasm32:
//! - no features: default to `estimate_size`
//! - weealloc: default to `estimate_size`
Expand Down Expand Up @@ -92,16 +97,19 @@ cfg_if! {
jemallocator::usable_size(ptr)
}

} else {
} else if #[cfg(target_os = "linux")] {

// default allocator used
/// Macos, ios and android calls jemalloc.
/// Linux call system allocator (currently malloc).
extern "C" {
#[cfg_attr(any(prefixed_jemalloc, target_os = "macos", target_os = "ios", target_os = "android"), link_name = "je_malloc_usable_size")]
pub fn malloc_usable_size(ptr: *const c_void) -> usize;
}

} else {
// default allocator for non linux or windows system use estimate
pub unsafe extern "C" fn malloc_usable_size(_ptr: *const c_void) -> usize {
unreachable!("estimate heapsize or feature allocator needed")
}

}

}
Expand Down
13 changes: 9 additions & 4 deletions parity-util-mem/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#![cfg_attr(not(feature = "std"), feature(core_intrinsics))]
#![cfg_attr(not(feature = "std"), feature(alloc))]


#[macro_use]
extern crate cfg_if;

Expand All @@ -43,7 +44,6 @@ pub use cod::clear::Clear;

cfg_if! {
if #[cfg(all(
feature = "jemalloc-global",
feature = "jemalloc-global",
not(target_os = "windows"),
not(target_arch = "wasm32")
Expand All @@ -69,7 +69,13 @@ cfg_if! {

pub mod allocators;

#[cfg(feature = "estimate-heapsize")]
#[cfg(any(
all(
target_os = "macos",
not(feature = "jemalloc-global"),
),
feature = "estimate-heapsize"
))]
pub mod sizeof;

#[cfg(not(feature = "std"))]
Expand Down Expand Up @@ -141,7 +147,7 @@ impl<T: AsMut<[u8]>> DerefMut for Memzero<T> {
}
}

#[cfg(std)]
#[cfg(feature = "std")]
#[cfg(test)]
mod test {
use std::sync::Arc;
Expand All @@ -153,5 +159,4 @@ mod test {
let s = val.malloc_size_of();
assert!(s > 0);
}

}
Loading