示例#1
0
    private void LoadAll()
    {
        m_IsReady = false;
        try {
            foreach (var filePath in m_List)
            {
                if (File.Exists(filePath))
                {
                    try {
                        Debug.LogFormat("Loading \"{0}\"", filePath);
                        var rawSnapshot = Unity.MemoryProfilerForExtension.Editor.Format.QueriedMemorySnapshot.Load(filePath);
                        //var rawSnapshot = UnityEditor.Profiling.Memory.Experimental.PackedMemorySnapshot.Load(filePath);
                        if (null == rawSnapshot)
                        {
                            Debug.LogErrorFormat("MemoryProfiler: Unrecognized memory snapshot format '{0}'.", filePath);
                        }
                        Debug.LogFormat("Completed loading \"{0}\"", filePath);

                        Debug.LogFormat("Crawling \"{0}\"", filePath);
                        ProgressBarDisplay.ShowBar(string.Format("Opening snapshot: {0}", System.IO.Path.GetFileNameWithoutExtension(filePath)));

                        var cachedSnapshot = new CachedSnapshot(rawSnapshot);
                        using (s_CrawlManagedData.Auto()) {
                            var crawling = Crawler.Crawl(cachedSnapshot);
                            crawling.MoveNext(); //start execution

                            var   status          = crawling.Current as EnumerationStatus;
                            float progressPerStep = 1.0f / status.StepCount;
                            while (crawling.MoveNext())
                            {
                                ProgressBarDisplay.UpdateProgress(status.CurrentStep * progressPerStep, status.StepStatus);
                            }
                        }
                        ProgressBarDisplay.ClearBar();
                        var rawSchema = new RawSchema();
                        rawSchema.SetupSchema(cachedSnapshot, new ObjectDataFormatter());
                        var managedObjcts = rawSchema.GetTableByName("AllManagedObjects") as ObjectListTable;
                        var nativeObjects = rawSchema.GetTableByName("AllNativeObjects") as ObjectListTable;
                        Debug.LogFormat("Completed crawling \"{0}\"", filePath);

                        Debug.LogFormat("Saving \"{0}\"", filePath);
                        var path     = Path.GetDirectoryName(filePath);
                        var snapName = Path.GetFileNameWithoutExtension(filePath);

                        string exportPath1 = Path.Combine(path, snapName + "_MANAGEDHEAP_SnapshotExport_" + CsLibrary.DateTime.Now.ToString("dd_MM_yyyy_hh_mm_ss") + ".csv");
                        if (!String.IsNullOrEmpty(exportPath1))
                        {
                            System.IO.StreamWriter sw = new System.IO.StreamWriter(exportPath1);
                            sw.WriteLine("Managed_Objects,Size,Address");
                            int ct = cachedSnapshot.SortedManagedHeapEntries.Count;
                            for (int i = 0; i < ct; i++)
                            {
                                var size = cachedSnapshot.SortedManagedHeapEntries.Size(i);
                                var addr = cachedSnapshot.SortedManagedHeapEntries.Address(i);
                                sw.WriteLine("Managed," + size + "," + addr);
                                ResourceProcessor.Instance.DisplayProgressBar("write managed heap ...", i, ct);
                            }
                            sw.Flush();
                            sw.Close();
                        }

                        string exportPath2 = Path.Combine(path, snapName + "_MANAGED_SnapshotExport_" + CsLibrary.DateTime.Now.ToString("dd_MM_yyyy_hh_mm_ss") + ".csv");
                        if (!String.IsNullOrEmpty(exportPath2))
                        {
                            m_ManagedGroups.Clear();
                            System.IO.StreamWriter sw = new System.IO.StreamWriter(exportPath2);
                            sw.WriteLine("Index,Type,RefCount,Size,Address");

                            int ct = cachedSnapshot.SortedManagedObjects.Count;
                            for (int i = 0; i < ct; i++)
                            {
                                var size = cachedSnapshot.SortedManagedObjects.Size(i);
                                var addr = cachedSnapshot.SortedManagedObjects.Address(i);
                                ManagedObjectInfo objInfo;
                                int    index    = -1;
                                int    refCount = 0;
                                string typeName = string.Empty;
                                if (cachedSnapshot.CrawledData.MangedObjectIndexByAddress.TryGetValue(addr, out index))
                                {
                                    objInfo  = cachedSnapshot.CrawledData.ManagedObjects[index];
                                    index    = objInfo.ManagedObjectIndex;
                                    refCount = objInfo.RefCount;
                                    if (objInfo.ITypeDescription >= 0 && objInfo.ITypeDescription < cachedSnapshot.typeDescriptions.Count)
                                    {
                                        typeName = cachedSnapshot.typeDescriptions.typeDescriptionName[objInfo.ITypeDescription];
                                    }
                                }
                                sw.WriteLine("" + index + ",\"" + typeName + "\"," + refCount + "," + size + "," + addr);

                                ManagedGroupInfo info;
                                if (m_ManagedGroups.TryGetValue(typeName, out info))
                                {
                                    ++info.Count;
                                    info.Size += size;
                                }
                                else
                                {
                                    string g  = string.Empty;
                                    int    si = typeName.IndexOf('.');
                                    if (si > 0)
                                    {
                                        g = typeName.Substring(0, si);
                                        if (!s_ManagedGroupNames.Contains(g))
                                        {
                                            g = string.Empty;
                                        }
                                    }
                                    info = new ManagedGroupInfo {
                                        Group = g, Type = typeName, Count = 1, Size = size
                                    };
                                    m_ManagedGroups.Add(typeName, info);
                                }
                                if (i % 1000 == 0)
                                {
                                    ResourceProcessor.Instance.DisplayProgressBar("write managed objects ...", i, ct);
                                }
                            }
                            sw.Flush();
                            sw.Close();

                            string dir       = Path.GetDirectoryName(exportPath2);
                            string fn        = Path.GetFileNameWithoutExtension(exportPath2);
                            string gpath     = Path.Combine(dir, fn + "_groups.csv");
                            var    lastGroup = "A000000";
                            using (var outsw = new StreamWriter(gpath)) {
                                outsw.WriteLine("group,type,count,size");
                                int curCt   = 0;
                                int totalCt = m_ManagedGroups.Count;
                                foreach (var pair in m_ManagedGroups)
                                {
                                    var info = pair.Value;
                                    var g    = info.Group;
                                    if (!string.IsNullOrEmpty(lastGroup) && string.IsNullOrEmpty(info.Group))
                                    {
                                        g = lastGroup + "__";
                                    }
                                    if (!string.IsNullOrEmpty(info.Group))
                                    {
                                        lastGroup = info.Group;
                                    }
                                    outsw.WriteLine("\"{0}\",\"{1}\",{2},{3}", g, info.Type, info.Count, info.Size);
                                    if (curCt % 100 == 0)
                                    {
                                        ResourceProcessor.Instance.DisplayProgressBar("write managed object group ...", curCt, totalCt);
                                    }
                                    ++curCt;
                                }
                            }
                            string gpath2 = Path.Combine(dir, fn + "_groups_forcmp.csv");
                            using (var outsw = new StreamWriter(gpath2)) {
                                outsw.WriteLine("type,count,size");
                                int curCt   = 0;
                                int totalCt = m_ManagedGroups.Count;
                                foreach (var pair in m_ManagedGroups)
                                {
                                    var info = pair.Value;
                                    outsw.WriteLine("\"{0}\",{1},{2}", info.Type, info.Count, info.Size);
                                    if (curCt % 100 == 0)
                                    {
                                        ResourceProcessor.Instance.DisplayProgressBar("write managed object group for cmp ...", curCt, totalCt);
                                    }
                                    ++curCt;
                                }
                            }
                        }

                        string exportPath = Path.Combine(path, snapName + "_NATIVE_SnapshotExport_" + CsLibrary.DateTime.Now.ToString("dd_MM_yyyy_hh_mm_ss") + ".csv");
                        if (!String.IsNullOrEmpty(exportPath))
                        {
                            m_NativeGroups.Clear();
                            System.IO.StreamWriter sw = new System.IO.StreamWriter(exportPath);
                            sw.WriteLine("Index,Name,Type,RefCount,InstanceID,Size,Address");
                            int ct = cachedSnapshot.SortedNativeObjects.Count;
                            for (int i = 0; i < ct; i++)
                            {
                                var size       = cachedSnapshot.SortedNativeObjects.Size(i);
                                var addr       = cachedSnapshot.SortedNativeObjects.Address(i);
                                var name       = cachedSnapshot.SortedNativeObjects.Name(i);
                                var refCount   = cachedSnapshot.SortedNativeObjects.Refcount(i);
                                var instanceId = cachedSnapshot.SortedNativeObjects.InstanceId(i);
                                int index;
                                if (!cachedSnapshot.nativeObjects.instanceId2Index.TryGetValue(instanceId, out index))
                                {
                                    index = -1;
                                }
                                var    nativeTypeIndex = cachedSnapshot.SortedNativeObjects.NativeTypeArrayIndex(i);
                                string typeName        = string.Empty;
                                if (nativeTypeIndex >= 0 && nativeTypeIndex < cachedSnapshot.nativeTypes.Count)
                                {
                                    typeName = cachedSnapshot.nativeTypes.typeName[nativeTypeIndex];
                                }

                                sw.WriteLine("" + index + ",\"" + name + "\",\"" + typeName + "\"," + refCount + "," + instanceId + "," + size + "," + addr);

                                NativeGroupInfo info;
                                if (m_NativeGroups.TryGetValue(typeName, out info))
                                {
                                    ++info.Count;
                                    info.Size += size;
                                }
                                else
                                {
                                    info = new NativeGroupInfo {
                                        Type = typeName, Count = 1, Size = size
                                    };
                                    m_NativeGroups.Add(typeName, info);
                                }
                                if (i % 100 == 0)
                                {
                                    ResourceProcessor.Instance.DisplayProgressBar("write native objects ...", i, ct);
                                }
                            }
                            sw.Flush();
                            sw.Close();

                            string dir   = Path.GetDirectoryName(exportPath);
                            string fn    = Path.GetFileNameWithoutExtension(exportPath);
                            string gpath = Path.Combine(dir, fn + "_groups.csv");
                            using (var outsw = new StreamWriter(gpath)) {
                                outsw.WriteLine("type,count,size");
                                int curCt   = 0;
                                int totalCt = m_NativeGroups.Count;
                                foreach (var pair in m_NativeGroups)
                                {
                                    var info = pair.Value;
                                    outsw.WriteLine("\"{0}\",{1},{2}", info.Type, info.Count, info.Size);
                                    ResourceProcessor.Instance.DisplayProgressBar("write native object group ...", curCt, totalCt);
                                    ++curCt;
                                }
                            }
                        }

                        Debug.LogFormat("Completed saving \"{0}\"", filePath);
                    }
                    catch (Exception ex) {
                        UnityEngine.Debug.LogErrorFormat("file {0} exception {1}\n{2}", filePath, ex.Message, ex.StackTrace);
                    }
                }
            }
            EditorUtility.ClearProgressBar();
        }
        finally {
            m_IsReady = true;
        }
    }
        public void DrawPlainData()
        {
            if (_unpackedCrawl != null)
            {
                GUILayout.Label(" ");
                if (GUILayout.Button("Save managed heap data to an external .csv file", blueColorStyle))
                {
                    string exportPath = EditorUtility.SaveFilePanel("Save Snapshot Info", Application.dataPath, "MANAGEDHEAP_SnapshotExport_" + DateTime.Now.ToString("dd_MM_yyyy_hh_mm_ss") + ".csv", "csv");
                    if (!String.IsNullOrEmpty(exportPath))
                    {
                        System.IO.StreamWriter sw = new System.IO.StreamWriter(exportPath);
                        sw.WriteLine(" Managed Objects , Size , Address ");
                        for (int i = 0; i < _unpackedCrawl.managedHeap.Length; i++)
                        {
                            MemorySection memorySection = _unpackedCrawl.managedHeap[i];
                            sw.WriteLine("Managed," + memorySection.bytes.Length + "," + memorySection.startAddress);
                        }
                        sw.Flush();
                        sw.Close();
                    }
                }
                if (GUILayout.Button("Save full list of MANAGED objects data to an external .csv file", blueColorStyle))
                {
                    string exportPath = EditorUtility.SaveFilePanel("Save Snapshot Info", Application.dataPath, "MANAGED_SnapshotExport_" + DateTime.Now.ToString("dd_MM_yyyy_hh_mm_ss") + ".csv", "csv");
                    if (!String.IsNullOrEmpty(exportPath))
                    {
                        m_ManagedGroups.Clear();
                        System.IO.StreamWriter sw = new System.IO.StreamWriter(exportPath);
                        sw.WriteLine(" Managed Objects , Size , Caption , Type , Number of References (Total), Referenced By (Total), Address ");
                        for (int i = 0; i < _unpackedCrawl.managedObjects.Length; i++)
                        {
                            ManagedObject managedObject = _unpackedCrawl.managedObjects[i];
                            sw.WriteLine("Managed," + managedObject.size + "," + CleanStrings(managedObject.caption) + "," + CleanStrings(managedObject.typeDescription.name) + "," + managedObject.references.Length + "," + managedObject.referencedBy.Length + "," + managedObject.address);

                            string           type = managedObject.typeDescription.name;
                            long             size = managedObject.size;
                            ManagedGroupInfo info;
                            if (m_ManagedGroups.TryGetValue(type, out info))
                            {
                                ++info.Count;
                                info.Size += size;
                            }
                            else
                            {
                                string g  = string.Empty;
                                int    si = type.IndexOf('.');
                                if (si > 0)
                                {
                                    g = type.Substring(0, si);
                                    if (!s_ManagedGroupNames.Contains(g))
                                    {
                                        g = string.Empty;
                                    }
                                }
                                info = new ManagedGroupInfo {
                                    Group = g, Type = type, Count = 1, Size = size
                                };
                                m_ManagedGroups.Add(type, info);
                            }
                        }
                        sw.Flush();
                        sw.Close();

                        string dir       = Path.GetDirectoryName(exportPath);
                        string fn        = Path.GetFileNameWithoutExtension(exportPath);
                        string gpath     = Path.Combine(dir, fn + "_groups.csv");
                        var    lastGroup = "A000000";
                        using (var outsw = new StreamWriter(gpath)) {
                            outsw.WriteLine("group,type,count,size");
                            foreach (var pair in m_ManagedGroups)
                            {
                                var info = pair.Value;
                                var g    = info.Group;
                                if (!string.IsNullOrEmpty(lastGroup) && string.IsNullOrEmpty(info.Group))
                                {
                                    g = lastGroup + "__";
                                }
                                if (!string.IsNullOrEmpty(info.Group))
                                {
                                    lastGroup = info.Group;
                                }
                                outsw.WriteLine("{0},\"{1}\",{2},{3}", g, info.Type, info.Count, info.Size);
                            }
                        }
                        string gpath2 = Path.Combine(dir, fn + "_groups_forcmp.csv");
                        using (var outsw = new StreamWriter(gpath2)) {
                            outsw.WriteLine("type,count,size");
                            foreach (var pair in m_ManagedGroups)
                            {
                                var info = pair.Value;
                                outsw.WriteLine("\"{0}\",{1},{2}", info.Type, info.Count, info.Size);
                            }
                        }
                    }
                }
                if (GUILayout.Button("Save full list of NATIVE objects data to an external .csv file", greenColorStyle))
                {
                    string exportPath = EditorUtility.SaveFilePanel("Save Snapshot Info", Application.dataPath, "NATIVE_SnapshotExport_" + DateTime.Now.ToString("dd_MM_yyyy_hh_mm_ss") + ".csv", "csv");
                    if (!String.IsNullOrEmpty(exportPath))
                    {
                        m_NativeGroups.Clear();
                        System.IO.StreamWriter sw = new System.IO.StreamWriter(exportPath);
                        sw.WriteLine(" Native Objects , Size , Caption , Class Name , Name , Number of References (Total), Referenced By (Total), InstanceID ");
                        for (int i = 0; i < _unpackedCrawl.nativeObjects.Length; i++)
                        {
                            NativeUnityEngineObject nativeObject = _unpackedCrawl.nativeObjects[i];
                            sw.WriteLine("Native," + nativeObject.size + "," + CleanStrings(nativeObject.caption) + "," + CleanStrings(nativeObject.className) + "," + CleanStrings(nativeObject.name) + "," + nativeObject.references.Length + "," + nativeObject.referencedBy.Length + "," + nativeObject.instanceID);

                            string          type = nativeObject.className;
                            long            size = nativeObject.size;
                            NativeGroupInfo info;
                            if (m_NativeGroups.TryGetValue(type, out info))
                            {
                                ++info.Count;
                                info.Size += size;
                            }
                            else
                            {
                                info = new NativeGroupInfo {
                                    Type = type, Count = 1, Size = size
                                };
                                m_NativeGroups.Add(type, info);
                            }
                        }
                        sw.Flush();
                        sw.Close();

                        string dir   = Path.GetDirectoryName(exportPath);
                        string fn    = Path.GetFileNameWithoutExtension(exportPath);
                        string gpath = Path.Combine(dir, fn + "_groups.csv");
                        using (var outsw = new StreamWriter(gpath)) {
                            outsw.WriteLine("type,count,size");
                            foreach (var pair in m_NativeGroups)
                            {
                                var info = pair.Value;
                                outsw.WriteLine("\"{0}\",{1},{2}", info.Type, info.Count, info.Size);
                            }
                        }
                    }
                }
                GUILayout.Label(" ");
                GUILayout.Label("Managed Objects (Total: " + _unpackedCrawl.managedObjects.Length + ") - First 10 Elements: ");
                GUILayout.Label(" ");
                for (int i = 0; i < _unpackedCrawl.managedObjects.Length && i < 10; i++)
                {
                    ManagedObject managedObject = _unpackedCrawl.managedObjects[i];
                    GUILayout.Label("Address: " + managedObject.address + ", Caption: " + managedObject.caption + ", Size: " + managedObject.size);
                }
                GUILayout.Label(" ");
                GUILayout.Label("Native Objects (Total: " + _unpackedCrawl.nativeObjects.Length + ") - First 10 Elements:");
                GUILayout.Label(" ");
                for (int i = 0; i < _unpackedCrawl.nativeObjects.Length && i < 10; i++)
                {
                    NativeUnityEngineObject nativeObject = _unpackedCrawl.nativeObjects[i];
                    GUILayout.Label("InstanceID: " + nativeObject.instanceID + ", Name: " + nativeObject.name + ", Size: " + nativeObject.size);
                }
            }
        }