public void Setup(CachedSnapshot snapshot) { m_Snapshot = snapshot; ProgressBarDisplay.ShowBar("Setup memory map"); SetupSortedData(); SetupRegions(); CreateGroups(); SetupGroups(); ProgressBarDisplay.ClearBar(); }
public void Setup(CachedSnapshot snapshotA, CachedSnapshot snapshotB) { m_Snapshots[0] = snapshotA; m_Snapshots[1] = snapshotB; ProgressBarDisplay.ShowBar("Setup memory map diff"); SetupSortedData(); SetupRegions(); CreateGroups(); SetupGroups(); ProgressBarDisplay.ClearBar(); }
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; } }