src/jdk.jfr/share/classes/jdk/jfr/internal/MetadataRepository.java
branchJEP-349-branch
changeset 57360 5d043a159d5c
parent 52334 a181612f0715
child 57459 df39f8d8f4d6
--- a/src/jdk.jfr/share/classes/jdk/jfr/internal/MetadataRepository.java	Fri May 17 15:53:21 2019 +0200
+++ b/src/jdk.jfr/share/classes/jdk/jfr/internal/MetadataRepository.java	Fri May 17 16:02:27 2019 +0200
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -61,6 +61,8 @@
     private boolean staleMetadata = true;
     private boolean unregistered;
     private long lastUnloaded = -1;
+    private boolean flushMetadata;
+    private short flushCounter = 0;
 
     public MetadataRepository() {
         initializeJVMEventTypes();
@@ -142,6 +144,7 @@
         typeLibrary.addType(handler.getPlatformEventType());
         if (jvm.isRecording()) {
             storeDescriptorInJVM(); // needed for emergency dump
+            flushMetadata = true;
             settingsManager.setEventControl(handler.getEventControl());
             settingsManager.updateRetransform(Collections.singletonList((eventClass)));
         } else {
@@ -255,12 +258,12 @@
         staleMetadata = true;
     }
 
-    // Lock around setOutput ensures that other threads dosn't
-    // emit event after setOutput and unregister the event class, before a call
+    // Lock around setOutput ensures that other threads don't
+    // emit events after setOutput and unregister the event class, before a call
     // to storeDescriptorInJVM
-    synchronized void setOutput(String filename) {
+    synchronized void setOutput(String filename) {
         jvm.setOutput(filename);
-
+        flushMetadata = false;
         unregisterUnloaded();
         if (unregistered) {
             staleMetadata = typeLibrary.clearUnregistered();
@@ -269,6 +272,7 @@
         if (staleMetadata) {
             storeDescriptorInJVM();
         }
+        flushCounter = 0;
     }
 
     private void unregisterUnloaded() {
@@ -307,4 +311,11 @@
         throw new InternalError("Mirror class must have annotation " + MirrorEvent.class.getName());
     }
 
+    public synchronized void flush() {
+        jvm.flush(flushMetadata || flushCounter == 0, ++flushCounter);
+        if (flushCounter == Short.MAX_VALUE) {
+            flushCounter = 0;
+        }
+        this.flushMetadata = false;
+    }
 }