< prev index next >

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

Print this page




  44 import java.util.LinkedHashMap;
  45 import java.util.LinkedList;
  46 import java.util.List;
  47 import java.util.Map;
  48 import java.util.StringJoiner;
  49 import java.util.TimerTask;
  50 import java.util.TreeMap;
  51 
  52 import jdk.jfr.Configuration;
  53 import jdk.jfr.FlightRecorderListener;
  54 import jdk.jfr.Recording;
  55 import jdk.jfr.RecordingState;
  56 import jdk.jfr.internal.SecuritySupport.SafePath;
  57 
  58 public final class PlatformRecording implements AutoCloseable {
  59 
  60     private final PlatformRecorder recorder;
  61     private final long id;
  62     // Recording settings
  63     private Map<String, String> settings = new LinkedHashMap<>();


  64     private Duration duration;
  65     private Duration maxAge;
  66     private long maxSize;
  67 
  68     private WriteableUserPath destination;
  69 
  70     private boolean toDisk = true;
  71     private String name;
  72     private boolean dumpOnExit;
  73     private SafePath dumpOnExitDirectory = new SafePath(".");
  74     // Timestamp information
  75     private Instant stopTime;
  76     private Instant startTime;
  77 
  78     // Misc, information
  79     private RecordingState state = RecordingState.NEW;
  80     private long size;
  81     private final LinkedList<RepositoryChunk> chunks = new LinkedList<>();
  82     private volatile Recording recording;
  83     private TimerTask stopTask;


 164 
 165         if (dest != null) {
 166             try {
 167                 dumpStopped(dest);
 168                 Logger.log(LogTag.JFR, LogLevel.INFO, "Wrote recording \"" + getName() + "\" (" + getId() + ") to " + dest.getText());
 169                 notifyIfStateChanged(newState, oldState);
 170                 close(); // remove if copied out
 171             } catch(IOException e) {
 172                 // throw e; // BUG8925030
 173             }
 174         } else {
 175             notifyIfStateChanged(newState, oldState);
 176         }
 177         return true;
 178     }
 179 
 180     public void scheduleStart(Duration delay) {
 181         synchronized (recorder) {
 182             ensureOkForSchedule();
 183 

 184             startTime = Instant.now().plus(delay);
 185             LocalDateTime now = LocalDateTime.now().plus(delay);
 186             setState(RecordingState.DELAYED);
 187             startTask = createStartTask();
 188             recorder.getTimer().schedule(startTask, delay.toMillis());
 189             Logger.log(LogTag.JFR, LogLevel.INFO, "Scheduled recording \"" + getName() + "\" (" + getId() + ") to start at " + now);
 190         }
 191     }
 192 
 193     private void ensureOkForSchedule() {
 194         if (getState() != RecordingState.NEW) {
 195             throw new IllegalStateException("Only a new recoridng can be scheduled for start");
 196         }
 197     }
 198 
 199     private TimerTask createStartTask() {
 200         // Taking ref. to recording here.
 201         // Opens up for memory leaks.
 202         return new TimerTask() {
 203             @Override


 205                 synchronized (recorder) {
 206                     if (getState() != RecordingState.DELAYED) {
 207                         return;
 208                     }
 209                     start();
 210                 }
 211             }
 212         };
 213     }
 214 
 215     void scheduleStart(Instant startTime) {
 216         synchronized (recorder) {
 217             ensureOkForSchedule();
 218             this.startTime = startTime;
 219             setState(RecordingState.DELAYED);
 220             startTask = createStartTask();
 221             recorder.getTimer().schedule(startTask, startTime.toEpochMilli());
 222         }
 223     }
 224 




 225     public Map<String, String> getSettings() {
 226         synchronized (recorder) {
 227             return settings;
 228         }
 229     }
 230 




 231     public long getSize() {
 232         return size;
 233     }
 234 
 235     public Instant getStopTime() {
 236         synchronized (recorder) {
 237             return stopTime;
 238         }
 239     }
 240 
 241     public Instant getStartTime() {
 242         synchronized (recorder) {
 243             return startTime;
 244         }
 245     }
 246 
 247     public Long getMaxSize() {
 248         synchronized (recorder) {
 249             return maxSize;
 250         }


 450 
 451     public void setSettings(Map<String, String> settings) {
 452         setSettings(settings, true);
 453     }
 454 
 455     private void setSettings(Map<String, String> settings, boolean update) {
 456         if (LogTag.JFR_SETTING.shouldLog(LogLevel.INFO.level) && update) {
 457             TreeMap<String, String> ordered = new TreeMap<>(settings);
 458             Logger.log(LogTag.JFR_SETTING, LogLevel.INFO, "New settings for recording \"" + getName() + "\" (" + getId() + ")");
 459             for (Map.Entry<String, String> entry : ordered.entrySet()) {
 460                 String text = entry.getKey() + "=\"" + entry.getValue() + "\"";
 461                 Logger.log(LogTag.JFR_SETTING, LogLevel.INFO, text);
 462             }
 463         }
 464         synchronized (recorder) {
 465             this.settings = new LinkedHashMap<>(settings);
 466             if (getState() == RecordingState.RUNNING && update) {
 467                 recorder.updateSettings();
 468             }
 469         }




 470     }
 471 
 472     private void notifyIfStateChanged(RecordingState newState, RecordingState oldState) {
 473         if (oldState == newState) {
 474             return;
 475         }
 476         for (FlightRecorderListener cl : PlatformRecorder.getListeners()) {
 477             try {
 478                 cl.recordingStateChanged(getRecording());
 479             } catch (RuntimeException re) {
 480                 Logger.log(JFR, WARN, "Error notifying recorder listener:" + re.getMessage());
 481             }
 482         }
 483     }
 484 
 485     public void setRecording(Recording recording) {
 486         this.recording = recording;
 487     }
 488 
 489     public Recording getRecording() {




  44 import java.util.LinkedHashMap;
  45 import java.util.LinkedList;
  46 import java.util.List;
  47 import java.util.Map;
  48 import java.util.StringJoiner;
  49 import java.util.TimerTask;
  50 import java.util.TreeMap;
  51 
  52 import jdk.jfr.Configuration;
  53 import jdk.jfr.FlightRecorderListener;
  54 import jdk.jfr.Recording;
  55 import jdk.jfr.RecordingState;
  56 import jdk.jfr.internal.SecuritySupport.SafePath;
  57 
  58 public final class PlatformRecording implements AutoCloseable {
  59 
  60     private final PlatformRecorder recorder;
  61     private final long id;
  62     // Recording settings
  63     private Map<String, String> settings = new LinkedHashMap<>();
  64     private List<String> configNames;
  65     private Duration delay;
  66     private Duration duration;
  67     private Duration maxAge;
  68     private long maxSize;
  69 
  70     private WriteableUserPath destination;
  71 
  72     private boolean toDisk = true;
  73     private String name;
  74     private boolean dumpOnExit;
  75     private SafePath dumpOnExitDirectory = new SafePath(".");
  76     // Timestamp information
  77     private Instant stopTime;
  78     private Instant startTime;
  79 
  80     // Misc, information
  81     private RecordingState state = RecordingState.NEW;
  82     private long size;
  83     private final LinkedList<RepositoryChunk> chunks = new LinkedList<>();
  84     private volatile Recording recording;
  85     private TimerTask stopTask;


 166 
 167         if (dest != null) {
 168             try {
 169                 dumpStopped(dest);
 170                 Logger.log(LogTag.JFR, LogLevel.INFO, "Wrote recording \"" + getName() + "\" (" + getId() + ") to " + dest.getText());
 171                 notifyIfStateChanged(newState, oldState);
 172                 close(); // remove if copied out
 173             } catch(IOException e) {
 174                 // throw e; // BUG8925030
 175             }
 176         } else {
 177             notifyIfStateChanged(newState, oldState);
 178         }
 179         return true;
 180     }
 181 
 182     public void scheduleStart(Duration delay) {
 183         synchronized (recorder) {
 184             ensureOkForSchedule();
 185 
 186             this.delay = delay;
 187             startTime = Instant.now().plus(delay);
 188             LocalDateTime now = LocalDateTime.now().plus(delay);
 189             setState(RecordingState.DELAYED);
 190             startTask = createStartTask();
 191             recorder.getTimer().schedule(startTask, delay.toMillis());
 192             Logger.log(LogTag.JFR, LogLevel.INFO, "Scheduled recording \"" + getName() + "\" (" + getId() + ") to start at " + now);
 193         }
 194     }
 195 
 196     private void ensureOkForSchedule() {
 197         if (getState() != RecordingState.NEW) {
 198             throw new IllegalStateException("Only a new recoridng can be scheduled for start");
 199         }
 200     }
 201 
 202     private TimerTask createStartTask() {
 203         // Taking ref. to recording here.
 204         // Opens up for memory leaks.
 205         return new TimerTask() {
 206             @Override


 208                 synchronized (recorder) {
 209                     if (getState() != RecordingState.DELAYED) {
 210                         return;
 211                     }
 212                     start();
 213                 }
 214             }
 215         };
 216     }
 217 
 218     void scheduleStart(Instant startTime) {
 219         synchronized (recorder) {
 220             ensureOkForSchedule();
 221             this.startTime = startTime;
 222             setState(RecordingState.DELAYED);
 223             startTask = createStartTask();
 224             recorder.getTimer().schedule(startTask, startTime.toEpochMilli());
 225         }
 226     }
 227 
 228     public Duration getDelay() {
 229         return delay;
 230     }
 231 
 232     public Map<String, String> getSettings() {
 233         synchronized (recorder) {
 234             return settings;
 235         }
 236     }
 237 
 238     public List<String> getConfigNames() {
 239         return configNames;
 240     }
 241 
 242     public long getSize() {
 243         return size;
 244     }
 245 
 246     public Instant getStopTime() {
 247         synchronized (recorder) {
 248             return stopTime;
 249         }
 250     }
 251 
 252     public Instant getStartTime() {
 253         synchronized (recorder) {
 254             return startTime;
 255         }
 256     }
 257 
 258     public Long getMaxSize() {
 259         synchronized (recorder) {
 260             return maxSize;
 261         }


 461 
 462     public void setSettings(Map<String, String> settings) {
 463         setSettings(settings, true);
 464     }
 465 
 466     private void setSettings(Map<String, String> settings, boolean update) {
 467         if (LogTag.JFR_SETTING.shouldLog(LogLevel.INFO.level) && update) {
 468             TreeMap<String, String> ordered = new TreeMap<>(settings);
 469             Logger.log(LogTag.JFR_SETTING, LogLevel.INFO, "New settings for recording \"" + getName() + "\" (" + getId() + ")");
 470             for (Map.Entry<String, String> entry : ordered.entrySet()) {
 471                 String text = entry.getKey() + "=\"" + entry.getValue() + "\"";
 472                 Logger.log(LogTag.JFR_SETTING, LogLevel.INFO, text);
 473             }
 474         }
 475         synchronized (recorder) {
 476             this.settings = new LinkedHashMap<>(settings);
 477             if (getState() == RecordingState.RUNNING && update) {
 478                 recorder.updateSettings();
 479             }
 480         }
 481     }
 482 
 483     public void setConfigNames(List<String> configNames) {
 484         this.configNames = configNames;
 485     }
 486 
 487     private void notifyIfStateChanged(RecordingState newState, RecordingState oldState) {
 488         if (oldState == newState) {
 489             return;
 490         }
 491         for (FlightRecorderListener cl : PlatformRecorder.getListeners()) {
 492             try {
 493                 cl.recordingStateChanged(getRecording());
 494             } catch (RuntimeException re) {
 495                 Logger.log(JFR, WARN, "Error notifying recorder listener:" + re.getMessage());
 496             }
 497         }
 498     }
 499 
 500     public void setRecording(Recording recording) {
 501         this.recording = recording;
 502     }
 503 
 504     public Recording getRecording() {


< prev index next >