Skip to content

Commit f21f0a5

Browse files
add metric
Signed-off-by: Little-Wallace <[email protected]>
1 parent 28803f4 commit f21f0a5

File tree

5 files changed

+49
-31
lines changed

5 files changed

+49
-31
lines changed

src/engine.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ where
214214
let stats = self.global_stats.clone();
215215
return Box::pin(async move {
216216
let (file_num, offset, tracker) = r.await?;
217+
// cacluate memtable cost
218+
let t1 = now.elapsed().as_micros();
217219
if file_num > 0 {
218220
for mut item in items {
219221
if let LogItemContent::Entries(entries) = &mut item.content {
@@ -228,8 +230,8 @@ where
228230
);
229231
}
230232
}
231-
let t = now.elapsed().as_micros();
232-
stats.add_write_duration_change(t as usize);
233+
let t2 = now.elapsed().as_micros();
234+
stats.add_write_duration_change((t2 - t1) as usize, t2 as usize);
233235
Ok(bytes)
234236
});
235237
}
@@ -573,18 +575,27 @@ where
573575
}
574576
let write_count = self.global_stats.write_count.load(Ordering::Relaxed);
575577
let write_cost = self.global_stats.write_cost.load(Ordering::Relaxed);
578+
let mem_cost = self.global_stats.mem_cost.load(Ordering::Relaxed);
576579
let max_write_cost = self.global_stats.max_write_cost.load(Ordering::Relaxed);
580+
let max_mem_cost = self.global_stats.max_mem_cost.load(Ordering::Relaxed);
577581
self.global_stats
578582
.write_count
579583
.fetch_sub(write_count, Ordering::Relaxed);
580584
self.global_stats
581585
.write_cost
582586
.fetch_sub(write_cost, Ordering::Relaxed);
587+
self.global_stats
588+
.mem_cost
589+
.fetch_sub(mem_cost, Ordering::Relaxed);
583590
return Box::pin(async move {
584591
let mut stats = r.await?;
585592
// transfer micro to sec
586-
stats.avg_write_cost = write_cost / write_count;
593+
if write_count > 0 {
594+
stats.avg_write_cost = write_cost / write_count;
595+
stats.avg_mem_cost = mem_cost / write_count;
596+
}
587597
stats.max_write_cost = max_write_cost;
598+
stats.max_mem_cost = max_mem_cost;
588599
Ok(stats)
589600
});
590601
}

src/lib.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ pub struct GlobalStats {
5555
write_count: AtomicUsize,
5656
write_cost: AtomicUsize,
5757
max_write_cost: AtomicUsize,
58+
mem_cost: AtomicUsize,
59+
max_mem_cost: AtomicUsize,
5860
}
5961

