1 /* 2 * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 25 package org.graalvm.compiler.phases.common; 26 27 import java.util.Arrays; 28 import java.util.Collection; 29 import java.util.HashSet; 30 31 import org.graalvm.compiler.core.common.cfg.Loop; 32 import org.graalvm.compiler.graph.Node; 33 import org.graalvm.compiler.nodes.AbstractBeginNode; 34 import org.graalvm.compiler.nodes.AbstractEndNode; 35 import org.graalvm.compiler.nodes.AbstractMergeNode; 36 import org.graalvm.compiler.nodes.CallTargetNode; 37 import org.graalvm.compiler.nodes.ConstantNode; 38 import org.graalvm.compiler.nodes.DeoptimizeNode; 39 import org.graalvm.compiler.nodes.FixedNode; 40 import org.graalvm.compiler.nodes.FixedWithNextNode; 41 import org.graalvm.compiler.nodes.IfNode; 42 import org.graalvm.compiler.nodes.Invoke; 43 import org.graalvm.compiler.nodes.LogicNode; 44 import org.graalvm.compiler.nodes.ParameterNode; 45 import org.graalvm.compiler.nodes.ReturnNode; 46 import org.graalvm.compiler.nodes.SafepointNode; 47 import org.graalvm.compiler.nodes.StructuredGraph; 48 import org.graalvm.compiler.nodes.StructuredGraph.ScheduleResult; 49 import org.graalvm.compiler.nodes.UnwindNode; 50 import org.graalvm.compiler.nodes.VirtualState; 51 import org.graalvm.compiler.nodes.calc.BinaryNode; 52 import org.graalvm.compiler.nodes.calc.ConvertNode; 53 import org.graalvm.compiler.nodes.calc.FloatDivNode; 54 import org.graalvm.compiler.nodes.calc.IntegerDivRemNode; 55 import org.graalvm.compiler.nodes.calc.MulNode; 56 import org.graalvm.compiler.nodes.calc.NotNode; 57 import org.graalvm.compiler.nodes.calc.ReinterpretNode; 58 import org.graalvm.compiler.nodes.calc.RemNode; 59 import org.graalvm.compiler.nodes.cfg.Block; 60 import org.graalvm.compiler.nodes.cfg.ControlFlowGraph; 61 import org.graalvm.compiler.nodes.debug.DynamicCounterNode; 62 import org.graalvm.compiler.nodes.extended.SwitchNode; 63 import org.graalvm.compiler.nodes.java.AbstractNewObjectNode; 64 import org.graalvm.compiler.nodes.java.AccessMonitorNode; 65 import org.graalvm.compiler.nodes.java.MonitorIdNode; 66 import org.graalvm.compiler.nodes.memory.Access; 67 import org.graalvm.compiler.nodes.spi.ValueProxy; 68 import org.graalvm.compiler.nodes.virtual.VirtualObjectNode; 69 import org.graalvm.compiler.phases.Phase; 70 import org.graalvm.compiler.phases.schedule.SchedulePhase; 71 72 /** 73 * This phase add counters for the dynamically executed number of nodes. Incrementing the counter 74 * for each node would be too costly, so this phase takes the compromise that it trusts split 75 * probabilities, but not loop frequencies. This means that it will insert counters at the start of 76 * a method and at each loop header. 77 * 78 * A schedule is created so that floating nodes can also be taken into account. The weight of a node 79 * is determined heuristically in the {@link ProfileCompiledMethodsPhase#getNodeWeight(Node)} 80 * method. 81 * 82 * Additionally, there's a second counter that's only increased for code sections without invokes. 83 */ 84 public class ProfileCompiledMethodsPhase extends Phase { 85 86 private static final String GROUP_NAME = "~profiled weight"; 87 private static final String GROUP_NAME_WITHOUT = "~profiled weight (invoke-free sections)"; 88 private static final String GROUP_NAME_INVOKES = "~profiled invokes"; 89 90 private static final boolean WITH_SECTION_HEADER = Boolean.parseBoolean(System.getProperty("ProfileCompiledMethodsPhase.WITH_SECTION_HEADER", "false")); 91 private static final boolean WITH_INVOKE_FREE_SECTIONS = Boolean.parseBoolean(System.getProperty("ProfileCompiledMethodsPhase.WITH_FREE_SECTIONS", "false")); 92 private static final boolean WITH_INVOKES = Boolean.parseBoolean(System.getProperty("ProfileCompiledMethodsPhase.WITH_INVOKES", "true")); 93 94 @Override 95 protected void run(StructuredGraph graph) { 96 SchedulePhase schedule = new SchedulePhase(graph.getOptions()); 97 schedule.apply(graph, false); 98 99 ControlFlowGraph cfg = ControlFlowGraph.compute(graph, true, true, true, true); 100 for (Loop<Block> loop : cfg.getLoops()) { 101 double loopProbability = cfg.blockFor(loop.getHeader().getBeginNode()).getRelativeFrequency(); 102 if (loopProbability > (1D / Integer.MAX_VALUE)) { 103 addSectionCounters(loop.getHeader().getBeginNode(), loop.getBlocks(), loop.getChildren(), graph.getLastSchedule(), cfg); 104 } 105 } 106 // don't put the counter increase directly after the start (problems with OSR) 107 FixedWithNextNode current = graph.start(); 108 while (current.next() instanceof FixedWithNextNode) { 109 current = (FixedWithNextNode) current.next(); 110 } 111 addSectionCounters(current, Arrays.asList(cfg.getBlocks()), cfg.getLoops(), graph.getLastSchedule(), cfg); 112 113 if (WITH_INVOKES) { 114 for (Node node : graph.getNodes()) { 115 if (node instanceof Invoke) { 116 Invoke invoke = (Invoke) node; 117 DynamicCounterNode.addCounterBefore(GROUP_NAME_INVOKES, invoke.callTarget().targetName(), 1, true, invoke.asNode()); 118 119 } 120 } 121 } 122 } 123 124 private static void addSectionCounters(FixedWithNextNode start, Collection<Block> sectionBlocks, Collection<Loop<Block>> childLoops, ScheduleResult schedule, ControlFlowGraph cfg) { 125 HashSet<Block> blocks = new HashSet<>(sectionBlocks); 126 for (Loop<Block> loop : childLoops) { 127 blocks.removeAll(loop.getBlocks()); 128 } 129 long increment = DynamicCounterNode.clampIncrement((long) (getSectionWeight(schedule, blocks) / cfg.blockFor(start).getRelativeFrequency())); 130 DynamicCounterNode.addCounterBefore(GROUP_NAME, sectionHead(start), increment, true, start.next()); 131 if (WITH_INVOKE_FREE_SECTIONS && !hasInvoke(blocks)) { 132 DynamicCounterNode.addCounterBefore(GROUP_NAME_WITHOUT, sectionHead(start), increment, true, start.next()); 133 } 134 } 135 136 private static String sectionHead(Node node) { 137 if (WITH_SECTION_HEADER) { 138 return node.toString(); 139 } else { 140 return ""; 141 } 142 } 143 144 private static double getSectionWeight(ScheduleResult schedule, Collection<Block> blocks) { 145 double count = 0; 146 for (Block block : blocks) { 147 double blockProbability = block.getRelativeFrequency(); 148 for (Node node : schedule.getBlockToNodesMap().get(block)) { 149 count += blockProbability * getNodeWeight(node); 150 } 151 } 152 return count; 153 } 154 155 private static double getNodeWeight(Node node) { 156 if (node instanceof AbstractMergeNode) { 157 return ((AbstractMergeNode) node).phiPredecessorCount(); 158 } else if (node instanceof AbstractBeginNode || node instanceof AbstractEndNode || node instanceof MonitorIdNode || node instanceof ConstantNode || node instanceof ParameterNode || 159 node instanceof CallTargetNode || node instanceof ValueProxy || node instanceof VirtualObjectNode || node instanceof ReinterpretNode) { 160 return 0; 161 } else if (node instanceof AccessMonitorNode) { 162 return 10; 163 } else if (node instanceof Access) { 164 return 2; 165 } else if (node instanceof LogicNode || node instanceof ConvertNode || node instanceof NotNode) { 166 return 1; 167 } else if (node instanceof IntegerDivRemNode || node instanceof FloatDivNode || node instanceof RemNode) { 168 return 10; 169 } else if (node instanceof MulNode) { 170 return 3; 171 } else if (node instanceof Invoke) { 172 return 5; 173 } else if (node instanceof IfNode || node instanceof SafepointNode || node instanceof BinaryNode) { 174 return 1; 175 } else if (node instanceof SwitchNode) { 176 return node.successors().count(); 177 } else if (node instanceof ReturnNode || node instanceof UnwindNode || node instanceof DeoptimizeNode) { 178 return node.successors().count(); 179 } else if (node instanceof AbstractNewObjectNode) { 180 return 10; 181 } else if (node instanceof VirtualState) { 182 return 0; 183 } 184 return 2; 185 } 186 187 private static boolean hasInvoke(Collection<Block> blocks) { 188 boolean hasInvoke = false; 189 for (Block block : blocks) { 190 for (FixedNode fixed : block.getNodes()) { 191 if (fixed instanceof Invoke) { 192 hasInvoke = true; 193 } 194 } 195 } 196 return hasInvoke; 197 } 198 199 @Override 200 public boolean checkContract() { 201 return false; 202 } 203 204 }