Skip to content

Commit 3cc0d46

Browse files
committed
fix(cardano-chain-follower): thread logic
Signed-off-by: bkioshn <[email protected]>
1 parent 1bf8f75 commit 3cc0d46

File tree

1 file changed

+16
-9
lines changed
  • rust/cardano-chain-follower/src/stats

1 file changed

+16
-9
lines changed

rust/cardano-chain-follower/src/stats/thread.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ struct InnerThreadStat {
3636

3737
impl Serialize for InnerThreadStat {
3838
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
39-
where S: Serializer {
39+
where
40+
S: Serializer,
41+
{
4042
let mut state = serializer.serialize_struct("ThreadStat", 6)?;
4143

4244
state.serialize_field("counter", &self.counter.load(Ordering::SeqCst))?;
@@ -81,11 +83,11 @@ impl Default for InnerThreadStat {
8183

8284
impl InnerThreadStat {
8385
/// Update the CPU time including the total CPU time and the latest CPU time.
84-
fn update_cpu_time(&self) {
86+
fn update_total_time(&self) {
8587
// Get the current CPU time as a Duration
8688
let current_time = ThreadTime::now().as_duration();
8789

88-
if let Ok(mut latest_cpu_time) = self.latest_cpu_time.lock() {
90+
if let Ok(latest_cpu_time) = self.latest_cpu_time.lock() {
8991
// Calculate elapsed time (current - previous)
9092
let elapsed = if current_time > *latest_cpu_time {
9193
current_time - *latest_cpu_time
@@ -98,8 +100,12 @@ impl InnerThreadStat {
98100
*total_cpu_time += elapsed;
99101
}
100102
}
101-
// Update latest time to the current time
102-
*latest_cpu_time = current_time;
103+
}
104+
}
105+
106+
fn update_latest_time(&self) {
107+
if let Ok(mut latest_cpu_time) = self.latest_cpu_time.lock() {
108+
*latest_cpu_time = ThreadTime::now().as_duration();
103109
}
104110
}
105111

@@ -124,19 +130,20 @@ impl ThreadStat {
124130
/// Stop the thread.
125131
pub(crate) fn stop_thread(&self) {
126132
self.0.is_running.store(false, Ordering::SeqCst);
127-
self.0.update_cpu_time();
133+
self.0.update_latest_time();
134+
self.0.update_total_time();
128135
}
129136

130137
/// Resume the thread.
131138
pub(crate) fn resume_thread(&self) {
132-
self.0.is_running.store(true, Ordering::SeqCst);
133-
self.0.update_cpu_time();
134139
self.0.increment_counter();
140+
self.0.update_latest_time();
135141
}
136142

137143
/// Pause the thread.
138144
pub(crate) fn pause_thread(&self) {
139-
self.0.update_cpu_time();
145+
self.0.update_latest_time();
146+
self.0.update_total_time();
140147
}
141148

142149
/// Is the thread running?

0 commit comments

Comments
 (0)