Search in sources :

Example 1 with GcEventSubscriber

use of org.gridkit.jvmtool.gcmon.GcEventSubscriber in project jvm-tools by aragozin.

the class GcEventSubscriberTest method capture_gc_sample.

@Test
@Ignore("GC notifications are not supported on Java 6")
public void capture_gc_sample() throws InterruptedException {
    GcEventSubscriber poller = new GcEventSubscriber(ManagementFactory.getPlatformMBeanServer(), encoder);
    Assert.assertTrue(poller.subscribe());
    events.clear();
    Assert.assertTrue("No events", events.isEmpty());
    System.gc();
    Thread.sleep(100);
    Assert.assertFalse("One or more events excepted", events.isEmpty());
}
Also used : GcEventSubscriber(org.gridkit.jvmtool.gcmon.GcEventSubscriber) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with GcEventSubscriber

use of org.gridkit.jvmtool.gcmon.GcEventSubscriber in project jvm-tools by aragozin.

the class CaptureCheck method capture.

@Test
public void capture() throws FileNotFoundException, IOException {
    File dump = new File(taget);
    if (dump.getParentFile() != null) {
        dump.getParentFile().mkdirs();
    }
    UniversalEventWriter writer = ThreadEventCodec.createEventWriter(new FileOutputStream(dump));
    MyEventWriter twriter = TypedEventWriterProxy.decorate(writer).pass(ThreadSnapshotEvent.class).pass(GarbageCollectionEvent.class).facade(MyEventWriter.class);
    MBeanServerConnection conn = AttachManager.getJmxConnection(PID);
    GcEventSubscriber subscriber = new GcEventSubscriber(conn, new SimpleGcEventEncoder(twriter));
    if (!subscriber.subscribe()) {
        // polling fallback
        subscriber.schedule(500);
    }
    ThreadDumpSampler tdumper = new ThreadDumpSampler();
    ThreadMXBean threadMXBean = ThreadMXBeanEx.BeanHelper.connectThreadMXBean(conn);
    if (threadMXBean.isThreadContentionMonitoringSupported()) {
        threadMXBean.setThreadContentionMonitoringEnabled(true);
    }
    tdumper.connect(threadMXBean);
    long deadline = System.currentTimeMillis() + captureTime;
    ThreadEventAdapter threadWriter = new ThreadEventAdapter(twriter);
    while (System.currentTimeMillis() < deadline) {
        tdumper.collect(threadWriter);
    }
    twriter.close();
    System.out.println("Dump complete [" + dump.getPath() + "] " + dump.length() + " bytes");
    int tc = 0;
    int ntc = 0;
    EventReader<Event> reader = ThreadEventCodec.createEventReader(new FileInputStream(dump));
    for (Event e : reader) {
        if (e instanceof ThreadSnapshotEvent) {
            ++tc;
        } else {
            ++ntc;
        }
    }
    System.out.println("Thread events: " + tc + " Non thread events: " + ntc);
}
Also used : SimpleGcEventEncoder(org.gridkit.jvmtool.gcmon.SimpleGcEventEncoder) ThreadMXBean(java.lang.management.ThreadMXBean) UniversalEventWriter(org.gridkit.jvmtool.event.UniversalEventWriter) FileInputStream(java.io.FileInputStream) GcEventSubscriber(org.gridkit.jvmtool.gcmon.GcEventSubscriber) ThreadSnapshotEvent(org.gridkit.jvmtool.codec.stacktrace.ThreadSnapshotEvent) FileOutputStream(java.io.FileOutputStream) GarbageCollectionEvent(org.gridkit.jvmtool.gcmon.GarbageCollectionEvent) ThreadSnapshotEvent(org.gridkit.jvmtool.codec.stacktrace.ThreadSnapshotEvent) Event(org.gridkit.jvmtool.event.Event) GarbageCollectionEvent(org.gridkit.jvmtool.gcmon.GarbageCollectionEvent) File(java.io.File) MBeanServerConnection(javax.management.MBeanServerConnection) ThreadDumpSampler(org.gridkit.jvmtool.stacktrace.ThreadDumpSampler) Test(org.junit.Test)

Aggregations

GcEventSubscriber (org.gridkit.jvmtool.gcmon.GcEventSubscriber)2 Test (org.junit.Test)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 ThreadMXBean (java.lang.management.ThreadMXBean)1 MBeanServerConnection (javax.management.MBeanServerConnection)1 ThreadSnapshotEvent (org.gridkit.jvmtool.codec.stacktrace.ThreadSnapshotEvent)1 Event (org.gridkit.jvmtool.event.Event)1 UniversalEventWriter (org.gridkit.jvmtool.event.UniversalEventWriter)1 GarbageCollectionEvent (org.gridkit.jvmtool.gcmon.GarbageCollectionEvent)1 SimpleGcEventEncoder (org.gridkit.jvmtool.gcmon.SimpleGcEventEncoder)1 ThreadDumpSampler (org.gridkit.jvmtool.stacktrace.ThreadDumpSampler)1 Ignore (org.junit.Ignore)1