Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
AlgExecStateSvc.cpp
Go to the documentation of this file.
1 #include "AlgExecStateSvc.h"
7 
9 
10 //=============================================================================
11 
13 
14  // seriously? do we have no way of getting a Service by type???
15  std::string wbn;
16  for ( auto& is : serviceLocator()->getServices() ) {
17  IHiveWhiteBoard* iwb = dynamic_cast<IHiveWhiteBoard*>( is );
18  if ( iwb ) {
19  wbn = is->name();
20  if ( msgLevel( MSG::VERBOSE ) ) verbose() << "HiveWhiteBoard service name is " << wbn << endmsg;
21  break;
22  }
23  }
24 
26  wbs = serviceLocator()->service( wbn, false );
27 
28  if ( wbs.isValid() ) {
29  m_algStates.resize( wbs->getNumberOfStores() );
30  m_eventStatus.resize( wbs->getNumberOfStores() );
31  } else {
32  m_algStates.resize( 1 );
33  m_eventStatus.resize( 1 );
34  }
35 
36  if ( msgLevel( MSG::DEBUG ) ) debug() << "resizing state containers to : " << m_algStates.size() << endmsg;
37 
38  SmartIF<IAlgManager> algMan( serviceLocator() );
39  if ( !algMan.isValid() ) {
40  fatal() << "could not get the AlgManager" << endmsg;
41  throw GaudiException( "In AlgExecStateSvc, unable to get the AlgManager!", "AlgExecStateSvc", StatusCode::FAILURE );
42  }
43 
44  m_isInit = true;
45 
46  auto algos = algMan->getAlgorithms();
47  for ( auto& alg : algos ) addAlg( alg );
48  for ( auto& alg : m_preInitAlgs ) addAlg( alg );
49 
50  if ( msgLevel( MSG::VERBOSE ) ) {
52  dump( ost, Gaudi::Hive::currentContext() );
53  verbose() << "dumping state:\n" << ost.str() << endmsg;
54  }
55 }
56 
57 //-----------------------------------------------------------------------------
58 
60 
61  if ( !m_isInit ) {
62  fatal() << "AlgExecStateSvc not initialized before first use" << endmsg;
63  throw GaudiException( "AlgExecStateSvc not initialized before first use!", "AlgExecStateSvc", StatusCode::FAILURE );
64  }
65 }
66 
67 //-----------------------------------------------------------------------------
68 
69 void AlgExecStateSvc::dump( std::ostringstream& ost, const EventContext& ctx ) const {
70  size_t slotID = ctx.valid() ? ctx.slot() : 0;
71 
72  ost << " [slot: " << slotID << ", incident: " << m_eventStatus.at( slotID ) << "]:\n\n";
73 
74  auto& algState = m_algStates.at( slotID );
75  auto ml = std::accumulate( begin( algState ), end( algState ), size_t{0},
76  []( size_t m, const auto& as ) { return std::max( m, as.first.str().length() ); } );
77 
78  for ( auto& e : algState ) ost << " + " << std::setw( ml ) << e.first.str() << " " << e.second << '\n';
79 }
80 
81 //-----------------------------------------------------------------------------
82 
84  if ( !m_isInit ) {
85  if ( msgLevel( MSG::DEBUG ) ) debug() << "preInit: will add Alg " << alg.str() << " later" << endmsg;
86  m_preInitAlgs.push_back( alg );
87  return;
88  }
89 
90  if ( !m_algStates.empty() && m_algStates.front().find( alg ) != m_algStates.front().end() ) {
91  // already added
92  return;
93  }
94 
95  {
96  // in theory, this should only get called during initialization (serial)
97  // so shouldn't have to protect with a mutex...
99 
100  AlgExecState s;
101  for ( auto& a : m_algStates ) a[alg] = s;
102  m_errorCount[alg] = 0;
103  }
104 
105  if ( msgLevel( MSG::DEBUG ) )
106  debug() << "adding alg " << alg.str() << " to " << m_algStates.size() << " slots" << endmsg;
107 }
108 
109 //-----------------------------------------------------------------------------
110 
111 const AlgExecState& AlgExecStateSvc::algExecState( const Gaudi::StringKey& algName, const EventContext& ctx ) const {
112  checkInit();
113 
114  auto& algState = m_algStates.at( ctx.slot() );
115  auto itr = algState.find( algName );
116  if ( UNLIKELY( itr == algState.end() ) ) {
117  throw GaudiException{"cannot find Alg " + algName.str() + " in AlgStateMap", name(), StatusCode::FAILURE};
118  }
119  return itr->second;
120 }
121 
122 //-----------------------------------------------------------------------------
123 
126 
127  auto& algState = m_algStates.at( ctx.slot() );
128  auto itr = algState.find( iAlg->nameKey() );
129 
130  if ( UNLIKELY( itr == algState.end() ) ) {
131  throw GaudiException{std::string{"cannot find Alg "} + iAlg->name() + " in AlgStateMap", name(),
133  }
134 
135  return itr->second;
136 }
137 
138 //-----------------------------------------------------------------------------
139 
141  checkInit();
142  return m_algStates.at( ctx.slot() );
143 }
144 
145 //-----------------------------------------------------------------------------
146 
148  checkInit();
149  return m_eventStatus.at( ctx.slot() );
150 }
151 
152 //-----------------------------------------------------------------------------
153 
156  m_eventStatus.at( ctx.slot() ) = sc;
157 }
158 
159 //-----------------------------------------------------------------------------
160 
161 void AlgExecStateSvc::updateEventStatus( const bool& fail, const EventContext& ctx ) {
163  auto& status = m_eventStatus.at( ctx.slot() );
164  if ( status == EventStatus::Success ) {
165  if ( fail ) status = EventStatus::AlgFail;
166  } else if ( status == EventStatus::Invalid ) {
167  status = ( fail ? EventStatus::AlgFail : EventStatus::Success );
168  }
169 }
170 
171 //-----------------------------------------------------------------------------
172 
174  if ( msgLevel( MSG::DEBUG ) ) verbose() << "reset(" << ctx.slot() << ")" << endmsg;
175 
177  for ( auto& e : m_algStates.at( ctx.slot() ) ) e.second.reset();
178 
180 }
181 
182 //-----------------------------------------------------------------------------
183 
184 unsigned int AlgExecStateSvc::algErrorCount( const IAlgorithm* iAlg ) const {
185  auto itr = m_errorCount.find( iAlg->nameKey() );
186  if ( itr == m_errorCount.end() ) {
187  error() << "Unable to find Algorithm \"" << iAlg->name() << "\" in map"
188  << " of ErrorCounts" << endmsg;
189  return 0;
190  }
191 
192  return itr->second;
193 }
194 
195 //-----------------------------------------------------------------------------
196 
198  auto itr = m_errorCount.find( iAlg->nameKey() );
199  if ( itr != m_errorCount.end() ) {
200  itr->second = 0;
201  } else {
202  error() << "Unable to find Algorithm \"" << iAlg->name() << "\" in map"
203  << " of ErrorCounts" << endmsg;
204  }
205 }
206 
207 //-----------------------------------------------------------------------------
208 
210  auto itr = m_errorCount.find( iAlg->nameKey() );
211  if ( itr == m_errorCount.end() ) {
212  error() << "Unable to find Algorithm \"" << iAlg->name() << "\" in map"
213  << " of ErrorCounts" << endmsg;
214  return 0;
215  }
216  return ++( itr->second );
217 }
void resetErrorCount(const IAlgorithm *iAlg) override
#define UNLIKELY(x)
Definition: Kernel.h:89
A service that keeps track of the execution state of Algorithm.
Define general base for Gaudi exception.
const std::string & name() const override
Retrieve name of the service.
Definition: Service.cpp:274
std::vector< AlgStateMap_t > m_algStates
const AlgExecState & algExecState(const Gaudi::StringKey &algName, const EventContext &ctx) const override
ContextID_t slot() const
Definition: EventContext.h:48
std::map< Gaudi::StringKey, std::atomic< unsigned int > > m_errorCount
MsgStream & verbose() const
shortcut for the method msgStream(MSG::VERBOSE)
void dump(std::ostringstream &ost, const EventContext &ctx) const override
class MergingTransformer< Out(const vector_of_const_< In > void
void addAlg(const Gaudi::StringKey &algName) override
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:31
STL class.
The helper class to represent the efficient "key" for access.
Definition: StringKey.h:34
T setw(T...args)
STL class.
#define DECLARE_COMPONENT(type)
void checkInit() const
T at(T...args)
T push_back(T...args)
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
constexpr double m
Definition: SystemOfUnits.h:92
GAUDI_API const EventContext & currentContext()
unsigned int incrementErrorCount(const IAlgorithm *iAlg) override
std::vector< Gaudi::StringKey > m_preInitAlgs
virtual const Gaudi::StringKey & nameKey() const =0
StringKey rep of name.
T max(T...args)
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
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
bool isValid() const
Allow for check if smart pointer is valid.
Definition: SmartIF.h:62
const EventStatus::Status & eventStatus(const EventContext &ctx) const override
string s
Definition: gaudirun.py:312
constexpr static const auto FAILURE
Definition: StatusCode.h:86
virtual const std::vector< IAlgorithm * > & getAlgorithms() const =0
Return the list of Algorithms.
std::vector< EventStatus::Status > m_eventStatus
AttribStringParser::Iterator begin(const AttribStringParser &parser)
T accumulate(T...args)
MsgStream & fatal() const
shortcut for the method msgStream(MSG::FATAL)
virtual size_t getNumberOfStores() const =0
Get the number of &#39;slots&#39;.
void setEventStatus(const EventStatus::Status &sc, const EventContext &ctx) override
const std::string & str() const
the actual string
Definition: StringKey.h:45
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192
bool valid() const
Definition: EventContext.h:51
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)