as_debug.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. #if defined(__GNUC__) || defined( AS_MARMALADE )
  35. #ifdef __ghs__
  36. // WIIU defines __GNUC__ but types are not defined here in 'conventional' way
  37. #include <types.h>
  38. typedef signed char int8_t;
  39. typedef unsigned char uint8_t;
  40. typedef signed short int16_t;
  41. typedef unsigned short uint16_t;
  42. typedef signed int int32_t;
  43. typedef unsigned int uint32_t;
  44. typedef signed long long int64_t;
  45. typedef unsigned long long uint64_t;
  46. typedef float float32_t;
  47. typedef double float64_t;
  48. #else
  49. // Define mkdir for GNUC
  50. #include <sys/stat.h>
  51. #include <sys/types.h>
  52. #define _mkdir(dirname) mkdir(dirname, S_IRWXU)
  53. #endif
  54. #else
  55. #include <direct.h>
  56. #endif
  57. #if defined(_MSC_VER) && defined(AS_PROFILE)
  58. // Currently only do profiling with MSVC++
  59. #include <mmsystem.h>
  60. #include "as_string.h"
  61. #include "as_map.h"
  62. #include "as_string_util.h"
  63. BEGIN_AS_NAMESPACE
  64. struct TimeCount
  65. {
  66. double time;
  67. int count;
  68. double max;
  69. double min;
  70. };
  71. class CProfiler
  72. {
  73. public:
  74. CProfiler()
  75. {
  76. // We need to know how often the clock is updated
  77. __int64 tps;
  78. if( !QueryPerformanceFrequency((LARGE_INTEGER *)&tps) )
  79. usePerformance = false;
  80. else
  81. {
  82. usePerformance = true;
  83. ticksPerSecond = double(tps);
  84. }
  85. timeOffset = GetTime();
  86. }
  87. ~CProfiler()
  88. {
  89. WriteSummary();
  90. }
  91. double GetTime()
  92. {
  93. if( usePerformance )
  94. {
  95. __int64 ticks;
  96. QueryPerformanceCounter((LARGE_INTEGER *)&ticks);
  97. return double(ticks)/ticksPerSecond - timeOffset;
  98. }
  99. return double(timeGetTime())/1000.0 - timeOffset;
  100. }
  101. double Begin(const char *name)
  102. {
  103. double time = GetTime();
  104. // Add the scope to the key
  105. if( key.GetLength() )
  106. key += "|";
  107. key += name;
  108. // Compensate for the time spent writing to the file
  109. timeOffset += GetTime() - time;
  110. return time;
  111. }
  112. void End(const char *name, double beginTime)
  113. {
  114. double time = GetTime();
  115. double elapsed = time - beginTime;
  116. // Update the profile info for this scope
  117. asSMapNode<asCString, TimeCount> *cursor;
  118. if( map.MoveTo(&cursor, key) )
  119. {
  120. cursor->value.time += elapsed;
  121. cursor->value.count++;
  122. if( cursor->value.max < elapsed )
  123. cursor->value.max = elapsed;
  124. if( cursor->value.min > elapsed )
  125. cursor->value.min = elapsed;
  126. }
  127. else
  128. {
  129. TimeCount tc = {elapsed, 1, elapsed, elapsed};
  130. map.Insert(key, tc);
  131. }
  132. // Remove the inner most scope from the key
  133. int n = key.FindLast("|");
  134. if( n > 0 )
  135. key.SetLength(n);
  136. else
  137. key.SetLength(0);
  138. // Compensate for the time spent writing to the file
  139. timeOffset += GetTime() - time;
  140. }
  141. protected:
  142. void WriteSummary()
  143. {
  144. // Write the analyzed info into a file for inspection
  145. _mkdir("AS_DEBUG");
  146. FILE *fp;
  147. #if _MSC_VER >= 1500 && !defined(AS_MARMALADE)
  148. fopen_s(&fp, "AS_DEBUG/profiling_summary.txt", "wt");
  149. #else
  150. fp = fopen("AS_DEBUG/profiling_summary.txt", "wt");
  151. #endif
  152. if( fp == 0 )
  153. return;
  154. fprintf(fp, "%-60s %10s %15s %15s %15s %15s\n\n", "Scope", "Count", "Tot time", "Avg time", "Max time", "Min time");
  155. asSMapNode<asCString, TimeCount> *cursor;
  156. map.MoveLast(&cursor);
  157. while( cursor )
  158. {
  159. asCString key = cursor->key;
  160. int count;
  161. int n = key.FindLast("|", &count);
  162. if( count )
  163. {
  164. key = asCString(" ", count) + key.SubString(n+1);
  165. }
  166. 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);
  167. map.MovePrev(&cursor, cursor);
  168. }
  169. fclose(fp);
  170. }
  171. double timeOffset;
  172. double ticksPerSecond;
  173. bool usePerformance;
  174. asCString key;
  175. asCMap<asCString, TimeCount> map;
  176. };
  177. extern CProfiler g_profiler;
  178. class CProfilerScope
  179. {
  180. public:
  181. CProfilerScope(const char *name)
  182. {
  183. this->name = name;
  184. beginTime = g_profiler.Begin(name);
  185. }
  186. ~CProfilerScope()
  187. {
  188. g_profiler.End(name, beginTime);
  189. }
  190. protected:
  191. const char *name;
  192. double beginTime;
  193. };
  194. #define TimeIt(x) CProfilerScope profilescope(x)
  195. END_AS_NAMESPACE
  196. #else // _MSC_VER && AS_PROFILE
  197. // Define it so nothing is done
  198. #define TimeIt(x)
  199. #endif // !(_MSC_VER && AS_PROFILE)
  200. #endif // _WIN32_WCE
  201. #endif // AS_WII
  202. #else // !defined(AS_DEBUG)
  203. // Define it so nothing is done
  204. #define TimeIt(x)
  205. #endif // !defined(AS_DEBUG)
  206. #endif