Class Reference for E1039 Core & Analysis Software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PHTimeServer.cc
Go to the documentation of this file.
1 // $Id: PHTimeServer.C,v 1.5 2014/01/12 04:14:40 pinkenbu Exp $
2 
11 #include "PHTimeServer.h"
12 #include <algorithm>
13 #include <stdexcept>
14 #include <cstdio>
15 
16 using namespace std;
17 
18 //_________________________________________________________
20 {
21  string tmp_key( key );
22 
23  int version = 0;
24  while( ( _timers.find(tmp_key) ) != _timers.end() )
25  {
26  version++;
27  ostringstream o;
28  o << key << "_" << version;
29  tmp_key = o.str();
30  }
31 
32  // create a new timer
33  _timers.insert( pair<string,timer>( tmp_key, timer( tmp_key, _timer_id ) ) );
34  _timer_id++;
35 
36  // returns timer
37  return _timers.find( tmp_key )->second;
38 }
39 //_________________________________________________________
41 {
42  string tmp_key( key );
43 
44  int version = 0;
45  while( ( _single_shot_timers.find(tmp_key) ) != _single_shot_timers.end() )
46  {
47  version++;
48  ostringstream o;
49  o << key << "_" << version;
50  tmp_key = o.str();
51  }
52 
53  // create a new timer
54  _single_shot_timers.insert( pair<string,timer>( tmp_key, timer( tmp_key, _single_shot_timer_id ) ) );
55  _single_shot_timer_id++;
56 
57  // returns timer
58  return _single_shot_timers.find( tmp_key )->second;
59 }
60 
61 //_________________________________________________________
63 {
64  // check for existing timer matching key
65  time_iterator _iter = _timers.find( key );
66  if( _iter != _timers.end() ) return _iter->second;
67  else
68  {
69  ostringstream what;
70  what << "unknown timer \"" << key << "\" requested.";
71  throw invalid_argument(what.str());
72  }
73 }
74 
75 //_________________________________________________________
77 {
78  // check for existing timer matching key
79  time_iterator _iter = _single_shot_timers.find( key );
80  if( _iter != _single_shot_timers.end() ) return _iter->second;
81  else
82  {
83  ostringstream what;
84  what << "unknown timer \"" << key << "\" requested.";
85  throw invalid_argument(what.str());
86  }
87 }
88 
89 //_________________________________________________________
90 void PHTimeServer::print( ostream & out ) const
91 {
92  PHTimer::PRINT(out,"Mutoo PHTimeServer");
93 
94  // run over normal timers
95  for( const_time_iterator iter = _timers.begin(); iter != _timers.end(); ++iter )
96  {
97 
98  char str[512];
99  sprintf( str, "%-20s [%2i] - %-6g ms (%s)-.",
100  iter->second.get()->get_name().c_str(),
101  iter->second.get_uid(),
102  iter->second.get()->elapsed(),
103  (char*)((iter->second.get()->get_state() == PHTimer::RUN) ? " (running)":" (stopped)")
104  );
105  out << str << endl;
106  }
107 
108  // run over single_shot timers
109  PHTimer::PRINT(out,"Mutoo PHTimeServer - single_shots");
110  for( const_time_iterator iter = _single_shot_timers.begin(); iter != _single_shot_timers.end(); ++iter ){
111  char str[512];
112  sprintf( str, "single_shot - %-20s [%2i] - %-6g ms (%s)-.",
113  iter->second.get()->get_name().c_str(),
114  iter->second.get_uid(),
115  iter->second.get()->elapsed(),
116  (char*)((iter->second.get()->get_state() == PHTimer::RUN) ? " (running)":" (stopped)")
117  );
118  out << str << endl;
119  }
120 
121  PHTimer::PRINT(out,"**");
122 
123  return;
124 }
125 
126 //_________________________________________________________
127 void PHTimeServer::print_stat( ostream & out ) const
128 {
129 
130  // print nothing if no timer was registered
131  if( _timers.empty() && _single_shot_timers.empty() ) return;
132 
133  // header
134  PHTimer::PRINT(out,"Mutoo PHTimeServer statistics");
135 
136  // normal timers
137  for( const_time_iterator iter = _timers.begin(); iter != _timers.end(); ++iter )
138  if( iter->second.get()->get_ncycle() )
139  {
140 
141  char str[512];
142  sprintf( str, "%-20s [%2i] - Accumulated time: %-6g ms. cycles: %-10u. Time per cycle: %-6g ms",
143  iter->second.get()->get_name().c_str(),
144  iter->second.get_uid(),
145  iter->second.get()->get_accumulated_time(),
146  iter->second.get()->get_ncycle(),
147  iter->second.get()->get_time_per_cycle() );
148  out << str << endl;
149 
150  }
151 
152  // single shot timers
153  PHTimer::PRINT(out,"Mutoo PHTimeServer single_shots statistics");
154  for( const_time_iterator iter = _single_shot_timers.begin(); iter != _single_shot_timers.end(); ++iter )
155  if( iter->second.get()->get_ncycle() )
156  {
157 
158  char str[512];
159  sprintf( str, "single_shot - %-20s [%2i] - accumulated: %-6g ms.",
160  iter->second.get()->get_name().c_str(),
161  iter->second.get_uid(),
162  iter->second.get()->get_accumulated_time() );
163  out << str;
164 
165  // check timer _was_ single shot
166  if( iter->second.get()->get_ncycle() != 1 )
167  out << " WARNING: single_shot started more than once.";
168 
169  out << endl;
170 
171  }
172 
173  PHTimer::PRINT(out,"**");
174 
175  return;
176 }
PHTimer server for accessing external information.
wrapper around PHTimer, for storage in a map
Definition: PHTimeServer.h:35
timer insert_new_single_shot(const std::string &)
insert new single_shot timer in map.
Definition: PHTimeServer.cc:40
timer get_single_shot_timer(const std::string &)
retrieve existing timer. throw exception if not found
Definition: PHTimeServer.cc:76
void print_stat(std::ostream &out=std::cout) const
dump all registered timer statistics.
timer insert_new(const std::string &)
insert new timer in map.
Definition: PHTimeServer.cc:19
timer get_timer(const std::string &)
retrieve existing timer. throw exception if not found
Definition: PHTimeServer.cc:62
void print(std::ostream &out=std::cout) const
dump all registered timer value.
Definition: PHTimeServer.cc:90
@ RUN
Definition: PHTimer.h:32
static void PRINT(std::ostream &os=std::cout, const std::string &message="")
print a message (formated) to a stream
Definition: PHTimer.cc:75