memSnapshot.cpp 26.4 KB
Newer Older
Z
zgu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/*
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
 * 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.
 *
 * 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.
 *
 */

#include "precompiled.hpp"
#include "runtime/mutexLocker.hpp"
#include "utilities/decoder.hpp"
#include "services/memBaseline.hpp"
#include "services/memPtr.hpp"
#include "services/memPtrArray.hpp"
#include "services/memSnapshot.hpp"
#include "services/memTracker.hpp"

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#ifdef ASSERT

void decode_pointer_record(MemPointerRecord* rec) {
  tty->print("Pointer: [" PTR_FORMAT " - " PTR_FORMAT  "] size = %d bytes", rec->addr(),
    rec->addr() + rec->size(), (int)rec->size());
  tty->print(" type = %s", MemBaseline::type2name(FLAGS_TO_MEMORY_TYPE(rec->flags())));
  if (rec->is_vm_pointer()) {
    if (rec->is_allocation_record()) {
      tty->print_cr(" (reserve)");
    } else if (rec->is_commit_record()) {
      tty->print_cr(" (commit)");
    } else if (rec->is_uncommit_record()) {
      tty->print_cr(" (uncommit)");
    } else if (rec->is_deallocation_record()) {
      tty->print_cr(" (release)");
    } else {
      tty->print_cr(" (tag)");
    }
  } else {
53
    if (rec->is_arena_memory_record()) {
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
      tty->print_cr(" (arena size)");
    } else if (rec->is_allocation_record()) {
      tty->print_cr(" (malloc)");
    } else {
      tty->print_cr(" (free)");
    }
  }
  if (MemTracker::track_callsite()) {
    char buf[1024];
    address pc = ((MemPointerRecordEx*)rec)->pc();
    if (pc != NULL && os::dll_address_to_function_name(pc, buf, sizeof(buf), NULL)) {
      tty->print_cr("\tfrom %s", buf);
    } else {
      tty->print_cr("\tcould not decode pc = " PTR_FORMAT "", pc);
    }
  }
}

void decode_vm_region_record(VMMemRegion* rec) {
  tty->print("VM Region [" PTR_FORMAT " - " PTR_FORMAT "]", rec->addr(),
    rec->addr() + rec->size());
  tty->print(" type = %s", MemBaseline::type2name(FLAGS_TO_MEMORY_TYPE(rec->flags())));
  if (rec->is_allocation_record()) {
    tty->print_cr(" (reserved)");
  } else if (rec->is_commit_record()) {
    tty->print_cr(" (committed)");
  } else {
    ShouldNotReachHere();
  }
  if (MemTracker::track_callsite()) {
    char buf[1024];
    address pc = ((VMMemRegionEx*)rec)->pc();
    if (pc != NULL && os::dll_address_to_function_name(pc, buf, sizeof(buf), NULL)) {
      tty->print_cr("\tfrom %s", buf);
    } else {
      tty->print_cr("\tcould not decode pc = " PTR_FORMAT "", pc);
    }

  }
}

#endif

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

bool VMMemPointerIterator::insert_record(MemPointerRecord* rec) {
  VMMemRegionEx new_rec;
  assert(rec->is_allocation_record() || rec->is_commit_record(),
    "Sanity check");
  if (MemTracker::track_callsite()) {
    new_rec.init((MemPointerRecordEx*)rec);
  } else {
    new_rec.init(rec);
  }
  return insert(&new_rec);
}

bool VMMemPointerIterator::insert_record_after(MemPointerRecord* rec) {
  VMMemRegionEx new_rec;
  assert(rec->is_allocation_record() || rec->is_commit_record(),
    "Sanity check");
  if (MemTracker::track_callsite()) {
    new_rec.init((MemPointerRecordEx*)rec);
  } else {
    new_rec.init(rec);
  }
  return insert_after(&new_rec);
}

