| # Gaudi Framework, version v21r6 |

Home |  | Generated: 11 Nov 2009

# 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
    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   if (ServiceHandle<IToolSvc>("ToolSvc/ToolSvc",name()).retrieve().isFailure()) {
    00174     m_log << MSG::ERROR << "could not retrieve the ToolSvc handle !"
    00175           << endmsg;
    00176     return StatusCode::FAILURE;
    00177   }
    00178
    00179   // add listener to be triggered by first BeginEvent with low priority
    00180   // so it gets called first
    00181   const bool rethrow = false;
    00182   const bool oneShot = true; // make the listener called only once
    00183   m_incidentSvc->addListener(this,IncidentType::BeginEvent,
    00184                              std::numeric_limits<long>::min(),rethrow,oneShot);
    00185
    00186   m_isInitialized = true;
    00187
    00188   return StatusCode::SUCCESS;
    00189
    00190 }
    00191 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00192
    00193 StatusCode HistorySvc::captureState() {
    00194
    00195   if (m_jobHistory == 0) {
    00196     m_jobHistory = new JobHistory;
    00197     IJobOptionsSvc *jo;
    00198     if (service("JobOptionsSvc",jo).isFailure()) {
    00199        m_log << MSG::ERROR
    00200              << "Could not get jobOptionsSvc - "
    00201              << "not adding properties to JobHistory" << endmsg;
    00202     } else {
    00203
    00204       bool foundAppMgr(false);
    00205
    00206       std::vector<std::string> clients = jo->getClients();
    00207       std::vector<std::string>::const_iterator it;
    00208       std::vector<const Property*>::const_iterator itr;
    00209       for (it=clients.begin(); it!=clients.end(); ++it) {
    00210         if (*it == "ApplicationMgr") {
    00211           foundAppMgr = true;
    00212         }
    00213         const std::vector<const Property*> *props = jo->getProperties(*it);
    00214         for (itr=props->begin(); itr != props->end(); ++itr) {
    00215           m_jobHistory->addProperty( *it, *itr );
    00216         }
    00217       }
    00218
    00219       if (!foundAppMgr) {
    00220         IProperty *ap;
    00221         if (service("ApplicationMgr",ap).isFailure()) {
    00222           m_log << MSG::ERROR << "could not get the ApplicationMgr" << endmsg;
    00223         } else {
    00224           std::vector<Property*>::const_iterator itr2;
    00225           const std::vector<Property*> props = ap->getProperties();
    00226           for (itr2=props.begin(); itr2 != props.end(); ++itr2) {
    00227             m_jobHistory->addProperty( "ApplicationMgr", *itr2 );
    00228           }
    00229         }
    00230       }
    00231
    00232     }
    00233   }
    00234
    00236
    00237   StatusCode sc;
    00238   IAlgManager* algMgr = 0;
    00239   sc = Gaudi::svcLocator()->queryInterface( IAlgManager::interfaceID(),
    00240                                             pp_cast<void>(&algMgr) );
    00241   if (sc.isFailure()) {
    00242     m_log << MSG::ERROR << "Could not get AlgManager" << endmsg;
    00243     return StatusCode::FAILURE;
    00244   } else {
    00245
    00246     std::list<IAlgorithm*> algs;
    00247     algs = algMgr->getAlgorithms();
    00248     std::list<IAlgorithm*>::const_iterator itr;
    00249     for (itr=algs.begin(); itr!=algs.end(); ++itr) {
    00250       Algorithm* alg = dynamic_cast<Algorithm*> (*itr);
    00251       if (alg == 0) {
    00252         m_log << MSG::WARNING << "Algorithm " << (*itr)->name()
    00253               << " does not inherit from Algorithm. Not registering it."
    00254               << endmsg;
    00255       } else {
    00256         registerAlg( *alg ).ignore();
    00257       }
    00258     }
    00259
    00260     m_log << MSG::INFO;
    00261     m_log << "Registered " << algs.size() << " Algorithms" << endmsg;
    00262
    00263   }
    00264
    00266
    00267   m_isInitialized = true;
    00268   std::set<const IAlgTool*>::const_iterator itra;
    00269   for (itra = m_ialgtools.begin(); itra != m_ialgtools.end(); ++itra) {
    00270     (const_cast<IAlgTool*>(*itra))->addRef();
    00271     registerAlgTool(**itra).ignore();
    00272   }
    00273
    00274   m_log << MSG::INFO << "Registered " << m_algtools.size() << " AlgTools"
    00275         << endmsg;
    00276
    00278
    00279   std::list<IService*> svcs = Gaudi::svcLocator()->getServices();
    00280
    00281   std::list<IService*>::const_iterator itrs;
    00282   for (itrs=svcs.begin(); itrs!=svcs.end(); ++itrs) {
    00283     (*itrs)->addRef();
    00284     registerSvc(**itrs).ignore();
    00285   }
    00286
    00287   m_log << MSG::INFO;
    00288   m_log << "Registered " << svcs.size() << " Services" << endmsg;
    00289
    00290   return StatusCode::SUCCESS;
    00291
    00292
    00293 }
    00294
    00295 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00296
    00297 StatusCode HistorySvc::finalize() {
    00298
    00299   if (!m_activate) return StatusCode::SUCCESS;
    00300
    00301   if (m_dump) {
    00302     listProperties().ignore();
    00303   }
    00304
    00305   if (m_outputFile != "") {
    00306     std::ofstream ofs;
    00307     ofs.open(m_outputFile.c_str());
    00308     if (!ofs) {
    00309       m_log << MSG::ERROR << "Unable to open output file \"m_outputFile\""
    00310             << endmsg;
    00311     } else {
    00312
    00313       //      dumpProperties(ofs);
    00314       dumpState(ofs);
    00315
    00316       ofs.close();
    00317     }
    00318   }
    00319
    00320   clearState();
    00321
    00322   StatusCode status = Service::finalize();
    00323
    00324   if ( status.isSuccess() )
    00325     m_log << MSG::INFO << "Service finalised successfully" << endmsg;
    00326
    00327   return status;
    00328
    00329 }
    00330
    00331 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00332
    00333 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00334 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00335
    00336 StatusCode
    00337 HistorySvc::registerAlg(const Algorithm &alg) {
    00338
    00339   JobHistory *job = getJobHistory();
    00340   if (m_algmap.find(&alg) != m_algmap.end()) {
    00341     m_log << MSG::WARNING << "Algorithm " << alg.name()
    00342           << " already registered with HistorySvc" << endmsg;
    00343     return StatusCode::SUCCESS;
    00344   }
    00345
    00346   (const_cast<Algorithm*>(&alg))->addRef();
    00347
    00348   m_algs.insert(&alg);
    00349
    00350   AlgorithmHistory *algHist = new AlgorithmHistory(alg, job);
    00351   m_algmap[&alg] = algHist;
    00352
    00353 #ifndef NDEBUG
    00354   m_log << MSG::DEBUG << "Registering algorithm: ";
    00355   m_log.setColor(MSG::CYAN);
    00356   m_log << alg.name() << endmsg;
    00357   m_log.resetColor();
    00358 #endif
    00359
    00360   return StatusCode(StatusCode::SUCCESS,true);
    00361
    00362 }
    00363 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00364
    00365 StatusCode
    00366 HistorySvc::listProperties(const Algorithm &alg) const {
    00367
    00368   m_log << MSG::INFO << "Dumping properties for " << alg.name() << endl;
    00369
    00370   AlgorithmHistory *hist = getAlgHistory( alg );
    00371
    00372   if (hist == 0) {
    00373     return StatusCode::FAILURE;
    00374   }
    00375
    00376   ostringstream ost;
    00377   ost << *hist;
    00378
    00379   std::string str = ost.str();
    00380
    00381   m_log << MSG::INFO << alg.name() << " --> " << endl << str << endmsg;
    00382
    00383   return StatusCode(StatusCode::SUCCESS,true);
    00384
    00385 }
    00386 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00387
    00388 void
    00389 HistorySvc::dumpProperties(const Algorithm &alg, std::ofstream& ofs) const {
    00390
    00391   AlgorithmHistory *hist = getAlgHistory( alg );
    00392
    00393   if (hist == 0) {
    00394     return;
    00395   }
    00396
    00397   AlgorithmHistory::PropertyList::const_iterator itr;
    00398   for (itr=hist->properties().begin(); itr!=hist->properties().end(); ++itr) {
    00399     ofs << alg.name() << "  " << dumpProp(*itr) << std::endl;
    00400   }
    00401
    00402 }
    00403 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00404
    00405 AlgorithmHistory*
    00406 HistorySvc::getAlgHistory(const Algorithm &alg) const {
    00407
    00408   const Algorithm *palg = &alg;
    00409   set<const Algorithm*>::const_iterator itr = m_algs.find(palg);
    00410   if ( itr == m_algs.end() ) {
    00411     m_log << MSG::WARNING << "Algorithm " << alg.name() << " not registered"
    00412           << endmsg;
    00413     return 0;
    00414   }
    00415
    00416   map<const Algorithm*, AlgorithmHistory*>::const_iterator itr2;
    00417   itr2 = m_algmap.find( *itr );
    00418
    00419   return ( itr2->second );
    00420
    00421 }
    00422 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00423
    00424 void
    00425 HistorySvc::getAlgHistory(std::set<AlgorithmHistory*>& algs) const {
    00426
    00427   set<const Algorithm*>::const_iterator itr;
    00428   for (itr=m_algs.begin(); itr!=m_algs.end(); ++itr) {
    00429     AlgorithmHistory *ah = m_algmap.find(*itr)->second;
    00430
    00431     algs.insert(ah);
    00432   }
    00433
    00434 }
    00435
    00436
    00437 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00438 StatusCode
    00439 HistorySvc::registerJob() {
    00440
    00441
    00442   return StatusCode(StatusCode::SUCCESS,true);
    00443
    00444
    00445 }
    00446 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00447
    00448
    00449 StatusCode
    00450 HistorySvc::listProperties() const {
    00451
    00452   m_log << MSG::INFO;
    00453   m_log.setColor(MSG::CYAN);
    00454   m_log << "Dumping properties for all Algorithms" << endmsg;
    00455
    00456   std::map<const Algorithm*, AlgorithmHistory*>::const_iterator itr;
    00457   for (itr=m_algmap.begin(); itr != m_algmap.end(); ++itr) {
    00458     const Algorithm* alg = itr->first;
    00459
    00460     listProperties( *alg ).ignore();
    00461
    00462   }
    00463
    00464   m_log << MSG::INFO;
    00465   m_log.setColor(MSG::CYAN);
    00466   m_log << "Dumping properties for all AlgTools" << endmsg;
    00467
    00468   std::map<const AlgTool*, AlgToolHistory*>::const_iterator itr_a;
    00469   for (itr_a=m_algtoolmap.begin(); itr_a != m_algtoolmap.end(); ++itr_a) {
    00470     const AlgTool* alg = itr_a->first;
    00471
    00472     listProperties( *alg ).ignore();
    00473
    00474   }
    00475
    00476   m_log << MSG::INFO;
    00477   m_log.setColor(MSG::CYAN);
    00478   m_log << "Dumping properties for all Services" << endmsg;
    00479
    00480   std::map<const IService*, ServiceHistory*>::const_iterator itr_s;
    00481   for (itr_s=m_svcmap.begin(); itr_s != m_svcmap.end(); ++itr_s) {
    00482     const IService* svc = itr_s->first;
    00483
    00484     listProperties( *svc ).ignore();
    00485
    00486   }
    00487
    00488   m_log << MSG::INFO;
    00489   m_log.setColor(MSG::CYAN);
    00490   m_log << "Dumping properties for Job";
    00491   m_log.resetColor();
    00492
    00493   ostringstream ost;
    00494   ost << *m_jobHistory;
    00495   std::string str = ost.str();
    00496
    00497   m_log << std::endl << str << endmsg;
    00498
    00499   return StatusCode(StatusCode::SUCCESS,true);
    00500
    00501 }
    00502 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00503
    00504 void
    00505 HistorySvc::dumpProperties(std::ofstream& ofs) const {
    00506
    00507
    00508   ofs << "GLOBAL" << std::endl;
    00509   const JobHistory::PropertyList props = m_jobHistory->properties();
    00510   JobHistory::PropertyList::const_iterator itrj;
    00511   for (itrj=props.begin(); itrj != props.end(); ++itrj) {
    00512     std::string client = itrj->first;
    00513     const Property* prp = itrj->second;
    00514     ofs << client << "  " << dumpProp(prp) << std::endl;
    00515   }
    00516
    00517   ofs << std::endl << "SERVICES" << std::endl;
    00518   std::map<const IService*, ServiceHistory*>::const_iterator itr_s;
    00519   for (itr_s=m_svcmap.begin(); itr_s != m_svcmap.end(); ++itr_s) {
    00520     const IService* svc = itr_s->first;
    00521
    00522     dumpProperties( *svc, ofs );
    00523
    00524   }
    00525
    00526   ofs << std::endl << "ALGORITHMS" << std::endl;
    00527   std::map<const Algorithm*, AlgorithmHistory*>::const_iterator itr;
    00528   for (itr=m_algmap.begin(); itr != m_algmap.end(); ++itr) {
    00529     const Algorithm* alg = itr->first;
    00530
    00531     dumpProperties( *alg, ofs );
    00532
    00533   }
    00534
    00535   ofs << std::endl << "ALGTOOLS" << std::endl;
    00536   std::map<const AlgTool*, AlgToolHistory*>::const_iterator itr_a;
    00537   for (itr_a=m_algtoolmap.begin(); itr_a != m_algtoolmap.end(); ++itr_a) {
    00538     const AlgTool* alg = itr_a->first;
    00539
    00540     dumpProperties( *alg, ofs );
    00541
    00542   }
    00543
    00544
    00545 }
    00546 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00547
    00548 JobHistory*
    00549 HistorySvc::getJobHistory() const {
    00550
    00551   return m_jobHistory;
    00552
    00553 }
    00554
    00555 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00556 IAlgorithm*
    00557 HistorySvc::getCurrentIAlg() const {
    00558   if (p_algCtxSvc == 0) {
    00559     m_log << MSG::WARNING << "trying to create DataHistoryObj before "
    00560           << "HistorySvc has been initialized" << endmsg;
    00561     return 0;
    00562
    00563   } else {
    00564     return p_algCtxSvc->currentAlg();
    00565   }
    00566 }
    00567
    00568
    00569 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00570
    00571 DataHistory*
    00572 HistorySvc::createDataHistoryObj(const CLID& id, const std::string& key,
    00573                                  const std::string& /* storeName */) {
    00574
    00575   if (!m_activate) return 0;
    00576
    00577
    00578   AlgorithmHistory *algHist;
    00579
    00580   IAlgorithm* ialg = getCurrentIAlg();
    00581   if (ialg == 0) {
    00582 #ifndef NDEBUG
    00583     m_log << MSG::DEBUG
    00584           << "Could not discover current Algorithm:" << endl
    00585           << "          object CLID: " << id << "  key: \"" << key
    00586           << "\"" << endmsg;
    00587 #endif
    00588     algHist = 0;
    00589   } else {
    00590     Algorithm* alg = dynamic_cast<Algorithm*>(ialg);
    00591     if (alg != 0) {
    00592       algHist = getAlgHistory( *alg );
    00593     } else {
    00594       m_log << MSG::WARNING
    00595             << "Could not extract concerete Algorithm:"
    00596             << endl
    00597             << "          object CLID: " << id << "  key: \"" << key
    00598             << "\"" << endmsg;
    00599       algHist = 0;
    00600     }
    00601   }
    00602
    00603   DataHistory *hist = new DataHistory(id, key, algHist);
    00604
    00605   return hist;
    00606
    00607 }
    00608
    00609 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00610 StatusCode
    00611 HistorySvc::registerDataHistory(const CLID& id, const std::string& key,
    00612                                 const std::string& storeName) {
    00613
    00614   DHH dhh(id,key);
    00615
    00616   pair<DHMitr,DHMitr> mitr = m_datMap.equal_range(dhh);
    00617
    00618   if (mitr.first == mitr.second) {
    00619     // not in the map
    00620     DataHistory *dh = createDataHistoryObj(id,key,storeName);
    00621     m_datMap.insert(pair<DHH,DataHistory*>(dhh,dh));
    00622   } else {
    00623     // found at least one
    00624     bool match(false);
    00625
    00626     std::string algName;
    00627     IAlgorithm *ialg = getCurrentIAlg();
    00628     if (ialg != 0) {
    00629       algName = ialg->name();
    00630     } else {
    00631       algName = "UNKNOWN";
    00632     }
    00633
    00634     for (DHMitr itr = mitr.first; itr != mitr.second; ++itr) {
    00635       DataHistory *dh = itr->second;
    00636       if (dh->algorithmHistory()->algorithm_name() == algName) {
    00637         match = true;
    00638         break;
    00639       }
    00640     }
    00641
    00642     if (! match) {
    00643       DataHistory *dh = createDataHistoryObj(id,key,storeName);
    00644       m_datMap.insert(pair<DHH,DataHistory*>(dhh,dh));
    00645     }
    00646   }
    00647
    00648   return StatusCode::SUCCESS;
    00649
    00650 }
    00651
    00652
    00653 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00654 DataHistory*
    00655 HistorySvc::getDataHistory(const CLID& id, const std::string& key,
    00656                            const std::string& /*storeName*/) const {
    00657
    00658   DHH dhh(id,key);
    00659
    00660   pair<DHMCitr,DHMCitr> mitr = m_datMap.equal_range(dhh);
    00661
    00662   if(mitr.first == mitr.second) {
    00663     return 0;
    00664   }
    00665
    00666   return mitr.first->second;
    00667
    00668 }
    00669
    00670 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00671
    00672 int
    00673 HistorySvc::getDataHistory(const CLID& id, const std::string& key,
    00674                            const std::string& /*storeName*/,
    00675                            std::list<DataHistory*>& dhlist) const {
    00676
    00677   DHH dhh(id,key);
    00678
    00679   int n(0);
    00680
    00681   pair<DHMCitr,DHMCitr> mitr = m_datMap.equal_range(dhh);
    00682
    00683   for (DHMCitr itr=mitr.first; itr != mitr.second; ++itr) {
    00684     dhlist.push_back(itr->second);
    00685     n++;
    00686   }
    00687
    00688   return n;
    00689
    00690 }
    00691
    00692 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00693
    00694 StatusCode
    00695 HistorySvc::registerSvc(const IService &svc) {
    00696
    00697   if ( svc.name() == "HistoryStore" ) {
    00698     //    m_log << MSG::WARNING << "not registering store" << endmsg;
    00699     return StatusCode(StatusCode::SUCCESS,true);
    00700   }
    00701
    00702   JobHistory *job = getJobHistory();
    00703   const IService* psvc = &svc;
    00704   map<const IService*, ServiceHistory*>::const_iterator itr =
    00705     m_svcmap.find(psvc);
    00706   if (itr == m_svcmap.end()) {
    00707
    00708 #ifndef NDEBUG
    00709     m_log << MSG::DEBUG << "Registering Service: ";
    00710     m_log.setColor(MSG::CYAN);
    00711     m_log << svc.name() << endmsg;
    00712 #endif
    00713
    00714     m_svcs.insert(psvc);
    00715
    00716     ServiceHistory *svcHist = new ServiceHistory(&svc, job);
    00717     m_svcmap[psvc] = svcHist;
    00718
    00719     (const_cast<IService*>(psvc))->addRef();
    00720
    00721   }
    00722
    00723   m_log.resetColor();
    00724   return StatusCode(StatusCode::SUCCESS,true);
    00725
    00726 }
    00727
    00728 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00729
    00730 ServiceHistory*
    00731 HistorySvc::getServiceHistory(const IService &svc) const {
    00732
    00733   const IService *psvc = &svc;
    00734   map<const IService*, ServiceHistory*>::const_iterator itr =
    00735     m_svcmap.find(psvc);
    00736   if ( itr == m_svcmap.end() ) {
    00737     m_log << MSG::WARNING << "Service " << svc.name() << " not registered"
    00738           << endmsg;
    00739     return 0;
    00740   }
    00741
    00742   return ( itr->second );
    00743
    00744 }
    00745 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00746
    00747 void
    00748 HistorySvc::getServiceHistory(std::set<ServiceHistory*>& svcs) const {
    00749
    00750   set<const IService*>::const_iterator itr;
    00751   for (itr=m_svcs.begin(); itr!=m_svcs.end(); ++itr) {
    00752     ServiceHistory *sh = m_svcmap.find( *itr )->second;
    00753
    00754     svcs.insert(sh);
    00755   }
    00756
    00757 }
    00758
    00759 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00760
    00761 StatusCode
    00762 HistorySvc::listProperties(const IService &svc) const {
    00763
    00764   m_log << MSG::INFO << "Dumping properties for " << svc.name() << endl;
    00765
    00766   ServiceHistory *hist = getServiceHistory( svc );
    00767
    00768   if (hist == 0) {
    00769     return StatusCode::FAILURE;
    00770   }
    00771
    00772   ostringstream ost;
    00773   ost << *hist;
    00774
    00775   std::string str = ost.str();
    00776
    00777   m_log << MSG::INFO << svc.name() << " --> " << endl << str << endmsg;
    00778
    00779
    00780
    00781   return StatusCode(StatusCode::SUCCESS,true);
    00782
    00783 }
    00784
    00785 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00786
    00787 void
    00788 HistorySvc::dumpProperties(const IService &svc, std::ofstream &ofs) const {
    00789
    00790   ServiceHistory *hist = getServiceHistory( svc );
    00791
    00792   if (hist == 0) {
    00793     return;
    00794   }
    00795
    00796   ServiceHistory::PropertyList::const_iterator itr;
    00797   for (itr=hist->properties().begin(); itr != hist->properties().end();++itr) {
    00798     ofs << svc.name() << "  " << dumpProp(*itr) << std::endl;
    00799   }
    00800
    00801 }
    00802
    00803 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00804
    00805 StatusCode
    00806 HistorySvc::registerAlgTool(const IAlgTool& ialg) {
    00807
    00808   if (! m_isInitialized) {
    00809     if (p_algCtxSvc == 0) {
    00810       if ( service("AlgContextSvc",p_algCtxSvc,true).isFailure() ) {
    00811         m_log << MSG::ERROR << "unable to get the AlgContextSvc" << endmsg;
    00812         return StatusCode::FAILURE;
    00813       }
    00814     }
    00815
    00816     m_ialgtools.insert(&ialg);
    00817     return StatusCode::SUCCESS;
    00818   }
    00819
    00820   const AlgTool *alg = dynamic_cast<const AlgTool*>( &ialg );
    00821   if ( alg == 0 ) {
    00822     m_log << MSG::ERROR << "Could not dcast IAlgTool \"" << ialg.name()
    00823           << "\" to an AlgTool" << endmsg;
    00824     return StatusCode::FAILURE;
    00825   }
    00826
    00827   if (m_algtools.find(alg) != m_algtools.end()) {
    00828     m_log << MSG::WARNING << "AlgTool " << ialg.name()
    00829           << " already registered in HistorySvc" << endmsg;
    00830     return StatusCode::SUCCESS;
    00831   }
    00832
    00833   m_algtools.insert(alg);
    00834   (const_cast<AlgTool*>(alg))->addRef();
    00835
    00836   const JobHistory *job = getJobHistory();
    00837   AlgToolHistory *algHist = new AlgToolHistory(*alg, job);
    00838   m_algtoolmap[alg] = algHist;
    00839
    00840 #ifndef NDEBUG
    00841   m_log << MSG::DEBUG << "Registering algtool: ";
    00842   m_log.setColor(MSG::CYAN);
    00843   m_log << alg->name() << endmsg;
    00844   m_log.resetColor();
    00845 #endif
    00846
    00847   return StatusCode::SUCCESS;
    00848
    00849 }
    00850
    00851 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00852
    00853 StatusCode
    00854 HistorySvc::listProperties(const IAlgTool& alg) const {
    00855
    00856   m_log << MSG::INFO << "Dumping properties for " << alg.name() << endl;
    00857
    00858   AlgToolHistory *hist = getAlgToolHistory( alg );
    00859
    00860   if (hist == 0) {
    00861     return StatusCode::FAILURE;
    00862   }
    00863
    00864   ostringstream ost;
    00865   ost << *hist;
    00866
    00867   std::string str = ost.str();
    00868
    00869   m_log << MSG::INFO << alg.name() << " --> " << endl << str << endmsg;
    00870
    00871   return StatusCode::SUCCESS;
    00872
    00873 }
    00874 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00875
    00876 void
    00877 HistorySvc::dumpProperties(const IAlgTool& alg, std::ofstream &ofs) const {
    00878
    00879   AlgToolHistory *hist = getAlgToolHistory( alg );
    00880
    00881   if (hist == 0) {
    00882     return;
    00883   }
    00884
    00885   AlgToolHistory::PropertyList::const_iterator itr;
    00886   for (itr=hist->properties().begin(); itr!=hist->properties().end(); ++itr) {
    00887     ofs << alg.name() << "  " << dumpProp(*itr) << std::endl;
    00888   }
    00889
    00890 }
    00891
    00892 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00893
    00894 AlgToolHistory*
    00895 HistorySvc::getAlgToolHistory(const IAlgTool& alg) const {
    00896
    00897   const AlgTool *palg = dynamic_cast<const AlgTool*>(&alg);
    00898   set<const AlgTool*>::const_iterator itr = m_algtools.find(palg);
    00899   if ( itr == m_algtools.end() ) {
    00900     m_log << MSG::WARNING << "AlgTool " << alg.name() << " not registered"
    00901           << endmsg;
    00902     return 0;
    00903   }
    00904
    00905   map<const AlgTool*, AlgToolHistory*>::const_iterator itr2;
    00906   itr2 = m_algtoolmap.find( *itr );
    00907
    00908   return ( itr2->second );
    00909
    00910 }
    00911
    00912 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00913
    00914 void
    00915 HistorySvc::getAlgToolHistory(std::set<AlgToolHistory*>& algs) const {
    00916
    00917   set<const AlgTool*>::const_iterator itr;
    00918   for (itr=m_algtools.begin(); itr!=m_algtools.end(); ++itr) {
    00919     AlgToolHistory *ah = m_algtoolmap.find(*itr)->second;
    00920
    00921     algs.insert(ah);
    00922   }
    00923
    00924 }
    00925
    00926 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00927
    00928 void
    00929 HistorySvc::handle(const Incident& incident) {
    00930
    00931   if (incident.type() == IncidentType::BeginEvent) {
    00932     if (captureState().isFailure()) {
    00933       m_log << MSG::WARNING << "Error capturing state." << endl
    00934             << "Will try again at next BeginEvent incident" << endmsg;
    00935     }
    00936   }
    00937
    00938 }
    00939
    00940 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00941
    00942
    00943 std::string
    00944 HistorySvc::dumpProp(const Property* prop) const {
    00945   std::ostringstream ost;
    00946   prop->fillStream(ost);
    00947   return ost.str();
    00948 }
    00949
    00950 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00951
    00952 void
    00953 HistorySvc::dumpState(std::ofstream& ofs) const {
    00954
    00955
    00956   ofs << "GLOBAL" << std::endl;
    00957   const JobHistory::PropertyList props = m_jobHistory->properties();
    00958   JobHistory::PropertyList::const_iterator itrj;
    00959   for (itrj=props.begin(); itrj != props.end(); ++itrj) {
    00960     std::string client = itrj->first;
    00961     const Property* prp = itrj->second;
    00962     ofs << client << "  " << dumpProp(prp) << std::endl;
    00963   }
    00964
    00965
    00966   ofs << "SERVICES" << std::endl;
    00967   std::map<const IService*, ServiceHistory*>::const_iterator itr_s;
    00968   for (itr_s=m_svcmap.begin(); itr_s != m_svcmap.end(); ++itr_s) {
    00969     const IService* svc = itr_s->first;
    00970
    00971     dumpState( *svc, ofs );
    00972
    00973   }
    00974
    00975   ofs << "ALGORITHMS" << std::endl;
    00976   std::map<const Algorithm*, AlgorithmHistory*>::const_iterator itr;
    00977   for (itr=m_algmap.begin(); itr != m_algmap.end(); ++itr) {
    00978     const Algorithm* alg = itr->first;
    00979
    00980     dumpState( *alg, ofs );
    00981
    00982   }
    00983
    00984
    00985   ofs << "ALGTOOLS" << std::endl;
    00986   std::map<const AlgTool*, AlgToolHistory*>::const_iterator itr_a;
    00987   for (itr_a=m_algtoolmap.begin(); itr_a != m_algtoolmap.end(); ++itr_a) {
    00988     const AlgTool* alg = itr_a->first;
    00989
    00990     dumpState( *alg, ofs );
    00991
    00992   }
    00993
    00994 }
    00995
    00996 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    00997
    00998 void
    00999 HistorySvc::dumpState(const Algorithm &alg, std::ofstream& ofs) const {
    01000
    01001   AlgorithmHistory *hist = getAlgHistory( alg );
    01002
    01003   if (hist == 0) {
    01004     return;
    01005   }
    01006
    01007   ofs << ">> " << alg.name() << endl << *hist << endl;
    01008
    01009 }
    01010
    01011 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    01012
    01013 void
    01014 HistorySvc::dumpState(const IService &svc, std::ofstream& ofs) const {
    01015
    01016   ServiceHistory *hist = getServiceHistory( svc );
    01017
    01018   if (hist == 0) {
    01019     return;
    01020   }
    01021
    01022   ofs << ">> " << svc.name() << endl << *hist << endl;
    01023
    01024 }
    01025
    01026 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    01027
    01028 void
    01029 HistorySvc::dumpState(const IAlgTool &alg, std::ofstream& ofs) const {
    01030
    01031   AlgToolHistory *hist = getAlgToolHistory( alg );
    01032
    01033   if (hist == 0) {
    01034     return;
    01035   }
    01036
    01037   ofs << ">> " << alg.name() << endl << *hist << endl;
    01038
    01039 }