The Gaudi Framework  master (540bd3e5)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
MonitoringHub.h
Go to the documentation of this file.
1 /*****************************************************************************\
2 * (c) Copyright 2020-2025 CERN for the benefit of the LHCb Collaboration *
3 * *
4 * This software is distributed under the terms of the GNU General Public *
5 * Licence version 3 (GPL Version 3), copied verbatim in the file "COPYING". *
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 #include <deque>
13 #include <functional>
14 #include <iostream>
15 #include <nlohmann/json.hpp>
16 #include <string>
17 #include <typeindex>
18 #include <typeinfo>
19 
20 namespace Gaudi::Monitoring {
21 
22  namespace details {
23 
24  // type trait checking whether a given type has a friend method reset
25  template <typename Arg, typename = void> // this 3rd parameter defaults to void
26  struct has_reset_method : std::false_type {};
27  template <typename Arg>
28  struct has_reset_method<Arg, std::void_t<decltype( reset( std::declval<Arg&>() ) )>> : std::true_type {};
29  template <typename Arg>
31 
32  // type trait checking whether a given type has a friend method mergeAndReset
33  template <typename Arg, typename = void> // this 3rd parameter defaults to void
34  struct has_mergeAndReset_method : std::false_type {};
35  template <typename Arg>
37  Arg, std::void_t<decltype( mergeAndReset( std::declval<Arg&>(), std::declval<Arg&>() ) )>> : std::true_type {};
38  template <typename Arg>
40 
41  } // namespace details
42 
47  struct Hub {
62  class Entity {
63  public:
64  template <typename T>
65  Entity( std::string component, std::string name, std::string type, T& ent )
66  : component{ std::move( component ) }
67  , name{ std::move( name ) }
68  , type{ std::move( type ) }
69  , m_ptr{ &ent }
70  , m_typeIndex{ typeid( T ) }
71  , m_getJSON{ []( void const* ptr ) -> nlohmann::json { return *reinterpret_cast<const T*>( ptr ); } }
72  , m_reset{ []( void* ptr ) {
73  if constexpr ( details::has_reset_method_v<T> ) { reset( *reinterpret_cast<T*>( ptr ) ); }
74  } }
75  , m_mergeAndReset{ []( void* e, void* o ) {
76  if constexpr ( details::has_mergeAndReset_method_v<T> ) {
77  mergeAndReset( *reinterpret_cast<T*>( e ), *reinterpret_cast<T*>( o ) );
78  }
79  } } {}
81  std::string component;
83  std::string name;
85  std::string type;
87  std::type_index typeIndex() const { return m_typeIndex; }
90  j = std::invoke( e.m_getJSON, e.m_ptr );
91  }
93  friend void reset( Entity const& e ) { std::invoke( e.m_reset, e.m_ptr ); }
100  friend void mergeAndReset( Entity const& ent, Entity const& other ) {
101  if ( ent.typeIndex() != other.typeIndex() ) {
102  throw std::runtime_error( std::string( "Entity: mergeAndReset called on different types: " ) +
103  ent.typeIndex().name() + " and " + other.typeIndex().name() );
104  }
105  std::invoke( ent.m_mergeAndReset, ent.m_ptr, other.m_ptr );
106  }
108  bool operator==( Entity const& ent ) const { return id() == ent.id(); }
110  void* id() const { return m_ptr; }
111 
112  private:
114  void* m_ptr{ nullptr };
115  // The next 4 members are needed for type erasure
116  // indeed, their implementation is internal type dependant
117  // (see Constructor above and the usage of T in the reinterpret_cast)
118  std::type_index m_typeIndex;
120  nlohmann::json ( *m_getJSON )( void const* );
122  void ( *m_reset )( void* );
124  void ( *m_mergeAndReset )( void*, void* );
125  };
126 
128  struct Sink {
129  virtual void registerEntity( Entity ent ) = 0;
130  virtual void removeEntity( Entity const& ent ) = 0;
131  virtual ~Sink() = default;
132  };
133 
134  Hub() { m_sinks.reserve( 5 ); }
135 
136  template <typename T>
137  void registerEntity( std::string c, std::string n, std::string t, T& ent ) {
138  registerEntity( { std::move( c ), std::move( n ), std::move( t ), ent } );
139  }
140  void registerEntity( Entity ent ) {
141  std::for_each( begin( m_sinks ), end( m_sinks ), [ent]( auto sink ) { sink->registerEntity( ent ); } );
142  m_entities.emplace( ent.id(), std::move( ent ) );
143  }
144  template <typename T>
145  void removeEntity( T& ent ) {
146  auto it = m_entities.find( &ent );
147  if ( it != m_entities.end() ) {
148  std::for_each( begin( m_sinks ), end( m_sinks ), [&it]( auto sink ) { sink->removeEntity( it->second ); } );
149  m_entities.erase( it );
150  }
151  }
152 
153  void addSink( Sink* sink ) {
155  [sink]( auto ent ) { sink->registerEntity( ent.second ); } );
156  m_sinks.push_back( sink );
157  }
158  void removeSink( Sink* sink ) {
159  auto it = std::find( begin( m_sinks ), end( m_sinks ), sink );
160  if ( it != m_sinks.end() ) m_sinks.erase( it );
161  }
162 
163  private:
164  std::vector<Sink*> m_sinks;
165  std::map<void*, Entity> m_entities;
166  };
167 } // namespace Gaudi::Monitoring
Gaudi::Monitoring::details::has_reset_method_v
constexpr bool has_reset_method_v
Definition: MonitoringHub.h:30
Gaudi::Monitoring::Hub::Sink
Interface reporting services must implement.
Definition: MonitoringHub.h:128
Gaudi::Monitoring::Hub::Entity::m_mergeAndReset
void(* m_mergeAndReset)(void *, void *)
function calling merge and reset on internal data with the internal data of another entity
Definition: MonitoringHub.h:124
Gaudi::Monitoring::Hub::Entity::name
std::string name
name of the entity
Definition: MonitoringHub.h:83
Gaudi::Monitoring::details::has_mergeAndReset_method
Definition: MonitoringHub.h:34
Gaudi::Monitoring::Hub::m_sinks
std::vector< Sink * > m_sinks
Definition: MonitoringHub.h:164
GaudiPartProp.decorators.std
std
Definition: decorators.py:32
jsonFromLHCbLog.json
json
Definition: jsonFromLHCbLog.py:86
Gaudi::Monitoring
Definition: JSONSink.cpp:19
gaudirun.c
c
Definition: gaudirun.py:525
Gaudi::Monitoring::Hub::Entity::to_json
friend void to_json(nlohmann::json &j, Gaudi::Monitoring::Hub::Entity const &e)
conversion to json via nlohmann library
Definition: MonitoringHub.h:89
Gaudi::Monitoring::Hub::addSink
void addSink(Sink *sink)
Definition: MonitoringHub.h:153
Gaudi::Monitoring::Hub::Sink::removeEntity
virtual void removeEntity(Entity const &ent)=0
bug_34121.t
t
Definition: bug_34121.py:31
Gaudi::Monitoring::Hub::Entity::operator==
bool operator==(Entity const &ent) const
operator== for comparison with an entity
Definition: MonitoringHub.h:108
Gaudi::Utils::begin
AttribStringParser::Iterator begin(const AttribStringParser &parser)
Definition: AttribStringParser.h:136
Gaudi::Monitoring::Hub::Sink::registerEntity
virtual void registerEntity(Entity ent)=0
Gaudi::cxx::for_each
void for_each(ContainerOfSynced &c, Fun &&f)
Definition: SynchronizedValue.h:98
details
Definition: AnyDataWrapper.h:19
ProduceConsume.j
j
Definition: ProduceConsume.py:104
Gaudi::Monitoring::Hub::removeSink
void removeSink(Sink *sink)
Definition: MonitoringHub.h:158
Gaudi::Monitoring::Hub::Entity::m_getJSON
nlohmann::json(* m_getJSON)(void const *)
function converting the internal data to json.
Definition: MonitoringHub.h:120
Gaudi::Monitoring::Hub::registerEntity
void registerEntity(std::string c, std::string n, std::string t, T &ent)
Definition: MonitoringHub.h:137
Gaudi::Monitoring::Hub::Entity::typeIndex
std::type_index typeIndex() const
function to get internal type
Definition: MonitoringHub.h:87
Gaudi::Monitoring::Hub::removeEntity
void removeEntity(T &ent)
Definition: MonitoringHub.h:145
Gaudi::Monitoring::Hub::Entity::id
void * id() const
unique identifier, actually mapped to internal pointer
Definition: MonitoringHub.h:110
Gaudi::Monitoring::details::has_mergeAndReset_method_v
constexpr bool has_mergeAndReset_method_v
Definition: MonitoringHub.h:39
Gaudi::Monitoring::Hub::Entity::m_typeIndex
std::type_index m_typeIndex
Definition: MonitoringHub.h:118
cpluginsvc.n
n
Definition: cpluginsvc.py:234
Gaudi::Monitoring::Hub::registerEntity
void registerEntity(Entity ent)
Definition: MonitoringHub.h:140
Gaudi::Monitoring::Hub::Entity::m_ptr
void * m_ptr
pointer to the actual data inside this Entity
Definition: MonitoringHub.h:114
Gaudi::Monitoring::Hub::Entity::mergeAndReset
friend void mergeAndReset(Entity const &ent, Entity const &other)
function calling merge and reset on internal data with the internal data of another entity
Definition: MonitoringHub.h:100
Gaudi::Monitoring::Hub::Entity::Entity
Entity(std::string component, std::string name, std::string type, T &ent)
Definition: MonitoringHub.h:65
Gaudi::Monitoring::Hub::m_entities
std::map< void *, Entity > m_entities
Definition: MonitoringHub.h:165
IOTest.end
end
Definition: IOTest.py:125
Gaudi::Monitoring::Hub
Central entity in a Gaudi application that manages monitoring objects (i.e.
Definition: MonitoringHub.h:47
Gaudi::Monitoring::Hub::Entity::reset
friend void reset(Entity const &e)
function resetting internal data
Definition: MonitoringHub.h:93
Gaudi::Monitoring::Hub::Entity
Wrapper class for arbitrary monitoring objects.
Definition: MonitoringHub.h:62
Gaudi::Monitoring::details::has_reset_method
Definition: MonitoringHub.h:26
Gaudi::Monitoring::Hub::Hub
Hub()
Definition: MonitoringHub.h:134
Gaudi::Monitoring::Hub::Entity::m_reset
void(* m_reset)(void *)
function reseting internal data.
Definition: MonitoringHub.h:122
Gaudi::Monitoring::Hub::Sink::~Sink
virtual ~Sink()=default
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