The Gaudi Framework  master (37c0b60a)
MonitoringHub.h
Go to the documentation of this file.
1 /*****************************************************************************\
2 * (c) Copyright 2020-2024 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 
13 #include <GaudiKernel/detected.h>
14 
15 #include <deque>
16 #include <functional>
17 #include <iostream>
18 #include <nlohmann/json.hpp>
19 #include <string>
20 #include <typeindex>
21 #include <typeinfo>
22 
23 namespace Gaudi::Monitoring {
24 
25  namespace details {
26 
27  // type trait checking whether a given type has a friend method reset
28  template <typename Arg, typename = void> // this 3rd parameter defaults to void
30  template <typename Arg>
31  struct has_reset_method<Arg, std::void_t<decltype( reset( std::declval<Arg&>() ) )>> : std::true_type {};
32  template <typename Arg>
34 
35  // type trait checking whether a given type has a friend method mergeAndReset
36  template <typename Arg, typename = void> // this 3rd parameter defaults to void
38  template <typename Arg>
40  Arg, std::void_t<decltype( mergeAndReset( std::declval<Arg&>(), std::declval<Arg&>() ) )>> : std::true_type {};
41  template <typename Arg>
43 
44  } // namespace details
45 
50  struct Hub {
65  class Entity {
66  public:
67  template <typename T>
70  , name{ std::move( name ) }
71  , type{ std::move( type ) }
72  , m_ptr{ &ent }
73  , m_typeIndex{ typeid( T ) }
74  , m_getJSON{ []( void const* ptr ) -> nlohmann::json { return *reinterpret_cast<const T*>( ptr ); } }
75  , m_reset{ []( void* ptr ) {
76  if constexpr ( details::has_reset_method_v<T> ) { reset( *reinterpret_cast<T*>( ptr ) ); }
77  } }
78  , m_mergeAndReset{ []( void* e, void* o ) {
79  if constexpr ( details::has_mergeAndReset_method_v<T> ) {
80  mergeAndReset( *reinterpret_cast<T*>( e ), *reinterpret_cast<T*>( o ) );
81  }
82  } } {}
93  j = std::invoke( e.m_getJSON, e.m_ptr );
94  }
96  friend void reset( Entity const& e ) { std::invoke( e.m_reset, e.m_ptr ); }
103  friend void mergeAndReset( Entity const& ent, Entity const& other ) {
104  if ( ent.typeIndex() != other.typeIndex() ) {
105  throw std::runtime_error( std::string( "Entity: mergeAndReset called on different types: " ) +
106  ent.typeIndex().name() + " and " + other.typeIndex().name() );
107  }
108  std::invoke( ent.m_mergeAndReset, ent.m_ptr, other.m_ptr );
109  }
111  bool operator==( Entity const& ent ) const { return id() == ent.id(); }
113  void* id() const { return m_ptr; }
114 
115  private:
117  void* m_ptr{ nullptr };
118  // The next 4 members are needed for type erasure
119  // indeed, their implementation is internal type dependant
120  // (see Constructor above and the usage of T in the reinterpret_cast)
123  nlohmann::json ( *m_getJSON )( void const* );
125  void ( *m_reset )( void* );
127  void ( *m_mergeAndReset )( void*, void* );
128  };
129 
131  struct Sink {
132  virtual void registerEntity( Entity ent ) = 0;
133  virtual void removeEntity( Entity const& ent ) = 0;
134  virtual ~Sink() = default;
135  };
136 
137  Hub() { m_sinks.reserve( 5 ); }
138 
139  template <typename T>
141  registerEntity( { std::move( c ), std::move( n ), std::move( t ), ent } );
142  }
143  void registerEntity( Entity ent ) {
144  std::for_each( begin( m_sinks ), end( m_sinks ), [ent]( auto sink ) { sink->registerEntity( ent ); } );
145  m_entities.emplace( ent.id(), std::move( ent ) );
146  }
147  template <typename T>
148  void removeEntity( T& ent ) {
149  auto it = m_entities.find( &ent );
150  if ( it != m_entities.end() ) {
151  std::for_each( begin( m_sinks ), end( m_sinks ), [&it]( auto sink ) { sink->removeEntity( it->second ); } );
152  m_entities.erase( it );
153  }
154  }
155 
156  void addSink( Sink* sink ) {
158  [sink]( auto ent ) { sink->registerEntity( ent.second ); } );
159  m_sinks.push_back( sink );
160  }
161  void removeSink( Sink* sink ) {
162  auto it = std::find( begin( m_sinks ), end( m_sinks ), sink );
163  if ( it != m_sinks.end() ) m_sinks.erase( it );
164  }
165 
166  private:
169  };
170 } // namespace Gaudi::Monitoring
Gaudi::Monitoring::details::has_reset_method_v
constexpr bool has_reset_method_v
Definition: MonitoringHub.h:33
Gaudi::Monitoring::Hub::Sink
Interface reporting services must implement.
Definition: MonitoringHub.h:131
std::for_each
T for_each(T... args)
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:127
Gaudi::Monitoring::Hub::Entity::name
std::string name
name of the entity
Definition: MonitoringHub.h:86
std::false_type
std::string
STL class.
Gaudi::Monitoring::details::has_mergeAndReset_method
Definition: MonitoringHub.h:37
Gaudi::Monitoring::Hub::m_sinks
std::vector< Sink * > m_sinks
Definition: MonitoringHub.h:167
std::move
T move(T... args)
std::vector
STL class.
std::find
T find(T... args)
std::type_index::name
T name(T... args)
jsonFromLHCbLog.json
json
Definition: jsonFromLHCbLog.py:86
std::type_index
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:92
Gaudi::Monitoring::Hub::addSink
void addSink(Sink *sink)
Definition: MonitoringHub.h:156
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:111
Gaudi::Utils::begin
AttribStringParser::Iterator begin(const AttribStringParser &parser)
Definition: AttribStringParser.h:136
Gaudi::Monitoring::Hub::Sink::registerEntity
virtual void registerEntity(Entity ent)=0
details
Definition: AnyDataWrapper.h:19
ProduceConsume.j
j
Definition: ProduceConsume.py:104
Gaudi::Monitoring::Hub::removeSink
void removeSink(Sink *sink)
Definition: MonitoringHub.h:161
Gaudi::Monitoring::Hub::Entity::m_getJSON
nlohmann::json(* m_getJSON)(void const *)
function converting the internal data to json.
Definition: MonitoringHub.h:123
Gaudi::Monitoring::Hub::registerEntity
void registerEntity(std::string c, std::string n, std::string t, T &ent)
Definition: MonitoringHub.h:140
std::runtime_error
STL class.
Gaudi::Monitoring::Hub::Entity::typeIndex
std::type_index typeIndex() const
function to get internal type
Definition: MonitoringHub.h:90
Gaudi::Monitoring::Hub::removeEntity
void removeEntity(T &ent)
Definition: MonitoringHub.h:148
std::map
STL class.
Gaudi::Monitoring::Hub::Entity::id
void * id() const
unique identifier, actually mapped to internal pointer
Definition: MonitoringHub.h:113
Gaudi::Monitoring::details::has_mergeAndReset_method_v
constexpr bool has_mergeAndReset_method_v
Definition: MonitoringHub.h:42
Gaudi::Monitoring::Hub::Entity::m_typeIndex
std::type_index m_typeIndex
Definition: MonitoringHub.h:121
cpluginsvc.n
n
Definition: cpluginsvc.py:234
Gaudi::Monitoring::Hub::registerEntity
void registerEntity(Entity ent)
Definition: MonitoringHub.h:143
Gaudi::Monitoring::Hub::Entity::m_ptr
void * m_ptr
pointer to the actual data inside this Entity
Definition: MonitoringHub.h:117
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:103
std
STL namespace.
Gaudi::Monitoring::Hub::Entity::Entity
Entity(std::string component, std::string name, std::string type, T &ent)
Definition: MonitoringHub.h:68
detected.h
Gaudi::Monitoring::Hub::m_entities
std::map< void *, Entity > m_entities
Definition: MonitoringHub.h:168
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:50
Gaudi::Monitoring::Hub::Entity::reset
friend void reset(Entity const &e)
function resetting internal data
Definition: MonitoringHub.h:96
Gaudi::Monitoring::Hub::Entity
Wrapper class for arbitrary monitoring objects.
Definition: MonitoringHub.h:65
Gaudi::Monitoring::details::has_reset_method
Definition: MonitoringHub.h:29
Gaudi::Monitoring::Hub::Hub
Hub()
Definition: MonitoringHub.h:137
Gaudi::Monitoring::Hub::Entity::m_reset
void(* m_reset)(void *)
function reseting internal data.
Definition: MonitoringHub.h:125
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:88
Gaudi::Monitoring::Hub::Entity::component
std::string component
name of the component owning the Entity
Definition: MonitoringHub.h:84