y_profiling.inc 7.5 KB

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