// we don't consolidate reserved regions, since they may be categorized
// in different types.
bool VMMemPointerIterator::add_reserved_region(MemPointerRecord* rec) {
  assert(rec->is_allocation_record(), "Sanity check");
126
  VMMemRegion* reserved_region = (VMMemRegion*)current();
127 128

  // we don't have anything yet
129
  if (reserved_region == NULL) {
130 131 132
    return insert_record(rec);
  }

133
  assert(reserved_region->is_reserved_region(), "Sanity check");
134
  // duplicated records
135
  if (reserved_region->is_same_region(rec)) {
136 137
    return true;
  }
138 139 140 141 142 143 144 145 146 147 148 149 150
  // Overlapping stack regions indicate that a JNI thread failed to
  // detach from the VM before exiting. This leaks the JavaThread object.
  if (CheckJNICalls)  {
      guarantee(FLAGS_TO_MEMORY_TYPE(reserved_region->flags()) != mtThreadStack ||
         !reserved_region->overlaps_region(rec),
         "Attached JNI thread exited without being detached");
  }
  // otherwise, we should not have overlapping reserved regions
  assert(FLAGS_TO_MEMORY_TYPE(reserved_region->flags()) == mtThreadStack ||
    reserved_region->base() > rec->addr(), "Just check: locate()");
  assert(FLAGS_TO_MEMORY_TYPE(reserved_region->flags()) == mtThreadStack ||
    !reserved_region->overlaps_region(rec), "overlapping reserved regions");

151 152 153 154 155 156
  return insert_record(rec);
}

// we do consolidate committed regions
bool VMMemPointerIterator::add_committed_region(MemPointerRecord* rec) {
  assert(rec->is_commit_record(), "Sanity check");
157 158
  VMMemRegion* reserved_rgn = (VMMemRegion*)current();
  assert(reserved_rgn->is_reserved_region() && reserved_rgn->contains_region(rec),
159 160 161 162
    "Sanity check");

  // thread's native stack is always marked as "committed", ignore
  // the "commit" operation for creating stack guard pages
163
  if (FLAGS_TO_MEMORY_TYPE(reserved_rgn->flags()) == mtThreadStack &&
164 165 166 167
      FLAGS_TO_MEMORY_TYPE(rec->flags()) != mtThreadStack) {
    return true;
  }

168 169 170
  // if the reserved region has any committed regions
  VMMemRegion* committed_rgn  = (VMMemRegion*)next();
  while (committed_rgn != NULL && committed_rgn->is_committed_region()) {
171
    // duplicated commit records
172
    if(committed_rgn->contains_region(rec)) {
173
      return true;
174 175 176 177 178
    } else if (committed_rgn->overlaps_region(rec)) {
      // overlaps front part
      if (rec->addr() < committed_rgn->addr()) {
        committed_rgn->expand_region(rec->addr(),
          committed_rgn->addr() - rec->addr());
179
      } else {
180 181 182 183 184 185 186
        // overlaps tail part
        address committed_rgn_end = committed_rgn->addr() +
              committed_rgn->size();
        assert(committed_rgn_end < rec->addr() + rec->size(),
             "overlap tail part");
        committed_rgn->expand_region(committed_rgn_end,
          (rec->addr() + rec->size()) - committed_rgn_end);
187
      }
188 189 190
    } else if (committed_rgn->base() + committed_rgn->size() == rec->addr()) {
      // adjunct each other
      committed_rgn->expand_region(rec->addr(), rec->size());
191 192 193
      VMMemRegion* next_reg = (VMMemRegion*)next();
      // see if we can consolidate next committed region
      if (next_reg != NULL && next_reg->is_committed_region() &&
194 195 196
        next_reg->base() == committed_rgn->base() + committed_rgn->size()) {
          committed_rgn->expand_region(next_reg->base(), next_reg->size());
          // delete merged region
197 198 199
          remove();
      }
      return true;
200 201 202
    } else if (committed_rgn->base() > rec->addr()) {
      // found the location, insert this committed region
      return insert_record(rec);
203
    }
204
    committed_rgn = (VMMemRegion*)next();
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
  }
  return insert_record(rec);
}

