src/share/vm/gc_implementation/g1/g1CodeCacheRemSet.hpp

Print this page




  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 #ifndef SHARE_VM_GC_IMPLEMENTATION_G1_G1CODECACHEREMSET_HPP
  26 #define SHARE_VM_GC_IMPLEMENTATION_G1_G1CODECACHEREMSET_HPP
  27 
  28 #include "memory/allocation.hpp"
  29 #include "memory/freeList.hpp"
  30 #include "runtime/globals.hpp"
  31 
  32 class CodeBlobClosure;
  33 








  34 class G1CodeRootChunk : public CHeapObj<mtGC> {
  35  private:
  36   static const int NUM_ENTRIES = 32;
  37  public:
  38   G1CodeRootChunk*     _next;
  39   G1CodeRootChunk*     _prev;
  40 
  41   nmethod** _top;








  42 
  43   nmethod* _data[NUM_ENTRIES];


  44 
  45   nmethod** bottom() const {
  46     return (nmethod**) &(_data[0]);


  47   }
  48 
  49   nmethod** end() const {
  50     return (nmethod**) &(_data[NUM_ENTRIES]);
  51   }
  52 
  53  public:
  54   G1CodeRootChunk();
  55   ~G1CodeRootChunk() {}
  56 
  57   static size_t word_size() { return (size_t)(align_size_up_(sizeof(G1CodeRootChunk), HeapWordSize) / HeapWordSize); }
  58 
  59   // FreeList "interface" methods
  60 
  61   G1CodeRootChunk* next() const         { return _next; }
  62   G1CodeRootChunk* prev() const         { return _prev; }
  63   void set_next(G1CodeRootChunk* v)     { _next = v; assert(v != this, "Boom");}
  64   void set_prev(G1CodeRootChunk* v)     { _prev = v; assert(v != this, "Boom");}
  65   void clear_next()       { set_next(NULL); }
  66   void clear_prev()       { set_prev(NULL); }
  67 
  68   size_t size() const { return word_size(); }
  69 
  70   void link_next(G1CodeRootChunk* ptr)  { set_next(ptr); }
  71   void link_prev(G1CodeRootChunk* ptr)  { set_prev(ptr); }
  72   void link_after(G1CodeRootChunk* ptr) {
  73     link_next(ptr);
  74     if (ptr != NULL) ptr->link_prev((G1CodeRootChunk*)this);
  75   }
  76 
  77   bool is_free()                 { return true; }
  78 
  79   // New G1CodeRootChunk routines
  80 
  81   void reset();
  82 
  83   bool is_empty() const {
  84     return _top == bottom();
  85   }
  86 
  87   bool is_full() const {
  88     return _top == (nmethod**)end();
  89   }
  90 
  91   bool contains(nmethod* method) {
  92     nmethod** cur = bottom();
  93     while (cur != _top) {
  94       if (*cur == method) return true;
  95       cur++;
  96     }
  97     return false;
  98   }
  99 
 100   bool add(nmethod* method) {
 101     if (is_full()) return false;
 102     *_top = method;










 103     _top++;
 104     return true;
 105   }
 106 
 107   bool remove(nmethod* method) {
 108     nmethod** cur = bottom();
 109     while (cur != _top) {
 110       if (*cur == method) {
 111         memmove(cur, cur + 1, (_top - (cur + 1)) * sizeof(nmethod**));
 112         _top--;
 113         return true;
 114       }
 115       cur++;
 116     }
 117     return false;
 118   }
 119 
 120   void nmethods_do(CodeBlobClosure* blk);
 121 
 122   nmethod* pop() {
 123     if (is_empty()) {
 124       return NULL;

 125     }


 126     _top--;
 127     return *_top;





 128   }
 129 };
 130 
 131 // Manages free chunks.
 132 class G1CodeRootChunkManager VALUE_OBJ_CLASS_SPEC {
 133  private:
 134   // Global free chunk list management
 135   FreeList<G1CodeRootChunk> _free_list;
 136   // Total number of chunks handed out
 137   size_t _num_chunks_handed_out;
 138 
 139  public:
 140   G1CodeRootChunkManager();
 141 
 142   G1CodeRootChunk* new_chunk();
 143   void free_chunk(G1CodeRootChunk* chunk);
 144   // Free all elements of the given list.
 145   void free_all_chunks(FreeList<G1CodeRootChunk>* list);
 146 
 147   void initialize();


 176 
 177   size_t _length;
 178   FreeList<G1CodeRootChunk> _list;
 179   G1CodeRootChunkManager* _manager;
 180 
 181  public:
 182   // If an instance is initialized with a chunk manager of NULL, use the global
 183   // default one.
 184   G1CodeRootSet(G1CodeRootChunkManager* manager = NULL);
 185   ~G1CodeRootSet();
 186 
 187   static void purge_chunks(size_t keep_ratio);
 188 
 189   static size_t free_chunks_static_mem_size();
 190   static size_t free_chunks_mem_size();
 191 
 192   // Search for the code blob from the recently allocated ones to find duplicates more quickly, as this
 193   // method is likely to be repeatedly called with the same nmethod.
 194   void add(nmethod* method);
 195 
 196   void remove(nmethod* method);
 197   nmethod* pop();
 198 
 199   bool contains(nmethod* method);
 200 
 201   void clear();
 202 
 203   void nmethods_do(CodeBlobClosure* blk) const;
 204 
 205   bool is_empty() { return length() == 0; }
 206 
 207   // Length in elements
 208   size_t length() const { return _length; }
 209 
 210   // Static data memory size in bytes of this set.
 211   static size_t static_mem_size();
 212   // Memory size in bytes taken by this set.
 213   size_t mem_size();
 214 
 215   static void test() PRODUCT_RETURN;
 216 };


  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 #ifndef SHARE_VM_GC_IMPLEMENTATION_G1_G1CODECACHEREMSET_HPP
  26 #define SHARE_VM_GC_IMPLEMENTATION_G1_G1CODECACHEREMSET_HPP
  27 
  28 #include "memory/allocation.hpp"
  29 #include "memory/freeList.hpp"
  30 #include "runtime/globals.hpp"
  31 
  32 class CodeBlobClosure;
  33 
  34 // The elements of the G1CodeRootChunk is either:
  35 //  1) nmethod pointers
  36 //  2) nodes in an internally chained free list
  37 typedef union {
  38   nmethod* _nmethod;
  39   void*    _link;
  40 } NmethodOrLink;
  41 
  42 class G1CodeRootChunk : public CHeapObj<mtGC> {
  43  private:
  44   static const int NUM_ENTRIES = 32;
  45  public:
  46   G1CodeRootChunk*     _next;
  47   G1CodeRootChunk*     _prev;
  48 
  49   NmethodOrLink*          _top;
  50   // First free position within the chunk.
  51   volatile NmethodOrLink* _free;
  52 
  53   NmethodOrLink _data[NUM_ENTRIES];
  54 
  55   NmethodOrLink* bottom() const {
  56     return (NmethodOrLink*) &(_data[0]);
  57   }
  58 
  59   NmethodOrLink* end() const {
  60     return (NmethodOrLink*) &(_data[NUM_ENTRIES]);
  61   }
  62 
  63   bool is_link(NmethodOrLink* nmethod_or_link) {
  64     return nmethod_or_link->_link == NULL ||
  65         (bottom() <= nmethod_or_link->_link
  66         && nmethod_or_link->_link < end());
  67   }
  68 
  69   bool is_nmethod(NmethodOrLink* nmethod_or_link) {
  70     return !is_link(nmethod_or_link);
  71   }
  72 
  73  public:
  74   G1CodeRootChunk();
  75   ~G1CodeRootChunk() {}
  76 
  77   static size_t word_size() { return (size_t)(align_size_up_(sizeof(G1CodeRootChunk), HeapWordSize) / HeapWordSize); }
  78 
  79   // FreeList "interface" methods
  80 
  81   G1CodeRootChunk* next() const         { return _next; }
  82   G1CodeRootChunk* prev() const         { return _prev; }
  83   void set_next(G1CodeRootChunk* v)     { _next = v; assert(v != this, "Boom");}
  84   void set_prev(G1CodeRootChunk* v)     { _prev = v; assert(v != this, "Boom");}
  85   void clear_next()       { set_next(NULL); }
  86   void clear_prev()       { set_prev(NULL); }
  87 
  88   size_t size() const { return word_size(); }
  89 
  90   void link_next(G1CodeRootChunk* ptr)  { set_next(ptr); }
  91   void link_prev(G1CodeRootChunk* ptr)  { set_prev(ptr); }
  92   void link_after(G1CodeRootChunk* ptr) {
  93     link_next(ptr);
  94     if (ptr != NULL) ptr->link_prev((G1CodeRootChunk*)this);
  95   }
  96 
  97   bool is_free()                 { return true; }
  98 
  99   // New G1CodeRootChunk routines
 100 
 101   void reset();
 102 
 103   bool is_empty() const {
 104     return _top == bottom();
 105   }
 106 
 107   bool is_full() const {
 108     return _top == end() && _free == NULL;
 109   }
 110 
 111   bool contains(nmethod* method) {
 112     NmethodOrLink* cur = bottom();
 113     while (cur != _top) {
 114       if (cur->_nmethod == method) return true;
 115       cur++;
 116     }
 117     return false;
 118   }
 119 
 120   bool add(nmethod* method) {
 121     if (is_full()) {
 122       return false;
 123     }
 124 
 125     if (_free != NULL) {
 126       // Take from internally chained free list
 127       NmethodOrLink* first_free = (NmethodOrLink*)_free;
 128       _free = (NmethodOrLink*)_free->_link;
 129       first_free->_nmethod = method;
 130     } else {
 131       // Take from top.
 132       _top->_nmethod = method;
 133       _top++;

 134     }
 135 






 136     return true;
 137   }
 138 
 139   bool remove_lock_free(nmethod* method);


 140 
 141   void nmethods_do(CodeBlobClosure* blk);
 142 
 143   nmethod* pop() {
 144     if (_free != NULL) {
 145       // Kill the free list.
 146       _free = NULL;
 147     }
 148 
 149     while (!is_empty()) {
 150       _top--;
 151       if (is_nmethod(_top)) {
 152         return _top->_nmethod;
 153       }
 154     }
 155 
 156     return NULL;
 157   }
 158 };
 159 
 160 // Manages free chunks.
 161 class G1CodeRootChunkManager VALUE_OBJ_CLASS_SPEC {
 162  private:
 163   // Global free chunk list management
 164   FreeList<G1CodeRootChunk> _free_list;
 165   // Total number of chunks handed out
 166   size_t _num_chunks_handed_out;
 167 
 168  public:
 169   G1CodeRootChunkManager();
 170 
 171   G1CodeRootChunk* new_chunk();
 172   void free_chunk(G1CodeRootChunk* chunk);
 173   // Free all elements of the given list.
 174   void free_all_chunks(FreeList<G1CodeRootChunk>* list);
 175 
 176   void initialize();


 205 
 206   size_t _length;
 207   FreeList<G1CodeRootChunk> _list;
 208   G1CodeRootChunkManager* _manager;
 209 
 210  public:
 211   // If an instance is initialized with a chunk manager of NULL, use the global
 212   // default one.
 213   G1CodeRootSet(G1CodeRootChunkManager* manager = NULL);
 214   ~G1CodeRootSet();
 215 
 216   static void purge_chunks(size_t keep_ratio);
 217 
 218   static size_t free_chunks_static_mem_size();
 219   static size_t free_chunks_mem_size();
 220 
 221   // Search for the code blob from the recently allocated ones to find duplicates more quickly, as this
 222   // method is likely to be repeatedly called with the same nmethod.
 223   void add(nmethod* method);
 224 
 225   void remove_lock_free(nmethod* method);
 226   nmethod* pop();
 227 
 228   bool contains(nmethod* method);
 229 
 230   void clear();
 231 
 232   void nmethods_do(CodeBlobClosure* blk) const;
 233 
 234   bool is_empty() { return length() == 0; }
 235 
 236   // Length in elements
 237   size_t length() const { return _length; }
 238 
 239   // Static data memory size in bytes of this set.
 240   static size_t static_mem_size();
 241   // Memory size in bytes taken by this set.
 242   size_t mem_size();
 243 
 244   static void test() PRODUCT_RETURN;
 245 };