-
Notifications
You must be signed in to change notification settings - Fork 0
/
GCHeapDump.cs
1081 lines (959 loc) · 38.6 KB
/
GCHeapDump.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Security;
using System.Text.RegularExpressions;
using System.Xml;
using FastSerialization;
using Graphs;
using Address = System.UInt64;
/// <summary>
/// Represents a .GCDump file. You can open it for reading with the construtor
/// and you can write one with WriteMemoryGraph
/// </summary>
public class GCHeapDump : IFastSerializable, IFastSerializableVersion
{
public GCHeapDump(string inputFileName) :
this(new Deserializer(inputFileName, new SerializationConfiguration() { StreamLabelWidth = StreamLabelWidth.FourBytes }))
{ }
public GCHeapDump(Stream inputStream, string streamName) :
this(new Deserializer(inputStream, streamName, new SerializationConfiguration() { StreamLabelWidth = StreamLabelWidth.FourBytes }))
{ }
/// <summary>
/// Writes the memory graph 'graph' as a .gcump file to 'outputFileName'
/// 'toolName' is the name of the tool generating the data. It is persisted in the GCDump file
/// and can be used by the viewer to customize the view.
///
/// TODO can't set the rest of the meta-data associated with the graph this way.
/// </summary>
public static void WriteMemoryGraph(MemoryGraph graph, string outputFileName, string toolName = null)
{
GCHeapDump dumper = new(graph);
dumper.CreationTool = toolName;
dumper.Write(outputFileName);
}
/// <summary>
/// The
/// </summary>
public MemoryGraph MemoryGraph { get { return m_graph; } internal set { m_graph = value; } }
/// <summary>
/// Information about COM objects that is not contained in the MemoryGraph.
/// </summary>
public InteropInfo InteropInfo { get { return m_interop; } internal set { m_interop = value; } }
/// <summary>
/// TODO FIX NOW REMOVE DO NOT USE Use MemoryGraph.Is64Bit instead.
/// Was this dump taken from a 64 bit process
/// </summary>
public bool Is64Bit { get { return MemoryGraph.Is64Bit; } }
// sampling support.
/// <summary>
/// If we have sampled, sampleCount * ThisMultiplier = originalCount. If sampling not done then == 1
/// </summary>
public float AverageCountMultiplier { get; internal set; }
/// <summary>
/// If we have sampled sampledSize * thisMultiplier = originalSize. If sampling not done then == 1
/// </summary>
public float AverageSizeMultiplier { get; internal set; }
/// <summary>
/// This can be null. If non-null it indicates that only a sample of the GC graph was persisted in
/// the MemoryGraph filed. To get an approximation of the original heap, each type's count should be
/// scaled CountMultipliersByType[T] to get the unsampled count of the original heap.
///
/// We can't use a uniform number for all types because we want to see all large objects, and we
/// want to include paths to root for all objects, which means we can only approximate a uniform scaling.
/// </summary>
public float[] CountMultipliersByType { get; internal set; }
public DotNetHeapInfo DotNetHeapInfo { get; internal set; }
public JSHeapInfo JSHeapInfo { get; internal set; }
/// <summary>
/// This is the log file that was generated at the time of collection
/// </summary>
public string CollectionLog { get; internal set; }
public DateTime TimeCollected { get; internal set; }
public string MachineName { get; internal set; }
public string ProcessName { get; internal set; }
public int ProcessID { get; internal set; }
public long TotalProcessCommit { get; internal set; }
public long TotalProcessWorkingSet { get; internal set; }
/// <summary>
/// Returns a string that represents the tool that created this GCDump file. May be null if not known/supported.
/// </summary>
public string CreationTool { get; set; }
public struct ProcessInfo
{
public int ID;
public bool UsesDotNet;
public bool UsesJavaScript;
}
/// <summary>
/// returns a list of ProcessInfos that indicate which processes
/// have use a runtime .NET or JavaScript that we can potentially dump
///
/// Note that for 64 bit systems this will ONLY return processes that
/// have the same bitness as the current process (for PerfView it is 32 bit)
/// </summary>
public static Dictionary<int, ProcessInfo> GetProcessesWithGCHeaps()
{
Dictionary<int, ProcessInfo> ret = new();
// Do the 64 bit processes first, then do us
if (System.Environment.Is64BitOperatingSystem && !System.Environment.Is64BitProcess)
{
GetProcessesWithGCHeapsFromHeapDump(ret);
}
ProcessInfo info = default(ProcessInfo);
foreach (Process process in Process.GetProcesses())
{
try
{
if (process == null)
{
continue;
}
info.ID = process.Id;
if (info.ID is 0 or 4) // these are special and cause failures otherwise
{
continue;
}
info.UsesDotNet = false;
info.UsesJavaScript = false;
foreach (ProcessModule module in process.Modules)
{
if (module == null)
{
continue;
}
string fileName = module.FileName;
if (fileName.EndsWith("clr.dll", StringComparison.OrdinalIgnoreCase))
{
info.UsesDotNet = true;
}
else if (fileName.EndsWith("coreclr.dll", StringComparison.OrdinalIgnoreCase))
{
info.UsesDotNet = true;
}
else if (fileName.EndsWith("mscorwks.dll", StringComparison.OrdinalIgnoreCase))
{
info.UsesDotNet = true;
}
else if (0 <= fileName.IndexOf(@"\mrt", StringComparison.OrdinalIgnoreCase))
{
info.UsesDotNet = true;
}
else if (fileName.EndsWith("jscript9.dll", StringComparison.OrdinalIgnoreCase))
{
info.UsesJavaScript = true;
}
else if (fileName.EndsWith("chakra.dll", StringComparison.OrdinalIgnoreCase))
{
info.UsesJavaScript = true;
}
}
}
catch (Exception)
{
}
if (info.UsesJavaScript || info.UsesDotNet)
{
// Merge with previous values.
if (ret.TryGetValue(info.ID, out ProcessInfo prev))
{
info.UsesDotNet |= prev.UsesDotNet;
info.UsesJavaScript |= prev.UsesJavaScript;
}
ret[info.ID] = info;
}
}
return ret;
}
#region private
/// <summary>
/// Writes the data to 'outputFileName'
/// </summary>
private void Write(string outputFileName)
{
Debug.Assert(MemoryGraph != null);
Serializer serializer = new(new IOStreamStreamWriter(outputFileName, config: new SerializationConfiguration() { StreamLabelWidth = StreamLabelWidth.FourBytes }), this);
serializer.Close();
}
// Creation APIs
internal GCHeapDump(MemoryGraph graph)
{
m_graph = graph;
AverageCountMultiplier = 1;
AverageSizeMultiplier = 1;
}
// For serialization
private GCHeapDump() { }
private GCHeapDump(Deserializer deserializer)
{
deserializer.RegisterFactory(typeof(MemoryGraph), delegate { return new MemoryGraph(1); });
deserializer.RegisterFactory(typeof(Module), delegate { return new Module(0); });
deserializer.RegisterFactory(typeof(InteropInfo), delegate { return new InteropInfo(); });
deserializer.RegisterFactory(typeof(GCHeapDump), delegate { return this; });
deserializer.RegisterFactory(typeof(GCHeapDumpSegment), delegate { return new GCHeapDumpSegment(); });
deserializer.RegisterFactory(typeof(JSHeapInfo), delegate { return new JSHeapInfo(); });
deserializer.RegisterFactory(typeof(DotNetHeapInfo), delegate { return new DotNetHeapInfo(); });
try
{
GCHeapDump entryObj = (GCHeapDump)deserializer.GetEntryObject();
Debug.Assert(entryObj == this);
}
catch (Exception e)
{
throw new ApplicationException("Error opening file " + deserializer.Name + " Message: " + e.Message);
}
}
private static void GetProcessesWithGCHeapsFromHeapDump(Dictionary<int, ProcessInfo> ret)
{
#if PERFVIEW
// TODO FIX NOW, need to work for PerfView64
var heapDumpExe = Path.Combine(Utilities.SupportFiles.SupportFileDir, @"amd64\HeapDump.exe");
var cmd = Utilities.Command.Run(heapDumpExe + " /GetProcessesWithGCHeaps");
var info = new ProcessInfo();
int idx = 0;
var output = cmd.Output;
for (; ; )
{
var newLineIdx = output.IndexOf('\n', idx);
if (newLineIdx < 0)
break;
if (idx + 5 <= newLineIdx && output[idx] != '#')
{
info.UsesDotNet = (output[idx] == 'N');
info.UsesJavaScript = (output[idx + 1] == 'J');
var idStr = output.Substring(idx + 3, newLineIdx - idx - 4);
if (int.TryParse(idStr, out info.ID))
ret[info.ID] = info;
}
idx = newLineIdx + 1;
}
#endif
}
void IFastSerializable.ToStream(Serializer serializer)
{
serializer.Write(m_graph);
serializer.Write(m_graph.Is64Bit); // This is redundant but graph did not used to hold this value
// we write the bit here to preserve compatibility.
serializer.Write(AverageCountMultiplier);
serializer.Write(AverageSizeMultiplier);
serializer.Write(JSHeapInfo);
serializer.Write(DotNetHeapInfo);
serializer.Write(CollectionLog);
serializer.Write(TimeCollected.Ticks);
serializer.Write(MachineName);
serializer.Write(ProcessName);
serializer.Write(ProcessID);
serializer.Write(TotalProcessCommit);
serializer.Write(TotalProcessWorkingSet);
if (CountMultipliersByType == null)
{
serializer.Write(0);
}
else
{
serializer.Write(CountMultipliersByType.Length);
for (int i = 0; i < CountMultipliersByType.Length; i++)
{
serializer.Write(CountMultipliersByType[i]);
}
}
// All fields after version 8 should go here and should be in
// the version order (thus always add at the end). Also use the
// WriteTagged variation to write.
serializer.WriteTagged(m_interop);
serializer.WriteTagged(CreationTool);
}
void IFastSerializable.FromStream(Deserializer deserializer)
{
// This is the old crufy way of reading things in. We can abandon this eventually
if (deserializer.VersionBeingRead < 8)
{
PreVersion8FromStream(deserializer);
return;
}
if (deserializer.VersionBeingRead == 8)
{
throw new SerializationException("Unsupported version GCDump version: 8");
}
deserializer.Read(out m_graph);
deserializer.ReadBool(); // Used to be Is64Bit but that is now on m_graph and we want to keep compatibility.
AverageCountMultiplier = deserializer.ReadFloat();
AverageSizeMultiplier = deserializer.ReadFloat();
JSHeapInfo = (JSHeapInfo)deserializer.ReadObject();
DotNetHeapInfo = (DotNetHeapInfo)deserializer.ReadObject();
CollectionLog = deserializer.ReadString();
TimeCollected = new DateTime(deserializer.ReadInt64());
MachineName = deserializer.ReadString();
ProcessName = deserializer.ReadString();
ProcessID = deserializer.ReadInt();
TotalProcessCommit = deserializer.ReadInt64();
TotalProcessWorkingSet = deserializer.ReadInt64();
deserializer.Read(out int count);
if (count != 0)
{
float[] a = new float[count];
for (int i = 0; i < a.Length; i++)
{
a[i] = deserializer.ReadFloat();
}
CountMultipliersByType = a;
}
// Things after version 8 go here. Always add the the end, and it should always work
// and use the tagged variation.
deserializer.TryReadTagged(ref m_interop);
string creationTool = null;
deserializer.TryReadTagged(ref creationTool);
CreationTool = creationTool;
}
/// <summary>
/// Deals with legacy formats. We should be able to get rid of eventually.
/// </summary>
private void PreVersion8FromStream(Deserializer deserializer)
{
DotNetHeapInfo = new DotNetHeapInfo();
deserializer.Read(out m_graph);
DotNetHeapInfo.SizeOfAllSegments = deserializer.ReadInt64();
deserializer.ReadInt64(); // Size of dumped objects
deserializer.ReadInt64(); // Number of dumped objects
deserializer.ReadBool(); // All objects dumped
if (deserializer.VersionBeingRead >= 5)
{
CollectionLog = deserializer.ReadString();
TimeCollected = new DateTime(deserializer.ReadInt64());
MachineName = deserializer.ReadString();
ProcessName = deserializer.ReadString();
ProcessID = deserializer.ReadInt();
TotalProcessCommit = deserializer.ReadInt64();
TotalProcessWorkingSet = deserializer.ReadInt64();
if (deserializer.VersionBeingRead >= 6)
{
// Skip the segments
int count = deserializer.ReadInt();
for (int i = 0; i < count; i++)
{
deserializer.ReadObject();
}
if (deserializer.VersionBeingRead >= 7)
{
deserializer.ReadBool(); // Is64bit
}
}
}
}
int IFastSerializableVersion.Version
{
// As long as we are on a tagged plan, we don't really have to increment this because
// the tagged values we put in the stream do this for us, but it does not hurt and
// acts as good documentation so we do increment it when we change things.
get { return 10; }
}
int IFastSerializableVersion.MinimumVersionCanRead
{
// We support back to version 4
get { return 4; }
}
int IFastSerializableVersion.MinimumReaderVersion
{
// Since version 8 we are on a Tagged plan
get { return 8; }
}
private MemoryGraph m_graph;
private InteropInfo m_interop;
#endregion
}
public class JSHeapInfo : IFastSerializable
{
#region private
void IFastSerializable.ToStream(Serializer serializer)
{
}
void IFastSerializable.FromStream(Deserializer deserializer)
{
}
#endregion
}
public class InteropInfo : IFastSerializable
{
public class RCWInfo
{
internal NodeIndex node;
internal int refCount;
internal ulong addrIUnknown;
internal ulong addrJupiter;
internal ulong addrVTable;
internal int firstComInf;
internal int countComInf;
}
public class CCWInfo
{
internal NodeIndex node;
internal int refCount;
internal ulong addrIUnknown;
internal ulong addrHandle;
internal int firstComInf;
internal int countComInf;
}
public class ComInterfaceInfo
{
internal bool fRCW;
internal int owner;
internal NodeTypeIndex typeID;
internal ulong addrInterface;
internal ulong addrFirstVTable;
internal ulong addrFirstFunc;
}
public class InteropModuleInfo
{
public ulong baseAddress;
public uint fileSize;
public uint timeStamp;
public string fileName;
public int loadOrder; // unused when serializing
private string _moduleName; // unused when serializing
public string moduleName
{
get
{
if (_moduleName == null)
{
int pos = fileName.LastIndexOf('\\');
if (pos < 0)
{
pos = fileName.LastIndexOf(':');
}
if (pos > 0)
{
_moduleName = fileName.Substring(pos + 1);
}
else
{
_moduleName = fileName;
}
}
return _moduleName;
}
}
public static int CompareBase(InteropModuleInfo one, InteropModuleInfo two)
{
return (int)(one.baseAddress - two.baseAddress);
}
}
internal int m_countRCWs;
internal int m_countCCWs;
internal int m_countInterfaces;
internal int m_countRCWInterfaces; // only used in the deserializing case.
internal int m_countModules;
internal List<RCWInfo> m_listRCWInfo;
internal List<CCWInfo> m_listCCWInfo;
internal List<ComInterfaceInfo> m_listComInterfaceInfo;
internal List<InteropModuleInfo> m_listModules;
public InteropInfo(bool fInitLater = false)
{
if (!fInitLater)
{
m_listRCWInfo = new List<RCWInfo>();
m_listCCWInfo = new List<CCWInfo>();
m_listComInterfaceInfo = new List<ComInterfaceInfo>();
m_listModules = new List<InteropModuleInfo>();
}
}
public int currentRCWCount { get { return m_listRCWInfo.Count; } }
public int currentCCWCount { get { return m_listCCWInfo.Count; } }
public int currentInterfaceCount { get { return m_listComInterfaceInfo.Count; } }
public int currentModuleCount { get { return m_listModules.Count; } }
public void AddRCW(RCWInfo rcwInfo)
{
m_listRCWInfo.Add(rcwInfo);
}
public void AddCCW(CCWInfo ccwInfo)
{
m_listCCWInfo.Add(ccwInfo);
}
public void AddComInterface(ComInterfaceInfo interfaceInfo)
{
m_listComInterfaceInfo.Add(interfaceInfo);
}
public void AddModule(InteropModuleInfo moduleInfo)
{
m_listModules.Add(moduleInfo);
}
public bool InteropInfoExists()
{
return ((currentRCWCount != 0) || (currentCCWCount != 0));
}
// The format we are writing out is:
// total # of RCWs/CCWs. If this is 0, it means there's no interop info.
// # of RCWs
// # of CCWs
// # of interfaces
// # of modules
// RCWs
// CCWs
// Interfaces
// Modules
void IFastSerializable.ToStream(Serializer serializer)
{
int countRCWCCW = m_listRCWInfo.Count + m_listCCWInfo.Count;
serializer.Write(countRCWCCW);
if (countRCWCCW == 0)
{
return;
}
serializer.Write(m_listRCWInfo.Count);
serializer.Write(m_listCCWInfo.Count);
serializer.Write(m_listComInterfaceInfo.Count);
serializer.Write(m_listModules.Count);
for (int i = 0; i < m_listRCWInfo.Count; i++)
{
serializer.Write((int)m_listRCWInfo[i].node);
serializer.Write(m_listRCWInfo[i].refCount);
serializer.Write((long)m_listRCWInfo[i].addrIUnknown);
serializer.Write((long)m_listRCWInfo[i].addrJupiter);
serializer.Write((long)m_listRCWInfo[i].addrVTable);
serializer.Write(m_listRCWInfo[i].firstComInf);
serializer.Write(m_listRCWInfo[i].countComInf);
}
for (int i = 0; i < m_listCCWInfo.Count; i++)
{
serializer.Write((int)m_listCCWInfo[i].node);
serializer.Write(m_listCCWInfo[i].refCount);
serializer.Write((long)m_listCCWInfo[i].addrIUnknown);
serializer.Write((long)m_listCCWInfo[i].addrHandle);
serializer.Write(m_listCCWInfo[i].firstComInf);
serializer.Write(m_listCCWInfo[i].countComInf);
}
for (int i = 0; i < m_listComInterfaceInfo.Count; i++)
{
serializer.Write(m_listComInterfaceInfo[i].fRCW ? (byte)1 : (byte)0);
serializer.Write(m_listComInterfaceInfo[i].owner);
serializer.Write((int)m_listComInterfaceInfo[i].typeID);
serializer.Write((long)m_listComInterfaceInfo[i].addrInterface);
serializer.Write((long)m_listComInterfaceInfo[i].addrFirstVTable);
serializer.Write((long)m_listComInterfaceInfo[i].addrFirstFunc);
}
for (int i = 0; i < m_listModules.Count; i++)
{
serializer.Write((long)m_listModules[i].baseAddress);
serializer.Write((int)m_listModules[i].fileSize);
serializer.Write((int)m_listModules[i].timeStamp);
serializer.Write(m_listModules[i].fileName);
}
}
void IFastSerializable.FromStream(Deserializer deserializer)
{
int countRCWCCW = deserializer.ReadInt();
if (countRCWCCW == 0)
{
return;
}
m_countRCWs = deserializer.ReadInt();
m_countCCWs = deserializer.ReadInt();
m_countInterfaces = deserializer.ReadInt();
m_countModules = deserializer.ReadInt();
m_listRCWInfo = new List<RCWInfo>(m_countRCWs);
m_listCCWInfo = new List<CCWInfo>(m_countCCWs);
m_listComInterfaceInfo = new List<ComInterfaceInfo>(m_countInterfaces);
m_listModules = new List<InteropModuleInfo>(m_countModules);
m_countRCWInterfaces = 0;
for (int i = 0; i < m_countRCWs; i++)
{
RCWInfo infoRCW = new();
infoRCW.node = (NodeIndex)deserializer.ReadInt();
infoRCW.refCount = deserializer.ReadInt();
infoRCW.addrIUnknown = (ulong)deserializer.ReadInt64();
infoRCW.addrJupiter = (ulong)deserializer.ReadInt64();
infoRCW.addrVTable = (ulong)deserializer.ReadInt64();
infoRCW.firstComInf = deserializer.ReadInt();
infoRCW.countComInf = deserializer.ReadInt();
m_listRCWInfo.Add(infoRCW);
m_countRCWInterfaces += infoRCW.countComInf;
}
for (int i = 0; i < m_countCCWs; i++)
{
CCWInfo infoCCW = new();
infoCCW.node = (NodeIndex)deserializer.ReadInt();
infoCCW.refCount = deserializer.ReadInt();
infoCCW.addrIUnknown = (ulong)deserializer.ReadInt64();
infoCCW.addrHandle = (ulong)deserializer.ReadInt64();
infoCCW.firstComInf = deserializer.ReadInt();
infoCCW.countComInf = deserializer.ReadInt();
m_listCCWInfo.Add(infoCCW);
}
for (int i = 0; i < m_countInterfaces; i++)
{
ComInterfaceInfo infoInterface = new();
infoInterface.fRCW = ((deserializer.ReadByte() == 1) ? true : false);
infoInterface.owner = deserializer.ReadInt();
infoInterface.typeID = (NodeTypeIndex)deserializer.ReadInt();
infoInterface.addrInterface = (ulong)deserializer.ReadInt64();
infoInterface.addrFirstVTable = (ulong)deserializer.ReadInt64();
infoInterface.addrFirstFunc = (ulong)deserializer.ReadInt64();
m_listComInterfaceInfo.Add(infoInterface);
}
for (int i = 0; i < m_countModules; i++)
{
InteropModuleInfo infoModule = new();
infoModule.baseAddress = (ulong)deserializer.ReadInt64();
infoModule.fileSize = (uint)deserializer.ReadInt();
infoModule.timeStamp = (uint)deserializer.ReadInt();
deserializer.Read(out infoModule.fileName);
infoModule.loadOrder = i;
m_listModules.Add(infoModule);
}
m_listModules.Sort(InteropModuleInfo.CompareBase);
}
}
/// <summary>
/// Reads the format as an XML file. It it is a very simple format Here is an example.
/// <graph>
/// <rootIndex>3</rootIndex>
/// <nodeTypes>
/// <nodeType Index="1" Size="10" Name="Type1" Module="MyModule" />
/// <nodeType Index="2" Size="1" Name="Type2" />
/// <nodeType Index="3" Size="20" Name="Type3" />
/// <nodeType Index="4" Size="10" Name="Type4" Module="MyModule" />
/// </nodeTypes>
/// <nodes>
/// <node Index="1" Size="100" TypeIndex="1" >2</node>
/// <node Index="2" TypeIndex="1" >3</node>
/// <node Index="3" TypeIndex="1" >1 4</node>
/// <node Index="4" TypeIndex="4" >2</node>
/// </nodes>
/// </graph>
///
/// </summary>
internal static class XmlGcHeapDump
{
public static GCHeapDump ReadGCHeapDumpFromXml(string fileName)
{
XmlReaderSettings settings = new() { IgnoreWhitespace = true, IgnoreComments = true };
using (XmlReader reader = XmlReader.Create(fileName, settings))
{
reader.ReadToDescendant("GCHeapDump");
return ReadGCHeapDumpFromXml(reader);
}
}
public static GCHeapDump ReadGCHeapDumpFromXml(XmlReader reader)
{
if (reader.NodeType != XmlNodeType.Element)
{
throw new InvalidOperationException("Must advance to GCHeapDump element (e.g. call ReadToDescendant)");
}
string elementName = reader.Name;
int inputDepth = reader.Depth;
reader.Read(); // Advance to children
GCHeapDump ret = new((MemoryGraph)null);
while (inputDepth < reader.Depth)
{
if (reader.NodeType == XmlNodeType.Element)
{
switch (reader.Name)
{
case "MemoryGraph":
ret.MemoryGraph = ReadMemoryGraphFromXml(reader);
break;
case "CollectionLog":
ret.CollectionLog = reader.ReadElementContentAsString();
break;
case "TimeCollected":
ret.TimeCollected = DateTime.Parse(reader.ReadElementContentAsString());
break;
case "MachineName":
ret.MachineName = reader.ReadElementContentAsString();
break;
case "ProcessName":
ret.ProcessName = reader.ReadElementContentAsString();
break;
case "ProcessID":
ret.ProcessID = reader.ReadElementContentAsInt();
break;
case "CountMultipliersByType":
List<float> multipliers = new();
ReadCountMultipliersByTypeFromXml(reader, multipliers);
ret.CountMultipliersByType = multipliers.ToArray();
break;
case "TotalProcessCommit":
ret.TotalProcessCommit = reader.ReadElementContentAsLong();
break;
case "TotalProcessWorkingSet":
ret.TotalProcessWorkingSet = reader.ReadElementContentAsLong();
break;
default:
Debug.WriteLine("Skipping unknown element {0}", reader.Name);
reader.Skip();
break;
}
}
else if (!reader.Read())
{
break;
}
}
if (ret.MemoryGraph == null)
{
throw new ApplicationException(elementName + " does not have MemoryGraph field.");
}
return ret;
}
public static MemoryGraph ReadMemoryGraphFromXml(XmlReader reader)
{
if (reader.NodeType != XmlNodeType.Element)
{
throw new InvalidOperationException("Must advance to MemoryGraph element (e.g. call ReadToDescendant)");
}
MemoryGraph graph = new(10);
Debug.Assert((int)graph.NodeTypeIndexLimit == 1);
NodeIndex firstNode = graph.CreateNode(); // Use one up
Debug.Assert(firstNode == 0);
Debug.Assert((int)graph.NodeIndexLimit == 1);
int inputDepth = reader.Depth;
reader.Read(); // Advance to children
while (inputDepth < reader.Depth)
{
if (reader.NodeType == XmlNodeType.Element)
{
switch (reader.Name)
{
case "NodeTypes":
ReadNodeTypesFromXml(reader, graph);
break;
case "Nodes":
ReadNodesFromXml(reader, graph);
break;
case "RootIndex":
graph.RootIndex = (NodeIndex)reader.ReadElementContentAsInt();
break;
default:
Debug.WriteLine("Skipping unknown element {0}", reader.Name);
reader.Skip();
break;
}
}
else if (!reader.Read())
{
break;
}
}
graph.AllowReading();
return graph;
}
internal static void WriteGCDumpToXml(GCHeapDump gcDump, StreamWriter writer)
{
writer.WriteLine("<GCHeapDump>");
writer.WriteLine("<TimeCollected>{0}</TimeCollected>", gcDump.TimeCollected);
if (!string.IsNullOrWhiteSpace(gcDump.CollectionLog))
{
writer.WriteLine("<CollectionLog>{0}</CollectionLog>", SecurityElement.Escape(gcDump.CollectionLog));
}
if (!string.IsNullOrWhiteSpace(gcDump.MachineName))
{
writer.WriteLine("<MachineName>{0}</MachineName>", gcDump.MachineName);
}
if (!string.IsNullOrWhiteSpace(gcDump.ProcessName))
{
writer.WriteLine("<ProcessName>{0}</ProcessName>", SecurityElement.Escape(gcDump.ProcessName));
}
if (gcDump.ProcessID != 0)
{
writer.WriteLine("<ProcessName>{0}</ProcessName>", gcDump.ProcessID);
}
if (gcDump.TotalProcessCommit != 0)
{
writer.WriteLine("<TotalProcessCommit>{0}</TotalProcessCommit>", gcDump.TotalProcessCommit);
}
if (gcDump.TotalProcessWorkingSet != 0)
{
writer.WriteLine("<TotalProcessWorkingSet>{0}</TotalProcessWorkingSet>", gcDump.TotalProcessWorkingSet);
}
if (gcDump.CountMultipliersByType != null)
{
NodeType typeStorage = gcDump.MemoryGraph.AllocTypeNodeStorage();
writer.WriteLine("<CountMultipliersByType>");
for (int i = 0; i < gcDump.CountMultipliersByType.Length; i++)
{
writer.WriteLine("<CountMultipliers TypeIndex=\"{0}\" TypeName=\"{1}\" Value=\"{2:f4}\"/>", i,
SecurityElement.Escape(gcDump.MemoryGraph.GetType((NodeTypeIndex)i, typeStorage).Name),
gcDump.CountMultipliersByType[i]);
}
writer.WriteLine("</CountMultipliersByType>");
}
// TODO this is not complete. See the ToStream for more. Does not include interop etc.
// Write the memory graph, which is the main event.
gcDump.MemoryGraph.WriteXml(writer);
writer.WriteLine("</GCHeapDump>");
}
#region private
private static void ReadCountMultipliersByTypeFromXml(XmlReader reader, List<float> countMultipliers)
{
Debug.Assert(reader.NodeType == XmlNodeType.Element);
int inputDepth = reader.Depth;
reader.Read(); // Advance to children
while (inputDepth < reader.Depth)
{
if (reader.NodeType == XmlNodeType.Element)
{
switch (reader.Name)
{
case "CountMultiplier":
countMultipliers.Add(FetchFloat(reader, "Value", 1));
reader.Skip();
break;
default:
Debug.WriteLine("Skipping unknown element {0}", reader.Name);
reader.Skip();
break;
}
}
else if (!reader.Read())
{
break;
}
}
}
/// <summary>
/// Reads the NodeTypes element
/// </summary>
private static void ReadNodeTypesFromXml(XmlReader reader, MemoryGraph graph)
{
Debug.Assert(reader.NodeType == XmlNodeType.Element);
int inputDepth = reader.Depth;
reader.Read(); // Advance to children
while (inputDepth < reader.Depth)
{
if (reader.NodeType == XmlNodeType.Element)
{
switch (reader.Name)
{
case "NodeType":
{
NodeTypeIndex readTypeIndex = (NodeTypeIndex)FetchInt(reader, "Index", -1);
int size = FetchInt(reader, "Size");
string typeName = reader.GetAttribute("Name");
string moduleName = reader.GetAttribute("Module");
if (typeName == null)
{
throw new ApplicationException("NodeType element does not have a Name attribute");
}
if (readTypeIndex == NodeTypeIndex.Invalid)
{
throw new ApplicationException("NodeType element does not have a Index attribute.");
}
if (readTypeIndex != 0 || typeName != "UNDEFINED")
{
NodeTypeIndex typeIndex = graph.CreateType(typeName, moduleName, size);
if (readTypeIndex != typeIndex)
{
throw new ApplicationException("NodeType Indexes do not start at 1 and increase consecutively.");
}
}
reader.Skip();
}
break;
default:
Debug.WriteLine("Skipping unknown element {0}", reader.Name);
reader.Skip();
break;
}
}
else if (!reader.Read())
{
break;
}
}
}
/// <summary>
/// Reads the Nodes Element
/// </summary>
private static void ReadNodesFromXml(XmlReader reader, MemoryGraph graph)
{
Debug.Assert(reader.NodeType == XmlNodeType.Element);
int inputDepth = reader.Depth;
reader.Read(); // Advance to children
GrowableArray<NodeIndex> children = new(1000);
NodeType typeStorage = graph.AllocTypeNodeStorage();
while (inputDepth < reader.Depth)
{
if (reader.NodeType == XmlNodeType.Element)
{
switch (reader.Name)
{
case "Node":
{
NodeIndex readNodeIndex = (NodeIndex)FetchInt(reader, "Index", -1);
NodeTypeIndex typeIndex = (NodeTypeIndex)FetchInt(reader, "TypeIndex", -1);
int size = FetchInt(reader, "Size");
if (readNodeIndex == NodeIndex.Invalid)
{
throw new ApplicationException("Node element does not have a Index attribute.");