Profiler.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. #include "../Precompiled.h"
  23. #include "../Core/Profiler.h"
  24. #include "../Core/StringUtils.h"
  25. namespace Atomic
  26. {
  27. Profiler::Profiler(Context* context)
  28. : Object(context)
  29. {
  30. SetEnabled(true);
  31. #if !ATOMIC_PROFILING
  32. enableEventProfiling_ = false;
  33. #endif
  34. }
  35. Profiler::~Profiler()
  36. {
  37. }
  38. void Profiler::SetEnabled(bool enabled)
  39. {
  40. #if ATOMIC_PROFILING
  41. ::profiler::setEnabled(enabled);
  42. #endif
  43. }
  44. bool Profiler::GetEnabled() const
  45. {
  46. #if ATOMIC_PROFILING
  47. return ::profiler::isEnabled();
  48. #else
  49. return false;
  50. #endif
  51. }
  52. void Profiler::StartListen(unsigned short port)
  53. {
  54. #if ATOMIC_PROFILING
  55. ::profiler::startListen(port);
  56. #endif
  57. }
  58. void Profiler::StopListen()
  59. {
  60. #if ATOMIC_PROFILING
  61. ::profiler::stopListen();
  62. #endif
  63. }
  64. bool Profiler::GetListening() const
  65. {
  66. #if ATOMIC_PROFILING
  67. return ::profiler::isListening();
  68. #else
  69. return false;
  70. #endif
  71. }
  72. void Profiler::SetEventTracingEnabled(bool enable)
  73. {
  74. #if ATOMIC_PROFILING
  75. ::profiler::setEventTracingEnabled(enable);
  76. #endif
  77. }
  78. bool Profiler::GetEventTracingEnabled()
  79. {
  80. #if ATOMIC_PROFILING
  81. return ::profiler::isEventTracingEnabled();
  82. #else
  83. return false;
  84. #endif
  85. }
  86. void Profiler::SetLowPriorityEventTracing(bool isLowPriority)
  87. {
  88. #if ATOMIC_PROFILING
  89. ::profiler::setLowPriorityEventTracing(isLowPriority);
  90. #endif
  91. }
  92. bool Profiler::GetLowPriorityEventTracing()
  93. {
  94. #if ATOMIC_PROFILING
  95. return ::profiler::isLowPriorityEventTracing();
  96. #else
  97. return false;
  98. #endif
  99. }
  100. void Profiler::SaveProfilerData(const String& filePath)
  101. {
  102. #if ATOMIC_PROFILING
  103. ::profiler::dumpBlocksToFile(filePath.CString());
  104. #endif
  105. }
  106. void Profiler::SetEventProfilingEnabled(bool enabled)
  107. {
  108. #if ATOMIC_PROFILING
  109. enableEventProfiling_ = enabled;
  110. #endif
  111. }
  112. bool Profiler::GetEventProfilingEnabled() const
  113. {
  114. return enableEventProfiling_;
  115. }
  116. void Profiler::BeginBlock(const char* name, const char* file, int line, unsigned int argb, unsigned char status)
  117. {
  118. #if ATOMIC_PROFILING
  119. // Line used as starting hash value for efficiency.
  120. // This is likely to not play well with hot code reload.
  121. unsigned hash = StringHash::Calculate(file, (unsigned)line);
  122. HashMap<unsigned, ::profiler::BaseBlockDescriptor*>::Iterator it = blockDescriptorCache_.Find(hash);
  123. const ::profiler::BaseBlockDescriptor* desc = 0;
  124. if (it == blockDescriptorCache_.End())
  125. {
  126. String uniqueName = ToString("%s:%d", file, line);
  127. desc = ::profiler::registerDescription((::profiler::EasyBlockStatus)status, uniqueName.CString(), name, file,
  128. line, ::profiler::BLOCK_TYPE_BLOCK, argb, true);
  129. }
  130. else
  131. desc = it->second_;
  132. ::profiler::beginNonScopedBlock(desc, name);
  133. #endif
  134. }
  135. void Profiler::EndBlock()
  136. {
  137. #if ATOMIC_PROFILING
  138. ::profiler::endBlock();
  139. #endif
  140. }
  141. }