00001
00002
00003
00004 #include "ApplicationMgr.h"
00005 #include "ServiceManager.h"
00006 #include "AlgorithmManager.h"
00007 #include "DLLClassManager.h"
00008
00009 #include "GaudiKernel/TypeNameString.h"
00010 #include "GaudiKernel/IService.h"
00011 #include "GaudiKernel/IRunable.h"
00012 #include "GaudiKernel/IMessageSvc.h"
00013 #include "GaudiKernel/IJobOptionsSvc.h"
00014
00015 #include "GaudiKernel/SmartIF.h"
00016 #include "GaudiKernel/MsgStream.h"
00017 #include "GaudiKernel/PropertyMgr.h"
00018 #include "GaudiKernel/ObjectFactory.h"
00019
00020 #include "GaudiKernel/GaudiException.h"
00021 #include "GaudiKernel/ThreadGaudi.h"
00022
00023 #include "GaudiKernel/StatusCode.h"
00024 #include "GaudiKernel/Time.h"
00025 #include "GaudiKernel/System.h"
00026 using System::getEnv;
00027 using System::isEnvSet;
00028
00029 #include <algorithm>
00030 #include <cassert>
00031 #include <ctime>
00032 #include <limits>
00033
00034 static const char* s_eventloop = "EventLoop";
00035 static const char* s_runable = "Runable";
00036
00037 DECLARE_OBJECT_FACTORY(ApplicationMgr)
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 ApplicationMgr::ApplicationMgr(IInterface*): base_class() {
00048
00049 addRef();
00050
00051
00052 m_messageSvc = 0;
00053 m_jobOptionsSvc = 0;
00054
00055
00056 m_managers[IService::interfaceID().id()] = new ServiceManager(this);
00057 m_managers[IAlgorithm::interfaceID().id()] = new AlgorithmManager(this);
00058
00059 m_svcLocator = svcManager();
00060
00061
00062
00063 m_classManager = new DLLClassManager(this);
00064 m_propertyMgr = new PropertyMgr(this);
00065
00066 m_name = "ApplicationMgr";
00067 m_state = Gaudi::StateMachine::OFFLINE;
00068 m_targetState = Gaudi::StateMachine::OFFLINE;
00069
00070 m_propertyMgr->declareProperty("Go", m_SIGo = 0 );
00071 m_propertyMgr->declareProperty("Exit", m_SIExit = 0 );
00072 m_propertyMgr->declareProperty("Dlls", m_dllNameList );
00073 m_propertyMgr->declareProperty("ExtSvc", m_extSvcNameList );
00074 m_propertyMgr->declareProperty("CreateSvc", m_createSvcNameList );
00075 m_propertyMgr->declareProperty("ExtSvcCreates", m_extSvcCreates=true );
00076
00077 m_propertyMgr->declareProperty("SvcMapping", m_svcMapping );
00078 m_propertyMgr->declareProperty("SvcOptMapping", m_svcOptMapping );
00079
00080 m_propertyMgr->declareProperty("TopAlg", m_topAlgNameList );
00081 m_propertyMgr->declareProperty("OutStream", m_outStreamNameList );
00082 m_propertyMgr->declareProperty("OutStreamType", m_outStreamType = "OutputStream" );
00083 m_propertyMgr->declareProperty("MessageSvcType",m_messageSvcType= "MessageSvc" );
00084 m_propertyMgr->declareProperty("JobOptionsSvcType",
00085 m_jobOptionsSvcType = "JobOptionsSvc" );
00086 m_propertyMgr->declareProperty( s_runable, m_runableType = "AppMgrRunable");
00087 m_propertyMgr->declareProperty( s_eventloop, m_eventLoopMgr = "EventLoopMgr");
00088
00089 m_propertyMgr->declareProperty("HistogramPersistency", m_histPersName="NONE");
00090
00091
00092 m_propertyMgr->declareProperty("JobOptionsType", m_jobOptionsType = "FILE");
00093 m_propertyMgr->declareProperty("JobOptionsPath", m_jobOptionsPath = "");
00094 m_propertyMgr->declareProperty("EvtMax", m_evtMax = -1);
00095 m_propertyMgr->declareProperty("EvtSel", m_evtsel );
00096 m_propertyMgr->declareProperty("OutputLevel", m_outputLevel = MSG::INFO);
00097
00098 m_propertyMgr->declareProperty("MultiThreadExtSvc", m_multiThreadSvcNameList);
00099 m_propertyMgr->declareProperty("NoOfThreads", m_noOfEvtThreads = 0);
00100 m_propertyMgr->declareProperty("AppName", m_appName = "ApplicationMgr");
00101 m_propertyMgr->declareProperty("AppVersion", m_appVersion = "");
00102
00103 m_propertyMgr->declareProperty("AuditTools", m_auditTools = false);
00104 m_propertyMgr->declareProperty("AuditServices", m_auditSvcs = false);
00105 m_propertyMgr->declareProperty("AuditAlgorithms", m_auditAlgs = false);
00106
00107 m_propertyMgr->declareProperty("ActivateHistory", m_actHistory = false);
00108 m_propertyMgr->declareProperty("StatusCodeCheck", m_codeCheck = false);
00109
00110 m_propertyMgr->declareProperty("Environment", m_environment);
00111
00112
00113 m_propertyMgr->declareProperty("InitializationLoopCheck", m_loopCheck = true)
00114 ->declareUpdateHandler(&ApplicationMgr::initLoopCheckHndlr, this);
00115 svcManager()->setLoopCheckEnabled(m_loopCheck);
00116
00117
00118 m_propertyMgr->declareProperty
00119 ( "PropertiesPrint",
00120 m_propertiesPrint = false,
00121 "Flag to activate the printout of properties" );
00122
00123 m_propertyMgr->declareProperty("ReflexPluginDebugLevel", m_reflexDebugLevel = 0 );
00124
00125 m_propertyMgr->declareProperty("StopOnSignal", m_stopOnSignal = false,
00126 "Flag to enable/disable the signal handler that schedule a stop of the event loop");
00127
00128 m_propertyMgr->declareProperty("StalledEventMonitoring", m_stalledEventMonitoring = false,
00129 "Flag to enable/disable the monitoring and reporting of stalled events");
00130
00131 m_propertyMgr->declareProperty("ReturnCode", m_returnCode = 0,
00132 "Return code of the application. Set internally in case of error conditions.");
00133
00134
00135 m_SIGo.declareUpdateHandler ( &ApplicationMgr::SIGoHandler , this );
00136 m_SIExit.declareUpdateHandler( &ApplicationMgr::SIExitHandler , this );
00137 m_topAlgNameList.declareUpdateHandler(&ApplicationMgr::evtLoopPropertyHandler, this);
00138 m_outStreamNameList.declareUpdateHandler(&ApplicationMgr::evtLoopPropertyHandler, this);
00139 m_outStreamType.declareUpdateHandler(&ApplicationMgr::evtLoopPropertyHandler, this);
00140 m_reflexDebugLevel.declareUpdateHandler(&ApplicationMgr::reflexDebugPropertyHandler, this);
00141 m_svcMapping.push_back("EvtDataSvc/EventDataSvc");
00142 m_svcMapping.push_back("DetDataSvc/DetectorDataSvc");
00143 m_svcMapping.push_back("HistogramSvc/HistogramDataSvc");
00144 m_svcMapping.push_back("HbookCnv::PersSvc/HbookHistSvc");
00145 m_svcMapping.push_back("RootHistCnv::PersSvc/RootHistSvc");
00146 m_svcMapping.push_back("EvtPersistencySvc/EventPersistencySvc");
00147 m_svcMapping.push_back("DetPersistencySvc/DetectorPersistencySvc");
00148 m_svcMapping.push_back("HistogramPersistencySvc/HistogramPersistencySvc");
00149 }
00150
00151
00152
00153
00154 ApplicationMgr::~ApplicationMgr() {
00155 if( m_classManager ) m_classManager->release();
00156 if( m_propertyMgr ) m_propertyMgr->release();
00157 if( m_messageSvc ) m_messageSvc->release();
00158 if( m_jobOptionsSvc ) m_jobOptionsSvc->release();
00159 }
00160
00161
00162
00163
00164 StatusCode ApplicationMgr::queryInterface
00165 ( const InterfaceID& iid ,
00166 void** ppvi )
00167 {
00168 if ( 0 == ppvi ) { return StatusCode::FAILURE ; }
00169
00170
00171 StatusCode sc = base_class::queryInterface(iid,ppvi);
00172 if (sc.isSuccess()) return sc;
00173
00174
00175 if ( ISvcLocator ::interfaceID() . versionMatch ( iid ) )
00176 { return serviceLocator()-> queryInterface ( iid , ppvi ) ; }
00177 else if ( ISvcManager ::interfaceID() . versionMatch ( iid ) )
00178 { return svcManager() -> queryInterface ( iid , ppvi ) ; }
00179 else if ( IAlgManager ::interfaceID() . versionMatch ( iid ) )
00180 { return algManager() -> queryInterface ( iid , ppvi ) ; }
00181 else if ( IClassManager ::interfaceID() . versionMatch ( iid ) )
00182 { return m_classManager -> queryInterface ( iid , ppvi ) ; }
00183 else if ( IProperty ::interfaceID() . versionMatch ( iid ) )
00184 { return m_propertyMgr -> queryInterface ( iid , ppvi ) ; }
00185 else if ( IMessageSvc ::interfaceID() . versionMatch ( iid ) )
00186 {
00187 *ppvi = reinterpret_cast<void*>(m_messageSvc.get());
00188 if (m_messageSvc) {
00189 m_messageSvc->addRef();
00190 }
00191
00192
00193 return StatusCode::SUCCESS;
00194 }
00195 *ppvi = 0;
00196 return StatusCode::FAILURE;
00197 }
00198
00199
00200
00201
00202 StatusCode ApplicationMgr::i_startup() {
00203 StatusCode sc;
00204
00205
00206 m_classManager->loadModule("").ignore();
00207
00208
00209 SmartIF<IService> msgsvc = svcManager()->createService(Gaudi::Utils::TypeNameString("MessageSvc", m_messageSvcType));
00210 if( !msgsvc.isValid() ) {
00211 fatal() << "Error creating MessageSvc of type " << m_messageSvcType << endmsg;
00212 return sc;
00213 }
00214
00215 SmartIF<IService> jobsvc = svcManager()->createService(Gaudi::Utils::TypeNameString("JobOptionsSvc", m_jobOptionsSvcType));
00216 if( !jobsvc.isValid() ) {
00217 fatal() << "Error creating JobOptionsSvc" << endmsg;
00218 return sc;
00219 }
00220
00221 SmartIF<IProperty> jobOptsIProp(jobsvc);
00222 if ( !jobOptsIProp.isValid() ) {
00223 fatal() << "Error locating JobOptionsSvc" << endmsg;
00224 return sc;
00225 }
00226 sc = jobOptsIProp->setProperty( StringProperty("TYPE", m_jobOptionsType) );
00227 if( !sc.isSuccess() ) {
00228 fatal() << "Error setting TYPE option in JobOptionsSvc" << endmsg;
00229 return sc;
00230 }
00231
00232 if ( m_jobOptionsPath != "") {
00233 sc = jobOptsIProp->setProperty( StringProperty("PATH", m_jobOptionsPath) );
00234 if( !sc.isSuccess() ) {
00235 fatal() << "Error setting PATH option in JobOptionsSvc" << endmsg;
00236 return sc;
00237 }
00238 }
00239 else if ( isEnvSet("JOBOPTPATH") ) {
00240 sc = jobOptsIProp->setProperty (StringProperty("PATH",
00241 getEnv("JOBOPTPATH")));
00242 if( !sc.isSuccess() ) {
00243 fatal()
00244 << "Error setting PATH option in JobOptionsSvc from env"
00245 << endmsg;
00246 return sc;
00247 }
00248 }
00249 else {
00250 sc = jobOptsIProp->setProperty (StringProperty("PATH",
00251 "../options/job.opts"));
00252 if( !sc.isSuccess() ) {
00253 fatal()
00254 << "Error setting PATH option in JobOptionsSvc to default"
00255 << endmsg;
00256 return sc;
00257 }
00258 }
00259 jobOptsIProp->release();
00260
00261
00262
00263 SmartIF<IProperty> msgSvcIProp(msgsvc);
00264 msgSvcIProp->setProperty( IntegerProperty("OutputLevel", m_outputLevel)).ignore();
00265 msgSvcIProp->release();
00266
00267 sc = jobsvc->sysInitialize();
00268 if( !sc.isSuccess() ) {
00269 fatal() << "Error initializing JobOptionsSvc" << endmsg;
00270 return sc;
00271 }
00272 sc = msgsvc->sysInitialize();
00273 if( !sc.isSuccess() ) {
00274 fatal() << "Error initializing MessageSvc" << endmsg;
00275 return sc;
00276 }
00277
00278
00279 m_messageSvc = m_svcLocator->service("MessageSvc");
00280 if( !m_messageSvc.isValid() ) {
00281 fatal() << "Error retrieving MessageSvc." << endmsg;
00282 return sc;
00283 }
00284 m_jobOptionsSvc = m_svcLocator->service("JobOptionsSvc");
00285 if( !m_jobOptionsSvc.isValid() ) {
00286 fatal() << "Error retrieving JobOptionsSvc." << endmsg;
00287 return sc;
00288 }
00289
00290 return sc;
00291 }
00292
00293
00294
00295
00296 StatusCode ApplicationMgr::configure() {
00297
00298 MsgStream tlog( m_messageSvc, name() );
00299 if( Gaudi::StateMachine::CONFIGURED == m_state ) {
00300 tlog << MSG::INFO << "Already Configured" << endmsg;
00301 return StatusCode::SUCCESS;
00302 }
00303 else if( Gaudi::StateMachine::OFFLINE != m_state ) {
00304 tlog << MSG::FATAL
00305 << "configure: Invalid state \"" << m_state << "\"" << endmsg;
00306 return StatusCode::FAILURE;
00307 }
00308 m_targetState = Gaudi::StateMachine::CONFIGURED;
00309
00310 StatusCode sc;
00311 sc = i_startup();
00312 if ( !sc.isSuccess() ) {
00313 return sc;
00314 }
00315
00316 MsgStream log( m_messageSvc, name() );
00317
00318
00319 log << MSG::DEBUG << "Getting my own properties" << endmsg;
00320 sc = m_jobOptionsSvc->setMyProperties( name(), m_propertyMgr );
00321 if( !sc.isSuccess() ) {
00322 log << MSG::WARNING << "Problems getting my properties from JobOptionsSvc"
00323 << endmsg;
00324 return sc;
00325 }
00326
00327
00328 if( m_outputLevel != MSG::NIL && !m_appName.empty() ) {
00329 assert(m_messageSvc != 0);
00330 m_messageSvc->setOutputLevel( name(), m_outputLevel );
00331
00332 log << MSG::ALWAYS
00333 << std::endl
00334 << "=================================================================="
00335 << "=================================================================="
00336 << std::endl
00337 << " "
00338 << " Welcome to " << m_appName;
00339
00340 if( "" != m_appVersion ) {
00341 log << MSG::ALWAYS << " version " << m_appVersion;
00342 }
00343 else {
00344 log << MSG::ALWAYS << " $Revision: 1.77 $";
00345 }
00346
00347
00348 log << MSG::ALWAYS
00349 << std::endl
00350 << " "
00351 << " running on " << System::hostName()
00352 << " on " << Gaudi::Time::current().format(true) << std::endl
00353 << "=================================================================="
00354 << "=================================================================="
00355 << endmsg;
00356 }
00357
00358
00359 if ( m_propertiesPrint )
00360 {
00361 typedef std::vector<Property*> Properties;
00362 const Properties& properties = m_propertyMgr->getProperties() ;
00363 log << MSG::ALWAYS
00364 << "List of ALL properties of "
00365 << System::typeinfoName ( typeid( *this ) ) << "/" << this->name()
00366 << " #properties = " << properties.size() << endmsg ;
00367 for ( Properties::const_iterator property = properties.begin() ;
00368 properties.end() != property ; ++property )
00369 { log << "Property ['Name': Value] = " << ( **property) << endmsg ; }
00370 }
00371
00372
00373 if (m_codeCheck) {
00374 StatusCode::enableChecking();
00375 sc = addMultiSvc("StatusCodeSvc", -9999);
00376 if ( sc.isFailure() ) {
00377 log << MSG::FATAL << "Error adding StatusCodeSvc for multiple threads" << endmsg;
00378 return StatusCode::FAILURE;
00379 }
00380 } else {
00381 StatusCode::disableChecking();
00382 }
00383
00384
00385 std::map<std::string,std::string>::iterator var;
00386 for ( var = m_environment.begin(); var != m_environment.end(); ++var ) {
00387 const std::string &name = var->first;
00388 const std::string &value = var->second;
00389 std::string old = System::getEnv(name.c_str());
00390 if ( !old.empty() && (old != "UNKNOWN" )) {
00391 log << MSG::WARNING;
00392 }
00393 else {
00394 log << MSG::DEBUG;
00395 }
00396 log << "Setting " << name << " = " << value << endmsg;
00397 System::setEnv(name,value);
00398 }
00399
00400
00401 VectorName::const_iterator j;
00402 for(j=m_svcMapping.begin(); j != m_svcMapping.end(); ++j) {
00403 Gaudi::Utils::TypeNameString itm(*j);
00404 if ( declareMultiSvcType(itm.name(), itm.type()).isFailure() ) {
00405 log << MSG::ERROR << "configure: declaring svc type:'" << *j << "' failed." << endmsg;
00406 return StatusCode::FAILURE;
00407 }
00408 }
00409 for(j=m_svcOptMapping.begin(); j != m_svcOptMapping.end(); ++j) {
00410 Gaudi::Utils::TypeNameString itm(*j);
00411 if ( declareMultiSvcType(itm.name(), itm.type()).isFailure() ) {
00412 log << MSG::ERROR << "configure: declaring svc type:'" << *j << "' failed." << endmsg;
00413 return StatusCode::FAILURE;
00414 }
00415 }
00416
00417
00418
00419 sc = decodeDllNameList( );
00420 if ( sc.isFailure( ) ) {
00421 log << MSG::ERROR << "Failure loading declared DLL's" << endmsg;
00422 return sc;
00423 }
00424
00425
00426
00427 sc = decodeExtSvcNameList();
00428 if ( sc.isFailure( ) ) {
00429 log << MSG::ERROR << "Failure during external service association" << endmsg;
00430 return sc;
00431 }
00432
00433 sc = decodeMultiThreadSvcNameList( );
00434 if ( sc.isFailure( ) ) {
00435 log << MSG::ERROR << "Failure during multi thread service creation"
00436 << endmsg;
00437 return sc;
00438 }
00439
00440 sc = decodeCreateSvcNameList();
00441 if ( sc.isFailure( ) ) {
00442 log << MSG::ERROR << "Failure during external service creation" << endmsg;
00443 return sc;
00444 }
00445
00446
00447
00448
00449
00450 Gaudi::Utils::TypeNameString evtloop_item(m_eventLoopMgr);
00451 sc = addMultiSvc(evtloop_item, 100);
00452 if( !sc.isSuccess() ) {
00453 log << MSG::FATAL << "Error adding :" << m_eventLoopMgr << endmsg;
00454 return sc;
00455 }
00456
00457 if (m_noOfEvtThreads == 0) {
00458 m_runable = m_svcLocator->service(m_runableType);
00459 if( !m_runable.isValid() ) {
00460 log << MSG::FATAL
00461 << "Error retrieving Runable:" << m_runableType
00462 << "\n Check option ApplicationMgr." << s_runable << endmsg;
00463 return sc;
00464 }
00465 m_processingMgr = m_svcLocator->service(evtloop_item);
00466 if( !m_processingMgr.isValid() ) {
00467 log << MSG::FATAL
00468 << "Error retrieving Processing manager:" << m_eventLoopMgr
00469 << "\n Check option ApplicationMgr." << s_eventloop
00470 << "\n No events will be processed." << endmsg;
00471 return sc;
00472 }
00473 }
00474
00475
00476 m_extSvcNameList.declareUpdateHandler (&ApplicationMgr::extSvcNameListHandler,
00477 this);
00478 m_createSvcNameList.declareUpdateHandler (&ApplicationMgr::createSvcNameListHandler,
00479 this);
00480 m_multiThreadSvcNameList.declareUpdateHandler
00481 (&ApplicationMgr::multiThreadSvcNameListHandler, this);
00482 m_dllNameList.declareUpdateHandler (&ApplicationMgr::dllNameListHandler,
00483 this );
00484
00485 if (m_actHistory) {
00486
00487 sc = svcManager()->addService("HistorySvc",std::numeric_limits<int>::max());
00488 if ( sc.isFailure() ) {
00489 log << MSG::FATAL << "Error adding HistorySvc" << endmsg;
00490 return StatusCode::FAILURE;
00491 }
00492
00493 if (m_noOfEvtThreads > 0) {
00494 sc = addMultiSvc("HistorySvc",std::numeric_limits<int>::max());
00495 if ( sc.isFailure() ) {
00496 log << MSG::FATAL << "Error adding HistorySvc for multiple threads"
00497 << endmsg;
00498 return StatusCode::FAILURE;
00499 }
00500 }
00501 }
00502
00503 log << MSG::INFO << "Application Manager Configured successfully" << endmsg;
00504 m_state = m_targetState;
00505 return StatusCode::SUCCESS;
00506 }
00507
00508
00509
00510
00511 StatusCode ApplicationMgr::initialize() {
00512
00513 MsgStream log( m_messageSvc, name() );
00514 StatusCode sc;
00515
00516
00517
00518 if (m_stopOnSignal) {
00519
00520 std::string svcname("Gaudi::Utils::StopSignalHandler");
00521 sc = svcManager()->addService(svcname);
00522 if ( sc.isFailure() ) {
00523 log << MSG::INFO << "Cannot instantiate " << svcname << "signals will be ignored" << endmsg;
00524 }
00525 }
00526
00527 if (m_stalledEventMonitoring) {
00528
00529 std::string svcname("StalledEventMonitor");
00530 sc = svcManager()->addService(svcname);
00531 if ( sc.isFailure() ) {
00532 log << MSG::INFO << "Cannot instantiate " << svcname << "signals will be ignored" << endmsg;
00533 }
00534 }
00535
00536 if( m_state == Gaudi::StateMachine::INITIALIZED ) {
00537 log << MSG::INFO << "Already Initialized!" << endmsg;
00538 return StatusCode::SUCCESS;
00539 }
00540 else if( m_state != Gaudi::StateMachine::CONFIGURED ) {
00541 log << MSG::FATAL
00542 << "initialize: Invalid state \"" << m_state << "\"" << endmsg;
00543 return StatusCode::FAILURE;
00544 }
00545 m_targetState = Gaudi::StateMachine::INITIALIZED;
00546
00547
00548
00549
00550 sc = svcManager()->initialize();
00551 if( !sc.isSuccess() ) return sc;
00552
00553
00554
00555
00556 log << MSG::INFO << "Application Manager Initialized successfully" << endmsg;
00557 m_state = m_targetState;
00558
00559 return sc;
00560 }
00561
00562
00563
00564
00565 StatusCode ApplicationMgr::start() {
00566
00567 MsgStream log( m_messageSvc, name() );
00568 StatusCode sc;
00569
00570 if( m_state == Gaudi::StateMachine::RUNNING ) {
00571 log << MSG::INFO << "Already Initialized!" << endmsg;
00572 return StatusCode::SUCCESS;
00573 }
00574 else if( m_state != Gaudi::StateMachine::INITIALIZED ) {
00575 log << MSG::FATAL
00576 << "start: Invalid state \"" << m_state << "\"" << endmsg;
00577 return StatusCode::FAILURE;
00578 }
00579 m_targetState = Gaudi::StateMachine::RUNNING;
00580
00581
00582
00583
00584 sc = svcManager()->start();
00585 if( !sc.isSuccess() ) return sc;
00586
00587
00588
00589
00590 log << MSG::INFO << "Application Manager Started successfully" << endmsg;
00591 m_state = m_targetState;
00592
00593 return sc;
00594 }
00595
00596
00597
00598
00599 StatusCode ApplicationMgr::nextEvent(int maxevt) {
00600 if( m_state != Gaudi::StateMachine::RUNNING ) {
00601 MsgStream log( m_messageSvc, name() );
00602 log << MSG::FATAL << "nextEvent: Invalid state \"" << m_state << "\""
00603 << endmsg;
00604 return StatusCode::FAILURE;
00605 }
00606 if (!m_processingMgr.isValid()) {
00607 MsgStream log( m_messageSvc, name() );
00608 log << MSG::FATAL << "No event processing manager specified. Check option:"
00609 << s_eventloop << endmsg;
00610 return StatusCode::FAILURE;
00611 }
00612 return m_processingMgr->nextEvent(maxevt);
00613 }
00614
00615
00616
00617
00618 StatusCode ApplicationMgr::stop() {
00619
00620 MsgStream log( m_messageSvc, name() );
00621 StatusCode sc;
00622
00623 if( m_state == Gaudi::StateMachine::INITIALIZED ) {
00624 log << MSG::INFO << "Already Initialized!" << endmsg;
00625 return StatusCode::SUCCESS;
00626 }
00627 else if( m_state != Gaudi::StateMachine::RUNNING ) {
00628 log << MSG::FATAL
00629 << "stop: Invalid state \"" << m_state << "\"" << endmsg;
00630 return StatusCode::FAILURE;
00631 }
00632 m_targetState = Gaudi::StateMachine::INITIALIZED;
00633
00634
00635 sc = algManager()->stop();
00636 if( !sc.isSuccess() ) return sc;
00637
00638
00639
00640
00641 sc = svcManager()->stop();
00642 if( !sc.isSuccess() ) return sc;
00643
00644
00645
00646
00647 log << MSG::INFO << "Application Manager Stopped successfully" << endmsg;
00648 m_state = m_targetState;
00649
00650 return sc;
00651 }
00652
00653
00654
00655
00656 StatusCode ApplicationMgr::finalize() {
00657 MsgStream log( m_messageSvc, name() );
00658 if( m_state == Gaudi::StateMachine::CONFIGURED ) {
00659 log << MSG::INFO << "Already Finalized" << endmsg;
00660 return StatusCode::SUCCESS;
00661 }
00662 else if( m_state != Gaudi::StateMachine::INITIALIZED ) {
00663 log << MSG::FATAL << "finalize: Invalid state \"" << m_state << "\""
00664 << endmsg;
00665 return StatusCode::FAILURE;
00666 }
00667 m_targetState = Gaudi::StateMachine::CONFIGURED;
00668
00669
00670 m_svcLocator->service<IProperty>("MessageSvc")->setProperty(BooleanProperty("enableSuppression", false)).ignore();
00671
00672
00673 StatusCode sc = algManager()->finalize();
00674
00675
00676
00677 sc = svcManager()->finalize();
00678
00679
00680
00681
00682 if (m_codeCheck) {
00683 StatusCode::disableChecking();
00684 }
00685
00686 log << MSG::INFO << "Application Manager Finalized successfully" << endmsg;
00687
00688 m_state = m_targetState;
00689 return sc;
00690 }
00691
00692
00693
00694
00695 StatusCode ApplicationMgr::terminate() {
00696 MsgStream log( m_messageSvc, name() );
00697
00698 if( m_state == Gaudi::StateMachine::OFFLINE ) {
00699 log << MSG::INFO << "Already Offline" << endmsg;
00700 return StatusCode::SUCCESS;
00701 }
00702 else if( m_state != Gaudi::StateMachine::CONFIGURED ) {
00703 log << MSG::FATAL << "terminate: Invalid state \"" << m_state << "\""
00704 << endmsg;
00705 return StatusCode::FAILURE;
00706 }
00707
00708 m_targetState = Gaudi::StateMachine::OFFLINE;
00709
00710
00711 log << MSG::INFO << "Application Manager Terminated successfully" << endmsg;
00712
00713 {
00714 SmartIF<IProperty> prop(m_messageSvc);
00715 if (prop.isValid()) {
00716 prop->setProperty(BooleanProperty("AuditFinalize", false));
00717 }
00718 }
00719 {
00720 SmartIF<IProperty> prop(m_jobOptionsSvc);
00721 if (prop.isValid()) {
00722 prop->setProperty(BooleanProperty("AuditFinalize", false));
00723 }
00724 }
00725
00726
00727 SmartIF<IService> svc(m_messageSvc);
00728 if ( !svc.isValid() ) {
00729 log << MSG::ERROR << "Could not get the IService interface of the MessageSvc" << endmsg;
00730 } else {
00731 svc->sysFinalize().ignore();
00732 }
00733
00734
00735 svc = m_jobOptionsSvc;
00736 if ( !svc.isValid() ) {
00737 log << MSG::ERROR << "Could not get the IService interface of the JobOptionsSvc" << endmsg;
00738 } else {
00739 svc->sysFinalize().ignore();
00740 }
00741
00742 m_state = m_targetState;
00743 return StatusCode::SUCCESS;
00744 }
00745
00746
00747
00748
00749 StatusCode ApplicationMgr::GoToState(Gaudi::StateMachine::State state, bool ignoreFailures) {
00750 StatusCode sc = StatusCode(StatusCode::SUCCESS,true);
00751
00752 switch (state) {
00753
00754 case Gaudi::StateMachine::OFFLINE:
00755 switch (m_state) {
00756 case Gaudi::StateMachine::OFFLINE : return StatusCode::SUCCESS; break;
00757 case Gaudi::StateMachine::CONFIGURED : return terminate(); break;
00758 default:
00759 sc = GoToState(Gaudi::StateMachine::CONFIGURED);
00760 if (sc.isSuccess()) {
00761 return terminate();
00762 } break;
00763 } break;
00764
00765 case Gaudi::StateMachine::CONFIGURED:
00766 switch (m_state) {
00767 case Gaudi::StateMachine::CONFIGURED : return StatusCode::SUCCESS; break;
00768 case Gaudi::StateMachine::OFFLINE : return configure(); break;
00769 case Gaudi::StateMachine::INITIALIZED : return finalize(); break;
00770 default:
00771 sc = GoToState(Gaudi::StateMachine::INITIALIZED);
00772 if (sc.isSuccess()) {
00773 return finalize();
00774 } break;
00775 } break;
00776
00777 case Gaudi::StateMachine::INITIALIZED:
00778 switch (m_state) {
00779 case Gaudi::StateMachine::INITIALIZED : return StatusCode::SUCCESS; break;
00780 case Gaudi::StateMachine::CONFIGURED : return initialize(); break;
00781 case Gaudi::StateMachine::RUNNING : return stop(); break;
00782 default:
00783 sc = GoToState(Gaudi::StateMachine::CONFIGURED);
00784 if (sc.isSuccess()) {
00785 return initialize();
00786 } break;
00787 } break;
00788
00789 case Gaudi::StateMachine::RUNNING:
00790 switch (m_state) {
00791 case Gaudi::StateMachine::RUNNING : return StatusCode::SUCCESS; break;
00792 case Gaudi::StateMachine::INITIALIZED : return start(); break;
00793 default:
00794 sc = GoToState(Gaudi::StateMachine::INITIALIZED);
00795 if (sc.isSuccess()) {
00796 return start();
00797 } break;
00798 } break;
00799
00800 }
00801
00802
00803
00804 if (ignoreFailures){
00805
00806 m_state = state;
00807 return StatusCode::SUCCESS;
00808 }
00809
00810 return sc;
00811 }
00812
00813
00814
00815
00816 StatusCode ApplicationMgr::run() {
00817 StatusCode sc = StatusCode::SUCCESS;
00818
00819 sc = GoToState(Gaudi::StateMachine::RUNNING);
00820 if ( sc.isSuccess() ) {
00821 MsgStream log(m_messageSvc, name());
00822 if ( m_runable != 0 ) {
00823 sc = m_runable->run();
00824 if ( !sc.isSuccess() ) {
00825 log << MSG::FATAL << "Application execution failed. Ending the job."
00826 << endmsg;
00827 }
00828 } else {
00829 log << MSG::FATAL << "Application has no runable object. Check option:"
00830 << s_runable << endmsg;
00831 }
00832 }
00833 if (sc.isSuccess()) {
00834 sc = GoToState(Gaudi::StateMachine::OFFLINE);
00835 }
00836
00837 if (sc.isFailure()) {
00838 GoToState(Gaudi::StateMachine::OFFLINE,true).ignore();
00839 }
00840 return sc;
00841 }
00842
00843
00844
00845
00846 StatusCode ApplicationMgr::executeEvent(void* par) {
00847 MsgStream log( m_messageSvc, name() );
00848 if( m_state == Gaudi::StateMachine::RUNNING ) {
00849 if ( m_processingMgr.isValid() ) {
00850 return m_processingMgr->executeEvent(par);
00851 }
00852 }
00853 log << MSG::FATAL << "executeEvent: Invalid state \"" << FSMState() << "\""
00854 <<endmsg;
00855 return StatusCode::FAILURE;
00856 }
00857
00858
00859
00860
00861 StatusCode ApplicationMgr::executeRun(int evtmax) {
00862 MsgStream log( m_messageSvc, name() );
00863 if( m_state == Gaudi::StateMachine::RUNNING ) {
00864 if ( m_processingMgr.isValid() ) {
00865 return m_processingMgr->executeRun(evtmax);
00866 }
00867 log << MSG::WARNING << "No EventLoop Manager specified " << endmsg;
00868 return StatusCode::SUCCESS;
00869 }
00870 log << MSG::FATAL << "executeRun: Invalid state \"" << FSMState() << "\""
00871 << endmsg;
00872 return StatusCode::FAILURE;
00873 }
00874
00875
00876
00877
00878 StatusCode ApplicationMgr::stopRun() {
00879 MsgStream log( m_messageSvc, name() );
00880 if( m_state == Gaudi::StateMachine::RUNNING ) {
00881 if ( m_processingMgr.isValid() ) {
00882 return m_processingMgr->stopRun();
00883 }
00884 log << MSG::WARNING << "No EventLoop Manager specified " << endmsg;
00885 return StatusCode::SUCCESS;
00886 }
00887 log << MSG::FATAL << "stopRun: Invalid state \"" << FSMState() << "\""
00888 << endmsg;
00889 return StatusCode::FAILURE;
00890 }
00891
00892 const std::string& ApplicationMgr::name() const {
00893 return m_name;
00894 }
00895
00896
00897 Gaudi::StateMachine::State ApplicationMgr::FSMState( ) const {
00898 return m_state;
00899 }
00900
00901 Gaudi::StateMachine::State ApplicationMgr::targetFSMState( ) const {
00902 return m_targetState;
00903 }
00904
00905
00906
00907
00908
00909 StatusCode ApplicationMgr::reinitialize() {
00910 StatusCode retval = StatusCode::SUCCESS;
00911 StatusCode sc;
00912 if ( m_state < Gaudi::StateMachine::INITIALIZED ) {
00913 throw GaudiException("Cannot reinitialize application if not INITIALIZED or RUNNING",
00914 "ApplicationMgr::reinitialize", StatusCode::FAILURE);
00915 }
00916 if ( m_state == Gaudi::StateMachine::RUNNING ) {
00917 retval = GoToState(Gaudi::StateMachine::INITIALIZED);
00918 }
00919 sc = svcManager()->reinitialize();
00920 if (sc.isFailure()) retval = sc;
00921 sc = algManager()->reinitialize();
00922 if (sc.isFailure()) retval = sc;
00923 return retval;
00924 }
00925
00926
00927
00928
00929 StatusCode ApplicationMgr::restart() {
00930 StatusCode retval = StatusCode::SUCCESS;
00931 StatusCode sc;
00932 if ( m_state != Gaudi::StateMachine::RUNNING ) {
00933 throw GaudiException("Cannot restart application if not RUNNING",
00934 "ApplicationMgr::restart", StatusCode::FAILURE);
00935 }
00936 sc = svcManager()->restart();
00937 if (sc.isFailure()) retval = sc;
00938 sc = algManager()->restart();
00939 if (sc.isFailure()) retval = sc;
00940 return retval;
00941 }
00942
00943
00944
00945
00946 void ApplicationMgr::SIGoHandler( Property& ) {
00947
00948 MsgStream log (m_messageSvc, name());
00949 StatusCode sc;
00950
00951
00952 sc = reinitialize();
00953
00954 executeRun(m_evtMax);
00955
00956 return;
00957 }
00958
00959
00960
00961
00962 void ApplicationMgr::SIExitHandler( Property& ) {
00963 StatusCode status;
00964 status = finalize();
00965 status = terminate();
00966 ::exit( 0 );
00967 }
00968
00969
00970
00971
00972 void ApplicationMgr::evtLoopPropertyHandler( Property& p ) {
00973 if ( m_processingMgr.isValid() ) {
00974 SmartIF<IProperty> props(m_processingMgr);
00975 if ( props.isValid() ) {
00976 props->setProperty( p ).ignore();
00977 }
00978 }
00979 }
00980
00981
00982
00983
00984 void ApplicationMgr::createSvcNameListHandler( Property& ) {
00985 if ( !(decodeCreateSvcNameList()).isSuccess() ) {
00986 throw GaudiException("Failed to create ext services",
00987 "MinimalEventLoopMgr::createSvcNameListHandler",
00988 StatusCode::FAILURE);
00989 }
00990 }
00991
00992
00993
00994 StatusCode ApplicationMgr::decodeCreateSvcNameList() {
00995 StatusCode result = StatusCode::SUCCESS;
00996 const std::vector<std::string>& theNames = m_createSvcNameList.value( );
00997 VectorName::const_iterator it(theNames.begin());
00998 VectorName::const_iterator et(theNames.end());
00999 while(result.isSuccess() && it != et) {
01000 Gaudi::Utils::TypeNameString item(*it++);
01001 if( (result = svcManager()->addService(item, 10) ).isFailure()) {
01002 MsgStream log( m_messageSvc, m_name );
01003 log << MSG::ERROR << "decodeCreateSvcNameList: Cannot create service "
01004 << item.type() << "/" << item.name() << endmsg;
01005 } else {
01006 MsgStream log( m_messageSvc, m_name );
01007 log << MSG::DEBUG << "decodeCreateSvcNameList: Created service "
01008 << item.type() << "/" << item.name() << endmsg;
01009 }
01010 }
01011 return result;
01012 }
01013
01014
01015
01016
01017 void ApplicationMgr::extSvcNameListHandler( Property& ) {
01018 if ( !(decodeExtSvcNameList( )).isSuccess() ) {
01019 throw GaudiException("Failed to declare ext services",
01020 "MinimalEventLoopMgr::extSvcNameListHandler",
01021 StatusCode::FAILURE);
01022 }
01023 }
01024
01025
01026
01027
01028 StatusCode ApplicationMgr::decodeExtSvcNameList( ) {
01029 StatusCode result = StatusCode::SUCCESS;
01030
01031 std::vector<std::string> theNames = m_extSvcNameList.value( );
01032
01033 VectorName::const_iterator it(theNames.begin());
01034 VectorName::const_iterator et(theNames.end());
01035 while(result.isSuccess() && it != et) {
01036 Gaudi::Utils::TypeNameString item(*it++);
01037 if (m_extSvcCreates == true) {
01038 if ( (result = svcManager()->addService(item, 10)).isFailure()) {
01039 MsgStream log( m_messageSvc, m_name );
01040 log << MSG::ERROR << "decodeExtSvcNameList: Cannot create service "
01041 << item.type() << "/" << item.name() << endmsg;
01042 }
01043 } else {
01044 if( ( result = svcManager()->declareSvcType(item.name(),
01045 item.type()) ).isFailure()) {
01046 MsgStream log( m_messageSvc, m_name );
01047 log << MSG::ERROR << "decodeExtSvcNameList: Cannot declare service "
01048 << item.type() << "/" << item.name() << endmsg;
01049 }
01050 }
01051 }
01052 return result;
01053 }
01054
01055
01056
01057
01058 void ApplicationMgr::multiThreadSvcNameListHandler( Property& ) {
01059 if ( !(decodeMultiThreadSvcNameList( )).isSuccess() ) {
01060 throw GaudiException("Failed to create copies of mt services",
01061 "MinimalEventLoopMgr::multiThreadSvcNameListHandler",
01062 StatusCode::FAILURE);
01063 }
01064
01065 }
01066
01067
01068
01069
01070 StatusCode ApplicationMgr::decodeMultiThreadSvcNameList( ) {
01071 StatusCode result = StatusCode::SUCCESS;
01072 const std::vector<std::string>& theNames = m_multiThreadSvcNameList.value( );
01073 for(int iCopy=0; iCopy<m_noOfEvtThreads; ++iCopy) {
01074 for (VectorName::const_iterator it = theNames.begin();
01075 it != theNames.end();
01076 ++it) {
01077 Gaudi::Utils::TypeNameString item(*it);
01078 result = addMultiSvc(item, 10);
01079
01080 if( result.isFailure() ) {
01081 MsgStream log( m_messageSvc, m_name );
01082 log << MSG::ERROR
01083 << "decodeMultiThreadSvcNameList: Cannot create service "
01084 << item.type() << "/" << item.name() << endmsg;
01085 } else {
01086 MsgStream log( m_messageSvc, m_name );
01087 log << MSG::VERBOSE
01088 << "decodeMultiThreadSvcNameList: created service "
01089 << item.type() << "/" << item.name() << endmsg;
01090 }
01091 }
01092 }
01093 return result;
01094 }
01095
01096
01097
01098 StatusCode ApplicationMgr::declareMultiSvcType(const std::string& name,
01099 const std::string& type) {
01100 StatusCode result = StatusCode::SUCCESS;
01101 MsgStream log( m_messageSvc, m_name );
01102 if (0 == m_noOfEvtThreads) {
01103 result = svcManager()->declareSvcType(name, type);
01104 if( result.isFailure() ) {
01105 log << MSG::ERROR << "declareMultiSvcType: Cannot declare service "
01106 << type << "/" << name << endmsg;
01107 } else {
01108 log << MSG::VERBOSE << "declareMultiSvcType: declared service "
01109 << type << "/" << name << endmsg;
01110 }
01111 } else {
01112 for(int iCopy=0; iCopy<m_noOfEvtThreads; ++iCopy) {
01113 std::string thrName(name + getGaudiThreadIDfromID(iCopy));
01114 result = svcManager()->declareSvcType(thrName, type);
01115 if( result.isFailure() ) {
01116 log << MSG::ERROR << "declareMultiSvcType: Cannot declare service "
01117 << type << "/" << thrName << endmsg;
01118 } else {
01119 log << MSG::VERBOSE << "declareMultiSvcType: declared service "
01120 << type << "/" << thrName << endmsg;
01121 }
01122 }
01123 }
01124 return result;
01125 }
01126
01127
01128
01129 StatusCode ApplicationMgr::addMultiSvc(const Gaudi::Utils::TypeNameString &typeName,
01130 int prio) {
01131 using Gaudi::Utils::TypeNameString;
01132 StatusCode result = StatusCode::SUCCESS;
01133 MsgStream log( m_messageSvc, m_name );
01134 if (0 == m_noOfEvtThreads) {
01135 result = svcManager()->addService(typeName, prio);
01136
01137 if( result.isFailure() ) {
01138 log << MSG::ERROR << "addMultiSvc: Cannot add service "
01139 << typeName.type() << "/" << typeName.name() << endmsg;
01140 } else {
01141 log << MSG::VERBOSE << "addMultiSvc: added service "
01142 << typeName.type() << "/" << typeName.name() << endmsg;
01143 }
01144 } else {
01145 for(int iCopy=0; iCopy<m_noOfEvtThreads; ++iCopy) {
01146 const std::string &type = typeName.type();
01147 std::string thrName(typeName.name() + getGaudiThreadIDfromID(iCopy));
01148 result = svcManager()->addService(TypeNameString(thrName, type), prio);
01149 if( result.isFailure() ) {
01150 log << MSG::ERROR << "addMultiSvc: Cannot add service "
01151 << type << "/" << thrName << endmsg;
01152 } else {
01153 log << MSG::VERBOSE << "addMultiSvc: added service "
01154 << type << "/" << thrName << endmsg;
01155 }
01156 }
01157 }
01158 return result;
01159 }
01160
01161
01162
01163
01164 void ApplicationMgr::dllNameListHandler( Property& ) {
01165 if ( !(decodeDllNameList( )).isSuccess() ) {
01166 throw GaudiException("Failed to load DLLs.",
01167 "MinimalEventLoopMgr::dllNameListHandler",
01168 StatusCode::FAILURE);
01169 }
01170 }
01171
01172
01173
01174
01175 StatusCode ApplicationMgr::decodeDllNameList() {
01176
01177 MsgStream log( m_messageSvc, m_name );
01178 StatusCode result = StatusCode::SUCCESS;
01179
01180
01181
01182 std::vector<std::string> newList;
01183 std::map<std::string,unsigned int> dllInList, duplicateList;
01184 {for ( std::vector<std::string>::const_iterator it = m_dllNameList.value().begin();
01185 it != m_dllNameList.value().end(); ++it ) {
01186 if ( 0 == dllInList[*it] ) {
01187 newList.push_back(*it);
01188 } else { ++duplicateList[*it]; }
01189 ++dllInList[*it];
01190 }}
01191
01192
01193
01194 if ( !duplicateList.empty() ) {
01195 log << MSG::DEBUG << "Removed duplicate entries for modules : ";
01196 for ( std::map<std::string,unsigned int>::const_iterator it = duplicateList.begin();
01197 it != duplicateList.end(); ++it ) {
01198 log << it->first << "(" << 1+it->second << ")";
01199 if ( it != --duplicateList.end() ) log << ", ";
01200 }
01201 log << endmsg;
01202 }
01203
01204
01205 const std::vector<std::string>& theNames = newList;
01206
01207
01208 log << MSG::DEBUG << "Loading declared DLL's" << endmsg;
01209
01210 std::vector<std::string> successNames, failNames;
01211 std::vector<std::string>::const_iterator it;
01212
01213 for (it = theNames.begin(); it != theNames.end(); it++) {
01214 if (std::find (m_okDlls.rbegin(), m_okDlls.rend(), *it) == m_okDlls.rend()){
01215
01216 StatusCode status = m_classManager->loadModule( (*it) );
01217 if( status.isFailure() ) {
01218 failNames.push_back(*it);
01219 result = StatusCode::FAILURE;
01220 }
01221 else {
01222 successNames.push_back(*it);
01223 }
01224 }
01225 }
01226
01227
01228 if ( !successNames.empty() ) {
01229 log << MSG::INFO << "Successfully loaded modules : ";
01230 for (it = successNames.begin(); it != successNames.end(); it++) {
01231 log<< (*it);
01232 if( (it+1) != successNames.end()) log << ", ";
01233
01234 m_okDlls.push_back( *it );
01235 }
01236 log << endmsg;
01237 }
01238
01239 if ( result == StatusCode::FAILURE ) {
01240 log << MSG::WARNING << "Failed to load modules: ";
01241 for (it = failNames.begin(); it != failNames.end(); it++) {
01242 log<< (*it);
01243 if( (it+1) != failNames.end()) log << ", ";
01244 }
01245 log << endmsg;
01246 }
01247 return result;
01248 }
01249
01250
01251
01252
01253 void ApplicationMgr::reflexDebugPropertyHandler( Property& )
01254 {
01255
01256 MsgStream log (m_messageSvc, name());
01257 log << MSG::INFO
01258 << "Updating ROOT::Reflex::PluginService::SetDebug(level) to level="
01259 << (int)m_reflexDebugLevel
01260 << endmsg;
01261 ROOT::Reflex::PluginService::SetDebug(m_reflexDebugLevel);
01262 }
01263
01264
01265
01266
01267 void ApplicationMgr::initLoopCheckHndlr(Property&) {
01268 svcManager()->setLoopCheckEnabled(m_loopCheck);
01269 }