The Gaudi Framework  master (b9786168)
Loading...
Searching...
No Matches
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
25namespace Gaudi::Monitoring {
26
41 class BaseSink : public Service, public Hub::Sink {
42
43 public:
44 using Service::Service;
45
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;
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
Base class for all Sinks registering to the Monitoring Hub Should be extended by actual Sinks.
Definition BaseSink.h:41
Gaudi::Property< std::vector< std::string > > m_componentsToSave
Definition BaseSink.h:138
StatusCode stop() override
Definition BaseSink.h:89
virtual void flush(bool isStop)=0
pure virtual method to be defined by children and responsible for flushing current data of the Sink.
StatusCode initialize() override
Definition BaseSink.h:46
void applyToAllEntities(Callable func) const
applies a callable to all monitoring entities
Definition BaseSink.h:101
void registerEntity(Hub::Entity ent) override
handles registration of a new entity
Definition BaseSink.h:52
StatusCode start() override
Definition BaseSink.h:73
void removeEntity(Hub::Entity const &ent) override
handles removal of an entity
Definition BaseSink.h:60
std::thread m_flushThread
Handling of regular flushes, if requested.
Definition BaseSink.h:144
std::set< Gaudi::Monitoring::Hub::Entity, EntityOrder > m_monitoringEntities
Definition BaseSink.h:135
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::Property< std::vector< std::string > > m_namesToSave
Definition BaseSink.h:136
Gaudi::Property< float > m_autoFlushPeriod
Definition BaseSink.h:146
Service(std::string name, ISvcLocator *svcloc)
Standard Constructor.
Definition Service.cpp:340
std::promise< void > m_flushThreadStop
Definition BaseSink.h:145
Gaudi::Property< std::vector< std::string > > m_typesToSave
Definition BaseSink.h:140
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
Wrapper class for arbitrary monitoring objects.
std::string component
name of the component owning the Entity
void * id() const
unique identifier, actually mapped to internal pointer
std::string type
type of the entity, see comment above concerning its format and usage
std::string name
name of the entity
Implementation of property with value of concrete type.
Definition PropertyFwd.h:27
Gaudi::Monitoring::Hub & monitoringHub()
SmartIF< ISvcLocator > & serviceLocator() const override
Retrieve pointer to service locator.
Definition Service.cpp:336
const std::string & name() const override
Retrieve name of the service.
Definition Service.cpp:333
StatusCode stop() override
Definition Service.cpp:181
StatusCode start() override
Definition Service.cpp:187
StatusCode initialize() override
Definition Service.cpp:118
Service(std::string name, ISvcLocator *svcloc)
Standard Constructor.
Definition Service.cpp:340
This class is used for returning status codes from appropriate routines.
Definition StatusCode.h:64
StatusCode andThen(F &&f, ARGS &&... args) const
Chain code blocks making the execution conditional a success result.
Definition StatusCode.h:163
list of entities we are dealing with
Definition BaseSink.h:130
bool operator()(const Gaudi::Monitoring::Hub::Entity &lhs, const Gaudi::Monitoring::Hub::Entity &rhs) const
Definition BaseSink.h:131
Interface reporting services must implement.
void addSink(Sink *sink)