src/jdk.jfr/share/classes/jdk/jfr/consumer/EventFileStream.java
author egahlin
Fri, 24 May 2019 19:39:31 +0200
branchJEP-349-branch
changeset 57372 50ca040843ea
parent 57361 53dccc90a5be
child 57374 41f0051285e0
permissions -rw-r--r--
Prepare infrastructure for multiple implementations of EventStream
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57361
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
     1
/*
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
     4
 *
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    10
 *
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    15
 * accompanied this code).
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    16
 *
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    20
 *
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    23
 * questions.
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    24
 */
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    25
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    26
package jdk.jfr.consumer;
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    27
57372
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    28
import java.io.IOException;
57361
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    29
import java.nio.file.Path;
57372
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    30
import java.security.AccessControlContext;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    31
import java.security.AccessController;
57361
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    32
import java.time.Duration;
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    33
import java.util.Objects;
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    34
import java.util.function.Consumer;
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    35
57372
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    36
import jdk.jfr.internal.consumer.EventConsumer;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    37
import jdk.jfr.internal.consumer.RecordingInput;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    38
57361
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    39
/**
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    40
 * Implementation of an event stream that operates against a recording file.
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    41
 *
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    42
 */
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    43
final class EventFileStream implements EventStream {
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    44
57372
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    45
    final static class FileEventConsumer extends EventConsumer {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    46
        private final RecordingInput input;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    47
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    48
        public FileEventConsumer(AccessControlContext acc, RecordingInput input) throws IOException {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    49
            super(acc);
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    50
            this.input = input;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    51
        }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    52
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    53
        @Override
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    54
        public void process() throws Exception {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    55
            // TODO This need more work; filter, multiple chunk etc
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    56
            ChunkParser cp = new ChunkParser(input);
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    57
            while (true) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    58
                RecordedEvent e = cp.readEvent();
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    59
                dispatch(e);
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    60
            }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    61
        }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    62
    }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    63
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    64
    private final RecordingInput input;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    65
    private final FileEventConsumer eventConsumer;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    66
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    67
    public EventFileStream(Path path) throws IOException {
57361
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    68
        Objects.requireNonNull(path);
57372
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    69
        input = new RecordingInput(path.toFile());
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    70
        eventConsumer = new FileEventConsumer(AccessController.getContext(), input);
57361
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    71
    }
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    72
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    73
    @Override
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    74
    public void onEvent(Consumer<RecordedEvent> action) {
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    75
        Objects.requireNonNull(action);
57372
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    76
        eventConsumer.onEvent(action);
57361
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    77
    }
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    78
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    79
    @Override
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    80
    public void onEvent(String eventName, Consumer<RecordedEvent> action) {
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    81
        Objects.requireNonNull(eventName);
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    82
        Objects.requireNonNull(action);
57372
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    83
        eventConsumer.onEvent(eventName, action);
57361
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    84
    }
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    85
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    86
    @Override
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    87
    public void onFlush(Runnable action) {
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    88
        Objects.requireNonNull(action);
57372
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    89
        eventConsumer.onFlush(action);
57361
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    90
    }
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    91
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    92
    @Override
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    93
    public void onClose(Runnable action) {
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    94
        Objects.requireNonNull(action);
57372
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    95
        eventConsumer.addCloseAction(action);
57361
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    96
    }
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    97
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    98
    @Override
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    99
    public void close() {
57372
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
   100
        eventConsumer.setClosed(true);
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
   101
        eventConsumer.runCloseActions();
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
   102
        try {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
   103
            input.close();
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
   104
        } catch (IOException e) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
   105
            // ignore
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
   106
        }
57361
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   107
    }
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   108
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   109
    @Override
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   110
    public boolean remove(Object action) {
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   111
        Objects.requireNonNull(action);
57372
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
   112
        return eventConsumer.remove(action);
57361
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   113
    }
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   114
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   115
    @Override
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   116
    public void start() {
57372
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
   117
        eventConsumer.start(0);
57361
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   118
    }
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   119
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   120
    @Override
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   121
    public void startAsync() {
57372
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
   122
        eventConsumer.startAsync(0);
57361
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   123
    }
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   124
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   125
    @Override
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   126
    public void awaitTermination(Duration timeout) {
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   127
        Objects.requireNonNull(timeout);
57372
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
   128
        eventConsumer.awaitTermination(timeout);
57361
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   129
    }
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   130
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   131
    @Override
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   132
    public void awaitTermination() {
57372
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
   133
        eventConsumer.awaitTermination();
57361
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   134
    }
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   135
}