memRecorder.cpp 5.3 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/atomic.hpp"
#include "services/memBaseline.hpp"
#include "services/memRecorder.hpp"
#include "services/memPtr.hpp"
#include "services/memTracker.hpp"

MemPointer* SequencedRecordIterator::next_record() {
34 35 36 37 38 39
  MemPointerRecord* itr_cur = (MemPointerRecord*)_itr.current();
  if (itr_cur == NULL)  {
    return itr_cur;
  }

  MemPointerRecord* itr_next = (MemPointerRecord*)_itr.next();
Z
zgu 已提交
40

41 42 43 44
  // don't collapse virtual memory records
  while (itr_next != NULL && !itr_cur->is_vm_pointer() &&
    !itr_next->is_vm_pointer() &&
    same_kind(itr_cur, itr_next)) {
Z
zgu 已提交
45
    itr_cur = itr_next;
46
    itr_next = (MemPointerRecord*)_itr.next();
Z
zgu 已提交
47 48 49 50 51 52
  }

  return itr_cur;
}


53
volatile jint MemRecorder::_instance_count = 0;
Z
zgu 已提交
54 55 56

MemRecorder::MemRecorder() {
  assert(MemTracker::is_on(), "Native memory tracking is off");
57
  Atomic::inc(&_instance_count);
58
  set_generation();
Z
zgu 已提交
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

  if (MemTracker::track_callsite()) {
    _pointer_records = new (std::nothrow)FixedSizeMemPointerArray<SeqMemPointerRecordEx,
        DEFAULT_RECORDER_PTR_ARRAY_SIZE>();
  } else {
    _pointer_records = new (std::nothrow)FixedSizeMemPointerArray<SeqMemPointerRecord,
        DEFAULT_RECORDER_PTR_ARRAY_SIZE>();
  }
  _next = NULL;


  if (_pointer_records != NULL) {
    // recode itself
    record((address)this, (MemPointerRecord::malloc_tag()|mtNMT|otNMTRecorder),
        sizeof(MemRecorder), CALLER_PC);
    record((address)_pointer_records, (MemPointerRecord::malloc_tag()|mtNMT|otNMTRecorder),
        _pointer_records->instance_size(),CURRENT_PC);
  }
}

MemRecorder::~MemRecorder() {
  if (_pointer_records != NULL) {
    if (MemTracker::is_on()) {
      MemTracker::record_free((address)_pointer_records, mtNMT);
      MemTracker::record_free((address)this, mtNMT);
    }
    delete _pointer_records;
  }
87 88 89 90 91 92
  // delete all linked recorders
  while (_next != NULL) {
    MemRecorder* tmp = _next;
    _next = _next->next();
    tmp->set_next(NULL);
    delete tmp;
Z
zgu 已提交
93 94 95 96 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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
  }
  Atomic::dec(&_instance_count);
}

// Sorting order:
//   1. memory block address
//   2. mem pointer record tags
//   3. sequence number
int MemRecorder::sort_record_fn(const void* e1, const void* e2) {
  const MemPointerRecord* p1 = (const MemPointerRecord*)e1;
  const MemPointerRecord* p2 = (const MemPointerRecord*)e2;
  int delta = UNSIGNED_COMPARE(p1->addr(), p2->addr());
  if (delta == 0) {
    int df = UNSIGNED_COMPARE((p1->flags() & MemPointerRecord::tag_masks),
                              (p2->flags() & MemPointerRecord::tag_masks));
    if (df == 0) {
      assert(p1->seq() != p2->seq(), "dup seq");
      return p1->seq() - p2->seq();
    } else {
      return df;
    }
  } else {
    return delta;
  }
}

bool MemRecorder::record(address p, MEMFLAGS flags, size_t size, address pc) {
#ifdef ASSERT
  if (MemPointerRecord::is_virtual_memory_record(flags)) {
    assert((flags & MemPointerRecord::tag_masks) != 0, "bad virtual memory record");
  } else {
    assert((flags & MemPointerRecord::tag_masks) == MemPointerRecord::malloc_tag() ||
           (flags & MemPointerRecord::tag_masks) == MemPointerRecord::free_tag() ||
           IS_ARENA_OBJ(flags),
           "bad malloc record");
  }
  // a recorder should only hold records within the same generation
  unsigned long cur_generation = SequenceGenerator::current_generation();
  assert(cur_generation == _generation,
         "this thread did not enter sync point");
#endif

  if (MemTracker::track_callsite()) {
    SeqMemPointerRecordEx ap(p, flags, size, pc);
    debug_only(check_dup_seq(ap.seq());)
    return _pointer_records->append(&ap);
  } else {
    SeqMemPointerRecord ap(p, flags, size);
    debug_only(check_dup_seq(ap.seq());)
    return _pointer_records->append(&ap);
  }
}

  // iterator for alloc pointers
SequencedRecordIterator MemRecorder::pointer_itr() {
  assert(_pointer_records != NULL, "just check");
  _pointer_records->sort((FN_SORT)sort_record_fn);
  return SequencedRecordIterator(_pointer_records);
}


void MemRecorder::set_generation() {
  _generation = SequenceGenerator::current_generation();
}

158 159
#ifdef ASSERT

Z
zgu 已提交
160 161 162 163 164 165 166 167 168 169
void MemRecorder::check_dup_seq(jint seq) const {
  MemPointerArrayIteratorImpl itr(_pointer_records);
  MemPointerRecord* rc = (MemPointerRecord*)itr.current();
  while (rc != NULL) {
    assert(rc->seq() != seq, "dup seq");
    rc = (MemPointerRecord*)itr.next();
  }
}

#endif