Search in sources :

Example 1 with BasicThreadInfo

use of com.newrelic.agent.threads.BasicThreadInfo in project newrelic-java-agent by newrelic.

the class TransactionProfile method transactionFinished.

public void transactionFinished(TransactionData transactionData) {
    final List<MetricNameTime> cpuTimes = new ArrayList<>();
    for (TransactionActivity activity : transactionData.getTransactionActivities()) {
        ThreadInfo threadInfo = threadMXBean.getThreadInfo(activity.getThreadId(), 0);
        if (null != threadInfo) {
            final List<List<StackTraceElement>> backtraces = new ArrayList<>();
            Map<Tracer, Collection<Tracer>> tracerTree = buildChildren(activity.getTracers(), backtraces);
            String threadName = threadNameNormalizer.getNormalizedThreadName(new BasicThreadInfo(threadInfo));
            threadActivityProfiles.get(threadName).add(activity, tracerTree);
            if (!backtraces.isEmpty()) {
                ProfileTree tree = threadProfiles.get(threadName);
                for (List<StackTraceElement> stack : backtraces) {
                    stack = DiscoveryProfile.getScrubbedStackTrace(stack);
                    Collections.reverse(stack);
                    tree.addStackTrace(stack, true);
                }
            }
            long cpuTime = activity.getTotalCpuTime();
            if (cpuTime > 0) {
                MetricName name = MetricName.create(transactionData.getBlameMetricName(), threadName);
                cpuTimes.add(new MetricNameTime(name, cpuTime));
            }
        }
    }
    if (!cpuTimes.isEmpty()) {
        ServiceFactory.getStatsService().doStatsWork(new StatsWork() {

            @Override
            public void doWork(StatsEngine statsEngine) {
                for (MetricNameTime time : cpuTimes) {
                    statsEngine.getResponseTimeStats(time.name).recordResponseTime(time.cpuTime, TimeUnit.NANOSECONDS);
                }
            }

            @Override
            public String getAppName() {
                return null;
            }
        }, transactionData.getBlameMetricName());
    }
}
Also used : DefaultTracer(com.newrelic.agent.tracers.DefaultTracer) Tracer(com.newrelic.agent.tracers.Tracer) ArrayList(java.util.ArrayList) TransactionActivity(com.newrelic.agent.TransactionActivity) StatsEngine(com.newrelic.agent.stats.StatsEngine) MetricName(com.newrelic.agent.metric.MetricName) ThreadInfo(java.lang.management.ThreadInfo) BasicThreadInfo(com.newrelic.agent.threads.BasicThreadInfo) StatsWork(com.newrelic.agent.stats.StatsWork) BasicThreadInfo(com.newrelic.agent.threads.BasicThreadInfo) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with BasicThreadInfo

use of com.newrelic.agent.threads.BasicThreadInfo in project newrelic-java-agent by newrelic.

the class TransactionProfile method addStackTrace.

public void addStackTrace(List<StackTraceElement> stackTraceList) {
    String threadName = threadNameNormalizer.getNormalizedThreadName(new BasicThreadInfo(Thread.currentThread()));
    threadProfiles.getIfPresent(threadName).addStackTrace(stackTraceList, true);
}
Also used : BasicThreadInfo(com.newrelic.agent.threads.BasicThreadInfo)

Example 3 with BasicThreadInfo

use of com.newrelic.agent.threads.BasicThreadInfo in project newrelic-java-agent by newrelic.

the class DiscoveryProfile method noticeStartTracer.

public void noticeStartTracer(int signatureId) {
    String threadName = threadNameNormalizer.getNormalizedThreadName(new BasicThreadInfo(Thread.currentThread()));
    Object key = profile.getStringMap().addString(threadName);
    discoveryProfileTrees.get(key).addStackTrace(getScrubbedCurrentThreadStackTrace(), true);
}
Also used : BasicThreadInfo(com.newrelic.agent.threads.BasicThreadInfo) JSONObject(org.json.simple.JSONObject)

Example 4 with BasicThreadInfo

use of com.newrelic.agent.threads.BasicThreadInfo in project newrelic-java-agent by newrelic.

