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.*;
028
029import com.oracle.graal.truffle.*;
030import com.oracle.truffle.api.nodes.*;
031import com.oracle.truffle.api.nodes.NodeUtil.NodeCountFilter;
032
033public final class TraceSplittingListener extends AbstractDebugCompilationListener {
034
035    private TraceSplittingListener() {
036    }
037
038    public static void install(GraalTruffleRuntime runtime) {
039        if (TraceTruffleSplitting.getValue()) {
040            runtime.addCompilationListener(new TraceSplittingListener());
041        }
042    }
043
044    private int splitCount;
045
046    @Override
047    public void notifyCompilationSplit(OptimizedDirectCallNode callNode) {
048        OptimizedCallTarget callTarget = callNode.getCallTarget();
049        String label = String.format("split %3s-%-4s-%-4s ", splitCount++, callNode.getCurrentCallTarget().getCloneIndex(), callNode.getCallCount());
050        log(callTarget, 0, label, callTarget.toString(), callTarget.getDebugProperties());
051
052        if (TruffleSplittingNew.getValue()) {
053            Map<TruffleStamp, OptimizedCallTarget> splitTargets = callTarget.getSplitVersions();
054            logProfile(callTarget.getArgumentStamp(), callTarget);
055            for (TruffleStamp profile : splitTargets.keySet()) {
056                logProfile(profile, splitTargets.get(profile));
057            }
058        }
059    }
060
061    private static void logProfile(TruffleStamp stamp, OptimizedCallTarget target) {
062        String id = String.format("@%8h %s", target.hashCode(), target.getSourceCallTarget() == null ? "orig." : "split");
063        target.log(String.format("%16s%-20sCallers: %3d, Nodes:%10s %s", "", id, target.getKnownCallSiteCount(), //
064                        String.format("%d (%d/%d)", count(target, NodeCost.MONOMORPHIC), count(target, NodeCost.POLYMORPHIC), count(target, NodeCost.MEGAMORPHIC)), stamp));
065    }
066
067    private static int count(OptimizedCallTarget target, final NodeCost otherCost) {
068        return NodeUtil.countNodes(target.getRootNode(), new NodeCountFilter() {
069            public boolean isCounted(Node node) {
070                return node.getCost() == otherCost;
071            }
072        });
073    }
074
075}