private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed try { sampler = Sampler.getSampler(10, 100000, new File("."), "dump"); sampler.start(); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); throw new RuntimeException(ex); } }//GEN-LAST:event_jMenuItem1ActionPerformed
private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed if (sampler != null) { try { sampler.stop(); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); throw new RuntimeException(ex); } Map<String, SampleNode> samples = sampler.getStackCollectionsAndReset(); for (Map.Entry<String, SampleNode> entry : samples.entrySet()) { try { setFrames(entry.getValue(), "self:" + entry.getKey()); } catch (IOException ex) { throw new UncheckedIOException(ex); } } } }//GEN-LAST:event_jMenuItem2ActionPerformed
public static synchronized Sampler getSampler(final int sampleTimeMillis, final int dumpTimeMillis, final SamplerSupplier collector, final File dumpFolder, final String dumpFilePrefix) throws InterruptedException { if (instance == null) { try { instance = new Sampler(sampleTimeMillis, dumpTimeMillis, collector, dumpFolder.getCanonicalFile(), dumpFilePrefix); } catch (IOException ex) { throw new UncheckedIOException(ex); } instance.registerJmx(); return instance; } else { instance.dispose(); try { instance = new Sampler(sampleTimeMillis, dumpTimeMillis, collector, dumpFolder.getCanonicalFile(), dumpFilePrefix); } catch (IOException ex) { throw new UncheckedIOException(ex); } instance.registerJmx(); return instance; } }
@Override public void doRun() throws InterruptedException, IOException { sampler.stop(); sampler.dumpToFile(); sampler.dispose(); }
@Test public void testJmx() throws InterruptedException, IOException { Sampler sampler = new Sampler((t) -> new SimpleStackCollector(t)); sampler.registerJmx(); sampler.start(); main(new String[]{}); sampler.stop(); SampleNode original = sampler.getStackCollections().values().iterator().next(); File file = sampler.dumpToFile(); LOG.debug("Samples saved to {}", file); Assert.assertNotNull(file); Assert.assertTrue(file.exists()); SampleNode loaded = Converter.load(file); Assert.assertEquals(original, loaded); sampler.stop(); }
@Test public void testSampler2() throws InterruptedException, IOException { Sampler sampler = Sampler.getSampler(5, 2000, new File(org.spf4j.base.Runtime.TMP_FOLDER), "test"); sampler.start(); File dumpToFile = sampler.dumpToFile("id"); LOG.debug("saved to file {}", dumpToFile); Assert.assertThat(dumpToFile.getAbsolutePath(), Matchers.not(Matchers.containsString(":"))); sampler.stop(); }
public Map<String, SampleNode> sampleTest(final Sampler sampler, final String filename) throws InterruptedException, IOException { sampler.registerJmx(); sampler.start(); MonitorTest.main(new String[]{}); final File serializedFile = File.createTempFile(filename, ".ssdump3"); Map<String, SampleNode> collected = sampler.getStackCollectionsAndReset(); Converter.saveLabeledDumps(serializedFile, collected); LOG.debug("Dumped to file {}", serializedFile); sampler.stop(); Map<String, SampleNode> loadedDumps = Converter.loadLabeledDumps(serializedFile); for (Map.Entry<String, SampleNode> entry : loadedDumps.entrySet()) { LOG.debug("Loaded {}", entry.getKey()); } return collected; } }
@Test public void testSampler() throws InterruptedException { Sampler sampler = Sampler.getSampler(5, 2000, new File(org.spf4j.base.Runtime.TMP_FOLDER), "test"); LogAssert expect = TestLoggers.sys() .expect(Sampler.class.getName(), Level.INFO, 5000, TimeUnit.MILLISECONDS, LogMatchers.hasFormat("Stack samples written to {}")); sampler.start(); LOG.debug("started sampling"); expect.assertObservation(); sampler.stop(); }
final Sampler sampler = new Sampler(options.sampleInterval, options.dumpInterval, (t) -> new FastStackCollector(false, true, new Thread[]{t}), options.dumpFolder, options.dumpFilePrefix); sampler.registerJmx(); sampler.start();
@Override public void testRunStarted(final Description description) { sampler.start(); }
@Override public void testRunFinished(final Result result) throws InterruptedException { sampler.stop(); }
@Override public void testFailure(final Failure failure) throws IOException { File dumpToFile = sampler.dumpToFile(new File(destinationFolder, failure.getTestHeader() + ".ssdump2")); if (dumpToFile != null) { LOG.info("Profile saved to {}", dumpToFile); } }
public Spf4jRunListener() { sampler = new Sampler(Integer.getInteger("spf4j.junit.sampleTimeMillis", 5), Integer.getInteger("spf4j.junit.dumpAfterMillis", Integer.MAX_VALUE), (t) -> new FastStackCollector(false, true, new Thread[] {t})); destinationFolder = new File(System.getProperty("spf4j.junit.destinationFolder", "target/junit-ssdump")); if (!destinationFolder.mkdirs() && !destinationFolder.canWrite()) { throw new ExceptionInInitializerError("Unable to write to " + destinationFolder); } }
public static synchronized Sampler getSampler(final int sampleTimeMillis, final int dumpTimeMillis, final File dumpFolder, final String dumpFilePrefix) throws InterruptedException { return getSampler(sampleTimeMillis, dumpTimeMillis, (t) -> new FastStackCollector(false, true, new Thread[]{t}), dumpFolder, dumpFilePrefix); }
@SuppressFBWarnings("MDM_THREAD_YIELD") public static void main(final String[] args) throws InterruptedException, TimeoutException, SQLException, IOException { String connectionString = args[0]; String semaphoreName = args[1]; JdbcDataSource ds = new JdbcDataSource(); ds.setURL(connectionString); ds.setUser("sa"); ds.setPassword("sa"); Sampler s = new Sampler(5, 10000); s.registerJmx(); s.start(); LOG.info("started sampling"); JdbcSemaphore semaphore = new JdbcSemaphore(ds, semaphoreName, 3); for (int i = 0; i < 50; i++) { semaphore.acquire(1, 1L, TimeUnit.SECONDS); Thread.sleep((long) (Math.random() * 10) + 10); LOG.info("beat"); Thread.sleep((long) (Math.random() * 10) + 10); semaphore.release(); } semaphore.close(); File dumpToFile = s.dumpToFile(); LOG.info("stack samples dumped to {}", dumpToFile); s.stop(); System.exit(0); }
@Test(timeout = 60000) public void testLifoExecSQ() throws InterruptedException, IOException { LifoThreadPoolExecutorSQP executor = new LifoThreadPoolExecutorSQP("test", 2, 8, 20, 0); File destFolder = new File(org.spf4j.base.Runtime.TMP_FOLDER); Sampler s = Sampler.getSampler(20, 10000, destFolder, "lifeTest1"); s.start(); org.spf4j.base.Runtime.gc(5000); Thread.sleep(100); long time = CpuUsageSampler.getProcessCpuTimeNanos(); Thread.sleep(3000); long cpuTime = CpuUsageSampler.getProcessCpuTimeNanos() - time; File dumpToFile = s.dumpToFile(); Assert.assertEquals(destFolder.getCanonicalFile(), dumpToFile.getParentFile().getCanonicalFile()); LOG.info("Cpu profile saved to {}", dumpToFile); LOG.debug("CPU time = {} ns", cpuTime); s.stop(); Assert.assertTrue("CPU Time = " + cpuTime, cpuTime < 1500000000); // 6069497000 with bug 53945000/8035000/6000000 without bug without profiler, 119628000 with profiler executor.shutdown(); executor.awaitTermination(1, TimeUnit.SECONDS); }
@Override @SuppressFBWarnings("ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD") public void beforeIteration(final BenchmarkParams benchmarkParams, final IterationParams iterationParams) { benchmarkName = benchmarkParams.id(); SAMPLER.start(); }
@PreDestroy public void dispose() throws InterruptedException { stop(); Registry.unregister(this); }
@JmxExport(description = "save stack samples to file") @Nullable public File dumpToFile() throws IOException { return dumpToFile((String) null); }
@Test public void testDumpDefault() throws InterruptedException, IOException { Sampler sampler = new Sampler(1); sampleTest(sampler, "stackSample"); }