src/jdk.jfr/share/classes/jdk/jfr/internal/Utils.java
author mgronlun
Wed, 30 Oct 2019 19:43:52 +0100
changeset 58863 c16ac7a2eba4
parent 53013 c8b2a408628b
child 59312 43eee1237934
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:
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     1
/*
58863
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 53013
diff changeset
     2
 * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     4
 *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    10
 *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    15
 * accompanied this code).
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    16
 *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    20
 *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    23
 * questions.
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    24
 */
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    25
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    26
package jdk.jfr.internal;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    27
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    28
import static java.util.concurrent.TimeUnit.MICROSECONDS;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    29
import static java.util.concurrent.TimeUnit.MILLISECONDS;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    30
import static java.util.concurrent.TimeUnit.NANOSECONDS;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    31
import static java.util.concurrent.TimeUnit.SECONDS;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    32
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    33
import java.io.FileOutputStream;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    34
import java.io.FileWriter;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    35
import java.io.IOException;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    36
import java.io.PrintWriter;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    37
import java.io.RandomAccessFile;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    38
import java.lang.annotation.Annotation;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    39
import java.lang.annotation.Repeatable;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    40
import java.lang.reflect.Field;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    41
import java.lang.reflect.InvocationTargetException;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    42
import java.lang.reflect.Method;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    43
import java.lang.reflect.Modifier;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    44
import java.nio.file.Path;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    45
import java.time.Duration;
58863
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 53013
diff changeset
    46
import java.time.Instant;
50745
a390cbb82d47 8203929: Limit amount of data for JFR.dump
egahlin
parents: 50113
diff changeset
    47
import java.time.LocalDateTime;
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    48
import java.util.ArrayList;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    49
import java.util.Arrays;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    50
import java.util.Collections;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    51
import java.util.HashMap;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    52
import java.util.List;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    53
import java.util.Map;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    54
import java.util.Objects;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    55
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    56
import jdk.internal.org.objectweb.asm.ClassReader;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    57
import jdk.internal.org.objectweb.asm.util.CheckClassAdapter;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    58
import jdk.jfr.Event;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    59
import jdk.jfr.FlightRecorderPermission;
50745
a390cbb82d47 8203929: Limit amount of data for JFR.dump
egahlin
parents: 50113
diff changeset
    60
