src/jdk.jfr/share/classes/jdk/jfr/internal/Utils.java
author egahlin
Fri, 17 May 2019 16:02:27 +0200
branchJEP-349-branch
changeset 57360 5d043a159d5c
parent 53013 c8b2a408628b
child 57459 df39f8d8f4d6
permissions -rw-r--r--
Preview
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
     1
/*
57360
5d043a159d5c Preview
egahlin
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;
50745
a390cbb82d47 8203929: Limit amount of data for JFR.dump
egahlin
parents: 50113
diff changeset
    46
import java.time.LocalDateTime;
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    47
import java.util.ArrayList;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    48
import java.util.Arrays;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    49
import java.util.Collections;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    50
import java.util.HashMap;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    51
import java.util.List;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    52
import java.util.Map;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    53
import java.util.Objects;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    54
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    55
import jdk.internal.org.objectweb.asm.ClassReader;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    56
import jdk.internal.org.objectweb.asm.util.CheckClassAdapter;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    57
import jdk.jfr.Event;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    58
import jdk.jfr.FlightRecorderPermission;
50745
a390cbb82d47 8203929: Limit amount of data for JFR.dump
egahlin
parents: 50113
diff changeset
    59
import jdk.jfr.Recording;
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    60
import jdk.jfr.RecordingState;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    61
import jdk.jfr.internal.handlers.EventHandler;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    62
import jdk.jfr.internal.settings.PeriodSetting;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    63
import jdk.jfr.internal.settings.StackTraceSetting;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    64
import jdk.jfr.internal.settings.ThresholdSetting;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    65
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    66
public final class Utils {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    67
52901
3ba9ff4d4aaf 8212232: Wrong metadata for the configuration of the cutoff for old object sample events
egahlin
parents: 52850
diff changeset
    68
    private static final String INFINITY = "infinity";
3ba9ff4d4aaf 8212232: Wrong metadata for the configuration of the cutoff for old object sample events
egahlin
parents: 52850
diff changeset
    69
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    70
    private static Boolean SAVE_GENERATED;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    71
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    72
    public static final String EVENTS_PACKAGE_NAME = "jdk.jfr.events";
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    73
    public static final String INSTRUMENT_PACKAGE_NAME = "jdk.jfr.internal.instrument";
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    74
    public static final String HANDLERS_PACKAGE_NAME = "jdk.jfr.internal.handlers";
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    75
    public static final String REGISTER_EVENT = "registerEvent";
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    76
    public static final String ACCESS_FLIGHT_RECORDER = "accessFlightRecorder";
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    77
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    78
    private final static String LEGACY_EVENT_NAME_PREFIX = "com.oracle.jdk.";
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    79
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    80
    public static void checkAccessFlightRecorder() throws SecurityException {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    81
        SecurityManager sm = System.getSecurityManager();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    82
        if (sm != null) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    83
            sm.checkPermission(new FlightRecorderPermission(ACCESS_FLIGHT_RECORDER));
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    84
        }
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
    public static void checkRegisterPermission() throws SecurityException {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    88
        SecurityManager sm = System.getSecurityManager();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    89
        if (sm != null) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    90
            sm.checkPermission(new FlightRecorderPermission(REGISTER_EVENT));
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    91
        }
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
    private static enum TimespanUnit {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    95
        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
    96
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    97
        final String text;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    98
        final long amount;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
    99
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   100
        TimespanUnit(String unit, long amount) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   101
            this.text = unit;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   102
            this.amount = amount;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   103
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   104
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   105
53013
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   106
    // 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
   107
    private static String formatDataAmount(String formatter, long amount) {
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   108
        int exp = (int) (Math.log(Math.abs(amount)) / Math.log(1024));
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   109
        char unitPrefix = "kMGTPE".charAt(exp - 1);
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   110
        return String.format(formatter, amount / Math.pow(1024, exp), unitPrefix);
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   111
    }
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
    public static String formatBytesCompact(long bytes) {
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   114
        if (bytes < 1024) {
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   115
            return String.valueOf(bytes);
52850
f527b24990d7 8205516: JFR tool
egahlin
parents: 52334
diff changeset
   116
        }
53013
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   117
        return formatDataAmount("%.1f%cB", bytes);
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   118
    }
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
    public static String formatBits(long bits) {
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   121
        if (bits == 1 || bits == -1) {
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   122
            return bits + " bit";
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   123
        }
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   124
        if (bits < 1024 && bits > -1024) {
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   125
            return bits + " bits";
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   126
        }
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   127
        return formatDataAmount("%.1f %cbit", bits);
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   128
    }
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
    public static String formatBytes(long bytes) {
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   131
        if (bytes == 1 || bytes == -1) {
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   132
            return bytes + " byte";
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   133
        }
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   134
        if (bytes < 1024 && bytes > -1024) {
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   135
            return bytes + " bytes";
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   136
        }
53013
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   137
        return formatDataAmount("%.1f %cB", bytes);
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   138
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   139
53013
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   140
    public static String formatBytesPerSecond(long bytes) {
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   141
        if (bytes < 1024 && bytes > -1024) {
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   142
            return bytes + " byte/s";
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   143
        }
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   144
        return formatDataAmount("%.1f %cB/s", bytes);
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   145
    }
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
    public static String formatBitsPerSecond(long bits) {
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   148
        if (bits < 1024 && bits > -1024) {
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   149
            return bits + " bps";
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   150
        }
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   151
        return formatDataAmount("%.1f %cbps", bits);
c8b2a408628b 8215175: Inconsistencies in JFR event metadata
egahlin
parents: 52901
diff changeset
   152
    }
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   153
    public static String formatTimespan(Duration dValue, String separation) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   154
        if (dValue == null) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   155
            return "0";
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   156
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   157
        long value = dValue.toNanos();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   158
        TimespanUnit result = TimespanUnit.NANOSECONDS;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   159
        for (TimespanUnit unit : TimespanUnit.values()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   160
            result = unit;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   161
            long amount = unit.amount;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   162
            if (result == TimespanUnit.DAYS || value < amount || value % amount != 0) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   163
                break;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   164
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   165
            value /= amount;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   166
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   167
        return String.format("%d%s%s", value, separation, result.text);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   168
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   169
52901
3ba9ff4d4aaf 8212232: Wrong metadata for the configuration of the cutoff for old object sample events
egahlin
parents: 52850
diff changeset
   170
    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
   171
        if (INFINITY.equals(s)) {
3ba9ff4d4aaf 8212232: Wrong metadata for the configuration of the cutoff for old object sample events
egahlin
parents: 52850
diff changeset
   172
            return Long.MAX_VALUE;
3ba9ff4d4aaf 8212232: Wrong metadata for the configuration of the cutoff for old object sample events
egahlin
parents: 52850
diff changeset
   173
        }
3ba9ff4d4aaf 8212232: Wrong metadata for the configuration of the cutoff for old object sample events
egahlin
parents: 52850
diff changeset
   174
        return parseTimespan(s);
3ba9ff4d4aaf 8212232: Wrong metadata for the configuration of the cutoff for old object sample events
egahlin
parents: 52850
diff changeset
   175
    }
3ba9ff4d4aaf 8212232: Wrong metadata for the configuration of the cutoff for old object sample events
egahlin
parents: 52850
diff changeset
   176
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   177
    public static long parseTimespan(String s) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   178
        if (s.endsWith("ns")) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   179
            return Long.parseLong(s.substring(0, s.length() - 2).trim());
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   180
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   181
        if (s.endsWith("us")) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   182
            return NANOSECONDS.convert(Long.parseLong(s.substring(0, s.length() - 2).trim()), MICROSECONDS);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   183
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   184
        if (s.endsWith("ms")) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   185
            return NANOSECONDS.convert(Long.parseLong(s.substring(0, s.length() - 2).trim()), MILLISECONDS);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   186
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   187
        if (s.endsWith("s")) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   188
            return NANOSECONDS.convert(Long.parseLong(s.substring(0, s.length() - 1).trim()), SECONDS);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   189
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   190
        if (s.endsWith("m")) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   191
            return 60 * NANOSECONDS.convert(Long.parseLong(s.substring(0, s.length() - 1).trim()), SECONDS);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   192
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   193
        if (s.endsWith("h")) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   194
            return 60 * 60 * NANOSECONDS.convert(Long.parseLong(s.substring(0, s.length() - 1).trim()), SECONDS);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   195
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   196
        if (s.endsWith("d")) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   197
            return 24 * 60 * 60 * NANOSECONDS.convert(Long.parseLong(s.substring(0, s.length() - 1).trim()), SECONDS);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   198
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   199
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   200
        try {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   201
            Long.parseLong(s);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   202
        } catch (NumberFormatException nfe) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   203
            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
   204
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   205
        // Only accept values with units
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   206
        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
   207
    }
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
     * Return all annotations as they are visible in the source code
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   211
     *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   212
     * @param clazz class to return annotations from
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   213
     *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   214
     * @return list of annotation
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   215
     *
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   216
     */
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   217
    static List<Annotation> getAnnotations(Class<?> clazz) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   218
        List<Annotation> annos = new ArrayList<>();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   219
        for (Annotation a : clazz.getAnnotations()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   220
            annos.addAll(getAnnotation(a));
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   221
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   222
        return annos;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   223
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   224
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   225
    private static List<? extends Annotation> getAnnotation(Annotation a) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   226
        Class<?> annotated = a.annotationType();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   227
        Method valueMethod = getValueMethod(annotated);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   228
        if (valueMethod != null) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   229
            Class<?> returnType = valueMethod.getReturnType();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   230
            if (returnType.isArray()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   231
                Class<?> candidate = returnType.getComponentType();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   232
                Repeatable r = candidate.getAnnotation(Repeatable.class);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   233
                if (r != null) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   234
                    Class<?> repeatClass = r.value();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   235
                    if (annotated == repeatClass) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   236
                        return getAnnotationValues(a, valueMethod);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   237
                    }
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
        List<Annotation> annos = new ArrayList<>();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   242
        annos.add(a);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   243
        return annos;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   244
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   245
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   246
    static boolean isAfter(RecordingState stateToTest, RecordingState b) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   247
        return stateToTest.ordinal() > b.ordinal();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   248
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   249
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   250
    static boolean isBefore(RecordingState stateToTest, RecordingState b) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   251
        return stateToTest.ordinal() < b.ordinal();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   252
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   253
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   254
    static boolean isState(RecordingState stateToTest, RecordingState... states) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   255
        for (RecordingState s : states) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   256
            if (s == stateToTest) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   257
                return true;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   258
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   259
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   260
        return false;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   261
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   262
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   263
    private static List<Annotation> getAnnotationValues(Annotation a, Method valueMethod) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   264
        try {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   265
            return Arrays.asList((Annotation[]) valueMethod.invoke(a, new Object[0]));
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   266
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   267
            return new ArrayList<>();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   268
        }
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
    private static Method getValueMethod(Class<?> annotated) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   272
        try {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   273
            return annotated.getMethod("value", new Class<?>[0]);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   274
        } catch (NoSuchMethodException e) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   275
            return null;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   276
        }
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
    public static void touch(Path dumpFile) throws IOException {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   280
        RandomAccessFile raf = new RandomAccessFile(dumpFile.toFile(), "rw");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   281
        raf.close();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   282
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   283
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   284
    public static Class<?> unboxType(Class<?> t) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   285
        if (t == Integer.class) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   286
            return int.class;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   287
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   288
        if (t == Long.class) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   289
            return long.class;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   290
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   291
        if (t == Float.class) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   292
            return float.class;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   293
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   294
        if (t == Double.class) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   295
            return double.class;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   296
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   297
        if (t == Byte.class) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   298
            return byte.class;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   299
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   300
        if (t == Short.class) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   301
            return short.class;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   302
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   303
        if (t == Boolean.class) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   304
            return boolean.class;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   305
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   306
        if (t == Character.class) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   307
            return char.class;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   308
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   309
        return t;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   310
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   311
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   312
    static long nanosToTicks(long nanos) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   313
        return (long) (nanos * JVM.getJVM().getTimeConversionFactor());
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   314
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   315
52334
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   316
    static synchronized EventHandler getHandler(Class<? extends jdk.internal.event.Event> eventClass) {
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   317
        Utils.ensureValidEventSubclass(eventClass);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   318
        try {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   319
            Field f = eventClass.getDeclaredField(EventInstrumentation.FIELD_EVENT_HANDLER);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   320
            SecuritySupport.setAccessible(f);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   321
            return (EventHandler) f.get(null);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   322
        } catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException e) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   323
            throw new InternalError("Could not access event handler");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   324
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   325
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   326
52334
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   327
    static synchronized void setHandler(Class<? extends jdk.internal.event.Event> eventClass, EventHandler handler) {
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   328
        Utils.ensureValidEventSubclass(eventClass);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   329
        try {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   330
            Field field = eventClass.getDeclaredField(EventInstrumentation.FIELD_EVENT_HANDLER);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   331
            SecuritySupport.setAccessible(field);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   332
            field.set(null, handler);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   333
        } catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException e) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   334
            throw new InternalError("Could not access event handler");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   335
        }
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
    public static Map<String, String> sanitizeNullFreeStringMap(Map<String, String> settings) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   339
        HashMap<String, String> map = new HashMap<>(settings.size());
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   340
        for (Map.Entry<String, String> e : settings.entrySet()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   341
            String key = e.getKey();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   342
            if (key == null) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   343
                throw new NullPointerException("Null key is not allowed in map");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   344
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   345
            String value = e.getValue();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   346
            if (value == null) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   347
                throw new NullPointerException("Null value is not allowed in map");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   348
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   349
            map.put(key, value);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   350
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   351
        return map;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   352
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   353
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   354
    public static <T> List<T> sanitizeNullFreeList(List<T> elements, Class<T> clazz) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   355
        List<T> sanitized = new ArrayList<>(elements.size());
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   356
        for (T element : elements) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   357
            if (element == null) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   358
                throw new NullPointerException("Null is not an allowed element in list");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   359
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   360
            if (element.getClass() != clazz) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   361
                throw new ClassCastException();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   362
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   363
            sanitized.add(element);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   364
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   365
        return sanitized;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   366
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   367
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   368
    static List<Field> getVisibleEventFields(Class<?> clazz) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   369
        Utils.ensureValidEventSubclass(clazz);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   370
        List<Field> fields = new ArrayList<>();
52334
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   371
        for (Class<?> c = clazz; c != jdk.internal.event.Event.class; c = c.getSuperclass()) {
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   372
            for (Field field : c.getDeclaredFields()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   373
                // skip private field in base classes
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   374
                if (c == clazz || !Modifier.isPrivate(field.getModifiers())) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   375
                    fields.add(field);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   376
                }
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
        return fields;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   380
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   381
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   382
    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
   383
        if (jdk.internal.event.Event.class.isAssignableFrom(eventClass) && Modifier.isAbstract(eventClass.getModifiers())) {
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   384
            throw new IllegalArgumentException("Abstract event classes are not allowed");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   385
        }
52334
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   386
        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
   387
            throw new IllegalArgumentException("Must be a subclass to " + Event.class.getName());
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   388
        }
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
    public static void writeGeneratedASM(String className, byte[] bytes) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   392
        if (SAVE_GENERATED == null) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   393
            // We can't calculate value statically because it will force
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   394
            // initialization of SecuritySupport, which cause
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   395
            // UnsatisfiedLinkedError on JDK 8 or non-Oracle JDKs
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   396
            SAVE_GENERATED = SecuritySupport.getBooleanProperty("jfr.save.generated.asm");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   397
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   398
        if (SAVE_GENERATED) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   399
            try {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   400
                try (FileOutputStream fos = new FileOutputStream(className + ".class")) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   401
                    fos.write(bytes);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   402
                }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   403
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   404
                try (FileWriter fw = new FileWriter(className + ".asm"); PrintWriter pw = new PrintWriter(fw)) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   405
                    ClassReader cr = new ClassReader(bytes);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   406
                    CheckClassAdapter.verify(cr, true, pw);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   407
                }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   408
                Logger.log(LogTag.JFR_SYSTEM_BYTECODE, LogLevel.INFO, "Instrumented code saved to " + className + ".class and .asm");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   409
            } catch (IOException e) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   410
                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
   411
            }
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
52334
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   415
    public static void ensureInitialized(Class<? extends jdk.internal.event.Event> eventClass) {
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   416
        SecuritySupport.ensureClassIsInitialized(eventClass);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   417
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   418
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   419
    public static Object makePrimitiveArray(String typeName, List<Object> values) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   420
        int length = values.size();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   421
        switch (typeName) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   422
        case "int":
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   423
            int[] ints = new int[length];
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   424
            for (int i = 0; i < length; i++) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   425
                ints[i] = (int) values.get(i);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   426
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   427
            return ints;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   428
        case "long":
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   429
            long[] longs = new long[length];
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   430
            for (int i = 0; i < length; i++) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   431
                longs[i] = (long) values.get(i);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   432
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   433
            return longs;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   434
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   435
        case "float":
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   436
            float[] floats = new float[length];
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   437
            for (int i = 0; i < length; i++) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   438
                floats[i] = (float) values.get(i);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   439
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   440
            return floats;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   441
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   442
        case "double":
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   443
            double[] doubles = new double[length];
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   444
            for (int i = 0; i < length; i++) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   445
                doubles[i] = (double) values.get(i);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   446
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   447
            return doubles;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   448
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   449
        case "short":
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   450
            short[] shorts = new short[length];
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   451
            for (int i = 0; i < length; i++) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   452
                shorts[i] = (short) values.get(i);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   453
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   454
            return shorts;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   455
        case "char":
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   456
            char[] chars = new char[length];
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   457
            for (int i = 0; i < length; i++) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   458
                chars[i] = (char) values.get(i);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   459
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   460
            return chars;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   461
        case "byte":
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   462
            byte[] bytes = new byte[length];
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   463
            for (int i = 0; i < length; i++) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   464
                bytes[i] = (byte) values.get(i);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   465
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   466
            return bytes;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   467
        case "boolean":
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   468
            boolean[] booleans = new boolean[length];
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   469
            for (int i = 0; i < length; i++) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   470
                booleans[i] = (boolean) values.get(i);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   471
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   472
            return booleans;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   473
        case "java.lang.String":
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   474
            String[] strings = new String[length];
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   475
            for (int i = 0; i < length; i++) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   476
                strings[i] = (String) values.get(i);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   477
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   478
            return strings;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   479
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   480
        return null;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   481
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   482
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   483
    public static boolean isSettingVisible(Control c, boolean hasEventHook) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   484
        if (c instanceof ThresholdSetting) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   485
            return !hasEventHook;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   486
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   487
        if (c instanceof PeriodSetting) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   488
            return hasEventHook;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   489
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   490
        if (c instanceof StackTraceSetting) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   491
            return !hasEventHook;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   492
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   493
        return true;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   494
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   495
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   496
    public static boolean isSettingVisible(long typeId, boolean hasEventHook) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   497
        if (ThresholdSetting.isType(typeId)) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   498
            return !hasEventHook;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   499
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   500
        if (PeriodSetting.isType(typeId)) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   501
            return hasEventHook;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   502
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   503
        if (StackTraceSetting.isType(typeId)) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   504
            return !hasEventHook;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   505
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   506
        return true;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   507
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   508
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   509
    public static Type getValidType(Class<?> type, String name) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   510
        Objects.requireNonNull(type, "Null is not a valid type for value descriptor " + name);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   511
        if (type.isArray()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   512
            type = type.getComponentType();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   513
            if (type != String.class && !type.isPrimitive()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   514
                throw new IllegalArgumentException("Only arrays of primitives and Strings are allowed");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   515
            }
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
        Type knownType = Type.getKnownType(type);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   519
        if (knownType == null || knownType == Type.STACK_TRACE) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   520
            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
   521
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   522
        return knownType;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   523
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   524
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   525
    public static <T> List<T> smallUnmodifiable(List<T> list) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   526
        if (list.isEmpty()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   527
            return Collections.emptyList();
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   528
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   529
        if (list.size() == 1) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   530
            return Collections.singletonList(list.get(0));
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   531
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   532
        return Collections.unmodifiableList(list);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   533
    }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   534
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   535
    public static String upgradeLegacyJDKEvent(String eventName) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   536
        if (eventName.length() <= LEGACY_EVENT_NAME_PREFIX.length()) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   537
            return eventName;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   538
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   539
        if (eventName.startsWith(LEGACY_EVENT_NAME_PREFIX)) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   540
            int index = eventName.lastIndexOf(".");
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   541
            if (index == LEGACY_EVENT_NAME_PREFIX.length() - 1) {
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   542
                return Type.EVENT_NAME_PREFIX + eventName.substring(index + 1);
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   543
            }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   544
        }
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   545
        return eventName;
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   546
    }
