src/jdk.jfr/share/classes/jdk/jfr/internal/consumer/AbstractEventStream.java
author egahlin
Fri, 29 Nov 2019 17:31:01 +0100
changeset 59327 2c3578aa0bdf
parent 59310 72f3dd43dd28
permissions -rw-r--r--
8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice() Reviewed-by: mgronlun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58863
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
     1
/*
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
     4
 *
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    10
 *
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    15
 * accompanied this code).
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    16
 *
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    20
 *
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    23
 * questions.
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    24
 */
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    25
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    26
package jdk.jfr.internal.consumer;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    27
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    28
import java.io.IOException;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    29
import java.security.AccessControlContext;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    30
import java.security.AccessController;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    31
import java.security.PrivilegedAction;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    32
import java.time.Duration;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    33
import java.time.Instant;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    34
import java.util.Objects;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    35
import java.util.concurrent.atomic.AtomicLong;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    36
import java.util.function.Consumer;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    37
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    38
import jdk.jfr.consumer.EventStream;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    39
import jdk.jfr.consumer.RecordedEvent;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    40
import jdk.jfr.internal.LogLevel;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    41
import jdk.jfr.internal.LogTag;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    42
import jdk.jfr.internal.Logger;
59226
a0f39cc47387 8233700: EventStream not closed
egahlin
parents: 58863
diff changeset
    43
import jdk.jfr.internal.PlatformRecording;
58863
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    44
import jdk.jfr.internal.SecuritySupport;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    45
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    46
/*
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    47
 * Purpose of this class is to simplify the implementation of
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    48
 * an event stream.
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    49
 */
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    50
abstract class AbstractEventStream implements EventStream {
59310
72f3dd43dd28 8234888: EventStream::close doesn't abort streaming thread
egahlin
parents: 59226
diff changeset
    51
    private final static AtomicLong counter = new AtomicLong(0);
58863
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    52
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    53
    private final Object terminated = new Object();
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    54
    private final Runnable flushOperation = () -> dispatcher().runFlushActions();
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    55
    private final AccessControlContext accessControllerContext;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    56
    private final StreamConfiguration configuration = new StreamConfiguration();
59226
a0f39cc47387 8233700: EventStream not closed
egahlin
parents: 58863
diff changeset
    57
    private final PlatformRecording recording;
58863
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    58
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    59
    private volatile Thread thread;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    60
    private Dispatcher dispatcher;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    61
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    62
    private volatile boolean closed;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    63
59226
a0f39cc47387 8233700: EventStream not closed
egahlin
parents: 58863
diff changeset
    64
    AbstractEventStream(AccessControlContext acc, PlatformRecording recording) throws IOException {
58863
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    65
        this.accessControllerContext = Objects.requireNonNull(acc);
59226
a0f39cc47387 8233700: EventStream not closed
egahlin
parents: 58863
diff changeset
    66
        this.recording = recording;
58863
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    67
    }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    68
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    69
    @Override
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    70
    abstract public void start();
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    71
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    72
    @Override
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    73
    abstract public void startAsync();
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    74
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    75
    @Override
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    76
    abstract public void close();
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    77
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    78
    protected final Dispatcher dispatcher() {
59327
2c3578aa0bdf 8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents: 59310
diff changeset
    79
        if (configuration.hasChanged()) { // quick check
58863
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    80
            synchronized (configuration) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    81
                dispatcher = new Dispatcher(configuration);
59327
2c3578aa0bdf 8234671: JFR api/consumer/recordingstream/TestStart.java failed due to timeout at testStartTwice()
egahlin
parents: 59310
diff changeset
    82
                configuration.setChanged(false);
58863
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    83
            }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    84
        }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    85
        return dispatcher;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    86
    }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    87
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    88
    @Override
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    89
    public final void setOrdered(boolean ordered) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    90
        configuration.setOrdered(ordered);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    91
    }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    92
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    93
    @Override
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    94
    public final void setReuse(boolean reuse) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    95
        configuration.setReuse(reuse);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    96
    }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    97
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    98
    @Override
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    99
    public final void setStartTime(Instant startTime) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   100
        Objects.nonNull(startTime);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   101
        synchronized (configuration) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   102
            if (configuration.started) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   103
                throw new IllegalStateException("Stream is already started");
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   104
            }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   105
            if (startTime.isBefore(Instant.EPOCH)) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   106
                startTime = Instant.EPOCH;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   107
            }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   108
            configuration.setStartTime(startTime);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   109
        }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   110
    }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   111
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   112
    @Override
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   113
    public final void setEndTime(Instant endTime) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   114
        Objects.requireNonNull(endTime);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   115
        synchronized (configuration) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   116
            if (configuration.started) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   117
                throw new IllegalStateException("Stream is already started");
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   118
            }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   119
            configuration.setEndTime(endTime);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   120
        }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   121
    }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   122
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   123
    @Override
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   124
    public final void onEvent(Consumer<RecordedEvent> action) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   125
        Objects.requireNonNull(action);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   126
        configuration.addEventAction(action);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   127
    }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   128
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   129
    @Override
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   130
    public final void onEvent(String eventName, Consumer<RecordedEvent> action) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   131
        Objects.requireNonNull(eventName);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   132
        Objects.requireNonNull(action);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   133
        configuration.addEventAction(eventName, action);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   134
    }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   135
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   136
    @Override
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   137
    public final void onFlush(Runnable action) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   138
        Objects.requireNonNull(action);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   139
        configuration.addFlushAction(action);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   140
    }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   141
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   142
    @Override
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   143
    public final void onClose(Runnable action) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   144
        Objects.requireNonNull(action);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   145
        configuration.addCloseAction(action);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   146
    }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   147
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   148
    @Override
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   149
    public final void onError(Consumer<Throwable> action) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   150
        Objects.requireNonNull(action);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   151
        configuration.addErrorAction(action);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   152
    }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   153
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   154
    @Override
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   155
    public final boolean remove(Object action) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   156
        Objects.requireNonNull(action);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   157
        return configuration.remove(action);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   158
    }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   159
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   160
    @Override
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   161
    public final void awaitTermination() throws InterruptedException {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   162
        awaitTermination(Duration.ofMillis(0));
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   163
    }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   164
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   165
    @Override
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   166
    public final void awaitTermination(Duration timeout) throws InterruptedException {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   167
        Objects.requireNonNull(timeout);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   168
        if (timeout.isNegative()) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   169
            throw new IllegalArgumentException("timeout value is negative");
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   170
        }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   171
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   172
        long base = System.currentTimeMillis();
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   173
        long now = 0;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   174
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   175
        long millis;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   176
        try {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   177
            millis = Math.multiplyExact(timeout.getSeconds(), 1000);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   178
        } catch (ArithmeticException a) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   179
            millis = Long.MAX_VALUE;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   180
        }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   181
        int nanos = timeout.toNanosPart();
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   182
        if (nanos == 0 && millis == 0) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   183
            synchronized (terminated) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   184
                while (!isClosed()) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   185
                    terminated.wait(0);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   186
                }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   187
            }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   188
        } else {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   189
            while (!isClosed()) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   190
                long delay = millis - now;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   191
                if (delay <= 0) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   192
                    break;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   193
                }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   194
                synchronized (terminated) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   195
                    terminated.wait(delay, nanos);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   196
                }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   197
                now = System.currentTimeMillis() - base;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   198
            }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   199
        }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   200
    }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   201
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   202
    protected abstract void process() throws IOException;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   203
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   204
    protected final void setClosed(boolean closed) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   205
        this.closed = closed;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   206
    }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   207
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   208
    protected final boolean isClosed() {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   209
        return closed;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   210
    }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   211
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   212
    public final void startAsync(long startNanos) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   213
        startInternal(startNanos);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   214
        Runnable r = () -> run(accessControllerContext);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   215
        thread = SecuritySupport.createThreadWitNoPermissions(nextThreadName(), r);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   216
        thread.start();
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   217
    }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   218
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   219
    public final void start(long startNanos) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   220
        startInternal(startNanos);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   221
        thread = Thread.currentThread();
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   222
        run(accessControllerContext);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   223
    }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   224
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   225
    protected final Runnable getFlushOperation() {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   226
        return flushOperation;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   227
    }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   228
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   229
    private void startInternal(long startNanos) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   230
        synchronized (configuration) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   231
            if (configuration.started) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   232
                throw new IllegalStateException("Event stream can only be started once");
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   233
            }
