/** * @file include/ActivMiddleware/ActivFramework/LatencyProfiler.h * * @brief Header file for the latency profiler class. */ #if (!defined ACTIV_FRAMEWORK_LATENCY_PROFILER_H) #define ACTIV_FRAMEWORK_LATENCY_PROFILER_H #include "ActivMiddleware/ActivFramework/External.h" #include "ActivMiddleware/ActivBase/IniFileParser.h" #include "ActivMiddleware/ActivFramework/ThreadTimer.h" #include "ActivMiddleware/ActivFramework/ManagedEndPoint.h" #include "ActivMiddleware/CxxYZ/array.h" #include "ActivMiddleware/CxxYZ/functional.h" #include "ActivMiddleware/CxxYZ/map.h" #include "ActivMiddleware/CxxYZ/unordered_map.h" #include "ActivMiddleware/CxxYZ/vector.h" //! Activ namespace. namespace Activ { class Component; class UiIo; //! Latency profiler. class LatencyProfiler { public: //! Percentile update state. enum PercentileUpdateState { PERCENTILE_UPDATE_STATE_DISABLED, PERCENTILE_UPDATE_STATE_ENABLED, PERCENTILE_UPDATE_STATE_LOGGING }; //! Settings class. class Settings { public: /** * @brief Bucket info. * * Describes the specification of the buckets for some range of values: * - the interval size of the increments stored as an exponent of power-2 (m_granularityExponent). * - the index offset in the buckets frequency list (m_baseOffset). */ class BucketInfo { public: //! Default constructor. ACTIV_FRAMEWORK_API BucketInfo(); uint32_t m_granularityExponent; ///< The granularity exponent. uint32_t m_baseOffset; ///< The base offset. }; ACTIV_BOOST_STATIC_CONSTEXPR size_t BUCKET_INFO_MAX_EXPONENT_INDEX = 32; typedef CxxYZ::array BucketInfoList; ///< The bucket info list. typedef CxxYZ::vector PercentileList; ///< The percentile list. /** * @brief Create bucket info list helper. * * The bucket info list details the specification of evaluating the correct index of the histogram * frequency buckets from a latency value (in nanoseconds). * * Each BucketInfo deals with a range of values explicitly defined in powers of two: * * BucketInfoList[0]: covers only latency=0 * BucketInfoList[1]: 2^(0) <= latency < 2^(1) * BucketInfoList[2]: 2^(1) <= latency < 2^(2) * ... * BucketInfoList[x]: 2^(x-1) <= latency < 2^(x) * ... * BucketInfoList[32]: 2^(31) <= latency < 2^(32) * * A single BucketInfo tells us the offset start position (m_baseOffset) and the power-2 exponent * of the granularity (m_granularityExponent) which determines the granularity of the bucket info range. * * From the above, we see the latency values covered in the bucket info list is 0 to UINT32_MAX (~4.29 seconds). * * Default bucket info list (when calling GetDefault()) * * Note: x is power-2 exponent. * * Range (x=exponent) Increment (x=Exponent) Buckets Index offset * 0<=x<1 (1.00 ns to 2.00 ns) x=8 (256.00 ns) 1 0 * 1<=x<2 (2.00 ns to 4.00 ns) x=8 (256.00 ns) 1 0 * 2<=x<3 (4.00 ns to 8.00 ns) x=8 (256.00 ns) 1 0 * 3<=x<4 (8.00 ns to 16.00 ns ... 1 0 * 4<=x<5 (16.00 ns to 32.00 ns) 1 0 * 5<=x<6 (32.00 ns to 64.00 ns) 1 0 * 6<=x<7 (64.00 ns to 128.00 ns) 1 0 * 7<=x<8 (128.00 ns to 256.00 ns) 1 0 * 8<=x<9 (256.00 ns to 512.00 ns) 1 1 * 9<=x<10 (512.00 ns to 1024.00 ns) 2 2 * 10<=x<11 (1024.00 ns to 2.05 us) 4 4 * 11<=x<12 (2.05 us to 4.10 us) 8 8 * 12<=x<13 (4.10 us to 8.19 us) 16 16 * 13<=x<14 (8.19 us to 16.38 us) 32 32 * 14<=x<15 (16.38 us to 32.77 us) 64 64 * 15<=x<16 (32.77 us to 65.54 us) x=10 (1024.00 ns) 32 128 * 16<=x<17 (65.54 us to 131.07 us) ... (1024.00 ns) 64 160 * 17<=x<18 (131.07 us to 262.14 us) (1024.00 ns) 128 224 * 18<=x<19 (262.14 us to 524.29 us) (1024.00 ns) 256 352 * 19<=x<20 (524.29 us to 1048.58 us) x=12 (4.10 us) 128 608 * 20<=x<21 (1048.58 us to 2.10 ms) ... (4.10 us) 256 736 * 21<=x<22 (2.10 ms to 4.19 ms) (4.10 us) 512 992 * 22<=x<23 (4.19 ms to 8.39 ms) (4.10 us) 1024 1504 * 23<=x<24 (8.39 ms to 16.78 ms) x=13 (8.19 us) 1024 2528 * 24<=x<25 (16.78 ms to 33.55 ms) (8.19 us) 2048 3552 * 25<=x<26 (33.55 ms to 67.11 ms) x=16 (65.54 us) 512 5600 * 26<=x<27 (67.11 ms to 134.22 ms) x=17 (131.07 us) 512 6112 * 27<=x<28 (134.22 ms to 268.44 ms) x=18 (262.14 us) 512 6624 * 28<=x<29 (268.44 ms to 536.87 ms) x=19 (524.29 us) 512 7136 * 29<=x<30 (536.87 ms to 1073.74 ms) x=20 (1048.58 us) 512 7648 * 30<=x<31 (1073.74 ms to 2147.48 ms) x=21 (2.10 ms) 512 8160 * 31<=x<32 (2147.48 ms to 4294.97 ms) x=22 (4.19 ms) 512 8672 * * Total buckets = 9184 */ class CreateBucketInfoListHelper { public: //! Bucket specification class BucketSpecification { public: /** * @brief Constructor. * * @param exponentMin the exponent min. * @param exponentMax the exponent max. * @param exponentIncrement the exponent increment. */ BucketSpecification(const uint32_t exponentMin, const uint32_t exponentMax, const uint32_t exponentIncrement) : m_power2ExponentMin(exponentMin), m_power2ExponentMax(exponentMax), m_power2ExponentIncrement(exponentIncrement) { } uint32_t m_power2ExponentMin; uint32_t m_power2ExponentMax; uint32_t m_power2ExponentIncrement; }; typedef CxxYZ::vector BucketSpecificationList; ///< The bucket specification list type. typedef LatencyProfiler::Settings::BucketInfoList BucketInfoList; ///< The bucket info list type. /** * @brief Get. * * Get the bucket info list from an environment variable (string). If the environment variable does not exist, * the default value will be returned instead. * * Format: * * "=,=,..." * * Explanation: * * "Everything upto 2^(max1) has bucket increment granularity of 2^(incr1)" * "Everything between 2^(max1) and 2^(max2) has bucket increment granularity of 2^(incr2)" * ... * * Example: * * "15=8,19=10,23=12,25=13,26=16,27=17,28=18,29=19,30=20,31=21,32=22" * * @param oBucketInfoString the optional bucket info list string. * @param defaultBucketInfoList the default bucket info list. * * @throw Exception. * * @return the bucket info list */ ACTIV_FRAMEWORK_API static BucketInfoList Get(const Boost::optional &oBucketInfoString, const BucketInfoList &defaultBucketInfoList = GetDefault()); /** * @brief Get the default bucket info list. * * 0 < range-exponent <= 15, increment-exponent=8 * 15 < range-exponent <= 19, increment-exponent=10 * 19 < range-exponent <= 23, increment-exponent=12 * 23 < range-exponent <= 25, increment-exponent=13 * 25 < range-exponent <= 26, increment-exponent=16 * 26 < range-exponent <= 27, increment-exponent=17 * 27 < range-exponent <= 28, increment-exponent=18 * 28 < range-exponent <= 29, increment-exponent=19 * 29 < range-exponent <= 30, increment-exponent=20 * 30 < range-exponent <= 31, increment-exponent=21 * 31 < range-exponent <= 32, increment-exponent=22 */ ACTIV_FRAMEWORK_API static BucketInfoList GetDefault(); /** * @brief Parse. * * Parse the bucket info list from an ini file section iterator. * * Format: * * [LatencyProfiler:HistogramBuckets] * = ("Everything upto 2^(max1) has bucket increment granularity of 2^(incr1)). * = * ("Everything between 2^(max1) and 2^(max2) has bucket increment granularity of 2^(incr2)). * ... * * Example: * * [LatencyProfiler:HistogramBuckets] * 15 = 8 * 19 = 10 * 23 = 12 * 25 = 13 * 26 = 16 * 27 = 17 * 28 = 18 * 29 = 19 * 30 = 20 * 31 = 21 * 32 = 22 * * @param sectionIterator the section iterator. * @param bucketInfoList the bucket info list. * * @throw Exception */ static void Parse(const IniFileParser::ConstSectionIterator §ionIterator, BucketInfoList &bucketInfoList); /** * @brief Create. * * A supplementary function, called through from Get, GetDefault and Parse. * * @param bucketInfoList the bucket info list. * @param bucketSpecificationList the bucket specification. * * @retval STATUS_CODE_SUCCESS * @retval STATUS_CODE_INVALID_PARAMETER * @retval STATUS_CODE_OUT_OF_RANGE */ ACTIV_FRAMEWORK_API static StatusCode Create(BucketInfoList &bucketInfoList, const BucketSpecificationList &bucketSpecificationList); private: /** * @brief Internal add bucket info layer. * * The range of latency values is determined by the power 2 exponent, which takes a start power-2 * exponent (inclusive) and an end power-2 exponent (exclusive). The increment size power-2 exponent * determines the granularity of the range, which each bucket entry covers. * * @param bucketInfoList the bucket info list. * @param bucketSpecification the bucket specification. * * @retval STATUS_CODE_SUCCESS * @retval STATUS_CODE_INVALID_PARAMETER * @retval STATUS_CODE_OUT_OF_RANGE */ static StatusCode InternalAddBucketInfoLayer(BucketInfoList &bucketInfoList, const BucketSpecification &bucketSpecification); }; //! Default constructor. ACTIV_FRAMEWORK_API Settings(); /** * @brief Read settings from an ini file section. * * @param sectionIterator iterator to an ini file section. * * @throw Exception */ ACTIV_FRAMEWORK_API void Read(const IniFileParser::ConstSectionIterator §ionIterator); //! Copy constructor. ACTIV_FRAMEWORK_API Settings(const Settings &rhs); //! Copy assignment operator. ACTIV_FRAMEWORK_API Settings &operator=(const Settings &rhs); //! Is enabled. bool IsEnabled() const; //! Is percentile log enabled. bool IsPercentileLogEnabled() const; //! Get total number of histogram buckets. size_t GetTotalNumberOfHistogramBuckets() const; String m_logDirectory; ///< Default: 'latency'. The subdirectory to place log files in. uint32_t m_logInterval; ///< Default: 1000ms. The log interval (ms). uint32_t m_percentileLogInterval; ///< Default: 0 (disabled). The percentile log interval (ms). uint32_t m_logNumberOfBackups; ///< Default: 14. The log number of backups. uint64_t m_sampleFrequency; ///< Default: 0 (disabled). The sample frequency. uint64_t m_sampleInterval; ///< Default: 5000. The sample interval (ms), forces an update when sampled message rates are low. /** * @brief Range of offset in ms to add to timer. A random offset within the range is chosen. * * If there is only one value then this is used as the offset. */ std::vector m_percentileLogTimerOffsetRange; PercentileList m_percentileList; ///< The percentile list. BucketInfoList m_bucketInfoList; ///< The bucket info list. String m_additionalColumnName; ///< The optional name of an additional column in the log file. }; // Latency statistics. class Statistics { public: //! Constructor. Statistics(); //! Reset(). void Reset(); //! Update with latest measurement. void Update(const int64_t latency); //! Addition operator Statistics &operator+=(const Statistics &rhs); int64_t m_min; ///< The min latency value (nS). int64_t m_max; ///< The max latency value (nS). uint64_t m_count; ///< The latency count. int64_t m_sum; ///< The (total latency) sum. uint64_t m_negative; ///< The negative (count). }; typedef CxxYZ::vector HistogramFrequencies; ///< The histogram frequencies type. class HistogramStatistics { public: //Default Constructor HistogramStatistics(); //! Constructor. HistogramStatistics(const size_t histogramFrequenciesSize); //! Update with latency. void Update(const Settings::BucketInfoList &bucketInfoList, const uint64_t latency); //! Is histogram frequencies initialized? bool IsInitialized() const; //! Initialize histogram frequencies. void Initialize(const size_t histogramFrequenciesSize); /** * @brief Combine with @a rhs. * * Note this will clear the contents of @a rhs. * * @param rhs the statistics to combine with and clear. */ void Combine(HistogramStatistics &rhs); size_t m_peakIndex; ///< The histogram frequency peak index. size_t m_oversized; ///< The number of oversized histogram frequency updates. HistogramFrequencies m_frequencies; ///< The histogram frequency list. }; //! Percentile calculator. class PercentileCalculator { public: typedef CxxYZ::unique_ptr Ptr; ///< The percentile calculator pointer type. typedef CxxYZ::map HistogramStatisticsMap; ///< The index to histogram statistics map type. typedef CxxYZ::unordered_map HistogramEntryNamesMap; ///< The index to histogram entry name map type. ACTIV_FRAMEWORK_API static const uint16_t DEFAULT_HISTOGRAM_INDEX; struct Statistics { HistogramFrequencies m_percentileResultsList; uint64_t m_numberOfPercentileUpdates; uint64_t m_numberOfOversizedUpdates; double m_percentageBucketPeak; }; typedef CxxYZ::function LogCallback; /** * @brief Constructor. * * @param name for the uniquely identifying the log file. * @param component the component. * @param settings the settings. * @param isUsingInternalTimer is the logging driven by the internal timer? * @param logCallback callback on log entry. * @param indexToEntryNameMap map from entry indexes to entry names. */ ACTIV_FRAMEWORK_API PercentileCalculator(const String &name, Component &component, const Settings &settings, const bool isUsingInternalTimer = true, const LogCallback &logCallback = LogCallback(), const HistogramEntryNamesMap &indexToEntryNameMap = HistogramEntryNamesMap()); //! Destructor. ACTIV_FRAMEWORK_API ~PercentileCalculator(); //! Update with a latency. void Update(const uint64_t latency); /** * @brief Combine histogram stats with another histogram. * * Note this will clear the contents of @a rhs. * This should not be used while the percentile calculator is being updated. * * @param rhs the statistics to combine with and clear. * @param histogramindex the index of the histogram to combine with (empty for default). */ void CombineHistogram(HistogramStatistics &rhs, const uint16_t histogramindex = DEFAULT_HISTOGRAM_INDEX); //! Start one round of logging. ACTIV_FRAMEWORK_API void StartLogging(); //! Get state. PercentileUpdateState GetState() const; private: //! On task. bool OnTask(); //! On reset task. bool OnResetTask(); //! On percentile log. bool OnPercentileLog(); void InitializeLogging(HistogramStatistics &histogramStatistics); const String m_name; Component & m_component; HistogramStatisticsMap m_histogramStatistics; ///< The statistics to work out the percentiles for. HistogramStatistics & m_defaultHistogramStatistics; ///< Reference to the default histogram statistics. HistogramFrequencies m_bufferedLatencyHistogramUpdates; ///< The buffered latency histogram updates. const Settings::PercentileList & m_percentileList; ///< The percentile list. const Settings::BucketInfoList & m_bucketInfoList; ///< The bucket info list. const String & m_additionalColumnName; ///< The optional additional column name for the log file. const LogCallback m_logCallback; const HistogramEntryNamesMap m_entryIndexToNameMap; PercentileUpdateState m_updateState; ManagedEndPoint::ScopedPostEndPointProcessingTask m_percentileCalculatorTask; ///< The background percentile calculator task. size_t m_indexPositionDuringTask; ///< The index position during task. DateTime m_cachedLocalDateTime; ///< The cached local date time. Statistics m_cachedStatistics; ///< The cached statistics; uint64_t m_numberOfBufferedUpdates; ///< The number of buffered updates. HistogramStatisticsMap::iterator m_histogramStatisticsIterator; ///< The histogram statistics map iterator. HistogramFrequencies::const_iterator m_histogramPivotIterator; ///< The histogram pivot iterator. Settings::PercentileList::const_iterator m_percentileListIterator; ///< The percentile list iterator. const CxxYZ::vector m_latencyAtBucketEntryList; ///< The latency index. ScopedAddress m_logAddress; ///< The system latency log address. CxxYZ::unique_ptr m_pLogTimer; ///< The log timer. }; /** * @brief Consrtructor. * * @param component the component. * @param settings the settings. * @param name the name. */ ACTIV_FRAMEWORK_API LatencyProfiler(Component &component, const Settings &settings, const String &name); //! Destructor. ACTIV_FRAMEWORK_API ~LatencyProfiler(); /** * @brief Should update. * * m_sampleCounter is incremented by 1, and returns whether it exceeded the frequency * threshold defined by Settings::m_sampleFrequency. * * @return Boolean. */ bool ShouldUpdate(); /** * @brief Update. * * If sample frequency is required, ShouldUpdate(...) should be called before using this function. * * @param startTimespec the start timespec */ void Update(const timespec &startTimespec); /** * @brief Update. * * If sample frequency is required, ShouldUpdate(...) should be called before using this function. * * @param startTimespec the start timespec. * @param endTimespec the end timespec. */ void Update(const timespec &startTimespec, const timespec &endTimespec); /** * @brief Update. * * If sample frequency is required, ShouldUpdate(...) should be called before using this function. * * @param startTimestamp the start timestamp in nanoseconds. */ void Update(const int64_t startTimestamp); /** * @brief Update with latency. * * The latency difference is already assumed to be pre-calculated. * If sample frequency is required, ShouldUpdate(...) should be called before using this function. * * @param latency the latency in nanoseconds. */ void UpdateWithLatency(const int64_t latency); /** * @brief Possibly update. * * Internally calls ShouldUpdate(), and updates latency statistics if true. * * @param startTimespec the start timespec. */ void PossiblyUpdate(const timespec &startTimespec); /** * @brief Possibly update. * * Internally calls ShouldUpdate(), and updates latency statistics if true. * * @param startTimespec the start timespec. * @param endTimespec the end timespec. */ void PossiblyUpdate(const timespec &startTimespec, const timespec &endTimespec); /** * @brief Possibly update. * * Internally calls ShouldUpdate(), and updates latency statistics if true. * * @param startTimestamp the start timestamp in nanoseconds. */ void PossiblyUpdate(const int64_t startTimestamp); /** * @brief Possibly update with latency. * * Internally calls ShouldUpdate(), and updates latency statistics if true. * * @param latency the latency in nanoseconds. */ void PossiblyUpdateWithLatency(const int64_t latency); //! Get percentile update state, PercentileUpdateState GetPercentileUpdateState() const; //! Get name. const String &GetName() const; //! Display settings. ACTIV_FRAMEWORK_API void UiDisplaySettings(const UiIo &uiIo) const; private: static const size_t PERCENTILE_ACCUMULATOR_INCREMENT = 256; static const size_t PERCENTILE_CLEARDOWN_INCREMENT = 1024; static const size_t PERCENTILE_ADD_BUFFERED_UPDATES_INCREMENT = 32; //! Clear. void Clear(); //! Process log timer. void ProcessLogTimer(); //! Process sample interval timer. void ProcessSampleIntervalTimer(); typedef CxxYZ::unique_ptr ChimeTimerPtr; ///< The chime timer pointer. /** * @brief Internal update. * * @param latency the latency. */ void InternalUpdate(const int64_t latency); const Settings m_settings; ///< The settings. const String m_name; ///< The name. uint64_t m_sampleCounter; ///< The sample counter. Statistics m_statistics; Component & m_component; ///< The component. ScopedAddress m_logAddress; ///< The system latency log address. PercentileCalculator::Ptr m_pPercentileCalculator; ///< The percentile calculator component pointer. ThreadTimer::ChimeTimer m_logTimer; ///< The log timer. ChimeTimerPtr m_pSampleIntervalTimer; ///< The sample interval timer pointer. }; // --------------------------------------------------------------------------------------------------------------------------------- inline bool LatencyProfiler::Settings::IsEnabled() const { return (0 != m_sampleFrequency); } // --------------------------------------------------------------------------------------------------------------------------------- inline bool LatencyProfiler::Settings::IsPercentileLogEnabled() const { return IsEnabled() && (0 != m_percentileLogInterval); } // --------------------------------------------------------------------------------------------------------------------------------- inline size_t LatencyProfiler::Settings::GetTotalNumberOfHistogramBuckets() const { return static_cast(((UINT64_C(1) + UINT32_MAX) >> (m_bucketInfoList.back().m_granularityExponent + UINT64_C(1))) + m_bucketInfoList.rbegin()->m_baseOffset) + 1; } // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- inline bool LatencyProfiler::ShouldUpdate() { if (m_sampleCounter >= m_settings.m_sampleFrequency) { m_sampleCounter = 1; return true; } ++m_sampleCounter; return false; } // --------------------------------------------------------------------------------------------------------------------------------- inline void LatencyProfiler::Update(const timespec &startTimespec) { timespec nowTimespec; (void) timespec_get(&nowTimespec, TIME_UTC); Update(startTimespec, nowTimespec); } // --------------------------------------------------------------------------------------------------------------------------------- inline void LatencyProfiler::Update(const timespec &startTimespec, const timespec &endTimespec) { const int64_t latency = (endTimespec.tv_sec - startTimespec.tv_sec) * 1000000000 + (static_cast(endTimespec.tv_nsec) - startTimespec.tv_nsec); InternalUpdate(latency); } // --------------------------------------------------------------------------------------------------------------------------------- inline void LatencyProfiler::Update(const int64_t startTimestamp) { timespec nowTimespec; (void) timespec_get(&nowTimespec, TIME_UTC); const int64_t latency = ((static_cast(nowTimespec.tv_sec) * 1000000000) + nowTimespec.tv_nsec) - startTimestamp; InternalUpdate(latency); } // --------------------------------------------------------------------------------------------------------------------------------- inline void LatencyProfiler::UpdateWithLatency(const int64_t latency) { InternalUpdate(latency); } // --------------------------------------------------------------------------------------------------------------------------------- inline void LatencyProfiler::PossiblyUpdate(const timespec &startTimespec) { if (ShouldUpdate()) Update(startTimespec); } // --------------------------------------------------------------------------------------------------------------------------------- inline void LatencyProfiler::PossiblyUpdate(const timespec &startTimespec, const timespec &endTimespec) { if (ShouldUpdate()) Update(startTimespec, endTimespec); } // --------------------------------------------------------------------------------------------------------------------------------- inline void LatencyProfiler::PossiblyUpdate(const int64_t startTimestamp) { if (ShouldUpdate()) Update(startTimestamp); } // --------------------------------------------------------------------------------------------------------------------------------- inline void LatencyProfiler::PossiblyUpdateWithLatency(const int64_t latency) { if (ShouldUpdate()) UpdateWithLatency(latency); } // --------------------------------------------------------------------------------------------------------------------------------- inline LatencyProfiler::PercentileUpdateState LatencyProfiler::GetPercentileUpdateState() const { return (m_pPercentileCalculator ? m_pPercentileCalculator->GetState() : PERCENTILE_UPDATE_STATE_DISABLED); } // --------------------------------------------------------------------------------------------------------------------------------- inline const String &LatencyProfiler::GetName() const { return m_name; } // --------------------------------------------------------------------------------------------------------------------------------- inline void LatencyProfiler::Clear() { m_statistics.Reset(); } // --------------------------------------------------------------------------------------------------------------------------------- inline void LatencyProfiler::ProcessSampleIntervalTimer() { m_sampleCounter = m_settings.m_sampleFrequency; } // --------------------------------------------------------------------------------------------------------------------------------- inline void LatencyProfiler::InternalUpdate(const int64_t latency) { m_statistics.Update(latency); if (m_pPercentileCalculator) { if (ACTIV_LIKELY(latency >= 0)) m_pPercentileCalculator->Update(static_cast(latency)); } } // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- inline LatencyProfiler::Statistics::Statistics() : m_min(Boost::integer_traits::const_max), m_max(Boost::integer_traits::const_min), m_count(0), m_sum(0), m_negative(0) { } // --------------------------------------------------------------------------------------------------------------------------------- inline void LatencyProfiler::Statistics::Reset() { m_min = Boost::integer_traits::const_max; m_max = Boost::integer_traits::const_min; m_count = 0; m_sum = 0; m_negative = 0; } // --------------------------------------------------------------------------------------------------------------------------------- inline void LatencyProfiler::Statistics::Update(const int64_t latency) { if (ACTIV_UNLIKELY(latency < m_min)) m_min = latency; if (ACTIV_UNLIKELY(latency > m_max)) m_max = latency; m_sum += latency; ++m_count; // TODO: Should negative count to the other stats too? if (ACTIV_UNLIKELY(latency < 0)) ++m_negative; } // --------------------------------------------------------------------------------------------------------------------------------- inline LatencyProfiler::Statistics &LatencyProfiler::Statistics::operator+=(const Statistics &rhs) { if (m_min > rhs.m_min) m_min = rhs.m_min; if (m_max < rhs.m_max) m_max = rhs.m_max; m_count += rhs.m_count; m_sum += rhs.m_sum; m_negative += rhs.m_negative; return *this; } // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- inline LatencyProfiler::HistogramStatistics::HistogramStatistics() : m_peakIndex(0), m_oversized(0) { } // --------------------------------------------------------------------------------------------------------------------------------- inline LatencyProfiler::HistogramStatistics::HistogramStatistics(const size_t histogramFrequenciesSize) : m_peakIndex(0), m_oversized(0), m_frequencies(histogramFrequenciesSize) { } // --------------------------------------------------------------------------------------------------------------------------------- inline void LatencyProfiler::HistogramStatistics::Update(const Settings::BucketInfoList &bucketInfoList, const uint64_t latency) { if (ACTIV_LIKELY(latency <= UINT32_MAX)) { const size_t highestBitPos = HighestBitPosition()(static_cast(latency)); const Settings::BucketInfo &bucketInfo = bucketInfoList[highestBitPos]; const uint64_t latencyWithHighestBitCleared = (latency & ~(UINT64_C(1) << (highestBitPos - 1))); const size_t latencyBucketOffset = static_cast(latencyWithHighestBitCleared >> bucketInfo.m_granularityExponent); const size_t frequencyIndex = bucketInfo.m_baseOffset + latencyBucketOffset; ++m_frequencies[frequencyIndex]; if (ACTIV_UNLIKELY(m_peakIndex < frequencyIndex)) m_peakIndex = frequencyIndex; } else { ++m_oversized; } } // --------------------------------------------------------------------------------------------------------------------------------- inline bool LatencyProfiler::HistogramStatistics::IsInitialized() const { return m_frequencies.size() > 0; } // --------------------------------------------------------------------------------------------------------------------------------- inline void LatencyProfiler::HistogramStatistics::Initialize(const size_t histogramFrequenciesSize) { assert(!IsInitialized()); m_frequencies.resize(histogramFrequenciesSize); } // --------------------------------------------------------------------------------------------------------------------------------- inline void LatencyProfiler::HistogramStatistics::Combine(HistogramStatistics &rhs) { if (m_peakIndex < rhs.m_peakIndex) m_peakIndex = rhs.m_peakIndex; m_oversized += rhs.m_oversized; if (ACTIV_UNLIKELY(m_peakIndex >= m_frequencies.size())) m_frequencies.resize(m_peakIndex + 1); for (size_t i = 0, endI = std::min(m_peakIndex, rhs.m_peakIndex); i <= endI; ++i) { m_frequencies[i] += rhs.m_frequencies[i]; rhs.m_frequencies[i] = 0; } rhs.m_peakIndex = 0; rhs.m_oversized = 0; } // --------------------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------------------- inline void LatencyProfiler::PercentileCalculator::Update(const uint64_t latency) { switch (m_updateState) { case PERCENTILE_UPDATE_STATE_ENABLED: m_defaultHistogramStatistics.Update(m_bucketInfoList, latency); break; case PERCENTILE_UPDATE_STATE_LOGGING: // Cache latencies when it is currently logging in the background. m_bufferedLatencyHistogramUpdates.push_back(latency); break; default: assert(false); } } // --------------------------------------------------------------------------------------------------------------------------------- inline void LatencyProfiler::PercentileCalculator::CombineHistogram(HistogramStatistics &rhs, const uint16_t histogramIndex) { HistogramStatisticsMap::iterator iter = m_histogramStatistics.find(histogramIndex); if (ACTIV_UNLIKELY(m_histogramStatistics.end() == iter)) { // Default histogram will always exist so just use its size for the histogram stats parameter. iter = m_histogramStatistics.emplace(histogramIndex, HistogramStatistics(m_histogramStatistics[DEFAULT_HISTOGRAM_INDEX].m_frequencies.size())).first; } iter->second.Combine(rhs); } // --------------------------------------------------------------------------------------------------------------------------------- inline LatencyProfiler::PercentileUpdateState LatencyProfiler::PercentileCalculator::GetState() const { return m_updateState; } } // namespace Activ #endif // (!defined ACTIV_FRAMEWORK_LATENCY_PROFILER_H)