rev 4873 : 8015972: Refactor the sending of the object count after GC event

   1 /*
   2  * Copyright (c) 2002, 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.
   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.inline.hpp"

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