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());
}
}
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);
}
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);
}
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));
}
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());
}
Aggregations