blob: cb1b11dc048247966c0b841b975d14bd1a2bb4dd [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2005 Sun Microsystems, Inc. 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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26
27/*
28 * The contents of this file are subject to the Sun Public License
29 * Version 1.0 (the "License"); you may not use this file except in
30 * compliance with the License. A copy of the License is available at
31 * http://www.sun.com/, and in the file LICENSE.html in the
32 * doc directory.
33 *
34 * The Original Code is HAT. The Initial Developer of the
35 * Original Code is Bill Foote, with contributions from others
36 * at JavaSoft/Sun. Portions created by Bill Foote and others
37 * at Javasoft/Sun are Copyright (C) 1997-2004. All Rights Reserved.
38 *
39 * In addition to the formal license, I ask that you don't
40 * change the history or donations files without permission.
41 *
42 */
43
44package com.sun.tools.hat.internal.server;
45
46import com.sun.tools.hat.internal.model.*;
47import java.util.*;
48
49public class FinalizerSummaryQuery extends QueryHandler {
50 public void run() {
51 Enumeration objs = snapshot.getFinalizerObjects();
52 startHtml("Finalizer Summary");
53
54 out.println("<p align='center'>");
55 out.println("<b><a href='/'>All Classes (excluding platform)</a></b>");
56 out.println("</p>");
57
58 printFinalizerSummary(objs);
59 endHtml();
60 }
61
62 private static class HistogramElement {
63 public HistogramElement(JavaClass clazz) {
64 this.clazz = clazz;
65 }
66
67 public void updateCount() {
68 this.count++;
69 }
70
71 public int compare(HistogramElement other) {
72 long diff = other.count - count;
73 return (diff == 0L)? 0 : ((diff > 0L)? +1 : -1);
74 }
75
76 public JavaClass getClazz() {
77 return clazz;
78 }
79
80 public long getCount() {
81 return count;
82 }
83
84 private JavaClass clazz;
85 private long count;
86 }
87
88 private void printFinalizerSummary(Enumeration objs) {
89 int count = 0;
90 Map<JavaClass, HistogramElement> map = new HashMap<JavaClass, HistogramElement>();
91
92 while (objs.hasMoreElements()) {
93 JavaHeapObject obj = (JavaHeapObject) objs.nextElement();
94 count++;
95 JavaClass clazz = obj.getClazz();
96 if (! map.containsKey(clazz)) {
97 map.put(clazz, new HistogramElement(clazz));
98 }
99 HistogramElement element = map.get(clazz);
100 element.updateCount();
101 }
102
103 out.println("<p align='center'>");
104 out.println("<b>");
105 out.println("Total ");
106 if (count != 0) {
107 out.print("<a href='/finalizerObjects/'>instances</a>");
108 } else {
109 out.print("instances");
110 }
111 out.println(" pending finalization: ");
112 out.print(count);
113 out.println("</b></p><hr>");
114
115 if (count == 0) {
116 return;
117 }
118
119 // calculate and print histogram
120 HistogramElement[] elements = new HistogramElement[map.size()];
121 map.values().toArray(elements);
122 Arrays.sort(elements, new Comparator<HistogramElement>() {
123 public int compare(HistogramElement o1, HistogramElement o2) {
124 return o1.compare(o2);
125 }
126 });
127
128 out.println("<table border=1 align=center>");
129 out.println("<tr><th>Count</th><th>Class</th></tr>");
130 for (int j = 0; j < elements.length; j++) {
131 out.println("<tr><td>");
132 out.println(elements[j].getCount());
133 out.println("</td><td>");
134 printClass(elements[j].getClazz());
135 out.println("</td><tr>");
136 }
137 out.println("</table>");
138 }
139}