Profiler.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. static ProfilersVec s_profilers;
  25. Il2CppProfileFlags Profiler::s_profilerEvents;
  26. void Profiler::Install(Il2CppProfiler *prof, Il2CppProfileFunc shutdownCallback)
  27. {
  28. ProfilerDesc* desc = (ProfilerDesc*)calloc(1, sizeof(ProfilerDesc));
  29. desc->profiler = prof;
  30. desc->shutdownCallback = shutdownCallback;
  31. s_profilers.push_back(desc);
  32. }
  33. void Profiler::SetEvents(Il2CppProfileFlags events)
  34. {
  35. Il2CppProfileFlags value = IL2CPP_PROFILE_NONE;
  36. if (s_profilers.size())
  37. s_profilers.back()->events = events;
  38. for (ProfilersVec::iterator iter = s_profilers.begin(); iter != s_profilers.end(); iter++)
  39. value = (Il2CppProfileFlags)(value | (*iter)->events);
  40. s_profilerEvents = value;
  41. }
  42. void Profiler::InstallEnterLeave(Il2CppProfileMethodFunc enter, Il2CppProfileMethodFunc fleave)
  43. {
  44. if (!s_profilers.size())
  45. return;
  46. s_profilers.back()->methodEnterCallback = enter;
  47. s_profilers.back()->methodLeaveCallback = fleave;
  48. }
  49. void Profiler::InstallAllocation(Il2CppProfileAllocFunc callback)
  50. {
  51. if (!s_profilers.size())
  52. return;
  53. s_profilers.back()->allocationCallback = callback;
  54. }
  55. void Profiler::InstallGC(Il2CppProfileGCFunc callback, Il2CppProfileGCResizeFunc heap_resize_callback)
  56. {
  57. if (!s_profilers.size())
  58. return;
  59. s_profilers.back()->gcEventCallback = callback;
  60. s_profilers.back()->gcHeapResizeCallback = heap_resize_callback;
  61. }
  62. void Profiler::InstallFileIO(Il2CppProfileFileIOFunc callback)
  63. {
  64. if (!s_profilers.size())
  65. return;
  66. s_profilers.back()->fileioCallback = callback;
  67. }
  68. void Profiler::InstallThread(Il2CppProfileThreadFunc start, Il2CppProfileThreadFunc end)
  69. {
  70. if (!s_profilers.size())
  71. return;
  72. s_profilers.back()->threadStartCallback = start;
  73. s_profilers.back()->threadEndCallback = end;
  74. }
  75. void Profiler::Allocation(Il2CppObject *obj, Il2CppClass *klass)
  76. {
  77. for (ProfilersVec::const_iterator iter = s_profilers.begin(); iter != s_profilers.end(); iter++)
  78. {
  79. if (((*iter)->events & IL2CPP_PROFILE_ALLOCATIONS) && (*iter)->allocationCallback)
  80. (*iter)->allocationCallback((*iter)->profiler, obj, klass);
  81. }
  82. }
  83. void Profiler::MethodEnter(const MethodInfo *method)
  84. {
  85. for (ProfilersVec::const_iterator iter = s_profilers.begin(); iter != s_profilers.end(); iter++)
  86. {
  87. if (((*iter)->events & IL2CPP_PROFILE_ENTER_LEAVE) && (*iter)->methodEnterCallback)
  88. (*iter)->methodEnterCallback((*iter)->profiler, method);
  89. }
  90. }
  91. void Profiler::MethodExit(const MethodInfo *method)
  92. {
  93. for (ProfilersVec::const_iterator iter = s_profilers.begin(); iter != s_profilers.end(); iter++)
  94. {
  95. if (((*iter)->events & IL2CPP_PROFILE_ENTER_LEAVE) && (*iter)->methodLeaveCallback)
  96. (*iter)->methodLeaveCallback((*iter)->profiler, method);
  97. }
  98. }
  99. void Profiler::GCEvent(Il2CppGCEvent eventType)
  100. {
  101. for (ProfilersVec::const_iterator iter = s_profilers.begin(); iter != s_profilers.end(); iter++)
  102. {
  103. if (((*iter)->events & IL2CPP_PROFILE_GC) && (*iter)->gcEventCallback)
  104. (*iter)->gcEventCallback((*iter)->profiler, eventType, 0);
  105. }
  106. }
  107. void Profiler::GCHeapResize(int64_t newSize)
  108. {
  109. for (ProfilersVec::const_iterator iter = s_profilers.begin(); iter != s_profilers.end(); iter++)
  110. {
  111. if (((*iter)->events & IL2CPP_PROFILE_GC) && (*iter)->gcEventCallback)
  112. (*iter)->gcHeapResizeCallback((*iter)->profiler, newSize);
  113. }
  114. }
  115. void Profiler::FileIO(Il2CppProfileFileIOKind kind, int count)
  116. {
  117. for (ProfilersVec::const_iterator iter = s_profilers.begin(); iter != s_profilers.end(); iter++)
  118. {
  119. if (((*iter)->events & IL2CPP_PROFILE_FILEIO) && (*iter)->fileioCallback)
  120. (*iter)->fileioCallback((*iter)->profiler, kind, count);
  121. }
  122. }
  123. void Profiler::ThreadStart(unsigned long tid)
  124. {
  125. for (ProfilersVec::const_iterator iter = s_profilers.begin(); iter != s_profilers.end(); iter++)
  126. {
  127. if (((*iter)->events & IL2CPP_PROFILE_THREADS) && (*iter)->threadStartCallback)
  128. (*iter)->threadStartCallback((*iter)->profiler, tid);
  129. }
  130. }
  131. void Profiler::ThreadEnd(unsigned long tid)
  132. {
  133. for (ProfilersVec::const_iterator iter = s_profilers.begin(); iter != s_profilers.end(); iter++)
  134. {
  135. if (((*iter)->events & IL2CPP_PROFILE_THREADS) && (*iter)->threadEndCallback)
  136. (*iter)->threadEndCallback((*iter)->profiler, tid);
  137. }
  138. }
  139. void Profiler::Shutdown()
  140. {
  141. for (ProfilersVec::iterator iter = s_profilers.begin(); iter != s_profilers.end(); iter++)
  142. {
  143. if ((*iter)->shutdownCallback)
  144. (*iter)->shutdownCallback((*iter)->profiler);
  145. }
  146. }
  147. } /* namespace vm */
  148. } /* namespace il2cpp */
  149. #endif // IL2CPP_ENABLE_PROFILER