ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PHTimeServer.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file PHTimeServer.cc
1 // $Id: PHTimeServer.C,v 1.5 2014/01/12 04:14:40 pinkenbu Exp $
2 
11 #include "PHTimeServer.h"
12 
13 #include <cstdio>
14 #include <stdexcept>
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())
67  return _iter->second;
68  else
69  {
70  ostringstream what;
71  what << "unknown timer \"" << key << "\" requested.";
72  throw invalid_argument(what.str());
73  }
74 }
75 
76 //_________________________________________________________
78 {
79  // check for existing timer matching key
80  time_iterator _iter = _single_shot_timers.find(key);
81  if (_iter != _single_shot_timers.end())
82  return _iter->second;
83  else
84  {
85  ostringstream what;
86  what << "unknown timer \"" << key << "\" requested.";
87  throw invalid_argument(what.str());
88  }
89 }
90 
91 //_________________________________________________________
92 void PHTimeServer::print(ostream& out) const
93 {
94  PHTimer::PRINT(out, "Mutoo PHTimeServer");
95 
96  // run over normal timers
97  for (const_time_iterator iter = _timers.begin(); iter != _timers.end(); ++iter)
98  {
99  char str[512];
100  sprintf(str, "%-20s [%2i] - %-6g ms (%s)-.",
101  iter->second.get()->get_name().c_str(),
102  iter->second.get_uid(),
103  iter->second.get()->elapsed(),
104  (char*) ((iter->second.get()->get_state() == PHTimer::RUN) ? " (running)" : " (stopped)"));
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  {
112  char str[512];
113  sprintf(str, "single_shot - %-20s [%2i] - %-6g ms (%s)-.",
114  iter->second.get()->get_name().c_str(),
115  iter->second.get_uid(),
116  iter->second.get()->elapsed(),
117  (char*) ((iter->second.get()->get_state() == PHTimer::RUN) ? " (running)" : " (stopped)"));
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  // print nothing if no timer was registered
130  if (_timers.empty() && _single_shot_timers.empty()) return;
131 
132  // header
133  PHTimer::PRINT(out, "Mutoo PHTimeServer statistics");
134 
135  // normal timers
136  for (const_time_iterator iter = _timers.begin(); iter != _timers.end(); ++iter)
137  if (iter->second.get()->get_ncycle())
138  {
139  char str[512];
140  sprintf(str, "%-20s [%2i] - Accumulated time: %-6g ms. cycles: %-10u. Time per cycle: %-6g ms",
141  iter->second.get()->get_name().c_str(),
142  iter->second.get_uid(),
143  iter->second.get()->get_accumulated_time(),
144  iter->second.get()->get_ncycle(),
145  iter->second.get()->get_time_per_cycle());
146  out << str << endl;
147  }
148 
149  // single shot timers
150  PHTimer::PRINT(out, "Mutoo PHTimeServer single_shots statistics");
151  for (const_time_iterator iter = _single_shot_timers.begin(); iter != _single_shot_timers.end(); ++iter)
152  if (iter->second.get()->get_ncycle())
153  {
154  char str[512];
155  sprintf(str, "single_shot - %-20s [%2i] - accumulated: %-6g ms.",
156  iter->second.get()->get_name().c_str(),
157  iter->second.get_uid(),
158  iter->second.get()->get_accumulated_time());
159  out << str;
160 
161  // check timer _was_ single shot
162  if (iter->second.get()->get_ncycle() != 1)
163  out << " WARNING: single_shot started more than once.";
164 
165  out << endl;
166  }
167 
168  PHTimer::PRINT(out, "**");
169 
170  return;
171 }