Midge  v3.9.2
Data Processing Framework
stream_timer.cc
Go to the documentation of this file.
1 /*
2  * stream_timer.cc
3  *
4  * Created on: Feb 26, 2016
5  * Author: nsoblath
6  */
7 
8 #include "stream_timer.hh"
9 
10 #ifdef ENABLE_STREAM_TIMING
11 
12 #include "node.hh"
13 
14 #include "logger.hh"
15 
16 #include <sstream>
17 
18 namespace midge
19 {
20 
21  LOGGER( mlog, "stream_timer" );
22 
23  stream_timer::stream_timer() :
24  f_state( state::starting ),
25  f_total_work_time( ns_t::zero() ),
26  f_n_work_periods( 0 ),
27  f_total_blocked_time( ns_t::zero() ),
28  f_n_blocked_periods( 0 ),
29  f_timer_start( time_point_t::min() ),
30  f_unpaused_buffer( ns_t::zero() ),
31  f_unpaused_state( state::paused )
32  {
33  }
34 
35  stream_timer::~stream_timer()
36  {
37  }
38 
39  void stream_timer::report( const std::string& a_label ) const
40  {
41  typedef std::chrono::duration< double, std::ratio<1,1> > s_flt_t;
42 
43  s_flt_t t_total_work_time_s = std::chrono::duration_cast< s_flt_t >( f_total_work_time );
44  s_flt_t t_total_blocked_time_s = std::chrono::duration_cast< s_flt_t >( f_total_blocked_time );
45 
46  s_flt_t t_work_period = s_flt_t::zero();
47  double t_working_rate = std::numeric_limits< double >::infinity();
48  if( f_n_work_periods != 0 )
49  {
50  t_work_period = t_total_work_time_s / f_n_work_periods;
51  t_working_rate = 1. / t_work_period.count();
52  }
53 
54  s_flt_t t_blocked_period = s_flt_t::zero();
55  if( f_n_blocked_periods != 0 ) t_blocked_period = t_total_blocked_time_s / f_n_blocked_periods;
56 
57  std::stringstream t_timer_report;
58  t_timer_report << "Stream timer report for " << a_label << "\n";
59  t_timer_report << "-----------------------------------------------------------------------------------------------\n";
60  if( f_state != state::starting )
61  {
62  t_timer_report << " * Stream advanced " << f_n_blocked_periods << " times\n";
63  t_timer_report << " * Total time spent working: " << t_total_work_time_s.count() << " s\n";
64  t_timer_report << " * Total time waiting to advance: " << t_total_blocked_time_s.count() << " s\n";
65  t_timer_report << " * Mean working period: " << t_work_period.count() << " s\n";
66  t_timer_report << " * Mean blocked period: " << t_blocked_period.count() << " s\n";
67  t_timer_report << " * Stream working rate: " << t_working_rate << " s^-1\n";
68  }
69  else
70  {
71  t_timer_report << "Timer was unused\n";
72  }
73  LINFO( mlog, t_timer_report.str() );
74  }
75 
76  void stream_timer::reset()
77  {
78  f_state = state::paused;
79  f_total_work_time = ns_t::zero();
80  f_n_work_periods = 0;
81  f_total_blocked_time = ns_t::zero();
82  f_n_blocked_periods = 0;
83  f_timer_start = time_point_t::min();
84  f_unpaused_buffer = ns_t::zero();
85  f_unpaused_state = state::paused;
86  return;
87  }
88 
89 
90 } /* namespace midge */
91 
92 #endif /* ENABLE_STREAM_TIMER */
Definition: _buffer.hh:11