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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
#include "filesystem_engine.h"
#include "filesystem.h"
#include "dt_instrumentation_server.h"
#include "dt_send.h"
#include "tier1/utlstring.h"
#include "utllinkedlist.h"
#include "dt.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
#define DELTA_DISTANCE_BAND 200
#define NUM_DELTA_DISTANCE_BANDS (8000/DELTA_DISTANCE_BAND)
// Data we track per SendTable on the server.
class CDTISendTable
{
public:
// Which SendTable we're interested in.
CUtlString m_NetTableName;
// How many cycles we've spent in certain calls.
CCycleCount m_nCalcDeltaCycles;
int m_nCalcDeltaCalls;
CCycleCount m_nEncodeCycles;
int m_nEncodeCalls;
CCycleCount m_nShouldTransmitCycles;
int m_nShouldTransmitCalls;
CCycleCount m_nWriteDeltaPropsCycles;
// Used to determine how much the class uses manual mode.
int m_nChangeAutoDetects;
int m_nNoChanges;
// Set to false if no events were recorded for this class.
bool HadAnyAction() const { return m_nCalcDeltaCalls || m_nEncodeCalls || m_nShouldTransmitCalls; }
// This tracks how many times an entity was delta'd for each distance from a client.
unsigned short m_DistanceDeltaCounts[NUM_DELTA_DISTANCE_BANDS];
};
static CCycleCount g_TotalServerDTICycles;
static CUtlLinkedList<CDTISendTable*, unsigned short> g_DTISendTables;
bool g_bServerDTIEnabled = false;
static char const *g_pServerDTIFilename = 0;
static bool g_bFirstHookTimer = true;
static CCycleCount g_ServerDTITimer;
void ServerDTI_Init( char const *pFilename )
{
g_pServerDTIFilename = pFilename;
g_bServerDTIEnabled = true;
g_TotalServerDTICycles.Init();
g_bFirstHookTimer = true;
}
void ServerDTI_Term()
{
if ( !g_pServerDTIFilename )
return;
ServerDTI_Flush();
g_DTISendTables.PurgeAndDeleteElements();
g_pServerDTIFilename = 0;
g_bServerDTIEnabled = false;
}
void ServerDTI_Flush()
{
if ( !g_pServerDTIFilename )
return;
CCycleCount curTime;
curTime.Sample();
CCycleCount runningTime;
CCycleCount::Sub( curTime, g_ServerDTITimer, runningTime );
// Write out a file that can be used by Excel.
FileHandle_t fp = g_pFileSystem->Open( g_pServerDTIFilename, "wt", "LOGDIR" );
if( fp != FILESYSTEM_INVALID_HANDLE )
{
// Write the header.
g_pFileSystem->FPrintf( fp,
"DTName"
"\tCalcDelta calls"
"\tCalcDelta ms"
"\tEncode calls"
"\tEncode ms"
"\tShouldTransmit calls"
"\tShouldTransmit ms"
"\tWriteDeltaProps ms"
"\t%% manual mode"
"\tTotal"
"\tPercent"
"\n"
);
// Calculate totals.
CCycleCount totalCalcDelta, totalEncode, totalShouldTransmit, totalDeltaProps;
totalCalcDelta.Init();
totalEncode.Init();
totalShouldTransmit.Init();
FOR_EACH_LL( g_DTISendTables, i )
{
CDTISendTable *pTable = g_DTISendTables[i];
CCycleCount::Add( pTable->m_nCalcDeltaCycles, totalCalcDelta, totalCalcDelta );
CCycleCount::Add( pTable->m_nEncodeCycles, totalEncode, totalEncode );
CCycleCount::Add( pTable->m_nShouldTransmitCycles, totalShouldTransmit, totalShouldTransmit );
CCycleCount::Add( pTable->m_nWriteDeltaPropsCycles, totalDeltaProps, totalDeltaProps );
}
FOR_EACH_LL( g_DTISendTables, j )
{
CDTISendTable *pTable = g_DTISendTables[j];
if ( !pTable->HadAnyAction() )
continue;
CCycleCount total;
CCycleCount::Add( pTable->m_nEncodeCycles, pTable->m_nCalcDeltaCycles, total );
CCycleCount::Add( pTable->m_nShouldTransmitCycles, total, total );
g_pFileSystem->FPrintf( fp,
"%s"
"\t%d"
"\t%.3f"
"\t%d"
"\t%.3f"
"\t%d"
"\t%.3f"
"\t%.3f"
"\t%.2f"
"\t%.3f"
"\t%.3f"
"\n",
pTable->m_NetTableName.String(),
pTable->m_nCalcDeltaCalls,
pTable->m_nCalcDeltaCycles.GetMillisecondsF(),
pTable->m_nEncodeCalls,
pTable->m_nEncodeCycles.GetMillisecondsF(),
pTable->m_nShouldTransmitCalls,
pTable->m_nShouldTransmitCycles.GetMillisecondsF(),
pTable->m_nWriteDeltaPropsCycles.GetMillisecondsF(),
(float)pTable->m_nNoChanges * 100.0f / (pTable->m_nNoChanges + pTable->m_nChangeAutoDetects),
total.GetMillisecondsF(),
total.GetMillisecondsF() * 100 / runningTime.GetMillisecondsF()
);
}
g_pFileSystem->FPrintf( fp, "\n\n" );
g_pFileSystem->FPrintf( fp,
"Total profile ms:"
"\t%.3f\n",
runningTime.GetMillisecondsF()
);
g_pFileSystem->FPrintf( fp,
"Total CalcDelta ms:"
"\t%.3f"
"\tPercent:"
"\t%.3f\n",
totalCalcDelta.GetMillisecondsF(),
totalCalcDelta.GetMillisecondsF() * 100.0 / runningTime.GetMillisecondsF()
);
g_pFileSystem->FPrintf( fp,
"Total Encode ms:"
"\t%.3f"
"\tPercent:"
"\t%.3f\n",
totalEncode.GetMillisecondsF(),
totalEncode.GetMillisecondsF() * 100.0 / runningTime.GetMillisecondsF()
);
g_pFileSystem->FPrintf( fp,
"Total ShouldTransmit ms:"
"\t%.3f"
"\tPercent:"
"\t%.3f\n",
totalShouldTransmit.GetMillisecondsF(),
totalShouldTransmit.GetMillisecondsF() * 100.0 / runningTime.GetMillisecondsF()
);
g_pFileSystem->FPrintf( fp,
"Total WriteDeltaProps ms:"
"\t%.3f"
"\tPercent:"
"\t%.3f\n",
totalDeltaProps.GetMillisecondsF(),
totalDeltaProps.GetMillisecondsF() * 100.0 / runningTime.GetMillisecondsF()
);
g_pFileSystem->Close( fp );
Msg( "DTI: Wrote delta distances into %s.\n", g_pServerDTIFilename );
}
// Write the delta distances.
const char *pDeltaDistancesFilename = "dti_delta_distances.txt";
fp = g_pFileSystem->Open( pDeltaDistancesFilename, "wt", "LOGDIR" );
if( fp != FILESYSTEM_INVALID_HANDLE )
{
// Write the column labels.
g_pFileSystem->FPrintf( fp, "ClassName" );
for ( int i=0; i < NUM_DELTA_DISTANCE_BANDS; i++ )
{
g_pFileSystem->FPrintf( fp, "\t<%d", (i+1) * DELTA_DISTANCE_BAND );
}
g_pFileSystem->FPrintf( fp, "\n" );
// Now write the data.
FOR_EACH_LL( g_DTISendTables, j )
{
CDTISendTable *pTable = g_DTISendTables[j];
if ( !pTable->HadAnyAction() )
continue;
g_pFileSystem->FPrintf( fp, "%s", pTable->m_NetTableName.String() );
for ( int i=0; i < NUM_DELTA_DISTANCE_BANDS; i++ )
{
g_pFileSystem->FPrintf( fp, "\t%d", pTable->m_DistanceDeltaCounts[i] );
}
g_pFileSystem->FPrintf( fp, "\n" );
}
g_pFileSystem->Close( fp );
Msg( "DTI: Wrote instrumentation data into %s.\n", pDeltaDistancesFilename );
}
}
CDTISendTable* ServerDTI_HookTable( SendTable *pTable )
{
if ( !g_bServerDTIEnabled )
return NULL;
CDTISendTable *pRet = new CDTISendTable;
memset( pRet, 0, sizeof( *pRet ) );
pRet->m_NetTableName.Set( pTable->m_pNetTableName );
g_DTISendTables.AddToTail( pRet );
return pRet;
}
void ServerDTI_AddEntityEncodeEvent( SendTable *pSendTable, float distToPlayer )
{
CSendTablePrecalc *pPrecalc = pSendTable->m_pPrecalc;
if ( !pPrecalc || !pPrecalc->m_pDTITable )
return;
CDTISendTable *pTable = pPrecalc->m_pDTITable;
if ( !pTable )
return;
int iDist = (int)( distToPlayer / DELTA_DISTANCE_BAND );
iDist = clamp( iDist, 0, NUM_DELTA_DISTANCE_BANDS - 1 );
pTable->m_DistanceDeltaCounts[iDist]++;
}
void _ServerDTI_HookTimer( const SendTable *pSendTable, ServerDTITimerType timerType, CCycleCount const &count )
{
CSendTablePrecalc *pPrecalc = pSendTable->m_pPrecalc;
if ( !pPrecalc || !pPrecalc->m_pDTITable )
return;
CDTISendTable *pTable = pPrecalc->m_pDTITable;
if ( g_bFirstHookTimer )
{
g_ServerDTITimer.Sample();
g_bFirstHookTimer = false;
}
// Add to the total cycles.
CCycleCount::Add( count, g_TotalServerDTICycles, g_TotalServerDTICycles );
if ( timerType == SERVERDTI_CALCDELTA )
{
CCycleCount::Add( count, pTable->m_nCalcDeltaCycles, pTable->m_nCalcDeltaCycles );
++pTable->m_nCalcDeltaCalls;
}
else if ( timerType == SERVERDTI_ENCODE )
{
CCycleCount::Add( count, pTable->m_nEncodeCycles, pTable->m_nEncodeCycles );
++pTable->m_nEncodeCalls;
}
else if ( timerType == SERVERDTI_SHOULDTRANSMIT )
{
CCycleCount::Add( count, pTable->m_nShouldTransmitCycles, pTable->m_nShouldTransmitCycles );
++pTable->m_nShouldTransmitCalls;
}
else if ( timerType == SERVERDTI_WRITE_DELTA_PROPS )
{
CCycleCount::Add( count, pTable->m_nWriteDeltaPropsCycles, pTable->m_nWriteDeltaPropsCycles );
}
}
void _ServerDTI_RegisterNetworkStateChange( SendTable *pSendTable, bool bStateChanged )
{
CSendTablePrecalc *pPrecalc = pSendTable->m_pPrecalc;
if ( !pPrecalc || !pPrecalc->m_pDTITable )
return;
CDTISendTable *pTable = pPrecalc->m_pDTITable;
if ( bStateChanged )
++pTable->m_nChangeAutoDetects;
else
++pTable->m_nNoChanges;
}
|