2020-11-02 04:15:39 +08:00
|
|
|
/*
|
|
|
|
* Histogram.cpp
|
|
|
|
*
|
|
|
|
* This source file is part of the FoundationDB open source project
|
|
|
|
*
|
2022-03-22 04:36:23 +08:00
|
|
|
* Copyright 2013-2022 Apple Inc. and the FoundationDB project authors
|
2020-11-02 04:15:39 +08:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <flow/Histogram.h>
|
2020-11-06 02:25:21 +08:00
|
|
|
#include <flow/flow.h>
|
|
|
|
#include <flow/UnitTest.h>
|
2020-11-02 04:15:39 +08:00
|
|
|
// TODO: remove dependency on fdbrpc.
|
|
|
|
|
|
|
|
// we need to be able to check if we're in simulation so that the histograms are properly
|
|
|
|
// scoped to the right "machine".
|
|
|
|
// either we pull g_simulator into flow, or flow (and the I/O path) will be unable to log performance
|
|
|
|
// metrics.
|
2021-06-05 00:52:29 +08:00
|
|
|
#include <limits>
|
2020-11-02 04:15:39 +08:00
|
|
|
|
2020-11-13 07:38:51 +08:00
|
|
|
#pragma region HistogramRegistry
|
|
|
|
|
2020-11-06 02:26:51 +08:00
|
|
|
HistogramRegistry& GetHistogramRegistry() {
|
2022-06-24 06:35:19 +08:00
|
|
|
auto h = g_network->global(INetwork::enHistogram);
|
|
|
|
|
|
|
|
if (h) {
|
|
|
|
return *reinterpret_cast<HistogramRegistry*>(h);
|
|
|
|
} else {
|
|
|
|
auto res = new HistogramRegistry();
|
|
|
|
g_network->setGlobal(INetwork::enHistogram, res);
|
|
|
|
return *res;
|
2020-11-06 02:26:51 +08:00
|
|
|
}
|
2020-11-02 04:15:39 +08:00
|
|
|
}
|
|
|
|
|
2020-11-06 02:26:51 +08:00
|
|
|
void HistogramRegistry::registerHistogram(Histogram* h) {
|
|
|
|
if (histograms.find(h->name()) != histograms.end()) {
|
|
|
|
TraceEvent(SevError, "HistogramDoubleRegistered").detail("group", h->group).detail("op", h->op);
|
|
|
|
ASSERT(false);
|
|
|
|
}
|
|
|
|
histograms.insert(std::pair<std::string, Histogram*>(h->name(), h));
|
2020-11-02 04:15:39 +08:00
|
|
|
}
|
|
|
|
|
2020-11-06 02:26:51 +08:00
|
|
|
void HistogramRegistry::unregisterHistogram(Histogram* h) {
|
2020-11-07 11:13:45 +08:00
|
|
|
std::string name = h->name();
|
|
|
|
if (histograms.find(name) == histograms.end()) {
|
2020-11-06 02:26:51 +08:00
|
|
|
TraceEvent(SevError, "HistogramNotRegistered").detail("group", h->group).detail("op", h->op);
|
|
|
|
}
|
2020-11-07 11:13:45 +08:00
|
|
|
int count = histograms.erase(name);
|
|
|
|
ASSERT(count == 1);
|
2020-11-02 04:15:39 +08:00
|
|
|
}
|
|
|
|
|
2021-06-10 02:37:14 +08:00
|
|
|
Histogram* HistogramRegistry::lookupHistogram(std::string const& name) {
|
2020-11-06 02:26:51 +08:00
|
|
|
auto h = histograms.find(name);
|
|
|
|
if (h == histograms.end()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return h->second;
|
2020-11-06 02:25:21 +08:00
|
|
|
}
|
|
|
|
|
2021-08-25 00:57:39 +08:00
|
|
|
void HistogramRegistry::logReport(double elapsed) {
|
|
|
|
for (auto& i : histograms) {
|
2021-08-28 04:47:30 +08:00
|
|
|
// Reset all buckets in writeToLog function
|
2021-08-25 00:57:39 +08:00
|
|
|
i.second->writeToLog(elapsed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HistogramRegistry::clear() {
|
2020-11-06 02:26:51 +08:00
|
|
|
for (auto& i : histograms) {
|
|
|
|
i.second->clear();
|
|
|
|
}
|
2020-11-02 04:15:39 +08:00
|
|
|
}
|
|
|
|
|
2020-11-13 07:38:51 +08:00
|
|
|
#pragma endregion // HistogramRegistry
|
|
|
|
|
|
|
|
#pragma region Histogram
|
|
|
|
|
2021-08-25 01:00:24 +08:00
|
|
|
const char* const Histogram::UnitToStringMapper[] = { "microseconds", "bytes", "bytes_per_second",
|
|
|
|
"percentage", "count", "none" };
|
2021-07-15 07:31:13 +08:00
|
|
|
|
2021-08-25 00:57:39 +08:00
|
|
|
void Histogram::writeToLog(double elapsed) {
|
2020-11-06 02:26:51 +08:00
|
|
|
bool active = false;
|
|
|
|
for (uint32_t i = 0; i < 32; i++) {
|
|
|
|
if (buckets[i]) {
|
|
|
|
active = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!active) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
TraceEvent e(SevInfo, "Histogram");
|
2021-07-15 07:31:13 +08:00
|
|
|
e.detail("Group", group).detail("Op", op).detail("Unit", UnitToStringMapper[(size_t)unit]);
|
2021-08-25 06:47:04 +08:00
|
|
|
if (elapsed > 0)
|
|
|
|
e.detail("Elapsed", elapsed);
|
2021-07-27 08:40:34 +08:00
|
|
|
int totalCount = 0;
|
2020-11-06 02:26:51 +08:00
|
|
|
for (uint32_t i = 0; i < 32; i++) {
|
2021-06-05 04:09:52 +08:00
|
|
|
uint64_t value = uint64_t(1) << (i + 1);
|
2020-11-13 07:38:51 +08:00
|
|
|
|
2020-11-06 02:26:51 +08:00
|
|
|
if (buckets[i]) {
|
2021-07-27 08:40:34 +08:00
|
|
|
totalCount += buckets[i];
|
2020-11-06 02:26:51 +08:00
|
|
|
switch (unit) {
|
2020-11-13 07:38:51 +08:00
|
|
|
case Unit::microseconds:
|
2021-08-27 04:02:55 +08:00
|
|
|
e.detail(format("LessThan%u.%03u", int(value / 1000), int(value % 1000)), buckets[i]);
|
2020-11-06 02:26:51 +08:00
|
|
|
break;
|
|
|
|
case Unit::bytes:
|
2020-11-13 07:38:51 +08:00
|
|
|
case Unit::bytes_per_second:
|
2021-08-27 04:02:55 +08:00
|
|
|
e.detail(format("LessThan%" PRIu64, value), buckets[i]);
|
2020-11-06 02:26:51 +08:00
|
|
|
break;
|
2021-08-27 04:08:11 +08:00
|
|
|
case Unit::percentageLinear:
|
2021-07-13 07:07:18 +08:00
|
|
|
e.detail(format("LessThan%f", (i + 1) * 0.04), buckets[i]);
|
2021-06-24 05:54:41 +08:00
|
|
|
break;
|
2021-08-27 04:08:11 +08:00
|
|
|
case Unit::countLinear:
|
2021-08-21 00:24:59 +08:00
|
|
|
value = uint64_t((i + 1) * ((upperBound - lowerBound) / 31.0));
|
2021-08-27 04:02:55 +08:00
|
|
|
e.detail(format("LessThan%" PRIu64, value), buckets[i]);
|
2021-06-29 07:32:04 +08:00
|
|
|
break;
|
2021-07-28 11:05:17 +08:00
|
|
|
case Unit::MAXHISTOGRAMUNIT:
|
|
|
|
e.detail(format("Default%u", i), buckets[i]);
|
|
|
|
break;
|
2020-11-06 02:26:51 +08:00
|
|
|
default:
|
|
|
|
ASSERT(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-27 08:40:34 +08:00
|
|
|
e.detail("TotalCount", totalCount);
|
2021-08-28 04:45:11 +08:00
|
|
|
clear();
|
2020-11-02 04:15:39 +08:00
|
|
|
}
|
2020-11-06 02:25:21 +08:00
|
|
|
|
2021-07-13 07:07:18 +08:00
|
|
|
std::string Histogram::drawHistogram() {
|
2021-06-26 07:12:38 +08:00
|
|
|
|
|
|
|
std::stringstream result;
|
2021-06-25 04:08:40 +08:00
|
|
|
|
|
|
|
const char* verticalLine = "├\0";
|
|
|
|
const char* origin = "└\0";
|
|
|
|
const char* emptyCell = "------\0";
|
|
|
|
const char* halfCell = "---▄▄▄\0";
|
|
|
|
const char* fullCell = "---███\0";
|
|
|
|
const char* xFull = "---▀▀▀\0";
|
|
|
|
const char* xEmpty = "------\0";
|
|
|
|
const char* lineEnd = "--- \0";
|
|
|
|
const unsigned int width = std::strlen(emptyCell);
|
|
|
|
|
|
|
|
int max_lines = 23;
|
|
|
|
uint32_t total = 0;
|
|
|
|
double maxPct = 0;
|
|
|
|
|
2021-07-13 07:07:18 +08:00
|
|
|
for (int i = 0; i < 32; i++) {
|
2021-06-25 04:08:40 +08:00
|
|
|
total += buckets[i];
|
|
|
|
}
|
2021-07-13 07:07:18 +08:00
|
|
|
for (int i = 0; i < 32; i++) {
|
|
|
|
maxPct = std::max(maxPct, (100.0 * buckets[i]) / total);
|
2021-06-25 04:08:40 +08:00
|
|
|
}
|
|
|
|
|
2021-07-13 07:07:18 +08:00
|
|
|
double intervalSize = (maxPct < (max_lines - 3)) ? 1 : maxPct / (max_lines - 3);
|
2021-06-25 04:08:40 +08:00
|
|
|
unsigned int lines = (maxPct < (max_lines - 3)) ? (unsigned int)maxPct : (max_lines - 3);
|
|
|
|
|
2021-07-13 07:07:18 +08:00
|
|
|
result << "Total Inputs: " << total << std::fixed << "\n";
|
|
|
|
result << "Percent"
|
|
|
|
<< "\n";
|
|
|
|
for (int l = 0; l < lines; l++) {
|
2021-06-25 04:08:40 +08:00
|
|
|
double currHeight = (lines - l) * intervalSize;
|
|
|
|
double halfFullHeight = currHeight - intervalSize / 4;
|
2021-07-13 07:07:18 +08:00
|
|
|
result << std::setw(6) << std::setprecision(2) << currHeight << " " << verticalLine;
|
|
|
|
for (int i = 0; i < 32; i++) {
|
2021-06-25 04:08:40 +08:00
|
|
|
double pct = (100.0 * buckets[i]) / total;
|
2021-07-13 07:07:18 +08:00
|
|
|
if (pct > currHeight)
|
|
|
|
result << fullCell;
|
|
|
|
else if (pct > halfFullHeight)
|
|
|
|
result << halfCell;
|
|
|
|
else
|
|
|
|
result << emptyCell;
|
2021-06-25 04:08:40 +08:00
|
|
|
}
|
2021-07-13 07:07:18 +08:00
|
|
|
result << lineEnd << "\n";
|
2021-06-25 04:08:40 +08:00
|
|
|
}
|
|
|
|
|
2021-07-13 07:07:18 +08:00
|
|
|
result << " 0.00 " << origin;
|
|
|
|
for (int i = 0; i < 32; i++) {
|
2021-06-25 04:08:40 +08:00
|
|
|
double pct = (100.0 * buckets[i]) / total;
|
2021-07-13 07:07:18 +08:00
|
|
|
if (pct > intervalSize / 4)
|
|
|
|
result << xFull;
|
|
|
|
else
|
|
|
|
result << xEmpty;
|
2021-06-25 04:08:40 +08:00
|
|
|
}
|
2021-07-13 07:07:18 +08:00
|
|
|
result << lineEnd << "\n";
|
2021-06-25 04:08:40 +08:00
|
|
|
|
2021-07-13 07:07:18 +08:00
|
|
|
result << std::string(9, ' ');
|
|
|
|
for (int i = 0; i < 32; i++) {
|
|
|
|
result << std::left << std::setw(width) << " B" + std::to_string(i);
|
2021-06-25 04:08:40 +08:00
|
|
|
}
|
2021-07-13 07:07:18 +08:00
|
|
|
result << "\n";
|
2021-06-26 07:12:38 +08:00
|
|
|
return result.str();
|
2021-06-25 04:08:40 +08:00
|
|
|
}
|
|
|
|
|
2020-11-13 07:38:51 +08:00
|
|
|
#pragma endregion // Histogram
|
|
|
|
|
2020-11-06 02:25:21 +08:00
|
|
|
TEST_CASE("/flow/histogram/smoke_test") {
|
2020-11-06 02:26:51 +08:00
|
|
|
{
|
2022-09-20 02:35:58 +08:00
|
|
|
Reference<Histogram> h = Histogram::getHistogram("smoke_test"_sr, "counts"_sr, Histogram::Unit::bytes);
|
2020-11-06 02:25:21 +08:00
|
|
|
|
2020-11-06 02:26:51 +08:00
|
|
|
h->sample(0);
|
|
|
|
ASSERT(h->buckets[0] == 1);
|
|
|
|
h->sample(1);
|
|
|
|
ASSERT(h->buckets[0] == 2);
|
2020-11-06 02:25:21 +08:00
|
|
|
|
2020-11-06 02:26:51 +08:00
|
|
|
h->sample(2);
|
|
|
|
ASSERT(h->buckets[1] == 1);
|
2020-11-06 02:25:21 +08:00
|
|
|
|
2020-11-06 02:26:51 +08:00
|
|
|
GetHistogramRegistry().logReport();
|
2020-11-06 02:25:21 +08:00
|
|
|
|
2020-11-06 02:26:51 +08:00
|
|
|
ASSERT(h->buckets[0] == 0);
|
|
|
|
h->sample(0);
|
|
|
|
ASSERT(h->buckets[0] == 1);
|
2022-09-20 02:35:58 +08:00
|
|
|
h = Histogram::getHistogram("smoke_test"_sr, "counts2"_sr, Histogram::Unit::bytes);
|
2020-11-06 02:25:21 +08:00
|
|
|
|
2020-11-06 02:26:51 +08:00
|
|
|
// confirm that old h was deallocated.
|
2022-09-20 02:35:58 +08:00
|
|
|
h = Histogram::getHistogram("smoke_test"_sr, "counts"_sr, Histogram::Unit::bytes);
|
2020-11-06 02:26:51 +08:00
|
|
|
ASSERT(h->buckets[0] == 0);
|
2020-11-06 02:25:21 +08:00
|
|
|
|
2022-09-20 02:35:58 +08:00
|
|
|
h = Histogram::getHistogram("smoke_test"_sr, "times"_sr, Histogram::Unit::microseconds);
|
2020-11-06 02:25:21 +08:00
|
|
|
|
2020-11-06 02:26:51 +08:00
|
|
|
h->sampleSeconds(0.000000);
|
|
|
|
h->sampleSeconds(0.0000019);
|
|
|
|
ASSERT(h->buckets[0] == 2);
|
|
|
|
h->sampleSeconds(0.0000021);
|
|
|
|
ASSERT(h->buckets[1] == 1);
|
|
|
|
h->sampleSeconds(0.000015);
|
|
|
|
ASSERT(h->buckets[3] == 1);
|
2020-11-06 02:25:21 +08:00
|
|
|
|
2020-11-06 02:26:51 +08:00
|
|
|
h->sampleSeconds(4400.0);
|
|
|
|
ASSERT(h->buckets[31] == 1);
|
2020-11-06 02:25:21 +08:00
|
|
|
|
2020-11-06 02:26:51 +08:00
|
|
|
GetHistogramRegistry().logReport();
|
|
|
|
}
|
2020-11-06 02:25:21 +08:00
|
|
|
|
2020-11-06 02:26:51 +08:00
|
|
|
// h has been deallocated. Does this crash?
|
|
|
|
GetHistogramRegistry().logReport();
|
|
|
|
|
|
|
|
return Void();
|
2020-11-13 07:38:51 +08:00
|
|
|
}
|