bool VMMemPointerIterator::remove_uncommitted_region(MemPointerRecord* rec) {
  assert(rec->is_uncommit_record(), "sanity check");
  VMMemRegion* cur;
  cur = (VMMemRegion*)current();
  assert(cur->is_reserved_region() && cur->contains_region(rec),
    "Sanity check");
  // thread's native stack is always marked as "committed", ignore
  // the "commit" operation for creating stack guard pages
  if (FLAGS_TO_MEMORY_TYPE(cur->flags()) == mtThreadStack &&
      FLAGS_TO_MEMORY_TYPE(rec->flags()) != mtThreadStack) {
    return true;
  }

  cur = (VMMemRegion*)next();
  while (cur != NULL && cur->is_committed_region()) {
    // region already uncommitted, must be due to duplicated record
    if (cur->addr() >= rec->addr() + rec->size()) {
      break;
    } else if (cur->contains_region(rec)) {
      // uncommit whole region
      if (cur->is_same_region(rec)) {
        remove();
        break;
      } else if (rec->addr() == cur->addr() ||
        rec->addr() + rec->size() == cur->addr() + cur->size()) {
        // uncommitted from either end of current memory region.
        cur->exclude_region(rec->addr(), rec->size());
        break;
      } else { // split the committed region and release the middle
        address high_addr = cur->addr() + cur->size();
        size_t sz = high_addr - rec->addr();
        cur->exclude_region(rec->addr(), sz);
        sz = high_addr - (rec->addr() + rec->size());
        if (MemTracker::track_callsite()) {
          MemPointerRecordEx tmp(rec->addr() + rec->size(), cur->flags(), sz,
             ((VMMemRegionEx*)cur)->pc());
          return insert_record_after(&tmp);
        } else {
          MemPointerRecord tmp(rec->addr() + rec->size(), cur->flags(), sz);
          return insert_record_after(&tmp);
        }
      }
    }
    cur = (VMMemRegion*)next();
  }

  // we may not find committed record due to duplicated records
  return true;
}

bool VMMemPointerIterator::remove_released_region(MemPointerRecord* rec) {
  assert(rec->is_deallocation_record(), "Sanity check");
  VMMemRegion* cur = (VMMemRegion*)current();
  assert(cur->is_reserved_region() && cur->contains_region(rec),
    "Sanity check");
Z
zgu 已提交
264 265
  if (rec->is_same_region(cur)) {
    // release whole reserved region
266
#ifdef ASSERT
Z
zgu 已提交
267 268 269
    VMMemRegion* next_region = (VMMemRegion*)peek_next();
    // should not have any committed memory in this reserved region
    assert(next_region == NULL || !next_region->is_committed_region(), "Sanity check");
270 271 272 273 274 275
#endif
    remove();
  } else if (rec->addr() == cur->addr() ||
    rec->addr() + rec->size() == cur->addr() + cur->size()) {
    // released region is at either end of this region
    cur->exclude_region(rec->addr(), rec->size());
Z
zgu 已提交
276
    assert(check_reserved_region(), "Integrity check");
277 278 279 280 281 282 283 284
  } else { // split the reserved region and release the middle
    address high_addr = cur->addr() + cur->size();
    size_t sz = high_addr - rec->addr();
    cur->exclude_region(rec->addr(), sz);
    sz = high_addr - rec->addr() - rec->size();
    if (MemTracker::track_callsite()) {
      MemPointerRecordEx tmp(rec->addr() + rec->size(), cur->flags(), sz,
        ((VMMemRegionEx*)cur)->pc());
Z
zgu 已提交
285 286 287
      bool ret = insert_reserved_region(&tmp);
      assert(!ret || check_reserved_region(), "Integrity check");
      return ret;
288 289
    } else {
      MemPointerRecord tmp(rec->addr() + rec->size(), cur->flags(), sz);
Z
zgu 已提交
290 291 292
      bool ret = insert_reserved_region(&tmp);
      assert(!ret || check_reserved_region(), "Integrity check");
      return ret;
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
    }
  }
  return true;
}

bool VMMemPointerIterator::insert_reserved_region(MemPointerRecord* rec) {
  // skip all 'commit' records associated with previous reserved region
  VMMemRegion* p = (VMMemRegion*)next();
  while (p != NULL && p->is_committed_region() &&
         p->base() + p->size() < rec->addr()) {
    p = (VMMemRegion*)next();
  }
  return insert_record(rec);
}

