EventProfiler.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../Core/Profiler.h"
  5. namespace Urho3D
  6. {
  7. /// Event profiling data for one block in the event profiling tree.
  8. /// @nobind
  9. class URHO3D_API EventProfilerBlock : public ProfilerBlock
  10. {
  11. public:
  12. /// Construct with the specified parent block and event ID.
  13. EventProfilerBlock(EventProfilerBlock* parent, StringHash eventID) :
  14. ProfilerBlock(parent, GetEventNameRegister().GetString(eventID).CString()),
  15. eventID_(eventID)
  16. {
  17. }
  18. /// Return child block with the specified event ID.
  19. EventProfilerBlock* GetChild(StringHash eventID)
  20. {
  21. for (Vector<ProfilerBlock*>::Iterator i = children_.Begin(); i != children_.End(); ++i)
  22. {
  23. auto* eventProfilerBlock = static_cast<EventProfilerBlock*>(*i);
  24. if (eventProfilerBlock->eventID_ == eventID)
  25. return eventProfilerBlock;
  26. }
  27. auto* newBlock = new EventProfilerBlock(this, eventID);
  28. children_.Push(newBlock);
  29. return newBlock;
  30. }
  31. /// Event ID.
  32. StringHash eventID_;
  33. };
  34. /// Hierarchical performance event profiler subsystem.
  35. class URHO3D_API EventProfiler : public Profiler
  36. {
  37. URHO3D_OBJECT(EventProfiler, Profiler);
  38. public:
  39. /// Construct.
  40. explicit EventProfiler(Context* context);
  41. /// Activate the event profiler to collect information. This incurs slight performance hit on each SendEvent. By default inactive.
  42. static void SetActive(bool newActive) { active = newActive; }
  43. /// Return true if active.
  44. static bool IsActive() { return active; }
  45. /// Begin timing a profiling block based on an event ID.
  46. void BeginBlock(StringHash eventID)
  47. {
  48. // Profiler supports only the main thread currently
  49. if (!Thread::IsMainThread())
  50. return;
  51. current_ = static_cast<EventProfilerBlock*>(current_)->GetChild(eventID);
  52. current_->Begin();
  53. }
  54. private:
  55. /// Profiler active. Default false.
  56. static bool active;
  57. };
  58. }