the class ProfileTest method testGetCallCount.

/*
     * @Test public void testGetCallCounts() { Profile profile = new Profile(50);
     * 
     * Assert.assertEquals(0, profile.getProfileTree(ThreadType.BasicThreadType.OTHER).getCallCounts().size());
     * 
     * StackTraceElement stackElement = new StackTraceElement("com.acme.rocket", "launch", "rocket.java", 123);
     * profile.add(new ProfileSegment(stackElement, (ProfileSegment)null), null);
     * 
     * Map<ProfiledMethod,Integer> callCounts =
     * profile.getProfileTree(ThreadType.BasicThreadType.OTHER).getCallCounts(); Integer count =
     * (Integer)callCounts.get(new ProfiledMethod(stackElement)); Assert.assertEquals(1, count.longValue()); }
     */
@Test
public void testGetCallCount() {
    ProfilerParameters parameters = new ProfilerParameters(0L, 0L, 0L, false, false, false, null, null).setProfilerFormat("v2");
    ThreadMXBean threadMXBean = mock(ThreadMXBean.class);
    when(threadMXBean.getThreadCpuTime(0)).thenReturn(10L);
    IProfile profile = new Profile(parameters, TransactionGuidFactory.generate16CharGuid(), threadNameNormalizer, threadMXBean);
    StackTraceElement methodOnce = new StackTraceElement("com.acme.rocket", "launch", "rocket.java", 123);
    StackTraceElement methodTwice = new StackTraceElement("com.acme.explosive", "detonate", "explosive.java", 123);
    StackTraceElement methodCaller = new StackTraceElement("com.wileecoyote.cartoon", "show", "cartoon.java", 123);
    StackTraceElement methodCallerToo = new StackTraceElement("com.wileecoyote.cartoon", "show", "cartoon.java", 321);
    // profile.addStackTrace(0, false,methodCaller);
    ThreadInfo threadInfo1 = getMockedThreadInfo(0, methodOnce, methodCaller);
    profile.addStackTrace(threadInfo1, true, ThreadType.BasicThreadType.OTHER);
    ThreadInfo threadInfo2 = getMockedThreadInfo(0, methodTwice);
    profile.addStackTrace(threadInfo2, true, ThreadType.BasicThreadType.OTHER);
    ThreadInfo threadInfo3 = getMockedThreadInfo(0, methodTwice, methodCaller);
    profile.addStackTrace(threadInfo3, true, ThreadType.BasicThreadType.OTHER);
    String normalizedThread1Name = threadNameNormalizer.getNormalizedThreadName(new BasicThreadInfo(threadInfo1));
    ProfileSegment methodCallerSegment = profile.getProfileTree(normalizedThread1Name).getSegmentForMethod(methodCaller);
    Assert.assertEquals(2, methodCallerSegment.getCallCount(methodCallerSegment.getMethod()));
    ProfileSegment methodCallerTooSegment = profile.getProfileTree(normalizedThread1Name).getSegmentForMethod(methodCallerToo);
    Assert.assertNull(methodCallerTooSegment);
    Map<ProfiledMethod, ProfileSegment> methodCallerChilden = methodCallerSegment.getChildMap();
    Assert.assertEquals(2, methodCallerChilden.size());
    ProfiledMethod methodOnceProfiledMethod = profile.getProfiledMethodFactory().getProfiledMethod(methodOnce);
    Assert.assertEquals(1, methodCallerChilden.get(methodOnceProfiledMethod).getCallCount(methodOnceProfiledMethod));
    ProfiledMethod methodTwiceProfiledMethod = profile.getProfiledMethodFactory().getProfiledMethod(methodTwice);
    Assert.assertEquals(1, methodCallerChilden.get(methodTwiceProfiledMethod).getCallCount(methodTwiceProfiledMethod));
}
Also used : ThreadMXBean(java.lang.management.ThreadMXBean) ThreadInfo(java.lang.management.ThreadInfo) BasicThreadInfo(com.newrelic.agent.threads.BasicThreadInfo) BasicThreadInfo(com.newrelic.agent.threads.BasicThreadInfo) ProfilerParameters(com.newrelic.agent.profile.ProfilerParameters) Test(org.junit.Test)

