Encapsulate EdenStats

Summary:
EdenStats is currently an alias for ThreadLocal<EdenThreadStats>. I want to split EdenThreadStats into two structs which are allocated independently, but EdenStats's interface makes this impossible.

Refactor EdenStats from an alias to a class and encapsulate the underlying ThreadLocal<EdenThreadStats> member.

This diff should not change behavior.

Reviewed By: simpkins

Differential Revision: D14822272

fbshipit-source-id: 691f4731aa22ecbdcd3535ee0bb0b99c816ffc3d
This commit is contained in:
Matt Glazar 2019-04-14 20:42:26 -07:00 committed by Facebook Github Bot
parent 11195c2b0b
commit 1057039562
6 changed files with 41 additions and 25 deletions

View File

@ -18,8 +18,6 @@
#include "eden/fs/utils/PathFuncs.h"
namespace folly {
template <class T, class Tag, class AccessMode>
class ThreadLocal;
template <class T>
class Future;
} // namespace folly
@ -35,12 +33,10 @@ namespace eden {
class DirList;
class Dispatcher;
class EdenThreadStats;
class EdenStatsTag;
class RequestData;
class EdenStats;
class FileHandle;
class MountPoint;
using EdenStats = folly::ThreadLocal<EdenThreadStats, EdenStatsTag, void>;
class RequestData;
class Dispatcher {
fuse_init_out connInfo_;

View File

@ -64,7 +64,8 @@ void RequestData::finishRequest() {
const auto now = steady_clock::now();
const auto now_since_epoch = duration_cast<seconds>(now.time_since_epoch());
const auto diff = duration_cast<microseconds>(now - startTime_);
stats_->get()->recordLatency(latencyHistogram_, diff, now_since_epoch);
stats_->getStatsForCurrentThread().recordLatency(
latencyHistogram_, diff, now_since_epoch);
latencyHistogram_ = nullptr;
stats_ = nullptr;
}

View File

@ -1193,9 +1193,7 @@ void EdenServer::shutdownSubscribers() {
}
void EdenServer::flushStatsNow() {
for (auto& stats : serverState_->getStats().accessAllThreads()) {
stats.aggregate();
}
serverState_->getStats().aggregate();
}
void EdenServer::reportProcStats() {

View File

@ -709,7 +709,7 @@ HgImporter::ChunkHeader HgImporter::readChunkHeader(
HgImporter::TransactionID HgImporter::sendManifestRequest(
folly::StringPiece revName) {
#if defined(EDEN_HAVE_STATS)
stats_->get()->hgImporterManifest.addValue(1);
stats_->getStatsForCurrentThread().hgImporterManifest.addValue(1);
#endif
auto txnID = nextRequestID_++;
@ -732,7 +732,8 @@ HgImporter::TransactionID HgImporter::sendManifestRequest(
HgImporter::TransactionID HgImporter::sendManifestNodeRequest(
folly::StringPiece revName) {
#if defined(EDEN_HAVE_STATS)
stats_->get()->hgImporterManifestNodeForCommit.addValue(1);
stats_->getStatsForCurrentThread().hgImporterManifestNodeForCommit.addValue(
1);
#endif
auto txnID = nextRequestID_++;
@ -756,7 +757,7 @@ HgImporter::TransactionID HgImporter::sendFileRequest(
RelativePathPiece path,
Hash revHash) {
#if defined(EDEN_HAVE_STATS)
stats_->get()->hgImporterCatFile.addValue(1);
stats_->getStatsForCurrentThread().hgImporterCatFile.addValue(1);
#endif
auto txnID = nextRequestID_++;
@ -782,7 +783,7 @@ HgImporter::TransactionID HgImporter::sendFileRequest(
HgImporter::TransactionID HgImporter::sendPrefetchFilesRequest(
const std::vector<std::pair<RelativePath, Hash>>& files) {
#if defined(EDEN_HAVE_STATS)
stats_->get()->hgImporterPrefetchFiles.addValue(1);
stats_->getStatsForCurrentThread().hgImporterPrefetchFiles.addValue(1);
#endif
auto txnID = nextRequestID_++;
@ -841,7 +842,7 @@ HgImporter::TransactionID HgImporter::sendFetchTreeRequest(
RelativePathPiece path,
Hash pathManifestNode) {
#if defined(EDEN_HAVE_STATS)
stats_->get()->hgImporterFetchTree.addValue(1);
stats_->getStatsForCurrentThread().hgImporterFetchTree.addValue(1);
#endif
auto txnID = nextRequestID_++;

View File

@ -26,6 +26,16 @@ constexpr std::chrono::microseconds kBucketSize{1000};
namespace facebook {
namespace eden {
EdenThreadStats& EdenStats::getStatsForCurrentThread() {
return *threadLocalStats_.get();
}
void EdenStats::aggregate() {
for (auto& stats : threadLocalStats_.accessAllThreads()) {
stats.aggregate();
}
}
EdenThreadStats::EdenThreadStats() {}
EdenThreadStats::Histogram EdenThreadStats::createHistogram(

View File

@ -9,24 +9,34 @@
*/
#pragma once
#include <folly/ThreadLocal.h>
#include "common/stats/ThreadLocalStats.h"
#include "eden/fs/eden-config.h"
namespace folly {
template <class T, class Tag, class AccessMode>
class ThreadLocal;
}
namespace facebook {
namespace eden {
/**
* A tag class for using with folly::ThreadLocal when storing EdenThreadStats.
*/
class EdenStatsTag {};
class EdenThreadStats;
using EdenStats = folly::ThreadLocal<EdenThreadStats, EdenStatsTag, void>;
class EdenStats {
public:
/**
* This function can be called on any thread.
*
* The returned object can be used only on the current thread.
*/
EdenThreadStats& getStatsForCurrentThread();
/**
* This function can be called on any thread.
*/
void aggregate();
private:
class ThreadLocalTag {};
folly::ThreadLocal<EdenThreadStats, ThreadLocalTag, void> threadLocalStats_;
};
/**
* EdenThreadStats contains various thread-local stats structures.