/// <summary> /// Loads a memory snapshot from the specified 'filePath' and stores the result in 'snapshot'. /// </summary> /// <param name="filePath">Absolute file path</param> public bool LoadFromFile(string filePath) { busyString = "Loading"; using (var fileStream = new System.IO.FileStream(filePath, System.IO.FileMode.Open)) { using (var reader = new System.IO.BinaryReader(fileStream)) { try { PackedMemorySnapshotHeader.Read(reader, out header, out busyString); if (!header.isValid) { throw new Exception("Invalid header."); } PackedNativeType.Read(reader, out nativeTypes, out busyString); PackedNativeUnityEngineObject.Read(reader, out nativeObjects, out busyString); PackedGCHandle.Read(reader, out gcHandles, out busyString); PackedConnection.Read(reader, out connections, out busyString); PackedMemorySection.Read(reader, out managedHeapSections, out busyString); PackedManagedType.Read(reader, out managedTypes, out busyString); PackedVirtualMachineInformation.Read(reader, out virtualMachineInformation, out busyString); } catch (System.Exception e) { Debug.LogException(e); return(false); } } } return(true); }
public static void Read(System.IO.BinaryReader reader, out PackedMemorySection[] value, out string stateString) { value = new PackedMemorySection[0]; stateString = ""; var version = reader.ReadInt32(); if (version >= 1) { var length = reader.ReadInt32(); value = new PackedMemorySection[length]; var onePercent = Math.Max(1, value.Length / 100); for (int n = 0, nend = value.Length; n < nend; ++n) { if ((n % onePercent) == 0) { stateString = string.Format("Loading Managed Heap Sections\n{0}/{1}, {2:F0}% done", n + 1, length, ((n + 1) / (float)length) * 100); } var count = reader.ReadInt32(); value[n].bytes = reader.ReadBytes(count); value[n].startAddress = reader.ReadUInt64(); value[n].arrayIndex = -1; } } }
public void Inspect(PackedMemorySection item) { var job = new Job { snapshot = snapshot, memorySection = item, referencedByControl = m_ReferencedByControl, referencesControl = m_ReferencesControl }; ScheduleJob(job); }
/// <summary> /// Converts an Unity PackedMemorySnapshot to our own format. /// </summary> public static PackedMemorySnapshot FromMemoryProfiler(MemorySnapshotProcessingArgs args) { var source = args.source; var value = new PackedMemorySnapshot(); try { VerifyMemoryProfilerSnapshot(source); value.busyString = "Loading Header"; value.header = PackedMemorySnapshotHeader.FromMemoryProfiler(); value.busyString = string.Format("Loading {0} Native Types", source.nativeTypes.Length); value.nativeTypes = PackedNativeType.FromMemoryProfiler(source.nativeTypes); value.busyString = string.Format("Loading {0} Native Objects", source.nativeObjects.Length); value.nativeObjects = PackedNativeUnityEngineObject.FromMemoryProfiler(source.nativeObjects); value.busyString = string.Format("Loading {0} GC Handles", source.gcHandles.Length); value.gcHandles = PackedGCHandle.FromMemoryProfiler(source.gcHandles); value.busyString = string.Format("Loading {0} Object Connections", source.connections.Length); if (args.excludeNativeFromConnections) { value.connections = ConnectionsFromMemoryProfilerWithoutNativeHACK(value, source); } else { value.connections = PackedConnection.FromMemoryProfiler(source.connections); } value.busyString = string.Format("Loading {0} Managed Heap Sections", source.managedHeapSections.Length); value.managedHeapSections = PackedMemorySection.FromMemoryProfiler(source.managedHeapSections); value.busyString = string.Format("Loading {0} Managed Types", source.typeDescriptions.Length); value.managedTypes = PackedManagedType.FromMemoryProfiler(source.typeDescriptions); value.busyString = "Loading VM Information"; value.virtualMachineInformation = PackedVirtualMachineInformation.FromMemoryProfiler(source.virtualMachineInformation); } catch (System.Exception e) { Debug.LogException(e); value = null; throw; } return(value); }
public static PackedMemorySection[] FromMemoryProfiler(UnityEditor.MemoryProfiler.MemorySection[] source) { var value = new PackedMemorySection[source.Length]; for (int n = 0, nend = source.Length; n < nend; ++n) { value[n] = new PackedMemorySection { bytes = source[n].bytes, startAddress = source[n].startAddress, arrayIndex = -1 }; } return(value); }
/// <summary> /// Saves the specfified memory snapshot as a file, using the specified 'filePath'. /// </summary> public void SaveToFile(string filePath) { using (var fileStream = new System.IO.FileStream(filePath, System.IO.FileMode.OpenOrCreate)) { using (var writer = new System.IO.BinaryWriter(fileStream)) { PackedMemorySnapshotHeader.Write(writer, header); PackedNativeType.Write(writer, nativeTypes); PackedNativeUnityEngineObject.Write(writer, nativeObjects); PackedGCHandle.Write(writer, gcHandles); PackedConnection.Write(writer, connections); PackedMemorySection.Write(writer, managedHeapSections); PackedManagedType.Write(writer, managedTypes); PackedVirtualMachineInformation.Write(writer, virtualMachineInformation); } } }
public void GetConnectionsCount(PackedMemorySection memorySection, out int referencesCount) { referencesCount = 0; if (memorySection.bytes == null || memorySection.bytes.Length == 0) { return; } var startAddress = memorySection.startAddress; var endAddress = startAddress + (uint)memorySection.bytes.Length; for (int n = 0, nend = managedObjects.Length; n < nend; ++n) { var mo = managedObjects[n]; if (mo.address >= startAddress && mo.address < endAddress) { referencesCount++; } } }
public void GetConnections(PackedMemorySection memorySection, List <PackedConnection> references, List <PackedConnection> referencedBy) { if (memorySection.bytes == null || memorySection.bytes.Length == 0) { return; } var startAddress = memorySection.startAddress; var endAddress = startAddress + (uint)memorySection.bytes.Length; for (int n = 0, nend = managedObjects.Length; n < nend; ++n) { var mo = managedObjects[n]; if (mo.address >= startAddress && mo.address < endAddress) { references.Add(new PackedConnection() { toKind = PackedConnection.Kind.Managed, to = n }); } } }
public static PackedMemorySection[] FromMemoryProfiler(UnityEditor.Profiling.Memory.Experimental.PackedMemorySnapshot snapshot) { var source = snapshot.managedHeapSections; var value = new PackedMemorySection[source.GetNumEntries()]; var sourceBytes = new byte[source.bytes.GetNumEntries()][]; source.bytes.GetEntries(0, source.bytes.GetNumEntries(), ref sourceBytes); var sourceStartAddresses = new ulong[source.startAddress.GetNumEntries()]; source.startAddress.GetEntries(0, source.startAddress.GetNumEntries(), ref sourceStartAddresses); for (int n = 0, nend = value.Length; n < nend; ++n) { value[n] = new PackedMemorySection { bytes = sourceBytes[n], startAddress = sourceStartAddresses[n], arrayIndex = -1 }; } return(value); }
public static Color32[] GetManagedMemorySectionUsageAsTextureData(PackedMemorySnapshot snapshot, PackedMemorySection memorySection) { List <PackedConnection> references = new List <PackedConnection>(); snapshot.GetConnections(memorySection, references, null); var pixels = new Color32[k_TextureWidth * k_TextureHeight]; var total = new Rect(0, 0, k_TextureWidth, k_TextureHeight); var addressSpace = memorySection.size; for (int n = 0, nend = pixels.Length; n < nend; ++n) { pixels[n] = k_ManagedMemoryColor; } for (int n = 0; n < references.Count; ++n) { var reference = references[n]; ulong address = 0; ulong size = 0; switch (reference.toKind) { case PackedConnection.Kind.Managed: size = (ulong)snapshot.managedObjects[reference.to].size; address = snapshot.managedObjects[reference.to].address; break; default: Debug.LogErrorFormat("{0} not supported yet", reference.toKind); continue; } var offset = address - memorySection.startAddress; var left = (int)((total.width / addressSpace) * offset); var width = (int)Mathf.Max(1, (total.width / addressSpace) * size); for (int y = 0; y < k_TextureHeight; ++y) { for (int x = left; x < left + width; ++x) { var index = k_TextureWidth * y + x; pixels[index] = k_ManagedObjectMemoryColor; } } } return(pixels); }