77eee052e6c452774d5421f3470f988c8cb034a9
[matthijs/ABM2.git] / ABM2 / Engine / profiler.h
1 #ifndef PROFILER_H_INCLUDED\r
2 #define PROFILER_H_INCLUDED\r
3 \r
4 #define MAX_PROFILER_SAMPLES 50\r
5 \r
6 class IProfilerOutputHandler;\r
7 class CProfileSample;\r
8 \r
9 class CProfileSample\r
10 {\r
11 public:\r
12         CProfileSample(std::string sampleName);\r
13         ~CProfileSample();\r
14 \r
15         static void Output();\r
16 \r
17         static void ResetSample(std::string sampleName);\r
18         static void ResetAll();\r
19 \r
20         static IProfilerOutputHandler *outputHandler;\r
21 \r
22         static bool bProfilerIsRunning;\r
23 \r
24 protected:\r
25         //index into the array of samples\r
26         int iSampleIndex;\r
27         int iParentIndex;\r
28 \r
29         inline float GetTime(){ return ((float)GetTickCount())/1000.0f; }\r
30 \r
31         static struct profileSample\r
32         {\r
33                 profileSample()\r
34                 {\r
35                         bIsValid=false; \r
36                         dataCount=0;\r
37                         averagePc=minPc=maxPc=-1;\r
38                 }\r
39 \r
40                 bool bIsValid;          //whether or not this sample is valid (for use with fixed-size arrays)\r
41                 bool bIsOpen;           //is this sample currently being profiled?\r
42                 unsigned int callCount; //number of times this sample has been profiled this frame\r
43                 std::string name;       //name of the sample\r
44                 \r
45                 float startTime;        //starting time on the clock, in seconds\r
46                 float totalTime;        //total time recorded across all profiles of this sample\r
47                 float childTime;        //total time taken by children of this sample\r
48 \r
49                 int parentCount;        //number of parents this sample has (useful for indenting)\r
50 \r
51                 float averagePc;        //average percentage of game loop time taken up\r
52                 float minPc;            //minimum percentage of game loop time taken up\r
53                 float maxPc;            //maximum percentage of game loop time taken up\r
54                 unsigned long dataCount;//number of percentage values that have been stored\r
55         } samples[MAX_PROFILER_SAMPLES];\r
56         static int lastOpenedSample;\r
57         static int openSampleCount;\r
58         static float rootBegin, rootEnd;\r
59 };\r
60 \r
61 class IProfilerOutputHandler\r
62 {\r
63 public:\r
64         virtual void BeginOutput(float tTotal)=0;\r
65         virtual void Sample(float fMin, float fAvg, float fMax, float tAvg, int callCount, std::string name, int parentCount)=0;\r
66         virtual void EndOutput()=0;\r
67 };\r
68 \r
69 #ifdef _DEBUG\r
70 #define PROFILE(name) CProfileSample _profile_sample(name);\r
71 #else\r
72 #define PROFILE(name)\r
73 #endif\r
74 \r
75 #endif\r