src/share/vm/services/memRecorder.cpp
Index Unified diffs Context diffs Sdiffs Wdiffs Patch New Old Previous File Next File hotspot Sdiff src/share/vm/services

src/share/vm/services/memRecorder.cpp

Print this page


   1 /*
   2  * Copyright (c) 2012, 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  *


  52 
  53 volatile jint MemRecorder::_instance_count = 0;
  54 
  55 MemRecorder::MemRecorder() {
  56   assert(MemTracker::is_on(), "Native memory tracking is off");
  57   Atomic::inc(&_instance_count);
  58   set_generation();
  59 
  60   if (MemTracker::track_callsite()) {
  61     _pointer_records = new (std::nothrow)FixedSizeMemPointerArray<SeqMemPointerRecordEx,
  62         DEFAULT_RECORDER_PTR_ARRAY_SIZE>();
  63   } else {
  64     _pointer_records = new (std::nothrow)FixedSizeMemPointerArray<SeqMemPointerRecord,
  65         DEFAULT_RECORDER_PTR_ARRAY_SIZE>();
  66   }
  67   _next = NULL;
  68 
  69 
  70   if (_pointer_records != NULL) {
  71     // recode itself

  72     record((address)this, (MemPointerRecord::malloc_tag()|mtNMT|otNMTRecorder),
  73         sizeof(MemRecorder), CALLER_PC);
  74     record((address)_pointer_records, (MemPointerRecord::malloc_tag()|mtNMT|otNMTRecorder),
  75         _pointer_records->instance_size(),CURRENT_PC);
  76   }
  77 }
  78 
  79 MemRecorder::~MemRecorder() {
  80   if (_pointer_records != NULL) {
  81     if (MemTracker::is_on()) {
  82       MemTracker::record_free((address)_pointer_records, mtNMT);
  83       MemTracker::record_free((address)this, mtNMT);

  84     }
  85     delete _pointer_records;
  86   }
  87   // delete all linked recorders
  88   while (_next != NULL) {
  89     MemRecorder* tmp = _next;
  90     _next = _next->next();
  91     tmp->set_next(NULL);
  92     delete tmp;
  93   }
  94   Atomic::dec(&_instance_count);
  95 }
  96 
  97 // Sorting order:
  98 //   1. memory block address
  99 //   2. mem pointer record tags
 100 //   3. sequence number
 101 int MemRecorder::sort_record_fn(const void* e1, const void* e2) {
 102   const MemPointerRecord* p1 = (const MemPointerRecord*)e1;
 103   const MemPointerRecord* p2 = (const MemPointerRecord*)e2;
 104   int delta = UNSIGNED_COMPARE(p1->addr(), p2->addr());
 105   if (delta == 0) {
 106     int df = UNSIGNED_COMPARE((p1->flags() & MemPointerRecord::tag_masks),
 107                               (p2->flags() & MemPointerRecord::tag_masks));
 108     if (df == 0) {
 109       assert(p1->seq() != p2->seq(), "dup seq");
 110       return p1->seq() - p2->seq();
 111     } else {
 112       return df;
 113     }
 114   } else {
 115     return delta;
 116   }
 117 }
 118 
 119 bool MemRecorder::record(address p, MEMFLAGS flags, size_t size, address pc) {

 120 #ifdef ASSERT
 121   if (MemPointerRecord::is_virtual_memory_record(flags)) {
 122     assert((flags & MemPointerRecord::tag_masks) != 0, "bad virtual memory record");
 123   } else {
 124     assert((flags & MemPointerRecord::tag_masks) == MemPointerRecord::malloc_tag() ||
 125            (flags & MemPointerRecord::tag_masks) == MemPointerRecord::free_tag() ||
 126            IS_ARENA_OBJ(flags),
 127            "bad malloc record");
 128   }
 129   // a recorder should only hold records within the same generation
 130   unsigned long cur_generation = SequenceGenerator::current_generation();
 131   assert(cur_generation == _generation,
 132          "this thread did not enter sync point");
 133 #endif
 134 
 135   if (MemTracker::track_callsite()) {
 136     SeqMemPointerRecordEx ap(p, flags, size, pc);
 137     debug_only(check_dup_seq(ap.seq());)
 138     return _pointer_records->append(&ap);
 139   } else {
 140     SeqMemPointerRecord ap(p, flags, size);
 141     debug_only(check_dup_seq(ap.seq());)
 142     return _pointer_records->append(&ap);
 143   }
 144 }
 145 
 146   // iterator for alloc pointers
 147 SequencedRecordIterator MemRecorder::pointer_itr() {
 148   assert(_pointer_records != NULL, "just check");
 149   _pointer_records->sort((FN_SORT)sort_record_fn);
 150   return SequencedRecordIterator(_pointer_records);
 151 }
 152 
 153 
 154 void MemRecorder::set_generation() {
 155   _generation = SequenceGenerator::current_generation();
 156 }
 157 
 158 #ifdef ASSERT
 159 
 160 void MemRecorder::check_dup_seq(jint seq) const {
   1 /*
   2  * Copyright (c) 2012, 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  *


  52 
  53 volatile jint MemRecorder::_instance_count = 0;
  54 
  55 MemRecorder::MemRecorder() {
  56   assert(MemTracker::is_on(), "Native memory tracking is off");
  57   Atomic::inc(&_instance_count);
  58   set_generation();
  59 
  60   if (MemTracker::track_callsite()) {
  61     _pointer_records = new (std::nothrow)FixedSizeMemPointerArray<SeqMemPointerRecordEx,
  62         DEFAULT_RECORDER_PTR_ARRAY_SIZE>();
  63   } else {
  64     _pointer_records = new (std::nothrow)FixedSizeMemPointerArray<SeqMemPointerRecord,
  65         DEFAULT_RECORDER_PTR_ARRAY_SIZE>();
  66   }
  67   _next = NULL;
  68 
  69 
  70   if (_pointer_records != NULL) {
  71     // recode itself
  72     address pc = CURRENT_PC;
  73     record((address)this, (MemPointerRecord::malloc_tag()|mtNMT|otNMTRecorder),
  74         sizeof(MemRecorder), SequenceGenerator::next(), pc);
  75     record((address)_pointer_records, (MemPointerRecord::malloc_tag()|mtNMT|otNMTRecorder),
  76         _pointer_records->instance_size(), SequenceGenerator::next(), pc);
  77   }
  78 }
  79 
  80 MemRecorder::~MemRecorder() {
  81   if (_pointer_records != NULL) {
  82     if (MemTracker::is_on()) {
  83       NMTTrackOp op(NMTTrackOp::FreeOp);
  84       op.execute_op((address)_pointer_records, mtNMT);
  85       op.execute_op((address)this, mtNMT);
  86     }
  87     delete _pointer_records;
  88   }
  89   // delete all linked recorders
  90   while (_next != NULL) {
  91     MemRecorder* tmp = _next;
  92     _next = _next->next();
  93     tmp->set_next(NULL);
  94     delete tmp;
  95   }
  96   Atomic::dec(&_instance_count);
  97 }
  98 
  99 // Sorting order:
 100 //   1. memory block address
 101 //   2. mem pointer record tags
 102 //   3. sequence number
 103 int MemRecorder::sort_record_fn(const void* e1, const void* e2) {
 104   const MemPointerRecord* p1 = (const MemPointerRecord*)e1;
 105   const MemPointerRecord* p2 = (const MemPointerRecord*)e2;
 106   int delta = UNSIGNED_COMPARE(p1->addr(), p2->addr());
 107   if (delta == 0) {
 108     int df = UNSIGNED_COMPARE((p1->flags() & MemPointerRecord::tag_masks),
 109                               (p2->flags() & MemPointerRecord::tag_masks));
 110     if (df == 0) {
 111       assert(p1->seq() != p2->seq(), "dup seq");
 112       return p1->seq() - p2->seq();
 113     } else {
 114       return df;
 115     }
 116   } else {
 117     return delta;
 118   }
 119 }
 120 
 121 bool MemRecorder::record(address p, MEMFLAGS flags, size_t size, jint seq, address pc) {
 122   assert(seq > 0, "No sequence number");
 123 #ifdef ASSERT
 124   if (MemPointerRecord::is_virtual_memory_record(flags)) {
 125     assert((flags & MemPointerRecord::tag_masks) != 0, "bad virtual memory record");
 126   } else {
 127     assert((flags & MemPointerRecord::tag_masks) == MemPointerRecord::malloc_tag() ||
 128            (flags & MemPointerRecord::tag_masks) == MemPointerRecord::free_tag() ||
 129            IS_ARENA_OBJ(flags),
 130            "bad malloc record");
 131   }
 132   // a recorder should only hold records within the same generation
 133   unsigned long cur_generation = SequenceGenerator::current_generation();
 134   assert(cur_generation == _generation,
 135          "this thread did not enter sync point");
 136 #endif
 137 
 138   if (MemTracker::track_callsite()) {
 139     SeqMemPointerRecordEx ap(p, flags, size, seq, pc);
 140     debug_only(check_dup_seq(ap.seq());)
 141     return _pointer_records->append(&ap);
 142   } else {
 143     SeqMemPointerRecord ap(p, flags, size, seq);
 144     debug_only(check_dup_seq(ap.seq());)
 145     return _pointer_records->append(&ap);
 146   }
 147 }
 148 
 149   // iterator for alloc pointers
 150 SequencedRecordIterator MemRecorder::pointer_itr() {
 151   assert(_pointer_records != NULL, "just check");
 152   _pointer_records->sort((FN_SORT)sort_record_fn);
 153   return SequencedRecordIterator(_pointer_records);
 154 }
 155 
 156 
 157 void MemRecorder::set_generation() {
 158   _generation = SequenceGenerator::current_generation();
 159 }
 160 
 161 #ifdef ASSERT
 162 
 163 void MemRecorder::check_dup_seq(jint seq) const {
src/share/vm/services/memRecorder.cpp
Index Unified diffs Context diffs Sdiffs Wdiffs Patch New Old Previous File Next File