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