Skip to content

Commit 6670884

Browse files
committed
Rework context capturing concept
1 parent d5fd724 commit 6670884

File tree

7 files changed

+99
-16
lines changed

7 files changed

+99
-16
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,4 @@
1010
@Target(ElementType.TYPE)
1111
@Retention(RetentionPolicy.RUNTIME)
1212
public @interface ContextAware {
13-
boolean value() default true;
1413
}

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
package jdk.jfr;
22

3+
import java.io.Closeable;
4+
import java.lang.invoke.MethodHandles;
5+
import java.lang.invoke.VarHandle;
6+
import java.lang.invoke.MethodHandles.Lookup;
7+
import java.lang.reflect.Constructor;
8+
import java.lang.reflect.Field;
9+
import java.lang.reflect.InvocationTargetException;
10+
import java.util.ArrayList;
11+
import java.util.List;
312
import java.util.stream.Stream;
413

514
import jdk.jfr.internal.context.BaseContextType;
@@ -10,5 +19,40 @@ public static interface Registration {
1019
Stream<Class<? extends ContextType>> types();
1120
}
1221

22+
public static final class Captured<T extends ContextType.Capturable<T>> implements Closeable, AutoCloseable {
23+
private final T parent, current;
24+
25+
Captured(T parent, T current) {
26+
assert parent != null;
27+
assert current != null;
28+
this.parent = parent;
29+
this.current = current;
30+
}
31+
32+
@Override
33+
public void close() {
34+
parent.set();
35+
}
36+
37+
public T get() {
38+
return current;
39+
}
40+
41+
public Captured<T> capture() {
42+
return current.capture();
43+
}
44+
}
45+
46+
public static abstract class Capturable<T extends Capturable<T>> extends ContextType {
47+
public Capturable() {}
48+
49+
@SuppressWarnings("unchecked")
50+
public final Captured<T> capture() {
51+
return new Captured<T>((T) this, snapshot());
52+
}
53+
54+
abstract protected T snapshot();
55+
}
56+
1357
protected ContextType() {}
1458
}

src/jdk.jfr/share/classes/jdk/jfr/internal/EventInstrumentation.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -192,15 +192,11 @@ boolean isEnabled() {
192192
}
193193

194194
boolean isContextAware() {
195-
Boolean result = annotationValue(classNode, ANNOTATION_CONTEXT_AWARE_DESCRIPTOR, Boolean.class, Boolean.TRUE);
196-
if (result != null) {
197-
return result.booleanValue();
195+
if (hasAnnotation(classNode, ANNOTATION_CONTEXT_AWARE_DESCRIPTOR)) {
196+
return true;
198197
}
199198
if (superClass != null) {
200-
ContextAware e = superClass.getAnnotation(ContextAware.class);
201-
if (e != null) {
202-
return e.value();
203-
}
199+
return superClass.getAnnotation(ContextAware.class) != null;
204200
}
205201
return false;
206202
}
@@ -236,6 +232,17 @@ private static <T> T annotationValue(ClassNode classNode, String typeDescriptor,
236232
return null;
237233
}
238234

235+
private static boolean hasAnnotation(ClassNode classNode, String typeDescriptor) {
236+
if (classNode.visibleAnnotations != null) {
237+
for (AnnotationNode a : classNode.visibleAnnotations) {
238+
if (typeDescriptor.equals(a.desc)) {
239+
return true;
240+
}
241+
}
242+
}
243+
return false;
244+
}
245+
239246
private static List<SettingInfo> buildSettingInfos(Class<?> superClass, ClassNode classNode) {
240247
Set<String> methodSet = new HashSet<>();
241248
List<SettingInfo> settingInfos = new ArrayList<>();

src/jdk.jfr/share/classes/jdk/jfr/internal/TypeLibrary.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ public static synchronized Type createType(Class<?> clazz, List<AnnotationElemen
268268

269269
if (eventType) {
270270
ContextAware ctxAware = clazz.getAnnotation(ContextAware.class);
271-
addImplicitFields(type, true, true, true, true , false, ctxAware != null && ctxAware.value());
271+
addImplicitFields(type, true, true, true, true , false, ctxAware != null);
272272
addUserFields(clazz, type, dynamicFields);
273273
type.trimFields();
274274
}

src/jdk.jfr/share/classes/jdk/jfr/internal/context/BaseContextType.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
package jdk.jfr.internal.context;
22

3-
import java.nio.LongBuffer;
4-
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
5-
6-
import jdk.jfr.ContextType;
73
import jdk.jfr.FlightRecorder;
84
import jdk.jfr.internal.JVM;
95
import jdk.jfr.internal.PlatformRecorder;
10-
import jdk.jfr.internal.context.ContextWriter;
116

127
public abstract class BaseContextType {
138
public final void set() {

test/micro/org/openjdk/bench/jdk/jfr/JfrContextBenchmark.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import jdk.jfr.ContextType;
1111
import jdk.jfr.FlightRecorder;
1212
import jdk.jfr.Recording;
13+
import jdk.jfr.ContextType.Captured;
1314

1415
@BenchmarkMode(Mode.AverageTime)
1516
@Warmup(iterations = 10, time = 500, timeUnit = TimeUnit.MILLISECONDS)
@@ -32,7 +33,6 @@ public class JfrContextBenchmark {
3233
}
3334
if (recordJFR) {
3435
Recording r = new Recording();
35-
// r.setDestination(Paths.get("/tmp/bench.jfr"));
3636
r.start();
3737
}
3838
}
@@ -48,4 +48,23 @@ public void operationWithContext(Blackhole bh) throws IOException {
4848
ctx.unset();
4949
bh.consume(ctx);
5050
}
51+
52+
@Benchmark
53+
@Threads(2)
54+
public void operationWithCapturedContext(Blackhole bh) throws IOException {
55+
try (Captured<TracingJfrContext> ctxCapture = new TracingJfrContext().capture()) {
56+
ctxCapture.get().withValues("trace-1", "span-1").set();
57+
TracedEvent.commit(10);
58+
try (Captured<TracingJfrContext> ctx1Capture = ctxCapture.capture()) {
59+
TracingJfrContext ctx1 = ctx1Capture.get();
60+
ctx1.spanid = "span-2";
61+
ctx1.set();
62+
TracedEvent.commit(20);
63+
bh.consume(ctx1);
64+
}
65+
// this should be within context [traceid=trace-1, spanid=span-1]
66+
TracedEvent.commit(30);
67+
}
68+
69+
}
5170
}

test/micro/org/openjdk/bench/jdk/jfr/TracingJfrContext.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,29 @@
44
import jdk.jfr.Name;
55

66
@Name("tracing_context")
7-
public class TracingJfrContext extends ContextType {
7+
public final class TracingJfrContext extends ContextType.Capturable<TracingJfrContext> {
88
@Name("traceid")
99
public String traceid;
1010

1111
@Name("spanid")
1212
public String spanid;
13+
14+
public TracingJfrContext withValues(String traceid, String spanid) {
15+
this.traceid = traceid;
16+
this.spanid = spanid;
17+
return this;
18+
}
19+
20+
@Override
21+
protected TracingJfrContext snapshot() {
22+
return new TracingJfrContext(traceid, spanid);
23+
}
24+
25+
public TracingJfrContext(String traceid, String spanid) {
26+
this.traceid = traceid;
27+
this.spanid = spanid;
28+
}
29+
30+
public TracingJfrContext() {}
31+
1332
}

0 commit comments

Comments
 (0)