as_debug.h 5.6 KB

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