6062
impl GlobalStats {
@@ -70,11 +72,17 @@ impl GlobalStats {
7072
pub fn add_cache_miss(&self, count: usize) {
7173
self.cache_miss.fetch_add(count, Ordering::Relaxed);
7274
}
73-
pub fn add_write_duration_change(&self, dur: usize) {
75+
pub fn add_write_duration_change(&self, memtable_duration: usize, write_duration: usize) {
7476
self.write_count.fetch_add(1, Ordering::Relaxed);
75-
self.write_cost.fetch_add(dur, Ordering::Relaxed);
76-
if dur > self.max_write_cost.load(Ordering::Relaxed) {
77-
self.max_write_cost.store(dur, Ordering::Relaxed);
77+
self.write_cost.fetch_add(write_duration, Ordering::Relaxed);
78+
self.mem_cost
79+
.fetch_add(memtable_duration, Ordering::Relaxed);
80+
if write_duration > self.max_write_cost.load(Ordering::Relaxed) {
81+
self.max_write_cost.store(write_duration, Ordering::Relaxed);
82+
}
83+
if memtable_duration > self.max_mem_cost.load(Ordering::Relaxed) {
84+
self.max_write_cost
85+
.store(memtable_duration, Ordering::Relaxed);
7886
}
7987
}
8088

src/log_batch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ mod tests {
673673
entries
674674
.encode_to::<Entry>(&mut encoded, &mut entries_size1)
675675
.unwrap();
676-
for idx in entries.entries_index.borrow_mut().iter_mut() {
676+
for idx in entries.entries_index.iter_mut() {
677677
idx.file_num = file_num;
678678
}
679679
let (mut s, mut entries_size2) = (encoded.as_slice(), 0);

src/util.rs

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -324,12 +324,14 @@ impl<T: Clone> Drop for Worker<T> {
324324

325325
#[derive(Clone, Debug, Copy, PartialEq, Default)]
326326
pub struct Statistic {
327-
pub wal_cost: usize,
328-
pub sync_cost: usize,
327+
pub avg_wal_cost: usize,
328+
pub avg_sync_cost: usize,
329329
pub avg_write_cost: usize,
330+
pub avg_mem_cost: usize,
330331
pub max_wal_cost: usize,
331332
pub max_sync_cost: usize,
332333
pub max_write_cost: usize,
334+
pub max_mem_cost: usize,
333335
pub freq: usize,
334336
}
335337

@@ -342,18 +344,9 @@ fn max(left: usize, right: usize) -> usize {
342344
}
343345

344346
impl Statistic {
345-
pub fn add(&mut self, other: &Self) {
346-
self.wal_cost += other.wal_cost;
347-
self.sync_cost += other.sync_cost;
348-
self.freq += other.freq;
349-
self.max_wal_cost = max(self.max_wal_cost, other.max_wal_cost);
350-
self.max_write_cost = max(self.max_write_cost, other.max_write_cost);
351-
self.max_sync_cost = max(self.max_sync_cost, other.max_sync_cost);
352-
}
353-
354347
pub fn clear(&mut self) {
355-
self.wal_cost = 0;
356-
self.sync_cost = 0;
348+
self.avg_wal_cost = 0;
349+
self.avg_sync_cost = 0;
357350
self.avg_write_cost = 0;
358351
self.max_wal_cost = 0;
359352
self.max_sync_cost = 0;
@@ -363,18 +356,20 @@ impl Statistic {
363356

364357
#[inline]
365358
pub fn add_wal(&mut self, wal: usize) {
366-
self.wal_cost += wal;
359+
self.avg_wal_cost += wal;
367360
self.max_wal_cost = max(self.max_wal_cost, wal);
368361
}
369362

370363
#[inline]
371364
pub fn add_sync(&mut self, sync: usize) {
372-
self.sync_cost += sync;
365+
self.avg_sync_cost += sync;
373366
self.max_sync_cost = max(self.max_sync_cost, sync);
374367
}
375368

376-
#[inline]
377-
pub fn add_one(&mut self) {
378-
self.freq += 1;
369+
pub fn divide(&mut self) {
370+
if self.freq > 0 {
371+
self.avg_wal_cost /= self.freq;
372+
self.avg_sync_cost /= self.freq;
373+
}
379374
}
380375
}

src/wal.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ where
5858
return Ok(());
5959
}
6060
LogMsg::Metric(cb) => {
61-
let _ = cb.send(self.statistic.clone());
62-
self.statistic.clear();
61+
self.report(cb);
6362
continue;
6463
}
6564
};
@@ -79,8 +78,7 @@ where
7978
return Ok(());
8079
}
8180
LogMsg::Metric(cb) => {
82-
let _ = cb.send(self.statistic.clone());
83-
self.statistic.clear();
81+
self.report(cb);
8482
continue;
8583
}
8684
};
@@ -118,12 +116,18 @@ where
118116
self.statistic.add_wal(wal_cost as usize);
119117
self.statistic
120118
.add_sync((wal_cost - before_sync_cost) as usize);
121-
self.statistic.add_one();
119+
self.statistic.freq += 1;
122120
for (offset, sender) in write_ret.drain(..) {
123121
let _ = sender.send((file_num, base_offset + offset, tracker.clone()));
124122
}
125123
write_buffer.clear();
126124
}
127125
Ok(())
128126
}
127+
128+
pub fn report(&mut self, cb: Sender<Statistic>) {
129+
self.statistic.divide();
130+
let _ = cb.send(self.statistic.clone());
131+
self.statistic.clear();
132+
}
129133
}

0 commit comments

Comments
 (0)