Profiler.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //
  2. // Copyright (c) 2017 the Atomic 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 "../Container/Str.h"
  24. #include "../Core/Thread.h"
  25. #include "../Core/Timer.h"
  26. #if ATOMIC_PROFILING
  27. # include <easy/profiler.h>
  28. #else
  29. namespace profiler { class BaseBlockDescriptor {}; };
  30. #endif
  31. namespace Atomic
  32. {
  33. static const int PROFILER_DEFAULT_PORT = 28077;
  34. static const uint32_t PROFILER_COLOR_DEFAULT = 0xffffecb3;
  35. static const uint32_t PROFILER_COLOR_EVENTS = 0xffff9800;
  36. static const uint32_t PROFILER_COLOR_RESOURCES = 0xff00bcd4;
  37. // Copied from easy_profiler
  38. enum ProfilerBlockStatus
  39. {
  40. OFF = 0,
  41. ON = 1,
  42. FORCE_ON = ON | 2,
  43. OFF_RECURSIVE = 4,
  44. ON_WITHOUT_CHILDREN = ON | OFF_RECURSIVE,
  45. FORCE_ON_WITHOUT_CHILDREN = FORCE_ON | OFF_RECURSIVE,
  46. };
  47. /// Hierarchical performance profiler subsystem.
  48. class ATOMIC_API Profiler : public Object
  49. {
  50. ATOMIC_OBJECT(Profiler, Object);
  51. public:
  52. /// Construct.
  53. Profiler(Context* context);
  54. /// Destruct.
  55. virtual ~Profiler();
  56. /// Enables or disables profiler.
  57. void SetEnabled(bool enabled);
  58. /// Returns true if profiler is enabled, false otherwise.
  59. bool GetEnabled() const;
  60. /// Enables or disables event profiling.
  61. void SetEventProfilingEnabled(bool enabled);
  62. /// Returns true if event profiling is enabled, false otherwise.
  63. bool GetEventProfilingEnabled() const;
  64. /// Starts listening for incoming profiler tool connections.
  65. void StartListen(unsigned short port=PROFILER_DEFAULT_PORT);
  66. /// Stops listening for incoming profiler tool connections.
  67. void StopListen();
  68. /// Returns true if profiler is currently listening for incoming connections.
  69. bool GetListening() const;
  70. /// Enables or disables event tracing. This is windows-specific, does nothing on other OS.
  71. void SetEventTracingEnabled(bool enable);
  72. /// Returns true if event tracing is enabled, false otherwise.
  73. bool GetEventTracingEnabled();
  74. /// Enables or disables low priority event tracing. This is windows-specific, does nothing on other OS.
  75. void SetLowPriorityEventTracing(bool isLowPriority);
  76. /// Returns true if low priority event tracing is enabled, false otherwise.
  77. bool GetLowPriorityEventTracing();
  78. /// Save profiler data to a file.
  79. void SaveProfilerData(const String& filePath);
  80. /// Begin non-scoped profiled block. Block has to be terminated with call to EndBlock(). This is slow and is for
  81. /// integration with scripting lnaguages. Use ATOMIC_PROFILE* macros when writing c++ code instead.
  82. void BeginBlock(const char* name, const char* file, int line, unsigned int argb=PROFILER_COLOR_DEFAULT,
  83. unsigned char status=ProfilerBlockStatus::ON);
  84. /// End block started with BeginBlock().
  85. void EndBlock();
  86. private:
  87. bool enableEventProfiling_ = true;
  88. HashMap<unsigned, ::profiler::BaseBlockDescriptor*> blockDescriptorCache_;
  89. };
  90. #if ATOMIC_PROFILING
  91. # define ATOMIC_PROFILE(name, ...) EASY_BLOCK(#name, __VA_ARGS__)
  92. # define ATOMIC_PROFILE_SCOPED(name, ...) EASY_BLOCK(name, __VA_ARGS__)
  93. # define ATOMIC_PROFILE_NONSCOPED(name, ...) EASY_NONSCOPED_BLOCK(name, __VA_ARGS__)
  94. # define ATOMIC_PROFILE_END(...) EASY_END_BLOCK
  95. # define ATOMIC_PROFILE_THREAD(name) EASY_THREAD(name)
  96. #else
  97. # define ATOMIC_PROFILE(name, ...)
  98. # define ATOMIC_PROFILE_NONSCOPED(name, ...)
  99. # define ATOMIC_PROFILE_SCOPED(name, ...)
  100. # define ATOMIC_PROFILE_END(...)
  101. # define ATOMIC_PROFILE_THREAD(name)
  102. #endif
  103. }