Instrumenter doesn't have to do profiling

This commit is contained in:
Andrey Breslav
2013-05-13 16:06:55 +04:00
parent 7782621617
commit 9efc885c37
@@ -12,7 +12,7 @@ import java.util.*;
public class Preloader { public class Preloader {
public static final int PRELOADER_ARG_COUNT = 4; public static final int PRELOADER_ARG_COUNT = 4;
private static final String PROFILE = "profile="; private static final String INSTRUMENT_PREFIX = "instrument=";
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
if (args.length < PRELOADER_ARG_COUNT) { if (args.length < PRELOADER_ARG_COUNT) {
@@ -33,9 +33,9 @@ public class Preloader {
return; return;
} }
String profilingModeStr = args[3]; String modeStr = args[3];
ProfilingMode profilingMode = parseMeasureTime(profilingModeStr); Mode mode = parseMode(modeStr);
URL[] instrumentersClasspath = parseInstrumentersClasspath(profilingMode, profilingModeStr); URL[] instrumentersClasspath = parseInstrumentersClasspath(mode, modeStr);
long startTime = System.nanoTime(); long startTime = System.nanoTime();
@@ -43,7 +43,7 @@ public class Preloader {
ClassLoader withInstrumenter = instrumentersClasspath.length > 0 ? new URLClassLoader(instrumentersClasspath, parent) : parent; ClassLoader withInstrumenter = instrumentersClasspath.length > 0 ? new URLClassLoader(instrumentersClasspath, parent) : parent;
Handler handler = getHandler(profilingMode, withInstrumenter); Handler handler = getHandler(mode, withInstrumenter);
ClassLoader preloaded = ClassPreloadingUtils.preloadClasses(files, classNumber, withInstrumenter, handler); ClassLoader preloaded = ClassPreloadingUtils.preloadClasses(files, classNumber, withInstrumenter, handler);
Class<?> mainClass = preloaded.loadClass(mainClassCanonicalName); Class<?> mainClass = preloaded.loadClass(mainClassCanonicalName);
@@ -53,7 +53,7 @@ public class Preloader {
mainMethod.invoke(0, new Object[] {Arrays.copyOfRange(args, PRELOADER_ARG_COUNT, args.length)}); mainMethod.invoke(0, new Object[] {Arrays.copyOfRange(args, PRELOADER_ARG_COUNT, args.length)});
} }
finally { finally {
if (profilingMode != ProfilingMode.NO_TIME) { if (mode != Mode.NO_TIME) {
long dt = System.nanoTime() - startTime; long dt = System.nanoTime() - startTime;
System.out.format("Total time: %.3fs\n", dt / 1e9); System.out.format("Total time: %.3fs\n", dt / 1e9);
} }
@@ -61,11 +61,11 @@ public class Preloader {
} }
} }
private static URL[] parseInstrumentersClasspath(ProfilingMode profilingMode, String profilingModeStr) private static URL[] parseInstrumentersClasspath(Mode mode, String modeStr)
throws MalformedURLException { throws MalformedURLException {
URL[] instrumentersClasspath; URL[] instrumentersClasspath;
if (profilingMode == ProfilingMode.PROFILE) { if (mode == Mode.INSTRUMENT) {
List<File> instrumentersClassPathFiles = parseClassPath(getClassPath(profilingModeStr)); List<File> instrumentersClassPathFiles = parseClassPath(getClassPath(modeStr));
instrumentersClasspath = new URL[instrumentersClassPathFiles.size()]; instrumentersClasspath = new URL[instrumentersClassPathFiles.size()];
for (int i = 0; i < instrumentersClassPathFiles.size(); i++) { for (int i = 0; i < instrumentersClassPathFiles.size(); i++) {
File file = instrumentersClassPathFiles.get(i); File file = instrumentersClassPathFiles.get(i);
@@ -78,8 +78,8 @@ public class Preloader {
return instrumentersClasspath; return instrumentersClasspath;
} }
private static String getClassPath(String profilingModeStr) { private static String getClassPath(String modeStr) {
return profilingModeStr.substring(PROFILE.length()); return modeStr.substring(INSTRUMENT_PREFIX.length());
} }
private static List<File> parseClassPath(String classpath) { private static List<File> parseClassPath(String classpath) {
@@ -96,10 +96,10 @@ public class Preloader {
return files; return files;
} }
private static Handler getHandler(ProfilingMode profilingMode, ClassLoader withInstrumenter) { private static Handler getHandler(Mode mode, ClassLoader withInstrumenter) {
if (profilingMode == ProfilingMode.NO_TIME) return new Handler(); if (mode == Mode.NO_TIME) return new Handler();
final Instrumenter instrumenter = profilingMode == ProfilingMode.PROFILE ? loadInstrumenter(withInstrumenter) : null; final Instrumenter instrumenter = mode == Mode.INSTRUMENT ? loadInstrumenter(withInstrumenter) : null;
final int[] counter = new int[1]; final int[] counter = new int[1];
final int[] size = new int[1]; final int[] size = new int[1];
@@ -137,24 +137,24 @@ public class Preloader {
} }
} }
private enum ProfilingMode { private enum Mode {
NO_TIME, NO_TIME,
TIME, TIME,
PROFILE INSTRUMENT
} }
private static ProfilingMode parseMeasureTime(String arg) { private static Mode parseMode(String arg) {
if ("time".equals(arg)) return ProfilingMode.TIME; if ("time".equals(arg)) return Mode.TIME;
if ("notime".equals(arg)) return ProfilingMode.NO_TIME; if ("notime".equals(arg)) return Mode.NO_TIME;
if (arg.startsWith(PROFILE)) return ProfilingMode.PROFILE; if (arg.startsWith(INSTRUMENT_PREFIX)) return Mode.INSTRUMENT;
System.out.println("Unrecognized argument: " + arg); System.out.println("Unrecognized argument: " + arg);
printUsageAndExit(); printUsageAndExit();
return ProfilingMode.NO_TIME; return Mode.NO_TIME;
} }
private static void printUsageAndExit() { private static void printUsageAndExit() {
System.out.println("Usage: Preloader <paths to jars> <main class> <class number estimate> <time|notime|profile=<profiling class path>> <parameters to pass to the main class>"); System.out.println("Usage: Preloader <paths to jars> <main class> <class number estimate> <notime|time|instrument=<instrumenters class path>> <parameters to pass to the main class>");
System.exit(1); System.exit(1);
} }