| The Gaudi Framework v29r0 (ff2e7097)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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