Search in sources :

Example 1 with SimpleGcEventEncoder

use of org.gridkit.jvmtool.gcmon.SimpleGcEventEncoder 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

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 GcEventSubscriber (org.gridkit.jvmtool.gcmon.GcEventSubscriber)1 SimpleGcEventEncoder (org.gridkit.jvmtool.gcmon.SimpleGcEventEncoder)1 ThreadDumpSampler (org.gridkit.jvmtool.stacktrace.ThreadDumpSampler)1 Test (org.junit.Test)1