Skip to content

Commit ed4f8e1

Browse files
committed
Added profile memory allocations option
1 parent a038353 commit ed4f8e1

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

ydb/tests/tools/kqprun/kqprun.cpp

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <util/system/env.h>
1111

1212
#include <ydb/core/base/backtrace.h>
13+
#include <ydb/core/blob_depot/mon_main.h>
1314

1415
#include <ydb/library/aclib/aclib.h>
1516
#include <ydb/library/yaml_config/yaml_config.h>
@@ -21,6 +22,10 @@
2122
#include <yt/yql/providers/yt/gateway/file/yql_yt_file_comp_nodes.h>
2223
#include <yt/yql/providers/yt/lib/yt_download/yt_download.h>
2324

25+
#ifdef PROFILE_MEMORY_ALLOCATIONS
26+
#include <library/cpp/lfalloc/alloc_profiler/profiler.h>
27+
#endif
28+
2429

2530
namespace NKqpRun {
2631

@@ -248,7 +253,7 @@ struct TExecutionOptions {
248253
ythrow yexception() << "Invalid trace opt id " << *traceOptId << ", it should be less than number of scipt queries " << numberScripts;
249254
}
250255
if (numberScripts == 1) {
251-
Cout << colors.Red() << "Warning: trace opt id is not necessary for single script mode, use -T script" << Endl;
256+
Cout << colors.Red() << "Warning: trace opt id is not necessary for single script mode" << Endl;
252257
}
253258
}
254259
}
@@ -421,6 +426,8 @@ TIntrusivePtr<NKikimr::NMiniKQL::IMutableFunctionRegistry> CreateFunctionRegistr
421426
class TMain : public TMainClassArgs {
422427
inline static const TString YqlToken = GetEnv(YQL_TOKEN_VARIABLE);
423428
inline static std::vector<std::unique_ptr<TFileOutput>> FileHolders;
429+
inline static IOutputStream* ProfileAllocationsOutput = nullptr;
430+
inline static NColorizer::TColors CoutColors = NColorizer::AutoColors(Cout);
424431

425432
TExecutionOptions ExecutionOptions;
426433
TRunnerOptions RunnerOptions;
@@ -477,6 +484,21 @@ class TMain : public TMainClassArgs {
477484
const std::map<TString, TResult> ChoicesMap;
478485
};
479486

487+
#ifdef PROFILE_MEMORY_ALLOCATIONS
488+
public:
489+
static void FinishProfileMemoryAllocations() {
490+
if (ProfileAllocationsOutput) {
491+
NAllocProfiler::StopAllocationSampling(*ProfileAllocationsOutput);
492+
} else {
493+
TString output;
494+
TStringOutput stream(output);
495+
NAllocProfiler::StopAllocationSampling(stream);
496+
497+
Cout << CoutColors.Red() << "Warning: profile memory allocations output is not specified, please use flag `--profile-output` for writing profile info (dump size " << NKikimr::NBlobDepot::FormatByteSize(output.size()) << ")" << CoutColors.Default() << Endl;
498+
}
499+
}
500+
#endif
501+
480502
protected:
481503
void RegisterOptions(NLastGetopt::TOpts& options) override {
482504
options.SetTitle("KqpRun -- tool to execute queries by using kikimr provider (instead of dq provider in DQrun tool)");
@@ -681,6 +703,10 @@ class TMain : public TMainClassArgs {
681703
RunnerOptions.ScriptQueryTimelineFiles.emplace_back(file);
682704
});
683705

706+
options.AddLongOption("profile-output", "File with profile memory allocations output (use '-' to write in stdout)")
707+
.RequiredArgument("file")
708+
.StoreMappedResultT<TString>(&ProfileAllocationsOutput, &GetDefaultOutput);
709+
684710
// Pipeline settings
685711

686712
TChoices<TExecutionOptions::EExecutionCase> executionCase({
@@ -883,7 +909,26 @@ class TMain : public TMainClassArgs {
883909
ythrow yexception() << "Tables mapping is not supported without emulate YT mode";
884910
}
885911

912+
#ifdef PROFILE_MEMORY_ALLOCATIONS
913+
if (RunnerOptions.YdbSettings.VerboseLevel >= 1) {
914+
Cout << CoutColors.Cyan() << "Starting profile memory allocations" << CoutColors.Default() << Endl;
915+
}
916+
NAllocProfiler::StartAllocationSampling(true);
917+
#else
918+
if (ProfileAllocationsOutput) {
919+
ythrow yexception() << "Profile memory allocations disabled, please rebuild kqprun with flag `-D PROFILE_MEMORY_ALLOCATIONS`";
920+
}
921+
#endif
922+
886923
RunScript(ExecutionOptions, RunnerOptions);
924+
925+
#ifdef PROFILE_MEMORY_ALLOCATIONS
926+
if (RunnerOptions.YdbSettings.VerboseLevel >= 1) {
927+
Cout << CoutColors.Cyan() << "Finishing profile memory allocations" << CoutColors.Default() << Endl;
928+
}
929+
FinishProfileMemoryAllocations();
930+
#endif
931+
887932
return 0;
888933
}
889934
};
@@ -910,6 +955,17 @@ void SegmentationFaultHandler(int) {
910955
abort();
911956
}
912957

958+
#ifdef PROFILE_MEMORY_ALLOCATIONS
959+
void InterruptHandler(int) {
960+
NColorizer::TColors colors = NColorizer::AutoColors(Cerr);
961+
962+
Cout << colors.Red() << "Execution interrupted, finishing profile memory allocations..." << colors.Default() << Endl;
963+
TMain::FinishProfileMemoryAllocations();
964+
965+
abort();
966+
}
967+
#endif
968+
913969
} // anonymous namespace
914970

915971
} // namespace NKqpRun
@@ -918,6 +974,10 @@ int main(int argc, const char* argv[]) {
918974
std::set_terminate(NKqpRun::KqprunTerminateHandler);
919975
signal(SIGSEGV, &NKqpRun::SegmentationFaultHandler);
920976

977+
#ifdef PROFILE_MEMORY_ALLOCATIONS
978+
signal(SIGINT, &NKqpRun::InterruptHandler);
979+
#endif
980+
921981
try {
922982
NKqpRun::TMain().Run(argc, argv);
923983
} catch (...) {

ydb/tests/tools/kqprun/ya.make

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
PROGRAM(kqprun)
22

3+
IF (PROFILE_MEMORY_ALLOCATIONS)
4+
MESSAGE("Enabled profile memory allocations")
5+
ALLOCATOR(LF_DBG)
6+
CFLAGS(-D PROFILE_MEMORY_ALLOCATIONS)
7+
ENDIF()
8+
39
SRCS(
410
kqprun.cpp
511
)

0 commit comments

Comments
 (0)