Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
internal_funclevel.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
20// $File: //depot/GeneralsMD/Staging/code/Libraries/Source/profile/internal_funclevel.h $
21// $Author: mhoffe $
22// $Revision: #4 $
23// $DateTime: 2003/08/14 13:43:29 $
24//
25// ©2003 Electronic Arts
26//
27// Function level profiling (internal header)
29#ifdef _MSC_VER
30# pragma once
31#endif
32#ifndef INTERNAL_FUNCLEVEL_H // Include guard
33#define INTERNAL_FUNCLEVEL_H
34
35class ProfileFuncLevelTracer
36{
37 friend class ProfileCmdInterface;
38
39 // can't copy this
40 ProfileFuncLevelTracer(const ProfileFuncLevelTracer&);
41 ProfileFuncLevelTracer& operator=(const ProfileFuncLevelTracer&);
42
43public:
44 enum
45 {
46 // # of simultaneous frame recordings
48 };
49
51 class UnsignedMap
52 {
53 UnsignedMap(const UnsignedMap&);
54 UnsignedMap& operator=(const UnsignedMap&);
55
56 struct Entry
57 {
58 Entry *next;
59 unsigned val;
60 unsigned count;
61 };
62
63 enum
64 {
65 // do not make this number too large
66 // a single function uses approx HASH_SIZE*20 bytes!
67 HASH_SIZE = 131
68 };
69
70 Entry *e;
71 unsigned alloc,used;
72 Entry *hash[HASH_SIZE];
73 bool writeLock;
74
75 void _Insert(unsigned at, unsigned val, int countAdd);
76
77 public:
80
81 void Clear(void);
82 void Insert(unsigned val, int countAdd);
83 unsigned Enumerate(int index);
84 unsigned GetCount(int index);
85 void Copy(const UnsignedMap &src);
86 void MixIn(const UnsignedMap &src);
87 };
88
90 struct Profile
91 {
93 __int64 callCount;
94
96 __int64 tickPure;
97
99 __int64 tickTotal;
100
103
105 ProfileFuncLevelTracer *tracer;
106
107 void Copy(const Profile &src)
108 {
110 tickPure=src.tickPure;
112 caller.Copy(src.caller);
113 }
114
115 void MixIn(const Profile &src)
116 {
117 callCount+=src.callCount;
118 tickPure+=src.tickPure;
119 tickTotal+=src.tickTotal;
120 caller.MixIn(src.caller);
121 }
122 };
123
125 class ProfileMap
126 {
127 ProfileMap(const ProfileMap&);
128 ProfileMap& operator=(const ProfileMap&);
129
130 struct List
131 {
132 List *next;
133 int frame;
134 Profile p;
135 };
136
137 List *root,**tail;
138
139 public:
142
143 Profile *Find(int frame);
144 void Append(int frame, const Profile &p);
145 void MixIn(int frame, const Profile &p);
146 };
147
149 struct Function
150 {
152 unsigned addr;
153
156
159
162
164 int depth;
165
168
170 unsigned funcLine;
171
173 char *funcName;
174
175 Function(ProfileFuncLevelTracer *tr)
176 {
177 glob.tracer=tr;
178 for (int k=0;k<MAX_FRAME_RECORDS;k++)
179 cur[k].tracer=tr;
181 funcLine=0;
182 }
183 };
184
187
195 void Enter(unsigned addr, unsigned esp, unsigned ret);
196
205 unsigned Leave(unsigned esp);
206
210 static void Shutdown(void);
211
215 static int FrameStart(void);
216
220 static void FrameEnd(int which, int mixIndex);
221
225 static void ClearTotals(void);
226
232 static ProfileFuncLevelTracer *GetFirst(void)
233 {
234 return head;
235 }
236
242 ProfileFuncLevelTracer *GetNext(void)
243 {
244 return next;
245 }
246
247 Function *FindFunction(unsigned addr)
248 {
249 return func.Find(addr);
250 }
251
252 Function *EnumFunction(unsigned index)
253 {
254 return func.Enumerate(index);
255 }
256
257private:
259 struct StackEntry
260 {
262 Function *func;
263
265 unsigned esp;
266
268 unsigned retVal;
269
271 __int64 tickEnter;
272
274 __int64 tickSubTime;
275 };
276
278 class FunctionMap
279 {
280 FunctionMap(const FunctionMap&);
281 FunctionMap& operator=(const FunctionMap&);
282
283 struct Entry
284 {
285 Entry *next;
286 Function *funcPtr;
287 };
288
289 enum
290 {
291 HASH_SIZE = 15551
292 };
293
294 Entry *e;
295 unsigned alloc,used;
296 Entry *hash[HASH_SIZE];
297
298 public:
299 FunctionMap(void);
300 ~FunctionMap();
301
302 Function *Find(unsigned addr);
303 void Insert(Function *funcPtr);
304 Function *Enumerate(int index);
305 };
306
308 static ProfileFuncLevelTracer *head;
309
311 ProfileFuncLevelTracer *next;
312
314 static bool shuttingDown;
315
317 StackEntry *stack;
318
320 int usedStack;
321
323 int totalStack;
324
326 int maxDepth;
327
329 static int curFrame;
330
332 FunctionMap func;
333
335 static unsigned frameRecordMask;
336
338 static bool recordCaller;
339};
340
341inline void ProfileFuncLevelTracer::UnsignedMap::Insert(unsigned val, int countAdd)
342{
343 // in hash?
344 unsigned at=(val/16)%HASH_SIZE;
345 for (Entry *e=hash[at];e;e=e->next)
346 if (e->val==val)
347 {
348 e->count+=countAdd;
349 return;
350 }
351 _Insert(at,val,countAdd);
352}
353
354inline ProfileFuncLevelTracer::Function *ProfileFuncLevelTracer::FunctionMap::Find(unsigned addr)
355{
356 for (Entry *e=hash[(addr/16)%HASH_SIZE];e;e=e->next)
357 if (e->funcPtr->addr==addr)
358 return e->funcPtr;
359 return NULL;
360}
361
362#endif // INTERNAL_FUNCLEVEL_H
#define NULL
Definition BaseType.h:92
void Append(int frame, const Profile &p)
void MixIn(int frame, const Profile &p)
simple unique unsigned/unsigned map
void Insert(unsigned val, int countAdd)
void Copy(const UnsignedMap &src)
void MixIn(const UnsignedMap &src)
static ProfileFuncLevelTracer * GetFirst(void)
static int FrameStart(void)
ProfileFuncLevelTracer * GetNext(void)
unsigned Leave(unsigned esp)
static void FrameEnd(int which, int mixIndex)
Function * FindFunction(unsigned addr)
void Enter(unsigned addr, unsigned esp, unsigned ret)
static void ClearTotals(void)
Function * EnumFunction(unsigned index)
static void Shutdown(void)
unsigned funcPtr[1]
function entry (map address -> Function)
Function(ProfileFuncLevelTracer *tr)
ProfileMap frame
frame based profiles
unsigned funcLine
function source line
int depth
current call depth (for recursion)
Profile cur[MAX_FRAME_RECORDS]
profile for current frame(s)
unsigned addr
address of this function
ProfileFuncLevelTracer * tracer
tracer for this profile