Skip to content

Commit e841897

Browse files
committed
8319374: JFR: Remove instrumentation for exception events
Reviewed-by: mgronlun, alanb
1 parent cd9719b commit e841897

18 files changed

+234
-247
lines changed

src/java.base/share/classes/java/lang/Error.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1995, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1995, 2023, 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
@@ -25,6 +25,8 @@
2525

2626
package java.lang;
2727

28+
import jdk.internal.event.ThrowableTracer;
29+
2830
/**
2931
* An {@code Error} is a subclass of {@code Throwable}
3032
* that indicates serious problems that a reasonable application
@@ -53,6 +55,9 @@ public class Error extends Throwable {
5355
*/
5456
public Error() {
5557
super();
58+
if (Throwable.jfrTracing) {
59+
ThrowableTracer.traceError(getClass(), null);
60+
}
5661
}
5762

5863
/**
@@ -65,6 +70,9 @@ public Error() {
6570
*/
6671
public Error(String message) {
6772
super(message);
73+
if (Throwable.jfrTracing) {
74+
ThrowableTracer.traceError(getClass(), message);
75+
}
6876
}
6977

7078
/**
@@ -83,6 +91,9 @@ public Error(String message) {
8391
*/
8492
public Error(String message, Throwable cause) {
8593
super(message, cause);
94+
if (Throwable.jfrTracing) {
95+
ThrowableTracer.traceError(getClass(), message);
96+
}
8697
}
8798

8899
/**
@@ -100,6 +111,9 @@ public Error(String message, Throwable cause) {
100111
*/
101112
public Error(Throwable cause) {
102113
super(cause);
114+
if (Throwable.jfrTracing) {
115+
ThrowableTracer.traceError(getClass(), null);
116+
}
103117
}
104118

