Exemplo n.º 1
0
        public void WriteSizeTrack(SizeTracker tracker)
        {
            var streamPos = _stream.Position;
            int length    = (int)((streamPos + _position) - tracker);

            if ((int)tracker <= streamPos)
            {
                _trackBytes[0] = (byte)length;
                _trackBytes[1] = (byte)(length >> 8);
                _trackBytes[2] = (byte)(length >> 16);
                _trackBytes[3] = (byte)(length >> 24);

                _stream.Position = (long)tracker - 4;
                _stream.Write(_trackBytes, 0, 4);
                _stream.Position = streamPos;
            }
            else
            {
                var offset = (int)(tracker - streamPos);

                _buffer[offset - 4] = (byte)length;
                _buffer[offset - 3] = (byte)(length >> 8);
                _buffer[offset - 2] = (byte)(length >> 16);
                _buffer[offset - 1] = (byte)(length >> 24);
            }
        }
 public override void Write(Utf8JsonWriter writer, ITypeData value, JsonSerializerOptions options)
 {
     // We basically want to verbosely write out our this typeref, everything else should use the simple type ref converter.
     // Everything else in this type should also just be serialized normally.
     writer.WriteStartObject();
     writer.WritePropertyName(nameof(value.This));
     WriteThis(writer, value.This, options);
     // Write each of the other properties explicitly by converting
     writer.WritePropertyName(nameof(value.Attributes));
     JsonSerializer.Serialize(writer, value.Attributes, options);
     writer.WritePropertyName(nameof(value.ImplementingInterfaces));
     JsonSerializer.Serialize(writer, value.ImplementingInterfaces, options);
     writer.WritePropertyName(nameof(value.InstanceFields));
     JsonSerializer.Serialize(writer, value.InstanceFields, options);
     writer.WritePropertyName(nameof(value.Layout));
     JsonSerializer.Serialize(writer, value.Layout, options);
     writer.WritePropertyName(nameof(value.Methods));
     JsonSerializer.Serialize(writer, value.Methods, options);
     writer.WritePropertyName(nameof(value.NestedTypes));
     JsonSerializer.Serialize(writer, value.NestedTypes, options);
     writer.WritePropertyName(nameof(value.Parent));
     JsonSerializer.Serialize(writer, value.Parent, options);
     writer.WritePropertyName(nameof(value.Properties));
     JsonSerializer.Serialize(writer, value.Properties, options);
     writer.WritePropertyName(nameof(value.Specifiers));
     JsonSerializer.Serialize(writer, value.Specifiers, options);
     writer.WritePropertyName(nameof(value.StaticFields));
     JsonSerializer.Serialize(writer, value.StaticFields, options);
     writer.WritePropertyName(nameof(value.Type));
     JsonSerializer.Serialize(writer, value.Type, options);
     writer.WritePropertyName(nameof(value.TypeDefIndex));
     JsonSerializer.Serialize(writer, value.TypeDefIndex, options);
     writer.WriteNumber("Size", SizeTracker.GetSize(types, value));
     writer.WriteEndObject();
 }
Exemplo n.º 3
0
            public static HeapAllocationSizes[] GetProcessHeapSizes(string dmpDir)
            {
                if (HeapDiagnostics && dmpDir != null)
                {
                    SizeTracker.NextDump();
                    TRACK_SIZES.ForEach(t => t.OpenDumpFile(dmpDir));
                }
                var count  = GetProcessHeaps(0, null);
                var buffer = new IntPtr[count];

                GetProcessHeaps(count, buffer);
                var sizes = new HeapAllocationSizes[count];

                for (int i = 0; i < count; i++)
                {
                    var committedSizes = HeapDiagnostics ? new Dictionary <long, int>() : null;
                    var stringCounts   = HeapDiagnostics ? new Dictionary <string, int>() : null;

                    var h = buffer[i];
                    HeapLock(h);
                    var e = new PROCESS_HEAP_ENTRY();
                    while (HeapWalk(h, ref e))
                    {
                        if ((e.wFlags & PROCESS_HEAP_ENTRY_WFLAGS.PROCESS_HEAP_ENTRY_BUSY) != 0)
                        {
                            sizes[i].Committed += e.cbData + e.cbOverhead;

                            if (committedSizes != null)
                            {
                                // Update count
                                if (!committedSizes.ContainsKey(e.cbData))
                                {
                                    committedSizes[e.cbData] = 0;
                                }
                                committedSizes[e.cbData]++;
                            }

                            if (stringCounts != null)
                            {
                                // Find string(s)
                                var    byteData = new byte[e.cbData];
                                IntPtr pData    = e.lpData;
                                Marshal.Copy(pData, byteData, 0, byteData.Length);
                                TRACK_SIZES.ForEach(t => t.DumpUnseenSize(pData, byteData));
                                foreach (var byteString in FindStrings(byteData))
                                {
                                    if (!stringCounts.ContainsKey(byteString))
                                    {
                                        stringCounts[byteString] = 0;
                                    }
                                    stringCounts[byteString]++;
                                }
                            }
                        }
                        else if ((e.wFlags & PROCESS_HEAP_ENTRY_WFLAGS.PROCESS_HEAP_UNCOMMITTED_RANGE) != 0)
                        {
                            sizes[i].Reserved += e.cbData + e.cbOverhead;
                        }
                        else
                        {
                            sizes[i].Unknown += e.cbData + e.cbOverhead;
                        }
                    }
                    HeapUnlock(h);

                    if (committedSizes != null)
                    {
                        sizes[i].CommittedSizes = committedSizes.OrderByDescending(p => p.Value).ToList();
                    }
                    if (stringCounts != null)
                    {
                        sizes[i].StringCounts = stringCounts.OrderByDescending(p => p.Value).ToList();
                    }
                }
                if (HeapDiagnostics)
                {
                    TRACK_SIZES.ForEach(t => t.CloseDumpFile());
                }
                return(sizes);
            }