1 /*
   2  * Copyright (c) 2002, 2016, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_MEMORY_HEAPINSPECTION_HPP
  26 #define SHARE_VM_MEMORY_HEAPINSPECTION_HPP
  27 
  28 #include "memory/allocation.hpp"
  29 #include "oops/objArrayOop.hpp"
  30 #include "oops/oop.hpp"
  31 #include "oops/annotations.hpp"
  32 #include "utilities/macros.hpp"
  33 
  34 #if INCLUDE_SERVICES
  35 
  36 
  37 // HeapInspection
  38 
  39 // KlassInfoTable is a bucket hash table that
  40 // maps Klass*s to extra information:
  41 //    instance count and instance word size.
  42 //
  43 // A KlassInfoBucket is the head of a link list
  44 // of KlassInfoEntry's
  45 //
  46 // KlassInfoHisto is a growable array of pointers
  47 // to KlassInfoEntry's and is used to sort
  48 // the entries.
  49 
  50 #define HEAP_INSPECTION_COLUMNS_DO(f) \
  51     f(inst_size, InstSize, \
  52         "Size of each object instance of the Java class") \
  53     f(inst_count, InstCount, \
  54         "Number of object instances of the Java class")  \
  55     f(inst_bytes, InstBytes, \
  56         "This is usually (InstSize * InstNum). The only exception is " \
  57         "java.lang.Class, whose InstBytes also includes the slots " \
  58         "used to store static fields. InstBytes is not counted in " \
  59         "ROAll, RWAll or Total") \
  60     f(mirror_bytes, Mirror, \
  61         "Size of the Klass::java_mirror() object") \
  62     f(klass_bytes, KlassBytes, \
  63         "Size of the InstanceKlass or ArrayKlass for this class. " \
  64         "Note that this includes VTab, ITab, OopMap") \
  65     f(secondary_supers_bytes, K_secondary_supers, \
  66         "Number of bytes used by the Klass::secondary_supers() array") \
  67     f(vtab_bytes, VTab, \
  68         "Size of the embedded vtable in InstanceKlass") \
  69     f(itab_bytes, ITab, \
  70         "Size of the embedded itable in InstanceKlass") \
  71     f(nonstatic_oopmap_bytes, OopMap, \
  72         "Size of the embedded nonstatic_oop_map in InstanceKlass") \
  73     f(methods_array_bytes, IK_methods, \
  74         "Number of bytes used by the InstanceKlass::methods() array") \
  75     f(method_ordering_bytes, IK_method_ordering, \
  76         "Number of bytes used by the InstanceKlass::method_ordering() array") \
  77     f(default_methods_array_bytes, IK_default_methods, \
  78         "Number of bytes used by the InstanceKlass::default_methods() array") \
  79     f(default_vtable_indices_bytes, IK_default_vtable_indices, \
  80         "Number of bytes used by the InstanceKlass::default_vtable_indices() array") \
  81     f(local_interfaces_bytes, IK_local_interfaces, \
  82         "Number of bytes used by the InstanceKlass::local_interfaces() array") \
  83     f(transitive_interfaces_bytes, IK_transitive_interfaces, \
  84         "Number of bytes used by the InstanceKlass::transitive_interfaces() array") \
  85     f(fields_bytes, IK_fields, \
  86         "Number of bytes used by the InstanceKlass::fields() array") \
  87     f(inner_classes_bytes, IK_inner_classes, \
  88         "Number of bytes used by the InstanceKlass::inner_classes() array") \
  89     f(signers_bytes, IK_signers, \
  90         "Number of bytes used by the InstanceKlass::singers() array") \
  91     f(class_annotations_bytes, class_annotations, \
  92         "Size of class annotations") \
  93     f(class_type_annotations_bytes, class_type_annotations, \
  94         "Size of class type annotations") \
  95     f(fields_annotations_bytes, fields_annotations, \
  96         "Size of field annotations") \
  97     f(fields_type_annotations_bytes, fields_type_annotations, \
  98         "Size of field type annotations") \
  99     f(methods_annotations_bytes, methods_annotations, \
 100         "Size of method annotations") \
 101     f(methods_parameter_annotations_bytes, methods_parameter_annotations, \
 102         "Size of method parameter annotations") \
 103     f(methods_type_annotations_bytes, methods_type_annotations, \
 104         "Size of methods type annotations") \
 105     f(methods_default_annotations_bytes, methods_default_annotations, \
 106         "Size of methods default annotations") \
 107     f(annotations_bytes, annotations, \
 108         "Size of all annotations") \
 109     f(cp_bytes, Cp, \
 110         "Size of InstanceKlass::constants()") \
 111     f(cp_tags_bytes, CpTags, \
 112         "Size of InstanceKlass::constants()->tags()") \
 113     f(cp_cache_bytes, CpCache, \
 114         "Size of InstanceKlass::constants()->cache()") \
 115     f(cp_operands_bytes, CpOperands, \
 116         "Size of InstanceKlass::constants()->operands()") \
 117     f(cp_refmap_bytes, CpRefMap, \
 118         "Size of InstanceKlass::constants()->reference_map()") \
 119     f(cp_all_bytes, CpAll, \
 120         "Sum of Cp + CpTags + CpCache + CpOperands + CpRefMap") \
 121     f(method_count, MethodCount, \
 122         "Number of methods in this class") \
 123     f(method_bytes, MethodBytes, \
 124         "Size of the Method object") \
 125     f(const_method_bytes, ConstMethod, \
 126         "Size of the ConstMethod object") \
 127     f(method_data_bytes, MethodData, \
 128         "Size of the MethodData object") \
 129     f(stackmap_bytes, StackMap, \
 130         "Size of the stackmap_data") \
 131     f(bytecode_bytes, Bytecodes, \
 132         "Of the MethodBytes column, how much are the space taken up by bytecodes") \
 133     f(method_all_bytes, MethodAll, \
 134         "Sum of MethodBytes + Constmethod + Stackmap + Methoddata") \
 135     f(ro_bytes, ROAll, \
 136         "Size of all class meta data that could (potentially) be placed " \
 137         "in read-only memory. (This could change with CDS design)") \
 138     f(rw_bytes, RWAll, \
 139         "Size of all class meta data that must be placed in read/write " \
 140         "memory. (This could change with CDS design) ") \
 141     f(total_bytes, Total, \
 142         "ROAll + RWAll. Note that this does NOT include InstBytes.")
 143 
 144 // Size statistics for a Klass - filled in by Klass::collect_statistics()
 145 class KlassSizeStats {
 146 public:
 147 #define COUNT_KLASS_SIZE_STATS_FIELD(field, name, help)   _index_ ## field,
 148 #define DECLARE_KLASS_SIZE_STATS_FIELD(field, name, help) julong _ ## field;
 149 
 150   enum {
 151     HEAP_INSPECTION_COLUMNS_DO(COUNT_KLASS_SIZE_STATS_FIELD)
 152     _num_columns
 153   };
 154 
 155   HEAP_INSPECTION_COLUMNS_DO(DECLARE_KLASS_SIZE_STATS_FIELD)
 156 
 157   static int count(oop x);
 158 
 159   static int count_array(objArrayOop x);
 160 
 161   template <class T> static int count(T* x) {
 162     return (HeapWordSize * ((x) ? (x)->size() : 0));
 163   }
 164 
 165   template <class T> static int count_array(T* x) {
 166     if (x == NULL) {
 167       return 0;
 168     }
 169     if (x->length() == 0) {
 170       // This is a shared array, e.g., Universe::the_empty_int_array(). Don't
 171       // count it to avoid double-counting.
 172       return 0;
 173     }
 174     return HeapWordSize * x->size();
 175   }
 176 };
 177 
 178 
 179 
 180 
 181 class KlassInfoEntry: public CHeapObj<mtInternal> {
 182  private:
 183   KlassInfoEntry* _next;
 184   Klass*          _klass;
 185   long            _instance_count;
 186   size_t          _instance_words;
 187   long            _index;
 188   bool            _do_print; // True if we should print this class when printing the class hierarchy.
 189   GrowableArray<KlassInfoEntry*>* _subclasses;
 190 
 191  public:
 192   KlassInfoEntry(Klass* k, KlassInfoEntry* next) :
 193     _klass(k), _instance_count(0), _instance_words(0), _next(next), _index(-1),
 194     _do_print(false), _subclasses(NULL)
 195   {}
 196   ~KlassInfoEntry();
 197   KlassInfoEntry* next() const   { return _next; }
 198   bool is_equal(const Klass* k)  { return k == _klass; }
 199   Klass* klass()  const      { return _klass; }
 200   long count()    const      { return _instance_count; }
 201   void set_count(long ct)    { _instance_count = ct; }
 202   size_t words()  const      { return _instance_words; }
 203   void set_words(size_t wds) { _instance_words = wds; }
 204   void set_index(long index) { _index = index; }
 205   long index()    const      { return _index; }
 206   GrowableArray<KlassInfoEntry*>* subclasses() const { return _subclasses; }
 207   void add_subclass(KlassInfoEntry* cie);
 208   void set_do_print(bool do_print) { _do_print = do_print; }
 209   bool do_print() const      { return _do_print; }
 210   int compare(KlassInfoEntry* e1, KlassInfoEntry* e2);
 211   void print_on(outputStream* st) const;
 212   const char* name() const;
 213 };
 214 
 215 class KlassInfoClosure : public StackObj {
 216  public:
 217   // Called for each KlassInfoEntry.
 218   virtual void do_cinfo(KlassInfoEntry* cie) = 0;
 219 };
 220 
 221 class KlassInfoBucket: public CHeapObj<mtInternal> {
 222  private:
 223   KlassInfoEntry* _list;
 224   KlassInfoEntry* list()           { return _list; }
 225   void set_list(KlassInfoEntry* l) { _list = l; }
 226  public:
 227   KlassInfoEntry* lookup(Klass* k);
 228   void initialize() { _list = NULL; }
 229   void empty();
 230   void iterate(KlassInfoClosure* cic);
 231 };
 232 
 233 class KlassInfoTable: public StackObj {
 234  private:
 235   int _size;
 236   static const int _num_buckets = 20011;
 237   size_t _size_of_instances_in_words;
 238 
 239   // An aligned reference address (typically the least
 240   // address in the perm gen) used for hashing klass
 241   // objects.
 242   HeapWord* _ref;
 243 
 244   KlassInfoBucket* _buckets;
 245   uint hash(const Klass* p);
 246   KlassInfoEntry* lookup(Klass* k); // allocates if not found!
 247 
 248   class AllClassesFinder : public KlassClosure {
 249     KlassInfoTable *_table;
 250    public:
 251     AllClassesFinder(KlassInfoTable* table) : _table(table) {}
 252     virtual void do_klass(Klass* k);
 253   };
 254 
 255  public:
 256   KlassInfoTable(bool add_all_classes);
 257   ~KlassInfoTable();
 258   bool record_instance(const oop obj);
 259   void iterate(KlassInfoClosure* cic);
 260   bool allocation_failed() { return _buckets == NULL; }
 261   size_t size_of_instances_in_words() const;
 262 
 263   friend class KlassInfoHisto;
 264   friend class KlassHierarchy;
 265 };
 266 
 267 class KlassHierarchy : AllStatic {
 268  public:
 269   static void print_class_hierarchy(outputStream* st, bool print_interfaces,  bool print_subclasses,
 270                                     char* classname);
 271 
 272  private:
 273   static void set_do_print_for_class_hierarchy(KlassInfoEntry* cie, KlassInfoTable* cit,
 274                                                bool print_subclasse);
 275   static void print_class(outputStream* st, KlassInfoEntry* cie, bool print_subclasses);
 276 };
 277 
 278 class KlassInfoHisto : public StackObj {
 279  private:
 280   static const int _histo_initial_size = 1000;
 281   KlassInfoTable *_cit;
 282   GrowableArray<KlassInfoEntry*>* _elements;
 283   GrowableArray<KlassInfoEntry*>* elements() const { return _elements; }
 284   static int sort_helper(KlassInfoEntry** e1, KlassInfoEntry** e2);
 285   void print_elements(outputStream* st) const;
 286   void print_class_stats(outputStream* st, bool csv_format, const char *columns);
 287   julong annotations_bytes(Array<AnnotationArray*>* p) const;
 288   const char *_selected_columns;
 289   bool is_selected(const char *col_name);
 290   void print_title(outputStream* st, bool csv_format,
 291                    bool selected_columns_table[], int width_table[],
 292                    const char *name_table[]);
 293 
 294   template <class T> static int count_bytes(T* x) {
 295     return (HeapWordSize * ((x) ? (x)->size() : 0));
 296   }
 297 
 298   template <class T> static int count_bytes_array(T* x) {
 299     if (x == NULL) {
 300       return 0;
 301     }
 302     if (x->length() == 0) {
 303       // This is a shared array, e.g., Universe::the_empty_int_array(). Don't
 304       // count it to avoid double-counting.
 305       return 0;
 306     }
 307     return HeapWordSize * x->size();
 308   }
 309 
 310   static void print_julong(outputStream* st, int width, julong n) {
 311     int num_spaces = width - julong_width(n);
 312     if (num_spaces > 0) {
 313       st->print("%*s", num_spaces, "");
 314     }
 315     st->print(JULONG_FORMAT, n);
 316   }
 317 
 318   static int julong_width(julong n) {
 319     if (n == 0) {
 320       return 1;
 321     }
 322     int w = 0;
 323     while (n > 0) {
 324       n /= 10;
 325       w += 1;
 326     }
 327     return w;
 328   }
 329 
 330   static int col_width(julong n, const char *name) {
 331     int w = julong_width(n);
 332     int min = (int)(strlen(name));
 333     if (w < min) {
 334         w = min;
 335     }
 336     // add a leading space for separation.
 337     return w + 1;
 338   }
 339 
 340  public:
 341   KlassInfoHisto(KlassInfoTable* cit);
 342   ~KlassInfoHisto();
 343   void add(KlassInfoEntry* cie);
 344   void print_histo_on(outputStream* st, bool print_class_stats, bool csv_format, const char *columns);
 345   void sort();
 346 };
 347 
 348 #endif // INCLUDE_SERVICES
 349 
 350 // These declarations are needed since the declaration of KlassInfoTable and
 351 // KlassInfoClosure are guarded by #if INLCUDE_SERVICES
 352 class KlassInfoTable;
 353 class KlassInfoClosure;
 354 
 355 class HeapInspection : public StackObj {
 356   bool _csv_format; // "comma separated values" format for spreadsheet.
 357   bool _print_help;
 358   bool _print_class_stats;
 359   const char* _columns;
 360  public:
 361   HeapInspection(bool csv_format, bool print_help,
 362                  bool print_class_stats, const char *columns) :
 363       _csv_format(csv_format), _print_help(print_help),
 364       _print_class_stats(print_class_stats), _columns(columns) {}
 365   void heap_inspection(outputStream* st) NOT_SERVICES_RETURN;
 366   size_t populate_table(KlassInfoTable* cit, BoolObjectClosure* filter = NULL) NOT_SERVICES_RETURN_(0);
 367   static void find_instances_at_safepoint(Klass* k, GrowableArray<oop>* result) NOT_SERVICES_RETURN;
 368  private:
 369   void iterate_over_heap(KlassInfoTable* cit, BoolObjectClosure* filter = NULL);
 370 };
 371 
 372 #endif // SHARE_VM_MEMORY_HEAPINSPECTION_HPP