as_debug.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2014 Andreas Jonsson
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any
  6. damages arising from the use of this software.
  7. Permission is granted to anyone to use this software for any
  8. purpose, including commercial applications, and to alter it and
  9. redistribute it freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you
  11. must not claim that you wrote the original software. If you use
  12. this software in a product, an acknowledgment in the product
  13. documentation would be appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and
  15. must not be misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source
  17. distribution.
  18. The original version of this library can be located at:
  19. http://www.angelcode.com/angelscript/
  20. Andreas Jonsson
  21. [email protected]
  22. */
  23. //
  24. // as_debug.h
  25. //
  26. #ifndef AS_DEBUG_H
  27. #define AS_DEBUG_H
  28. #include "as_config.h"
  29. #if defined(AS_DEBUG)
  30. #ifndef AS_WII
  31. // The Wii SDK doesn't have these, we'll survive without AS_DEBUG
  32. #ifndef _WIN32_WCE
  33. // Neither does WinCE
  34. #ifndef AS_PSVITA
  35. // Possible on PSVita, but requires SDK access
  36. #if defined(__GNUC__) || defined( AS_MARMALADE )
  37. #ifdef __ghs__
  38. // WIIU defines __GNUC__ but types are not defined here in 'conventional' way
  39. #include <types.h>
  40. typedef signed char int8_t;
  41. typedef unsigned char uint8_t;
  42. typedef signed short int16_t;
  43. typedef unsigned short uint16_t;
  44. typedef signed int int32_t;
  45. typedef unsigned int uint32_t;
  46. typedef signed long long int64_t;
  47. typedef unsigned long long uint64_t;
  48. typedef float float32_t;
  49. typedef double float64_t;
  50. #else
  51. // Define mkdir for GNUC
  52. #include <sys/stat.h>
  53. #include <sys/types.h>
  54. #define _mkdir(dirname) mkdir(dirname, S_IRWXU)
  55. #endif
  56. #else
  57. #include <direct.h>
  58. #endif
  59. #if defined(_MSC_VER) && defined(AS_PROFILE)
  60. // Currently only do profiling with MSVC++
  61. #include <mmsystem.h>
  62. #include "as_string.h"
  63. #include "as_map.h"
  64. #include "as_string_util.h"
  65. BEGIN_AS_NAMESPACE
  66. struct TimeCount
  67. {
  68. double time;
  69. int count;
  70. double max;
  71. double min;
  72. };
  73. class CProfiler
  74. {
  75. public:
  76. CProfiler()
  77. {
  78. // We need to know how often the clock is updated
  79. __int64 tps;
  80. if( !QueryPerformanceFrequency((LARGE_INTEGER *)&tps) )
  81. usePerformance = false;
  82. else
  83. {
  84. usePerformance = true;
  85. ticksPerSecond = double(tps);
  86. }
  87. timeOffset = GetTime();
  88. }
  89. ~CProfiler()
  90. {
  91. WriteSummary();
  92. }
  93. double GetTime()
  94. {
  95. if( usePerformance )
  96. {
  97. __int64 ticks;
  98. QueryPerformanceCounter((LARGE_INTEGER *)&ticks);
  99. return double(ticks)/ticksPerSecond - timeOffset;
  100. }
  101. return double(timeGetTime())/1000.0 - timeOffset;
  102. }
  103. double Begin(const char *name)
  104. {
  105. double time = GetTime();
  106. // Add the scope to the key
  107. if( key.GetLength() )
  108. key += "|";
  109. key += name;
  110. // Compensate for the time spent writing to the file
  111. timeOffset += GetTime() - time;
  112. return time;
  113. }
  114. void End(const char *name, double beginTime)
  115. {
  116. double time = GetTime();
  117. double elapsed = time - beginTime;
  118. // Update the profile info for this scope
  119. asSMapNode<asCString, TimeCount> *cursor;
  120. if( map.MoveTo(&cursor, key) )
  121. {
  122. cursor->value.time += elapsed;
  123. cursor->value.count++;
  124. if( cursor->value.max < elapsed )
  125. cursor->value.max = elapsed;
  126. if( cursor->value.min > elapsed )
  127. cursor->value.min = elapsed;
  128. }
  129. else
  130. {
  131. TimeCount tc = {elapsed, 1, elapsed, elapsed};
  132. map.Insert(key, tc);
  133. }
  134. // Remove the inner most scope from the key
  135. int n = key.FindLast("|");
  136. if( n > 0 )
  137. key.SetLength(n);
  138. else
  139. key.SetLength(0);
  140. // Compensate for the time spent writing to the file
  141. timeOffset += GetTime() - time;
  142. }
  143. protected:
  144. void WriteSummary()
  145. {
  146. // Write the analyzed info into a file for inspection
  147. _mkdir("AS_DEBUG");
  148. FILE *fp;
  149. #if _MSC_VER >= 1500 && !defined(AS_MARMALADE)
  150. fopen_s(&fp, "AS_DEBUG/profiling_summary.txt", "wt");
  151. #else
  152. fp = fopen("AS_DEBUG/profiling_summary.txt", "wt");
  153. #endif
  154. if( fp == 0 )
  155. return;
  156. fprintf(fp, "%-60s %10s %15s %15s %15s %15s\n\n", "Scope", "Count", "Tot time", "Avg time", "Max time", "Min time");
  157. asSMapNode<asCString, TimeCount> *cursor;
  158. map.MoveLast(&cursor);
  159. while( cursor )
  160. {
  161. asCString key = cursor->key;
  162. int count;
  163. int n = key.FindLast("|", &count);
  164. if( count )
  165. {
  166. key = asCString(" ", count) + key.SubString(n+1);
  167. }
  168. fprintf(fp, "%-60s %10d %15.6f %15.6f %15.6f %15.6f\n", key.AddressOf(), cursor->value.count, cursor->value.time, cursor->value.time / cursor->value.count, cursor->value.max, cursor->value.min);
  169. map.MovePrev(&cursor, cursor);
  170. }
  171. fclose(fp);
  172. }
  173. double timeOffset;
  174. double ticksPerSecond;
  175. bool usePerformance;
  176. asCString key;
  177. asCMap<asCString, TimeCount> map;
  178. };
  179. extern CProfiler g_profiler;
  180. class CProfilerScope
  181. {
  182. public:
  183. CProfilerScope(const char *name)
  184. {
  185. this->name = name;
  186. beginTime = g_profiler.Begin(name);
  187. }
  188. ~CProfilerScope()
  189. {
  190. g_profiler.End(name, beginTime);
  191. }
  192. protected:
  193. const char *name;
  194. double beginTime;
  195. };
  196. #define TimeIt(x) CProfilerScope profilescope(x)
  197. END_AS_NAMESPACE
  198. #else // _MSC_VER && AS_PROFILE
  199. // Define it so nothing is done
  200. #define TimeIt(x)
  201. #endif // !(_MSC_VER && AS_PROFILE)
  202. #endif // AS_PSVITA
  203. #endif // _WIN32_WCE
  204. #endif // AS_WII
  205. #else // !defined(AS_DEBUG)
  206. // Define it so nothing is done
  207. #define TimeIt(x)
  208. #endif // !defined(AS_DEBUG)
  209. #endif