Analysis Software
Documentation for sPHENIX simulation 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 //_________________________________________________________
18 {
19  std::string tmp_key(key);
20 
21  int version = 0;
22  while ((_timers.find(tmp_key)) != _timers.end())
23  {
24  version++;
25  std::ostringstream o;
26  o << key << "_" << version;
27  tmp_key = o.str();
28  }
29 
30  // create a new timer
31  _timers.insert(std::pair<std::string, timer>(tmp_key, timer(tmp_key, _timer_id)));
32  _timer_id++;
33 
34  // returns timer
35  return _timers.find(tmp_key)->second;
36 }
37 //_________________________________________________________
39 {
40  std::string tmp_key(key);
41 
42  int version = 0;
43  while ((_single_shot_timers.find(tmp_key)) != _single_shot_timers.end())
44  {
45  version++;
46  std::ostringstream o;
47  o << key << "_" << version;
48  tmp_key = o.str();
49  }
50 
51  // create a new timer
52  _single_shot_timers.insert(std::pair<std::string, timer>(tmp_key, timer(tmp_key, _single_shot_timer_id)));
54 
55  // returns timer
56  return _single_shot_timers.find(tmp_key)->second;
57 }
58 
59 //_________________________________________________________
61 {
62  // check for existing timer matching key
63  time_iterator _iter = _timers.find(key);
64  if (_iter != _timers.end())
65  return _iter->second;
66  else
67  {
68  std::ostringstream what;
69  what << "unknown timer \"" << key << "\" requested.";
70  throw std::invalid_argument(what.str());
71  }
72 }
73 
74 //_________________________________________________________
76 {
77  // check for existing timer matching key
78  time_iterator _iter = _single_shot_timers.find(key);
79  if (_iter != _single_shot_timers.end())
80  return _iter->second;
81  else
82  {
83  std::ostringstream what;
84  what << "unknown timer \"" << key << "\" requested.";
85  throw std::invalid_argument(what.str());
86  }
87 }
88 
89 //_________________________________________________________
90 void PHTimeServer::print(std::ostream& out) const
91 {
92  PHTimer::PRINT(out, "Mutoo PHTimeServer");
93 
94  // run over normal timers
95  for (const auto & _timer : _timers)
96  {
97  char str[512];
98  sprintf(str, "%-20s [%2i] - %-6g ms (%s)-.",
99  _timer.second.get()->get_name().c_str(),
100  _timer.second.get_uid(),
101  _timer.second.get()->elapsed(),
102  (char*) ((_timer.second.get()->get_state() == PHTimer::RUN) ? " (running)" : " (stopped)"));
103  out << str << std::endl;
104  }
105 
106  // run over single_shot timers
107  PHTimer::PRINT(out, "Mutoo PHTimeServer - single_shots");
108  for (const auto & _single_shot_timer : _single_shot_timers)
109  {
110  char str[512];
111  sprintf(str, "single_shot - %-20s [%2i] - %-6g ms (%s)-.",
112  _single_shot_timer.second.get()->get_name().c_str(),
113  _single_shot_timer.second.get_uid(),
114  _single_shot_timer.second.get()->elapsed(),
115  (char*) ((_single_shot_timer.second.get()->get_state() == PHTimer::RUN) ? " (running)" : " (stopped)"));
116  out << str << std::endl;
117  }
118 
119  PHTimer::PRINT(out, "**");
120 
121  return;
122 }
123 
124 //_________________________________________________________
125 void PHTimeServer::print_stat(std::ostream& out) const
126 {
127  // print nothing if no timer was registered
128  if (_timers.empty() && _single_shot_timers.empty()) return;
129 
130  // header
131  PHTimer::PRINT(out, "Mutoo PHTimeServer statistics");
132 
133  // normal timers
134  for (const auto & _timer : _timers)
135  if (_timer.second.get()->get_ncycle())
136  {
137  char str[512];
138  sprintf(str, "%-20s [%2i] - Accumulated time: %-6g ms. cycles: %-10u. Time per cycle: %-6g ms",
139  _timer.second.get()->get_name().c_str(),
140  _timer.second.get_uid(),
141  _timer.second.get()->get_accumulated_time(),
142  _timer.second.get()->get_ncycle(),
143  _timer.second.get()->get_time_per_cycle());
144  out << str << std::endl;
145  }
146 
147  // single shot timers
148  PHTimer::PRINT(out, "Mutoo PHTimeServer single_shots statistics");
149  for (const auto & _single_shot_timer : _single_shot_timers)
150  if (_single_shot_timer.second.get()->get_ncycle())
151  {
152  char str[512];
153  sprintf(str, "single_shot - %-20s [%2i] - accumulated: %-6g ms.",
154  _single_shot_timer.second.get()->get_name().c_str(),
155  _single_shot_timer.second.get_uid(),
156  _single_shot_timer.second.get()->get_accumulated_time());
157  out << str;
158 
159  // check timer _was_ single shot
160  if (_single_shot_timer.second.get()->get_ncycle() != 1)
161  out << " WARNING: single_shot started more than once.";
162 
163  out << std::endl;
164  }
165 
166  PHTimer::PRINT(out, "**");
167 
168  return;
169 }