use of com.oracle.truffle.tools.profiler.CPUSamplerData in project graal by oracle.
the class ProfilerCLITest method testSamplerJson.
@Test
@SuppressWarnings("deprecation")
public void testSamplerJson() {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final ByteArrayOutputStream err = new ByteArrayOutputStream();
Context context = Context.newBuilder().in(System.in).out(out).err(err).option("cpusampler", "true").option("cpusampler.Output", "json").build();
Source defaultSourceForSampling = makeSource("ROOT(" + "DEFINE(foo,ROOT(SLEEP(1)))," + "DEFINE(bar,ROOT(BLOCK(STATEMENT,LOOP(10, CALL(foo)))))," + "DEFINE(baz,ROOT(BLOCK(STATEMENT,LOOP(10, CALL(bar)))))," + "CALL(baz),CALL(bar)" + ")");
for (int i = 0; i < 10; i++) {
context.eval(defaultSourceForSampling);
}
CPUSampler sampler = CPUSampler.find(context.getEngine());
Map<Thread, Collection<ProfilerNode<CPUSampler.Payload>>> threadToNodesMap;
final long period;
final long sampleCount;
final boolean gatherSelfHitTimes;
synchronized (sampler) {
Map<TruffleContext, CPUSamplerData> data = sampler.getData();
CPUSamplerData samplerData = data.values().iterator().next();
threadToNodesMap = samplerData.getThreadData();
period = sampler.getPeriod();
sampleCount = samplerData.getSamples();
gatherSelfHitTimes = sampler.isGatherSelfHitTimes();
context.close();
}
JSONArray contexts = (JSONArray) new JSONObject(out.toString()).get("contexts");
JSONObject firstContext = (JSONObject) contexts.get(0);
Assert.assertEquals("Period wrong in json", period, Long.valueOf((Integer) firstContext.get("period")).longValue());
Assert.assertEquals("Sample count not correct in json", sampleCount, Long.valueOf((Integer) firstContext.get("sample_count")).longValue());
Assert.assertEquals("Gather self times not correct in json", gatherSelfHitTimes, firstContext.get("gathered_hit_times"));
JSONArray profile = (JSONArray) firstContext.get("profile");
// We are single threaded in this test
JSONObject perThreadProfile = (JSONObject) profile.get(0);
JSONArray samples = (JSONArray) perThreadProfile.get("samples");
Collection<ProfilerNode<CPUSampler.Payload>> profilerNodes = threadToNodesMap.get(Thread.currentThread());
deepCompare(samples, profilerNodes);
}
use of com.oracle.truffle.tools.profiler.CPUSamplerData in project graal by oracle.
the class CPUSamplerCLI method handleOutput.
static void handleOutput(TruffleInstrument.Env env, CPUSampler sampler) {
PrintStream out = chooseOutputStream(env);
Map<TruffleContext, CPUSamplerData> data = sampler.getData();
OptionValues options = env.getOptions();
switch(chooseOutput(options)) {
case HISTOGRAM:
printWarnings(sampler, out);
printSamplingHistogram(out, options, data);
break;
case CALLTREE:
printWarnings(sampler, out);
printSamplingCallTree(out, options, data);
break;
case JSON:
printSamplingJson(out, options, data);
break;
case FLAMEGRAPH:
SVGSamplerOutput.printSamplingFlameGraph(out, data);
}
}
use of com.oracle.truffle.tools.profiler.CPUSamplerData in project graal by oracle.
the class CPUSamplerTest method testTiers.
@Test
public void testTiers() {
Assume.assumeFalse(Truffle.getRuntime().getClass().toString().contains("Default"));
Context.Builder builder = Context.newBuilder().option("engine.FirstTierCompilationThreshold", Integer.toString(FIRST_TIER_THRESHOLD)).option("engine.LastTierCompilationThreshold", Integer.toString(2 * FIRST_TIER_THRESHOLD)).option("engine.BackgroundCompilation", "false");
Map<TruffleContext, CPUSamplerData> data;
try (Context c = builder.build()) {
CPUSampler cpuSampler = CPUSampler.find(c.getEngine());
cpuSampler.setCollecting(true);
for (int i = 0; i < 3 * FIRST_TIER_THRESHOLD; i++) {
c.eval(defaultSourceForSampling);
}
data = cpuSampler.getData();
}
CPUSamplerData samplerData = data.values().iterator().next();
Collection<ProfilerNode<CPUSampler.Payload>> profilerNodes = samplerData.getThreadData().values().iterator().next();
ProfilerNode<CPUSampler.Payload> root = profilerNodes.iterator().next();
for (ProfilerNode<CPUSampler.Payload> child : root.getChildren()) {
CPUSampler.Payload payload = child.getPayload();
int numberOfTiers = payload.getNumberOfTiers();
Assert.assertEquals(3, numberOfTiers);
for (int i = 0; i < numberOfTiers; i++) {
Assert.assertTrue(payload.getTierTotalCount(i) >= 0);
Assert.assertTrue(payload.getTierSelfCount(i) >= 0);
}
}
}
use of com.oracle.truffle.tools.profiler.CPUSamplerData in project graal by oracle.
the class CPUSamplerTest method testCollectingAndHasData.
@Test
public void testCollectingAndHasData() {
sampler.setCollecting(true);
Map<TruffleContext, CPUSamplerData> before = sampler.getData();
Assert.assertEquals(0, before.values().iterator().next().getSamples());
Assert.assertTrue(sampler.isCollecting());
Assert.assertFalse(sampler.hasData());
for (int i = 0; i < executionCount; i++) {
eval(defaultSourceForSampling);
}
Map<TruffleContext, CPUSamplerData> after = sampler.getData();
Assert.assertNotEquals(0, after.values().iterator().next().getSamples());
Assert.assertTrue(sampler.isCollecting());
Assert.assertTrue(sampler.hasData());
sampler.setCollecting(false);
Assert.assertFalse(sampler.isCollecting());
Assert.assertTrue(sampler.hasData());
sampler.clearData();
Map<TruffleContext, CPUSamplerData> cleared = sampler.getData();
Assert.assertFalse(sampler.isCollecting());
Assert.assertEquals(0, cleared.values().iterator().next().getSamples());
Assert.assertFalse(sampler.hasData());
}
Aggregations