src/jdk.jfr/share/classes/jdk/jfr/consumer/AbstractEventStream.java
author egahlin
Fri, 09 Aug 2019 01:18:18 +0200
branchJEP-349-branch
changeset 57690 9316d02dd4a5
parent 57628 f5f590eaecf5
child 57753 4883a96b6d37
permissions -rw-r--r--
Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
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;
57690
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
    48
import jdk.jfr.internal.Utils;
57604
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    49
import jdk.jfr.internal.consumer.InternalEventFilter;
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
/*
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    52
 * Purpose of this class is to simplify the implementation of
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    53
 * an event stream. In particular, it handles:
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    54
 *
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    55
 * - configuration storage
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    56
 * - atomic updates to a configuration
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    57
 * - dispatch mechanism
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    58
 * - error handling
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    59
 * - security
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
 */
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    62
abstract class AbstractEventStream implements Runnable {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    63
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    64
    public static final class StreamConfiguration {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    65
        private static final Runnable[] NO_ACTIONS = new Runnable[0];
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    66
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    67
        private Runnable[] flushActions = NO_ACTIONS;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    68
        private Runnable[] closeActions = NO_ACTIONS;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    69
        private EventDispatcher[] dispatchers = NO_DISPATCHERS;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    70
        private InternalEventFilter eventFilter = InternalEventFilter.ACCEPT_ALL;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    71
        private boolean closed = false;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    72
        private boolean reuse = true;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    73
        private boolean ordered = true;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    74
        private Instant startTime = null;
57690
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
    75
        private Instant endTime = null;
57604
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    76
        private boolean started = false;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    77
        private long startNanos = 0;
57690
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
    78
        private long endNanos = Long.MAX_VALUE;
57604
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    79
        private LongMap<EventDispatcher[]> dispatcherLookup = new LongMap<>();
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    80
        private boolean changed = false;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    81
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    82
        public StreamConfiguration(StreamConfiguration configuration) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    83
            this.flushActions = configuration.flushActions;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    84
            this.closeActions = configuration.closeActions;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    85
            this.dispatchers = configuration.dispatchers;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    86
            this.eventFilter = configuration.eventFilter;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    87
            this.closed = configuration.closed;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    88
            this.reuse = configuration.reuse;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    89
            this.ordered = configuration.ordered;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    90
            this.startTime = configuration.startTime;
57690
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
    91
            this.endTime = configuration.endTime;
57604
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    92
            this.started = configuration.started;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    93
            this.startNanos = configuration.startNanos;
57690
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
    94
            this.endNanos = configuration.endNanos;
57604
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    95
            this.dispatcherLookup = configuration.dispatcherLookup;
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
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    98
        public StreamConfiguration() {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
    99
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   100
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   101
        final public StreamConfiguration remove(Object action) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   102
            flushActions = remove(flushActions, action);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   103
            closeActions = remove(closeActions, action);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   104
            dispatchers = removeDispatch(dispatchers, action);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   105
            return this;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   106
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   107
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   108
        final public StreamConfiguration addDispatcher(EventDispatcher e) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   109
            dispatchers = add(dispatchers, e);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   110
            eventFilter = buildFilter(dispatchers);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   111
            dispatcherLookup = new LongMap<>();
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   112
            return this;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   113
        }
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
        final public StreamConfiguration addFlushAction(Runnable action) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   116
            flushActions = add(flushActions, action);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   117
            return this;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   118
        }
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
        final public StreamConfiguration addCloseAction(Runnable action) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   121
            closeActions = add(closeActions, action);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   122
            return this;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   123
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   124
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   125
        final public StreamConfiguration setClosed(boolean closed) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   126
            this.closed = closed;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   127
            changed = true;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   128
            return this;
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 boolean isClosed() {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   132
            return closed;
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[] getCloseActions() {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   136
            return closeActions;
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
        final public Runnable[] getFlushActions() {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   140
            return flushActions;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   141
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   142
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   143
        private EventDispatcher[] removeDispatch(EventDispatcher[] array, Object action) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   144
            List<EventDispatcher> list = new ArrayList<>(array.length);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   145
            boolean modified = false;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   146
            for (int i = 0; i < array.length; i++) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   147
                if (array[i].action != action) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   148
                    list.add(array[i]);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   149
                } else {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   150
                    modified = true;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   151
                }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   152
            }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   153
            EventDispatcher[] result = list.toArray(new EventDispatcher[0]);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   154
            if (modified) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   155
                eventFilter = buildFilter(result);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   156
                dispatcherLookup = new LongMap<>();
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   157
                changed = true;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   158
            }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   159
            return result;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   160
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   161
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   162
        private <T> T[] remove(T[] array, Object action) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   163
            List<T> list = new ArrayList<>(array.length);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   164
            for (int i = 0; i < array.length; i++) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   165
                if (array[i] != action) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   166
                    list.add(array[i]);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   167
                } else {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   168
                    changed = true;
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
            }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   171
            return list.toArray(array);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   172
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   173
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   174
        private <T> T[] add(T[] array, T object) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   175
            List<T> list = new ArrayList<>(Arrays.asList(array));
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   176
            list.add(object);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   177
            changed = true;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   178
            return list.toArray(array);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   179
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   180
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   181
        private static InternalEventFilter buildFilter(EventDispatcher[] dispatchers) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   182
            InternalEventFilter ef = new InternalEventFilter();
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   183
            for (EventDispatcher ed : dispatchers) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   184
                String name = ed.eventName;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   185
                if (name == null) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   186
                    return InternalEventFilter.ACCEPT_ALL;
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
                ef.setThreshold(name, 0);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   189
            }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   190
            return ef.threadSafe();
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   191
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   192
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   193
        final public StreamConfiguration setReuse(boolean reuse) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   194
            this.reuse = reuse;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   195
            changed = true;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   196
            return this;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   197
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   198
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   199
        final public StreamConfiguration setOrdered(boolean ordered) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   200
            this.ordered = ordered;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   201
            changed = true;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   202
            return this;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   203
        }
