The Gaudi Framework  v30r3 (a5ef0a68)
AlgExecStateSvc.cpp
Go to the documentation of this file.
1 #include "AlgExecStateSvc.h"
7 
9 
10 //=============================================================================
11 
13 {
14 
15  // seriously? do we have no way of getting a Service by type???
16  std::string wbn;
17  for ( auto& is : serviceLocator()->getServices() ) {
18  IHiveWhiteBoard* iwb = dynamic_cast<IHiveWhiteBoard*>( is );
19  if ( iwb ) {
20  wbn = is->name();
21  if ( msgLevel( MSG::VERBOSE ) ) verbose() << "HiveWhiteBoard service name is " << wbn << endmsg;
22  break;
23  }
24  }
25 
27  wbs = serviceLocator()->service( wbn, false );
28 
29  if ( wbs.isValid() ) {
30  m_algStates.resize( wbs->getNumberOfStores() );
31  m_eventStatus.resize( wbs->getNumberOfStores() );
32  } else {
33  m_algStates.resize( 1 );
34  m_eventStatus.resize( 1 );
35  }
36 
37  if ( msgLevel( MSG::DEBUG ) ) debug() << "resizing state containers to : " << m_algStates.size() << endmsg;
38 
39  SmartIF<IAlgManager> algMan( serviceLocator() );
40  if ( !algMan.isValid() ) {
41  fatal() << "could not get the AlgManager" << endmsg;
42  throw GaudiException( "In AlgExecStateSvc, unable to get the AlgManager!", "AlgExecStateSvc", StatusCode::FAILURE );
43  }
44 
45  m_isInit = true;
46 
47  auto algos = algMan->getAlgorithms();
48  for ( auto& alg : algos ) addAlg( alg );
49  for ( auto& alg : m_preInitAlgs ) addAlg( alg );
50 
51  if ( msgLevel( MSG::VERBOSE ) ) {
53  dump( ost, Gaudi::Hive::currentContext() );
54  verbose() << "dumping state:\n" << ost.str() << endmsg;
55  }
56 }
57 
58 //-----------------------------------------------------------------------------
59 
61 {
62 
63  if ( !m_isInit ) {
64  fatal() << "AlgExecStateSvc not initialized before first use" << endmsg;
65  throw GaudiException( "AlgExecStateSvc not initialized before first use!", "AlgExecStateSvc", StatusCode::FAILURE );
66  }
67 }
68 
69 //-----------------------------------------------------------------------------
70 
72 {
73  size_t slotID = ctx.valid() ? ctx.slot() : 0;
74 
75  ost << " [slot: " << slotID << ", incident: " << m_eventStatus.at( slotID ) << "]:\n\n";
76 
77  auto& algState = m_algStates.at( slotID );
78  auto ml = std::accumulate( begin( algState ), end( algState ), size_t{0},
79  []( size_t m, const auto& as ) { return std::max( m, as.first.str().length() ); } );
80 
81  for ( auto& e : algState ) ost << " + " << std::setw( ml ) << e.first.str() << " " << e.second << '\n';
82 }
83 
84 //-----------------------------------------------------------------------------
85 
86 void AlgExecStateSvc::addAlg( IAlgorithm* iAlg ) { return addAlg( iAlg->nameKey() ); }
87 
88 //-----------------------------------------------------------------------------
89 
91 {
92  if ( !m_isInit ) {
93  if ( msgLevel( MSG::DEBUG ) ) debug() << "preInit: will add Alg " << alg.str() << " later" << endmsg;
94  m_preInitAlgs.push_back( alg );
95  return;
96  }
97 
98  if ( !m_algStates.empty() && m_algStates.front().find( alg ) != m_algStates.front().end() ) {
99  // already added
100  return;
101  }
102 
103  {
104  // in theory, this should only get called during initialization (serial)
105  // so shouldn't have to protect with a mutex...
107 
108  AlgExecState s;
109  for ( auto& a : m_algStates ) a[alg] = s;
110  m_errorCount[alg] = 0;
111  }
112 
113  if ( msgLevel( MSG::DEBUG ) )
114  debug() << "adding alg " << alg.str() << " to " << m_algStates.size() << " slots" << endmsg;
115 }
116 
117 //-----------------------------------------------------------------------------
118 
120 {
121  checkInit();
122 
123  auto& algState = m_algStates.at( ctx.slot() );
124  auto itr = algState.find( algName );
125  if ( UNLIKELY( itr == algState.end() ) ) {
126  throw GaudiException{"cannot find Alg " + algName.str() + " in AlgStateMap", name(), StatusCode::FAILURE};
127  }
128  return itr->second;
129 }
130 
131 //-----------------------------------------------------------------------------
132 
134 {
135  return algExecState( iAlg->nameKey(), ctx );
136 }
137 
138 //-----------------------------------------------------------------------------
139 
141 {
143 
144  auto& algState = m_algStates.at( ctx.slot() );
145  auto itr = algState.find( iAlg->nameKey() );
146 
147  if ( UNLIKELY( itr == algState.end() ) ) {
148  throw GaudiException{std::string{"cannot find Alg "} + iAlg->name() + " in AlgStateMap", name(),
150  }
151 
152  return itr->second;
153 }
154 
155 //-----------------------------------------------------------------------------
156 
158 {
159  checkInit();
160  return m_algStates.at( ctx.slot() );
161 }
162 
163 //-----------------------------------------------------------------------------
164 
166 {
167  checkInit();
168  return m_eventStatus.at( ctx.slot() );
169 }
170 
171 //-----------------------------------------------------------------------------
172 
174 {
176  m_eventStatus.at( ctx.slot() ) = sc;
177 }
178 
179 //-----------------------------------------------------------------------------
180 
181 void AlgExecStateSvc::updateEventStatus( const bool& fail, const EventContext& ctx )
182 {
184  auto& status = m_eventStatus.at( ctx.slot() );
185  if ( status == EventStatus::Success ) {
186  if ( fail ) status = EventStatus::AlgFail;
187  } else if ( status == EventStatus::Invalid ) {
188  status = ( fail ? EventStatus::AlgFail : EventStatus::Success );
189  }
190 }
191 
192 //-----------------------------------------------------------------------------
193 
195 {
196  if ( msgLevel( MSG::DEBUG ) ) verbose() << "reset(" << ctx.slot() << ")" << endmsg;
197 
199  for ( auto& e : m_algStates.at( ctx.slot() ) ) e.second.reset();
200 
202 }
203 
204 //-----------------------------------------------------------------------------
205 
206 unsigned int AlgExecStateSvc::algErrorCount( const IAlgorithm* iAlg ) const
207 {
208  auto itr = m_errorCount.find( iAlg->nameKey() );
209  if ( itr == m_errorCount.end() ) {
210  error() << "Unable to find Algorithm \"" << iAlg->name() << "\" in map"
211  << " of ErrorCounts" << endmsg;
212  return 0;
213  }
214 
215  return itr->second;
216 }
217 
218 //-----------------------------------------------------------------------------
219 
221 {
222  auto itr = m_errorCount.find( iAlg->nameKey() );
223  if ( itr != m_errorCount.end() ) {
224  itr->second = 0;
225  } else {
226  error() << "Unable to find Algorithm \"" << iAlg->name() << "\" in map"
227  << " of ErrorCounts" << endmsg;
228  }
229 }
230 
231 //-----------------------------------------------------------------------------
232 
234 {
235  auto itr = m_errorCount.find( iAlg->nameKey() );
236  if ( itr == m_errorCount.end() ) {
237  error() << "Unable to find Algorithm \"" << iAlg->name() << "\" in map"
238  << " of ErrorCounts" << endmsg;
239  return 0;
240  }
241  return ++( itr->second );
242 }
void resetErrorCount(const IAlgorithm *iAlg) override
#define UNLIKELY(x)
Definition: Kernel.h:122
constexpr static const auto FAILURE
Definition: StatusCode.h:88
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:288
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:40
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
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)
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:94
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:68
const EventStatus::Status & eventStatus(const EventContext &ctx) const override
string s
Definition: gaudirun.py:253
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:47
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
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)