profile.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #pragma once
  2. #include "log_level.h"
  3. #include "context.h"
  4. #include <util/system/datetime.h>
  5. #define YQL_PROFILE_SCOPE(level, name) \
  6. ::NYql::NLog::TProfilingScope Y_GENERATE_UNIQUE_ID(ps)( \
  7. name, ::NYql::NLog::ELevel::level, __FILE__, __LINE__)
  8. #define YQL_PROFILE_BLOCK_IMPL(level, name) \
  9. ::NYql::NLog::TProfilingScope( \
  10. name, ::NYql::NLog::ELevel::level, __FILE__, __LINE__)
  11. #define YQL_PROFILE_SCOPE_VAL(level, name) \
  12. TAutoPtr<::NYql::NLog::TProfilingScope>(new ::NYql::NLog::TProfilingScope(\
  13. name, ::NYql::NLog::ELevel::level, __FILE__, __LINE__, \
  14. ::NYql::NLog::CurrentLogContextPath()))
  15. #define YQL_PROFILE_BIND_VAL(future, scopeVal) \
  16. future.Apply([scopeVal](const decltype(future)& f) { \
  17. return f.GetValue(); \
  18. });
  19. #define YQL_PROFILE_BLOCK(level, name) \
  20. if (auto Y_GENERATE_UNIQUE_ID(t) = YQL_PROFILE_SCOPE_VAL(level, name)) { \
  21. goto Y_CAT(YQL_LOG_CTX_LABEL, __LINE__); \
  22. } else Y_CAT(YQL_LOG_CTX_LABEL, __LINE__):
  23. #define YQL_PROFILE_FUNC(level) YQL_PROFILE_SCOPE(level, __FUNCTION__)
  24. #define YQL_PROFILE_FUNCSIG(level) YQL_PROFILE_SCOPE(level, Y_FUNC_SIGNATURE)
  25. #define YQL_PROFILE_FUNC_VAL(level) YQL_PROFILE_SCOPE_VAL(level, __FUNCTION__)
  26. #define YQL_PROFILE_FUNCSIG_VAL(level) YQL_PROFILE_SCOPE_VAL(level, Y_FUNC_SIGNATURE)
  27. namespace NYql {
  28. namespace NLog {
  29. /**
  30. * @brief Adds elapsed execution time to log when goes outside of scope.
  31. */
  32. class TProfilingScope {
  33. public:
  34. TProfilingScope(const char* name, ELevel level, const char* file, int line,
  35. std::pair<TString, TString> logCtxPath = std::make_pair(TString(), TString()))
  36. : Name_(name)
  37. , Level_(level)
  38. , File_(file)
  39. , Line_(line)
  40. , StartedAt_(::MicroSeconds())
  41. , LogCtxPath_(std::move(logCtxPath))
  42. {
  43. }
  44. ~TProfilingScope();
  45. explicit inline operator bool() const noexcept {
  46. return true;
  47. }
  48. private:
  49. const char* Name_;
  50. ELevel Level_;
  51. const char* File_;
  52. int Line_;
  53. ui64 StartedAt_;
  54. std::pair<TString, TString> LogCtxPath_;
  55. };
  56. } // namspace NLog
  57. } // namspace NYql