public static PackedNativeType[] FromMemoryProfiler(UnityEditor.Profiling.Memory.Experimental.PackedMemorySnapshot snapshot) { var source = snapshot.nativeTypes; var value = new PackedNativeType[source.GetNumEntries()]; var sourceTypeName = new string[source.typeName.GetNumEntries()]; source.typeName.GetEntries(0, source.typeName.GetNumEntries(), ref sourceTypeName); var sourceNativeBaseTypeArrayIndex = new int[source.nativeBaseTypeArrayIndex.GetNumEntries()]; source.nativeBaseTypeArrayIndex.GetEntries(0, source.nativeBaseTypeArrayIndex.GetNumEntries(), ref sourceNativeBaseTypeArrayIndex); for (int n = 0, nend = value.Length; n < nend; ++n) { value[n] = new PackedNativeType { name = sourceTypeName[n], nativeBaseTypeArrayIndex = sourceNativeBaseTypeArrayIndex[n], nativeTypeArrayIndex = n, managedTypeArrayIndex = -1, }; } ; return(value); }
public bool IsSubclassOf(PackedNativeType type, int baseTypeIndex) { if (type.nativeTypeArrayIndex == baseTypeIndex) { return(true); } if (baseTypeIndex < 0 || type.nativeTypeArrayIndex < 0) { return(false); } var guard = 0; while (type.nativeBaseTypeArrayIndex != -1) { // safety code in case of an infite loop if (++guard > 64) { break; } if (type.nativeBaseTypeArrayIndex == baseTypeIndex) { return(true); } // get type of the base class type = nativeTypes[type.nativeBaseTypeArrayIndex]; } return(false); }
/// <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); }
/// <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); }
/// <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 static PackedNativeType[] FromMemoryProfiler(UnityEditor.MemoryProfiler.PackedNativeType[] source) { var value = new PackedNativeType[source.Length]; for (int n = 0, nend = source.Length; n < nend; ++n) { value[n] = new PackedNativeType { name = source[n].name, #if UNITY_5_6_OR_NEWER nativeBaseTypeArrayIndex = source[n].nativeBaseTypeArrayIndex, #else nativeBaseTypeArrayIndex = source[n].baseClassId, #endif nativeTypeArrayIndex = n, managedTypeArrayIndex = -1, }; } ; return(value); }
/// <summary> /// Reads a PackedNativeType array from the specified reader and stores the result in the specified value. /// </summary> public static void Read(System.IO.BinaryReader reader, out PackedNativeType[] value, out string stateString) { value = new PackedNativeType[0]; stateString = ""; var version = reader.ReadInt32(); if (version >= 1) { var length = reader.ReadInt32(); stateString = string.Format("Loading {0} Native Types", length); value = new PackedNativeType[length]; for (int n = 0, nend = value.Length; n < nend; ++n) { value[n].name = reader.ReadString(); value[n].nativeBaseTypeArrayIndex = reader.ReadInt32(); value[n].nativeTypeArrayIndex = n; value[n].managedTypeArrayIndex = -1; } } }