57690
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
   204
        public StreamConfiguration setEndTime(Instant endTime) {
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
   205
            this.endTime = endTime;
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
   206
            this.endNanos = Utils.timeToNanos(endTime);
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
   207
            changed = true;
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
   208
            return this;
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
   209
        }
57604
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 StreamConfiguration setStartTime(Instant startTime) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   212
            this.startTime = startTime;
57690
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
   213
            this.startNanos = Utils.timeToNanos(startTime);
57604
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   214
            changed = true;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   215
            return this;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   216
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   217
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   218
        final public Instant getStartTime() {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   219
            return startTime;
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
57690
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
   222
        public Object getEndTime() {
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
   223
            return endTime;
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
   224
        }
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
   225
57604
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   226
        final public boolean isStarted() {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   227
            return started;
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 StreamConfiguration setStartNanos(long startNanos) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   231
            this.startNanos = startNanos;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   232
            changed = true;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   233
            return this;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   234
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   235
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   236
        final public void setStarted(boolean started) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   237
            this.started = started;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   238
            changed = true;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   239
        }
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
        final public boolean hasChanged() {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   242
            return changed;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   243
        }
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
        final public boolean getReuse() {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   246
            return reuse;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   247
        }
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
        final public boolean getOrdered() {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   250
            return ordered;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   251
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   252
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   253
        final public InternalEventFilter getFiler() {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   254
            return eventFilter;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   255
        }
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
        final public long getStartNanos() {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   258
            return startNanos;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   259
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   260
57690
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
   261
        final public long getEndNanos() {
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
   262
            return endNanos;
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
   263
        }
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
   264
57604
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   265
        final public InternalEventFilter getFilter() {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   266
            return eventFilter;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   267
        }
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
        final public String toString() {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   270
            StringBuilder sb = new StringBuilder();
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   271
            for (Runnable flush : flushActions) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   272
                sb.append("Flush Action: ").append(flush).append("\n");
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
            for (Runnable close : closeActions) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   275
                sb.append("Close Action: " + close + "\n");
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   276
            }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   277
            for (EventDispatcher dispatcher : dispatchers) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   278
                sb.append("Dispatch Action: " + dispatcher.eventName + "(" + dispatcher + ") \n");
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   279
            }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   280
            sb.append("Closed: ").append(closed).append("\n");
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   281
            sb.append("Reuse: ").append(reuse).append("\n");
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   282
            sb.append("Ordered: ").append(ordered).append("\n");
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   283
            sb.append("Started: ").append(started).append("\n");
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   284
            sb.append("Start Time: ").append(startTime).append("\n");
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   285
            sb.append("Start Nanos: ").append(startNanos).append("\n");
57690
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
   286
            sb.append("End Time: ").append(endTime).append("\n");
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
   287
            sb.append("End Nanos: ").append(endNanos).append("\n");
57604
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   288
            return sb.toString();
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
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   291
        private EventDispatcher[] getDispatchers() {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   292
            return dispatchers;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   293
        }
57690
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
   294
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
   295
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
   296
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
   297
57604
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
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   300
    final static class EventDispatcher {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   301
        final static EventDispatcher[] NO_DISPATCHERS = new EventDispatcher[0];
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   302
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   303
        final private String eventName;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   304
        final private Consumer<RecordedEvent> action;
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
        public EventDispatcher(Consumer<RecordedEvent> action) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   307
            this(null, action);
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
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   310
        public EventDispatcher(String eventName, Consumer<RecordedEvent> action) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   311
            this.eventName = eventName;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   312
            this.action = action;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   313
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   314
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   315
        public void offer(RecordedEvent event) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   316
            action.accept(event);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   317
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   318
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   319
        public boolean accepts(EventType eventType) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   320
            return (eventName == null || eventType.getName().equals(eventName));
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   321
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   322
    }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   323
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   324
    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
   325
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   326
    private final static EventDispatcher[] NO_DISPATCHERS = new EventDispatcher[0];
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   327
    private final AccessControlContext accessControlContext;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   328
    private final Thread thread;
57690
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
   329
    private final boolean active;
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
   330
    // Update by updateConfiguration()
57604
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   331
    protected StreamConfiguration configuration = new StreamConfiguration();
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   332
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   333
    // Cache the last event type and dispatch.
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   334
    private EventType lastEventType;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   335
    private EventDispatcher[] lastEventDispatch;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   336
57690
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
   337
    public AbstractEventStream(AccessControlContext acc, boolean active) throws IOException {
57604
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   338
        this.accessControlContext = acc;
57690
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
   339
        this.active = active;
57614
b49f5c13baa7 Cleaning up
egahlin
parents: 57604
diff changeset
   340
        // Create thread object in constructor to ensure caller has
57604
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   341
        // permission before constructing object
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   342
        thread = new Thread(this);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   343
        thread.setDaemon(true);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   344
    }
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
    public final void run() {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   347
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   348
            @Override
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   349
            public Void run() {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   350
                execute();
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   351
                return null;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   352
            }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   353
        }, accessControlContext);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   354
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   355
    }
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
    private void execute() {
57614
b49f5c13baa7 Cleaning up
egahlin
parents: 57604
diff changeset
   358
        JVM.getJVM().exclude(Thread.currentThread());
57604
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   359
        try {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   360
            process();
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   361
        } catch (IOException e) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   362
            if (!isClosed()) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   363
                logException(e);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   364
            }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   365
        } catch (Exception e) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   366
            logException(e);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   367
        } finally {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   368
            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
   369
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   370
    }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   371
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   372
    private void logException(Exception e) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   373
        // FIXME: e.printStackTrace(); for debugging purposes,
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   374
        // remove before before integration
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   375
        e.printStackTrace();
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   376
        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
   377
    }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   378
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   379
    public abstract void process() throws IOException;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   380
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   381
    protected final void clearLastDispatch() {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   382
        lastEventDispatch = null;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   383
        lastEventType = null;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   384
    }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   385
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   386
    protected final void dispatch(RecordedEvent event) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   387
        EventType type = event.getEventType();
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   388
        EventDispatcher[] ret = null;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   389
        if (type == lastEventType) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   390
            ret = lastEventDispatch;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   391
        } else {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   392
            ret = configuration.dispatcherLookup.get(type.getId());
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   393
            if (ret == null) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   394
                List<EventDispatcher> list = new ArrayList<>();
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   395
                for (EventDispatcher e : configuration.getDispatchers()) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   396
                    if (e.accepts(type)) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   397
                        list.add(e);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   398
                    }
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
                ret = list.isEmpty() ? NO_DISPATCHERS : list.toArray(new EventDispatcher[0]);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   401
                configuration.dispatcherLookup.put(type.getId(), ret);
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
            lastEventDispatch = ret;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   404
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   405
        for (int i = 0; i < ret.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
                ret[i].offer(event);
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 runCloseActions() {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   415
        Runnable[] cas = configuration.getCloseActions();
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   416
        for (int i = 0; i < cas.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
                cas[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
    public final void runFlushActions() {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   426
        Runnable[] fas = configuration.getFlushActions();
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   427
        for (int i = 0; i < fas.length; i++) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   428
            try {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   429
                fas[i].run();
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   430
            } catch (Exception e) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   431
                logException(e);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   432
            }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   433
        }
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
    // Purpose of synchronizing the following methods is
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   437
    // to serialize changes to the configuration, so only one
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   438
    // thread at a time can change the configuration.
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
    // The purpose is not to guard the configuration field. A new
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   441
    // configuration is published using updateConfiguration
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
    public final synchronized boolean remove(Object action) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   444
        return updateConfiguration(new StreamConfiguration(configuration).remove(action));
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   445
    }
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
    public final synchronized void onEvent(Consumer<RecordedEvent> action) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   448
        add(new EventDispatcher(action));
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   449
    }
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
    public final synchronized void onEvent(String eventName, Consumer<RecordedEvent> action) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   452
        add(new EventDispatcher(eventName, action));
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   453
    }
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
    private final synchronized void add(EventDispatcher e) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   456
        updateConfiguration(new StreamConfiguration(configuration).addDispatcher(e));
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   457
    }
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
    public final synchronized void onFlush(Runnable action) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   460
        updateConfiguration(new StreamConfiguration(configuration).addFlushAction(action));
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   461
    }
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
    public final synchronized void addCloseAction(Runnable action) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   464
        updateConfiguration(new StreamConfiguration(configuration).addCloseAction(action));
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   465
    }
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
    public final synchronized void setClosed(boolean closed) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   468
        updateConfiguration(new StreamConfiguration(configuration).setClosed(closed));
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   469
    }
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
    public final synchronized void setReuse(boolean reuse) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   472
        updateConfiguration(new StreamConfiguration(configuration).setReuse(reuse));
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   473
    }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   474
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   475
    public final synchronized void setOrdered(boolean ordered) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   476
        updateConfiguration(new StreamConfiguration(configuration).setOrdered(ordered));
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   477
    }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   478
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   479
    public final synchronized void setStartNanos(long startNanos) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   480
        updateConfiguration(new StreamConfiguration(configuration).setStartNanos(startNanos));
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   481
    }
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
    public final synchronized void setStartTime(Instant startTime) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   484
        Objects.nonNull(startTime);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   485
        if (configuration.isStarted()) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   486
            throw new IllegalStateException("Stream is already started");
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   487
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   488
        if (startTime.isBefore(Instant.EPOCH)) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   489
            startTime = Instant.EPOCH;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   490
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   491
        updateConfiguration(new StreamConfiguration(configuration).setStartTime(startTime));
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   492
    }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   493
