Gaudi Framework, version v22r2

Home   Generated: Tue May 10 2011

HistorySvc.cpp

Go to the documentation of this file.
00001 // $Id: HistorySvc.cpp,v 1.18 2008/06/04 12:35:15 marcocle Exp $
00002 
00003 #ifndef GAUDISVC_FASTHISTORYSVC_H
00004  #include "HistorySvc.h"
00005 #endif
00006 
00007 #include "GaudiKernel/HistoryObj.h"
00008 
00009 #include "GaudiKernel/JobHistory.h"
00010 #include "GaudiKernel/AlgorithmHistory.h"
00011 #include "GaudiKernel/AlgToolHistory.h"
00012 #include "GaudiKernel/DataHistory.h"
00013 #include "GaudiKernel/ServiceHistory.h"
00014 
00015 #ifndef KERNEL_SVCFACTORY_H
00016  #include "GaudiKernel/SvcFactory.h"
00017 #endif
00018 #ifndef GAUDIKERNEL_ISVCLOCATOR_H
00019  #include "GaudiKernel/ISvcLocator.h"
00020 #endif
00021 
00022 #include "GaudiKernel/System.h"
00023 #include "GaudiKernel/Bootstrap.h"
00024 #include "GaudiKernel/IAlgManager.h"
00025 #include "GaudiKernel/IAlgorithm.h"
00026 #include "GaudiKernel/Algorithm.h"
00027 #include "GaudiKernel/IAlgTool.h"
00028 #include "GaudiKernel/AlgTool.h"
00029 #include "GaudiKernel/IService.h"
00030 #include "GaudiKernel/IJobOptionsSvc.h"
00031 #include "GaudiKernel/IAppMgrUI.h"
00032 #include "GaudiKernel/IIncidentSvc.h"
00033 #include "GaudiKernel/IToolSvc.h"
00034 #include "GaudiKernel/ServiceHandle.h"
00035 
00036 #include "GaudiKernel/IAlgContextSvc.h"
00037 
00038 #include <iostream>
00039 #include <fstream>
00040 #include <sstream>
00041 #include <limits>
00042 
00043 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00044 DECLARE_SERVICE_FACTORY(HistorySvc)
00045 
00046 using namespace std;
00047 
00048 //
00050 //
00051 
00052 struct DHH {
00053   CLID id;
00054   std::string key;
00055 
00056   DHH(const CLID& i, const std::string& k):id(i), key(k) {}
00057 
00058   bool operator < ( DHH const &rhs ) const {
00059     if (id != rhs.id) {
00060       return (id < rhs.id);
00061     } else {
00062       return (key < rhs.key);
00063     }
00064   }
00065 };
00066 
00067 void fenv_dummy(char**) {}
00068 
00069 HistorySvc::HistorySvc( const std::string& name, ISvcLocator* svc )
00070   : base_class( name, svc ),
00071     m_isInitialized(false),
00072     m_dump(false),
00073     p_algCtxSvc(0),
00074     m_jobHistory(0),
00075     m_outputFile(""),
00076     m_incidentSvc(0),
00077     m_log(msgSvc(), name )
00078 
00079 {
00080     declareProperty("Dump",m_dump);
00081     declareProperty("Activate", m_activate=true);
00082     declareProperty("OutputFile",m_outputFile);
00083 
00084     // hack to bring in environ
00085     vector<string> envtmp = System::getEnv();
00086     envtmp.size(); // prevent icc remark #177: X declared but never referenced
00087 }
00088 
00089 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00090 
00091 HistorySvc::~HistorySvc() {
00092   delete m_jobHistory;
00093 }
00094 
00095 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00096 
00097 StatusCode HistorySvc::reinitialize() {
00098 
00099   clearState();
00100 
00101   m_state = Gaudi::StateMachine::OFFLINE;
00102 
00103   return initialize();
00104 
00105 }
00106 
00107 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00108 
00109 void HistorySvc::clearState() {
00110   m_algs.clear();
00111   std::map<const Algorithm*, AlgorithmHistory*>::iterator algitr;
00112   for (algitr = m_algmap.begin(); algitr != m_algmap.end(); ++algitr) {
00113     AlgorithmHistory* h = algitr->second;
00114     (const_cast<Algorithm*> (algitr->first))->release();
00115     delete h;
00116   }
00117   m_algmap.clear();
00118 
00119   m_ialgtools.clear();
00120   m_algtools.clear();
00121   std::map<const AlgTool*, AlgToolHistory*>::iterator atitr;
00122   for (atitr=m_algtoolmap.begin(); atitr != m_algtoolmap.end(); ++atitr) {
00123     (const_cast<AlgTool*> (atitr->first))->release();
00124     delete atitr->second;
00125   }
00126   m_algtoolmap.clear();
00127 
00128   m_svcs.clear();
00129   std::map<const IService*, ServiceHistory*>::iterator svitr;
00130   for (svitr = m_svcmap.begin(); svitr != m_svcmap.end(); ++svitr) {
00131     (const_cast<IService*> (svitr->first))->release();
00132     delete svitr->second;
00133   }
00134   m_svcmap.clear();
00135 }
00136 
00137 
00138 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00139 
00140 StatusCode HistorySvc::initialize() {
00141 
00142   StatusCode status = Service::initialize();
00143   m_log.setLevel( m_outputLevel.value() );
00144 
00145   if (status.isFailure()) {
00146 #ifndef NDEBUG
00147     m_log << MSG::DEBUG << "Failed to initialize the base class (Service)"
00148           << endmsg;
00149 #endif
00150     return status;
00151   }
00152 
00153 #ifndef NDEBUG
00154   m_log << MSG::DEBUG << "Initializing HistorySvc" << endmsg;
00155 #endif
00156 
00157   if (!m_activate) return StatusCode::SUCCESS;
00158 
00159   static const bool CREATEIF(true);
00160 
00161   if ( service("AlgContextSvc",p_algCtxSvc,CREATEIF).isFailure() ) {
00162     m_log << MSG::ERROR << "unable to get the AlgContextSvc" << endmsg;
00163     return StatusCode::FAILURE;
00164   }
00165 
00166   if (service("IncidentSvc", m_incidentSvc, CREATEIF).isFailure()) {
00167     m_log << MSG::ERROR << "unable to get the IncidentSvc" << endmsg;
00168     return StatusCode::FAILURE;
00169   }
00170 
00171   // create a weak dependency on the ToolSvc, so that it doesn't get deleted
00172   // before we're done with it in finalize
00173   m_toolSvc = serviceLocator()->service("ToolSvc");
00174   if (! m_toolSvc)  {
00175     m_log << MSG::ERROR << "could not retrieve the ToolSvc handle !"
00176           << endmsg;
00177     return StatusCode::FAILURE;
00178   }
00179 
00180   // add listener to be triggered by first BeginEvent with low priority
00181   // so it gets called first
00182   const bool rethrow = false;
00183   const bool oneShot = true; // make the listener called only once
00184   m_incidentSvc->addListener(this,IncidentType::BeginEvent,
00185                              std::numeric_limits<long>::min(),rethrow,oneShot);
00186 
00187   m_isInitialized = true;
00188 
00189   return StatusCode::SUCCESS;
00190 
00191 }
00192 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00193 
00194 StatusCode HistorySvc::captureState() {
00195 
00196   if (m_jobHistory == 0) {
00197     m_jobHistory = new JobHistory;
00198     IJobOptionsSvc *jo;
00199     if (service("JobOptionsSvc",jo).isFailure()) {
00200        m_log << MSG::ERROR
00201              << "Could not get jobOptionsSvc - "
00202              << "not adding properties to JobHistory" << endmsg;
00203     } else {
00204 
00205       bool foundAppMgr(false);
00206 
00207       std::vector<std::string> clients = jo->getClients();
00208       std::vector<std::string>::const_iterator it;
00209       std::vector<const Property*>::const_iterator itr;
00210       for (it=clients.begin(); it!=clients.end(); ++it) {
00211         if (*it == "ApplicationMgr") {
00212           foundAppMgr = true;
00213         }
00214         const std::vector<const Property*> *props = jo->getProperties(*it);
00215         for (itr=props->begin(); itr != props->end(); ++itr) {
00216           m_jobHistory->addProperty( *it, *itr );
00217         }
00218       }
00219 
00220       if (!foundAppMgr) {
00221         IProperty *ap;
00222         if (service("ApplicationMgr",ap).isFailure()) {
00223           m_log << MSG::ERROR << "could not get the ApplicationMgr" << endmsg;
00224         } else {
00225           std::vector<Property*>::const_iterator itr2;
00226           const std::vector<Property*> props = ap->getProperties();
00227           for (itr2=props.begin(); itr2 != props.end(); ++itr2) {
00228             m_jobHistory->addProperty( "ApplicationMgr", *itr2 );
00229           }
00230         }
00231       }
00232 
00233     }
00234   }
00235 
00237 
00238   StatusCode sc;
00239   IAlgManager* algMgr = 0;
00240   sc = Gaudi::svcLocator()->queryInterface( IAlgManager::interfaceID(),
00241                                             pp_cast<void>(&algMgr) );
00242   if (sc.isFailure()) {
00243     m_log << MSG::ERROR << "Could not get AlgManager" << endmsg;
00244     return StatusCode::FAILURE;
00245   } else {
00246 
00247     std::list<IAlgorithm*> algs;
00248     algs = algMgr->getAlgorithms();
00249     std::list<IAlgorithm*>::const_iterator itr;
00250     for (itr=algs.begin(); itr!=algs.end(); ++itr) {
00251       Algorithm* alg = dynamic_cast<Algorithm*> (*itr);
00252       if (alg == 0) {
00253         m_log << MSG::WARNING << "Algorithm " << (*itr)->name()
00254               << " does not inherit from Algorithm. Not registering it."
00255               << endmsg;
00256       } else {
00257         registerAlg( *alg ).ignore();
00258       }
00259     }
00260 
00261     m_log << MSG::INFO;
00262     m_log << "Registered " << algs.size() << " Algorithms" << endmsg;
00263 
00264   }
00265 
00267 
00268   m_isInitialized = true;
00269   std::set<const IAlgTool*>::const_iterator itra;
00270   for (itra = m_ialgtools.begin(); itra != m_ialgtools.end(); ++itra) {
00271     (const_cast<IAlgTool*>(*itra))->addRef();
00272     registerAlgTool(**itra).ignore();
00273   }
00274 
00275   m_log << MSG::INFO << "Registered " << m_algtools.size() << " AlgTools"
00276         << endmsg;
00277 
00279 
00280   std::list<IService*> svcs = Gaudi::svcLocator()->getServices();
00281 
00282   std::list<IService*>::const_iterator itrs;
00283   for (itrs=svcs.begin(); itrs!=svcs.end(); ++itrs) {
00284     (*itrs)->addRef();
00285     registerSvc(**itrs).ignore();
00286   }
00287 
00288   m_log << MSG::INFO;
00289   m_log << "Registered " << svcs.size() << " Services" << endmsg;
00290 
00291   return StatusCode::SUCCESS;
00292 
00293 
00294 }
00295 
00296 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00297 
00298 StatusCode HistorySvc::stop() {
00299 
00300   if (!m_activate) return StatusCode::SUCCESS;
00301 
00302   if (m_dump) {
00303     listProperties().ignore();
00304   }
00305 
00306   if (m_outputFile != "") {
00307     std::ofstream ofs;
00308     ofs.open(m_outputFile.c_str());
00309     if (!ofs) {
00310       m_log << MSG::ERROR << "Unable to open output file \"m_outputFile\""
00311             << endmsg;
00312     } else {
00313 
00314       //      dumpProperties(ofs);
00315       dumpState(ofs);
00316 
00317       ofs.close();
00318     }
00319   }
00320 
00321   clearState();
00322 
00323   return StatusCode::SUCCESS;
00324 
00325 }
00326 
00327 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00328 
00329 StatusCode HistorySvc::finalize() {
00330 
00331  
00332   clearState();
00333 
00334   StatusCode status = Service::finalize();
00335 
00336   if ( status.isSuccess() )
00337     m_log << MSG::INFO << "Service finalised successfully" << endmsg;
00338 
00339   return status;
00340 
00341 }
00342 
00343 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00344 
00345 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00346 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00347 
00348 StatusCode
00349 HistorySvc::registerAlg(const Algorithm &alg) {
00350 
00351   JobHistory *job = getJobHistory();
00352   if (m_algmap.find(&alg) != m_algmap.end()) {
00353     m_log << MSG::WARNING << "Algorithm " << alg.name()
00354           << " already registered with HistorySvc" << endmsg;
00355     return StatusCode::SUCCESS;
00356   }
00357 
00358   (const_cast<Algorithm*>(&alg))->addRef();
00359 
00360   m_algs.insert(&alg);
00361 
00362   AlgorithmHistory *algHist = new AlgorithmHistory(alg, job);
00363   m_algmap[&alg] = algHist;
00364 
00365 #ifndef NDEBUG
00366   m_log << MSG::DEBUG << "Registering algorithm: ";
00367   m_log.setColor(MSG::CYAN);
00368   m_log << alg.name() << endmsg;
00369   m_log.resetColor();
00370 #endif
00371 
00372   return StatusCode(StatusCode::SUCCESS,true);
00373 
00374 }
00375 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00376 
00377 StatusCode
00378 HistorySvc::listProperties(const Algorithm &alg) const {
00379 
00380   m_log << MSG::INFO << "Dumping properties for " << alg.name() << endl;
00381 
00382   AlgorithmHistory *hist = getAlgHistory( alg );
00383 
00384   if (hist == 0) {
00385     return StatusCode::FAILURE;
00386   }
00387 
00388   ostringstream ost;
00389   ost << *hist;
00390 
00391   std::string str = ost.str();
00392 
00393   m_log << MSG::INFO << alg.name() << " --> " << endl << str << endmsg;
00394 
00395   return StatusCode(StatusCode::SUCCESS,true);
00396 
00397 }
00398 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00399 
00400 void
00401 HistorySvc::dumpProperties(const Algorithm &alg, std::ofstream& ofs) const {
00402 
00403   AlgorithmHistory *hist = getAlgHistory( alg );
00404 
00405   if (hist == 0) {
00406     return;
00407   }
00408 
00409   AlgorithmHistory::PropertyList::const_iterator itr;
00410   for (itr=hist->properties().begin(); itr!=hist->properties().end(); ++itr) {
00411     ofs << alg.name() << "  " << dumpProp(*itr) << std::endl;
00412   }
00413 
00414 }
00415 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00416 
00417 AlgorithmHistory*
00418 HistorySvc::getAlgHistory(const Algorithm &alg) const {
00419 
00420   const Algorithm *palg = &alg;
00421   set<const Algorithm*>::const_iterator itr = m_algs.find(palg);
00422   if ( itr == m_algs.end() ) {
00423     m_log << MSG::WARNING << "Algorithm " << alg.name() << " not registered"
00424           << endmsg;
00425     return 0;
00426   }
00427 
00428   map<const Algorithm*, AlgorithmHistory*>::const_iterator itr2;
00429   itr2 = m_algmap.find( *itr );
00430 
00431   return ( itr2->second );
00432 
00433 }
00434 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00435 
00436 void
00437 HistorySvc::getAlgHistory(std::set<AlgorithmHistory*>& algs) const {
00438 
00439   set<const Algorithm*>::const_iterator itr;
00440   for (itr=m_algs.begin(); itr!=m_algs.end(); ++itr) {
00441     AlgorithmHistory *ah = m_algmap.find(*itr)->second;
00442 
00443     algs.insert(ah);
00444   }
00445 
00446 }
00447 
00448 
00449 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00450 StatusCode
00451 HistorySvc::registerJob() {
00452 
00453 
00454   return StatusCode(StatusCode::SUCCESS,true);
00455 
00456 
00457 }
00458 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00459 
00460 
00461 StatusCode
00462 HistorySvc::listProperties() const {
00463 
00464   m_log << MSG::INFO;
00465   m_log.setColor(MSG::CYAN);
00466   m_log << "Dumping properties for all Algorithms (" << m_algmap.size() 
00467         << ")" << endmsg;
00468 
00469   std::map<const Algorithm*, AlgorithmHistory*>::const_iterator itr;
00470   for (itr=m_algmap.begin(); itr != m_algmap.end(); ++itr) {
00471     const Algorithm* alg = itr->first;
00472 
00473     listProperties( *alg ).ignore();
00474 
00475   }
00476 
00477   m_log << MSG::INFO;
00478   m_log.setColor(MSG::CYAN);
00479   m_log << "Dumping properties for all AlgTools (" << m_algtoolmap.size()
00480         << ")" << endmsg;
00481 
00482   std::map<const AlgTool*, AlgToolHistory*>::const_iterator itr_a;
00483   for (itr_a=m_algtoolmap.begin(); itr_a != m_algtoolmap.end(); ++itr_a) {
00484     m_log << MSG::DEBUG << " --> " << itr_a->second->algtool_name() << endmsg;
00485     const AlgTool* alg = itr_a->first;
00486 
00487     listProperties( *alg ).ignore();
00488 
00489   }
00490 
00491   m_log << MSG::INFO;
00492   m_log.setColor(MSG::CYAN);
00493   m_log << "Dumping properties for all Services (" << m_svcmap.size()
00494         << ")" << endmsg;
00495 
00496   std::map<const IService*, ServiceHistory*>::const_iterator itr_s;
00497   for (itr_s=m_svcmap.begin(); itr_s != m_svcmap.end(); ++itr_s) {
00498     const IService* svc = itr_s->first;
00499 
00500     listProperties( *svc ).ignore();
00501 
00502   }
00503 
00504   m_log << MSG::INFO;
00505   m_log.setColor(MSG::CYAN);
00506   m_log << "Dumping properties for Job";
00507   m_log.resetColor();
00508 
00509   ostringstream ost;
00510   ost << *m_jobHistory;
00511   std::string str = ost.str();
00512 
00513   m_log << std::endl << str << endmsg;
00514 
00515   return StatusCode(StatusCode::SUCCESS,true);
00516 
00517 }
00518 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00519 
00520 void
00521 HistorySvc::dumpProperties(std::ofstream& ofs) const {
00522 
00523 
00524   ofs << "GLOBAL" << std::endl;
00525   const JobHistory::PropertyList props = m_jobHistory->properties();
00526   JobHistory::PropertyList::const_iterator itrj;
00527   for (itrj=props.begin(); itrj != props.end(); ++itrj) {
00528     std::string client = itrj->first;
00529     const Property* prp = itrj->second;
00530     ofs << client << "  " << dumpProp(prp) << std::endl;
00531   }
00532 
00533   ofs << std::endl << "SERVICES" << std::endl;
00534   std::map<const IService*, ServiceHistory*>::const_iterator itr_s;
00535   for (itr_s=m_svcmap.begin(); itr_s != m_svcmap.end(); ++itr_s) {
00536     const IService* svc = itr_s->first;
00537 
00538     dumpProperties( *svc, ofs );
00539 
00540   }
00541 
00542   ofs << std::endl << "ALGORITHMS" << std::endl;
00543   std::map<const Algorithm*, AlgorithmHistory*>::const_iterator itr;
00544   for (itr=m_algmap.begin(); itr != m_algmap.end(); ++itr) {
00545     const Algorithm* alg = itr->first;
00546 
00547     dumpProperties( *alg, ofs );
00548 
00549   }
00550 
00551   ofs << std::endl << "ALGTOOLS" << std::endl;
00552   std::map<const AlgTool*, AlgToolHistory*>::const_iterator itr_a;
00553   for (itr_a=m_algtoolmap.begin(); itr_a != m_algtoolmap.end(); ++itr_a) {
00554     const AlgTool* alg = itr_a->first;
00555 
00556     dumpProperties( *alg, ofs );
00557 
00558   }
00559 
00560 
00561 }
00562 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00563 
00564 JobHistory*
00565 HistorySvc::getJobHistory() const {
00566 
00567   return m_jobHistory;
00568 
00569 }
00570 
00571 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00572 IAlgorithm*
00573 HistorySvc::getCurrentIAlg() const {
00574   if (p_algCtxSvc == 0) {
00575     m_log << MSG::WARNING << "trying to create DataHistoryObj before "
00576           << "HistorySvc has been initialized" << endmsg;
00577     return 0;
00578 
00579   } else {
00580     return p_algCtxSvc->currentAlg();
00581   }
00582 }
00583 
00584 
00585 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00586 
00587 DataHistory*
00588 HistorySvc::createDataHistoryObj(const CLID& id, const std::string& key,
00589                                  const std::string& /* storeName */) {
00590 
00591   if (!m_activate) return 0;
00592 
00593 
00594   AlgorithmHistory *algHist;
00595 
00596   IAlgorithm* ialg = getCurrentIAlg();
00597   if (ialg == 0) {
00598 #ifndef NDEBUG
00599     m_log << MSG::DEBUG
00600           << "Could not discover current Algorithm:" << endl
00601           << "          object CLID: " << id << "  key: \"" << key
00602           << "\"" << endmsg;
00603 #endif
00604     algHist = 0;
00605   } else {
00606     Algorithm* alg = dynamic_cast<Algorithm*>(ialg);
00607     if (alg != 0) {
00608       algHist = getAlgHistory( *alg );
00609     } else {
00610       m_log << MSG::WARNING
00611             << "Could not extract concerete Algorithm:"
00612             << endl
00613             << "          object CLID: " << id << "  key: \"" << key
00614             << "\"" << endmsg;
00615       algHist = 0;
00616     }
00617   }
00618 
00619   DataHistory *hist = new DataHistory(id, key, algHist);
00620 
00621   return hist;
00622 
00623 }
00624 
00625 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00626 StatusCode
00627 HistorySvc::registerDataHistory(const CLID& id, const std::string& key,
00628                                 const std::string& storeName) {
00629 
00630   DHH dhh(id,key);
00631 
00632   pair<DHMitr,DHMitr> mitr = m_datMap.equal_range(dhh);
00633 
00634   if (mitr.first == mitr.second) {
00635     // not in the map
00636     DataHistory *dh = createDataHistoryObj(id,key,storeName);
00637     m_datMap.insert(pair<DHH,DataHistory*>(dhh,dh));
00638   } else {
00639     // found at least one
00640     bool match(false);
00641 
00642     std::string algName;
00643     IAlgorithm *ialg = getCurrentIAlg();
00644     if (ialg != 0) {
00645       algName = ialg->name();
00646     } else {
00647       algName = "UNKNOWN";
00648     }
00649 
00650     for (DHMitr itr = mitr.first; itr != mitr.second; ++itr) {
00651       DataHistory *dh = itr->second;
00652       if (dh->algorithmHistory()->algorithm_name() == algName) {
00653         match = true;
00654         break;
00655       }
00656     }
00657 
00658     if (! match) {
00659       DataHistory *dh = createDataHistoryObj(id,key,storeName);
00660       m_datMap.insert(pair<DHH,DataHistory*>(dhh,dh));
00661     }
00662   }
00663 
00664   return StatusCode::SUCCESS;
00665 
00666 }
00667 
00668 
00669 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00670 DataHistory*
00671 HistorySvc::getDataHistory(const CLID& id, const std::string& key,
00672                            const std::string& /*storeName*/) const {
00673 
00674   DHH dhh(id,key);
00675 
00676   pair<DHMCitr,DHMCitr> mitr = m_datMap.equal_range(dhh);
00677 
00678   if(mitr.first == mitr.second) {
00679     return 0;
00680   }
00681 
00682   return mitr.first->second;
00683 
00684 }
00685 
00686 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00687 
00688 int
00689 HistorySvc::getDataHistory(const CLID& id, const std::string& key,
00690                            const std::string& /*storeName*/,
00691                            std::list<DataHistory*>& dhlist) const {
00692 
00693   DHH dhh(id,key);
00694 
00695   int n(0);
00696 
00697   pair<DHMCitr,DHMCitr> mitr = m_datMap.equal_range(dhh);
00698 
00699   for (DHMCitr itr=mitr.first; itr != mitr.second; ++itr) {
00700     dhlist.push_back(itr->second);
00701     n++;
00702   }
00703 
00704   return n;
00705 
00706 }
00707 
00708 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00709 
00710 StatusCode
00711 HistorySvc::registerSvc(const IService &svc) {
00712 
00713   if ( svc.name() == "HistoryStore" ) {
00714     //    m_log << MSG::WARNING << "not registering store" << endmsg;
00715     return StatusCode(StatusCode::SUCCESS,true);
00716   }
00717 
00718   JobHistory *job = getJobHistory();
00719   const IService* psvc = &svc;
00720   map<const IService*, ServiceHistory*>::const_iterator itr =
00721     m_svcmap.find(psvc);
00722   if (itr == m_svcmap.end()) {
00723 
00724 #ifndef NDEBUG
00725     m_log << MSG::DEBUG << "Registering Service: ";
00726     m_log.setColor(MSG::CYAN);
00727     m_log << svc.name() << endmsg;
00728 #endif
00729 
00730     m_svcs.insert(psvc);
00731 
00732     ServiceHistory *svcHist = new ServiceHistory(&svc, job);
00733     m_svcmap[psvc] = svcHist;
00734 
00735     (const_cast<IService*>(psvc))->addRef();
00736 
00737   }
00738 
00739   m_log.resetColor();
00740   return StatusCode(StatusCode::SUCCESS,true);
00741 
00742 }
00743 
00744 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00745 
00746 ServiceHistory*
00747 HistorySvc::getServiceHistory(const IService &svc) const {
00748 
00749   const IService *psvc = &svc;
00750   map<const IService*, ServiceHistory*>::const_iterator itr =
00751     m_svcmap.find(psvc);
00752   if ( itr == m_svcmap.end() ) {
00753     m_log << MSG::WARNING << "Service " << svc.name() << " not registered"
00754           << endmsg;
00755     return 0;
00756   }
00757 
00758   return ( itr->second );
00759 
00760 }
00761 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00762 
00763 void
00764 HistorySvc::getServiceHistory(std::set<ServiceHistory*>& svcs) const {
00765 
00766   set<const IService*>::const_iterator itr;
00767   for (itr=m_svcs.begin(); itr!=m_svcs.end(); ++itr) {
00768     ServiceHistory *sh = m_svcmap.find( *itr )->second;
00769 
00770     svcs.insert(sh);
00771   }
00772 
00773 }
00774 
00775 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00776 
00777 StatusCode
00778 HistorySvc::listProperties(const IService &svc) const {
00779 
00780   m_log << MSG::INFO << "Dumping properties for " << svc.name() << endl;
00781 
00782   ServiceHistory *hist = getServiceHistory( svc );
00783 
00784   if (hist == 0) {
00785     return StatusCode::FAILURE;
00786   }
00787 
00788   ostringstream ost;
00789   ost << *hist;
00790 
00791   std::string str = ost.str();
00792 
00793   m_log << MSG::INFO << svc.name() << " --> " << endl << str << endmsg;
00794 
00795 
00796 
00797   return StatusCode(StatusCode::SUCCESS,true);
00798 
00799 }
00800 
00801 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00802 
00803 void
00804 HistorySvc::dumpProperties(const IService &svc, std::ofstream &ofs) const {
00805 
00806   ServiceHistory *hist = getServiceHistory( svc );
00807 
00808   if (hist == 0) {
00809     return;
00810   }
00811 
00812   ServiceHistory::PropertyList::const_iterator itr;
00813   for (itr=hist->properties().begin(); itr != hist->properties().end();++itr) {
00814     ofs << svc.name() << "  " << dumpProp(*itr) << std::endl;
00815   }
00816 
00817 }
00818 
00819 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00820 
00821 StatusCode
00822 HistorySvc::registerAlgTool(const IAlgTool& ialg) {
00823 
00824   if (! m_isInitialized) {
00825     if (p_algCtxSvc == 0) {
00826       if ( service("AlgContextSvc",p_algCtxSvc,true).isFailure() ) {
00827         m_log << MSG::ERROR << "unable to get the AlgContextSvc" << endmsg;
00828         return StatusCode::FAILURE;
00829       }
00830     }
00831 
00832     m_ialgtools.insert(&ialg);
00833     return StatusCode::SUCCESS;
00834   }
00835 
00836   const AlgTool *alg = dynamic_cast<const AlgTool*>( &ialg );
00837   if ( alg == 0 ) {
00838     m_log << MSG::ERROR << "Could not dcast IAlgTool \"" << ialg.name()
00839           << "\" to an AlgTool" << endmsg;
00840     return StatusCode::FAILURE;
00841   }
00842 
00843   if (m_algtools.find(alg) != m_algtools.end()) {
00844     m_log << MSG::WARNING << "AlgTool " << ialg.name()
00845           << " already registered in HistorySvc" << endmsg;
00846     return StatusCode::SUCCESS;
00847   }
00848 
00849   m_algtools.insert(alg);
00850   (const_cast<AlgTool*>(alg))->addRef();
00851 
00852   const JobHistory *job = getJobHistory();
00853   AlgToolHistory *algHist = new AlgToolHistory(*alg, job);
00854   m_algtoolmap[alg] = algHist;
00855 
00856 #ifndef NDEBUG
00857   m_log << MSG::DEBUG << "Registering algtool: ";
00858   m_log.setColor(MSG::CYAN);
00859   m_log << alg->name() << endmsg;
00860   m_log.resetColor();
00861 #endif
00862 
00863   return StatusCode::SUCCESS;
00864 
00865 }
00866 
00867 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00868 
00869 StatusCode
00870 HistorySvc::listProperties(const IAlgTool& alg) const {
00871 
00872   m_log << MSG::INFO << "Dumping properties for " << alg.name() << endl;
00873 
00874   AlgToolHistory *hist = getAlgToolHistory( alg );
00875 
00876   if (hist == 0) {
00877     return StatusCode::FAILURE;
00878   }
00879 
00880   ostringstream ost;
00881   ost << *hist;
00882 
00883   std::string str = ost.str();
00884 
00885   m_log << MSG::INFO << alg.name() << " --> " << endl << str << endmsg;
00886 
00887   return StatusCode::SUCCESS;
00888 
00889 }
00890 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00891 
00892 void
00893 HistorySvc::dumpProperties(const IAlgTool& alg, std::ofstream &ofs) const {
00894 
00895   AlgToolHistory *hist = getAlgToolHistory( alg );
00896 
00897   if (hist == 0) {
00898     return;
00899   }
00900 
00901   AlgToolHistory::PropertyList::const_iterator itr;
00902   for (itr=hist->properties().begin(); itr!=hist->properties().end(); ++itr) {
00903     ofs << alg.name() << "  " << dumpProp(*itr) << std::endl;
00904   }
00905 
00906 }
00907 
00908 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00909 
00910 AlgToolHistory*
00911 HistorySvc::getAlgToolHistory(const IAlgTool& alg) const {
00912 
00913   const AlgTool *palg = dynamic_cast<const AlgTool*>(&alg);
00914   set<const AlgTool*>::const_iterator itr = m_algtools.find(palg);
00915   if ( itr == m_algtools.end() ) {
00916     m_log << MSG::WARNING << "AlgTool " << alg.name() << " not registered"
00917           << endmsg;
00918     return 0;
00919   }
00920 
00921   map<const AlgTool*, AlgToolHistory*>::const_iterator itr2;
00922   itr2 = m_algtoolmap.find( *itr );
00923 
00924   return ( itr2->second );
00925 
00926 }
00927 
00928 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00929 
00930 void
00931 HistorySvc::getAlgToolHistory(std::set<AlgToolHistory*>& algs) const {
00932 
00933   set<const AlgTool*>::const_iterator itr;
00934   for (itr=m_algtools.begin(); itr!=m_algtools.end(); ++itr) {
00935     AlgToolHistory *ah = m_algtoolmap.find(*itr)->second;
00936 
00937     algs.insert(ah);
00938   }
00939 
00940 }
00941 
00942 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00943 
00944 void
00945 HistorySvc::handle(const Incident& incident) {
00946 
00947   if (incident.type() == IncidentType::BeginEvent) {
00948     if (captureState().isFailure()) {
00949       m_log << MSG::WARNING << "Error capturing state." << endl
00950             << "Will try again at next BeginEvent incident" << endmsg;
00951     }
00952   }
00953 
00954 }
00955 
00956 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00957 
00958 
00959 std::string
00960 HistorySvc::dumpProp(const Property* prop) const {
00961   std::ostringstream ost;
00962   prop->fillStream(ost);
00963   return ost.str();
00964 }
00965 
00966 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
00967 
00968 void
00969 HistorySvc::dumpState(std::ofstream& ofs) const {
00970 
00971 
00972   ofs << "GLOBAL" << std::endl;
00973   const JobHistory::PropertyList props = m_jobHistory->properties();
00974   JobHistory::PropertyList::const_iterator itrj;
00975   for (itrj=props.begin(); itrj != props.end(); ++itrj) {
00976     std::string client = itrj->first;
00977     const Property* prp = itrj->second;
00978     ofs << client << "  " << dumpProp(prp) << std::endl;
00979   }
00980 
00981 
00982   ofs << "SERVICES" << std::endl;
00983   std::map<const IService*, ServiceHistory*>::const_iterator itr_s;
00984   for (itr_s=m_svcmap.begin(); itr_s != m_svcmap.end(); ++itr_s) {
00985     const IService* svc = itr_s->first;
00986 
00987     dumpState( *svc, ofs );
00988 
00989   }
00990 
00991   ofs << "ALGORITHMS" << std::endl;
00992   std::map<const Algorithm*, AlgorithmHistory*>::const_iterator itr;
00993   for (itr=m_algmap.begin(); itr != m_algmap.end(); ++itr) {
00994     const Algorithm* alg = itr->first;
00995 
00996     dumpState( *alg, ofs );
00997 
00998   }
00999 
01000 
01001   ofs << "ALGTOOLS" << std::endl;
01002   std::map<const AlgTool*, AlgToolHistory*>::const_iterator itr_a;
01003   for (itr_a=m_algtoolmap.begin(); itr_a != m_algtoolmap.end(); ++itr_a) {
01004     const AlgTool* alg = itr_a->first;
01005 
01006     dumpState( *alg, ofs );
01007 
01008   }
01009 
01010 }
01011 
01012 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
01013 
01014 void
01015 HistorySvc::dumpState(const Algorithm &alg, std::ofstream& ofs) const {
01016 
01017   AlgorithmHistory *hist = getAlgHistory( alg );
01018 
01019   if (hist == 0) {
01020     return;
01021   }
01022 
01023   ofs << ">> " << alg.name() << endl << *hist << endl;
01024 
01025 }
01026 
01027 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
01028 
01029 void
01030 HistorySvc::dumpState(const IService &svc, std::ofstream& ofs) const {
01031 
01032   ServiceHistory *hist = getServiceHistory( svc );
01033 
01034   if (hist == 0) {
01035     return;
01036   }
01037 
01038   ofs << ">> " << svc.name() << endl << *hist << endl;
01039 
01040 }
01041 
01042 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
01043 
01044 void
01045 HistorySvc::dumpState(const IAlgTool &alg, std::ofstream& ofs) const {
01046 
01047   AlgToolHistory *hist = getAlgToolHistory( alg );
01048 
01049   if (hist == 0) {
01050     return;
01051   }
01052 
01053   ofs << ">> " << alg.name() << endl << *hist << endl;
01054 
01055 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines

Generated at Tue May 10 2011 18:54:02 for Gaudi Framework, version v22r2 by Doxygen version 1.7.2 written by Dimitri van Heesch, © 1997-2004