11// Copyright (c) 2014-2016, The Regents of the University of California.
2- // Copyright (c) 2016-2017 , Nefeli Networks, Inc.
2+ // Copyright (c) 2016-2019 , Nefeli Networks, Inc.
33// All rights reserved.
44//
55// Redistribution and use in source and binary forms, with or without
4242
4343#include < glog/logging.h>
4444
45+ #include " ../pb/util_msg.pb.h"
46+
4547// Class for general purpose histogram. T generally should be an
4648// integral type, though floating point types will also work.
4749// A bin b_i corresponds for the range [i * width, (i + 1) * width)
@@ -52,7 +54,9 @@ class Histogram {
5254 public:
5355 static_assert (std::is_arithmetic<T>::value, " Arithmetic type required." );
5456 struct Summary {
55- size_t count; // # of all samples. If 0, min, max and avg are also 0
57+ size_t num_buckets; // Number of buckets in the histogram
58+ size_t bucket_width; // Resolution of the measured data
59+ size_t count; // # of samples (including above_range). If 0, min, max and avg are also 0
5660 size_t above_range; // # of samples beyond the histogram range
5761 T min; // Min value
5862 T max; // Max value. May be underestimated if above_range > 0
@@ -124,6 +128,8 @@ class Histogram {
124128 // percentile_values
125129 const Summary Summarize (const std::vector<double > &percentiles = {}) const {
126130 Summary ret = {};
131+ ret.num_buckets = num_buckets ();
132+ ret.bucket_width = bucket_width_;
127133 uint64_t count = std::accumulate (buckets_.begin (), buckets_.end (), 0 );
128134 ret.count = count;
129135 ret.above_range = buckets_.back ();
@@ -201,4 +207,21 @@ class Histogram {
201207 std::vector<std::atomic<uint64_t >> buckets_;
202208};
203209
210+ bool IsValidPercentiles (const std::vector<double > &percentiles);
211+
212+ template <typename T>
213+ void SetHistogram (bess::pb::HistogramSummary *r, const T &summary) {
214+ r->set_num_buckets (summary.num_buckets );
215+ r->set_bucket_width (summary.bucket_width );
216+ r->set_count (summary.count );
217+ r->set_above_range (summary.above_range );
218+ r->set_min (summary.min );
219+ r->set_max (summary.max );
220+ r->set_avg (summary.avg );
221+ r->set_total (summary.total );
222+ for (const auto &val : summary.percentile_values ) {
223+ r->add_percentile_values (val);
224+ }
225+ }
226+
204227#endif // BESS_UTILS_HISTOGRAM_H_
0 commit comments