@Override public void testFinished(final Description description) throws IOException { File dump = sampler.dumpToFile(new File(destinationFolder, description.getDisplayName() + ".ssdump2")); if (dump != null) { LOG.info("Profile saved to {}", dump); lastWrittenFile = dump; } }
@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); } }
@JmxExport(description = "save stack samples to file") @Nullable public File dumpToFile() throws IOException { return dumpToFile((String) null); }
@Override public void doRun() throws InterruptedException, IOException { sampler.stop(); sampler.dumpToFile(); sampler.dispose(); }
/** * Dumps the sampled stacks to file. the collected samples are reset * * @param id - id will be added to file name returns the name of the file. * @return - the file name where the data was persisted or null if there was no data to persist. * @throws IOException - io issues while persisting data. */ @JmxExport(value = "dumpToSpecificFile", description = "save stack samples to file") @Nullable @SuppressFBWarnings("PATH_TRAVERSAL_IN") // not possible the provided ID is validated for path separators. public synchronized File dumpToFile( @JmxExport(value = "fileID", description = "the ID that will be part of the file name") @Nullable final String id) throws IOException { String fileName = filePrefix + CharSequences.validatedFileName(((id == null) ? "" : '_' + id) + '_' + DateTimeFormats.COMPACT_TS_FORMAT.format( Timing.getCurrentTiming().fromNanoTimeToInstant(lastDumpTimeNanos)) + '_' + DateTimeFormats.COMPACT_TS_FORMAT.format(Instant.now())); File file = new File(dumpFolder, fileName); return dumpToFile(file); }
if (nanosSinceLastDump >= dumpTimeNanos) { dumpCounterNanos = 0; File dumpFile = dumpToFile(); lastDumpTimeNanos = TimeSource.nanoTime(); LOG.info("Stack samples written to {}", dumpFile);
@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(); }
@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); }
@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 testLifoExecSQ2() throws InterruptedException, IOException, ExecutionException { 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); List<Runnable> running = executor.shutdownNow(); Assert.assertTrue(running.isEmpty()); 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); // 3260523000 with bug 148672000 without bug with profiler executor.shutdown(); executor.awaitTermination(1, TimeUnit.SECONDS); }
@Test(timeout = 60000) public void testLifoExecSQ() throws InterruptedException, IOException, ExecutionException { 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); ArrayBlockingQueue<Thread> q = new ArrayBlockingQueue<>(1); Future<Object> f = executor.submit(() -> { q.put(Thread.currentThread()); return null; }); q.take().interrupt(); Assert.assertNull(f.get()); 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); // 3260523000 with bug 148672000 without bug with profiler executor.shutdown(); executor.awaitTermination(1, TimeUnit.SECONDS); }
@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(); }
semaphore.close(); s.stop(); LOG.debug("dumped samples to {}", s.dumpToFile()); } finally { JdbcHeartBeat.stopHeartBeats();