Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:17:27

0001 /*!
0002 \file       PHTimeServer.cxx
0003 \brief   PHTimer server for accessing external information
0004 \author Hugo Pereira
0005 \version $Revision: 1.5 $
0006 \date       $Date: 2014/01/12 04:14:40 $
0007 */
0008 
0009 #include "PHTimeServer.h"
0010 
0011 #include <boost/format.hpp>
0012 
0013 #include <cstdio>
0014 #include <stdexcept>
0015 
0016 //_________________________________________________________
0017 PHTimeServer::timer PHTimeServer::insert_new(const std::string& key)
0018 {
0019   std::string tmp_key(key);
0020 
0021   int version = 0;
0022   while ((_timers.find(tmp_key)) != _timers.end())
0023   {
0024     version++;
0025     std::ostringstream o;
0026     o << key << "_" << version;
0027     tmp_key = o.str();
0028   }
0029 
0030   // create a new timer
0031   _timers.insert(std::pair<std::string, timer>(tmp_key, timer(tmp_key, _timer_id)));
0032   _timer_id++;
0033 
0034   // returns timer
0035   return _timers.find(tmp_key)->second;
0036 }
0037 //_________________________________________________________
0038 PHTimeServer::timer PHTimeServer::insert_new_single_shot(const std::string& key)
0039 {
0040   std::string tmp_key(key);
0041 
0042   int version = 0;
0043   while ((_single_shot_timers.find(tmp_key)) != _single_shot_timers.end())
0044   {
0045     version++;
0046     std::ostringstream o;
0047     o << key << "_" << version;
0048     tmp_key = o.str();
0049   }
0050 
0051   // create a new timer
0052   _single_shot_timers.insert(std::pair<std::string, timer>(tmp_key, timer(tmp_key, _single_shot_timer_id)));
0053   _single_shot_timer_id++;
0054 
0055   // returns timer
0056   return _single_shot_timers.find(tmp_key)->second;
0057 }
0058 
0059 //_________________________________________________________
0060 PHTimeServer::timer PHTimeServer::get_timer(const std::string& key)
0061 {
0062   // check for existing timer matching key
0063   time_iterator _iter = _timers.find(key);
0064   if (_iter != _timers.end())
0065   {
0066     return _iter->second;
0067   }
0068 
0069   std::ostringstream what;
0070   what << "unknown timer \"" << key << "\" requested.";
0071   throw std::invalid_argument(what.str());
0072 }
0073 
0074 //_________________________________________________________
0075 PHTimeServer::timer PHTimeServer::get_single_shot_timer(const std::string& key)
0076 {
0077   // check for existing timer matching key
0078   time_iterator _iter = _single_shot_timers.find(key);
0079   if (_iter != _single_shot_timers.end())
0080   {
0081     return _iter->second;
0082   }
0083 
0084   std::ostringstream what;
0085   what << "unknown timer \"" << key << "\" requested.";
0086   throw std::invalid_argument(what.str());
0087 }
0088 
0089 //_________________________________________________________
0090 void PHTimeServer::print(std::ostream& out) const
0091 {
0092   PHTimer::PRINT(out, "Mutoo PHTimeServer");
0093 
0094   // run over normal timers
0095   for (const auto& _timer : _timers)
0096   {
0097     //    char str[512];
0098     out << (boost::format("%-20s [%2i] - %-6g ms (%s)-.") % _timer.second.get()->get_name() % _timer.second.get_uid() % _timer.second.get()->elapsed() % ((char*) ((_timer.second.get()->get_state() == PHTimer::RUN) ? " (running)" : " (stopped)"))).str() << std::endl;
0099   }
0100 
0101   // run over single_shot timers
0102   PHTimer::PRINT(out, "Mutoo PHTimeServer - single_shots");
0103   for (const auto& _single_shot_timer : _single_shot_timers)
0104   {
0105     out << (boost::format("single_shot - %-20s [%2i] - %-6g ms (%s)-.") % _single_shot_timer.second.get()->get_name() % _single_shot_timer.second.get_uid() % _single_shot_timer.second.get()->elapsed() % ((char*) ((_single_shot_timer.second.get()->get_state() == PHTimer::RUN) ? " (running)" : " (stopped)"))).str() << std::endl;
0106   }
0107 
0108   PHTimer::PRINT(out, "**");
0109 
0110   return;
0111 }
0112 
0113 //_________________________________________________________
0114 void PHTimeServer::print_stat(std::ostream& out) const
0115 {
0116   // print nothing if no timer was registered
0117   if (_timers.empty() && _single_shot_timers.empty())
0118   {
0119     return;
0120   }
0121 
0122   // header
0123   PHTimer::PRINT(out, "Mutoo PHTimeServer statistics");
0124 
0125   // normal timers
0126   for (const auto& _timer : _timers)
0127   {
0128     if (_timer.second.get()->get_ncycle())
0129     {
0130       out << (boost::format("%-20s [%2i] - Accumulated time: %-6g ms. cycles: %-10u. Time per cycle: %-6g ms") % _timer.second.get()->get_name() % _timer.second.get_uid() % _timer.second.get()->get_accumulated_time() % _timer.second.get()->get_ncycle() % _timer.second.get()->get_time_per_cycle()).str() << std::endl;
0131     }
0132   }
0133 
0134   // single shot timers
0135   PHTimer::PRINT(out, "Mutoo PHTimeServer single_shots statistics");
0136   for (const auto& _single_shot_timer : _single_shot_timers)
0137   {
0138     if (_single_shot_timer.second.get()->get_ncycle())
0139     {
0140       out << (boost::format("single_shot - %-20s [%2i] - accumulated: %-6g ms.") % _single_shot_timer.second.get()->get_name() % _single_shot_timer.second.get_uid() % _single_shot_timer.second.get()->get_accumulated_time()).str();
0141 
0142       // check timer _was_ single shot
0143       if (_single_shot_timer.second.get()->get_ncycle() != 1)
0144       {
0145         out << " WARNING: single_shot started more than once.";
0146       }
0147 
0148       out << std::endl;
0149     }
0150   }
0151 
0152   PHTimer::PRINT(out, "**");
0153 
0154   return;
0155 }