heapInspection.cpp 9.4 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24
 *
 */

25 26 27 28 29 30 31 32 33 34
#include "precompiled.hpp"
#include "gc_interface/collectedHeap.hpp"
#include "memory/genCollectedHeap.hpp"
#include "memory/heapInspection.hpp"
#include "memory/resourceArea.hpp"
#include "runtime/os.hpp"
#include "utilities/globalDefinitions.hpp"
#ifndef SERIALGC
#include "gc_implementation/parallelScavenge/parallelScavengeHeap.hpp"
#endif
D
duke 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

// HeapInspection

int KlassInfoEntry::compare(KlassInfoEntry* e1, KlassInfoEntry* e2) {
  if(e1->_instance_words > e2->_instance_words) {
    return -1;
  } else if(e1->_instance_words < e2->_instance_words) {
    return 1;
  }
  return 0;
}

void KlassInfoEntry::print_on(outputStream* st) const {
  ResourceMark rm;
  const char* name;;
50 51
  if (_klass->name() != NULL) {
    name = _klass->external_name();
D
duke 已提交
52 53 54 55 56 57 58 59 60 61 62 63
  } else {
    if (_klass == Universe::boolArrayKlassObj())         name = "<boolArrayKlass>";         else
    if (_klass == Universe::charArrayKlassObj())         name = "<charArrayKlass>";         else
    if (_klass == Universe::singleArrayKlassObj())       name = "<singleArrayKlass>";       else
    if (_klass == Universe::doubleArrayKlassObj())       name = "<doubleArrayKlass>";       else
    if (_klass == Universe::byteArrayKlassObj())         name = "<byteArrayKlass>";         else
    if (_klass == Universe::shortArrayKlassObj())        name = "<shortArrayKlass>";        else
    if (_klass == Universe::intArrayKlassObj())          name = "<intArrayKlass>";          else
    if (_klass == Universe::longArrayKlassObj())         name = "<longArrayKlass>";         else
      name = "<no name>";
  }
  // simplify the formatting (ILP32 vs LP64) - always cast the numbers to 64-bit
64
  st->print_cr(INT64_FORMAT_W(13) "  " UINT64_FORMAT_W(13) "  %s",
D
duke 已提交
65 66 67 68 69
               (jlong)  _instance_count,
               (julong) _instance_words * HeapWordSize,
               name);
}

70
KlassInfoEntry* KlassInfoBucket::lookup(Klass* const k) {
D
duke 已提交
71 72 73 74 75 76 77 78
  KlassInfoEntry* elt = _list;
  while (elt != NULL) {
    if (elt->is_equal(k)) {
      return elt;
    }
    elt = elt->next();
  }
  elt = new KlassInfoEntry(k, list());
79 80 81 82
  // We may be out of space to allocate the new entry.
  if (elt != NULL) {
    set_list(elt);
  }
D
duke 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
  return elt;
}

void KlassInfoBucket::iterate(KlassInfoClosure* cic) {
  KlassInfoEntry* elt = _list;
  while (elt != NULL) {
    cic->do_cinfo(elt);
    elt = elt->next();
  }
}

void KlassInfoBucket::empty() {
  KlassInfoEntry* elt = _list;
  _list = NULL;
  while (elt != NULL) {
    KlassInfoEntry* next = elt->next();
    delete elt;
    elt = next;
  }
}

KlassInfoTable::KlassInfoTable(int size, HeapWord* ref) {
105
  _size = 0;
D
duke 已提交
106
  _ref = ref;
Z
zgu 已提交
107
  _buckets = NEW_C_HEAP_ARRAY(KlassInfoBucket, size, mtInternal);
108 109 110 111 112
  if (_buckets != NULL) {
    _size = size;
    for (int index = 0; index < _size; index++) {
      _buckets[index].initialize();
    }
D
duke 已提交
113 114 115 116
  }
}

KlassInfoTable::~KlassInfoTable() {
117 118 119 120
  if (_buckets != NULL) {
    for (int index = 0; index < _size; index++) {
      _buckets[index].empty();
    }
Z
zgu 已提交
121
    FREE_C_HEAP_ARRAY(KlassInfoBucket, _buckets, mtInternal);
122
    _size = 0;
D
duke 已提交
123 124 125
  }
}

126 127
uint KlassInfoTable::hash(Klass* p) {
  assert(p->is_metadata(), "all klasses are metadata");
D
duke 已提交
128 129 130
  return (uint)(((uintptr_t)p - (uintptr_t)_ref) >> 2);
}

131
KlassInfoEntry* KlassInfoTable::lookup(Klass* const k) {
D
duke 已提交
132
  uint         idx = hash(k) % _size;
133
  assert(_buckets != NULL, "Allocation failure should have been caught");
D
duke 已提交
134
  KlassInfoEntry*  e   = _buckets[idx].lookup(k);
135 136 137
  // Lookup may fail if this is a new klass for which we
  // could not allocate space for an new entry.
  assert(e == NULL || k == e->klass(), "must be equal");
D
duke 已提交
138 139 140
  return e;
}

141 142 143
// Return false if the entry could not be recorded on account
// of running out of space required to create a new entry.
bool KlassInfoTable::record_instance(const oop obj) {
144
  Klass*        k = obj->klass();
D
duke 已提交
145
  KlassInfoEntry* elt = lookup(k);
146 147 148 149 150 151 152 153 154
  // elt may be NULL if it's a new klass for which we
  // could not allocate space for a new entry in the hashtable.
  if (elt != NULL) {
    elt->set_count(elt->count() + 1);
    elt->set_words(elt->words() + obj->size());
    return true;
  } else {
    return false;
  }
D
duke 已提交
155 156 157
}

