1 /* 2 * Copyright (c) 2012, 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.hotspot; 26 27 import static java.lang.Thread.currentThread; 28 29 import java.io.FileNotFoundException; 30 import java.io.FileOutputStream; 31 import java.io.PrintStream; 32 import java.lang.annotation.ElementType; 33 import java.lang.annotation.Retention; 34 import java.lang.annotation.RetentionPolicy; 35 import java.lang.annotation.Target; 36 import java.lang.reflect.Field; 37 import java.lang.reflect.Modifier; 38 import java.util.ArrayDeque; 39 import java.util.ArrayList; 40 import java.util.Arrays; 41 import java.util.Date; 42 import java.util.Deque; 43 import java.util.Locale; 44 import java.util.concurrent.ConcurrentLinkedDeque; 45 46 import org.graalvm.compiler.debug.CSVUtil; 47 import org.graalvm.compiler.options.Option; 48 import org.graalvm.compiler.options.OptionKey; 49 import org.graalvm.compiler.options.OptionValues; 50 import org.graalvm.compiler.serviceprovider.GraalServices; 51 52 import jdk.vm.ci.hotspot.HotSpotInstalledCode; 53 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod; 54 55 @SuppressWarnings("unused") 56 public final class CompilationStatistics { 57 58 public static class Options { 59 // @formatter:off 60 @Option(help = "Enables CompilationStatistics.") 61 public static final OptionKey<Boolean> UseCompilationStatistics = new OptionKey<>(false); 62 // @formatter:on 63 } 64 65 private static final long RESOLUTION = 100000000; 66 67 private static final CompilationStatistics DUMMY = new CompilationStatistics(null, false); 68 69 private static ConcurrentLinkedDeque<CompilationStatistics> list = new ConcurrentLinkedDeque<>(); 70 71 private static final ThreadLocal<Deque<CompilationStatistics>> current = new ThreadLocal<Deque<CompilationStatistics>>() { 72 73 @Override 74 protected Deque<CompilationStatistics> initialValue() { 75 return new ArrayDeque<>(); 76 } 77 }; 78 79 @Retention(RetentionPolicy.RUNTIME) 80 @Target(ElementType.FIELD) 81 private static @interface NotReported { 82 } 83 84 @Retention(RetentionPolicy.RUNTIME) 85 @Target(ElementType.FIELD) 86 private static @interface TimeValue { 87 } 88 89 private static long zeroTime = System.nanoTime(); 90 91 private static long getThreadAllocatedBytes() { 92 return GraalServices.getCurrentThreadAllocatedBytes(); 93 } 94 95 @NotReported private final long startTime; 96 @NotReported private long threadAllocatedBytesStart; 97 98 private int bytecodeCount; 99 private int codeSize; 100 @TimeValue private long duration; 101 private long memoryUsed; 102 private final boolean osr; 103 private final String holder; 104 private final String name; 105 private final String signature; 106 107 private CompilationStatistics(HotSpotResolvedJavaMethod method, boolean osr) { 108 this.osr = osr; 109 if (method != null) { 110 holder = method.getDeclaringClass().getName(); 111 name = method.getName(); 112 signature = method.getSignature().toMethodDescriptor(); 113 startTime = System.nanoTime(); 114 bytecodeCount = method.getCodeSize(); 115 threadAllocatedBytesStart = getThreadAllocatedBytes(); 116 } else { 117 assert DUMMY == null : "only DUMMY has no method"; 118 holder = ""; 119 name = ""; 120 signature = ""; 121 startTime = 0; 122 } 123 } 124 125 public void finish(HotSpotResolvedJavaMethod method, HotSpotInstalledCode code) { 126 if (isEnabled()) { 127 duration = System.nanoTime() - startTime; 128 codeSize = (int) code.getCodeSize(); 129 memoryUsed = getThreadAllocatedBytes() - threadAllocatedBytesStart; 130 if (current.get().getLast() != this) { 131 throw new RuntimeException("mismatch in finish()"); 132 } 133 current.get().removeLast(); 134 } 135 } 136 137 public static CompilationStatistics current() { 138 return current.get().isEmpty() ? null : current.get().getLast(); 139 } 140 141 public static CompilationStatistics create(OptionValues options, HotSpotResolvedJavaMethod method, boolean isOSR) { 142 if (Options.UseCompilationStatistics.getValue(options)) { 143 CompilationStatistics stats = new CompilationStatistics(method, isOSR); 144 list.add(stats); 145 current.get().addLast(stats); 146 return stats; 147 } else { 148 return DUMMY; 149 } 150 } 151 152 public boolean isEnabled() { 153 return this != DUMMY; 154 } 155 156 @SuppressWarnings("deprecation") 157 public static void clear(String dumpName) { 158 try { 159 ConcurrentLinkedDeque<CompilationStatistics> snapshot = list; 160 long snapshotZeroTime = zeroTime; 161 162 list = new ConcurrentLinkedDeque<>(); 163 zeroTime = System.nanoTime(); 164 165 Date now = new Date(); 166 String dateString = (now.getYear() + 1900) + "-" + (now.getMonth() + 1) + "-" + now.getDate() + "-" + now.getHours() + "" + now.getMinutes(); 167 168 dumpCompilations(snapshot, dumpName, dateString); 169 170 try (FileOutputStream fos = new FileOutputStream("timeline_" + dateString + "_" + dumpName + ".csv", true); PrintStream out = new PrintStream(fos)) { 171 172 long[] timeSpent = new long[10000]; 173 int maxTick = 0; 174 for (CompilationStatistics stats : snapshot) { 175 long start = stats.startTime - snapshotZeroTime; 176 long duration = stats.duration; 177 if (start < 0) { 178 duration -= -start; 179 start = 0; 180 } 181 182 int tick = (int) (start / RESOLUTION); 183 long timeLeft = RESOLUTION - (start % RESOLUTION); 184 185 while (tick < timeSpent.length && duration > 0) { 186 if (tick > maxTick) { 187 maxTick = tick; 188 } 189 timeSpent[tick] += Math.min(timeLeft, duration); 190 duration -= timeLeft; 191 tick++; 192 timeLeft = RESOLUTION; 193 } 194 } 195 String timelineName = System.getProperty("stats.timeline.name"); 196 if (timelineName != null && !timelineName.isEmpty()) { 197 out.printf("%s%c", CSVUtil.Escape.escape(timelineName), CSVUtil.SEPARATOR); 198 } 199 for (int i = 0; i < maxTick; i++) { 200 out.printf("%d%c", normalize(timeSpent[i]), CSVUtil.SEPARATOR); 201 } 202 // print last column 203 out.printf("%d", normalize(timeSpent[maxTick])); 204 out.println(); 205 } 206 } catch (Exception e) { 207 throw new RuntimeException(e); 208 } 209 } 210 211 private static long normalize(long time) { 212 return time * 100 / RESOLUTION; 213 } 214 215 protected static void dumpCompilations(ConcurrentLinkedDeque<CompilationStatistics> snapshot, String dumpName, String dateString) throws IllegalAccessException, FileNotFoundException { 216 String fileName = "compilations_" + dateString + "_" + dumpName + ".csv"; 217 char separator = '\t'; 218 try (PrintStream out = new PrintStream(fileName)) { 219 // output the list of all compilations 220 221 Field[] declaredFields = CompilationStatistics.class.getDeclaredFields(); 222 ArrayList<Field> fields = new ArrayList<>(); 223 for (Field field : declaredFields) { 224 if (!Modifier.isStatic(field.getModifiers()) && !field.isAnnotationPresent(NotReported.class)) { 225 fields.add(field); 226 } 227 } 228 String format = CSVUtil.buildFormatString("%s", separator, fields.size()); 229 CSVUtil.Escape.println(out, separator, CSVUtil.QUOTE, CSVUtil.ESCAPE, format, fields.toArray()); 230 for (CompilationStatistics stats : snapshot) { 231 Object[] values = new Object[fields.size()]; 232 for (int i = 0; i < fields.size(); i++) { 233 Field field = fields.get(i); 234 if (field.isAnnotationPresent(TimeValue.class)) { 235 double value = field.getLong(stats) / 1000000d; 236 values[i] = String.format(Locale.ENGLISH, "%.3f", value); 237 } else { 238 values[i] = field.get(stats); 239 } 240 } 241 CSVUtil.Escape.println(out, separator, CSVUtil.QUOTE, CSVUtil.ESCAPE, format, values); 242 } 243 } 244 } 245 }