-
Notifications
You must be signed in to change notification settings - Fork 1
/
CodeCheckpointDebugMgr.cpp
450 lines (361 loc) · 15.3 KB
/
CodeCheckpointDebugMgr.cpp
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
////////////////////////////////////////////////////////////////////////////
//
// Crytek Engine Source File.
// Copyright (C), Crytek Studios, 2010.
// -------------------------------------------------------------------------
// Created: 06/30/2010 by Morgan K
// Description: A debug class to track and output code checkpoint state
// -------------------------------------------------------------------------
// History: Created by Morgan Kita
//
////////////////////////////////////////////////////////////////////////////
#include "StdAfx.h"
#include "GameCVars.h"
#include "CodeCheckpointDebugMgr.h"
const static int LABEL_LENGTH = 100;
const static int BUFF_SIZE = LABEL_LENGTH + 3;
CCodeCheckpointDebugMgr* s_pCodeCheckPointDebugManager = NULL;
bool SortDebugRecord( const CCodeCheckpointDebugMgr::CheckpointDebugRecord& rec1, const CCodeCheckpointDebugMgr::CheckpointDebugRecord& rec2 )
{
return rec1.m_lastHitTime > rec2.m_lastHitTime;
}
void CmdCodeCheckPointSearch(IConsoleCmdArgs *pArgs)
{
if(pArgs->GetArgCount() > 2)
CryLogAlways("Usage: %s <substring>", pArgs->GetArg(0));
CCodeCheckpointDebugMgr::RecordNameCountPairs foundPointNames;
string searchString;
if (pArgs->GetArgCount() == 2)
searchString = pArgs->GetArg(1);
CCodeCheckpointDebugMgr::RetrieveCodeCheckpointDebugMgr()->SearchCheckpoints(foundPointNames, searchString);
CryLogAlways("Search with substring:%s return the following %i hit checkpoints", searchString.c_str(), (int32)foundPointNames.size());
for(CCodeCheckpointDebugMgr::RecordNameCountPairs::iterator fIt = foundPointNames.begin(); fIt != foundPointNames.end(); ++fIt)
CryLogAlways("Checkpoint:%s Count:%i", fIt->first.c_str(), fIt->second);
}
CCodeCheckpointDebugMgr* CCodeCheckpointDebugMgr::s_pCodeCheckPointDebugManager = NULL;
CCodeCheckpointDebugMgr* CCodeCheckpointDebugMgr::RetrieveCodeCheckpointDebugMgr()
{
if(!s_pCodeCheckPointDebugManager)
s_pCodeCheckPointDebugManager = new CCodeCheckpointDebugMgr();
return s_pCodeCheckPointDebugManager;
}
CCodeCheckpointDebugMgr::CCodeCheckpointDebugMgr()
:REGISTER_GAME_MECHANISM(CCodeCheckpointDebugMgr),m_timeSinceLastRun(0)
{
m_debug_ccoverage = 0;
m_debug_ccoverage_rate = 0.05f;
m_debug_ccoverage_maxlines = 10;
m_debug_ccoverage_filter_maxcount = 0;
m_debug_ccoverage_filter_mincount = 0;
IConsole * pConsole = GetISystem()->GetIConsole();
assert (pConsole);
pConsole->AddCommand("ft_debug_checkpoint_search", CmdCodeCheckPointSearch, VF_CHEAT, "FEATURE TESTER: Search for code checkpoints that have been encountered by substring");
pConsole->Register("ft_debug_ccoverage", &m_debug_ccoverage, m_debug_ccoverage, VF_CHEAT, "FEATURE TESTER: Turn on debug drawing of code checkpoints. 1 = watched checkpoints only. 2 = unwatched only. 3 = both with watched higher priority. 4 = both equal prioirity");
pConsole->Register("ft_debug_ccoverage_rate", &m_debug_ccoverage_rate, m_debug_ccoverage_rate, VF_CHEAT, "FEATURE TESTER: Max number of code checkpoints to output");
pConsole->Register("ft_debug_ccoverage_maxlines", &m_debug_ccoverage_maxlines, m_debug_ccoverage_maxlines, VF_CHEAT, "FEATURE TESTER: Max number of code checkpoints to output");
pConsole->Register("ft_debug_ccoverage_filter_maxcount", &m_debug_ccoverage_filter_maxcount, m_debug_ccoverage_filter_maxcount, VF_CHEAT, "FEATURE TEST: Only print out checkpoints with less than this number of hits");
pConsole->Register("ft_debug_ccoverage_filter_mincount", &m_debug_ccoverage_filter_mincount, m_debug_ccoverage_filter_mincount, VF_CHEAT, "FEATURE TEST: Only print out checkpoints with more than this number of hit");
string filePath = PathUtil::Make( "../USER", "CodeCheckpointList.txt" );
ReadFile(filePath.c_str());
}
/// Searches all registered check points for the given substring and returns their counts in the input list
void CCodeCheckpointDebugMgr::SearchCheckpoints(RecordNameCountPairs& outputList, string& searchStr) const
{
SearchRecordForString searchFunc(outputList, searchStr);
std::for_each(m_watchedPoints.begin(), m_watchedPoints.end(), searchFunc);
std::for_each(m_unwatchedPoints.begin(), m_unwatchedPoints.end(), searchFunc);
}
void CCodeCheckpointDebugMgr::ReadFile(const char* fileName)
{
FILE * cpFile = gEnv->pCryPak->FOpen(fileName, "r");
if(cpFile)
{
// Temporary buffer
//char cBuffer[BUFF_SIZE];
char* lineBlock = new char[BUFF_SIZE + 1];
//char* appendBlock = lineBlock;
while(int numRead = GetLine(lineBlock, cpFile))
{
ICodeCheckpointMgr* pCheckpointManager = gEnv->pCodeCheckpointMgr;
if(pCheckpointManager)
pCheckpointManager->GetCheckpointIndex(lineBlock);
RegisterWatchPoint(lineBlock);
}
SAFE_DELETE_ARRAY(lineBlock);
gEnv->pCryPak->FClose(cpFile);
}
}
///Getline wrapper to break on newlines as adapted from the old AI Code Coverage Manager
int CCodeCheckpointDebugMgr::GetLine( char * pBuff, FILE * fp )
{
CRY_ASSERT(pBuff && fp);
/// Wraps fgets to remove newlines and use correct buffer/string lengths
/// Must use CryPak FGets on CryPak files
/// Will retrieve up to the next newline character in the file buffer.
if (!gEnv->pCryPak->FGets( pBuff, BUFF_SIZE, fp ))
return 0;
/// Convert newlines to \0 and discover length
int i=0;
int nLimit = LABEL_LENGTH + 1;
for (; i < nLimit ; i++)
{
char c = pBuff[i];
if (c == '\n' || c == '\r' || c== '\0')
break;
}
/// If i == LABEL_LENGTH, the string was of maximum length
/// If i == LABEL_LENGTH + 1, the string was too long
if (i == nLimit)
{
// Malformed - fail
pBuff[--i] = '\0';
return 0;
}
/// Overwrite the last character (usually line terminator) with string delimiter
pBuff[i] = '\0';
if ( i == 0 )
return 0;
return i+1;
}
void CCodeCheckpointDebugMgr::Update(float dt)
{
//Don't do any work if we haven't requested this as active
if(!m_debug_ccoverage)
return;
m_timeSinceLastRun += dt;
if( m_timeSinceLastRun > m_debug_ccoverage_rate)
{
UpdateRecords();
m_timeSinceLastRun = 0;
}
DrawDebugInfo();
}
void CCodeCheckpointDebugMgr::DrawDebugInfo()
{
int displayLevel = m_debug_ccoverage;
IRenderer* pRenderer = gEnv->pRenderer;
int totalHit = (int) std::count_if(m_unwatchedPoints.begin(), m_unwatchedPoints.end(), RecordHasHits);
int watchedHit = (int) std::count_if(m_watchedPoints.begin(), m_watchedPoints.end(), RecordHasHits);
totalHit += watchedHit;
TCheckpointDebugVector outputPoints;
outputPoints.reserve(m_watchedPoints.size() + m_unwatchedPoints.size());
//Get the sorted output points. For now assume you want to sort based on time
//Only show watched
if(displayLevel == 1)
{
outputPoints.assign(m_watchedPoints.begin(), m_watchedPoints.end());
std::sort(outputPoints.begin(), outputPoints.end(), SortDebugRecord);
}
//Only show unwatched
else if(displayLevel == 2)
{
outputPoints.assign(m_unwatchedPoints.begin(), m_unwatchedPoints.end());
std::sort(outputPoints.begin(), outputPoints.end(), SortDebugRecord);
}
//Show both with watched having priority
else if(displayLevel == 3)
{
///Retrieve and sort watched
TCheckpointDebugVector watchedPts;
watchedPts.assign(m_watchedPoints.begin(), m_watchedPoints.end());
std::sort(watchedPts.begin(), watchedPts.end(), SortDebugRecord);
///Retrieve and sort unwatched
TCheckpointDebugVector unwatchedPts;
unwatchedPts.assign(m_unwatchedPoints.begin(), m_unwatchedPoints.end());
std::sort(unwatchedPts.begin(), unwatchedPts.end(), SortDebugRecord);
///Combined with watched first
outputPoints.assign(watchedPts.begin(), watchedPts.end());
outputPoints.insert(outputPoints.end(),unwatchedPts.begin(), unwatchedPts.end());
}
//Show both with equal priority
else if(displayLevel == 4)
{
///Combine sort
outputPoints.assign(m_unwatchedPoints.begin(), m_unwatchedPoints.end());
outputPoints.insert(outputPoints.end(), m_watchedPoints.begin(), m_watchedPoints.end());
std::sort(outputPoints.begin(), outputPoints.end(), SortDebugRecord);
}
float percHit = 0.0f;
if(!m_watchedPoints.empty())
percHit = (float) watchedHit / m_watchedPoints.size();
static float statusColor[] = {1.0f, 1.0f, 1.0f, 1.0f};
float height = (float) 0.05 * pRenderer->GetHeight();
pRenderer->Draw2dLabel(30.f, height, 2.f, statusColor, false,
"THit: %i | TWatched: %i | WatchedHit: %i | %% WatchedHit: %.2f",
totalHit, (int32)m_watchedPoints.size(), watchedHit, percHit * 100.0f );
//Output the interesting lines
float outputOffset = 0.08f;
int numberOutput = 0;
int maxNumberOutput = m_debug_ccoverage_maxlines;
for(TCheckpointDebugVector::iterator outputIt = outputPoints.begin();
outputIt != outputPoints.end() && numberOutput < maxNumberOutput;
++outputIt)
{
static float watchedColor[] = {1.0f, 0.0f, 1.0f, 1.0f};
static float unwatchedColor[] = {0.0f, 0.0f, 1.0f, 1.0f};
//Check filters and skip outputting of ones that don't qualify
int filterMin = m_debug_ccoverage_filter_mincount, filterMax = m_debug_ccoverage_filter_maxcount;
if(filterMax && (int)outputIt->m_currHitcount > filterMax)
continue;
else if(filterMin && (int)outputIt->m_currHitcount < filterMin)
continue;
pRenderer->Draw2dLabel(30.f, outputOffset * pRenderer->GetHeight(), 2.f, outputIt->m_queried? watchedColor : unwatchedColor, false,
"CheckPoint: %s Count:%i", outputIt->m_name.c_str(), outputIt->m_currHitcount);
//Update the display output height
outputOffset += 0.03f;
++numberOutput;
}
}
///Register name to be marked as a watch point
void CCodeCheckpointDebugMgr::RegisterWatchPoint(const string& name)
{
//Check if its already watched, if so just increment
TCheckpointDebugList::iterator currentLocation = std::find_if(m_watchedPoints.begin(), m_watchedPoints.end(), RecordMatchAndUpdate(name));
bool recordFound = currentLocation != m_watchedPoints.end();
//Check unwatched
if(currentLocation == m_watchedPoints.end())
{
currentLocation = std::find_if(m_unwatchedPoints.begin(), m_unwatchedPoints.end(), RecordMatchAndUpdate(name));
if(currentLocation != m_unwatchedPoints.end())
{
recordFound = true;
//Move this record to the appropriate watched location
TCheckpointDebugList::iterator currwIt = m_watchedPoints.begin();
for(;currwIt != m_watchedPoints.end(); ++currwIt)
{
if(currentLocation->m_checkPointIdx < currwIt->m_checkPointIdx)
{
currentLocation->UpdateWatched();
m_watchedPoints.insert(currwIt,*currentLocation);
break;
}
}
if(currwIt == m_watchedPoints.end())
{
currentLocation->UpdateWatched();
m_watchedPoints.push_back(*currentLocation);
}
m_unwatchedPoints.erase(currentLocation);
}
}
//Point hasn't been reached yet so bookmark it
if(!recordFound)
{
//Check if bookmark exists, if so update the count of requests for it
TBookmarkMap::iterator currBookmark = m_bookmarkedNames.find(name);
if(currBookmark != m_bookmarkedNames.end())
++currBookmark->second;
else
m_bookmarkedNames.insert(std::make_pair(name, 1));
}
}
///Unregister name to be marked as a watch point
void CCodeCheckpointDebugMgr::UnregisterWatchPoint(const string& name)
{
TCheckpointDebugList::iterator currentLocation = m_watchedPoints.end();
//Check for an existing record in the watched points and if found decrement its refcount of watchers
currentLocation = std::find_if(m_watchedPoints.begin(), m_watchedPoints.end(), RecordMatchAndUpdate(name, -1));
if(currentLocation != m_watchedPoints.end())
{
//If there is no longer a reference to this checkpoint then move it back to the unwatched at the appropriate spot.
if(currentLocation->m_refCount <= 0)
{
TCheckpointDebugList::iterator currwIt = m_unwatchedPoints.begin();
for(;currwIt != m_unwatchedPoints.end(); ++currwIt)
{
if(currentLocation->m_checkPointIdx < currwIt->m_checkPointIdx)
{
m_unwatchedPoints.insert(currwIt,*currentLocation);
break;
}
}
if(currwIt == m_unwatchedPoints.end())
m_unwatchedPoints.push_back(*currentLocation);
m_watchedPoints.erase(currentLocation);
}
}
///If we haven't yet reached the checkpoint somehow then check the bookmarks to see if one was waiting
if(currentLocation == m_watchedPoints.end())
{
///Check if bookmark exists, if so update the count of requests for it
TBookmarkMap::iterator currBookmark = m_bookmarkedNames.find(name);
if(currBookmark != m_bookmarkedNames.end())
{
--currBookmark->second;
if(currBookmark->second <= 0)
m_bookmarkedNames.erase(currBookmark);
}
else
///Shouldn't be able to request to unregister a watch point that is not currently registered
CRY_ASSERT(false);
}
}
void CCodeCheckpointDebugMgr::UpdateRecord(const CCodeCheckpoint* pCheckpoint,CheckpointDebugRecord& record )
{
if(!pCheckpoint)
return;
//Update the existing record
if(record.m_prevHitcount < pCheckpoint->HitCount())
{
record.m_prevHitcount = record.m_currHitcount;
record.m_currHitcount = pCheckpoint->HitCount();
record.m_lastHitTime = gEnv->pTimer->GetCurrTime();
}
}
void CCodeCheckpointDebugMgr::UpdateRecords()
{
//Retrieve the latest snapshot
ICodeCheckpointMgr* pCodeCheckpointMgr = gEnv->pCodeCheckpointMgr;
if (pCodeCheckpointMgr)
{
size_t nextSnapshotCount = pCodeCheckpointMgr->GetTotalCount();
size_t currIdx = 0;
TCheckpointDebugList::iterator unwatchedIt = m_unwatchedPoints.begin(),watchedIt = m_watchedPoints.begin();
///Update existing points with their snapshot
size_t currSnapShotcount = m_watchedPoints.size() + m_unwatchedPoints.size();
while( currIdx < currSnapShotcount && currIdx < nextSnapshotCount)
{
string name = pCodeCheckpointMgr->GetCheckPointName(currIdx);
const CCodeCheckpoint* pCheckpoint = pCodeCheckpointMgr->GetCheckpoint(currIdx);
///Check if we are a watched point
if(watchedIt != m_watchedPoints.end() && (name == watchedIt->m_name))
{
CheckpointDebugRecord& currentRec = *watchedIt;
UpdateRecord(pCheckpoint, currentRec);
++watchedIt;
}
//Otherwise checked if we are unwatched point
else if(unwatchedIt != m_unwatchedPoints.end() && (name == unwatchedIt->m_name))
{
CheckpointDebugRecord& currentRec = *unwatchedIt;
UpdateRecord(pCheckpoint, currentRec);
++unwatchedIt;
}
///This should never happen, as each existing point should already have been inserted into either the watched or unwatched vector.
else
{
CRY_ASSERT(false);
}
++currIdx;
}
///Add entities that are new to this snapshot
while(currIdx < nextSnapshotCount)
{
const char* name = pCodeCheckpointMgr->GetCheckPointName(currIdx);
const CCodeCheckpoint* pCheckpoint = pCodeCheckpointMgr->GetCheckpoint(currIdx);
CheckpointDebugRecord newRecord(pCheckpoint, name, currIdx);
//If we have a current request to watch this point, make sure it goes into watched points
TBookmarkMap::iterator bookmarkedIt = m_bookmarkedNames.find(name);
if(bookmarkedIt != m_bookmarkedNames.end())
{
newRecord.UpdateWatched(bookmarkedIt->second);
m_watchedPoints.push_back(newRecord);
//Clean up the bookmark as its already been hit
m_bookmarkedNames.erase(bookmarkedIt);
}
else
m_unwatchedPoints.push_back(newRecord);
++currIdx;
}
}
}