1/*
2 * Copyright (c) 2004, 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
25package sun.jvm.hotspot.runtime;
26
27import java.util.*;
28import sun.jvm.hotspot.debugger.*;
29import sun.jvm.hotspot.oops.*;
30import sun.jvm.hotspot.types.*;
31
32public class PerfMemory {
33    private static AddressField  startField;
34    private static AddressField  endField;
35    private static AddressField  topField;
36    private static CIntegerField capacityField;
37    private static AddressField  prologueField;
38    private static JIntField     initializedField;
39
40    static {
41        VM.registerVMInitializedObserver(new Observer() {
42                public void update(Observable o, Object data) {
43                    initialize(VM.getVM().getTypeDataBase());
44                }
45            });
46    }
47
48    private static synchronized void initialize(TypeDataBase db) {
49        Type type = db.lookupType("PerfMemory");
50        startField = type.getAddressField("_start");
51        endField = type.getAddressField("_end");
52        topField = type.getAddressField("_top");
53        capacityField = type.getCIntegerField("_capacity");
54        prologueField = type.getAddressField("_prologue");
55        initializedField = type.getJIntField("_initialized");
56    }
57
58    // Accessors
59    public static Address start() {
60        return startField.getValue();
61    }
62
63    public static Address end() {
64        return endField.getValue();
65    }
66
67    public static Address top() {
68        return topField.getValue();
69    }
70
71    public static long capacity() {
72        return capacityField.getValue();
73    }
74
75    public static boolean initialized() {
76        return ((int) initializedField.getValue()) != 0;
77    }
78
79    public static PerfDataPrologue prologue() {
80        return (PerfDataPrologue) VMObjectFactory.newObject(
81                   PerfDataPrologue.class, prologueField.getValue());
82    }
83
84    public static boolean contains(Address addr) {
85        return start() != null &&
86            addr.minus(start()) >= 0 &&
87            end().minus(addr) > 0;
88    }
89
90    // an interface supplied to iterate PerfDataEntries
91    public static interface PerfDataEntryVisitor {
92        // returns false to stop the iteration
93        public boolean visit(PerfDataEntry pde);
94    }
95
96    public static void iterate(PerfDataEntryVisitor visitor) {
97        PerfDataPrologue header = prologue();
98        int off = header.entryOffset();
99        int num = header.numEntries();
100        Address addr = header.getAddress();
101
102        for (int i = 0; i < num; i++) {
103            PerfDataEntry pde = (PerfDataEntry) VMObjectFactory.newObject(
104                               PerfDataEntry.class, addr.addOffsetTo(off));
105            off += pde.entryLength();
106            if (visitor.visit(pde) == false) return;
107        }
108    }
109}
110