The Gaudi Framework  v37r1 (a7f61348)
BaseSink.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 2022 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 #include <Gaudi/MonitoringHub.h>
13 #include <Gaudi/Property.h>
14 #include <GaudiKernel/Service.h>
15 
16 #include <chrono>
17 #include <future>
18 #include <memory>
19 #include <mutex>
20 #include <string>
21 #include <thread>
22 #include <vector>
23 
24 namespace Gaudi::Monitoring {
25 
40  class BaseSink : public Service, public Hub::Sink {
41 
42  public:
43  using Service::Service;
44 
45  StatusCode initialize() override {
46  // registers itself to the Monitoring Hub
47  return Service::initialize().andThen( [&] { serviceLocator()->monitoringHub().addSink( this ); } );
48  }
49 
51  void registerEntity( Hub::Entity ent ) override {
52  if ( wanted( ent.type, m_typesToSave ) && wanted( ent.name, m_namesToSave ) &&
54  m_monitoringEntities.emplace( ent.id(), std::move( ent ) );
55  }
56  }
57 
59  void removeEntity( Hub::Entity const& ent ) override {
60  auto it = m_monitoringEntities.find( ent.id() );
61  if ( it != m_monitoringEntities.end() ) { m_monitoringEntities.erase( it ); }
62  }
63 
70  virtual void flush( bool isStop ) = 0;
71 
72  StatusCode start() override {
73  return Service::start().andThen( [&] {
74  // promise needs to be recreated in case of a restart
76  // enable periodic output file flush if requested
78  m_flushThread = std::thread{ [this, flushStop = m_flushThreadStop.get_future()]() {
79  using namespace std::chrono_literals;
80  while ( flushStop.wait_for( m_autoFlushPeriod.value() * 1s ) == std::future_status::timeout ) {
81  flush( false );
82  }
83  } };
84  }
85  } );
86  }
87 
88  StatusCode stop() override {
89  m_flushThreadStop.set_value(); // tell the flush thread we are stopping
90  if ( m_flushThread.joinable() ) m_flushThread.join(); // and wait that it exits
91  flush( true );
92  return Service::stop();
93  }
94 
95  protected:
99  template <typename Callable>
100  void applyToAllEntities( Callable func ) const {
101  std::for_each( begin( m_monitoringEntities ), end( m_monitoringEntities ),
102  [func]( auto& p ) { func( p.second ); } );
103  }
104 
110  applyToAllEntities( [&sortedEntities]( auto& ent ) { sortedEntities[ent.component][ent.name] = ent; } );
111  return sortedEntities;
112  }
113 
117  if ( searchNames.empty() ) { return true; }
118  for ( const auto& searchName : searchNames ) {
119  const std::regex regex( searchName );
120  if ( std::regex_match( name, regex ) ) { return true; }
121  }
122  return false;
123  }
124 
128  this, "NamesToSave", {}, "List of regexps used to match names of entities to save" };
130  this, "ComponentsToSave", {}, "List of regexps used to match component names of entities to save" };
132  this, "TypesToSave", {}, "List of regexps used to match type names of entities to save" };
133 
137  Gaudi::Property<float> m_autoFlushPeriod{
138  this, "AutoFlushPeriod", 0.,
139  "if different from 0, indicates every how many seconds to force a write of the FSR data to OutputFile (this "
140  "parameter makes sense only if used in conjunction with OutputFile)" };
141  };
142 
143 } // namespace Gaudi::Monitoring
Gaudi::Monitoring::Hub::Sink
Interface reporting services must implement.
Definition: MonitoringHub.h:165
Gaudi::Monitoring::BaseSink
Base class for all Sinks registering to the Monitoring Hub Should be extended by actual Sinks.
Definition: BaseSink.h:40
std::for_each
T for_each(T... args)
Gaudi::Monitoring::BaseSink::m_flushThread
std::thread m_flushThread
Handling of regular flushes, if requested.
Definition: BaseSink.h:135
Gaudi::Monitoring::Hub::Entity::name
std::string name
name of the entity
Definition: MonitoringHub.h:119
Service::initialize
StatusCode initialize() override
Definition: Service.cpp:118
std::string
STL class.
Gaudi::Monitoring::BaseSink::m_namesToSave
Gaudi::Property< std::vector< std::string > > m_namesToSave
Definition: BaseSink.h:127
Gaudi::Monitoring::BaseSink::removeEntity
void removeEntity(Hub::Entity const &ent) override
handles removal of an entity
Definition: BaseSink.h:59
Gaudi::Monitoring::BaseSink::m_componentsToSave
Gaudi::Property< std::vector< std::string > > m_componentsToSave
Definition: BaseSink.h:129
StatusCode::andThen
StatusCode andThen(F &&f, ARGS &&... args) const
Chain code blocks making the execution conditional a success result.
Definition: StatusCode.h:163
std::move
T move(T... args)
bug_34121.name
name
Definition: bug_34121.py:20
MonitoringHub.h
Service::start
StatusCode start() override
Definition: Service.cpp:187
Gaudi::Monitoring::BaseSink::m_autoFlushPeriod
Gaudi::Property< float > m_autoFlushPeriod
Definition: BaseSink.h:137
std::vector< std::string >
std::map::find
T find(T... args)
std::promise< void >
Gaudi::Monitoring::BaseSink::registerEntity
void registerEntity(Hub::Entity ent) override
handles registration of a new entity
Definition: BaseSink.h:51
std::map::emplace
T emplace(T... args)
std::regex_match
T regex_match(T... args)
Gaudi::Monitoring
Definition: JSONSink.cpp:19
Gaudi::Monitoring::BaseSink::initialize
StatusCode initialize() override
Definition: BaseSink.h:45
Gaudi::Monitoring::BaseSink::stop
StatusCode stop() override
Definition: BaseSink.h:88
Service
Definition: Service.h:46
Gaudi::Monitoring::BaseSink::start
StatusCode start() override
Definition: BaseSink.h:72
Gaudi::Monitoring::BaseSink::m_typesToSave
Gaudi::Property< std::vector< std::string > > m_typesToSave
Definition: BaseSink.h:131
StatusCode
Definition: StatusCode.h:65
std::thread
STL class.
GaudiPluginService.cpluginsvc.func
func
Definition: cpluginsvc.py:236
CLHEP::begin
double * begin(CLHEP::HepVector &v)
Definition: TupleAlg.cpp:45
Gaudi::Property::value
const ValueType & value() const
Definition: Property.h:239
std::map::erase
T erase(T... args)
std::map
STL class.
Gaudi::Monitoring::BaseSink::wanted
bool wanted(std::string name, std::vector< std::string > searchNames)
deciding whether a given name matches the list of regexps given empty list means everything matches
Definition: BaseSink.h:116
std::regex
Gaudi::Monitoring::Hub::Entity::id
void * id() const
unique identifier, actually mapped to internal pointer
Definition: MonitoringHub.h:146
Service.h
Service::stop
StatusCode stop() override
Definition: Service.cpp:181
Gaudi::Monitoring::BaseSink::flush
virtual void flush(bool isStop)=0
pure virtual method to be defined by children and responsible for flushing current data of the Sink.
Service::Service
Service(std::string name, ISvcLocator *svcloc)
Standard Constructor
Definition: Service.cpp:339
std::vector::empty
T empty(T... args)
Gaudi::Monitoring::BaseSink::applyToAllEntities
void applyToAllEntities(Callable func) const
applies a callable to all monitoring entities
Definition: BaseSink.h:100
std::map::end
T end(T... args)
Gaudi::Monitoring::BaseSink::m_monitoringEntities
std::map< void *, Gaudi::Monitoring::Hub::Entity > m_monitoringEntities
list of entities we are dealing with
Definition: BaseSink.h:126
IOTest.end
end
Definition: IOTest.py:123
Gaudi::Monitoring::Hub::Entity
Wrapper class for arbitrary monitoring objects.
Definition: MonitoringHub.h:100
Gaudi::Monitoring::BaseSink::sortedEntitiesAsJSON
std::map< std::string, std::map< std::string, nlohmann::json > > sortedEntitiesAsJSON() const
returns all entities in JSON format, grouped by component first and then name
Definition: BaseSink.h:108
std::numeric_limits
Gaudi::Property
Implementation of property with value of concrete type.
Definition: Property.h:39
Property.h
Service::serviceLocator
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator
Definition: Service.cpp:335
Gaudi::Monitoring::Hub::Entity::type
std::string type
type of the entity, see comment above concerning its format and usage
Definition: MonitoringHub.h:121
Gaudi::Monitoring::Hub::Entity::component
std::string component
name of the component owning the Entity
Definition: MonitoringHub.h:117
Gaudi::Monitoring::BaseSink::m_flushThreadStop
std::promise< void > m_flushThreadStop
Definition: BaseSink.h:136