Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix struct typed memory observation's appending issue. #180

Merged
merged 1 commit into from
Mar 26, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a pretty big hammer... but OK

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only real alternative would be to make a copy first and have appendPointedNodes remove from the copy.

}

/**
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