10
10
#include < util/system/env.h>
11
11
12
12
#include < ydb/core/base/backtrace.h>
13
+ #include < ydb/core/blob_depot/mon_main.h>
13
14
14
15
#include < ydb/library/aclib/aclib.h>
15
16
#include < ydb/library/yaml_config/yaml_config.h>
21
22
#include < yt/yql/providers/yt/gateway/file/yql_yt_file_comp_nodes.h>
22
23
#include < yt/yql/providers/yt/lib/yt_download/yt_download.h>
23
24
25
+ #ifdef PROFILE_MEMORY_ALLOCATIONS
26
+ #include < library/cpp/lfalloc/alloc_profiler/profiler.h>
27
+ #endif
28
+
24
29
25
30
namespace NKqpRun {
26
31
@@ -248,7 +253,7 @@ struct TExecutionOptions {
248
253
ythrow yexception () << " Invalid trace opt id " << *traceOptId << " , it should be less than number of scipt queries " << numberScripts;
249
254
}
250
255
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;
252
257
}
253
258
}
254
259
}
@@ -421,6 +426,8 @@ TIntrusivePtr<NKikimr::NMiniKQL::IMutableFunctionRegistry> CreateFunctionRegistr
421
426
class TMain : public TMainClassArgs {
422
427
inline static const TString YqlToken = GetEnv(YQL_TOKEN_VARIABLE);
423
428
inline static std::vector<std::unique_ptr<TFileOutput>> FileHolders;
429
+ inline static IOutputStream* ProfileAllocationsOutput = nullptr ;
430
+ inline static NColorizer::TColors CoutColors = NColorizer::AutoColors(Cout);
424
431
425
432
TExecutionOptions ExecutionOptions;
426
433
TRunnerOptions RunnerOptions;
@@ -477,6 +484,21 @@ class TMain : public TMainClassArgs {
477
484
const std::map<TString, TResult> ChoicesMap;
478
485
};
479
486
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
+
480
502
protected:
481
503
void RegisterOptions (NLastGetopt::TOpts& options) override {
482
504
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 {
681
703
RunnerOptions.ScriptQueryTimelineFiles .emplace_back (file);
682
704
});
683
705
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
+
684
710
// Pipeline settings
685
711
686
712
TChoices<TExecutionOptions::EExecutionCase> executionCase ({
@@ -883,7 +909,26 @@ class TMain : public TMainClassArgs {
883
909
ythrow yexception () << " Tables mapping is not supported without emulate YT mode" ;
884
910
}
885
911
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
+
886
923
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
+
887
932
return 0 ;
888
933
}
889
934
};
@@ -910,6 +955,17 @@ void SegmentationFaultHandler(int) {
910
955
abort ();
911
956
}
912
957
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
+
913
969
} // anonymous namespace
914
970
915
971
} // namespace NKqpRun
@@ -918,6 +974,10 @@ int main(int argc, const char* argv[]) {
918
974
std::set_terminate (NKqpRun::KqprunTerminateHandler);
919
975
signal (SIGSEGV, &NKqpRun::SegmentationFaultHandler);
920
976
977
+ #ifdef PROFILE_MEMORY_ALLOCATIONS
978
+ signal (SIGINT, &NKqpRun::InterruptHandler);
979
+ #endif
980
+
921
981
try {
922
982
NKqpRun::TMain ().Run (argc, argv);
923
983
} catch (...) {
0 commit comments