| The Gaudi Framework v28r2p1 (f1a77ff4)

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