1616 */
1717
1818#include " hdrhistogram.h"
19+
1920#include < folly/lang/Assume.h>
20- #include < hdr_histogram.h>
2121#include < nlohmann/json.hpp>
2222#include < platform/cb_malloc.h>
2323#include < iostream>
@@ -51,17 +51,17 @@ HdrHistogram::HdrHistogram(uint64_t lowestTrackableValue,
5151 significantFigures,
5252 &hist, // Pointer to initialise
5353 cb_calloc);
54-
55- histogram.reset (hist);
54+ histogram.wlock ()->reset (hist);
5655 defaultIterationMode = iterMode;
5756}
5857
5958HdrHistogram& HdrHistogram::operator +=(const HdrHistogram& other) {
60- if (other.histogram != nullptr && other.getValueCount () > 0 ) {
59+ if (other.histogram .rlock ()->get () != nullptr &&
60+ other.getValueCount () > 0 ) {
6161 // work out if we need to resize the receiving histogram
6262 uint64_t newMin = this ->getMinTrackableValue ();
6363 uint64_t newMax = this ->getMaxTrackableValue ();
64- int32_t newSigfig = this ->histogram -> significant_figures ;
64+ int32_t newSigfig = this ->getSigFigAccuracy () ;
6565 bool resize = false ;
6666
6767 if (other.getMinTrackableValue () < this ->getMinTrackableValue ()) {
@@ -73,92 +73,93 @@ HdrHistogram& HdrHistogram::operator+=(const HdrHistogram& other) {
7373 resize = true ;
7474 }
7575
76- if (other.histogram ->significant_figures >
77- this ->histogram ->significant_figures ) {
78- newSigfig = other.histogram ->significant_figures ;
76+ if (other.getSigFigAccuracy () > this ->getSigFigAccuracy ()) {
77+ newSigfig = other.getSigFigAccuracy ();
7978 resize = true ;
8079 }
8180 if (resize) {
8281 this ->resize (newMin, newMax, newSigfig);
8382 }
84- hdr_add (this ->histogram .get (), other.histogram .get ());
83+ hdr_add (this ->histogram .rlock ()-> get (), other.histogram .rlock ()-> get ());
8584 }
8685 return *this ;
8786}
8887
8988bool HdrHistogram::addValue (uint64_t v) {
9089 // A hdr_histogram cannot store 0, therefore we add a bias of +1.
9190 int64_t vBiased = v + 1 ;
92- return hdr_record_value (histogram.get (), vBiased);
91+ return hdr_record_value (histogram.rlock ()-> get (), vBiased);
9392}
9493
9594bool HdrHistogram::addValueAndCount (uint64_t v, uint64_t count) {
9695 // A hdr_histogram cannot store 0, therefore we add a bias of +1.
9796 int64_t vBiased = v + 1 ;
98- return hdr_record_values (histogram.get (), vBiased, count);
97+ return hdr_record_values (histogram.rlock ()-> get (), vBiased, count);
9998}
10099
101100uint64_t HdrHistogram::getValueCount () const {
102- return static_cast <uint64_t >(histogram->total_count );
101+ return static_cast <uint64_t >(histogram. rlock ()-> get () ->total_count );
103102}
104103
105104uint64_t HdrHistogram::getMinValue () const {
105+ // check there are values in the histogram otherwise hdr_min() will return
106+ // 0 which will cause use to return a UINT64_MAX.
106107 if (getValueCount ()) {
107- return static_cast <uint64_t >(hdr_min (histogram.get ()) - 1 );
108+ return static_cast <uint64_t >(hdr_min (histogram.rlock ()-> get ()) - 1 );
108109 }
109110 return 0 ;
110111}
111112
112113uint64_t HdrHistogram::getMaxValue () const {
114+ // check there are values in the histogram otherwise hdr_max() will return
115+ // 0 which will cause use to return a UINT64_MAX.
113116 if (getValueCount ()) {
114- return static_cast <uint64_t >(hdr_max (histogram.get ()) - 1 );
117+ return static_cast <uint64_t >(hdr_max (histogram.rlock ()-> get ()) - 1 );
115118 }
116119 return 0 ;
117120}
118121
119122void HdrHistogram::reset () {
120- hdr_reset (histogram.get ());
123+ hdr_reset (histogram.wlock ()-> get ());
121124}
122125
123126uint64_t HdrHistogram::getValueAtPercentile (double percentage) const {
124127 // We added the bias of +1 to the input value (see
125128 // addValueToFreqHistogram). Therefore need to minus the bias
126129 // before returning the value from the histogram.
127130 // Note: If the histogram is empty we just want to return 0;
128- uint64_t value = hdr_value_at_percentile (histogram.get (), percentage);
131+ uint64_t value =
132+ hdr_value_at_percentile (histogram.rlock ()->get (), percentage);
129133 uint64_t result = getValueCount () > 0 ? (value - 1 ) : 0 ;
130134 return result;
131135}
132136
133137HdrHistogram::Iterator HdrHistogram::makeLinearIterator (
134138 int64_t valueUnitsPerBucket) const {
135- HdrHistogram::Iterator iter{};
136- iter.type = Iterator::IterMode::Linear;
137- hdr_iter_linear_init (&iter, histogram.get (), valueUnitsPerBucket);
138- return iter;
139+ HdrHistogram::Iterator itr (histogram, Iterator::IterMode::Linear);
140+ hdr_iter_linear_init (&itr, itr.histoRLockPtr ->get (), valueUnitsPerBucket);
141+ return itr;
139142}
140143
141144HdrHistogram::Iterator HdrHistogram::makeLogIterator (int64_t firstBucketWidth,
142145 double log_base) const {
143- HdrHistogram::Iterator iter{} ;
144- iter. type = Iterator::IterMode::Log;
145- hdr_iter_log_init (&iter, histogram. get (), firstBucketWidth, log_base);
146- return iter ;
146+ HdrHistogram::Iterator itr (histogram, Iterator::IterMode::Log) ;
147+ hdr_iter_log_init (
148+ &itr, itr. histoRLockPtr -> get (), firstBucketWidth, log_base);
149+ return itr ;
147150}
148151
149152HdrHistogram::Iterator HdrHistogram::makePercentileIterator (
150153 uint32_t ticksPerHalfDist) const {
151- HdrHistogram::Iterator iter{};
152- iter.type = Iterator::IterMode::Percentiles;
153- hdr_iter_percentile_init (&iter, histogram.get (), ticksPerHalfDist);
154- return iter;
154+ HdrHistogram::Iterator itr (histogram, Iterator::IterMode::Percentiles);
155+ hdr_iter_percentile_init (&itr, itr.histoRLockPtr ->get (), ticksPerHalfDist);
156+ return itr;
155157}
156158
157159HdrHistogram::Iterator HdrHistogram::makeRecordedIterator () const {
158- HdrHistogram::Iterator iter{};
159- iter.type = Iterator::IterMode::Recorded;
160- hdr_iter_recorded_init (&iter, histogram.get ());
161- return iter;
160+ HdrHistogram::Iterator itr (histogram, Iterator::IterMode::Recorded);
161+ hdr_iter_recorded_init (&itr, itr.histoRLockPtr ->get ());
162+ return itr;
162163}
163164
164165HdrHistogram::Iterator HdrHistogram::makeIterator (
@@ -246,16 +247,16 @@ HdrHistogram::getNextValueAndPercentile(Iterator& iter) const {
246247}
247248
248249void HdrHistogram::printPercentiles () const {
249- hdr_percentiles_print (histogram.get (), stdout, 5 , 1.0 , CLASSIC);
250+ hdr_percentiles_print (histogram.rlock ()-> get (), stdout, 5 , 1.0 , CLASSIC);
250251}
251252
252253void HdrHistogram::dumpLinearValues (int64_t bucketWidth) {
253- HdrHistogram::Iterator itr = makeLinearIterator (bucketWidth);
254+ auto itr = makeLinearIterator (bucketWidth);
254255 std::cout << dumpValues (itr).str ();
255256}
256257
257258void HdrHistogram::dumpLogValues (int64_t firstBucketWidth, double log_base) {
258- HdrHistogram::Iterator itr = makeLogIterator (firstBucketWidth, log_base);
259+ auto itr = makeLogIterator (firstBucketWidth, log_base);
259260 std::cout << dumpValues (itr).str ();
260261}
261262
@@ -275,7 +276,7 @@ nlohmann::json HdrHistogram::to_json(Iterator::IterMode itrType) {
275276 nlohmann::json rootObj;
276277
277278 // Five is the number of iteration steps per half-distance to 100%.
278- Iterator itr = makePercentileIterator (5 );
279+ auto itr = makePercentileIterator (5 );
279280
280281 rootObj[" total" ] = itr.total_count ;
281282 // bucketsLow represents the starting value of the first bucket
@@ -302,11 +303,11 @@ std::string HdrHistogram::to_string() {
302303}
303304
304305size_t HdrHistogram::getMemFootPrint () const {
305- return hdr_get_memory_size (histogram.get ()) + sizeof (HdrHistogram);
306+ return hdr_get_memory_size (histogram.rlock ()-> get ()) + sizeof (HdrHistogram);
306307}
307308
308309double HdrHistogram::getMean () const {
309- return hdr_mean (histogram.get ()) - 1 ;
310+ return hdr_mean (histogram.rlock ()-> get ()) - 1 ;
310311}
311312
312313void HdrHistogram::resize (uint64_t lowestTrackableValue,
@@ -331,6 +332,6 @@ void HdrHistogram::resize(uint64_t lowestTrackableValue,
331332 &hist, // Pointer to initialise
332333 cb_calloc);
333334
334- hdr_add (hist, histogram.get ());
335- histogram.reset (hist);
335+ hdr_add (hist, histogram.rlock ()-> get ());
336+ histogram.wlock ()-> reset (hist);
336337}
0 commit comments