The Gaudi Framework  master (01b473db)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
BaseSink.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 2022-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 
13 #include <Gaudi/MonitoringHub.h>
14 #include <Gaudi/Property.h>
15 #include <GaudiKernel/Service.h>
16 
17 #include <chrono>
18 #include <future>
19 #include <memory>
20 #include <mutex>
21 #include <string>
22 #include <thread>
23 #include <vector>
24 
25 namespace Gaudi::Monitoring {
26 
41  class BaseSink : public Service, public Hub::Sink {
42 
43  public:
44  using Service::Service;
45 
46  StatusCode initialize() override {
47  // registers itself to the Monitoring Hub
48  return Service::initialize().andThen( [&] { serviceLocator()->monitoringHub().addSink( this ); } );
49  }
50 
52  void registerEntity( Hub::Entity ent ) override {
53  if ( wanted( ent.type, m_typesToSave ) && wanted( ent.name, m_namesToSave ) &&
55  m_monitoringEntities.emplace( std::move( ent ) );
56  }
57  }
58 
60  void removeEntity( Hub::Entity const& ent ) override {
61  auto it = m_monitoringEntities.find( ent );
62  if ( it != m_monitoringEntities.end() ) { m_monitoringEntities.erase( it ); }
63  }
64 
71  virtual void flush( bool isStop ) = 0;
72 
73  StatusCode start() override {
74  return Service::start().andThen( [&] {
75  // promise needs to be recreated in case of a restart
76  m_flushThreadStop = std::promise<void>{};
77  // enable periodic output file flush if requested
78  if ( m_autoFlushPeriod.value() > std::numeric_limits<float>::epsilon() ) {
79  m_flushThread = std::thread{ [this, flushStop = m_flushThreadStop.get_future()]() {
80  using namespace std::chrono_literals;
81  while ( flushStop.wait_for( m_autoFlushPeriod.value() * 1s ) == std::future_status::timeout ) {
82  flush( false );
83  }
84  } };
85  }
86  } );
87  }
88 
89  StatusCode stop() override {
90  m_flushThreadStop.set_value(); // tell the flush thread we are stopping
91  if ( m_flushThread.joinable() ) m_flushThread.join(); // and wait that it exits
92  flush( true );
93  return Service::stop();
94  }
95 
96  protected:
100  template <typename Callable>
101  void applyToAllEntities( Callable func ) const {
102  std::for_each( begin( m_monitoringEntities ), end( m_monitoringEntities ), [func]( auto& p ) { func( p ); } );
103  }
104 
110  template <typename Callable>
111  void applyToAllSortedEntities( Callable func ) const {
112  std::vector<Hub::Entity const*> sortedEntities;
113  applyToAllEntities( [&sortedEntities]( auto& ent ) { sortedEntities.emplace_back( &ent ); } );
114  std::sort( sortedEntities.begin(), sortedEntities.end(), []( const auto* lhs, const auto* rhs ) {
115  return std::tie( lhs->component, lhs->name ) < std::tie( rhs->component, rhs->name );
116  } );
117  for ( auto const* ent : sortedEntities ) { func( ent->component, ent->name, *ent ); }
118  }
119 
122  bool wanted( std::string const& name, std::vector<std::string> const& searchNames ) {
123  return searchNames.empty() || std::any_of( searchNames.begin(), searchNames.end(), [&]( const auto& searchName ) {
124  const std::regex regex( searchName );
125  return std::regex_match( name, regex );
126  } );
127  }
128 
130  struct EntityOrder final {
132  return lhs.id() < rhs.id();
133  }
134  };
135  std::set<Gaudi::Monitoring::Hub::Entity, EntityOrder> m_monitoringEntities;
137  this, "NamesToSave", {}, "List of regexps used to match names of entities to save" };
139  this, "ComponentsToSave", {}, "List of regexps used to match component names of entities to save" };
141  this, "TypesToSave", {}, "List of regexps used to match type names of entities to save" };
142 
144  std::thread m_flushThread;
145  std::promise<void> m_flushThreadStop;
146  Gaudi::Property<float> m_autoFlushPeriod{
147  this, "AutoFlushPeriod", 0.,
148  "if different from 0, indicates every how many seconds to force a write of the FSR data to OutputFile (this "
149  "parameter makes sense only if used in conjunction with OutputFile)" };
150  };
151 
152 } // namespace Gaudi::Monitoring
Gaudi::Monitoring::BaseSink::m_monitoringEntities
std::set< Gaudi::Monitoring::Hub::Entity, EntityOrder > m_monitoringEntities
Definition: BaseSink.h:135
Gaudi::Monitoring::Hub::Sink
Interface reporting services must implement.
Definition: MonitoringHub.h:128
Gaudi::Monitoring::BaseSink
Base class for all Sinks registering to the Monitoring Hub Should be extended by actual Sinks.
Definition: BaseSink.h:41
Gaudi::Monitoring::BaseSink::m_flushThread
std::thread m_flushThread
Handling of regular flushes, if requested.
Definition: BaseSink.h:144
Gaudi::Monitoring::Hub::Entity::name
std::string name
name of the entity
Definition: MonitoringHub.h:83
Service::initialize
StatusCode initialize() override
Definition: Service.cpp:118
Gaudi::Monitoring::BaseSink::m_namesToSave
Gaudi::Property< std::vector< std::string > > m_namesToSave
Definition: BaseSink.h:136
Gaudi::Monitoring::BaseSink::removeEntity
void removeEntity(Hub::Entity const &ent) override
handles removal of an entity
Definition: BaseSink.h:60
Gaudi::Monitoring::BaseSink::EntityOrder
list of entities we are dealing with
Definition: BaseSink.h:130
Gaudi::Monitoring::BaseSink::m_componentsToSave
Gaudi::Property< std::vector< std::string > > m_componentsToSave
Definition: BaseSink.h:138
StatusCode::andThen
StatusCode andThen(F &&f, ARGS &&... args) const
Chain code blocks making the execution conditional a success result.
Definition: StatusCode.h:163
MonitoringHub.h
Service::start
StatusCode start() override
Definition: Service.cpp:187
Gaudi::Monitoring::BaseSink::m_autoFlushPeriod
Gaudi::Property< float > m_autoFlushPeriod
Definition: BaseSink.h:146
Gaudi::Monitoring::BaseSink::EntityOrder::operator()
bool operator()(const Gaudi::Monitoring::Hub::Entity &lhs, const Gaudi::Monitoring::Hub::Entity &rhs) const
Definition: BaseSink.h:131
Gaudi::Monitoring::BaseSink::registerEntity
void registerEntity(Hub::Entity ent) override
handles registration of a new entity
Definition: BaseSink.h:52
Gaudi::Monitoring
Definition: JSONSink.cpp:19
Gaudi::Monitoring::BaseSink::initialize
StatusCode initialize() override
Definition: BaseSink.h:46
Gaudi::Monitoring::BaseSink::stop
StatusCode stop() override
Definition: BaseSink.h:89
Service
Definition: Service.h:39
Gaudi::Monitoring::BaseSink::start
StatusCode start() override
Definition: BaseSink.h:73
Gaudi::Monitoring::BaseSink::m_typesToSave
Gaudi::Property< std::vector< std::string > > m_typesToSave
Definition: BaseSink.h:140
Gaudi::Utils::begin
AttribStringParser::Iterator begin(const AttribStringParser &parser)
Definition: AttribStringParser.h:135
StatusCode
Definition: StatusCode.h:64
Gaudi::cxx::for_each
void for_each(ContainerOfSynced &c, Fun &&f)
Definition: SynchronizedValue.h:98
cpluginsvc.func
func
Definition: cpluginsvc.py:235
Gaudi::Property::value
const ValueType & value() const
Definition: Property.h:229
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:111
Gaudi::Monitoring::Hub::Entity::id
void * id() const
unique identifier, actually mapped to internal pointer
Definition: MonitoringHub.h:110
Service.h
ConditionsStallTest.name
name
Definition: ConditionsStallTest.py:77
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:340
Gaudi::Monitoring::BaseSink::applyToAllEntities
void applyToAllEntities(Callable func) const
applies a callable to all monitoring entities
Definition: BaseSink.h:101
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:122
Gaudi::Monitoring::Hub::Entity
Wrapper class for arbitrary monitoring objects.
Definition: MonitoringHub.h:62
Gaudi::Property
Implementation of property with value of concrete type.
Definition: Property.h:35
Property.h
Service::serviceLocator
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator
Definition: Service.cpp:336
Gaudi::Monitoring::Hub::Entity::type
std::string type
type of the entity, see comment above concerning its format and usage
Definition: MonitoringHub.h:85
Gaudi::Monitoring::Hub::Entity::component
std::string component
name of the component owning the Entity
Definition: MonitoringHub.h:81
Gaudi::Monitoring::BaseSink::m_flushThreadStop
std::promise< void > m_flushThreadStop
Definition: BaseSink.h:145