1 /*
   2  * Copyright (c) 1997, 2006, 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 // A Cache for storing (method, bci) -> oopMap.
  26 // The memory management system uses the cache when locating object
  27 // references in an interpreted frame.
  28 //
  29 // OopMapCache's are allocated lazily per instanceKlass.
  30 
  31 // The oopMap (InterpreterOopMap) is stored as a bit mask. If the
  32 // bit_mask can fit into two words it is stored in
  33 // the _bit_mask array, otherwise it is allocated on the heap.
  34 // For OopMapCacheEntry the bit_mask is allocated in the C heap
  35 // because these entries persist between garbage collections.
  36 // For InterpreterOopMap the bit_mask is allocated in
  37 // a resource area for better performance.  InterpreterOopMap
  38 // should only be created and deleted during same garbage collection.
  39 //
  40 // If ENABBLE_ZAP_DEAD_LOCALS is defined, two bits are used
  41 // per entry instead of one. In all cases,
  42 // the first bit is set to indicate oops as opposed to other
  43 // values. If the second bit is available,
  44 // it is set for dead values. We get the following encoding:
  45 //
  46 // 00 live value
  47 // 01 live oop
  48 // 10 dead value
  49 // 11 <unused>                                   (we cannot distinguish between dead oops or values with the current oop map generator)
  50 
  51 
  52 class OffsetClosure  {
  53  public:
  54   virtual void offset_do(int offset) = 0;
  55 };
  56 
  57 
  58 class InterpreterOopMap: ResourceObj {
  59   friend class OopMapCache;
  60 
  61  public:
  62   enum {
  63     N                = 2,                // the number of words reserved
  64                                          // for inlined mask storage
  65     small_mask_limit = N * BitsPerWord,  // the maximum number of bits
  66                                          // available for small masks,
  67                                          // small_mask_limit can be set to 0
  68                                          // for testing bit_mask allocation
  69 
  70 #ifdef ENABLE_ZAP_DEAD_LOCALS
  71     bits_per_entry   = 2,
  72     dead_bit_number  = 1,
  73 #else
  74     bits_per_entry   = 1,
  75 #endif
  76     oop_bit_number   = 0
  77   };
  78 
  79  private:
  80   methodOop      _method;         // the method for which the mask is valid
  81   unsigned short _bci;            // the bci    for which the mask is valid
  82   int            _mask_size;      // the mask size in bits
  83   int            _expression_stack_size; // the size of the expression stack in slots
  84 
  85  protected:
  86   intptr_t       _bit_mask[N];    // the bit mask if
  87                                   // mask_size <= small_mask_limit,
  88                                   // ptr to bit mask otherwise
  89                                   // "protected" so that sub classes can
  90                                   // access it without using trickery in
  91                                   // methd bit_mask().
  92 #ifdef ASSERT
  93   bool _resource_allocate_bit_mask;
  94 #endif
  95 
  96   // access methods
  97   methodOop      method() const                  { return _method; }
  98   void           set_method(methodOop v)         { _method = v; }
  99   int            bci() const                     { return _bci; }
 100   void           set_bci(int v)                  { _bci = v; }
 101   int            mask_size() const               { return _mask_size; }
 102   void           set_mask_size(int v)            { _mask_size = v; }
 103   int            number_of_entries() const       { return mask_size() / bits_per_entry; }
 104   // Test bit mask size and return either the in-line bit mask or allocated
 105   // bit mask.
 106   uintptr_t*  bit_mask()                         { return (uintptr_t*)(mask_size() <= small_mask_limit ? (intptr_t)_bit_mask : _bit_mask[0]); }
 107 
 108   // return the word size of_bit_mask.  mask_size() <= 4 * MAX_USHORT
 109   size_t mask_word_size() {
 110     return (mask_size() + BitsPerWord - 1) / BitsPerWord;
 111   }
 112 
 113   uintptr_t entry_at(int offset)            { int i = offset * bits_per_entry; return bit_mask()[i / BitsPerWord] >> (i % BitsPerWord); }
 114 
 115   void set_expression_stack_size(int sz)    { _expression_stack_size = sz; }
 116 
 117 #ifdef ENABLE_ZAP_DEAD_LOCALS
 118   bool is_dead(int offset)                       { return (entry_at(offset) & (1 << dead_bit_number)) != 0; }
 119 #endif
 120 
 121   // Lookup
 122   bool match(methodHandle method, int bci)       { return _method == method() && _bci == bci; }
 123   bool is_empty();
 124 
 125   // Initialization
 126   void initialize();
 127 
 128  public:
 129   InterpreterOopMap();
 130   ~InterpreterOopMap();
 131 
 132   // Copy the OopMapCacheEntry in parameter "from" into this
 133   // InterpreterOopMap.  If the _bit_mask[0] in "from" points to
 134   // allocated space (i.e., the bit mask was to large to hold
 135   // in-line), allocate the space from a Resource area.
 136   void resource_copy(OopMapCacheEntry* from);
 137 
 138   void iterate_oop(OffsetClosure* oop_closure);
 139   void oop_iterate(OopClosure * blk);
 140   void oop_iterate(OopClosure * blk, MemRegion mr);
 141   void verify();
 142   void print();
 143 
 144   bool is_oop  (int offset)                      { return (entry_at(offset) & (1 << oop_bit_number )) != 0; }
 145 
 146   int expression_stack_size()                    { return _expression_stack_size; }
 147 
 148 #ifdef ENABLE_ZAP_DEAD_LOCALS
 149   void iterate_all(OffsetClosure* oop_closure, OffsetClosure* value_closure, OffsetClosure* dead_closure);
 150 #endif
 151 };
 152 
 153 class OopMapCache : public CHeapObj {
 154  private:
 155   enum { _size        = 32,     // Use fixed size for now
 156          _probe_depth = 3       // probe depth in case of collisions
 157   };
 158 
 159   OopMapCacheEntry* _array;
 160 
 161   unsigned int hash_value_for(methodHandle method, int bci);
 162   OopMapCacheEntry* entry_at(int i) const;
 163 
 164   Mutex _mut;
 165 
 166   void flush();
 167 
 168  public:
 169   OopMapCache();
 170   ~OopMapCache();                                // free up memory
 171 
 172   // flush cache entry is occupied by an obsolete method
 173   void flush_obsolete_entries();
 174 
 175   // Returns the oopMap for (method, bci) in parameter "entry".
 176   // Returns false if an oop map was not found.
 177   void lookup(methodHandle method, int bci, InterpreterOopMap* entry);
 178 
 179   // Compute an oop map without updating the cache or grabbing any locks (for debugging)
 180   static void compute_one_oop_map(methodHandle method, int bci, InterpreterOopMap* entry);
 181 
 182   // Helpers
 183   // Iterate over the entries in the cached OopMapCacheEntry's
 184   void oop_iterate(OopClosure *blk);
 185   void oop_iterate(OopClosure *blk, MemRegion mr);
 186   void verify();
 187 
 188   // Returns total no. of bytes allocated as part of OopMapCache's
 189   static long memory_usage()                     PRODUCT_RETURN0;
 190 };