| The Gaudi Framework v30r0 (c919700c)

- 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 size_t slotID = ctx.valid() ? ctx.slot() : 0;
115
116 ost << "Event: " << trans( m_eventStatus.at( slotID ) ) << std::endl;
117 ost << "Algs: " << m_algStates.at( slotID ).size() << std::endl;
118
119 size_t ml( 0 );
120 for ( auto& e : m_algStates.at( slotID ) ) {
121 if ( e.first.str().length() > ml ) {
122 ml = e.first.str().length();
123 }
124 }
125
126 ost << " - Slot " << slotID << std::endl;
127 const AlgStateMap_t& m = m_algStates.at( slotID );
128 for ( auto& e : m ) {
129 ost << " + " << std::setw( ml ) << e.first.str() << " " << e.second << std::endl;
130 }
131 }
132
133 //-----------------------------------------------------------------------------
134
135 void AlgExecStateSvc::addAlg( IAlgorithm* iAlg ) { return addAlg( iAlg->nameKey() ); }
136
137 //-----------------------------------------------------------------------------
138
139 void AlgExecStateSvc::addAlg( const Gaudi::StringKey& alg )
140 {
141
142 if ( !m_isInit ) {
143 if ( msgLevel( MSG::DEBUG ) ) debug() << "preInit: will add Alg " << alg.str() << " later" << endmsg;
144 m_preInitAlgs.push_back( alg );
145 return;
146 }
147
148 if ( m_algStates.size() > 0 ) {
149 if ( m_algStates.at( 0 ).find( alg ) != m_algStates.at( 0 ).end() ) {
150 // already added
151 return;
152 }
153 }
154
155 {
156 // in theory, this should only get called during initialization (serial)
157 // so shouldn't have to protect with a mutex...
158 std::lock_guard<std::mutex> lock( m_mut );
159
160 AlgExecState s;
161 for ( size_t i = 0; i < m_algStates.size(); ++i ) {
162 m_algStates.at( i )[alg] = s;
163 }
164
165 m_errorCount[alg] = 0;
166 }
167
168 if ( msgLevel( MSG::DEBUG ) )
169 debug() << "adding alg " << alg.str() << " to " << m_algStates.size() << " slots" << endmsg;
170 }
171
172 //-----------------------------------------------------------------------------
173
174 const AlgExecState& AlgExecStateSvc::algExecState( const Gaudi::StringKey& algName, const EventContext& ctx ) const
175 {
176
177 checkInit();
178
179 AlgStateMap_t::const_iterator itr = m_algStates.at( ctx.slot() ).find( algName );
180
181 if ( UNLIKELY( itr == m_algStates.at( ctx.slot() ).end() ) ) {
182 throw GaudiException{std::string{"cannot find Alg "} + algName.str() + " in AlgStateMap", name(),
183 StatusCode::FAILURE};
184 }
185
186 return itr->second;
187 }
188
189 //-----------------------------------------------------------------------------
190
191 const AlgExecState& AlgExecStateSvc::algExecState( IAlgorithm* iAlg, const EventContext& ctx ) const
192 {
193
194 return algExecState( iAlg->nameKey(), ctx );
195 }
196
197 //-----------------------------------------------------------------------------
198
199 AlgExecState& AlgExecStateSvc::algExecState( IAlgorithm* iAlg, const EventContext& ctx )
200 {
201
202 std::call_once( m_initFlag, &AlgExecStateSvc::init, this );
203
204 AlgStateMap_t::iterator itr = m_algStates.at( ctx.slot() ).find( iAlg->nameKey() );
205
206 if ( UNLIKELY( itr == m_algStates.at( ctx.slot() ).end() ) ) {
207 throw GaudiException{std::string{"cannot find Alg "} + iAlg->name() + " in AlgStateMap", name(),
208 StatusCode::FAILURE};
209 }
210
211 return itr->second;
212 }
213
214 //-----------------------------------------------------------------------------
215
216 const IAlgExecStateSvc::AlgStateMap_t& AlgExecStateSvc::algExecStates( const EventContext& ctx ) const
217 {
218
219 checkInit();
220
221 return m_algStates.at( ctx.slot() );
222 }
223
224 //-----------------------------------------------------------------------------
225
226 const EventStatus::Status& AlgExecStateSvc::eventStatus( const EventContext& ctx ) const
227 {
228 checkInit();
229 return m_eventStatus.at( ctx.slot() );
230 }
231
232 //-----------------------------------------------------------------------------
233
234 void AlgExecStateSvc::setEventStatus( const EventStatus::Status& sc, const EventContext& ctx )
235 {
236 std::call_once( m_initFlag, &AlgExecStateSvc::init, this );
237 m_eventStatus.at( ctx.slot() ) = sc;
238 }
239
240 //-----------------------------------------------------------------------------
241
242 void AlgExecStateSvc::updateEventStatus( const bool& fail, const EventContext& ctx )
243 {
244 std::call_once( m_initFlag, &AlgExecStateSvc::init, this );
245 if ( m_eventStatus.at( ctx.slot() ) == EventStatus::Success ) {
246 if ( fail ) m_eventStatus.at( ctx.slot() ) = EventStatus::AlgFail;
247 } else if ( m_eventStatus.at( ctx.slot() ) == EventStatus::Invalid ) {
248 if ( !fail ) {
249 m_eventStatus.at( ctx.slot() ) = EventStatus::Success;
250 } else {
251 m_eventStatus.at( ctx.slot() ) = EventStatus::AlgFail;
252 }
253 }
254 }
255
256 //-----------------------------------------------------------------------------
257
258 void AlgExecStateSvc::reset( const EventContext& ctx )
259 {
260
261 if ( msgLevel( MSG::DEBUG ) ) verbose() << "reset(" << ctx.slot() << ")" << endmsg;
262
263 std::call_once( m_initFlag, &AlgExecStateSvc::init, this );
264 for ( auto& e : m_algStates.at( ctx.slot() ) ) {
265 e.second.reset();
266 }
267
268 m_eventStatus.at( ctx.slot() ) = EventStatus::Invalid;
269 }
270
271 //-----------------------------------------------------------------------------
272
273 unsigned int AlgExecStateSvc::algErrorCount( const IAlgorithm* iAlg ) const
274 {
275 AlgErrorMap_t::const_iterator itr = m_errorCount.find( iAlg->nameKey() );
276
277 if ( itr != m_errorCount.end() ) {
278 return itr->second;
279 } else {
280 error() << "Unable to find Algorithm \"" << iAlg->name() << "\" in map"
281 << " of ErrorCounts" << endmsg;
282 return 0;
283 }
284 }
285
286 //-----------------------------------------------------------------------------
287
288 void AlgExecStateSvc::resetErrorCount( const IAlgorithm* iAlg )
289 {
290 AlgErrorMap_t::iterator itr = m_errorCount.find( iAlg->nameKey() );
291 if ( itr != m_errorCount.end() ) {
292 itr->second = 0;
293 } else {
294 error() << "Unable to find Algorithm \"" << iAlg->name() << "\" in map"
295 << " of ErrorCounts" << endmsg;
296 }
297 }
298
299 //-----------------------------------------------------------------------------
300
301 unsigned int AlgExecStateSvc::incrementErrorCount( const IAlgorithm* iAlg )
302 {
303 AlgErrorMap_t::iterator itr = m_errorCount.find( iAlg->nameKey() );
304 if ( itr != m_errorCount.end() ) {
305 return ( ++( itr->second ) );
306 } else {
307 error() << "Unable to find Algorithm \"" << iAlg->name() << "\" in map"
308 << " of ErrorCounts" << endmsg;
309 return 0;
310 }
311 }
312
313 //-----------------------------------------------------------------------------
314
315 std::string AlgExecStateSvc::trans( const EventStatus::Status& s ) const
316 {
317 switch ( s ) {
318 case EventStatus::Invalid:
319 return "Invalid";
320 case EventStatus::Success:
321 return "Success";
322 case EventStatus::AlgFail:
323 return "AlgFail";
324 case EventStatus::AlgStall:
325 return "AlgStall";
326 case EventStatus::Other:
327 return "Other";
328 default:
329 return "Should not happen";
330 }
331 }
AlgExecStateSvc::resetErrorCount
void resetErrorCount(const IAlgorithm *iAlg) override
Definition: AlgExecStateSvc.cpp:288
UNLIKELY
#define UNLIKELY(x)
Definition: Kernel.h:128
StatusCode::SUCCESS
Definition: StatusCode.h:29
Service::initialize
StatusCode initialize() override
Definition: Service.cpp:64
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
AlgExecStateSvc::algExecState
const AlgExecState & algExecState(const Gaudi::StringKey &algName, const EventContext &ctx) const override
Definition: AlgExecStateSvc.cpp:174
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:50
ThreadLocalContext.h
CommonMessagingBase::verbose
MsgStream & verbose() const
shortcut for the method msgStream(MSG::VERBOSE)
Definition: CommonMessaging.h:118
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:242
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: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: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:106
SvcFactory.h
CommonMessagingBase::warning
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
Definition: CommonMessaging.h:109
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:94
std::ostringstream
STL class.
GaudiException.h
AlgExecStateSvc::m_errorCount
AlgErrorMap_t m_errorCount
Definition: AlgExecStateSvc.h:64
EventStatus::AlgStall
Definition: IAlgExecStateSvc.h:67
EventStatus::Invalid
Definition: IAlgExecStateSvc.h:67
Gaudi::Hive::currentContext
GAUDI_API const EventContext & currentContext()
Definition: ThreadLocalContext.cpp:24
AlgExecStateSvc::incrementErrorCount
unsigned int incrementErrorCount(const IAlgorithm *iAlg) override
Definition: AlgExecStateSvc.cpp:301
std::ostringstream::str
T str(T...args)
EventStatus::Success
Definition: IAlgExecStateSvc.h:67
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:67
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:315
IAlgorithm::nameKey
virtual const Gaudi::StringKey & nameKey() const =0
StringKey rep of name.
IHiveWhiteBoard
Definition: IHiveWhiteBoard.h:17
EventStatus::Status
Status
Definition: IAlgExecStateSvc.h:67
AlgExecStateSvc::algErrorCount
unsigned int algErrorCount(const IAlgorithm *iAlg) const override
Definition: AlgExecStateSvc.cpp:273
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:216
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:258
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:115
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:226
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
StatusCode::FAILURE
Definition: StatusCode.h:29
CommonMessagingBase::fatal
MsgStream & fatal() const
shortcut for the method msgStream(MSG::FATAL)
Definition: CommonMessaging.h:100
IHiveWhiteBoard::getNumberOfStores
virtual size_t getNumberOfStores() const =0
Get the number of &#39;slots&#39;.
CommonMessagingBase::msgLevel
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
Definition: CommonMessaging.h:124
AlgExecStateSvc::setEventStatus
void setEventStatus(const EventStatus::Status &sc, const EventContext &ctx) override
Definition: AlgExecStateSvc.cpp:234
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:135
EventContext::valid
bool valid() const
Definition: EventContext.h:41