The Gaudi Framework  master (d98a2936)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
AlgExecStateSvc.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2025 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 #pragma once
12 
14 #include <GaudiKernel/IAlgorithm.h>
15 #include <GaudiKernel/Service.h>
16 
17 #include <fmt/format.h>
18 
19 #include <mutex>
20 #include <unordered_map>
21 #include <vector>
22 
27 class AlgExecStateSvc : public extends<Service, IAlgExecStateSvc> {
28 
33  public:
34  bool filterPassed() const { return m_filterPassed; }
35  void setFilterPassed( bool f = true ) { m_filterPassed = f; }
36  AlgExecStateRef::State state() const { return m_state; }
39  m_state = s;
40  m_execStatus = sc;
41  }
42  const StatusCode& execStatus() const { return m_execStatus; }
44  void reset() { *this = AlgExecStateInternal{}; }
45 
46  friend std::ostream& operator<<( std::ostream& ost, const AlgExecStateInternal& s ) {
47  ost << "e: ";
48  switch ( s.state() ) {
50  return ost << "n";
52  return ost << "e";
53  default:
54  return ost << "d f: " << s.filterPassed() << " sc: " << s.execStatus();
55  }
56  }
57 
58  private:
59  bool m_filterPassed{ true };
62  };
63 
64  using AlgStates = std::vector<AlgExecStateInternal>;
65  using AlgStatesWithSubSlot = std::unordered_map<AlgExecStateRef::AlgKey, AlgExecStateInternal>;
66 
67 public:
68  using extends::extends;
69 
70  AlgExecStateRef algExecState( const IAlgorithm* iAlg, const EventContext& ctx ) override {
71  return { *this, ctx, algKey( iAlg->name() ) };
72  }
73  AlgExecStateRef algExecState( const std::string& algName, const EventContext& ctx ) override {
74  return { *this, ctx, algKey( algName ) };
75  }
76  AlgExecStateRefConst algExecState( const IAlgorithm* iAlg, const EventContext& ctx ) const override {
77  return { *this, ctx, algKey( iAlg->name() ) };
78  }
79  AlgExecStateRefConst algExecState( const std::string& algName, const EventContext& ctx ) const override {
80  return { *this, ctx, algKey( algName ) };
81  }
82  void reset( const EventContext& ctx ) override;
83 
86 
87  const EventStatus::Status& eventStatus( const EventContext& ctx ) const override;
88  void setEventStatus( const EventStatus::Status& sc, const EventContext& ctx ) override;
89  void updateEventStatus( const bool& b, const EventContext& ctx ) override;
90 
91  unsigned int algErrorCount( const IAlgorithm* iAlg ) const override;
92  void resetErrorCount() override;
93  void resetErrorCount( const IAlgorithm* iAlg ) override;
94  unsigned int incrementErrorCount( const IAlgorithm* iAlg ) override;
95 
96  void dump( std::ostream& ost, const EventContext& ctx ) const override;
97  void dump( std::ostream&, const EventContext&, AlgExecStateRef::AlgKey ) const override;
98 
99 private:
100  bool filterPassed( const EventContext&, AlgExecStateRef::AlgKey ) const override;
101  void setFilterPassed( const EventContext&, AlgExecStateRef::AlgKey, bool ) override;
105  const StatusCode& execStatus( const EventContext&, AlgExecStateRef::AlgKey ) const override;
106  void setExecStatus( const EventContext&, AlgExecStateRef::AlgKey, const StatusCode& ) override;
107  const std::string& algName( AlgExecStateRef::AlgKey ) const override;
108 
109  AlgExecStateInternal& getInternalState( const EventContext& ctx, AlgExecStateRef::AlgKey k );
110  AlgExecStateInternal const& getInternalState( const EventContext& ctx, AlgExecStateRef::AlgKey k ) const;
111  AlgExecStateRef::AlgKey algKey( const std::string& algName ) const { return m_algNameToIndex.at( algName ); }
112 
113  // one vector entry per event slot
114  std::vector<AlgStates> m_algStates;
115  // one map entry per event slot and subslot, with AlgKey as a key, for sparse storage
116  std::vector<std::vector<AlgStatesWithSubSlot>> m_algSubSlotStates;
117  // algorithm name to slot (with heterogeneous lookup), needed to regognize double addition
118  std::unordered_map<Gaudi::StringKey, AlgExecStateRef::AlgKey, Gaudi::StringKeyHash, std::equal_to<>> m_algNameToIndex;
119 
120  std::vector<EventStatus::Status> m_eventStatus;
121  std::vector<Gaudi::StringKey> m_preInitAlgs;
122  // keep the names of the algorithms by AlgKey for logging purposes
123  std::vector<std::string> m_algNames;
124 
125  // Cannot be a vector as atomics are not copy/move constructible
126  std::unordered_map<AlgExecStateRef::AlgKey, std::atomic<unsigned int>> m_errorCount;
127 
128  void init();
129  void checkInit() const;
130  std::once_flag m_initFlag;
131  bool m_isInit{ false };
132 
133  mutable std::mutex m_mut;
134 };
AlgExecStateSvc::incrementErrorCount
unsigned int incrementErrorCount(const IAlgorithm *iAlg) override
Definition: AlgExecStateSvc.cpp:151
AlgExecStateSvc::init
void init()
Definition: AlgExecStateSvc.cpp:20
AlgExecStateRef
wrapper on an Algorithm state.
Definition: IAlgExecStateSvc.h:32
AlgExecState::None
@ None
Definition: IAlgExecStateSvc.h:23
AlgExecStateSvc::AlgStates
std::vector< AlgExecStateInternal > AlgStates
Definition: AlgExecStateSvc.h:64
AlgExecStateSvc::AlgExecStateInternal::setFilterPassed
void setFilterPassed(bool f=true)
Definition: AlgExecStateSvc.h:35
AlgExecStateSvc::dump
void dump(std::ostream &ost, const EventContext &ctx) const override
Definition: AlgExecStateSvc.cpp:189
gaudirun.s
string s
Definition: gaudirun.py:346
AlgExecStateSvc::algExecState
AlgExecStateRefConst algExecState(const std::string &algName, const EventContext &ctx) const override
Definition: AlgExecStateSvc.h:79
AlgExecStateSvc::m_eventStatus
std::vector< EventStatus::Status > m_eventStatus
Definition: AlgExecStateSvc.h:120
AlgExecStateSvc::AlgExecStateInternal::operator<<
friend std::ostream & operator<<(std::ostream &ost, const AlgExecStateInternal &s)
Definition: AlgExecStateSvc.h:46
AlgExecStateSvc::getInternalState
AlgExecStateInternal & getInternalState(const EventContext &ctx, AlgExecStateRef::AlgKey k)
Definition: AlgExecStateSvc.cpp:67
AlgExecStateSvc::AlgExecStateInternal::execStatus
const StatusCode & execStatus() const
Definition: AlgExecStateSvc.h:42
AlgExecStateSvc::AlgExecStateInternal::setState
void setState(AlgExecStateRef::State s)
Definition: AlgExecStateSvc.h:37
AlgExecStateSvc::checkInit
void checkInit() const
AlgExecStateSvc::m_preInitAlgs
std::vector< Gaudi::StringKey > m_preInitAlgs
Definition: AlgExecStateSvc.h:121
AlgExecStateSvc::execStatus
const StatusCode & execStatus(const EventContext &, AlgExecStateRef::AlgKey) const override
Definition: AlgExecStateSvc.cpp:176
AlgExecStateSvc::addAlg
AlgExecStateRef::AlgKey addAlg(const Gaudi::StringKey &) override
Definition: AlgExecStateSvc.cpp:42
AlgExecState::State
State
Definition: IAlgExecStateSvc.h:23
AlgExecStateSvc::eventStatus
const EventStatus::Status & eventStatus(const EventContext &ctx) const override
Definition: AlgExecStateSvc.cpp:119
AlgExecStateSvc::m_mut
std::mutex m_mut
Definition: AlgExecStateSvc.h:133
AlgExecStateSvc::AlgExecStateInternal::filterPassed
bool filterPassed() const
Definition: AlgExecStateSvc.h:34
AlgExecStateSvc
A service that keeps track of the execution state of Algorithm.
Definition: AlgExecStateSvc.h:27
Gaudi::StringKey
Definition: StringKey.h:66
AlgExecStateSvc::m_algSubSlotStates
std::vector< std::vector< AlgStatesWithSubSlot > > m_algSubSlotStates
Definition: AlgExecStateSvc.h:116
AlgExecStateSvc::updateEventStatus
void updateEventStatus(const bool &b, const EventContext &ctx) override
Definition: AlgExecStateSvc.cpp:129
GaudiPython.Pythonizations.ctx
ctx
Definition: Pythonizations.py:578
StatusCode
Definition: StatusCode.h:64
AlgExecStateSvc::AlgExecStateInternal::setState
void setState(AlgExecStateRef::State s, const StatusCode &sc)
Definition: AlgExecStateSvc.h:38
AlgExecStateSvc::algErrorCount
unsigned int algErrorCount(const IAlgorithm *iAlg) const override
Definition: AlgExecStateSvc.cpp:139
IAlgorithm
Definition: IAlgorithm.h:36
AlgExecStateSvc::AlgExecStateInternal::m_filterPassed
bool m_filterPassed
Definition: AlgExecStateSvc.h:59
AlgExecStateSvc::AlgExecStateInternal::m_state
AlgExecStateRef::State m_state
Definition: AlgExecStateSvc.h:60
AlgExecStateSvc::m_isInit
bool m_isInit
Definition: AlgExecStateSvc.h:131
AlgExecState::Executing
@ Executing
Definition: IAlgExecStateSvc.h:23
AlgExecStateSvc::filterPassed
bool filterPassed(const EventContext &, AlgExecStateRef::AlgKey) const override
Definition: AlgExecStateSvc.cpp:155
AlgExecStateSvc::m_algNames
std::vector< std::string > m_algNames
Definition: AlgExecStateSvc.h:123
AlgExecStateSvc::AlgExecStateInternal::setExecStatus
void setExecStatus(const StatusCode &sc=StatusCode::SUCCESS)
Definition: AlgExecStateSvc.h:43
AlgExecStateSvc::setState
void setState(const EventContext &, AlgExecStateRef::AlgKey, AlgExecStateRef::State) override
Definition: AlgExecStateSvc.cpp:167
AlgExecStateSvc::AlgExecStateInternal::m_execStatus
StatusCode m_execStatus
Definition: AlgExecStateSvc.h:61
AlgExecStateSvc::setEventStatus
void setEventStatus(const EventStatus::Status &sc, const EventContext &ctx) override
Definition: AlgExecStateSvc.cpp:124
extends
Base class used to extend a class implementing other interfaces.
Definition: extends.h:19
AlgExecStateSvc::algExecState
AlgExecStateRefConst algExecState(const IAlgorithm *iAlg, const EventContext &ctx) const override
Definition: AlgExecStateSvc.h:76
AlgExecStateSvc::reset
void reset(const EventContext &ctx) override
Definition: AlgExecStateSvc.cpp:203
AlgExecStateSvc::m_initFlag
std::once_flag m_initFlag
Definition: AlgExecStateSvc.h:130
AlgExecStateSvc::m_errorCount
std::unordered_map< AlgExecStateRef::AlgKey, std::atomic< unsigned int > > m_errorCount
Definition: AlgExecStateSvc.h:126
Service.h
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:99
AlgExecStateSvc::AlgExecStateInternal::state
AlgExecStateRef::State state() const
Definition: AlgExecStateSvc.h:36
AlgExecStateRef::AlgKey
AlgKey
Definition: IAlgExecStateSvc.h:35
AlgExecStateSvc::m_algNameToIndex
std::unordered_map< Gaudi::StringKey, AlgExecStateRef::AlgKey, Gaudi::StringKeyHash, std::equal_to<> > m_algNameToIndex
Definition: AlgExecStateSvc.h:118
AlgExecStateRefConst
wrapper on an Algorithm state, const version.
Definition: IAlgExecStateSvc.h:58
EventContext
Definition: EventContext.h:34
EventStatus::Status
Status
Definition: IAlgExecStateSvc.h:75
AlgExecStateSvc::algName
const std::string & algName(AlgExecStateRef::AlgKey) const override
Definition: AlgExecStateSvc.cpp:184
AlgExecStateSvc::AlgExecStateInternal::reset
void reset()
Definition: AlgExecStateSvc.h:44
IAlgExecStateSvc::addAlg
virtual AlgExecStateRef::AlgKey addAlg(const Gaudi::StringKey &algName)=0
adds a new algorithm to the service
AlgExecStateSvc::algExecState
AlgExecStateRef algExecState(const IAlgorithm *iAlg, const EventContext &ctx) override
Definition: AlgExecStateSvc.h:70
AlgExecStateSvc::algExecState
AlgExecStateRef algExecState(const std::string &algName, const EventContext &ctx) override
Definition: AlgExecStateSvc.h:73
AlgExecStateSvc::algKey
AlgExecStateRef::AlgKey algKey(const std::string &algName) const
Definition: AlgExecStateSvc.h:111
IAlgorithm.h
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:100
IAlgExecStateSvc.h
AlgExecStateSvc::m_algStates
std::vector< AlgStates > m_algStates
Definition: AlgExecStateSvc.h:114
AlgExecStateSvc::resetErrorCount
void resetErrorCount() override
Definition: AlgExecStateSvc.cpp:143
AlgExecStateSvc::AlgExecStateInternal
internal state of an algorithm
Definition: AlgExecStateSvc.h:32
AlgExecStateSvc::AlgStatesWithSubSlot
std::unordered_map< AlgExecStateRef::AlgKey, AlgExecStateInternal > AlgStatesWithSubSlot
Definition: AlgExecStateSvc.h:65
AlgExecStateSvc::state
AlgExecStateRef::State state(const EventContext &, AlgExecStateRef::AlgKey) const override
Definition: AlgExecStateSvc.cpp:163
AlgExecStateSvc::setFilterPassed
void setFilterPassed(const EventContext &, AlgExecStateRef::AlgKey, bool) override
Definition: AlgExecStateSvc.cpp:159
AlgExecStateSvc::setExecStatus
void setExecStatus(const EventContext &, AlgExecStateRef::AlgKey, const StatusCode &) override
Definition: AlgExecStateSvc.cpp:180