58112
|
1 |
/*
|
|
2 |
* Copyright (c) 2019, 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 |
|
57859
|
24 |
package jdk.jfr.internal.dcmd;
|
|
25 |
import java.time.Duration;
|
|
26 |
import java.time.Instant;
|
|
27 |
import java.time.LocalDateTime;
|
|
28 |
import java.time.ZoneOffset;
|
|
29 |
import java.time.format.DateTimeFormatter;
|
|
30 |
import java.util.ArrayList;
|
|
31 |
import java.util.Collections;
|
|
32 |
import java.util.HashMap;
|
|
33 |
import java.util.Iterator;
|
|
34 |
import java.util.List;
|
|
35 |
import java.util.Map;
|
|
36 |
import java.util.Objects;
|
|
37 |
import java.util.StringJoiner;
|
|
38 |
|
|
39 |
import jdk.jfr.consumer.RecordedEvent;
|
|
40 |
import jdk.jfr.consumer.RecordedFrame;
|
|
41 |
import jdk.jfr.consumer.RecordedMethod;
|
|
42 |
import jdk.jfr.consumer.RecordedStackTrace;
|
|
43 |
import jdk.jfr.consumer.RecordingStream;
|
|
44 |
|
58151
|
45 |
// TODO: This file is to be deleted before integration
|
|
46 |
|
57859
|
47 |
/**
|
|
48 |
*
|
|
49 |
* HEALTH REPORT
|
|
50 |
*
|
|
51 |
* Example agent that shows how event streaming can be used to gather statistics
|
|
52 |
* of a running application.
|
|
53 |
*
|
|
54 |
* Usage:
|
|
55 |
*
|
|
56 |
* $ java -javaagent:health-report.jar[=interval=<interval>] MyApp
|
|
57 |
*
|
|
58 |
* where interval is how often statistics should be written to standard out.
|
|
59 |
*
|
|
60 |
* Example,
|
|
61 |
*
|
|
62 |
* $ java -javaagent:health-report.jar MyApp
|
|
63 |
*
|
|
64 |
* For testing purposes it is also possible to just run Main.java and it will
|
|
65 |
* run an allocation loop
|
|
66 |
*
|
|
67 |
* $ java Main.java
|
|
68 |
*
|
|
69 |
*/
|
|
70 |
public final class Health {
|
|
71 |
|
|
72 |
private static final String TEMPLATE =
|
|
73 |
"=================== HEALTH REPORT === $FLUSH_TIME ====================\n" +
|
|
74 |
"| GC: $GC_NAME Phys. memory: $PHYSIC_MEM Alloc Rate: $ALLOC_RATE |\n" +
|
|
75 |
"| OC Count : $OC_COUNT Initial Heap: $INIT_HEAP Total Alloc: $TOT_ALLOC |\n" +
|
|
76 |
"| OC Pause Avg: $OC_AVG Used Heap : $USED_HEAP Thread Count: $THREADS |\n" +
|
|
77 |
"| OC Pause Max: $OC_MAX Commit. Heap: $COM_HEAP Class Count : $CLASSES |\n" +
|
|
78 |
"| YC Count : $YC_COUNT CPU Machine : $MACH_CPU Safepoints: $SAFEPOINTS |\n" +
|
|
79 |
"| YC Pause Avg: $YC_AVG CPU JVM User : $USR_CPU Max Safepoint: $MAX_SAFE |\n" +
|
|
80 |
"| YC Pause Max: $YC_MAX CPU JVM System: $SYS_CPU Max Comp. Time: $MAX_COM |\n" +
|
|
81 |
"|--- Top Allocation Methods ------------------------------- -----------------|\n" +
|
|
82 |
"| $ALLOCACTION_TOP_FRAME $AL_PE |\n" +
|
|
83 |
"| $ALLOCACTION_TOP_FRAME $AL_PE |\n" +
|
|
84 |
"| $ALLOCACTION_TOP_FRAME $AL_PE |\n" +
|
|
85 |
"| $ALLOCACTION_TOP_FRAME $AL_PE |\n" +
|
|
86 |
"| $ALLOCACTION_TOP_FRAME $AL_PE |\n" +
|
|
87 |
"|--- Hot Methods ------------------------------------------------------------|\n" +
|
|
88 |
"| $EXECUTION_TOP_FRAME $EX_PE |\n" +
|
|
89 |
"| $EXECUTION_TOP_FRAME $EX_PE |\n" +
|
|
90 |
"| $EXECUTION_TOP_FRAME $EX_PE |\n" +
|
|
91 |
"| $EXECUTION_TOP_FRAME $EX_PE |\n" +
|
|
92 |
"| $EXECUTION_TOP_FRAME $EX_PE |\n" +
|
|
93 |
"==============================================================================\n";
|
|
94 |
|
|
95 |
public final static Field FLUSH_TIME = new Field();
|
|
96 |
|
|
97 |
public final static Field GC_NAME = new Field();
|
|
98 |
public final static Field OC_COUNT= new Field(Option.COUNT);
|
|
99 |
public final static Field OC_AVG = new Field(Option.AVERAGE, Option.DURATION);
|
|
100 |
public final static Field OC_MAX = new Field(Option.MAX, Option.DURATION);
|
|
101 |
public final static Field YC_COUNT = new Field(Option.COUNT);
|
|
102 |
public final static Field YC_AVG = new Field(Option.AVERAGE, Option.DURATION);
|
|
103 |
public final static Field YC_MAX= new Field(Option.MAX, Option.DURATION);
|
|
104 |
|
|
105 |
public final static Field PHYSIC_MEM = new Field(Option.BYTES);
|
|
106 |
public final static Field INIT_HEAP = new Field(Option.BYTES);
|
|
107 |
public final static Field USED_HEAP = new Field(Option.BYTES);
|
|
108 |
public final static Field COM_HEAP = new Field(Option.BYTES);
|
|
109 |
public final static Field MACH_CPU = new Field(Option.PERCENTAGE);
|
|
110 |
public final static Field USR_CPU= new Field(Option.PERCENTAGE);
|
|
111 |
public final static Field SYS_CPU = new Field(Option.PERCENTAGE);
|
|
112 |
|
|
113 |
public final static Field ALLOC_RATE = new Field(Option.BYTES_PER_SECOND);
|
|
114 |
public final static Field TOT_ALLOC = new Field(Option.TOTAL, Option.BYTES);
|
|
115 |
public final static Field THREADS = new Field();
|
|
116 |
public final static Field CLASSES = new Field();
|
|
117 |
public final static Field SAFEPOINTS = new Field(Option.COUNT);
|
|
118 |
public final static Field MAX_SAFE = new Field(Option.MAX, Option.DURATION);
|
|
119 |
public final static Field MAX_COM = new Field(Option.MAX, Option.DURATION);
|
|
120 |
|
|
121 |
public final static Field ALLOCACTION_TOP_FRAME = new Field();
|
|
122 |
public final static Field AL_PE = new Field(Option.NORMALIZED, Option.TOTAL);
|
|
123 |
|
|
124 |
public final static Field EXECUTION_TOP_FRAME = new Field();
|
|
125 |
public final static Field EX_PE = new Field(Option.NORMALIZED, Option.COUNT);
|
|
126 |
|
|
127 |
private static RecordingStream rs;
|
|
128 |
|
|
129 |
public static void start() {
|
|
130 |
Duration duration = Duration.ofSeconds(1);
|
|
131 |
|
|
132 |
rs = new RecordingStream();
|
|
133 |
// Event configuration
|
|
134 |
rs.enable("jdk.CPULoad").withPeriod(duration);
|
|
135 |
rs.enable("jdk.YoungGarbageCollection").withoutThreshold();
|
|
136 |
rs.enable("jdk.OldGarbageCollection").withoutThreshold();
|
|
137 |
rs.enable("jdk.GCHeapSummary").withPeriod(duration);
|
|
138 |
rs.enable("jdk.PhysicalMemory").withPeriod(duration);
|
|
139 |
rs.enable("jdk.GCConfiguration").withPeriod(duration);
|
|
140 |
rs.enable("jdk.SafepointBegin");
|
|
141 |
rs.enable("jdk.SafepointEnd");
|
|
142 |
rs.enable("jdk.ObjectAllocationOutsideTLAB").withStackTrace();
|
|
143 |
rs.enable("jdk.ObjectAllocationInNewTLAB").withStackTrace();
|
|
144 |
rs.enable("jdk.ExecutionSample").withPeriod(Duration.ofMillis(10)).withStackTrace();
|
|
145 |
rs.enable("jdk.JavaThreadStatistics").withPeriod(duration);
|
|
146 |
rs.enable("jdk.ClassLoadingStatistics").withPeriod(duration);
|
|
147 |
rs.enable("jdk.Compilation").withoutThreshold();
|
|
148 |
rs.enable("jdk.GCHeapConfiguration").withPeriod(duration);
|
|
149 |
rs.enable("jdk.Flush").withoutThreshold();
|
|
150 |
|
|
151 |
// Dispatch handlers
|
|
152 |
rs.onEvent("jdk.CPULoad", Health::onCPULoad);
|
|
153 |
rs.onEvent("jdk.YoungGarbageCollection", Health::onYoungColletion);
|
|
154 |
rs.onEvent("jdk.OldGarbageCollection", Health::onOldCollection);
|
|
155 |
rs.onEvent("jdk.GCHeapSummary", Health::onGCHeapSummary);
|
|
156 |
rs.onEvent("jdk.PhysicalMemory", Health::onPhysicalMemory);
|
|
157 |
rs.onEvent("jdk.GCConfiguration", Health::onGCConfiguration);
|
|
158 |
rs.onEvent("jdk.SafepointBegin", Health::onSafepointBegin);
|
|
159 |
rs.onEvent("jdk.SafepointEnd", Health::onSafepointEnd);
|
|
160 |
rs.onEvent("jdk.ObjectAllocationOutsideTLAB", Health::onObjectAllocationOutsideTLAB);
|
|
161 |
rs.onEvent("jdk.ObjectAllocationInNewTLAB", Health::onObjectAllocationInNewTLAB);
|
|
162 |
rs.onEvent("jdk.ExecutionSample", Health::onExecutionSample);
|
|
163 |
rs.onEvent("jdk.JavaThreadStatistics", Health::onJavaThreadStatistics);
|
|
164 |
rs.onEvent("jdk.ClassLoadingStatistics", Health::onClassLoadingStatistics);
|
|
165 |
rs.onEvent("jdk.Compilation", Health::onCompilation);
|
|
166 |
rs.onEvent("jdk.GCHeapConfiguration", Health::onGCHeapConfiguration);
|
|
167 |
rs.onEvent("jdk.Flush", Health::onFlushpoint);
|
|
168 |
|
|
169 |
rs.onFlush(Health::printReport);
|
|
170 |
rs.startAsync();
|
|
171 |
System.out.println("Health started");
|
|
172 |
}
|
|
173 |
|
|
174 |
private static void onCPULoad(RecordedEvent event) {
|
|
175 |
MACH_CPU.addSample(event.getDouble("machineTotal"));
|
|
176 |
SYS_CPU.addSample(event.getDouble("jvmSystem"));
|
|
177 |
USR_CPU.addSample(event.getDouble("jvmUser"));
|
|
178 |
}
|
|
179 |
|
|
180 |
private static void onYoungColletion(RecordedEvent event) {
|
|
181 |
long nanos = event.getDuration().toNanos();
|
|
182 |
YC_COUNT.addSample(nanos);
|
|
183 |
YC_MAX.addSample(nanos);
|
|
184 |
YC_AVG.addSample(nanos);
|
|
185 |
}
|
|
186 |
|
|
187 |
private static void onOldCollection(RecordedEvent event) {
|
|
188 |
long nanos = event.getDuration().toNanos();
|
|
189 |
OC_COUNT.addSample(nanos);
|
|
190 |
OC_MAX.addSample(nanos);
|
|
191 |
OC_AVG.addSample(nanos);
|
|
192 |
}
|
|
193 |
|
|
194 |
private static void onGCHeapSummary(RecordedEvent event) {
|
|
195 |
USED_HEAP.addSample(event.getLong("heapUsed"));
|
|
196 |
COM_HEAP.addSample(event.getLong("heapSpace.committedSize"));
|
|
197 |
}
|
|
198 |
|
|
199 |
private static void onPhysicalMemory(RecordedEvent event) {
|
|
200 |
PHYSIC_MEM.addSample(event.getLong("totalSize"));
|
|
201 |
}
|
|
202 |
|
|
203 |
private static void onCompilation(RecordedEvent event) {
|
|
204 |
MAX_COM.addSample(event.getDuration().toNanos());
|
|
205 |
}
|
|
206 |
|
|
207 |
private static void onGCConfiguration(RecordedEvent event) {
|
|
208 |
String gc = event.getString("oldCollector");
|
|
209 |
String yc = event.getString("youngCollector");
|
|
210 |
if (yc != null) {
|
|
211 |
gc += "/" + yc;
|
|
212 |
}
|
|
213 |
GC_NAME.addSample(gc);
|
|
214 |
}
|
|
215 |
|
|
216 |
private final static Map<Long, Instant> safepointBegin = new HashMap<>();
|
|
217 |
|
|
218 |
private static void onSafepointBegin(RecordedEvent event) {
|
|
219 |
safepointBegin.put(event.getValue("safepointId"), event.getEndTime());
|
|
220 |
}
|
|
221 |
|
|
222 |
private static void onSafepointEnd(RecordedEvent event) {
|
|
223 |
long id = event.getValue("safepointId");
|
|
224 |
Instant begin = safepointBegin.get(id);
|
|
225 |
if (begin != null) {
|
|
226 |
long nanos = Duration.between(begin, event.getEndTime()).toNanos();
|
|
227 |
safepointBegin.remove(id);
|
|
228 |
SAFEPOINTS.addSample(nanos);
|
|
229 |
MAX_SAFE.addSample(nanos);
|
|
230 |
}
|
|
231 |
}
|
|
232 |
|
|
233 |
private static void onObjectAllocationOutsideTLAB(RecordedEvent event) {
|
|
234 |
onAllocationSample(event, event.getLong("allocationSize"));
|
|
235 |
}
|
|
236 |
|
|
237 |
private static void onObjectAllocationInNewTLAB(RecordedEvent event) {
|
|
238 |
onAllocationSample(event, event.getLong("tlabSize"));
|
|
239 |
}
|
|
240 |
|
|
241 |
private static double totalAllocated;
|
|
242 |
private static long firstAllocationTime = -1;
|
|
243 |
private static void onAllocationSample(RecordedEvent event, long size) {
|
|
244 |
String topFrame = topFrame(event.getStackTrace());
|
|
245 |
if (topFrame != null) {
|
|
246 |
ALLOCACTION_TOP_FRAME.addSample(topFrame, size);
|
|
247 |
AL_PE.addSample(topFrame, size);
|
|
248 |
}
|
|
249 |
TOT_ALLOC.addSample(size);
|
|
250 |
// ALLOC_RATE.addRate(timetsamp, amount);
|
|
251 |
long timestamp = event.getEndTime().toEpochMilli();
|
|
252 |
totalAllocated += size;
|
|
253 |
if (firstAllocationTime > 0) {
|
|
254 |
long elapsedTime = timestamp - firstAllocationTime;
|
|
255 |
if (elapsedTime > 0) {
|
|
256 |
double rate = 1000.0 * (totalAllocated / elapsedTime);
|
|
257 |
ALLOC_RATE.addSample(rate);
|
|
258 |
}
|
|
259 |
} else {
|
|
260 |
firstAllocationTime = timestamp;
|
|
261 |
}
|
|
262 |
}
|
|
263 |
|
|
264 |
private static void onExecutionSample(RecordedEvent event) {
|
|
265 |
String topFrame = topFrame(event.getStackTrace());
|
|
266 |
EXECUTION_TOP_FRAME.addSample(topFrame, 1);
|
|
267 |
EX_PE.addSample(topFrame, 1);
|
|
268 |
}
|
|
269 |
|
|
270 |
private static void onJavaThreadStatistics(RecordedEvent event) {
|
|
271 |
THREADS.addSample(event.getDouble("activeCount"));
|
|
272 |
}
|
|
273 |
|
|
274 |
private static void onClassLoadingStatistics(RecordedEvent event) {
|
|
275 |
long diff = event.getLong("loadedClassCount") - event.getLong("unloadedClassCount");
|
|
276 |
CLASSES.addSample(diff);
|
|
277 |
}
|
|
278 |
|
|
279 |
private static void onGCHeapConfiguration(RecordedEvent event) {
|
|
280 |
INIT_HEAP.addSample(event.getLong("initialSize"));
|
|
281 |
}
|
|
282 |
|
|
283 |
private final static DateTimeFormatter FORMATTER =
|
|
284 |
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
285 |
private static void onFlushpoint(RecordedEvent event) {
|
|
286 |
Instant i = event.getEndTime();
|
|
287 |
LocalDateTime l = LocalDateTime.ofInstant(i, ZoneOffset.systemDefault());
|
|
288 |
FLUSH_TIME.addSample(FORMATTER.format(l));
|
|
289 |
}
|
|
290 |
|
|
291 |
// # # # TEMPLATE AND SAMPLING # # #
|
|
292 |
|
|
293 |
private enum Option {
|
|
294 |
BYTES, PERCENTAGE, DURATION, BYTES_PER_SECOND, NORMALIZED, COUNT, AVERAGE, TOTAL, MAX
|
|
295 |
}
|
|
296 |
|
|
297 |
private static final class Record {
|
|
298 |
private final Object key;
|
|
299 |
private int count = 1;
|
|
300 |
private double total;
|
|
301 |
private double max;
|
|
302 |
private Object value = null;
|
|
303 |
public Record(Object key, String sample) {
|
|
304 |
this.key = key;
|
|
305 |
this.value = sample;
|
|
306 |
}
|
|
307 |
|
|
308 |
public Record(Object object, double sample) {
|
|
309 |
this.key = object;
|
|
310 |
this.value = sample;
|
|
311 |
this.max = sample;
|
|
312 |
this.total = sample;
|
|
313 |
}
|
|
314 |
|
|
315 |
public long getCount() {
|
|
316 |
return count;
|
|
317 |
}
|
|
318 |
|
|
319 |
public double getAverage() {
|
|
320 |
return total / count;
|
|
321 |
}
|
|
322 |
|
|
323 |
public double getMax() {
|
|
324 |
return max;
|
|
325 |
}
|
|
326 |
|
|
327 |
public double getTotal() {
|
|
328 |
return total;
|
|
329 |
}
|
|
330 |
|
|
331 |
public int hashCode() {
|
|
332 |
return key == null ? 0 : key.hashCode();
|
|
333 |
}
|
|
334 |
|
|
335 |
public boolean equals(Object o) {
|
|
336 |
if (o instanceof Record) {
|
|
337 |
Record that = (Record) o;
|
|
338 |
return Objects.equals(that.key, this.key);
|
|
339 |
}
|
|
340 |
return false;
|
|
341 |
}
|
|
342 |
}
|
|
343 |
|
|
344 |
private static final class Field implements Iterable<Record> {
|
|
345 |
private final HashMap<Object, Record> histogram = new HashMap<>();
|
|
346 |
private final Option[] options;
|
|
347 |
private double norm;
|
|
348 |
|
|
349 |
public Field(Option... options) {
|
|
350 |
this.options = options;
|
|
351 |
}
|
|
352 |
|
|
353 |
public void addSample(double sample) {
|
|
354 |
addSample(this, sample);
|
|
355 |
}
|
|
356 |
|
|
357 |
public void addSample(String sample) {
|
|
358 |
histogram.merge(this, new Record(this, sample), (a, b) -> {
|
|
359 |
a.count++;
|
|
360 |
a.value = sample;
|
|
361 |
return a;
|
|
362 |
});
|
|
363 |
}
|
|
364 |
|
|
365 |
public void addSample(Object key, double sample) {
|
|
366 |
histogram.merge(key, new Record(key, sample), (a, b) -> {
|
|
367 |
a.count++;
|
|
368 |
a.total += sample;
|
|
369 |
a.value = sample;
|
|
370 |
a.max = Math.max(a.max, sample);
|
|
371 |
return a;
|
|
372 |
});
|
|
373 |
}
|
|
374 |
|
|
375 |
public boolean hasOption(Option option) {
|
|
376 |
for (Option o : options) {
|
|
377 |
if (o == option) {
|
|
378 |
return true;
|
|
379 |
}
|
|
380 |
}
|
|
381 |
return false;
|
|
382 |
}
|
|
383 |
|
|
384 |
@Override
|
|
385 |
public Iterator<Record> iterator() {
|
|
386 |
List<Record> records = new ArrayList<>(histogram.values());
|
|
387 |
Collections.sort(records, (a, b) -> Long.compare(b.getCount(), a.getCount()));
|
|
388 |
if (hasOption(Option.TOTAL)) {
|
|
389 |
Collections.sort(records, (a, b) -> Double.compare(b.getTotal(), a.getTotal()));
|
|
390 |
}
|
|
391 |
if (hasOption(Option.NORMALIZED)) {
|
|
392 |
norm = 0.0;
|
|
393 |
for (Record r : records) {
|
|
394 |
if (hasOption(Option.TOTAL)) {
|
|
395 |
norm += r.getTotal();
|
|
396 |
}
|
|
397 |
if (hasOption(Option.COUNT)) {
|
|
398 |
norm += r.getCount();
|
|
399 |
}
|
|
400 |
}
|
|
401 |
}
|
|
402 |
return records.iterator();
|
|
403 |
}
|
|
404 |
|
|
405 |
public double getNorm() {
|
|
406 |
return norm;
|
|
407 |
}
|
|
408 |
}
|
|
409 |
|
|
410 |
private static void printReport() {
|
|
411 |
try {
|
|
412 |
StringBuilder template = new StringBuilder(TEMPLATE);
|
|
413 |
for (java.lang.reflect.Field f : Health.class.getDeclaredFields()) {
|
|
414 |
String variable = "$" + f.getName();
|
|
415 |
if (f.getType() == Field.class) {
|
|
416 |
writeParam(template, variable, (Field) f.get(null));
|
|
417 |
}
|
|
418 |
}
|
|
419 |
System.out.println(template.toString());
|
|
420 |
} catch (Exception e) {
|
|
421 |
e.printStackTrace();
|
|
422 |
}
|
|
423 |
}
|
|
424 |
|
|
425 |
private static void writeParam(StringBuilder template, String variable, Field param) {
|
|
426 |
Iterator<Record> it = param.iterator();
|
|
427 |
int lastIndex = 0;
|
|
428 |
while (true) {
|
|
429 |
int index = template.indexOf(variable, lastIndex);
|
|
430 |
if (index == -1) {
|
|
431 |
return;
|
|
432 |
}
|
|
433 |
lastIndex = index + 1;
|
|
434 |
Record record = it.hasNext() ? it.next() : null;
|
|
435 |
Object value = null;
|
|
436 |
if (record != null) {
|
|
437 |
value = (record.key == param) ? record.value : record.key;
|
|
438 |
}
|
|
439 |
if (value != null) {
|
|
440 |
if (param.hasOption(Option.MAX)) {
|
|
441 |
value = record.getMax();
|
|
442 |
}
|
|
443 |
if (param.hasOption(Option.COUNT)) {
|
|
444 |
value = record.getCount();
|
|
445 |
}
|
|
446 |
if (param.hasOption(Option.AVERAGE)) {
|
|
447 |
value = record.getAverage();
|
|
448 |
}
|
|
449 |
if (param.hasOption(Option.TOTAL)) {
|
|
450 |
value = record.getTotal();
|
|
451 |
}
|
|
452 |
}
|
|
453 |
if (param.hasOption(Option.COUNT)) {
|
|
454 |
value = value == null ? 0 : value;
|
|
455 |
}
|
|
456 |
if (param.hasOption(Option.BYTES)) {
|
|
457 |
value = formatBytes((Number) value);
|
|
458 |
}
|
|
459 |
if (param.hasOption(Option.DURATION)) {
|
|
460 |
value = formatDuration((Number) value);
|
|
461 |
}
|
|
462 |
if (param.hasOption(Option.BYTES_PER_SECOND)) {
|
|
463 |
if (value != null) {
|
|
464 |
value = formatBytes((Number) value) + "/s";
|
|
465 |
}
|
|
466 |
}
|
|
467 |
if (param.hasOption(Option.NORMALIZED)) {
|
|
468 |
if (value != null) {
|
|
469 |
double d = ((Number) value).doubleValue() / param.getNorm();
|
|
470 |
value = formatPercentage(d);
|
|
471 |
}
|
|
472 |
}
|
|
473 |
if (param.hasOption(Option.PERCENTAGE)) {
|
|
474 |
value = formatPercentage((Number) value);
|
|
475 |
}
|
|
476 |
String text;
|
|
477 |
if (value == null) {
|
|
478 |
text = record == null ? "" : "N/A";
|
|
479 |
} else {
|
|
480 |
text = String.valueOf(value);
|
|
481 |
}
|
|
482 |
int length = Math.max(text.length(), variable.length());
|
|
483 |
for (int i = 0; i < length; i++) {
|
|
484 |
char c = i < text.length() ? text.charAt(i) : ' ';
|
|
485 |
template.setCharAt(index + i, c);
|
|
486 |
}
|
|
487 |
}
|
|
488 |
}
|
|
489 |
|
|
490 |
// # # # FORMATTING # # #
|
|
491 |
|
|
492 |
enum TimespanUnit {
|
|
493 |
NANOSECONDS("ns", 1000), MICROSECONDS("us", 1000), MILLISECONDS("ms", 1000), SECONDS("s", 60), MINUTES("m", 60),
|
|
494 |
HOURS("h", 24), DAYS("d", 7);
|
|
495 |
|
|
496 |
final String text;
|
|
497 |
final long amount;
|
|
498 |
|
|
499 |
TimespanUnit(String unit, long amount) {
|
|
500 |
this.text = unit;
|
|
501 |
this.amount = amount;
|
|
502 |
}
|
|
503 |
}
|
|
504 |
|
|
505 |
private static String formatDuration(Number value) {
|
|
506 |
if (value == null) {
|
|
507 |
return "N/A";
|
|
508 |
}
|
|
509 |
double t = value.doubleValue();
|
|
510 |
TimespanUnit result = TimespanUnit.NANOSECONDS;
|
|
511 |
for (TimespanUnit unit : TimespanUnit.values()) {
|
|
512 |
result = unit;
|
|
513 |
if (t < 1000) {
|
|
514 |
break;
|
|
515 |
}
|
|
516 |
t = t / unit.amount;
|
|
517 |
}
|
|
518 |
return String.format("%.1f %s", t, result.text);
|
|
519 |
}
|
|
520 |
|
|
521 |
private static String formatPercentage(Number value) {
|
|
522 |
if (value == null) {
|
|
523 |
return "N/A";
|
|
524 |
}
|
|
525 |
return String.format("%6.2f %%", value.doubleValue() * 100);
|
|
526 |
}
|
|
527 |
|
|
528 |
private static String formatBytes(Number value) {
|
|
529 |
if (value == null) {
|
|
530 |
return "N/A";
|
|
531 |
}
|
|
532 |
long bytes = value.longValue();
|
|
533 |
if (bytes >= 1024 * 1024l) {
|
|
534 |
return bytes / (1024 * 1024L) + " MB";
|
|
535 |
}
|
|
536 |
if (bytes >= 1024) {
|
|
537 |
return bytes / 1024 + " kB";
|
|
538 |
}
|
|
539 |
return bytes + " bytes";
|
|
540 |
}
|
|
541 |
|
|
542 |
private static String topFrame(RecordedStackTrace stackTrace) {
|
|
543 |
if (stackTrace == null) {
|
|
544 |
return null;
|
|
545 |
}
|
|
546 |
List<RecordedFrame> frames = stackTrace.getFrames();
|
|
547 |
if (!frames.isEmpty()) {
|
|
548 |
RecordedFrame topFrame = frames.get(0);
|
|
549 |
if (topFrame.isJavaFrame()) {
|
|
550 |
return formatMethod(topFrame.getMethod());
|
|
551 |
}
|
|
552 |
}
|
|
553 |
return null;
|
|
554 |
}
|
|
555 |
|
|
556 |
private static String formatMethod(RecordedMethod m) {
|
|
557 |
StringBuilder sb = new StringBuilder();
|
|
558 |
String typeName = m.getType().getName();
|
|
559 |
typeName = typeName.substring(typeName.lastIndexOf('.') + 1);
|
|
560 |
sb.append(typeName).append(".").append(m.getName());
|
|
561 |
sb.append("(");
|
|
562 |
StringJoiner sj = new StringJoiner(", ");
|
|
563 |
String md = m.getDescriptor().replace("/", ".");
|
|
564 |
String parameter = md.substring(1, md.lastIndexOf(")"));
|
|
565 |
for (String qualifiedName : decodeDescriptors(parameter)) {
|
|
566 |
sj.add(qualifiedName.substring(qualifiedName.lastIndexOf('.') + 1));
|
|
567 |
}
|
|
568 |
sb.append(sj.length() > 10 ? "..." : sj);
|
|
569 |
sb.append(")");
|
|
570 |
return sb.toString();
|
|
571 |
}
|
|
572 |
|
|
573 |
private static List<String> decodeDescriptors(String descriptor) {
|
|
574 |
List<String> descriptors = new ArrayList<>();
|
|
575 |
for (int index = 0; index < descriptor.length(); index++) {
|
|
576 |
String arrayBrackets = "";
|
|
577 |
while (descriptor.charAt(index) == '[') {
|
|
578 |
arrayBrackets += "[]";
|
|
579 |
index++;
|
|
580 |
}
|
|
581 |
char c = descriptor.charAt(index);
|
|
582 |
String type;
|
|
583 |
switch (c) {
|
|
584 |
case 'L':
|
|
585 |
int endIndex = descriptor.indexOf(';', index);
|
|
586 |
type = descriptor.substring(index + 1, endIndex);
|
|
587 |
index = endIndex;
|
|
588 |
break;
|
|
589 |
case 'I':
|
|
590 |
type = "int";
|
|
591 |
break;
|
|
592 |
case 'J':
|
|
593 |
type = "long";
|
|
594 |
break;
|
|
595 |
case 'Z':
|
|
596 |
type = "boolean";
|
|
597 |
break;
|
|
598 |
case 'D':
|
|
599 |
type = "double";
|
|
600 |
break;
|
|
601 |
case 'F':
|
|
602 |
type = "float";
|
|
603 |
break;
|
|
604 |
case 'S':
|
|
605 |
type = "short";
|
|
606 |
break;
|
|
607 |
case 'C':
|
|
608 |
type = "char";
|
|
609 |
break;
|
|
610 |
case 'B':
|
|
611 |
type = "byte";
|
|
612 |
break;
|
|
613 |
default:
|
|
614 |
type = "<unknown-descriptor-type>";
|
|
615 |
}
|
|
616 |
descriptors.add(type + arrayBrackets);
|
|
617 |
}
|
|
618 |
return descriptors;
|
|
619 |
}
|
|
620 |
}
|
|
621 |
|