diff --git a/groups/bdl/bdlm/bdlm_metricsregistry.cpp b/groups/bdl/bdlm/bdlm_metricsregistry.cpp index 9eff88d597..48d3d7cc81 100644 --- a/groups/bdl/bdlm/bdlm_metricsregistry.cpp +++ b/groups/bdl/bdlm/bdlm_metricsregistry.cpp @@ -7,49 +7,293 @@ BSLS_IDENT_RCSID(bdlm_metricsregistry_cpp,"$Id$ $CSID$") #include +#include +#include #include +#include +#include + +#include + namespace BloombergLP { namespace bdlm { - // --------------------- - // class MetricsRegistry - // --------------------- + // ========================== + // class MetricsRegistry_Data + // ========================== + +class MetricsRegistry_Data { + // This is a *component* *private* type used to describe the data + // associated with a metrics registration. DO NOT USE. + + public: + // PUBLIC DATA + MetricDescriptor d_descriptor; + MetricsAdapter::Callback d_callback; + MetricsAdapter::CallbackHandle d_adaptedHandle; + + // TRAITS + BSLMF_NESTED_TRAIT_DECLARATION(MetricsRegistry_Data, + bslma::UsesBslmaAllocator); + + // CREATORS + MetricsRegistry_Data(); + // Create a 'MetricsRegistry_Data' object that uses the default + // allocator to supply memory. + + explicit MetricsRegistry_Data(bslma::Allocator *basicAllocator); + // Create a 'MetricsRegistry_Data' object that uses the specified + // 'basicAllocator' to supply memory. + + MetricsRegistry_Data(const MetricsRegistry_Data& original, + bslma::Allocator *basicAllocator = 0); + // Create a 'MetricsRegistry_Data' object having the same value as the + // specified 'original' object. Optionally specify a 'basicAllocator' + // to supply memory; otherwise, the default allocator is used. + + MetricsRegistry_Data(bslmf::MovableRef original) + BSLS_KEYWORD_NOEXCEPT; + // Create a 'MetricsRegistry_Data' object having the same value and the + // same allocator as the specified 'original' object. The value of + // 'original' becomes unspecified but valid, and its allocator remains + // unchanged. + + MetricsRegistry_Data( + bslmf::MovableRef original, + bslma::Allocator *basicAllocator); + // Create a 'MetricsRegistry_Data' object having the same value as the + // specified 'original' object, using the specified 'basicAllocator' to + // supply memory. The allocator of 'original' remains unchanged. If + // 'original' and the newly created object have the same allocator, + // then the value of 'original' becomes unspecified but valid, and no + // exceptions will be thrown; otherwise 'original' is unchanged and an + // exception may be thrown. + + // ACCESSORS + bslma::Allocator *allocator() const; + // Return the allocator used by this object to supply memory. +}; + + // ========================== + // class MetricsRegistry_Impl + // ========================== + +class MetricsRegistry_Impl { + // This class is a mechanism that implements the 'MetricsRegistry' class + // (which uses a "pimpl" design idiom). + + // PRIVATE TYPES + typedef MetricsAdapter::Callback Callback; + typedef MetricsAdapter::CallbackHandle CallbackHandle; + + public: + // TYPES + typedef bsl::map MetricDataMap; + + private: + // DATA + MetricsAdapter *d_metricsAdapter_p; // held, but not owned, metrics + // adapter + + MetricDataMap d_metricData; // registration data + + CallbackHandle d_nextKey; // next key for inserting into + // 'd_metricData' + + mutable bslmt::Mutex d_mutex; // mutex to protect the metrics + // registry and 'd_metricData' + + // FRIENDS + friend class MetricsRegistryRegistrationHandle; + + // NOT IMPLEMENTED + MetricsRegistry_Impl(const MetricsRegistry_Impl&) BSLS_KEYWORD_DELETED; + MetricsRegistry_Impl& operator=( + const MetricsRegistry_Impl&) BSLS_KEYWORD_DELETED; -// PRIVATE MANIPULATORS -int MetricsRegistry::removeCollectionCallback(const CallbackHandle& handle) + public: + // TRAITS + BSLMF_NESTED_TRAIT_DECLARATION(MetricsRegistry_Impl, + bslma::UsesBslmaAllocator); + + // CREATORS + explicit MetricsRegistry_Impl(bslma::Allocator *basicAllocator = 0); + // Create a 'MetricsRegistry' object that stores the information + // necessary to forward the register and unregister of metrics to a + // metrics registry supplied with 'setMetricsRegistry'. Optionally + // specify a 'basicAllocator' used to supply memory. If + // 'basicAllocator' is 0, the currently installed default allocator is + // used. + + ~MetricsRegistry_Impl(); + // Unregister all registered metrics and destroy this 'MetricsRegistry' + // object. + + // MANIPULATORS + CallbackHandle registerCollectionCallback( + const bdlm::MetricDescriptor& descriptor, + const Callback& callback); + // Register the metric described by the specified 'descriptor' and + // associate it with the specified 'callback' to collect data from the + // metric. Return a handle for the registered callback. The metric + // and associated callback remain registered with this registry until + // either the handle is unregistered or destroyed. When a + // 'MetricsAdapter' is associated with this registry using + // 'setMetricsAdapter', this objects registers all the registered + // metrics and callbacks with that adapter, and similarly unregisters + // them if the 'MetricAdapter' is later disassociated with this + // registry (either on this objects destruction, or due to a call to + // 'removeMetricsAdapter' or 'setMetricsAdapter'). In this way, a + // 'MetricsRegistry' serves as an intermediary between users of 'bdlm' + // that register metrics and the subsystem for collecting and + // publishing metrics being adapted by a concrete instance of + // 'bdlm::MetricAdapter'. + + int removeCollectionCallback(const CallbackHandle& handle); + // Remove the callback associated with the specified 'handle' from the + // associated adapter, if provided, and from this registry. Return 0 + // on success, or a non-zero value if 'handle' cannot be found. + + void removeMetricsAdapter(MetricsAdapter *adapter); + // If the specified 'adapter' is the currently associated registrar, + // remove all registered metrics from it and disassociate the registry. + // Note that this operation takes an 'adapter' to disambiguate + // multiple, potentially concurrent, calls to this method and + // 'setMetricsAdapter'. + + void setMetricsAdapter(MetricsAdapter *adapter); + // Configure this metrics registry to register all metrics collection + // callbacks with the specified 'adapter'. This operation first, if + // there is already an associated metrics adapter, unregisters all the + // collection callbacks from that adapter, then registers the + // collection callbacks with the new 'adapter'. + + // ACCESSORS + + int numRegisteredCollectionCallbacks() const; + // Return the number of registered metrics collection callbacks. + + // Aspects + + bslma::Allocator *allocator() const; + // Return the allocator used by this object to supply memory. +}; + + // ==================================== + // class MetricsRegistry_ElementProctor + // ==================================== + +class MetricsRegistry_ElementProctor { + // 'MetricDataMap' element proctor. + + // PRIVATE TYPES + typedef MetricsRegistry_Impl::MetricDataMap MetricDataMap; + + // DATA + MetricDataMap& d_map; + MetricDataMap::iterator d_it; + + // NOT IMPLEMENTED + MetricsRegistry_ElementProctor( + const MetricsRegistry_ElementProctor&) BSLS_KEYWORD_DELETED; + MetricsRegistry_ElementProctor &operator=( + const MetricsRegistry_ElementProctor&) BSLS_KEYWORD_DELETED; + public: + // CREATORS + MetricsRegistry_ElementProctor(MetricDataMap& map, + MetricDataMap::iterator iter); + // Create a proctor that conditionally manages an element in the + // specified 'map' pointed by the specified 'iter' by erasing it (if + // not released -- see 'release') upon destruction. + + ~MetricsRegistry_ElementProctor(); + // Destroy this proctor, and erase the element it manages if the + // 'release' member function wasn't called. + + // MANIPULATORS + void release(); + // Release from management the element currently managed by this + // proctor. +}; + // -------------------------- + // class MetricsRegistry_Data + // -------------------------- + +// CREATORS +inline +MetricsRegistry_Data::MetricsRegistry_Data() +: d_descriptor() +, d_callback() +, d_adaptedHandle() { - bslmt::LockGuard guard(&d_mutex); +} - MetricDataMap::iterator iter = d_metricData.find(handle); - if (iter != d_metricData.end()) { - if (d_metricsAdapter_p) { - d_metricsAdapter_p->removeCollectionCallback( - iter->second.d_handle); - } - d_metricData.erase(iter); - return 0; - } - return 1; +inline +MetricsRegistry_Data::MetricsRegistry_Data(bslma::Allocator *basicAllocator) +: d_descriptor(basicAllocator) +, d_callback(bsl::allocator_arg, basicAllocator) +, d_adaptedHandle() +{ } -// CLASS METHODS -MetricsRegistry& MetricsRegistry::singleton() +inline +MetricsRegistry_Data::MetricsRegistry_Data( + const MetricsRegistry_Data& original, + bslma::Allocator *basicAllocator) +: d_descriptor(original.d_descriptor, basicAllocator) +, d_callback(bsl::allocator_arg, basicAllocator, original.d_callback) +, d_adaptedHandle(original.d_adaptedHandle) { - static MetricsRegistry *s_metricsRegistry_p; +} - BSLMT_ONCE_DO { - static MetricsRegistry s_metricsRegistry( - bslma::Default::globalAllocator()); +inline +MetricsRegistry_Data::MetricsRegistry_Data( + bslmf::MovableRef original) BSLS_KEYWORD_NOEXCEPT +: d_descriptor(bslmf::MovableRefUtil::move( + bslmf::MovableRefUtil::access(original).d_descriptor)) +, d_callback(bslmf::MovableRefUtil::move( + bslmf::MovableRefUtil::access(original).d_callback)) +, d_adaptedHandle(bslmf::MovableRefUtil::move( + bslmf::MovableRefUtil::access(original).d_adaptedHandle)) +{ +} - s_metricsRegistry_p = &s_metricsRegistry; - } +inline +MetricsRegistry_Data::MetricsRegistry_Data( + bslmf::MovableRef original, + bslma::Allocator *basicAllocator) +: d_descriptor(bslmf::MovableRefUtil::move( + bslmf::MovableRefUtil::access(original).d_descriptor), basicAllocator) +, d_callback(bsl::allocator_arg, basicAllocator, bslmf::MovableRefUtil::move( + bslmf::MovableRefUtil::access(original).d_callback)) +, d_adaptedHandle(bslmf::MovableRefUtil::move( + bslmf::MovableRefUtil::access(original).d_adaptedHandle)) +{ +} - return *s_metricsRegistry_p; +// ACCESSORS + // Aspects +inline +bslma::Allocator *MetricsRegistry_Data::allocator() const +{ + return d_descriptor.allocator(); } + // -------------------------- + // class MetricsRegistry_Impl + // -------------------------- + // CREATORS -MetricsRegistry::~MetricsRegistry() +inline +MetricsRegistry_Impl::MetricsRegistry_Impl(bslma::Allocator *basicAllocator) +: d_metricsAdapter_p(0) +, d_metricData(basicAllocator) +, d_nextKey(0) +{ +} + +MetricsRegistry_Impl::~MetricsRegistry_Impl() { bslmt::LockGuard guard(&d_mutex); @@ -58,52 +302,77 @@ MetricsRegistry::~MetricsRegistry() iter != d_metricData.end(); ++iter) { d_metricsAdapter_p->removeCollectionCallback( - iter->second.d_handle); + iter->second.d_adaptedHandle); } d_metricsAdapter_p = 0; } } // MANIPULATORS -MetricsRegistryRegistrationHandle MetricsRegistry::registerCollectionCallback( +MetricsRegistry_Impl::CallbackHandle +MetricsRegistry_Impl::registerCollectionCallback( const bdlm::MetricDescriptor& descriptor, const Callback& callback) { bslmt::LockGuard guard(&d_mutex); - MetricsRegistry_Data& data = d_metricData[d_nextKey]; + bsl::pair res = + d_metricData.try_emplace(d_nextKey); + BSLS_ASSERT(res.second); + MetricsRegistry_ElementProctor proctor(d_metricData, res.first); + + MetricsRegistry_Data& data = res.first->second; data.d_descriptor = descriptor; data.d_callback = callback; - if (d_metricsAdapter_p) { - data.d_handle = + data.d_adaptedHandle = d_metricsAdapter_p->registerCollectionCallback(descriptor, callback); } - else { - data.d_handle = 0; - } - return MetricsRegistryRegistrationHandle(this, d_nextKey++); + proctor.release(); + return d_nextKey++; } -void MetricsRegistry::removeMetricsAdapter(MetricsAdapter *adapter) +int MetricsRegistry_Impl::removeCollectionCallback( + const CallbackHandle& handle) { bslmt::LockGuard guard(&d_mutex); - if (d_metricsAdapter_p && d_metricsAdapter_p == adapter) { + MetricDataMap::iterator iter = d_metricData.find(handle); + if (iter != d_metricData.end()) { + if (d_metricsAdapter_p) { + d_metricsAdapter_p->removeCollectionCallback( + iter->second.d_adaptedHandle); + } + d_metricData.erase(iter); + return 0; // RETURN + } + return 1; +} + +void MetricsRegistry_Impl::removeMetricsAdapter(MetricsAdapter *adapter) +{ + BSLS_ASSERT(adapter); + bslmt::LockGuard guard(&d_mutex); + + if (d_metricsAdapter_p == adapter) { for (MetricDataMap::iterator iter = d_metricData.begin(); iter != d_metricData.end(); ++iter) { d_metricsAdapter_p->removeCollectionCallback( - iter->second.d_handle); + iter->second.d_adaptedHandle); } d_metricsAdapter_p = 0; } + else { + BSLS_LOG_ERROR("Attempt to remove unknown 'MetricsAdapter'"); + } } -void MetricsRegistry::setMetricsAdapter(MetricsAdapter *adapter) +void MetricsRegistry_Impl::setMetricsAdapter(MetricsAdapter *adapter) { + BSLS_ASSERT(adapter); bslmt::LockGuard guard(&d_mutex); for (MetricDataMap::iterator iter = d_metricData.begin(); @@ -111,9 +380,9 @@ void MetricsRegistry::setMetricsAdapter(MetricsAdapter *adapter) ++iter) { if (d_metricsAdapter_p) { d_metricsAdapter_p->removeCollectionCallback( - iter->second.d_handle); + iter->second.d_adaptedHandle); } - iter->second.d_handle = adapter->registerCollectionCallback( + iter->second.d_adaptedHandle = adapter->registerCollectionCallback( iter->second.d_descriptor, iter->second.d_callback); } @@ -121,6 +390,177 @@ void MetricsRegistry::setMetricsAdapter(MetricsAdapter *adapter) d_metricsAdapter_p = adapter; } +// ACCESSORS + +int MetricsRegistry_Impl::numRegisteredCollectionCallbacks() const +{ + bslmt::LockGuard guard(&d_mutex); + return static_cast(d_metricData.size()); +} + + // Aspects + +inline +bslma::Allocator *MetricsRegistry_Impl::allocator() const +{ + return d_metricData.get_allocator().mechanism(); +} + + // ------------------------------------ + // class MetricsRegistry_ElementProctor + // ------------------------------------ + +// CREATORS +inline +MetricsRegistry_ElementProctor::MetricsRegistry_ElementProctor( + MetricDataMap& map, + MetricDataMap::iterator iter) +: d_map(map) +, d_it(iter) +{ +} + +inline +MetricsRegistry_ElementProctor::~MetricsRegistry_ElementProctor() +{ + if (d_it != d_map.end()) { + d_map.erase(d_it); + } +} + +// MANIPULATORS +inline +void MetricsRegistry_ElementProctor::release() +{ + d_it = d_map.end(); +} + + // --------------------- + // class MetricsRegistry + // --------------------- + +// CREATORS +MetricsRegistry::MetricsRegistry(bslma::Allocator *basicAllocator) +: d_impl(bsl::allocate_shared(basicAllocator)) +{ +} + +MetricsRegistry::~MetricsRegistry() +{ +} + +// CLASS METHODS +MetricsRegistry& MetricsRegistry::defaultInstance() +{ + static MetricsRegistry *s_metricsRegistry_p; + + BSLMT_ONCE_DO { + // Note that the 'MetricsRegistryImp' held by this 'MetricsRegistry' + // should be released when 's_metricsRegistry' is destroyed, but the + // control block for the shared pointer will not be released until all + // 'MetricsRegistryRegistrationHandle' objects that referred to the + // 'MetricsRegistry' have been destroyed. I.e., this may appear to + // leak the allocation of the control block if there are + // 'MetricsRegistryRegistrationHandle' objects that are not destroyed. + static MetricsRegistry s_metricsRegistry( + bslma::Default::globalAllocator()); + + s_metricsRegistry_p = &s_metricsRegistry; + } + + return *s_metricsRegistry_p; +} + +// MANIPULATORS +void MetricsRegistry::registerCollectionCallback( + MetricsRegistryRegistrationHandle *result, + const bdlm::MetricDescriptor& descriptor, + const Callback& callback) +{ + BSLS_ASSERT(result); + CallbackHandle key = d_impl->registerCollectionCallback(descriptor, + callback); + MetricsRegistryRegistrationHandle(d_impl, key).swap(*result); +} + +void MetricsRegistry::removeMetricsAdapter(MetricsAdapter *adapter) +{ + d_impl->removeMetricsAdapter(adapter); +} + +void MetricsRegistry::setMetricsAdapter(MetricsAdapter *adapter) +{ + d_impl->setMetricsAdapter(adapter); +} + +// ACCESSORS + +int MetricsRegistry::numRegisteredCollectionCallbacks() const +{ + return d_impl->numRegisteredCollectionCallbacks(); +} + + // Aspects + +bslma::Allocator *MetricsRegistry::allocator() const +{ + return d_impl->allocator(); +} + + // --------------------------------------- + // class MetricsRegistryRegistrationHandle + // --------------------------------------- + +// CREATORS +MetricsRegistryRegistrationHandle::MetricsRegistryRegistrationHandle( + const bsl::weak_ptr& registry, + MetricsRegistry::CallbackHandle handle) +: d_registry(registry) +, d_handle(handle) +{ +} + +MetricsRegistryRegistrationHandle::MetricsRegistryRegistrationHandle( + bslmf::MovableRef original) + BSLS_KEYWORD_NOEXCEPT +: d_registry(MoveUtil::move(MoveUtil::access(original).d_registry)) +, d_handle(MoveUtil::move(MoveUtil::access(original).d_handle)) +{ +} + +MetricsRegistryRegistrationHandle::~MetricsRegistryRegistrationHandle() +{ + unregister(); +} + +// MANIPULATORS +MetricsRegistryRegistrationHandle& +MetricsRegistryRegistrationHandle::operator=( + bslmf::MovableRef original) + BSLS_KEYWORD_NOEXCEPT +{ + MetricsRegistryRegistrationHandle& ref = original; + if (&ref != this) { + unregister(); + d_registry = MoveUtil::move(ref.d_registry); + d_handle = MoveUtil::move(ref.d_handle); + } + return *this; +} + +int MetricsRegistryRegistrationHandle::unregister() +{ + int rv = 0; + + bsl::shared_ptr registry = d_registry.lock(); + if (registry) { + rv = registry->removeCollectionCallback(d_handle); + } + d_registry.reset(); + + return rv; +} + } // close package namespace } // close enterprise namespace diff --git a/groups/bdl/bdlm/bdlm_metricsregistry.h b/groups/bdl/bdlm/bdlm_metricsregistry.h index 53547a3844..725da6693f 100644 --- a/groups/bdl/bdlm/bdlm_metricsregistry.h +++ b/groups/bdl/bdlm/bdlm_metricsregistry.h @@ -15,11 +15,22 @@ BSLS_IDENT("$Id: $") // bdlm::MetricsRegistryRegistrationHandle: registration handle providing RAII // //@DESCRIPTION: This component contains a mechanism, 'bdlm::MetricsRegistry', -// providing a metric registry that is transferable to implementations of the -// 'bdlm::MetricsAdapter' protocol, a singleton that is the default registry -// for the process, and a registration handle class, -// 'bdlm::MetricsRegistryRegistrationHandle', that provides RAII semantics for -// metric registration. +// that provides a registry of metrics that is transferable to implementations +// of the 'bdlm::MetricsAdapter' protocol. 'bdlm', as a low-level metrics +// facility, does not directly manage schedulers to collect metrics values or +// publishers to publish those value. Instead 'bdlm' is designed to allow +// applications to plug in different high-level feature-rich metrics collection +// and publication frameworks (without requiring a library dependency on those +// frameworks). A 'bdlm::MetricsRegistry' effectively serves as a proxy for a +// higher-level metrics collection system implementing the 'MetricsAdapter' +// protocol -- it keeps track of registered metrics allowing a higher-level +// metrics subsystem to be installed (by calling 'setMetricsAdapter) at any +// time, either before or after a metric is registered. +// +// A singleton instance of 'MetricsRegistry' is available from the +// 'defaultRegistry' class method. This component also provides a registration +// handle class, 'bdlm::MetricsRegistryRegistrationHandle', that provides RAII +// semantics for metric registration. // ///Thread Safety ///------------- @@ -33,50 +44,90 @@ BSLS_IDENT("$Id: $") // This section illustrates intended use of this component. // ///Example 1: Using 'bdlm::MetricsRegistry' -///- - - - - - - - - - - - - - - - - - - - - - - +///- - - - - - - - - - - - - - - - - - - - // This example demonstrates the initialization and usage of the // 'bdlm::MetricsRegistry' object, allowing for registering metric callback // functions with the 'bdlm' monitoring system. // -// First, we provide a metric function to be used during callback registration -// with the 'bdlm' monitoring system: +// First, we declare a class that provides some metric for the 'bdlm' +// monitoring system: +//.. +// class LowLevelFacility { +// // PRIVATE DATA +// bdlm::MetricsRegistryRegistrationHandle d_metricHandle; +// public: +// // CREATORS +// explicit LowLevelFacility(bdlm::MetricsRegistry& metricsRegistry = +// bdlm::MetricsRegistry::defaultInstance()); +// +// // ACCESSORS +// int someMetric() const +// { +// return 0; // just a stub +// } +// }; +//.. +// Next, we provide a metric function to be used during callback registration: //.. -// void elidedMetric(BloombergLP::bdlm::Metric *value) +// void metricCallback(bdlm::Metric *value, const LowLevelFacility *object) // { -// (void)value; -// // ... +// *value = bdlm::Metric::Gauge(object->someMetric()); // } //.. -// Then, we construct a 'bdlm::MetricsDescriptor' object to be used when -// registering the callback function: +// Here is the constructor definition that registers the collection callback: //.. -// bdlm::MetricDescriptor descriptor("bdlm", -// "example", -// 1, -// "bdlmmetricsregistry", -// "bmr", -// "identifier"); +// LowLevelFacility::LowLevelFacility(bdlm::MetricsRegistry& metricsRegistry) +// { +// // Construct a 'bdlm::MetricsDescriptor' object to be used when +// // registering the callback function: +// bdlm::MetricDescriptor descriptor("bdlm", +// "example", +// 1, +// "bdlmmetricsregistry", +// "bmr", +// "identifier"); +// +// // Register the collection callback: +// metricsRegistry.registerCollectionCallback( +// &d_metricHandle, +// descriptor, +// bdlf::BindUtil::bind(&metricCallback, +// bdlf::PlaceHolders::_1, +// this)); +// assert(d_metricHandle.isRegistered()); +// } //.. -// Next, we construct a 'bdlm::MetricsRegistry' object with a test allocator: +// Notice that the compiler-supplied destructor is sufficient because the +// 'd_metricHandle' will deregister the metric on destruction. +// +// Now, we construct a 'bdlm::MetricsRegistry' object with a test allocator: //.. -// bslma::TestAllocator ta; -// bdlm::MetricsRegistry registry(&ta); +// bslma::TestAllocator ta; +// bdlm::MetricsRegistry registry(&ta); +// assert(registry.numRegisteredCollectionCallbacks() == 0); //.. -// Now, we register the collection callback: +// Then, we create the object and pass the constructed 'bdlm::MetricsRegistry' +// object there: //.. -// bdlm::MetricsRegistryRegistrationHandle handle = -// registry.registerCollectionCallback(descriptor, -// &elidedMetric); +// { +// LowLevelFacility facility(registry); +// assert(registry.numRegisteredCollectionCallbacks() == 1); //.. -// Finally, we remove the callback from the monitoring system, and verify the -// callback was successfully removed: +// If we don't provide a 'bdlm::MetricsRegistry' object explicitly, the default +// global instance will be used. +// +// Finally, the callback is removed the monitoring system by the destructor of +// 'facility' object: //.. -// assert(0 == handle.unregister()); +// } // 'facility.d_metricHandle.unregister()' is called here +// assert(registry.numRegisteredCollectionCallbacks() == 0); //.. #include #include +#include + #include #include @@ -84,135 +135,45 @@ BSLS_IDENT("$Id: $") #include #include -#include -#include +#include #include -#include - namespace BloombergLP { namespace bdlm { class MetricsRegistryRegistrationHandle; - - // ========================== - // class MetricsRegistry_Data - // ========================== - -class MetricsRegistry_Data { - // This is a *component* *private* type used to describe the data - // associated with a metrics registration. DO NOT USE. - - public: - // PUBLIC DATA - MetricDescriptor d_descriptor; - MetricsAdapter::Callback d_callback; - MetricsAdapter::CallbackHandle d_handle; - - // TRAITS - BSLMF_NESTED_TRAIT_DECLARATION(MetricsRegistry_Data, - bslma::UsesBslmaAllocator); - - // CREATORS - MetricsRegistry_Data(); - // Create a 'MetricsRegistry_Data' object that uses the default - // allocator to supply memory. - - explicit MetricsRegistry_Data(bslma::Allocator *basicAllocator); - // Create a 'MetricsRegistry_Data' object that uses the specified - // 'basicAllocator' to supply memory. - - MetricsRegistry_Data(const MetricsRegistry_Data& original, - bslma::Allocator *basicAllocator = 0); - // Create a 'MetricsRegistry_Data' object having the same value as the - // specified 'original' object. Optionally specify a 'basicAllocator' - // to supply memory; otherwise, the default allocator is used. - - MetricsRegistry_Data(bslmf::MovableRef original) - BSLS_KEYWORD_NOEXCEPT; - // Create a 'MetricsRegistry_Data' object having the same value and the - // same allocator as the specified 'original' object. The value of - // 'original' becomes unspecified but valid, and its allocator remains - // unchanged. - - MetricsRegistry_Data( - bslmf::MovableRef original, - bslma::Allocator *basicAllocator); - // Create a 'MetricsRegistry_Data' object having the same value as the - // specified 'original' object, using the specified 'basicAllocator' to - // supply memory. The allocator of 'original' remains unchanged. If - // 'original' and the newly created object have the same allocator, - // then the value of 'original' becomes unspecified but valid, and no - // exceptions will be thrown; otherwise 'original' is unchanged and an - // exception may be thrown. - - // ACCESSORS - bslma::Allocator *allocator() const; - // Return the allocator used by this object to supply memory. -}; +class MetricsRegistry_Impl; // ===================== // class MetricsRegistry // ===================== class MetricsRegistry { - // This class implements a pure abstract interface for clients and - // suppliers of metrics registries. The implementation registers callbacks - // with an associated 'bdlm::MetricsRegistry' to enable monitoring of - // statistics collection objects. This class is *usually* a singleton. + // This class implements a mechanism that provides a registry of metrics + // that is transferable to implementations of the 'bdlm::MetricsAdapter' + // protocol. This class is *usually* a singleton. + + // DATA + bsl::shared_ptr d_impl; public: // TYPES typedef MetricsAdapter::Callback Callback; typedef MetricsAdapter::CallbackHandle CallbackHandle; - private: - // PRIVATE TYPES - typedef bsl::map MetricDataMap; - - // DATA - MetricsAdapter *d_metricsAdapter_p; // held, but not - // owned, metrics - // adapter - - MetricDataMap d_metricData; // registration data - - CallbackHandle d_nextKey; // next key for - // inserting into - // 'd_metricData' - - bslmt::Mutex d_mutex; // mutex to protect - // the metrics - // registry and - // 'd_metricData' - - // PRIVATE MANIPULATORS - int removeCollectionCallback(const CallbackHandle& handle); - // Remove the callback associated with the specified 'handle' from the - // associated adapter, if provided, and from this registry. Return 0 - // on success, or a non-zero value if 'handle' cannot be found. - - // FRIENDS - friend class MetricsRegistryRegistrationHandle; - - // NOT IMPLEMENTED - MetricsRegistry(const MetricsRegistry&) BSLS_KEYWORD_DELETED; - MetricsRegistry& operator=(const MetricsRegistry&) BSLS_KEYWORD_DELETED; - - public: // TRAITS BSLMF_NESTED_TRAIT_DECLARATION(MetricsRegistry, bslma::UsesBslmaAllocator); // CLASS METHODS - static MetricsRegistry& singleton(); + static MetricsRegistry& defaultInstance(); // Return a non-'const' reference to the metrics registry singleton. // CREATORS - MetricsRegistry(bslma::Allocator *basicAllocator = 0); + explicit MetricsRegistry(bslma::Allocator *basicAllocator = 0); // Create a 'MetricsRegistry' object that stores the information - // necessary to forward the register and unregister of metrics to a - // metrics registry supplied with 'setMetricsRegistry'. Optionally + // necessary to forward the registration and unregistration of metrics + // to an adapter supplied with 'setMetricsRegistry'. Optionally // specify a 'basicAllocator' used to supply memory. If // 'basicAllocator' is 0, the currently installed default allocator is // used. @@ -222,26 +183,46 @@ class MetricsRegistry { // object. // MANIPULATORS - MetricsRegistryRegistrationHandle registerCollectionCallback( - const bdlm::MetricDescriptor& descriptor, - const Callback& callback); - // Store the specified 'descriptor' and 'callback'. If an adapter has - // been provided, register the metric. The returned - // 'MetricsRegistryRegistrationHandle' will unsubscribe the callback by - // invoking 'unsubscribe'. + void registerCollectionCallback( + MetricsRegistryRegistrationHandle *result, + const bdlm::MetricDescriptor& descriptor, + const Callback& callback); + // Register the metric described by the specified 'descriptor' and + // associate it with the specified 'callback' to collect data from the + // metric, and load the specified 'result' with a handle can be used + // later to unregister the metric. After this operation completes, + // 'result->isRegistered()' will be 'true'. The metric and associated + // callback remain registered with this registry until either the + // handle is unregistered or destroyed. When a 'MetricsAdapter' is + // associated with this registry using 'setMetricsAdapter', this + // objects registers all the registered metrics and callbacks with that + // adapter, and similarly unregisters them if the 'MetricAdapter' is + // later disassociated with this registry (either on this objects + // destruction, or due to a call to 'removeMetricsAdapter' or + // 'setMetricsAdapter'). In this way, a 'MetricsRegistry' serves as an + // intermediary between users of 'bdlm' that register metrics and the + // subsystem for collecting and publishing metrics being adapted by a + // concrete instance of 'bdlm::MetricAdapter'. void removeMetricsAdapter(MetricsAdapter *adapter); // If the specified 'adapter' is the currently associated registrar, - // remove all metrics from it and disassociate the registry. + // remove all registered metrics from it and disassociate the registry. + // Note that this operation takes an 'adapter' to disambiguate + // multiple, potentially concurrent, calls to this method and + // 'setMetricsAdapter'. void setMetricsAdapter(MetricsAdapter *adapter); - // Transfer all metrics to the specified 'adapter'. If there is a - // currently associated adapter, remove all metrics from it. Register - // all metrics with the provided 'adapter' and define 'adapter' as the - // associated registry. + // Configure this metrics registry to register all metrics collection + // callbacks with the specified 'adapter'. This operation first, if + // there is already an associated metrics adapter, unregisters all the + // collection callbacks from that adapter, then registers the + // collection callbacks with the new 'adapter'. // ACCESSORS + int numRegisteredCollectionCallbacks() const; + // Return the number of registered metrics collection callbacks. + // Aspects bslma::Allocator *allocator() const; @@ -253,134 +234,84 @@ class MetricsRegistry { // ======================================= class MetricsRegistryRegistrationHandle { - // DATA - MetricsRegistry *d_registry; // associated registry - MetricsRegistry::CallbackHandle d_handle; // handle for registration + // This class implements a registration handle that provides RAII semantics + // for metric registration. - public: - // CREATORS - MetricsRegistryRegistrationHandle(); - // Create a default 'MetricsRegistryRegistrationHandle' object. + // PRIVATE TYPES + typedef bslmf::MovableRefUtil MoveUtil; - MetricsRegistryRegistrationHandle( - const MetricsRegistryRegistrationHandle& obj); - // Create a 'MetricsRegistryRegistrationHandle' object having the same - // registration as the specified 'obj'. + // DATA + bsl::weak_ptr d_registry; // associated registry + MetricsRegistry::CallbackHandle d_handle; // handle for registration + // PRIVATE CREATORS MetricsRegistryRegistrationHandle( - MetricsRegistry *registry, - MetricsRegistry::CallbackHandle handle); + const bsl::weak_ptr& registry, + MetricsRegistry::CallbackHandle handle); // Create a 'MetricsRegistryRegistrationHandle' object having the // associated specified 'registry' and the specified 'handle' in the // 'registry'. + // FRIENDS + friend class MetricsRegistry; + + // NOT IMPLEMENTED + MetricsRegistryRegistrationHandle( + const MetricsRegistryRegistrationHandle &) BSLS_KEYWORD_DELETED; + MetricsRegistryRegistrationHandle& operator=( + const MetricsRegistryRegistrationHandle &) BSLS_KEYWORD_DELETED; + public: + // CREATORS + MetricsRegistryRegistrationHandle(); + // Create a 'MetricsRegistryRegstrationHandle' object that is not + // associated with a registered metrics collection callback + // ('isRegistered' will return 'false'). + + MetricsRegistryRegistrationHandle( + bslmf::MovableRef original) + BSLS_KEYWORD_NOEXCEPT; + // Create a 'MetricsRegistryRegistrationHandle' object that will manage + // the metric collection callback registration associated with the + // specified 'original' handle. After creating this object, 'original' + // will no longer manage the registration, and + // 'original.isRegistered()' will be 'false'. If 'original' does not + // manage a registration when this object is created, then neither this + // object nor 'original' will manage a registration. + ~MetricsRegistryRegistrationHandle(); - // Unregister the metric and destroy this - // 'MetricsRegistryRegistrationHandle' object. + // Unregister the metric collection callback associated with this + // handle (if 'isRegistered' is 'true'), and destroy this object. // MANIPULATORS - MetricsRegistryRegistrationHandle& operator=( - const MetricsRegistryRegistrationHandle& rhs); - // Assign to this object the registration from the specified 'rhs' - // object to this object. + MetricsRegistryRegistrationHandle &operator=( + bslmf::MovableRef original) + BSLS_KEYWORD_NOEXCEPT; + // Unregister the metric collection callback associated with this + // handle (if 'isRegistered' is 'true'). Take an ownership on the + // metric collection callback registration associated with the + // specified 'original' handle. Afterwards, 'original' will no longer + // manage the registration, and 'original.isRegistered()' will be + // 'false'. + + void swap(MetricsRegistryRegistrationHandle& other) BSLS_KEYWORD_NOEXCEPT; + // Exchange the associated metric collection callback registration with + // the one controlled by the specified 'other' handle. int unregister(); // Unregister the metric from the associated registry. Return 0 on - // success, or a non-zero value if this handle cannot be found in the - // associated registry. + // success, and a non-zero value if this handle is not currently + // associated with a registered metrics collection callback. + + // ACCESSORS + bool isRegistered() const; + // Return 'true' if this handle has an associated registered metrics + // collection callback. }; // ============================================================================ // INLINE DEFINITIONS // ============================================================================ - // ========================== - // class MetricsRegistry_Data - // ========================== - -// CREATORS -inline -MetricsRegistry_Data::MetricsRegistry_Data() -: d_descriptor() -, d_callback() -, d_handle() -{ -} - -inline -MetricsRegistry_Data::MetricsRegistry_Data(bslma::Allocator *basicAllocator) -: d_descriptor(basicAllocator) -, d_callback(bsl::allocator_arg, basicAllocator) -, d_handle() -{ -} - -inline -MetricsRegistry_Data::MetricsRegistry_Data( - const MetricsRegistry_Data& original, - bslma::Allocator *basicAllocator) -: d_descriptor(original.d_descriptor, basicAllocator) -, d_callback(bsl::allocator_arg, basicAllocator, original.d_callback) -, d_handle(original.d_handle) -{ -} - -inline -MetricsRegistry_Data::MetricsRegistry_Data( - bslmf::MovableRef original) BSLS_KEYWORD_NOEXCEPT -: d_descriptor(bslmf::MovableRefUtil::move( - bslmf::MovableRefUtil::access(original).d_descriptor)) -, d_callback(bslmf::MovableRefUtil::move( - bslmf::MovableRefUtil::access(original).d_callback)) -, d_handle(bslmf::MovableRefUtil::move( - bslmf::MovableRefUtil::access(original).d_handle)) -{ -} - -inline -MetricsRegistry_Data::MetricsRegistry_Data( - bslmf::MovableRef original, - bslma::Allocator *basicAllocator) -: d_descriptor(bslmf::MovableRefUtil::move( - bslmf::MovableRefUtil::access(original).d_descriptor), basicAllocator) -, d_callback(bsl::allocator_arg, basicAllocator, bslmf::MovableRefUtil::move( - bslmf::MovableRefUtil::access(original).d_callback)) -, d_handle(bslmf::MovableRefUtil::move( - bslmf::MovableRefUtil::access(original).d_handle)) -{ -} - -// ACCESSORS - // Aspects -inline -bslma::Allocator *MetricsRegistry_Data::allocator() const -{ - return d_descriptor.allocator(); -} - - // --------------------- - // class MetricsRegistry - // --------------------- - -// CREATORS -inline -MetricsRegistry::MetricsRegistry(bslma::Allocator *basicAllocator) -: d_metricsAdapter_p(0) -, d_metricData(basicAllocator) -, d_nextKey(0) -{ -} - -// ACCESSORS - - // Aspects - -inline -bslma::Allocator *MetricsRegistry::allocator() const -{ - return d_metricData.get_allocator().mechanism(); -} - // --------------------------------------- // class MetricsRegistryRegistrationHandle // --------------------------------------- @@ -388,55 +319,25 @@ bslma::Allocator *MetricsRegistry::allocator() const // CREATORS inline MetricsRegistryRegistrationHandle::MetricsRegistryRegistrationHandle() -: d_registry(0) +: d_registry() , d_handle(0) { } -inline -MetricsRegistryRegistrationHandle::MetricsRegistryRegistrationHandle( - MetricsRegistry *registry, - MetricsRegistry::CallbackHandle handle) -: d_registry(registry) -, d_handle(handle) -{ -} - -inline -MetricsRegistryRegistrationHandle::MetricsRegistryRegistrationHandle( - const MetricsRegistryRegistrationHandle& obj) -: d_registry(obj.d_registry) -, d_handle(obj.d_handle) -{ -} - -inline -MetricsRegistryRegistrationHandle::~MetricsRegistryRegistrationHandle() -{ -} - // MANIPULATORS inline -MetricsRegistryRegistrationHandle& MetricsRegistryRegistrationHandle:: - operator=(const MetricsRegistryRegistrationHandle& rhs) +void MetricsRegistryRegistrationHandle::swap( + MetricsRegistryRegistrationHandle& other) BSLS_KEYWORD_NOEXCEPT { - d_registry = rhs.d_registry; - d_handle = rhs.d_handle; - - return *this; + bslalg::SwapUtil::swap(&d_registry, &other.d_registry); + bslalg::SwapUtil::swap(&d_handle, &other.d_handle); } +// ACCESSORS inline -int MetricsRegistryRegistrationHandle::unregister() +bool MetricsRegistryRegistrationHandle::isRegistered() const { - int rv = 1; - - if (d_registry) { - rv = d_registry->removeCollectionCallback(d_handle); - d_registry = 0; - } - - return rv; + return !d_registry.expired(); } } // close package namespace diff --git a/groups/bdl/bdlm/bdlm_metricsregistry.t.cpp b/groups/bdl/bdlm/bdlm_metricsregistry.t.cpp index 3f92eeb054..b156b4aa97 100644 --- a/groups/bdl/bdlm/bdlm_metricsregistry.t.cpp +++ b/groups/bdl/bdlm/bdlm_metricsregistry.t.cpp @@ -3,6 +3,8 @@ #include +#include + #include #include @@ -244,20 +246,61 @@ bool TestMetricsAdapter::verify(const bdlm::MetricDescriptor& descriptor) const // This section illustrates intended use of this component. // ///Example 1: Using 'bdlm::MetricsRegistry' -///- - - - - - - - - - - - - - - - - - - - - - - +///- - - - - - - - - - - - - - - - - - - - // This example demonstrates the initialization and usage of the // 'bdlm::MetricsRegistry' object, allowing for registering metric callback // functions with the 'bdlm' monitoring system. // -// First, we provide a metric function to be used during callback registration -// with the 'bdlm' monitoring system: +// First, we declare a class that provides some metric for the 'bdlm' +// monitoring system: +//.. + class LowLevelFacility { + // PRIVATE DATA + bdlm::MetricsRegistryRegistrationHandle d_metricHandle; + public: + // CREATORS + explicit LowLevelFacility(bdlm::MetricsRegistry& metricsRegistry = + bdlm::MetricsRegistry::defaultInstance()); + + // ACCESSORS + int someMetric() const + { + return 0; // just a stub + } + }; +//.. +// Next, we provide a metric function to be used during callback registration: +//.. + void metricCallback(bdlm::Metric *value, const LowLevelFacility *object) + { + *value = bdlm::Metric::Gauge(object->someMetric()); + } +//.. +// Here is the constructor definition that registers the collection callback: //.. - void elidedMetric(BloombergLP::bdlm::Metric *value) + LowLevelFacility::LowLevelFacility(bdlm::MetricsRegistry& metricsRegistry) { - (void)value; - // ... + // Construct a 'bdlm::MetricsDescriptor' object to be used when + // registering the callback function: + bdlm::MetricDescriptor descriptor("bdlm", + "example", + 1, + "bdlmmetricsregistry", + "bmr", + "identifier"); + + // Register the collection callback: + metricsRegistry.registerCollectionCallback( + &d_metricHandle, + descriptor, + bdlf::BindUtil::bind(&metricCallback, + bdlf::PlaceHolders::_1, + this)); + ASSERT(d_metricHandle.isRegistered()); } //.. +// Notice that the compiler-supplied destructor is sufficient because the +// 'd_metricHandle' will deregister the metric on destruction. // ============================================================================ // MAIN PROGRAM @@ -327,8 +370,8 @@ int main(int argc, char *argv[]) typedef bsl::function Callback; Callback fn(bsl::allocator_arg, &oa, LargeCallback()); - BloombergLP::bdlm::MetricsRegistryRegistrationHandle handle = - mX.registerCollectionCallback(md, fn); + BloombergLP::bdlm::MetricsRegistryRegistrationHandle handle; + mX.registerCollectionCallback(&handle, md, fn); ASSERTV(da.numBytesInUse(), da.numBlocksInUse(), @@ -357,31 +400,27 @@ int main(int argc, char *argv[]) << "USAGE EXAMPLE" << endl << "=============" << endl; -// Then, we construct a 'bdlm::MetricsDescriptor' object to be used when -// registering the callback function: -//.. - bdlm::MetricDescriptor descriptor("bdlm", - "example", - 1, - "bdlmmetricsregistry", - "bmr", - "identifier"); +// Now, we construct a 'bdlm::MetricsRegistry' object with a test allocator: //.. -// Next, we construct a 'bdlm::MetricsRegistry' object with a test allocator: + bslma::TestAllocator ta; + bdlm::MetricsRegistry registry(&ta); + ASSERT(registry.numRegisteredCollectionCallbacks() == 0); //.. - bslma::TestAllocator ta; - bdlm::MetricsRegistry registry(&ta); +// Then, we create the object and pass the constructed 'bdlm::MetricsRegistry' +// object there: //.. -// Now, we register the collection callback: -//.. - bdlm::MetricsRegistryRegistrationHandle handle = - registry.registerCollectionCallback(descriptor, - &elidedMetric); + { + LowLevelFacility facility(registry); + ASSERT(registry.numRegisteredCollectionCallbacks() == 1); //.. -// Finally, we remove the callback from the monitoring system, and verify the -// callback was successfully removed: +// If we don't provide a 'bdlm::MetricsRegistry' object explicitly, the default +// global instance will be used. +// +// Finally, the callback is removed the monitoring system by the destructor of +// 'facility' object: //.. - ASSERT(0 == handle.unregister()); + } // 'facility.d_metricHandle.unregister()' is called here + ASSERT(registry.numRegisteredCollectionCallbacks() == 0); //.. } break; case 1: { @@ -413,8 +452,8 @@ int main(int argc, char *argv[]) ASSERT(0 == adapter.size()); - ObjHandle handle = mX.registerCollectionCallback(descriptor, - &testMetric); + ObjHandle handle; + mX.registerCollectionCallback(&handle, descriptor, &testMetric); ASSERT(0 == adapter.size()); diff --git a/groups/bdl/bdlm/doc/bdlm.txt b/groups/bdl/bdlm/doc/bdlm.txt index d9a01def27..bd7b033823 100644 --- a/groups/bdl/bdlm/doc/bdlm.txt +++ b/groups/bdl/bdlm/doc/bdlm.txt @@ -4,10 +4,22 @@ @MNEMONIC: Basic Development Library Metrics (bdlm) -@DESCRIPTION: The 'bdlm' package provides a metrics registrar protocol (i.e., a - pure abstract interface). In addition, 'bdlm' also provides a singleton - "default metrics registry" that will then be visible to all BDE and - BDE-compliant code throughout that process. +@DESCRIPTION: The 'bdlm' package provides a means for low-level library + software to collect and publish metrics through a metric publishing framework, + without a library depending on the metrics publishing framework. The 'bdlm' + package provides a protocol (i.e., a pure abstract interface), + 'bdlm::MetricsAdapter', that can be implemented for (higher level) metrics + facilities. In addition, it also provides a registry of metrics, + 'bdlm::MetricRegistry', that allows low-level users of 'bdlm' to register + metrics at any time, serving as an intermediary with concrete + 'bdlm::MetricsAdapter' implementations (which may be configured before or + after the creation of any particular metric). + + As a low-level metrics facility, this package does not directly manage + schedulers to collect metrics values or publishers to publish collected + values. Instead it is designed to allow applications to plug in different + high-level feature-rich metrics collection and publication frameworks (without + requiring a library dependency on those frameworks). /Hierarchical Synopsis /--------------------- diff --git a/groups/bdl/bdlmt/bdlmt_eventscheduler.cpp b/groups/bdl/bdlmt/bdlmt_eventscheduler.cpp index 5b71c01f2a..5a515d0026 100644 --- a/groups/bdl/bdlmt/bdlmt_eventscheduler.cpp +++ b/groups/bdl/bdlmt/bdlmt_eventscheduler.cpp @@ -44,11 +44,10 @@ BSLS_IDENT_RCSID(bdlmt_eventscheduler_cpp,"$Id$ $CSID$") // Implementation note: When casting, we often cast through 'void *' or // 'const void *' to avoid getting alignment warnings. -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS namespace { -void startLagMetric(BloombergLP::bdlm::Metric *value, - BloombergLP::bdlmt::EventScheduler *object) +void startLagMetric(BloombergLP::bdlm::Metric *value, + const BloombergLP::bdlmt::EventScheduler *object) { BloombergLP::bsls::TimeInterval now = object->now(); BloombergLP::bsls::TimeInterval next = object->nextPendingEventTime(); @@ -62,7 +61,6 @@ void startLagMetric(BloombergLP::bdlm::Metric *value, } } // close unnamed namespace -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) namespace BloombergLP { @@ -320,13 +318,12 @@ void EventScheduler::dispatchEvents() } } -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS void EventScheduler::initialize(bdlm::MetricsRegistry *metricsRegistry, const bsl::string_view& metricsIdentifier) { bdlm::MetricsRegistry *registry = metricsRegistry - ? metricsRegistry - : &bdlm::MetricsRegistry::singleton(); + ? metricsRegistry + : &bdlm::MetricsRegistry::defaultInstance(); bdlm::InstanceCount::Value instanceNumber = bdlm::InstanceCount::nextInstanceNumber(); @@ -339,13 +336,13 @@ void EventScheduler::initialize(bdlm::MetricsRegistry *metricsRegistry, "es", metricsIdentifier); - d_startLagHandle = registry->registerCollectionCallback( + registry->registerCollectionCallback( + &d_startLagHandle, md, bdlf::BindUtil::bind(&startLagMetric, bdlf::PlaceHolders::_1, this)); } -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) void EventScheduler::releaseCurrentEvents() { @@ -459,14 +456,11 @@ EventScheduler::EventScheduler(bslma::Allocator *basicAllocator) , d_waitCount(0) , d_clockType(bsls::SystemClockType::e_REALTIME) { -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS initialize( 0, bdlm::MetricDescriptor::k_USE_METRICS_ADAPTER_OBJECT_ID_SELECTION); -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) } -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS EventScheduler::EventScheduler(const bsl::string_view& metricsIdentifier, bdlm::MetricsRegistry *metricsRegistry, bslma::Allocator *basicAllocator) @@ -488,7 +482,6 @@ EventScheduler::EventScheduler(const bsl::string_view& metricsIdentifier, { initialize(metricsRegistry, metricsIdentifier); } -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) EventScheduler::EventScheduler(bsls::SystemClockType::Enum clockType, bslma::Allocator *basicAllocator) @@ -508,14 +501,11 @@ EventScheduler::EventScheduler(bsls::SystemClockType::Enum clockType, , d_waitCount(0) , d_clockType(clockType) { -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS initialize( 0, bdlm::MetricDescriptor::k_USE_METRICS_ADAPTER_OBJECT_ID_SELECTION); -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) } -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS EventScheduler::EventScheduler(bsls::SystemClockType::Enum clockType, const bsl::string_view& metricsIdentifier, bdlm::MetricsRegistry *metricsRegistry, @@ -538,7 +528,6 @@ EventScheduler::EventScheduler(bsls::SystemClockType::Enum clockType, { initialize(metricsRegistry, metricsIdentifier); } -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) #ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY EventScheduler::EventScheduler( @@ -560,14 +549,11 @@ EventScheduler::EventScheduler( , d_waitCount(0) , d_clockType(bsls::SystemClockType::e_REALTIME) { -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS initialize( 0, bdlm::MetricDescriptor::k_USE_METRICS_ADAPTER_OBJECT_ID_SELECTION); -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) } -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS EventScheduler::EventScheduler( const bsl::chrono::system_clock&, const bsl::string_view& metricsIdentifier, @@ -591,7 +577,6 @@ EventScheduler::EventScheduler( { initialize(metricsRegistry, metricsIdentifier); } -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) EventScheduler::EventScheduler( const bsl::chrono::steady_clock&, @@ -612,14 +597,11 @@ EventScheduler::EventScheduler( , d_waitCount(0) , d_clockType(bsls::SystemClockType::e_MONOTONIC) { -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS initialize( 0, bdlm::MetricDescriptor::k_USE_METRICS_ADAPTER_OBJECT_ID_SELECTION); -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) } -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS EventScheduler::EventScheduler( const bsl::chrono::steady_clock&, const bsl::string_view& metricsIdentifier, @@ -643,7 +625,6 @@ EventScheduler::EventScheduler( { initialize(metricsRegistry, metricsIdentifier); } -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) #endif // defined(BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY) EventScheduler::EventScheduler( @@ -665,14 +646,11 @@ EventScheduler::EventScheduler( , d_waitCount(0) , d_clockType(bsls::SystemClockType::e_REALTIME) { -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS initialize( 0, bdlm::MetricDescriptor::k_USE_METRICS_ADAPTER_OBJECT_ID_SELECTION); -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) } -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS EventScheduler::EventScheduler( const EventScheduler::Dispatcher& dispatcherFunctor, const bsl::string_view& metricsIdentifier, @@ -696,7 +674,6 @@ EventScheduler::EventScheduler( { initialize(metricsRegistry, metricsIdentifier); } -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) EventScheduler::EventScheduler( const EventScheduler::Dispatcher& dispatcherFunctor, @@ -718,14 +695,11 @@ EventScheduler::EventScheduler( , d_waitCount(0) , d_clockType(clockType) { -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS initialize( 0, bdlm::MetricDescriptor::k_USE_METRICS_ADAPTER_OBJECT_ID_SELECTION); -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) } -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS EventScheduler::EventScheduler( const EventScheduler::Dispatcher& dispatcherFunctor, bsls::SystemClockType::Enum clockType, @@ -750,7 +724,6 @@ EventScheduler::EventScheduler( { initialize(metricsRegistry, metricsIdentifier); } -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) #ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY EventScheduler::EventScheduler( @@ -773,14 +746,11 @@ EventScheduler::EventScheduler( , d_waitCount(0) , d_clockType(bsls::SystemClockType::e_REALTIME) { -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS initialize( 0, bdlm::MetricDescriptor::k_USE_METRICS_ADAPTER_OBJECT_ID_SELECTION); -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) } -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS EventScheduler::EventScheduler( const EventScheduler::Dispatcher& dispatcherFunctor, const bsl::chrono::system_clock&, @@ -805,7 +775,6 @@ EventScheduler::EventScheduler( { initialize(metricsRegistry, metricsIdentifier); } -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) EventScheduler::EventScheduler( const EventScheduler::Dispatcher& dispatcherFunctor, @@ -827,14 +796,11 @@ EventScheduler::EventScheduler( , d_waitCount(0) , d_clockType(bsls::SystemClockType::e_MONOTONIC) { -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS initialize( 0, bdlm::MetricDescriptor::k_USE_METRICS_ADAPTER_OBJECT_ID_SELECTION); -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) } -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS EventScheduler::EventScheduler( const EventScheduler::Dispatcher& dispatcherFunctor, const bsl::chrono::steady_clock&, @@ -859,16 +825,11 @@ EventScheduler::EventScheduler( { initialize(metricsRegistry, metricsIdentifier); } -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) #endif // defined(BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY) EventScheduler::~EventScheduler() { BSLS_ASSERT(bslmt::ThreadUtil::invalidHandle() == d_dispatcherThread); - -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS - d_startLagHandle.unregister(); -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) } // MANIPULATORS diff --git a/groups/bdl/bdlmt/bdlmt_eventscheduler.h b/groups/bdl/bdlmt/bdlmt_eventscheduler.h index 0d6882db6b..de39376561 100644 --- a/groups/bdl/bdlmt/bdlmt_eventscheduler.h +++ b/groups/bdl/bdlmt/bdlmt_eventscheduler.h @@ -462,10 +462,6 @@ BSLS_IDENT("$Id: $") #include #endif -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS -#error "This component does not support metric collection at this time." -#endif - namespace BloombergLP { namespace bdlmt { @@ -702,10 +698,8 @@ class EventScheduler { bsls::SystemClockType::Enum d_clockType; // clock type used -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS bdlm::MetricsRegistryRegistrationHandle d_startLagHandle; // start lag metric handle -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) // PRIVATE CLASS METHODS static bsls::Types::Int64 returnZero(); @@ -770,14 +764,12 @@ class EventScheduler { // implements the dispatching thread. -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS void initialize(bdlm::MetricsRegistry *metricsRegistry, const bsl::string_view& metricsIdentifier); // Initialize this event scheduler using the stored attributes and the // specified 'metricsRegistry' and 'metricsIdentifier'. If // 'metricsRegistry' is 0, 'bdlm::MetricsRegistry::singleton()' is // used. -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) void releaseCurrentEvents(); // Release 'd_currentRecurringEvent' and 'd_currentEvent', if they @@ -855,7 +847,6 @@ class EventScheduler { // 'basicAllocator' is 0, the currently installed default allocator is // used. -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS explicit EventScheduler(const bsl::string_view& metricsIdentifier, bdlm::MetricsRegistry *metricsRegistry, bslma::Allocator *basicAllocator = 0); @@ -869,7 +860,6 @@ class EventScheduler { // used. Optionally specify a 'basicAllocator' used to supply memory. // If 'basicAllocator' is 0, the currently installed default allocator // is used. -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) explicit EventScheduler(bsls::SystemClockType::Enum clockType, bslma::Allocator *basicAllocator = 0); @@ -881,7 +871,6 @@ class EventScheduler { // 'basicAllocator' used to supply memory. If 'basicAllocator' is 0, // the currently installed default allocator is used. -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS explicit EventScheduler(bsls::SystemClockType::Enum clockType, const bsl::string_view& metricsIdentifier, bdlm::MetricsRegistry *metricsRegistry, @@ -897,7 +886,6 @@ class EventScheduler { // used. Optionally specify a 'basicAllocator' used to supply memory. // If 'basicAllocator' is 0, the currently installed default allocator // is used. -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) #ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY explicit EventScheduler( @@ -911,7 +899,6 @@ class EventScheduler { // 'basicAllocator' is 0, the currently installed default allocator is // used. -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS explicit EventScheduler( const bsl::chrono::system_clock&, const bsl::string_view& metricsIdentifier, @@ -927,7 +914,6 @@ class EventScheduler { // used. Optionally specify a 'basicAllocator' used to supply memory. // If 'basicAllocator' is 0, the currently installed default allocator // is used. -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) explicit EventScheduler( const bsl::chrono::steady_clock&, @@ -940,7 +926,6 @@ class EventScheduler { // 'basicAllocator' is 0, the currently installed default allocator is // used. -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS explicit EventScheduler( const bsl::chrono::steady_clock&, const bsl::string_view& metricsIdentifier, @@ -956,7 +941,6 @@ class EventScheduler { // used. Optionally specify a 'basicAllocator' used to supply memory. // If 'basicAllocator' is 0, the currently installed default allocator // is used. -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) #endif // defined(BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY) explicit EventScheduler(const Dispatcher& dispatcherFunctor, @@ -969,7 +953,6 @@ class EventScheduler { // 'basicAllocator' is 0, the currently installed default allocator is // used. -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS explicit EventScheduler(const Dispatcher& dispatcherFunctor, const bsl::string_view& metricsIdentifier, bdlm::MetricsRegistry *metricsRegistry, @@ -984,7 +967,6 @@ class EventScheduler { // used. Optionally specify a 'basicAllocator' used to supply memory. // If 'basicAllocator' is 0, the currently installed default allocator // is used. -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) EventScheduler(const Dispatcher& dispatcherFunctor, bsls::SystemClockType::Enum clockType, @@ -997,7 +979,6 @@ class EventScheduler { // 'basicAllocator' used to supply memory. If 'basicAllocator' is 0, // the currently installed default allocator is used. -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS EventScheduler(const Dispatcher& dispatcherFunctor, bsls::SystemClockType::Enum clockType, const bsl::string_view& metricsIdentifier, @@ -1014,7 +995,6 @@ class EventScheduler { // used. Optionally specify a 'basicAllocator' used to supply memory. // If 'basicAllocator' is 0, the currently installed default allocator // is used. -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) #ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY EventScheduler(const Dispatcher& dispatcherFunctor, @@ -1028,7 +1008,6 @@ class EventScheduler { // 'basicAllocator' is 0, the currently installed default allocator is // used. -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS EventScheduler(const Dispatcher& dispatcherFunctor, const bsl::chrono::system_clock&, const bsl::string_view& metricsIdentifier, @@ -1044,7 +1023,6 @@ class EventScheduler { // used. Optionally specify a 'basicAllocator' used to supply memory. // If 'basicAllocator' is 0, the currently installed default allocator // is used. -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) EventScheduler(const Dispatcher& dispatcherFunctor, const bsl::chrono::steady_clock&, @@ -1057,7 +1035,6 @@ class EventScheduler { // 'basicAllocator' is 0, the currently installed default allocator is // used. -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS EventScheduler(const Dispatcher& dispatcherFunctor, const bsl::chrono::steady_clock&, const bsl::string_view& metricsIdentifier, @@ -1073,7 +1050,6 @@ class EventScheduler { // used. Optionally specify a 'basicAllocator' used to supply memory. // If 'basicAllocator' is 0, the currently installed default allocator // is used. -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) #endif // defined(BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY) ~EventScheduler(); @@ -2204,8 +2180,6 @@ bslma::Allocator *EventScheduler::allocator() const } // close package namespace } // close enterprise namespace -#undef BDLMT_EVENTSCHEDULER_ENABLE_METRICS - #endif // ---------------------------------------------------------------------------- diff --git a/groups/bdl/bdlmt/bdlmt_eventscheduler.t.cpp b/groups/bdl/bdlmt/bdlmt_eventscheduler.t.cpp index f10322d19d..bcec0df62e 100644 --- a/groups/bdl/bdlmt/bdlmt_eventscheduler.t.cpp +++ b/groups/bdl/bdlmt/bdlmt_eventscheduler.t.cpp @@ -997,8 +997,6 @@ MakeChronoTest(bsl::chrono::duration start_time, } #endif -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS - // ======================== // class TestMetricsAdapter // ======================== @@ -1110,8 +1108,6 @@ bool TestMetricsAdapter::verify(const bsl::string& name) const && d_descriptors[0].objectIdentifier() == name; } -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) - // ============================================================================ // HELPER CLASSES AND FUNCTIONS FOR TESTING // ============================================================================ @@ -2880,10 +2876,9 @@ int main(int argc, char *argv[]) veryVeryVerbose = argc > 4; int nExec; -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS - // access the metrics registry singleton before assign the global allocator - bdlm::MetricsRegistry::singleton(); -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) + // access the metrics registry default instance before assign the global + // allocator + bdlm::MetricsRegistry::defaultInstance(); // CONCERN: 'BSLS_REVIEW' failures should lead to test failures. bsls::ReviewFailureHandlerGuard reviewGuard(&bsls::Review::failByAbort); @@ -3903,17 +3898,15 @@ int main(int argc, char *argv[]) << "============================" << "=============================" << endl; -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS TestMetricsAdapter defaultAdapter; TestMetricsAdapter otherAdapter; bdlm::MetricsRegistry& defaultRegistry = - bdlm::MetricsRegistry::singleton(); + bdlm::MetricsRegistry::defaultInstance(); bdlm::MetricsRegistry otherRegistry; defaultRegistry.setMetricsAdapter(&defaultAdapter); otherRegistry.setMetricsAdapter(&otherAdapter); -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) using namespace EVENTSCHEDULER_TEST_CASE_20; using namespace bdlf::PlaceHolders; @@ -3942,7 +3935,6 @@ int main(int argc, char *argv[]) x.stop(); } -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS ASSERT(defaultAdapter.verify("")); defaultAdapter.reset(); @@ -4040,7 +4032,6 @@ int main(int argc, char *argv[]) ASSERT(otherAdapter.verify("b")); otherAdapter.reset(); #endif // defined(BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY) -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) } break; case 19: { // -------------------------------------------------------------------- @@ -4068,17 +4059,15 @@ int main(int argc, char *argv[]) << "============================" << "=======================" << endl; -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS TestMetricsAdapter defaultAdapter; TestMetricsAdapter otherAdapter; bdlm::MetricsRegistry& defaultRegistry = - bdlm::MetricsRegistry::singleton(); + bdlm::MetricsRegistry::defaultInstance(); bdlm::MetricsRegistry otherRegistry; defaultRegistry.setMetricsAdapter(&defaultAdapter); otherRegistry.setMetricsAdapter(&otherAdapter); -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) using namespace EVENTSCHEDULER_TEST_CASE_19; using namespace bdlf::PlaceHolders; @@ -4102,7 +4091,6 @@ int main(int argc, char *argv[]) ASSERT( 1 == testObj.numExecuted() ); x.stop(); } -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS ASSERT(defaultAdapter.verify("")); defaultAdapter.reset(); @@ -4197,7 +4185,6 @@ int main(int argc, char *argv[]) ASSERT(otherAdapter.verify("b")); otherAdapter.reset(); #endif // defined(BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY) -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) } break; case 18: { // -------------------------------------------------------------------- @@ -5614,17 +5601,15 @@ int main(int argc, char *argv[]) << "============================" << "============================" << endl; -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS TestMetricsAdapter defaultAdapter; TestMetricsAdapter otherAdapter; bdlm::MetricsRegistry& defaultRegistry = - bdlm::MetricsRegistry::singleton(); + bdlm::MetricsRegistry::defaultInstance(); bdlm::MetricsRegistry otherRegistry; defaultRegistry.setMetricsAdapter(&defaultAdapter); otherRegistry.setMetricsAdapter(&otherAdapter); -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) using namespace EVENTSCHEDULER_TEST_CASE_8; using namespace bdlf::PlaceHolders; @@ -5651,7 +5636,6 @@ int main(int argc, char *argv[]) ASSERT( 1 == testObj.numExecuted() ); x.stop(); } -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS ASSERT(defaultAdapter.verify("")); defaultAdapter.reset(); @@ -5680,8 +5664,6 @@ int main(int argc, char *argv[]) } ASSERT(otherAdapter.verify("b")); otherAdapter.reset(); -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) - } break; case 7: { // placeholder @@ -7107,17 +7089,15 @@ int main(int argc, char *argv[]) << "BREATHING TEST" << endl << "==============" << endl; -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS TestMetricsAdapter defaultAdapter; TestMetricsAdapter otherAdapter; bdlm::MetricsRegistry& defaultRegistry = - bdlm::MetricsRegistry::singleton(); + bdlm::MetricsRegistry::defaultInstance(); bdlm::MetricsRegistry otherRegistry; defaultRegistry.setMetricsAdapter(&defaultAdapter); otherRegistry.setMetricsAdapter(&otherAdapter); -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) using namespace EVENTSCHEDULER_TEST_CASE_1; { @@ -7165,10 +7145,8 @@ int main(int argc, char *argv[]) ASSERT(0 == x.numRecurringEvents()); x.stop(); } -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS ASSERT(defaultAdapter.verify("")); defaultAdapter.reset(); -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) { // Create and start a scheduler object, schedule an event at T2. @@ -7209,10 +7187,8 @@ int main(int argc, char *argv[]) LOOP_ASSERT(nExec, 0 == nExec); // ok, even if overslept x.stop(); } -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS ASSERT(defaultAdapter.verify("")); defaultAdapter.reset(); -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) { // Create and start a scheduler object, schedule 2 events at @@ -7289,10 +7265,8 @@ int main(int argc, char *argv[]) ASSERT(0 == x.numRecurringEvents()); x.stop(); } -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS ASSERT(defaultAdapter.verify("")); defaultAdapter.reset(); -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) { // Create and start a scheduler object, schedule a clock of T3 @@ -7352,10 +7326,8 @@ int main(int argc, char *argv[]) // ASSERT(1 == x.numRecurringEvents()); x.stop(); } -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS ASSERT(defaultAdapter.verify("")); defaultAdapter.reset(); -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) { // Create and start a scheduler object, schedule a clock of T3 @@ -7435,10 +7407,8 @@ int main(int argc, char *argv[]) LOOP_ASSERT(nExec, 1 == nExec); x.stop(); } -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS ASSERT(defaultAdapter.verify("")); defaultAdapter.reset(); -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) { // Create and start a scheduler object, schedule an event at T2, @@ -7477,10 +7447,8 @@ int main(int argc, char *argv[]) // case, this is not a failure, do not stop. x.stop(); } -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS ASSERT(defaultAdapter.verify("")); defaultAdapter.reset(); -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) { // Create and start a scheduler object, schedule an event at T2, @@ -7511,10 +7479,8 @@ int main(int argc, char *argv[]) ASSERT( 0 != x.cancelEvent(h) ); x.stop(); } -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS ASSERT(defaultAdapter.verify("")); defaultAdapter.reset(); -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) { // Create and start a scheduler object, schedule 3 events at T, T2, @@ -7582,10 +7548,8 @@ int main(int argc, char *argv[]) // case, this is not a failure, do not stop. x.stop(); } -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS ASSERT(defaultAdapter.verify("")); defaultAdapter.reset(); -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) { // Create and start a scheduler object, schedule a clock of T3, let @@ -7629,10 +7593,8 @@ int main(int argc, char *argv[]) // Else complain but do not stop the test suite. x.stop(); } -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS ASSERT(defaultAdapter.verify("")); defaultAdapter.reset(); -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) #ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY { @@ -7679,10 +7641,8 @@ int main(int argc, char *argv[]) // Else complain but do not stop the test suite. x.stop(); } -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS ASSERT(defaultAdapter.verify("")); defaultAdapter.reset(); -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) #endif // defined(BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY) { @@ -7724,10 +7684,8 @@ int main(int argc, char *argv[]) LOOP2_ASSERT(NEXEC2, nExec, NEXEC2 == nExec); x.stop(); } -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS ASSERT(defaultAdapter.verify("")); defaultAdapter.reset(); -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) #ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY { @@ -7772,10 +7730,8 @@ int main(int argc, char *argv[]) LOOP2_ASSERT(NEXEC2, nExec, NEXEC2 == nExec); x.stop(); } -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS ASSERT(defaultAdapter.verify("")); defaultAdapter.reset(); -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) #endif // defined(BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY) { @@ -7844,10 +7800,8 @@ int main(int argc, char *argv[]) LOOP2_ASSERT(NEXEC2, elapsed, T6 < elapsed); } } -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS ASSERT(defaultAdapter.verify("")); defaultAdapter.reset(); -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) #ifdef BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY { @@ -7919,13 +7873,10 @@ int main(int argc, char *argv[]) LOOP2_ASSERT(NEXEC2, elapsed, T6 < elapsed); } } -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS ASSERT(defaultAdapter.verify("")); defaultAdapter.reset(); -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) #endif // defined(BSLS_LIBRARYFEATURES_HAS_CPP11_BASELINE_LIBRARY) -#ifdef BDLMT_EVENTSCHEDULER_ENABLE_METRICS // verify metric registration { @@ -7951,7 +7902,6 @@ int main(int argc, char *argv[]) } ASSERT(otherAdapter.verify("b")); otherAdapter.reset(); -#endif // defined(BDLMT_EVENTSCHEDULER_ENABLE_METRICS) } break; case -1: { // -------------------------------------------------------------------- diff --git a/groups/bdl/bdlmt/bdlmt_fixedthreadpool.cpp b/groups/bdl/bdlmt/bdlmt_fixedthreadpool.cpp index 5e6e9d6d23..c7fcb54297 100644 --- a/groups/bdl/bdlmt/bdlmt_fixedthreadpool.cpp +++ b/groups/bdl/bdlmt/bdlmt_fixedthreadpool.cpp @@ -59,23 +59,21 @@ void initBlockSet(sigset_t *blockSet) } #endif -#ifdef BDLMT_FIXEDTHREADPOOL_ENABLE_METRICS -void backlogMetric(BloombergLP::bdlm::Metric *value, - BloombergLP::bdlmt::FixedThreadPool *object) +void backlogMetric(BloombergLP::bdlm::Metric *value, + const BloombergLP::bdlmt::FixedThreadPool *object) { *value = BloombergLP::bdlm::Metric::Gauge( object->numPendingJobs() + object->numActiveThreads() - object->numThreadsStarted()); } -void usedCapacityMetric(BloombergLP::bdlm::Metric *value, - BloombergLP::bdlmt::FixedThreadPool *object) +void usedCapacityMetric(BloombergLP::bdlm::Metric *value, + const BloombergLP::bdlmt::FixedThreadPool *object) { *value = BloombergLP::bdlm::Metric::Gauge( static_cast(object->numPendingJobs()) / object->queueCapacity()); } -#endif // defined(BDLMT_THREADPOOL_ENABLE_METRICS) } // close unnamed namespace @@ -90,7 +88,6 @@ namespace bdlmt { const char FixedThreadPool::s_defaultThreadName[16] = { "bdl.FixedPool" }; // PRIVATE MANIPULATORS -#ifdef BDLMT_FIXEDTHREADPOOL_ENABLE_METRICS void FixedThreadPool::initialize(bdlm::MetricsRegistry *metricsRegistry, const bsl::string_view& metricsIdentifier) { @@ -106,8 +103,8 @@ void FixedThreadPool::initialize(bdlm::MetricsRegistry *metricsRegistry, #endif bdlm::MetricsRegistry *registry = metricsRegistry - ? metricsRegistry - : &bdlm::MetricsRegistry::singleton(); + ? metricsRegistry + : &bdlm::MetricsRegistry::defaultInstance(); bdlm::InstanceCount::Value instanceNumber = bdlm::InstanceCount::nextInstanceNumber(); @@ -128,33 +125,20 @@ void FixedThreadPool::initialize(bdlm::MetricsRegistry *metricsRegistry, "ftp", metricsIdentifier); - d_backlogHandle = registry->registerCollectionCallback( + registry->registerCollectionCallback( + &d_backlogHandle, mdBacklog, bdlf::BindUtil::bind(&backlogMetric, bdlf::PlaceHolders::_1, this)); - d_usedCapacityHandle = registry->registerCollectionCallback( + registry->registerCollectionCallback( + &d_usedCapacityHandle, mdUsedCapacity, bdlf::BindUtil::bind(&usedCapacityMetric, bdlf::PlaceHolders::_1, this)); } -#else -void FixedThreadPool::initialize() -{ - d_queue.disablePushBack(); - d_queue.disablePopFront(); - - if (d_threadAttributes.threadName().empty()) { - d_threadAttributes.setThreadName(s_defaultThreadName); - } - -#if defined(BSLS_PLATFORM_OS_UNIX) - initBlockSet(&d_blockSet); -#endif -} -#endif // defined(BDLMT_THREADPOOL_ENABLE_METRICS) void FixedThreadPool::workerThread() { @@ -215,16 +199,11 @@ FixedThreadPool::FixedThreadPool( { BSLS_ASSERT_OPT(1 <= numThreads); -#ifdef BDLMT_FIXEDTHREADPOOL_ENABLE_METRICS initialize( 0, bdlm::MetricDescriptor::k_USE_METRICS_ADAPTER_OBJECT_ID_SELECTION); -#else - initialize(); -#endif // defined(BDLMT_THREADPOOL_ENABLE_METRICS) } -#ifdef BDLMT_FIXEDTHREADPOOL_ENABLE_METRICS FixedThreadPool::FixedThreadPool( const bslmt::ThreadAttributes& threadAttributes, int numThreads, @@ -244,7 +223,6 @@ FixedThreadPool::FixedThreadPool( initialize(metricsRegistry, metricsIdentifier); } -#endif // defined(BDLMT_THREADPOOL_ENABLE_METRICS) FixedThreadPool::FixedThreadPool(int numThreads, int maxNumPendingJobs, @@ -259,16 +237,11 @@ FixedThreadPool::FixedThreadPool(int numThreads, { BSLS_ASSERT_OPT(1 <= numThreads); -#ifdef BDLMT_FIXEDTHREADPOOL_ENABLE_METRICS initialize( 0, bdlm::MetricDescriptor::k_USE_METRICS_ADAPTER_OBJECT_ID_SELECTION); -#else - initialize(); -#endif // defined(BDLMT_THREADPOOL_ENABLE_METRICS) } -#ifdef BDLMT_FIXEDTHREADPOOL_ENABLE_METRICS FixedThreadPool::FixedThreadPool(int numThreads, int maxNumPendingJobs, const bsl::string_view& metricsIdentifier, @@ -286,15 +259,10 @@ FixedThreadPool::FixedThreadPool(int numThreads, initialize(metricsRegistry, metricsIdentifier); } -#endif // defined(BDLMT_THREADPOOL_ENABLE_METRICS) FixedThreadPool::~FixedThreadPool() { shutdown(); -#ifdef BDLMT_FIXEDTHREADPOOL_ENABLE_METRICS - d_backlogHandle.unregister(); - d_usedCapacityHandle.unregister(); -#endif // defined(BDLMT_THREADPOOL_ENABLE_METRICS) } // MANIPULATORS diff --git a/groups/bdl/bdlmt/bdlmt_fixedthreadpool.h b/groups/bdl/bdlmt/bdlmt_fixedthreadpool.h index e3d3d163ea..f383bb53e4 100644 --- a/groups/bdl/bdlmt/bdlmt_fixedthreadpool.h +++ b/groups/bdl/bdlmt/bdlmt_fixedthreadpool.h @@ -343,10 +343,6 @@ BSLS_IDENT("$Id: $") #endif // BDE_DONT_ALLOW_TRANSITIVE_INCLUDES -#ifdef BDLMT_FIXEDTHREADPOOL_ENABLE_METRICS -#error "This component does not support metric collection at this time." -#endif - namespace BloombergLP { #ifndef BDE_OMIT_INTERNAL_DEPRECATED @@ -436,7 +432,6 @@ class FixedThreadPool { // blocked in managed threads #endif -#ifdef BDLMT_FIXEDTHREADPOOL_ENABLE_METRICS bdlm::MetricsRegistryRegistrationHandle d_backlogHandle; // backlog metric handle @@ -444,22 +439,14 @@ class FixedThreadPool { bdlm::MetricsRegistryRegistrationHandle d_usedCapacityHandle; // used capacity metric // handle -#endif // defined(BDLMT_FIXEDTHREADPOOL_ENABLE_METRICS) // PRIVATE MANIPULATORS -#ifdef BDLMT_FIXEDTHREADPOOL_ENABLE_METRICS void initialize(bdlm::MetricsRegistry *metricsRegistry, const bsl::string_view& metricsIdentifier); // Initialize this thread pool using the stored attributes and the // specified 'metricsRegistry' and 'metricsIdentifier'. If // 'metricsRegistry' is 0, 'bdlm::MetricsRegistry::singleton()' is // used. -#else - void initialize(); - // Perform one-time object initialization. Note that it is necessary - // for this function to be invoked exactly once in each constructor - // overload. -#endif // defined(BDLMT_THREADPOOL_ENABLE_METRICS) void workerThread(); // The main function executed by each worker thread. @@ -485,7 +472,6 @@ class FixedThreadPool { // the currently installed default allocator is used. The behavior is // undefined unless '1 <= numThreads'. -#ifdef BDLMT_FIXEDTHREADPOOL_ENABLE_METRICS FixedThreadPool(int numThreads, int maxNumPendingJobs, const bsl::string_view& metricsIdentifier, @@ -500,7 +486,6 @@ class FixedThreadPool { // used. Optionally specify a'basicAllocator' used to supply memory. // If 'basicAllocator' is 0, the currently installed default allocator // is used. The behavior is undefined unless '1 <= numThreads'. -#endif // defined(BDLMT_THREADPOOL_ENABLE_METRICS) FixedThreadPool(const bslmt::ThreadAttributes& threadAttributes, int numThreads, @@ -514,7 +499,6 @@ class FixedThreadPool { // allocator is used. The behavior is undefined unless // '1 <= numThreads'. -#ifdef BDLMT_FIXEDTHREADPOOL_ENABLE_METRICS FixedThreadPool(const bslmt::ThreadAttributes& threadAttributes, int numThreads, int maxNumPendingJobs, @@ -531,7 +515,6 @@ class FixedThreadPool { // 'basicAllocator' used to supply memory. If 'basicAllocator' is 0, // the currently installed default allocator is used. The behavior is // undefined unless '1 <= numThreads'. -#endif // defined(BDLMT_THREADPOOL_ENABLE_METRICS) ~FixedThreadPool(); // Remove all pending jobs from the queue without executing them, block diff --git a/groups/bdl/bdlmt/bdlmt_fixedthreadpool.t.cpp b/groups/bdl/bdlmt/bdlmt_fixedthreadpool.t.cpp index 382cfbff75..05b1c31f6a 100644 --- a/groups/bdl/bdlmt/bdlmt_fixedthreadpool.t.cpp +++ b/groups/bdl/bdlmt/bdlmt_fixedthreadpool.t.cpp @@ -107,6 +107,7 @@ using bsl::flush; // [16] CONCERN: 'start()' failure behavior // [17] CONCERN: 'drain', 'shutdown', 'stop' behavior when '!isStarted()' // [18] DRQS 167232024: 'drain' FAILS TO WAIT FOR ALL JOBS TO FINISH +// [19] CONCERN: POOL OBJECT CAN OUTLIVE USED 'MetricsRegistry' // ============================================================================ // STANDARD BDE ASSERT TEST FUNCTION @@ -203,8 +204,6 @@ bslma::TestAllocator taDefault; // GLOBAL CLASSES FOR TESTING // ---------------------------------------------------------------------------- -#ifdef BDLMT_FIXEDTHREADPOOL_ENABLE_METRICS - // ======================== // class TestMetricsAdapter // ======================== @@ -331,8 +330,6 @@ bool TestMetricsAdapter::verify(const bsl::string& name) const && d_descriptors[1].objectIdentifier() == name; } -#endif // defined(BDLMT_THREADPOOL_ENABLE_METRICS) - // ============================================================================ // HELPER CLASSES AND FUNCTIONS FOR TESTING // ---------------------------------------------------------------------------- @@ -1093,10 +1090,9 @@ int main(int argc, char *argv[]) veryVerbose = argc > 3; veryVeryVerbose = argc > 4; -#ifdef BDLMT_FIXEDTHREADPOOL_ENABLE_METRICS - // access the metrics registry singleton before assign the global allocator - bdlm::MetricsRegistry::singleton(); -#endif // defined(BDLMT_THREADPOOL_ENABLE_METRICS) + // access the metrics registry default instance before assign the global + // allocator + bdlm::MetricsRegistry::defaultInstance(); bslma::DefaultAllocatorGuard guard(&taDefault); bslma::TestAllocator testAllocator(veryVeryVerbose); @@ -1107,6 +1103,38 @@ int main(int argc, char *argv[]) cout << "TEST " << __FILE__ << " CASE " << test << endl; switch (test) { case 0: // case 0 is always the first case + case 19: { + // -------------------------------------------------------------------- + // TESTING CONCERN: POOL OBJECT CAN OUTLIVE USED 'MetricsRegistry' + // + // Concerns: + //: 1 'MetricsRegistry' object passed during construction can end its + //: lifetime before the pool's destructor is called without causing + //: segfault or any other use-after-free issue. See DRQS 173705664 + //: and DRQS 174980848 for more details. + // + // Plan: + //: 1 Create a 'MetricsRegistry' object on a free store and pass it to + //: the constructed pool. Delete the 'MetricsRegistry' object. The + //: pool destruction mustn't cause core dumps or other similar + //: issues. + // + // Testing: + // CONCERN: POOL OBJECT CAN OUTLIVE USED 'MetricsRegistry' + // -------------------------------------------------------------------- + if (verbose) cout << + "\nTESTING CONCERN: POOL OBJECT CAN OUTLIVE USED 'MetricsRegistry'" + "\n===============================================================" + << endl; + + bdlm::MetricsRegistry *metricsRegistry = + new bdlm::MetricsRegistry(&testAllocator); + Obj pool(2, 4, "metricsIdentifier", metricsRegistry, &testAllocator); + + // Prematurely kill the registry + delete metricsRegistry; + // 'pool' outlives the 'MetricsRegistry' object w/o segfault + } break; case 18: { // -------------------------------------------------------------------- // DRQS 167232024: 'drain' FAILS TO WAIT FOR ALL JOBS TO FINISH @@ -2266,17 +2294,15 @@ int main(int argc, char *argv[]) const int NUM_VALUES = sizeof VALUES / sizeof *VALUES; -#ifdef BDLMT_FIXEDTHREADPOOL_ENABLE_METRICS TestMetricsAdapter defaultAdapter; TestMetricsAdapter otherAdapter; bdlm::MetricsRegistry& defaultRegistry = - bdlm::MetricsRegistry::singleton(); + bdlm::MetricsRegistry::defaultInstance(); bdlm::MetricsRegistry otherRegistry; defaultRegistry.setMetricsAdapter(&defaultAdapter); otherRegistry.setMetricsAdapter(&otherAdapter); -#endif // defined(BDLMT_THREADPOOL_ENABLE_METRICS) for (int i = 0; i < NUM_VALUES; ++i) { const int THREADS = VALUES[i].d_numThreads; @@ -2295,7 +2321,6 @@ int main(int argc, char *argv[]) ASSERTV(i, 0 == X.numThreadsStarted()); ASSERTV(i, QUEUE_CAPACITY == X.queueCapacity()); } -#ifdef BDLMT_FIXEDTHREADPOOL_ENABLE_METRICS ASSERT(defaultAdapter.verify("")); defaultAdapter.reset(); @@ -2338,7 +2363,6 @@ int main(int argc, char *argv[]) } ASSERT(otherAdapter.verify("b")); otherAdapter.reset(); -#endif // defined(BDLMT_THREADPOOL_ENABLE_METRICS) { bslmt::ThreadAttributes attr; @@ -2350,7 +2374,6 @@ int main(int argc, char *argv[]) ASSERTV(i, 0 == X.numThreadsStarted()); ASSERTV(i, QUEUE_CAPACITY == X.queueCapacity()); } -#ifdef BDLMT_FIXEDTHREADPOOL_ENABLE_METRICS ASSERT(defaultAdapter.verify("")); defaultAdapter.reset(); @@ -2407,7 +2430,6 @@ int main(int argc, char *argv[]) } ASSERT(otherAdapter.verify("b")); otherAdapter.reset(); -#endif // defined(BDLMT_THREADPOOL_ENABLE_METRICS) } } } break; diff --git a/groups/bdl/bdlmt/bdlmt_threadpool.cpp b/groups/bdl/bdlmt/bdlmt_threadpool.cpp index 03872d4c5e..fdff8e6b15 100644 --- a/groups/bdl/bdlmt/bdlmt_threadpool.cpp +++ b/groups/bdl/bdlmt/bdlmt_threadpool.cpp @@ -40,18 +40,16 @@ BSLS_IDENT_RCSID(bdlmt_threadpool_cpp,"$Id$ $CSID$") #include #include -#ifdef BDLMT_THREADPOOL_ENABLE_METRICS namespace { -void backlogMetric(BloombergLP::bdlm::Metric *value, - BloombergLP::bdlmt::ThreadPool *object) +void backlogMetric(BloombergLP::bdlm::Metric *value, + const BloombergLP::bdlmt::ThreadPool *object) { *value = BloombergLP::bdlm::Metric::Gauge( object->numPendingJobs() - object->numWaitingThreads()); } } // close unnamed namespace -#endif // defined(BDLMT_THREADPOOL_ENABLE_METRICS) namespace BloombergLP { namespace bdlmt { @@ -132,7 +130,6 @@ void ThreadPool::doEnqueueJob(bslmf::MovableRef job) wakeThreadIfNeeded(); } -#ifdef BDLMT_THREADPOOL_ENABLE_METRICS void ThreadPool::initialize(bdlm::MetricsRegistry *metricsRegistry, const bsl::string_view& metricsIdentifier) { @@ -150,8 +147,8 @@ void ThreadPool::initialize(bdlm::MetricsRegistry *metricsRegistry, #endif bdlm::MetricsRegistry *registry = metricsRegistry - ? metricsRegistry - : &bdlm::MetricsRegistry::singleton(); + ? metricsRegistry + : &bdlm::MetricsRegistry::defaultInstance(); bdlm::InstanceCount::Value instanceNumber = bdlm::InstanceCount::nextInstanceNumber(); @@ -164,29 +161,13 @@ void ThreadPool::initialize(bdlm::MetricsRegistry *metricsRegistry, "tp", metricsIdentifier); - d_backlogHandle = registry->registerCollectionCallback( + registry->registerCollectionCallback( + &d_backlogHandle, md, bdlf::BindUtil::bind(&backlogMetric, bdlf::PlaceHolders::_1, this)); } -#else -void ThreadPool::initialize() -{ - if (d_threadAttributes.threadName().empty()) { - d_threadAttributes.setThreadName(s_defaultThreadName); - } - - // Force all threads to be detached. - - d_threadAttributes.setDetachedState( - bslmt::ThreadAttributes::e_CREATE_DETACHED); - -#if defined(BSLS_PLATFORM_OS_UNIX) - initBlockSet(); -#endif -} -#endif // defined(BDLMT_THREADPOOL_ENABLE_METRICS) void ThreadPool::wakeThreadIfNeeded() { @@ -451,16 +432,11 @@ ThreadPool::ThreadPool(const bslmt::ThreadAttributes& threadAttributes, d_maxIdleTime.setTotalMilliseconds(maxIdleTime); -#ifdef BDLMT_THREADPOOL_ENABLE_METRICS initialize( 0, bdlm::MetricDescriptor::k_USE_METRICS_ADAPTER_OBJECT_ID_SELECTION); -#else - initialize(); -#endif // defined(BDLMT_THREADPOOL_ENABLE_METRICS) } -#ifdef BDLMT_THREADPOOL_ENABLE_METRICS ThreadPool::ThreadPool(const bslmt::ThreadAttributes& threadAttributes, int minThreads, int maxThreads, @@ -487,7 +463,6 @@ ThreadPool::ThreadPool(const bslmt::ThreadAttributes& threadAttributes, initialize(metricsRegistry, metricsIdentifier); } -#endif // defined(BDLMT_THREADPOOL_ENABLE_METRICS) ThreadPool::ThreadPool(const bslmt::ThreadAttributes& threadAttributes, int minThreads, @@ -511,16 +486,11 @@ ThreadPool::ThreadPool(const bslmt::ThreadAttributes& threadAttributes, BSLS_ASSERT(bsls::TimeInterval(0, 0) <= maxIdleTime); BSLS_ASSERT(INT_MAX >= maxIdleTime.totalMilliseconds()); -#ifdef BDLMT_THREADPOOL_ENABLE_METRICS initialize( 0, bdlm::MetricDescriptor::k_USE_METRICS_ADAPTER_OBJECT_ID_SELECTION); -#else - initialize(); -#endif // defined(BDLMT_THREADPOOL_ENABLE_METRICS) } -#ifdef BDLMT_THREADPOOL_ENABLE_METRICS ThreadPool::ThreadPool(const bslmt::ThreadAttributes& threadAttributes, int minThreads, int maxThreads, @@ -547,14 +517,10 @@ ThreadPool::ThreadPool(const bslmt::ThreadAttributes& threadAttributes, initialize(metricsRegistry, metricsIdentifier); } -#endif // defined(BDLMT_THREADPOOL_ENABLE_METRICS) ThreadPool::~ThreadPool() { shutdown(); -#ifdef BDLMT_THREADPOOL_ENABLE_METRICS - d_backlogHandle.unregister(); -#endif // defined(BDLMT_THREADPOOL_ENABLE_METRICS) } // MANIPULATORS diff --git a/groups/bdl/bdlmt/bdlmt_threadpool.h b/groups/bdl/bdlmt/bdlmt_threadpool.h index d0014b05bc..542d648b75 100644 --- a/groups/bdl/bdlmt/bdlmt_threadpool.h +++ b/groups/bdl/bdlmt/bdlmt_threadpool.h @@ -376,10 +376,6 @@ BSLS_IDENT("$Id: $") #include #endif // BDE_DONT_ALLOW_TRANSITIVE_INCLUDES -#ifdef BDLMT_THREADPOOL_ENABLE_METRICS -#error "This component does not support metric collection at this time." -#endif - namespace BloombergLP { #ifndef BDE_OMIT_INTERNAL_DEPRECATED @@ -477,10 +473,8 @@ class ThreadPool { // managed threads #endif -#ifdef BDLMT_THREADPOOL_ENABLE_METRICS bdlm::MetricsRegistryRegistrationHandle d_backlogHandle; // backlog metric handle -#endif // defined(BDLMT_THREADPOOL_ENABLE_METRICS) // CLASS DATA static const char s_defaultThreadName[16]; // default name of threads @@ -498,19 +492,12 @@ class ThreadPool { // signal the next waiting thread if any. Note that this method must // be called with 'd_mutex' locked. -#ifdef BDLMT_THREADPOOL_ENABLE_METRICS void initialize(bdlm::MetricsRegistry *metricsRegistry, const bsl::string_view& metricsIdentifier); // Initialize this thread pool using the stored attributes and the // specified 'metricsRegistry' and 'metricsIdentifier'. If // 'metricsRegistry' is 0, 'bdlm::MetricsRegistry::singleton()' is // used. -#else - void initialize(); - // Perform one-time object initialization. Note that it is necessary - // for this function to be invoked exactly once in each constructor - // overload. -#endif // defined(BDLMT_THREADPOOL_ENABLE_METRICS) void wakeThreadIfNeeded(); // Signal this thread and pop the current thread from the wait list. @@ -558,7 +545,6 @@ class ThreadPool { // undefined unless '0 <= minThreads', 'minThreads <= maxThreads', and // '0 <= maxIdleTime'. -#ifdef BDLMT_THREADPOOL_ENABLE_METRICS ThreadPool(const bslmt::ThreadAttributes& threadAttributes, int minThreads, int maxThreads, @@ -578,7 +564,6 @@ class ThreadPool { // the currently installed default allocator is used. The behavior is // undefined unless '0 <= minThreads', 'minThreads <= maxThreads', and // '0 <= maxIdleTime'. -#endif // defined(BDLMT_THREADPOOL_ENABLE_METRICS) ThreadPool(const bslmt::ThreadAttributes& threadAttributes, int minThreads, @@ -596,7 +581,6 @@ class ThreadPool { // and the 'maxIdleTime' has a value less than or equal to 'INT_MAX' // milliseconds. -#ifdef BDLMT_THREADPOOL_ENABLE_METRICS ThreadPool(const bslmt::ThreadAttributes& threadAttributes, int minThreads, int maxThreads, @@ -617,7 +601,6 @@ class ThreadPool { // undefined unless '0 <= minThreads', 'minThreads <= maxThreads', // '0 <= maxIdleTime', and the 'maxIdleTime' has a value less than or // equal to 'INT_MAX' milliseconds. -#endif // defined(BDLMT_THREADPOOL_ENABLE_METRICS) ~ThreadPool(); // Call 'shutdown()' and destroy this thread pool. diff --git a/groups/bdl/bdlmt/bdlmt_threadpool.t.cpp b/groups/bdl/bdlmt/bdlmt_threadpool.t.cpp index 1c6bfdf5b4..90126d0b59 100644 --- a/groups/bdl/bdlmt/bdlmt_threadpool.t.cpp +++ b/groups/bdl/bdlmt/bdlmt_threadpool.t.cpp @@ -184,7 +184,6 @@ int veryVeryVerbose; // GLOBAL CLASSES FOR TESTING // ---------------------------------------------------------------------------- -#ifdef BDLMT_THREADPOOL_ENABLE_METRICS // ======================== // class TestMetricsAdapter @@ -296,8 +295,6 @@ bool TestMetricsAdapter::verify(const bsl::string& name) const && d_descriptors[0].objectIdentifier() == name; } -#endif // defined(BDLMT_THREADPOOL_ENABLE_METRICS) - // ============================================================================ // HELPER CLASSES AND FUNCTIONS FOR TESTING // ---------------------------------------------------------------------------- @@ -1026,10 +1023,9 @@ int main(int argc, char *argv[]) veryVerbose = argc > 3; veryVeryVerbose = argc > 4; -#ifdef BDLMT_THREADPOOL_ENABLE_METRICS - // access the metrics registry singleton before assign the global allocator - bdlm::MetricsRegistry::singleton(); -#endif // defined(BDLMT_THREADPOOL_ENABLE_METRICS) + // access the metrics registry default instance before assign the global + // allocator + bdlm::MetricsRegistry::defaultInstance(); bslmt::Configuration::setDefaultThreadStackSize( bslmt::Configuration::recommendedDefaultThreadStackSize()); @@ -1728,17 +1724,15 @@ int main(int argc, char *argv[]) const int NUM_VALUES = sizeof VALUES / sizeof *VALUES; -#ifdef BDLMT_THREADPOOL_ENABLE_METRICS TestMetricsAdapter defaultAdapter; TestMetricsAdapter otherAdapter; bdlm::MetricsRegistry& defaultRegistry = - bdlm::MetricsRegistry::singleton(); + bdlm::MetricsRegistry::defaultInstance(); bdlm::MetricsRegistry otherRegistry; defaultRegistry.setMetricsAdapter(&defaultAdapter); otherRegistry.setMetricsAdapter(&otherAdapter); -#endif // defined(BDLMT_THREADPOOL_ENABLE_METRICS) for (int i = 0; i < NUM_VALUES; ++i) { const int MIN = VALUES[i].d_minThreads; @@ -1762,7 +1756,6 @@ int main(int argc, char *argv[]) ASSERTV(i, IDLE == X.maxIdleTime()); ASSERTV(i, 0 == X.threadFailures()); } -#ifdef BDLMT_THREADPOOL_ENABLE_METRICS ASSERT(defaultAdapter.verify("")); defaultAdapter.reset(); @@ -1816,7 +1809,6 @@ int main(int argc, char *argv[]) } ASSERT(otherAdapter.verify("b")); otherAdapter.reset(); -#endif // defined(BDLMT_THREADPOOL_ENABLE_METRICS) } if (verbose) cout << "\nNegative Testing." << endl; @@ -1828,10 +1820,8 @@ int main(int argc, char *argv[]) ASSERT_PASS(Obj(attr, 10, 10, 1000)); ASSERT_FAIL(Obj(attr, 10, 10, -1)); -#ifdef BDLMT_THREADPOOL_ENABLE_METRICS ASSERT_PASS(Obj(attr, 10, 10, 1000, "", 0)); ASSERT_FAIL(Obj(attr, 10, 10, -1, "", 0)); -#endif // defined(BDLMT_THREADPOOL_ENABLE_METRICS) } } break; case 5: { @@ -2324,17 +2314,15 @@ int main(int argc, char *argv[]) const int NUM_VALUES = sizeof VALUES / sizeof *VALUES; -#ifdef BDLMT_THREADPOOL_ENABLE_METRICS TestMetricsAdapter defaultAdapter; TestMetricsAdapter otherAdapter; bdlm::MetricsRegistry& defaultRegistry = - bdlm::MetricsRegistry::singleton(); + bdlm::MetricsRegistry::defaultInstance(); bdlm::MetricsRegistry otherRegistry; defaultRegistry.setMetricsAdapter(&defaultAdapter); otherRegistry.setMetricsAdapter(&otherAdapter); -#endif // defined(BDLMT_THREADPOOL_ENABLE_METRICS) for (int i = 0; i < NUM_VALUES; ++i) { // SEC and NANOSEC conflict with Solaris macros @@ -2361,7 +2349,6 @@ int main(int argc, char *argv[]) ASSERTV(i, IDLE_TIME == X.maxIdleTimeInterval()); ASSERTV(i, 0 == X.threadFailures()); } -#ifdef BDLMT_THREADPOOL_ENABLE_METRICS ASSERT(defaultAdapter.verify("")); defaultAdapter.reset(); @@ -2417,7 +2404,6 @@ int main(int argc, char *argv[]) } ASSERT(otherAdapter.verify("b")); otherAdapter.reset(); -#endif // defined(BDLMT_THREADPOOL_ENABLE_METRICS) } #ifdef BSLS_TIMEINTERVAL_PROVIDES_CHRONO_CONVERSIONS @@ -2511,7 +2497,6 @@ int main(int argc, char *argv[]) ASSERT_PASS(Obj(attr, 9, 10, TimeInterval( 1, 0))); ASSERT_FAIL(Obj(attr, 10, 10, TimeInterval(-1, -1))); -#ifdef BDLMT_THREADPOOL_ENABLE_METRICS ASSERT_PASS(Obj(attr, 0, 100, TimeInterval( 1, 0), "", 0)); ASSERT_FAIL(Obj(attr, -1, 100, TimeInterval( 1, 0), @@ -2524,7 +2509,6 @@ int main(int argc, char *argv[]) "", 0)); ASSERT_FAIL(Obj(attr, 10, 10, TimeInterval(-1, -1), "", 0)); -#endif // defined(BDLMT_THREADPOOL_ENABLE_METRICS) TimeInterval VALID; VALID.setTotalMilliseconds(INT_MAX); @@ -2534,10 +2518,8 @@ int main(int argc, char *argv[]) ASSERT_PASS(Obj(attr, 9, 10, VALID )); ASSERT_FAIL(Obj(attr, 9, 10, INVALID)); -#ifdef BDLMT_THREADPOOL_ENABLE_METRICS ASSERT_PASS(Obj(attr, 9, 10, VALID , "", 0)); ASSERT_FAIL(Obj(attr, 9, 10, INVALID, "", 0)); -#endif // defined(BDLMT_THREADPOOL_ENABLE_METRICS) } } } diff --git a/groups/bdl/bdlmt/bdlmt_timereventscheduler.cpp b/groups/bdl/bdlmt/bdlmt_timereventscheduler.cpp index 4995c8a1ba..3e76265cb4 100644 --- a/groups/bdl/bdlmt/bdlmt_timereventscheduler.cpp +++ b/groups/bdl/bdlmt/bdlmt_timereventscheduler.cpp @@ -69,7 +69,6 @@ bsl::function createDefaultCurrentTimeFunctor( clockType); } -#ifdef BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS void startLagMetric(BloombergLP::bdlm::Metric *value, BloombergLP::bdlmt::TimerEventScheduler *object) { @@ -83,7 +82,6 @@ void startLagMetric(BloombergLP::bdlm::Metric *value, (now - next).totalSecondsAsDouble()); } } -#endif // defined(BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS) } // close unnamed namespace @@ -385,14 +383,13 @@ namespace bdlmt { const char TimerEventScheduler::s_defaultThreadName[16] = { "bdl.TimerEvent" }; // PRIVATE MANIPULATORS -#ifdef BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS void TimerEventScheduler::initialize( bdlm::MetricsRegistry *metricsRegistry, const bsl::string_view& metricsIdentifier) { bdlm::MetricsRegistry *registry = metricsRegistry - ? metricsRegistry - : &bdlm::MetricsRegistry::singleton(); + ? metricsRegistry + : &bdlm::MetricsRegistry::defaultInstance(); bdlm::InstanceCount::Value instanceNumber = bdlm::InstanceCount::nextInstanceNumber(); @@ -405,13 +402,13 @@ void TimerEventScheduler::initialize( "tes", metricsIdentifier); - d_startLagHandle = registry->registerCollectionCallback( + registry->registerCollectionCallback( + &d_startLagHandle, md, bdlf::BindUtil::bind(&startLagMetric, bdlf::PlaceHolders::_1, this)); } -#endif // defined(BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS) void TimerEventScheduler::yieldToDispatcher() { @@ -452,14 +449,11 @@ TimerEventScheduler::TimerEventScheduler(bslma::Allocator* basicAllocator) , d_numClocks(0) , d_clockType(bsls::SystemClockType::e_REALTIME) { -#ifdef BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS initialize( 0, bdlm::MetricDescriptor::k_USE_METRICS_ADAPTER_OBJECT_ID_SELECTION); -#endif // defined(BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS) } -#ifdef BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS TimerEventScheduler::TimerEventScheduler( const bsl::string_view& metricsIdentifier, bdlm::MetricsRegistry *metricsRegistry, @@ -486,7 +480,6 @@ TimerEventScheduler::TimerEventScheduler( { initialize(metricsRegistry, metricsIdentifier); } -#endif // defined(BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS) TimerEventScheduler::TimerEventScheduler( bsls::SystemClockType::Enum clockType, @@ -512,14 +505,11 @@ TimerEventScheduler::TimerEventScheduler( , d_numClocks(0) , d_clockType(clockType) { -#ifdef BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS initialize( 0, bdlm::MetricDescriptor::k_USE_METRICS_ADAPTER_OBJECT_ID_SELECTION); -#endif // defined(BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS) } -#ifdef BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS TimerEventScheduler::TimerEventScheduler( bsls::SystemClockType::Enum clockType, const bsl::string_view& metricsIdentifier, @@ -548,7 +538,6 @@ TimerEventScheduler::TimerEventScheduler( { initialize(metricsRegistry, metricsIdentifier); } -#endif // defined(BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS) TimerEventScheduler::TimerEventScheduler( const TimerEventScheduler::Dispatcher& dispatcherFunctor, @@ -573,14 +562,11 @@ TimerEventScheduler::TimerEventScheduler( , d_numClocks(0) , d_clockType(bsls::SystemClockType::e_REALTIME) { -#ifdef BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS initialize( 0, bdlm::MetricDescriptor::k_USE_METRICS_ADAPTER_OBJECT_ID_SELECTION); -#endif // defined(BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS) } -#ifdef BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS TimerEventScheduler::TimerEventScheduler( const TimerEventScheduler::Dispatcher& dispatcherFunctor, const bsl::string_view& metricsIdentifier, @@ -608,7 +594,6 @@ TimerEventScheduler::TimerEventScheduler( { initialize(metricsRegistry, metricsIdentifier); } -#endif // defined(BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS) TimerEventScheduler::TimerEventScheduler( const TimerEventScheduler::Dispatcher& dispatcherFunctor, @@ -634,14 +619,11 @@ TimerEventScheduler::TimerEventScheduler( , d_numClocks(0) , d_clockType(clockType) { -#ifdef BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS initialize( 0, bdlm::MetricDescriptor::k_USE_METRICS_ADAPTER_OBJECT_ID_SELECTION); -#endif // defined(BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS) } -#ifdef BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS TimerEventScheduler::TimerEventScheduler( const TimerEventScheduler::Dispatcher& dispatcherFunctor, bsls::SystemClockType::Enum clockType, @@ -670,7 +652,6 @@ TimerEventScheduler::TimerEventScheduler( { initialize(metricsRegistry, metricsIdentifier); } -#endif // defined(BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS) TimerEventScheduler::TimerEventScheduler(int numEvents, int numClocks, @@ -700,14 +681,11 @@ TimerEventScheduler::TimerEventScheduler(int numEvents, BSLS_ASSERT(numEvents < (1 << 24) - 1); BSLS_ASSERT(numClocks < (1 << 24) - 1); -#ifdef BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS initialize( 0, bdlm::MetricDescriptor::k_USE_METRICS_ADAPTER_OBJECT_ID_SELECTION); -#endif // defined(BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS) } -#ifdef BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS TimerEventScheduler::TimerEventScheduler( int numEvents, int numClocks, @@ -741,7 +719,6 @@ TimerEventScheduler::TimerEventScheduler( initialize(metricsRegistry, metricsIdentifier); } -#endif // defined(BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS) TimerEventScheduler::TimerEventScheduler( int numEvents, @@ -774,14 +751,11 @@ TimerEventScheduler::TimerEventScheduler( BSLS_ASSERT(numEvents < (1 << 24) - 1); BSLS_ASSERT(numClocks < (1 << 24) - 1); -#ifdef BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS initialize( 0, bdlm::MetricDescriptor::k_USE_METRICS_ADAPTER_OBJECT_ID_SELECTION); -#endif // defined(BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS) } -#ifdef BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS TimerEventScheduler::TimerEventScheduler( int numEvents, int numClocks, @@ -817,7 +791,6 @@ TimerEventScheduler::TimerEventScheduler( initialize(metricsRegistry, metricsIdentifier); } -#endif // defined(BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS) TimerEventScheduler::TimerEventScheduler( int numEvents, @@ -850,14 +823,11 @@ TimerEventScheduler::TimerEventScheduler( BSLS_ASSERT(numEvents < (1 << 24) - 1); BSLS_ASSERT(numClocks < (1 << 24) - 1); -#ifdef BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS initialize( 0, bdlm::MetricDescriptor::k_USE_METRICS_ADAPTER_OBJECT_ID_SELECTION); -#endif // defined(BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS) } -#ifdef BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS TimerEventScheduler::TimerEventScheduler( int numEvents, int numClocks, @@ -893,7 +863,6 @@ TimerEventScheduler::TimerEventScheduler( initialize(metricsRegistry, metricsIdentifier); } -#endif // defined(BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS) TimerEventScheduler::TimerEventScheduler( int numEvents, @@ -926,14 +895,11 @@ TimerEventScheduler::TimerEventScheduler( BSLS_ASSERT(numEvents < (1 << 24) - 1); BSLS_ASSERT(numClocks < (1 << 24) - 1); -#ifdef BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS initialize( 0, bdlm::MetricDescriptor::k_USE_METRICS_ADAPTER_OBJECT_ID_SELECTION); -#endif // defined(BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS) } -#ifdef BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS TimerEventScheduler::TimerEventScheduler( int numEvents, int numClocks, @@ -969,15 +935,10 @@ TimerEventScheduler::TimerEventScheduler( initialize(metricsRegistry, metricsIdentifier); } -#endif // defined(BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS) TimerEventScheduler::~TimerEventScheduler() { stop(); - -#ifdef BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS - d_startLagHandle.unregister(); -#endif // defined(BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS) } // MANIPULATORS diff --git a/groups/bdl/bdlmt/bdlmt_timereventscheduler.h b/groups/bdl/bdlmt/bdlmt_timereventscheduler.h index 3a0e28796c..7e40003166 100644 --- a/groups/bdl/bdlmt/bdlmt_timereventscheduler.h +++ b/groups/bdl/bdlmt/bdlmt_timereventscheduler.h @@ -349,10 +349,6 @@ BSLS_IDENT("$Id: $") #include #include -#ifdef BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS -#error "This component does not support metric collection at this time." -#endif - namespace BloombergLP { namespace bdlmt { @@ -530,10 +526,8 @@ class TimerEventScheduler { // microseconds from epoch of next // cached event -#ifdef BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS bdlm::MetricsRegistryRegistrationHandle d_startLagHandle; // start lag handle -#endif // defined(BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS) private: // NOT IMPLEMENTED @@ -546,14 +540,12 @@ class TimerEventScheduler { private: // PRIVATE MANIPULATORS -#ifdef BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS void initialize(bdlm::MetricsRegistry *metricsRegistry, const bsl::string_view& metricsIdentifier); // Initialize this event scheduler using the stored attributes and the // specified 'metricsRegistry' and 'metricsIdentifier'. If // 'metricsRegistry' is 0, 'bdlm::MetricsRegistry::singleton()' is // used. -#endif // defined(BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS) void yieldToDispatcher(); // Repeatedly wake up dispatcher thread until it noticeably starts @@ -576,7 +568,6 @@ class TimerEventScheduler { // scheduled non-recurring events and recurring events defaults to an // implementation defined constant. -#ifdef BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS explicit TimerEventScheduler(const bsl::string_view& metricsIdentifier, bdlm::MetricsRegistry *metricsRegistry, bslma::Allocator *basicAllocator = 0); @@ -592,7 +583,6 @@ class TimerEventScheduler { // the currently installed default allocator is used. Note that the // maximal number of scheduled non-recurring events and recurring // events defaults to an implementation defined constant. -#endif // defined(BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS) explicit TimerEventScheduler( bsls::SystemClockType::Enum clockType, @@ -607,7 +597,6 @@ class TimerEventScheduler { // maximal number of scheduled non-recurring events and recurring // events defaults to an implementation defined constant. -#ifdef BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS explicit TimerEventScheduler( bsls::SystemClockType::Enum clockType, const bsl::string_view& metricsIdentifier, @@ -626,7 +615,6 @@ class TimerEventScheduler { // is used. Note that the maximal number of scheduled non-recurring // events and recurring events defaults to an implementation defined // constant. -#endif // defined(BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS) explicit TimerEventScheduler(const Dispatcher& dispatcherFunctor, bslma::Allocator *basicAllocator = 0); @@ -640,7 +628,6 @@ class TimerEventScheduler { // scheduled non-recurring events and recurring events defaults to an // implementation defined constant. -#ifdef BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS explicit TimerEventScheduler(const Dispatcher& dispatcherFunctor, const bsl::string_view& metricsIdentifier, bdlm::MetricsRegistry *metricsRegistry, @@ -657,7 +644,6 @@ class TimerEventScheduler { // the currently installed default allocator is used. Note that the // maximal number of scheduled non-recurring events and recurring // events defaults to an implementation defined constant. -#endif // defined(BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS) explicit TimerEventScheduler( const Dispatcher& dispatcherFunctor, @@ -673,7 +659,6 @@ class TimerEventScheduler { // maximal number of scheduled non-recurring events and recurring // events defaults to an implementation defined constant. -#ifdef BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS explicit TimerEventScheduler( const Dispatcher& dispatcherFunctor, bsls::SystemClockType::Enum clockType, @@ -693,7 +678,6 @@ class TimerEventScheduler { // is used. Note that the maximal number of scheduled non-recurring // events and recurring events defaults to an implementation defined // constant. -#endif // defined(BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS) TimerEventScheduler(int numEvents, int numClocks, @@ -709,7 +693,6 @@ class TimerEventScheduler { // used. The behavior is undefined unless '0 <= numEvents < 2**24' and // '0 <= numClocks < 2**24'. -#ifdef BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS TimerEventScheduler(int numEvents, int numClocks, const bsl::string_view& metricsIdentifier, @@ -729,7 +712,6 @@ class TimerEventScheduler { // the currently installed default allocator is used. The behavior is // undefined unless '0 <= numEvents < 2**24' and // '0 <= numClocks < 2**24'. -#endif // defined(BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS) TimerEventScheduler(int numEvents, int numClocks, @@ -746,7 +728,6 @@ class TimerEventScheduler { // installed default allocator is used. The behavior is undefined // unless '0 <= numEvents < 2**24' and '0 <= numClocks < 2**24'. -#ifdef BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS TimerEventScheduler(int numEvents, int numClocks, bsls::SystemClockType::Enum clockType, @@ -767,7 +748,6 @@ class TimerEventScheduler { // If 'basicAllocator' is 0, the currently installed default allocator // is used. The behavior is undefined unless '0 <= numEvents < 2**24' // and '0 <= numClocks < 2**24'. -#endif // defined(BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS) TimerEventScheduler(int numEvents, int numClocks, @@ -784,7 +764,6 @@ class TimerEventScheduler { // used. The behavior is undefined unless '0 <= numEvents < 2**24' and // '0 <= numClocks < 2**24'. -#ifdef BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS TimerEventScheduler(int numEvents, int numClocks, const Dispatcher& dispatcherFunctor, @@ -805,7 +784,6 @@ class TimerEventScheduler { // the currently installed default allocator is used. The behavior is // undefined unless '0 <= numEvents < 2**24' and // '0 <= numClocks < 2**24'. -#endif // defined(BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS) TimerEventScheduler(int numEvents, int numClocks, @@ -823,7 +801,6 @@ class TimerEventScheduler { // installed default allocator is used. The behavior is undefined // unless '0 <= numEvents < 2**24' and '0 <= numClocks < 2**24'. -#ifdef BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS TimerEventScheduler(int numEvents, int numClocks, const Dispatcher& dispatcherFunctor, @@ -845,7 +822,6 @@ class TimerEventScheduler { // If 'basicAllocator' is 0, the currently installed default allocator // is used. The behavior is undefined unless '0 <= numEvents < 2**24' // and '0 <= numClocks < 2**24'. -#endif // defined(BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS) ~TimerEventScheduler(); // Stop this scheduler, discard all the unprocessed events and destroy diff --git a/groups/bdl/bdlmt/bdlmt_timereventscheduler.t.cpp b/groups/bdl/bdlmt/bdlmt_timereventscheduler.t.cpp index b5872fbac5..495eca7bfa 100644 --- a/groups/bdl/bdlmt/bdlmt_timereventscheduler.t.cpp +++ b/groups/bdl/bdlmt/bdlmt_timereventscheduler.t.cpp @@ -749,8 +749,6 @@ static void cancelAllClocksCallback(Obj *scheduler, int wait) // HELPER CLASSES AND FUNCTIONS FOR TESTING // ============================================================================ -#ifdef BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS - static bdlm::InstanceCount::Value s_count = 0; // ======================== @@ -872,8 +870,6 @@ bool TestMetricsAdapter::verify(const bsl::string& name) const && d_descriptors[0].objectIdentifier() == name; } -#endif // defined(BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS) - // ---------------------------------------------------------------------------- // USAGE EXAMPLE RELATED ENTITIES // ---------------------------------------------------------------------------- @@ -3110,10 +3106,9 @@ int main(int argc, char *argv[]) veryVeryVeryVerbose = argc > 5; int nExec; -#ifdef BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS - // access the metrics registry singleton before assign the global allocator - bdlm::MetricsRegistry::singleton(); -#endif // defined(BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS) + // access the metrics registry default instance before assign the global + // allocator + bdlm::MetricsRegistry::defaultInstance(); bslma::TestAllocator ta; @@ -3493,17 +3488,15 @@ int main(int argc, char *argv[]) << "=================================" << "===========================" << endl; -#ifdef BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS TestMetricsAdapter defaultAdapter; TestMetricsAdapter otherAdapter; bdlm::MetricsRegistry& defaultRegistry = - bdlm::MetricsRegistry::singleton(); + bdlm::MetricsRegistry::defaultInstance(); bdlm::MetricsRegistry otherRegistry; defaultRegistry.setMetricsAdapter(&defaultAdapter); otherRegistry.setMetricsAdapter(&otherAdapter); -#endif // defined(BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS) using namespace TIMER_EVENT_SCHEDULER_TEST_CASE_24; using namespace bdlf::PlaceHolders; @@ -3530,7 +3523,6 @@ int main(int argc, char *argv[]) makeSureTestObjectIsExecuted(testObj, mT, 100); ASSERT( 1 == testObj.numExecuted() ); } -#ifdef BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS ASSERT(defaultAdapter.verify("")); defaultAdapter.reset(); @@ -3574,7 +3566,6 @@ int main(int argc, char *argv[]) } ASSERT(otherAdapter.verify("b")); otherAdapter.reset(); -#endif // defined(BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS) } break; case 23: { // ----------------------------------------------------------------- @@ -3604,17 +3595,15 @@ int main(int argc, char *argv[]) << "=================================" << "=======================" << endl; -#ifdef BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS TestMetricsAdapter defaultAdapter; TestMetricsAdapter otherAdapter; bdlm::MetricsRegistry& defaultRegistry = - bdlm::MetricsRegistry::singleton(); + bdlm::MetricsRegistry::defaultInstance(); bdlm::MetricsRegistry otherRegistry; defaultRegistry.setMetricsAdapter(&defaultAdapter); otherRegistry.setMetricsAdapter(&otherAdapter); -#endif // defined(BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS) using namespace TIMER_EVENT_SCHEDULER_TEST_CASE_23; using namespace bdlf::PlaceHolders; @@ -3640,7 +3629,6 @@ int main(int argc, char *argv[]) makeSureTestObjectIsExecuted(testObj, mT, 100); ASSERT( 1 == testObj.numExecuted() ); } -#ifdef BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS ASSERT(defaultAdapter.verify("")); defaultAdapter.reset(); @@ -3669,7 +3657,6 @@ int main(int argc, char *argv[]) } ASSERT(otherAdapter.verify("b")); otherAdapter.reset(); -#endif // defined(BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS) } break; case 22: { // ----------------------------------------------------------------- @@ -3698,17 +3685,15 @@ int main(int argc, char *argv[]) << "=================================" << "=====================" << endl; -#ifdef BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS TestMetricsAdapter defaultAdapter; TestMetricsAdapter otherAdapter; bdlm::MetricsRegistry& defaultRegistry = - bdlm::MetricsRegistry::singleton(); + bdlm::MetricsRegistry::defaultInstance(); bdlm::MetricsRegistry otherRegistry; defaultRegistry.setMetricsAdapter(&defaultAdapter); otherRegistry.setMetricsAdapter(&otherAdapter); -#endif // defined(BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS) using namespace TIMER_EVENT_SCHEDULER_TEST_CASE_22; using namespace bdlf::PlaceHolders; @@ -3731,7 +3716,6 @@ int main(int argc, char *argv[]) makeSureTestObjectIsExecuted(testObj, mT, 100); ASSERT( 1 == testObj.numExecuted() ); } -#ifdef BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS ASSERT(defaultAdapter.verify("")); defaultAdapter.reset(); @@ -3764,7 +3748,6 @@ int main(int argc, char *argv[]) } ASSERT(otherAdapter.verify("b")); otherAdapter.reset(); -#endif // defined(BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS) } break; case 21: { // ----------------------------------------------------------------- @@ -3793,17 +3776,15 @@ int main(int argc, char *argv[]) << "=================================" << "=================" << endl; -#ifdef BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS TestMetricsAdapter defaultAdapter; TestMetricsAdapter otherAdapter; bdlm::MetricsRegistry& defaultRegistry = - bdlm::MetricsRegistry::singleton(); + bdlm::MetricsRegistry::defaultInstance(); bdlm::MetricsRegistry otherRegistry; defaultRegistry.setMetricsAdapter(&defaultAdapter); otherRegistry.setMetricsAdapter(&otherAdapter); -#endif // defined(BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS) using namespace TIMER_EVENT_SCHEDULER_TEST_CASE_21; using namespace bdlf::PlaceHolders; @@ -3826,7 +3807,6 @@ int main(int argc, char *argv[]) makeSureTestObjectIsExecuted(testObj, mT, 100); ASSERT( 1 == testObj.numExecuted() ); } -#ifdef BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS ASSERT(defaultAdapter.verify("")); defaultAdapter.reset(); @@ -3855,7 +3835,6 @@ int main(int argc, char *argv[]) } ASSERT(otherAdapter.verify("b")); otherAdapter.reset(); -#endif // defined(BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS) } break; case 20: { // ----------------------------------------------------------------- @@ -3885,17 +3864,15 @@ int main(int argc, char *argv[]) << "=================================" << "=============================" << endl; -#ifdef BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS TestMetricsAdapter defaultAdapter; TestMetricsAdapter otherAdapter; bdlm::MetricsRegistry& defaultRegistry = - bdlm::MetricsRegistry::singleton(); + bdlm::MetricsRegistry::defaultInstance(); bdlm::MetricsRegistry otherRegistry; defaultRegistry.setMetricsAdapter(&defaultAdapter); otherRegistry.setMetricsAdapter(&otherAdapter); -#endif // defined(BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS) using namespace TIMER_EVENT_SCHEDULER_TEST_CASE_20; using namespace bdlf::PlaceHolders; @@ -3922,7 +3899,6 @@ int main(int argc, char *argv[]) makeSureTestObjectIsExecuted(testObj, mT, 100); ASSERT( 1 == testObj.numExecuted() ); } -#ifdef BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS ASSERT(defaultAdapter.verify("")); defaultAdapter.reset(); @@ -3954,7 +3930,6 @@ int main(int argc, char *argv[]) } ASSERT(otherAdapter.verify("b")); otherAdapter.reset(); -#endif // defined(BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS) } break; case 19: { // ----------------------------------------------------------------- @@ -3983,17 +3958,15 @@ int main(int argc, char *argv[]) << "=================================" << "===========================" << endl; -#ifdef BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS TestMetricsAdapter defaultAdapter; TestMetricsAdapter otherAdapter; bdlm::MetricsRegistry& defaultRegistry = - bdlm::MetricsRegistry::singleton(); + bdlm::MetricsRegistry::defaultInstance(); bdlm::MetricsRegistry otherRegistry; defaultRegistry.setMetricsAdapter(&defaultAdapter); otherRegistry.setMetricsAdapter(&otherAdapter); -#endif // defined(BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS) using namespace TIMER_EVENT_SCHEDULER_TEST_CASE_19; using namespace bdlf::PlaceHolders; @@ -4016,7 +3989,6 @@ int main(int argc, char *argv[]) makeSureTestObjectIsExecuted(testObj, mT, 100); ASSERT( 1 == testObj.numExecuted() ); } -#ifdef BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS ASSERT(defaultAdapter.verify("")); defaultAdapter.reset(); @@ -4045,7 +4017,6 @@ int main(int argc, char *argv[]) } ASSERT(otherAdapter.verify("b")); otherAdapter.reset(); -#endif // defined(BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS) } break; case 18: { // ----------------------------------------------------------------- @@ -5052,17 +5023,15 @@ int main(int argc, char *argv[]) << "=================================" << "============================" << endl; -#ifdef BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS TestMetricsAdapter defaultAdapter; TestMetricsAdapter otherAdapter; bdlm::MetricsRegistry& defaultRegistry = - bdlm::MetricsRegistry::singleton(); + bdlm::MetricsRegistry::defaultInstance(); bdlm::MetricsRegistry otherRegistry; defaultRegistry.setMetricsAdapter(&defaultAdapter); otherRegistry.setMetricsAdapter(&otherAdapter); -#endif // defined(BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS) using namespace TIMER_EVENT_SCHEDULER_TEST_CASE_8; using namespace bdlf::PlaceHolders; @@ -5088,7 +5057,6 @@ int main(int argc, char *argv[]) makeSureTestObjectIsExecuted(testObj, mT, 100); ASSERT( 1 == testObj.numExecuted() ); } -#ifdef BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS ASSERT(defaultAdapter.verify("")); defaultAdapter.reset(); @@ -5117,7 +5085,6 @@ int main(int argc, char *argv[]) } ASSERT(otherAdapter.verify("b")); otherAdapter.reset(); -#endif // defined(BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS) } break; case 7: { // ----------------------------------------------------------------- @@ -6090,17 +6057,15 @@ int main(int argc, char *argv[]) << "BREATHING TEST" << endl << "==============" << endl; -#ifdef BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS TestMetricsAdapter defaultAdapter; TestMetricsAdapter otherAdapter; bdlm::MetricsRegistry& defaultRegistry = - bdlm::MetricsRegistry::singleton(); + bdlm::MetricsRegistry::defaultInstance(); bdlm::MetricsRegistry otherRegistry; defaultRegistry.setMetricsAdapter(&defaultAdapter); otherRegistry.setMetricsAdapter(&otherAdapter); -#endif // defined(BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS) using namespace TIMER_EVENT_SCHEDULER_TEST_CASE_1; @@ -6119,7 +6084,6 @@ int main(int argc, char *argv[]) tg.joinAll(); -#ifdef BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS // verify metric registration s_count = NUM_THREADS; @@ -6148,7 +6112,6 @@ int main(int argc, char *argv[]) } ASSERT(otherAdapter.verify("b")); otherAdapter.reset(); -#endif // defined(BDLMT_TIMEREVENTSCHEDULER_ENABLE_METRICS) } break; case -1: { // --------------------------------------------------------------------