2011-03-18 23:37:00 +01:00
|
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
|
|
|
2013-02-15 20:53:17 +01:00
|
|
|
#include "util/histogram.h"
|
|
|
|
|
2013-01-29 21:23:31 +01:00
|
|
|
#include <cassert>
|
2011-03-18 23:37:00 +01:00
|
|
|
#include <math.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "port/port.h"
|
|
|
|
|
|
|
|
namespace leveldb {
|
|
|
|
|
2013-01-29 21:23:31 +01:00
|
|
|
HistogramBucketMapper::HistogramBucketMapper() :
|
|
|
|
// Add newer bucket index here.
|
|
|
|
// Should be alwyas added in sorted order.
|
|
|
|
bucketValues_({
|
2011-03-18 23:37:00 +01:00
|
|
|
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 14, 16, 18, 20, 25, 30, 35, 40, 45,
|
|
|
|
50, 60, 70, 80, 90, 100, 120, 140, 160, 180, 200, 250, 300, 350, 400, 450,
|
|
|
|
500, 600, 700, 800, 900, 1000, 1200, 1400, 1600, 1800, 2000, 2500, 3000,
|
|
|
|
3500, 4000, 4500, 5000, 6000, 7000, 8000, 9000, 10000, 12000, 14000,
|
|
|
|
16000, 18000, 20000, 25000, 30000, 35000, 40000, 45000, 50000, 60000,
|
|
|
|
70000, 80000, 90000, 100000, 120000, 140000, 160000, 180000, 200000,
|
|
|
|
250000, 300000, 350000, 400000, 450000, 500000, 600000, 700000, 800000,
|
|
|
|
900000, 1000000, 1200000, 1400000, 1600000, 1800000, 2000000, 2500000,
|
|
|
|
3000000, 3500000, 4000000, 4500000, 5000000, 6000000, 7000000, 8000000,
|
|
|
|
9000000, 10000000, 12000000, 14000000, 16000000, 18000000, 20000000,
|
|
|
|
25000000, 30000000, 35000000, 40000000, 45000000, 50000000, 60000000,
|
|
|
|
70000000, 80000000, 90000000, 100000000, 120000000, 140000000, 160000000,
|
|
|
|
180000000, 200000000, 250000000, 300000000, 350000000, 400000000,
|
|
|
|
450000000, 500000000, 600000000, 700000000, 800000000, 900000000,
|
2013-01-29 21:23:31 +01:00
|
|
|
1000000000}),
|
|
|
|
maxBucketValue_(bucketValues_.back()),
|
|
|
|
minBucketValue_(bucketValues_.front()) {
|
|
|
|
for (size_t i =0; i < bucketValues_.size(); ++i) {
|
|
|
|
valueIndexMap_[bucketValues_[i]] = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const size_t HistogramBucketMapper::IndexForValue(const uint64_t value) const {
|
|
|
|
if (value >= maxBucketValue_) {
|
|
|
|
return bucketValues_.size() - 1;
|
|
|
|
} else if ( value >= minBucketValue_ ) {
|
|
|
|
std::map<uint64_t, uint64_t>::const_iterator lowerBound =
|
|
|
|
valueIndexMap_.lower_bound(value);
|
|
|
|
if (lowerBound != valueIndexMap_.end()) {
|
|
|
|
return lowerBound->second;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
const HistogramBucketMapper bucketMapper;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-02-15 20:53:17 +01:00
|
|
|
HistogramImpl::HistogramImpl() :
|
2013-02-01 02:07:31 +01:00
|
|
|
min_(bucketMapper.LastValue()),
|
|
|
|
max_(0),
|
|
|
|
num_(0),
|
|
|
|
sum_(0),
|
|
|
|
sum_squares_(0),
|
2013-01-29 21:23:31 +01:00
|
|
|
buckets_(std::vector<uint64_t>(bucketMapper.BucketCount(), 0)) {}
|
2011-03-18 23:37:00 +01:00
|
|
|
|
2013-02-15 20:53:17 +01:00
|
|
|
void HistogramImpl::Clear() {
|
2013-01-29 21:23:31 +01:00
|
|
|
min_ = bucketMapper.LastValue();
|
2011-03-18 23:37:00 +01:00
|
|
|
max_ = 0;
|
|
|
|
num_ = 0;
|
|
|
|
sum_ = 0;
|
|
|
|
sum_squares_ = 0;
|
2013-01-29 21:23:31 +01:00
|
|
|
buckets_.resize(bucketMapper.BucketCount(), 0);
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
|
|
|
|
2013-02-15 20:53:17 +01:00
|
|
|
void HistogramImpl::Add(uint64_t value) {
|
2013-01-29 21:23:31 +01:00
|
|
|
const size_t index = bucketMapper.IndexForValue(value);
|
|
|
|
buckets_[index] += 1;
|
2011-03-18 23:37:00 +01:00
|
|
|
if (min_ > value) min_ = value;
|
|
|
|
if (max_ < value) max_ = value;
|
|
|
|
num_++;
|
|
|
|
sum_ += value;
|
|
|
|
sum_squares_ += (value * value);
|
|
|
|
}
|
|
|
|
|
2013-02-15 20:53:17 +01:00
|
|
|
void HistogramImpl::Add(double value) {
|
2013-01-29 21:23:31 +01:00
|
|
|
Add(static_cast<uint64_t>(value));
|
|
|
|
}
|
|
|
|
|
2013-02-15 20:53:17 +01:00
|
|
|
void HistogramImpl::Merge(const HistogramImpl& other) {
|
2011-08-22 23:08:51 +02:00
|
|
|
if (other.min_ < min_) min_ = other.min_;
|
|
|
|
if (other.max_ > max_) max_ = other.max_;
|
|
|
|
num_ += other.num_;
|
|
|
|
sum_ += other.sum_;
|
|
|
|
sum_squares_ += other.sum_squares_;
|
2013-01-29 21:23:31 +01:00
|
|
|
for (int b = 0; b < bucketMapper.BucketCount(); b++) {
|
2011-08-22 23:08:51 +02:00
|
|
|
buckets_[b] += other.buckets_[b];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-15 20:53:17 +01:00
|
|
|
double HistogramImpl::Median() const {
|
2011-03-18 23:37:00 +01:00
|
|
|
return Percentile(50.0);
|
|
|
|
}
|
|
|
|
|
2013-02-15 20:53:17 +01:00
|
|
|
double HistogramImpl::Percentile(double p) const {
|
2011-03-18 23:37:00 +01:00
|
|
|
double threshold = num_ * (p / 100.0);
|
|
|
|
double sum = 0;
|
2013-01-29 21:23:31 +01:00
|
|
|
for (int b = 0; b < bucketMapper.BucketCount(); b++) {
|
2011-03-18 23:37:00 +01:00
|
|
|
sum += buckets_[b];
|
|
|
|
if (sum >= threshold) {
|
|
|
|
// Scale linearly within this bucket
|
2013-01-29 21:23:31 +01:00
|
|
|
double left_point = (b == 0) ? 0 : bucketMapper.BucketLimit(b-1);
|
|
|
|
double right_point = bucketMapper.BucketLimit(b);
|
2011-03-18 23:37:00 +01:00
|
|
|
double left_sum = sum - buckets_[b];
|
|
|
|
double right_sum = sum;
|
2013-01-29 21:23:31 +01:00
|
|
|
double pos = 0;
|
|
|
|
double right_left_diff = right_sum - left_sum;
|
|
|
|
if (right_left_diff != 0) {
|
|
|
|
pos = (threshold - left_sum) / (right_sum - left_sum);
|
|
|
|
}
|
2011-03-18 23:37:00 +01:00
|
|
|
double r = left_point + (right_point - left_point) * pos;
|
|
|
|
if (r < min_) r = min_;
|
|
|
|
if (r > max_) r = max_;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return max_;
|
|
|
|
}
|
|
|
|
|
2013-02-15 20:53:17 +01:00
|
|
|
double HistogramImpl::Average() const {
|
2011-03-18 23:37:00 +01:00
|
|
|
if (num_ == 0.0) return 0;
|
|
|
|
return sum_ / num_;
|
|
|
|
}
|
|
|
|
|
2013-02-15 20:53:17 +01:00
|
|
|
double HistogramImpl::StandardDeviation() const {
|
2011-03-18 23:37:00 +01:00
|
|
|
if (num_ == 0.0) return 0;
|
|
|
|
double variance = (sum_squares_ * num_ - sum_ * sum_) / (num_ * num_);
|
|
|
|
return sqrt(variance);
|
|
|
|
}
|
|
|
|
|
2013-02-15 20:53:17 +01:00
|
|
|
std::string HistogramImpl::ToString() const {
|
2011-03-18 23:37:00 +01:00
|
|
|
std::string r;
|
|
|
|
char buf[200];
|
|
|
|
snprintf(buf, sizeof(buf),
|
|
|
|
"Count: %.0f Average: %.4f StdDev: %.2f\n",
|
|
|
|
num_, Average(), StandardDeviation());
|
|
|
|
r.append(buf);
|
|
|
|
snprintf(buf, sizeof(buf),
|
|
|
|
"Min: %.4f Median: %.4f Max: %.4f\n",
|
|
|
|
(num_ == 0.0 ? 0.0 : min_), Median(), max_);
|
|
|
|
r.append(buf);
|
2013-02-07 02:12:43 +01:00
|
|
|
snprintf(buf, sizeof(buf),
|
|
|
|
"Percentiles: P50: %.2f P75: %.2f P99: %.2f P99.9: %.2f\n",
|
|
|
|
Percentile(50), Percentile(75), Percentile(99), Percentile(99.9));
|
|
|
|
r.append(buf);
|
2011-03-18 23:37:00 +01:00
|
|
|
r.append("------------------------------------------------------\n");
|
|
|
|
const double mult = 100.0 / num_;
|
|
|
|
double sum = 0;
|
2013-01-29 21:23:31 +01:00
|
|
|
for (int b = 0; b < bucketMapper.BucketCount(); b++) {
|
2011-03-18 23:37:00 +01:00
|
|
|
if (buckets_[b] <= 0.0) continue;
|
|
|
|
sum += buckets_[b];
|
|
|
|
snprintf(buf, sizeof(buf),
|
2013-02-07 02:12:43 +01:00
|
|
|
"[ %7ld, %7ld ) %8ld %7.3f%% %7.3f%% ",
|
2013-01-29 21:23:31 +01:00
|
|
|
((b == 0) ? 0 : bucketMapper.BucketLimit(b-1)), // left
|
|
|
|
bucketMapper.BucketLimit(b), // right
|
|
|
|
buckets_[b], // count
|
|
|
|
mult * buckets_[b], // percentage
|
|
|
|
mult * sum); // cumulative percentage
|
2011-03-18 23:37:00 +01:00
|
|
|
r.append(buf);
|
|
|
|
|
|
|
|
// Add hash marks based on percentage; 20 marks for 100%.
|
|
|
|
int marks = static_cast<int>(20*(buckets_[b] / num_) + 0.5);
|
|
|
|
r.append(marks, '#');
|
|
|
|
r.push_back('\n');
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2013-02-15 20:53:17 +01:00
|
|
|
void HistogramImpl::Data(HistogramData * const data) const {
|
|
|
|
assert(data);
|
|
|
|
data->median = Median();
|
|
|
|
data->percentile95 = Percentile(95);
|
|
|
|
data->percentile99 = Percentile(99);
|
|
|
|
data->average = Average();
|
|
|
|
data->standard_deviation = StandardDeviation();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace levedb
|