AndroidProfiler.cpp 6.2 KB

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