1 /*
   2  * Copyright (c) 2009, 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 com.sun.hotspot.tools.compiler;
  26 
  27 import java.io.PrintStream;
  28 import java.util.ArrayList;
  29 import java.util.List;
  30 
  31 public class CallSite {
  32 
  33     private int bci;
  34     private Method method;
  35     private int count;
  36     private String receiver;
  37     private int receiver_count;
  38     private String reason;
  39     private List<CallSite> calls;
  40     private int endNodes;
  41     private double timeStamp;
  42 
  43     CallSite() {
  44     }
  45 
  46     CallSite(int bci, Method m) {
  47         this.bci = bci;
  48         this.method = m;
  49     }
  50 
  51     void add(CallSite site) {
  52         if (getCalls() == null) {
  53             setCalls(new ArrayList<CallSite>());
  54         }
  55         getCalls().add(site);
  56     }
  57 
  58     CallSite last() {
  59         return last(-1);
  60     }
  61 
  62     CallSite last(int fromEnd) {
  63         return getCalls().get(getCalls().size() + fromEnd);
  64     }
  65 
  66     public String toString() {
  67         StringBuilder sb = new StringBuilder();
  68         if (getReason() == null) {
  69             sb.append("  @ " + getBci() + " " + getMethod());
  70         } else {
  71             sb.append("- @ " + getBci() + " " + getMethod() + " " + getReason());
  72         }
  73         sb.append("\n");
  74         if (getCalls() != null) {
  75             for (CallSite site : getCalls()) {
  76                 sb.append(site);
  77                 sb.append("\n");
  78             }
  79         }
  80         return sb.toString();
  81     }
  82 
  83     public void print(PrintStream stream) {
  84         print(stream, 0);
  85     }
  86 
  87     void emit(PrintStream stream, int indent) {
  88         for (int i = 0; i < indent; i++) {
  89             stream.print(' ');
  90         }
  91     }
  92     private static boolean compat = true;
  93 
  94     public void print(PrintStream stream, int indent) {
  95         emit(stream, indent);
  96         String m = getMethod().getHolder().replace('/', '.') + "::" + getMethod().getName();
  97         if (getReason() == null) {
  98             stream.print("  @ " + getBci() + " " + m + " (" + getMethod().getBytes() + " bytes)");
  99 
 100         } else {
 101             if (isCompat()) {
 102                 stream.print("  @ " + getBci() + " " + m + " " + getReason());
 103             } else {
 104                 stream.print("- @ " + getBci() + " " + m +
 105                         " (" + getMethod().getBytes() + " bytes) " + getReason());
 106             }
 107         }
 108         if (getEndNodes() > 0) {
 109           stream.printf(" (end time: %6.4f nodes: %d)", getTimeStamp(), getEndNodes());
 110         }
 111         stream.println("");
 112         if (getReceiver() != null) {
 113             emit(stream, indent + 4);
 114             //                 stream.println("type profile " + method.holder + " -> " + receiver + " (" +
 115             //                                receiver_count + "/" + count + "," + (receiver_count * 100 / count) + "%)");
 116             stream.println("type profile " + getMethod().getHolder() + " -> " + getReceiver() + " (" +
 117                     (getReceiverCount() * 100 / getCount()) + "%)");
 118         }
 119         if (getCalls() != null) {
 120             for (CallSite site : getCalls()) {
 121                 site.print(stream, indent + 2);
 122             }
 123         }
 124     }
 125 
 126     public int getBci() {
 127         return bci;
 128     }
 129 
 130     public void setBci(int bci) {
 131         this.bci = bci;
 132     }
 133 
 134     public Method getMethod() {
 135         return method;
 136     }
 137 
 138     public void setMethod(Method method) {
 139         this.method = method;
 140     }
 141 
 142     public int getCount() {
 143         return count;
 144     }
 145 
 146     public void setCount(int count) {
 147         this.count = count;
 148     }
 149 
 150     public String getReceiver() {
 151         return receiver;
 152     }
 153 
 154     public void setReceiver(String receiver) {
 155         this.receiver = receiver;
 156     }
 157 
 158     public int getReceiverCount() {
 159         return receiver_count;
 160     }
 161 
 162     public void setReceiver_count(int receiver_count) {
 163         this.receiver_count = receiver_count;
 164     }
 165 
 166     public String getReason() {
 167         return reason;
 168     }
 169 
 170     public void setReason(String reason) {
 171         this.reason = reason;
 172     }
 173 
 174     public List<CallSite> getCalls() {
 175         return calls;
 176     }
 177 
 178     public void setCalls(List<CallSite> calls) {
 179         this.calls = calls;
 180     }
 181 
 182     public static boolean isCompat() {
 183         return compat;
 184     }
 185 
 186     public static void setCompat(boolean aCompat) {
 187         compat = aCompat;
 188     }
 189 
 190     void setEndNodes(int n) {
 191         endNodes = n;
 192     }
 193 
 194     public int getEndNodes() {
 195         return endNodes;
 196     }
 197 
 198     void setTimeStamp(double time) {
 199         timeStamp = time;
 200     }
 201 
 202     public double getTimeStamp() {
 203         return timeStamp;
 204     }
 205 
 206 }