-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathGpProf.pas
More file actions
749 lines (679 loc) · 20.2 KB
/
Copy pathGpProf.pas
File metadata and controls
749 lines (679 loc) · 20.2 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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
{$R-,C-,Q-,O+,H+}
unit GpProf;
interface
{$WARN SYMBOL_PLATFORM OFF}
{$WARN SYMBOL_DEPRECATED OFF}
{$INCLUDE GpProf.inc}
(******************************************************************************)
uses
GpProfCommonTypes;
type
/// <summary>
/// Marker interface for a measure point scope. upon destruction, the scope will be stored.
/// </summary>
IMeasurePointScope = interface
end;
procedure ProfilerStart;
procedure ProfilerStop;
procedure ProfilerStartThread;
procedure ProfilerEnterProc(const aProcID: Cardinal);
procedure ProfilerExitProc(const aProcID: Cardinal);
procedure ProfilerEnterMP(const aMeasurePointId: UTF8String);
procedure ProfilerExitMP(const aMeasurePointId: UTF8String);
function CreateMeasurePointScope(const aMeasurePointId: string): IMeasurePointScope;
procedure ProfilerTerminate;
{$IFDEF UNICODE}
procedure NameThreadForDebugging(const AThreadName: AnsiString; AThreadID: TThreadID = TThreadID(-1)); overload; inline;
procedure NameThreadForDebugging(const AThreadName: string; AThreadID: TThreadID = TThreadID(-1)); overload;
{$ELSE}
procedure NameThreadForDebugging(const AThreadName: string; AThreadID: TThreadID = TThreadID(-1));
{$ENDIF}
implementation
uses
Windows,
Classes,
Contnrs,
SysUtils,
IniFiles,
GpProfH,
GpProfLists,
GpProfCommon;
const
BUF_SIZE_DEFAULT = 64 * 1024; // 64 KB
type
TMeasurePointScope = class(TInterfacedObject, IMeasurePointScope)
private
fMeasurePointId : UTF8String;
public
constructor Create(const aMeasurePointId : UTF8String);
destructor Destroy; override;
end;
var
prfFile : THandle;
prfBuf : pointer;
prfBufOffs : integer;
prfLock : TRTLCriticalSection;
prfFreq : TLargeInteger;
prfCounter : TLargeInteger;
prfModuleName : string;
prfName : string;
prfRunning : boolean;
prfLastTick : TLargeInteger;
prfOnlyThread : Cardinal;
prfThreads : TThreadIdList;
prfThreadsInfo : TThreadInformationList;
prfThreadBytes : integer;
prfMaxThreadNum: Cardinal;
prfMeasurePoints: TMeasurePointList;
prfMeasurePointBytes : integer;
prfMaxMeasurePointNum: Cardinal;
prfInitialized : boolean;
prfDisabled : boolean;
profProcSize : integer;
profCompressTicks : boolean;
profCompressThreads : boolean;
profCompressMeasurePoints: boolean;
profProfilingAutostart: boolean;
profProfilingMemoryEnabled: boolean;
profPrfOutputFile : string;
profTableName : string;
profBufSize : integer = BUF_SIZE_DEFAULT;
procedure FlushFile; {$IFDEF HAS_INLINE}inline;{$ENDIF}
var
written: DWORD;
begin
if WriteFile(prfFile, prfBuf^, prfBufOffs, written, nil) then begin
prfBufOffs := 0;
end else
RaiseLastWin32Error;
end; { FlushFile }
function OffsetPtr(ptr: Pointer; offset: NativeUInt): Pointer; {$IFDEF HAS_INLINE}inline;{$ENDIF}
begin
Result := Pointer(NativeUInt(ptr) + offset);
end; { OffsetPtr }
procedure Transmit(const buf; count: integer);
var
bufp : pointer;
place : integer;
written: DWORD;
begin
place := profBufSize - prfBufOffs;
if place <= count then begin
Move(buf,OffsetPtr(prfBuf,prfBufOffs)^,place); // fill the buffer
prfBufOffs := profBufSize;
FlushFile;
Dec(count,place);
bufp := OffsetPtr(@buf,place);
if count >= profBufSize then begin
if WriteFile(prfFile, bufp^, count, written, nil) then begin
count := 0;
end else
RaiseLastWin32Error;
end;
end else
bufp := @buf;
if count > 0 then begin // store leftovers
Move(bufp^,OffsetPtr(prfBuf,prfBufOffs)^,count);
Inc(prfBufOffs,count);
end;
end; { Transmit }
{$IFDEF HAS_MEMORY_MANAGER_STATE}
function GetMemWorkingSize() : Cardinal;
var
i: Integer;
LState: TMemoryManagerState;
LSmallBlocks: TSmallBlockTypeStates;
LSmallBlock: ^TSmallBlockTypeState;
begin
Result := 0;
GetMemoryManagerState(LState);
LSmallBlocks := LState.SmallBlockTypeStates;
for i := Low(LSmallBlocks) to High(LSmallBlocks) do
begin
LSmallBlock := @LSmallBlocks[i];
Inc(Result,LSmallBlock.AllocatedBlockCount*LSmallBlock.UseableBlockSize);
end;
Inc(Result,LState.TotalAllocatedMediumBlockSize);
Inc(Result,LState.TotalAllocatedLargeBlockSize);
end;
{$ELSE}
function GetMemWorkingSize() : Cardinal;
begin
// AllocMemSize is updated by the heap lock on every GetMem/FreeMem call
// Reading an aligned 32-bit Cardinal is atomic on x86, so no torn reads occur
Result := System.AllocMemSize;
end;
{$ENDIF}
procedure WriteMemWorkingSize(); {$IFDEF HAS_INLINE}inline;{$ENDIF}
var
lMemUsed : Cardinal;
begin
lMemUsed := GetMemWorkingSize();
Transmit(lMemUsed, sizeof(Cardinal));
end;
procedure WriteInt(int: integer); {$IFDEF HAS_INLINE}inline;{$ENDIF}
begin
Transmit(int, SizeOf(integer));
end;
procedure WriteCardinal(value: Cardinal); {$IFDEF HAS_INLINE}inline;{$ENDIF}
begin
Transmit(value, SizeOf(Cardinal));
end;
procedure WriteTag(tag: byte); {$IFDEF HAS_INLINE}inline;{$ENDIF}
begin
Transmit(tag, SizeOf(byte));
end;
procedure WriteProcID(id: integer); {$IFDEF HAS_INLINE}inline;{$ENDIF}
begin
Transmit(id, profProcSize);
end;
procedure WriteBool(bool: boolean); {$IFDEF HAS_INLINE}inline;{$ENDIF}
begin
Transmit(bool, 1);
end;
procedure WriteUtf8String(const aValue: UTF8String); {$IFDEF HAS_INLINE}inline;{$ENDIF}
var
len: integer;
begin
len := Length(aValue);
WriteCardinal(len);
if len > 0 then
Transmit(aValue[1], len);
end;
procedure WriteTicks(ticks: TLargeInteger);
type
TTick = array [1..8] of Byte;
var
diff: integer;
begin
if not profCompressTicks then Transmit(ticks, Sizeof(ticks))
else begin
if prfLastTick = -1 then diff := 8
else begin
diff := 8;
while (diff > 0) and (TTick(ticks)[diff] = TTick(prfLastTick)[diff]) do
Dec(diff);
Inc(diff);
end;
Transmit(diff, 1);
Transmit(ticks, diff);
prfLastTick := ticks;
end;
end; { WriteTicks }
procedure WriteThread(const aThreadId: Cardinal);
const
marker: integer = 0;
var
remap: integer;
begin
if not profCompressThreads then Transmit(aThreadId, Sizeof(integer))
else begin
remap := prfThreads.Remap(aThreadId);
if prfThreads.Count >= prfMaxThreadNum then begin
Transmit(marker, prfThreadBytes);
prfMaxThreadNum := 2 * prfMaxThreadNum;
prfThreadBytes := prfThreadBytes + 1;
end;
Transmit(remap, prfThreadBytes);
end;
end; { WriteThread }
procedure WriteMeasurePoint(const aId: UTF8String; const aRemap, aCount: integer);
const
marker: integer = 0;
begin
if not profCompressMeasurePoints then WriteUtf8String(aId)
else begin
if Cardinal(aCount) >= prfMaxMeasurePointNum then begin
Transmit(marker, prfMeasurePointBytes);
prfMaxMeasurePointNum := 256 * prfMaxMeasurePointNum;
Inc(prfMeasurePointBytes);
end;
Transmit(aRemap, prfMeasurePointBytes);
end;
end;
procedure FlushCounter; {$IFDEF HAS_INLINE}inline;{$ENDIF}
begin
if prfCounter <> 0 then begin
WriteTicks(prfCounter);
prfCounter := 0;
end;
end; { FlushCounter }
procedure ProfilerEnterProc(const aProcID : Cardinal);
var
ct : Cardinal;
cnt: TLargeInteger;
begin
QueryPerformanceCounter(cnt);
ct := GetCurrentThreadID;
{$B+}
if prfRunning and ((prfOnlyThread = 0) or (prfOnlyThread = ct)) then begin
{$B-}
EnterCriticalSection(prfLock);
try
FlushCounter;
WriteTag(PR_ENTERPROC);
WriteThread(ct);
WriteProcID(aProcID);
WriteTicks(cnt);
if profProfilingMemoryEnabled then
WriteMemWorkingSize();
QueryPerformanceCounter(prfCounter);
finally LeaveCriticalSection(prfLock); end;
end;
end; { ProfilerEnterProc }
procedure ProfilerExitProc(const aProcID : Cardinal);
var
ct : Cardinal;
cnt: TLargeinteger;
begin
QueryPerformanceCounter(Cnt);
ct := GetCurrentThreadID;
{$B+}
if prfRunning and ((prfOnlyThread = 0) or (prfOnlyThread = ct)) then begin
{$B-}
EnterCriticalSection(prfLock);
try
FlushCounter;
WriteTag(PR_EXITPROC);
WriteThread(ct);
WriteProcID(aProcID);
WriteTicks(Cnt);
if profProfilingMemoryEnabled then
WriteMemWorkingSize();
QueryPerformanceCounter(prfCounter);
finally LeaveCriticalSection(prfLock); end;
end;
end; { ProfilerExitProc }
function CreateMeasurePointScope(const aMeasurePointId : String): IMeasurePointScope;
begin
result := TMeasurePointScope.Create(UTF8Encode(aMeasurePointId));
end;
procedure ProfilerStart;
begin
if not prfDisabled then begin
EnterCriticalSection(prfLock);
try prfRunning := true;
finally LeaveCriticalSection(prfLock); end;
end;
end; { ProfilerStart }
procedure ProfilerStop;
begin
if not prfDisabled then begin
EnterCriticalSection(prfLock);
try prfRunning := false;
finally LeaveCriticalSection(prfLock); end;
end;
end; { ProfilerStop }
procedure ProfilerStartThread;
begin
EnterCriticalSection(prfLock);
try
prfRunning := true;
prfOnlyThread := GetCurrentThreadID;
finally LeaveCriticalSection(prfLock); end;
end; { ProfilerStartThread }
function CombineNames(const fName, newExt: string): string;
begin
Result := Copy(fName,1,Length(fName)-Length(ExtractFileExt(fName)))+'.'+newExt;
end; { CombineNames }
{$IFDEF UNICODE}
procedure NameThreadForDebugging(const AThreadName: AnsiString; AThreadID: TThreadID);
begin
NameThreadForDebugging(string(aThreadName), aThreadID);
end; { NameThreadForDebugging }
{$ENDIF}
procedure NameThreadForDebugging(const AThreadName: string; AThreadID: TThreadID);
var LEntry : TThreadInformation;
begin
{$IFDEF HAS_NAME_THREAD_FOR_DEBUGGING}
TThread.NameThreadForDebugging(aThreadName, aThreadId);
{$ELSE}
GpProfCommon.NameThreadForDebugging(aThreadName, aThreadId);
{$ENDIF}
if not prfDisabled then
begin
LEntry := TThreadInformation.Create;
LEntry.ID := AThreadId;
LEntry.Name := UTF8Encode(AThreadName);
prfThreadsInfo.Add(LEntry);
end;
end; { NameThreadForDebugging }
procedure ReadIncSettings;
var
LBuf: array [0..256] of char;
LIniFileName: string;
LIni: TMemIniFile;
LBufSizeKB: Integer;
begin
GetModuleFileName(HInstance,LBuf,256);
prfModuleName := string(LBuf);
prfDisabled := true;
profProfilingAutostart := false;
LIniFileName := Copy(prfModuleName,1,Length(prfModuleName)-Length(ExtractFileExt(prfModuleName)))+'.GPI';
if not FileExists(LIniFileName) then
MessageBox(0, PChar(Format('Cannot find initialization file %s! '+
'Profiling will be disabled.', [LIniFileName])), 'GpProfile', MB_OK + MB_ICONERROR)
else begin
LIni := TMemIniFile.Create(LIniFileName);
try
profTableName := LIni.ReadString('IDTables','TableName','');
if profTableName <> '' then begin
if not FileExists(profTableName) then
MessageBox(0, PChar(Format('Cannot find data file %s! '+
'Profiling will be disabled.', [profTableName])), 'GpProfile',
MB_OK + MB_ICONERROR)
else begin
profProcSize := LIni.ReadInteger('Procedures','ProcSize',4);
profCompressTicks := LIni.ReadBool('Performance','CompressTicks',false);
profCompressThreads := LIni.ReadBool('Performance','CompressThreads',false);
profCompressMeasurePoints := LIni.ReadBool('Performance','CompressMeasurePoints',false);
profProfilingAutostart := LIni.ReadBool('Performance','ProfilingAutostart',true);
profProfilingMemoryEnabled := LIni.ReadBool('Performance','ProfilingMemSupport',false);
profPrfOutputFile := LIni.ReadString('Output','PrfOutputFilename','$(ModulePath)');
profPrfOutputFile := ResolvePrfRuntimePlaceholders(profPrfOutputFile);
LBufSizeKB := BUF_SIZE_DEFAULT div 1024;
profBufSize := LIni.ReadInteger('Output','BufSizeKB',LBufSizeKB);
if (profBufSize >= LBufSizeKB) and (profBufSize < 1024 * 1024 {1 GB}) then
begin
profBufSize := profBufSize * 1024;
end else
begin
MessageBox(0, PChar(Format('Invalid BufSizeKB value: %d, fallback to the default size %d KB',
[profBufSize, LBufSizeKB])), 'GpProfile', MB_OK + MB_ICONERROR);
profBufSize := BUF_SIZE_DEFAULT;
end;
prfDisabled := false;
end;
end;
finally
LIni.Free;
end;
end;
end; { ReadIncSettings }
procedure AppendToErrorLog(const aFilename, aMessage : string);
var
myFile : TextFile;
begin
AssignFile(myFile, aFilename);
if FileExists(aFilename) then
Append(myFile)
else
Rewrite(myFile);
WriteLn(myFile, aMessage);
CloseFile(myFile);
end;
procedure Initialize();
var
LErrorPath : string;
begin
try
ReadIncSettings;
if not prfDisabled then begin
prfRunning := profProfilingAutostart;
prfCounter := 0;
prfOnlyThread := 0;
prfThreads := TThreadIdList.Create;
prfThreadsInfo := TThreadInformationList.Create;
prfMaxThreadNum := 256;
prfThreadBytes := 1;
prfMeasurePoints := TMeasurePointList.Create;
prfMaxMeasurePointNum := 256;
prfMeasurePointBytes:= 1;
prfLastTick := -1;
prfName := CombineNames(prfModuleName, 'prf');
if profPrfOutputFile <> '' then
prfName := profPrfOutputFile + '.prf';
GetMem(prfBuf,profBufSize);
Win32Check(prfBuf <> nil);
prfBufOffs := 0;
InitializeCriticalSection(prfLock);
prfFile := CreateFile(PChar(prfName), GENERIC_WRITE, 0, nil, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL or FILE_FLAG_SEQUENTIAL_SCAN, 0);
Win32Check(prfFile <> INVALID_HANDLE_VALUE);
QueryPerformanceFrequency(prfFreq);
end;
except
on e: exception do
begin
LErrorPath := ChangeFileExt(prfName, '.err');
AppendToErrorLog(LErrorPath, 'Error in initialize: '+e.message);
raise;
end;
end;
end; { Initialize }
procedure WriteHeader;
begin
WriteTag(PR_PRFVERSION);
if profProfilingMemoryEnabled then
WriteInt(PRF_VERSION_WITH_MEM)
else
WriteInt(PRF_VERSION);
WriteTag(PR_COMPTICKS);
WriteBool(profCompressTicks);
WriteTag(PR_COMPTHREADS);
WriteBool(profCompressThreads);
WriteTag(PR_FREQUENCY);
WriteTicks(prfFreq);
WriteTag(PR_PROCSIZE);
WriteInt(profProcSize);
WriteTag(PR_COMPRESS_MEASURE_POINTS);
WriteBool(profCompressMeasurePoints);
WriteTag(PR_ENDHEADER);
end; { WriteHeader }
procedure CopyTables;
var
p: pointer;
f: file;
fs: integer;
begin
if not FileExists(profTableName) then prfDisabled := true
else begin
Assign(f,profTableName);
Reset(f,1);
try
fs := FileSize(f);
if fs > 0 then
begin
GetMem(p,fs);
try
BlockRead(f,p^,fs);
Transmit(p^,fs);
finally
FreeMem(p);
end;
end;
finally
Close(f);
end;
end;
end; { CopyTables }
procedure WriteCalibration;
var
i : integer;
run: boolean;
begin
run := prfRunning;
prfRunning := True;
WriteTag(PR_STARTCALIB);
WriteInt(CALIB_CNT);
for i := 1 to CALIB_CNT + 1 do begin
ProfilerEnterProc(0);
ProfilerExitProc(0);
end;
FlushCounter;
WriteTag(PR_ENDCALIB);
prfRunning := run;
end; { WriteCalibration }
procedure Finalize;
begin
Win32Check(CloseHandle(prfFile));
FreeMem(prfBuf);
prfThreads.Free;
prfThreadsInfo.free;
prfMeasurePoints.free;
DeleteCriticalSection(prfLock);
end; { Finalize }
procedure ProfilerTerminate;
var
i: integer;
LItem: TThreadInformation;
LThreadsIdItem: TTLEl;
LMeasurePointItem: TMPLEl;
begin
if not prfInitialized then Exit;
ProfilerStop;
prfInitialized := False;
EnterCriticalSection(prfLock);
try
FlushCounter;
WriteTag(PR_ENDDATA);
// write thread names
WriteTag(PR_START_THREADINFO);
WriteCardinal(prfThreadsInfo.count);
for i := 0 to prfThreadsInfo.count-1 do
begin
LItem := TThreadInformation(prfThreadsInfo[i]);
WriteCardinal(LItem.ID);
WriteUtf8String(LItem.Name);
end;
WriteTag(PR_END_THREADINFO);
// write compressed thread ids
if profCompressThreads then
begin
WriteTag(PR_START_THREAD_ID_LIST);
WriteCardinal(prfThreads.Count);
for i := 0 to prfThreads.Count-1 do
begin
LThreadsIdItem := prfThreads.Items[i];
WriteCardinal(LThreadsIdItem.tleThread);
WriteCardinal(LThreadsIdItem.tleRemap);
end;
WriteTag(PR_END_THREAD_ID_LIST);
end;
// write compressed measure point names
if profCompressMeasurePoints then
begin
prfMeasurePoints.Lock;
try
WriteTag(PR_START_MEASURE_POINTS_LIST);
WriteCardinal(prfMeasurePoints.Count);
for i := 0 to prfMeasurePoints.Count-1 do
begin
LMeasurePointItem := prfMeasurePoints.Items[i];
WriteUtf8String(LMeasurePointItem.mpleId);
WriteCardinal(LMeasurePointItem.mpleRemap);
end;
WriteTag(PR_END_MEASURE_POINTS_LIST);
finally
prfMeasurePoints.Unlock;
end;
end;
if prfBufOffs > 0 then
FlushFile;
finally
LeaveCriticalSection(prfLock);
end;
Finalize;
end; { ProfilerTerminate }
procedure CompressMeasurePoint(const aMeasurePointId: UTF8String; out aRemap, aCount: integer);
begin
if aMeasurePointId <> '' then
begin
if not profCompressMeasurePoints then
begin
aRemap := 0;
aCount := 0;
end else
begin
prfMeasurePoints.Lock;
try
aRemap := prfMeasurePoints.Remap(aMeasurePointId);
aCount := prfMeasurePoints.Count;
finally
prfMeasurePoints.Unlock;
end;
end;
end
else
raise Exception.Create('GpProf: Measure Point ID can''t be empty!');
end;
procedure ProfilerEnterMP(const aMeasurePointId: UTF8String);
var
ct : Cardinal;
cnt: TLargeInteger;
remap, count: integer;
begin
CompressMeasurePoint(aMeasurePointId,remap,count);
QueryPerformanceCounter(cnt);
ct := GetCurrentThreadID;
{$B+}
if prfRunning and ((prfOnlyThread = 0) or (prfOnlyThread = ct)) then begin
{$B-}
EnterCriticalSection(prfLock);
try
FlushCounter;
WriteTag(PR_ENTER_MP);
WriteThread(ct);
WriteMeasurePoint(aMeasurePointId,remap,count);
WriteTicks(Cnt);
if profProfilingMemoryEnabled then
WriteMemWorkingSize();
QueryPerformanceCounter(prfCounter);
finally
LeaveCriticalSection(prfLock);
end;
end;
end;
procedure ProfilerExitMP(const aMeasurePointId: UTF8String);
var
ct : Cardinal;
cnt: TLargeInteger;
remap, count: integer;
begin
QueryPerformanceCounter(Cnt);
ct := GetCurrentThreadID;
CompressMeasurePoint(aMeasurePointId,remap,count);
{$B+}
if prfRunning and ((prfOnlyThread = 0) or (prfOnlyThread = ct)) then begin
{$B-}
EnterCriticalSection(prfLock);
try
FlushCounter;
WriteTag(PR_EXIT_MP);
WriteThread(ct);
WriteMeasurePoint(aMeasurePointId,remap,count);
WriteTicks(Cnt);
if profProfilingMemoryEnabled then
WriteMemWorkingSize();
QueryPerformanceCounter(prfCounter);
finally
LeaveCriticalSection(prfLock);
end;
end;
end;
{ TMeasurePointScope }
constructor TMeasurePointScope.Create(const aMeasurePointId: UTF8String);
begin
inherited Create;
fMeasurePointId := aMeasurePointId;
ProfilerEnterMP(fMeasurePointId);
end;
destructor TMeasurePointScope.Destroy;
begin
ProfilerExitMP(fMeasurePointId);
inherited;
end;
initialization
prfInitialized := false;
Initialize;
if not prfDisabled then begin
WriteHeader;
CopyTables;
WriteCalibration;
WriteTag(PR_STARTDATA);
prfInitialized := true;
gpprof.NameThreadForDebugging('Main Application Thread', MainThreadID);
end;
finalization
ProfilerTerminate;
end.