EventProfiler.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //
  2. // Copyright (c) 2008-2017 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 Atomic
  25. {
  26. /// Event profiling data for one block in the event profiling tree.
  27. class ATOMIC_API EventProfilerBlock : public ProfilerBlock
  28. {
  29. public:
  30. /// Construct with the specified parent block and event ID.
  31. EventProfilerBlock(EventProfilerBlock* parent, StringHash eventID) :
  32. ProfilerBlock(parent, EventNameRegistrar::GetEventName(eventID).CString()),
  33. eventID_(eventID)
  34. {
  35. }
  36. /// Return child block with the specified event ID.
  37. EventProfilerBlock* GetChild(StringHash eventID)
  38. {
  39. for (PODVector<ProfilerBlock*>::Iterator i = children_.Begin(); i != children_.End(); ++i)
  40. {
  41. EventProfilerBlock* eventProfilerBlock = static_cast<EventProfilerBlock*>(*i);
  42. if (eventProfilerBlock->eventID_ == eventID)
  43. return eventProfilerBlock;
  44. }
  45. EventProfilerBlock* newBlock = new EventProfilerBlock(this, eventID);
  46. children_.Push(newBlock);
  47. return newBlock;
  48. }
  49. /// Event ID.
  50. StringHash eventID_;
  51. };
  52. /// Hierarchical performance event profiler subsystem.
  53. class ATOMIC_API EventProfiler : public Profiler
  54. {
  55. ATOMIC_OBJECT(EventProfiler, Profiler);
  56. public:
  57. /// Construct.
  58. EventProfiler(Context* context);
  59. /// Activate the event profiler to collect information. This incurs slight performance hit on each SendEvent. By default inactive.
  60. static void SetActive(bool newActive) { active = newActive; }
  61. /// Return true if active.
  62. static bool IsActive() { return active; }
  63. /// Begin timing a profiling block based on an event ID.
  64. void BeginBlock(StringHash eventID)
  65. {
  66. // Profiler supports only the main thread currently
  67. if (!Thread::IsMainThread())
  68. return;
  69. current_ = static_cast<EventProfilerBlock*>(current_)->GetChild(eventID);
  70. current_->Begin();
  71. }
  72. private:
  73. /// Profiler active. Default false.
  74. static bool active;
  75. };
  76. }