src/jdk.jfr/share/classes/jdk/jfr/internal/tool/Print.java
author mgronlun
Wed, 30 Oct 2019 19:43:52 +0100
changeset 58863 c16ac7a2eba4
parent 54815 1e83e1a600cb
permissions -rw-r--r--
8226511: Implement JFR Event Streaming Reviewed-by: egahlin, mseledtsov, mgronlun Contributed-by: erik.gahlin@oracle.com, mikhailo.seledtsov@oracle.com, markus.gronlund@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
52850
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
     1
/*
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
     2
 * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
     4
 *
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    10
 *
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    15
 * accompanied this code).
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    16
 *
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    20
 *
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    23
 * questions.
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    24
 */
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    25
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    26
package jdk.jfr.internal.tool;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    27
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    28
import java.io.IOException;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    29
import java.io.PrintStream;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    30
import java.io.PrintWriter;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    31
import java.nio.charset.Charset;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    32
import java.nio.file.Path;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    33
import java.util.ArrayList;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    34
import java.util.Deque;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    35
import java.util.HashMap;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    36
import java.util.List;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    37
import java.util.Map;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    38
import java.util.function.Function;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    39
import java.util.function.Predicate;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    40
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    41
import jdk.jfr.EventType;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    42
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    43
final class Print extends Command {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    44
    @Override
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    45
    public String getName() {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    46
        return "print";
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    47
    }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    48
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    49
    @Override
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    50
    public List<String> getOptionSyntax() {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    51
        List<String> list = new ArrayList<>();
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    52
        list.add("[--xml|--json]");
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    53
        list.add("[--categories <filter>]");
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    54
        list.add("[--events <filter>]");
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    55
        list.add("[--stack-depth <depth>]");
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    56
        list.add("<file>");
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    57
        return list;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    58
    }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    59
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    60
    @Override
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    61
    protected String getTitle() {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    62
        return "Print contents of a recording file";
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    63
    }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    64
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    65
    @Override
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    66
    public String getDescription() {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    67
        return getTitle() + ". See 'jfr help print' for details.";
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    68
    }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    69
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    70
    @Override
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    71
    public void displayOptionUsage(PrintStream stream) {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    72
        stream.println("  --xml                   Print recording in XML format");
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    73
        stream.println();
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    74
        stream.println("  --json                  Print recording in JSON format");
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    75
        stream.println();
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    76
        stream.println("  --categories <filter>   Select events matching a category name.");
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    77
        stream.println("                          The filter is a comma-separated list of names,");
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    78
        stream.println("                          simple and/or qualified, and/or quoted glob patterns");
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    79
        stream.println();
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    80
        stream.println("  --events <filter>       Select events matching an event name.");
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    81
        stream.println("                          The filter is a comma-separated list of names,");
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    82
        stream.println("                          simple and/or qualified, and/or quoted glob patterns");
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    83
        stream.println();
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    84
        stream.println("  --stack-depth <depth>   Number of frames in stack traces, by default 5");
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    85
        stream.println();
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    86
        stream.println("  <file>                  Location of the recording file (.jfr)");
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    87
        stream.println();
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    88
        stream.println();
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    89
        stream.println("Example usage:");
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    90
        stream.println();
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    91
        stream.println(" jfr print --events OldObjectSample recording.jfr");
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    92
        stream.println();
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    93
        stream.println(" jfr print --events CPULoad,GarbageCollection recording.jfr");
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    94
        stream.println();
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    95
        stream.println(" jfr print --categories \"GC,JVM,Java*\" recording.jfr");
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    96
        stream.println();
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    97
        stream.println(" jfr print --events \"jdk.*\" --stack-depth 64 recording.jfr");
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    98
        stream.println();
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
    99
        stream.println(" jfr print --json --events CPULoad recording.jfr");
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   100
    }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   101
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   102
    @Override
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   103
    public void execute(Deque<String> options) throws UserSyntaxException, UserDataException {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   104
        Path file = getJFRInputFile(options);
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   105
        PrintWriter pw = new PrintWriter(System.out, false, Charset.forName("UTF-8"));
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   106
        Predicate<EventType> eventFilter = null;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   107
        int stackDepth = 5;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   108
        EventPrintWriter eventWriter = null;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   109
        int optionCount = options.size();
54815
1e83e1a600cb 8221569: JFR tool produces incorrect output when both --categories and --events are specified
cito
parents: 52850
diff changeset
   110
        boolean foundEventFilter = false;
1e83e1a600cb 8221569: JFR tool produces incorrect output when both --categories and --events are specified
cito
parents: 52850
diff changeset
   111
        boolean foundCategoryFilter = false;
52850
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   112
        while (optionCount > 0) {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   113
            if (acceptFilterOption(options, "--events")) {
54815
1e83e1a600cb 8221569: JFR tool produces incorrect output when both --categories and --events are specified
cito
parents: 52850
diff changeset
   114
                if (foundEventFilter) {
1e83e1a600cb 8221569: JFR tool produces incorrect output when both --categories and --events are specified
cito
parents: 52850
diff changeset
   115
                    throw new UserSyntaxException("use --events event1,event2,event3 to include multiple events");
1e83e1a600cb 8221569: JFR tool produces incorrect output when both --categories and --events are specified
cito
parents: 52850
diff changeset
   116
                }
1e83e1a600cb 8221569: JFR tool produces incorrect output when both --categories and --events are specified
cito
parents: 52850
diff changeset
   117
                foundEventFilter = true;
52850
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   118
                String filter = options.remove();
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   119
                warnForWildcardExpansion("--events", filter);
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   120
                eventFilter = addEventFilter(filter, eventFilter);
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   121
            }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   122
            if (acceptFilterOption(options, "--categories")) {
54815
1e83e1a600cb 8221569: JFR tool produces incorrect output when both --categories and --events are specified
cito
parents: 52850
diff changeset
   123
                if (foundCategoryFilter) {
1e83e1a600cb 8221569: JFR tool produces incorrect output when both --categories and --events are specified
cito
parents: 52850
diff changeset
   124
                    throw new UserSyntaxException("use --categories category1,category2 to include multiple categories");
1e83e1a600cb 8221569: JFR tool produces incorrect output when both --categories and --events are specified
cito
parents: 52850
diff changeset
   125
                }
1e83e1a600cb 8221569: JFR tool produces incorrect output when both --categories and --events are specified
cito
parents: 52850
diff changeset
   126
                foundCategoryFilter = true;
52850
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   127
                String filter = options.remove();
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   128
                warnForWildcardExpansion("--categories", filter);
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   129
                eventFilter = addCategoryFilter(filter, eventFilter);
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   130
            }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   131
            if (acceptOption(options, "--stack-depth")) {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   132
                String value = options.pop();
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   133
                try {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   134
                    stackDepth = Integer.parseInt(value);
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   135
                    if (stackDepth < 0) {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   136
                        throw new UserSyntaxException("stack depth must be zero or a positive integer.");
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   137
                    }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   138
                } catch (NumberFormatException nfe) {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   139
                    throw new UserSyntaxException("not a valid value for --stack-depth");
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   140
                }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   141
            }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   142
            if (acceptFormatterOption(options, eventWriter, "--json")) {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   143
                eventWriter = new JSONWriter(pw);
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   144
            }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   145
            if (acceptFormatterOption(options, eventWriter, "--xml")) {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   146
                eventWriter = new XMLWriter(pw);
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   147
            }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   148
            if (optionCount == options.size()) {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   149
                // No progress made
54815
1e83e1a600cb 8221569: JFR tool produces incorrect output when both --categories and --events are specified
cito
parents: 52850
diff changeset
   150
                checkCommonError(options, "--event", "--events");
1e83e1a600cb 8221569: JFR tool produces incorrect output when both --categories and --events are specified
cito
parents: 52850
diff changeset
   151
                checkCommonError(options, "--category", "--categories");
52850
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   152
                throw new UserSyntaxException("unknown option " + options.peek());
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   153
            }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   154
            optionCount = options.size();
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   155
        }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   156
        if (eventWriter == null) {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   157
            eventWriter = new PrettyWriter(pw); // default to pretty printer
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   158
        }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   159
        eventWriter.setStackDepth(stackDepth);
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   160
        if (eventFilter != null) {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   161
            eventFilter = addCache(eventFilter, eventType -> eventType.getId());
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   162
            eventWriter.setEventFilter(eventFilter);
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   163
        }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   164
        try {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   165
            eventWriter.print(file);
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   166
        } catch (IOException ioe) {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   167
            couldNotReadError(file, ioe);
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   168
        }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   169
        pw.flush();
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   170
    }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   171
54815
1e83e1a600cb 8221569: JFR tool produces incorrect output when both --categories and --events are specified
cito
parents: 52850
diff changeset
   172
    private void checkCommonError(Deque<String> options, String typo, String correct) throws UserSyntaxException {
1e83e1a600cb 8221569: JFR tool produces incorrect output when both --categories and --events are specified
cito
parents: 52850
diff changeset
   173
       if (typo.equals(options.peek())) {
1e83e1a600cb 8221569: JFR tool produces incorrect output when both --categories and --events are specified
cito
parents: 52850
diff changeset
   174
           throw new UserSyntaxException("unknown option " + typo + ", did you mean " + correct + "?");
1e83e1a600cb 8221569: JFR tool produces incorrect output when both --categories and --events are specified
cito
parents: 52850
diff changeset
   175
       }
1e83e1a600cb 8221569: JFR tool produces incorrect output when both --categories and --events are specified
cito
parents: 52850
diff changeset
   176
    }
1e83e1a600cb 8221569: JFR tool produces incorrect output when both --categories and --events are specified
cito
parents: 52850
diff changeset
   177
52850
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   178
    private static boolean acceptFormatterOption(Deque<String> options, EventPrintWriter eventWriter, String expected) throws UserSyntaxException {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   179
        if (expected.equals(options.peek())) {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   180
            if (eventWriter != null) {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   181
                throw new UserSyntaxException("only one format can be specified at a time");
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   182
            }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   183
            options.remove();
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   184
            return true;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   185
        }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   186
        return false;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   187
    }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   188
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   189
    private static <T, X> Predicate<T> addCache(final Predicate<T> filter, Function<T, X> cacheFunction) {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   190
        Map<X, Boolean> cache = new HashMap<>();
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   191
        return t -> cache.computeIfAbsent(cacheFunction.apply(t), x -> filter.test(t));
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   192
    }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   193
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   194
    private static <T> Predicate<T> recurseIfPossible(Predicate<T> filter) {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   195
        return x -> filter != null && filter.test(x);
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   196
    }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   197
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   198
    private static Predicate<EventType> addCategoryFilter(String filterText, Predicate<EventType> eventFilter) throws UserSyntaxException {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   199
        List<String> filters = explodeFilter(filterText);
54815
1e83e1a600cb 8221569: JFR tool produces incorrect output when both --categories and --events are specified
cito
parents: 52850
diff changeset
   200
        Predicate<EventType> newFilter = recurseIfPossible(eventType -> {
52850
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   201
            for (String category : eventType.getCategoryNames()) {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   202
                for (String filter : filters) {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   203
                    if (match(category, filter)) {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   204
                        return true;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   205
                    }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   206
                    if (category.contains(" ") && acronomify(category).equals(filter)) {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   207
                        return true;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   208
                    }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   209
                }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   210
            }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   211
            return false;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   212
        });
54815
1e83e1a600cb 8221569: JFR tool produces incorrect output when both --categories and --events are specified
cito
parents: 52850
diff changeset
   213
        return eventFilter == null ? newFilter : eventFilter.or(newFilter);
52850
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   214
    }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   215
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   216
    private static String acronomify(String multipleWords) {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   217
        boolean newWord = true;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   218
        String acronym = "";
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   219
        for (char c : multipleWords.toCharArray()) {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   220
            if (newWord) {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   221
                if (Character.isAlphabetic(c) && Character.isUpperCase(c)) {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   222
                    acronym += c;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   223
                }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   224
            }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   225
            newWord = Character.isWhitespace(c);
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   226
        }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   227
        return acronym;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   228
    }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   229
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   230
    private static Predicate<EventType> addEventFilter(String filterText, final Predicate<EventType> eventFilter) throws UserSyntaxException {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   231
        List<String> filters = explodeFilter(filterText);
54815
1e83e1a600cb 8221569: JFR tool produces incorrect output when both --categories and --events are specified
cito
parents: 52850
diff changeset
   232
        Predicate<EventType> newFilter = recurseIfPossible(eventType -> {
52850
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   233
            for (String filter : filters) {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   234
                String fullEventName = eventType.getName();
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   235
                if (match(fullEventName, filter)) {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   236
                    return true;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   237
                }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   238
                String eventName = fullEventName.substring(fullEventName.lastIndexOf(".") + 1);
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   239
                if (match(eventName, filter)) {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   240
                    return true;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   241
                }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   242
            }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   243
            return false;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   244
        });
54815
1e83e1a600cb 8221569: JFR tool produces incorrect output when both --categories and --events are specified
cito
parents: 52850
diff changeset
   245
        return eventFilter == null ? newFilter : eventFilter.or(newFilter);
52850
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   246
    }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   247
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   248
    private static boolean match(String text, String filter) {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   249
        if (filter.length() == 0) {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   250
            // empty filter string matches if string is empty
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   251
            return text.length() == 0;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   252
        }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   253
        if (filter.charAt(0) == '*') { // recursive check
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   254
            filter = filter.substring(1);
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   255
            for (int n = 0; n <= text.length(); n++) {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   256
                if (match(text.substring(n), filter))
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   257
                    return true;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   258
            }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   259
        } else if (text.length() == 0) {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   260
            // empty string and non-empty filter does not match
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   261
            return false;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   262
        } else if (filter.charAt(0) == '?') {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   263
            // eat any char and move on
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   264
            return match(text.substring(1), filter.substring(1));
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   265
        } else if (filter.charAt(0) == text.charAt(0)) {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   266
            // eat chars and move on
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   267
            return match(text.substring(1), filter.substring(1));
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   268
        }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   269
        return false;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   270
    }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   271
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   272
    private static List<String> explodeFilter(String filter) throws UserSyntaxException {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   273
        List<String> list = new ArrayList<>();
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   274
        for (String s : filter.split(",")) {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   275
            s = s.trim();
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   276
            if (!s.isEmpty()) {
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   277
                list.add(s);
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   278
            }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   279
        }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   280
        return list;
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   281
    }
f527b24990d7 8205516: JFR tool
egahlin
parents:
diff changeset
   282
}