| The Gaudi Framework v32r1 (f65d50dc)

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 DECLARE_COMPONENT( AlgExecStateSvc )
9
10 //=============================================================================
11
12 void AlgExecStateSvc::init() {
13
14 // seriously? do we have no way of getting a Service by type???
15 std::string wbn;
16 for ( auto& is : serviceLocator()->getServices() ) {
17 IHiveWhiteBoard* iwb = dynamic_cast<IHiveWhiteBoard*>( is );
18 if ( iwb ) {
19 wbn = is->name();
20 if ( msgLevel( MSG::VERBOSE ) ) verbose() << "HiveWhiteBoard service name is " << wbn << endmsg;
21 break;
22 }
23 }
24
25 SmartIF<IHiveWhiteBoard> wbs;
26 wbs = serviceLocator()->service( wbn, false );
27
28 if ( wbs.isValid() ) {
29 m_algStates.resize( wbs->getNumberOfStores() );
30 m_eventStatus.resize( wbs->getNumberOfStores() );
31 } else {
32 m_algStates.resize( 1 );
33 m_eventStatus.resize( 1 );
34 }
35
36 if ( msgLevel( MSG::DEBUG ) ) debug() << "resizing state containers to : " << m_algStates.size() << endmsg;
37
38 SmartIF<IAlgManager> algMan( serviceLocator() );
39 if ( !algMan.isValid() ) {
40 fatal() << "could not get the AlgManager" << endmsg;
41 throw GaudiException( "In AlgExecStateSvc, unable to get the AlgManager!", "AlgExecStateSvc", StatusCode::FAILURE );
42 }
43
44 m_isInit = true;
45
46 auto algos = algMan->getAlgorithms();
47 for ( auto& alg : algos ) addAlg( alg );
48 for ( auto& alg : m_preInitAlgs ) addAlg( alg );
49
50 if ( msgLevel( MSG::VERBOSE ) ) {
51 std::ostringstream ost;
52 dump( ost, Gaudi::Hive::currentContext() );
53 verbose() << "dumping state:\n" << ost.str() << endmsg;
54 }
55 }
56
57 //-----------------------------------------------------------------------------
58
59 void AlgExecStateSvc::checkInit() const {
60
61 if ( !m_isInit ) {
62 fatal() << "AlgExecStateSvc not initialized before first use" << endmsg;
63 throw GaudiException( "AlgExecStateSvc not initialized before first use!", "AlgExecStateSvc", StatusCode::FAILURE );
64 }
65 }
66
67 //-----------------------------------------------------------------------------
68
69 void AlgExecStateSvc::dump( std::ostringstream& ost, const EventContext& ctx ) const {
70 size_t slotID = ctx.valid() ? ctx.slot() : 0;
71
72 ost << " [slot: " << slotID << ", incident: " << m_eventStatus.at( slotID ) << "]:\n\n";
73
74 auto& algState = m_algStates.at( slotID );
75 auto ml = std::accumulate( begin( algState ), end( algState ), size_t{0},
76 []( size_t m, const auto& as ) { return std::max( m, as.first.str().length() ); } );
77
78 for ( auto& e : algState ) ost << " + " << std::setw( ml ) << e.first.str() << " " << e.second << '\n';
79 }
80
81 //-----------------------------------------------------------------------------
82
83 void AlgExecStateSvc::addAlg( const Gaudi::StringKey& alg ) {
84 if ( !m_isInit ) {
85 if ( msgLevel( MSG::DEBUG ) ) debug() << "preInit: will add Alg " << alg.str() << " later" << endmsg;
86 m_preInitAlgs.push_back( alg );
87 return;
88 }
89
90 if ( !m_algStates.empty() && m_algStates.front().find( alg ) != m_algStates.front().end() ) {
91 // already added
92 return;
93 }
94
95 {
96 // in theory, this should only get called during initialization (serial)
97 // so shouldn't have to protect with a mutex...
98 std::lock_guard<std::mutex> lock( m_mut );
99
100 AlgExecState s;
101 for ( auto& a : m_algStates ) a[alg] = s;
102 m_errorCount[alg] = 0;
103 }
104
105 if ( msgLevel( MSG::DEBUG ) )
106 debug() << "adding alg " << alg.str() << " to " << m_algStates.size() << " slots" << endmsg;
107 }
108
109 //-----------------------------------------------------------------------------
110
111 const AlgExecState& AlgExecStateSvc::algExecState( const Gaudi::StringKey& algName, const EventContext& ctx ) const {
112 checkInit();
113
114 auto& algState = m_algStates.at( ctx.slot() );
115 auto itr = algState.find( algName );
116 if ( UNLIKELY( itr == algState.end() ) ) {
117 throw GaudiException{"cannot find Alg " + algName.str() + " in AlgStateMap", name(), StatusCode::FAILURE};
118 }
119 return itr->second;
120 }
121
122 //-----------------------------------------------------------------------------
123
124 AlgExecState& AlgExecStateSvc::algExecState( IAlgorithm* iAlg, const EventContext& ctx ) {
125 std::call_once( m_initFlag, &AlgExecStateSvc::init, this );
126
127 auto& algState = m_algStates.at( ctx.slot() );
128 auto itr = algState.find( iAlg->nameKey() );
129
130 if ( UNLIKELY( itr == algState.end() ) ) {
131 throw GaudiException{std::string{"cannot find Alg "} + iAlg->name() + " in AlgStateMap", name(),
132 StatusCode::FAILURE};
133 }
134
135 return itr->second;
136 }
137
138 //-----------------------------------------------------------------------------
139
140 const IAlgExecStateSvc::AlgStateMap_t& AlgExecStateSvc::algExecStates( const EventContext& ctx ) const {
141 checkInit();
142 return m_algStates.at( ctx.slot() );
143 }
144
145 //-----------------------------------------------------------------------------
146
147 const EventStatus::Status& AlgExecStateSvc::eventStatus( const EventContext& ctx ) const {
148 checkInit();
149 return m_eventStatus.at( ctx.slot() );
150 }
151
152 //-----------------------------------------------------------------------------
153
154 void AlgExecStateSvc::setEventStatus( const EventStatus::Status& sc, const EventContext& ctx ) {
155 std::call_once( m_initFlag, &AlgExecStateSvc::init, this );
156 m_eventStatus.at( ctx.slot() ) = sc;
157 }
158
159 //-----------------------------------------------------------------------------
160
161 void AlgExecStateSvc::updateEventStatus( const bool& fail, const EventContext& ctx ) {
162 std::call_once( m_initFlag, &AlgExecStateSvc::init, this );
163 auto& status = m_eventStatus.at( ctx.slot() );
164 if ( status == EventStatus::Success ) {
165 if ( fail ) status = EventStatus::AlgFail;
166 } else if ( status == EventStatus::Invalid ) {
167 status = ( fail ? EventStatus::AlgFail : EventStatus::Success );
168 }
169 }
170
171 //-----------------------------------------------------------------------------
172
173 void AlgExecStateSvc::reset( const EventContext& ctx ) {
174 if ( msgLevel( MSG::DEBUG ) ) verbose() << "reset(" << ctx.slot() << ")" << endmsg;
175
176 std::call_once( m_initFlag, &AlgExecStateSvc::init, this );
177 for ( auto& e : m_algStates.at( ctx.slot() ) ) e.second.reset();
178
179 m_eventStatus.at( ctx.slot() ) = EventStatus::Invalid;
180 }
181
182 //-----------------------------------------------------------------------------
183
184 unsigned int AlgExecStateSvc::algErrorCount( const IAlgorithm* iAlg ) const {
185 auto itr = m_errorCount.find( iAlg->nameKey() );
186 if ( itr == m_errorCount.end() ) {
187 error() << "Unable to find Algorithm \"" << iAlg->name() << "\" in map"
188 << " of ErrorCounts" << endmsg;
189 return 0;
190 }
191
192 return itr->second;
193 }
194
195 //-----------------------------------------------------------------------------
196
197 void AlgExecStateSvc::resetErrorCount( const IAlgorithm* iAlg ) {
198 auto itr = m_errorCount.find( iAlg->nameKey() );
199 if ( itr != m_errorCount.end() ) {
200 itr->second = 0;
201 } else {
202 error() << "Unable to find Algorithm \"" << iAlg->name() << "\" in map"
203 << " of ErrorCounts" << endmsg;
204 }
205 }
206
207 //-----------------------------------------------------------------------------
208
209 unsigned int AlgExecStateSvc::incrementErrorCount( const IAlgorithm* iAlg ) {
210 auto itr = m_errorCount.find( iAlg->nameKey() );
211 if ( itr == m_errorCount.end() ) {
212 error() << "Unable to find Algorithm \"" << iAlg->name() << "\" in map"
213 << " of ErrorCounts" << endmsg;
214 return 0;
215 }
216 return ++( itr->second );
217 }
AlgExecStateSvc::resetErrorCount
void resetErrorCount(const IAlgorithm *iAlg) override
Definition: AlgExecStateSvc.cpp:197
UNLIKELY
#define UNLIKELY(x)
Definition: Kernel.h:96
AlgExecStateSvc
A service that keeps track of the execution state of Algorithm.
Definition: AlgExecStateSvc.h:16
SmartIF< IHiveWhiteBoard >
EventContext::slot
ContextID_t slot() const
Definition: EventContext.h:41
GaudiException
Define general base for Gaudi exception.
Definition: GaudiException.h:21
ManySmallAlgs.alg
alg
Definition: ManySmallAlgs.py:69
AlgExecStateSvc::m_algStates
std::vector< AlgStateMap_t > m_algStates
Definition: AlgExecStateSvc.h:46
AlgExecStateSvc::algErrorCount
unsigned int algErrorCount(const IAlgorithm *iAlg) const override
Definition: AlgExecStateSvc.cpp:184
AlgExecStateSvc::init
void init()
Definition: AlgExecStateSvc.cpp:12
Gaudi::StringKey::str
const std::string & str() const
the actual string
Definition: StringKey.h:45
AlgExecStateSvc::m_errorCount
std::map< Gaudi::StringKey, std::atomic< unsigned int > > m_errorCount
Definition: AlgExecStateSvc.h:51
ThreadLocalContext.h
MSG::DEBUG
Definition: IMessageSvc.h:15
SmartIF::isValid
bool isValid() const
Allow for check if smart pointer is valid.
Definition: SmartIF.h:62
EventContext.h
AlgExecStateSvc::addAlg
void addAlg(const Gaudi::StringKey &algName) override
Definition: AlgExecStateSvc.cpp:83
std::map::end
T end(T... args)
IAlgManager::getAlgorithms
virtual const std::vector< IAlgorithm * > & getAlgorithms() const =0
Return the list of Algorithms.
AlgExecStateSvc::updateEventStatus
void updateEventStatus(const bool &b, const EventContext &ctx) override
Definition: AlgExecStateSvc.cpp:161
AlgExecStateSvc::m_initFlag
std::once_flag m_initFlag
Definition: AlgExecStateSvc.h:55
AlgExecStateSvc::dump
void dump(std::ostringstream &ost, const EventContext &ctx) const override
Definition: AlgExecStateSvc.cpp:69
MSG::VERBOSE
Definition: IMessageSvc.h:15
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.
EventContext::valid
bool valid() const
Definition: EventContext.h:44
AlgExecStateSvc::checkInit
void checkInit() const
Definition: AlgExecStateSvc.cpp:59
Gaudi::StringKey
The helper class to represent the efficient "key" for access.
Definition: StringKey.h:34
std::setw
T setw(T... args)
CommonMessaging< implements< IService, IProperty, IStateful > >::msgLevel
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
Definition: CommonMessaging.h:138
std::string
STL class.
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
std::vector::at
T at(T... args)
Service::name
const std::string & name() const override
Retrieve name of the service.
Definition: Service.cpp:274
std::vector::push_back
T push_back(T... args)
IAlgorithm::nameKey
virtual const Gaudi::StringKey & nameKey() const =0
StringKey rep of name.
CommonMessagingBase::error
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
Definition: CommonMessaging.h:98
AlgExecStateSvc::algExecState
const AlgExecState & algExecState(const Gaudi::StringKey &algName, const EventContext &ctx) const override
Definition: AlgExecStateSvc.cpp:111
Gaudi::Units::m
constexpr double m
Definition: SystemOfUnits.h:92
std::ostringstream
STL class.
GaudiException.h
CommonMessagingBase::verbose
MsgStream & verbose() const
shortcut for the method msgStream(MSG::VERBOSE)
Definition: CommonMessaging.h:110
AlgExecStateSvc::algExecStates
const AlgStateMap_t & algExecStates(const EventContext &ctx) const override
Definition: AlgExecStateSvc.cpp:140
EventStatus::Invalid
Definition: IAlgExecStateSvc.h:63
Gaudi::Hive::currentContext
GAUDI_API const EventContext & currentContext()
Definition: ThreadLocalContext.cpp:20
AlgExecStateSvc::incrementErrorCount
unsigned int incrementErrorCount(const IAlgorithm *iAlg) override
Definition: AlgExecStateSvc.cpp:209
std::ostringstream::str
T str(T... args)
EventStatus::Success
Definition: IAlgExecStateSvc.h:63
CommonMessagingBase::debug
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
Definition: CommonMessaging.h:107
IHiveWhiteBoard::getNumberOfStores
virtual size_t getNumberOfStores() const =0
Get the number of 'slots'.
IOTest.end
def end
Definition: IOTest.py:112
EventStatus::AlgFail
Definition: IAlgExecStateSvc.h:63
AlgExecStateSvc::m_preInitAlgs
std::vector< Gaudi::StringKey > m_preInitAlgs
Definition: AlgExecStateSvc.h:49
std::max
T max(T... args)
IHiveWhiteBoard
Definition: IHiveWhiteBoard.h:17
EventStatus::Status
Status
Definition: IAlgExecStateSvc.h:63
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:56
AlgExecStateSvc::m_mut
std::mutex m_mut
Definition: AlgExecStateSvc.h:58
std::map::find
T find(T... args)
AlgExecStateSvc::reset
void reset(const EventContext &ctx) override
Definition: AlgExecStateSvc.cpp:173
std::lock_guard
IHiveWhiteBoard.h
genconfuser.verbose
verbose
Definition: genconfuser.py:19
gaudirun.s
string s
Definition: gaudirun.py:316
AlgExecStateSvc.h
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:86
IAlgManager.h
AlgExecStateSvc::eventStatus
const EventStatus::Status & eventStatus(const EventContext &ctx) const override
Definition: AlgExecStateSvc.cpp:147
AlgExecStateSvc::m_eventStatus
std::vector< EventStatus::Status > m_eventStatus
Definition: AlgExecStateSvc.h:48
Gaudi::Utils::begin
AttribStringParser::Iterator begin(const AttribStringParser &parser)
Definition: AttribStringParser.h:120
std::accumulate
T accumulate(T... args)
AlgExecStateSvc::setEventStatus
void setEventStatus(const EventStatus::Status &sc, const EventContext &ctx) override
Definition: AlgExecStateSvc.cpp:154
AlgExecState
Definition: IAlgExecStateSvc.h:27
CommonMessagingBase::fatal
MsgStream & fatal() const
shortcut for the method msgStream(MSG::FATAL)
Definition: CommonMessaging.h:92
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192