src/jdk.jfr/share/classes/jdk/jfr/internal/consumer/AbstractEventStream.java
author mgronlun
Wed, 30 Oct 2019 19:43:52 +0100
changeset 58863 c16ac7a2eba4
child 59226 a0f39cc47387
permissions -rw-r--r--
8226511: Implement JFR Event Streaming Reviewed-by: egahlin, mseledtsov, mgronlun Contributed-by: erik.gahlin@oracle.com, mikhailo.seledtsov@oracle.com, markus.gronlund@oracle.com
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;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    43
import jdk.jfr.internal.SecuritySupport;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    44
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    45
/*
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    46
 * Purpose of this class is to simplify the implementation of
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    47
 * an event stream.
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    48
 */
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    49
abstract class AbstractEventStream implements EventStream {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    50
    private final static AtomicLong counter = new AtomicLong(1);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    51
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    52
    private final Object terminated = new Object();
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    53
    private final boolean active;
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();
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    57
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    58
    private volatile Thread thread;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    59
    private Dispatcher dispatcher;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    60
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    61
    private volatile boolean closed;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    62
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    63
    AbstractEventStream(AccessControlContext acc, boolean active) throws IOException {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    64
        this.accessControllerContext = Objects.requireNonNull(acc);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    65
        this.active = active;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    66
    }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    67
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    68
    @Override
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    69
    abstract public void start();
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    70
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    71
    @Override
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    72
    abstract public void startAsync();
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    73
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    74
    @Override
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    75
    abstract public void close();
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    76
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    77
    protected final Dispatcher dispatcher() {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    78
        if (configuration.hasChanged()) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    79
            synchronized (configuration) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    80
                dispatcher = new Dispatcher(configuration);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    81
            }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    82
        }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    83
        return dispatcher;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    84
    }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    85
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    86
    @Override
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    87
    public final void setOrdered(boolean ordered) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    88
        configuration.setOrdered(ordered);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    89
    }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    90
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    91
    @Override
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    92
    public final void setReuse(boolean reuse) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    93
        configuration.setReuse(reuse);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    94
    }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    95
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    96
    @Override
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    97
    public final void setStartTime(Instant startTime) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    98
        Objects.nonNull(startTime);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
    99
        synchronized (configuration) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   100
            if (configuration.started) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   101
                throw new IllegalStateException("Stream is already started");
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   102
            }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   103
            if (startTime.isBefore(Instant.EPOCH)) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   104
                startTime = Instant.EPOCH;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   105
            }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   106
            configuration.setStartTime(startTime);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   107
        }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   108
    }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   109
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   110
    @Override
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   111
    public final void setEndTime(Instant endTime) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   112
        Objects.requireNonNull(endTime);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   113
        synchronized (configuration) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   114
            if (configuration.started) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   115
                throw new IllegalStateException("Stream is already started");
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   116
            }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   117
            configuration.setEndTime(endTime);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   118
        }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   119
    }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   120
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   121
    @Override
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   122
    public final void onEvent(Consumer<RecordedEvent> action) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   123
        Objects.requireNonNull(action);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   124
        configuration.addEventAction(action);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   125
    }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   126
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   127
    @Override
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   128
    public final void onEvent(String eventName, Consumer<RecordedEvent> action) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   129
        Objects.requireNonNull(eventName);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   130
        Objects.requireNonNull(action);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   131
        configuration.addEventAction(eventName, action);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   132
    }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   133
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   134
    @Override
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   135
    public final void onFlush(Runnable action) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   136
        Objects.requireNonNull(action);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   137
        configuration.addFlushAction(action);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   138
    }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   139
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   140
    @Override
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   141
    public final void onClose(Runnable action) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   142
        Objects.requireNonNull(action);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   143
        configuration.addCloseAction(action);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   144
    }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   145
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   146
    @Override
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   147
    public final void onError(Consumer<Throwable> action) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   148
        Objects.requireNonNull(action);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   149
        configuration.addErrorAction(action);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   150
    }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   151
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   152
    @Override
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   153
    public final boolean remove(Object action) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   154
        Objects.requireNonNull(action);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   155
        return configuration.remove(action);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   156
    }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   157
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   158
    @Override
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   159
    public final void awaitTermination() throws InterruptedException {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   160
        awaitTermination(Duration.ofMillis(0));
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   161
    }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   162
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   163
    @Override
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   164
    public final void awaitTermination(Duration timeout) throws InterruptedException {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   165
        Objects.requireNonNull(timeout);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   166
        if (timeout.isNegative()) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   167
            throw new IllegalArgumentException("timeout value is negative");
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   168
        }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   169
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   170
        long base = System.currentTimeMillis();
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   171
        long now = 0;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   172
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   173
        long millis;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   174
        try {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   175
            millis = Math.multiplyExact(timeout.getSeconds(), 1000);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   176
        } catch (ArithmeticException a) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   177
            millis = Long.MAX_VALUE;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   178
        }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   179
        int nanos = timeout.toNanosPart();
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   180
        if (nanos == 0 && millis == 0) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   181
            synchronized (terminated) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   182
                while (!isClosed()) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   183
                    terminated.wait(0);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   184
                }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   185
            }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   186
        } else {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   187
            while (!isClosed()) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   188
                long delay = millis - now;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   189
                if (delay <= 0) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   190
                    break;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   191
                }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   192
                synchronized (terminated) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   193
                    terminated.wait(delay, nanos);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   194
                }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   195
                now = System.currentTimeMillis() - base;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   196
            }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   197
        }
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
    protected abstract void process() throws IOException;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   201
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   202
    protected final void setClosed(boolean closed) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   203
        this.closed = closed;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   204
    }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   205
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   206
    protected final boolean isClosed() {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   207
        return closed;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   208
    }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   209
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   210
    public final void startAsync(long startNanos) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   211
        startInternal(startNanos);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   212
        Runnable r = () -> run(accessControllerContext);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   213
        thread = SecuritySupport.createThreadWitNoPermissions(nextThreadName(), r);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   214
        thread.start();
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   215
    }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   216
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   217
    public final void start(long startNanos) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   218
        startInternal(startNanos);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   219
        thread = Thread.currentThread();
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   220
        run(accessControllerContext);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   221
    }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   222
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   223
    protected final Runnable getFlushOperation() {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   224
        return flushOperation;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   225
    }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   226
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   227
    private void startInternal(long startNanos) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   228
        synchronized (configuration) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   229
            if (configuration.started) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   230
                throw new IllegalStateException("Event stream can only be started once");
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   231
            }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   232
            if (active && configuration.startTime == null) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   233
                configuration.setStartNanos(startNanos);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   234
            }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   235
            configuration.setStarted(true);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   236
        }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   237
    }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   238
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   239
    private void execute() {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   240
        try {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   241
            process();
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   242
        } catch (IOException ioe) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   243
            // This can happen if a chunk file is removed, or
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   244
            // a file is access that has been closed
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   245
            // This is "normal" behavior for streaming and the
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   246
            // stream will be closed when this happens.
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   247
        } finally {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   248
            Logger.log(LogTag.JFR_SYSTEM_STREAMING, LogLevel.DEBUG, "Execution of stream ended.");
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   249
            try {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   250
                close();
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   251
            } finally {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   252
                synchronized (terminated) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   253
                    terminated.notifyAll();
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   254
                }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   255
            }
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
    private void run(AccessControlContext accessControlContext) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   260
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   261
            @Override
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   262
            public Void run() {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   263
                execute();
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   264
                return null;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   265
            }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   266
        }, accessControlContext);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   267
    }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   268
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   269
    private String nextThreadName() {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   270
        counter.incrementAndGet();
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   271
        return "JFR Event Stream " + counter;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   272
    }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents:
diff changeset
   273
}