50745
a390cbb82d47 8203929: Limit amount of data for JFR.dump
egahlin
parents: 50113
diff changeset
   547
52334
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   548
    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
   549
        Class<?> cMirror = Objects.requireNonNull(mirror);
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   550
        Class<?> cReal = Objects.requireNonNull(real);
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   551
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   552
        while (cReal != null) {
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   553
            Map<String, Field> mirrorFields = new HashMap<>();
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   554
            if (cMirror != null) {
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   555
                for (Field f : cMirror.getDeclaredFields()) {
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   556
                    if (isSupportedType(f.getType())) {
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   557
                        mirrorFields.put(f.getName(), f);
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   558
                    }
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
            for (Field realField : cReal.getDeclaredFields()) {
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   562
                if (isSupportedType(realField.getType())) {
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   563
                    String fieldName = realField.getName();
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   564
                    Field mirrorField = mirrorFields.get(fieldName);
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   565
                    if (mirrorField == null) {
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   566
                        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
   567
                    }
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   568
                    if (realField.getModifiers() != mirrorField.getModifiers()) {
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   569
                        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
   570
                    }
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   571
                    mirrorFields.remove(fieldName);
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   572
                }
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
            if (!mirrorFields.isEmpty()) {
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   575
                throw new InternalError(
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   576
                        "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
   577
            }
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   578
            if (cMirror != null) {
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   579
                cMirror = cMirror.getSuperclass();
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   580
            }
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   581
            cReal = cReal.getSuperclass();
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   582
        }
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
    private static boolean isSupportedType(Class<?> type) {
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   586
        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
   587
            return false;
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   588
        }
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   589
        return Type.isValidJavaFieldType(type.getName());
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   590
    }
a181612f0715 8203629: Produce events in the JDK without a dependency on jdk.jfr
egahlin
parents: 50745
diff changeset
   591
50745
a390cbb82d47 8203929: Limit amount of data for JFR.dump
egahlin
parents: 50113
diff changeset
   592
    public static String makeFilename(Recording recording) {
a390cbb82d47 8203929: Limit amount of data for JFR.dump
egahlin
parents: 50113
diff changeset
   593
        String pid = JVM.getJVM().getPid();
a390cbb82d47 8203929: Limit amount of data for JFR.dump
egahlin
parents: 50113
diff changeset
   594
        String date = Repository.REPO_DATE_FORMAT.format(LocalDateTime.now());
a390cbb82d47 8203929: Limit amount of data for JFR.dump
egahlin
parents: 50113
diff changeset
   595
        String idText = recording == null ? "" :  "-id-" + Long.toString(recording.getId());
a390cbb82d47 8203929: Limit amount of data for JFR.dump
egahlin
parents: 50113
diff changeset
   596
        return "hotspot-" + "pid-" + pid + idText + "-" + date + ".jfr";
a390cbb82d47 8203929: Limit amount of data for JFR.dump
egahlin
parents: 50113
diff changeset
   597
    }
57360
5d043a159d5c Preview
egahlin
parents: 53013
diff changeset
   598
    public static void takeNap(long millis) {
5d043a159d5c Preview
egahlin
parents: 53013
diff changeset
   599
        try {
5d043a159d5c Preview
egahlin
parents: 53013
diff changeset
   600
            Thread.sleep(millis);
5d043a159d5c Preview
egahlin
parents: 53013
diff changeset
   601
        } catch (InterruptedException e) {
5d043a159d5c Preview
egahlin
parents: 53013
diff changeset
   602
        }
5d043a159d5c Preview
egahlin
parents: 53013
diff changeset
   603
    }
50113
caf115bb98ad 8199712: Flight Recorder
egahlin
parents:
diff changeset
   604
}