AndroidProfiler.cpp 6.4 KB

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