Search in sources :

Example 1 with UniversalEventWriter

use of org.gridkit.jvmtool.event.UniversalEventWriter in project jvm-tools by aragozin.

the class ThreadEventReaderTest method read_dump_v1_rewrite_and_compare.

@Test
public void read_dump_v1_rewrite_and_compare() throws FileNotFoundException, IOException {
    String sourceFile = "src/test/resources/dump_v1.std";
    File file = new File("target/tmp/" + testName.getMethodName() + "-" + System.currentTimeMillis() + ".std");
    file.getParentFile().mkdirs();
    file.delete();
    FileOutputStream fow = new FileOutputStream(file);
    UniversalEventWriter writer = ThreadEventCodec.createEventWriter(fow);
    EventReader<Event> reader = ThreadEventCodec.createEventReader(new FileInputStream(sourceFile));
    copyAllTraces(reader, writer);
    writer.close();
    System.out.println("New file " + file.length() + " bytes (original " + new File(sourceFile).length() + " bytes)");
    reader = ThreadEventCodec.createEventReader(new FileInputStream(file));
    EventReader<Event> origReader = ThreadEventCodec.createEventReader(new FileInputStream("src/test/resources/dump_v1.std"));
    assertThat((Iterable<Event>) reader).is(exactlyAs(origReader));
}
Also used : FileOutputStream(java.io.FileOutputStream) Event(org.gridkit.jvmtool.event.Event) ErrorEvent(org.gridkit.jvmtool.event.ErrorEvent) UniversalEventWriter(org.gridkit.jvmtool.event.UniversalEventWriter) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 2 with UniversalEventWriter

use of org.gridkit.jvmtool.event.UniversalEventWriter 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)

Example 3 with UniversalEventWriter

use of org.gridkit.jvmtool.event.UniversalEventWriter in project jvm-tools by aragozin.

the class EventReadWriteTest method writeReadEvents.

private EventReader<Event> writeReadEvents(Event... events) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    UniversalEventWriter writer = ThreadEventCodec.createEventWriter(bos);
    for (Event e : events) {
        writer.store(e);
    }
    writer.close();
    byte[] data = bos.toByteArray();
    System.out.println("Encoded " + events.length + " events into " + data.length + " bytes");
    ByteArrayInputStream bis = new ByteArrayInputStream(data);
    EventReader<Event> reader = ThreadEventCodec.createEventReader(bis);
    return reader;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Event(org.gridkit.jvmtool.event.Event) CommonEvent(org.gridkit.jvmtool.event.CommonEvent) ErrorEvent(org.gridkit.jvmtool.event.ErrorEvent) ByteArrayOutputStream(java.io.ByteArrayOutputStream) UniversalEventWriter(org.gridkit.jvmtool.event.UniversalEventWriter)

Example 4 with UniversalEventWriter

use of org.gridkit.jvmtool.event.UniversalEventWriter in project jvm-tools by aragozin.

the class ThreadEventReaderTest method read_dump_v2_rewrite_and_compare.

@Test
public void read_dump_v2_rewrite_and_compare() throws FileNotFoundException, IOException {
    File sourceFile = new File("src/test/resources/dump_v2.std");
    File file = new File("target/tmp/" + testName.getMethodName() + "-" + System.currentTimeMillis() + ".std");
    file.getParentFile().mkdirs();
    file.delete();
    FileOutputStream fow = new FileOutputStream(file);
    UniversalEventWriter writer = ThreadEventCodec.createEventWriter(fow);
    EventReader<Event> reader = ThreadEventCodec.createEventReader(new FileInputStream(sourceFile));
    copyAllTraces(reader, writer);
    writer.close();
    System.out.println("New file " + file.length() + " bytes (original " + sourceFile.length() + " bytes)");
    reader = ThreadEventCodec.createEventReader(new FileInputStream(file));
    EventReader<Event> origReader = ThreadEventCodec.createEventReader(new FileInputStream(sourceFile));
    assertThat((Iterable<Event>) reader).is(exactlyAs(origReader));
}
Also used : FileOutputStream(java.io.FileOutputStream) Event(org.gridkit.jvmtool.event.Event) ErrorEvent(org.gridkit.jvmtool.event.ErrorEvent) UniversalEventWriter(org.gridkit.jvmtool.event.UniversalEventWriter) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 5 with UniversalEventWriter

use of org.gridkit.jvmtool.event.UniversalEventWriter in project jvm-tools by aragozin.

the class ThreadEventReaderTest method read_dump_v4_rewrite_and_compare.

@Test
public void read_dump_v4_rewrite_and_compare() throws FileNotFoundException, IOException {
    File sourceFile = new File("src/test/resources/dump_v4.std");
    File file = new File("target/tmp/" + testName.getMethodName() + "-" + System.currentTimeMillis() + ".std");
    file.getParentFile().mkdirs();
    file.delete();
    FileOutputStream fow = new FileOutputStream(file);
    UniversalEventWriter writer = ThreadEventCodec.createEventWriter(fow);
    EventReader<Event> reader = ThreadEventCodec.createEventReader(new FileInputStream(sourceFile));
    copyAllTraces(reader, writer);
    writer.close();
    System.out.println("New file " + file.length() + " bytes (original " + sourceFile.length() + " bytes)");
    reader = ThreadEventCodec.createEventReader(new FileInputStream(file));
    EventReader<Event> origReader = ThreadEventCodec.createEventReader(new FileInputStream(sourceFile));
    assertThat((Iterable<Event>) reader).is(exactlyAs(origReader));
}
Also used : FileOutputStream(java.io.FileOutputStream) Event(org.gridkit.jvmtool.event.Event) ErrorEvent(org.gridkit.jvmtool.event.ErrorEvent) UniversalEventWriter(org.gridkit.jvmtool.event.UniversalEventWriter) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Aggregations

Event (org.gridkit.jvmtool.event.Event)5 UniversalEventWriter (org.gridkit.jvmtool.event.UniversalEventWriter)5 File (java.io.File)4 FileInputStream (java.io.FileInputStream)4 FileOutputStream (java.io.FileOutputStream)4 ErrorEvent (org.gridkit.jvmtool.event.ErrorEvent)4 Test (org.junit.Test)4 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ThreadMXBean (java.lang.management.ThreadMXBean)1 MBeanServerConnection (javax.management.MBeanServerConnection)1 ThreadSnapshotEvent (org.gridkit.jvmtool.codec.stacktrace.ThreadSnapshotEvent)1 CommonEvent (org.gridkit.jvmtool.event.CommonEvent)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