No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Profiler.cpp 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #include "il2cpp-config.h"
  2. #include "utils/dynamic_array.h"
  3. #include "vm/Profiler.h"
  4. #if IL2CPP_ENABLE_PROFILER
  5. namespace il2cpp
  6. {
  7. namespace vm
  8. {
  9. struct ProfilerDesc
  10. {
  11. Il2CppProfiler* profiler;
  12. Il2CppProfileFlags events;
  13. Il2CppProfileFunc shutdownCallback;
  14. Il2CppProfileMethodFunc methodEnterCallback;
  15. Il2CppProfileMethodFunc methodLeaveCallback;
  16. Il2CppProfileAllocFunc allocationCallback;
  17. Il2CppProfileGCFunc gcEventCallback;
  18. Il2CppProfileGCResizeFunc gcHeapResizeCallback;
  19. Il2CppProfileFileIOFunc fileioCallback;
  20. Il2CppProfileThreadFunc threadStartCallback;
  21. Il2CppProfileThreadFunc threadEndCallback;
  22. };
  23. typedef il2cpp::utils::dynamic_array<ProfilerDesc*> ProfilersVec;
  24. struct ProfilerContext
  25. {
  26. ProfilersVec m_profilers;
  27. };
  28. ProfilerContext* s_ProfilerContext = nullptr;
  29. Il2CppProfileFlags Profiler::s_profilerEvents;
  30. void Profiler::Install(Il2CppProfiler *prof, Il2CppProfileFunc shutdownCallback)
  31. {
  32. AllocateStaticData();
  33. ProfilerDesc* desc = (ProfilerDesc*)IL2CPP_CALLOC(1, sizeof(ProfilerDesc));
  34. desc->profiler = prof;
  35. desc->shutdownCallback = shutdownCallback;
  36. s_ProfilerContext->m_profilers.push_back(desc);
  37. }
  38. void Profiler::Shutdown()
  39. {
  40. for (ProfilersVec::iterator iter = s_ProfilerContext->m_profilers.begin(); iter != s_ProfilerContext->m_profilers.end(); iter++)
  41. {
  42. if ((*iter)->shutdownCallback)
  43. (*iter)->shutdownCallback((*iter)->profiler);
  44. IL2CPP_FREE((void*)(*iter));
  45. }
  46. s_ProfilerContext->m_profilers.clear();
  47. }
  48. void Profiler::SetEvents(Il2CppProfileFlags events)
  49. {
  50. Il2CppProfileFlags value = IL2CPP_PROFILE_NONE;
  51. if (s_ProfilerContext->m_profilers.size())
  52. s_ProfilerContext->m_profilers.back()->events = events;
  53. for (ProfilersVec::iterator iter = s_ProfilerContext->m_profilers.begin(); iter != s_ProfilerContext->m_profilers.end(); iter++)
  54. value = (Il2CppProfileFlags)(value | (*iter)->events);
  55. s_profilerEvents = value;
  56. }
  57. void Profiler::InstallEnterLeave(Il2CppProfileMethodFunc enter, Il2CppProfileMethodFunc fleave)
  58. {
  59. if (!s_ProfilerContext->m_profilers.size())
  60. return;
  61. s_ProfilerContext->m_profilers.back()->methodEnterCallback = enter;
  62. s_ProfilerContext->m_profilers.back()->methodLeaveCallback = fleave;
  63. }
  64. void Profiler::InstallAllocation(Il2CppProfileAllocFunc callback)
  65. {
  66. if (!s_ProfilerContext->m_profilers.size())
  67. return;
  68. s_ProfilerContext->m_profilers.back()->allocationCallback = callback;
  69. }
  70. void Profiler::InstallGC(Il2CppProfileGCFunc callback, Il2CppProfileGCResizeFunc heap_resize_callback)
  71. {
  72. if (!s_ProfilerContext->m_profilers.size())
  73. return;
  74. s_ProfilerContext->m_profilers.back()->gcEventCallback = callback;
  75. s_ProfilerContext->m_profilers.back()->gcHeapResizeCallback = heap_resize_callback;
  76. }
  77. void Profiler::InstallFileIO(Il2CppProfileFileIOFunc callback)
  78. {
  79. if (!s_ProfilerContext->m_profilers.size())
  80. return;
  81. s_ProfilerContext->m_profilers.back()->fileioCallback = callback;
  82. }
  83. void Profiler::InstallThread(Il2CppProfileThreadFunc start, Il2CppProfileThreadFunc end)
  84. {
  85. if (!s_ProfilerContext->m_profilers.size())
  86. return;
  87. s_ProfilerContext->m_profilers.back()->threadStartCallback = start;
  88. s_ProfilerContext->m_profilers.back()->threadEndCallback = end;
  89. }
  90. void Profiler::Allocation(Il2CppObject *obj, Il2CppClass *klass)
  91. {
  92. for (ProfilersVec::const_iterator iter = s_ProfilerContext->m_profilers.begin(); iter != s_ProfilerContext->m_profilers.end(); iter++)
  93. {
  94. if (((*iter)->events & IL2CPP_PROFILE_ALLOCATIONS) && (*iter)->allocationCallback)
  95. (*iter)->allocationCallback((*iter)->profiler, obj, klass);
  96. }
  97. }
  98. void Profiler::MethodEnter(const MethodInfo *method)
  99. {
  100. for (ProfilersVec::const_iterator iter = s_ProfilerContext->m_profilers.begin(); iter != s_ProfilerContext->m_profilers.end(); iter++)
  101. {
  102. if (((*iter)->events & IL2CPP_PROFILE_ENTER_LEAVE) && (*iter)->methodEnterCallback)
  103. (*iter)->methodEnterCallback((*iter)->profiler, method);
  104. }
  105. }
  106. void Profiler::MethodExit(const MethodInfo *method)
  107. {
  108. for (ProfilersVec::const_iterator iter = s_ProfilerContext->m_profilers.begin(); iter != s_ProfilerContext->m_profilers.end(); iter++)
  109. {
  110. if (((*iter)->events & IL2CPP_PROFILE_ENTER_LEAVE) && (*iter)->methodLeaveCallback)
  111. (*iter)->methodLeaveCallback((*iter)->profiler, method);
  112. }
  113. }
  114. void Profiler::GCEvent(Il2CppGCEvent eventType)
  115. {
  116. for (ProfilersVec::const_iterator iter = s_ProfilerContext->m_profilers.begin(); iter != s_ProfilerContext->m_profilers.end(); iter++)
  117. {
  118. if (((*iter)->events & IL2CPP_PROFILE_GC) && (*iter)->gcEventCallback)
  119. (*iter)->gcEventCallback((*iter)->profiler, eventType, 0);
  120. }
  121. }
  122. void Profiler::GCHeapResize(int64_t newSize)
  123. {
  124. for (ProfilersVec::const_iterator iter = s_ProfilerContext->m_profilers.begin(); iter != s_ProfilerContext->m_profilers.end(); iter++)
  125. {
  126. if (((*iter)->events & IL2CPP_PROFILE_GC) && (*iter)->gcEventCallback)
  127. (*iter)->gcHeapResizeCallback((*iter)->profiler, newSize);
  128. }
  129. }
  130. void Profiler::FileIO(Il2CppProfileFileIOKind kind, int count)
  131. {
  132. for (ProfilersVec::const_iterator iter = s_ProfilerContext->m_profilers.begin(); iter != s_ProfilerContext->m_profilers.end(); iter++)
  133. {
  134. if (((*iter)->events & IL2CPP_PROFILE_FILEIO) && (*iter)->fileioCallback)
  135. (*iter)->fileioCallback((*iter)->profiler, kind, count);
  136. }
  137. }
  138. void Profiler::ThreadStart(unsigned long tid)
  139. {
  140. for (ProfilersVec::const_iterator iter = s_ProfilerContext->m_profilers.begin(); iter != s_ProfilerContext->m_profilers.end(); iter++)
  141. {
  142. if (((*iter)->events & IL2CPP_PROFILE_THREADS) && (*iter)->threadStartCallback)
  143. (*iter)->threadStartCallback((*iter)->profiler, tid);
  144. }
  145. }
  146. void Profiler::ThreadEnd(unsigned long tid)
  147. {
  148. for (ProfilersVec::const_iterator iter = s_ProfilerContext->m_profilers.begin(); iter != s_ProfilerContext->m_profilers.end(); iter++)
  149. {
  150. if (((*iter)->events & IL2CPP_PROFILE_THREADS) && (*iter)->threadEndCallback)
  151. (*iter)->threadEndCallback((*iter)->profiler, tid);
  152. }
  153. }
  154. void Profiler::AllocateStaticData()
  155. {
  156. if (s_ProfilerContext == nullptr)
  157. s_ProfilerContext = new ProfilerContext();
  158. }
  159. void Profiler::FreeStaticData()
  160. {
  161. delete s_ProfilerContext;
  162. s_ProfilerContext = nullptr;
  163. }
  164. } /* namespace vm */
  165. } /* namespace il2cpp */
  166. #endif // IL2CPP_ENABLE_PROFILER