2020#include < folly/lang/Assume.h>
2121#include < nlohmann/json.hpp>
2222#include < platform/cb_malloc.h>
23+ #include < spdlog/fmt/fmt.h>
2324#include < iostream>
2425#include < optional>
25- #include < sstream>
2626
2727// Custom deleter for the hdr_histogram struct.
2828void HdrHistogram::HdrDeleter::operator ()(struct hdr_histogram * val) {
@@ -85,6 +85,17 @@ HdrHistogram& HdrHistogram::operator+=(const HdrHistogram& other) {
8585 return *this ;
8686}
8787
88+ HdrHistogram& HdrHistogram::operator =(const HdrHistogram& other) {
89+ if (this != &other) {
90+ // reset this object to make sure we are in a state to copy too
91+ this ->reset ();
92+ // take advantage of the code already written in for the addition
93+ // assigment operator
94+ *this += other;
95+ }
96+ return *this ;
97+ }
98+
8899bool HdrHistogram::addValue (uint64_t v) {
89100 // A hdr_histogram cannot store 0, therefore we add a bias of +1.
90101 int64_t vBiased = v + 1 ;
@@ -187,26 +198,26 @@ HdrHistogram::Iterator HdrHistogram::getHistogramsIterator() const {
187198 return makeIterator (defaultIterationMode);
188199}
189200
190- std::optional<std::pair<uint64_t , uint64_t >> HdrHistogram::getNextValueAndCount (
191- Iterator& iter) const {
201+ std::optional<std::pair<uint64_t , uint64_t >>
202+ HdrHistogram:: Iterator::getNextValueAndCount () {
192203 std::optional<std::pair<uint64_t , uint64_t >> valueAndCount;
193- if (hdr_iter_next (&iter )) {
194- auto value = static_cast <uint64_t >(iter. value );
204+ if (hdr_iter_next (this )) {
205+ auto value = static_cast <uint64_t >(this -> value );
195206 uint64_t count = 0 ;
196- switch (iter. type ) {
207+ switch (type) {
197208 case Iterator::IterMode::Log:
198- count = iter. specifics .log .count_added_in_this_iteration_step ;
209+ count = specifics.log .count_added_in_this_iteration_step ;
199210 break ;
200211 case Iterator::IterMode::Linear:
201- count = iter. specifics .linear .count_added_in_this_iteration_step ;
212+ count = specifics.linear .count_added_in_this_iteration_step ;
202213 break ;
203214 case Iterator::IterMode::Percentiles:
204- value = iter. highest_equivalent_value ;
205- count = iter. cumulative_count - iter. lastCumulativeCount ;
206- iter. lastCumulativeCount = iter. cumulative_count ;
215+ value = highest_equivalent_value;
216+ count = cumulative_count - lastCumulativeCount;
217+ lastCumulativeCount = cumulative_count;
207218 break ;
208219 case Iterator::IterMode::Recorded:
209- count = iter. count ;
220+ count = this -> count ;
210221 break ;
211222 }
212223
@@ -220,59 +231,61 @@ std::optional<std::pair<uint64_t, uint64_t>> HdrHistogram::getNextValueAndCount(
220231}
221232
222233std::optional<std::tuple<uint64_t , uint64_t , uint64_t >>
223- HdrHistogram::getNextBucketLowHighAndCount (Iterator& iter) const {
234+ HdrHistogram::Iterator:: getNextBucketLowHighAndCount () {
224235 std::optional<std::tuple<uint64_t , uint64_t , uint64_t >> bucketHighLowCount;
225- auto valueAndCount = getNextValueAndCount (iter );
236+ auto valueAndCount = getNextValueAndCount ();
226237 if (valueAndCount.has_value ()) {
227- bucketHighLowCount = std::make_tuple (
228- iter. lastVal , valueAndCount->first , valueAndCount->second ) ;
229- iter. lastVal = valueAndCount->first ;
238+ bucketHighLowCount = {
239+ lastVal, valueAndCount->first , valueAndCount->second } ;
240+ lastVal = valueAndCount->first ;
230241 }
231242 return bucketHighLowCount;
232243}
233244
234245std::optional<std::pair<uint64_t , double >>
235- HdrHistogram::getNextValueAndPercentile (Iterator& iter) const {
246+ HdrHistogram::Iterator:: getNextValueAndPercentile () {
236247 std::optional<std::pair<uint64_t , double >> valueAndPer;
237- if (iter. type == Iterator::IterMode::Percentiles) {
238- if (hdr_iter_next (&iter )) {
248+ if (type == Iterator::IterMode::Percentiles) {
249+ if (hdr_iter_next (this )) {
239250 // We subtract one from the lowest value as we have added a one
240251 // offset as the underlying hdr_histogram cannot store 0 and
241252 // therefore the value must be greater than or equal to 1.
242- valueAndPer = std::make_pair (iter. highest_equivalent_value - 1 ,
243- iter. specifics .percentiles .percentile ) ;
253+ valueAndPer = { highest_equivalent_value - 1 ,
254+ specifics.percentiles .percentile } ;
244255 }
245256 }
246257 return valueAndPer;
247258}
248259
249- void HdrHistogram::printPercentiles () const {
250- hdr_percentiles_print (histogram.rlock ()->get (), stdout, 5 , 1.0 , CLASSIC);
260+ std::string HdrHistogram::Iterator::dumpValues () {
261+ fmt::memory_buffer dump;
262+ while (auto pair = getNextValueAndCount ()) {
263+ fmt::format_to (dump,
264+ " Value[{}-{}]\t Count:{}\t\n " ,
265+ value_iterated_from,
266+ value_iterated_to,
267+ pair->second );
268+ }
269+ fmt::format_to (dump, " Total:\t {}\n " , total_count);
270+ return {dump.data (), dump.size ()};
251271}
252272
253- void HdrHistogram::dumpLinearValues (int64_t bucketWidth) {
254- auto itr = makeLinearIterator (bucketWidth);
255- std::cout << dumpValues (itr).str ();
273+ void HdrHistogram::printPercentiles () const {
274+ hdr_percentiles_print (histogram.rlock ()->get (), stdout, 5 , 1.0 , CLASSIC);
256275}
257276
258- void HdrHistogram::dumpLogValues (int64_t firstBucketWidth, double log_base) {
259- auto itr = makeLogIterator (firstBucketWidth, log_base );
260- std::cout << dumpValues ( itr). str ( );
277+ void HdrHistogram::dumpLinearValues (int64_t bucketWidth) const {
278+ HdrHistogram::Iterator itr = makeLinearIterator (bucketWidth );
279+ fmt::print (stdout, itr. dumpValues () );
261280}
262281
263- std::stringstream HdrHistogram::dumpValues (Iterator& itr) {
264- std::stringstream dump;
265- while (auto pair = getNextValueAndCount (itr)) {
266- dump << " Value[" << itr.value_iterated_from << " -"
267- << itr.value_iterated_to << " ]\t Count:\t " << pair->second
268- << std::endl;
269- }
270- dump << " Total:\t " << itr.total_count << std::endl;
271-
272- return dump;
282+ void HdrHistogram::dumpLogValues (int64_t firstBucketWidth,
283+ double log_base) const {
284+ HdrHistogram::Iterator itr = makeLogIterator (firstBucketWidth, log_base);
285+ fmt::print (stdout, itr.dumpValues ());
273286}
274287
275- nlohmann::json HdrHistogram::to_json (Iterator::IterMode itrType) {
288+ nlohmann::json HdrHistogram::to_json () const {
276289 nlohmann::json rootObj;
277290
278291 // Five is the number of iteration steps per half-distance to 100%.
@@ -285,7 +298,7 @@ nlohmann::json HdrHistogram::to_json(Iterator::IterMode itrType) {
285298 nlohmann::json dataArr;
286299
287300 int64_t lastval = 0 ;
288- while (auto pair = getNextValueAndPercentile (itr )) {
301+ while (auto pair = itr. getNextValueAndPercentile ()) {
289302 if (!pair.has_value ())
290303 break ;
291304
@@ -298,7 +311,7 @@ nlohmann::json HdrHistogram::to_json(Iterator::IterMode itrType) {
298311 return rootObj;
299312}
300313
301- std::string HdrHistogram::to_string () {
314+ std::string HdrHistogram::to_string () const {
302315 return to_json ().dump ();
303316}
304317
0 commit comments