src/share/vm/memory/heapInspection.cpp

Thu, 12 Oct 2017 21:27:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 21:27:07 +0800
changeset 7535
7ae4e26cb1e0
parent 7074
833b0f92429a
parent 6876
710a3c8b516e
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2002, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "classfile/classLoaderData.hpp"
aoqi@0 27 #include "gc_interface/collectedHeap.hpp"
aoqi@0 28 #include "memory/genCollectedHeap.hpp"
aoqi@0 29 #include "memory/heapInspection.hpp"
aoqi@0 30 #include "memory/resourceArea.hpp"
aoqi@0 31 #include "runtime/os.hpp"
aoqi@0 32 #include "utilities/globalDefinitions.hpp"
aoqi@0 33 #include "utilities/macros.hpp"
aoqi@0 34 #if INCLUDE_ALL_GCS
aoqi@0 35 #include "gc_implementation/parallelScavenge/parallelScavengeHeap.hpp"
aoqi@0 36 #endif // INCLUDE_ALL_GCS
aoqi@0 37
aoqi@0 38 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
aoqi@0 39
aoqi@0 40 // HeapInspection
aoqi@0 41
aoqi@0 42 int KlassInfoEntry::compare(KlassInfoEntry* e1, KlassInfoEntry* e2) {
aoqi@0 43 if(e1->_instance_words > e2->_instance_words) {
aoqi@0 44 return -1;
aoqi@0 45 } else if(e1->_instance_words < e2->_instance_words) {
aoqi@0 46 return 1;
aoqi@0 47 }
aoqi@0 48 // Sort alphabetically, note 'Z' < '[' < 'a', but it's better to group
aoqi@0 49 // the array classes before all the instance classes.
aoqi@0 50 ResourceMark rm;
aoqi@0 51 const char* name1 = e1->klass()->external_name();
aoqi@0 52 const char* name2 = e2->klass()->external_name();
aoqi@0 53 bool d1 = (name1[0] == '[');
aoqi@0 54 bool d2 = (name2[0] == '[');
aoqi@0 55 if (d1 && !d2) {
aoqi@0 56 return -1;
aoqi@0 57 } else if (d2 && !d1) {
aoqi@0 58 return 1;
aoqi@0 59 } else {
aoqi@0 60 return strcmp(name1, name2);
aoqi@0 61 }
aoqi@0 62 }
aoqi@0 63
aoqi@0 64 const char* KlassInfoEntry::name() const {
aoqi@0 65 const char* name;
aoqi@0 66 if (_klass->name() != NULL) {
aoqi@0 67 name = _klass->external_name();
aoqi@0 68 } else {
aoqi@0 69 if (_klass == Universe::boolArrayKlassObj()) name = "<boolArrayKlass>"; else
aoqi@0 70 if (_klass == Universe::charArrayKlassObj()) name = "<charArrayKlass>"; else
aoqi@0 71 if (_klass == Universe::singleArrayKlassObj()) name = "<singleArrayKlass>"; else
aoqi@0 72 if (_klass == Universe::doubleArrayKlassObj()) name = "<doubleArrayKlass>"; else
aoqi@0 73 if (_klass == Universe::byteArrayKlassObj()) name = "<byteArrayKlass>"; else
aoqi@0 74 if (_klass == Universe::shortArrayKlassObj()) name = "<shortArrayKlass>"; else
aoqi@0 75 if (_klass == Universe::intArrayKlassObj()) name = "<intArrayKlass>"; else
aoqi@0 76 if (_klass == Universe::longArrayKlassObj()) name = "<longArrayKlass>"; else
aoqi@0 77 name = "<no name>";
aoqi@0 78 }
aoqi@0 79 return name;
aoqi@0 80 }
aoqi@0 81
aoqi@0 82 void KlassInfoEntry::print_on(outputStream* st) const {
aoqi@0 83 ResourceMark rm;
aoqi@0 84
aoqi@0 85 // simplify the formatting (ILP32 vs LP64) - always cast the numbers to 64-bit
aoqi@0 86 st->print_cr(INT64_FORMAT_W(13) " " UINT64_FORMAT_W(13) " %s",
aoqi@0 87 (jlong) _instance_count,
aoqi@0 88 (julong) _instance_words * HeapWordSize,
aoqi@0 89 name());
aoqi@0 90 }
aoqi@0 91
aoqi@0 92 KlassInfoEntry* KlassInfoBucket::lookup(Klass* const k) {
aoqi@0 93 KlassInfoEntry* elt = _list;
aoqi@0 94 while (elt != NULL) {
aoqi@0 95 if (elt->is_equal(k)) {
aoqi@0 96 return elt;
aoqi@0 97 }
aoqi@0 98 elt = elt->next();
aoqi@0 99 }
aoqi@0 100 elt = new (std::nothrow) KlassInfoEntry(k, list());
aoqi@0 101 // We may be out of space to allocate the new entry.
aoqi@0 102 if (elt != NULL) {
aoqi@0 103 set_list(elt);
aoqi@0 104 }
aoqi@0 105 return elt;
aoqi@0 106 }
aoqi@0 107
aoqi@0 108 void KlassInfoBucket::iterate(KlassInfoClosure* cic) {
aoqi@0 109 KlassInfoEntry* elt = _list;
aoqi@0 110 while (elt != NULL) {
aoqi@0 111 cic->do_cinfo(elt);
aoqi@0 112 elt = elt->next();
aoqi@0 113 }
aoqi@0 114 }
aoqi@0 115
aoqi@0 116 void KlassInfoBucket::empty() {
aoqi@0 117 KlassInfoEntry* elt = _list;
aoqi@0 118 _list = NULL;
aoqi@0 119 while (elt != NULL) {
aoqi@0 120 KlassInfoEntry* next = elt->next();
aoqi@0 121 delete elt;
aoqi@0 122 elt = next;
aoqi@0 123 }
aoqi@0 124 }
aoqi@0 125
aoqi@0 126 void KlassInfoTable::AllClassesFinder::do_klass(Klass* k) {
aoqi@0 127 // This has the SIDE EFFECT of creating a KlassInfoEntry
aoqi@0 128 // for <k>, if one doesn't exist yet.
aoqi@0 129 _table->lookup(k);
aoqi@0 130 }
aoqi@0 131
aoqi@0 132 KlassInfoTable::KlassInfoTable(bool need_class_stats) {
aoqi@0 133 _size_of_instances_in_words = 0;
aoqi@0 134 _size = 0;
aoqi@0 135 _ref = (HeapWord*) Universe::boolArrayKlassObj();
aoqi@0 136 _buckets =
aoqi@0 137 (KlassInfoBucket*) AllocateHeap(sizeof(KlassInfoBucket) * _num_buckets,
zgu@7074 138 mtInternal, CURRENT_PC, AllocFailStrategy::RETURN_NULL);
aoqi@0 139 if (_buckets != NULL) {
aoqi@0 140 _size = _num_buckets;
aoqi@0 141 for (int index = 0; index < _size; index++) {
aoqi@0 142 _buckets[index].initialize();
aoqi@0 143 }
aoqi@0 144 if (need_class_stats) {
aoqi@0 145 AllClassesFinder finder(this);
aoqi@0 146 ClassLoaderDataGraph::classes_do(&finder);
aoqi@0 147 }
aoqi@0 148 }
aoqi@0 149 }
aoqi@0 150
aoqi@0 151 KlassInfoTable::~KlassInfoTable() {
aoqi@0 152 if (_buckets != NULL) {
aoqi@0 153 for (int index = 0; index < _size; index++) {
aoqi@0 154 _buckets[index].empty();
aoqi@0 155 }
aoqi@0 156 FREE_C_HEAP_ARRAY(KlassInfoBucket, _buckets, mtInternal);
aoqi@0 157 _size = 0;
aoqi@0 158 }
aoqi@0 159 }
aoqi@0 160
aoqi@0 161 uint KlassInfoTable::hash(const Klass* p) {
aoqi@0 162 return (uint)(((uintptr_t)p - (uintptr_t)_ref) >> 2);
aoqi@0 163 }
aoqi@0 164
aoqi@0 165 KlassInfoEntry* KlassInfoTable::lookup(Klass* k) {
aoqi@0 166 uint idx = hash(k) % _size;
aoqi@0 167 assert(_buckets != NULL, "Allocation failure should have been caught");
aoqi@0 168 KlassInfoEntry* e = _buckets[idx].lookup(k);
aoqi@0 169 // Lookup may fail if this is a new klass for which we
aoqi@0 170 // could not allocate space for an new entry.
aoqi@0 171 assert(e == NULL || k == e->klass(), "must be equal");
aoqi@0 172 return e;
aoqi@0 173 }
aoqi@0 174
aoqi@0 175 // Return false if the entry could not be recorded on account
aoqi@0 176 // of running out of space required to create a new entry.
aoqi@0 177 bool KlassInfoTable::record_instance(const oop obj) {
aoqi@0 178 Klass* k = obj->klass();
aoqi@0 179 KlassInfoEntry* elt = lookup(k);
aoqi@0 180 // elt may be NULL if it's a new klass for which we
aoqi@0 181 // could not allocate space for a new entry in the hashtable.
aoqi@0 182 if (elt != NULL) {
aoqi@0 183 elt->set_count(elt->count() + 1);
aoqi@0 184 elt->set_words(elt->words() + obj->size());
aoqi@0 185 _size_of_instances_in_words += obj->size();
aoqi@0 186 return true;
aoqi@0 187 } else {
aoqi@0 188 return false;
aoqi@0 189 }
aoqi@0 190 }
aoqi@0 191
aoqi@0 192 void KlassInfoTable::iterate(KlassInfoClosure* cic) {
aoqi@0 193 assert(_size == 0 || _buckets != NULL, "Allocation failure should have been caught");
aoqi@0 194 for (int index = 0; index < _size; index++) {
aoqi@0 195 _buckets[index].iterate(cic);
aoqi@0 196 }
aoqi@0 197 }
aoqi@0 198
aoqi@0 199 size_t KlassInfoTable::size_of_instances_in_words() const {
aoqi@0 200 return _size_of_instances_in_words;
aoqi@0 201 }
aoqi@0 202
aoqi@0 203 int KlassInfoHisto::sort_helper(KlassInfoEntry** e1, KlassInfoEntry** e2) {
aoqi@0 204 return (*e1)->compare(*e1,*e2);
aoqi@0 205 }
aoqi@0 206
aoqi@0 207 KlassInfoHisto::KlassInfoHisto(KlassInfoTable* cit, const char* title) :
aoqi@0 208 _cit(cit),
aoqi@0 209 _title(title) {
aoqi@0 210 _elements = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<KlassInfoEntry*>(_histo_initial_size, true);
aoqi@0 211 }
aoqi@0 212
aoqi@0 213 KlassInfoHisto::~KlassInfoHisto() {
aoqi@0 214 delete _elements;
aoqi@0 215 }
aoqi@0 216
aoqi@0 217 void KlassInfoHisto::add(KlassInfoEntry* cie) {
aoqi@0 218 elements()->append(cie);
aoqi@0 219 }
aoqi@0 220
aoqi@0 221 void KlassInfoHisto::sort() {
aoqi@0 222 elements()->sort(KlassInfoHisto::sort_helper);
aoqi@0 223 }
aoqi@0 224
aoqi@0 225 void KlassInfoHisto::print_elements(outputStream* st) const {
aoqi@0 226 // simplify the formatting (ILP32 vs LP64) - store the sum in 64-bit
aoqi@0 227 jlong total = 0;
aoqi@0 228 julong totalw = 0;
aoqi@0 229 for(int i=0; i < elements()->length(); i++) {
aoqi@0 230 st->print("%4d: ", i+1);
aoqi@0 231 elements()->at(i)->print_on(st);
aoqi@0 232 total += elements()->at(i)->count();
aoqi@0 233 totalw += elements()->at(i)->words();
aoqi@0 234 }
aoqi@0 235 st->print_cr("Total " INT64_FORMAT_W(13) " " UINT64_FORMAT_W(13),
aoqi@0 236 total, totalw * HeapWordSize);
aoqi@0 237 }
aoqi@0 238
aoqi@0 239 #define MAKE_COL_NAME(field, name, help) #name,
aoqi@0 240 #define MAKE_COL_HELP(field, name, help) help,
aoqi@0 241
aoqi@0 242 static const char *name_table[] = {
aoqi@0 243 HEAP_INSPECTION_COLUMNS_DO(MAKE_COL_NAME)
aoqi@0 244 };
aoqi@0 245
aoqi@0 246 static const char *help_table[] = {
aoqi@0 247 HEAP_INSPECTION_COLUMNS_DO(MAKE_COL_HELP)
aoqi@0 248 };
aoqi@0 249
aoqi@0 250 bool KlassInfoHisto::is_selected(const char *col_name) {
aoqi@0 251 if (_selected_columns == NULL) {
aoqi@0 252 return true;
aoqi@0 253 }
aoqi@0 254 if (strcmp(_selected_columns, col_name) == 0) {
aoqi@0 255 return true;
aoqi@0 256 }
aoqi@0 257
aoqi@0 258 const char *start = strstr(_selected_columns, col_name);
aoqi@0 259 if (start == NULL) {
aoqi@0 260 return false;
aoqi@0 261 }
aoqi@0 262
aoqi@0 263 // The following must be true, because _selected_columns != col_name
aoqi@0 264 if (start > _selected_columns && start[-1] != ',') {
aoqi@0 265 return false;
aoqi@0 266 }
aoqi@0 267 char x = start[strlen(col_name)];
aoqi@0 268 if (x != ',' && x != '\0') {
aoqi@0 269 return false;
aoqi@0 270 }
aoqi@0 271
aoqi@0 272 return true;
aoqi@0 273 }
aoqi@0 274
aoqi@0 275 PRAGMA_FORMAT_NONLITERAL_IGNORED_EXTERNAL
aoqi@0 276 void KlassInfoHisto::print_title(outputStream* st, bool csv_format,
aoqi@0 277 bool selected[], int width_table[],
aoqi@0 278 const char *name_table[]) {
aoqi@0 279 if (csv_format) {
aoqi@0 280 st->print("Index,Super");
aoqi@0 281 for (int c=0; c<KlassSizeStats::_num_columns; c++) {
aoqi@0 282 if (selected[c]) {st->print(",%s", name_table[c]);}
aoqi@0 283 }
aoqi@0 284 st->print(",ClassName");
aoqi@0 285 } else {
aoqi@0 286 st->print("Index Super");
aoqi@0 287 for (int c=0; c<KlassSizeStats::_num_columns; c++) {
aoqi@0 288 PRAGMA_DIAG_PUSH
aoqi@0 289 PRAGMA_FORMAT_NONLITERAL_IGNORED_INTERNAL
aoqi@0 290 if (selected[c]) {st->print(str_fmt(width_table[c]), name_table[c]);}
aoqi@0 291 PRAGMA_DIAG_POP
aoqi@0 292 }
aoqi@0 293 st->print(" ClassName");
aoqi@0 294 }
aoqi@0 295
aoqi@0 296 if (is_selected("ClassLoader")) {
aoqi@0 297 st->print(",ClassLoader");
aoqi@0 298 }
aoqi@0 299 st->cr();
aoqi@0 300 }
aoqi@0 301
aoqi@0 302 void KlassInfoHisto::print_class_stats(outputStream* st,
aoqi@0 303 bool csv_format, const char *columns) {
aoqi@0 304 ResourceMark rm;
aoqi@0 305 KlassSizeStats sz, sz_sum;
aoqi@0 306 int i;
aoqi@0 307 julong *col_table = (julong*)(&sz);
aoqi@0 308 julong *colsum_table = (julong*)(&sz_sum);
aoqi@0 309 int width_table[KlassSizeStats::_num_columns];
aoqi@0 310 bool selected[KlassSizeStats::_num_columns];
aoqi@0 311
aoqi@0 312 _selected_columns = columns;
aoqi@0 313
aoqi@0 314 memset(&sz_sum, 0, sizeof(sz_sum));
aoqi@0 315 for (int c=0; c<KlassSizeStats::_num_columns; c++) {
aoqi@0 316 selected[c] = is_selected(name_table[c]);
aoqi@0 317 }
aoqi@0 318
aoqi@0 319 for(i=0; i < elements()->length(); i++) {
aoqi@0 320 elements()->at(i)->set_index(i+1);
aoqi@0 321 }
aoqi@0 322
aoqi@0 323 for (int pass=1; pass<=2; pass++) {
aoqi@0 324 if (pass == 2) {
aoqi@0 325 print_title(st, csv_format, selected, width_table, name_table);
aoqi@0 326 }
aoqi@0 327 for(i=0; i < elements()->length(); i++) {
aoqi@0 328 KlassInfoEntry* e = (KlassInfoEntry*)elements()->at(i);
aoqi@0 329 const Klass* k = e->klass();
aoqi@0 330
aoqi@0 331 memset(&sz, 0, sizeof(sz));
aoqi@0 332 sz._inst_count = e->count();
aoqi@0 333 sz._inst_bytes = HeapWordSize * e->words();
aoqi@0 334 k->collect_statistics(&sz);
aoqi@0 335 sz._total_bytes = sz._ro_bytes + sz._rw_bytes;
aoqi@0 336
aoqi@0 337 if (pass == 1) {
aoqi@0 338 for (int c=0; c<KlassSizeStats::_num_columns; c++) {
aoqi@0 339 colsum_table[c] += col_table[c];
aoqi@0 340 }
aoqi@0 341 } else {
aoqi@0 342 int super_index = -1;
aoqi@0 343 if (k->oop_is_instance()) {
aoqi@0 344 Klass* super = ((InstanceKlass*)k)->java_super();
aoqi@0 345 if (super) {
aoqi@0 346 KlassInfoEntry* super_e = _cit->lookup(super);
aoqi@0 347 if (super_e) {
aoqi@0 348 super_index = super_e->index();
aoqi@0 349 }
aoqi@0 350 }
aoqi@0 351 }
aoqi@0 352
aoqi@0 353 if (csv_format) {
aoqi@0 354 st->print("%d,%d", e->index(), super_index);
aoqi@0 355 for (int c=0; c<KlassSizeStats::_num_columns; c++) {
aoqi@0 356 if (selected[c]) {st->print("," JULONG_FORMAT, col_table[c]);}
aoqi@0 357 }
aoqi@0 358 st->print(",%s",e->name());
aoqi@0 359 } else {
aoqi@0 360 st->print("%5d %5d", e->index(), super_index);
aoqi@0 361 for (int c=0; c<KlassSizeStats::_num_columns; c++) {
aoqi@0 362 if (selected[c]) {print_julong(st, width_table[c], col_table[c]);}
aoqi@0 363 }
aoqi@0 364 st->print(" %s", e->name());
aoqi@0 365 }
aoqi@0 366 if (is_selected("ClassLoader")) {
aoqi@0 367 ClassLoaderData* loader_data = k->class_loader_data();
aoqi@0 368 st->print(",");
aoqi@0 369 loader_data->print_value_on(st);
aoqi@0 370 }
aoqi@0 371 st->cr();
aoqi@0 372 }
aoqi@0 373 }
aoqi@0 374
aoqi@0 375 if (pass == 1) {
aoqi@0 376 for (int c=0; c<KlassSizeStats::_num_columns; c++) {
aoqi@0 377 width_table[c] = col_width(colsum_table[c], name_table[c]);
aoqi@0 378 }
aoqi@0 379 }
aoqi@0 380 }
aoqi@0 381
aoqi@0 382 sz_sum._inst_size = 0;
aoqi@0 383
aoqi@0 384 if (csv_format) {
aoqi@0 385 st->print(",");
aoqi@0 386 for (int c=0; c<KlassSizeStats::_num_columns; c++) {
aoqi@0 387 if (selected[c]) {st->print("," JULONG_FORMAT, colsum_table[c]);}
aoqi@0 388 }
aoqi@0 389 } else {
aoqi@0 390 st->print(" ");
aoqi@0 391 for (int c=0; c<KlassSizeStats::_num_columns; c++) {
aoqi@0 392 if (selected[c]) {print_julong(st, width_table[c], colsum_table[c]);}
aoqi@0 393 }
aoqi@0 394 st->print(" Total");
aoqi@0 395 if (sz_sum._total_bytes > 0) {
aoqi@0 396 st->cr();
aoqi@0 397 st->print(" ");
aoqi@0 398 for (int c=0; c<KlassSizeStats::_num_columns; c++) {
aoqi@0 399 if (selected[c]) {
aoqi@0 400 switch (c) {
aoqi@0 401 case KlassSizeStats::_index_inst_size:
aoqi@0 402 case KlassSizeStats::_index_inst_count:
aoqi@0 403 case KlassSizeStats::_index_method_count:
aoqi@0 404 PRAGMA_DIAG_PUSH
aoqi@0 405 PRAGMA_FORMAT_NONLITERAL_IGNORED_INTERNAL
aoqi@0 406 st->print(str_fmt(width_table[c]), "-");
aoqi@0 407 PRAGMA_DIAG_POP
aoqi@0 408 break;
aoqi@0 409 default:
aoqi@0 410 {
aoqi@0 411 double perc = (double)(100) * (double)(colsum_table[c]) / (double)sz_sum._total_bytes;
aoqi@0 412 PRAGMA_DIAG_PUSH
aoqi@0 413 PRAGMA_FORMAT_NONLITERAL_IGNORED_INTERNAL
aoqi@0 414 st->print(perc_fmt(width_table[c]), perc);
aoqi@0 415 PRAGMA_DIAG_POP
aoqi@0 416 }
aoqi@0 417 }
aoqi@0 418 }
aoqi@0 419 }
aoqi@0 420 }
aoqi@0 421 }
aoqi@0 422 st->cr();
aoqi@0 423
aoqi@0 424 if (!csv_format) {
aoqi@0 425 print_title(st, csv_format, selected, width_table, name_table);
aoqi@0 426 }
aoqi@0 427 }
aoqi@0 428
aoqi@0 429 julong KlassInfoHisto::annotations_bytes(Array<AnnotationArray*>* p) const {
aoqi@0 430 julong bytes = 0;
aoqi@0 431 if (p != NULL) {
aoqi@0 432 for (int i = 0; i < p->length(); i++) {
aoqi@0 433 bytes += count_bytes_array(p->at(i));
aoqi@0 434 }
aoqi@0 435 bytes += count_bytes_array(p);
aoqi@0 436 }
aoqi@0 437 return bytes;
aoqi@0 438 }
aoqi@0 439
aoqi@0 440 void KlassInfoHisto::print_histo_on(outputStream* st, bool print_stats,
aoqi@0 441 bool csv_format, const char *columns) {
aoqi@0 442 if (print_stats) {
aoqi@0 443 print_class_stats(st, csv_format, columns);
aoqi@0 444 } else {
aoqi@0 445 st->print_cr("%s",title());
aoqi@0 446 print_elements(st);
aoqi@0 447 }
aoqi@0 448 }
aoqi@0 449
aoqi@0 450 class HistoClosure : public KlassInfoClosure {
aoqi@0 451 private:
aoqi@0 452 KlassInfoHisto* _cih;
aoqi@0 453 public:
aoqi@0 454 HistoClosure(KlassInfoHisto* cih) : _cih(cih) {}
aoqi@0 455
aoqi@0 456 void do_cinfo(KlassInfoEntry* cie) {
aoqi@0 457 _cih->add(cie);
aoqi@0 458 }
aoqi@0 459 };
aoqi@0 460
aoqi@0 461 class RecordInstanceClosure : public ObjectClosure {
aoqi@0 462 private:
aoqi@0 463 KlassInfoTable* _cit;
aoqi@0 464 size_t _missed_count;
aoqi@0 465 BoolObjectClosure* _filter;
aoqi@0 466 public:
aoqi@0 467 RecordInstanceClosure(KlassInfoTable* cit, BoolObjectClosure* filter) :
aoqi@0 468 _cit(cit), _missed_count(0), _filter(filter) {}
aoqi@0 469
aoqi@0 470 void do_object(oop obj) {
aoqi@0 471 if (should_visit(obj)) {
aoqi@0 472 if (!_cit->record_instance(obj)) {
aoqi@0 473 _missed_count++;
aoqi@0 474 }
aoqi@0 475 }
aoqi@0 476 }
aoqi@0 477
aoqi@0 478 size_t missed_count() { return _missed_count; }
aoqi@0 479
aoqi@0 480 private:
aoqi@0 481 bool should_visit(oop obj) {
aoqi@0 482 return _filter == NULL || _filter->do_object_b(obj);
aoqi@0 483 }
aoqi@0 484 };
aoqi@0 485
aoqi@0 486 size_t HeapInspection::populate_table(KlassInfoTable* cit, BoolObjectClosure *filter) {
aoqi@0 487 ResourceMark rm;
aoqi@0 488
aoqi@0 489 RecordInstanceClosure ric(cit, filter);
aoqi@0 490 Universe::heap()->object_iterate(&ric);
aoqi@0 491 return ric.missed_count();
aoqi@0 492 }
aoqi@0 493
aoqi@0 494 void HeapInspection::heap_inspection(outputStream* st) {
aoqi@0 495 ResourceMark rm;
aoqi@0 496
aoqi@0 497 if (_print_help) {
aoqi@0 498 for (int c=0; c<KlassSizeStats::_num_columns; c++) {
aoqi@0 499 st->print("%s:\n\t", name_table[c]);
aoqi@0 500 const int max_col = 60;
aoqi@0 501 int col = 0;
aoqi@0 502 for (const char *p = help_table[c]; *p; p++,col++) {
aoqi@0 503 if (col >= max_col && *p == ' ') {
aoqi@0 504 st->print("\n\t");
aoqi@0 505 col = 0;
aoqi@0 506 } else {
aoqi@0 507 st->print("%c", *p);
aoqi@0 508 }
aoqi@0 509 }
aoqi@0 510 st->print_cr(".\n");
aoqi@0 511 }
aoqi@0 512 return;
aoqi@0 513 }
aoqi@0 514
aoqi@0 515 KlassInfoTable cit(_print_class_stats);
aoqi@0 516 if (!cit.allocation_failed()) {
aoqi@0 517 size_t missed_count = populate_table(&cit);
aoqi@0 518 if (missed_count != 0) {
aoqi@0 519 st->print_cr("WARNING: Ran out of C-heap; undercounted " SIZE_FORMAT
aoqi@0 520 " total instances in data below",
aoqi@0 521 missed_count);
aoqi@0 522 }
aoqi@0 523
aoqi@0 524 // Sort and print klass instance info
aoqi@0 525 const char *title = "\n"
aoqi@0 526 " num #instances #bytes class name\n"
aoqi@0 527 "----------------------------------------------";
aoqi@0 528 KlassInfoHisto histo(&cit, title);
aoqi@0 529 HistoClosure hc(&histo);
aoqi@0 530
aoqi@0 531 cit.iterate(&hc);
aoqi@0 532
aoqi@0 533 histo.sort();
aoqi@0 534 histo.print_histo_on(st, _print_class_stats, _csv_format, _columns);
aoqi@0 535 } else {
aoqi@0 536 st->print_cr("WARNING: Ran out of C-heap; histogram not generated");
aoqi@0 537 }
aoqi@0 538 st->flush();
aoqi@0 539 }
aoqi@0 540
aoqi@0 541 class FindInstanceClosure : public ObjectClosure {
aoqi@0 542 private:
aoqi@0 543 Klass* _klass;
aoqi@0 544 GrowableArray<oop>* _result;
aoqi@0 545
aoqi@0 546 public:
aoqi@0 547 FindInstanceClosure(Klass* k, GrowableArray<oop>* result) : _klass(k), _result(result) {};
aoqi@0 548
aoqi@0 549 void do_object(oop obj) {
aoqi@0 550 if (obj->is_a(_klass)) {
aoqi@0 551 _result->append(obj);
aoqi@0 552 }
aoqi@0 553 }
aoqi@0 554 };
aoqi@0 555
aoqi@0 556 void HeapInspection::find_instances_at_safepoint(Klass* k, GrowableArray<oop>* result) {
aoqi@0 557 assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped");
aoqi@0 558 assert(Heap_lock->is_locked(), "should have the Heap_lock");
aoqi@0 559
aoqi@0 560 // Ensure that the heap is parsable
aoqi@0 561 Universe::heap()->ensure_parsability(false); // no need to retire TALBs
aoqi@0 562
aoqi@0 563 // Iterate over objects in the heap
aoqi@0 564 FindInstanceClosure fic(k, result);
aoqi@0 565 // If this operation encounters a bad object when using CMS,
aoqi@0 566 // consider using safe_object_iterate() which avoids metadata
aoqi@0 567 // objects that may contain bad references.
aoqi@0 568 Universe::heap()->object_iterate(&fic);
aoqi@0 569 }

mercurial