001/*
002 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.truffle.debug;
024
025import static com.oracle.graal.truffle.TruffleCompilerOptions.*;
026
027import java.util.*;
028import java.util.function.*;
029import java.util.stream.*;
030
031import com.oracle.graal.truffle.*;
032import com.oracle.truffle.api.*;
033
034public final class PrintCallTargetProfiling extends AbstractDebugCompilationListener {
035
036    public static void install(GraalTruffleRuntime runtime) {
037        if (TruffleCallTargetProfiling.getValue()) {
038            runtime.addCompilationListener(new PrintCallTargetProfiling());
039        }
040    }
041
042    @Override
043    public void notifyShutdown(GraalTruffleRuntime runtime) {
044        Map<OptimizedCallTarget, List<OptimizedCallTarget>> groupedTargets = Truffle.getRuntime().getCallTargets().stream().map(target -> (OptimizedCallTarget) target).collect(
045                        Collectors.groupingBy(target -> {
046                            if (target.getSourceCallTarget() != null) {
047                                return target.getSourceCallTarget();
048                            }
049                            return target;
050                        }));
051
052        List<OptimizedCallTarget> uniqueSortedTargets = groupedTargets.keySet().stream().sorted(
053                        (target1, target2) -> sumCalls(groupedTargets.get(target2), p -> p.getTotalCallCount()) - sumCalls(groupedTargets.get(target1), p -> p.getTotalCallCount())).collect(
054                        Collectors.toList());
055
056        int totalDirectCallCount = 0;
057        int totalInlinedCallCount = 0;
058        int totalIndirectCallCount = 0;
059        int totalTotalCallCount = 0;
060        int totalInterpretedCallCount = 0;
061        int totalInvalidationCount = 0;
062
063        runtime.log(""); // empty line
064        runtime.log(String.format(" %-50s  | %-15s || %-15s | %-15s || %-15s | %-15s | %-15s || %3s ", "Call Target", "Total Calls", "Interp. Calls", "Opt. Calls", "Direct Calls", "Inlined Calls",
065                        "Indirect Calls", "Invalidations"));
066        for (OptimizedCallTarget uniqueCallTarget : uniqueSortedTargets) {
067            List<OptimizedCallTarget> allCallTargets = groupedTargets.get(uniqueCallTarget);
068            int directCallCount = sumCalls(allCallTargets, p -> p.getDirectCallCount());
069            int indirectCallCount = sumCalls(allCallTargets, p -> p.getIndirectCallCount());
070            int inlinedCallCount = sumCalls(allCallTargets, p -> p.getInlinedCallCount());
071            int interpreterCallCount = sumCalls(allCallTargets, p -> p.getInterpreterCallCount());
072            int totalCallCount = sumCalls(allCallTargets, p -> p.getTotalCallCount());
073            int invalidationCount = allCallTargets.stream().collect(Collectors.summingInt(target -> target.getCompilationProfile().getInvalidationCount()));
074
075            totalDirectCallCount += directCallCount;
076            totalInlinedCallCount += inlinedCallCount;
077            totalIndirectCallCount += indirectCallCount;
078            totalInvalidationCount += invalidationCount;
079            totalInterpretedCallCount += interpreterCallCount;
080            totalTotalCallCount += totalCallCount;
081
082            if (totalCallCount > 0) {
083                runtime.log(String.format("  %-50s | %15d || %15d | %15d || %15d | %15d | %15d || %3d", uniqueCallTarget, totalCallCount, interpreterCallCount, totalCallCount - interpreterCallCount,
084                                directCallCount, inlinedCallCount, indirectCallCount, invalidationCount));
085            }
086
087        }
088
089        runtime.log(String.format(" %-50s  | %15d || %15d | %15d || %15d | %15d | %15d || %3d", "Total", totalTotalCallCount, totalInterpretedCallCount, totalTotalCallCount -
090                        totalInterpretedCallCount, totalDirectCallCount, totalInlinedCallCount, totalIndirectCallCount, totalInvalidationCount));
091
092    }
093
094    private static int sumCalls(List<OptimizedCallTarget> targets, Function<TraceCompilationProfile, Integer> function) {
095        return targets.stream().collect(Collectors.summingInt(target -> function.apply((TraceCompilationProfile) target.getCompilationProfile())));
096    }
097}