From c1d996af9b4c60192627a7ea77a181980d0f8b81 Mon Sep 17 00:00:00 2001 From: Little-Wallace Date: Thu, 22 Oct 2020 18:04:34 +0800 Subject: [PATCH] fix freq Signed-off-by: Little-Wallace --- src/engine.rs | 4 +++- src/log_batch.rs | 2 +- src/util.rs | 27 +++++++++------------------ src/wal.rs | 14 +++++++++----- 4 files changed, 22 insertions(+), 25 deletions(-) diff --git a/src/engine.rs b/src/engine.rs index 31b502b5..294934b1 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -583,7 +583,9 @@ where return Box::pin(async move { let mut stats = r.await?; // transfer micro to sec - stats.avg_write_cost = write_cost / write_count; + if write_count > 0 { + stats.avg_write_cost = write_cost / write_count; + } stats.max_write_cost = max_write_cost; Ok(stats) }); diff --git a/src/log_batch.rs b/src/log_batch.rs index 64e045fe..a16d695a 100644 --- a/src/log_batch.rs +++ b/src/log_batch.rs @@ -673,7 +673,7 @@ mod tests { entries .encode_to::(&mut encoded, &mut entries_size1) .unwrap(); - for idx in entries.entries_index.borrow_mut().iter_mut() { + for idx in entries.entries_index.iter_mut() { idx.file_num = file_num; } let (mut s, mut entries_size2) = (encoded.as_slice(), 0); diff --git a/src/util.rs b/src/util.rs index b1cdc4be..b2db8b5c 100644 --- a/src/util.rs +++ b/src/util.rs @@ -324,8 +324,8 @@ impl Drop for Worker { #[derive(Clone, Debug, Copy, PartialEq, Default)] pub struct Statistic { - pub wal_cost: usize, - pub sync_cost: usize, + pub avg_wal_cost: usize, + pub avg_sync_cost: usize, pub avg_write_cost: usize, pub max_wal_cost: usize, pub max_sync_cost: usize, @@ -342,18 +342,9 @@ fn max(left: usize, right: usize) -> usize { } impl Statistic { - pub fn add(&mut self, other: &Self) { - self.wal_cost += other.wal_cost; - self.sync_cost += other.sync_cost; - self.freq += other.freq; - self.max_wal_cost = max(self.max_wal_cost, other.max_wal_cost); - self.max_write_cost = max(self.max_write_cost, other.max_write_cost); - self.max_sync_cost = max(self.max_sync_cost, other.max_sync_cost); - } - pub fn clear(&mut self) { - self.wal_cost = 0; - self.sync_cost = 0; + self.avg_wal_cost = 0; + self.avg_sync_cost = 0; self.avg_write_cost = 0; self.max_wal_cost = 0; self.max_sync_cost = 0; @@ -363,18 +354,18 @@ impl Statistic { #[inline] pub fn add_wal(&mut self, wal: usize) { - self.wal_cost += wal; + self.avg_wal_cost += wal; self.max_wal_cost = max(self.max_wal_cost, wal); } #[inline] pub fn add_sync(&mut self, sync: usize) { - self.sync_cost += sync; + self.avg_sync_cost += sync; self.max_sync_cost = max(self.max_sync_cost, sync); } - #[inline] - pub fn add_one(&mut self) { - self.freq += 1; + pub fn divide(&mut self) { + self.avg_wal_cost /= self.freq; + self.avg_sync_cost /= self.freq; } } diff --git a/src/wal.rs b/src/wal.rs index d56805dc..eab62cf0 100644 --- a/src/wal.rs +++ b/src/wal.rs @@ -58,8 +58,7 @@ where return Ok(()); } LogMsg::Metric(cb) => { - let _ = cb.send(self.statistic.clone()); - self.statistic.clear(); + self.report(cb); continue; } }; @@ -79,8 +78,7 @@ where return Ok(()); } LogMsg::Metric(cb) => { - let _ = cb.send(self.statistic.clone()); - self.statistic.clear(); + self.report(cb); continue; } }; @@ -118,7 +116,7 @@ where self.statistic.add_wal(wal_cost as usize); self.statistic .add_sync((wal_cost - before_sync_cost) as usize); - self.statistic.add_one(); + self.statistic.freq += 1; for (offset, sender) in write_ret.drain(..) { let _ = sender.send((file_num, base_offset + offset, tracker.clone())); } @@ -126,4 +124,10 @@ where } Ok(()) } + + pub fn report(&mut self, cb: Sender) { + self.statistic.divide(); + let _ = cb.send(self.statistic.clone()); + self.statistic.clear(); + } }