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