bool VMMemPointerIterator::split_reserved_region(VMMemRegion* rgn, address new_rgn_addr, size_t new_rgn_size) {
  assert(rgn->contains_region(new_rgn_addr, new_rgn_size), "Not fully contained");
  address pc = (MemTracker::track_callsite() ? ((VMMemRegionEx*)rgn)->pc() : NULL);
  if (rgn->base() == new_rgn_addr) { // new region is at the beginning of the region
    size_t sz = rgn->size() - new_rgn_size;
    // the original region becomes 'new' region
    rgn->exclude_region(new_rgn_addr + new_rgn_size, sz);
     // remaining becomes next region
    MemPointerRecordEx next_rgn(new_rgn_addr + new_rgn_size, rgn->flags(), sz, pc);
    return insert_reserved_region(&next_rgn);
  } else if (rgn->base() + rgn->size() == new_rgn_addr + new_rgn_size) {
    rgn->exclude_region(new_rgn_addr, new_rgn_size);
    MemPointerRecordEx next_rgn(new_rgn_addr, rgn->flags(), new_rgn_size, pc);
    return insert_reserved_region(&next_rgn);
  } else {
    // the orginal region will be split into three
    address rgn_high_addr = rgn->base() + rgn->size();
    // first region
    rgn->exclude_region(new_rgn_addr, (rgn_high_addr - new_rgn_addr));
    // the second region is the new region
    MemPointerRecordEx new_rgn(new_rgn_addr, rgn->flags(), new_rgn_size, pc);
    if (!insert_reserved_region(&new_rgn)) return false;
    // the remaining region
    MemPointerRecordEx rem_rgn(new_rgn_addr + new_rgn_size, rgn->flags(),
      rgn_high_addr - (new_rgn_addr + new_rgn_size), pc);
    return insert_reserved_region(&rem_rgn);
  }
}

337 338 339 340 341 342
static int sort_in_seq_order(const void* p1, const void* p2) {
  assert(p1 != NULL && p2 != NULL, "Sanity check");
  const MemPointerRecord* mp1 = (MemPointerRecord*)p1;
  const MemPointerRecord* mp2 = (MemPointerRecord*)p2;
  return (mp1->seq() - mp2->seq());
}
Z
zgu 已提交
343

344 345 346 347
bool StagingArea::init() {
  if (MemTracker::track_callsite()) {
    _malloc_data = new (std::nothrow)MemPointerArrayImpl<SeqMemPointerRecordEx>();
    _vm_data = new (std::nothrow)MemPointerArrayImpl<SeqMemPointerRecordEx>();
Z
zgu 已提交
348
  } else {
349 350
    _malloc_data = new (std::nothrow)MemPointerArrayImpl<SeqMemPointerRecord>();
    _vm_data = new (std::nothrow)MemPointerArrayImpl<SeqMemPointerRecord>();
Z
zgu 已提交
351 352
  }

353 354 355
  if (_malloc_data != NULL && _vm_data != NULL &&
      !_malloc_data->out_of_memory() &&
      !_vm_data->out_of_memory()) {
Z
zgu 已提交
356
    return true;
357 358 359 360 361 362
  } else {
    if (_malloc_data != NULL) delete _malloc_data;
    if (_vm_data != NULL) delete _vm_data;
    _malloc_data = NULL;
    _vm_data = NULL;
    return false;
Z
zgu 已提交
363 364 365 366
  }
}


367
VMRecordIterator StagingArea::virtual_memory_record_walker() {
368 369 370
  MemPointerArray* arr = vm_data();
  // sort into seq number order
  arr->sort((FN_SORT)sort_in_seq_order);
371
  return VMRecordIterator(arr);
Z
zgu 已提交
372 373
}

374

