Exemple #1
0
        private static void SnapshotFinished(string path, bool result)
        {
            if (result)
            {
                UnityEditor.Profiling.Memory.Experimental.PackedMemorySnapshot snapshot = UnityEditor.Profiling.Memory.Experimental.PackedMemorySnapshot.Load(path);

                OnSnapshotReceived(new PackedMemorySnapshot(snapshot));
            }
            else
            {
                OnSnapshotReceived(null);
            }
        }
Exemple #2
0
        public static PackedVirtualMachineInformation FromMemoryProfiler(UnityEditor.Profiling.Memory.Experimental.PackedMemorySnapshot snapshot)
        {
            var source = snapshot.virtualMachineInformation;

            var value = new PackedVirtualMachineInformation
            {
                pointerSize               = source.pointerSize,
                objectHeaderSize          = source.objectHeaderSize,
                arrayHeaderSize           = source.arrayHeaderSize,
                arrayBoundsOffsetInHeader = source.arrayBoundsOffsetInHeader,
                arraySizeOffsetInHeader   = source.arraySizeOffsetInHeader,
                allocationGranularity     = source.allocationGranularity,
                heapFormatVersion         = 2019,
            };

            return(value);
        }
        public static                       PackedGCHandle[] FromMemoryProfiler(UnityEditor.Profiling.Memory.Experimental.PackedMemorySnapshot snapshot)
        {
            var source = snapshot.gcHandles;
            var value  = new PackedGCHandle[source.GetNumEntries()];

            var sourceTargets = new ulong[source.target.GetNumEntries()];

            source.target.GetEntries(0, source.target.GetNumEntries(), ref sourceTargets);

            for (int n = 0, nend = value.Length; n < nend; ++n)
            {
                value[n] = new PackedGCHandle
                {
                    target = sourceTargets[n],
                    gcHandlesArrayIndex      = n,
                    managedObjectsArrayIndex = -1,
                };
            }
            return(value);
        }
        static void VerifyMemoryProfilerSnapshot(UnityEditor.Profiling.Memory.Experimental.PackedMemorySnapshot snapshot)
        {
            if (snapshot == null)
            {
                throw new Exception("No snapshot was found.");
            }

            if (snapshot.typeDescriptions == null || snapshot.typeDescriptions.GetNumEntries() == 0)
            {
                throw new Exception("The snapshot does not contain any 'typeDescriptions'. This is a known issue when using .NET 4.x Scripting Runtime.\n(Case 1079363) PackedMemorySnapshot: .NET 4.x Scripting Runtime breaks memory snapshot");
            }

            if (snapshot.managedHeapSections == null || snapshot.managedHeapSections.GetNumEntries() == 0)
            {
                throw new Exception("The snapshot does not contain any 'managedHeapSections'. This is a known issue when using .NET 4.x Scripting Runtime.\n(Case 1079363) PackedMemorySnapshot: .NET 4.x Scripting Runtime breaks memory snapshot");
            }

            if (snapshot.gcHandles == null || snapshot.gcHandles.GetNumEntries() == 0)
            {
                throw new Exception("The snapshot does not contain any 'gcHandles'. This is a known issue when using .NET 4.x Scripting Runtime.\n(Case 1079363) PackedMemorySnapshot: .NET 4.x Scripting Runtime breaks memory snapshot");
            }

            if (snapshot.nativeTypes == null || snapshot.nativeTypes.GetNumEntries() == 0)
            {
                throw new Exception("The snapshot does not contain any 'nativeTypes'.");
            }

            if (snapshot.nativeObjects == null || snapshot.nativeObjects.GetNumEntries() == 0)
            {
                throw new Exception("The snapshot does not contain any 'nativeObjects'.");
            }

            if (snapshot.connections == null || snapshot.connections.GetNumEntries() == 0)
            {
                throw new Exception("The snapshot does not contain any 'connections'.");
            }
        }
Exemple #5
0
        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);
        }
Exemple #6
0
        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);
        }
