EventProfiler.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Core/Profiler.h"
  24. namespace Urho3D
  25. {
  26. /// Event profiling data for one block in the event profiling tree.
  27. /// @nobind
  28. class URHO3D_API EventProfilerBlock : public ProfilerBlock
  29. {
  30. public:
  31. /// Construct with the specified parent block and event ID.
  32. EventProfilerBlock(EventProfilerBlock* parent, StringHash eventID) :
  33. ProfilerBlock(parent, GetEventNameRegister().GetString(eventID).CString()),
  34. eventID_(eventID)
  35. {
  36. }
  37. /// Return child block with the specified event ID.
  38. EventProfilerBlock* GetChild(StringHash eventID)
  39. {
  40. for (PODVector<ProfilerBlock*>::Iterator i = children_.Begin(); i != children_.End(); ++i)
  41. {
  42. auto* eventProfilerBlock = static_cast<EventProfilerBlock*>(*i);
  43. if (eventProfilerBlock->eventID_ == eventID)
  44. return eventProfilerBlock;
  45. }
  46. auto* newBlock = new EventProfilerBlock(this, eventID);
  47. children_.Push(newBlock);
  48. return newBlock;
  49. }
  50. /// Event ID.
  51. StringHash eventID_;
  52. };
  53. /// Hierarchical performance event profiler subsystem.
  54. class URHO3D_API EventProfiler : public Profiler
  55. {
  56. URHO3D_OBJECT(EventProfiler, Profiler);
  57. public:
  58. /// Construct.
  59. explicit EventProfiler(Context* context);
  60. /// Activate the event profiler to collect information. This incurs slight performance hit on each SendEvent. By default inactive.
  61. static void SetActive(bool newActive) { active = newActive; }
  62. /// Return true if active.
  63. static bool IsActive() { return active; }
  64. /// Begin timing a profiling block based on an event ID.
  65. void BeginBlock(StringHash eventID)
  66. {
  67. // Profiler supports only the main thread currently
  68. if (!Thread::IsMainThread())
  69. return;
  70. current_ = static_cast<EventProfilerBlock*>(current_)->GetChild(eventID);
  71. current_->Begin();
  72. }
  73. private:
  74. /// Profiler active. Default false.
  75. static bool active;
  76. };
  77. }