Skip to content

Commit a17366b

Browse files
committed
Add timer trace logs
1 parent a04dd5e commit a17366b

File tree

5 files changed

+98
-13
lines changed

5 files changed

+98
-13
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

util/logger/src/macros.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ macro_rules! log_target {
8585
(TEST_SCRIPT) => {
8686
"test_script"
8787
};
88+
(TIMER) => {
89+
"timer"
90+
};
8891
(TRIE) => {
8992
"trie"
9093
};

util/timer/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ authors = ["CodeChain Team <[email protected]>"]
77

88
[dependencies]
99
parking_lot = "0.6.0"
10+
log = "0.4.6"
11+
codechain-logger = { path = "../logger" }

util/timer/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1616

1717
extern crate parking_lot;
18+
#[macro_use]
19+
extern crate log;
20+
#[macro_use]
21+
extern crate codechain_logger as clogger;
1822

1923
mod timer;
2024

util/timer/src/timer.rs

Lines changed: 87 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -244,42 +244,55 @@ impl SchedulerInner {
244244
}
245245
};
246246

247+
ctrace!(
248+
TIMER,
249+
"schedule(TimerName({}), {:?}, after: {:?}, repeat: {:?})",
250+
requested_timer.timer_name,
251+
schedule_id,
252+
after,
253+
repeat,
254+
);
255+
247256
let schedule = Schedule {
248257
at: Instant::now() + after,
249258
schedule_id,
250259
repeat,
251260
state_control,
252261
handler,
262+
timer_name: requested_timer.timer_name,
253263
};
254264
// state_control become an attached one (Def 1)
255265
self.heap.push(Reverse(TimeOrdered(schedule)));
256266
Ok(())
257267
}
258268

259-
fn try_reschedule(&mut self, mut schedule: Schedule) {
269+
fn try_reschedule(&mut self, mut schedule: Schedule) -> bool {
260270
schedule.at = Instant::now() + schedule.repeat.expect("Schedule should have repeat interval");
261271
match self.states.entry(schedule.schedule_id) {
262272
Entry::Vacant(_) => {
263273
// 'schedule.state_control' was detached one (Def 1).
264274
// Don't reschedule since it is Cancelled (Rule 1)
265275
// schedule is going to be removed
276+
false
266277
}
267278
Entry::Occupied(entry) => {
268279
if Arc::ptr_eq(entry.get(), &schedule.state_control) {
269280
// schedule.state_control was attached one, (Def 1)
270281
// just re-push to heap.
271282
self.heap.push(Reverse(TimeOrdered(schedule)));
283+
true
272284
} else if entry.get().is_cancelled() {
273285
// Detach the entry (Corollary 1) before it become garbage (Note 1),
274286
entry.remove();
275-
// 'schedule.state_control' was detached one (Def 1).
276-
// Don't reschedule since it is Cancelled (Rule 1)
277-
// schedule is going to be removed
287+
// 'schedule.state_control' was detached one (Def 1).
288+
// Don't reschedule since it is Cancelled (Rule 1)
289+
// schedule is going to be removed
290+
false
278291
} else {
279292
unreachable!("Rule 1 was violated");
280293
}
281294
}
282-
};
295+
}
283296
}
284297