Z
zgu 已提交
375 376 377 378 379 380 381 382 383
MemSnapshot::MemSnapshot() {
  if (MemTracker::track_callsite()) {
    _alloc_ptrs = new (std::nothrow) MemPointerArrayImpl<MemPointerRecordEx>();
    _vm_ptrs = new (std::nothrow)MemPointerArrayImpl<VMMemRegionEx>(64, true);
  } else {
    _alloc_ptrs = new (std::nothrow) MemPointerArrayImpl<MemPointerRecord>();
    _vm_ptrs = new (std::nothrow)MemPointerArrayImpl<VMMemRegion>(64, true);
  }

384
  _staging_area.init();
385
  _lock = new (std::nothrow) Mutex(Monitor::max_nonleaf - 1, "memSnapshotLock");
Z
zgu 已提交
386
  NOT_PRODUCT(_untracked_count = 0;)
387
  _number_of_classes = 0;
Z
zgu 已提交
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
}

MemSnapshot::~MemSnapshot() {
  assert(MemTracker::shutdown_in_progress(), "native memory tracking still on");
  {
    MutexLockerEx locker(_lock);
    if (_alloc_ptrs != NULL) {
      delete _alloc_ptrs;
      _alloc_ptrs = NULL;
    }

    if (_vm_ptrs != NULL) {
      delete _vm_ptrs;
      _vm_ptrs = NULL;
    }
  }

  if (_lock != NULL) {
    delete _lock;
    _lock = NULL;
  }
}

411 412

void MemSnapshot::copy_seq_pointer(MemPointerRecord* dest, const MemPointerRecord* src) {
Z
zgu 已提交
413 414
  assert(dest != NULL && src != NULL, "Just check");
  assert(dest->addr() == src->addr(), "Just check");
415 416 417 418 419 420 421 422
  assert(dest->seq() > 0 && src->seq() > 0, "not sequenced");

  if (MemTracker::track_callsite()) {
    *(SeqMemPointerRecordEx*)dest = *(SeqMemPointerRecordEx*)src;
  } else {
    *(SeqMemPointerRecord*)dest = *(SeqMemPointerRecord*)src;
  }
}
Z
zgu 已提交
423

424 425 426
void MemSnapshot::assign_pointer(MemPointerRecord*dest, const MemPointerRecord* src) {
  assert(src != NULL && dest != NULL, "Just check");
  assert(dest->seq() == 0 && src->seq() >0, "cast away sequence");
Z
zgu 已提交
427 428 429 430

  if (MemTracker::track_callsite()) {
    *(MemPointerRecordEx*)dest = *(MemPointerRecordEx*)src;
  } else {
431
    *(MemPointerRecord*)dest = *(MemPointerRecord*)src;
Z
zgu 已提交
432 433 434
  }
}

435
// merge a recorder to the staging area
Z
zgu 已提交
436 437 438 439 440 441
bool MemSnapshot::merge(MemRecorder* rec) {
  assert(rec != NULL && !rec->out_of_memory(), "Just check");

  SequencedRecordIterator itr(rec->pointer_itr());

  MutexLockerEx lock(_lock, true);
442
  MemPointerIterator malloc_staging_itr(_staging_area.malloc_data());
443 444 445 446 447
  MemPointerRecord* incoming_rec = (MemPointerRecord*) itr.current();
  MemPointerRecord* matched_rec;

  while (incoming_rec != NULL) {
    if (incoming_rec->is_vm_pointer()) {
448
      // we don't do anything with virtual memory records during merge
449
      if (!_staging_area.vm_data()->append(incoming_rec)) {
Z
zgu 已提交
450 451
        return false;
      }
452
    } else {
453 454
      // locate matched record and/or also position the iterator to proper
      // location for this incoming record.
455 456 457 458 459
      matched_rec = (MemPointerRecord*)malloc_staging_itr.locate(incoming_rec->addr());
      // we have not seen this memory block in this generation,
      // so just add to staging area
      if (matched_rec == NULL) {
        if (!malloc_staging_itr.insert(incoming_rec)) {
460
          return false;
Z
zgu 已提交
461
        }
462 463 464 465
      } else if (incoming_rec->addr() == matched_rec->addr()) {
        // whoever has higher sequence number wins
        if (incoming_rec->seq() > matched_rec->seq()) {
          copy_seq_pointer(matched_rec, incoming_rec);
466
        }
467 468
      } else if (incoming_rec->addr() < matched_rec->addr()) {
        if (!malloc_staging_itr.insert(incoming_rec)) {
Z
zgu 已提交
469 470 471
          return false;
        }
      } else {
472
        ShouldNotReachHere();
Z
zgu 已提交
473 474
      }
    }
475
    incoming_rec = (MemPointerRecord*)itr.next();
Z
zgu 已提交
476 477 478 479 480 481 482
  }
  NOT_PRODUCT(void check_staging_data();)
  return true;
}


