src/jdk.jfr/share/classes/jdk/jfr/consumer/EventDirectoryStream.java
author egahlin
Fri, 24 May 2019 20:30:12 +0200
branchJEP-349-branch
changeset 57373 400db63e4937
parent 57372 50ca040843ea
child 57376 8e8a06a3059c
permissions -rw-r--r--
Move LongMap to util. Update use cases
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57361
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
     1
/*
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
     4
 *
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    10
 *
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    15
 * accompanied this code).
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    16
 *
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    20
 *
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    23
 * questions.
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    24
 */
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    25
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    26
package jdk.jfr.consumer;
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    27
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    28
import java.io.IOException;
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    29
import java.security.AccessControlContext;
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    30
import java.time.Duration;
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    31
import java.time.Instant;
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    32
import java.util.Objects;
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    33
import java.util.function.Consumer;
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    34
57372
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    35
import jdk.jfr.internal.consumer.EventConsumer;
57361
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    36
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    37
final class EventDirectoryStream implements EventStream {
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    38
57373
400db63e4937 Move LongMap to util. Update use cases
egahlin
parents: 57372
diff changeset
    39
    private static class EventSetConsumer extends EventConsumer {
57372
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    40
        private EventSetLocation location;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    41
        private EventSet eventSet;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    42
        private int eventSetIndex;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    43
        private int eventArrayIndex;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    44
        private RecordedEvent[] currentEventArray = new RecordedEvent[0];
57361
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    45
57373
400db63e4937 Move LongMap to util. Update use cases
egahlin
parents: 57372
diff changeset
    46
        public EventSetConsumer(AccessControlContext acc) throws IOException {
57372
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    47
            super(acc);
57361
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    48
        }
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    49
57372
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    50
        public void process() throws Exception, IOException {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    51
            this.location = EventSetLocation.current();
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    52
            this.eventSet = location.acquire(startNanos, null); // use timestamp
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    53
                                                                // from
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    54
            if (eventSet == null) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    55
                return;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    56
            }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    57
            while (!isClosed()) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    58
                processSegment();
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    59
                runFlushActions();
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    60
                do {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    61
                    if (isClosed()) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    62
                        return;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    63
                    }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    64
                    currentEventArray = eventSet.readEvents(eventSetIndex);
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    65
                    if (currentEventArray == EventSet.END_OF_SET) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    66
                        eventSet = eventSet.next(eventFilter);
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    67
                        if (eventSet == null || isClosed()) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    68
                            return;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    69
                        }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    70
                        eventSetIndex = 0;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    71
                        continue;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    72
                    }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    73
                    if (currentEventArray == null) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    74
                        return; // no more events
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    75
                    }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    76
                    eventSetIndex++;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    77
                } while (currentEventArray.length == 0);
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    78
                eventArrayIndex = 0;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    79
            }
57361
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    80
        }
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
    81
57372
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    82
        private void processSegment() {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    83
            while (eventArrayIndex < currentEventArray.length) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    84
                RecordedEvent e = currentEventArray[eventArrayIndex++];
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    85
                if (e == null) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    86
                    return;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    87
                }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    88
                dispatch(e);
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    89
            }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    90
        }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    91
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    92
        public void close() {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    93
            setClosed(true);
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    94
            // TODO: Data races here, must fix
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    95
            synchronized (this) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    96
                if (eventSet != null) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    97
                    eventSet.release(null);
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    98
                }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
    99
                if (location != null) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
   100
                    location.release();
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
   101
                }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
   102
            }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
   103
            runCloseActions();
57361
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   104
        }
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   105
    }
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   106
57373
400db63e4937 Move LongMap to util. Update use cases
egahlin
parents: 57372
diff changeset
   107
    private final EventSetConsumer eventConsumer;
57361
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   108
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   109
    public EventDirectoryStream(AccessControlContext acc) throws IOException {
57373
400db63e4937 Move LongMap to util. Update use cases
egahlin
parents: 57372
diff changeset
   110
        eventConsumer = new EventSetConsumer(acc);
57361
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   111
    }
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   112
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   113
    public void close() {
57372
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
   114
        eventConsumer.close();
57361
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   115
    }
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   116
57372
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
   117
    public void onFlush(Runnable action) {
57361
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   118
        Objects.requireNonNull(action);
57372
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
   119
        eventConsumer.onFlush(action);
57361
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   120
    }
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   121
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   122
    void start(long startNanos) {
57372
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
   123
        eventConsumer.start(startNanos);
57361
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   124
    }
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   125
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   126
    @Override
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   127
    public void start() {
57372
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
   128
        start(Instant.now().toEpochMilli() * 1000 * 1000L);
57361
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   129
    }
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   130
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   131
    @Override
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   132
    public void startAsync() {
57372
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
   133
        startAsync(Instant.now().toEpochMilli() * 1000 * 1000L);
57361
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   134
    }
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   135
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   136
    void startAsync(long startNanos) {
57372
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
   137
        eventConsumer.startAsync(startNanos);
57361
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   138
    }
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   139
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   140
    @Override
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   141
    public void onEvent(Consumer<RecordedEvent> action) {
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   142
        Objects.requireNonNull(action);
57372
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
   143
        eventConsumer.onEvent(action);
57361
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   144
    }
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   145
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   146
    @Override
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   147
    public void onEvent(String eventName, Consumer<RecordedEvent> action) {
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   148
        Objects.requireNonNull(eventName);
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   149
        Objects.requireNonNull(action);
57372
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
   150
        eventConsumer.onEvent(eventName, action);
57361
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   151
    }
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   152
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   153
    @Override
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   154
    public void onClose(Runnable action) {
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   155
        Objects.requireNonNull(action);
57372
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
   156
        eventConsumer.addCloseAction(action);
57361
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   157
    }
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   158
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   159
    @Override
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   160
    public boolean remove(Object action) {
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   161
        Objects.requireNonNull(action);
57372
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
   162
        return eventConsumer.remove(action);
57361
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   163
    }
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   164
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   165
    @Override
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   166
    public void awaitTermination(Duration timeout) {
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   167
        Objects.requireNonNull(timeout);
57372
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
   168
        eventConsumer.awaitTermination(timeout);
57361
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   169
    }
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   170
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   171
    @Override
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   172
    public void awaitTermination() {
57372
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents: 57361
diff changeset
   173
        eventConsumer.awaitTermination(Duration.ofMillis(0));
57361
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   174
    }
53dccc90a5be Preview-addendum
mgronlun
parents:
diff changeset
   175
}