sapling/eden/fs/telemetry/RequestMetricsScope.cpp
Katie Mancini e299b71988 Explicitly track current/pending imports -- refactor and expose current metrics
Summary:
As mentioned in D20629833, adding metrics for live
imports in `eden top` gives more transparency to the
imports process and makes identifying import related
issues easier. This is set up to expose metrics for live
imports like those for pending imports in `eden top`.

Similar to D20611728 exposing this via these counters
will log this data. Having this data persisted will allow
tracking the performance of imports, and does the set
up for more pro-active fixing of issues. Further we can
look back to see issues that are no longer occurring, but
still of interest.

This also refactors the registration code so that it requires
no copy pasting to add a new counter. Avoiding copy paste
errors when adding more counters and making it easier to
maintain.

Reviewed By: chadaustin

Differential Revision: D20630813

fbshipit-source-id: 8a7a2a0135c7b7a5cde960b84dcb434c6c99eaeb
2020-04-09 12:35:22 -07:00

71 lines
1.9 KiB
C++

/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This software may be used and distributed according to the terms of the
* GNU General Public License version 2.
*/
#include "RequestMetricsScope.h"
#include "eden/fs/utils/Bug.h"
namespace facebook {
namespace eden {
RequestMetricsScope::RequestMetricsScope(
LockedRequestWatchList* pendingRequestWatches)
: pendingRequestWatches_(pendingRequestWatches) {
folly::stop_watch<> watch;
{
auto startTimes = pendingRequestWatches_->wlock();
requestWatch_ = startTimes->insert(startTimes->end(), watch);
}
}
RequestMetricsScope::~RequestMetricsScope() {
{
auto startTimes = pendingRequestWatches_->wlock();
startTimes->erase(requestWatch_);
}
}
std::string RequestMetricsScope::stringOfRequestMetric(RequestMetric metric) {
switch (metric) {
case RequestMetric::COUNT:
return "count";
case RequestMetric::MAX_DURATION_US:
return "max_duration_us";
}
EDEN_BUG() << "unknown metric " << static_cast<int>(metric);
}
size_t RequestMetricsScope::getMetricFromWatches(
RequestMetric metric,
LockedRequestWatchList& watches) {
switch (metric) {
case COUNT:
return watches.rlock()->size();
case MAX_DURATION_US:
return static_cast<size_t>(
std::chrono::duration_cast<std::chrono::microseconds>(
getMaxDuration(watches))
.count());
}
EDEN_BUG() << "unknown metric " << static_cast<int>(metric);
}
RequestMetricsScope::DefaultRequestDuration RequestMetricsScope::getMaxDuration(
LockedRequestWatchList& watches) {
DefaultRequestDuration maxDurationImport{0};
{
auto lockedWatches = watches.rlock();
for (const auto& watch : *lockedWatches) {
maxDurationImport = std::max(maxDurationImport, watch.elapsed());
}
}
return maxDurationImport;
}
} // namespace eden
} // namespace facebook