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 package org.graalvm.compiler.phases.common; 24 25 import java.util.Arrays; 26 import java.util.Collection; 27 import java.util.HashSet; 28 29 import org.graalvm.compiler.core.common.cfg.Loop; 30 import org.graalvm.compiler.graph.Node; 31 import org.graalvm.compiler.nodes.AbstractBeginNode; 32 import org.graalvm.compiler.nodes.AbstractEndNode; 33 import org.graalvm.compiler.nodes.AbstractMergeNode; 34 import org.graalvm.compiler.nodes.CallTargetNode; 35 import org.graalvm.compiler.nodes.ConstantNode; 36 import org.graalvm.compiler.nodes.DeoptimizeNode; 37 import org.graalvm.compiler.nodes.FixedNode; 38 import org.graalvm.compiler.nodes.FixedWithNextNode; 39 import org.graalvm.compiler.nodes.IfNode; 40 import org.graalvm.compiler.nodes.Invoke; 41 import org.graalvm.compiler.nodes.LogicNode; 42 import org.graalvm.compiler.nodes.ParameterNode; 43 import org.graalvm.compiler.nodes.ReturnNode; 44 import org.graalvm.compiler.nodes.SafepointNode; 45 import org.graalvm.compiler.nodes.StructuredGraph; 46 import org.graalvm.compiler.nodes.StructuredGraph.ScheduleResult; 47 import org.graalvm.compiler.nodes.UnwindNode; 48 import org.graalvm.compiler.nodes.VirtualState; 49 import org.graalvm.compiler.nodes.calc.BinaryNode; 50 import org.graalvm.compiler.nodes.calc.ConvertNode; 51 import org.graalvm.compiler.nodes.calc.DivNode; 52 import org.graalvm.compiler.nodes.calc.IntegerDivRemNode; 53 import org.graalvm.compiler.nodes.calc.MulNode; 54 import org.graalvm.compiler.nodes.calc.NotNode; 55 import org.graalvm.compiler.nodes.calc.ReinterpretNode; 56 import org.graalvm.compiler.nodes.calc.RemNode; 57 import org.graalvm.compiler.nodes.cfg.Block; 58 import org.graalvm.compiler.nodes.cfg.ControlFlowGraph; 59 import org.graalvm.compiler.nodes.debug.DynamicCounterNode; 60 import org.graalvm.compiler.nodes.extended.SwitchNode; 61 import org.graalvm.compiler.nodes.java.AbstractNewObjectNode; 62 import org.graalvm.compiler.nodes.java.AccessMonitorNode; 63 import org.graalvm.compiler.nodes.java.MonitorIdNode; 64 import org.graalvm.compiler.nodes.memory.Access; 65 import org.graalvm.compiler.nodes.spi.ValueProxy; 66 import org.graalvm.compiler.nodes.virtual.VirtualObjectNode; 67 import org.graalvm.compiler.phases.Phase; 68 import org.graalvm.compiler.phases.schedule.SchedulePhase; 69 70 /** 71 * This phase add counters for the dynamically executed number of nodes. Incrementing the counter 72 * for each node would be too costly, so this phase takes the compromise that it trusts split 73 * probabilities, but not loop frequencies. This means that it will insert counters at the start of 74 * a method and at each loop header. 75 * 76 * A schedule is created so that floating nodes can also be taken into account. The weight of a node 77 * is determined heuristically in the {@link ProfileCompiledMethodsPhase#getNodeWeight(Node)} 78 * method. 79 * 80 * Additionally, there's a second counter that's only increased for code sections without invokes. 81 */ 82 public class ProfileCompiledMethodsPhase extends Phase { 83 84 private static final String GROUP_NAME = "~profiled weight"; 85 private static final String GROUP_NAME_WITHOUT = "~profiled weight (invoke-free sections)"; 86 private static final String GROUP_NAME_INVOKES = "~profiled invokes"; 87 88 private static final boolean WITH_SECTION_HEADER = Boolean.parseBoolean(System.getProperty("ProfileCompiledMethodsPhase.WITH_SECTION_HEADER", "false")); 89 private static final boolean WITH_INVOKE_FREE_SECTIONS = Boolean.parseBoolean(System.getProperty("ProfileCompiledMethodsPhase.WITH_FREE_SECTIONS", "false")); 90 private static final boolean WITH_INVOKES = Boolean.parseBoolean(System.getProperty("ProfileCompiledMethodsPhase.WITH_INVOKES", "true")); 91 92 @Override 93 protected void run(StructuredGraph graph) { 94 SchedulePhase schedule = new SchedulePhase(); 95 schedule.apply(graph, false); 96 97 ControlFlowGraph cfg = ControlFlowGraph.compute(graph, true, true, true, true); 98 for (Loop<Block> loop : cfg.getLoops()) { 99 double loopProbability = cfg.blockFor(loop.getHeader().getBeginNode()).probability(); 100 if (loopProbability > (1D / Integer.MAX_VALUE)) { 101 addSectionCounters(loop.getHeader().getBeginNode(), loop.getBlocks(), loop.getChildren(), graph.getLastSchedule(), cfg); 102 } 103 } 104 // don't put the counter increase directly after the start (problems with OSR) 105 FixedWithNextNode current = graph.start(); 106 while (current.next() instanceof FixedWithNextNode) { 107 current = (FixedWithNextNode) current.next(); 108 } 109 addSectionCounters(current, Arrays.asList(cfg.getBlocks()), cfg.getLoops(), graph.getLastSchedule(), cfg); 110 111 if (WITH_INVOKES) { 112 for (Node node : graph.getNodes()) { 113 if (node instanceof Invoke) { 114 Invoke invoke = (Invoke) node; 115 DynamicCounterNode.addCounterBefore(GROUP_NAME_INVOKES, invoke.callTarget().targetName(), 1, true, invoke.asNode()); 116 117 } 118 } 119 } 120 } 121 122 private static void addSectionCounters(FixedWithNextNode start, Collection<Block> sectionBlocks, Collection<Loop<Block>> childLoops, ScheduleResult schedule, ControlFlowGraph cfg) { 123 HashSet<Block> blocks = new HashSet<>(sectionBlocks); 124 for (Loop<?> loop : childLoops) { 125 blocks.removeAll(loop.getBlocks()); 126 } 127 double weight = getSectionWeight(schedule, blocks) / cfg.blockFor(start).probability(); 128 DynamicCounterNode.addCounterBefore(GROUP_NAME, sectionHead(start), (long) weight, true, start.next()); 129 if (WITH_INVOKE_FREE_SECTIONS && !hasInvoke(blocks)) { 130 DynamicCounterNode.addCounterBefore(GROUP_NAME_WITHOUT, sectionHead(start), (long) weight, true, start.next()); 131 } 132 } 133 134 private static String sectionHead(Node node) { 135 if (WITH_SECTION_HEADER) { 136 return node.toString(); 137 } else { 138 return ""; 139 } 140 } 141 142 private static double getSectionWeight(ScheduleResult schedule, Collection<Block> blocks) { 143 double count = 0; 144 for (Block block : blocks) { 145 double blockProbability = block.probability(); 146 for (Node node : schedule.getBlockToNodesMap().get(block)) { 147 count += blockProbability * getNodeWeight(node); 148 } 149 } 150 return count; 151 } 152 153 private static double getNodeWeight(Node node) { 154 if (node instanceof AbstractMergeNode) { 155 return ((AbstractMergeNode) node).phiPredecessorCount(); 156 } else if (node instanceof AbstractBeginNode || node instanceof AbstractEndNode || node instanceof MonitorIdNode || node instanceof ConstantNode || node instanceof ParameterNode || 157 node instanceof CallTargetNode || node instanceof ValueProxy || node instanceof VirtualObjectNode || node instanceof ReinterpretNode) { 158 return 0; 159 } else if (node instanceof AccessMonitorNode) { 160 return 10; 161 } else if (node instanceof Access) { 162 return 2; 163 } else if (node instanceof LogicNode || node instanceof ConvertNode || node instanceof BinaryNode || node instanceof NotNode) { 164 return 1; 165 } else if (node instanceof IntegerDivRemNode || node instanceof DivNode || node instanceof RemNode) { 166 return 10; 167 } else if (node instanceof MulNode) { 168 return 3; 169 } else if (node instanceof Invoke) { 170 return 5; 171 } else if (node instanceof IfNode || node instanceof SafepointNode) { 172 return 1; 173 } else if (node instanceof SwitchNode) { 174 return node.successors().count(); 175 } else if (node instanceof ReturnNode || node instanceof UnwindNode || node instanceof DeoptimizeNode) { 176 return node.successors().count(); 177 } else if (node instanceof AbstractNewObjectNode) { 178 return 10; 179 } else if (node instanceof VirtualState) { 180 return 0; 181 } 182 return 2; 183 } 184 185 private static boolean hasInvoke(Collection<Block> blocks) { 186 boolean hasInvoke = false; 187 for (Block block : blocks) { 188 for (FixedNode fixed : block.getNodes()) { 189 if (fixed instanceof Invoke) { 190 hasInvoke = true; 191 } 192 } 193 } 194 return hasInvoke; 195 } 196 197 @Override 198 public boolean checkContract() { 199 return false; 200 } 201 202 }