if (!hasJit()) { // Test requires JIT for creating profiling infos. return;
}
// Register `file2` with an empty jar. Even though `file2` is registered before `file`, the // runtime should not write bootclasspath methods to `file2`, and it should not even create // `file2`.
File file2 = createTempFile();
file2.deleteOnExit();
String emptyJarPath =
System.getenv("DEX_LOCATION") + "/res/art-gtest-jars-MainEmptyUncompressed.jar";
VMRuntime.registerAppInfo("test.app", file2.getPath(), file2.getPath(), new String[] {emptyJarPath}, VMRuntime.CODE_PATH_TYPE_SPLIT_APK);
// Delete the files so that we can check if the runtime creates them. The runtime should // create `file` and `file3` but not `file2`.
file.delete();
file2.delete();
file3.delete();
// Test that the runtime saves the profiling info of an app method in a .jar file.
Method appMethod =
Main.class.getDeclaredMethod("testAddMethodToProfile", File.class, Method.class);
testAddMethodToProfile(file, appMethod);
// Test that the runtime saves the profiling info of an app method in a .dex file.
ClassLoader dexClassLoader = (ClassLoader) Class.forName("dalvik.system.PathClassLoader")
.getDeclaredConstructor(String.class, ClassLoader.class)
.newInstance(dexPath, null/* parent */); Class<?> c = Class.forName("Main", true/* initialize */, dexClassLoader);
Method methodInDex = c.getMethod("main", (new String[0]).getClass());
testAddMethodToProfile(file3, methodInDex);
// Test that the runtime saves the profiling info of a bootclasspath method.
Method bootMethod = File.class.getDeclaredMethod("exists"); if (bootMethod.getDeclaringClass().getClassLoader() != Object.class.getClassLoader()) {
System.out.println("Class loader does not match boot class");
}
testAddMethodToProfile(file, bootMethod);
// We never expect System.console to be executed before Main.main gets invoked, and therefore // it should never be in a profile.
Method bootNotInProfileMethod = System.class.getDeclaredMethod("console");
testMethodNotInProfile(file, bootNotInProfileMethod);
testProfileNotExist(file2);
if (!isForBootImage(file.getPath())) { thrownew Error("Expected profile to be for boot image");
}
// Test that: // 1. The runtime always writes to disk upon a forced save, even if there is nothing to update. // 2. The profile for the primary APK is always the last one to write. // The checks may yield false negatives. Repeat multiple times to reduce the chance of false // negatives. for (int i = 0; i < 10; i++) {
Duration primaryTimestampBefore = getMTime(file.getPath());
Duration splitTimestampBefore = getMTime(file3.getPath());
ensureProfileProcessing();
Duration primaryTimestampAfter = getMTime(file.getPath());
Duration splitTimestampAfter = getMTime(file3.getPath());
if (primaryTimestampAfter.compareTo(primaryTimestampBefore) <= 0) { thrownew Error(
String.format("Profile for primary APK not updated (before: %d, after: %d)",
primaryTimestampBefore.toNanos(), primaryTimestampAfter.toNanos()));
} if (splitTimestampAfter.compareTo(splitTimestampBefore) <= 0) { thrownew Error(
String.format("Profile for split APK not updated (before: %d, after: %d)",
primaryTimestampBefore.toNanos(), primaryTimestampAfter.toNanos()));
} if (primaryTimestampAfter.compareTo(splitTimestampAfter) < 0) { thrownew Error(String.format( "Profile for primary APK is unexpected updated before profile for "
+ "split APK (primary: %d, split: %d)",
primaryTimestampAfter.toNanos(), splitTimestampAfter.toNanos()));
}
}
}
staticvoid testAddMethodToProfile(File file, Method m) { // Make sure we have a profile info for this method without the need to loop.
ensureProfilingInfo(m); // Make sure the profile gets saved.
ensureProfileProcessing(); // Verify that the profile was saved and contains the method. if (!presentInProfile(file.getPath(), m)) { thrownew RuntimeException("Expected method " + m + " to be in the profile");
}
}
staticvoid testMethodNotInProfile(File file, Method m) { // Make sure the profile gets saved.
ensureProfileProcessing(); // Verify that the profile was saved and contains the method. if (presentInProfile(file.getPath(), m)) { thrownew RuntimeException("Did not expect method " + m + " to be in the profile");
}
}
staticvoid testProfileNotExist(File file) { // Make sure the profile saving has been attempted.
ensureProfileProcessing(); // Verify that the profile does not exist. if (file.exists()) { thrownew RuntimeException("Did not expect " + file + " to exist");
}
}
// Ensure a method has a profiling info. publicstaticvoid ensureProfilingInfo(Method method) {
ensureJitBaselineCompiled(method.getDeclaringClass(), method.getName());
} publicstaticnativevoid ensureJitBaselineCompiled(Class<?> cls, String methodName); // Ensures the profile saver does its usual processing. publicstaticnativevoid ensureProfileProcessing(); // Checks if the profiles saver knows about the method. publicstaticnativeboolean presentInProfile(String profile, Method method); // Returns true if the profile is for the boot image. publicstaticnativeboolean isForBootImage(String profile); publicstaticnativeboolean hasJit();
privatestatic Duration getMTime(String path) throws Exception { // We cannot use `Files.getLastModifiedTime` because it doesn't have nanosecond precision.
StructTimespec st_mtim = Os.stat(path).st_mtim; return Duration.ofSeconds(st_mtim.tv_sec).plus(Duration.ofNanos(st_mtim.tv_nsec));
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.