// promote data to next generation
483
bool MemSnapshot::promote(int number_of_classes) {
484 485 486
  assert(_alloc_ptrs != NULL && _vm_ptrs != NULL, "Just check");
  assert(_staging_area.malloc_data() != NULL && _staging_area.vm_data() != NULL,
         "Just check");
Z
zgu 已提交
487
  MutexLockerEx lock(_lock, true);
488 489 490 491

  MallocRecordIterator  malloc_itr = _staging_area.malloc_record_walker();
  bool promoted = false;
  if (promote_malloc_records(&malloc_itr)) {
492
    VMRecordIterator vm_itr = _staging_area.virtual_memory_record_walker();
493 494 495 496 497 498 499
    if (promote_virtual_memory_records(&vm_itr)) {
      promoted = true;
    }
  }

  NOT_PRODUCT(check_malloc_pointers();)
  _staging_area.clear();
500
  _number_of_classes = number_of_classes;
501 502 503 504 505 506 507 508 509 510 511
  return promoted;
}

bool MemSnapshot::promote_malloc_records(MemPointerArrayIterator* itr) {
  MemPointerIterator malloc_snapshot_itr(_alloc_ptrs);
  MemPointerRecord* new_rec = (MemPointerRecord*)itr->current();
  MemPointerRecord* matched_rec;
  while (new_rec != NULL) {
    matched_rec = (MemPointerRecord*)malloc_snapshot_itr.locate(new_rec->addr());
    // found matched memory block
    if (matched_rec != NULL && new_rec->addr() == matched_rec->addr()) {
512
      // snapshot already contains 'live' records
513
      assert(matched_rec->is_allocation_record() || matched_rec->is_arena_memory_record(),
514 515
             "Sanity check");
      // update block states
516 517 518 519 520 521 522 523 524
      if (new_rec->is_allocation_record()) {
        assign_pointer(matched_rec, new_rec);
      } else if (new_rec->is_arena_memory_record()) {
        if (new_rec->size() == 0) {
          // remove size record once size drops to 0
          malloc_snapshot_itr.remove();
        } else {
          assign_pointer(matched_rec, new_rec);
        }
Z
zgu 已提交
525
      } else {
526 527 528 529 530
        // a deallocation record
        assert(new_rec->is_deallocation_record(), "Sanity check");
        // an arena record can be followed by a size record, we need to remove both
        if (matched_rec->is_arena_record()) {
          MemPointerRecord* next = (MemPointerRecord*)malloc_snapshot_itr.peek_next();
531
          if (next->is_arena_memory_record() && next->is_memory_record_of_arena(matched_rec)) {
532
            malloc_snapshot_itr.remove();
Z
zgu 已提交
533 534
          }
        }
535 536
        // the memory is deallocated, remove related record(s)
        malloc_snapshot_itr.remove();
Z
zgu 已提交
537 538
      }
    } else {
539 540 541
      // don't insert size 0 record
      if (new_rec->is_arena_memory_record() && new_rec->size() == 0) {
        new_rec = NULL;
542
      }
543

544
      if (new_rec != NULL) {
545
        if  (new_rec->is_allocation_record() || new_rec->is_arena_memory_record()) {
546 547 548 549 550 551 552
          if (matched_rec != NULL && new_rec->addr() > matched_rec->addr()) {
            if (!malloc_snapshot_itr.insert_after(new_rec)) {
              return false;
            }
          } else {
            if (!malloc_snapshot_itr.insert(new_rec)) {
              return false;
Z
zgu 已提交
553 554 555
            }
          }
        }
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
#ifndef PRODUCT
        else if (!has_allocation_record(new_rec->addr())) {
          // NMT can not track some startup memory, which is allocated before NMT is on
          _untracked_count ++;
        }
#endif
      }
    }
    new_rec = (MemPointerRecord*)itr->next();
  }
  return true;
}

