11#include " allocation_tag_profiler.h"
22
3+ #ifdef __x86_64__
34#include < yt/yt/core/concurrency/periodic_executor.h>
5+
6+ #include < yt/yt/core/ypath/token.h>
7+
48#include < yt/yt/library/ytprof/heap_profiler.h>
9+ #endif
510
611namespace NYT ::NYTProf {
712
13+ #ifdef __x86_64__
14+
815using namespace NProfiling ;
916using namespace NConcurrency ;
1017
1118// //////////////////////////////////////////////////////////////////////////////
1219
13- THeapUsageProfiler::THeapUsageProfiler (
14- std::vector<TString> tags,
15- IInvokerPtr invoker,
16- std::optional<TDuration> updatePeriod,
17- std::optional<i64 > samplingRate,
18- NProfiling::TProfiler profiler)
19- : Profiler_(std::move(profiler))
20- , TagTypes_(std::move(tags))
21- , UpdateExecutor_(New<TPeriodicExecutor>(
22- std::move (invoker),
23- BIND(&THeapUsageProfiler::UpdateGauges, MakeWeak(this )),
24- std::move(updatePeriod)))
20+ class TAllocationTagProfiler
21+ : public IAllocationTagProfiler
2522{
26- if (samplingRate) {
27- tcmalloc::MallocExtension::SetProfileSamplingRate (*samplingRate);
23+ public:
24+ TAllocationTagProfiler (
25+ std::vector<TString> tagNames,
26+ IInvokerPtr invoker,
27+ std::optional<TDuration> updatePeriod,
28+ std::optional<i64 > samplingRate,
29+ NProfiling::TProfiler profiler)
30+ : Profiler_(std::move(profiler))
31+ , TagNames_(std::move(tagNames))
32+ , UpdateExecutor_(New<TPeriodicExecutor>(
33+ std::move (invoker),
34+ BIND(&TAllocationTagProfiler::UpdateGauges, MakeWeak(this )),
35+ std::move(updatePeriod)))
36+ {
37+ if (samplingRate) {
38+ tcmalloc::MallocExtension::SetProfileSamplingRate (*samplingRate);
39+ }
40+
41+ UpdateExecutor_->Start ();
2842 }
2943
30- UpdateExecutor_->Start ();
31- }
44+ private:
45+ const NProfiling::TProfiler Profiler_;
46+ const std::vector<TString> TagNames_;
3247
33- void THeapUsageProfiler::UpdateGauges ()
34- {
35- const auto memorySnapshot = GetMemoryUsageSnapshot ();
36- YT_VERIFY (memorySnapshot);
37-
38- for (const auto & tagType : TagTypes_) {
39- auto & heapUsageMap = HeapUsageByType_.emplace (tagType, THashMap<TString, TGauge>{}).first ->second ;
40- const auto & snapshotSlice = memorySnapshot->GetUsage (tagType);
41-
42- for (auto &[tag, gauge] : heapUsageMap) {
43- if (const auto & iter = snapshotSlice.find (tag)) {
44- gauge.Update (iter->second );
45- } else {
46- gauge.Update (0.0 );
47- }
48- }
48+ const NConcurrency::TPeriodicExecutorPtr UpdateExecutor_;
49+
50+ THashMap<TString, THashMap<TString, NProfiling::TGauge>> HeapUsageByType_;
51+
52+ void UpdateGauges ()
53+ {
54+ auto memorySnapshot = GetMemoryUsageSnapshot ();
55+ YT_VERIFY (memorySnapshot);
4956
50- for (const auto & [tag, usage] : snapshotSlice) {
51- auto gauge = heapUsageMap.find (tag);
57+ for (const auto & tagName : TagNames_) {
58+ auto & heapUsageMap = HeapUsageByType_.emplace (tagName, THashMap<TString, TGauge>{}).first ->second ;
59+ const auto & snapshotSlice = memorySnapshot->GetUsage (tagName);
5260
53- if (gauge.IsEnd ()) {
54- gauge = heapUsageMap.emplace (tag, Profiler_
55- .WithTag (tagType, tag)
56- .Gauge (tagType))
57- .first ;
58- gauge->second .Update (usage);
61+ for (auto & [tagValue, gauge] : heapUsageMap) {
62+ if (auto it = snapshotSlice.find (tagValue)) {
63+ gauge.Update (it->second );
64+ } else {
65+ gauge.Update (0.0 );
66+ }
67+ }
68+
69+ for (const auto & [tagValue, usage] : snapshotSlice) {
70+ auto it = heapUsageMap.find (tagValue);
71+ if (it == heapUsageMap.end ()) {
72+ it = heapUsageMap.emplace (tagValue, Profiler_
73+ .WithTag (tagName, tagValue)
74+ .Gauge (Format (" /%v" , NYPath::ToYPathLiteral (tagName))))
75+ .first ;
76+ it->second .Update (usage);
77+ }
5978 }
6079 }
6180 }
62- }
81+ };
6382
64- // /////////////////////////////////////////////////////////////////
83+ # else
6584
66- THeapUsageProfilerPtr CreateHeapProfilerWithTags (
67- std::vector<TString>&& tags,
85+ class TAllocationTagProfiler
86+ : public IAllocationTagProfiler
87+ {
88+ public:
89+ TAllocationTagProfiler (auto &&... /* args*/ )
90+ { }
91+ };
92+
93+ #endif
94+
95+ // //////////////////////////////////////////////////////////////////////////////
96+
97+ IAllocationTagProfilerPtr CreateAllocationTagProfiler (
98+ std::vector<TString> tagNames,
6899 IInvokerPtr invoker,
69100 std::optional<TDuration> updatePeriod,
70101 std::optional<i64 > samplingRate,
71102 NYT::NProfiling::TProfiler profiler)
72103{
73- return New<THeapUsageProfiler >(
74- std::move (tags ),
104+ return New<TAllocationTagProfiler >(
105+ std::move (tagNames ),
75106 std::move (invoker),
76107 std::move (updatePeriod),
77108 std::move (samplingRate),
@@ -81,4 +112,3 @@ THeapUsageProfilerPtr CreateHeapProfilerWithTags(
81112// //////////////////////////////////////////////////////////////////////////////
82113
83114} // namespace NYT::NYTProf
84-
0 commit comments