// Number of iterations to "warm up" and get methods compiled/inlined. // (this is conservative) staticfinalint WARMUP_LOOPS = 100000;
// Number of timing trials staticfinalint TIMING_TRIALS = 10;
// Seconds to run this in "stress" mode. This is double the average // time to expose the races of bug 6302839 on a Blade 1000. Invoke // the program with the "stress" option multiple times for more // confidence. staticfinalint STRESS_MILLISECONDS = 300000; staticfinalint SET_TIMING_LOOPS = 10000;
// Max seconds to run before terminating the test ("declaring victory"). staticint MAX_MILLISECONDS = 100;
// Number of iterations to time staticfinalint GET_TIMING_LOOPS = 10000000;
// Set true by main thread when NPE caught or time to terminate. // Set true by other thread when NPE caught. It makes // no difference where the NPE is thrown. staticvolatileboolean stopthreads = false;
// Number of getProperty invocations between main loop checks staticfinalint GETPROPERTY_LOOPS = 30000;
// Used by race and timing tests. Must get set non-null at lease once. static SecurityManager sm = new SecurityManager();
if (argv.length > 0) { if (argv[0].equals("time")) {
// Run the timing method // First warm up the method to make sure it gets compiled for (int i = 0; i < WARMUP_LOOPS; i++) {
timeit(1, 1, 1);
}
System.out.println("boo");
// Now do the actual timing
timeit(TIMING_TRIALS, GET_TIMING_LOOPS, SET_TIMING_LOOPS);
} elseif (argv[0].equals("stress")) {
// For stress test the test duration is boosted
MAX_MILLISECONDS = STRESS_MILLISECONDS;
} else { thrownew RuntimeException( "SecurityRace: " + argv[0]
+ " argument to main not recognized");
} // if argv
} // if length
long start = System.currentTimeMillis(),
end = start + MAX_MILLISECONDS;
// Create and start racing thread
(newThread(new SecurityRace())).start();
// main thread alternates batches of getProperty() with time checks try { do { if (stopthreads) {
// other thread suffered an NPE thrownew RuntimeException("SecurityRace failed with NPE");
}
for (int i = 0; i < GETPROPERTY_LOOPS; i++) {
s = System.getProperty("java.version");
}
} while (System.currentTimeMillis() < end);
} catch (NullPointerException e) { thrownew RuntimeException("SecurityRace failed with NPE");
} finally {
// make sure other thread terminates
stopthreads = true;
}
} // main
// System.security mutator. publicvoid run() { try { while (true) { if (stopthreads) { return;
}
System.setSecurityManager(sm);
// The goal is to catch another thread testing the // value set above and trying to use it after it's // nulled below.
System.setSecurityManager(null);
}
} catch (NullPointerException e) {
stopthreads = true;
return;
}
}
// Time method execution. Collects trials number of timings // for the number of accessor and mutator invocation loops // specified. publicstaticvoid timeit(int timing_trials, int get_timing_loops, int set_timing_loops) { try { long start;
// Time the methods and report average. // Time multiple trials so noise is apparent and a // T test can be used to establish significance. for (int j = 0; j < timing_trials; j++) {
start = System.nanoTime();
for (int i = 0; i < get_timing_loops; i++) {
sm = System.getSecurityManager();
}
// Don't print for "warmup" case. This might mean that // the compiler fails to compile the println (setting it // up to execute via interpretation using an "uncommon trap") // but we don't care if this println runs slowly! if (timing_trials > 1) {
System.out.println((float) (System.nanoTime() - start)
/ (float) get_timing_loops);
}
}
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.