void KlassInfoTable::iterate(KlassInfoClosure* cic) {
158
  assert(_size == 0 || _buckets != NULL, "Allocation failure should have been caught");
D
duke 已提交
159 160 161 162 163 164 165 166 167 168 169
  for (int index = 0; index < _size; index++) {
    _buckets[index].iterate(cic);
  }
}

int KlassInfoHisto::sort_helper(KlassInfoEntry** e1, KlassInfoEntry** e2) {
  return (*e1)->compare(*e1,*e2);
}

KlassInfoHisto::KlassInfoHisto(const char* title, int estimatedCount) :
  _title(title) {
Z
zgu 已提交
170
  _elements = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<KlassInfoEntry*>(estimatedCount,true);
D
duke 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
}

KlassInfoHisto::~KlassInfoHisto() {
  delete _elements;
}

void KlassInfoHisto::add(KlassInfoEntry* cie) {
  elements()->append(cie);
}

void KlassInfoHisto::sort() {
  elements()->sort(KlassInfoHisto::sort_helper);
}

void KlassInfoHisto::print_elements(outputStream* st) const {
  // simplify the formatting (ILP32 vs LP64) - store the sum in 64-bit
  jlong total = 0;
  julong totalw = 0;
  for(int i=0; i < elements()->length(); i++) {
    st->print("%4d: ", i+1);
    elements()->at(i)->print_on(st);
    total += elements()->at(i)->count();
    totalw += elements()->at(i)->words();
  }
195
  st->print_cr("Total " INT64_FORMAT_W(13) "  " UINT64_FORMAT_W(13),
D
duke 已提交
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
               total, totalw * HeapWordSize);
}

void KlassInfoHisto::print_on(outputStream* st) const {
  st->print_cr("%s",title());
  print_elements(st);
}

class HistoClosure : public KlassInfoClosure {
 private:
  KlassInfoHisto* _cih;
 public:
  HistoClosure(KlassInfoHisto* cih) : _cih(cih) {}

  void do_cinfo(KlassInfoEntry* cie) {
    _cih->add(cie);
  }
};

class RecordInstanceClosure : public ObjectClosure {
 private:
  KlassInfoTable* _cit;
218
  size_t _missed_count;
D
duke 已提交
219
 public:
220 221
  RecordInstanceClosure(KlassInfoTable* cit) :
    _cit(cit), _missed_count(0) {}
D
duke 已提交
222 223

  void do_object(oop obj) {
224 225 226
    if (!_cit->record_instance(obj)) {
      _missed_count++;
    }
D
duke 已提交
227
  }
228 229

  size_t missed_count() { return _missed_count; }
D
duke 已提交
230 231
};

232
void HeapInspection::heap_inspection(outputStream* st, bool need_prologue) {
D
duke 已提交
233
  ResourceMark rm;
234 235
  // Get some random number for ref (the hash key)
  HeapWord* ref = (HeapWord*) Universe::boolArrayKlassObj();
D
duke 已提交
236
  CollectedHeap* heap = Universe::heap();
237
  bool is_shared_heap = false;
238

D
duke 已提交
239 240
  // Collect klass instance info
  KlassInfoTable cit(KlassInfoTable::cit_size, ref);
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
  if (!cit.allocation_failed()) {
    // Iterate over objects in the heap
    RecordInstanceClosure ric(&cit);
    Universe::heap()->object_iterate(&ric);

    // Report if certain classes are not counted because of
    // running out of C-heap for the histogram.
    size_t missed_count = ric.missed_count();
    if (missed_count != 0) {
      st->print_cr("WARNING: Ran out of C-heap; undercounted " SIZE_FORMAT
                   " total instances in data below",
                   missed_count);
    }
    // Sort and print klass instance info
    KlassInfoHisto histo("\n"
                     " num     #instances         #bytes  class name\n"
                     "----------------------------------------------",
                     KlassInfoHisto::histo_initial_size);
    HistoClosure hc(&histo);
    cit.iterate(&hc);
    histo.sort();
    histo.print_on(st);
  } else {
    st->print_cr("WARNING: Ran out of C-heap; histogram not generated");
  }
D
duke 已提交
266 267
  st->flush();

268
  if (need_prologue && is_shared_heap) {
269 270
    SharedHeap* sh = (SharedHeap*)heap;
    sh->gc_epilogue(false /* !full */); // release all acquired locks, etc.
D
duke 已提交
271 272 273 274 275
  }
}

class FindInstanceClosure : public ObjectClosure {
 private:
276
  Klass* _klass;
D
duke 已提交
277 278 279
  GrowableArray<oop>* _result;

 public:
280
  FindInstanceClosure(Klass* k, GrowableArray<oop>* result) : _klass(k), _result(result) {};
D
duke 已提交
281 282 283 284 285 286 287 288

  void do_object(oop obj) {
    if (obj->is_a(_klass)) {
      _result->append(obj);
    }
  }
};

289
void HeapInspection::find_instances_at_safepoint(Klass* k, GrowableArray<oop>* result) {
D
duke 已提交
290
  assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped");
291
  assert(Heap_lock->is_locked(), "should have the Heap_lock");
D
duke 已提交
292 293 294 295 296 297

  // Ensure that the heap is parsable
  Universe::heap()->ensure_parsability(false);  // no need to retire TALBs

  // Iterate over objects in the heap
  FindInstanceClosure fic(k, result);
298
  // If this operation encounters a bad object when using CMS,
299
  // consider using safe_object_iterate() which avoids metadata
300
  // objects that may contain bad references.
D
duke 已提交
301 302
  Universe::heap()->object_iterate(&fic);
}