Adapt Java event commit() method for streaming JEP-349-branch
authoregahlin
Mon, 05 Aug 2019 23:57:47 +0200
branchJEP-349-branch
changeset 57641 5fb8ececb9e6
parent 57640 46a77fccd251
child 57690 9316d02dd4a5
Adapt Java event commit() method for streaming
src/jdk.jfr/share/classes/jdk/jfr/internal/EventInstrumentation.java
--- a/src/jdk.jfr/share/classes/jdk/jfr/internal/EventInstrumentation.java	Mon Aug 05 22:40:46 2019 +0200
+++ b/src/jdk.jfr/share/classes/jdk/jfr/internal/EventInstrumentation.java	Mon Aug 05 23:57:47 2019 +0200
@@ -121,6 +121,7 @@
     private final boolean untypedEventHandler;
     private boolean guardHandlerReference;
     private Class<?> superClass;
+    private final static boolean streamingCommit = !SecuritySupport.getBooleanProperty("jfr.instrument.streaming");
 
     EventInstrumentation(Class<?> superClass, byte[] bytes, long id) {
         this.superClass = superClass;
@@ -364,7 +365,39 @@
         });
 
         // MyEvent#commit() - Java event writer
-        updateMethod(METHOD_COMMIT, methodVisitor -> {
+        if (streamingCommit) {
+            updateMethod(METHOD_COMMIT, methodVisitor -> {
+                // if (shouldCommit()) {
+                methodVisitor.visitCode();
+                methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
+                methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, getInternalClassName(), METHOD_EVENT_SHOULD_COMMIT.getName(), METHOD_EVENT_SHOULD_COMMIT.getDescriptor(), false);
+                Label end = new Label();
+                methodVisitor.visitJumpInsn(Opcodes.IFEQ, end);
+                methodVisitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
+                methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
+                //   startTime = EventHandler.timestamp() - duration;
+                methodVisitor.visitMethodInsn(Opcodes.INVOKESTATIC, TYPE_EVENT_HANDLER.getInternalName(), METHOD_TIME_STAMP.getName(),
+                        METHOD_TIME_STAMP.getDescriptor(), false);
+                methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
+                methodVisitor.visitFieldInsn(Opcodes.GETFIELD, getInternalClassName(), FIELD_DURATION, "J");
+                methodVisitor.visitInsn(Opcodes.LSUB);
+                methodVisitor.visitFieldInsn(Opcodes.PUTFIELD, getInternalClassName(), FIELD_START_TIME, "J");
+                //   eventHandlerX.write(...);
+                getEventHandler(methodVisitor);
+                methodVisitor.visitTypeInsn(Opcodes.CHECKCAST, eventHandlerXInternalName);
+                for (FieldInfo fi : fieldInfos) {
+                    methodVisitor.visitVarInsn(Opcodes.ALOAD, 0);
+                    methodVisitor.visitFieldInsn(Opcodes.GETFIELD, fi.internalClassName, fi.fieldName, fi.fieldDescriptor);
+                }
+                methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL, eventHandlerXInternalName, writeMethod.getName(), writeMethod.getDescriptor(), false);
+                // }
+                methodVisitor.visitLabel(end);
+                methodVisitor.visitFrame(Opcodes.F_SAME, 0, null, 0, null);
+                methodVisitor.visitInsn(Opcodes.RETURN);
+                methodVisitor.visitEnd();
+            });
+        } else {
+            updateMethod(METHOD_COMMIT, methodVisitor -> {
                 // if (!isEnable()) {
                 // return;
                 // }
@@ -431,6 +464,7 @@
                 methodVisitor.visitInsn(Opcodes.RETURN);
                 methodVisitor.visitEnd();
             });
+        }
 
         // MyEvent#shouldCommit()
         updateMethod(METHOD_EVENT_SHOULD_COMMIT, methodVisitor -> {