-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
276 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
jdcallgraph/src/main/java/com/dkarv/jdcallgraph/FastFieldAccessRecorder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/* | ||
* MIT License | ||
* <p> | ||
* Copyright (c) 2017 David Krebs | ||
* <p> | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* <p> | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* <p> | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package com.dkarv.jdcallgraph; | ||
|
||
import com.dkarv.jdcallgraph.callgraph.CallGraph; | ||
import com.dkarv.jdcallgraph.data.DataDependenceGraph; | ||
import com.dkarv.jdcallgraph.util.StackItem; | ||
import com.dkarv.jdcallgraph.util.StackItemCache; | ||
import com.dkarv.jdcallgraph.util.config.Config; | ||
import com.dkarv.jdcallgraph.util.log.Logger; | ||
import com.dkarv.jdcallgraph.util.options.Target; | ||
import java.io.IOException; | ||
|
||
/** | ||
* Improved version of the fast field access recorder tuned for performance. The downsides are: | ||
* - It might not always be right | ||
* - No logging available | ||
*/ | ||
public class FastFieldAccessRecorder { | ||
private static final Logger LOG = new Logger(FastFieldAccessRecorder.class); | ||
|
||
/** | ||
* Collect the call graph per thread. | ||
*/ | ||
static DataDependenceGraph GRAPH; | ||
private static final boolean needCombined; | ||
private static final int SIZE = 10; | ||
private static final int[] HASH_CACHE = new int[SIZE]; | ||
private static final String[] CACHE = new String[SIZE]; | ||
private static int POS = SIZE - 1; | ||
|
||
static { | ||
boolean combined = false; | ||
for (Target t : Config.getInst().writeTo()) { | ||
if (t == Target.COMBINED_DOT) { | ||
combined = true; | ||
} | ||
} | ||
needCombined = combined; | ||
try { | ||
GRAPH = new DataDependenceGraph(); | ||
} catch (IOException e) { | ||
throw new IllegalStateException("Error setting up data dependence graph", e); | ||
} | ||
} | ||
|
||
public static void write(String fromClass, String fromMethod, int lineNumber, String fieldClass, | ||
String fieldName) { | ||
int hash = 31 * fieldClass.hashCode() + fieldName.hashCode(); | ||
for (int i = 0; i < SIZE; i++) { | ||
if (HASH_CACHE[i] == hash) { | ||
HASH_CACHE[i] = -1; | ||
CACHE[i] = null; | ||
} | ||
} | ||
|
||
try { | ||
StackItem item = StackItemCache.get(fromClass, fromMethod, lineNumber, false); | ||
GRAPH.addWrite(item, fieldClass + "::" + fieldName); | ||
} catch (Exception e) { | ||
LOG.error("Error in write", e); | ||
} | ||
} | ||
|
||
public static void read(String fromClass, String fromMethod, int lineNumber, String fieldClass, | ||
String fieldName) { | ||
int hash = 31 * fieldClass.hashCode() + fieldName.hashCode(); | ||
for (int j = 0, i = POS; j < SIZE; j++, i = (i + 1) % SIZE) { | ||
if (HASH_CACHE[i] == hash) { | ||
String s = | ||
fromClass + "%" + fromMethod + "%" + lineNumber + "%" + fieldClass + "%" + fieldName; | ||
if (s.equals(CACHE[i])) { | ||
// we already saw this read recently | ||
return; | ||
} | ||
} | ||
} | ||
|
||
// store in cache | ||
POS = (POS - 1 + SIZE) % SIZE; | ||
HASH_CACHE[POS] = hash; | ||
CACHE[POS] = | ||
fromClass + "%" + fromMethod + "%" + lineNumber + "%" + fieldClass + "%" + fieldName; | ||
|
||
try { | ||
CallGraph callGraph; | ||
if (needCombined) { | ||
callGraph = CallRecorder.GRAPHS.get(Thread.currentThread().getId()); | ||
} else { | ||
callGraph = null; | ||
} | ||
StackItem item = StackItemCache.get(fromClass, fromMethod, lineNumber, false); | ||
GRAPH.addRead(item, fieldClass + "::" + fieldName, callGraph); | ||
} catch (Exception e) { | ||
LOG.error("Error in read", e); | ||
} | ||
} | ||
|
||
public static void log(Object o) { | ||
LOG.info("log: {}", o); | ||
} | ||
|
||
public static void shutdown() { | ||
try { | ||
GRAPH.finish(); | ||
} catch (IOException e) { | ||
LOG.error("Error finishing call graph.", e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
jdcallgraph/src/test/java/com/dkarv/jdcallgraph/FastFieldAccessRecorderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package com.dkarv.jdcallgraph; | ||
|
||
import com.dkarv.jdcallgraph.callgraph.CallGraph; | ||
import com.dkarv.jdcallgraph.data.DataDependenceGraph; | ||
import com.dkarv.jdcallgraph.util.StackItem; | ||
import com.dkarv.jdcallgraph.util.config.Config; | ||
import com.dkarv.jdcallgraph.util.config.ConfigUtils; | ||
import java.io.IOException; | ||
import java.util.Random; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.Mockito; | ||
|
||
public class FastFieldAccessRecorderTest { | ||
private static Random RND = new Random(123987); | ||
|
||
private DataDependenceGraph graph; | ||
private String[][] methods = new String[20][]; | ||
private int[] lines = new int[methods.length]; | ||
private String[][] fields = new String[methods.length][]; | ||
|
||
@Before | ||
public void setUp() { | ||
Config c = new Config(); | ||
ConfigUtils.inject(c); | ||
|
||
graph = Mockito.mock(DataDependenceGraph.class); | ||
FastFieldAccessRecorder.GRAPH = graph; | ||
|
||
for (int i = 0; i < methods.length; i++) { | ||
methods[i] = new String[]{"class_" + RND.nextInt(), "method_" + RND.nextInt()}; | ||
lines[i] = RND.nextInt(); | ||
fields[i] = new String[]{"class-" + RND.nextInt(), "field-" + RND.nextInt()}; | ||
} | ||
} | ||
|
||
@Test | ||
public void testWrite() throws IOException { | ||
write(0); | ||
verifyWrite(0); | ||
|
||
write(1); | ||
verifyWrite(1); | ||
} | ||
|
||
@Test | ||
public void testRead() throws IOException { | ||
read(0); | ||
verifyRead(0); | ||
} | ||
|
||
@Test | ||
public void testReadCache() throws IOException { | ||
read(0); | ||
verifyRead(0); | ||
|
||
// should ignore further reads | ||
read(0); | ||
read(1); | ||
verifyRead(1); | ||
|
||
read(1); | ||
for (int i = 0; i < 10; i++) { | ||
read(0); | ||
} | ||
} | ||
|
||
@Test | ||
public void testCacheReset() throws IOException { | ||
read(0); | ||
verifyRead(0); | ||
|
||
write(0); | ||
verifyWrite(0); | ||
read(0); | ||
verifyRead(0, 2); | ||
} | ||
|
||
@After | ||
public void after() { | ||
Mockito.verifyNoMoreInteractions(graph); | ||
} | ||
|
||
private void write(int i) { | ||
FastFieldAccessRecorder | ||
.write(methods[i][0], methods[i][1], lines[i], fields[i][0], fields[i][1]); | ||
} | ||
|
||
private void read(int i) { | ||
FastFieldAccessRecorder | ||
.read(methods[i][0], methods[i][1], lines[i], fields[i][0], fields[i][1]); | ||
} | ||
|
||
private void verifyWrite(int i) throws IOException { | ||
verifyWrite(i, 1); | ||
} | ||
|
||
private void verifyWrite(int i, int n) throws IOException { | ||
Mockito.verify(graph, Mockito.times(n)) | ||
.addWrite(Mockito.eq(new StackItem(methods[i][0], methods[i][1], lines[i])), | ||
Mockito.eq(fields[i][0] + "::" + fields[i][1])); | ||
} | ||
|
||
private void verifyRead(int i) throws IOException { | ||
verifyRead(i, 1); | ||
} | ||
|
||
private void verifyRead(int i, int n) throws IOException { | ||
Mockito.verify(graph, Mockito.times(n)) | ||
.addRead(Mockito.eq(new StackItem(methods[i][0], methods[i][1], lines[i])), | ||
Mockito.eq(fields[i][0] + "::" + fields[i][1]), Mockito.<CallGraph>isNull()); | ||
} | ||
} |