2020-11-02 04:15:39 +08:00
|
|
|
/*
|
|
|
|
* Histogram.h
|
|
|
|
*
|
|
|
|
* This source file is part of the FoundationDB open source project
|
|
|
|
*
|
|
|
|
* Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef FLOW_HISTOGRAM_H
|
|
|
|
#define FLOW_HISTOGRAM_H
|
|
|
|
#pragma once
|
|
|
|
|
2020-11-03 03:09:45 +08:00
|
|
|
#include <flow/Arena.h>
|
|
|
|
|
2020-11-02 04:15:39 +08:00
|
|
|
#include <string>
|
|
|
|
#include <map>
|
2020-11-13 07:38:51 +08:00
|
|
|
#include <unordered_map>
|
2021-06-24 04:46:17 +08:00
|
|
|
#include <iostream>
|
2021-06-25 04:08:40 +08:00
|
|
|
#include <iomanip>
|
2020-11-02 04:15:39 +08:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <intrin.h>
|
|
|
|
#pragma intrinsic(_BitScanReverse)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
class Histogram;
|
|
|
|
|
|
|
|
class HistogramRegistry {
|
2020-11-06 02:26:51 +08:00
|
|
|
public:
|
|
|
|
void registerHistogram(Histogram* h);
|
|
|
|
void unregisterHistogram(Histogram* h);
|
2021-06-10 02:37:14 +08:00
|
|
|
Histogram* lookupHistogram(std::string const& name);
|
2020-11-06 02:26:51 +08:00
|
|
|
void logReport();
|
|
|
|
|
|
|
|
private:
|
2020-11-07 11:13:45 +08:00
|
|
|
// This map is ordered by key so that ops within the same group end up
|
|
|
|
// next to each other in the trace log.
|
2020-11-06 02:26:51 +08:00
|
|
|
std::map<std::string, Histogram*> histograms;
|
2020-11-02 04:15:39 +08:00
|
|
|
};
|
|
|
|
|
2020-11-06 02:26:51 +08:00
|
|
|
HistogramRegistry& GetHistogramRegistry();
|
2020-11-02 04:15:39 +08:00
|
|
|
|
2020-11-07 11:13:45 +08:00
|
|
|
/*
|
|
|
|
* A fast histogram with power-of-two spaced buckets.
|
|
|
|
*
|
|
|
|
* For more information about this technique, see:
|
|
|
|
* https://www.fsl.cs.stonybrook.edu/project-osprof.html
|
|
|
|
*/
|
2020-11-24 03:40:24 +08:00
|
|
|
class Histogram final : public ReferenceCounted<Histogram> {
|
2020-11-02 04:15:39 +08:00
|
|
|
public:
|
2021-06-24 07:02:55 +08:00
|
|
|
enum class Unit { microseconds, bytes, bytes_per_second, percentage, record_counter };
|
2020-11-02 04:15:39 +08:00
|
|
|
|
2020-11-06 02:25:21 +08:00
|
|
|
private:
|
2021-06-25 00:48:26 +08:00
|
|
|
//static const std::unordered_map<Unit, std::string> UnitToStringMapper;
|
|
|
|
const std::unordered_map<Unit, std::string> UnitToStringMapper;
|
2020-11-13 07:38:51 +08:00
|
|
|
|
2021-06-24 07:02:55 +08:00
|
|
|
Histogram(std::string const& group, std::string const& op, Unit unit, HistogramRegistry& registry, uint32_t lower, uint32_t upper)
|
2021-06-25 00:48:26 +08:00
|
|
|
: UnitToStringMapper (
|
2021-06-29 03:23:09 +08:00
|
|
|
{ { Histogram::Unit::microseconds, "microseconds" },
|
|
|
|
{ Histogram::Unit::bytes, "bytes" },
|
2021-06-25 00:48:26 +08:00
|
|
|
{ Histogram::Unit::bytes_per_second, "bytes_per_second" },
|
|
|
|
{ Histogram::Unit::percentage, "percentage" },
|
|
|
|
{ Histogram::Unit::record_counter, "record_counter" },
|
2021-06-29 03:23:09 +08:00
|
|
|
}),
|
|
|
|
group(group), op(op), unit(unit), registry(registry), lowerBound(lower), upperBound(upper), ReferenceCounted<Histogram>() {
|
2021-06-25 00:48:26 +08:00
|
|
|
|
|
|
|
ASSERT(UnitToStringMapper.find(unit) != UnitToStringMapper.end());
|
2020-11-13 07:38:51 +08:00
|
|
|
|
2021-06-29 07:32:04 +08:00
|
|
|
ASSERT(upperBound >= lowerBound);
|
|
|
|
|
2020-11-06 02:26:51 +08:00
|
|
|
clear();
|
|
|
|
}
|
2020-11-02 04:15:39 +08:00
|
|
|
|
2021-06-10 02:37:14 +08:00
|
|
|
static std::string generateName(std::string const& group, std::string const& op) { return group + ":" + op; }
|
2020-11-06 02:25:21 +08:00
|
|
|
|
|
|
|
public:
|
2020-11-06 02:26:51 +08:00
|
|
|
~Histogram() { registry.unregisterHistogram(this); }
|
|
|
|
|
2021-06-24 07:02:55 +08:00
|
|
|
static Reference<Histogram> getHistogram(StringRef group, StringRef op, Unit unit, uint32_t lower = 0, uint32_t upper = UINT32_MAX) {
|
2020-11-06 02:26:51 +08:00
|
|
|
std::string group_str = group.toString();
|
|
|
|
std::string op_str = op.toString();
|
|
|
|
std::string name = generateName(group_str, op_str);
|
|
|
|
HistogramRegistry& registry = GetHistogramRegistry();
|
|
|
|
Histogram* h = registry.lookupHistogram(name);
|
|
|
|
if (!h) {
|
2021-06-24 07:02:55 +08:00
|
|
|
h = new Histogram(group_str, op_str, unit, registry, lower, upper);
|
2020-11-07 11:13:45 +08:00
|
|
|
registry.registerHistogram(h);
|
|
|
|
return Reference<Histogram>(h);
|
|
|
|
} else {
|
|
|
|
return Reference<Histogram>::addRef(h);
|
2020-11-06 02:26:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-07 11:13:45 +08:00
|
|
|
// This histogram buckets samples into powers of two.
|
2020-11-06 02:26:51 +08:00
|
|
|
inline void sample(uint32_t sample) {
|
2020-11-07 11:13:45 +08:00
|
|
|
size_t idx;
|
2020-11-02 04:15:39 +08:00
|
|
|
#ifdef _WIN32
|
2020-11-06 02:26:51 +08:00
|
|
|
unsigned long index;
|
2020-11-07 11:13:45 +08:00
|
|
|
// _BitScanReverse sets index to the position of the first non-zero bit, so
|
|
|
|
// _BitScanReverse(sample) ~= log_2(sample). _BitScanReverse returns false if
|
|
|
|
// sample is zero.
|
|
|
|
idx = _BitScanReverse(&index, sample) ? index : 0;
|
2020-11-02 04:15:39 +08:00
|
|
|
#else
|
2020-11-07 11:13:45 +08:00
|
|
|
// __builtin_clz counts the leading zeros in its uint32_t argument. So, 31-clz ~= log_2(sample).
|
|
|
|
// __builtin_clz(0) is undefined.
|
|
|
|
idx = sample ? (31 - __builtin_clz(sample)) : 0;
|
2020-11-02 04:15:39 +08:00
|
|
|
#endif
|
2020-11-07 11:13:45 +08:00
|
|
|
ASSERT(idx < 32);
|
|
|
|
buckets[idx]++;
|
2020-11-06 02:26:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void sampleSeconds(double delta) {
|
|
|
|
uint64_t delta_usec = (delta * 1000000);
|
|
|
|
if (delta_usec > UINT32_MAX) {
|
|
|
|
sample(UINT32_MAX);
|
|
|
|
} else {
|
|
|
|
sample((uint32_t)(delta * 1000000)); // convert to microseconds and truncate to integer
|
|
|
|
}
|
|
|
|
}
|
2021-06-24 07:02:55 +08:00
|
|
|
// Histogram buckets samples into linear interval of size 4 percent.
|
2021-06-24 05:54:41 +08:00
|
|
|
inline void samplePercentage(double pct) {
|
|
|
|
ASSERT(pct>=0.0);
|
|
|
|
if (pct >= 1.28){
|
|
|
|
pct = 1.24;
|
|
|
|
}
|
|
|
|
size_t idx = (pct*100) / 4;
|
|
|
|
ASSERT(idx < 32);
|
|
|
|
buckets[idx]++;
|
|
|
|
}
|
2020-11-06 02:26:51 +08:00
|
|
|
|
2021-06-24 07:02:55 +08:00
|
|
|
// Histogram buckets samples into one of the same sized buckets
|
|
|
|
// This is used when the distance b/t upperBound and lowerBound are relativly small
|
|
|
|
inline void sampleRecordCounter(uint32_t sample) {
|
2021-06-30 02:32:21 +08:00
|
|
|
if(sample > upperBound){
|
|
|
|
sample = upperBound;
|
|
|
|
}
|
2021-06-29 00:32:10 +08:00
|
|
|
size_t idx = ( (sample - lowerBound) * 31.0 ) / (upperBound - lowerBound);
|
2021-06-29 04:55:22 +08:00
|
|
|
ASSERT(idx < 32);
|
2021-06-24 07:02:55 +08:00
|
|
|
buckets[idx]++;
|
|
|
|
}
|
|
|
|
|
2021-06-30 12:01:28 +08:00
|
|
|
void updateUpperBound(uint32_t upperBound){
|
|
|
|
this->upperBound = upperBound;
|
|
|
|
}
|
|
|
|
|
2020-11-06 02:26:51 +08:00
|
|
|
void clear() {
|
|
|
|
for (uint32_t& i : buckets) {
|
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void writeToLog();
|
|
|
|
|
2021-06-10 02:37:14 +08:00
|
|
|
std::string name() const { return generateName(this->group, this->op); }
|
2020-11-06 02:26:51 +08:00
|
|
|
|
2021-06-26 07:12:38 +08:00
|
|
|
std::string drawHistogram();
|
2021-06-25 04:08:40 +08:00
|
|
|
|
2020-11-06 02:26:51 +08:00
|
|
|
std::string const group;
|
|
|
|
std::string const op;
|
|
|
|
Unit const unit;
|
|
|
|
HistogramRegistry& registry;
|
|
|
|
uint32_t buckets[32];
|
2021-06-24 07:02:55 +08:00
|
|
|
uint32_t lowerBound;
|
|
|
|
uint32_t upperBound;
|
2020-11-02 04:15:39 +08:00
|
|
|
};
|
|
|
|
|
2020-11-24 03:40:24 +08:00
|
|
|
#endif // FLOW_HISTOGRAM_H
|