Midge  v3.9.2
Data Processing Framework
stream_timer.hh
Go to the documentation of this file.
1 /*
2  * stream_timer.hh
3  *
4  * Created on: Feb 26, 2016
5  * Author: nsoblath
6  */
7 
8 #ifndef MIDGE_STREAM_TIMER_HH_
9 #define MIDGE_STREAM_TIMER_HH_
10 
11 #ifdef ENABLE_STREAM_TIMING
12 #include <chrono>
13 #include <string>
14 
15 namespace midge
16 {
17  class node;
18 
19  class stream_timer
20  {
21  private:
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;
25 
26  public:
27  stream_timer();
28  virtual ~stream_timer();
29 
30  void increment_begin();
31  void increment_locked();
32 
33  void pause_timer();
34  void resume_timer();
35 
36  void report( const std::string& a_label ) const;
37 
38  void reset();
39 
40  private:
41  enum class state
42  {
43  starting,
44  working,
45  blocked,
46  paused
47  };
48  state f_state;
49 
50  ns_t f_total_work_time;
51  uint64_t f_n_work_periods;
52 
53  ns_t f_total_blocked_time;
54  uint64_t f_n_blocked_periods;
55 
56  time_point_t f_timer_start;
57  ns_t f_unpaused_buffer;
58  state f_unpaused_state;
59  };
60 
61  inline void stream_timer::increment_begin()
62  {
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();
70  return;
71  }
72 
73  inline void stream_timer::increment_locked()
74  {
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();
82  return;
83  }
84 
85  inline void stream_timer::pause_timer()
86  {
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;
91  return;
92  }
93 
94  inline void stream_timer::resume_timer()
95  {
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();
100  return;
101  }
102 
103 } /* namespace midge */
104 
105 #endif /* ENABLE_STREAM_TIMING */
106 
107 #ifdef ENABLE_STREAM_TIMING
108 #define IF_STREAM_TIMING_ENABLED( x_line ) x_line
109 #else
110 #define IF_STREAM_TIMING_ENABLED( x_line )
111 #endif
112 
113 #endif /* MIDGE_STREAM_TIMER_HH_ */
Definition: _buffer.hh:11