1 /*
   2  * Copyright (c) 1998, 2010, 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  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "code/oopRecorder.hpp"
  27 #include "memory/allocation.inline.hpp"
  28 #include "oops/oop.inline.hpp"
  29 
  30 #ifdef ASSERT
  31 int OopRecorder::_find_index_calls = 0;
  32 int OopRecorder::_hit_indexes      = 0;
  33 int OopRecorder::_missed_indexes   = 0;
  34 #endif //ASSERT
  35 
  36 
  37 OopRecorder::OopRecorder(Arena* arena) {
  38   _handles  = NULL;
  39   _indexes  = NULL;
  40   _arena    = arena;
  41   _complete = false;
  42 }
  43 
  44 OopRecorder::IndexCache::IndexCache() {
  45   assert(first_index > 0, "initial zero state of cache must be invalid index");
  46   Copy::zero_to_bytes(&_cache[0], sizeof(_cache));
  47 }
  48 
  49 int OopRecorder::oop_size() {
  50   _complete = true;
  51   if (_handles == NULL)  return 0;
  52   return _handles->length() * sizeof(oop);
  53 }
  54 
  55 void OopRecorder::copy_to(nmethod* nm) {
  56   assert(_complete, "must be frozen");
  57   maybe_initialize();  // get non-null handles, even if we have no oops
  58   nm->copy_oops(_handles);
  59 }
  60 
  61 void OopRecorder::maybe_initialize() {
  62   if (_handles == NULL) {
  63     if (_arena != NULL) {
  64       _handles  = new(_arena) GrowableArray<jobject>(_arena, 10, 0, 0);
  65       _no_finds = new(_arena) GrowableArray<int>(    _arena, 10, 0, 0);
  66     } else {
  67       _handles  = new GrowableArray<jobject>(10, 0, 0);
  68       _no_finds = new GrowableArray<int>(    10, 0, 0);
  69     }
  70   }
  71 }
  72 
  73 
  74 jobject OopRecorder::handle_at(int index) {
  75   // there is always a NULL virtually present as first object
  76   if (index == null_index)  return NULL;
  77   return _handles->at(index - first_index);
  78 }
  79 
  80 
  81 // Local definition.  Used only in this module.
  82 inline bool OopRecorder::is_real_jobject(jobject h) {
  83   return h != NULL && h != (jobject)Universe::non_oop_word();
  84 }
  85 
  86 
  87 int OopRecorder::add_handle(jobject h, bool make_findable) {
  88   assert(!_complete, "cannot allocate more elements after size query");
  89   maybe_initialize();
  90   // indexing uses 1 as an origin--0 means null
  91   int index = _handles->length() + first_index;
  92   _handles->append(h);
  93 
  94   // Support correct operation of find_index().
  95   assert(!(make_findable && !is_real_jobject(h)), "nulls are not findable");
  96   if (make_findable) {
  97     // This index may be returned from find_index().
  98     if (_indexes != NULL) {
  99       int* cloc = _indexes->cache_location(h);
 100       _indexes->set_cache_location_index(cloc, index);
 101     } else if (index == index_cache_threshold && _arena != NULL) {
 102       _indexes = new(_arena) IndexCache();
 103       for (int i = 0; i < _handles->length(); i++) {
 104         // Load the cache with pre-existing elements.
 105         int index0 = i + first_index;
 106         if (_no_finds->contains(index0))  continue;
 107         int* cloc = _indexes->cache_location(_handles->at(i));
 108         _indexes->set_cache_location_index(cloc, index0);
 109       }
 110     }
 111   } else if (is_real_jobject(h)) {
 112     // Remember that this index is not to be returned from find_index().
 113     // This case is rare, because most or all uses of allocate_index pass
 114     // a jobject argument of NULL or Universe::non_oop_word.
 115     // Thus, the expected length of _no_finds is zero.
 116     _no_finds->append(index);
 117   }
 118 
 119   return index;
 120 }
 121 
 122 
 123 int OopRecorder::maybe_find_index(jobject h) {
 124   debug_only(_find_index_calls++);
 125   assert(!_complete, "cannot allocate more elements after size query");
 126   maybe_initialize();
 127   if (h == NULL)  return null_index;
 128   assert(is_real_jobject(h), "must be valid jobject");
 129   int* cloc = (_indexes == NULL)? NULL: _indexes->cache_location(h);
 130   if (cloc != NULL) {
 131     int cindex = _indexes->cache_location_index(cloc);
 132     if (cindex == 0) {
 133       return -1;   // We know this handle is completely new.
 134     }
 135     if (cindex >= first_index && _handles->at(cindex - first_index) == h) {
 136       debug_only(_hit_indexes++);
 137       return cindex;
 138     }
 139     if (!_indexes->cache_location_collision(cloc)) {
 140       return -1;   // We know the current cache occupant is unique to that cloc.
 141     }
 142   }
 143 
 144   // Not found in cache, due to a cache collision.  (Or, no cache at all.)
 145   // Do a linear search, most recent to oldest.
 146   for (int i = _handles->length() - 1; i >= 0; i--) {
 147     if (_handles->at(i) == h) {
 148       int findex = i + first_index;
 149       if (_no_finds->contains(findex))  continue;  // oops; skip this one
 150       if (cloc != NULL) {
 151         _indexes->set_cache_location_index(cloc, findex);
 152       }
 153       debug_only(_missed_indexes++);
 154       return findex;
 155     }
 156   }
 157   return -1;
 158 }