105119
/**
@@ -121,5 +135,8 @@ protected Error(String message, Throwable cause,
121135
boolean enableSuppression,
122136
boolean writableStackTrace) {
123137
super(message, cause, enableSuppression, writableStackTrace);
138+
if (Throwable.jfrTracing) {
139+
ThrowableTracer.traceError(getClass(), message);
140+
}
124141
}
125142
}

src/java.base/share/classes/java/lang/Throwable.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1994, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1994, 2023, 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
@@ -28,6 +28,7 @@
2828
import java.io.*;
2929
import java.util.*;
3030
import jdk.internal.access.SharedSecrets;
31+
import jdk.internal.event.ThrowableTracer;
3132
import jdk.internal.misc.InternalLock;
3233

3334
/**
@@ -118,6 +119,11 @@ public class Throwable implements Serializable {
118119
@java.io.Serial
119120
private static final long serialVersionUID = -3042686055658047285L;
120121

122+
/**
123+
* Flag that determines if exceptions should be traced by JFR
124+
*/
125+
static volatile boolean jfrTracing;
126+
121127
/**
122128
* The JVM saves some indication of the stack backtrace in this slot.
123129
*/
@@ -256,6 +262,9 @@ private static class SentinelHolder {
256262
*/
257263
public Throwable() {
258264
fillInStackTrace();
265+
if (jfrTracing) {
266+
ThrowableTracer.traceThrowable(getClass(), null);
267+
}
259268
}
260269

261270
/**
@@ -272,6 +281,9 @@ public Throwable() {
272281
public Throwable(String message) {
273282
fillInStackTrace();
274283
detailMessage = message;
284+
if (jfrTracing) {
285+
ThrowableTracer.traceThrowable(getClass(), message);
286+
}
275287
}
276288

277289
/**
@@ -295,6 +307,9 @@ public Throwable(String message, Throwable cause) {
295307
fillInStackTrace();
296308
detailMessage = message;
297309
this.cause = cause;
310+
if (jfrTracing) {
311+
ThrowableTracer.traceThrowable(getClass(), message);
312+
}
298313
}
299314

300315
/**
@@ -318,6 +333,9 @@ public Throwable(Throwable cause) {
318333
fillInStackTrace();
319334
detailMessage = (cause==null ? null : cause.toString());
320335
this.cause = cause;
336+
if (jfrTracing) {
337+
ThrowableTracer.traceThrowable(getClass(), null);
338+
}
321339
}
322340

323341
/**
@@ -370,8 +388,12 @@ protected Throwable(String message, Throwable cause,
370388
}
371389
detailMessage = message;
372390
this.cause = cause;
373-
if (!enableSuppression)
391+
if (!enableSuppression) {
374392
suppressedExceptions = null;
393+
}
394+
if (jfrTracing) {
395+
ThrowableTracer.traceThrowable(getClass(), message);
396+
}
375397
}
376398

377399
/**
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 2023, 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. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package jdk.internal.event;
26+
27+
/**
28+
* Event recording error exceptions.
29+
*/
30+
public final class ErrorThrownEvent extends Event {
31+
public String message;
32+
public Class<?> thrownClass;
33+
34+
public static void commit(long start, String message, Class<?> thrownClass) {
35+
// Generated by JFR
36+
}
37+
38+
public static boolean enabled() {
39+
// Generated by JFR
40+
return false;
41+
}
42+
43+
public static long timestamp() {
44+
// Generated by JFR
45+
return 0;
46+
}
47+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 2023, 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. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package jdk.internal.event;
26+
27+
/**
28+
* Event recording number of exceptions that has been created.
29+
*/
30+
public class ExceptionStatisticsEvent extends Event {
31+
32+
public long throwables;
33+
34+
public static void commit(long timestamp, long throwables) {
35+
// Generated by JFR
36+
}
37+
38+
public static boolean enabled() {
39+
// Generated by JFR
40+
return false;
41+
}
42+
43+
public static long timestamp() {
44+
// Generated by JFR
45+
return 0;
46+
}
47+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 2023, 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. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
package jdk.internal.event;
26+
27+
/**
28+
* Event recording all exception.
29+
*/
30+
public final class ExceptionThrownEvent extends Event {
31+
public String message;
32+
public Class<?> thrownClass;
33+
34+
public static void commit(long start, String message, Class<?> thrownClass) {
35+
// Generated by JFR
36+
}
37+
38+
public static boolean enabled() {
39+
// Generated by JFR
40+
return false;
41+
}
42+
43+
public static long timestamp() {
44+
// Generated by JFR
45+
return 0;
46+
}
47+
}

src/jdk.jfr/share/classes/jdk/jfr/internal/instrument/ThrowableTracer.java renamed to src/java.base/share/classes/jdk/internal/event/ThrowableTracer.java

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,47 +22,50 @@
2222
* or visit www.oracle.com if you need additional information or have any
2323
* questions.
2424
*/
25+
package jdk.internal.event;
2526

26-
package jdk.jfr.internal.instrument;
27-
27+
import java.lang.reflect.Field;
2828
import java.util.concurrent.atomic.AtomicLong;
2929

30-
import jdk.jfr.events.EventConfigurations;
31-
import jdk.jfr.events.ErrorThrownEvent;
32-
import jdk.jfr.events.ExceptionThrownEvent;
33-
import jdk.jfr.internal.event.EventConfiguration;
34-
30+
/**
31+
* Helper class for exception events.
32+
*/
3533
public final class ThrowableTracer {
3634

3735
private static final AtomicLong numThrowables = new AtomicLong();
3836

39-
public static void traceError(Error e, String message) {
40-
if (e instanceof OutOfMemoryError) {
37+
public static void enable() throws NoSuchFieldException, IllegalAccessException {
38+
Field field = Throwable.class.getDeclaredField("jfrTracing");
39+
field.setAccessible(true);
40+
field.setBoolean(null, true);
41+
}
42+
43+
public static void traceError(Class<?> clazz, String message) {
44+
if (OutOfMemoryError.class.isAssignableFrom(clazz)) {
4145
return;
4246
}
43-
long timestamp = EventConfiguration.timestamp();
4447

45-
EventConfiguration eventConfiguration1 = EventConfigurations.ERROR_THROWN;
46-
if (eventConfiguration1.isEnabled()) {
47-
ErrorThrownEvent.commit(timestamp, message, e.getClass());
48+
if (ErrorThrownEvent.enabled()) {
49+
long timestamp = ErrorThrownEvent.timestamp();
50+
ErrorThrownEvent.commit(timestamp, message, clazz);
4851
}
49-
EventConfiguration eventConfiguration2 = EventConfigurations.EXCEPTION_THROWN;
50-
if (eventConfiguration2.isEnabled()) {
51-
ExceptionThrownEvent.commit(timestamp, message, e.getClass());
52+
if (ExceptionThrownEvent.enabled()) {
53+
long timestamp = ExceptionThrownEvent.timestamp();
54+
ExceptionThrownEvent.commit(timestamp, message, clazz);
5255
}
5356
numThrowables.incrementAndGet();
5457
}
5558

56-
public static void traceThrowable(Throwable t, String message) {
57-
EventConfiguration eventConfiguration = EventConfigurations.EXCEPTION_THROWN;
58-
if (eventConfiguration.isEnabled()) {
59-
long timestamp = EventConfiguration.timestamp();
60-
ExceptionThrownEvent.commit(timestamp, message, t.getClass());
59+
public static void traceThrowable(Class<?> clazz, String message) {
60+
if (ExceptionThrownEvent.enabled()) {
61+
long timestamp = ExceptionThrownEvent.timestamp();
62+
ExceptionThrownEvent.commit(timestamp, message, clazz);
6163
}
6264
numThrowables.incrementAndGet();
6365
}
6466

65-
public static long numThrowables() {
66-
return numThrowables.get();
67+
public static void emitStatistics() {
68+
long timestamp = ExceptionStatisticsEvent.timestamp();
69+
ExceptionStatisticsEvent.commit(timestamp, numThrowables.get());
6770
}
6871
}

src/jdk.jfr/share/classes/jdk/jfr/events/ErrorThrownEvent.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,20 @@
2828
import jdk.jfr.Description;
2929
import jdk.jfr.Label;
3030
import jdk.jfr.Name;
31+
import jdk.jfr.internal.MirrorEvent;
3132
import jdk.jfr.internal.RemoveFields;
3233
import jdk.jfr.internal.Type;
3334

3435
@Name(Type.EVENT_NAME_PREFIX + "JavaErrorThrow")
3536
@Label("Java Error")
3637
@Category("Java Application")
3738
@Description("An object derived from java.lang.Error has been created. OutOfMemoryErrors are ignored")
39+
@MirrorEvent(className = "jdk.internal.event.ErrorThrownEvent")
3840
@RemoveFields("duration")
3941
public final class ErrorThrownEvent extends AbstractJDKEvent {
4042

41-
// The order of these fields must be the same as the parameters in
42-
// commit(..., String, Class)
43-
4443
@Label("Message")
4544
public String message;
4645

4746
@Label("Class")
48-
public Class<?> thrownClass;
49-
50-
public static void commit(long start, String message, Class<? extends Error> thrownClass) {
51-
// Generated
52-
}
53-
}
47+
public Class<?> thrownClass;}

0 commit comments

Comments
 (0)