add an example of constructing IRs and use of ReferenceCleanser

git-svn-id: https://wala.svn.sourceforge.net/svnroot/wala/trunk@3805 f5eafffb-2e1d-0410-98e4-8ec43c5233c4
This commit is contained in:
msridhar1 2010-03-15 22:58:41 +00:00
parent 4cda34d1e4
commit cab2bcfd07
2 changed files with 118 additions and 0 deletions

View File

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<launchConfiguration type="org.eclipse.jdt.launching.localJavaApplication">
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS">
<listEntry value="/com.ibm.wala.core.tests/src/com/ibm/wala/examples/analysis/ConstructAllIRs.java"/>
</listAttribute>
<listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES">
<listEntry value="1"/>
</listAttribute>
<listAttribute key="org.eclipse.jdt.launching.CLASSPATH">
<listEntry value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&#10;&lt;runtimeClasspathEntry containerPath=&quot;org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5&quot; javaProject=&quot;com.ibm.wala.core.tests&quot; path=&quot;1&quot; type=&quot;4&quot;/&gt;&#10;"/>
<listEntry value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&#10;&lt;runtimeClasspathEntry id=&quot;org.eclipse.jdt.launching.classpathentry.defaultClasspath&quot;&gt;&#10;&lt;memento exportedEntriesOnly=&quot;false&quot; project=&quot;com.ibm.wala.core.tests&quot;/&gt;&#10;&lt;/runtimeClasspathEntry&gt;&#10;"/>
<listEntry value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&#10;&lt;runtimeClasspathEntry path=&quot;3&quot; projectName=&quot;com.ibm.wala.core.testdata&quot; type=&quot;1&quot;/&gt;&#10;"/>
<listEntry value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&#10;&lt;runtimeClasspathEntry internalArchive=&quot;/com.ibm.wala.core.testdata/bin&quot; path=&quot;3&quot; type=&quot;2&quot;/&gt;&#10;"/>
<listEntry value="&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&#10;&lt;runtimeClasspathEntry internalArchive=&quot;/com.ibm.wala.core.testdata/src&quot; path=&quot;3&quot; type=&quot;2&quot;/&gt;&#10;"/>
</listAttribute>
<booleanAttribute key="org.eclipse.jdt.launching.DEFAULT_CLASSPATH" value="false"/>
<stringAttribute key="org.eclipse.jdt.launching.MAIN_TYPE" value="com.ibm.wala.examples.analysis.ConstructAllIRs"/>
<stringAttribute key="org.eclipse.jdt.launching.PROGRAM_ARGUMENTS" value="/Users/manu/Documents/workspaces/34workspace/com.ibm.wala.core.tests/dat/bcel.txt"/>
<stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="com.ibm.wala.core.tests"/>
<stringAttribute key="org.eclipse.jdt.launching.VM_ARGUMENTS" value="-ea -Xmx400M"/>
</launchConfiguration>

View File

@ -0,0 +1,97 @@
/*******************************************************************************
* Copyright (c) 2008 IBM Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package com.ibm.wala.examples.analysis;
import java.io.IOException;
import com.ibm.wala.classLoader.IClass;
import com.ibm.wala.classLoader.IMethod;
import com.ibm.wala.ipa.callgraph.AnalysisCache;
import com.ibm.wala.ipa.callgraph.AnalysisOptions;
import com.ibm.wala.ipa.callgraph.AnalysisScope;
import com.ibm.wala.ipa.callgraph.impl.Everywhere;
import com.ibm.wala.ipa.cha.ClassHierarchy;
import com.ibm.wala.ipa.cha.ClassHierarchyException;
import com.ibm.wala.util.config.AnalysisScopeReader;
import com.ibm.wala.util.perf.Stopwatch;
import com.ibm.wala.util.ref.ReferenceCleanser;
/**
* An analysis skeleton that simply constructs IRs for all methods in a class hierarchy. Illustrates the use of
* {@link ReferenceCleanser} to improve running time / reduce memory usage.
*/
public class ConstructAllIRs {
/**
* Should we periodically clear out soft reference caches in an attempt to help the GC?
*/
private final static boolean PERIODIC_WIPE_SOFT_CACHES = true;
/**
* Interval which defines the period to clear soft reference caches
*/
private final static int WIPE_SOFT_CACHE_INTERVAL = 2500;
/**
* Counter for wiping soft caches
*/
private static int wipeCount = 0;
/**
* First command-line argument should be location of scope file for application to analyze
*
* @throws IOException
* @throws ClassHierarchyException
*/
public static void main(String[] args) throws IOException, ClassHierarchyException {
String scopeFile = args[0];
// measure running time
Stopwatch s = new Stopwatch();
s.start();
AnalysisScope scope = AnalysisScopeReader.readJavaScope(scopeFile, null, ConstructAllIRs.class.getClassLoader());
// build a type hierarchy
System.out.print("building class hierarchy...");
ClassHierarchy cha = ClassHierarchy.make(scope);
System.out.println("done");
// register class hierarchy and AnalysisCache with the reference cleanser, so that their soft references are appropriately wiped
ReferenceCleanser.registerClassHierarchy(cha);
AnalysisCache cache = new AnalysisCache();
ReferenceCleanser.registerCache(cache);
AnalysisOptions options = new AnalysisOptions();
System.out.print("building IRs...");
for (IClass klass : cha) {
for (IMethod method : klass.getDeclaredMethods()) {
wipeSoftCaches();
// construct an IR; it will be cached
cache.getSSACache().findOrCreateIR(method, Everywhere.EVERYWHERE, options.getSSAOptions());
}
}
System.out.println("done");
s.stop();
System.out.println("RUNNING TIME: " + s.getElapsedMillis());
}
private static void wipeSoftCaches() {
if (PERIODIC_WIPE_SOFT_CACHES) {
wipeCount++;
if (wipeCount >= WIPE_SOFT_CACHE_INTERVAL) {
wipeCount = 0;
ReferenceCleanser.clearSoftCaches();
}
}
}
}