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 "memory/klassInfoClosure.hpp"
  30 #include "oops/oop.inline.hpp"
  31 
  32 
  33 // HeapInspection
  34 
  35 // KlassInfoTable is a bucket hash table that
  36 // maps klassOops to extra information:
  37 //    instance count and instance word size.
  38 //
  39 // A KlassInfoBucket is the head of a link list
  40 // of KlassInfoEntry's
  41 //
  42 // KlassInfoHisto is a growable array of pointers
  43 // to KlassInfoEntry's and is used to sort
  44 // the entries.
  45 
  46 class KlassInfoEntry: public CHeapObj<mtInternal> {
  47  private:
  48   KlassInfoEntry* _next;
  49   klassOop        _klass;
  50   long            _instance_count;
  51   size_t          _instance_words;
  52 
  53  public:
  54   KlassInfoEntry(klassOop k, KlassInfoEntry* next) :
  55     _klass(k), _instance_count(0), _instance_words(0), _next(next)
  56   {}
  57   KlassInfoEntry* next()     { return _next; }
  58   bool is_equal(klassOop k)  { return k == _klass; }
  59   klassOop klass()           { return _klass; }
  60   long count()               { return _instance_count; }
  61   void set_count(long ct)    { _instance_count = ct; }
  62   size_t words()             { return _instance_words; }
  63   void set_words(size_t wds) { _instance_words = wds; }
  64   int compare(KlassInfoEntry* e1, KlassInfoEntry* e2);
  65   void print_on(outputStream* st) const;
  66 };
  67 
  68 class KlassInfoBucket: public CHeapObj<mtInternal> {
  69  private:
  70   KlassInfoEntry* _list;
  71   KlassInfoEntry* list()           { return _list; }
  72   void set_list(KlassInfoEntry* l) { _list = l; }
  73  public:
  74   KlassInfoEntry* lookup(const klassOop k);
  75   void initialize() { _list = NULL; }
  76   void empty();
  77   void iterate(KlassInfoClosure* cic);
  78 };
  79 
  80 class KlassInfoTable: public StackObj {
  81  private:
  82   int _size;
  83   static const int _num_buckets = 20011;
  84   size_t _size_of_instances_in_words;
  85 
  86   // An aligned reference address (typically the least
  87   // address in the perm gen) used for hashing klass
  88   // objects.
  89   HeapWord* _ref;
  90 
  91   KlassInfoBucket* _buckets;
  92   uint hash(klassOop p);
  93   KlassInfoEntry* lookup(const klassOop k);
  94 
  95  public:
  96   KlassInfoTable(HeapWord* ref);
  97   ~KlassInfoTable();
  98   bool record_instance(const oop obj);
  99   void iterate(KlassInfoClosure* cic);
 100   bool allocation_failed() { return _buckets == NULL; }
 101   size_t size_of_instances_in_words() const;
 102 };
 103 
 104 class KlassInfoHisto : public StackObj {
 105  private:
 106   GrowableArray<KlassInfoEntry*>* _elements;
 107   GrowableArray<KlassInfoEntry*>* elements() const { return _elements; }
 108   const char* _title;
 109   const char* title() const { return _title; }
 110   static int sort_helper(KlassInfoEntry** e1, KlassInfoEntry** e2);
 111   void print_elements(outputStream* st) const;
 112   static const int _histo_initial_size = 1000;
 113  public:
 114   KlassInfoHisto(const char* title);
 115   ~KlassInfoHisto();
 116   void add(KlassInfoEntry* cie);
 117   void print_on(outputStream* st) const;
 118   void sort();
 119 };
 120 
 121 
 122 class HeapInspection : public AllStatic {
 123  public:
 124   static void heap_inspection(outputStream* st, bool need_prologue);
 125   static size_t populate_table(KlassInfoTable* cit,
 126                                bool need_prologue,
 127                                BoolObjectClosure* filter = NULL);
 128   static HeapWord* start_of_perm_gen();
 129   static void find_instances_at_safepoint(klassOop k, GrowableArray<oop>* result);
 130  private:
 131   static bool is_shared_heap();
 132   static void prologue();
 133   static void epilogue();
 134 };
 135 
 136 #endif // SHARE_VM_MEMORY_HEAPINSPECTION_HPP