aboutsummaryrefslogtreecommitdiff
path: root/agent/src/share/classes/sun/jvm/hotspot/utilities/soql/JSJavaVM.java
blob: 71d57c087f0fc6e079d5f18fb4184a4df1509ed4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
 * Copyright 2004-2007 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 *
 */

package sun.jvm.hotspot.utilities.soql;

import java.util.*;
import sun.jvm.hotspot.debugger.*;
import sun.jvm.hotspot.oops.*;
import sun.jvm.hotspot.runtime.*;

public class JSJavaVM extends DefaultScriptObject {
    private static final int FIELD_ADDRESS_SIZE = 0;
    private static final int FIELD_BUILD_INFO   = 1;
    private static final int FIELD_CPU          = 2;
    private static final int FIELD_FLAGS        = 3;
    private static final int FIELD_HEAP         = 4;
    private static final int FIELD_OS           = 5;
    private static final int FIELD_SYS_PROPS    = 6;
    private static final int FIELD_THREADS      = 7;
    private static final int FIELD_TYPE         = 8;
    private static final int FIELD_VERSION      = 9;
    private static final int FIELD_CLASS_PATH   = 10;
    private static final int FIELD_BOOT_CLASS_PATH  = 11;
    private static final int FIELD_USER_DIR     = 12;
    private static final int FIELD_UNDEFINED    = -1;

    public JSJavaVM(JSJavaFactory factory) {
        this.factory = factory;
        this.vm = VM.getVM();
    }

    public Object get(String name) {
        int fieldID = getFieldID(name);
        switch (fieldID) {
        case FIELD_ADDRESS_SIZE:
            return new Long(getVMBit());
        case FIELD_BUILD_INFO:
            return vm.getVMInternalInfo();
        case FIELD_CPU:
            return vm.getCPU();
        case FIELD_FLAGS:
            return getFlags();
        case FIELD_HEAP:
            return getHeap();
        case FIELD_OS:
            return vm.getOS();
        case FIELD_SYS_PROPS:
            return getSysProps();
        case FIELD_THREADS:
            return getThreads();
        case FIELD_TYPE:
            return getType();
        case FIELD_VERSION:
            return vm.getVMRelease();
        case FIELD_CLASS_PATH:
            return getClassPath();
        case FIELD_BOOT_CLASS_PATH:
            return getBootClassPath();
        case FIELD_USER_DIR:
            return getUserDir();
        case FIELD_UNDEFINED:
        default:
            return super.get(name);
        }
    }

    public Object[] getIds() {
        Object[] superIds = super.getIds();
        Object[] tmp = fields.keySet().toArray();
        Object[] res = new Object[superIds.length + tmp.length];
        System.arraycopy(tmp, 0, res, 0, tmp.length);
        System.arraycopy(superIds, 0, res, tmp.length, superIds.length);
        return res;
    }

    public boolean has(String name) {
        if (getFieldID(name) != FIELD_UNDEFINED) {
            return true;
        } else {
            return super.has(name);
        }
    }

    public void put(String name, Object value) {
        if (getFieldID(name) == FIELD_UNDEFINED) {
            super.put(name, value);
        }
    }

    public String toString() {
        StringBuffer buf = new StringBuffer();
        buf.append("Java Hotspot ");
        buf.append(getType());
        buf.append(' ');
        buf.append(getVMBit());
        buf.append(" bit VM (build ");
        buf.append(vm.getVMRelease());
        buf.append(")");
        return buf.toString();
    }

    //-- Internals only below this point
    private static Map fields = new HashMap();
    private static void addField(String name, int fieldId) {
        fields.put(name, new Integer(fieldId));
    }

    private static int getFieldID(String name) {
        Integer res = (Integer) fields.get(name);
        return (res != null)? res.intValue() : FIELD_UNDEFINED;
    }

    static {
        addField("addressSize", FIELD_ADDRESS_SIZE);
        addField("buildInfo", FIELD_BUILD_INFO);
        addField("cpu", FIELD_CPU);
        addField("flags", FIELD_FLAGS);
        addField("heap", FIELD_HEAP);
        addField("os", FIELD_OS);
        addField("sysProps", FIELD_SYS_PROPS);
        addField("threads", FIELD_THREADS);
        addField("type", FIELD_TYPE);
        addField("version", FIELD_VERSION);
        addField("classPath", FIELD_CLASS_PATH);
        addField("bootClassPath", FIELD_BOOT_CLASS_PATH);
        addField("userDir", FIELD_USER_DIR);
    }

    private long getVMBit() {
        // address size in bits
        return vm.getAddressSize() * 8;
    }

    private synchronized JSMap getFlags() {
        if (flagsCache == null) {
            VM.Flag[] flags = vm.getCommandLineFlags();
            Map map = new HashMap();
            if (flags != null) {
                for (int f = 0; f < flags.length; f++) {
                    VM.Flag flag = flags[f];
                    map.put(flag.getName(), flag.getValue());
                }
            }
            flagsCache = factory.newJSMap(map);
        }
        return flagsCache;
    }

    private synchronized JSJavaHeap getHeap() {
        if (heapCache == null) {
            heapCache = factory.newJSJavaHeap();
        }
        return heapCache;
    }

    private synchronized JSMap getSysProps() {
        if (sysPropsCache == null) {
            Properties props = vm.getSystemProperties();
            Map map = new HashMap();
            if (props != null) {
                Enumeration e = props.propertyNames();
                while (e.hasMoreElements()) {
                    String key = (String) e.nextElement();
                    map.put(key, props.getProperty(key));
                }
            }
            sysPropsCache = factory.newJSMap(map);
        }
        return sysPropsCache;
    }

    private synchronized JSList getThreads() {
        if (threadsCache == null) {
            List threads = new ArrayList(0);
            threadsCache = factory.newJSList(threads);
            JavaThread jthread = vm.getThreads().first();
            while (jthread != null) {
                threads.add(jthread);
                jthread = jthread.next();
            }
        }
        return threadsCache;
    }

    private String getType() {
        if (vm.isClientCompiler()) {
            return "Client";
        } else if (vm.isServerCompiler()) {
            return "Server";
        } else {
            return "Core";
        }
    }

    private String getClassPath() {
        return vm.getSystemProperty("java.class.path");
    }

    private String getBootClassPath() {
        return vm.getSystemProperty("sun.boot.class.path");
    }

    private String getUserDir() {
        return vm.getSystemProperty("user.dir");
    }

    private JSMap      flagsCache;
    private JSJavaHeap heapCache;
    private JSMap      sysPropsCache;
    private JSList     threadsCache;
    private final JSJavaFactory factory;
    private final VM vm;
}