The Gaudi Framework  v30r0 (c919700c)
AlgExecStateSvc.cpp
Go to the documentation of this file.
1 #include "AlgExecStateSvc.h"
7 
9 
11 
12 //=============================================================================
13 
14 AlgExecStateSvc::AlgExecStateSvc( const std::string& name, ISvcLocator* svcLoc )
15  : base_class( name, svcLoc ), m_isInit( false )
16 {
17 }
18 
19 //-----------------------------------------------------------------------------
20 
22 
23 //-----------------------------------------------------------------------------
24 
26 {
27 
28  std::string wbn;
29 
30  // seriously? do we have no way of getting a Service by type???
32  for ( auto& is : lst ) {
33  IHiveWhiteBoard* iwb = dynamic_cast<IHiveWhiteBoard*>( is );
34  if ( iwb != 0 ) {
35  wbn = is->name();
36  if ( msgLevel( MSG::VERBOSE ) ) verbose() << "HiveWhiteBoard service name is " << wbn << endmsg;
37  break;
38  }
39  }
40 
42  wbs = serviceLocator()->service( wbn, false );
43 
44  if ( wbs.isValid() ) {
47  } else {
48  m_algStates.resize( 1 );
49  m_eventStatus.resize( 1 );
50  }
51 
52  if ( msgLevel( MSG::DEBUG ) ) debug() << "resizing state containers to : " << m_algStates.size() << endmsg;
53 
55  if ( !algMan.isValid() ) {
56  fatal() << "could not get the AlgManager" << endmsg;
57  throw GaudiException( "In AlgExecStateSvc, unable to get the AlgManager!", "AlgExecStateSvc", StatusCode::FAILURE );
58  }
59 
60  m_isInit = true;
61 
62  auto algos = algMan->getAlgorithms();
63  for ( auto& alg : algos ) {
64  addAlg( alg );
65  }
66 
67  for ( auto& alg : m_preInitAlgs ) {
68  addAlg( alg );
69  }
70 
71  if ( msgLevel( MSG::VERBOSE ) ) {
74  verbose() << "dumping state:\n" << ost.str() << endmsg;
75  }
76 }
77 
78 //-----------------------------------------------------------------------------
79 
81 {
82 
83  if ( !m_isInit ) {
84  fatal() << "AlgExecStateSvc not initialized before first use" << endmsg;
85  throw GaudiException( "AlgExecStateSvc not initialized before first use!", "AlgExecStateSvc", StatusCode::FAILURE );
86  }
87 }
88 
89 //-----------------------------------------------------------------------------
90 
92 {
93 
94  // Initialise mother class (read properties, ...)
96  if ( !sc.isSuccess() ) {
97  warning() << "Base class could not be initialized" << endmsg;
98  return StatusCode::FAILURE;
99  }
100 
101  // moved all initialization to init(). hopefully it will get called in time....
102 
103  return StatusCode::SUCCESS;
104 }
105 
106 //-----------------------------------------------------------------------------
107 
109 
110 //-----------------------------------------------------------------------------
111 
113 {
114  size_t slotID = ctx.valid() ? ctx.slot() : 0;
115 
116  ost << "Event: " << trans( m_eventStatus.at( slotID ) ) << std::endl;
117  ost << "Algs: " << m_algStates.at( slotID ).size() << std::endl;
118 
119  size_t ml( 0 );
120  for ( auto& e : m_algStates.at( slotID ) ) {
121  if ( e.first.str().length() > ml ) {
122  ml = e.first.str().length();
123  }
124  }
125 
126  ost << " - Slot " << slotID << std::endl;
127  const AlgStateMap_t& m = m_algStates.at( slotID );
128  for ( auto& e : m ) {
129  ost << " + " << std::setw( ml ) << e.first.str() << " " << e.second << std::endl;
130  }
131 }
132 
133 //-----------------------------------------------------------------------------
134 
135 void AlgExecStateSvc::addAlg( IAlgorithm* iAlg ) { return addAlg( iAlg->nameKey() ); }
136 
137 //-----------------------------------------------------------------------------
138 
140 {
141 
142  if ( !m_isInit ) {
143  if ( msgLevel( MSG::DEBUG ) ) debug() << "preInit: will add Alg " << alg.str() << " later" << endmsg;
144  m_preInitAlgs.push_back( alg );
145  return;
146  }
147 
148  if ( m_algStates.size() > 0 ) {
149  if ( m_algStates.at( 0 ).find( alg ) != m_algStates.at( 0 ).end() ) {
150  // already added
151  return;
152  }
153  }
154 
155  {
156  // in theory, this should only get called during initialization (serial)
157  // so shouldn't have to protect with a mutex...
159 
160  AlgExecState s;
161  for ( size_t i = 0; i < m_algStates.size(); ++i ) {
162  m_algStates.at( i )[alg] = s;
163  }
164 
165  m_errorCount[alg] = 0;
166  }
167 
168  if ( msgLevel( MSG::DEBUG ) )
169  debug() << "adding alg " << alg.str() << " to " << m_algStates.size() << " slots" << endmsg;
170 }
171 
172 //-----------------------------------------------------------------------------
173 
175 {
176 
177  checkInit();
178 
179  AlgStateMap_t::const_iterator itr = m_algStates.at( ctx.slot() ).find( algName );
180 
181  if ( UNLIKELY( itr == m_algStates.at( ctx.slot() ).end() ) ) {
182  throw GaudiException{std::string{"cannot find Alg "} + algName.str() + " in AlgStateMap", name(),
184  }
185 
186  return itr->second;
187 }
188 
189 //-----------------------------------------------------------------------------
190 
192 {
193 
194  return algExecState( iAlg->nameKey(), ctx );
195 }
196 
197 //-----------------------------------------------------------------------------
198 
200 {
201 
203 
204  AlgStateMap_t::iterator itr = m_algStates.at( ctx.slot() ).find( iAlg->nameKey() );
205 
206  if ( UNLIKELY( itr == m_algStates.at( ctx.slot() ).end() ) ) {
207  throw GaudiException{std::string{"cannot find Alg "} + iAlg->name() + " in AlgStateMap", name(),
209  }
210 
211  return itr->second;
212 }
213 
214 //-----------------------------------------------------------------------------
215 
217 {
218 
219  checkInit();
220 
221  return m_algStates.at( ctx.slot() );
222 }
223 
224 //-----------------------------------------------------------------------------
225 
227 {
228  checkInit();
229  return m_eventStatus.at( ctx.slot() );
230 }
231 
232 //-----------------------------------------------------------------------------
233 
235 {
237  m_eventStatus.at( ctx.slot() ) = sc;
238 }
239 
240 //-----------------------------------------------------------------------------
241 
242 void AlgExecStateSvc::updateEventStatus( const bool& fail, const EventContext& ctx )
243 {
245  if ( m_eventStatus.at( ctx.slot() ) == EventStatus::Success ) {
246  if ( fail ) m_eventStatus.at( ctx.slot() ) = EventStatus::AlgFail;
247  } else if ( m_eventStatus.at( ctx.slot() ) == EventStatus::Invalid ) {
248  if ( !fail ) {
250  } else {
252  }
253  }
254 }
255 
256 //-----------------------------------------------------------------------------
257 
259 {
260 
261  if ( msgLevel( MSG::DEBUG ) ) verbose() << "reset(" << ctx.slot() << ")" << endmsg;
262 
264  for ( auto& e : m_algStates.at( ctx.slot() ) ) {
265  e.second.reset();
266  }
267 
269 }
270 
271 //-----------------------------------------------------------------------------
272 
273 unsigned int AlgExecStateSvc::algErrorCount( const IAlgorithm* iAlg ) const
274 {
275  AlgErrorMap_t::const_iterator itr = m_errorCount.find( iAlg->nameKey() );
276 
277  if ( itr != m_errorCount.end() ) {
278  return itr->second;
279  } else {
280  error() << "Unable to find Algorithm \"" << iAlg->name() << "\" in map"
281  << " of ErrorCounts" << endmsg;
282  return 0;
283  }
284 }
285 
286 //-----------------------------------------------------------------------------
287 
289 {
290  AlgErrorMap_t::iterator itr = m_errorCount.find( iAlg->nameKey() );
291  if ( itr != m_errorCount.end() ) {
292  itr->second = 0;
293  } else {
294  error() << "Unable to find Algorithm \"" << iAlg->name() << "\" in map"
295  << " of ErrorCounts" << endmsg;
296  }
297 }
298 
299 //-----------------------------------------------------------------------------
300 
302 {
303  AlgErrorMap_t::iterator itr = m_errorCount.find( iAlg->nameKey() );
304  if ( itr != m_errorCount.end() ) {
305  return ( ++( itr->second ) );
306  } else {
307  error() << "Unable to find Algorithm \"" << iAlg->name() << "\" in map"
308  << " of ErrorCounts" << endmsg;
309  return 0;
310  }
311 }
312 
313 //-----------------------------------------------------------------------------
314 
316 {
317  switch ( s ) {
319  return "Invalid";
321  return "Success";
323  return "AlgFail";
325  return "AlgStall";
326  case EventStatus::Other:
327  return "Other";
328  default:
329  return "Should not happen";
330  }
331 }
void resetErrorCount(const IAlgorithm *iAlg) override
#define UNLIKELY(x)
Definition: Kernel.h:128
StatusCode initialize() override
Definition: Service.cpp:64
A service that keeps track of the execution state of Algorithm *.
Define general base for Gaudi exception.
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
virtual StatusCode finalize() override final
const std::string & name() const override
Retrieve name of the service.
Definition: Service.cpp:289
const AlgExecState & algExecState(const Gaudi::StringKey &algName, const EventContext &ctx) const override
ContextID_t slot() const
Definition: EventContext.h:40
AlgStates_t m_algStates
bool isSuccess() const
Test for a status code of SUCCESS.
Definition: StatusCode.h:50
MsgStream & verbose() const
shortcut for the method msgStream(MSG::VERBOSE)
T endl(T...args)
void dump(std::ostringstream &ost, const EventContext &ctx) const override
STL namespace.
T end(T...args)
void updateEventStatus(const bool &b, const EventContext &ctx) override
std::once_flag m_initFlag
T call_once(T...args)
This class represents an entry point to all the event specific data.
Definition: EventContext.h:24
STL class.
The helper class to represent the efficient "key" for access.
Definition: StringKey.h:35
T setw(T...args)
T resize(T...args)
STL class.
void checkInit() const
T at(T...args)
StatusCode service(const Gaudi::Utils::TypeNameString &name, T *&svc, bool createIf=true)
Templated method to access a service by name.
Definition: ISvcLocator.h:79
T push_back(T...args)
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
constexpr double m
Definition: SystemOfUnits.h:94
AlgErrorMap_t m_errorCount
GAUDI_API const EventContext & currentContext()
unsigned int incrementErrorCount(const IAlgorithm *iAlg) override
auto end(reverse_wrapper< T > &w)
Definition: reverse.h:64
#define DECLARE_SERVICE_FACTORY(x)
Definition: Service.h:211
std::vector< Gaudi::StringKey > m_preInitAlgs
std::string trans(const EventStatus::Status &sc) const
virtual const Gaudi::StringKey & nameKey() const =0
StringKey rep of name.
unsigned int algErrorCount(const IAlgorithm *iAlg) const override
The IAlgorithm is the interface implemented by the Algorithm base class.
Definition: IAlgorithm.h:28
const AlgStateMap_t & algExecStates(const EventContext &ctx) const override
std::mutex m_mut
T find(T...args)
void reset(const EventContext &ctx) override
T size(T...args)
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
bool isValid() const
Allow for check if smart pointer is valid.
Definition: SmartIF.h:68
const EventStatus::Status & eventStatus(const EventContext &ctx) const override
~AlgExecStateSvc()
Destructor.
string s
Definition: gaudirun.py:253
virtual const std::list< IService * > & getServices() const =0
Get a reference to a service and create it if it does not exists.
virtual StatusCode initialize() override final
virtual const std::vector< IAlgorithm * > & getAlgorithms() const =0
Return the list of Algorithms.
std::vector< EventStatus::Status > m_eventStatus
MsgStream & fatal() const
shortcut for the method msgStream(MSG::FATAL)
virtual size_t getNumberOfStores() const =0
Get the number of &#39;slots&#39;.
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
void setEventStatus(const EventStatus::Status &sc, const EventContext &ctx) override
const std::string & str() const
the actual string
Definition: StringKey.h:47
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition: Service.cpp:292
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:209
void addAlg(IAlgorithm *iAlg) override
bool valid() const
Definition: EventContext.h:41