y_profiling.inc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /**--------------------------------------------------------------------------**\
  2. ====================================
  3. y_profiling - Run profilings easilly
  4. ====================================
  5. Description:
  6. Runs any functions named as profilings when the Profiling_Run function is called.
  7. Legal:
  8. Version: MPL 1.1
  9. The contents of this file are subject to the Mozilla Public License Version
  10. 1.1 (the "License"); you may not use this file except in compliance with
  11. the License. You may obtain a copy of the License at
  12. http://www.mozilla.org/MPL/
  13. Software distributed under the License is distributed on an "AS IS" basis,
  14. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  15. for the specific language governing rights and limitations under the
  16. License.
  17. The Original Code is the YSI profiling include.
  18. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  19. Portions created by the Initial Developer are Copyright (C) 2011
  20. the Initial Developer. All Rights Reserved.
  21. Contributors:
  22. ZeeX, koolk, JoeBullet/Google63, g_aSlice/Slice
  23. Thanks:
  24. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  25. ZeeX - Very productive conversations.
  26. koolk - IsPlayerinAreaEx code.
  27. TheAlpha - Danish translation.
  28. breadfish - German translation.
  29. Fireburn - Dutch translation.
  30. yom - French translation.
  31. 50p - Polish translation.
  32. Zamaroht - Spanish translation.
  33. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes
  34. for me to strive to better.
  35. Pixels^ - Running XScripters where the idea was born.
  36. Matite - Pestering me to release it and using it.
  37. Very special thanks to:
  38. Thiadmer - PAWN, whose limits continue to amaze me!
  39. Kye/Kalcor - SA:MP.
  40. SA:MP Team past, present and future - SA:MP.
  41. Version:
  42. 1.0
  43. Changelog:
  44. 17/12/16:
  45. First Version
  46. \**--------------------------------------------------------------------------**/
  47. #if defined _INC_y_profiling
  48. #endinput
  49. #endif
  50. #define _INC_y_profiling
  51. #include "y_debug"
  52. #if defined YSI_PROFILINGS
  53. #if defined INCLUDE_PROFILINGS
  54. #error Incompatible profiling options (YSI_PROFILINGS + INCLUDE_PROFILINGS)
  55. #endif
  56. #if !defined RUN_PROFILINGS
  57. #define RUN_PROFILINGS
  58. #endif
  59. #endif
  60. #if defined INCLUDE_PROFILINGS
  61. #define RUN_PROFILINGS
  62. #define Debug_PrintP printf
  63. #elseif defined RUN_PROFILINGS
  64. #define _AUTO_RUN_PROFILINGS
  65. #define Debug_PrintP printf
  66. #else
  67. #if _DEBUG > 0 || defined _YSI_SPECIAL_DEBUG
  68. #define RUN_PROFILINGS
  69. #define _AUTO_RUN_PROFILINGS
  70. #define Debug_PrintP printf
  71. #else
  72. #define Debug_PrintP(%0);
  73. #endif
  74. #endif
  75. #if defined LIGHT_PROFILE_REPORT
  76. #define PROFILE_REPORT(%0) H@(%0)
  77. #else
  78. #define PROFILE_REPORT(%0) printf(%0)
  79. #endif
  80. #if defined RUN_PROFILINGS
  81. #define ProfileInit:%0(%1) @yP_();@yP_()
  82. #define Profile:%0(%1) __TZ:@y_P%0(%1);__TZ:@y_P%0(%1)RUN_TIMING(#%0)y_P@%0();static y_P@%0()
  83. #define ProfileClose:%0(%1) @P_y();@P_y()
  84. #define __TZ:%0[%2](%1);%0[%2](%1)RUN_TIMING(%3)%4[%5](%6);%9[%5](%6) %0_%2(%1);%0_%2(%1)RUN_TIMING(%3,%2)%4_%2(%6);static %4_%2(%6)
  85. #else
  86. #define ProfileInit:%0(%1) static stock yP_@()
  87. #define Profile:%1() static stock __TZ:y_P@%1()
  88. #define ProfileClose:%0(%1) static stock P_y@()
  89. #define __TZ:%0[%2](%1) __TZ:%0_%2(%1)
  90. #endif
  91. #define @yP_%0\32; @yP_
  92. #define @y_P%0\32; @y_P
  93. #define @P_y%0\32; @P_y
  94. #define yP_@%0\32; yP_@
  95. #define y_P@%0\32; y_P@
  96. #define P_y@%0\32; P_y@
  97. // These all need to come AFTER the types are defined in case the have profilings.
  98. #include "..\YSI_Internal\y_version"
  99. #include "..\YSI_Storage\y_amx"
  100. #include "..\YSI_Internal\y_shortfunc"
  101. #include "y_debug"
  102. #include "y_utils"
  103. #include "..\YSI_Internal\y_natives"
  104. #include "..\YSI_Coding\y_va"
  105. #define Y_PROFILING_PROFILE_SEARCH _A<@y_P>
  106. #define Y_PROFILING_INIT _C<@yP_>
  107. #define Y_PROFILING_PROFILE _C<@y_P>
  108. #define Y_PROFILING_CLOSE _C<@P_y>
  109. static stock
  110. bool:YSI_g_sInProfile = false;
  111. /**--------------------------------------------------------------------------**\
  112. <summary>Profiling_Run</summary>
  113. <param name="&profilings">Number of profilings run.</param>
  114. <returns>
  115. Wether all profilings were sucessful or not.
  116. </returns>
  117. <remarks>
  118. -
  119. native Profiling_Run(&profilings, &fails, buffer[33] = "");
  120. </remarks>
  121. \**--------------------------------------------------------------------------**/
  122. stock bool:Profiling_Run(&profilings)
  123. {
  124. P:3("bool:Profiling_Run called: %i", profilings);
  125. #if defined RUN_PROFILINGS
  126. P:2("Profiling_Run() called");
  127. new
  128. idx,
  129. buffer[32];
  130. while ((idx = AMX_GetPublicNamePrefix(idx, buffer, Y_PROFILING_PROFILE_SEARCH)))
  131. {
  132. buffer[0] = Y_PROFILING_INIT,
  133. CallLocalFunction(buffer, ""),
  134. // Call the test.
  135. buffer[0] = Y_PROFILING_PROFILE;
  136. P:5("Profiling_Run(): Calling %s", unpack(buffer[1]));
  137. YSI_g_sInProfile = true,
  138. CallLocalFunction(buffer, ""),
  139. YSI_g_sInProfile = false,
  140. buffer[0] = Y_PROFILING_CLOSE,
  141. CallLocalFunction(buffer, ""),
  142. ++profilings;
  143. PROFILE_REPORT(" ");
  144. }
  145. #else
  146. #pragma unused profilings
  147. #endif
  148. }
  149. forward OnProfilingsComplete(profilings, fails);
  150. #if defined RUN_PROFILINGS
  151. #if defined _AUTO_RUN_PROFILINGS
  152. #if defined FILTERSCRIPT
  153. // Hook main in gamemodes.
  154. public OnFilterScriptInit()
  155. {
  156. #if defined Profiling_OnFilterScriptInit
  157. Profiling_OnFilterScriptInit();
  158. #endif
  159. Profiling_RunAll();
  160. }
  161. #if defined _ALS_OnFilterScriptInit
  162. #undef OnFilterScriptInit
  163. #else
  164. #define _ALS_OnFilterScriptInit
  165. #endif
  166. #define OnFilterScriptInit Profiling_OnFilterScriptInit
  167. #if defined Profiling_OnFilterScriptInit
  168. forward Profiling_OnFilterScriptInit();
  169. #endif
  170. #else
  171. // Hook main in gamemodes.
  172. main()
  173. {
  174. #if defined Profiling_main
  175. Profiling_main();
  176. #endif
  177. Profiling_RunAll();
  178. }
  179. #if defined _ALS_main
  180. #undef main
  181. #else
  182. #define _ALS_main
  183. #endif
  184. #define main forward Profiling_main(); public Profiling_main
  185. #endif
  186. #endif
  187. forward OnRuntimeError(code, &bool:suppress);
  188. public OnRuntimeError(code, &bool:suppress)
  189. {
  190. if (YSI_g_sInProfile)
  191. {
  192. // Fail the current profile if we see any runtime errors. Requires the
  193. // crashdetect plugin to function, but not to compile and run.
  194. P:P("Error: Runtime error detected");
  195. }
  196. #if defined Profiling_OnRuntimeError
  197. return Profiling_OnRuntimeError(code, suppress);
  198. #else
  199. return 1;
  200. #endif
  201. }
  202. #if defined _ALS_OnRuntimeError
  203. #undef OnRuntimeError
  204. #else
  205. #define _ALS_OnRuntimeError
  206. #endif
  207. #define OnRuntimeError Profiling_OnRuntimeError
  208. #if defined Profiling_OnRuntimeError
  209. forward Profiling_OnRuntimeError(code, &bool:suppress);
  210. #endif
  211. #endif
  212. stock Profiling_RunAll()
  213. {
  214. // Disable error messages (as we're likely to generate them).
  215. new
  216. startTime,
  217. endTime,
  218. profilings;
  219. // The timing will be inaccurate since it will include many prints, but it
  220. // isn't that important to be accurate.
  221. startTime = GetTickCount();
  222. PROFILE_REPORT(" ");
  223. PROFILE_REPORT(" ||========================|| ");
  224. PROFILE_REPORT(" || STARTING PROFILINGS... || ");
  225. PROFILE_REPORT(" ||========================|| ");
  226. PROFILE_REPORT(" ");
  227. Profiling_Run(profilings);
  228. printf("*** Profilings: %d", profilings);
  229. PROFILE_REPORT(" ");
  230. PROFILE_REPORT(" ||======================|| ");
  231. PROFILE_REPORT(" || PROFILINGS COMPLETE! || ");
  232. PROFILE_REPORT(" ||======================|| ");
  233. PROFILE_REPORT(" ");
  234. endTime = GetTickCount();
  235. printf("*** Time: %dms", endTime - startTime);
  236. PROFILE_REPORT(" ");
  237. CallLocalFunction("OnProfilingsComplete", "i", profilings);
  238. #if defined PROFILE_AUTO_EXIT
  239. SendRconCommand("exit");
  240. #endif
  241. }
  242. #include "..\YSI_Internal\y_shortfunc"