SequentialSchedulerSvc.cpp
Go to the documentation of this file.
1 // Framework includes
2 #include "GaudiKernel/SvcFactory.h"
3 #include "GaudiKernel/IAlgorithm.h"
4 #include "GaudiKernel/Algorithm.h" // will be IAlgorithm if context getter promoted to interface
5 #include "GaudiKernel/IProperty.h"
6 #include "GaudiKernel/AppReturnCode.h"
7 
8 // C++
9 #include <list>
10 #include <thread>
11 
12 // Local
13 #include "SequentialSchedulerSvc.h"
14 #include "AlgResourcePool.h"
15 #include "RetCodeGuard.h"
16 
17 // Instantiation of a static factory class used by clients to create instances of this service
19 
20 //===========================================================================
21 // Infrastructure methods
22 
24  base_class(name,svcLoc),
25  m_eventContext(nullptr),
26  m_freeSlots(1){
27  declareProperty("UseTopAlgList", m_useTopAlgList=true);
28 }
29 
30 //---------------------------------------------------------------------------
32 //---------------------------------------------------------------------------
33 
35 
36  // Initialise mother class (read properties, ...)
38  if (!sc.isSuccess())
39  warning () << "Base class could not be initialized" << endmsg;
40 
41  // Get the algo resource pool
42  SmartIF<IAlgResourcePool> algResourcePool (serviceLocator()->service("AlgResourcePool"));
43  if (!algResourcePool.isValid()){
44  error() << "Error retrieving AlgResourcePool" << endmsg;
45  return StatusCode::FAILURE;
46  }
47 
48  // Get the list of algorithms
49  m_algList = m_useTopAlgList ? algResourcePool->getTopAlgList() : algResourcePool->getFlatAlgList();
50  info() << "Found " << m_algList.size() << " algorithms" << endmsg;
51 
52  return StatusCode::SUCCESS;
53 
54 }
55 //---------------------------------------------------------------------------
56 
59  if (!sc.isSuccess())
60  warning () << "Base class could not be finalized" << endmsg;
61  return sc;
62 }
63 
64 //---------------------------------------------------------------------------
65 
70 
71  m_freeSlots--;
72 
73  debug() << "[pushNewEvent] Free slots are now: " << m_freeSlots << endmsg;
74 
75  // Call the resetExecuted() method of ALL "known" algorithms
76  // (From the MinimalEventLoopMgr)
77  SmartIF<IAlgManager> algMan(serviceLocator());
78  if (LIKELY(algMan.isValid())) {
79  for( IAlgorithm* ialg : algMan->getAlgorithms() ) {
80  if (LIKELY(nullptr != ialg)) ialg->resetExecuted();
81  }
82  }
83 
84  m_eventContext= eventContext;
85  bool eventfailed=false;
86 
87  for (IAlgorithm* ialgorithm : m_algList){
88 
89  Algorithm* this_algo = dynamic_cast<Algorithm*>(ialgorithm);
90  if (!this_algo){
91  throw GaudiException ("Cast to Algorithm failed!",
92  "SequentialSchedulerSvc",
94  }
95 
96  debug() << "Running algorithm " << this_algo->name() << endmsg;
97 
98  // m_eventContext->m_thread_id = pthread_self();
99  this_algo->setContext(m_eventContext);
100 
101  // Get the IProperty interface of the ApplicationMgr to pass it to RetCodeGuard
102  const SmartIF<IProperty> appmgr(serviceLocator());
103 
104  // Call the execute() method of all top algorithms
106  try {
108  sc = ialgorithm->sysExecute();
109  if (UNLIKELY(!sc.isSuccess())) {
110  warning() << "Execution of algorithm " << ialgorithm->name() << " failed" << endmsg;
111  eventfailed = true;
112  }
113  rcg.ignore(); // disarm the guard
114  } catch ( const GaudiException& Exception ) {
115  error() << ".executeEvent(): Exception with tag=" << Exception.tag()
116  << " thrown by " << ialgorithm->name() << endmsg;
117  error() << Exception << endmsg;
118  } catch ( const std::exception& Exception ) {
119  fatal() << ".executeEvent(): Standard std::exception thrown by "
120  << ialgorithm->name() << endmsg;
121  error() << Exception.what() << endmsg;
122  } catch(...) {
123  fatal() << ".executeEvent(): UNKNOWN Exception thrown by "
124  << ialgorithm->name() << endmsg;
125  }
126 
127  debug() << "Algorithm " << this_algo->name() << (eventfailed ? " failed" : " succeeded") << endmsg;
128  debug() << "Algorithm " << this_algo->name() << (this_algo->filterPassed() ? " passed" : " rejected") << endmsg;
129 
130  // DP it is important to propagate the failure of an event.
131  // We need to stop execution when this happens so that execute run can
132  // then receive the FAILURE
133  m_eventContext->setFail(eventfailed);
134 
135  if (eventfailed)
136  return StatusCode::FAILURE;
137  }
138  return StatusCode::SUCCESS;
139 
140 }
141 
142 //---------------------------------------------------------------------------
143 StatusCode SequentialSchedulerSvc::pushNewEvents(std::vector<EventContext*>& eventContexts){
144  StatusCode sc;
145  for (auto context : eventContexts){
146  sc = pushNewEvent(context);
147  if (sc != StatusCode::SUCCESS) return sc;
148  }
149  return sc;
150 }
151 
152 //---------------------------------------------------------------------------
155  m_freeSlots++;
156  eventContext = m_eventContext;
157  debug() << "[popFinishedEvent] Free slots are now: " << m_freeSlots << endmsg;
158  return StatusCode::SUCCESS;
159 }
160 
161 //---------------------------------------------------------------------------
166  return m_freeSlots == 1 ? StatusCode::FAILURE : popFinishedEvent(eventContext);
167 }
168 //---------------------------------------------------------------------------
169 
174 
175 //---------------------------------------------------------------------------
StatusCode initialize() override
Definition: Service.cpp:63
Define general base for Gaudi exception.
Helper class to set the application return code in case of early exit (e.g.
Definition: RetCodeGuard.h:9
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
StatusCode finalize() override
Definition: Service.cpp:188
std::list< IAlgorithm * > m_algList
Cache the list of algs to be executed.
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:76
STL namespace.
virtual StatusCode tryPopFinishedEvent(EventContext *&eventContext)
Try to fetch an event from the scheduler.
virtual StatusCode pushNewEvents(std::vector< EventContext * > &eventContexts)
bool filterPassed() const override
Did this algorithm pass or fail its filter criterion for the last event?
Definition: Algorithm.cpp:948
virtual const std::vector< IAlgorithm * > & getAlgorithms() const =0
Return the list of Algorithms.
virtual StatusCode finalize()
Finalise.
virtual std::list< IAlgorithm * > getFlatAlgList()=0
Get the flat list of algorithms.
virtual unsigned int freeSlots()
Get free slots number.
This class represents an entry point to all the event specific data.
Definition: EventContext.h:22
void setContext(EventContext *context)
set the context
Definition: Algorithm.h:556
const std::string & name() const override
The identifying name of the algorithm object.
Definition: Algorithm.cpp:919
constexpr int UnhandledException
Definition: AppReturnCode.h:27
virtual std::list< IAlgorithm * > getTopAlgList()=0
Get top list of algorithms.
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
void setFail(const bool &b=true)
Definition: EventContext.h:54
virtual const std::string & tag() const
name tag for the exception, or exception type
#define DECLARE_SERVICE_FACTORY(x)
Definition: Service.h:354
The IAlgorithm is the interface implemented by the Algorithm base class.
Definition: IAlgorithm.h:23
virtual StatusCode pushNewEvent(EventContext *eventContext)
Make an event available to the scheduler.
int m_freeSlots
The number of free slots (0 or 1)
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:77
virtual StatusCode initialize()
Initialise.
bool isValid() const
Allow for check if smart pointer is valid.
Definition: SmartIF.h:62
Base class used to extend a class implementing other interfaces.
Definition: extends.h:10
#define UNLIKELY(x)
Definition: Kernel.h:126
void ignore()
Definition: RetCodeGuard.h:13
virtual StatusCode popFinishedEvent(EventContext *&eventContext)
Blocks until an event is availble.
#define LIKELY(x)
Definition: Kernel.h:125
bool m_useTopAlgList
Decide if the top alglist or its flat version has to be used.
This SchedulerSvc implements the IScheduler interface.
EventContext * m_eventContext
The context of the event being processed.