profile.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include "profile.h"
  2. #include "log.h"
  3. #include <util/stream/format.h>
  4. #define YQL_PERF_LOG(level, file, line) YQL_LOG_IMPL( \
  5. ::NYql::NLog::YqlLogger(), ::NYql::NLog::EComponent::Perf, level, \
  6. ::NYql::NLog::TContextPreprocessor, file, line)
  7. namespace NYql {
  8. namespace NLog {
  9. TProfilingScope::~TProfilingScope() {
  10. if (Name_ == nullptr) {
  11. return;
  12. }
  13. try {
  14. double elapsed = static_cast<double>(::MicroSeconds() - StartedAt_);
  15. TStringBuf unit("us");
  16. if (elapsed > 1000000) {
  17. elapsed /= 1000000;
  18. unit = TStringBuf("s");
  19. } else if (elapsed > 1000) {
  20. elapsed /= 1000;
  21. unit = TStringBuf("ms");
  22. }
  23. auto doLog = [&]() {
  24. YQL_PERF_LOG(Level_, File_, Line_)
  25. << TStringBuf("Execution of [") << Name_
  26. << TStringBuf("] took ") << Prec(elapsed, 3) << unit;
  27. };
  28. if (!LogCtxPath_.first.empty() || !LogCtxPath_.second.empty()) {
  29. YQL_LOG_CTX_ROOT_SESSION_SCOPE(LogCtxPath_);
  30. doLog();
  31. } else {
  32. doLog();
  33. }
  34. } catch (const std::bad_alloc&) {
  35. // no memory, we will not write anything
  36. Y_ABORT("No memory");
  37. }
  38. }
  39. } // namspace NLog
  40. } // namspace NYql