public static Dictionary <string, MemType> PopulateTypes(CrawledMemorySnapshot snapshot) { var types = new Dictionary <string, MemType>(); foreach (ThingInMemory thingInMemory in snapshot.allObjects) { string typeName = MemUtil.GetGroupName(thingInMemory); if (typeName.Length == 0) { continue; } MemType theType; if (!types.TryGetValue(typeName, out theType)) { theType = new MemType(); theType.TypeName = MemUtil.GetCategoryLiteral(thingInMemory) + typeName; theType.Category = MemUtil.GetCategory(thingInMemory); theType.Objects = new List <object>(); types.Add(typeName, theType); } MemObject item = new MemObject(thingInMemory, snapshot); theType.Size += item.Size; theType.Count++; theType.Objects.Add(item); } return(types); }
public void RefreshData(CrawledMemorySnapshot unpackedCrawl) { _unpacked = unpackedCrawl; _types.Clear(); foreach (ThingInMemory thingInMemory in _unpacked.allObjects) { string typeName = MemUtil.GetGroupName(thingInMemory); if (typeName.Length == 0) { continue; } MemType theType; if (!_types.ContainsKey(typeName)) { theType = new MemType(); theType.TypeName = MemUtil.GetCategoryLiteral(thingInMemory) + typeName; theType.Category = MemUtil.GetCategory(thingInMemory); theType.Objects = new List <object>(); _types.Add(typeName, theType); } else { theType = _types[typeName]; } MemObject item = new MemObject(thingInMemory, _unpacked); theType.AddObject(item); } RefreshTables(); }
public void AddObject(MemObject mo) { Objects.Add(mo); Count = Objects.Count; Size += mo.Size; SizeLiterally = EditorUtility.FormatBytes(Size); }
void setUnchangedThings(ThingInMemory thingInMemory) { string typeName = MemUtil.GetGroupName(thingInMemory); if (typeName.Length == 0) { return; } int category = MemUtil.GetCategory(thingInMemory); MemObject item = new MemObject(thingInMemory, _unpacked); if (!_staticDetailInfo.isDetailStaticFileds(typeName, thingInMemory.caption, item.Size)) { MemType theType; Dictionary <string, MemType> unchangedDict; _diffDicts.TryGetValue(sDiffDictKey.unchangedDict, out unchangedDict); if (!unchangedDict.ContainsKey(typeName)) { theType = new MemType(); theType.TypeName = MemUtil.GetCategoryLiteral(thingInMemory) + typeName; theType.Category = category; theType.Objects = new List <object>(); unchangedDict.Add(typeName, theType); } else { theType = unchangedDict[typeName]; } theType.AddObject(item); } }
public void RefreshDiffData(CrawledMemorySnapshot unpackedCrawl, CrawledMemorySnapshot preUnpackedCrawl) { MemUtil.LoadSnapshotProgress(0.01f, "refresh Data"); _unpacked = unpackedCrawl; _preUnpacked = preUnpackedCrawl; resetDiffDicts(); _categories.Clear(); _staticDetailInfo.clear(); foreach (ThingInMemory thingInMemory in _unpacked.allObjects) { string typeName = MemUtil.GetGroupName(thingInMemory); if (typeName.Length == 0) { continue; } int category = MemUtil.GetCategory(thingInMemory); MemObject item = new MemObject(thingInMemory, _unpacked); MemCategory theCategory; if (!_categories.TryGetValue(category, out theCategory)) { theCategory = new MemCategory(); theCategory.Category = category; _categories.Add(category, theCategory); } theCategory.Size += item.Size; theCategory.Count++; } calculateCategoryInfo(); MemUtil.LoadSnapshotProgress(0.4f, "unpack all objs"); _unpackedInfos = new UnPackedInfos(_unpacked, _preUnpacked); _unpackedInfos.calculateCrawledDict(); List <ThingInMemory> addedList; List <ThingInMemory> unchangedList; List <ThingInMemory> removedList; getDiffDict(_unpackedInfos._unpackedThingsDict, _unpackedInfos._preunpackedThingsDict, out addedList, out removedList, out unchangedList); foreach (var thing in addedList) { _handleDiffObj(thing, sDiffType.AdditiveType, _unpacked); } foreach (var thing in removedList) { _handleDiffObj(thing, sDiffType.NegativeType, _unpacked); } foreach (var thing in unchangedList) { setUnchangedThings(thing); } MemUtil.LoadSnapshotProgress(0.8f, "check diff"); RefreshTables(); MemUtil.LoadSnapshotProgress(1.0f, "done"); }
public void RefreshData(CrawledMemorySnapshot unpackedCrawl) { _unpacked = unpackedCrawl; resetDiffDicts(); _categories.Clear(); _staticDetailInfo.clear(); foreach (ThingInMemory thingInMemory in _unpacked.allObjects) { string typeName = MemUtil.GetGroupName(thingInMemory); if (typeName.Length == 0) { continue; } int category = MemUtil.GetCategory(thingInMemory); MemObject item = new MemObject(thingInMemory, _unpacked); if (!_staticDetailInfo.isDetailStaticFileds(typeName, thingInMemory.caption, item.Size)) { MemType theType; Dictionary <string, MemType> unchangedDict; _diffDicts.TryGetValue(sDiffDictKey.unchangedDict, out unchangedDict); if (!unchangedDict.ContainsKey(typeName)) { theType = new MemType(); theType.TypeName = MemUtil.GetCategoryLiteral(thingInMemory) + typeName; theType.Category = category; theType.Objects = new List <object>(); unchangedDict.Add(typeName, theType); } else { theType = unchangedDict[typeName]; } theType.AddObject(item); } MemCategory theCategory; if (!_categories.TryGetValue(category, out theCategory)) { theCategory = new MemCategory(); theCategory.Category = category; _categories.Add(category, theCategory); } theCategory.Size += item.Size; theCategory.Count++; } calculateCategoryInfo(); RefreshTables(); }
void _handleDiffManangeObj(ManagedObject mao, string diffType, CrawledMemorySnapshot resultPacked) { var theType = _checkNewTypes(mao, diffType); if (theType == null) { return; } MemObject item = new MemObject(mao, resultPacked); string TypeName = MemUtil.GetGroupName(mao); mao.caption = MemUtil.GetCategoryLiteral(mao) + TypeName + diffType; theType.AddObject(item); }
private static List <object> DiffNative(Dictionary <int, MemObject> native1st, Dictionary <int, MemObject> native2nd) { HashSet <int> both = Intersect(native1st, native2nd); List <object> ret = new List <object>(); foreach (var p in native1st) { if (!both.Contains(p.Key)) { MarkStatus(p.Value, eDiffStatus.Removed); ret.Add(p.Value); } } foreach (var p in native2nd) { if (!both.Contains(p.Key)) { MarkStatus(p.Value, eDiffStatus.Added); ret.Add(p.Value); } } foreach (int i in both) { NativeUnityEngineObject obj1 = native1st[i]._thing as NativeUnityEngineObject; NativeUnityEngineObject obj2 = native2nd[i]._thing as NativeUnityEngineObject; MemObject mo = native2nd[i]; if (obj1.size == obj2.size) { MarkStatus(mo, eDiffStatus.Unchanged); } else if (obj1.size > obj2.size) { MarkStatus(mo, eDiffStatus.Decreased); ret.Add(mo); } else { MarkStatus(mo, eDiffStatus.Increased); ret.Add(mo); } } return(ret); }
private static List <object> DiffManaged(Dictionary <UInt64, MemObject> managed1st, Dictionary <UInt64, MemObject> managed2nd) { HashSet <UInt64> both = Intersect(managed1st, managed2nd); List <object> ret = new List <object>(); foreach (var p in managed1st) { if (!both.Contains(p.Key)) { MarkStatus(p.Value, eDiffStatus.Removed); ret.Add(p.Value); } } foreach (var p in managed2nd) { if (!both.Contains(p.Key)) { MarkStatus(p.Value, eDiffStatus.Added); ret.Add(p.Value); } } foreach (UInt64 i in both) { ManagedObject obj1 = managed1st[i]._thing as ManagedObject; ManagedObject obj2 = managed2nd[i]._thing as ManagedObject; MemObject mo = managed2nd[i]; if (obj1.size == obj2.size) { MarkStatus(mo, eDiffStatus.Unchanged); } else if (obj1.size > obj2.size) { MarkStatus(mo, eDiffStatus.Decreased); ret.Add(mo); } else { MarkStatus(mo, eDiffStatus.Increased); ret.Add(mo); } } return(ret); }
void _handleDiffNativeObj(NativeUnityEngineObject nat, string diffType, CrawledMemorySnapshot resultPacked) { var theType = _checkNewTypes(nat, diffType); if (theType == null) { return; } string TypeName = MemUtil.GetGroupName(nat); string diffTypeName = MemUtil.GetCategoryLiteral(nat) + TypeName + diffType; nat.className = diffTypeName; MemObject item = new MemObject(nat, resultPacked); theType.AddObject(item); }
void _handleDiffObj(ThingInMemory thing, string diffType, CrawledMemorySnapshot resultPacked) { var theType = _checkNewTypes(thing, diffType); if (theType == null) { return; } string TypeName = MemUtil.GetGroupName(thing); ThingInMemory newThings = null; if (thing is NativeUnityEngineObject) { var nat = thing as NativeUnityEngineObject; var newNat = new NativeUnityEngineObject(); newNat.caption = thing.caption; newNat.classID = nat.classID; newNat.className = TypeName + diffType; newNat.instanceID = nat.instanceID; newNat.isManager = false; newNat.size = thing.size; newNat.hideFlags = nat.hideFlags; newNat.isPersistent = nat.isPersistent; newNat.name = nat.name; newNat.referencedBy = thing.referencedBy; newNat.references = thing.references; newNat.isDontDestroyOnLoad = nat.isDontDestroyOnLoad; newThings = newNat; } else { var mao = thing as ManagedObject; var newMao = new ManagedObject(); newMao.caption = TypeName + diffType; newMao.address = mao.address; newMao.referencedBy = mao.referencedBy; newMao.references = mao.references; newMao.size = mao.size; newMao.typeDescription = mao.typeDescription; newThings = newMao; } MemObject item = new MemObject(newThings, resultPacked); theType.AddObject(item); }
private string resolvePackedForJson(CrawledMemorySnapshot packed) { if (packed == null) { return(null); } var _unpacked = packed; _types.Clear(); _categories.Clear(); foreach (ThingInMemory thingInMemory in packed.allObjects) { string typeName = MemUtil.GetGroupName(thingInMemory); if (typeName.Length == 0) { continue; } int category = MemUtil.GetCategory(thingInMemory); MemObject item = new MemObject(thingInMemory, _unpacked); MemType theType; if (!_types.ContainsKey(typeName)) { theType = new MemType(); theType.TypeName = MemUtil.GetCategoryLiteral(thingInMemory) + typeName; theType.Category = category; theType.Objects = new List <object>(); _types.Add(typeName, theType); } else { theType = _types[typeName]; } theType.AddObject(item); } //协议格式: //Data: //"obj" = "TypeName,Category,Count,size" //"info" ="RefCount,size,InstanceName,address,typeDescriptionIndex" //TypeDescs: //InstanceNames: Dictionary <int, string> typeDescDict = new Dictionary <int, string>(); Dictionary <int, string> instanceNameDict = new Dictionary <int, string>(); var jsonData = new JsonData(); foreach (var type in _types) { var typeData = new JsonData(); typeData["Obj"] = type.Key + "," + type.Value.Category + "," + type.Value.Count + "," + type.Value.Size; var objectDatas = new JsonData(); foreach (var obj in type.Value.Objects) { var objectData = new JsonData(); var memObj = obj as MemObject; string dataInfo; var instanceNameHash = memObj.InstanceName.GetHashCode(); if (!instanceNameDict.ContainsKey(instanceNameHash)) { instanceNameDict.Add(instanceNameHash, memObj.InstanceName); } dataInfo = memObj.RefCount + "," + memObj.Size + "," + instanceNameDict[instanceNameHash]; if (type.Value.Category == 2) { var manged = memObj._thing as ManagedObject; var typeDescriptionHash = manged.typeDescription.name.GetHashCode(); if (!typeDescDict.ContainsKey(typeDescriptionHash)) { typeDescDict.Add(typeDescriptionHash, manged.typeDescription.name); } dataInfo += "," + Convert.ToString((int)manged.address, 16) + "," + typeDescriptionHash; } objectData["info"] = dataInfo; objectDatas.Add(objectData); } typeData["memObj"] = objectDatas; jsonData.Add(typeData); } var resultJson = new JsonData(); resultJson["Data"] = jsonData; StringBuilder sb = new StringBuilder(); foreach (var key in typeDescDict.Keys) { sb.Append("[[" + key + "]:" + typeDescDict[key] + "],"); } resultJson["TypeDescs"] = sb.ToString(); sb.Remove(0, sb.Length); foreach (var key in instanceNameDict.Keys) { sb.Append("[[" + key + "]:" + instanceNameDict[key] + "],"); } resultJson["InstanceNames"] = sb.ToString(); return(resultJson.ToJson()); }
public void RefreshData(CrawledMemorySnapshot unpackedCrawl, CrawledMemorySnapshot preUnpackedCrawl = null) { _unpacked = unpackedCrawl; _preUnpacked = preUnpackedCrawl; _types.Clear(); _categories.Clear(); _staticDetailInfo.clear(); foreach (ThingInMemory thingInMemory in _unpacked.allObjects) { string typeName = MemUtil.GetGroupName(thingInMemory); if (typeName.Length == 0) { continue; } int category = MemUtil.GetCategory(thingInMemory); MemObject item = new MemObject(thingInMemory, _unpacked); if (!_staticDetailInfo.isDetailStaticFileds(typeName, thingInMemory.caption, item.Size)) { MemType theType; if (!_types.ContainsKey(typeName)) { theType = new MemType(); theType.TypeName = MemUtil.GetCategoryLiteral(thingInMemory) + typeName; theType.Category = category; theType.Objects = new List <object>(); _types.Add(typeName, theType); } else { theType = _types[typeName]; } theType.AddObject(item); } MemCategory theCategory; if (!_categories.TryGetValue(category, out theCategory)) { theCategory = new MemCategory(); theCategory.Category = category; _categories.Add(category, theCategory); } theCategory.Size += item.Size; theCategory.Count++; } int[] sizes = new int[MemConst.MemTypeCategories.Length]; int[] counts = new int[MemConst.MemTypeCategories.Length]; foreach (var item in _categories) { sizes[0] += item.Value.Size; counts[0] += item.Value.Count; if (item.Key == 1) { sizes[1] += item.Value.Size; counts[1] += item.Value.Count; } else if (item.Key == 2) { sizes[2] += item.Value.Size; counts[2] += item.Value.Count; } else { sizes[3] += item.Value.Size; counts[3] += item.Value.Count; } } for (int i = 0; i < _categoryLiterals.Length; i++) { _categoryLiterals[i] = string.Format("{0} ({1}, {2})", MemConst.MemTypeCategories[i], counts[i], EditorUtility.FormatBytes(sizes[i])); } freshUnpackInfos(); checkAddtiveThings(); checkNegativeThings(); RefreshTables(); }
public static void ShowTypeStats(MemType mt) { if (mt == null) { Debug.LogError("invalid type, ignored."); return; } if (mt.TypeName.EndsWith("System.String")) // this would excludes 'System.String[]' { ShowStringStats(mt); } // accumulate by 'referenced by' types Dictionary <string, HashSet <MemObject> > referenceMap = new Dictionary <string, HashSet <MemObject> >(); foreach (var obj in mt.Objects) { MemObject mo = obj as MemObject; if (mo != null && mo._thing != null) { foreach (var referencer in mo._thing.referencedBy) { string referencerTypeName = MemUtil.GetGroupName(referencer); HashSet <MemObject> things; if (!referenceMap.TryGetValue(referencerTypeName, out things)) { things = new HashSet <MemObject>(); referenceMap[referencerTypeName] = things; } things.Add(mo); } } } List <KeyValuePair <int, string> > lines = new List <KeyValuePair <int, string> >(); foreach (var p in referenceMap) { HashSet <MemObject> objects = p.Value; int totalSize = 0; foreach (var obj in objects) { totalSize += obj.Size; } lines.Add(new KeyValuePair <int, string>(objects.Count, string.Format("<{0, 80}> {1, 10}, {2, 10}", p.Key, objects.Count, EditorUtility.FormatBytes(totalSize)))); } lines.Sort((x, y) => x.Key.CompareTo(y.Key) * -1); // would sort all results from the largest to the smallest StringBuilder sb = new StringBuilder(); sb.AppendFormat("----- <type: {0}> -----\n", mt.TypeName); sb.AppendFormat(" {0} objects ({1}) are referenced by {2} types listed below:\n", mt.Objects.Count, mt.SizeLiterally, lines.Count); sb.AppendFormat("-----------------------\n"); sb.AppendFormat("<{0, 80}> {1, 10}, {2, 10}\n", "type", "count", "size"); sb.AppendFormat("<{0, 80}> {1, 10}, {2, 10}\n", "----", "-----", "----"); foreach (var line in lines) { sb.AppendLine(line.Value); } UnityEngine.Debug.Log(sb.ToString()); }
public static void ShowStringStats(MemType mt) { Dictionary <string, int> counter = new Dictionary <string, int>(); int pathCount = 0; int winPathCount = 0; StringBuilder sb = new StringBuilder(); foreach (var obj in mt.Objects) { MemObject mo = obj as MemObject; if (mo != null) { if (mo.InstanceName.Split(new char[] { '/' }).Length >= 3) { pathCount++; } if (mo.InstanceName.Split(new char[] { '\\' }).Length >= 3) { sb.AppendFormat(" {0}\n", mo.InstanceName); winPathCount++; } if (counter.ContainsKey(mo.InstanceName)) { counter[mo.InstanceName]++; } else { counter.Add(mo.InstanceName, 1); } } } UnityEngine.Debug.LogFormat("path: {0}, winPath: {1}", pathCount, winPathCount); UnityEngine.Debug.LogFormat("all win paths: \n{0}", sb.ToString()); List <KeyValuePair <int, string> > lines = new List <KeyValuePair <int, string> >(); foreach (var p in counter) { if (p.Value >= 2) { lines.Add(new KeyValuePair <int, string>(p.Value, p.Key)); } } lines.Sort((x, y) => x.Key.CompareTo(y.Key) * -1); // would sort all results from the largest to the smallest if (lines.Count > 100) { lines.RemoveRange(100, lines.Count - 100); } UnityEngine.Debug.Log("----- string repetitions -----"); foreach (var line in lines) { try { UnityEngine.Debug.LogFormat(" {0, 5} {1}\n", line.Key, line.Value); } catch (System.Exception ex) { UnityEngine.Debug.LogFormat(" {0, 5} {1}: {2}\n", line.Key, "<invalid string>", ex.Message); } } }
private static void MarkStatus(MemObject mo, eDiffStatus status) { mo.InstanceName = Prefixes[(int)status] + mo.InstanceName; }