1 /*
   2  * Copyright (c) 2002, 2020, 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_MEMORY_HEAPINSPECTION_HPP
  26 #define SHARE_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 #include "gc/shared/workgroup.hpp"
  34 
  35 #if INCLUDE_SERVICES
  36 
  37 
  38 // HeapInspection
  39 
  40 // KlassInfoTable is a bucket hash table that
  41 // maps Klass*s to extra information:
  42 //    instance count and instance word size.
  43 //
  44 // A KlassInfoBucket is the head of a link list
  45 // of KlassInfoEntry's
  46 //
  47 // KlassInfoHisto is a growable array of pointers
  48 // to KlassInfoEntry's and is used to sort
  49 // the entries.
  50 
  51 class KlassInfoEntry: public CHeapObj<mtInternal> {
  52  private:
  53   KlassInfoEntry* _next;
  54   Klass*          _klass;
  55   long            _instance_count;
  56   size_t          _instance_words;
  57   long            _index;
  58   bool            _do_print; // True if we should print this class when printing the class hierarchy.
  59   GrowableArray<KlassInfoEntry*>* _subclasses;
  60 
  61  public:
  62   KlassInfoEntry(Klass* k, KlassInfoEntry* next) :
  63     _next(next), _klass(k), _instance_count(0), _instance_words(0), _index(-1),
  64     _do_print(false), _subclasses(NULL)
  65   {}
  66   ~KlassInfoEntry();
  67   KlassInfoEntry* next() const   { return _next; }
  68   bool is_equal(const Klass* k)  { return k == _klass; }
  69   Klass* klass()  const      { return _klass; }
  70   long count()    const      { return _instance_count; }
  71   void set_count(long ct)    { _instance_count = ct; }
  72   size_t words()  const      { return _instance_words; }
  73   void set_words(size_t wds) { _instance_words = wds; }
  74   void set_index(long index) { _index = index; }
  75   long index()    const      { return _index; }
  76   GrowableArray<KlassInfoEntry*>* subclasses() const { return _subclasses; }
  77   void add_subclass(KlassInfoEntry* cie);
  78   void set_do_print(bool do_print) { _do_print = do_print; }
  79   bool do_print() const      { return _do_print; }
  80   int compare(KlassInfoEntry* e1, KlassInfoEntry* e2);
  81   void print_on(outputStream* st) const;
  82   const char* name() const;
  83 };
  84 
  85 class KlassInfoClosure : public StackObj {
  86  public:
  87   // Called for each KlassInfoEntry.
  88   virtual void do_cinfo(KlassInfoEntry* cie) = 0;
  89 };
  90 
  91 class KlassInfoBucket: public CHeapObj<mtInternal> {
  92  private:
  93   KlassInfoEntry* _list;
  94   KlassInfoEntry* list()           { return _list; }
  95   void set_list(KlassInfoEntry* l) { _list = l; }
  96  public:
  97   KlassInfoEntry* lookup(Klass* k);
  98   void initialize() { _list = NULL; }
  99   void empty();
 100   void iterate(KlassInfoClosure* cic);
 101 };
 102 
 103 class KlassInfoTable: public StackObj {
 104  private:
 105   static const int _num_buckets = 20011;
 106   size_t _size_of_instances_in_words;
 107 
 108   // An aligned reference address (typically the least
 109   // address in the perm gen) used for hashing klass
 110   // objects.
 111   HeapWord* _ref;
 112 
 113   KlassInfoBucket* _buckets;
 114   uint hash(const Klass* p);
 115   KlassInfoEntry* lookup(Klass* k); // allocates if not found!
 116 
 117   class AllClassesFinder;
 118 
 119  public:
 120   KlassInfoTable(bool add_all_classes);
 121   ~KlassInfoTable();
 122   bool record_instance(const oop obj);
 123   void iterate(KlassInfoClosure* cic);
 124   bool allocation_failed() { return _buckets == NULL; }
 125   size_t size_of_instances_in_words() const;
 126   bool merge(KlassInfoTable* table);
 127   bool merge_entry(const KlassInfoEntry* cie);
 128 
 129   friend class KlassInfoHisto;
 130   friend class KlassHierarchy;
 131 };
 132 
 133 class KlassHierarchy : AllStatic {
 134  public:
 135   static void print_class_hierarchy(outputStream* st, bool print_interfaces,  bool print_subclasses,
 136                                     char* classname);
 137 
 138  private:
 139   static void set_do_print_for_class_hierarchy(KlassInfoEntry* cie, KlassInfoTable* cit,
 140                                                bool print_subclasse);
 141   static void print_class(outputStream* st, KlassInfoEntry* cie, bool print_subclasses);
 142 };
 143 
 144 class KlassInfoHisto : public StackObj {
 145  private:
 146   static const int _histo_initial_size = 1000;
 147   KlassInfoTable *_cit;
 148   GrowableArray<KlassInfoEntry*>* _elements;
 149   GrowableArray<KlassInfoEntry*>* elements() const { return _elements; }
 150   static int sort_helper(KlassInfoEntry** e1, KlassInfoEntry** e2);
 151   void print_elements(outputStream* st) const;
 152   bool is_selected(const char *col_name);
 153 
 154   template <class T> static int count_bytes(T* x) {
 155     return (HeapWordSize * ((x) ? (x)->size() : 0));
 156   }
 157 
 158   template <class T> static int count_bytes_array(T* x) {
 159     if (x == NULL) {
 160       return 0;
 161     }
 162     if (x->length() == 0) {
 163       // This is a shared array, e.g., Universe::the_empty_int_array(). Don't
 164       // count it to avoid double-counting.
 165       return 0;
 166     }
 167     return HeapWordSize * x->size();
 168   }
 169 
 170   static void print_julong(outputStream* st, int width, julong n) {
 171     int num_spaces = width - julong_width(n);
 172     if (num_spaces > 0) {
 173       st->print("%*s", num_spaces, "");
 174     }
 175     st->print(JULONG_FORMAT, n);
 176   }
 177 
 178   static int julong_width(julong n) {
 179     if (n == 0) {
 180       return 1;
 181     }
 182     int w = 0;
 183     while (n > 0) {
 184       n /= 10;
 185       w += 1;
 186     }
 187     return w;
 188   }
 189 
 190   static int col_width(julong n, const char *name) {
 191     int w = julong_width(n);
 192     int min = (int)(strlen(name));
 193     if (w < min) {
 194         w = min;
 195     }
 196     // add a leading space for separation.
 197     return w + 1;
 198   }
 199 
 200  public:
 201   KlassInfoHisto(KlassInfoTable* cit);
 202   ~KlassInfoHisto();
 203   void add(KlassInfoEntry* cie);
 204   void print_histo_on(outputStream* st);
 205   void sort();
 206 };
 207 
 208 #endif // INCLUDE_SERVICES
 209 
 210 // These declarations are needed since the declaration of KlassInfoTable and
 211 // KlassInfoClosure are guarded by #if INLCUDE_SERVICES
 212 class KlassInfoTable;
 213 class KlassInfoClosure;
 214 
 215 class HeapInspection : public StackObj {
 216  public:
 217   void heap_inspection(outputStream* st, size_t parallel_thread_num = 1) NOT_SERVICES_RETURN;
 218   size_t populate_table(KlassInfoTable* cit, BoolObjectClosure* filter = NULL, size_t parallel_thread_num = 1) NOT_SERVICES_RETURN_(0);
 219   static void find_instances_at_safepoint(Klass* k, GrowableArray<oop>* result) NOT_SERVICES_RETURN;
 220  private:
 221   void iterate_over_heap(KlassInfoTable* cit, BoolObjectClosure* filter = NULL);
 222 };
 223 
 224 class ParHeapInspectTask : public AbstractGangTask {
 225  private:
 226   CollectedHeap* _heap;
 227   KlassInfoTable* _shared_cit;
 228   BoolObjectClosure* _filter;
 229   size_t* _shared_missed_count;
 230   bool _success;
 231   Mutex _mutex;
 232   size_t _par_thread_num;
 233 
 234  public:
 235   ParHeapInspectTask(CollectedHeap* heap, KlassInfoTable* shared_cit,
 236                      BoolObjectClosure* filter, size_t* shared_missed_count,
 237                      size_t parallel_thread_num) :
 238       AbstractGangTask("Iterating heap"),
 239       _heap(heap),
 240       _shared_cit(shared_cit),
 241       _filter(filter),
 242       _shared_missed_count(shared_missed_count),
 243       _success(true),
 244       _mutex(Mutex::leaf, "Parallel heap iteration data merge lock"),
 245       _par_thread_num(parallel_thread_num) { }
 246 
 247   bool is_success() {
 248     return _success;
 249   }
 250 
 251   CollectedHeap* getHeap() {
 252     return _heap;
 253   }
 254   virtual void work(uint worker_id);
 255   virtual void do_object_iterate_parallel(ObjectClosure* closure, uint worker_id) = 0;
 256 };
 257 
 258 #endif // SHARE_MEMORY_HEAPINSPECTION_HPP