src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/utilities/soql/JSJavaVM.java
author rehn
Tue, 21 May 2019 10:34:57 +0200
changeset 54955 46409371a691
parent 47216 71c04702a3d5
permissions -rw-r--r--
8223306: Remove threads linked list (use ThreadsList's array in SA) Reviewed-by: coleenp, dholmes, dcubed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
54955
46409371a691 8223306: Remove threads linked list (use ThreadsList's array in SA)
rehn
parents: 47216
diff changeset
     2
 * Copyright (c) 2004, 2019, Oracle and/or its affiliates. All rights reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
package sun.jvm.hotspot.utilities.soql;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
import java.util.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
import sun.jvm.hotspot.debugger.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
import sun.jvm.hotspot.oops.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
import sun.jvm.hotspot.runtime.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
public class JSJavaVM extends DefaultScriptObject {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
    private static final int FIELD_ADDRESS_SIZE = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
    private static final int FIELD_BUILD_INFO   = 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
    private static final int FIELD_CPU          = 2;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
    private static final int FIELD_FLAGS        = 3;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
    private static final int FIELD_HEAP         = 4;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
    private static final int FIELD_OS           = 5;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
    private static final int FIELD_SYS_PROPS    = 6;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
    private static final int FIELD_THREADS      = 7;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
    private static final int FIELD_TYPE         = 8;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
    private static final int FIELD_VERSION      = 9;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
    private static final int FIELD_CLASS_PATH   = 10;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
    private static final int FIELD_BOOT_CLASS_PATH  = 11;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
    private static final int FIELD_USER_DIR     = 12;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
    private static final int FIELD_UNDEFINED    = -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
    public JSJavaVM(JSJavaFactory factory) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
        this.factory = factory;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
        this.vm = VM.getVM();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
    public Object get(String name) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
        int fieldID = getFieldID(name);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
        switch (fieldID) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
        case FIELD_ADDRESS_SIZE:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
            return new Long(getVMBit());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
        case FIELD_BUILD_INFO:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
            return vm.getVMInternalInfo();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
        case FIELD_CPU:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
            return vm.getCPU();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
        case FIELD_FLAGS:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
            return getFlags();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
        case FIELD_HEAP:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
            return getHeap();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
        case FIELD_OS:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
            return vm.getOS();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
        case FIELD_SYS_PROPS:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
            return getSysProps();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
        case FIELD_THREADS:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
            return getThreads();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
        case FIELD_TYPE:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
            return getType();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
        case FIELD_VERSION:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
            return vm.getVMRelease();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
        case FIELD_CLASS_PATH:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
            return getClassPath();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
        case FIELD_USER_DIR:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
            return getUserDir();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
        case FIELD_UNDEFINED:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
        default:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
            return super.get(name);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
    public Object[] getIds() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
        Object[] superIds = super.getIds();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
        Object[] tmp = fields.keySet().toArray();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
        Object[] res = new Object[superIds.length + tmp.length];
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
        System.arraycopy(tmp, 0, res, 0, tmp.length);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
        System.arraycopy(superIds, 0, res, tmp.length, superIds.length);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
        return res;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
    public boolean has(String name) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
        if (getFieldID(name) != FIELD_UNDEFINED) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
            return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
        } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
            return super.has(name);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
    public void put(String name, Object value) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
        if (getFieldID(name) == FIELD_UNDEFINED) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
            super.put(name, value);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
    public String toString() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
        StringBuffer buf = new StringBuffer();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
        buf.append("Java Hotspot ");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
        buf.append(getType());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
        buf.append(' ');
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
        buf.append(getVMBit());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
        buf.append(" bit VM (build ");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
        buf.append(vm.getVMRelease());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
        buf.append(")");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
        return buf.toString();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
    //-- Internals only below this point
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
    private static Map fields = new HashMap();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
    private static void addField(String name, int fieldId) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
        fields.put(name, new Integer(fieldId));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
    private static int getFieldID(String name) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
        Integer res = (Integer) fields.get(name);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
        return (res != null)? res.intValue() : FIELD_UNDEFINED;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
    static {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
        addField("addressSize", FIELD_ADDRESS_SIZE);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
        addField("buildInfo", FIELD_BUILD_INFO);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
        addField("cpu", FIELD_CPU);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
        addField("flags", FIELD_FLAGS);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
        addField("heap", FIELD_HEAP);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
        addField("os", FIELD_OS);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
        addField("sysProps", FIELD_SYS_PROPS);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
        addField("threads", FIELD_THREADS);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
        addField("type", FIELD_TYPE);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
        addField("version", FIELD_VERSION);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
        addField("classPath", FIELD_CLASS_PATH);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
        addField("userDir", FIELD_USER_DIR);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
    private long getVMBit() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
        // address size in bits
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
        return vm.getAddressSize() * 8;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
    private synchronized JSMap getFlags() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
        if (flagsCache == null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
            VM.Flag[] flags = vm.getCommandLineFlags();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
            Map map = new HashMap();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
            if (flags != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
                for (int f = 0; f < flags.length; f++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
                    VM.Flag flag = flags[f];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
                    map.put(flag.getName(), flag.getValue());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
            flagsCache = factory.newJSMap(map);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
        return flagsCache;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
    private synchronized JSJavaHeap getHeap() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
        if (heapCache == null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
            heapCache = factory.newJSJavaHeap();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
        return heapCache;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
    private synchronized JSMap getSysProps() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
        if (sysPropsCache == null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
            Properties props = vm.getSystemProperties();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
            Map map = new HashMap();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
            if (props != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
                Enumeration e = props.propertyNames();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
                while (e.hasMoreElements()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
                    String key = (String) e.nextElement();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
                    map.put(key, props.getProperty(key));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
            sysPropsCache = factory.newJSMap(map);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
        return sysPropsCache;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
    private synchronized JSList getThreads() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
        if (threadsCache == null) {
54955
46409371a691 8223306: Remove threads linked list (use ThreadsList's array in SA)
rehn
parents: 47216
diff changeset
   192
            List threadsList = new ArrayList(0);
46409371a691 8223306: Remove threads linked list (use ThreadsList's array in SA)
rehn
parents: 47216
diff changeset
   193
            threadsCache = factory.newJSList(threadsList);
46409371a691 8223306: Remove threads linked list (use ThreadsList's array in SA)
rehn
parents: 47216
diff changeset
   194
            Threads threads = VM.getVM().getThreads();
46409371a691 8223306: Remove threads linked list (use ThreadsList's array in SA)
rehn
parents: 47216
diff changeset
   195
            for (int i = 0; i < threads.getNumberOfThreads(); i++) {
46409371a691 8223306: Remove threads linked list (use ThreadsList's array in SA)
rehn
parents: 47216
diff changeset
   196
                JavaThread thread = threads.getJavaThreadAt(i);
46409371a691 8223306: Remove threads linked list (use ThreadsList's array in SA)
rehn
parents: 47216
diff changeset
   197
                threadsList.add(thread);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
        return threadsCache;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
    private String getType() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
        if (vm.isClientCompiler()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
            return "Client";
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
        } else if (vm.isServerCompiler()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
            return "Server";
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
        } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
            return "Core";
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
    private String getClassPath() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
        return vm.getSystemProperty("java.class.path");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
    private String getUserDir() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
        return vm.getSystemProperty("user.dir");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
    private JSMap      flagsCache;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
    private JSJavaHeap heapCache;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
    private JSMap      sysPropsCache;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
    private JSList     threadsCache;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
    private final JSJavaFactory factory;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
    private final VM vm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
}