bool MemSnapshot::promote_virtual_memory_records(MemPointerArrayIterator* itr) {
  VMMemPointerIterator vm_snapshot_itr(_vm_ptrs);
  MemPointerRecord* new_rec = (MemPointerRecord*)itr->current();
572
  VMMemRegion*  reserved_rec;
573 574
  while (new_rec != NULL) {
    assert(new_rec->is_vm_pointer(), "Sanity check");
575 576 577 578 579 580

    // locate a reserved region that contains the specified address, or
    // the nearest reserved region has base address just above the specified
    // address
    reserved_rec = (VMMemRegion*)vm_snapshot_itr.locate(new_rec->addr());
    if (reserved_rec != NULL && reserved_rec->contains_region(new_rec)) {
581
      // snapshot can only have 'live' records
582 583 584 585 586 587 588
      assert(reserved_rec->is_reserved_region(), "Sanity check");
      if (new_rec->is_allocation_record()) {
        if (!reserved_rec->is_same_region(new_rec)) {
          // only deal with split a bigger reserved region into smaller regions.
          // So far, CDS is the only use case.
          if (!vm_snapshot_itr.split_reserved_region(reserved_rec, new_rec->addr(), new_rec->size())) {
            return false;
Z
zgu 已提交
589 590
          }
        }
591 592 593
      } else if (new_rec->is_uncommit_record()) {
        if (!vm_snapshot_itr.remove_uncommitted_region(new_rec)) {
          return false;
594
        }
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612
      } else if (new_rec->is_commit_record()) {
        // insert or expand existing committed region to cover this
        // newly committed region
        if (!vm_snapshot_itr.add_committed_region(new_rec)) {
          return false;
        }
      } else if (new_rec->is_deallocation_record()) {
        // release part or all memory region
        if (!vm_snapshot_itr.remove_released_region(new_rec)) {
          return false;
        }
      } else if (new_rec->is_type_tagging_record()) {
        // tag this reserved virtual memory range to a memory type. Can not re-tag a memory range
        // to different type.
        assert(FLAGS_TO_MEMORY_TYPE(reserved_rec->flags()) == mtNone ||
               FLAGS_TO_MEMORY_TYPE(reserved_rec->flags()) == FLAGS_TO_MEMORY_TYPE(new_rec->flags()),
               "Sanity check");
        reserved_rec->tag(new_rec->flags());
613
    } else {
614
        ShouldNotReachHere();
Z
zgu 已提交
615
          }
616
        } else {
617 618 619 620 621 622 623
      /*
       * The assertion failure indicates mis-matched virtual memory records. The likely
       * scenario is, that some virtual memory operations are not going through os::xxxx_memory()
       * api, which have to be tracked manually. (perfMemory is an example).
      */
      assert(new_rec->is_allocation_record(), "Sanity check");
      if (!vm_snapshot_itr.add_reserved_region(new_rec)) {
624
            return false;
Z
zgu 已提交
625 626
          }
  }
627 628 629
    new_rec = (MemPointerRecord*)itr->next();
  }
  return true;
Z
zgu 已提交
630 631
}

