Search in sources :

Example 1 with Event

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

the class ThreadEventCodec method createEventReader.

public static EventReader<Event> createEventReader(byte[] magic, InputStream is) throws IOException {
    if (Arrays.equals(magic, StackTraceCodec.MAGIC)) {
        StackTraceReader lreader = new StackTraceReaderV1(is);
        EventReader<Event> reader = new LegacyThreadEventReader(lreader);
        return reader;
    }
    if (Arrays.equals(magic, StackTraceCodec.MAGIC2)) {
        StackTraceReader lreader = new StackTraceReaderV2(is);
        EventReader<Event> reader = new LegacyThreadEventReader(lreader);
        return reader;
    } else // MAGIC3 is not used
    if (Arrays.equals(magic, StackTraceCodec.MAGIC4)) {
        EventReader<Event> reader = new StackTraceEventReaderV4(is).morph(new ThreadSnapshotExpander());
        return reader;
    } else {
        throw new IOException("Unknown magic '" + new String(magic) + "'");
    }
}
Also used : ThreadSnapshotExpander(org.gridkit.jvmtool.codec.stacktrace.ThreadSnapshotExpander) EventReader(org.gridkit.jvmtool.event.EventReader) Event(org.gridkit.jvmtool.event.Event) IOException(java.io.IOException)

Example 2 with Event

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

the class ThreadEventReaderTest method read_dump_v2.

@Test
public void read_dump_v2() throws FileNotFoundException, IOException {
    EventReader<Event> reader = ThreadEventCodec.createEventReader(new FileInputStream("src/test/resources/dump_v2.std"));
    int n = 0;
    for (Event e : reader) {
        if (e instanceof ErrorEvent) {
            fail("Error", ((ErrorEvent) e).exception());
        }
        //            System.out.println(((MultiCounterEvent)e).counters());
        ++n;
    }
    System.out.println("Read " + n + " traces from file");
}
Also used : ErrorEvent(org.gridkit.jvmtool.event.ErrorEvent) Event(org.gridkit.jvmtool.event.Event) ErrorEvent(org.gridkit.jvmtool.event.ErrorEvent) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 3 with Event

use of org.gridkit.jvmtool.event.Event 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 4 with Event

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

the class ThreadDumpSource method getUnclassifiedReader.

public EventReader<ThreadSnapshotEvent> getUnclassifiedReader() {
    if (files == null) {
        host.fail("No input files provided, used -f option");
    }
    final Iterator<String> it = files.iterator();
    ChainedEventReader<Event> reader = new ChainedEventReader<Event>() {

        @Override
        protected EventReader<Event> produceNext() {
            return it.hasNext() ? open(it.next()) : null;
        }

        private EventReader<Event> open(String next) {
            try {
                return ThreadEventCodec.createEventReader(new FileInputStream(next));
            } catch (IOException e) {
                return new SingleEventReader<Event>(new SimpleErrorEvent(e));
            }
        }
    };
    ShieldedEventReader<ThreadSnapshotEvent> shielderReader = new ShieldedEventReader<ThreadSnapshotEvent>(reader, ThreadSnapshotEvent.class, new ErrorHandler() {

        @Override
        public void onException(Exception e) {
            System.err.println("Stream reader error: " + e);
        }
    });
    return shielderReader;
}
Also used : ErrorHandler(org.gridkit.jvmtool.event.ErrorHandler) SimpleErrorEvent(org.gridkit.jvmtool.event.SimpleErrorEvent) IOException(java.io.IOException) ChainedEventReader(org.gridkit.jvmtool.event.ChainedEventReader) FileInputStream(java.io.FileInputStream) ParserException(org.gridkit.jvmtool.stacktrace.analytics.ParserException) IOException(java.io.IOException) ThreadSnapshotEvent(org.gridkit.jvmtool.codec.stacktrace.ThreadSnapshotEvent) ShieldedEventReader(org.gridkit.jvmtool.event.ShieldedEventReader) SimpleErrorEvent(org.gridkit.jvmtool.event.SimpleErrorEvent) ThreadSnapshotEvent(org.gridkit.jvmtool.codec.stacktrace.ThreadSnapshotEvent) Event(org.gridkit.jvmtool.event.Event)

Example 5 with Event

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

Event (org.gridkit.jvmtool.event.Event)10 FileInputStream (java.io.FileInputStream)7 ErrorEvent (org.gridkit.jvmtool.event.ErrorEvent)7 Test (org.junit.Test)6 UniversalEventWriter (org.gridkit.jvmtool.event.UniversalEventWriter)5 File (java.io.File)4 FileOutputStream (java.io.FileOutputStream)4 IOException (java.io.IOException)2 ThreadSnapshotEvent (org.gridkit.jvmtool.codec.stacktrace.ThreadSnapshotEvent)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ThreadMXBean (java.lang.management.ThreadMXBean)1 MBeanServerConnection (javax.management.MBeanServerConnection)1 ThreadSnapshotExpander (org.gridkit.jvmtool.codec.stacktrace.ThreadSnapshotExpander)1 ChainedEventReader (org.gridkit.jvmtool.event.ChainedEventReader)1 CommonEvent (org.gridkit.jvmtool.event.CommonEvent)1 ErrorHandler (org.gridkit.jvmtool.event.ErrorHandler)1 EventReader (org.gridkit.jvmtool.event.EventReader)1 ShieldedEventReader (org.gridkit.jvmtool.event.ShieldedEventReader)1 SimpleErrorEvent (org.gridkit.jvmtool.event.SimpleErrorEvent)1