Skip to content

Commit 1475372

Browse files
committed
Intermediate changes
1 parent 2170493 commit 1475372

File tree

2 files changed

+74
-11
lines changed

2 files changed

+74
-11
lines changed

contrib/libs/tcmalloc/ya.make

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ LICENSE(Apache-2.0)
55
LICENSE_TEXTS(.yandex_meta/licenses.list.txt)
66
ALLOCATOR_IMPL()
77

8-
# https://github.com/google/tcmalloc
98
VERSION(2021-10-04-45c59ccbc062ac96d83710205033c656e490d376)
109

10+
ORIGINAL_SOURCE(https://github.com/google/tcmalloc/archive/45c59ccbc062ac96d83710205033c656e490d376.tar.gz)
11+
1112
SRCS(
1213
# Options
1314
tcmalloc/want_hpaa.cc

yt/yt/library/oom/tcmalloc_memory_limit_handler.cpp

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
#include <yt/yt/core/misc/crash_handler.h>
1010
#include <yt/yt/core/misc/error.h>
1111

12+
#include <yt/yt/core/ytree/yson_struct.h>
13+
14+
#include <yt/yt/core/yson/writer.h>
15+
1216
#include <library/cpp/yt/string/format.h>
1317

1418
#include <library/cpp/yt/memory/atomic_intrusive_ptr.h>
@@ -31,9 +35,9 @@ namespace NYT {
3135

3236
////////////////////////////////////////////////////////////////////////////////
3337

34-
void CollectAndDumpMemoryProfile(const TString& memoryProfilePath)
38+
void CollectAndDumpMemoryProfile(const TString& memoryProfilePath, tcmalloc::ProfileType profileType)
3539
{
36-
auto profile = NYTProf::ReadHeapProfile(tcmalloc::ProfileType::kHeap);
40+
auto profile = NYTProf::ReadHeapProfile(profileType);
3741
SymbolizeByExternalPProf(&profile, NYTProf::TSymbolizationOptions{
3842
.RunTool = [] (const std::vector<TString>& args) {
3943
TShellCommand command{args[0], TList<TString>{args.begin()+1, args.end()}};
@@ -62,6 +66,40 @@ void SetupMemoryProfileTimeout(int timeout)
6266

6367
////////////////////////////////////////////////////////////////////////////////
6468

69+
DECLARE_REFCOUNTED_STRUCT(TOomProfilePaths)
70+
71+
struct TOomProfilePaths
72+
: public NYTree::TYsonStruct
73+
{
74+
TString HeapProfilePath;
75+
TString PeakProfilePath;
76+
77+
REGISTER_YSON_STRUCT(TOomProfilePaths);
78+
79+
static void Register(TRegistrar registrar)
80+
{
81+
registrar.Parameter("heap_profile_path", &TThis::HeapProfilePath)
82+
.Default();
83+
registrar.Parameter("peak_profile_path", &TThis::PeakProfilePath)
84+
.Default();
85+
}
86+
};
87+
88+
DEFINE_REFCOUNTED_TYPE(TOomProfilePaths)
89+
90+
////////////////////////////////////////////////////////////////////////////////
91+
92+
void DumpProfilePaths(const TOomProfilePathsPtr& links, const TString& fileName)
93+
{
94+
TFileOutput output(fileName);
95+
NYson::TYsonWriter writer(&output, NYson::EYsonFormat::Pretty);
96+
Serialize(links, &writer);
97+
writer.Flush();
98+
output.Finish();
99+
}
100+
101+
////////////////////////////////////////////////////////////////////////////////
102+
65103
class TTCMallocLimitHandler
66104
: public TRefCounted
67105
{
@@ -102,7 +140,6 @@ class TTCMallocLimitHandler
102140
std::condition_variable CV_;
103141
std::thread Thread_;
104142

105-
106143
void Handle()
107144
{
108145
std::unique_lock<std::mutex> lock(Mutex_);
@@ -114,19 +151,28 @@ class TTCMallocLimitHandler
114151
return;
115152
}
116153

117-
auto heapDumpPath = GetHeapDumpPath();
154+
auto timestamp = TInstant::Now().FormatLocalTime("%Y%m%dT%H%M%S");
155+
auto profilePaths = New<TOomProfilePaths>();
156+
auto profilePathsFile = GetProfilePaths(timestamp);
157+
profilePaths->HeapProfilePath = GetHeapDumpPath(timestamp);
158+
profilePaths->PeakProfilePath = GetPeakDumpPath(timestamp);
159+
118160
Cerr << "TTCMallocLimitHandler: Fork process to write heap profile: "
119-
<< heapDumpPath
161+
<< profilePaths->HeapProfilePath
162+
<< " peak profile path: " << profilePaths->PeakProfilePath
163+
<< " profiles path file: " << profilePathsFile
120164
<< Endl;
121165

122166
SetupMemoryProfileTimeout(Options_.Timeout.Seconds());
123167
auto childPid = fork();
124168

125169
if (childPid == 0) {
126170
SetupMemoryProfileTimeout(Options_.Timeout.Seconds());
127-
CollectAndDumpMemoryProfile(heapDumpPath);
171+
CollectAndDumpMemoryProfile(profilePaths->HeapProfilePath, tcmalloc::ProfileType::kHeap);
172+
CollectAndDumpMemoryProfile(profilePaths->PeakProfilePath, tcmalloc::ProfileType::kPeakHeap);
173+
DumpProfilePaths(profilePaths, profilePathsFile);
128174

129-
Cerr << "TTCMallocLimitHandler: Heap profile written" << Endl;
175+
Cerr << "TTCMallocLimitHandler: Heap profiles are written" << Endl;
130176
AbortProcess(ToUnderlying(EProcessExitCode::OK));
131177
}
132178

@@ -139,17 +185,33 @@ class TTCMallocLimitHandler
139185
AbortProcess(ToUnderlying(EProcessExitCode::OK));
140186
}
141187

142-
TString GetHeapDumpPath() const
188+
TString GetHeapDumpPath(const TString& timestamp) const
143189
{
144190
return Format(
145191
"%v/heap_%v.pb.gz",
146192
Options_.HeapDumpDirectory,
147-
TInstant::Now().FormatLocalTime("%Y%m%dT%H%M%S"));
193+
timestamp);
194+
}
195+
196+
TString GetPeakDumpPath(const TString& timestamp) const
197+
{
198+
return Format(
199+
"%v/peak_%v.pb.gz",
200+
Options_.HeapDumpDirectory,
201+
timestamp);
202+
}
203+
204+
TString GetProfilePaths(const TString& timestamp) const
205+
{
206+
return Format(
207+
"%v/oom_profile_paths_%v.pb.gz",
208+
Options_.HeapDumpDirectory,
209+
timestamp);
148210
}
149211

150212
void ExecWaitForChild(int pid)
151213
{
152-
Cerr << "TTCMallocLimitHandler: Before waiting for child" << Endl;
214+
Cerr << "TTCMallocLimitHandler: Start waiting for the child" << Endl;
153215

154216
auto command = Format("while [ -e /proc/%v ]; do sleep 1; done;", pid);
155217
execl("/bin/bash", "/bin/bash", "-c", command.c_str(), (void*)nullptr);

0 commit comments

Comments
 (0)