The Gaudi Framework  master (37c0b60a)
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( std::move( ent ) );
55  }
56  }
57 
59  void removeEntity( Hub::Entity const& ent ) override {
60  auto it = m_monitoringEntities.find( ent );
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 ), [func]( auto& p ) { func( p ); } );
102  }
103 
109  template <typename Callable>
110  void applyToAllSortedEntities( Callable func ) const {
111  std::vector<Hub::Entity const*> sortedEntities;
112  applyToAllEntities( [&sortedEntities]( auto& ent ) { sortedEntities.emplace_back( &ent ); } );
113  std::sort( sortedEntities.begin(), sortedEntities.end(), []( const auto* lhs, const auto* rhs ) {
114  return std::tie( lhs->component, lhs->name ) < std::tie( rhs->component, rhs->name );
115  } );
116  for ( auto const* ent : sortedEntities ) { func( ent->component, ent->name, *ent ); }
117  }
118 
121  bool wanted( std::string const& name, std::vector<std::string> const& searchNames ) {
122  return searchNames.empty() || std::any_of( searchNames.begin(), searchNames.end(), [&]( const auto& searchName ) {
123  const std::regex regex( searchName );
124  return std::regex_match( name, regex );
125  } );
126  }
127 
129  struct EntityOrder final {
131  return lhs.id() < rhs.id();
132  }
133  };
136  this, "NamesToSave", {}, "List of regexps used to match names of entities to save" };
138  this, "ComponentsToSave", {}, "List of regexps used to match component names of entities to save" };
140  this, "TypesToSave", {}, "List of regexps used to match type names of entities to save" };
141 
145  Gaudi::Property<float> m_autoFlushPeriod{
146  this, "AutoFlushPeriod", 0.,
147  "if different from 0, indicates every how many seconds to force a write of the FSR data to OutputFile (this "
148  "parameter makes sense only if used in conjunction with OutputFile)" };
149  };
150 
151 } // namespace Gaudi::Monitoring
Gaudi::Monitoring::BaseSink::m_monitoringEntities
std::set< Gaudi::Monitoring::Hub::Entity, EntityOrder > m_monitoringEntities
Definition: BaseSink.h:134
Gaudi::Monitoring::Hub::Sink
Interface reporting services must implement.
Definition: MonitoringHub.h:131
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:143
Gaudi::Monitoring::Hub::Entity::name
std::string name
name of the entity
Definition: MonitoringHub.h:86
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:135
Gaudi::Monitoring::BaseSink::removeEntity
void removeEntity(Hub::Entity const &ent) override
handles removal of an entity
Definition: BaseSink.h:59
Gaudi::Monitoring::BaseSink::EntityOrder
list of entities we are dealing with
Definition: BaseSink.h:129
Gaudi::Monitoring::BaseSink::m_componentsToSave
Gaudi::Property< std::vector< std::string > > m_componentsToSave
Definition: BaseSink.h:137
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)
MonitoringHub.h
Service::start
StatusCode start() override
Definition: Service.cpp:187
Gaudi::Monitoring::BaseSink::m_autoFlushPeriod
Gaudi::Property< float > m_autoFlushPeriod
Definition: BaseSink.h:145
Gaudi::Monitoring::BaseSink::EntityOrder::operator()
bool operator()(const Gaudi::Monitoring::Hub::Entity &lhs, const Gaudi::Monitoring::Hub::Entity &rhs) const
Definition: BaseSink.h:130
std::vector
STL class.
std::promise< void >
Gaudi::Monitoring::BaseSink::registerEntity
void registerEntity(Hub::Entity ent) override
handles registration of a new entity
Definition: BaseSink.h:51
Gaudi::Monitoring
Definition: JSONSink.cpp:19
Gaudi::Monitoring::BaseSink::initialize
StatusCode initialize() override
Definition: BaseSink.h:45
std::any_of
T any_of(T... args)
std::sort
T sort(T... args)
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:139
Gaudi::Utils::begin
AttribStringParser::Iterator begin(const AttribStringParser &parser)
Definition: AttribStringParser.h:136
StatusCode
Definition: StatusCode.h:65
std::thread
STL class.
cpluginsvc.func
func
Definition: cpluginsvc.py:235
Gaudi::Property::value
const ValueType & value() const
Definition: Property.h:237
Gaudi::Monitoring::BaseSink::applyToAllSortedEntities
void applyToAllSortedEntities(Callable func) const
applies a callable to all monitoring entities ordered by component the callable will be called once p...
Definition: BaseSink.h:110
Gaudi::Monitoring::Hub::Entity::id
void * id() const
unique identifier, actually mapped to internal pointer
Definition: MonitoringHub.h:113
Service.h
std::vector::emplace_back
T emplace_back(T... args)
ConditionsStallTest.name
name
Definition: ConditionsStallTest.py:77
Service::stop
StatusCode stop() override
Definition: Service.cpp:181
std::vector::begin
T begin(T... args)
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::vector::end
T end(T... args)
IOTest.end
end
Definition: IOTest.py:125
Gaudi::Monitoring::BaseSink::wanted
bool wanted(std::string const &name, std::vector< std::string > const &searchNames)
deciding whether a given name matches the list of regexps given empty list means everything matches
Definition: BaseSink.h:121
Gaudi::Monitoring::Hub::Entity
Wrapper class for arbitrary monitoring objects.
Definition: MonitoringHub.h:65
std::numeric_limits
std::set
STL class.
Gaudi::Property
Implementation of property with value of concrete type.
Definition: Property.h:37
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:88
Gaudi::Monitoring::Hub::Entity::component
std::string component
name of the component owning the Entity
Definition: MonitoringHub.h:84
Gaudi::Monitoring::BaseSink::m_flushThreadStop
std::promise< void > m_flushThreadStop
Definition: BaseSink.h:144