|
| 1 | +/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
| 2 | +/* |
| 3 | + * Copyright 2018 Couchbase, Inc |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +#include "hdrhistogram.h" |
| 19 | + |
| 20 | +#include <cstdlib> // Required due to the use of free |
| 21 | + |
| 22 | +// Custom deleter for the hdr_histogram struct. |
| 23 | +void HdrHistogram::HdrDeleter::operator()(struct hdr_histogram* val) { |
| 24 | + free(val); |
| 25 | +} |
| 26 | + |
| 27 | +HdrHistogram::HdrHistogram(uint64_t lowestTrackableValue, |
| 28 | + uint64_t highestTrackableValue, |
| 29 | + int significantFigures) { |
| 30 | + struct hdr_histogram* hist; |
| 31 | + // We add a bias of +1 to the lowest and highest trackable value |
| 32 | + // because we add +1 to all values we store in the histogram (as this |
| 33 | + // allows us to record the value 0). |
| 34 | + hdr_init(lowestTrackableValue + 1, |
| 35 | + highestTrackableValue + 1, // Add one because all values |
| 36 | + significantFigures, |
| 37 | + &hist); // Pointer to initialise |
| 38 | + histogram.reset(hist); |
| 39 | +} |
| 40 | + |
| 41 | +void HdrHistogram::addValue(uint64_t v) { |
| 42 | + // A hdr_histogram cannot store 0, therefore we add a bias of +1. |
| 43 | + int64_t vBiased = v + 1; |
| 44 | + hdr_record_value(histogram.get(), vBiased); |
| 45 | +} |
| 46 | + |
| 47 | +void HdrHistogram::addValueAndCount(uint64_t v, uint64_t count) { |
| 48 | + // A hdr_histogram cannot store 0, therefore we add a bias of +1. |
| 49 | + int64_t vBiased = v + 1; |
| 50 | + hdr_record_values(histogram.get(), vBiased, count); |
| 51 | +} |
| 52 | + |
| 53 | +uint64_t HdrHistogram::getValueCount() const { |
| 54 | + return histogram->total_count; |
| 55 | +} |
| 56 | + |
| 57 | +void HdrHistogram::reset() { |
| 58 | + hdr_reset(histogram.get()); |
| 59 | +} |
| 60 | + |
| 61 | +uint64_t HdrHistogram::getValueAtPercentile(double percentage) const { |
| 62 | + // We added the bias of +1 to the input value (see |
| 63 | + // addValueToFreqHistogram). Therefore need to minus the bias |
| 64 | + // before returning the value from the histogram. |
| 65 | + uint64_t value = hdr_value_at_percentile(histogram.get(), percentage); |
| 66 | + return (value - 1); |
| 67 | +} |
| 68 | + |
| 69 | +HdrHistogram::Iterator HdrHistogram::makeLinearIterator( |
| 70 | + int64_t valueUnitsPerBucket) const { |
| 71 | + HdrHistogram::Iterator iter; |
| 72 | + hdr_iter_linear_init(&iter, histogram.get(), valueUnitsPerBucket); |
| 73 | + return iter; |
| 74 | +} |
| 75 | + |
| 76 | +boost::optional<std::pair<uint64_t, uint64_t>> |
| 77 | +HdrHistogram::getNextValueAndCount(Iterator& iter) const { |
| 78 | + boost::optional<std::pair<uint64_t, uint64_t>> valueAndCount; |
| 79 | + if (hdr_iter_next(&iter)) { |
| 80 | + uint64_t value = iter.value; |
| 81 | + uint64_t count = hdr_count_at_value(histogram.get(), value); |
| 82 | + // We added the bias of +1 to the input value (see |
| 83 | + // addValueToFreqHistogram). Therefore need to minus the bias |
| 84 | + // before returning value. |
| 85 | + return valueAndCount = std::make_pair(value - 1, count); |
| 86 | + } else { |
| 87 | + return valueAndCount; |
| 88 | + } |
| 89 | +} |
0 commit comments