src/share/vm/memory/heapInspection.hpp

Print this page
rev 4773 : 8005849: JEP 167: Event-Based JVM Tracing
Reviewed-by: acorn, coleenp, sla
Contributed-by: Karen Kinnear <karen.kinnear@oracle.com>, Bengt Rutisson <bengt.rutisson@oracle.com>, Calvin Cheung <calvin.cheung@oracle.com>, Erik Gahlin <erik.gahlin@oracle.com>, Erik Helin <erik.helin@oracle.com>, Jesper Wilhelmsson <jesper.wilhelmsson@oracle.com>, Keith McGuigan <keith.mcguigan@oracle.com>, Mattias Tobiasson <mattias.tobiasson@oracle.com>, Markus Gronlund <markus.gronlund@oracle.com>, Mikael Auno <mikael.auno@oracle.com>, Nils Eliasson <nils.eliasson@oracle.com>, Nils Loodin <nils.loodin@oracle.com>, Rickard Backman <rickard.backman@oracle.com>, Staffan Larsen <staffan.larsen@oracle.com>, Stefan Karlsson <stefan.karlsson@oracle.com>, Yekaterina Kantserova <yekaterina.kantserova@oracle.com>


   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 


 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 
 228   // An aligned reference address (typically the least
 229   // address in the perm gen) used for hashing klass
 230   // objects.
 231   HeapWord* _ref;
 232 
 233   KlassInfoBucket* _buckets;
 234   uint hash(const Klass* p);
 235   KlassInfoEntry* lookup(Klass* k); // allocates if not found!
 236 
 237   class AllClassesFinder : public KlassClosure {
 238     KlassInfoTable *_table;
 239    public:
 240     AllClassesFinder(KlassInfoTable* table) : _table(table) {}
 241     virtual void do_klass(Klass* k);
 242   };
 243 
 244  public:
 245   // Table size
 246   enum {
 247     cit_size = 20011
 248   };
 249   KlassInfoTable(int size, HeapWord* ref, bool need_class_stats);
 250   ~KlassInfoTable();
 251   bool record_instance(const oop obj);
 252   void iterate(KlassInfoClosure* cic);
 253   bool allocation_failed() { return _buckets == NULL; }

 254 
 255   friend class KlassInfoHisto;
 256 };
 257 
 258 class KlassInfoHisto : public StackObj {
 259  private:

 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) {


 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   enum {
 338     histo_initial_size = 1000
 339   };
 340   KlassInfoHisto(KlassInfoTable* cit, const char* title,
 341              int estimatedCount);
 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 class HeapInspection : public StackObj {
 351   bool _csv_format; // "comma separated values" format for spreadsheet.
 352   bool _print_help;
 353   bool _print_class_stats;
 354   const char* _columns;
 355  public:
 356   HeapInspection(bool csv_format, bool print_help,
 357                  bool print_class_stats, const char *columns) :
 358       _csv_format(csv_format), _print_help(print_help),
 359       _print_class_stats(print_class_stats), _columns(columns) {}
 360   void heap_inspection(outputStream* st, bool need_prologue) NOT_SERVICES_RETURN;

 361   static void find_instances_at_safepoint(Klass* k, GrowableArray<oop>* result) NOT_SERVICES_RETURN;


 362 };
 363 
 364 #endif // SHARE_VM_MEMORY_HEAPINSPECTION_HPP


   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 "memory/klassInfoClosure.hpp"
  30 #include "oops/oop.inline.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 


 187   long            _index;
 188 
 189  public:
 190   KlassInfoEntry(Klass* k, KlassInfoEntry* next) :
 191     _klass(k), _instance_count(0), _instance_words(0), _next(next), _index(-1)
 192   {}
 193   KlassInfoEntry* next() const   { return _next; }
 194   bool is_equal(const Klass* k)  { return k == _klass; }
 195   Klass* klass()  const      { return _klass; }
 196   long count()    const      { return _instance_count; }
 197   void set_count(long ct)    { _instance_count = ct; }
 198   size_t words()  const      { return _instance_words; }
 199   void set_words(size_t wds) { _instance_words = wds; }
 200   void set_index(long index) { _index = index; }
 201   long index()    const      { return _index; }
 202   int compare(KlassInfoEntry* e1, KlassInfoEntry* e2);
 203   void print_on(outputStream* st) const;
 204   const char* name() const;
 205 };
 206 






 207 class KlassInfoBucket: public CHeapObj<mtInternal> {
 208  private:
 209   KlassInfoEntry* _list;
 210   KlassInfoEntry* list()           { return _list; }
 211   void set_list(KlassInfoEntry* l) { _list = l; }
 212  public:
 213   KlassInfoEntry* lookup(Klass* k);
 214   void initialize() { _list = NULL; }
 215   void empty();
 216   void iterate(KlassInfoClosure* cic);
 217 };
 218 
 219 class KlassInfoTable: public StackObj {
 220  private:
 221   int _size;
 222   static const int _num_buckets = 20011;
 223   size_t _size_of_instances_in_words;
 224 
 225   // An aligned reference address (typically the least
 226   // address in the perm gen) used for hashing klass
 227   // objects.
 228   HeapWord* _ref;
 229 
 230   KlassInfoBucket* _buckets;
 231   uint hash(const Klass* p);
 232   KlassInfoEntry* lookup(Klass* k); // allocates if not found!
 233 
 234   class AllClassesFinder : public KlassClosure {
 235     KlassInfoTable *_table;
 236    public:
 237     AllClassesFinder(KlassInfoTable* table) : _table(table) {}
 238     virtual void do_klass(Klass* k);
 239   };
 240 
 241  public:
 242   KlassInfoTable(bool need_class_stats);




 243   ~KlassInfoTable();
 244   bool record_instance(const oop obj);
 245   void iterate(KlassInfoClosure* cic);
 246   bool allocation_failed() { return _buckets == NULL; }
 247   size_t size_of_instances_in_words() const;
 248 
 249   friend class KlassInfoHisto;
 250 };
 251 
 252 class KlassInfoHisto : public StackObj {
 253  private:
 254   static const int _histo_initial_size = 1000;
 255   KlassInfoTable *_cit;
 256   GrowableArray<KlassInfoEntry*>* _elements;
 257   GrowableArray<KlassInfoEntry*>* elements() const { return _elements; }
 258   const char* _title;
 259   const char* title() const { return _title; }
 260   static int sort_helper(KlassInfoEntry** e1, KlassInfoEntry** e2);
 261   void print_elements(outputStream* st) const;
 262   void print_class_stats(outputStream* st, bool csv_format, const char *columns);
 263   julong annotations_bytes(Array<AnnotationArray*>* p) const;
 264   const char *_selected_columns;
 265   bool is_selected(const char *col_name);
 266   void print_title(outputStream* st, bool csv_format,
 267                    bool selected_columns_table[], int width_table[],
 268                    const char *name_table[]);
 269 
 270   template <class T> static int count_bytes(T* x) {
 271     return (HeapWordSize * ((x) ? (x)->size() : 0));
 272   }
 273 
 274   template <class T> static int count_bytes_array(T* x) {


 312     }
 313     int w = 0;
 314     while (n > 0) {
 315       n /= 10;
 316       w += 1;
 317     }
 318     return w;
 319   }
 320 
 321   static int col_width(julong n, const char *name) {
 322     int w = julong_width(n);
 323     int min = (int)(strlen(name));
 324     if (w < min) {
 325         w = min;
 326     }
 327     // add a leading space for separation.
 328     return w + 1;
 329   }
 330 
 331  public:
 332   KlassInfoHisto(KlassInfoTable* cit, const char* title);




 333   ~KlassInfoHisto();
 334   void add(KlassInfoEntry* cie);
 335   void print_histo_on(outputStream* st, bool print_class_stats, bool csv_format, const char *columns);
 336   void sort();
 337 };
 338 
 339 #endif // INCLUDE_SERVICES
 340 
 341 // These declarations are needed since teh declaration of KlassInfoTable and
 342 // KlassInfoClosure are guarded by #if INLCUDE_SERVICES
 343 class KlassInfoTable;
 344 class KlassInfoClosure;
 345 
 346 class HeapInspection : public StackObj {
 347   bool _csv_format; // "comma separated values" format for spreadsheet.
 348   bool _print_help;
 349   bool _print_class_stats;
 350   const char* _columns;
 351  public:
 352   HeapInspection(bool csv_format, bool print_help,
 353                  bool print_class_stats, const char *columns) :
 354       _csv_format(csv_format), _print_help(print_help),
 355       _print_class_stats(print_class_stats), _columns(columns) {}
 356   void heap_inspection(outputStream* st) NOT_SERVICES_RETURN;
 357   size_t populate_table(KlassInfoTable* cit, BoolObjectClosure* filter = NULL) NOT_SERVICES_RETURN;
 358   static void find_instances_at_safepoint(Klass* k, GrowableArray<oop>* result) NOT_SERVICES_RETURN;
 359  private:
 360   void iterate_over_heap(KlassInfoTable* cit, BoolObjectClosure* filter = NULL);
 361 };
 362 
 363 #endif // SHARE_VM_MEMORY_HEAPINSPECTION_HPP