summaryrefslogtreecommitdiffstats
path: root/library/cpp/actors/util/memory_tracker.cpp
diff options
context:
space:
mode:
authorinnokentii <[email protected]>2022-09-09 13:56:14 +0300
committerinnokentii <[email protected]>2022-09-09 13:56:14 +0300
commit7cd934b41bf8f1487cbb4eb783a1e7e7e0a8cbdc (patch)
tree685d3025cdc4b14befe8b9fd9c1448adadd321f9 /library/cpp/actors/util/memory_tracker.cpp
parent695f38b2d33b4c1962bc3ae11737025406116832 (diff)
Move TSharedData to actors lib
fix MemoryTrack usages move MemoryTrack to utils move TSharedData to actors lib
Diffstat (limited to 'library/cpp/actors/util/memory_tracker.cpp')
-rw-r--r--library/cpp/actors/util/memory_tracker.cpp103
1 files changed, 103 insertions, 0 deletions
diff --git a/library/cpp/actors/util/memory_tracker.cpp b/library/cpp/actors/util/memory_tracker.cpp
new file mode 100644
index 00000000000..8a12452c71c
--- /dev/null
+++ b/library/cpp/actors/util/memory_tracker.cpp
@@ -0,0 +1,103 @@
+#include "memory_tracker.h"
+
+#include <util/generic/xrange.h>
+
+namespace NActors {
+namespace NMemory {
+
+namespace NPrivate {
+
+TMemoryTracker* TMemoryTracker::Instance() {
+ return SingletonWithPriority<TMemoryTracker, 0>();
+}
+
+void TMemoryTracker::Initialize() {
+ GlobalMetrics.resize(Indices.size());
+}
+
+const std::map<TString, size_t>& TMemoryTracker::GetMetricIndices() const {
+ return Indices;
+}
+
+const std::unordered_set<size_t>& TMemoryTracker::GetSensors() const {
+ return Sensors;
+}
+
+TString TMemoryTracker::GetName(size_t index) const {
+ return Names[index];
+}
+
+size_t TMemoryTracker::GetCount() const {
+ return Indices.size();
+}
+
+void TMemoryTracker::GatherMetrics(std::vector<TMetric>& metrics) const {
+ metrics.resize(0);
+ auto count = GetCount();
+
+ if (!count || GlobalMetrics.size() != count) {
+ return;
+ }
+
+ TReadGuard guard(LockThreadInfo);
+
+ metrics.resize(count);
+ for (size_t i : xrange(count)) {
+ metrics[i] += GlobalMetrics[i];
+ }
+
+ for (auto info : ThreadInfo) {
+ auto& localMetrics = info->GetMetrics();
+ if (localMetrics.size() == count) {
+ for (size_t i : xrange(count)) {
+ metrics[i] += localMetrics[i];
+ }
+ }
+ }
+}
+
+size_t TMemoryTracker::RegisterStaticMemoryLabel(const char* name, bool hasSensor) {
+ size_t index = 0;
+ auto found = Indices.find(name);
+ if (found == Indices.end()) {
+ TString str(name);
+ auto next = Names.size();
+ Indices.emplace(str, next);
+ Names.push_back(str);
+ index = next;
+ } else {
+ index = found->second;
+ }
+
+ if (hasSensor) {
+ Sensors.emplace(index);
+ }
+ return index;
+}
+
+void TMemoryTracker::OnCreateThread(TThreadLocalInfo* info) {
+ TWriteGuard guard(LockThreadInfo);
+ ThreadInfo.insert(info);
+}
+
+void TMemoryTracker::OnDestroyThread(TThreadLocalInfo* info) {
+ TWriteGuard guard(LockThreadInfo);
+
+ auto count = GetCount();
+ if (count && GlobalMetrics.size() == count) {
+ const auto& localMetrics = info->GetMetrics();
+ if (localMetrics.size() == count) {
+ for (size_t i : xrange(count)) {
+ GlobalMetrics[i] += localMetrics[i];
+ }
+ }
+ }
+
+ ThreadInfo.erase(info);
+}
+
+}
+
+}
+}
+