forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProfileInstrument.cpp
More file actions
514 lines (449 loc) · 16.1 KB
/
ProfileInstrument.cpp
File metadata and controls
514 lines (449 loc) · 16.1 KB
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include "CommonCorePch.h"
#ifdef PROFILE_EXEC
#include "core\ProfileInstrument.h"
#define HIRES_PROFILER
namespace Js
{
///----------------------------------------------------------------------------
///----------------------------------------------------------------------------
///
/// class Profiler::UnitData
///
///----------------------------------------------------------------------------
///----------------------------------------------------------------------------
///----------------------------------------------------------------------------
///
/// UnitData::UnitData
///
/// Constructor
///
///----------------------------------------------------------------------------
UnitData::UnitData()
{
this->incl = 0;
this->excl = 0;
this->max = 0;
this->count = 0;
}
///----------------------------------------------------------------------------
///
/// UnitData::Add
///
///----------------------------------------------------------------------------
void
UnitData::Add(TimeStamp incl, TimeStamp excl)
{
this->incl += incl;
this->excl += excl;
this->count++;
if (incl > this->max)
{
this->max = incl;
}
}
///----------------------------------------------------------------------------
///----------------------------------------------------------------------------
///
/// class Profiler
///
///----------------------------------------------------------------------------
///----------------------------------------------------------------------------
///----------------------------------------------------------------------------
///
/// Profiler::Profiler
///
/// Constructor
///
///----------------------------------------------------------------------------
Profiler::Profiler(ArenaAllocator * allocator) :
alloc(allocator),
rootNode(NULL)
{
this->curNode = &this->rootNode;
for(int i = 0; i < PhaseCount; i++)
{
this->inclSumAtLevel[i] = 0;
}
}
///----------------------------------------------------------------------------
///
/// Profiler::Begin
///
///----------------------------------------------------------------------------
void
Profiler::Begin(Phase tag)
{
Push(TimeEntry(tag, GetTime()));
}
///----------------------------------------------------------------------------
///
/// Profiler::End
///
///----------------------------------------------------------------------------
void
Profiler::End(Phase tag)
{
Pop(TimeEntry(tag, GetTime()));
}
void
Profiler::Suspend(Phase tag, SuspendRecord * suspendRecord)
{
suspendRecord->count = 0;
Phase topTag;
do
{
topTag = timeStack.Peek()->tag;
Pop(TimeEntry(topTag, GetTime()));
suspendRecord->phase[suspendRecord->count++] = topTag;
} while(topTag != tag);
}
void
Profiler::Resume(SuspendRecord * suspendRecord)
{
while (suspendRecord->count)
{
suspendRecord->count--;
Begin(suspendRecord->phase[suspendRecord->count]);
}
}
///----------------------------------------------------------------------------
///
/// Profiler::EndAllUpTo
///
/// Ends all phases up to the specified phase. Useful for catching exceptions
/// after a phase was started, and ending all intermediate phases until the
/// first phase that was started.
///
///----------------------------------------------------------------------------
void
Profiler::EndAllUpTo(Phase tag)
{
Phase topTag;
do
{
topTag = timeStack.Peek()->tag;
Pop(TimeEntry(topTag, GetTime()));
} while(topTag != tag);
}
///----------------------------------------------------------------------------
///
/// Profiler::Push
///
/// 1. Push entry on stack.
/// 2. Update curNode
///
///----------------------------------------------------------------------------
void
Profiler::Push(TimeEntry entry)
{
AssertMsg(NULL != curNode, "Profiler Stack Corruption");
this->timeStack.Push(entry);
if(!curNode->ChildExistsAt(entry.tag))
{
TypeNode * node = AnewNoThrow(this->alloc, TypeNode, curNode);
// We crash if we run out of memory here and we don't care
curNode->SetChildAt(entry.tag, node);
}
curNode = curNode->GetChildAt(entry.tag);
}
///----------------------------------------------------------------------------
///
/// Profiler::Pop
///
/// Core logic for the timer. Calculated the exclusive, inclusive times.
/// There is a list inclSumAtLevel which stores accumulates the inclusive sum
/// of all the tags that where 'pushed' after this tag.
///
/// Consider the following calls. fx indicates Push and fx', the corresponding Pop
///
/// f1
/// f2
/// f3
/// f3'
/// f2'
/// f4
/// f5
/// f5'
/// f4'
/// f1'
///
/// calculating the inclusive times are trivial. Let us calculate the exclusive
/// time for f1. That would be
/// excl(f1) = incl(f1) - [incl(f2) + incl(f4)]
///
/// Basically if a function is at level 'x' then we need to deduct from its
/// exclusive times, the inclusive times of all the functions at level 'x + 1'
/// We don't care about deeper levels. Hence 'inclSumAtLevel' array which accumulates
/// the sum of variables at different levels.
///
/// Reseting the next level is also required. In the above example, f3 and f5 are
/// at the same level. if we don't reset level 3 when popping f2, then we will
/// have wrong sums for f4. So once a tag has been popped, all sums at its higher
/// levels is set to zero. (Of course we just need to reset the next level and
/// all above levels will invariably remain zero)
///
///----------------------------------------------------------------------------
void
Profiler::Pop(TimeEntry curEntry)
{
int curLevel = this->timeStack.Count();
TimeEntry *entry = this->timeStack.Pop();
AssertMsg(entry->tag == curEntry.tag, "Profiler Stack corruption, push pop entries do not correspond to the same tag");
TimeStamp inclusive = curEntry.time - entry->time;
TimeStamp exclusive = inclusive - this->inclSumAtLevel[curLevel +1];
Assert(inclusive >= 0);
Assert(exclusive >= 0);
this->inclSumAtLevel[curLevel + 1] = 0;
this->inclSumAtLevel[curLevel] += inclusive;
curNode->GetValue()->Add(inclusive, exclusive);
curNode = curNode->GetParent();
AssertMsg(curNode != NULL, "Profiler stack corruption");
}
void
Profiler::Merge(Profiler * profiler)
{
MergeTree(&rootNode, &profiler->rootNode);
if (profiler->timeStack.Count() > 1)
{
FixedStack<TimeEntry, MaxStackDepth> reverseStack;
do
{
reverseStack.Push(*profiler->timeStack.Pop());
}
while (profiler->timeStack.Count() > 1);
do
{
TimeEntry * entry = reverseStack.Pop();
this->Push(*entry);
profiler->timeStack.Push(*entry);
}
while (reverseStack.Count() != 0);
}
}
void
Profiler::MergeTree(TypeNode * toNode, TypeNode * fromNode)
{
UnitData * toData = toNode->GetValue();
const UnitData * fromData = fromNode->GetValue();
toData->count += fromData->count;
toData->incl += fromData->incl;
toData->excl += fromData->excl;
if (fromData->max > toData->max)
{
toData->max = fromData->max;
}
for (int i = 0; i < PhaseCount; i++)
{
if (fromNode->ChildExistsAt(i))
{
TypeNode * fromChild = fromNode->GetChildAt(i);
TypeNode * toChild;
if (!toNode->ChildExistsAt(i))
{
toChild = Anew(this->alloc, TypeNode, toNode);
toNode->SetChildAt(i, toChild);
}
else
{
toChild = toNode->GetChildAt(i);
}
MergeTree(toChild, fromChild);
}
}
}
///----------------------------------------------------------------------------
///
/// Profiler::PrintTree
///
/// Private method that walks the tree and prints it recursively.
///
///----------------------------------------------------------------------------
void
Profiler::PrintTree(TypeNode *node, TypeNode *baseNode, int column, TimeStamp freq)
{
const UnitData *base = baseNode->GetValue();
for(int i = 0; i < PhaseCount; i++)
{
if(node->ChildExistsAt(i))
{
UnitData *data = node->GetChildAt(i)->GetValue();
#ifdef ENABLE_DEBUG_CONFIG_OPTIONS
if( int(data->incl * 100 / base->incl) >= Configuration::Global.flags.ProfileThreshold) // threshold
#endif
{
Output::SkipToColumn(column);
Output::Print(L"%-*s %7.1f %5d %7.1f %5d %7.1f %7.1f %5d\n",
(Profiler::PhaseNameWidth-column), PhaseNames[i],
(double)data->incl / freq , // incl
int(data->incl * 100 / base->incl ), // incl %
(double)data->excl / freq , // excl
int(data->excl * 100 / base->incl ), // excl %
(double)data->max / freq , // max
(double)data->incl / ( freq * data->count ), // mean
int(data->count) // count
);
}
PrintTree(node->GetChildAt(i), baseNode, column + Profiler::TabWidth, freq);
}
}
}
///----------------------------------------------------------------------------
///
/// Profiler::Print
///
/// Pretty printer
///
///----------------------------------------------------------------------------
void
Profiler::Print(Phase baseTag)
{
if (baseTag == InvalidPhase)
{
baseTag = AllPhase; // default to all phase
}
const TimeStamp freq = this->GetFrequency();
bool foundNode = false;
ForEachNode(baseTag, &rootNode, [&](TypeNode *const baseNode, const Phase parentTag)
{
if(!foundNode)
{
foundNode = true;
Output::Print(L"%-*s:%7s %5s %7s %5s %7s %7s %5s\n",
(Profiler::PhaseNameWidth-0),
L"Profiler Report",
L"Incl",
L"(%)",
L"Excl",
L"(%)",
L"Max",
L"Mean",
L"Count"
);
Output::Print(L"-------------------------------------------------------------------------------\n");
}
UnitData *data = baseNode->GetValue();
if(0 == data->count)
{
Output::Print(L"The phase : %s was never started", PhaseNames[baseTag]);
return;
}
int indent = 0;
if(parentTag != InvalidPhase)
{
TypeNode *const parentNode = baseNode->GetParent();
Assert(parentNode);
Output::Print(L"%-*s\n", (Profiler::PhaseNameWidth-0), PhaseNames[parentTag]);
indent += Profiler::TabWidth;
}
if(indent)
{
Output::SkipToColumn(indent);
}
Output::Print(L"%-*s %7.1f %5d %7.1f %5d %7.1f %7.1f %5d\n",
(Profiler::PhaseNameWidth-indent),
PhaseNames[baseTag],
(double)data->incl / freq , // incl
int(100), // incl %
(double)data->excl / freq , // excl
int(data->excl * 100 / data->incl ), // excl %
(double)data->max / freq , // max
(double)data->incl / ( freq * data->count ),// mean
int(data->count) // count
);
indent += Profiler::TabWidth;
PrintTree(baseNode, baseNode, indent, freq);
});
if(foundNode)
{
Output::Print(L"-------------------------------------------------------------------------------\n");
Output::Flush();
}
}
///----------------------------------------------------------------------------
///
/// Profiler::FindNode
///
/// Does a tree traversal(DFS) and finds the first occurrence of the 'tag'
///
///----------------------------------------------------------------------------
template<class FVisit>
void
Profiler::ForEachNode(Phase tag, TypeNode *node, FVisit visit, Phase parentTag)
{
AssertMsg(node != NULL, "Invalid usage: node must always be non null");
for(int i = 0; i < PhaseCount; i++)
{
if(node->ChildExistsAt(i))
{
TypeNode * child = node->GetChildAt(i);
if(i == tag)
{
visit(child, parentTag);
}
else
{
ForEachNode(tag, child, visit, static_cast<Phase>(i));
}
}
}
}
///----------------------------------------------------------------------------
///
/// Profiler::GetTime
///
///----------------------------------------------------------------------------
TimeStamp
Profiler::GetTime()
{
#if !defined HIRES_PROFILER && (defined(_M_IX86) || defined(_M_X64))
return __rdtsc();
#else
LARGE_INTEGER tmp;
if(QueryPerformanceCounter(&tmp))
{
return tmp.QuadPart;
}
else
{
AssertMsg(0, "Could not get time. Don't know what to do");
return 0;
}
#endif
}
///----------------------------------------------------------------------------
///
/// Profiler::GetFrequency
///
///----------------------------------------------------------------------------
TimeStamp
Profiler::GetFrequency()
{
#if !defined HIRES_PROFILER && (defined(_M_IX86) || defined(_M_X64))
long long start, end;
int CPUInfo[4];
// Flush pipeline
__cpuid(CPUInfo, 0);
// Measure 1 second / 5
start = GetTime();
Sleep(1000/5);
end = GetTime();
return ((end - start) * 5) / FrequencyScale;
#else
LARGE_INTEGER tmp;
if(QueryPerformanceFrequency(&tmp))
{
return tmp.QuadPart / FrequencyScale;
}
else
{
AssertMsg(0, "Could not get time. Don't know what to do");
return 0;
}
#endif
}
} //namespace Js
#endif