| 1 |
/* |
| 2 |
* $Id$ |
| 3 |
* |
| 4 |
* Copyright (C) 2003-2009 JNode.org |
| 5 |
* |
| 6 |
* This library is free software; you can redistribute it and/or modify it |
| 7 |
* under the terms of the GNU Lesser General Public License as published |
| 8 |
* by the Free Software Foundation; either version 2.1 of the License, or |
| 9 |
* (at your option) any later version. |
| 10 |
* |
| 11 |
* This library is distributed in the hope that it will be useful, but |
| 12 |
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 13 |
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public |
| 14 |
* License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU Lesser General Public License |
| 17 |
* along with this library; If not, write to the Free Software Foundation, Inc., |
| 18 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 |
*/ |
| 20 |
|
| 21 |
package org.jnode.vm.memmgr.generational; |
| 22 |
|
| 23 |
import java.io.IOException; |
| 24 |
import java.util.TreeMap; |
| 25 |
|
| 26 |
import org.jnode.util.NumberUtils; |
| 27 |
import org.jnode.vm.memmgr.HeapStatistics; |
| 28 |
|
| 29 |
/** |
| 30 |
* @author Martin Husted Hartvig (hagar@jnode.org) |
| 31 |
*/ |
| 32 |
|
| 33 |
public final class GenHeapStatistics extends HeapStatistics { |
| 34 |
|
| 35 |
private int minInstanceCount = 0; |
| 36 |
private long minTotalSize = 0; |
| 37 |
private final TreeMap<String, HeapCounter> countData = new TreeMap<String, HeapCounter>(); |
| 38 |
|
| 39 |
private static final char newline = '\n'; |
| 40 |
|
| 41 |
public boolean contains(String classname) { |
| 42 |
return countData.containsKey(classname); |
| 43 |
} |
| 44 |
|
| 45 |
public void add(String className, int size) { |
| 46 |
HeapCounter count = (HeapCounter) countData.get(className); |
| 47 |
|
| 48 |
if (count == null) { |
| 49 |
count = new HeapCounter(className, size); |
| 50 |
countData.put(className, count); |
| 51 |
} |
| 52 |
|
| 53 |
count.inc(); |
| 54 |
|
| 55 |
count = null; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Sets the minimum number of instances a class must have before it is |
| 60 |
* listed in toString. |
| 61 |
* |
| 62 |
* @param count |
| 63 |
*/ |
| 64 |
public void setMinimumInstanceCount(int count) { |
| 65 |
this.minInstanceCount = count; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Sets the minimum bytes of occupied memory by all instances of a class |
| 70 |
* before it is listed in toString. |
| 71 |
* |
| 72 |
* @param bytes |
| 73 |
*/ |
| 74 |
public void setMinimumTotalSize(long bytes) { |
| 75 |
this.minTotalSize = bytes; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* {@inheritDoc} |
| 80 |
* |
| 81 |
* @throws IOException |
| 82 |
*/ |
| 83 |
public void writeTo(Appendable a) throws IOException { |
| 84 |
boolean first = true; |
| 85 |
|
| 86 |
for (HeapCounter c : countData.values()) { |
| 87 |
if ((c.getInstanceCount() >= minInstanceCount) && (c.getTotalSize() >= minTotalSize)) { |
| 88 |
if (first) { |
| 89 |
first = false; |
| 90 |
} else { |
| 91 |
a.append(newline); |
| 92 |
} |
| 93 |
c.append(a); |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* {@inheritDoc} |
| 100 |
*/ |
| 101 |
public String toString() { |
| 102 |
final StringBuilder sb = new StringBuilder(); |
| 103 |
try { |
| 104 |
writeTo(sb); |
| 105 |
} catch (IOException e) { |
| 106 |
// normally, it will never happen |
| 107 |
throw new RuntimeException(e); |
| 108 |
} |
| 109 |
return sb.toString(); |
| 110 |
} |
| 111 |
|
| 112 |
static final class HeapCounter { |
| 113 |
|
| 114 |
private final String name; |
| 115 |
private int instanceCount; |
| 116 |
private int objectSize = 0; |
| 117 |
|
| 118 |
private static final String usage = " memory usage="; |
| 119 |
|
| 120 |
public HeapCounter(String objectName, int objectSize) { |
| 121 |
this.name = objectName; |
| 122 |
this.objectSize = objectSize; |
| 123 |
this.instanceCount = 0; |
| 124 |
} |
| 125 |
|
| 126 |
public void inc() { |
| 127 |
instanceCount++; |
| 128 |
} |
| 129 |
|
| 130 |
public int getInstanceCount() { |
| 131 |
return instanceCount; |
| 132 |
} |
| 133 |
|
| 134 |
public int getObjectSize() { |
| 135 |
return objectSize; |
| 136 |
} |
| 137 |
|
| 138 |
public long getTotalSize() { |
| 139 |
return objectSize * (long) instanceCount; |
| 140 |
} |
| 141 |
|
| 142 |
public void append(Appendable a) throws IOException { |
| 143 |
a.append(name); |
| 144 |
a.append(" #"); |
| 145 |
a.append(Integer.toString(instanceCount)); |
| 146 |
|
| 147 |
if (objectSize != 0) { |
| 148 |
a.append(usage); |
| 149 |
long size = getTotalSize(); |
| 150 |
if (size >= 1024) { |
| 151 |
a.append(NumberUtils.toBinaryByte(size)).append(" ("); |
| 152 |
a.append(Long.toString(size)).append("b)"); |
| 153 |
} else { |
| 154 |
a.append(Long.toString(size)).append('b'); |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
} |