src/jdk.jfr/share/classes/jdk/jfr/internal/consumer/InternalEventFilter.java
author egahlin
Fri, 24 May 2019 19:39:31 +0200
branchJEP-349-branch
changeset 57372 50ca040843ea
child 57433 83e4343a6984
permissions -rw-r--r--
Prepare infrastructure for multiple implementations of EventStream
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57372
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
     1
/*
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
     4
 *
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    10
 *
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    15
 * accompanied this code).
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    16
 *
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    20
 *
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    23
 * questions.
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    24
 */
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    25
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    26
package jdk.jfr.internal.consumer;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    27
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    28
import java.util.Collection;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    29
import java.util.HashMap;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    30
import java.util.HashSet;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    31
import java.util.Map;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    32
import java.util.Set;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    33
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    34
public final class InternalEventFilter {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    35
    public static final InternalEventFilter ACCEPT_ALL = new InternalEventFilter();
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    36
    private final Map<String, Long> thresholds = new HashMap<>();
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    37
    private boolean acceptAll;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    38
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    39
    public static InternalEventFilter merge(Collection<InternalEventFilter> filters) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    40
        for (InternalEventFilter ef : filters) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    41
            if (ef.getAcceptAll()) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    42
                return ACCEPT_ALL;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    43
            }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    44
        }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    45
        if (filters.size() == 1) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    46
            return filters.iterator().next();
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    47
        }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    48
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    49
        Set<String> eventNames = new HashSet<>();
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    50
        for (InternalEventFilter ef : filters) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    51
            eventNames.addAll(ef.thresholds.keySet());
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    52
        }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    53
        InternalEventFilter result = new InternalEventFilter();
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    54
        for (String eventName : eventNames) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    55
            for (InternalEventFilter ef : filters) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    56
                Long l = ef.thresholds.get(eventName);
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    57
                if (l != null) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    58
                    result.setThreshold(eventName, l.longValue());
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    59
                }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    60
            }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    61
        }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    62
        return result;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    63
    }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    64
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    65
    private boolean getAcceptAll() {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    66
        return acceptAll;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    67
    }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    68
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    69
    public void setAcceptAll() {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    70
        acceptAll = true;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    71
    }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    72
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    73
    public void setThreshold(String eventName, long nanos) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    74
        Long l = thresholds.get(eventName);
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    75
        if (l != null) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    76
            l = Math.min(l, nanos);
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    77
        } else {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    78
            l = nanos;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    79
        }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    80
        thresholds.put(eventName, l);
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    81
    }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    82
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    83
    public long getThreshold(String eventName) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    84
        if (acceptAll) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    85
            return 0;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    86
        }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    87
        Long l = thresholds.get(eventName);
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    88
        if (l != null) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    89
            return l;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    90
        }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    91
        return -1;
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    92
    }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    93
    public String toString() {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    94
        if (acceptAll) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    95
            return "ACCEPT ALL";
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    96
        }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    97
        StringBuilder sb = new StringBuilder();
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    98
        for (String key : thresholds.keySet().toArray(new String[0])) {
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
    99
            Long value = thresholds.get(key);
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   100
            sb.append(key);
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   101
            sb.append(" = ");
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   102
            sb.append(value.longValue() / 1_000_000);
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   103
            sb.append(" ms");
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   104
        }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   105
        return sb.toString();
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   106
    }
50ca040843ea Prepare infrastructure for multiple implementations of EventStream
egahlin
parents:
diff changeset
   107
}