-
Notifications
You must be signed in to change notification settings - Fork 695
/
GcStats.cs
1654 lines (1482 loc) · 90.8 KB
/
GcStats.cs
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
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Corporation. All rights reserved
using Microsoft.Diagnostics.Tracing.Analysis;
using Microsoft.Diagnostics.Tracing.Analysis.GC;
using Microsoft.Diagnostics.Tracing.Parsers.Clr;
using Microsoft.Diagnostics.Utilities;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security;
using System.Text;
using System.Threading;
using Utilities;
namespace Stats
{
internal static class GcStats
{
public static void ToHtml(TextWriter writer, TraceProcess stats, TraceLoadedDotNetRuntime runtime, string fileName, bool doServerGCReport = false)
{
writer.WriteLine("<H3><A Name=\"Stats_{0}\"><font color=\"blue\">GC Stats for Process {1,5}: {2}</font><A></H3>", stats.ProcessID, stats.ProcessID, stats.Name);
writer.WriteLine("<UL>");
if (runtime.GC.Stats().GCVersionInfoMismatch)
{
writer.WriteLine("<LI><Font size=3 color=\"red\">Warning: Did not recognize the V4.0 GC Information events. Falling back to V2.0 behavior.</font></LI>");
}
if (!string.IsNullOrEmpty(stats.CommandLine))
{
writer.WriteLine("<LI>CommandLine: {0}</LI>", stats.CommandLine);
}
var runtimeBuiltTime = "";
if (runtime.RuntimeBuiltTime != default(DateTime))
{
runtimeBuiltTime = string.Format(" (built on {0})", runtime.RuntimeBuiltTime);
}
writer.WriteLine("<LI>Runtime Version: {0}{1}</LI>", runtime.RuntimeVersion ?? "<Unknown Runtime Version>", runtimeBuiltTime);
writer.WriteLine("<LI>CLR Startup Flags: {0}</LI>", runtime.StartupFlags.ToString());
writer.WriteLine("<LI>Total CPU Time: {0:n0} msec</LI>", stats.CPUMSec);
writer.WriteLine("<LI>Total GC CPU Time: {0:n0} msec</LI>", runtime.GC.Stats().TotalCpuMSec);
writer.WriteLine("<LI>Total Allocs : {0:n3} MB</LI>", runtime.GC.Stats().TotalAllocatedMB);
writer.WriteLine("<LI>GC CPU MSec/MB Alloc : {0:n3} MSec/MB</LI>", runtime.GC.Stats().TotalCpuMSec / runtime.GC.Stats().TotalAllocatedMB);
writer.WriteLine("<LI>Total GC Pause: {0:n1} msec</LI>", runtime.GC.Stats().TotalPauseTimeMSec);
writer.WriteLine("<LI>% Time paused for Garbage Collection: {0:f1}%</LI>", runtime.GC.Stats().GetGCPauseTimePercentage());
writer.WriteLine("<LI>% CPU Time spent Garbage Collecting: {0:f1}%</LI>", runtime.GC.Stats().TotalCpuMSec * 100.0 / stats.CPUMSec);
writer.WriteLine("<LI>Max GC Heap Size: {0:n3} MB</LI>", runtime.GC.Stats().MaxSizePeakMB);
if (stats.PeakWorkingSet != 0)
{
writer.WriteLine("<LI>Peak Process Working Set: {0:n3} MB</LI>", stats.PeakWorkingSet / 1000000.0);
}
if (stats.PeakWorkingSet != 0)
{
writer.WriteLine("<LI>Peak Virtual Memory Usage: {0:n3} MB</LI>", stats.PeakVirtual / 1000000.0);
}
var usersGuideFile = ClrStatsUsersGuide.WriteUsersGuide(fileName);
writer.WriteLine("<LI> <A HREF=\"{0}#UnderstandingGCPerf\">GC Perf Users Guide</A></LI>", usersGuideFile);
writer.WriteLine("<LI><A HREF=\"#Events_Pause_{0}\">GCs that > 200 msec Events</A></LI>", stats.ProcessID);
writer.WriteLine("<LI><A HREF=\"#LOH_allocation_Pause_{0}\">LOH allocation pause (due to background GC) > 200 msec Events</A></LI>", stats.ProcessID);
writer.WriteLine("<LI><A HREF=\"#Events_Gen2_{0}\">GCs that were Gen2</A></LI>", stats.ProcessID);
writer.WriteLine("<LI><A HREF=\"#Events_{0}\">Individual GC Events</A> </LI>", stats.ProcessID);
writer.WriteLine("<UL><LI> <A HREF=\"command:excel/{0}\">View in Excel</A></LI></UL>", stats.ProcessID);
writer.WriteLine("<LI> <A HREF=\"command:excel/perGeneration/{0}\">Per Generation GC Events in Excel</A></LI>", stats.ProcessID);
if (runtime.GC.Stats().HasDetailedGCInfo)
{
writer.WriteLine("<LI> <A HREF=\"command:xml/{0}\">Raw Data XML file (for debugging)</A></LI>", stats.ProcessID);
}
if (runtime.GC.Stats().FinalizedObjects.Count > 0)
{
writer.WriteLine("<LI><A HREF=\"#Finalization_{0}\">Finalized Objects</A> </LI>", stats.ProcessID);
writer.WriteLine("<UL><LI> <A HREF=\"command:excelFinalization/{0}\">View in Excel</A></LI></UL>", stats.ProcessID);
writer.WriteLine("<UL><LI> <A HREF=\"{0}#UnderstandingFinalization\">Finalization Perf Users Guide</A></LI></UL>", usersGuideFile);
}
else
{
writer.WriteLine("<LI><I>No finalized object counts available. No objects were finalized and/or the trace did not include the necessary information.</I></LI>");
}
writer.WriteLine("</UL>");
writer.WriteLine("<Center>");
writer.WriteLine("<Table Border=\"1\">");
writer.WriteLine("<TR><TH colspan=\"12\" Align=\"Center\">GC Rollup By Generation</TH></TR>");
writer.WriteLine("<TR><TH colspan=\"12\" Align=\"Center\">All times are in msec.</TH></TR>");
writer.WriteLine("<TR>" +
"<TH>Gen</TH>" +
"<TH>Count</TH>" +
"<TH>Max<BR/>Pause</TH>" +
"<TH>Max<BR/>Peak MB</TH>" +
"<TH>Max Alloc<BR/>MB/sec</TH>" +
"<TH>Total<BR/>Pause</TH>" +
"<TH>Total<BR/>Alloc MB</TH>" +
"<TH>Alloc MB/<BR/>MSec GC</TH>" +
"<TH>Survived MB/<BR/>MSec GC</TH>" +
"<TH>Mean<BR/>Pause</TH>" +
"<TH>Induced</TH>" +
(ShowPinnedInformation(runtime.GC.Stats()) ?
"<TH>Avg Pinned Obj %</TH>"
: string.Empty) +
"</TR>");
writer.WriteLine("<TR>" +
"<TD Align=\"Center\">{0}</TD>" +
"<TD Align=\"Center\">{1}</TD>" +
"<TD Align=\"Center\">{2:n1}</TD>" +
"<TD Align=\"Center\">{3:n1}</TD>" +
"<TD Align=\"Center\">{4:n3}</TD>" +
"<TD Align=\"Center\">{5:n1}</TD>" +
"<TD Align=\"Center\">{6:n1}</TD>" +
"<TD Align=\"Center\">{7:n1}</TD>" +
"<TD Align=\"Center\">{8:n3}</TD>" +
"<TD Align=\"Center\">{9:n1}</TD>" +
"<TD Align=\"Center\">{10}</TD>" +
(ShowPinnedInformation(runtime.GC.Stats()) ?
"<TD Align=\"Center\">{11}</TD>"
: string.Empty) +
"</TR>",
"ALL",
runtime.GC.Stats().Count,
runtime.GC.Stats().MaxPauseDurationMSec,
runtime.GC.Stats().MaxSizePeakMB,
runtime.GC.Stats().MaxAllocRateMBSec,
runtime.GC.Stats().TotalPauseTimeMSec,
runtime.GC.Stats().TotalAllocatedMB,
runtime.GC.Stats().TotalAllocatedMB / runtime.GC.Stats().TotalPauseTimeMSec,
runtime.GC.Stats().TotalPromotedMB / runtime.GC.Stats().TotalCpuMSec,
runtime.GC.Stats().MeanPauseDurationMSec,
runtime.GC.Stats().NumInduced,
(((runtime.GC.Stats().NumWithPinEvents != 0) && (runtime.GC.Stats().NumWithPinEvents == runtime.GC.Stats().NumWithPinPlugEvents)) ? (runtime.GC.Stats().PinnedObjectPercentage / runtime.GC.Stats().NumWithPinEvents) : double.NaN));
for (int genNum = 0; genNum < runtime.GC.Generations().Length; genNum++)
{
GCStats gen = (runtime.GC.Generations()[genNum] != null) ? runtime.GC.Generations()[genNum] : new GCStats();
writer.WriteLine("<TR " + GetGenerationBackgroundColorAttribute(genNum) + ">" +
"<TD Align=\"Center\">{0}</TD>" +
"<TD Align=\"Center\">{1}</TD>" +
"<TD Align=\"Center\">{2:n1}</TD>" +
"<TD Align=\"Center\">{3:n1}</TD>" +
"<TD Align=\"Center\">{4:n3}</TD>" +
"<TD Align=\"Center\">{5:n1}</TD>" +
"<TD Align=\"Center\">{6:n1}</TD>" +
"<TD Align=\"Center\">{7:n1}</TD>" +
"<TD Align=\"Center\">{8:n3}</TD>" +
"<TD Align=\"Center\">{9:n1}</TD>" +
"<TD Align=\"Center\">{10}</TD>" +
(ShowPinnedInformation(runtime.GC.Stats()) ?
"<TD Align=\"Center\">{11}</TD>"
: string.Empty) +
"</TR>",
genNum,
gen.Count,
gen.MaxPauseDurationMSec,
gen.MaxSizePeakMB,
gen.MaxAllocRateMBSec,
gen.TotalPauseTimeMSec,
gen.TotalAllocatedMB,
gen.TotalPauseTimeMSec / runtime.GC.Stats().TotalAllocatedMB,
gen.TotalPromotedMB / gen.TotalCpuMSec,
gen.MeanPauseDurationMSec,
gen.NumInduced,
(((gen.NumWithPinEvents != 0) && (gen.NumWithPinEvents == gen.NumWithPinPlugEvents)) ? (gen.PinnedObjectPercentage / gen.NumWithPinEvents) : double.NaN));
}
writer.WriteLine("</Table>");
writer.WriteLine("</Center>");
writer.WriteLine("<HR/>");
writer.WriteLine("<H4><A Name=\"Events_Pause_{0}\">Pause > 200 Msec GC Events for Process {1,5}: {2}<A></H4>", stats.ProcessID, stats.ProcessID, stats.Name);
PrintEventTable(writer, stats, runtime, 0, delegate (TraceGC _gc) { return _gc.PauseDurationMSec > 200; });
writer.WriteLine("<HR/>");
writer.WriteLine("<H4><A Name=\"LOH_allocation_Pause_{0}\">LOH Allocation Pause (due to background GC) > 200 Msec for Process {1,5}: {2}<A></H4>", stats.ProcessID, stats.ProcessID, stats.Name);
PrintLOHAllocLargePauseTable(writer, stats, runtime, 200);
writer.WriteLine("<HR/>");
writer.WriteLine("<H4><A Name=\"Events_Gen2_{0}\">Gen 2 for Process {1,5}: {2}<A></H4>", stats.ProcessID, stats.ProcessID, stats.Name);
PrintEventTable(writer, stats, runtime, 0, delegate (TraceGC _gc) { return _gc.Generation > 1; });
writer.WriteLine("<HR/>");
writer.WriteLine("<H4><A Name=\"Events_{0}\">All GC Events for Process {1,5}: {2}<A></H4>", stats.ProcessID, stats.ProcessID, stats.Name);
PrintEventTable(writer, stats, runtime, Math.Max(0, runtime.GC.GCs.Count - 1000));
PrintEventCondemnedReasonsTable(writer, stats, runtime, Math.Max(0, runtime.GC.GCs.Count - 1000));
if (PerfView.AppLog.InternalUser)
{
RenderServerGcConcurrencyGraphs(writer, stats, runtime, doServerGCReport);
}
if (runtime.GC.Stats().FinalizedObjects.Count > 0)
{
const int MaxResultsToShow = 20;
int resultsToShow = Math.Min(runtime.GC.Stats().FinalizedObjects.Count, MaxResultsToShow);
writer.WriteLine("<HR/>");
writer.WriteLine("<H4><A Name=\"Finalization_{0}\">Finalized Object Counts for Process {1,5}: {2}<A></H4>", stats.ProcessID, stats.ProcessID, stats.Name);
writer.WriteLine("<Center><Table Border=\"1\">");
writer.WriteLine("<TR><TH>Type</TH><TH>Count</TH></TR>");
foreach (var finalized in runtime.GC.Stats().FinalizedObjects.OrderByDescending(f => f.Value).Take(resultsToShow))
{
var encodedTypeName = SecurityElement.Escape(finalized.Key);
writer.WriteLine("<TR><TD Align=\"Center\">{0}</TD><TD Align=\"Center\">{1}</TD><TR>", encodedTypeName, finalized.Value);
}
writer.WriteLine("</Table></Center>");
if (resultsToShow < runtime.GC.Stats().FinalizedObjects.Count)
{
writer.WriteLine("<P><I>Only showing {0} of {1} rows.</I></P>", resultsToShow, runtime.GC.Stats().FinalizedObjects.Count);
}
writer.WriteLine("<P><A HREF=\"command:excelFinalization/{0}\">View the full list</A> in Excel.<P>", stats.ProcessID);
}
writer.WriteLine("<HR/><HR/><BR/><BR/>");
}
public static void ToXml(TextWriter writer, TraceProcess stats, TraceLoadedDotNetRuntime runtime, string indent)
{
writer.Write("{0}<GCProcess", indent);
writer.Write(" Process={0}", StringUtilities.QuotePadLeft(stats.Name, 10));
writer.Write(" ProcessID={0}", StringUtilities.QuotePadLeft(stats.ProcessID.ToString(), 5));
if (stats.CPUMSec != 0)
{
writer.Write(" ProcessCpuTimeMsec={0}", StringUtilities.QuotePadLeft(stats.CPUMSec.ToString("f0"), 5));
}
ToXmlAttribs(writer, runtime.GC.Stats());
if (runtime.RuntimeVersion != null)
{
writer.Write(" RuntimeVersion={0}", StringUtilities.QuotePadLeft(runtime.RuntimeVersion, 8));
writer.Write(" StartupFlags={0}", StringUtilities.QuotePadLeft(runtime.StartupFlags.ToString(), 10));
writer.Write(" CommandLine="); writer.Write(XmlUtilities.XmlQuote(stats.CommandLine));
}
if (stats.PeakVirtual != 0)
{
writer.Write(" PeakVirtualMB={0}", StringUtilities.QuotePadLeft((stats.PeakVirtual / 1000000.0).ToString(), 8));
}
if (stats.PeakWorkingSet != 0)
{
writer.Write(" PeakWorkingSetMB={0}", StringUtilities.QuotePadLeft((stats.PeakWorkingSet / 1000000.0).ToString(), 8));
}
writer.WriteLine(">");
writer.WriteLine("{0} <Generations Count=\"{1}\" TotalGCCount=\"{2}\" TotalAllocatedMB=\"{3:n3}\" TotalGCCpuMSec=\"{4:n3}\" MSecPerMBAllocated=\"{5:f3}\">",
indent, runtime.GC.Generations().Length, runtime.GC.Stats().Count, runtime.GC.Stats().TotalAllocatedMB, runtime.GC.Stats().TotalCpuMSec, runtime.GC.Stats().TotalCpuMSec / runtime.GC.Stats().TotalAllocatedMB);
for (int gen = 0; gen < runtime.GC.Generations().Length; gen++)
{
writer.Write("{0} <Generation Gen=\"{1}\"", indent, gen);
ToXmlAttribs(writer, runtime.GC.Generations()[gen]);
writer.WriteLine("/>");
}
writer.WriteLine("{0} </Generations>", indent);
writer.WriteLine("{0} <GCEvents Count=\"{1}\">", indent, runtime.GC.GCs.Count);
foreach (TraceGC _event in runtime.GC.GCs)
{
ToXmlAttribs(writer, stats, runtime, _event);
}
writer.WriteLine("{0} </GCEvents>", indent);
writer.WriteLine("{0} </GCProcess>", indent);
}
public static void ToCsv(string filePath, TraceLoadedDotNetRuntime runtime)
{
string listSeparator = Thread.CurrentThread.CurrentCulture.TextInfo.ListSeparator;
using (var writer = File.CreateText(filePath))
{
// 0 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 27 28
writer.WriteLine("Num{0}PauseStart{0}Reason{0}Gen{0}PauseMSec{0}Time In GC{0}AllocMB{0}Alloc MB/sec{0}PeakMB{0}AfterMB{0}Peak/After Ratio{0}PromotedMB{0}Gen0 Size{0}Gen0 Surv%{0}Gen0 Frag%{0}Gen1 Size{0}Gen1 Surv%{0}Gen1 Frag%{0}Gen2 Size{0}Gen2 Surv%{0}Gen2 Frag%{0}LOH Size{0}LOH Surv%{0}LOH Frag%{0}FinalizeSurviveMB{0}Pinned Object{0}% Pause Time{0}Suspend Msec", listSeparator);
for (int i = 0; i < runtime.GC.GCs.Count; i++)
{
var _event = runtime.GC.GCs[i];
if (!(_event.IsComplete))
{
continue;
}
var allocGen0MB = _event.GenSizeBeforeMB[(int)Gens.Gen0];
writer.WriteLine("{0}{26}{1:f3}{26}{2}{26}{3}{26}{4:f3}{26}{5:f1}{26}{6:f3}{26}{7:f2}{26}{8:f3}{26}{9:f3}{26}{10:2}{26}{11:f3}{26}{12:f3}{26}{13:f0}{26}{14:f2}{26}{15:f3}{26}{16:f0}{26}{17:f2}{26}{18:f3}{26}{19:f0}{26}{20:f2}{26}{21:f3}{26}{22:f0}{26}{23:f2}{26}{24:f2}{26}{25:f0}{26}{27:f1}{26}{28:f3}",
_event.Number,
_event.PauseStartRelativeMSec,
_event.Reason,
_event.GCGenerationName,
_event.PauseDurationMSec,
_event.PercentTimeInGC,
allocGen0MB,
(allocGen0MB * 1000.0) / _event.DurationSinceLastRestartMSec,
_event.HeapSizePeakMB,
_event.HeapSizeAfterMB,
_event.HeapSizePeakMB / _event.HeapSizeAfterMB,
_event.PromotedMB,
_event.GenSizeAfterMB(Gens.Gen0),
_event.SurvivalPercent(Gens.Gen0),
_event.GenFragmentationPercent(Gens.Gen0),
_event.GenSizeAfterMB(Gens.Gen1),
_event.SurvivalPercent(Gens.Gen1),
_event.GenFragmentationPercent(Gens.Gen1),
_event.GenSizeAfterMB(Gens.Gen2),
_event.SurvivalPercent(Gens.Gen2),
_event.GenFragmentationPercent(Gens.Gen2),
_event.GenSizeAfterMB(Gens.GenLargeObj),
_event.SurvivalPercent(Gens.GenLargeObj),
_event.GenFragmentationPercent(Gens.GenLargeObj),
_event.HeapStats.FinalizationPromotedSize / 1000000.0,
_event.HeapStats.PinnedObjectCount,
listSeparator,
_event.PauseTimePercentageSinceLastGC,
_event.SuspendDurationMSec);
}
}
}
public static void ToCsvFinalization(string filePath, TraceLoadedDotNetRuntime runtime)
{
string listSeparator = Thread.CurrentThread.CurrentCulture.TextInfo.ListSeparator;
using (var writer = File.CreateText(filePath))
{
writer.WriteLine("Type{0}Count", listSeparator);
foreach (var finalized in runtime.GC.Stats().FinalizedObjects.OrderByDescending(f => f.Value))
{
writer.WriteLine("{0}{1}{2}", finalized.Key.Replace(listSeparator, ""), listSeparator, finalized.Value);
}
}
}
public static void PerGenerationCsv(string filePath, TraceLoadedDotNetRuntime runtime)
{
// Sadly, streamWriter does not have a way of setting the IFormatProvider property
// So we have to do it in this ugly, global variable way.
string listSeparator = Thread.CurrentThread.CurrentCulture.TextInfo.ListSeparator;
using (var writer = File.CreateText(filePath))
{
writer.WriteLine("PauseStart{0}Num{0}Gen{0}GCStop{0}TickMB{0}" +
"Before0{0}Before1{0}Before2{0}Before3{0}" +
"After0{0}After1{0}After2{0}After3{0}" +
"Surv0{0}Surv1{0}Surv2{0}Surv3{0}" +
"In0{0}In1{0}In2{0}In3{0}" +
"Out0{0}Out1{0}Out2{0}Out3{0}" +
"Frag0{0}Frag1{0}Frag2{0}Frag3" +
(runtime.GC.Stats().HasDetailedGCInfo ? "{0}Budget0{0}Budget1{0}Budget2{0}Budget3" : ""), listSeparator);
foreach (TraceGC _event in runtime.GC.GCs)
{
if (!_event.IsComplete)
{
continue;
}
writer.WriteLine("{0:f3}{33}{1}{33}{2}{33}{3:f3}{33}{4:f3}{33}" +
"{5:f3}{33}{6:f3}{33}{7:f3}{33}{8:f3}{33}" +
"{9:f3}{33}{10:f3}{33}{11:f3}{33}{12:f3}{33}" +
"{13:f3}{33}{14:f3}{33}{15:f3}{33}{16:f3}{33}" +
"{17:f3}{33}{18:f3}{33}{19:f3}{33}{20:f3}{33}" +
"{21:f3}{33}{22:f3}{33}{23:f3}{33}{24:f3}{33}" +
"{25:f3}{33}{26:f3}{33}{27:f3}{33}{28:f3}" +
(runtime.GC.Stats().HasDetailedGCInfo ? "{33}{29:f3}{33}{30:f3}{33}{31:f3}{33}{32:f3}" : ""),
_event.PauseStartRelativeMSec,
_event.Number,
_event.GCGenerationName,
_event.DurationMSec + _event.StartRelativeMSec,
(_event.AllocedSinceLastGCBasedOnAllocTickMB[0] + _event.AllocedSinceLastGCBasedOnAllocTickMB[1]),
_event.GenSizeBeforeMB[(int)Gens.Gen0], _event.GenSizeBeforeMB[(int)Gens.Gen1],
_event.GenSizeBeforeMB[(int)Gens.Gen2], _event.GenSizeBeforeMB[(int)Gens.GenLargeObj],
_event.GenSizeAfterMB(Gens.Gen0), _event.GenSizeAfterMB(Gens.Gen1),
_event.GenSizeAfterMB(Gens.Gen2), _event.GenSizeAfterMB(Gens.GenLargeObj),
_event.GenPromotedMB(Gens.Gen0), _event.GenPromotedMB(Gens.Gen1),
_event.GenPromotedMB(Gens.Gen2), _event.GenPromotedMB(Gens.GenLargeObj),
_event.GenInMB(Gens.Gen0), _event.GenInMB(Gens.Gen1),
_event.GenInMB(Gens.Gen2), _event.GenInMB(Gens.GenLargeObj),
_event.GenOutMB(Gens.Gen0), _event.GenOutMB(Gens.Gen1),
_event.GenOutMB(Gens.Gen2), _event.GenOutMB(Gens.GenLargeObj),
_event.GenFragmentationMB(Gens.Gen0), _event.GenFragmentationMB(Gens.Gen1),
_event.GenFragmentationMB(Gens.Gen2), _event.GenFragmentationMB(Gens.GenLargeObj),
_event.GenBudgetMB(Gens.Gen0), _event.GenBudgetMB(Gens.Gen1),
_event.GenBudgetMB(Gens.Gen2), _event.GenBudgetMB(Gens.GenLargeObj),
listSeparator
);
}
}
}
public static void ToXmlAttribs(TextWriter writer, GCStats gc)
{
if (gc == null)
{
gc = new GCStats();
}
writer.Write(" GCCount={0}", StringUtilities.QuotePadLeft(gc.Count.ToString(), 6));
writer.Write(" MaxPauseDurationMSec={0}", StringUtilities.QuotePadLeft(gc.MaxPauseDurationMSec.ToString("n3"), 10));
writer.Write(" MeanPauseDurationMSec={0}", StringUtilities.QuotePadLeft(gc.MeanPauseDurationMSec.ToString("n3"), 10));
writer.Write(" MeanSizePeakMB={0}", StringUtilities.QuotePadLeft(gc.MeanSizePeakMB.ToString("f1"), 10));
writer.Write(" MeanSizeAfterMB={0}", StringUtilities.QuotePadLeft(gc.MeanSizeAfterMB.ToString("f1"), 10));
writer.Write(" TotalAllocatedMB={0}", StringUtilities.QuotePadLeft(gc.TotalAllocatedMB.ToString("f1"), 10));
writer.Write(" TotalGCDurationMSec={0}", StringUtilities.QuotePadLeft(gc.TotalCpuMSec.ToString("n3"), 10));
writer.Write(" MSecPerMBAllocated={0}", StringUtilities.QuotePadLeft((gc.TotalCpuMSec / gc.TotalAllocatedMB).ToString("n3"), 10));
writer.Write(" TotalPauseTimeMSec={0}", StringUtilities.QuotePadLeft(gc.TotalPauseTimeMSec.ToString("n3"), 10));
writer.Write(" MaxAllocRateMBSec={0}", StringUtilities.QuotePadLeft(gc.MaxAllocRateMBSec.ToString("n3"), 10));
writer.Write(" MeanGCCpuMSec={0}", StringUtilities.QuotePadLeft(gc.MeanCpuMSec.ToString("n3"), 10));
writer.Write(" MaxSuspendDurationMSec={0}", StringUtilities.QuotePadLeft(gc.MaxSuspendDurationMSec.ToString("n3"), 10));
writer.Write(" MaxSizePeakMB={0}", StringUtilities.QuotePadLeft(gc.MaxSizePeakMB.ToString("n3"), 10));
}
public static void ToXmlAttribs(TextWriter writer, TraceProcess stats, TraceLoadedDotNetRuntime runtime, TraceGC gc)
{
writer.Write(" <GCEvent");
writer.Write(" GCNumber={0}", StringUtilities.QuotePadLeft(gc.Number.ToString(), 10));
writer.Write(" GCGeneration={0}", StringUtilities.QuotePadLeft(gc.Generation.ToString(), 3));
writer.Write(" GCCpuMSec={0}", StringUtilities.QuotePadLeft(gc.GCCpuMSec.ToString("n0").ToString(), 10));
writer.Write(" ProcessCpuMSec={0}", StringUtilities.QuotePadLeft(gc.ProcessCpuMSec.ToString("n0").ToString(), 10));
writer.Write(" PercentTimeInGC={0}", StringUtilities.QuotePadLeft(gc.PercentTimeInGC.ToString("n2").ToString(), 10));
writer.Write(" PauseStartRelativeMSec={0}", StringUtilities.QuotePadLeft(gc.PauseStartRelativeMSec.ToString("n3").ToString(), 10));
writer.Write(" PauseDurationMSec={0}", StringUtilities.QuotePadLeft(gc.PauseDurationMSec.ToString("n3").ToString(), 10));
writer.Write(" PercentPauseTime={0}", StringUtilities.QuotePadLeft(gc.PauseTimePercentageSinceLastGC.ToString("n2").ToString(), 10));
writer.Write(" SizePeakMB={0}", StringUtilities.QuotePadLeft(gc.HeapSizePeakMB.ToString("n3"), 10));
writer.Write(" SizeAfterMB={0}", StringUtilities.QuotePadLeft(gc.HeapSizeAfterMB.ToString("n3"), 10));
writer.Write(" RatioPeakAfter={0}", StringUtilities.QuotePadLeft(gc.RatioPeakAfter.ToString("n3"), 5));
writer.Write(" AllocRateMBSec={0}", StringUtilities.QuotePadLeft(gc.AllocRateMBSec.ToString("n3"), 5));
writer.Write(" GCDurationMSec={0}", StringUtilities.QuotePadLeft(gc.DurationMSec.ToString("n3").ToString(), 10));
writer.Write(" SuspendDurationMSec={0}", StringUtilities.QuotePadLeft(gc.SuspendDurationMSec.ToString("n3").ToString(), 10));
writer.Write(" GCStartRelativeMSec={0}", StringUtilities.QuotePadLeft(gc.StartRelativeMSec.ToString("n3"), 10));
writer.Write(" DurationSinceLastRestartMSec={0}", StringUtilities.QuotePadLeft(gc.DurationSinceLastRestartMSec.ToString("n3"), 5));
writer.Write(" AllocedSinceLastGC={0}", StringUtilities.QuotePadLeft(gc.AllocedSinceLastGCMB.ToString("n3"), 5));
writer.Write(" Type={0}", StringUtilities.QuotePadLeft(gc.Type.ToString(), 18));
writer.Write(" Reason={0}", StringUtilities.QuotePadLeft(gc.Reason.ToString(), 27));
writer.WriteLine(">");
if (gc.HeapStats != null)
{
writer.Write(" <HeapStats");
writer.Write(" GenerationSize0=\"{0:n0}\"", gc.HeapStats.GenerationSize0);
writer.Write(" TotalPromotedSize0=\"{0:n0}\"", gc.HeapStats.TotalPromotedSize0);
writer.Write(" GenerationSize1=\"{0:n0}\"", gc.HeapStats.GenerationSize1);
writer.Write(" TotalPromotedSize1=\"{0:n0}\"", gc.HeapStats.TotalPromotedSize1);
writer.Write(" GenerationSize2=\"{0:n0}\"", gc.HeapStats.GenerationSize2);
writer.Write(" TotalPromotedSize2=\"{0:n0}\"", gc.HeapStats.TotalPromotedSize2);
writer.Write(" GenerationSize3=\"{0:n0}\"", gc.HeapStats.GenerationSize3);
writer.Write(" TotalPromotedSize3=\"{0:n0}\"", gc.HeapStats.TotalPromotedSize3);
writer.Write(" GenerationSize4=\"{0:n0}\"", gc.HeapStats.GenerationSize4);
writer.Write(" TotalPromotedSize4=\"{0:n0}\"", gc.HeapStats.TotalPromotedSize4);
writer.Write(" FinalizationPromotedSize=\"{0:n0}\"", gc.HeapStats.FinalizationPromotedSize);
writer.Write(" FinalizationPromotedCount=\"{0:n0}\"", gc.HeapStats.FinalizationPromotedCount);
writer.Write(" PinnedObjectCount=\"{0:n0}\"", gc.HeapStats.PinnedObjectCount);
writer.Write(" SinkBlockCount=\"{0:n0}\"", gc.HeapStats.SinkBlockCount);
writer.Write(" GCHandleCount=\"{0:n0}\"", gc.HeapStats.GCHandleCount);
writer.WriteLine("/>");
}
if (gc.GlobalHeapHistory != null)
{
writer.Write(" <GlobalHeapHistory");
writer.Write(" FinalYoungestDesired=\"{0:n0}\"", gc.GlobalHeapHistory.FinalYoungestDesired);
writer.Write(" NumHeaps=\"{0}\"", gc.GlobalHeapHistory.NumHeaps);
writer.Write(" CondemnedGeneration=\"{0}\"", gc.GlobalHeapHistory.CondemnedGeneration);
writer.Write(" Gen0ReductionCount=\"{0:n0}\"", gc.GlobalHeapHistory.Gen0ReductionCount);
writer.Write(" Reason=\"{0}\"", gc.GlobalHeapHistory.Reason);
writer.Write(" GlobalMechanisms=\"{0}\"", gc.GlobalHeapHistory.GlobalMechanisms);
writer.WriteLine("/>");
}
// I am seeing a GC with SuspendEE/RestartEE/HeapStats events yet in the middle we are missing GlobalHistory and some of
// the PerHeapHistory events so need to compensate for that.
if (gc.PerHeapHistories != null && gc.GlobalHeapHistory != null && gc.PerHeapHistories.Count > 0)
{
writer.WriteLine(" <PerHeapHistories Count=\"{0}\" MemoryLoad=\"{1}\">",
gc.PerHeapHistories.Count,
(gc.GlobalHeapHistory.HasMemoryPressure ? gc.GlobalHeapHistory.MemoryPressure : gc.PerHeapHistories[0].MemoryPressure));
int HeapNum = 0;
foreach (var perHeapHistory in gc.PerHeapHistories)
{
writer.Write(" <PerHeapHistory");
#if false // TODO FIX NOW
writer.Write(" MemoryPressure=\"{0:n0}\"", gc.perHeapHistory.MemoryPressure);
writer.Write(" MechanismHeapExpand=\"{0}\"", gc.perHeapHistory.MechanismHeapExpand);
writer.Write(" MechanismHeapCompact=\"{0}\"", gc.perHeapHistory.MechanismHeapCompact);
writer.Write(" InitialGenCondemned=\"{0}\"", gc.perHeapHistory.InitialGenCondemned);
writer.Write(" FinalGenCondemned=\"{0}\"", gc.perHeapHistory.FinalGenCondemned);
writer.Write(" GenWithExceededBudget=\"{0}\"", gc.perHeapHistory.GenWithExceededBudget);
writer.Write(" GenWithTimeTuning=\"{0}\"", gc.perHeapHistory.GenWithTimeTuning);
writer.Write(" GenCondemnedReasons=\"{0}\"", gc.perHeapHistory.GenCondemnedReasons);
#endif
if ((gc.PerHeapMarkTimes != null) && (gc.PerHeapMarkTimes.ContainsKey(HeapNum)))
{
MarkInfo mt = gc.PerHeapMarkTimes[HeapNum];
if (mt != null)
{
writer.Write(" MarkStack =\"{0:n3}", mt.MarkTimes[(int)MarkRootType.MarkStack]);
if (mt.MarkPromoted != null)
{
writer.Write("({0})", mt.MarkPromoted[(int)MarkRootType.MarkStack]);
}
writer.Write("\" MarkFQ =\"{0:n3}", mt.MarkTimes[(int)MarkRootType.MarkFQ]);
if (mt.MarkPromoted != null)
{
writer.Write("({0})", mt.MarkPromoted[(int)MarkRootType.MarkFQ]);
}
writer.Write("\" MarkHandles =\"{0:n3}", mt.MarkTimes[(int)MarkRootType.MarkHandles]);
if (mt.MarkPromoted != null)
{
writer.Write("({0})", mt.MarkPromoted[(int)MarkRootType.MarkHandles]);
}
writer.Write("\"");
if (gc.Generation != 2)
{
writer.Write(" MarkOldGen =\"{0:n3}", mt.MarkTimes[(int)MarkRootType.MarkOlder]);
if (mt.MarkPromoted != null)
{
writer.Write("({0})", mt.MarkPromoted[(int)MarkRootType.MarkOlder]);
}
writer.Write("\"");
}
if (mt.MarkTimes[(int)MarkRootType.MarkOverflow] != 0.0)
{
writer.Write(" MarkOverflow =\"{0:n3}", mt.MarkTimes[(int)MarkRootType.MarkOverflow]);
if (mt.MarkPromoted != null)
{
writer.Write("({0})", mt.MarkPromoted[(int)MarkRootType.MarkOverflow]);
}
}
}
}
else
{
writer.Write(" DataUnavailable=\"true\"");
}
writer.WriteLine(">");
var sb = new System.Text.StringBuilder();
for (int gens = 0; gens < perHeapHistory.GenData.Length; gens++)
{
sb.Clear();
sb.Append(" ");
writer.Write(perHeapHistory.GenData[(int)gens].ToXml((Gens)gens, sb).AppendLine().ToString());
}
writer.WriteLine(" </PerHeapHistory>");
HeapNum++;
}
writer.WriteLine(" </PerHeapHistories>");
}
writer.WriteLine(" </GCEvent>");
}
#region private
private static bool ShowPinnedInformation(GCStats stats)
{
if ((PerfView.AppLog.InternalUser) && (stats.NumWithPinEvents > 0))
{
return true;
}
return false;
}
private static bool HasServerGcThreadingInfo(TraceGC gc)
{
foreach (var heap in gc.ServerGcHeapHistories)
{
if (heap.SampleSpans.Count > 0 || heap.SwitchSpans.Count > 0)
{
return true;
}
}
return false;
}
private static bool RenderServerGcConcurrencyGraphs(TextWriter writer, TraceProcess stats, TraceLoadedDotNetRuntime runtime, bool doServerGCReport)
{
if (runtime.GC.Stats().HeapCount <= 1 || runtime.GC.Stats().IsServerGCUsed != 1)
{
return false;
}
TextWriter serverGCActivityStatsFile = null;
int gcGraphsToRender = 10;
var serverGCs = runtime.GC.GCs
.Where(gc => gc.Type != GCType.BackgroundGC && HasServerGcThreadingInfo(gc))
.OrderByDescending(gc => gc.DurationMSec + gc.SuspendDurationMSec)
.ToArray();
if (serverGCs.Length == 0)
{
return false;
}
if (doServerGCReport)
{
string name = "SGCStats-" + stats.Name + "-" + stats.ProcessID + ".txt";
serverGCActivityStatsFile = new StreamWriter(name, false);
}
writer.WriteLine("<h3>Longest Server GCs. (CPU time by heap)</h3>");
RenderServerGcLegend(writer);
foreach (var gc in serverGCs)
{
if (gcGraphsToRender == 0)
{
break;
}
if (ServerGcHistoryEx.ServerGcConcurrencyGraphs(writer, serverGCActivityStatsFile, gc))
{
gcGraphsToRender--;
}
}
if (serverGCActivityStatsFile != null)
{
serverGCActivityStatsFile.Close();
}
return true;
}
private static void RenderServerGcLegend(TextWriter writer)
{
writer.WriteLine("<svg width='500' height='200' >");
writer.WriteLine("<rect x='10' y='10' width='5' height='30' style='fill:rgb(0,200,0);' />");
writer.WriteLine("<text x='20' y='40'> GC thread working.</text>");
writer.WriteLine("</rect>");
writer.WriteLine("<rect x='10' y='50' width='5' height='30' style='fill:rgb(250,20,20);' />");
writer.WriteLine("<text x='20' y='80'>Another thread working, potentially taking CPU time from GC thread.</text>");
writer.WriteLine("</rect>");
writer.WriteLine("<rect x='10' y='90' width='5' height='30' style='fill:rgb(0,0,220);' />");
writer.WriteLine("<text x='20' y='120'>Idle.</text>");
writer.WriteLine("</rect>");
writer.WriteLine("<rect x='10' y='130' width='5' height='30' style='fill:rgb(0,100,220);' />");
writer.WriteLine("<text x='20' y='160'>Low priority thread is working. (Most likely not taking CPU time from GC thread)</text>");
writer.WriteLine("</rect>");
writer.WriteLine("<polygon points='10,170 10,190 13,180' style='fill:rgb(255,215,0);' />");
writer.WriteLine("<text x='20' y='185'>GC Join reset event.</text>");
writer.WriteLine("<rect x='10' y='200' width='15' height='4' style='fill:rgb(255,215,0);' />");
writer.WriteLine("<text x='30' y='205'>GC Join - start to end.</text>");
writer.WriteLine("</svg>");
}
private static void PrintEventTable(TextWriter writer, TraceProcess stats, TraceLoadedDotNetRuntime runtime, int start = 0, Predicate<TraceGC> filter = null)
{
writer.WriteLine("<Center>");
writer.WriteLine("<Table Border=\"1\">");
writer.WriteLine("<TR><TH colspan=\"" + (28 + (ShowPinnedInformation(runtime.GC.Stats()) ? 4 : 0)) + "\" Align=\"Center\">GC Events by Time</TH></TR>");
writer.WriteLine("<TR><TH colspan=\"" + (28 + (ShowPinnedInformation(runtime.GC.Stats()) ? 4 : 0)) + "\" Align=\"Center\">All times are in msec. Hover over columns for help.</TH></TR>");
writer.WriteLine("<TR>" +
"<TH>GC<BR/>Index</TH>" +
"<TH>Pause Start</TH>" +
"<TH Title=\"How this GC was triggered\">Trigger<BR/>Reason</TH>" +
"<TH Title=\"N=NonConcurrent, B=Background, F=Foreground (while background is running) I=Induced i=InducedNotForced\">Gen</TH>" +
"<TH Title=\"The time in milliseconds that it took to suspend all threads to start this GC. For background GCs, we pause multiple times, so this value may be higher than for foreground GCs.\">Suspend<BR/>Msec</TH>" +
"<TH Title=\"The amount of time that execution in managed code is blocked because the GC needs exclusive use to the heap. For background GCs this is small.\">Pause<BR/>MSec</TH>" +
"<TH Title=\"Since the last GC, GC pause time expressed as a percentage of total process time. For background GC, this includes the pause time for foreground GCs that occur during the background GC.\">%<BR/>Pause<BR/>Time</TH>" +
"<TH Title=\"Since the last GC, the GC CPU time divided by the total Process CPU time expressed as a percentage.\">% GC</TH>" +
"<TH Title=\"Amount allocated since the last GC occured\">Gen0<BR/>Alloc<BR/>MB</TH>" +
"<TH Title=\"The average allocation rate since the last GC.\">Gen0<BR/>Alloc<BR/>Rate<BR/>MB/sec</TH>" +
"<TH Title=\"The peak size of the GC during GC. (includes fragmentation)\">Peak<BR/>MB</TH>" +
"<TH Title=\"The size after GC (includes fragmentation)\">After<BR/>MB</TH>" +
"<TH>Ratio<BR/>Peak/After</TH>" +
"<TH Title=\"Memory this GC promoted\">Promoted<BR/>MB</TH>" +
"<TH Title=\"Size of gen0 at the end of this GC.\">Gen0<BR/>MB</TH>" +
"<TH Title=\"The % of objects in Gen0 that survived this GC.\">Gen0<BR/>Survival<BR/>Rate %</TH>" +
"<TH Title=\"The % of free space on gen0.\">Gen0<BR/>Frag<BR/>%</TH>" +
"<TH Title=\"Size of gen1 at the end of this GC.\">Gen1<BR/>MB</TH>" +
"<TH Title=\"The % of objects in Gen1 that survived this GC. Only available if we are doing a gen1 GC.\">Gen1<BR/>Survival<BR/>Rate %</TH>" +
"<TH Title=\"The % of free space on Gen1 that is betweeen live objects\">Gen1<BR/>Frag<BR/>%</TH>" +
"<TH Title=\"Size of Gen2 in MB at the end of this GC.\">Gen2<BR/>MB</TH>" +
"<TH Title=\"The % of objects in Gen2 that survived this GC. Only available if we are doing a gen2 GC.\">Gen2<BR/>Survival<BR/>Rate %</TH>" +
"<TH Title=\"The % of free space on gen2.\">Gen2<BR/>Frag<BR/>%</TH>" +
"<TH Title=\"Size of Large object heap (LOH) in MB at the end of this GC.\">LOH<BR/>MB</TH>" +
"<TH Title=\"The % of objects in the large object heap (LOH) that survived the GC. Only available if we are doing a gen2 GC.\">LOH<BR/>Survival<BR/>Rate %</TH>" +
"<TH Title=\"The % of free space that is between live objects on the large object heap (LOH).\">LOH<BR/>Frag<BR/>%</TH>" +
"<TH Title=\"The number of MB of objects that have finalizers (destructors) that survived this GC. \">Finalizable<BR/>Surv MB</TH>" +
"<TH Title=\"Number of pinned objects this GC promoted.\">Pinned<BR/>Obj</TH>" +
(ShowPinnedInformation(runtime.GC.Stats()) ?
"<TH Title=\"Size of pinned objects this GC promoted.\">Pinned<BR/>Obj<BR/>Size</TH>" +
"<TH Title=\"Percentage of pinned plugs occupied by pinned objects.\">Pinned<BR/>Obj<BR/>%</TH>" +
"<TH Title=\"Size of pinned plugs\">Pinned<BR/>Size</TH>" +
"<TH Title=\"Size of pinned plugs by GC\">GC<BR/>Pinned<BR/>Size</TH>"
: string.Empty) +
"</TR>");
if (start != 0)
{
writer.WriteLine("<TR><TD colspan=\"26\" Align=\"Center\"> {0} Beginning entries truncated, use <A HREF=\"command:excel/{1}\">View in Excel</A> to view all...</TD></TR>", start, stats.ProcessID);
}
for (int i = start; i < runtime.GC.GCs.Count; i++)
{
var _event = runtime.GC.GCs[i];
if (filter == null || filter(_event))
{
if (!_event.IsComplete)
{
continue;
}
var allocGen0MB = _event.UserAllocated[(int)Gens.Gen0];
writer.WriteLine("<TR " + GetGenerationBackgroundColorAttribute(_event.Generation) + ">" +
"<TD Align=\"right\">{0}</TD>" + // GC index
"<TD Align=\"right\">{1:n3}</TD>" + // Pause start
"<TD Align=\"right\">{2}</TD>" + // Reason
"<TD Align=\"right\">{3}</TD>" + // Gen
"<TD Align=\"right\">{4:n3}</TD>" + // Suspension time
"<TD Align=\"right\">{5:n3}</TD>" + // Pause duration
"<TD Align=\"right\">{6:n1}</TD>" + // % pause time since last GC
"<TD Align=\"right\">{7:n1}</TD>" + // % time in GC
"<TD Align=\"right\">{8:n3}</TD>" + // Amount Allocated in gen0
"<TD Align=\"right\">{9:n2}</TD>" + // Gen0 AllocRate
"<TD Align=\"right\">{10:n3}</TD>" + // Size at the beginning of this GC
"<TD Align=\"right\">{11:n3}</TD>" + // Size at the end of this GC
"<TD Align=\"right\">{12:n2}</TD>" + // Ratio of end/beginning
"<TD Align=\"right\">{13:n3}</TD>" + // Memory this GC promoted
"<TD Align=\"right\">{14:n3}</TD>" + // Gen0 size at the end of this GC
"<TD Align=\"right\">{15:n0}</TD>" + // Gen0 survival rate
"<TD Align=\"right\">{16:n2}</TD>" + // Gen0 frag ratio
"<TD Align=\"right\">{17:n3}</TD>" + // Gen1 size at the end of this GC
"<TD Align=\"right\">{18:n0}</TD>" + // Gen1 survival rate
"<TD Align=\"right\">{19:n2}</TD>" + // Gen1 frag ratio
"<TD Align=\"right\">{20:n3}</TD>" + // Gen2 size at the end of this GC
"<TD Align=\"right\">{21:n0}</TD>" + // Gen2 survivl rate
"<TD Align=\"right\">{22:n2}</TD>" + // Gen2 frag ratio
"<TD Align=\"right\">{23:n3}</TD>" + // LOH size at the end of this GC
"<TD Align=\"right\">{24:n0}</TD>" + // LOH survival rate
"<TD Align=\"right\">{25:n2}</TD>" + // LOH frag ratio
"<TD Align=\"right\">{26:n2}</TD>" + // Finalize promoted for this GC
"<TD Align=\"right\">{27:n0}</TD>" + // # of pinned object this GC saw
(ShowPinnedInformation(runtime.GC.Stats()) ?
"<TD Align=\"right\">{28:n0}</TD>" + // size of pinned object this GC saw
"<TD Align=\"right\">{29:n0}</TD>" + // percent of pinned object this GC saw
"<TD Align=\"right\">{30:n0}</TD>" + // size of pinned plugs
"<TD Align=\"right\">{31:n0}</TD>" // size of pinned plugs by GC
: string.Empty) +
"</TR>",
_event.Number,
_event.PauseStartRelativeMSec,
_event.Reason,
_event.GCGenerationName,
_event.SuspendDurationMSec,
_event.PauseDurationMSec,
_event.PauseTimePercentageSinceLastGC,
_event.PercentTimeInGC,
allocGen0MB,
(allocGen0MB * 1000.0) / _event.DurationSinceLastRestartMSec,
_event.HeapSizePeakMB,
_event.HeapSizeAfterMB,
_event.HeapSizePeakMB / _event.HeapSizeAfterMB,
_event.PromotedMB,
_event.GenSizeAfterMB(Gens.Gen0),
_event.SurvivalPercent(Gens.Gen0),
_event.GenFragmentationPercent(Gens.Gen0),
_event.GenSizeAfterMB(Gens.Gen1),
_event.SurvivalPercent(Gens.Gen1),
_event.GenFragmentationPercent(Gens.Gen1),
_event.GenSizeAfterMB(Gens.Gen2),
_event.SurvivalPercent(Gens.Gen2),
_event.GenFragmentationPercent(Gens.Gen2),
_event.GenSizeAfterMB(Gens.GenLargeObj),
_event.SurvivalPercent(Gens.GenLargeObj),
_event.GenFragmentationPercent(Gens.GenLargeObj),
_event.HeapStats.FinalizationPromotedSize / 1000000.0,
_event.HeapStats.PinnedObjectCount,
((_event.GetPinnedObjectSizes() != 0) ? _event.GetPinnedObjectSizes() : double.NaN),
((_event.GetPinnedObjectPercentage() != -1) ? _event.GetPinnedObjectPercentage() : double.NaN),
((_event.GetPinnedObjectPercentage() != -1) ? _event.TotalPinnedPlugSize : double.NaN),
((_event.GetPinnedObjectPercentage() != -1) ? (_event.TotalPinnedPlugSize - _event.TotalUserPinnedPlugSize) : double.NaN)
);
}
}
writer.WriteLine("</Table>");
writer.WriteLine("</Center>");
}
private static void PrintEventCondemnedReasonsTable(TextWriter writer, TraceProcess stats, TraceLoadedDotNetRuntime runtime, int start = 0, Predicate<TraceGC> filter = null)
{
// Validate that we actually have condemned reasons information.
int missingPerHeapHistories = 0;
bool perHeapHistoryPresent = false;
for (int i = 0; i < runtime.GC.GCs.Count; i++)
{
if (runtime.GC.GCs[i].IsComplete)
{
if (runtime.GC.GCs[i].PerHeapHistories == null)
{
missingPerHeapHistories++;
// Allow up to 5 complete events without per-heap histories
// before we assume that we don't have any per-heap history information.
if (missingPerHeapHistories >= 5)
{
return;
}
continue;
}
perHeapHistoryPresent = true;
break;
}
}
// Ensure that we have per-heap history data before continuing.
if (!perHeapHistoryPresent)
{
return;
}
bool isServerGC = (runtime.GC.Stats().IsServerGCUsed == 1);
List<TraceGC> events = new List<TraceGC>();
List<byte[]> condemnedReasonRows = new List<byte[]>();
List<int> heapIndexes = isServerGC ? new List<int>() : null;
for (int i = start; i < runtime.GC.GCs.Count; i++)
{
var _event = runtime.GC.GCs[i];
if (filter == null || filter(_event))
{
if (!_event.IsComplete)
{
continue;
}
}
events.Add(_event);
int heapIndexHighestGen;
condemnedReasonRows.Add(GetCondemnedReasonRow(_event, out heapIndexHighestGen));
if (isServerGC)
{
heapIndexes.Add(heapIndexHighestGen);
}
}
bool hasAnyContent = false;
bool[] columnHasContent = new bool[CondemnedReasonsHtmlHeader.Length];
for (int j = 0; j < CondemnedReasonsHtmlHeader.Length; j++)
{
foreach (byte[] condemnedReasonRow in condemnedReasonRows)
{
if (condemnedReasonRow[j] != 0)
{
hasAnyContent = true;
columnHasContent[j] = true;
break;
}
}
}
writer.WriteLine("<HR/>");
writer.WriteLine("<H4>Condemned reasons for GCs</H4>");
if (hasAnyContent)
{
writer.WriteLine("<P>This table gives a more detailed account of exactly why a GC decided to collect that generation. ");
writer.WriteLine("Hover over the column headings for more info.</P>");
}
else
{
writer.WriteLine("<P>The trace contains events for the condemned reason but there is none.</P>");
return;
}
if (start != 0)
{
writer.WriteLine("<TR><TD colspan=\"26\" Align=\"Center\"> {0} Beginning entries truncated</TD></TR>", start);
}
writer.WriteLine("<Center>");
writer.WriteLine("<Table Border=\"1\">");
writer.WriteLine("<TR><TH>GC Index</TH>");
if (isServerGC)
{
writer.WriteLine("<TH>Heap<BR/>Index</TH>");
}
for (int i = 0; i < CondemnedReasonsHtmlHeader.Length; i++)
{
if (columnHasContent[i])
{
writer.WriteLine("<TH Title=\"{0}\">{1}</TH>",
CondemnedReasonsHtmlHeader[i][1],
CondemnedReasonsHtmlHeader[i][0]);
}
}
writer.WriteLine("</TR>");
for (int i = 0; i < events.Count; i++)
{
TraceGC _event = events[i];
byte[] condemnedReasons = condemnedReasonRows[i];
writer.WriteLine("<TR " + GetGenerationBackgroundColorAttribute(_event.Generation) + ">" +
"<TD Align=\"center\">{0}</TD>{1}</TR>",
_event.Number,
PrintCondemnedReasonsToHtml(((heapIndexes == null) ? null : (int?)heapIndexes[i]), condemnedReasons, columnHasContent));
}
writer.WriteLine("</Table>");
writer.WriteLine("</Center>");
}
private static void PrintLOHAllocLargePauseTable(TextWriter writer, TraceProcess stats, TraceLoadedDotNetRuntime runtime, int minPauseMSec)
{
// Find the first event that has the LOH alloc pause info.
int index;
for (index = 0; index < runtime.GC.GCs.Count; index++)
{
if ((runtime.GC.GCs[index].LOHWaitThreads != null) && (runtime.GC.GCs[index].LOHWaitThreads.Count != 0))
{
break;
}
}
if (index == runtime.GC.GCs.Count)
{
return;
}
writer.WriteLine("<Center>");
writer.WriteLine("<Table Border=\"1\">");
writer.WriteLine("<TR>" +
"<TH>BGC<BR/>Index</TH>" +
"<TH>Thread<BR/>ID</TH>" +
"<TH>Pause<BR/>Start</TH>" +
"<TH>Pause MSec</TH>" +
"<TH Title=\"LOH allocation has to wait when BGC is threading its free list; or if the app has already allocated enough on LOH\">Pause<BR/>Reason</TH>" +
"</TR>");
while (index < runtime.GC.GCs.Count)
{
TraceGC _event = runtime.GC.GCs[index];
if (_event.LOHWaitThreads != null)
{
int longPauseCount = 0;
Dictionary<int, BGCAllocWaitInfo>.ValueCollection infoCollection = _event.LOHWaitThreads.Values;
foreach (BGCAllocWaitInfo info in infoCollection)
{
// First pass to know how many rows we'll need to print.
if (info.IsLOHWaitLong(minPauseMSec))
{
longPauseCount++;
}
}
if (longPauseCount > 0)
{
writer.WriteLine("<TR><TD Align=\"right\" rowspan=\"{0}\">{1}</TD>", longPauseCount, _event.Number);
bool isFirstRow = true;
foreach (KeyValuePair<int, BGCAllocWaitInfo> kvp in _event.LOHWaitThreads)
{
BGCAllocWaitInfo info = kvp.Value;
// Second pass to actually print.
if (info.IsLOHWaitLong(minPauseMSec))
{
if (isFirstRow)
{
isFirstRow = false;
}
else
{
writer.WriteLine("<TR>");
}
writer.WriteLine("<TD>{0}</TD><TD>{1:n3}</TD><TD>{2:n3}</TD><TD>{3}</TD>",
kvp.Key,
kvp.Value.WaitStartRelativeMSec,
(kvp.Value.WaitStopRelativeMSec - kvp.Value.WaitStartRelativeMSec),
kvp.Value.ToString());
}
}
}
}
index++;