632
#ifndef PRODUCT
Z
zgu 已提交
633 634 635 636 637 638 639 640
void MemSnapshot::print_snapshot_stats(outputStream* st) {
  st->print_cr("Snapshot:");
  st->print_cr("\tMalloced: %d/%d [%5.2f%%]  %dKB", _alloc_ptrs->length(), _alloc_ptrs->capacity(),
    (100.0 * (float)_alloc_ptrs->length()) / (float)_alloc_ptrs->capacity(), _alloc_ptrs->instance_size()/K);

  st->print_cr("\tVM: %d/%d [%5.2f%%] %dKB", _vm_ptrs->length(), _vm_ptrs->capacity(),
    (100.0 * (float)_vm_ptrs->length()) / (float)_vm_ptrs->capacity(), _vm_ptrs->instance_size()/K);

641 642 643 644 645 646 647 648 649
  st->print_cr("\tMalloc staging Area:     %d/%d [%5.2f%%] %dKB", _staging_area.malloc_data()->length(),
    _staging_area.malloc_data()->capacity(),
    (100.0 * (float)_staging_area.malloc_data()->length()) / (float)_staging_area.malloc_data()->capacity(),
    _staging_area.malloc_data()->instance_size()/K);

  st->print_cr("\tVirtual memory staging Area:     %d/%d [%5.2f%%] %dKB", _staging_area.vm_data()->length(),
    _staging_area.vm_data()->capacity(),
    (100.0 * (float)_staging_area.vm_data()->length()) / (float)_staging_area.vm_data()->capacity(),
    _staging_area.vm_data()->instance_size()/K);
Z
zgu 已提交
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666

  st->print_cr("\tUntracked allocation: %d", _untracked_count);
}

void MemSnapshot::check_malloc_pointers() {
  MemPointerArrayIteratorImpl mItr(_alloc_ptrs);
  MemPointerRecord* p = (MemPointerRecord*)mItr.current();
  MemPointerRecord* prev = NULL;
  while (p != NULL) {
    if (prev != NULL) {
      assert(p->addr() >= prev->addr(), "sorting order");
    }
    prev = p;
    p = (MemPointerRecord*)mItr.next();
  }
}

667
bool MemSnapshot::has_allocation_record(address addr) {
668
  MemPointerArrayIteratorImpl itr(_staging_area.malloc_data());
669 670 671 672 673 674 675 676 677 678 679 680
  MemPointerRecord* cur = (MemPointerRecord*)itr.current();
  while (cur != NULL) {
    if (cur->addr() == addr && cur->is_allocation_record()) {
      return true;
    }
    cur = (MemPointerRecord*)itr.next();
  }
  return false;
}
#endif // PRODUCT

#ifdef ASSERT
Z
zgu 已提交
681
void MemSnapshot::check_staging_data() {
682
  MemPointerArrayIteratorImpl itr(_staging_area.malloc_data());
Z
zgu 已提交
683 684 685 686 687 688 689 690 691 692
  MemPointerRecord* cur = (MemPointerRecord*)itr.current();
  MemPointerRecord* next = (MemPointerRecord*)itr.next();
  while (next != NULL) {
    assert((next->addr() > cur->addr()) ||
      ((next->flags() & MemPointerRecord::tag_masks) >
       (cur->flags() & MemPointerRecord::tag_masks)),
       "sorting order");
    cur = next;
    next = (MemPointerRecord*)itr.next();
  }
693 694 695 696 697 698 699

  MemPointerArrayIteratorImpl vm_itr(_staging_area.vm_data());
  cur = (MemPointerRecord*)vm_itr.current();
  while (cur != NULL) {
    assert(cur->is_vm_pointer(), "virtual memory pointer only");
    cur = (MemPointerRecord*)vm_itr.next();
  }
Z
zgu 已提交
700
}
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728

void MemSnapshot::dump_all_vm_pointers() {
  MemPointerArrayIteratorImpl itr(_vm_ptrs);
  VMMemRegion* ptr = (VMMemRegion*)itr.current();
  tty->print_cr("dump virtual memory pointers:");
  while (ptr != NULL) {
    if (ptr->is_committed_region()) {
      tty->print("\t");
    }
    tty->print("[" PTR_FORMAT " - " PTR_FORMAT "] [%x]", ptr->addr(),
      (ptr->addr() + ptr->size()), ptr->flags());

    if (MemTracker::track_callsite()) {
      VMMemRegionEx* ex = (VMMemRegionEx*)ptr;
      if (ex->pc() != NULL) {
        char buf[1024];
        if (os::dll_address_to_function_name(ex->pc(), buf, sizeof(buf), NULL)) {
          tty->print_cr("\t%s", buf);
        } else {
          tty->print_cr("");
        }
      }
    }

    ptr = (VMMemRegion*)itr.next();
  }
  tty->flush();
}
729
#endif // ASSERT
Z
zgu 已提交
730