Example 5 with BasicThreadInfo

use of com.newrelic.agent.threads.BasicThreadInfo in project newrelic-java-agent by newrelic.

the class ProfileTest method verifyProfile.

private void verifyProfile(IProfile profile, boolean simpleCompression) throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    OutputStreamWriter writer = new OutputStreamWriter(out);
    if (simpleCompression) {
        // If using simple compression the full payload will be deflated
        writer = new OutputStreamWriter(new DeflaterOutputStream(out, new Deflater(Deflater.DEFAULT_COMPRESSION)));
    }
    profile.writeJSONString(writer);
    writer.close();
    byte[] profileOutput = out.toByteArray();
    // Verify that data is less than the collector max of 1MB
    Assert.assertTrue(profileOutput.length < 1000000);
    JSONParser parser = new JSONParser();
    Object parse;
    // After verifying data size, inflate the data to parse the json
    if (simpleCompression) {
        InflaterInputStream inStream = new InflaterInputStream(new ByteArrayInputStream(profileOutput));
        String jsonOutput = CharStreams.toString(new InputStreamReader(inStream, Charsets.UTF_8));
        parse = parser.parse(jsonOutput);
    } else {
        parse = parser.parse(new String(profileOutput, Charsets.UTF_8));
    }
    Assert.assertTrue(parse instanceof List);
    List data = (List) parse;
    Assert.assertEquals(profile.getProfileId(), data.get(0));
    Assert.assertEquals(profile.getStartTimeMillis(), data.get(1));
    Assert.assertEquals(profile.getEndTimeMillis(), data.get(2));
    parser = new JSONParser();
    Object parsedData;
    if (simpleCompression) {
        parsedData = data.get(4);
    } else {
        String rawData = (String) data.get(4);
        byte[] compressedData = Base64.decodeBase64(rawData);
        InflaterInputStream inStream = new InflaterInputStream(new ByteArrayInputStream(compressedData));
        rawData = CharStreams.toString(new InputStreamReader(inStream, Charsets.UTF_8));
        parsedData = parser.parse(rawData);
    }
    Assert.assertTrue(parse instanceof List);
    Map trees = (Map) ((Map) parsedData).get("threads");
    Assert.assertEquals(1, trees.size());
    List segments = (List) trees.values().iterator().next();
    String normalizedThreadName = threadNameNormalizer.getNormalizedThreadName(new BasicThreadInfo(getMockedThreadInfo(0)));
    Assert.assertEquals(profile.getProfileTree(normalizedThreadName).getRootCount() + 1, segments.size());
}
Also used : InputStreamReader(java.io.InputStreamReader) InflaterInputStream(java.util.zip.InflaterInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Deflater(java.util.zip.Deflater) ByteArrayInputStream(java.io.ByteArrayInputStream) BasicThreadInfo(com.newrelic.agent.threads.BasicThreadInfo) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) JSONParser(org.json.simple.parser.JSONParser) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

BasicThreadInfo (com.newrelic.agent.threads.BasicThreadInfo)13 ThreadInfo (java.lang.management.ThreadInfo)8 Test (org.junit.Test)7 ArrayList (java.util.ArrayList)3 List (java.util.List)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 TransactionActivity (com.newrelic.agent.TransactionActivity)1 MetricName (com.newrelic.agent.metric.MetricName)1 ProfilerParameters (com.newrelic.agent.profile.ProfilerParameters)1 StatsEngine (com.newrelic.agent.stats.StatsEngine)1 StatsWork (com.newrelic.agent.stats.StatsWork)1 DefaultTracer (com.newrelic.agent.tracers.DefaultTracer)1 IgnoreChildSocketCalls (com.newrelic.agent.tracers.IgnoreChildSocketCalls)1 Tracer (com.newrelic.agent.tracers.Tracer)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStreamWriter (java.io.OutputStreamWriter)1 ThreadMXBean (java.lang.management.ThreadMXBean)1 Collection (java.util.Collection)1