[not part of build] interior pointer detection, cycle detection.

This commit is contained in:
beard%netscape.com 2000-09-22 07:32:52 +00:00
Родитель 83bad5d9c0
Коммит b1758848ed
4 изменённых файлов: 235 добавлений и 203 удалений

Просмотреть файл

@ -1,86 +0,0 @@
public class QuickSort {
public interface Comparator {
int compare(Object obj1, Object obj2);
}
Comparator itsComparator;
public QuickSort(Comparator comparator) {
itsComparator = comparator;
}
/** This is a generic version of C.A.R Hoare's Quick Sort
* algorithm. This will handle arrays that are already
* sorted, and arrays with duplicate keys.<BR>
*
* If you think of a one dimensional array as going from
* the lowest index on the left to the highest index on the right
* then the parameters to this function are lowest index or
* left and highest index or right. The first time you call
* this function it will be with the parameters 0, a.length - 1.
*
* @param a an Object array
* @param lo0 left boundary of array partition
* @param hi0 right boundary of array partition
*/
private void qsort(Object[] a, int lo0, int hi0) {
int lo = lo0;
int hi = hi0;
if ( hi0 > lo0) {
/* Arbitrarily establishing partition element as the midpoint of
* the array.
*/
Object mid = a[ ( lo0 + hi0 ) / 2 ];
// loop through the array until indices cross
while ( lo <= hi ) {
/* find the first element that is greater than or equal to
* the partition element starting from the left Index.
*/
while (( lo < hi0 ) && ( itsComparator.compare(a[lo], mid) < 0 ))
++lo;
/* find an element that is smaller than or equal to
* the partition element starting from the right Index.
*/
while (( hi > lo0 ) && ( itsComparator.compare(a[hi], mid) > 0 ))
--hi;
// if the indexes have not crossed, swap
if ( lo <= hi ) {
swap(a, lo, hi);
++lo;
--hi;
}
}
/* If the right index has not reached the left side of array
* must now sort the left partition.
*/
if ( lo0 < hi )
qsort( a, lo0, hi );
/* If the left index has not reached the right side of array
* must now sort the right partition.
*/
if ( lo < hi0 )
qsort( a, lo, hi0 );
}
}
private static void swap(Object[] a, int i, int j) {
Object temp = a[i];
a[i] = a[j];
a[j] = temp;
}
public void sort(Object[] a) {
qsort(a, 0, a.length - 1);
}
public void sort(Object[] a, int length) {
qsort(a, 0, length - 1);
}
}

Просмотреть файл

