summaryrefslogtreecommitdiffstats
path: root/yql/essentials/utils/log/profile.cpp
blob: 130bb05a4abaaf18a6bdba03b32af6e577b26731 (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
#include "profile.h"
#include "log.h"

#include <util/stream/format.h>


#define YQL_PERF_LOG(level, file, line) YQL_LOG_IMPL( \
    ::NYql::NLog::YqlLogger(), ::NYql::NLog::EComponent::Perf, level, \
    ::NYql::NLog::TContextPreprocessor, file, line)


namespace NYql {
namespace NLog {

TProfilingScope::~TProfilingScope() {
    if (Name_ == nullptr) {
        return;
    }

    try {
        double elapsed = static_cast<double>(::MicroSeconds() - StartedAt_);
        TStringBuf unit("us");
        if (elapsed > 1000000) {
            elapsed /= 1000000;
            unit = TStringBuf("s");
        } else if (elapsed > 1000) {
            elapsed /= 1000;
            unit = TStringBuf("ms");
        }

        auto doLog = [&]() {
            YQL_PERF_LOG(Level_, File_, Line_)
                    << TStringBuf("Execution of [") << Name_
                    << TStringBuf("] took ") << Prec(elapsed, 3) << unit;
        };

        if (!LogCtxPath_.first.empty() || !LogCtxPath_.second.empty()) {
            YQL_LOG_CTX_ROOT_SESSION_SCOPE(LogCtxPath_);
            doLog();
        } else {
            doLog();
        }
    } catch (const std::bad_alloc&) {
        // no memory, we will not write anything
        Y_ABORT("No memory");
    }
}

} // namspace NLog
} // namspace NYql