jdk

Форк
0
/
TestInstanceKlassSizeForInterface.java 
161 строка · 6.5 Кб
1
/*
2
 * Copyright (c) 2015, 2023, Oracle and/or its affiliates. All rights reserved.
3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
 *
5
 * This code is free software; you can redistribute it and/or modify it
6
 * under the terms of the GNU General Public License version 2 only, as
7
 * published by the Free Software Foundation.
8
 *
9
 * This code is distributed in the hope that it will be useful, but WITHOUT
10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12
 * version 2 for more details (a copy is included in the LICENSE file that
13
 * accompanied this code).
14
 *
15
 * You should have received a copy of the GNU General Public License version
16
 * 2 along with this work; if not, write to the Free Software Foundation,
17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
 *
19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
 * or visit www.oracle.com if you need additional information or have any
21
 * questions.
22
 */
23

24
import java.util.List;
25

26
import sun.jvm.hotspot.HotSpotAgent;
27
import sun.jvm.hotspot.utilities.SystemDictionaryHelper;
28
import sun.jvm.hotspot.oops.InstanceKlass;
29
import sun.jvm.hotspot.debugger.*;
30

31
import jdk.test.lib.apps.LingeredApp;
32
import jdk.test.lib.Asserts;
33
import jdk.test.lib.JDKToolLauncher;
34
import jdk.test.lib.JDKToolFinder;
35
import jdk.test.lib.Platform;
36
import jdk.test.lib.process.ProcessTools;
37
import jdk.test.lib.process.OutputAnalyzer;
38
import jdk.test.lib.SA.SATestUtils;
39
import jdk.test.lib.Utils;
40

41
/**
42
 * @test
43
 * @library /test/lib
44
 * @requires vm.hasSA
45
 * @modules java.base/jdk.internal.misc
46
 *          jdk.hotspot.agent/sun.jvm.hotspot
47
 *          jdk.hotspot.agent/sun.jvm.hotspot.utilities
48
 *          jdk.hotspot.agent/sun.jvm.hotspot.oops
49
 *          jdk.hotspot.agent/sun.jvm.hotspot.debugger
50
 * @build jdk.test.whitebox.WhiteBox
51
 * @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
52
 * @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. TestInstanceKlassSizeForInterface
53
 */
54

55
import jdk.test.whitebox.WhiteBox;
56

57
public class TestInstanceKlassSizeForInterface {
58

59
    public static WhiteBox wb = WhiteBox.getWhiteBox();
60

61
    private static LingeredAppWithInterface theApp = null;
62

63
    private static void SAInstanceKlassSize(int lingeredAppPid,
64
                                            String[] instanceKlassNames) {
65

66
        HotSpotAgent agent = new HotSpotAgent();
67
        try {
68
            agent.attach(lingeredAppPid);
69
        }
70
        catch (DebuggerException e) {
71
            System.out.println(e.getMessage());
72
            System.err.println("Unable to connect to process ID: " + lingeredAppPid);
73

74
            agent.detach();
75
            e.printStackTrace();
76
        }
77

78
        for (String instanceKlassName : instanceKlassNames) {
79
            InstanceKlass iKlass = SystemDictionaryHelper.findInstanceKlass(
80
                                       instanceKlassName);
81
            Asserts.assertNotNull(iKlass,
82
                String.format("Unable to find instance klass for %s", instanceKlassName));
83
            System.out.println("SA: The size of " + instanceKlassName +
84
                               " is " + iKlass.getSize());
85
        }
86
        agent.detach();
87
    }
88

89
    private static String getJcmdInstanceKlassSize(OutputAnalyzer output,
90
                                                   String instanceKlassName) {
91
        for (String s : output.asLines()) {
92
            if (s.contains(instanceKlassName)) {
93
                String tokens[];
94
                System.out.println(s);
95
                tokens = s.split("\\s+");
96
                return tokens[3];
97
            }
98
        }
99
        return null;
100
    }
101

102
    private static void createAnotherToAttach(
103
                            String[] instanceKlassNames,
104
                            int lingeredAppPid) throws Exception {
105
        // Start a new process to attach to the LingeredApp process to get SA info
106
        ProcessBuilder processBuilder = ProcessTools.createLimitedTestJavaProcessBuilder(
107
            "--add-modules=jdk.hotspot.agent",
108
            "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot=ALL-UNNAMED",
109
            "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.utilities=ALL-UNNAMED",
110
            "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.oops=ALL-UNNAMED",
111
            "--add-exports=jdk.hotspot.agent/sun.jvm.hotspot.debugger=ALL-UNNAMED",
112
            "-XX:+UnlockDiagnosticVMOptions",
113
            "-XX:+WhiteBoxAPI",
114
            "-Xbootclasspath/a:.",
115
            "TestInstanceKlassSizeForInterface",
116
            Integer.toString(lingeredAppPid));
117
        SATestUtils.addPrivilegesIfNeeded(processBuilder);
118
        OutputAnalyzer SAOutput = ProcessTools.executeProcess(processBuilder);
119
        SAOutput.shouldHaveExitValue(0);
120
        System.out.println(SAOutput.getOutput());
121

122
        // Match the sizes from both the output streams
123
        for (String instanceKlassName : instanceKlassNames) {
124
            Class<?> iklass = Class.forName(instanceKlassName);
125
            System.out.println ("Trying to match for " + instanceKlassName);
126
            String size = String.valueOf(wb.getKlassMetadataSize(iklass));
127
            boolean match = false;
128
            for (String s : SAOutput.asLines()) {
129
                if (s.contains(instanceKlassName)) {
130
                    Asserts.assertTrue(
131
                       s.contains(size), "The size computed by SA for" +
132
                       instanceKlassName + " does not match.");
133
                       match = true;
134
                    }
135
            }
136
            Asserts.assertTrue(match, "Found a match for " + instanceKlassName);
137
        }
138
    }
139

140
    public static void main (String... args) throws Exception {
141
        SATestUtils.skipIfCannotAttach(); // throws SkippedException if attach not expected to work.
142
        String[] instanceKlassNames = new String[] {
143
                                          "Language",
144
                                          "ParselTongue",
145
                                          "LingeredAppWithInterface$1"
146
                                      };
147

148
        if (args == null || args.length == 0) {
149
            try {
150
                theApp = new LingeredAppWithInterface();
151
                LingeredApp.startApp(theApp);
152
                createAnotherToAttach(instanceKlassNames,
153
                                      (int)theApp.getPid());
154
            } finally {
155
                LingeredApp.stopApp(theApp);
156
            }
157
        } else {
158
            SAInstanceKlassSize(Integer.parseInt(args[0]), instanceKlassNames);
159
        }
160
    }
161
}
162

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.