Search in sources :

Example 1 with ThreadSnapshotEvent

use of org.gridkit.jvmtool.codec.stacktrace.ThreadSnapshotEvent in project jvm-tools by aragozin.

the class ThreadDumpSource method getFilteredReader.

public EventReader<ThreadSnapshotEvent> getFilteredReader() {
    if (traceFilter == null && traceTrim == null && threadName == null && timeRange == null) {
        return getUnclassifiedReader();
    } else {
        EventReader<ThreadSnapshotEvent> reader = getUnclassifiedReader();
        if (threadName != null) {
            reader = reader.morph(new ThreadNameFilter(threadName));
        }
        if (timeRange != null) {
            String[] lh = timeRange.split("[-]");
            if (lh.length != 2) {
                host.fail("Invalid time range '" + timeRange + "'", "Valid format yyyy.MM.dd_HH:mm:ss-yyyy.MM.dd_HH:mm:ss hours and higher parts can be ommited");
            }
            TimeRangeChecker checker = new TimeRangeChecker(lh[0], lh[1], timeZone);
            reader = reader.morph(new TimeFilter(checker));
        }
        try {
            CachingFilterFactory factory = new CachingFilterFactory();
            if (traceFilter != null) {
                ThreadSnapshotFilter ts = TraceFilterPredicateParser.parseFilter(traceFilter, factory);
                reader = reader.morph(new ThreadEventFilter(ts));
            }
            if (traceTrim != null) {
                final PositionalStackMatcher mt = TraceFilterPredicateParser.parsePositionMatcher(traceTrim, factory);
                reader = reader.morph(new TrimProxy() {

                    @Override
                    public ThreadSnapshotEvent morph(ThreadSnapshotEvent event) {
                        int n = mt.matchNext(event, 0);
                        if (n >= 0) {
                            trimPoint = n;
                            return super.morph(event);
                        } else {
                            return null;
                        }
                    }
                });
            }
            return reader;
        } catch (ParserException e) {
            throw host.fail("Failed to parse trace filter - " + e.getMessage() + " at " + e.getOffset() + " [" + e.getParseText() + "]");
        }
    }
}
Also used : ParserException(org.gridkit.jvmtool.stacktrace.analytics.ParserException) TimeRangeChecker(org.gridkit.jvmtool.stacktrace.analytics.TimeRangeChecker) ThreadSnapshotFilter(org.gridkit.jvmtool.stacktrace.analytics.ThreadSnapshotFilter) ThreadSnapshotEvent(org.gridkit.jvmtool.codec.stacktrace.ThreadSnapshotEvent) ThreadEventFilter(org.gridkit.jvmtool.stacktrace.analytics.ThreadEventFilter) PositionalStackMatcher(org.gridkit.jvmtool.stacktrace.analytics.PositionalStackMatcher) CachingFilterFactory(org.gridkit.jvmtool.stacktrace.analytics.CachingFilterFactory)

Example 2 with ThreadSnapshotEvent

use of org.gridkit.jvmtool.codec.stacktrace.ThreadSnapshotEvent 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 3 with ThreadSnapshotEvent

use of org.gridkit.jvmtool.codec.stacktrace.ThreadSnapshotEvent 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 4 with ThreadSnapshotEvent

use of org.gridkit.jvmtool.codec.stacktrace.ThreadSnapshotEvent in project jvm-tools by aragozin.

the class EventDecoratorTest method verify_thread_snapshot_event.

@Test
public void verify_thread_snapshot_event() {
    ThreadSnapshotEventPojo a = new ThreadSnapshotEventPojo();
    a.timestamp(10000);
    a.tags().put("A", "1");
    a.tags().put("B", "2");
    a.tags().put("B", "3");
    a.tags().put("C", "4");
    a.counters().set("X", 10);
    a.counters().set("zzz", Long.MAX_VALUE);
    a.threadName("Test");
    a.threadState(State.NEW);
    ThreadSnapshotEventPojo b = new ThreadSnapshotEventPojo();
    b.loadFrom(a);
    assertThat(b).is(eventEquals(a));
    EventDecorator deco = new EventDecorator();
    ThreadSnapshotEvent c = deco.wrap(b);
    assertThat(c).is(eventEquals(a));
    deco.timestamp(77777);
    deco.tags().put("F", "10");
    deco.counters().set("__", 25);
    assertThat(c.timestamp()).isEqualTo(77777);
    assertThat(c.tags()).containsOnly("A", "B", "C", "F");
    assertThat(c.counters()).containsOnly("X", "zzz", "__");
    assertThat(c.threadName()).isEqualTo("Test");
    assertThat(c.threadState()).isEqualTo(State.NEW);
}
Also used : ThreadSnapshotEvent(org.gridkit.jvmtool.codec.stacktrace.ThreadSnapshotEvent) EventDecorator(org.gridkit.jvmtool.event.EventDecorator) ThreadSnapshotEventPojo(org.gridkit.jvmtool.codec.stacktrace.ThreadSnapshotEventPojo) Test(org.junit.Test)

Aggregations

ThreadSnapshotEvent (org.gridkit.jvmtool.codec.stacktrace.ThreadSnapshotEvent)4 FileInputStream (java.io.FileInputStream)2 Event (org.gridkit.jvmtool.event.Event)2 ParserException (org.gridkit.jvmtool.stacktrace.analytics.ParserException)2 Test (org.junit.Test)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 ThreadMXBean (java.lang.management.ThreadMXBean)1 MBeanServerConnection (javax.management.MBeanServerConnection)1 ThreadSnapshotEventPojo (org.gridkit.jvmtool.codec.stacktrace.ThreadSnapshotEventPojo)1 ChainedEventReader (org.gridkit.jvmtool.event.ChainedEventReader)1 ErrorHandler (org.gridkit.jvmtool.event.ErrorHandler)1 EventDecorator (org.gridkit.jvmtool.event.EventDecorator)1 ShieldedEventReader (org.gridkit.jvmtool.event.ShieldedEventReader)1 SimpleErrorEvent (org.gridkit.jvmtool.event.SimpleErrorEvent)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