57690
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
   494
    public final void setEndTime(Instant endTime) {
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
   495
    if (configuration.isStarted()) {
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
   496
        throw new IllegalStateException("Stream is already started");
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
   497
    }
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
   498
    updateConfiguration(new StreamConfiguration(configuration).setEndTime(endTime));
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
   499
}
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
   500
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
   501
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
   502
    protected boolean updateConfiguration(StreamConfiguration newConfiguration) {
57604
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   503
        // Changes to the configuration must be serialized, so make
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   504
        // sure that we have the monitor
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   505
        Thread.holdsLock(this);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   506
        if (newConfiguration.hasChanged()) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   507
            // Publish objects indirectly held by new configuration object
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   508
            VarHandle.releaseFence();
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   509
            configuration = newConfiguration;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   510
            // Publish the field reference. Making the field volatile
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   511
            // would be an alternative, but it is repeatedly read.
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   512
            VarHandle.releaseFence();
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   513
            return true;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   514
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   515
        return false;
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   516
    }
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
    public final boolean isClosed() {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   519
        return configuration.isClosed();
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   520
    }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   521
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   522
    public final void startAsync(long startNanos) {
57690
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
   523
        startInternal(startNanos);
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
   524
        thread.start();
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
   525
    }
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
   526
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
   527
    public final void start(long startNanos) {
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
   528
        startInternal(startNanos);
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
   529
        run();
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
   530
    }
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
   531
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
   532
    private void startInternal(long startNanos) {
57604
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   533
        synchronized (this) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   534
            if (configuration.isStarted()) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   535
                throw new IllegalStateException("Event stream can only be started once");
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   536
            }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   537
            StreamConfiguration c = new StreamConfiguration(configuration);
57690
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
   538
            if (active) {
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
   539
                c.setStartNanos(startNanos);
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
   540
            }
57604
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   541
            c.setStarted(true);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   542
            updateConfiguration(c);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   543
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   544
    }
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
    public final void awaitTermination(Duration timeout) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   547
        Objects.requireNonNull(timeout);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   548
        if (thread != Thread.currentThread()) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   549
            try {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   550
                thread.join(timeout.toMillis());
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   551
            } catch (InterruptedException e) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   552
                // ignore
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   553
            }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   554
        }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   555
    }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   556
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   557
    public final void awaitTermination() {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   558
        awaitTermination(Duration.ofMillis(0));
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   559
    }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   560
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   561
    abstract public void close();
57690
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57628
diff changeset
   562
57604
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents:
diff changeset
   563
}