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