The Gaudi Framework master (934d2c3c)
Loading...
Searching...
No Matches
AlgExecStateSvc.cpp
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2026 CERN for the benefit of the LHCb and ATLAS collaborations *
3* *
4* This software is distributed under the terms of the Apache version 2 licence, *
5* copied verbatim in the file "LICENSE". *
6* *
7* In applying this licence, CERN does not waive the privileges and immunities *
8* granted to it by virtue of its status as an Intergovernmental Organization *
9* or submit itself to any jurisdiction. *
10\***********************************************************************************/
11
12// Framework includes
19#include <GaudiKernel/Service.h>
21
22// STL includes
23#include <atomic>
24#include <mutex>
25#include <unordered_map>
26#include <vector>
27
28// FMT include
29#include <fmt/format.h>
30
35class AlgExecStateSvc : public extends<Service, IAlgExecStateSvc> {
36
41 public:
42 bool filterPassed() const { return m_filterPassed; }
43 void setFilterPassed( bool f = true ) { m_filterPassed = f; }
47 m_state = s;
48 m_execStatus = sc;
49 }
50 const StatusCode& execStatus() const { return m_execStatus; }
52 void reset() { *this = AlgExecStateInternal{}; }
53
54 friend std::ostream& operator<<( std::ostream& ost, const AlgExecStateInternal& s ) {
55 ost << "e: ";
56 switch ( s.state() ) {
57 case AlgExecStateRef::State::None:
58 return ost << "n";
59 case AlgExecStateRef::State::Executing:
60 return ost << "e";
61 default:
62 return ost << "d f: " << s.filterPassed() << " sc: " << s.execStatus();
63 }
64 }
65
66 private:
67 bool m_filterPassed{ true };
68 AlgExecStateRef::State m_state{ AlgExecStateRef::State::None };
70 };
71
74
75public:
76 using extends::extends;
77
78 AlgExecStateRef algExecState( const IAlgorithm* iAlg, const EventContext& ctx ) override {
79 return { *this, ctx, algKey( iAlg->name() ) };
80 }
81 AlgExecStateRef algExecState( const std::string& algName, const EventContext& ctx ) override {
82 return { *this, ctx, algKey( algName ) };
83 }
84 AlgExecStateRefConst algExecState( const IAlgorithm* iAlg, const EventContext& ctx ) const override {
85 return { *this, ctx, algKey( iAlg->name() ) };
86 }
87 AlgExecStateRefConst algExecState( const std::string& algName, const EventContext& ctx ) const override {
88 return { *this, ctx, algKey( algName ) };
89 }
90 void reset( const EventContext& ctx ) override;
91
94
95 const EventStatus::Status& eventStatus( const EventContext& ctx ) const override;
96 void setEventStatus( const EventStatus::Status& sc, const EventContext& ctx ) override;
97 void updateEventStatus( const bool& b, const EventContext& ctx ) override;
98
99 unsigned int algErrorCount( const IAlgorithm* iAlg ) const override;
100 void resetErrorCount() override;
101 void resetErrorCount( const IAlgorithm* iAlg ) override;
102 unsigned int incrementErrorCount( const IAlgorithm* iAlg ) override;
103
104 void dump( std::ostream& ost, const EventContext& ctx ) const override;
105 void dump( std::ostream&, const EventContext&, AlgExecStateRef::AlgKey ) const override;
106
107private:
108 bool filterPassed( const EventContext&, AlgExecStateRef::AlgKey ) const override;
109 void setFilterPassed( const EventContext&, AlgExecStateRef::AlgKey, bool ) override;
113 const StatusCode& execStatus( const EventContext&, AlgExecStateRef::AlgKey ) const override;
114 void setExecStatus( const EventContext&, AlgExecStateRef::AlgKey, const StatusCode& ) override;
115 const std::string& algName( AlgExecStateRef::AlgKey ) const override;
116
117 AlgExecStateInternal& getInternalState( const EventContext& ctx, AlgExecStateRef::AlgKey k );
118 AlgExecStateInternal const& getInternalState( const EventContext& ctx, AlgExecStateRef::AlgKey k ) const;
119 AlgExecStateRef::AlgKey algKey( const std::string& algName ) const { return m_algNameToIndex.at( algName ); }
120
121 // one vector entry per event slot
123 // one map entry per event slot and subslot, with AlgKey as a key, for sparse storage
125 // algorithm name to slot (with heterogeneous lookup), needed to regognize double addition
127
130 // keep the names of the algorithms by AlgKey for logging purposes
132
133 // Cannot be a vector as atomics are not copy/move constructible
135
136 void init();
137 void checkInit() const;
139 bool m_isInit{ false };
140
142};
143
145
147
148 const std::size_t slots = Gaudi::Concurrency::ConcurrencyFlags::concurrent()
150 : 1;
151 m_algStates.resize( slots );
152 m_eventStatus.resize( slots );
153
154 if ( msgLevel( MSG::DEBUG ) ) debug() << "resizing state containers to : " << slots << endmsg;
155
156 m_isInit = true;
157
158 // call addAlg for algorithms which called it too early.
159 for ( const auto& alg : m_preInitAlgs ) addAlg( alg );
160
161 if ( msgLevel( MSG::VERBOSE ) ) {
162 std::ostringstream ost;
164 verbose() << "dumping state:\n" << ost.str() << endmsg;
165 }
166}
167
169 if ( !m_isInit ) {
170 if ( msgLevel( MSG::DEBUG ) ) debug() << "preInit: will add Alg " << alg.str() << " later" << endmsg;
171 auto it = std::find( begin( m_preInitAlgs ), end( m_preInitAlgs ), alg );
172 if ( it == m_preInitAlgs.end() ) {
174 return static_cast<AlgExecStateRef::AlgKey>( m_preInitAlgs.size() - 1 );
175 } else {
176 return static_cast<AlgExecStateRef::AlgKey>( std::distance( begin( m_preInitAlgs ), it ) );
177 }
178 }
179 // try to create default state for first slot and retrieve the key
180 auto [it, b] =
181 m_algNameToIndex.try_emplace( alg, static_cast<AlgExecStateRef::AlgKey>( m_algStates.front().size() ) );
182 if ( b ) {
183 // create default state for all slots
184 for ( auto& a : m_algStates ) a.emplace_back();
185 m_errorCount.emplace( it->second, 0 );
186 m_algNames.push_back( alg );
187 if ( msgLevel( MSG::DEBUG ) )
188 debug() << "adding alg " << alg.str() << " to " << m_algStates.size() << " slots" << endmsg;
189 }
190 return it->second;
191}
192
196 // Sub-slots are dynamic
197 // Assuming the alg is known, look for a subslot
198 // Return the existing state, or create a new one
199 if ( ctx.usesSubSlot() ) {
200 // Use mutex since dynamic
201 std::scoped_lock lock( m_mut );
202
203 // Check that there is any sub-slot information for this slot
204 if ( ctx.slot() >= m_algSubSlotStates.size() ) m_algSubSlotStates.resize( ctx.slot() + 1 );
205
206 // Check that there is information for this sub-slot
207 auto& subSlots = m_algSubSlotStates[ctx.slot()];
208 if ( ctx.subSlot() >= subSlots.size() ) subSlots.resize( ctx.subSlot() + 1 );
209
210 // Find (and potentially extend) the AlgStates
211 return subSlots[ctx.subSlot()].try_emplace( k ).first->second;
212 }
213 // regular case with no subslot
214 assert( ctx.slot() < m_algStates.size() );
215 assert( static_cast<size_t>( k ) < m_algStates.at( ctx.slot() ).size() );
216 return m_algStates.at( ctx.slot() )[static_cast<size_t>( k )];
217}
218
220 AlgExecStateRef::AlgKey k ) const {
221 assert( m_isInit );
222
223 // Sub-slots are dynamic
224 // Assuming the alg is known, look for a subslot
225 // Return the existing state, or create a new one
226 if ( ctx.usesSubSlot() ) {
227 // Use mutex since dynamic
228 std::scoped_lock lock( m_mut );
229 // Check that there is any sub-slot information for this slot
230 if ( ctx.slot() >= m_algSubSlotStates.size() ) {
231 throw GaudiException( "Could not find slot in m_algSubSlotStates", "AlgExecStateSvc", StatusCode::FAILURE );
232 }
233 // Check that there is information for this sub-slot
234 auto& subSlots = m_algSubSlotStates[ctx.slot()];
235 if ( ctx.subSlot() >= subSlots.size() ) {
236 throw GaudiException( "Could not find subslot in m_algSubSlotStates", "AlgExecStateSvc", StatusCode::FAILURE );
237 }
238 // Find the AlgStates
239 return subSlots[ctx.subSlot()].at( k );
240 }
241 // regular case with no subslot
242 return m_algStates.at( ctx.slot() )[static_cast<size_t>( k )];
243}
244
246 assert( m_isInit );
247 return m_eventStatus.at( ctx.slot() );
248}
249
252 std::atomic_ref{ m_eventStatus.at( ctx.slot() ) }.store( sc, std::memory_order_relaxed );
253}
254
255void AlgExecStateSvc::updateEventStatus( const bool& fail, const EventContext& ctx ) {
257 // Called at the end of every algorithm, and algorithms of one event slot finish concurrently:
258 // update atomically so that a failure cannot be overwritten by a concurrent success.
259 std::atomic_ref status{ m_eventStatus.at( ctx.slot() ) };
260 auto current = status.load( std::memory_order_relaxed );
261 while ( true ) {
262 auto next = current;
263 if ( current == EventStatus::Success ) {
264 if ( fail ) next = EventStatus::AlgFail;
265 } else if ( current == EventStatus::Invalid ) {
266 next = ( fail ? EventStatus::AlgFail : EventStatus::Success );
267 }
268 if ( next == current || status.compare_exchange_weak( current, next, std::memory_order_relaxed ) ) { break; }
269 }
270}
271
272unsigned int AlgExecStateSvc::algErrorCount( const IAlgorithm* iAlg ) const {
273 assert( m_isInit );
274 return m_errorCount.at( algKey( iAlg->name() ) );
275}
278 for ( unsigned int k = 0; k < m_errorCount.size(); k++ ) m_errorCount[static_cast<AlgExecStateRef::AlgKey>( k )] = 0;
279}
282 m_errorCount[algKey( iAlg->name() )] = 0;
283}
286 return ++m_errorCount[algKey( iAlg->name() )];
287}
289 assert( m_isInit );
290 return getInternalState( ctx, k ).filterPassed();
291}
297 assert( m_isInit );
298 return getInternalState( ctx, k ).state();
299}
310 assert( m_isInit );
311 return getInternalState( ctx, k ).execStatus();
312}
318 assert( m_isInit );
319 return m_algNames[static_cast<size_t>( k )];
320}
321
322void AlgExecStateSvc::dump( std::ostream& ost, const EventContext& ctx ) const {
323 const size_t slotID = ctx.valid() ? ctx.slot() : 0;
324 ost << " [slot: " << slotID << ", incident: " << m_eventStatus.at( slotID ) << "]:\n\n";
325 auto& algState = m_algStates.at( slotID );
326 auto ml = std::accumulate( begin( m_algNames ), end( m_algNames ), size_t{ 0 },
327 []( size_t m, const auto& as ) { return std::max( m, as.length() ); } );
328 for ( size_t k = 0; const auto& e : algState )
329 ost << " + " << std::setw( ml ) << algName( static_cast<AlgExecStateRef::AlgKey>( k++ ) ) << " " << e << '\n';
330}
331
332void AlgExecStateSvc::dump( std::ostream& ost, const EventContext& ctx, AlgExecStateRef::AlgKey k ) const {
333 ost << getInternalState( ctx, k );
334}
335
337 if ( msgLevel( MSG::VERBOSE ) ) verbose() << "reset(" << ctx.slot() << ")" << endmsg;
338
340 for ( auto& it : m_algStates.at( ctx.slot() ) ) it.reset();
341
342 // Also clear sub slots
343 if ( ctx.slot() < m_algSubSlotStates.size() ) {
344 for ( auto& subSlot : m_algSubSlotStates[ctx.slot()] ) {
345 for ( auto& it : subSlot ) it.second.reset();
346 }
347 }
348
349 std::atomic_ref{ m_eventStatus.at( ctx.slot() ) }.store( EventStatus::Invalid, std::memory_order_relaxed );
350}
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition MsgStream.h:198
#define DECLARE_COMPONENT(type)
constexpr _InputIterator find(_InputIterator __first, _InputIterator __last, const _Tp &__val)
constexpr const _Tp & max(const _Tp &__a, const _Tp &__b)
void lock(_L1 &__l1, _L2 &__l2, _L3 &... __l3)
void call_once(once_flag &__once, _Callable &&__f, _Args &&... __args)
constexpr _Tp accumulate(_InputIterator __first, _InputIterator __last, _Tp __init)
_Setw setw(int __n)
constexpr iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
constexpr auto begin(_Container &__cont) -> decltype(__cont.begin())
constexpr auto end(_Container &__cont) -> decltype(__cont.end())
__string_type str() const
constexpr void push_back(const value_type &__x)
constexpr reference at(size_type __n)
constexpr const_iterator end() const noexcept
constexpr size_type size() const noexcept
mapped_type & at(const key_type &__k)
std::pair< iterator, bool > emplace(_Args &&... __args)
size_type size() const noexcept
pair< iterator, bool > try_emplace(const key_type &__k, _Args &&... __args)
wrapper on an Algorithm state, const version.
wrapper on an Algorithm state.
AlgExecState::State State
internal state of an algorithm
void setState(AlgExecStateRef::State s)
const StatusCode & execStatus() const
AlgExecStateRef::State state() const
void setState(AlgExecStateRef::State s, const StatusCode &sc)
friend std::ostream & operator<<(std::ostream &ost, const AlgExecStateInternal &s)
void setExecStatus(const StatusCode &sc=StatusCode::SUCCESS)
A service that keeps track of the execution state of Algorithm.
void reset(const EventContext &ctx) override
void dump(std::ostream &ost, const EventContext &ctx) const override
std::vector< std::string > m_algNames
std::vector< EventStatus::Status > m_eventStatus
AlgExecStateRef::AlgKey addAlg(const Gaudi::StringKey &) override
unsigned int incrementErrorCount(const IAlgorithm *iAlg) override
AlgExecStateRef algExecState(const IAlgorithm *iAlg, const EventContext &ctx) override
AlgExecStateRefConst algExecState(const IAlgorithm *iAlg, const EventContext &ctx) const override
void setFilterPassed(const EventContext &, AlgExecStateRef::AlgKey, bool) override
void updateEventStatus(const bool &b, const EventContext &ctx) override
AlgExecStateInternal & getInternalState(const EventContext &ctx, AlgExecStateRef::AlgKey k)
AlgExecStateRefConst algExecState(const std::string &algName, const EventContext &ctx) const override
const StatusCode & execStatus(const EventContext &, AlgExecStateRef::AlgKey) const override
void setState(const EventContext &, AlgExecStateRef::AlgKey, AlgExecStateRef::State) override
std::unordered_map< AlgExecStateRef::AlgKey, std::atomic< unsigned int > > m_errorCount
unsigned int algErrorCount(const IAlgorithm *iAlg) const override
std::vector< std::vector< AlgStatesWithSubSlot > > m_algSubSlotStates
void checkInit() const
void setEventStatus(const EventStatus::Status &sc, const EventContext &ctx) override
AlgExecStateRef algExecState(const std::string &algName, const EventContext &ctx) override
const EventStatus::Status & eventStatus(const EventContext &ctx) const override
std::vector< Gaudi::StringKey > m_preInitAlgs
std::unordered_map< AlgExecStateRef::AlgKey, AlgExecStateInternal > AlgStatesWithSubSlot
bool filterPassed(const EventContext &, AlgExecStateRef::AlgKey) const override
std::once_flag m_initFlag
std::vector< AlgStates > m_algStates
std::unordered_map< Gaudi::StringKey, AlgExecStateRef::AlgKey, Gaudi::StringKeyHash, std::equal_to<> > m_algNameToIndex
const std::string & algName(AlgExecStateRef::AlgKey) const override
AlgExecStateRef::AlgKey algKey(const std::string &algName) const
void setExecStatus(const EventContext &, AlgExecStateRef::AlgKey, const StatusCode &) override
AlgExecStateRef::State state(const EventContext &, AlgExecStateRef::AlgKey) const override
void resetErrorCount() override
std::vector< AlgExecStateInternal > AlgStates
MsgStream & verbose() const
shortcut for the method msgStream(MSG::VERBOSE)
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
This class represents an entry point to all the event specific data.
static GAUDI_API std::size_t numConcurrentEvents()
number of Concurrent Events (for MT)
static GAUDI_API bool concurrent()
serial operation, or some form of concurrency
Helper class for efficient "key" access for strings.
Definition StringKey.h:68
GaudiException(std::string Message, std::string Tag, StatusCode Code)
Constructor (1).
virtual AlgExecStateRef::AlgKey addAlg(const Gaudi::StringKey &algName)=0
adds a new algorithm to the service
The IAlgorithm is the interface implemented by the Algorithm base class.
Definition IAlgorithm.h:37
This class is used for returning status codes from appropriate routines.
Definition StatusCode.h:64
constexpr static const auto SUCCESS
Definition StatusCode.h:99
constexpr static const auto FAILURE
Definition StatusCode.h:100
Base class used to extend a class implementing other interfaces.
Definition extends.h:19
GAUDI_API const EventContext & currentContext()
@ DEBUG
Definition IMessageSvc.h:22
@ VERBOSE
Definition IMessageSvc.h:22