59226
a0f39cc47387 8233700: EventStream not closed
egahlin
parents: 58863
diff changeset
   234
            if (recording != null && configuration.startTime == null) {
58863
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   235
                configuration.setStartNanos(startNanos);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   236
            }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   237
            configuration.setStarted(true);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   238
        }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   239
    }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   240
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   241
    private void execute() {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   242
        try {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   243
            process();
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   244
        } catch (IOException ioe) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   245
            // This can happen if a chunk file is removed, or
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   246
            // a file is access that has been closed
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   247
            // This is "normal" behavior for streaming and the
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   248
            // stream will be closed when this happens.
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   249
        } finally {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   250
            Logger.log(LogTag.JFR_SYSTEM_STREAMING, LogLevel.DEBUG, "Execution of stream ended.");
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   251
            try {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   252
                close();
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   253
            } finally {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   254
                synchronized (terminated) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   255
                    terminated.notifyAll();
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   256
                }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   257
            }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   258
        }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   259
    }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   260
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   261
    private void run(AccessControlContext accessControlContext) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   262
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   263
            @Override
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   264
            public Void run() {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   265
                execute();
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   266
                return null;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   267
            }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   268
        }, accessControlContext);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   269
    }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   270
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   271
    private String nextThreadName() {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   272
        counter.incrementAndGet();
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   273
        return "JFR Event Stream " + counter;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   274
    }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   275
}