@ -62,7 +62,7 @@ class Type {
return "<A HREF=\"#" + mName + "_" + mSize + "\">&LT;" + mName + "&GT;</A> (" + mSize + ")"; return "<A HREF=\"#" + mName + "_" + mSize + "\">&LT;" + mName + "&GT;</A> (" + mSize + ")";
} }
static class Comparator implements QuickSort.Comparator { static class Comparator extends QuickSort.Comparator {
public int compare(Object obj1, Object obj2) { public int compare(Object obj1, Object obj2) {
Type t1 = (Type) obj1, t2 = (Type) obj2; Type t1 = (Type) obj1, t2 = (Type) obj2;
return (t1.mSize - t2.mSize); return (t1.mSize - t2.mSize);

Просмотреть файл

@ -90,7 +90,7 @@ public class bloatsoup {
} }
} }
static class ByTypeBloat implements QuickSort.Comparator { static class ByTypeBloat extends QuickSort.Comparator {
Histogram hist; Histogram hist;
ByTypeBloat(Histogram hist) { ByTypeBloat(Histogram hist) {
@ -108,7 +108,7 @@ public class bloatsoup {
* Sorts the bins of a histogram by (count * typeSize) to show the * Sorts the bins of a histogram by (count * typeSize) to show the
* most pressing leaks. * most pressing leaks.
*/ */
static class HistComparator implements QuickSort.Comparator { static class HistComparator extends QuickSort.Comparator {
Histogram hist; Histogram hist;
HistComparator(Histogram hist) { HistComparator(Histogram hist) {
@ -124,7 +124,7 @@ public class bloatsoup {
static void printHistogram(PrintWriter out, Histogram hist, String dir) throws IOException { static void printHistogram(PrintWriter out, Histogram hist, String dir) throws IOException {
// sort the types by histogram count. // sort the types by histogram count.
Object[] types = hist.objects(); Object[] types = hist.objects();
QuickSort sorter = new QuickSort(new leaksoup.HistComparator(hist)); QuickSort sorter = new QuickSort(new HistComparator(hist));
sorter.sort(types); sorter.sort(types);
out.println("<PRE>"); out.println("<PRE>");

Просмотреть файл

@ -37,24 +37,26 @@
import java.io.*; import java.io.*;
import java.util.*; import java.util.*;
class Leak { class Leak extends Reference {
String mAddress; String mName;
Type mType;
Object[] mReferences;
long mCrawlOffset; long mCrawlOffset;
int mCrawlCount; short mCrawlCount;
int mRefCount; short mRefCount;
short mChildCount;
Leak[] mParents; Leak[] mParents;
int mTotalSize; int mTotalSize;
boolean mMarked;
Leak(String addr, Type type, Object[] refs, long crawlOffset, int crawlCount) { Leak(String addr, Type type, Object[] refs, long crawlOffset, short crawlCount) {
mAddress = addr; super(addr, type, refs);
mReferences = refs; mName = addr;
mCrawlOffset = crawlOffset; mCrawlOffset = crawlOffset;
mCrawlCount = crawlCount; mCrawlCount = crawlCount;
mRefCount = 0; mRefCount = 0;
mType = type; mChildCount = 0;
mParents = null;
mTotalSize = 0; mTotalSize = 0;
mMarked = false;
} }
void setParents(Vector parents) { void setParents(Vector parents) {
@ -100,21 +102,114 @@ class Leak {
} }
} }
public String toString() { void clearMarks() {
return ("<A HREF=\"#" + mAddress + "\">" + mAddress + "</A> [" + mRefCount + "] " + mType + "{" + mTotalSize + "}"); // first, clear mark.
mMarked = false;
// then, visit all nodes that haven't been visited,
// and clear each one's mark.
int count = mReferences.length;
for (int i = 0; i < count; ++i) {
Object ref = mReferences[i];
if (ref instanceof Leak) {
Leak leak = (Leak) ref;
if (leak.mMarked)
leak.clearMarks();
}
}
} }
static class ByCount implements QuickSort.Comparator { static final char INDENT = '\t';
void printGraph(PrintWriter out) {
printGraph(out, 0);
clearMarks();
}
private void printGraph(PrintWriter out, int indent) {
// first, mark this node as having been visited.
// we only want to include nodes that haven't been
// visited in our total size.
mMarked = true;
for (int i = 0; i < indent; ++i)
out.print(INDENT);
out.println(toString());
// then, visit all nodes that haven't been visited,
// and include their total size in ours.
int count = mReferences.length;
if (count > 0) {
int subIndent = indent + 1;
for (int i = 0; i < count; ++i) {
Object ref = mReferences[i];
if (ref instanceof Leak) {
Leak leak = (Leak) ref;
if (!leak.mMarked)
leak.printGraph(out, subIndent);
}
}
}
}
void printCycle(PrintWriter out) {
printCycle(out, 0);
clearMarks();
}
private void printCycle(PrintWriter out, int indent) {
// first, mark this node as having been visited.
// we only want to include nodes that haven't been
// visited in our total size.
mMarked = true;
// then, visit all nodes that haven't been visited,
// and include their total size in ours.
if (mChildCount > 0) {
// don't print leaf nodes in a cycle. they aren't interesting.
for (int i = 0; i < indent; ++i)
out.print(INDENT);
out.println(toString());
int subIndent = indent + 1;
int count = mReferences.length;
for (int i = 0; i < count; ++i) {
Object ref = mReferences[i];
if (ref instanceof Leak) {
Leak leak = (Leak) ref;
if (!leak.mMarked)
leak.printCycle(out, subIndent);
}
}
}
}
public String toString() {
return ("<A HREF=\"#" + mName + "\">" + mName + "</A> [" + mRefCount + "] " + mType + "{" + mTotalSize + "}");
}
/**
* Sorts in order of increasing reference count.
*/
static class ByRefCount extends QuickSort.Comparator {
public int compare(Object obj1, Object obj2) { public int compare(Object obj1, Object obj2) {
Leak l1 = (Leak) obj1, l2 = (Leak) obj2; Leak l1 = (Leak) obj1, l2 = (Leak) obj2;
return (l1.mRefCount - l2.mRefCount); return (l1.mRefCount - l2.mRefCount);
} }
} }
/**
* Sorts in order of decreasing number of children.
*/
public static class ByChildCount extends QuickSort.Comparator {
public int compare(Object obj1, Object obj2) {
Leak l1 = (Leak) obj1, l2 = (Leak) obj2;
return (l2.mChildCount - l1.mChildCount);
}
}
/** /**
* Sorts in order of decreasing total size. * Sorts in order of decreasing total size.
*/ */
static class ByTotalSize implements QuickSort.Comparator { static class ByTotalSize extends QuickSort.Comparator {
public int compare(Object obj1, Object obj2) { public int compare(Object obj1, Object obj2) {
Leak l1 = (Leak) obj1, l2 = (Leak) obj2; Leak l1 = (Leak) obj1, l2 = (Leak) obj2;
return (l2.mTotalSize - l1.mTotalSize); return (l2.mTotalSize - l1.mTotalSize);
@ -218,7 +313,7 @@ public class leaksoup {
// record the offset of the stack crawl, which will be read in and formatted at the end, to save memory. // record the offset of the stack crawl, which will be read in and formatted at the end, to save memory.
long crawlOffset = reader.offset; long crawlOffset = reader.offset;
int crawlCount = 0; short crawlCount = 0;
for (line = reader.readLine(); line != null && !line.startsWith("Leaked "); line = reader.readLine()) for (line = reader.readLine(); line != null && !line.startsWith("Leaked "); line = reader.readLine())
++crawlCount; ++crawlCount;
@ -248,14 +343,14 @@ public class leaksoup {
Leak leak = (Leak) e.nextElement(); Leak leak = (Leak) e.nextElement();
Object[] refs = leak.mReferences; Object[] refs = leak.mReferences;
int count = refs.length; int count = refs.length;
for (int i = 0; i < count; i++) { for (int r = 0; r < count; ++r) {
String addr = (String) refs[i]; String addr = (String) refs[r];
Leak ref = (Leak) leakTable.get(addr); Leak ref = (Leak) leakTable.get(addr);
if (ref != null) { if (ref != null) {
// increase the ref count. // increase the ref count.
ref.mRefCount++; ref.mRefCount++;
// change string to ref itself. // change string to ref itself.
refs[i] = ref; refs[r] = ref;
// add leak to ref's parents vector. // add leak to ref's parents vector.
Vector parents = (Vector) parentTable.get(ref); Vector parents = (Vector) parentTable.get(ref);
if (parents == null) { if (parents == null) {
@ -272,6 +367,44 @@ public class leaksoup {
// be nice to the GC. // be nice to the GC.
leakTable.clear(); leakTable.clear();
leakTable = null; leakTable = null;
// sort the leaks by address, and find interior pointers.
{
QuickSort byAddress = new QuickSort(new Reference.ByAddress());
byAddress.sort(leaks);
}
for (int i = 0; i < leakCount; ++i) {
Leak leak = leaks[i];
Object[] refs = leak.mReferences;
int count = refs.length;
short childCount = 0;
for (int r = 0; r < count; ++r) {
if (refs[r] instanceof String) {
String addr = (String) refs[r];
if (addr.equals("0x00000000")) continue;
int address = (int) Long.parseLong(addr.substring(2), 16);
Leak ref = (Leak) Reference.findNearest(leaks, address);
if (ref != null) {
// increase the ref count.
ref.mRefCount++;
// change string to ref itself.
refs[r] = ref;
// add leak to ref's parents vector.
Vector parents = (Vector) parentTable.get(ref);
if (parents == null) {
parents = new Vector();
parentTable.put(ref, parents);
}
parents.addElement(leak);
++childCount;
}
} else {
++childCount;
}
}
leak.mChildCount = childCount;
}
// set the parents of each leak. // set the parents of each leak.
e = parentTable.keys(); e = parentTable.keys();
@ -292,32 +425,22 @@ public class leaksoup {
Date now = new Date(); Date now = new Date();
out.println("<TITLE>Leaks as of " + now + "</TITLE>"); out.println("<TITLE>Leaks as of " + now + "</TITLE>");
// print leak summary. // print leak summary.
out.println("<H2>Leak Summary</H2>"); out.println("<H2>Leak Summary</H2>");
out.println("total objects leaked = " + leakCount + "<BR>"); out.println("total objects leaked = " + leakCount + "<BR>");
out.println("total memory leaked = " + totalSize + " bytes.<BR>"); out.println("total memory leaked = " + totalSize + " bytes.<BR>");
// sort the leaks by reference count. then compute each root leak's total size. printLeakHistogram(out, hist);
QuickSort byCount = new QuickSort(new Leak.ByCount()); printLeakStructure(out, leaks);
byCount.sort(leaks);
for (int i = 0; i < leakCount; ++i) {
Leak leak = leaks[i];
if (leak.mTotalSize == 0)
leak.computeTotalSize();
}
// print the object histogram report.
out.println("<H2>Leak Histogram:</H2>");
printHistogram(out, hist);
// open original file again, as a RandomAccessFile, to read in stack crawl information. // open original file again, as a RandomAccessFile, to read in stack crawl information.
RandomAccessFile in = new RandomAccessFile(inputName, "r");
// print the leak report. // print the leak report.
if (ROOTS_ONLY) if (!ROOTS_ONLY) {
printRootLeaks(in, out, leaks); RandomAccessFile in = new RandomAccessFile(inputName, "r");
else
printLeaks(in, out, leaks); printLeaks(in, out, leaks);
in.close();
}
out.close(); out.close();
} catch (Exception e) { } catch (Exception e) {
@ -329,10 +452,10 @@ public class leaksoup {
* Sorts the bins of a histogram by (count * typeSize) to show the * Sorts the bins of a histogram by (count * typeSize) to show the
* most pressing leaks. * most pressing leaks.
*/ */
static class HistComparator implements QuickSort.Comparator { static class ByTypeBinSize extends QuickSort.Comparator {
Histogram hist; Histogram hist;
HistComparator(Histogram hist) { ByTypeBinSize(Histogram hist) {
this.hist = hist; this.hist = hist;
} }
@ -342,12 +465,13 @@ public class leaksoup {
} }
} }
static void printHistogram(PrintWriter out, Histogram hist) throws IOException { static void printLeakHistogram(PrintWriter out, Histogram hist) throws IOException {
// sort the types by histogram count. // sort the types by histogram count.
Object[] types = hist.objects(); Object[] types = hist.objects();
QuickSort sorter = new QuickSort(new HistComparator(hist)); QuickSort byTypeBinSize = new QuickSort(new ByTypeBinSize(hist));
sorter.sort(types); byTypeBinSize.sort(types);
out.println("<H2>Leak Histogram</H2>");
out.println("<PRE>"); out.println("<PRE>");
int index = types.length; int index = types.length;
while (index > 0) { while (index > 0) {
@ -358,6 +482,60 @@ public class leaksoup {
out.println("</PRE>"); out.println("</PRE>");
} }
static void printLeakStructure(PrintWriter out, Leak[] leaks) {
// print root leaks. consider only leaks with a reference
// count of 0, which when fixed, will hopefully reclaim
// all of the objects below them in the graph.
{
QuickSort byRefCount = new QuickSort(new Leak.ByRefCount());
byRefCount.sort(leaks);
}
int rootCount = 0;
int leakCount = leaks.length;
for (int i = 0; i < leakCount; ++i) {
Leak leak = leaks[i];
if (leak.mRefCount > 0)
break;
++rootCount;
leak.computeTotalSize();
}
{
QuickSort byTotalSize = new QuickSort(new Leak.ByTotalSize());
byTotalSize.sort(leaks, rootCount);
}
out.println("<H2>Leak Roots</H2>");
out.println("<PRE>");
for (int i = 0; i < rootCount; ++i) {
Leak leak = leaks[i];
leak.printGraph(out);
}
out.println("</PRE>");
// print leak cycles. traverse the leaks from objects with most number
// of children to least, so that leaf objects will be printed after
// their parents.
{
QuickSort byChildCount = new QuickSort(new Leak.ByChildCount());
byChildCount.sort(leaks);
}
out.println("<H2>Leak Cycles</H2>");
out.println("<PRE>");
for (int i = 0; i < leakCount; ++i) {
Leak leak = leaks[i];
// if an object's total size isn't known yet, then it must
// be a member of a cycle, since it wasn't reached when traversing roots.
if (leak.mTotalSize == 0) {
leak.computeTotalSize();
leak.printCycle(out);
}
}
out.println("</PRE>");
}
static StringBuffer appendChar(StringBuffer buffer, int ch) { static StringBuffer appendChar(StringBuffer buffer, int ch) {
if (ch > 32 && ch < 0x7F) { if (ch > 32 && ch < 0x7F) {
switch (ch) { switch (ch) {
@ -400,21 +578,11 @@ public class leaksoup {
QuickSort bySize = new QuickSort(new Leak.ByTotalSize()); QuickSort bySize = new QuickSort(new Leak.ByTotalSize());
bySize.sort(leaks); bySize.sort(leaks);
out.println("<H2>Leak Roots</H2>");
out.println("<PRE>");
int leakCount = leaks.length;
for (int i = 0; i < leakCount; i++) {
Leak leak = leaks[i];
if (leak.mRefCount == 0)
out.println(leak);
}
Type anchorType = null;
// now, print the report, sorted by type size. // now, print the report, sorted by type size.
for (int i = 0; i < leakCount; i++) { out.println("<PRE>");
Type anchorType = null;
int leakCount = leaks.length;
for (int i = 0; i < leakCount; ++i) {
Leak leak = leaks[i]; Leak leak = leaks[i];
if (anchorType != leak.mType) { if (anchorType != leak.mType) {
anchorType = leak.mType; anchorType = leak.mType;
@ -422,10 +590,10 @@ public class leaksoup {
out.println("<A NAME=\"" + anchorType.mName + "_" + anchorType.mSize + "\"></A>"); out.println("<A NAME=\"" + anchorType.mName + "_" + anchorType.mSize + "\"></A>");
out.println("<H3>" + anchorType + " Leaks</H3>"); out.println("<H3>" + anchorType + " Leaks</H3>");
} }
out.println("<A NAME=\"" + leak.mAddress + "\"></A>"); out.println("<A NAME=\"" + leak.mName + "\"></A>");
if (leak.mParents != null) { if (leak.mParents != null) {
out.print(leak); out.print(leak);
out.println(" <A HREF=\"#" + leak.mAddress + "_parents\">parents</A>"); out.println(" <A HREF=\"#" + leak.mName + "_parents\">parents</A>");
} else { } else {
out.println(leak); out.println(leak);
} }
@ -436,7 +604,7 @@ public class leaksoup {
printField(out, refs[j]); printField(out, refs[j]);
// print object's stack crawl: // print object's stack crawl:
in.seek(leak.mCrawlOffset); in.seek(leak.mCrawlOffset);
int crawlCount = leak.mCrawlCount; short crawlCount = leak.mCrawlCount;
while (crawlCount-- > 0) { while (crawlCount-- > 0) {
String line = in.readLine(); String line = in.readLine();
String location = FileLocator.getFileLocation(line); String location = FileLocator.getFileLocation(line);
@ -444,7 +612,7 @@ public class leaksoup {
} }
// print object's parents. // print object's parents.
if (leak.mParents != null) { if (leak.mParents != null) {
out.println("<A NAME=\"" + leak.mAddress + "_parents\"></A>"); out.println("<A NAME=\"" + leak.mName + "_parents\"></A>");
out.println("\nLeak Parents:"); out.println("\nLeak Parents:");
Leak[] parents = leak.mParents; Leak[] parents = leak.mParents;
count = parents.length; count = parents.length;
@ -452,56 +620,6 @@ public class leaksoup {
out.println("\t" + parents[j]); out.println("\t" + parents[j]);
} }
} }
out.println("</PRE>");
}
static void printRootLeaks(RandomAccessFile in, PrintWriter out, Leak[] leaks) throws IOException {
// sort the leaks by total size.
QuickSort bySize = new QuickSort(new Leak.ByTotalSize());
bySize.sort(leaks);
out.println("<H2>Leak Roots Only</H2>");
out.println("<PRE>");
int leakCount = leaks.length;
for (int i = 0; i < leakCount; i++) {
Leak leak = leaks[i];
if (leak.mRefCount == 0)
out.println(leak);
}
Type anchorType = null;
// now, print just the root leaks.
for (int i = 0; i < leakCount; i++) {
Leak leak = leaks[i];
if (leak.mRefCount > 0)
continue;
if (anchorType != leak.mType) {
anchorType = leak.mType;
out.println("\n<HR>");
out.println("<A NAME=\"" + anchorType.mName + "_" + anchorType.mSize + "\"></A>");
out.println("<H3>" + anchorType + " Leaks</H3>");
}
out.println("<A NAME=\"" + leak.mAddress + "\"></A>");
out.println(leak);
// print object's fields:
Object[] refs = leak.mReferences;
int count = refs.length;
for (int j = 0; j < count; j++)
printField(out, refs[j]);
// print object's stack crawl:
in.seek(leak.mCrawlOffset);
int crawlCount = leak.mCrawlCount;
while (crawlCount-- > 0) {
String line = in.readLine();
String location = FileLocator.getFileLocation(line);
out.println(location);
}
}
out.println("</PRE>"); out.println("</PRE>");
} }
} }