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