001: /*
002: * @(#)CVMMethodStats.java 1.5 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027: package vm;
028:
029: import consts.Const;
030: import consts.CVMConst;
031: import components.*;
032: import vm.*;
033: import jcc.Util;
034: import util.*;
035: import java.util.Arrays;
036: import java.util.Comparator;
037: import java.util.Enumeration;
038: import java.util.Hashtable;
039: import java.io.OutputStream;
040:
041: public class CVMMethodStats implements Comparator {
042:
043: int tableIdx;
044: Entry[] table;
045:
046: public class Entry {
047: // Remember these three
048: int argsSize;
049: String invoker;
050: int access;
051:
052: // Index in global table
053: int idx;
054:
055: //
056: // And count references. The top referenced guys are going to
057: // be at the top of the table
058: //
059: int refcount;
060:
061: Entry(int argsSize, String invoker, int access) {
062: this .argsSize = argsSize;
063: this .invoker = invoker;
064: this .access = access;
065: this .refcount = 1;
066: }
067:
068: public String toString() {
069: return "[" + "argsSize = " + argsSize + ", access = "
070: + access + ", invoker = " + invoker + "]";
071: }
072:
073: public int getIdx() {
074: return idx;
075: }
076:
077: public String getInvoker() {
078: return invoker;
079: }
080:
081: }
082:
083: public CVMMethodStats() {
084: super ();
085: // This should be more than enough
086: table = new Entry[1024];
087: tableIdx = 0;
088: }
089:
090: public void record(int argsSize, String invoker, int access) {
091: Entry entry = lookup(argsSize, invoker, access);
092: if (entry == null) {
093: entry = new Entry(argsSize, invoker, access);
094: // Created a new one. Add to table
095: entry.idx = tableIdx;
096: table[tableIdx++] = entry;
097: } else {
098: // Just increment refcount
099: entry.refcount++;
100: }
101: }
102:
103: public Entry lookup(int argsSize, String invoker, int access) {
104: for (int i = 0; i < tableIdx; i++) {
105: Entry entry = table[i];
106: if ((entry.argsSize == argsSize)
107: && (entry.access == access)
108: && invoker.equals(entry.invoker)) {
109: return entry;
110: }
111: }
112: return null;
113: }
114:
115: public void sort() {
116: Entry newTable[] = new Entry[tableIdx];
117: System.arraycopy(table, 0, newTable, 0, tableIdx);
118: Arrays.sort(newTable, this );
119: for (int i = 0; i < tableIdx; i++) {
120: newTable[i].idx = i;
121: }
122: table = newTable;
123: }
124:
125: public int compare(java.lang.Object o1, java.lang.Object o2) {
126: Entry obj1 = (Entry) o1;
127: Entry obj2 = (Entry) o2;
128:
129: if (obj1.refcount < obj2.refcount) {
130: return 1;
131: } else if (obj1.refcount == obj2.refcount)
132: return 0;
133: return -1;
134: }
135:
136: //
137: // Dump the first 256 elements at most
138: //
139: public void dump(runtime.CCodeWriter out) {
140: int numElements = Math.min(256, tableIdx);
141: out.println();
142: out.println("const CVMUint16 argsInvokerAccess[] = {");
143: for (int i = 0; i < numElements; i++) {
144: out.print(" /* idx=" + i + ", refcount="
145: + table[i].refcount + " */ ");
146: out.println(table[i].argsSize + ", " + table[i].invoker
147: + ", " + table[i].access + ",");
148: }
149: out.println("};");
150: out.println();
151: }
152: }
|