import jdk.jfr.Recording;
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    61
import jdk.jfr.RecordingState;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    62
import jdk.jfr.internal.handlers.EventHandler;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    63
import jdk.jfr.internal.settings.PeriodSetting;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    64
import jdk.jfr.internal.settings.StackTraceSetting;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    65
import jdk.jfr.internal.settings.ThresholdSetting;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    66
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    67
public final class Utils {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    68
58863
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 53013
diff changeset
    69
    private static final Object flushObject = new Object();
52901
3ba9ff4d4aaf 8212232: Wrong metadata for the configuration of the cutoff for old object sample events
egahlin
parents: 52850
diff changeset
    70
    private static final String INFINITY = "infinity";
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    71
    public static final String EVENTS_PACKAGE_NAME = "jdk.jfr.events";
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    72
    public static final String INSTRUMENT_PACKAGE_NAME = "jdk.jfr.internal.instrument";
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    73
    public static final String HANDLERS_PACKAGE_NAME = "jdk.jfr.internal.handlers";
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    74
    public static final String REGISTER_EVENT = "registerEvent";
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    75
    public static final String ACCESS_FLIGHT_RECORDER = "accessFlightRecorder";
58863
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 53013
diff changeset
    76
    private final static String LEGACY_EVENT_NAME_PREFIX = "com.oracle.jdk.";
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    77
58863
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 53013
diff changeset
    78
    private static Boolean SAVE_GENERATED;
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 53013
diff changeset
    79
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    80
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    81
    public static void checkAccessFlightRecorder() throws SecurityException {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    82
        SecurityManager sm = System.getSecurityManager();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    83
        if (sm != null) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    84
            sm.checkPermission(new FlightRecorderPermission(ACCESS_FLIGHT_RECORDER));
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    85
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    86
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    87
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    88
    public static void checkRegisterPermission() throws SecurityException {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    89
        SecurityManager sm = System.getSecurityManager();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    90
        if (sm != null) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    91
            sm.checkPermission(new FlightRecorderPermission(REGISTER_EVENT));
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    92
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    93
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    94
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    95
    private static enum TimespanUnit {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    96
        NANOSECONDS("ns", 1000), MICROSECONDS("us", 1000), MILLISECONDS("ms", 1000), SECONDS("s", 60), MINUTES("m", 60), HOURS("h", 24), DAYS("d", 7);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    97
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    98
        final String text;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    99
        final long amount;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   100
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   101
        TimespanUnit(String unit, long amount) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   102
            this.text = unit;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   103
            this.amount = amount;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   104
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   105
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   106
53013
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   107
    // Tjis method can't handle Long.MIN_VALUE because absolute value is negative
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   108
    private static String formatDataAmount(String formatter, long amount) {
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   109
        int exp = (int) (Math.log(Math.abs(amount)) / Math.log(1024));
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   110
        char unitPrefix = "kMGTPE".charAt(exp - 1);
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   111
        return String.format(formatter, amount / Math.pow(1024, exp), unitPrefix);
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   112
    }
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   113
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   114
    public static String formatBytesCompact(long bytes) {
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   115
        if (bytes < 1024) {
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   116
            return String.valueOf(bytes);
52850
f527b24990d7 8205516: JFR tool
egahlin
parents: 52334
diff changeset
   117
        }
53013
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   118
        return formatDataAmount("%.1f%cB", bytes);
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   119
    }
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   120
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   121
    public static String formatBits(long bits) {
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   122
        if (bits == 1 || bits == -1) {
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   123
            return bits + " bit";
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   124
        }
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   125
        if (bits < 1024 && bits > -1024) {
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   126
            return bits + " bits";
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   127
        }
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   128
        return formatDataAmount("%.1f %cbit", bits);
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   129
    }
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   130
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   131
    public static String formatBytes(long bytes) {
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   132
        if (bytes == 1 || bytes == -1) {
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   133
            return bytes + " byte";
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   134
        }
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   135
        if (bytes < 1024 && bytes > -1024) {
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   136
            return bytes + " bytes";
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   137
        }
53013
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   138
        return formatDataAmount("%.1f %cB", bytes);
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   139
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   140
53013
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   141
    public static String formatBytesPerSecond(long bytes) {
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   142
        if (bytes < 1024 && bytes > -1024) {
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   143
            return bytes + " byte/s";
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   144
        }
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   145
        return formatDataAmount("%.1f %cB/s", bytes);
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   146
    }
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   147
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   148
    public static String formatBitsPerSecond(long bits) {
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   149
        if (bits < 1024 && bits > -1024) {
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   150
            return bits + " bps";
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   151
        }
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   152
        return formatDataAmount("%.1f %cbps", bits);
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   153
    }
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   154
    public static String formatTimespan(Duration dValue, String separation) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   155
        if (dValue == null) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   156
            return "0";
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   157
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   158
        long value = dValue.toNanos();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   159
        TimespanUnit result = TimespanUnit.NANOSECONDS;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   160
        for (TimespanUnit unit : TimespanUnit.values()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   161
            result = unit;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   162
            long amount = unit.amount;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   163
            if (result == TimespanUnit.DAYS || value < amount || value % amount != 0) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   164
                break;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   165
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   166
            value /= amount;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   167
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   168
        return String.format("%d%s%s", value, separation, result.text);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   169
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   170
52901
3ba9ff4d4aaf 8212232: Wrong metadata for the configuration of the cutoff for old object sample events
egahlin
parents: 52850
diff changeset
   171
    public static long parseTimespanWithInfinity(String s) {
3ba9ff4d4aaf 8212232: Wrong metadata for the configuration of the cutoff for old object sample events
egahlin
parents: 52850
diff changeset
   172
        if (INFINITY.equals(s)) {
3ba9ff4d4aaf 8212232: Wrong metadata for the configuration of the cutoff for old object sample events
egahlin
parents: 52850
diff changeset
   173
            return Long.MAX_VALUE;
3ba9ff4d4aaf 8212232: Wrong metadata for the configuration of the cutoff for old object sample events
egahlin
parents: 52850
diff changeset
   174
        }
3ba9ff4d4aaf 8212232: Wrong metadata for the configuration of the cutoff for old object sample events
egahlin
parents: 52850
diff changeset
   175
        return parseTimespan(s);
3ba9ff4d4aaf 8212232: Wrong metadata for the configuration of the cutoff for old object sample events
egahlin
parents: 52850
diff changeset
   176
    }
3ba9ff4d4aaf 8212232: Wrong metadata for the configuration of the cutoff for old object sample events
egahlin
parents: 52850
diff changeset
   177
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   178
    public static long parseTimespan(String s) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   179
        if (s.endsWith("ns")) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   180
            return Long.parseLong(s.substring(0, s.length() - 2).trim());
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   181
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   182
        if (s.endsWith("us")) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   183
            return NANOSECONDS.convert(Long.parseLong(s.substring(0, s.length() - 2).trim()), MICROSECONDS);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   184
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   185
        if (s.endsWith("ms")) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   186
            return NANOSECONDS.convert(Long.parseLong(s.substring(0, s.length() - 2).trim()), MILLISECONDS);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   187
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   188
        if (s.endsWith("s")) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   189
            return NANOSECONDS.convert(Long.parseLong(s.substring(0, s.length() - 1).trim()), SECONDS);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   190
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   191
        if (s.endsWith("m")) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   192
            return 60 * NANOSECONDS.convert(Long.parseLong(s.substring(0, s.length() - 1).trim()), SECONDS);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   193
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   194
        if (s.endsWith("h")) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   195
            return 60 * 60 * NANOSECONDS.convert(Long.parseLong(s.substring(0, s.length() - 1).trim()), SECONDS);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   196
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   197
        if (s.endsWith("d")) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   198
            return 24 * 60 * 60 * NANOSECONDS.convert(Long.parseLong(s.substring(0, s.length() - 1).trim()), SECONDS);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   199
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   200
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   201
        try {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   202
            Long.parseLong(s);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   203
        } catch (NumberFormatException nfe) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   204
            throw new NumberFormatException("'" + s + "' is not a valid timespan. Shoule be numeric value followed by a unit, i.e. 20 ms. Valid units are ns, us, s, m, h and d.");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   205
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   206
        // Only accept values with units
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   207
        throw new NumberFormatException("Timespan + '" + s + "' is missing unit. Valid units are ns, us, s, m, h and d.");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   208
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   209
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   210
    /**
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   211
     * Return all annotations as they are visible in the source code
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   212
     *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   213
     * @param clazz class to return annotations from
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   214
     *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   215
     * @return list of annotation
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   216
     *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   217
     */
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   218
    static List<Annotation> getAnnotations(Class<?> clazz) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   219
        List<Annotation> annos = new ArrayList<>();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   220
        for (Annotation a : clazz.getAnnotations()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   221
            annos.addAll(getAnnotation(a));
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   222
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   223
        return annos;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   224
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   225
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   226
    private static List<? extends Annotation> getAnnotation(Annotation a) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   227
        Class<?> annotated = a.annotationType();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   228
        Method valueMethod = getValueMethod(annotated);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   229
        if (valueMethod != null) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   230
            Class<?> returnType = valueMethod.getReturnType();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   231
            if (returnType.isArray()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   232
                Class<?> candidate = returnType.getComponentType();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   233
                Repeatable r = candidate.getAnnotation(Repeatable.class);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   234
                if (r != null) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   235
                    Class<?> repeatClass = r.value();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   236
                    if (annotated == repeatClass) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   237
                        return getAnnotationValues(a, valueMethod);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   238
                    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   239
                }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   240
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   241
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   242
        List<Annotation> annos = new ArrayList<>();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   243
        annos.add(a);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   244
        return annos;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   245
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   246
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   247
    static boolean isAfter(RecordingState stateToTest, RecordingState b) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   248
        return stateToTest.ordinal() > b.ordinal();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   249
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   250
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   251
    static boolean isBefore(RecordingState stateToTest, RecordingState b) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   252
        return stateToTest.ordinal() < b.ordinal();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   253
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   254
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   255
    static boolean isState(RecordingState stateToTest, RecordingState... states) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   256
        for (RecordingState s : states) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   257
            if (s == stateToTest) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   258
                return true;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   259
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   260
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   261
        return false;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   262
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   263
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   264
    private static List<Annotation> getAnnotationValues(Annotation a, Method valueMethod) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   265
        try {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   266
            return Arrays.asList((Annotation[]) valueMethod.invoke(a, new Object[0]));
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   267
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   268
            return new ArrayList<>();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   269
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   270
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   271
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   272
    private static Method getValueMethod(Class<?> annotated) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   273
        try {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   274
            return annotated.getMethod("value", new Class<?>[0]);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   275
        } catch (NoSuchMethodException e) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   276
            return null;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   277
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   278
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   279
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   280
    public static void touch(Path dumpFile) throws IOException {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   281
        RandomAccessFile raf = new RandomAccessFile(dumpFile.toFile(), "rw");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   282
        raf.close();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   283
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   284
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   285
    public static Class<?> unboxType(Class<?> t) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   286
        if (t == Integer.class) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   287
            return int.class;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   288
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   289
        if (t == Long.class) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   290
            return long.class;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   291
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   292
        if (t == Float.class) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   293
            return float.class;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   294
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   295
        if (t == Double.class) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   296
            return double.class;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   297
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   298
        if (t == Byte.class) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   299
            return byte.class;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   300
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   301
        if (t == Short.class) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   302
            return short.class;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   303
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   304
        if (t == Boolean.class) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   305
            return boolean.class;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   306
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   307
        if (t == Character.class) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   308
            return char.class;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   309
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   310
        return t;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   311
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   312
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   313
    static long nanosToTicks(long nanos) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   314
        return (long) (nanos * JVM.getJVM().getTimeConversionFactor());
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   315
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   316
52334
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   317
    static synchronized EventHandler getHandler(Class<? extends jdk.internal.event.Event> eventClass) {
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   318
        Utils.ensureValidEventSubclass(eventClass);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   319
        try {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   320
            Field f = eventClass.getDeclaredField(EventInstrumentation.FIELD_EVENT_HANDLER);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   321
            SecuritySupport.setAccessible(f);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   322
            return (EventHandler) f.get(null);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   323
        } catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException e) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   324
            throw new InternalError("Could not access event handler");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   325
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   326
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   327
52334
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   328
    static synchronized void setHandler(Class<? extends jdk.internal.event.Event> eventClass, EventHandler handler) {
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   329
        Utils.ensureValidEventSubclass(eventClass);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   330
        try {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   331
            Field field = eventClass.getDeclaredField(EventInstrumentation.FIELD_EVENT_HANDLER);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   332
            SecuritySupport.setAccessible(field);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   333
            field.set(null, handler);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   334
        } catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException e) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   335
            throw new InternalError("Could not access event handler");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   336
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   337
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   338
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   339
    public static Map<String, String> sanitizeNullFreeStringMap(Map<String, String> settings) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   340
        HashMap<String, String> map = new HashMap<>(settings.size());
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   341
        for (Map.Entry<String, String> e : settings.entrySet()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   342
            String key = e.getKey();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   343
            if (key == null) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   344
                throw new NullPointerException("Null key is not allowed in map");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   345
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   346
            String value = e.getValue();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   347
            if (value == null) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   348
                throw new NullPointerException("Null value is not allowed in map");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   349
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   350
            map.put(key, value);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   351
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   352
        return map;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   353
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   354
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   355
    public static <T> List<T> sanitizeNullFreeList(List<T> elements, Class<T> clazz) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   356
        List<T> sanitized = new ArrayList<>(elements.size());
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   357
        for (T element : elements) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   358
            if (element == null) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   359
                throw new NullPointerException("Null is not an allowed element in list");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   360
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   361
            if (element.getClass() != clazz) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   362
                throw new ClassCastException();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   363
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   364
            sanitized.add(element);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   365
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   366
        return sanitized;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   367
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   368
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   369
    static List<Field> getVisibleEventFields(Class<?> clazz) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   370
        Utils.ensureValidEventSubclass(clazz);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   371
        List<Field> fields = new ArrayList<>();
52334
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   372
        for (Class<?> c = clazz; c != jdk.internal.event.Event.class; c = c.getSuperclass()) {
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   373
            for (Field field : c.getDeclaredFields()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   374
                // skip private field in base classes
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   375
                if (c == clazz || !Modifier.isPrivate(field.getModifiers())) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   376
                    fields.add(field);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   377
                }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   378
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   379
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   380
        return fields;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   381
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   382
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   383
    public static void ensureValidEventSubclass(Class<?> eventClass) {
52334
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   384
        if (jdk.internal.event.Event.class.isAssignableFrom(eventClass) && Modifier.isAbstract(eventClass.getModifiers())) {
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   385
            throw new IllegalArgumentException("Abstract event classes are not allowed");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   386
        }
52334
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   387
        if (eventClass == Event.class || eventClass == jdk.internal.event.Event.class || !jdk.internal.event.Event.class.isAssignableFrom(eventClass)) {
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   388
            throw new IllegalArgumentException("Must be a subclass to " + Event.class.getName());
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   389
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   390
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   391
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   392
    public static void writeGeneratedASM(String className, byte[] bytes) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   393
        if (SAVE_GENERATED == null) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   394
            // We can't calculate value statically because it will force
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   395
            // initialization of SecuritySupport, which cause
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   396
            // UnsatisfiedLinkedError on JDK 8 or non-Oracle JDKs
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   397
            SAVE_GENERATED = SecuritySupport.getBooleanProperty("jfr.save.generated.asm");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   398
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   399
        if (SAVE_GENERATED) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   400
            try {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   401
                try (FileOutputStream fos = new FileOutputStream(className + ".class")) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   402
                    fos.write(bytes);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   403
                }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   404
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   405
                try (FileWriter fw = new FileWriter(className + ".asm"); PrintWriter pw = new PrintWriter(fw)) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   406
                    ClassReader cr = new ClassReader(bytes);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   407
                    CheckClassAdapter.verify(cr, true, pw);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   408
                }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   409
                Logger.log(LogTag.JFR_SYSTEM_BYTECODE, LogLevel.INFO, "Instrumented code saved to " + className + ".class and .asm");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   410
            } catch (IOException e) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   411
                Logger.log(LogTag.JFR_SYSTEM_BYTECODE, LogLevel.INFO, "Could not save instrumented code, for " + className + ".class and .asm");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   412
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   413
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   414
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   415
52334
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   416
    public static void ensureInitialized(Class<? extends jdk.internal.event.Event> eventClass) {
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   417
        SecuritySupport.ensureClassIsInitialized(eventClass);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   418
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   419
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   420
    public static Object makePrimitiveArray(String typeName, List<Object> values) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   421
        int length = values.size();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   422
        switch (typeName) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   423
        case "int":
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   424
            int[] ints = new int[length];
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   425
            for (int i = 0; i < length; i++) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   426
                ints[i] = (int) values.get(i);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   427
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   428
            return ints;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   429
        case "long":
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   430
            long[] longs = new long[length];
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   431
            for (int i = 0; i < length; i++) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   432
                longs[i] = (long) values.get(i);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   433
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   434
            return longs;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   435
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   436
        case "float":
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   437
            float[] floats = new float[length];
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   438
            for (int i = 0; i < length; i++) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   439
                floats[i] = (float) values.get(i);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   440
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   441
            return floats;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   442
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   443
        case "double":
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   444
            double[] doubles = new double[length];
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   445
            for (int i = 0; i < length; i++) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   446
                doubles[i] = (double) values.get(i);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   447
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   448
            return doubles;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   449
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   450
        case "short":
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   451
            short[] shorts = new short[length];
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   452
            for (int i = 0; i < length; i++) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   453
                shorts[i] = (short) values.get(i);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   454
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   455
            return shorts;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   456
        case "char":
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   457
            char[] chars = new char[length];
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   458
            for (int i = 0; i < length; i++) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   459
                chars[i] = (char) values.get(i);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   460
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   461
            return chars;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   462
        case "byte":
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   463
            byte[] bytes = new byte[length];
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   464
            for (int i = 0; i < length; i++) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   465
                bytes[i] = (byte) values.get(i);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   466
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   467
            return bytes;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   468
        case "boolean":
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   469
            boolean[] booleans = new boolean[length];
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   470
            for (int i = 0; i < length; i++) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   471
                booleans[i] = (boolean) values.get(i);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   472
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   473
            return booleans;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   474
        case "java.lang.String":
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   475
            String[] strings = new String[length];
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   476
            for (int i = 0; i < length; i++) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   477
                strings[i] = (String) values.get(i);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   478
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   479
            return strings;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   480
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   481
        return null;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   482
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   483
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   484
    public static boolean isSettingVisible(Control c, boolean hasEventHook) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   485
        if (c instanceof ThresholdSetting) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   486
            return !hasEventHook;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   487
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   488
        if (c instanceof PeriodSetting) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   489
            return hasEventHook;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   490
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   491
        if (c instanceof StackTraceSetting) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   492
            return !hasEventHook;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   493
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   494
        return true;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   495
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   496
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   497
    public static boolean isSettingVisible(long typeId, boolean hasEventHook) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   498
        if (ThresholdSetting.isType(typeId)) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   499
            return !hasEventHook;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   500
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   501
        if (PeriodSetting.isType(typeId)) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   502
            return hasEventHook;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   503
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   504
        if (StackTraceSetting.isType(typeId)) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   505
            return !hasEventHook;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   506
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   507
        return true;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   508
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   509
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   510
    public static Type getValidType(Class<?> type, String name) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   511
        Objects.requireNonNull(type, "Null is not a valid type for value descriptor " + name);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   512
        if (type.isArray()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   513
            type = type.getComponentType();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   514
            if (type != String.class && !type.isPrimitive()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   515
                throw new IllegalArgumentException("Only arrays of primitives and Strings are allowed");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   516
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   517
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   518
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   519
        Type knownType = Type.getKnownType(type);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   520
        if (knownType == null || knownType == Type.STACK_TRACE) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   521
            throw new IllegalArgumentException("Only primitive types, java.lang.Thread, java.lang.String and java.lang.Class are allowed for value descriptors. " + type.getName());
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   522
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   523
        return knownType;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   524
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   525
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   526
    public static <T> List<T> smallUnmodifiable(List<T> list) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   527
        if (list.isEmpty()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   528
            return Collections.emptyList();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   529
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   530
        if (list.size() == 1) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   531
            return Collections.singletonList(list.get(0));
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   532
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   533
        return Collections.unmodifiableList(list);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   534
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   535
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   536
    public static String upgradeLegacyJDKEvent(String eventName) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   537
        if (eventName.length() <= LEGACY_EVENT_NAME_PREFIX.length()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   538
            return eventName;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   539
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   540
        if (eventName.startsWith(LEGACY_EVENT_NAME_PREFIX)) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   541
            int index = eventName.lastIndexOf(".");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   542
            if (index == LEGACY_EVENT_NAME_PREFIX.length() - 1) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   543
                return Type.EVENT_NAME_PREFIX + eventName.substring(index + 1);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   544
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   545
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   546
        return eventName;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   547
    }
50745
a390cbb82d47 8203929: Limit amount of data for JFR.dump
egahlin
parents: 50113
diff changeset
   548
52334
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   549
    public static void verifyMirror(Class<?> mirror, Class<?> real) {
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   550
        Class<?> cMirror = Objects.requireNonNull(mirror);
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   551
        Class<?> cReal = Objects.requireNonNull(real);
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   552
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   553
        while (cReal != null) {
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   554
            Map<String, Field> mirrorFields = new HashMap<>();
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   555
            if (cMirror != null) {
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   556
                for (Field f : cMirror.getDeclaredFields()) {
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   557
                    if (isSupportedType(f.getType())) {
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   558
                        mirrorFields.put(f.getName(), f);
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   559
                    }
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   560
                }
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   561
            }
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   562
            for (Field realField : cReal.getDeclaredFields()) {
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   563
                if (isSupportedType(realField.getType())) {
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   564
                    String fieldName = realField.getName();
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   565
                    Field mirrorField = mirrorFields.get(fieldName);
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   566
                    if (mirrorField == null) {
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   567
                        throw new InternalError("Missing mirror field for " + cReal.getName() + "#" + fieldName);
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   568
                    }
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   569
                    if (realField.getModifiers() != mirrorField.getModifiers()) {
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   570
                        throw new InternalError("Incorrect modifier for mirror field "+ cMirror.getName() + "#" + fieldName);
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   571
                    }
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   572
                    mirrorFields.remove(fieldName);
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   573
                }
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   574
            }
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   575
            if (!mirrorFields.isEmpty()) {
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   576
                throw new InternalError(
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   577
                        "Found additional fields in mirror class " + cMirror.getName() + " " + mirrorFields.keySet());
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   578
            }
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   579
            if (cMirror != null) {
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   580
                cMirror = cMirror.getSuperclass();
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   581
            }
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   582
            cReal = cReal.getSuperclass();
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   583
        }
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   584
    }
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   585
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   586
    private static boolean isSupportedType(Class<?> type) {
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   587
        if (Modifier.isTransient(type.getModifiers()) || Modifier.isStatic(type.getModifiers())) {
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   588
            return false;
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   589
        }
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   590
        return Type.isValidJavaFieldType(type.getName());
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   591
    }
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   592
50745
a390cbb82d47 8203929: Limit amount of data for JFR.dump
egahlin
parents: 50113
diff changeset
   593
    public static String makeFilename(Recording recording) {
a390cbb82d47 8203929: Limit amount of data for JFR.dump
egahlin
parents: 50113
diff changeset
   594
        String pid = JVM.getJVM().getPid();
a390cbb82d47 8203929: Limit amount of data for JFR.dump
egahlin
parents: 50113
diff changeset
   595
        String date = Repository.REPO_DATE_FORMAT.format(LocalDateTime.now());
a390cbb82d47 8203929: Limit amount of data for JFR.dump
egahlin
parents: 50113
diff changeset
   596
        String idText = recording == null ? "" :  "-id-" + Long.toString(recording.getId());
a390cbb82d47 8203929: Limit amount of data for JFR.dump
egahlin
parents: 50113
diff changeset
   597
        return "hotspot-" + "pid-" + pid + idText + "-" + date + ".jfr";
a390cbb82d47 8203929: Limit amount of data for JFR.dump
egahlin
parents: 50113
diff changeset
   598
    }
58863
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 53013
diff changeset
   599
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 53013
diff changeset
   600
    public static void takeNap(long millis) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 53013
diff changeset
   601
        try {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 53013
diff changeset
   602
            Thread.sleep(millis);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 53013
diff changeset
   603
        } catch (InterruptedException e) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 53013
diff changeset
   604
            // ok
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 53013
diff changeset
   605
        }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 53013
diff changeset
   606
    }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 53013
diff changeset
   607
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 53013
diff changeset
   608
    public static void notifyFlush() {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 53013
diff changeset
   609
        synchronized (flushObject) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 53013
diff changeset
   610
            flushObject.notifyAll();
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 53013
diff changeset
   611
        }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 53013
diff changeset
   612
    }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 53013
diff changeset
   613
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 53013
diff changeset
   614
    public static void waitFlush(long timeOut) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 53013
diff changeset
   615
        synchronized (flushObject) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 53013
diff changeset
   616
            try {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 53013
diff changeset
   617
                flushObject.wait(timeOut);
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 53013
diff changeset
   618
            } catch (InterruptedException e) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 53013
diff changeset
   619
                // OK
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 53013
diff changeset
   620
            }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 53013
diff changeset
   621
        }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 53013
diff changeset
   622
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 53013
diff changeset
   623
    }
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 53013
diff changeset
   624
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 53013
diff changeset
   625
    public static long timeToNanos(Instant timestamp) {
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 53013
diff changeset
   626
        return timestamp.getEpochSecond() * 1_000_000_000L + timestamp.getNano();
c16ac7a2eba4 8226511: Implement JFR Event Streaming
mgronlun
parents: 53013
diff changeset
   627
    }
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   628
}