Skip to content

Commit 4d6668e

Browse files
committed
8294242: JFR: jfr print doesn't handle infinite duration well
Reviewed-by: mgronlun
1 parent 5a9cd33 commit 4d6668e

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

src/jdk.jfr/share/classes/jdk/jfr/Timespan.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -32,6 +32,8 @@
3232

3333
/**
3434
* Event field annotation, specifies that the value is a duration.
35+
* <p>
36+
* If the annotated value equals {@code Long.MAX_VALUE), it represents forever.
3537
*
3638
* @since 9
3739
*/

src/jdk.jfr/share/classes/jdk/jfr/consumer/RecordedObject.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.time.Duration;
3232
import java.time.Instant;
3333
import java.time.OffsetDateTime;
34+
import java.time.temporal.ChronoUnit;
3435
import java.util.Comparator;
3536
import java.util.List;
3637
import java.util.Objects;
@@ -755,6 +756,10 @@ public final String getString(String name) {
755756
* the following types: {@code long}, {@code int}, {@code short}, {@code char},
756757
* and {@code byte}.
757758
* <p>
759+
* If the committed event value was {@code Long.MAX_VALUE},
760+
* regardless of the unit set by {@code @Timespan}, this method returns
761+
* {@link ChronoUnit.FOREVER.getDuration()}.
762+
* <p>
758763
* It's possible to index into a nested object using {@code "."} (for example,
759764
* {@code "aaa.bbb"}).
760765
* <p>
@@ -811,6 +816,9 @@ private Duration getDuration(long timespan, String name) {
811816
if (timespan == Long.MIN_VALUE) {
812817
return Duration.ofSeconds(Long.MIN_VALUE, 0);
813818
}
819+
if (timespan == Long.MAX_VALUE) {
820+
return ChronoUnit.FOREVER.getDuration();
821+
}
814822
Timespan ts = v.getAnnotation(Timespan.class);
815823
if (ts != null) {
816824
switch (ts.value()) {

src/jdk.jfr/share/classes/jdk/jfr/internal/tool/PrettyWriter.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.time.Duration;
3030
import java.time.OffsetDateTime;
3131
import java.time.format.DateTimeFormatter;
32+
import java.time.temporal.ChronoUnit;
3233
import java.util.ArrayList;
3334
import java.util.List;
3435
import java.util.StringJoiner;
@@ -553,6 +554,10 @@ private boolean printFormatted(ValueDescriptor field, Object value) {
553554
println("N/A");
554555
return true;
555556
}
557+
if (d.equals(ChronoUnit.FOREVER.getDuration())) {
558+
println("Forever");
559+
return true;
560+
}
556561
println(Utils.formatDuration(d));
557562
return true;
558563
}

test/jdk/jdk/jfr/api/consumer/TestRecordedObject.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -26,6 +26,7 @@
2626
import java.io.IOException;
2727
import java.time.Duration;
2828
import java.time.Instant;
29+
import java.time.temporal.ChronoUnit;
2930
import java.util.Arrays;
3031
import java.util.HashSet;
3132
import java.util.List;
@@ -92,6 +93,12 @@ static final class EventWithValues extends Event {
9293
@Timespan(Timespan.SECONDS)
9394
long durationSeconds = DURATION_VALUE.toSeconds();
9495

96+
@Timespan(Timespan.SECONDS)
97+
long foreverMillis = Long.MAX_VALUE;
98+
99+
@Timespan(Timespan.NANOSECONDS)
100+
long foreverNanoseconds = Long.MAX_VALUE;
101+
95102
@Timestamp(Timestamp.MILLISECONDS_SINCE_EPOCH)
96103
long instantMillis = 1000;
97104

@@ -179,6 +186,9 @@ private static void testTimeUnits(RecordedObject event) {
179186
Asserts.assertEquals(event.getDuration("durationMicros"), DURATION_VALUE);
180187
Asserts.assertEquals(event.getDuration("durationMillis"), DURATION_VALUE);
181188
Asserts.assertEquals(event.getDuration("durationSeconds"), DURATION_VALUE);
189+
Asserts.assertEquals(event.getDuration("foreverMillis"), ChronoUnit.FOREVER.getDuration());
190+
Asserts.assertEquals(event.getDuration("foreverNanoseconds"), ChronoUnit.FOREVER.getDuration());
191+
182192
Asserts.assertEquals(event.getInstant("instantMillis").toEpochMilli(), 1000L);
183193
if (!event.getInstant("instantTicks").isBefore(INSTANT_VALUE)) {
184194
throw new AssertionError("Expected start time of JVM to before call to Instant.now()");

0 commit comments

Comments
 (0)