src/jdk.jfr/share/classes/jdk/jfr/consumer/AbstractEventStream.java
author egahlin
Thu, 01 Aug 2019 16:48:26 +0200
branchJEP-349-branch
changeset 57614 b49f5c13baa7
parent 57604 838f9a7635b6
child 57628 f5f590eaecf5
permissions -rw-r--r--
Cleaning up
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
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    26
package jdk.jfr.consumer;
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.lang.invoke.VarHandle;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    30
import java.security.AccessControlContext;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    31
import java.security.AccessController;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    32
import java.security.PrivilegedAction;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    33
import java.time.Duration;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    34
import java.time.Instant;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    35
import java.util.ArrayList;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    36
import java.util.Arrays;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    37
import java.util.Comparator;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    38
import java.util.List;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    39
import java.util.Objects;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    40
import java.util.function.Consumer;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    41
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    42
import jdk.jfr.EventType;
57614
b49f5c13baa7 Cleaning up
egahlin
parents: 57604
diff changeset
    43
import jdk.jfr.internal.JVM;
57604
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    44
import jdk.jfr.internal.LogLevel;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    45
import jdk.jfr.internal.LogTag;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    46
import jdk.jfr.internal.Logger;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    47
import jdk.jfr.internal.LongMap;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    48
import jdk.jfr.internal.consumer.InternalEventFilter;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    49
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    50
/*
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    51
 * Purpose of this class is to simplify the implementation of
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    52
 * an event stream. In particular, it handles:
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    53
 *
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    54
 * - configuration storage
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    55
 * - atomic updates to a configuration
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    56
 * - dispatch mechanism
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    57
 * - error handling
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    58
 * - security
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    59
 *
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    60
 */
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    61
abstract class AbstractEventStream implements Runnable {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    62
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    63
    public static final class StreamConfiguration {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    64
        private static final Runnable[] NO_ACTIONS = new Runnable[0];
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    65
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    66
        private Runnable[] flushActions = NO_ACTIONS;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    67
        private Runnable[] closeActions = NO_ACTIONS;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    68
        private EventDispatcher[] dispatchers = NO_DISPATCHERS;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    69
        private InternalEventFilter eventFilter = InternalEventFilter.ACCEPT_ALL;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    70
        private boolean closed = false;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    71
        private boolean reuse = true;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    72
        private boolean ordered = true;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    73
        private Instant startTime = null;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    74
        private boolean started = false;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    75
        private long startNanos = 0;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    76
        private LongMap<EventDispatcher[]> dispatcherLookup = new LongMap<>();
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    77
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    78
        private boolean changed = false;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    79
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    80
        public StreamConfiguration(StreamConfiguration configuration) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    81
            this.flushActions = configuration.flushActions;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    82
            this.closeActions = configuration.closeActions;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    83
            this.dispatchers = configuration.dispatchers;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    84
            this.eventFilter = configuration.eventFilter;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    85
            this.closed = configuration.closed;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    86
            this.reuse = configuration.reuse;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    87
            this.ordered = configuration.ordered;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    88
            this.startTime = configuration.startTime;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    89
            this.started = configuration.started;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    90
            this.startNanos = configuration.startNanos;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    91
            this.dispatcherLookup = configuration.dispatcherLookup;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    92
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    93
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    94
        public StreamConfiguration() {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    95
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    96
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    97
        final public StreamConfiguration remove(Object action) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    98
            flushActions = remove(flushActions, action);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    99
            closeActions = remove(closeActions, action);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   100
            dispatchers = removeDispatch(dispatchers, action);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   101
            return this;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   102
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   103
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   104
        final public StreamConfiguration addDispatcher(EventDispatcher e) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   105
            dispatchers = add(dispatchers, e);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   106
            eventFilter = buildFilter(dispatchers);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   107
            dispatcherLookup = new LongMap<>();
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   108
            return this;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   109
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   110
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   111
        final public StreamConfiguration addFlushAction(Runnable action) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   112
            flushActions = add(flushActions, action);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   113
            return this;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   114
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   115
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   116
        final public StreamConfiguration addCloseAction(Runnable action) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   117
            closeActions = add(closeActions, action);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   118
            return this;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   119
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   120
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   121
        final public StreamConfiguration setClosed(boolean closed) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   122
            this.closed = closed;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   123
            changed = true;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   124
            return this;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   125
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   126
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   127
        final public boolean isClosed() {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   128
            return closed;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   129
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   130
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   131
        final public Runnable[] getCloseActions() {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   132
            return closeActions;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   133
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   134
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   135
        final public Runnable[] getFlushActions() {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   136
            return flushActions;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   137
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   138
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   139
        private EventDispatcher[] removeDispatch(EventDispatcher[] array, Object action) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   140
            List<EventDispatcher> list = new ArrayList<>(array.length);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   141
            boolean modified = false;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   142
            for (int i = 0; i < array.length; i++) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   143
                if (array[i].action != action) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   144
                    list.add(array[i]);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   145
                } else {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   146
                    modified = true;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   147
                }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   148
            }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   149
            EventDispatcher[] result = list.toArray(new EventDispatcher[0]);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   150
            if (modified) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   151
                eventFilter = buildFilter(result);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   152
                dispatcherLookup = new LongMap<>();
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   153
                changed = true;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   154
            }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   155
            return result;
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
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   158
        private <T> T[] remove(T[] array, Object action) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   159
            List<T> list = new ArrayList<>(array.length);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   160
            for (int i = 0; i < array.length; i++) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   161
                if (array[i] != action) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   162
                    list.add(array[i]);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   163
                } else {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   164
                    changed = true;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   165
                }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   166
            }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   167
            return list.toArray(array);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   168
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   169
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   170
        private <T> T[] add(T[] array, T object) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   171
            List<T> list = new ArrayList<>(Arrays.asList(array));
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   172
            list.add(object);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   173
            changed = true;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   174
            return list.toArray(array);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   175
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   176
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   177
        private static InternalEventFilter buildFilter(EventDispatcher[] dispatchers) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   178
            InternalEventFilter ef = new InternalEventFilter();
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   179
            for (EventDispatcher ed : dispatchers) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   180
                String name = ed.eventName;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   181
                if (name == null) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   182
                    return InternalEventFilter.ACCEPT_ALL;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   183
                }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   184
                ef.setThreshold(name, 0);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   185
            }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   186
            return ef.threadSafe();
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   187
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   188
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   189
        final public StreamConfiguration setReuse(boolean reuse) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   190
            this.reuse = reuse;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   191
            changed = true;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   192
            return this;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   193
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   194
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   195
        final public StreamConfiguration setOrdered(boolean ordered) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   196
            this.ordered = ordered;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   197
            changed = true;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   198
            return this;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   199
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   200
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   201
        final public StreamConfiguration setStartTime(Instant startTime) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   202
            this.startTime = startTime;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   203
            changed = true;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   204
            return this;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   205
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   206
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   207
        final public Instant getStartTime() {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   208
            return startTime;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   209
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   210
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   211
        final public boolean isStarted() {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   212
            return started;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   213
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   214
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   215
        final public StreamConfiguration setStartNanos(long startNanos) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   216
            this.startNanos = startNanos;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   217
            changed = true;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   218
            return this;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   219
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   220
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   221
        final public void setStarted(boolean started) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   222
            this.started = started;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   223
            changed = true;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   224
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   225
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   226
        final public boolean hasChanged() {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   227
            return changed;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   228
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   229
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   230
        final public boolean getReuse() {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   231
            return reuse;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   232
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   233
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   234
        final public boolean getOrdered() {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   235
            return ordered;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   236
        }
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
        final public InternalEventFilter getFiler() {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   239
            return eventFilter;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   240
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   241
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   242
        final public long getStartNanos() {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   243
            return startNanos;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   244
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   245
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   246
        final public InternalEventFilter getFilter() {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   247
            return eventFilter;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   248
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   249
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   250
        final public String toString() {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   251
            StringBuilder sb = new StringBuilder();
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   252
            for (Runnable flush : flushActions) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   253
                sb.append("Flush Action: ").append(flush).append("\n");
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   254
            }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   255
            for (Runnable close : closeActions) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   256
                sb.append("Close Action: " + close + "\n");
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
            for (EventDispatcher dispatcher : dispatchers) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   259
                sb.append("Dispatch Action: " + dispatcher.eventName + "(" + dispatcher + ") \n");
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   260
            }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   261
            sb.append("Closed: ").append(closed).append("\n");
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   262
            sb.append("Reuse: ").append(reuse).append("\n");
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   263
            sb.append("Ordered: ").append(ordered).append("\n");
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   264
            sb.append("Started: ").append(started).append("\n");
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   265
            sb.append("Start Time: ").append(startTime).append("\n");
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   266
            sb.append("Start Nanos: ").append(startNanos).append("\n");
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   267
            return sb.toString();
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   268
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   269
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   270
        private EventDispatcher[] getDispatchers() {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   271
            return dispatchers;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   272
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   273
    }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   274
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   275
    final static class EventDispatcher {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   276
        final static EventDispatcher[] NO_DISPATCHERS = new EventDispatcher[0];
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   277
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   278
        final private String eventName;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   279
        final private Consumer<RecordedEvent> action;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   280
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   281
        public EventDispatcher(Consumer<RecordedEvent> action) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   282
            this(null, action);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   283
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   284
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   285
        public EventDispatcher(String eventName, Consumer<RecordedEvent> action) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   286
            this.eventName = eventName;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   287
            this.action = action;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   288
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   289
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   290
        public void offer(RecordedEvent event) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   291
            action.accept(event);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   292
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   293
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   294
        public boolean accepts(EventType eventType) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   295
            return (eventName == null || eventType.getName().equals(eventName));
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   296
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   297
    }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   298
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   299
    public final static Instant NEXT_EVENT = Instant.now();
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   300
    public final static Comparator<? super RecordedEvent> END_TIME = (e1, e2) -> Long.compare(e1.endTimeTicks, e2.endTimeTicks);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   301
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   302
    private final static EventDispatcher[] NO_DISPATCHERS = new EventDispatcher[0];
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   303
    private final AccessControlContext accessControlContext;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   304
    private final Thread thread;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   305
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   306
    // Update bu updateConfiguration()
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   307
    protected StreamConfiguration configuration = new StreamConfiguration();
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   308
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   309
    // Cache the last event type and dispatch.
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   310
    private EventType lastEventType;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   311
    private EventDispatcher[] lastEventDispatch;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   312
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   313
    public AbstractEventStream(AccessControlContext acc) throws IOException {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   314
        this.accessControlContext = acc;
57614
b49f5c13baa7 Cleaning up
egahlin
parents: 57604
diff changeset
   315
        // Create thread object in constructor to ensure caller has
57604
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   316
        // permission before constructing object
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   317
        thread = new Thread(this);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   318
        thread.setDaemon(true);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   319
    }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   320
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   321
    public final void run() {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   322
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   323
            @Override
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   324
            public Void run() {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   325
                execute();
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   326
                return null;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   327
            }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   328
        }, accessControlContext);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   329
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   330
    }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   331
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   332
    private void execute() {
57614
b49f5c13baa7 Cleaning up
egahlin
parents: 57604
diff changeset
   333
        JVM.getJVM().exclude(Thread.currentThread());
57604
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   334
        try {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   335
            updateStartNanos();
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   336
            process();
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   337
        } catch (IOException e) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   338
            if (!isClosed()) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   339
                logException(e);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   340
            }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   341
        } catch (Exception e) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   342
            logException(e);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   343
        } finally {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   344
            Logger.log(LogTag.JFR_SYSTEM_STREAMING, LogLevel.DEBUG, "Execution of stream ended.");
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   345
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   346
    }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   347
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   348
    // User setting overrides default
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   349
    private void updateStartNanos() {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   350
        if (configuration.getStartTime() != null) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   351
            StreamConfiguration c = new StreamConfiguration(configuration);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   352
            try {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   353
                c.setStartNanos(c.getStartTime().toEpochMilli() * 1_000_000L);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   354
            } catch (ArithmeticException ae) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   355
                c.setStartNanos(Long.MAX_VALUE);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   356
            }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   357
            updateConfiguration(c);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   358
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   359
    }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   360
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   361
    private void logException(Exception e) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   362
        // FIXME: e.printStackTrace(); for debugging purposes,
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   363
        // remove before before integration
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   364
        e.printStackTrace();
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   365
        Logger.log(LogTag.JFR_SYSTEM_STREAMING, LogLevel.DEBUG, "Unexpected error processing stream. " + e.getMessage());
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   366
    }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   367
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   368
    public abstract void process() throws IOException;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   369
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   370
    protected final void clearLastDispatch() {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   371
        lastEventDispatch = null;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   372
        lastEventType = null;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   373
    }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   374
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   375
    protected final void dispatch(RecordedEvent event) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   376
        EventType type = event.getEventType();
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   377
        EventDispatcher[] ret = null;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   378
        if (type == lastEventType) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   379
            ret = lastEventDispatch;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   380
        } else {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   381
            ret = configuration.dispatcherLookup.get(type.getId());
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   382
            if (ret == null) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   383
                List<EventDispatcher> list = new ArrayList<>();
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   384
                for (EventDispatcher e : configuration.getDispatchers()) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   385
                    if (e.accepts(type)) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   386
                        list.add(e);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   387
                    }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   388
                }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   389
                ret = list.isEmpty() ? NO_DISPATCHERS : list.toArray(new EventDispatcher[0]);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   390
                configuration.dispatcherLookup.put(type.getId(), ret);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   391
            }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   392
            lastEventDispatch = ret;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   393
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   394
        for (int i = 0; i < ret.length; i++) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   395
            try {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   396
                ret[i].offer(event);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   397
            } catch (Exception e) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   398
                logException(e);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   399
            }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   400
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   401
    }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   402
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   403
    public final void runCloseActions() {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   404
        Runnable[] cas = configuration.getCloseActions();
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   405
        for (int i = 0; i < cas.length; i++) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   406
            try {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   407
                cas[i].run();
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   408
            } catch (Exception e) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   409
                logException(e);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   410
            }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   411
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   412
    }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   413
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   414
    public final void runFlushActions() {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   415
        Runnable[] fas = configuration.getFlushActions();
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   416
        for (int i = 0; i < fas.length; i++) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   417
            try {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   418
                fas[i].run();
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   419
            } catch (Exception e) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   420
                logException(e);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   421
            }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   422
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   423
    }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   424
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   425
    // Purpose of synchronizing the following methods is
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   426
    // to serialize changes to the configuration, so only one
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   427
    // thread at a time can change the configuration.
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   428
    //
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   429
    // The purpose is not to guard the configuration field. A new
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   430
    // configuration is published using updateConfiguration
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   431
    //
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   432
    public final synchronized boolean remove(Object action) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   433
        return updateConfiguration(new StreamConfiguration(configuration).remove(action));
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   434
    }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   435
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   436
    public final synchronized void onEvent(Consumer<RecordedEvent> action) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   437
        add(new EventDispatcher(action));
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   438
    }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   439
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   440
    public final synchronized void onEvent(String eventName, Consumer<RecordedEvent> action) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   441
        add(new EventDispatcher(eventName, action));
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   442
    }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   443
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   444
    private final synchronized void add(EventDispatcher e) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   445
        updateConfiguration(new StreamConfiguration(configuration).addDispatcher(e));
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   446
    }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   447
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   448
    public final synchronized void onFlush(Runnable action) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   449
        updateConfiguration(new StreamConfiguration(configuration).addFlushAction(action));
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   450
    }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   451
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   452
    public final synchronized void addCloseAction(Runnable action) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   453
        updateConfiguration(new StreamConfiguration(configuration).addCloseAction(action));
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   454
    }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   455
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   456
    public final synchronized void setClosed(boolean closed) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   457
        updateConfiguration(new StreamConfiguration(configuration).setClosed(closed));
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   458
    }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   459
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   460
    public final synchronized void setReuse(boolean reuse) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   461
        updateConfiguration(new StreamConfiguration(configuration).setReuse(reuse));
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   462
    }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   463
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   464
    public final synchronized void setOrdered(boolean ordered) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   465
        updateConfiguration(new StreamConfiguration(configuration).setOrdered(ordered));
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   466
    }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   467
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   468
    public final synchronized void setStartNanos(long startNanos) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   469
        updateConfiguration(new StreamConfiguration(configuration).setStartNanos(startNanos));
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   470
    }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   471
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   472
    public final synchronized void setStartTime(Instant startTime) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   473
        Objects.nonNull(startTime);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   474
        if (configuration.isStarted()) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   475
            throw new IllegalStateException("Stream is already started");
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   476
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   477
        if (startTime == null) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   478
            return;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   479
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   480
        if (startTime.isBefore(Instant.EPOCH)) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   481
            startTime = Instant.EPOCH;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   482
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   483
        updateConfiguration(new StreamConfiguration(configuration).setStartTime(startTime));
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   484
    }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   485
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   486
    private boolean updateConfiguration(StreamConfiguration newConfiguration) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   487
        // Changes to the configuration must be serialized, so make
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   488
        // sure that we have the monitor
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   489
        Thread.holdsLock(this);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   490
        if (newConfiguration.hasChanged()) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   491
            // Publish objects indirectly held by new configuration object
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   492
            VarHandle.releaseFence();
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   493
            configuration = newConfiguration;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   494
            // Publish the field reference. Making the field volatile
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   495
            // would be an alternative, but it is repeatedly read.
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   496
            VarHandle.releaseFence();
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   497
            return true;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   498
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   499
        return false;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   500
    }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   501
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   502
    public final boolean isClosed() {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   503
        return configuration.isClosed();
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   504
    }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   505
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   506
    public final void startAsync(long startNanos) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   507
        synchronized (this) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   508
            if (configuration.isStarted()) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   509
                throw new IllegalStateException("Event stream can only be started once");
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   510
            }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   511
            StreamConfiguration c = new StreamConfiguration(configuration);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   512
            c.setStartNanos(startNanos);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   513
            c.setStarted(true);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   514
            updateConfiguration(c);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   515
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   516
        thread.start();
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   517
    }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   518
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   519
    public final void start(long startNanos) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   520
        synchronized (this) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   521
            if (configuration.isStarted()) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   522
                throw new IllegalStateException("Event stream can only be started once");
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   523
            }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   524
            StreamConfiguration c = new StreamConfiguration(configuration);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   525
            c.setStartNanos(startNanos);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   526
            c.setStarted(true);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   527
            updateConfiguration(c);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   528
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   529
        run();
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   530
    }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   531
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   532
    public final void awaitTermination(Duration timeout) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   533
        Objects.requireNonNull(timeout);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   534
        if (thread != Thread.currentThread()) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   535
            try {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   536
                thread.join(timeout.toMillis());
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   537
            } catch (InterruptedException e) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   538
                // ignore
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   539
            }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   540
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   541
    }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   542
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   543
    public final void awaitTermination() {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   544
        awaitTermination(Duration.ofMillis(0));
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   545
    }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   546
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   547
    abstract public void close();
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   548
}