Search in sources :

Example 6 with CPUSamplerData

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);
}
Also used : TruffleContext(com.oracle.truffle.api.TruffleContext) Context(org.graalvm.polyglot.Context) TruffleContext(com.oracle.truffle.api.TruffleContext) JSONArray(com.oracle.truffle.tools.utils.json.JSONArray) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Source(org.graalvm.polyglot.Source) CPUSamplerData(com.oracle.truffle.tools.profiler.CPUSamplerData) ProfilerNode(com.oracle.truffle.tools.profiler.ProfilerNode) JSONObject(com.oracle.truffle.tools.utils.json.JSONObject) Collection(java.util.Collection) CPUSampler(com.oracle.truffle.tools.profiler.CPUSampler) Test(org.junit.Test)

Example 7 with CPUSamplerData

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);
    }
}
Also used : PrintStream(java.io.PrintStream) CPUSamplerData(com.oracle.truffle.tools.profiler.CPUSamplerData) OptionValues(org.graalvm.options.OptionValues) TruffleContext(com.oracle.truffle.api.TruffleContext)

Example 8 with CPUSamplerData

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);
        }
    }
}
Also used : TruffleContext(com.oracle.truffle.api.TruffleContext) Context(org.graalvm.polyglot.Context) CPUSamplerData(com.oracle.truffle.tools.profiler.CPUSamplerData) ProfilerNode(com.oracle.truffle.tools.profiler.ProfilerNode) TruffleContext(com.oracle.truffle.api.TruffleContext) Payload(com.oracle.truffle.tools.profiler.CPUSampler.Payload) Payload(com.oracle.truffle.tools.profiler.CPUSampler.Payload) TruffleSafepoint(com.oracle.truffle.api.TruffleSafepoint) CPUSampler(com.oracle.truffle.tools.profiler.CPUSampler) Test(org.junit.Test)

Example 9 with CPUSamplerData

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());
}
Also used : CPUSamplerData(com.oracle.truffle.tools.profiler.CPUSamplerData) TruffleContext(com.oracle.truffle.api.TruffleContext) TruffleSafepoint(com.oracle.truffle.api.TruffleSafepoint) Test(org.junit.Test)

Aggregations

CPUSamplerData (com.oracle.truffle.tools.profiler.CPUSamplerData)9 TruffleContext (com.oracle.truffle.api.TruffleContext)7 Test (org.junit.Test)5 ProfilerNode (com.oracle.truffle.tools.profiler.ProfilerNode)3 TruffleSafepoint (com.oracle.truffle.api.TruffleSafepoint)2 RootNode (com.oracle.truffle.api.nodes.RootNode)2 ProxyLanguage (com.oracle.truffle.api.test.polyglot.ProxyLanguage)2 CPUSampler (com.oracle.truffle.tools.profiler.CPUSampler)2 Payload (com.oracle.truffle.tools.profiler.CPUSampler.Payload)2 JSONArray (com.oracle.truffle.tools.utils.json.JSONArray)2 JSONObject (com.oracle.truffle.tools.utils.json.JSONObject)2 Collection (java.util.Collection)2 Context (org.graalvm.polyglot.Context)2 Params (com.oracle.truffle.tools.chromeinspector.commands.Params)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 PrintStream (java.io.PrintStream)1 ArrayList (java.util.ArrayList)1 OptionValues (org.graalvm.options.OptionValues)1 Source (org.graalvm.polyglot.Source)1