8 #ifndef MIDGE_STREAM_TIMER_HH_ 9 #define MIDGE_STREAM_TIMER_HH_ 11 #ifdef ENABLE_STREAM_TIMING 22 typedef std::chrono::steady_clock steady_clock_t;
23 typedef std::chrono::time_point< steady_clock_t > time_point_t;
24 typedef std::chrono::nanoseconds ns_t;
28 virtual ~stream_timer();
30 void increment_begin();
31 void increment_locked();
36 void report(
const std::string& a_label )
const;
50 ns_t f_total_work_time;
51 uint64_t f_n_work_periods;
53 ns_t f_total_blocked_time;
54 uint64_t f_n_blocked_periods;
56 time_point_t f_timer_start;
57 ns_t f_unpaused_buffer;
58 state f_unpaused_state;
61 inline void stream_timer::increment_begin()
63 if( f_state == state::blocked || f_unpaused_state == state::blocked )
return;
64 f_total_work_time += f_unpaused_buffer;
65 if( f_state == state::working ) f_total_work_time += steady_clock_t::now() - f_timer_start;
66 if( f_state != state::starting ) ++f_n_work_periods;
67 f_unpaused_buffer = ns_t::zero();
68 f_state = state::blocked;
69 f_timer_start = steady_clock_t::now();
73 inline void stream_timer::increment_locked()
75 if( f_state == state::working || f_unpaused_state == state::working )
return;
76 f_total_blocked_time += f_unpaused_buffer;
77 if( f_state == state::blocked ) f_total_blocked_time += steady_clock_t::now() - f_timer_start;
78 if( f_state != state::starting ) ++f_n_blocked_periods;
79 f_unpaused_buffer = ns_t::zero();
80 f_state = state::working;
81 f_timer_start = steady_clock_t::now();
85 inline void stream_timer::pause_timer()
87 if( f_state == state::paused || f_state == state::starting )
return;
88 f_unpaused_buffer += steady_clock_t::now() - f_timer_start;
89 f_unpaused_state = f_state;
90 f_state = state::paused;
94 inline void stream_timer::resume_timer()
96 if( f_state != state::paused )
return;
97 f_state = f_unpaused_state;
98 f_unpaused_state = state::paused;
99 f_timer_start = std::chrono::steady_clock::now();
107 #ifdef ENABLE_STREAM_TIMING 108 #define IF_STREAM_TIMING_ENABLED( x_line ) x_line 110 #define IF_STREAM_TIMING_ENABLED( x_line )