Exemple #7
0
        public static       PackedConnection[] FromMemoryProfiler(UnityEditor.Profiling.Memory.Experimental.PackedMemorySnapshot snapshot)
        {
            var result         = new List <PackedConnection>(1024 * 1024);
            var invalidIndices = 0;

            // Get all NativeObject instanceIds
            var nativeObjects            = snapshot.nativeObjects;
            var nativeObjectsCount       = nativeObjects.GetNumEntries();
            var nativeObjectsInstanceIds = new int[nativeObjects.instanceId.GetNumEntries()];

            nativeObjects.instanceId.GetEntries(0, nativeObjects.instanceId.GetNumEntries(), ref nativeObjectsInstanceIds);

            // Create lookup table from instanceId to NativeObject arrayindex
            var instanceIdToNativeObjectIndex = new Dictionary <int, int>(nativeObjectsInstanceIds.Length);

            for (var n = 0; n < nativeObjectsInstanceIds.Length; ++n)
            {
                instanceIdToNativeObjectIndex.Add(nativeObjectsInstanceIds[n], n);
            }

            // Connect NativeObject with its GCHandle
            var nativeObjectsGCHandleIndices = new int[nativeObjects.gcHandleIndex.GetNumEntries()];

            nativeObjects.gcHandleIndex.GetEntries(0, nativeObjects.gcHandleIndex.GetNumEntries(), ref nativeObjectsGCHandleIndices);

            var gcHandles      = snapshot.gcHandles;
            var gcHandlesCount = gcHandles.GetNumEntries();

            for (var n = 0; n < nativeObjectsGCHandleIndices.Length; ++n)
            {
                // Unity 2019.4.7f1 and earlier (maybe also newer) versions seem to have this issue:
                // (Case 1269293) PackedMemorySnapshot: nativeObjects.gcHandleIndex contains -1 always
                if (nativeObjectsGCHandleIndices[n] < 0 || nativeObjectsGCHandleIndices[n] >= gcHandlesCount)
                {
                    if (nativeObjectsGCHandleIndices[n] != -1)
                    {
                        invalidIndices++; // I guess -1 means no connection to a gcHandle
                    }
                    continue;
                }

                var packed = new PackedConnection();
                packed.from     = n;                               // nativeObject index
                packed.fromKind = Kind.Native;
                packed.to       = nativeObjectsGCHandleIndices[n]; // gcHandle index
                packed.toKind   = Kind.GCHandle;

                result.Add(packed);
            }

            if (invalidIndices > 0)
            {
                Debug.LogErrorFormat("Reconstructing native to gchandle connections. Found {0} invalid indices into nativeObjectsGCHandleIndices[{1}] array.", invalidIndices, gcHandlesCount);
            }


            // Connect NativeObject with NativeObject

            var source = snapshot.connections;

            int[] sourceFrom = new int[source.from.GetNumEntries()];
            source.from.GetEntries(0, source.from.GetNumEntries(), ref sourceFrom);

            int[] sourceTo = new int[source.to.GetNumEntries()];
            source.to.GetEntries(0, source.to.GetNumEntries(), ref sourceTo);

            var invalidInstanceIDs = 0;

            invalidIndices = 0;

            for (int n = 0, nend = sourceFrom.Length; n < nend; ++n)
            {
                var packed = new PackedConnection();

                if (!instanceIdToNativeObjectIndex.TryGetValue(sourceFrom[n], out packed.from))
                {
                    invalidInstanceIDs++;
                    continue; // NativeObject InstanceID not found
                }

                if (!instanceIdToNativeObjectIndex.TryGetValue(sourceTo[n], out packed.to))
                {
                    invalidInstanceIDs++;
                    continue; // NativeObject InstanceID not found
                }

                if (packed.from < 0 || packed.from >= nativeObjectsCount)
                {
                    invalidIndices++;
                    continue; // invalid index into array
                }

                if (packed.to < 0 || packed.to >= nativeObjectsCount)
                {
                    invalidIndices++;
                    continue; // invalid index into array
                }

                packed.fromKind = Kind.Native;
                packed.toKind   = Kind.Native;

                result.Add(packed);
            }

            if (invalidIndices > 0)
            {
                Debug.LogErrorFormat("Reconstructing native to native object connections. Found {0} invalid indices into nativeObjectsCount[{1}] array.", invalidIndices, nativeObjectsCount);
            }

            if (invalidInstanceIDs > 0)
            {
                Debug.LogErrorFormat("Reconstructing native to native object connections. Found {0} invalid instanceIDs.", invalidInstanceIDs, nativeObjectsCount);
            }


            return(result.ToArray());
        }