285298
fn cancel(&mut self, requested_timer: &TimerApi, timer_token: TimerToken) -> bool {
@@ -289,6 +302,7 @@ impl SchedulerInner {
289302
Entry::Vacant(_) => false,
290303
Entry::Occupied(entry) => {
291304
// Detach and cancel it (Rule 1)
305+
ctrace!(TIMER, "cancel(TimerName({}), {:?})", requested_timer.timer_name, schedule_id);
292306
let state_control = entry.remove();
293307
state_control.cancel()
294308
}
@@ -305,11 +319,35 @@ impl SchedulerInner {
305319
}
306320
let Reverse(TimeOrdered(timed_out)) = self.heap.pop().expect("It always have an item");
307321

322+
let timer_name = timed_out.timer_name;
323+
let schedule_id = timed_out.schedule_id;
324+
308325
if timed_out.repeat.is_some() {
309326
if let Some(callback) = Callback::from_schedule(&timed_out) {
310327
// timed_out.state_control is re-pushed only after it is popped (Rule 3)
311-
self.try_reschedule(timed_out.clone());
312-
worker_queue.enqueue(callback);
328+
if self.try_reschedule(timed_out.clone()) {
329+
ctrace!(
330+
TIMER,
331+
"handle_timeout(TimerName({}), {:?}, repeat): Enqueue to worker queue",
332+
timer_name,
333+
schedule_id
334+
);
335+
worker_queue.enqueue(callback);
336+
} else {
337+
ctrace!(
338+
TIMER,
339+
"handle_timeout(TimerName({}), {:?}, repeat): Cancelled schedule",
340+
timer_name,
341+
schedule_id
342+
);
343+
}
344+
} else {
345+
ctrace!(
346+
TIMER,
347+
"handle_timeout(TimerName({}), {:?}, repeat): Dropped TimeoutHandler",
348+
timer_name,
349+
schedule_id
350+
);
313351
}
314352
} else {
315353
let enqueue = match self.states.entry(timed_out.schedule_id) {
@@ -325,21 +363,41 @@ impl SchedulerInner {
325363
_ => false, // also detached.
326364
};
327365
// timed_out is anyway removed.
328-
if enqueue {
329-
if let Some(callback) = Callback::from_schedule(&timed_out) {
366+
367+
if let Some(callback) = Callback::from_schedule(&timed_out) {
368+
if enqueue {
369+
ctrace!(
370+
TIMER,
371+
"handle_timeout(TimerName({}), {:?}, once): Enqueue to worker queue",
372+
timer_name,
373+
schedule_id
374+
);
330375
worker_queue.enqueue(callback);
376+
} else {
377+
// 'timed_out.state_control' was detached one. (Def 1)
378+
// It already be cancelled (Rule 1)
379+
debug_assert!(timed_out.state_control.is_cancelled());
380+
ctrace!(
381+
TIMER,
382+
"handle_timeout(TimerName({}), {:?}, once): Cancelled schedule",
383+
timer_name,
384+
schedule_id
385+
);
331386
}
332387
} else {
333-
// 'timed_out.state_control' was detached one. (Def 1)
334-
// It already be cancelled (Rule 1)
335-
debug_assert!(timed_out.state_control.is_cancelled());
388+
ctrace!(
389+
TIMER,
390+
"handle_timeout(TimerName({}), {:?}, once): Dropped TimeoutHandler",
391+
timer_name,
392+
schedule_id
393+
);
336394
}
337395
}
338396
}
339397
}
340398
}
341399

342-
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
400+
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
343401
struct ScheduleId(TimerId, TimerToken);
344402

345403
/*
@@ -423,6 +481,7 @@ struct Schedule {
423481
repeat: Option<Duration>,
424482
state_control: Arc<ScheduleStateControl>,
425483
handler: Weak<TimeoutHandler>,
484+
timer_name: TimerName,
426485
}
427486

428487
struct TimeOrdered<T>(T);
@@ -458,6 +517,7 @@ struct Callback {
458517
from_oneshot_schedule: bool,
459518
state_control: Arc<ScheduleStateControl>,
460519
handler: Arc<TimeoutHandler>,
520+
timer_name: TimerName,
461521
}
462522

463523
impl Callback {
@@ -468,6 +528,7 @@ impl Callback {
468528
from_oneshot_schedule: schedule.repeat.is_none(),
469529
state_control: Arc::clone(&schedule.state_control),
470530
handler,
531+
timer_name: schedule.timer_name,
471532
})
472533
} else {
473534
None
@@ -488,11 +549,24 @@ fn worker_loop(queue: &Arc<WorkerQueue>) {
488549
callback.state_control.within_lock(|state| {
489550
debug_assert_ne!(*state, ScheduleState::Timeout);
490551
if *state != ScheduleState::Cancelled {
552+
ctrace!(
553+
TIMER,
554+
"worker_loop(TimerName({}), {:?}): Call on_timeout",
555+
callback.timer_name,
556+
callback.schedule_id
557+
);
491558
callback.handler.on_timeout(timer_token);
492559
debug_assert_eq!(callback.from_oneshot_schedule, *state == ScheduleState::Wait);
493560
if callback.from_oneshot_schedule {
494561
ScheduleStateControl::set_timeout(state);
495562
}
563+
} else {
564+
ctrace!(
565+
TIMER,
566+
"worker_loop(TimerName({}), {:?}): Cancelled callback",
567+
callback.timer_name,
568+
callback.schedule_id
569+
);
496570
}
497571
});
498572
}

0 commit comments

Comments
 (0)