Skip to content

Commit

Permalink
Fix struct typed memory observation's appending issue. (#180)
Browse files Browse the repository at this point in the history
- Bug: http://b/152407425.
  • Loading branch information
stellama0208 authored Mar 26, 2020
1 parent 7055e37 commit 9f81060
Showing 1 changed file with 13 additions and 26 deletions.
39 changes: 13 additions & 26 deletions gapic/src/main/com/google/gapid/models/Memory.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.common.primitives.UnsignedLongs;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
Expand All @@ -43,6 +44,8 @@
import com.google.gapid.util.Ranges;
import com.google.gapid.util.TypeInfos;

import java.util.Set;
import java.util.stream.Collectors;
import org.eclipse.swt.widgets.Shell;

import java.lang.ref.SoftReference;
Expand Down Expand Up @@ -641,38 +644,21 @@ private List<StructNode> loadChildren() {
/**
* Utility method. Simplify trees, especially for vulkan structs.
* 1. Remove redundant layers for all the trees.
* 2. Identify some trees to be the main trees. (Assume the main trees to be those with type
* TypeInfo.StructType, they usually contain key info like VkPresentInfoKHR, VkSubmitInfo...)
* 3. Combine trees together by appending some smaller trees to the main trees, if they are
* 2. Combine trees together by appending some smaller trees to the main trees, if they are
* related through a pointer field.
*/
public static List<StructNode> simplifyTrees(List<StructNode> trees) {
List<StructNode> simplified = new ArrayList<StructNode>();

// Remove redundant layers.
Map<Long, StructNode> nodes = new HashMap<Long, StructNode>();
for (StructNode tree : trees) {
nodes.put(tree.getRootAddress(), removeExtraLayers(tree));
}

// Find the main trees.
for (Iterator<Map.Entry<Long, StructNode>> it = nodes.entrySet().iterator(); it.hasNext(); ) {
Map.Entry<Long, StructNode> entry = it.next();
if (containsStructType(entry.getValue())) {
simplified.add(entry.getValue());
it.remove();
}
}

// Append other trees to the main trees if possible.
for (StructNode mainTree : simplified) {
appendPointedNodes(mainTree, nodes);
}

// Add the remaining unappended nodes to the returning result.
// Append pointed nodes to corresponding pointer field if possible.
Set<StructNode> appended = Sets.newHashSet();
for (StructNode node : nodes.values()) {
simplified.add(node);
appendPointedNodes(node, nodes, appended);
}
return simplified;
return nodes.values().stream().filter(n -> !appended.contains(n)).collect(Collectors.toList());
}

/**
Expand Down Expand Up @@ -717,19 +703,20 @@ private static boolean containsStructType(StructNode root) {
* Find all the nodes with type TypeInfo.PointerType in this tree, append the pointed tree to
* these nodes if possible.
*/
private static void appendPointedNodes(StructNode root, Map<Long, StructNode> nodes) {
private static void appendPointedNodes(StructNode root, Map<Long, StructNode> nodes,
Set<StructNode> appended) {
if (root == null) {
return;
}
if (root.getTypeCase() == TyCase.POINTER) {
long pointedAddress = root.getValue().getPointer().getAddress();
if (nodes.containsKey(pointedAddress)) {
root.getChildren().add(nodes.get(pointedAddress));
nodes.remove(pointedAddress);
appended.add(nodes.get(pointedAddress));
}
}
for (StructNode child : root.getChildren()) {
appendPointedNodes(child, nodes);
appendPointedNodes(child, nodes, appended);
}
}
}
Expand Down

0 comments on commit 9f81060

Please sign in to comment.