1 /*
   2  * Copyright (c) 1997, 2013, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 
  27 /*
  28  * The Original Code is HAT. The Initial Developer of the
  29  * Original Code is Bill Foote, with contributions from others
  30  * at JavaSoft/Sun.
  31  */
  32 
  33 package com.sun.tools.hat.internal.server;
  34 
  35 import com.sun.tools.hat.internal.model.*;
  36 import com.sun.tools.hat.internal.util.ArraySorter;
  37 import com.sun.tools.hat.internal.util.Comparer;
  38 import java.util.Enumeration;
  39 
  40 /**
  41  *
  42  * @author      Bill Foote
  43  */
  44 
  45 
  46 class InstancesCountQuery extends QueryHandler {
  47 
  48 
  49     private boolean excludePlatform;
  50 
  51     public InstancesCountQuery(boolean excludePlatform) {
  52         this.excludePlatform = excludePlatform;
  53     }
  54 
  55     public void run() {
  56         if (excludePlatform) {
  57             startHtml("Instance Counts for All Classes (excluding platform)");
  58         } else {
  59             startHtml("Instance Counts for All Classes (including platform)");
  60         }
  61 
  62         JavaClass[] classes = snapshot.getClassesArray();
  63         if (excludePlatform) {
  64             int num = 0;
  65             for (int i = 0; i < classes.length; i++) {
  66                 if (! PlatformClasses.isPlatformClass(classes[i])) {
  67                     classes[num++] = classes[i];
  68                 }
  69             }
  70             JavaClass[] tmp = new JavaClass[num];
  71             System.arraycopy(classes, 0, tmp, 0, tmp.length);
  72             classes = tmp;
  73         }
  74         ArraySorter.sort(classes, new Comparer() {
  75             public int compare(Object lhso, Object rhso) {
  76                 JavaClass lhs = (JavaClass) lhso;
  77                 JavaClass rhs = (JavaClass) rhso;
  78                 int diff = lhs.getInstancesCount(false)
  79                                 - rhs.getInstancesCount(false);
  80                 if (diff != 0) {
  81                     return -diff;       // Sort from biggest to smallest
  82                 }
  83                 String left = lhs.getName();
  84                 String right = rhs.getName();
  85                 if (left.startsWith("[") != right.startsWith("[")) {
  86                     // Arrays at the end
  87                     if (left.startsWith("[")) {
  88                         return 1;
  89                     } else {
  90                         return -1;
  91                     }
  92                 }
  93                 return left.compareTo(right);
  94             }
  95         });
  96 
  97         String lastPackage = null;
  98         long totalSize = 0;
  99         long instances = 0;
 100         for (int i = 0; i < classes.length; i++) {
 101             JavaClass clazz = classes[i];
 102             int count = clazz.getInstancesCount(false);
 103             print("" + count);
 104             printAnchorStart();
 105             print("instances/" + encodeForURL(classes[i]));
 106             out.print("\"> ");
 107             if (count == 1) {
 108                 print("instance");
 109             } else {
 110                 print("instances");
 111             }
 112             out.print("</a> ");
 113             if (snapshot.getHasNewSet()) {
 114                 Enumeration<JavaHeapObject> objects = clazz.getInstances(false);
 115                 int newInst = 0;
 116                 while (objects.hasMoreElements()) {
 117                     JavaHeapObject obj = objects.nextElement();
 118                     if (obj.isNew()) {
 119                         newInst++;
 120                     }
 121                 }
 122                 print("(");
 123                 printAnchorStart();
 124                 print("newInstances/" + encodeForURL(classes[i]));
 125                 out.print("\">");
 126                 print("" + newInst + " new");
 127                 out.print("</a>) ");
 128             }
 129             print("of ");
 130             printClass(classes[i]);
 131             out.println("<br>");
 132             instances += count;
 133             totalSize += classes[i].getTotalInstanceSize();
 134         }
 135         out.println("<h2>Total of " + instances + " instances occupying " + totalSize + " bytes.</h2>");
 136 
 137         out.println("<h2>Other Queries</h2>");
 138         out.println("<ul>");
 139 
 140         out.print("<li>");
 141         printAnchorStart();
 142         if (!excludePlatform) {
 143             out.print("showInstanceCounts/\">");
 144             print("Show instance counts for all classes (excluding platform)");
 145         } else {
 146             out.print("showInstanceCounts/includePlatform/\">");
 147             print("Show instance counts for all classes (including platform)");
 148         }
 149         out.println("</a>");
 150 
 151         out.print("<li>");
 152         printAnchorStart();
 153         out.print("allClassesWithPlatform/\">");
 154         print("Show All Classes (including platform)");
 155         out.println("</a>");
 156 
 157         out.print("<li>");
 158         printAnchorStart();
 159         out.print("\">");
 160         print("Show All Classes (excluding platform)");
 161         out.println("</a>");
 162 
 163         out.println("</ul>");
 164 
 165         endHtml();
 166     }
 167 
 168 
 169 }