The Gaudi Framework  v33r1 (b1225454)
AlgExecStateSvc.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2020 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 #include "AlgExecStateSvc.h"
17 
19 
20 //=============================================================================
21 
22 void AlgExecStateSvc::init() {
23 
26  : 1;
27 
28  m_algStates.resize( slots );
29  m_eventStatus.resize( slots );
30 
31  if ( msgLevel( MSG::DEBUG ) ) debug() << "resizing state containers to : " << slots << endmsg;
32 
33  SmartIF<IAlgManager> algMan( serviceLocator() );
34  if ( !algMan.isValid() ) {
35  fatal() << "could not get the AlgManager" << endmsg;
36  throw GaudiException( "In AlgExecStateSvc, unable to get the AlgManager!", "AlgExecStateSvc", StatusCode::FAILURE );
37  }
38 
39  m_isInit = true;
40 
41  for ( const auto& alg : algMan->getAlgorithms() ) addAlg( alg );
42  for ( const auto& alg : m_preInitAlgs ) addAlg( alg );
43 
44  if ( msgLevel( MSG::VERBOSE ) ) {
46  dump( ost, Gaudi::Hive::currentContext() );
47  verbose() << "dumping state:\n" << ost.str() << endmsg;
48  }
49 }
50 
51 //-----------------------------------------------------------------------------
52 
54 
55  if ( !m_isInit ) {
56  throw GaudiException( "AlgExecStateSvc not initialized before first use!", "AlgExecStateSvc", StatusCode::FAILURE );
57  }
58 }
59 
60 //-----------------------------------------------------------------------------
61 
63  const size_t slotID = ctx.valid() ? ctx.slot() : 0;
64 
65  ost << " [slot: " << slotID << ", incident: " << m_eventStatus.at( slotID ) << "]:\n\n";
66 
67  auto& algState = m_algStates.at( slotID );
68  auto ml = std::accumulate( begin( algState ), end( algState ), size_t{0},
69  []( size_t m, const auto& as ) { return std::max( m, as.first.str().length() ); } );
70 
71  for ( const auto& e : algState ) ost << " + " << std::setw( ml ) << e.first.str() << " " << e.second << '\n';
72 }
73 
74 //-----------------------------------------------------------------------------
75 
77  if ( !m_isInit ) {
78  if ( msgLevel( MSG::DEBUG ) ) debug() << "preInit: will add Alg " << alg.str() << " later" << endmsg;
80  return;
81  }
82 
83  if ( !m_algStates.empty() && m_algStates.front().find( alg ) != m_algStates.front().end() ) {
84  // already added
85  return;
86  }
87 
88  {
89  // in theory, this should only get called during initialization (serial)
90  // so shouldn't have to protect with a mutex...
91  std::scoped_lock lock( m_mut );
92 
94  for ( auto& a : m_algStates ) a[alg] = s;
95  m_errorCount[alg] = 0;
96  }
97 
98  if ( msgLevel( MSG::DEBUG ) )
99  debug() << "adding alg " << alg.str() << " to " << m_algStates.size() << " slots" << endmsg;
100 }
101 
102 //-----------------------------------------------------------------------------
103 
105  checkInit();
106 
107  auto& algState = m_algStates.at( ctx.slot() );
108  auto itr = algState.find( algName );
109  if ( UNLIKELY( itr == algState.end() ) ) {
110  throw GaudiException{"cannot find Alg " + algName.str() + " in AlgStateMap", name(), StatusCode::FAILURE};
111  }
112  return itr->second;
113 }
114 
115 //-----------------------------------------------------------------------------
116 
119 
120  auto& algState = m_algStates.at( ctx.slot() );
121  auto itr = algState.find( iAlg->nameKey() );
122 
123  if ( UNLIKELY( itr == algState.end() ) ) {
124  throw GaudiException{std::string{"cannot find Alg "} + iAlg->name() + " in AlgStateMap", name(),
126  }
127 
128  return itr->second;
129 }
130 
131 //-----------------------------------------------------------------------------
132 
134  checkInit();
135  return m_algStates.at( ctx.slot() );
136 }
137 
138 //-----------------------------------------------------------------------------
139 
141  checkInit();
142  return m_eventStatus.at( ctx.slot() );
143 }
144 
145 //-----------------------------------------------------------------------------
146 
149  m_eventStatus.at( ctx.slot() ) = sc;
150 }
151 
152 //-----------------------------------------------------------------------------
153 
154 void AlgExecStateSvc::updateEventStatus( const bool& fail, const EventContext& ctx ) {
156  auto& status = m_eventStatus.at( ctx.slot() );
157  if ( status == EventStatus::Success ) {
158  if ( fail ) status = EventStatus::AlgFail;
159  } else if ( status == EventStatus::Invalid ) {
160  status = ( fail ? EventStatus::AlgFail : EventStatus::Success );
161  }
162 }
163 
164 //-----------------------------------------------------------------------------
165 
167  if ( msgLevel( MSG::VERBOSE ) ) verbose() << "reset(" << ctx.slot() << ")" << endmsg;
168 
170  for ( auto& e : m_algStates.at( ctx.slot() ) ) e.second.reset();
171 
173 }
174 
175 //-----------------------------------------------------------------------------
176 
177 unsigned int AlgExecStateSvc::algErrorCount( const IAlgorithm* iAlg ) const {
178  auto itr = m_errorCount.find( iAlg->nameKey() );
179  if ( itr == m_errorCount.end() ) {
180  error() << "Unable to find Algorithm \"" << iAlg->name() << "\" in map"
181  << " of ErrorCounts" << endmsg;
182  return 0;
183  }
184 
185  return itr->second;
186 }
187 
188 //-----------------------------------------------------------------------------
189 
191  auto itr = m_errorCount.find( iAlg->nameKey() );
192  if ( itr != m_errorCount.end() ) {
193  itr->second = 0;
194  } else {
195  error() << "Unable to find Algorithm \"" << iAlg->name() << "\" in map"
196  << " of ErrorCounts" << endmsg;
197  }
198 }
199 
200 //-----------------------------------------------------------------------------
201 
203  auto itr = m_errorCount.find( iAlg->nameKey() );
204  if ( itr == m_errorCount.end() ) {
205  error() << "Unable to find Algorithm \"" << iAlg->name() << "\" in map"
206  << " of ErrorCounts" << endmsg;
207  return 0;
208  }
209  return ++( itr->second );
210 }
void resetErrorCount(const IAlgorithm *iAlg) override
#define UNLIKELY(x)
Definition: Kernel.h:106
A service that keeps track of the execution state of Algorithm.
Define general base for Gaudi exception.
std::vector< AlgStateMap_t > m_algStates
std::unordered_map< Gaudi::StringKey, std::atomic< unsigned int > > m_errorCount
unsigned int algErrorCount(const IAlgorithm *iAlg) const override
const std::string & str() const
the actual string
Definition: StringKey.h:56
bool isValid() const
Allow for check if smart pointer is valid.
Definition: SmartIF.h:72
void addAlg(const Gaudi::StringKey &algName) override
T end(T... args)
virtual const std::vector< IAlgorithm * > & getAlgorithms() const =0
Return the list of Algorithms.
void updateEventStatus(const bool &b, const EventContext &ctx) override
std::once_flag m_initFlag
void dump(std::ostringstream &ost, const EventContext &ctx) const override
T call_once(T... args)
This class represents an entry point to all the event specific data.
Definition: EventContext.h:34
STL class.
void checkInit() const
The helper class to represent the efficient "key" for access.
Definition: StringKey.h:44
T setw(T... args)
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
STL class.
#define DECLARE_COMPONENT(type)
T at(T... args)
const std::string & name() const override
Retrieve name of the service.
Definition: Service.cpp:284
T push_back(T... args)
virtual const Gaudi::StringKey & nameKey() const =0
StringKey rep of name.
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
static GAUDI_API bool concurrent()
serial operation, or some form of concurrency
const AlgExecState & algExecState(const Gaudi::StringKey &algName, const EventContext &ctx) const override
constexpr double m
MsgStream & verbose() const
shortcut for the method msgStream(MSG::VERBOSE)
const AlgStateMap_t & algExecStates(const EventContext &ctx) const override
GAUDI_API const EventContext & currentContext()
unsigned int incrementErrorCount(const IAlgorithm *iAlg) override
T str(T... args)
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
def end
Definition: IOTest.py:123
std::vector< Gaudi::StringKey > m_preInitAlgs
T max(T... args)
The IAlgorithm is the interface implemented by the Algorithm base class.
Definition: IAlgorithm.h:38
std::mutex m_mut
T find(T... args)
void reset(const EventContext &ctx) override
string s
Definition: gaudirun.py:328
constexpr static const auto FAILURE
Definition: StatusCode.h:101
const EventStatus::Status & eventStatus(const EventContext &ctx) const override
std::vector< EventStatus::Status > m_eventStatus
AttribStringParser::Iterator begin(const AttribStringParser &parser)
T accumulate(T... args)
void setEventStatus(const EventStatus::Status &sc, const EventContext &ctx) override
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
static GAUDI_API std::size_t numConcurrentEvents()
number of Concurrent Events (for MT)