test/jdk/jdk/jfr/jvm/TestClearStaleConstants.java
author mgronlun
Thu, 24 Oct 2019 16:37:22 +0200
changeset 58786 7909763ad193
permissions -rw-r--r--
8231081: TestMetadataRetention fails due to missing symbol id Reviewed-by: egahlin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58786
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
     1
/*
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
     4
 *
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    10
 *
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    15
 * accompanied this code).
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    16
 *
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    20
 *
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    23
 * questions.
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    24
 */
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    25
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    26
package jdk.jfr.jvm;
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    27
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    28
import java.time.Duration;
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    29
import java.util.List;
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    30
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    31
import jdk.jfr.consumer.RecordedClass;
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    32
import jdk.jfr.consumer.RecordedClassLoader;
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    33
import jdk.jfr.consumer.RecordedEvent;
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    34
import jdk.jfr.internal.JVM;
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    35
import jdk.jfr.Recording;
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    36
import jdk.test.lib.Asserts;
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    37
import jdk.test.lib.jfr.EventNames;
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    38
import jdk.test.lib.jfr.Events;
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    39
import jdk.test.lib.jfr.TestClassLoader;
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    40
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    41
/**
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    42
 * @test
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    43
 * @bug 8231081
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    44
 * @key jfr
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    45
 * @requires vm.hasJFR
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    46
 * @modules jdk.jfr/jdk.jfr.internal
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    47
 * @library /test/lib /test/jdk
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    48
 * @run main/othervm -Xlog:class+unload -Xlog:gc -Xmx16m jdk.jfr.jvm.TestClearStaleConstants
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    49
 */
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    50
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    51
/**
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    52
 * System.gc() will trigger class unloading if -XX:+ExplicitGCInvokesConcurrent is NOT set.
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    53
 * If this flag is set G1 will never unload classes on System.gc() and
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    54
 * As far as the "jfr" key guarantees no VM flags are set from the outside
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    55
 * it should be enough with System.gc().
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    56
 */
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    57
public final class TestClearStaleConstants {
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    58
    static class MyClass {
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    59
    }
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    60
    private final static String TEST_CLASS_NAME = "jdk.jfr.jvm.TestClearStaleConstants$MyClass";
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    61
    private final static String EVENT_NAME = EventNames.ClassDefine;
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    62
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    63
    // to prevent the compiler to optimize away all unread writes
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    64
    public static TestClassLoader firstClassLoader;
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    65
    public static TestClassLoader secondClassLoader;
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    66
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    67
    public static void main(String... args) throws Exception {
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    68
        firstClassLoader = new TestClassLoader();
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    69
        // define a  class using a class loader under a recording
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    70
        Class<?> clz = recordClassDefinition(firstClassLoader);
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    71
        JVM jvm = JVM.getJVM();
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    72
        // we will now tag the defined and loaded clz as being in use (no recordings are running here)
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    73
        jvm.getClassIdNonIntrinsic(clz);
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    74
        // null out for unload to occur
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    75
        firstClassLoader = null;
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    76
        clz = null;
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    77
        // provoke unload
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    78
        System.gc();
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    79
        // try to define another class _with the same name_ using a different class loader
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    80
        secondClassLoader = new TestClassLoader();
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    81
        // this will throw a NPE for 8231081 because it will reuse the same class name
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    82
        // that symbol was  marked as already serialized by the unload, but since no recordings were running
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    83
        // it was not written to any chunk. This creates a reference to a non-existing symbol, leading to an NPE (no symbol at the expected location).
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    84
        recordClassDefinition(secondClassLoader);
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    85
    }
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    86
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    87
    private static Class<?> recordClassDefinition(TestClassLoader classLoader) throws Exception  {
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    88
        try (Recording recording = new Recording())  {
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    89
            recording.enable(EVENT_NAME);
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    90
            recording.start();
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    91
            Class<?> clz = classLoader.loadClass(TEST_CLASS_NAME);
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    92
            recording.stop();
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    93
            assertClassDefineEvent(recording);
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    94
            return clz;
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    95
        }
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    96
    }
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    97
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    98
    private static void assertClassDefineEvent(Recording recording) throws Exception {
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
    99
        boolean isAnyFound = false;
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
   100
        for (RecordedEvent event : Events.fromRecording(recording)) {
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
   101
            System.out.println(event);
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
   102
            RecordedClass definedClass = event.getValue("definedClass");
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
   103
            if (TEST_CLASS_NAME.equals(definedClass.getName())) {
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
   104
                RecordedClassLoader definingClassLoader = definedClass.getClassLoader();
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
   105
                String definingName = definingClassLoader.getType().getName();
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
   106
                String testName = TestClassLoader.class.getName();
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
   107
                String errorMsg = "Expected " + testName + ", got " + definingName;
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
   108
                Asserts.assertEquals(testName, definingName, errorMsg);
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
   109
                Asserts.assertFalse(isAnyFound, "Found more than 1 event");
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
   110
                isAnyFound = true;
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
   111
            }
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
   112
        }
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
   113
        Asserts.assertTrue(isAnyFound, "No events found");
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
   114
    }
7909763ad193 8231081: TestMetadataRetention fails due to missing symbol id
mgronlun
parents:
diff changeset
   115
}