iOSProfiler.mm 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  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
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell 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
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. //
  23. // iOSMiniProfiler.mm
  24. // Torque2D
  25. //
  26. // Created by puap on 9/26/08.
  27. // Copyright 2008 PUAP. All rights reserved.
  28. //
  29. #import "iOSProfiler.h"
  30. //PUAP
  31. //--------------------------------------------------------------------------------------------------------------------------------------------
  32. iOSProfilerData g_iOSProfilerData[IOS_PROFILER_MAX_CALLS];
  33. int g_iOSProfilerCount = 0;
  34. bool g_iOSProfilerReady = false;
  35. void iOSProfilerResetAll() {
  36. for(int i=0; i<IOS_PROFILER_MAX_CALLS; i++){
  37. g_iOSProfilerData[i].name = NULL;
  38. g_iOSProfilerData[i].timeMach = 0;
  39. g_iOSProfilerData[i].timeMS = 0;
  40. g_iOSProfilerData[i].type = 0;
  41. g_iOSProfilerData[i].invokes = 0;
  42. }
  43. g_iOSProfilerCount = 0;
  44. }
  45. void iOSProfilerProfilerInit() {
  46. g_iOSProfilerReady = true;
  47. iOSProfilerResetAll();
  48. printf( "\n\nIniting PUAP Profiler\n");
  49. }
  50. void iOSProfilerStart( const char *name ) {
  51. if( g_iOSProfilerReady ) {
  52. int i = g_iOSProfilerCount++;
  53. g_iOSProfilerData[i].name = name;
  54. g_iOSProfilerData[i].timeMach = mach_absolute_time();
  55. g_iOSProfilerData[i].timeMS = Platform::getRealMilliseconds();
  56. g_iOSProfilerData[i].type = 0;
  57. g_iOSProfilerData[i].invokes = 0;
  58. if(g_iOSProfilerCount >= IOS_PROFILER_MAX_CALLS){
  59. g_iOSProfilerReady = false;
  60. }
  61. }
  62. }
  63. void iOSProfilerEnd( const char *name ) {
  64. if( g_iOSProfilerReady ) {
  65. int i = g_iOSProfilerCount++;
  66. g_iOSProfilerData[i].name = name;
  67. g_iOSProfilerData[i].timeMach = mach_absolute_time();
  68. g_iOSProfilerData[i].timeMS = Platform::getRealMilliseconds();
  69. g_iOSProfilerData[i].type = 1;
  70. g_iOSProfilerData[i].invokes = 0;
  71. if(g_iOSProfilerCount >= IOS_PROFILER_MAX_CALLS){
  72. g_iOSProfilerReady = false;
  73. }
  74. }
  75. }
  76. int iOSProfilerFindProfileEnd( const char *name, int startCount){
  77. int invokes = 0;
  78. for( int i = (startCount+1); i < g_iOSProfilerCount; i++ ) {
  79. if(g_iOSProfilerData[i].name == name || dStrcmp(g_iOSProfilerData[i].name, name)==0){
  80. if(g_iOSProfilerData[i].type == 1){
  81. if(invokes == 0){
  82. return i;
  83. }
  84. else{
  85. g_iOSProfilerData[i].invokes = invokes;
  86. invokes--;
  87. }
  88. }
  89. else{
  90. invokes++;
  91. g_iOSProfilerData[i].invokes = invokes;
  92. }
  93. }
  94. }
  95. return -1;
  96. }
  97. int iOSProfilerGetCount(){
  98. return g_iOSProfilerCount;
  99. }
  100. char pfbuffer[1024];
  101. void iOSProfilerPrintResult( int item ) {
  102. if(g_iOSProfilerData[item].type == 0){
  103. int endItem = iOSProfilerFindProfileEnd(g_iOSProfilerData[item].name, item);
  104. if(endItem < 0){
  105. return; //didn't find the end of this one
  106. }
  107. U32 startTimeMS = g_iOSProfilerData[item].timeMS;
  108. U32 endTimeMS = g_iOSProfilerData[endItem].timeMS;
  109. dSprintf(pfbuffer, 1024, "%s, %d, %d, %d\n",
  110. g_iOSProfilerData[item].name, startTimeMS, endTimeMS, endTimeMS - startTimeMS);
  111. printf( pfbuffer, "%s" );
  112. }
  113. }
  114. void iOSProfilerPrintAllResults() {
  115. dSprintf(pfbuffer, 1024, "\n\n \t Time Per Frame \t Average Time \t Calls Per Frame (avg) \t Time Per Call \t Name \n" );
  116. printf("%s", pfbuffer);
  117. for( int i = 0; i < g_iOSProfilerCount; i++ ) {
  118. iOSProfilerPrintResult( i );
  119. }
  120. dSprintf(pfbuffer, 1024, "\n <----------------------------------------------------------------------------------->\n " );
  121. printf("%s", pfbuffer);
  122. }
  123. //-Mat get instruction names
  124. /// The opcodes for the TorqueScript VM.
  125. const char *InstructionName[] = {
  126. "OP_FUNC_DECL",
  127. "OP_CREATE_OBJECT",
  128. "OP_ADD_OBJECT",
  129. "OP_END_OBJECT",
  130. "OP_JMPIFFNOT",
  131. "OP_JMPIFNOT",
  132. "OP_JMPIFF",
  133. "OP_JMPIF",
  134. "OP_JMPIFNOT_NP",
  135. "OP_JMPIF_NP",
  136. "OP_JMP",
  137. "OP_RETURN",
  138. "OP_CMPEQ",
  139. "OP_CMPGR",
  140. "OP_CMPGE",
  141. "OP_CMPLT",
  142. "OP_CMPLE",
  143. "OP_CMPNE",
  144. "OP_XOR",
  145. "OP_MOD",
  146. "OP_BITAND",
  147. "OP_BITOR",
  148. "OP_NOT",
  149. "OP_NOTF",
  150. "OP_ONESCOMPLEMENT",
  151. "OP_SHR",
  152. "OP_SHL",
  153. "OP_AND",
  154. "OP_OR",
  155. "OP_ADD",
  156. "OP_SUB",
  157. "OP_MUL",
  158. "OP_DIV",
  159. "OP_NEG",
  160. "OP_SETCURVAR",
  161. "OP_SETCURVAR_CREATE",
  162. "OP_SETCURVAR_ARRAY",
  163. "OP_SETCURVAR_ARRAY_CREATE",
  164. "OP_LOADVAR_UINT",
  165. "OP_LOADVAR_FLT",
  166. "OP_LOADVAR_STR",
  167. "OP_SAVEVAR_UINT",
  168. "OP_SAVEVAR_FLT",
  169. "OP_SAVEVAR_STR",
  170. "OP_SETCUROBJECT",
  171. "OP_SETCUROBJECT_NEW",
  172. "OP_SETCUROBJECT_INTERNAL",
  173. "OP_SETCURFIELD",
  174. "OP_SETCURFIELD_ARRAY",
  175. "OP_LOADFIELD_UINT",
  176. "OP_LOADFIELD_FLT",
  177. "OP_LOADFIELD_STR",
  178. "OP_SAVEFIELD_UINT",
  179. "OP_SAVEFIELD_FLT",
  180. "OP_SAVEFIELD_STR",
  181. "OP_STR_TO_UINT",
  182. "OP_STR_TO_FLT",
  183. "OP_STR_TO_NONE",
  184. "OP_FLT_TO_UINT",
  185. "OP_FLT_TO_STR",
  186. "OP_FLT_TO_NONE",
  187. "OP_UINT_TO_FLT",
  188. "OP_UINT_TO_STR",
  189. "OP_UINT_TO_NONE",
  190. "OP_LOADIMMED_UINT",
  191. "OP_LOADIMMED_FLT",
  192. "OP_TAG_TO_STR",
  193. "OP_LOADIMMED_STR",
  194. "OP_DOCBLOCK_STR",
  195. "OP_LOADIMMED_IDENT",
  196. "OP_CALLFUNC_RESOLVE",
  197. "OP_CALLFUNC",
  198. "OP_ADVANCE_STR",
  199. "OP_ADVANCE_STR_APPENDCHAR",
  200. "OP_ADVANCE_STR_COMMA",
  201. "OP_ADVANCE_STR_NUL",
  202. "OP_REWIND_STR",
  203. "OP_TERMINATE_REWIND_STR",
  204. "OP_COMPARE_STR",
  205. "OP_PUSH",
  206. "OP_PUSH_FRAME",
  207. "OP_BREAK",
  208. "OP_INVALID"
  209. };
  210. const char *getInstructionName( int index ) {
  211. return InstructionName[index];
  212. }