| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 |
* contributor license agreements. See the NOTICE file distributed with |
| 4 |
* this work for additional information regarding copyright ownership. |
| 5 |
* The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 |
* (the "License"); you may not use this file except in compliance with |
| 7 |
* the License. You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
*/ |
| 17 |
|
| 18 |
package org.apache.lucene.gradle; |
| 19 |
|
| 20 |
import java.io.IOException; |
| 21 |
import java.nio.file.Paths; |
| 22 |
import java.util.AbstractMap.SimpleEntry; |
| 23 |
import java.util.Arrays; |
| 24 |
import java.util.ArrayList; |
| 25 |
import java.util.HashMap; |
| 26 |
import java.util.List; |
| 27 |
import java.util.Locale; |
| 28 |
import java.util.Map; |
| 29 |
|
| 30 |
import jdk.jfr.consumer.RecordedClass; |
| 31 |
import jdk.jfr.consumer.RecordedEvent; |
| 32 |
import jdk.jfr.consumer.RecordedFrame; |
| 33 |
import jdk.jfr.consumer.RecordedMethod; |
| 34 |
import jdk.jfr.consumer.RecordedStackTrace; |
| 35 |
import jdk.jfr.consumer.RecordedThread; |
| 36 |
import jdk.jfr.consumer.RecordingFile; |
| 37 |
|
| 38 |
/** |
| 39 |
* Processes an array of recording files (from tests), and prints a simple histogram. |
| 40 |
* Inspired by the JFR example code. |
| 41 |
* Whole stacks are deduplicated (with the default stacksize being 1): you can drill deeper |
| 42 |
* by adjusting the parameters. |
| 43 |
*/ |
| 44 |
public class ProfileResults { |
| 45 |
/** |
| 46 |
* Formats a frame to a formatted line. This is deduplicated on! |
| 47 |
*/ |
| 48 |
static String frameToString(RecordedFrame frame, boolean lineNumbers) { |
| 49 |
StringBuilder builder = new StringBuilder(); |
| 50 |
RecordedMethod method = frame.getMethod(); |
| 51 |
RecordedClass clazz = method.getType(); |
| 52 |
if (clazz == null) { |
| 53 |
builder.append("<<"); |
| 54 |
builder.append(frame.getType()); |
| 55 |
builder.append(">>"); |
| 56 |
} else { |
| 57 |
builder.append(clazz.getName()); |
| 58 |
} |
| 59 |
builder.append("#"); |
| 60 |
builder.append(method.getName()); |
| 61 |
builder.append("()"); |
| 62 |
if (lineNumbers) { |
| 63 |
builder.append(":"); |
| 64 |
if (frame.getLineNumber() == -1) { |
| 65 |
builder.append("(" + frame.getType() + " code)"); |
| 66 |
} else { |
| 67 |
builder.append(frame.getLineNumber()); |
| 68 |
} |
| 69 |
} |
| 70 |
return builder.toString(); |
| 71 |
} |
| 72 |
|
| 73 |
/* |
| 74 |
* Making the java code wordy to work around gradle's broken property design, ugh! |
| 75 |
*/ |
| 76 |
public static final String MODE_KEY = "tests.profile.mode"; |
| 77 |
public static final String MODE_DEFAULT = "cpu"; |
| 78 |
public static final String STACKSIZE_KEY = "tests.profile.stacksize"; |
| 79 |
public static final String STACKSIZE_DEFAULT = "1"; |
| 80 |
public static final String COUNT_KEY = "tests.profile.count"; |
| 81 |
public static final String COUNT_DEFAULT = "10"; |
| 82 |
public static final String LINENUMBERS_KEY = "tests.profile.linenumbers"; |
| 83 |
public static final String LINENUMBERS_DEFAULT = "false"; |
| 84 |
|
| 85 |
/** |
| 86 |
* Driver method, for testing standalone. |
| 87 |
* <pre> |
| 88 |
* java -Dtests.profile.count=5 buildSrc/src/main/java/org/apache/lucene/gradle/ProfileResults.java \ |
| 89 |
* ./lucene/core/build/tmp/tests-cwd/somefile.jfr ... |
| 90 |
* </pre> |
| 91 |
*/ |
| 92 |
public static void main(String[] args) throws IOException { |
| 93 |
printReport(Arrays.asList(args), |
| 94 |
System.getProperty(MODE_KEY, MODE_DEFAULT), |
| 95 |
Integer.parseInt(System.getProperty(STACKSIZE_KEY, STACKSIZE_DEFAULT)), |
| 96 |
Integer.parseInt(System.getProperty(COUNT_KEY, COUNT_DEFAULT)), |
| 97 |
Boolean.parseBoolean(System.getProperty(LINENUMBERS_KEY, LINENUMBERS_DEFAULT))); |
| 98 |
} |
| 99 |
|
| 100 |
/** true if we care about this event */ |
| 101 |
static boolean isInteresting(String mode, RecordedEvent event) { |
| 102 |
String name = event.getEventType().getName(); |
| 103 |
switch(mode) { |
| 104 |
case "cpu": |
| 105 |
return (name.equals("jdk.ExecutionSample") || name.equals("jdk.NativeMethodSample")) && |
| 106 |
!isGradlePollThread(event.getThread("sampledThread")); |
| 107 |
case "heap": |
| 108 |
return (name.equals("jdk.ObjectAllocationInNewTLAB") || name.equals("jdk.ObjectAllocationOutsideTLAB")) && |
| 109 |
!isGradlePollThread(event.getThread("eventThread")); |
| 110 |
default: |
| 111 |
throw new UnsupportedOperationException(event.toString()); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
/** true if the thread is gradle epoll thread (which we don't care about) */ |
| 116 |
static boolean isGradlePollThread(RecordedThread thread) { |
| 117 |
return (thread != null && thread.getJavaName().startsWith("/127.0.0.1")); |
| 118 |
} |
| 119 |
|
| 120 |
/** value we accumulate for this event */ |
| 121 |
static long getValue(RecordedEvent event) { |
| 122 |
switch(event.getEventType().getName()) { |
| 123 |
case "jdk.ObjectAllocationInNewTLAB": |
| 124 |
return event.getLong("tlabSize"); |
| 125 |
case "jdk.ObjectAllocationOutsideTLAB": |
| 126 |
return event.getLong("allocationSize"); |
| 127 |
case "jdk.ExecutionSample": |
| 128 |
return 1L; |
| 129 |
case "jdk.NativeMethodSample": |
| 130 |
return 1L; |
| 131 |
default: |
| 132 |
throw new UnsupportedOperationException(event.toString()); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
/** format a value, if its huge, we show millions */ |
| 137 |
static String formatValue(long value) { |
| 138 |
if (value > 1_000_000) { |
| 139 |
return String.format("%dM", value / 1_000_000); |
| 140 |
} else { |
| 141 |
return Long.toString(value); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
/** fixed width used for printing the different columns */ |
| 146 |
private static final int COLUMN_SIZE = 14; |
| 147 |
private static final String COLUMN_PAD = "%-" + COLUMN_SIZE + "s"; |
| 148 |
private static String pad(String input) { |
| 149 |
return String.format(Locale.ROOT, COLUMN_PAD, input); |
| 150 |
} |
| 151 |
|
| 152 |
/** Process all the JFR files passed in args and print a merged summary. */ |
| 153 |
public static void printReport( |
| 154 |
List<String> files, |
| 155 |
String mode, |
| 156 |
int stacksize, |
| 157 |
int count, |
| 158 |
boolean lineNumbers) |
| 159 |
throws IOException { |
| 160 |
if (!"cpu".equals(mode) && !"heap".equals(mode)) { |
| 161 |
throw new IllegalArgumentException("tests.profile.mode must be one of (cpu,heap)"); |
| 162 |
} |
| 163 |
if (stacksize < 1) { |
| 164 |
throw new IllegalArgumentException("tests.profile.stacksize must be positive"); |
| 165 |
} |
| 166 |
if (count < 1) { |
| 167 |
throw new IllegalArgumentException("tests.profile.count must be positive"); |
| 168 |
} |
| 169 |
Map<String, SimpleEntry<String, Long>> histogram = new HashMap<>(); |
| 170 |
int totalEvents = 0; |
| 171 |
long sumValues = 0; |
| 172 |
String framePadding = " ".repeat(COLUMN_SIZE * 2); |
| 173 |
for (String file : files) { |
| 174 |
try (RecordingFile recording = new RecordingFile(Paths.get(file))) { |
| 175 |
while (recording.hasMoreEvents()) { |
| 176 |
RecordedEvent event = recording.readEvent(); |
| 177 |
if (!isInteresting(mode, event)) { |
| 178 |
continue; |
| 179 |
} |
| 180 |
RecordedStackTrace trace = event.getStackTrace(); |
| 181 |
if (trace != null) { |
| 182 |
StringBuilder stack = new StringBuilder(); |
| 183 |
for (int i = 0; i < Math.min(stacksize, trace.getFrames().size()); i++) { |
| 184 |
if (stack.length() > 0) { |
| 185 |
stack.append("\n") |
| 186 |
.append(framePadding) |
| 187 |
.append(" at "); |
| 188 |
} |
| 189 |
stack.append(frameToString(trace.getFrames().get(i), lineNumbers)); |
| 190 |
} |
| 191 |
String line = stack.toString(); |
| 192 |
SimpleEntry<String,Long> entry = histogram.computeIfAbsent(line, u -> new SimpleEntry<String, Long>(line, 0L)); |
| 193 |
long value = getValue(event); |
| 194 |
entry.setValue(entry.getValue() + value); |
| 195 |
totalEvents++; |
| 196 |
sumValues += value; |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
// print summary from histogram |
| 202 |
System.out.printf(Locale.ROOT, "PROFILE SUMMARY from %d events (total: %s)\n", totalEvents, formatValue(sumValues)); |
| 203 |
System.out.printf(Locale.ROOT, " tests.profile.mode=%s\n", mode); |
| 204 |
System.out.printf(Locale.ROOT, " tests.profile.count=%d\n", count); |
| 205 |
System.out.printf(Locale.ROOT, " tests.profile.stacksize=%d\n", stacksize); |
| 206 |
System.out.printf(Locale.ROOT, " tests.profile.linenumbers=%b\n", lineNumbers); |
| 207 |
System.out.printf(Locale.ROOT, "%s%sSTACK\n", pad("PERCENT"), pad(mode.toUpperCase(Locale.ROOT) + " SAMPLES")); |
| 208 |
List<SimpleEntry<String, Long>> entries = new ArrayList<>(histogram.values()); |
| 209 |
entries.sort((u, v) -> v.getValue().compareTo(u.getValue())); |
| 210 |
int seen = 0; |
| 211 |
for (SimpleEntry<String, Long> c : entries) { |
| 212 |
if (seen++ == count) { |
| 213 |
break; |
| 214 |
} |
| 215 |
String percent = String.format("%2.2f%%", 100 * (c.getValue() / (float) sumValues)); |
| 216 |
System.out.printf(Locale.ROOT, "%s%s%s\n", pad(percent), pad(formatValue(c.getValue())), c.getKey()); |
| 217 |
} |
| 218 |
} |
| 219 |
} |