as_debug.h 5.6 KB

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