parity_util_mem/
lib.rs

1// Copyright 2020 Parity Technologies
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9//! Crate for parity memory management related utilities.
10//! It includes global allocator choice, heap measurement and
11//! memory erasure.
12
13#![cfg_attr(not(feature = "std"), no_std)]
14
15#[cfg(not(feature = "std"))]
16extern crate alloc;
17
18cfg_if::cfg_if! {
19	if #[cfg(all(
20		feature = "jemalloc-global",
21		not(target_os = "windows"),
22		not(target_arch = "wasm32")
23	))] {
24		/// Global allocator
25		#[global_allocator]
26		pub static ALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
27
28		mod memory_stats_jemalloc;
29		use memory_stats_jemalloc as memory_stats;
30	} else if #[cfg(feature = "dlmalloc-global")] {
31		/// Global allocator
32		#[global_allocator]
33		pub static ALLOC: dlmalloc::GlobalDlmalloc = dlmalloc::GlobalDlmalloc;
34
35		mod memory_stats_noop;
36		use memory_stats_noop as memory_stats;
37	} else if #[cfg(all(
38			feature = "mimalloc-global",
39			not(target_arch = "wasm32")
40		))] {
41		/// Global allocator
42		#[global_allocator]
43		pub static ALLOC: mimalloc::MiMalloc = mimalloc::MiMalloc;
44
45		mod memory_stats_noop;
46		use memory_stats_noop as memory_stats;
47	} else {
48		// default allocator used
49		mod memory_stats_noop;
50		use memory_stats_noop as memory_stats;
51	}
52}
53
54pub mod allocators;
55
56#[cfg(any(
57	all(any(target_os = "macos", target_os = "ios"), not(feature = "jemalloc-global"),),
58	feature = "estimate-heapsize"
59))]
60pub mod sizeof;
61
62/// This is a copy of patched crate `malloc_size_of` as a module.
63/// We need to have it as an inner module to be able to define our own traits implementation,
64/// if at some point the trait become standard enough we could use the right way of doing it
65/// by implementing it in our type traits crates. At this time moving this trait to the primitive
66/// types level would impact too much of the dependencies to be easily manageable.
67#[macro_use]
68mod malloc_size;
69
70#[cfg(feature = "ethereum-impls")]
71pub mod ethereum_impls;
72
73#[cfg(feature = "primitive-types")]
74pub mod primitives_impls;
75
76pub use allocators::MallocSizeOfExt;
77pub use malloc_size::{MallocShallowSizeOf, MallocSizeOf, MallocSizeOfOps};
78
79pub use parity_util_mem_derive::*;
80
81/// Heap size of structure.
82///
83/// Structure can be anything that implements MallocSizeOf.
84pub fn malloc_size<T: MallocSizeOf + ?Sized>(t: &T) -> usize {
85	MallocSizeOf::size_of(t, &mut allocators::new_malloc_size_ops())
86}
87
88/// An error related to the memory stats gathering.
89#[derive(Clone, Debug)]
90pub struct MemoryStatsError(memory_stats::Error);
91
92#[cfg(feature = "std")]
93impl std::fmt::Display for MemoryStatsError {
94	fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
95		self.0.fmt(fmt)
96	}
97}
98
99#[cfg(feature = "std")]
100impl std::error::Error for MemoryStatsError {}
101
102/// Snapshot of collected memory metrics.
103#[non_exhaustive]
104#[derive(Debug, Clone)]
105pub struct MemoryAllocationSnapshot {
106	/// Total resident memory, in bytes.
107	pub resident: u64,
108	/// Total allocated memory, in bytes.
109	pub allocated: u64,
110}
111
112/// Accessor to the allocator internals.
113#[derive(Clone)]
114pub struct MemoryAllocationTracker(self::memory_stats::MemoryAllocationTracker);
115
116impl MemoryAllocationTracker {
117	/// Create an instance of an allocation tracker.
118	pub fn new() -> Result<Self, MemoryStatsError> {
119		self::memory_stats::MemoryAllocationTracker::new()
120			.map(MemoryAllocationTracker)
121			.map_err(MemoryStatsError)
122	}
123
124	/// Create an allocation snapshot.
125	pub fn snapshot(&self) -> Result<MemoryAllocationSnapshot, MemoryStatsError> {
126		self.0.snapshot().map_err(MemoryStatsError)
127	}
128}
129
130#[cfg(feature = "std")]
131#[cfg(test)]
132mod test {
133	use super::{malloc_size, MallocSizeOf, MallocSizeOfExt};
134	use std::sync::Arc;
135
136	#[test]
137	fn test_arc() {
138		let val = Arc::new("test".to_string());
139		let s = val.malloc_size_of();
140		assert!(s > 0);
141	}
142
143	#[test]
144	fn test_dyn() {
145		trait Augmented: MallocSizeOf {}
146		impl Augmented for Vec<u8> {}
147		let val: Arc<dyn Augmented> = Arc::new(vec![0u8; 1024]);
148		assert!(malloc_size(&*val) > 1000);
149	}
150}