perf.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #pragma once
  2. #include <util/system/types.h>
  3. #include <util/thread/singleton.h>
  4. namespace NLWTrace {
  5. struct TProbe;
  6. class TCpuTracker {
  7. private:
  8. const ui64 MinReportPeriod;
  9. // State
  10. bool Reporting = false;
  11. ui64 LastTs = 0;
  12. ui64 LastReportTs = 0;
  13. // Statistics
  14. ui64 MaxCycles;
  15. const TProbe* MaxProbe;
  16. ui64 MinCycles;
  17. ui64 ProbeCycles;
  18. ui64 Count;
  19. public:
  20. TCpuTracker();
  21. void Enter();
  22. void Exit(const TProbe* Probe);
  23. static TCpuTracker* TlsInstance() {
  24. struct TCpuTrackerkHolder {
  25. TCpuTracker Tracker;
  26. };
  27. return &FastTlsSingletonWithPriority<TCpuTrackerkHolder, 4>()->Tracker;
  28. }
  29. private:
  30. void AddStats(const TProbe* probe, ui64 cycles);
  31. void ResetStats();
  32. void Report();
  33. void ReportNotReentrant();
  34. static double MilliSeconds(ui64 cycles);
  35. };
  36. class TScopedThreadCpuTracker {
  37. private:
  38. const TProbe* Probe;
  39. TCpuTracker* Tracker;
  40. public:
  41. template <class T>
  42. explicit TScopedThreadCpuTracker(const T& probe)
  43. : Probe(&probe.Probe)
  44. , Tracker(TCpuTracker::TlsInstance())
  45. {
  46. Tracker->Enter();
  47. }
  48. ~TScopedThreadCpuTracker() {
  49. Tracker->Exit(Probe);
  50. }
  51. };
  52. }