src/jdk.jfr/share/classes/jdk/jfr/internal/consumer/EventFileStream.java
changeset 58863 c16ac7a2eba4
child 59226 a0f39cc47387
equal deleted inserted replaced
58861:2c3cc4b01880 58863:c16ac7a2eba4
       
     1 /*
       
     2  * Copyright (c) 2019, 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 
       
    26 package jdk.jfr.internal.consumer;
       
    27 
       
    28 import java.io.IOException;
       
    29 import java.nio.file.Path;
       
    30 import java.security.AccessControlContext;
       
    31 import java.util.Arrays;
       
    32 import java.util.Comparator;
       
    33 import java.util.Objects;
       
    34 
       
    35 import jdk.jfr.consumer.RecordedEvent;
       
    36 import jdk.jfr.internal.consumer.Dispatcher;
       
    37 import jdk.jfr.internal.consumer.FileAccess;
       
    38 import jdk.jfr.internal.consumer.RecordingInput;
       
    39 
       
    40 /**
       
    41  * Implementation of an event stream that operates against a recording file.
       
    42  *
       
    43  */
       
    44 public final class EventFileStream extends AbstractEventStream {
       
    45     private final static Comparator<? super RecordedEvent> EVENT_COMPARATOR = JdkJfrConsumer.instance().eventComparator();
       
    46 
       
    47     private final RecordingInput input;
       
    48 
       
    49     private ChunkParser currentParser;
       
    50     private RecordedEvent[] cacheSorted;
       
    51 
       
    52     public EventFileStream(AccessControlContext acc, Path path) throws IOException {
       
    53         super(acc, false);
       
    54         Objects.requireNonNull(path);
       
    55         this.input = new RecordingInput(path.toFile(), FileAccess.UNPRIVILIGED);
       
    56     }
       
    57 
       
    58     @Override
       
    59     public void start() {
       
    60         start(0);
       
    61     }
       
    62 
       
    63     @Override
       
    64     public void startAsync() {
       
    65         startAsync(0);
       
    66     }
       
    67 
       
    68     @Override
       
    69     public void close() {
       
    70         setClosed(true);
       
    71         dispatcher().runCloseActions();
       
    72         try {
       
    73             input.close();
       
    74         } catch (IOException e) {
       
    75             // ignore
       
    76         }
       
    77     }
       
    78 
       
    79     @Override
       
    80     protected void process() throws IOException {
       
    81         Dispatcher disp = dispatcher();
       
    82         long start = 0;
       
    83         long end = Long.MAX_VALUE;
       
    84         if (disp.startTime != null) {
       
    85             start = disp.startNanos;
       
    86         }
       
    87         if (disp.endTime != null) {
       
    88             end = disp.endNanos;
       
    89         }
       
    90 
       
    91         currentParser = new ChunkParser(input, disp.parserConfiguration);
       
    92         while (!isClosed()) {
       
    93             if (currentParser.getStartNanos() > end) {
       
    94                 close();
       
    95                 return;
       
    96             }
       
    97             disp = dispatcher();
       
    98             disp.parserConfiguration.filterStart = start;
       
    99             disp.parserConfiguration.filterEnd = end;
       
   100             currentParser.updateConfiguration(disp.parserConfiguration, true);
       
   101             currentParser.setFlushOperation(getFlushOperation());
       
   102             if (disp.parserConfiguration.isOrdered()) {
       
   103                 processOrdered(disp);
       
   104             } else {
       
   105                 processUnordered(disp);
       
   106             }
       
   107             if (isClosed() || currentParser.isLastChunk()) {
       
   108                 return;
       
   109             }
       
   110             currentParser = currentParser.nextChunkParser();
       
   111         }
       
   112     }
       
   113 
       
   114     private void processOrdered(Dispatcher c) throws IOException {
       
   115         if (cacheSorted == null) {
       
   116             cacheSorted = new RecordedEvent[10_000];
       
   117         }
       
   118         RecordedEvent event;
       
   119         int index = 0;
       
   120         while (true) {
       
   121             event = currentParser.readEvent();
       
   122             if (event == null) {
       
   123                 Arrays.sort(cacheSorted, 0, index, EVENT_COMPARATOR);
       
   124                 for (int i = 0; i < index; i++) {
       
   125                     c.dispatch(cacheSorted[i]);
       
   126                 }
       
   127                 return;
       
   128             }
       
   129             if (index == cacheSorted.length) {
       
   130                 RecordedEvent[] tmp = cacheSorted;
       
   131                 cacheSorted = new RecordedEvent[2 * tmp.length];
       
   132                 System.arraycopy(tmp, 0, cacheSorted, 0, tmp.length);
       
   133             }
       
   134             cacheSorted[index++] = event;
       
   135         }
       
   136     }
       
   137 
       
   138     private void processUnordered(Dispatcher c) throws IOException {
       
   139         while (!isClosed()) {
       
   140             RecordedEvent event = currentParser.readEvent();
       
   141             if (event == null) {
       
   142                 return;
       
   143             }
       
   144             c.dispatch(event);
       
   145         }
       
   146     }
       
   147 }