perfc 0.11.0
Loading...
Searching...
No Matches
perfc::Register< CounterTypes, TMetadata, TMutex > Class Template Reference

Register of counter variants, used for example to facilitate discovery/monitoring. More...

#include <perfc/register.hpp>

Detailed Description

template<class CounterTypes, class TMetadata, class TMutex = std::recursive_mutex>
class perfc::Register< CounterTypes, TMetadata, TMutex >

Register of counter variants, used for example to facilitate discovery/monitoring.

Note
A counter is uniquely identified by its address. If a counter is moved it must first be deregistered and then re-registered afterwards.

To define a register for double and std::uint64_t and a std::string as metadata:

#include <string>
#include <perfc/perfc.hpp>
Register register;
ScopedRegistration reg = register.Add(&counter, "metadata for counter");
Register of counter variants, used for example to facilitate discovery/monitoring.
Definition register.hpp:257
RAII object to manage automatic deregistration.
Definition register.hpp:149
Counter< double, void > CounterDouble
Definition counter.hpp:498
Counter< std::uint64_t, void > CounterU64
Definition counter.hpp:499
Convenience header that includes all perfc headers.
Helper used when declaring counter types in perfc::Register.
Definition register.hpp:46
Thread Safety
thread-safe – All operations take a mutex lock and are thread safe if Mutex type provides mutual exclusion (i.e. not a null-opt mutex). Register::Lock() returns a pointer-like object that holds a lock, providing mutual exclusion.
Template Parameters
CounterTypesSpecifies which counter types can be registered using the helper perfc::CounterTypes.
TMetadataSpecifies the metadata type used to associate each registered counter with. Type requirements: CopyConstructible and CopyAssignable
TMutexThe mutex type to use, defaults to std::recursive_mutex.

Definition at line 257 of file register.hpp.

Classes

struct  Entry
 Register entry representing the counter and associated metadata. More...
 

Public Types

using Mutex = TMutex
 
using MetadataType = TMetadata
 
using CounterVariant = typename CounterTypes::CounterVariant
 
using Container = std::vector<Entry>
 
using LockedContainer = Locked<Container, Mutex>
 
using LockedContainerConst = Locked<Container const, Mutex>
 

Public Member Functions

Access container of counters thread safely

Obtains proxy object to counters that holds lock for synchronized access.

Prefer a new nested scope to minimize lock duration:

// Minimal scope for Lock()
{
auto counters = register.Lock();
for (auto const& counter : *counters) {
...
}
}
Exceptions
Exceptionoriginating from Mutex::lock()
LockedContainer Lock ()
 
LockedContainerConst Lock () const
 
Counter registration
Note
Manual de-registration with Register::Remove is not necessary unless ScopedRegistration is released from managing the registration.
ScopedRegistration Add (CounterVariant counter, MetadataType metadata)
 Add a counter, identified by its address, together with metadata to the register.
 
bool Remove (CounterVariant counter)
 Remove counter from register.
 

Member Typedef Documentation

◆ Container

template<class CounterTypes, class TMetadata, class TMutex = std::recursive_mutex>
using perfc::Register< CounterTypes, TMetadata, TMutex >::Container = std::vector<Entry>

Definition at line 271 of file register.hpp.

◆ CounterVariant

template<class CounterTypes, class TMetadata, class TMutex = std::recursive_mutex>
using perfc::Register< CounterTypes, TMetadata, TMutex >::CounterVariant = typename CounterTypes::CounterVariant

Definition at line 261 of file register.hpp.

◆ LockedContainer

template<class CounterTypes, class TMetadata, class TMutex = std::recursive_mutex>
using perfc::Register< CounterTypes, TMetadata, TMutex >::LockedContainer = Locked<Container, Mutex>

Definition at line 272 of file register.hpp.

◆ LockedContainerConst

template<class CounterTypes, class TMetadata, class TMutex = std::recursive_mutex>
using perfc::Register< CounterTypes, TMetadata, TMutex >::LockedContainerConst = Locked<Container const, Mutex>

Definition at line 273 of file register.hpp.

◆ MetadataType

template<class CounterTypes, class TMetadata, class TMutex = std::recursive_mutex>
using perfc::Register< CounterTypes, TMetadata, TMutex >::MetadataType = TMetadata

Definition at line 260 of file register.hpp.

◆ Mutex

template<class CounterTypes, class TMetadata, class TMutex = std::recursive_mutex>
using perfc::Register< CounterTypes, TMetadata, TMutex >::Mutex = TMutex

Definition at line 259 of file register.hpp.

Member Function Documentation

◆ Add()

template<class CounterTypes, class TMetadata, class TMutex = std::recursive_mutex>
ScopedRegistration perfc::Register< CounterTypes, TMetadata, TMutex >::Add ( CounterVariant counter,
MetadataType metadata )
inlinenodiscard

Add a counter, identified by its address, together with metadata to the register.

Precondition
counter must point to a valid CounterType object.
Note
The caller must ensure that the counter is deregistered before the counter is deleted. The returned object aims to facilitate this.
May invalidate iterators.
Parameters
counterPointer to any of the supported counter types.
metadataA metadata object to be associated with counter.
Returns
Empty ScopedRegistration if counter already has been registered before.
Non-empty RAII registration management object that automatically deregisters the counter when destroyed (or manually released).
Exceptions
std::exception-derivedexceptions originating from standard library.

Definition at line 328 of file register.hpp.

◆ Lock() [1/2]

template<class CounterTypes, class TMetadata, class TMutex = std::recursive_mutex>
LockedContainer perfc::Register< CounterTypes, TMetadata, TMutex >::Lock ( )
inline
Returns
locked access to container of counters.

Definition at line 293 of file register.hpp.

◆ Lock() [2/2]

template<class CounterTypes, class TMetadata, class TMutex = std::recursive_mutex>
LockedContainerConst perfc::Register< CounterTypes, TMetadata, TMutex >::Lock ( ) const
inline
Returns
locked access to const container of counters.

Definition at line 297 of file register.hpp.

◆ Remove()

template<class CounterTypes, class TMetadata, class TMutex = std::recursive_mutex>
bool perfc::Register< CounterTypes, TMetadata, TMutex >::Remove ( CounterVariant counter)
inline

Remove counter from register.

Note
May invalidate iterators.
Returns
true if counter was found and erased, false otherwise.
Exceptions
std::exception-derivedexceptions originating from standard library.

Definition at line 346 of file register.hpp.


The documentation for this class was generated from the following file: