aboutsummaryrefslogtreecommitdiffstats
path: root/yt/cpp/mapreduce/client/job_profiler.cpp
blob: 330674f87f56d0f9220529494b6934c3e5ec5e2b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "job_profiler.h"

#include <yt/yt/library/ytprof/cpu_profiler.h>
#include <yt/yt/library/ytprof/external_pprof.h>
#include <yt/yt/library/ytprof/heap_profiler.h>
#include <yt/yt/library/ytprof/profile.h>
#include <yt/yt/library/ytprof/symbolize.h>

#include <yt/cpp/mapreduce/interface/logging/logger.h>
#include <yt/cpp/mapreduce/interface/logging/yt_log.h>

#include <library/cpp/yson/node/node_io.h>

#include <contrib/libs/tcmalloc/tcmalloc/malloc_extension.h>

#include <util/system/env.h>
#include <util/system/file.h>
#include <util/system/shellcommand.h>

namespace NYT {

using namespace NYTProf;

////////////////////////////////////////////////////////////////////////////////

static void RunSubprocess(const std::vector<TString>& cmd)
{
    auto command = cmd[0];
    auto args = TList<TString>(cmd.begin() + 1, cmd.end());

    TShellCommand(command, args)
        .Run()
        .Wait();
}

////////////////////////////////////////////////////////////////////////////////

class TJobProfiler
    : public IJobProfiler
{
public:
    TJobProfiler()
    {
        try {
            InitializeProfiler();
        } catch (const std::exception& ex) {
            YT_LOG_ERROR("Failed to initialize job profiler: %v",
                ex.what());
        }
    }

    void Start() override
    {
        if (CpuProfiler_) {
            CpuProfiler_->Start();
        }
    }

    void Stop() override
    {
        if (CpuProfiler_) {
            CpuProfiler_->Stop();

            auto profile = CpuProfiler_->ReadProfile();
            SymbolizeAndWriteProfile(&profile);
        }

        if (MemoryProfilingToken_) {
            auto profile = ConvertAllocationProfile(std::move(*MemoryProfilingToken_).Stop());
            SymbolizeAndWriteProfile(&profile);
        }

        if (ProfilePeakMemoryUsage_) {
            auto profile = ReadHeapProfile(tcmalloc::ProfileType::kPeakHeap);
            SymbolizeAndWriteProfile(&profile);
        }
    }

private:
    std::unique_ptr<TCpuProfiler> CpuProfiler_;

    std::optional<tcmalloc::MallocExtension::AllocationProfilingToken> MemoryProfilingToken_;

    bool ProfilePeakMemoryUsage_ = false;

    bool RunExternalSymbolizer_ = false;

    void InitializeProfiler()
    {
        auto profilerSpecYson = GetEnv("YT_JOB_PROFILER_SPEC");
        if (!profilerSpecYson) {
            return;
        }

        auto profilerSpec = NodeFromYsonString(profilerSpecYson);
        if (profilerSpec["type"] == "cpu") {
            auto samplingFrequency = profilerSpec["sampling_frequency"].AsInt64();
            CpuProfiler_ = std::make_unique<TCpuProfiler>(TCpuProfilerOptions{
                .SamplingFrequency = static_cast<int>(samplingFrequency),
            });
        } else if (profilerSpec["type"] == "memory") {
            MemoryProfilingToken_ = tcmalloc::MallocExtension::StartAllocationProfiling();
        } else if (profilerSpec["type"] == "peak_memory") {
            ProfilePeakMemoryUsage_ = true;
        }

        if (profilerSpec["run_external_symbolizer"] == true) {
            RunExternalSymbolizer_ = true;
        }
    }

    void SymbolizeAndWriteProfile(NYTProf::NProto::Profile* profile)
    {
        Symbolize(profile, /*filesOnly*/ true);
        AddBuildInfo(profile, TBuildInfo::GetDefault());

        if (RunExternalSymbolizer_) {
            SymbolizeByExternalPProf(profile, TSymbolizationOptions{
                .RunTool = RunSubprocess,
            });
        }

        auto serializedProfile = SerializeProfile(*profile);

        constexpr int ProfileFileDescriptor = 8;
        TFile profileFile(ProfileFileDescriptor);
        profileFile.Write(serializedProfile.data(), serializedProfile.size());
        profileFile.FlushData();
    }
};

////////////////////////////////////////////////////////////////////////////////

std::unique_ptr<IJobProfiler> CreateJobProfiler()
{
    return std::make_unique<TJobProfiler>();
}

////////////////////////////////////////////////////////////////////////////////

} // namespace NYT