Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
wwprofile.h
Go to the documentation of this file.
1/*
2** Command & Conquer Generals Zero Hour(tm)
3** Copyright 2025 Electronic Arts Inc.
4**
5** This program is free software: you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation, either version 3 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
15** You should have received a copy of the GNU General Public License
16** along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19/***********************************************************************************************
20 *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
21 ***********************************************************************************************
22 * *
23 * Project Name : WWDebug *
24 * *
25 * $Archive:: /Commando/Code/wwdebug/wwprofile.h $*
26 * *
27 * $Author:: Jani_p $*
28 * *
29 * $Modtime:: 3/25/02 2:05p $*
30 * *
31 * $Revision:: 14 $*
32 * *
33 *---------------------------------------------------------------------------------------------*
34 * Functions: *
35 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
36
37#if _MSC_VER >= 1000
38#pragma once
39#endif // _MSC_VER >= 1000
40
41//#define ENABLE_TIME_AND_MEMORY_LOG
42
43#ifndef WWPROFILE_H
44#define WWPROFILE_H
45
46#include "wwstring.h"
47
48#ifdef _UNIX
49typedef signed long long __int64;
50typedef signed long long _int64;
51#endif
52
53// enable profiling by default in debug mode.
54#ifdef WWDEBUG
55#define ENABLE_WWPROFILE
56#endif
57
58extern unsigned WWProfile_Get_System_Time(); // timeGetTime() wrapper
59class FileClass;
60
61/*
62** A node in the WWProfile Hierarchy Tree
63*/
65
66public:
67 WWProfileHierachyNodeClass( const char * name, WWProfileHierachyNodeClass * parent );
70
71 WWProfileHierachyNodeClass * Get_Sub_Node( const char * name );
72
76
80
81 void Reset( void );
82 void Call( void );
83 bool Return( void );
84
85 const char * Get_Name( void ) { return Name; }
86 int Get_Total_Calls( void ) { return TotalCalls; }
87 float Get_Total_Time( void ) { return TotalTime; }
88 void Set_Total_Calls(int calls) { TotalCalls=calls; }
89 void Set_Total_Time(float time) { TotalTime=time; }
90
92 void Write_To_File(FileClass* file,int recursion);
94
95
96protected:
97
98 const char * Name;
101 __int64 StartTime;
104
108};
109
111public:
112 WWProfileHierachyInfoClass( const char * name, WWProfileHierachyInfoClass * parent );
114
118
122
123 const char * Get_Name( void ) { return Name; }
124 void Set_Name( const char* name ) { Name=name; }
125 int Get_Total_Calls( void ) { return TotalCalls; }
126 float Get_Total_Time( void ) { return TotalTime; }
127 void Set_Total_Calls(int calls) { TotalCalls=calls; }
128 void Set_Total_Time(float time) { TotalTime=time; }
129
130protected:
131
135
139};
140
141/*
142** An iterator to navigate through the tree
143*/
145{
146public:
147 // Access all the children of the current parent
148 void First(void);
149 void Next(void);
150 bool Is_Done(void);
151
152 void Enter_Child( void ); // Make the current child the new parent
153 void Enter_Child( int index ); // Make the given child the new parent
154 void Enter_Parent( void ); // Make the current parent's parent the new parent
155
156 // Access the current child
157 const char * Get_Current_Name( void ) { return CurrentChild->Get_Name(); }
158 int Get_Current_Total_Calls( void ) { return CurrentChild->Get_Total_Calls(); }
159 float Get_Current_Total_Time( void ) { return CurrentChild->Get_Total_Time(); }
160
161 // Access the current parent
162 const char * Get_Current_Parent_Name( void ) { return CurrentParent->Get_Name(); }
163 int Get_Current_Parent_Total_Calls( void ) { return CurrentParent->Get_Total_Calls(); }
164 float Get_Current_Parent_Total_Time( void ) { return CurrentParent->Get_Total_Time(); }
165
166protected:
169
171 friend class WWProfileManager;
172};
173
174
175/*
176** An iterator to walk through the tree in depth first order
177*/
179{
180public:
181 void First(void);
182 void Next(void);
183 bool Is_Done(void);
184
185 // Access the current node
186 const char * Get_Current_Name( void ) { return CurrentNode->Get_Name(); }
187 int Get_Current_Total_Calls( void ) { return CurrentNode->Get_Total_Calls(); }
188 float Get_Current_Total_Time( void ) { return CurrentNode->Get_Total_Time(); }
189
190protected:
192
194 friend class WWProfileManager;
195};
196
197
198/*
199** The Manager for the WWProfile system
200*/
202public:
203 WWINLINE static void Enable_Profile(bool enable) { IsProfileEnabled=enable; }
204 WWINLINE static bool Is_Profile_Enabled() { return IsProfileEnabled; }
205
206 static void Start_Profile( const char * name );
207 static void Stop_Profile( void );
208
209 static void Start_Root_Profile( const char * name );
210 static void Stop_Root_Profile( void );
211
212 static void Reset( void );
213 static void Increment_Frame_Counter( void );
214 static int Get_Frame_Count_Since_Reset( void ) { return FrameCounter; }
215 static float Get_Time_Since_Reset( void );
216
217 static WWProfileIterator * Get_Iterator( void );
218 static void Release_Iterator( WWProfileIterator * iterator );
220 static void Release_In_Order_Iterator( WWProfileInOrderIterator * iterator );
221
222 static WWProfileHierachyNodeClass * Get_Root( void ) { return &Root; }
223
224 static void Begin_Collecting();
225 static void End_Collecting(const char* filename);
226
227 static void Load_Profile_Log(const char* filename, WWProfileHierachyInfoClass**& array, unsigned& count);
228
229private:
230 static WWProfileHierachyNodeClass Root;
231 static WWProfileHierachyNodeClass * CurrentNode;
232 static WWProfileHierachyNodeClass * CurrentRootNode;
233 static int FrameCounter;
234 static __int64 ResetTime;
235 static bool IsProfileEnabled;
236
238};
239
240
241/*
242** WWProfileSampleClass is a simple way to profile a function's scope
243** Use the WWPROFILE macro at the start of scope to time
244*/
246 bool IsRoot;
247 bool Enabled;
248public:
249 WWProfileSampleClass( const char * name, bool is_root ) : IsRoot(is_root), Enabled(WWProfileManager::Is_Profile_Enabled())
250 {
251 if (Enabled) {
252 if (IsRoot) WWProfileManager::Start_Root_Profile( name );
254 }
255 }
256
258 {
259 if (Enabled) {
262 }
263 }
264};
265
266#ifdef ENABLE_WWPROFILE
267#define WWPROFILE( name ) WWProfileSampleClass _wwprofile( name, false )
268#define WWROOTPROFILE( name ) WWProfileSampleClass _wwprofile( name, true )
269#else
270#define WWPROFILE( name )
271#define WWROOTPROFILE( name )
272#endif
273
274
275/*
276** WWTimeIt is like WWProfile, but it doesn't save anything, it just times one routine, regardless of thread
277*/
279public:
280 WWTimeItClass( const char * name );
281 ~WWTimeItClass( void );
282private:
283 const char * Name;
284 __int64 Time;
285};
286
287#ifdef ENABLE_WWPROFILE
288#define WWTIMEIT( name ) WWTimeItClass _wwtimeit( name )
289#else
290#define WWTIMEIT( name )
291#endif
292
293
294
295/*
296** WWMeasureItClass is like WWTimeItClass, but it pokes the result into the given float,
297** and can be used in the release build.
298*/
300public:
301 WWMeasureItClass( float * p_result );
302 ~WWMeasureItClass( void );
303
304private:
305 __int64 Time;
306 float * PResult;
307};
308
309// ----------------------------------------------------------------------------
310//
311// Use the first macro to log time and memory usage within the stack segment.
312// Use the second macro to log intermediate values. The intermediate values are
313// calculated from the previous intermediate log, so you can log how much each
314// item takes by placing the macro after each of the
315//
316// ----------------------------------------------------------------------------
317
318#ifdef ENABLE_TIME_AND_MEMORY_LOG
319#define WWLOG_PREPARE_TIME_AND_MEMORY(t) WWMemoryAndTimeLog memory_and_time_log(t)
320#define WWLOG_INTERMEDIATE(t) memory_and_time_log.Log_Intermediate(t)
321#else
322#define WWLOG_PREPARE_TIME_AND_MEMORY(t)
323#define WWLOG_INTERMEDIATE(t)
324#endif
325
341
342#endif // WWPROFILE_H
#define WWINLINE
Definition always.h:172
WWMeasureItClass(float *p_result)
WWProfileHierachyInfoClass * Get_Sibling(void)
Definition wwprofile.h:116
WWProfileHierachyInfoClass * Sibling
Definition wwprofile.h:138
void Set_Parent(WWProfileHierachyInfoClass *node)
Definition wwprofile.h:119
void Set_Sibling(WWProfileHierachyInfoClass *node)
Definition wwprofile.h:120
WWProfileHierachyInfoClass * Child
Definition wwprofile.h:137
WWProfileHierachyInfoClass * Get_Parent(void)
Definition wwprofile.h:115
WWProfileHierachyInfoClass(const char *name, WWProfileHierachyInfoClass *parent)
void Set_Total_Time(float time)
Definition wwprofile.h:128
void Set_Total_Calls(int calls)
Definition wwprofile.h:127
WWProfileHierachyInfoClass * Get_Child(void)
Definition wwprofile.h:117
void Set_Child(WWProfileHierachyInfoClass *node)
Definition wwprofile.h:121
const char * Get_Name(void)
Definition wwprofile.h:123
WWProfileHierachyInfoClass * Parent
Definition wwprofile.h:136
void Set_Name(const char *name)
Definition wwprofile.h:124
WWProfileHierachyNodeClass * Sibling
Definition wwprofile.h:107
void Set_Child(WWProfileHierachyNodeClass *node)
Definition wwprofile.h:79
const char * Get_Name(void)
Definition wwprofile.h:85
void Add_To_String_Compact(StringClass &string, int recursion)
WWProfileHierachyNodeClass * Get_Sub_Node(const char *name)
WWProfileHierachyNodeClass * Get_Parent(void)
Definition wwprofile.h:73
WWProfileHierachyNodeClass(const char *name, WWProfileHierachyNodeClass *parent)
WWProfileHierachyNodeClass * Get_Child(void)
Definition wwprofile.h:75
void Set_Sibling(WWProfileHierachyNodeClass *node)
Definition wwprofile.h:78
WWProfileHierachyNodeClass * Child
Definition wwprofile.h:106
void Write_To_File(FileClass *file, int recursion)
void Set_Total_Calls(int calls)
Definition wwprofile.h:88
void Set_Total_Time(float time)
Definition wwprofile.h:89
void Set_Parent(WWProfileHierachyNodeClass *node)
Definition wwprofile.h:77
WWProfileHierachyNodeClass * Get_Sibling(void)
Definition wwprofile.h:74
WWProfileHierachyNodeClass * Parent
Definition wwprofile.h:105
WWProfileHierachyNodeClass * Clone_Hierarchy(WWProfileHierachyNodeClass *parent)
const char * Get_Current_Name(void)
Definition wwprofile.h:186
float Get_Current_Total_Time(void)
Definition wwprofile.h:188
friend class WWProfileManager
Definition wwprofile.h:194
WWProfileHierachyNodeClass * CurrentNode
Definition wwprofile.h:191
int Get_Current_Total_Calls(void)
Definition wwprofile.h:187
WWProfileIterator(WWProfileHierachyNodeClass *start)
const char * Get_Current_Parent_Name(void)
Definition wwprofile.h:162
int Get_Current_Parent_Total_Calls(void)
Definition wwprofile.h:163
int Get_Current_Total_Calls(void)
Definition wwprofile.h:158
float Get_Current_Parent_Total_Time(void)
Definition wwprofile.h:164
WWProfileHierachyNodeClass * CurrentParent
Definition wwprofile.h:167
const char * Get_Current_Name(void)
Definition wwprofile.h:157
WWProfileHierachyNodeClass * CurrentChild
Definition wwprofile.h:168
friend class WWProfileManager
Definition wwprofile.h:171
bool Is_Done(void)
float Get_Current_Total_Time(void)
Definition wwprofile.h:159
void Enter_Child(void)
void Enter_Parent(void)
static WWProfileIterator * Get_Iterator(void)
static WWINLINE void Enable_Profile(bool enable)
Definition wwprofile.h:203
static void Release_In_Order_Iterator(WWProfileInOrderIterator *iterator)
static WWProfileInOrderIterator * Get_In_Order_Iterator(void)
static void Begin_Collecting()
static void Stop_Profile(void)
static void Start_Profile(const char *name)
static WWProfileHierachyNodeClass * Get_Root(void)
Definition wwprofile.h:222
static void Release_Iterator(WWProfileIterator *iterator)
static void Increment_Frame_Counter(void)
static void Load_Profile_Log(const char *filename, WWProfileHierachyInfoClass **&array, unsigned &count)
static void End_Collecting(const char *filename)
static void Start_Root_Profile(const char *name)
friend class WWProfileInOrderIterator
Definition wwprofile.h:237
static void Stop_Root_Profile(void)
static void Reset(void)
static int Get_Frame_Count_Since_Reset(void)
Definition wwprofile.h:214
static float Get_Time_Since_Reset(void)
static WWINLINE bool Is_Profile_Enabled()
Definition wwprofile.h:204
WWProfileSampleClass(const char *name, bool is_root)
Definition wwprofile.h:249
WWTimeItClass(const char *name)
void recursion(int level)
Definition test1.cpp:56
static unsigned TabCount
Definition wwprofile.h:335
void Log_Intermediate(const char *text)
StringClass Name
Definition wwprofile.h:334
unsigned IntermediateTimeStart
Definition wwprofile.h:329
int IntermediateAllocSizeStart
Definition wwprofile.h:333
WWMemoryAndTimeLog(const char *name)
int IntermediateAllocCountStart
Definition wwprofile.h:331
unsigned WWProfile_Get_System_Time()
Definition wwprofile.cpp:73