Skip to content

Commit fab3006

Browse files
jiacai2050ShiKaiWi
andauthored
refactor: use encoded_size as memory usage (#1213)
## Rationale In one of our cluster, we find the cost of calculate memory usage cost too much CPU, and it's rwlock contention to be blamed. <img width="1089" alt="image" src="https://github.com/CeresDB/ceresdb/assets/3848910/a9b77fdf-92b7-4fa9-9a35-7cfa15a4dfee"> ## Detailed Changes - Use atomic usize to record written row size. ## Test Plan --------- Co-authored-by: WEI Xikai <ShiKaiWi@users.noreply.github.com>
1 parent bd8c970 commit fab3006

File tree

4 files changed

+27
-14
lines changed

4 files changed

+27
-14
lines changed

analytic_engine/src/memtable/skiplist/mod.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@
1717
pub mod factory;
1818
pub mod iter;
1919

20-
use std::{
21-
convert::TryInto,
22-
sync::atomic::{self, AtomicU64, AtomicUsize},
23-
};
20+
use std::sync::atomic::{self, AtomicU64, AtomicUsize};
2421

2522
use arena::{Arena, BasicStats};
2623
use bytes_ext::Bytes;
@@ -153,12 +150,14 @@ impl<A: Arena<Stats = BasicStats> + Clone + Sync + Send + 'static> MemTable
153150
}
154151

155152
fn approximate_memory_usage(&self) -> usize {
156-
// Mem size of skiplist is u32, need to cast to usize
157-
match self.skiplist.mem_size().try_into() {
158-
Ok(v) => v,
159-
// The skiplist already use bytes larger than usize
160-
Err(_) => usize::MAX,
161-
}
153+
let encoded_size = self
154+
.metrics
155+
.row_encoded_size
156+
.load(atomic::Ordering::Relaxed);
157+
let arena_block_size = self.skiplist.arena_block_size();
158+
159+
// Ceil to block_size
160+
(encoded_size + arena_block_size - 1) / arena_block_size * arena_block_size
162161
}
163162

164163
fn set_last_sequence(&self, sequence: SequenceNumber) -> Result<()> {

components/arena/src/arena_trait.rs

+3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ pub trait Arena {
3636
/// Get arena's statistics.
3737
fn stats(&self) -> Self::Stats;
3838

39+
/// Get arena's block size.
40+
fn block_size(&self) -> usize;
41+
3942
// provided methods
4043

4144
/// Allocate required memory. Panic if failed.

components/arena/src/mono_inc.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,24 @@ const DEFAULT_ALIGN: usize = 8;
3838
#[derive(Clone)]
3939
pub struct MonoIncArena {
4040
core: Arc<RwLock<ArenaCore>>,
41+
block_size: usize,
4142
}
4243

4344
impl MonoIncArena {
44-
pub fn new(regular_block_size: usize) -> Self {
45+
pub fn new(block_size: usize) -> Self {
4546
Self {
4647
core: Arc::new(RwLock::new(ArenaCore::new(
47-
regular_block_size,
48+
block_size,
4849
Arc::new(NoopCollector {}),
4950
))),
51+
block_size,
5052
}
5153
}
5254

53-
pub fn with_collector(regular_block_size: usize, collector: CollectorRef) -> Self {
55+
pub fn with_collector(block_size: usize, collector: CollectorRef) -> Self {
5456
Self {
55-
core: Arc::new(RwLock::new(ArenaCore::new(regular_block_size, collector))),
57+
core: Arc::new(RwLock::new(ArenaCore::new(block_size, collector))),
58+
block_size,
5659
}
5760
}
5861
}
@@ -71,6 +74,10 @@ impl Arena for MonoIncArena {
7174
fn alloc(&self, layout: Layout) -> NonNull<u8> {
7275
self.core.write().unwrap().alloc(layout)
7376
}
77+
78+
fn block_size(&self) -> usize {
79+
self.block_size
80+
}
7481
}
7582

7683
struct ArenaCore {

components/skiplist/src/list.rs

+4
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,10 @@ impl<C, A: Arena<Stats = BasicStats> + Clone> Skiplist<C, A> {
252252
fn height(&self) -> usize {
253253
self.core.height.load(Ordering::SeqCst)
254254
}
255+
256+
pub fn arena_block_size(&self) -> usize {
257+
self.core.arena.block_size()
258+
}
255259
}
256260

257261
impl<C: KeyComparator, A: Arena<Stats = BasicStats> + Clone> Skiplist<C, A> {

0 commit comments

Comments
 (0)