1 /*
   2  * Copyright (c) 2012, 2015, 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 jdk.internal.jvmci.meta;
  24 
  25 import java.io.*;
  26 import java.lang.reflect.*;
  27 import java.util.*;
  28 
  29 /**
  30  * Miscellaneous collection of utility methods used by {@code jdk.internal.jvmci.meta} and its
  31  * clients.
  32  */
  33 public class MetaUtil {
  34 
  35     private static class ClassInfo {
  36         public long totalSize;
  37         public long instanceCount;
  38 
  39         @Override
  40         public String toString() {
  41             return "totalSize=" + totalSize + ", instanceCount=" + instanceCount;
  42         }
  43     }
  44 
  45     /**
  46      * Returns the number of bytes occupied by this constant value or constant object and
  47      * recursively all values reachable from this value.
  48      *
  49      * @param constant the constant whose bytes should be measured
  50      * @param printTopN print total size and instance count of the top n classes is desired
  51      * @return the number of bytes occupied by this constant
  52      */
  53     public static long getMemorySizeRecursive(MetaAccessProvider access, ConstantReflectionProvider constantReflection, JavaConstant constant, PrintStream out, int printTopN) {
  54         Set<JavaConstant> marked = new HashSet<>();
  55         Deque<JavaConstant> stack = new ArrayDeque<>();
  56         if (constant.getJavaKind() == JavaKind.Object && constant.isNonNull()) {
  57             marked.add(constant);
  58         }
  59         final HashMap<ResolvedJavaType, ClassInfo> histogram = new HashMap<>();
  60         stack.push(constant);
  61         long sum = 0;
  62         while (!stack.isEmpty()) {
  63             JavaConstant c = stack.pop();
  64             long memorySize = access.getMemorySize(constant);
  65             sum += memorySize;
  66             if (c.getJavaKind() == JavaKind.Object && c.isNonNull()) {
  67                 ResolvedJavaType clazz = access.lookupJavaType(c);
  68                 if (!histogram.containsKey(clazz)) {
  69                     histogram.put(clazz, new ClassInfo());
  70                 }
  71                 ClassInfo info = histogram.get(clazz);
  72                 info.instanceCount++;
  73                 info.totalSize += memorySize;
  74                 ResolvedJavaType type = access.lookupJavaType(c);
  75                 if (type.isArray()) {
  76                     if (!type.getComponentType().isPrimitive()) {
  77                         int length = constantReflection.readArrayLength(c);
  78                         for (int i = 0; i < length; i++) {
  79                             JavaConstant value = constantReflection.readArrayElement(c, i);
  80                             pushConstant(marked, stack, value);
  81                         }
  82                     }
  83                 } else {
  84                     ResolvedJavaField[] instanceFields = type.getInstanceFields(true);
  85                     for (ResolvedJavaField f : instanceFields) {
  86                         if (f.getJavaKind() == JavaKind.Object) {
  87                             JavaConstant value = constantReflection.readFieldValue(f, c);
  88                             pushConstant(marked, stack, value);
  89                         }
  90                     }
  91                 }
  92             }
  93         }
  94         ArrayList<ResolvedJavaType> clazzes = new ArrayList<>();
  95         clazzes.addAll(histogram.keySet());
  96         Collections.sort(clazzes, new Comparator<ResolvedJavaType>() {
  97 
  98             @Override
  99             public int compare(ResolvedJavaType o1, ResolvedJavaType o2) {
 100                 long l1 = histogram.get(o1).totalSize;
 101                 long l2 = histogram.get(o2).totalSize;
 102                 if (l1 > l2) {
 103                     return -1;
 104                 } else if (l1 == l2) {
 105                     return 0;
 106                 } else {
 107                     return 1;
 108                 }
 109             }
 110         });
 111 
 112         int z = 0;
 113         for (ResolvedJavaType c : clazzes) {
 114             if (z > printTopN) {
 115                 break;
 116             }
 117             out.println("Class " + c + ", " + histogram.get(c));
 118             ++z;
 119         }
 120 
 121         return sum;
 122     }
 123 
 124     private static void pushConstant(Set<JavaConstant> marked, Deque<JavaConstant> stack, JavaConstant value) {
 125         if (value.isNonNull()) {
 126             if (!marked.contains(value)) {
 127                 marked.add(value);
 128                 stack.push(value);
 129             }
 130         }
 131     }
 132 
 133     /**
 134      * Calls {@link JavaType#resolve(ResolvedJavaType)} on an array of types.
 135      */
 136     public static ResolvedJavaType[] resolveJavaTypes(JavaType[] types, ResolvedJavaType accessingClass) {
 137         ResolvedJavaType[] result = new ResolvedJavaType[types.length];
 138         for (int i = 0; i < result.length; i++) {
 139             result[i] = types[i].resolve(accessingClass);
 140         }
 141         return result;
 142     }
 143 
 144     /**
 145      * Extends the functionality of {@link Class#getSimpleName()} to include a non-empty string for
 146      * anonymous and local classes.
 147      *
 148      * @param clazz the class for which the simple name is being requested
 149      * @param withEnclosingClass specifies if the returned name should be qualified with the name(s)
 150      *            of the enclosing class/classes of {@code clazz} (if any). This option is ignored
 151      *            if {@code clazz} denotes an anonymous or local class.
 152      * @return the simple name
 153      */
 154     public static String getSimpleName(Class<?> clazz, boolean withEnclosingClass) {
 155         final String simpleName = clazz.getSimpleName();
 156         if (simpleName.length() != 0) {
 157             if (withEnclosingClass) {
 158                 String prefix = "";
 159                 Class<?> enclosingClass = clazz;
 160                 while ((enclosingClass = enclosingClass.getEnclosingClass()) != null) {
 161                     prefix = enclosingClass.getSimpleName() + "." + prefix;
 162                 }
 163                 return prefix + simpleName;
 164             }
 165             return simpleName;
 166         }
 167         // Must be an anonymous or local class
 168         final String name = clazz.getName();
 169         int index = name.indexOf('$');
 170         if (index == -1) {
 171             return name;
 172         }
 173         index = name.lastIndexOf('.', index);
 174         if (index == -1) {
 175             return name;
 176         }
 177         return name.substring(index + 1);
 178     }
 179 
 180     static String internalNameToJava(String name, boolean qualified, boolean classForNameCompatible) {
 181         switch (name.charAt(0)) {
 182             case 'L': {
 183                 String result = name.substring(1, name.length() - 1).replace('/', '.');
 184                 if (!qualified) {
 185                     final int lastDot = result.lastIndexOf('.');
 186                     if (lastDot != -1) {
 187                         result = result.substring(lastDot + 1);
 188                     }
 189                 }
 190                 return result;
 191             }
 192             case '[':
 193                 return classForNameCompatible ? name.replace('/', '.') : internalNameToJava(name.substring(1), qualified, classForNameCompatible) + "[]";
 194             default:
 195                 if (name.length() != 1) {
 196                     throw new IllegalArgumentException("Illegal internal name: " + name);
 197                 }
 198                 return JavaKind.fromPrimitiveOrVoidTypeChar(name.charAt(0)).getJavaName();
 199         }
 200     }
 201 
 202     /**
 203      * Turns an class name in internal format into a resolved Java type.
 204      */
 205     public static ResolvedJavaType classForName(String internal, MetaAccessProvider metaAccess, ClassLoader cl) {
 206         JavaKind k = JavaKind.fromTypeString(internal);
 207         try {
 208             String n = internalNameToJava(internal, true, true);
 209             return metaAccess.lookupJavaType(k.isPrimitive() ? k.toJavaClass() : Class.forName(n, true, cl));
 210         } catch (ClassNotFoundException cnfe) {
 211             throw new IllegalArgumentException("could not instantiate class described by " + internal, cnfe);
 212         }
 213     }
 214 
 215     /**
 216      * Convenient shortcut for calling
 217      * {@link #appendLocation(StringBuilder, ResolvedJavaMethod, int)} without having to supply a
 218      * {@link StringBuilder} instance and convert the result to a string.
 219      */
 220     public static String toLocation(ResolvedJavaMethod method, int bci) {
 221         return appendLocation(new StringBuilder(), method, bci).toString();
 222     }
 223 
 224     /**
 225      * Appends a string representation of a location specified by a given method and bci to a given
 226      * {@link StringBuilder}. If a stack trace element with a non-null file name and non-negative
 227      * line number is {@linkplain ResolvedJavaMethod#asStackTraceElement(int) available} for the
 228      * given method, then the string returned is the {@link StackTraceElement#toString()} value of
 229      * the stack trace element, suffixed by the bci location. For example:
 230      *
 231      * <pre>
 232      *     java.lang.String.valueOf(String.java:2930) [bci: 12]
 233      * </pre>
 234      *
 235      * Otherwise, the string returned is the value of applying {@link JavaMethod#format(String)}
 236      * with the format string {@code "%H.%n(%p)"}, suffixed by the bci location. For example:
 237      *
 238      * <pre>
 239      *     java.lang.String.valueOf(int) [bci: 12]
 240      * </pre>
 241      *
 242      * @param sb
 243      * @param method
 244      * @param bci
 245      */
 246     public static StringBuilder appendLocation(StringBuilder sb, ResolvedJavaMethod method, int bci) {
 247         if (method != null) {
 248             StackTraceElement ste = method.asStackTraceElement(bci);
 249             if (ste.getFileName() != null && ste.getLineNumber() > 0) {
 250                 sb.append(ste);
 251             } else {
 252                 sb.append(method.format("%H.%n(%p)"));
 253             }
 254         } else {
 255             sb.append("Null method");
 256         }
 257         return sb.append(" [bci: ").append(bci).append(']');
 258     }
 259 
 260     static void appendProfile(StringBuilder buf, AbstractJavaProfile<?, ?> profile, int bci, String type, String sep) {
 261         if (profile != null) {
 262             AbstractProfiledItem<?>[] pitems = profile.getItems();
 263             if (pitems != null) {
 264                 buf.append(String.format("%s@%d:", type, bci));
 265                 for (int j = 0; j < pitems.length; j++) {
 266                     AbstractProfiledItem<?> pitem = pitems[j];
 267                     buf.append(String.format(" %.6f (%s)%s", pitem.getProbability(), pitem.getItem(), sep));
 268                 }
 269                 if (profile.getNotRecordedProbability() != 0) {
 270                     buf.append(String.format(" %.6f <other %s>%s", profile.getNotRecordedProbability(), type, sep));
 271                 } else {
 272                     buf.append(String.format(" <no other %s>%s", type, sep));
 273                 }
 274             }
 275         }
 276     }
 277 
 278     /**
 279      * Converts a Java source-language class name into the internal form.
 280      *
 281      * @param className the class name
 282      * @return the internal name form of the class name
 283      */
 284     public static String toInternalName(String className) {
 285         if (className.startsWith("[")) {
 286             /* Already in the correct array style. */
 287             return className.replace('.', '/');
 288         }
 289 
 290         StringBuilder result = new StringBuilder();
 291         String base = className;
 292         while (base.endsWith("[]")) {
 293             result.append("[");
 294             base = base.substring(0, base.length() - 2);
 295         }
 296 
 297         switch (base) {
 298             case "boolean":
 299                 result.append("Z");
 300                 break;
 301             case "byte":
 302                 result.append("B");
 303                 break;
 304             case "short":
 305                 result.append("S");
 306                 break;
 307             case "char":
 308                 result.append("C");
 309                 break;
 310             case "int":
 311                 result.append("I");
 312                 break;
 313             case "float":
 314                 result.append("F");
 315                 break;
 316             case "long":
 317                 result.append("J");
 318                 break;
 319             case "double":
 320                 result.append("D");
 321                 break;
 322             case "void":
 323                 result.append("V");
 324                 break;
 325             default:
 326                 result.append("L").append(base.replace('.', '/')).append(";");
 327                 break;
 328         }
 329         return result.toString();
 330     }
 331 
 332     /**
 333      * Prepends the String {@code indentation} to every line in String {@code lines}, including a
 334      * possibly non-empty line following the final newline.
 335      */
 336     public static String indent(String lines, String indentation) {
 337         if (lines.length() == 0) {
 338             return lines;
 339         }
 340         final String newLine = "\n";
 341         if (lines.endsWith(newLine)) {
 342             return indentation + (lines.substring(0, lines.length() - 1)).replace(newLine, newLine + indentation) + newLine;
 343         }
 344         return indentation + lines.replace(newLine, newLine + indentation);
 345     }
 346 
 347     /**
 348      * Gets a string representation of an object based soley on its class and its
 349      * {@linkplain System#identityHashCode(Object) identity hash code}. This avoids and calls to
 350      * virtual methods on the object such as {@link Object#hashCode()}.
 351      */
 352     public static String identityHashCodeString(Object obj) {
 353         if (obj == null) {
 354             return "null";
 355         }
 356         return obj.getClass().getName() + "@" + System.identityHashCode(obj);
 357     }
 358 
 359     /**
 360      * Used to lookup constants from {@link Modifier} that are not public (VARARGS, SYNTHETIC etc.).
 361      */
 362     static int getNonPublicModifierStaticField(String name) {
 363         try {
 364             Field field = Modifier.class.getDeclaredField(name);
 365             field.setAccessible(true);
 366             return field.getInt(null);
 367         } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
 368             throw new InternalError(e);
 369         }
 370     }
 371 }