Skip to content

Commit 623cbd9

Browse files
authored
Merge pull request #4 from jycamus90/edge-weight
Few changes
2 parents b8d7150 + 16db074 commit 623cbd9

12 files changed

+817
-548
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package org.spideruci.analysis.trace;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.io.PrintStream;
6+
import java.util.ArrayList;
7+
import java.util.HashMap;
8+
import java.util.List;
9+
import java.util.Map;
10+
11+
import org.spideruci.analysis.trace.io.TraceReader;
12+
import org.spideruci.cerebro.layout.model.DynamicFlowGraph;
13+
import org.spideruci.cerebro.layout.model.FlowIdent;
14+
import org.spideruci.cerebro.layout.model.SourceLineNode;
15+
16+
public class MethodTraceDirReader extends TraceDirReader {
17+
private static HashMap<String, Map<String, SourceLineNode>> methodMap = new HashMap<>();
18+
19+
public MethodTraceDirReader() {
20+
// TODO Auto-generated constructor stub
21+
}
22+
23+
public static DynamicFlowGraph scanTraceFiles(File[] files, final String path)
24+
throws IOException {
25+
if(path == null) {
26+
throw new RuntimeException("path is null ... fuck it!");
27+
}
28+
29+
DynamicFlowGraph dynamicFlowGraph = new DynamicFlowGraph();
30+
for(File f : files) {
31+
if(f == null || !f.isFile() || !f.getName().endsWith(".trc")) {
32+
continue;
33+
}
34+
35+
String name = f.getName();
36+
System.out.println("Scanning " + name);
37+
PrintStream jsonTraceStream = new PrintStream(path + name + ".json");
38+
jsonTraceStream.println("[");
39+
40+
TraceReader traceReader = new TraceReader(f);
41+
HashMap<String, SourceLineNode> threadedPrevious = new HashMap<>();
42+
int count = 0;
43+
String prevMethod = "";
44+
String prevClass = "";
45+
46+
while(true) {
47+
TraceEvent event = traceReader.readNextExecutedEvent();
48+
if(event == null) {
49+
break;
50+
}
51+
52+
if(event.getExecInsnType() != EventType.$line$) {
53+
continue;
54+
}
55+
56+
String ownerClass = traceReader.getExecutedEventOwnerClass(event);
57+
String ownerMethod = traceReader.getExecutedEventOwnerMethod(event);
58+
int lineNumber = traceReader.getExecutedEventSourceLine(event);
59+
String threadId = event.getExecThreadId();
60+
61+
SourceLineNode node = new SourceLineNode(ownerClass, ownerMethod, lineNumber);
62+
63+
if(!ownerMethod.equals(prevMethod) && !ownerClass.equals(prevClass)){
64+
if(!methodMap.containsKey(node.methodName())){
65+
node = dynamicFlowGraph.addNode(node);
66+
Map<String, SourceLineNode> temp = new HashMap<>();
67+
temp.put(node.className(), node);
68+
methodMap.put(node.methodName(), temp);
69+
}
70+
else{
71+
if(methodMap.get(node.methodName()).get(node.className()) != null)
72+
node = methodMap.get(node.methodName()).get(node.className());
73+
else{
74+
node = dynamicFlowGraph.addNode(node);
75+
methodMap.get(node.methodName()).put(node.className(), node);
76+
}
77+
}
78+
79+
80+
spitJsonTrace(jsonTraceStream, node.id());
81+
count += 1;
82+
SourceLineNode previous = threadedPrevious.get(threadId);
83+
if(previous != null) {
84+
dynamicFlowGraph.addEdge(previous, node);
85+
}
86+
87+
threadedPrevious.put(threadId, node);
88+
}
89+
prevMethod = ownerMethod;
90+
prevClass = ownerClass;
91+
}
92+
93+
jsonTraceStream.println("0]");
94+
FlowIdent ident = new FlowIdent(name, count);
95+
dynamicFlowGraph.addFlows(ident);
96+
}
97+
98+
return dynamicFlowGraph;
99+
}
100+
101+
}

src/main/java/org/spideruci/analysis/trace/TraceDirReader.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public static DynamicFlowGraph scanTraceFiles(File[] files, final String path)
8484

8585
node = dynamicFlowGraph.addNode(node);
8686

87-
spitJsonTrace(jsonTraceStream, node.id);
87+
spitJsonTrace(jsonTraceStream, node.id());
8888
count += 1;
8989
SourceLineNode previous = threadedPrevious.get(threadId);
9090
if(previous != null) {
@@ -102,7 +102,7 @@ public static DynamicFlowGraph scanTraceFiles(File[] files, final String path)
102102
return dynamicFlowGraph;
103103
}
104104

105-
private static void spitJsonTrace(PrintStream out, int nodeId) {
105+
protected static void spitJsonTrace(PrintStream out, int nodeId) {
106106
out.print(nodeId + ",\n");
107107
}
108108

src/main/java/org/spideruci/cerebro/Cerebro.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import java.io.IOException;
88

99
import org.graphstream.ui.layout.springbox.BarnesHutLayout;
10-
10+
import org.spideruci.analysis.trace.MethodTraceDirReader;
1111
import org.spideruci.analysis.trace.TraceDirReader;
1212
import org.spideruci.cerebro.community.JungCommunityComputer;
1313
import org.spideruci.cerebro.layout.model.DynamicFlowGraph;
@@ -67,6 +67,7 @@ public static DynamicFlowGraph getDynamicFlowGraph(String[] args) throws IOExcep
6767
String tracePath = args[0];
6868
File[] files = TraceDirReader.getFiles(args[0]);
6969
dynamicFlowGraph = TraceDirReader.scanTraceFiles(files, tracePath);
70+
// dynamicFlowGraph = MethodTraceDirReader.scanTraceFiles(files, tracePath);
7071
}
7172

7273
return dynamicFlowGraph;

src/main/java/org/spideruci/cerebro/layout/Commands.java

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public enum Commands {
1616
COLOR_BY_METHOD,
1717
COLOR_BY_CLASS,
1818
REMOVE_COLOR,
19+
FIND_START_NODE,
1920
DONE,
2021
HELP;
2122

@@ -80,6 +81,9 @@ public static boolean handleInput(DynamicDisplay display,
8081
case COLOR_BY_CLASS:
8182
display.colorNodesByClass();
8283
break;
84+
case FIND_START_NODE:
85+
display.setStartNode();
86+
break;
8387
case HELP:
8488
System.out.println(Commands.mainMenu);
8589
break;

0 commit comments

Comments
 (0)