internal Graph BuildTypeGraph(int allocatedAfterTickIndex, int allocatedBeforeTickIndex, BuildTypeGraphOptions options, FilterForm filterForm) { Graph graph; if (filterForm.filterVersion != 0 || options != BuildTypeGraphOptions.LumpBySignature || allocatedAfterTickIndex > 0 || allocatedBeforeTickIndex < int.MaxValue) { graph = new Graph(this, Graph.GraphType.HeapGraph); graph.previousGraphTickIndex = allocatedAfterTickIndex; } else { Graph previousGraph = cachedGraph; if (previousGraph != null && previousGraph.graphSource == this) { return(previousGraph); } cachedGraph = graph = new Graph(this, Graph.GraphType.HeapGraph); if (previousGraph != null) { graph.previousGraphTickIndex = ((ObjectGraph)previousGraph.graphSource).tickIndex; foreach (Vertex v in previousGraph.vertices.Values) { Vertex newV = graph.FindOrCreateVertex(v.name, v.signature, v.moduleName); if (v.weightHistory == null) { newV.weightHistory = new ulong[1]; } else { ulong[] weightHistory = v.weightHistory; newV.weightHistory = new ulong[Math.Min(weightHistory.Length + 1, historyDepth)]; for (int i = v.weightHistory.Length - 1; i > 0; i--) { newV.weightHistory[i] = weightHistory[i - 1]; } } newV.weightHistory[0] = v.weight; } } } graph.typeGraphOptions = options; graph.filterVersion = filterForm.filterVersion; if (graph.previousGraphTickIndex < graph.allocatedAfterTickIndex) { graph.previousGraphTickIndex = graph.allocatedAfterTickIndex; } graph.allocatedAfterTickIndex = allocatedAfterTickIndex; graph.allocatedBeforeTickIndex = allocatedBeforeTickIndex; GcObject rootObject = CreateRootObject(); for (int i = 0; i < rootCount; i++) { roots[i].InterestLevel = InterestLevel.Ignore; } foreach (GcObject gcObject in idToObject.Values) { gcObject.parent = null; gcObject.vertex = null; gcObject.InterestLevel = InterestLevel.Ignore; } AssignParents(rootObject); int index = 0; foreach (GcType gcType in typeIdToGcType.Values) { gcType.index = index++; } GcType[] gcTypes = new GcType[index]; typeHintTable = new int[index]; foreach (GcType gcType in typeIdToGcType.Values) { gcTypes[gcType.index] = gcType; } AssignInterestLevelsToTypes(options, filterForm); for (int i = 0; i < rootCount; i++) { AssignInterestLevelToObject(rootIDs[i], roots[i], options, filterForm, true); } foreach (KeyValuePair <ulong, GcObject> keyValuePair in idToObject) { GcObject gcObject = keyValuePair.Value; if (gcObject.AllocTickIndex > allocatedAfterTickIndex && gcObject.AllocTickIndex < allocatedBeforeTickIndex) { AssignInterestLevelToObject(keyValuePair.Key, gcObject, options, filterForm, false); } else { gcObject.InterestLevel = InterestLevel.Ignore; } } foreach (GcObject gcObject in idToObject.Values) { if (gcObject.InterestLevel == InterestLevel.Ignore) { CheckForParentMarkingDescendant(gcObject); } } FindVertex(0, rootObject, graph, options); for (int i = 0; i < rootCount; i++) { roots[i].vertex = null; FindVertex(rootIDs[i], roots[i], graph, options); } foreach (KeyValuePair <ulong, GcObject> keyValuePair in idToObject) { ulong id = keyValuePair.Key; GcObject gcObject = keyValuePair.Value; if (gcObject.parent == null || (gcObject.InterestLevel & (InterestLevel.Interesting | InterestLevel.Display)) == InterestLevel.Ignore) { continue; } FindVertex(id, gcObject, graph, options); } Vertex[] pathFromRoot = new Vertex[32]; foreach (GcObject gcObject in idToObject.Values) { if (gcObject.parent == null || (gcObject.InterestLevel & (InterestLevel.Interesting | InterestLevel.Display)) == InterestLevel.Ignore || gcObject.AllocTickIndex <= allocatedAfterTickIndex || gcObject.AllocTickIndex >= allocatedBeforeTickIndex) { continue; } int levels = 0; for (GcObject pathObject = gcObject; pathObject != null; pathObject = pathObject.parent) { if (pathObject.vertex != null) { levels++; } } while (pathFromRoot.Length < levels + 1) { pathFromRoot = new Vertex[pathFromRoot.Length * 2]; } int level = levels; for (GcObject pathObject = gcObject; pathObject != null; pathObject = pathObject.parent) { if (pathObject.vertex != null) { level--; pathFromRoot[level] = pathObject.vertex; } } levels = Vertex.SqueezeOutRepetitions(pathFromRoot, levels); for (int j = 0; j < levels - 1; j++) { Vertex fromVertex = pathFromRoot[j]; Vertex toVertex = pathFromRoot[j + 1]; Edge edge = graph.FindOrCreateEdge(fromVertex, toVertex); edge.AddWeight(gcObject.Size(this)); } Vertex thisVertex = pathFromRoot[levels - 1]; thisVertex.basicWeight += gcObject.Size(this); thisVertex.count += 1; } foreach (Vertex v in graph.vertices.Values) { if (v.weight < v.outgoingWeight) { v.weight = v.outgoingWeight; } if (v.weight < v.incomingWeight) { v.weight = v.incomingWeight; } if (v.weightHistory == null) { v.weightHistory = new ulong[1]; } } foreach (Vertex v in graph.vertices.Values) { v.active = true; } graph.BottomVertex.active = false; return(graph); }
internal Graph BuildReferenceGraph(Graph orgGraph) { var graph = new Graph(this, Graph.GraphType.ReferenceGraph); Vertex[] pathFromRoot = new Vertex[32]; GcObject rootObject = CreateRootObject(); FindVertex(0, rootObject, graph, BuildTypeGraphOptions.LumpBySignature); rootObject.InterestLevel = InterestLevel.Interesting; foreach (GcObject gcObject in idToObject.Values) { gcObject.parent = null; } // We wish to find all references to certain selected objects, // or, to be precise, all references that keep these objects alive. // To do this, we use a breadth first traversal of the object graph, using // a queue of objects still to process. If we find a reference to one of the // selected objects, we don't actually include this object, but instead // just make note of the reference // Initialize rootObject.parent = null; var foundBeforeMarker = new GcObject(); var queue = new Queue <GcObject>(); queue.Enqueue(rootObject); // Loop while (queue.Count != 0) { GcObject head = queue.Dequeue(); foreach (GcObject refObject in head.References) { if (refObject.parent == null || refObject.parent == foundBeforeMarker) { // this is a reference to either one of the "selected" objects // or just to a new object if (refObject.vertex != null && refObject.vertex.selected && (refObject.InterestLevel & (InterestLevel.Interesting | InterestLevel.Display)) != InterestLevel.Ignore && refObject.AllocTickIndex > orgGraph.allocatedAfterTickIndex && refObject.AllocTickIndex < orgGraph.allocatedBeforeTickIndex) { // add <root> -> ... -> head -> refObject to the reference graph int levels = 0; for (GcObject pathObject = head; pathObject != null; pathObject = pathObject.parent) { levels++; } while (pathFromRoot.Length < levels + 2) { pathFromRoot = new Vertex[pathFromRoot.Length * 2]; } pathFromRoot[levels + 1] = graph.FindOrCreateVertex(refObject.vertex.name, refObject.vertex.signature, refObject.vertex.moduleName); int level = levels; for (GcObject pathObject = head; pathObject != null; pathObject = pathObject.parent) { if ((pathObject.InterestLevel & (InterestLevel.Interesting | InterestLevel.Display)) == InterestLevel.Ignore || pathObject.vertex == null) { pathFromRoot[level] = null; } else { pathFromRoot[level] = graph.FindOrCreateVertex(pathObject.vertex.name, pathObject.vertex.signature, pathObject.vertex.moduleName); } level--; } int nonZeroLevels = 0; for (int j = 0; j <= levels + 1; j++) { if (pathFromRoot[j] != null) { pathFromRoot[nonZeroLevels++] = pathFromRoot[j]; } } levels = Vertex.SqueezeOutRepetitions(pathFromRoot, nonZeroLevels); for (int j = 0; j < levels - 1; j++) { Vertex fromVertex = pathFromRoot[j]; Vertex toVertex = pathFromRoot[j + 1]; Edge edge = graph.FindOrCreateEdge(fromVertex, toVertex); edge.AddWeight(1); } Vertex thisVertex = pathFromRoot[levels - 1]; thisVertex.basicWeight += 1; if (refObject.parent == null) { thisVertex.count += 1; refObject.parent = foundBeforeMarker; } } else { refObject.parent = head; queue.Enqueue(refObject); } } } } foreach (Vertex v in graph.vertices.Values) { if (v.weight < v.outgoingWeight) { v.weight = v.outgoingWeight; } if (v.weight < v.incomingWeight) { v.weight = v.incomingWeight; } if (v.weightHistory == null) { v.weightHistory = new ulong[1]; } } foreach (Vertex v in graph.vertices.Values) { v.active = true; } graph.BottomVertex.active = false; return(graph); }
internal Graph BuildTypeGraph(int allocatedAfterTickIndex, int allocatedBeforeTickIndex, BuildTypeGraphOptions options) { if (graph == null || FilterForm.filterVersion != graphFilterVersion || allocatedAfterTickIndex >= 0 || allocatedBeforeTickIndex < int.MaxValue) { graph = new Graph(this); graph.graphType = Graph.GraphType.HeapGraph; graphFilterVersion = FilterForm.filterVersion; graph.previousGraphTickIndex = allocatedAfterTickIndex; } else { ObjectGraph previousGraph = (ObjectGraph)graph.graphSource; graph.previousGraphTickIndex = previousGraph.tickIndex; graph.graphSource = this; foreach (Vertex v in graph.vertices.Values) { if (v.weightHistory == null) { v.weightHistory = new uint[1]; } else { uint[] weightHistory = v.weightHistory; if (weightHistory.Length < historyDepth) { v.weightHistory = new uint[weightHistory.Length + 1]; } for (int i = v.weightHistory.Length - 1; i > 0; i--) { v.weightHistory[i] = weightHistory[i - 1]; } } v.weightHistory[0] = v.weight; v.weight = v.incomingWeight = v.outgoingWeight = v.basicWeight = 0; v.count = 0; foreach (Edge e in v.outgoingEdges.Values) { e.weight = 0; } } } if (graph.previousGraphTickIndex < graph.allocatedAfterTickIndex) { graph.previousGraphTickIndex = graph.allocatedAfterTickIndex; } graph.allocatedAfterTickIndex = allocatedAfterTickIndex; graph.allocatedBeforeTickIndex = allocatedBeforeTickIndex; GcType rootType = GetOrCreateGcType("<root>", 0); GcObject rootObject = GetOrCreateObject(0); rootObject.type = rootType; rootObject.references = roots; foreach (GcObject gcObject in idToObject.Values) { gcObject.level = int.MaxValue; gcObject.vertex = null; } AssignLevels(rootObject); AssignInterestLevels(); int index = 0; foreach (GcType gcType in typeNameToGcType.Values) { gcType.index = index++; } GcType[] gcTypes = new GcType[index]; typeHintTable = new int[index]; foreach (GcType gcType in typeNameToGcType.Values) { gcTypes[gcType.index] = gcType; } Vertex[] pathFromRoot = new Vertex[32]; foreach (GcObject gcObject in idToObject.Values) { if (gcObject.level == int.MaxValue || (gcObject.interestLevel & (InterestLevel.Interesting | InterestLevel.Display)) == InterestLevel.Ignore || gcObject.allocTickIndex <= allocatedAfterTickIndex || gcObject.allocTickIndex >= allocatedBeforeTickIndex) { continue; } while (pathFromRoot.Length < gcObject.level + 1) { pathFromRoot = new Vertex[pathFromRoot.Length * 2]; } for (GcObject pathObject = gcObject; pathObject != null; pathObject = pathObject.parent) { if ((pathObject.interestLevel & (InterestLevel.Interesting | InterestLevel.Display)) == InterestLevel.Ignore) { pathFromRoot[pathObject.level] = null; } else { pathFromRoot[pathObject.level] = FindVertex(pathObject, graph, options); } } int levels = 0; for (int i = 0; i <= gcObject.level; i++) { if (pathFromRoot[i] != null) { pathFromRoot[levels++] = pathFromRoot[i]; } } levels = Vertex.SqueezeOutRepetitions(pathFromRoot, levels); for (int i = 0; i < levels - 1; i++) { Vertex fromVertex = pathFromRoot[i]; Vertex toVertex = pathFromRoot[i + 1]; Edge edge = graph.FindOrCreateEdge(fromVertex, toVertex); edge.AddWeight(gcObject.size); } Vertex thisVertex = pathFromRoot[levels - 1]; thisVertex.basicWeight += gcObject.size; thisVertex.count += 1; } foreach (Vertex v in graph.vertices.Values) { if (v.weight < v.outgoingWeight) { v.weight = v.outgoingWeight; } if (v.weight < v.incomingWeight) { v.weight = v.incomingWeight; } if (v.weightHistory == null) { v.weightHistory = new uint[1]; } } foreach (Vertex v in graph.vertices.Values) { v.active = true; } graph.BottomVertex.active = false; return(graph); }