g1CodeCacheRemSet.cpp 10.3 KB
Newer Older
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
/*
 * Copyright (c) 2014, 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"
26
#include "code/codeCache.hpp"
27 28
#include "code/nmethod.hpp"
#include "gc_implementation/g1/g1CodeCacheRemSet.hpp"
29 30
#include "gc_implementation/g1/heapRegion.hpp"
#include "memory/heap.hpp"
31
#include "memory/iterator.hpp"
32 33 34
#include "oops/oop.inline.hpp"
#include "utilities/hashtable.inline.hpp"
#include "utilities/stack.inline.hpp"
35

36 37
PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC

38 39 40
class CodeRootSetTable : public Hashtable<nmethod*, mtGC> {
  friend class G1CodeRootSetTest;
  typedef HashtableEntry<nmethod*, mtGC> Entry;
41

42
  static CodeRootSetTable* volatile _purge_list;
43

44
  CodeRootSetTable* _purge_next;
45

46 47 48 49
  unsigned int compute_hash(nmethod* nm) {
    uintptr_t hash = (uintptr_t)nm;
    return hash ^ (hash >> 7); // code heap blocks are 128byte aligned
  }
50

51
  Entry* new_entry(nmethod* nm);
52

53 54 55
 public:
  CodeRootSetTable(int size) : Hashtable<nmethod*, mtGC>(size, sizeof(Entry)), _purge_next(NULL) {}
  ~CodeRootSetTable();
56

57 58 59
  // Needs to be protected locks
  bool add(nmethod* nm);
  bool remove(nmethod* nm);
60

61 62
  // Can be called without locking
  bool contains(nmethod* nm);
63

64
  int entry_size() const { return BasicHashtable<mtGC>::entry_size(); }
65

66 67
  void copy_to(CodeRootSetTable* new_table);
  void nmethods_do(CodeBlobClosure* blk);
68

69 70
  template<typename CB>
  void remove_if(CB& should_remove);
71

72 73
  static void purge_list_append(CodeRootSetTable* tbl);
  static void purge();
74

75 76
  static size_t static_mem_size() {
    return sizeof(_purge_list);
77
  }
78
};
79

80
CodeRootSetTable* volatile CodeRootSetTable::_purge_list = NULL;
81

82 83 84 85 86
CodeRootSetTable::Entry* CodeRootSetTable::new_entry(nmethod* nm) {
  unsigned int hash = compute_hash(nm);
  Entry* entry = (Entry*) new_entry_free_list();
  if (entry == NULL) {
    entry = (Entry*) NEW_C_HEAP_ARRAY2(char, entry_size(), mtGC, CURRENT_PC);
87
  }
88 89 90 91
  entry->set_next(NULL);
  entry->set_hash(hash);
  entry->set_literal(nm);
  return entry;
92 93
}

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
CodeRootSetTable::~CodeRootSetTable() {
  for (int index = 0; index < table_size(); ++index) {
    for (Entry* e = bucket(index); e != NULL; ) {
      Entry* to_remove = e;
      // read next before freeing.
      e = e->next();
      unlink_entry(to_remove);
      FREE_C_HEAP_ARRAY(char, to_remove, mtGC);
    }
  }
  assert(number_of_entries() == 0, "should have removed all entries");
  free_buckets();
  for (BasicHashtableEntry<mtGC>* e = new_entry_free_list(); e != NULL; e = new_entry_free_list()) {
    FREE_C_HEAP_ARRAY(char, e, mtGC);
  }
109 110
}

111 112 113 114 115 116
bool CodeRootSetTable::add(nmethod* nm) {
  if (!contains(nm)) {
    Entry* e = new_entry(nm);
    int index = hash_to_index(e->hash());
    add_entry(index, e);
    return true;
117
  }
118
  return false;
119 120
}

121 122 123 124 125 126 127 128 129
bool CodeRootSetTable::contains(nmethod* nm) {
  int index = hash_to_index(compute_hash(nm));
  for (Entry* e = bucket(index); e != NULL; e = e->next()) {
    if (e->literal() == nm) {
      return true;
    }
  }
  return false;
}
130

131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
bool CodeRootSetTable::remove(nmethod* nm) {
  int index = hash_to_index(compute_hash(nm));
  Entry* previous = NULL;
  for (Entry* e = bucket(index); e != NULL; previous = e, e = e->next()) {
    if (e->literal() == nm) {
      if (previous != NULL) {
        previous->set_next(e->next());
      } else {
        set_entry(index, e->next());
      }
      free_entry(e);
      return true;
    }
  }
  return false;
146 147
}

148 149 150 151 152 153 154
void CodeRootSetTable::copy_to(CodeRootSetTable* new_table) {
  for (int index = 0; index < table_size(); ++index) {
    for (Entry* e = bucket(index); e != NULL; e = e->next()) {
      new_table->add(e->literal());
    }
  }
  new_table->copy_freelist(this);
155 156
}

157 158 159 160 161 162 163
void CodeRootSetTable::nmethods_do(CodeBlobClosure* blk) {
  for (int index = 0; index < table_size(); ++index) {
    for (Entry* e = bucket(index); e != NULL; e = e->next()) {
      blk->do_code_blob(e->literal());
    }
  }
}
164

165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
template<typename CB>
void CodeRootSetTable::remove_if(CB& should_remove) {
  for (int index = 0; index < table_size(); ++index) {
    Entry* previous = NULL;
    Entry* e = bucket(index);
    while (e != NULL) {
      Entry* next = e->next();
      if (should_remove(e->literal())) {
        if (previous != NULL) {
          previous->set_next(next);
        } else {
          set_entry(index, next);
        }
        free_entry(e);
      } else {
        previous = e;
      }
      e = next;
    }
  }
}
186

187 188
G1CodeRootSet::~G1CodeRootSet() {
  delete _table;
189 190
}

191 192
CodeRootSetTable* G1CodeRootSet::load_acquire_table() {
  return (CodeRootSetTable*) OrderAccess::load_ptr_acquire(&_table);
193 194
}

195 196
void G1CodeRootSet::allocate_small_table() {
  _table = new CodeRootSetTable(SmallSize);
197 198
}

199 200 201 202 203 204 205
void CodeRootSetTable::purge_list_append(CodeRootSetTable* table) {
  for (;;) {
    table->_purge_next = _purge_list;
    CodeRootSetTable* old = (CodeRootSetTable*) Atomic::cmpxchg_ptr(table, &_purge_list, table->_purge_next);
    if (old == table->_purge_next) {
      break;
    }
206
  }
207 208
}

209 210 211 212 213 214 215 216
void CodeRootSetTable::purge() {
  CodeRootSetTable* table = _purge_list;
  _purge_list = NULL;
  while (table != NULL) {
    CodeRootSetTable* to_purge = table;
    table = table->_purge_next;
    delete to_purge;
  }
217 218
}

219 220
void G1CodeRootSet::move_to_large() {
  CodeRootSetTable* temp = new CodeRootSetTable(LargeSize);
221

222
  _table->copy_to(temp);
223

224
  CodeRootSetTable::purge_list_append(_table);
225

226 227
  OrderAccess::release_store_ptr(&_table, temp);
}
228

229 230 231

void G1CodeRootSet::purge() {
  CodeRootSetTable::purge();
232 233
}

234 235
size_t G1CodeRootSet::static_mem_size() {
  return CodeRootSetTable::static_mem_size();
236 237
}

238 239 240 241 242 243 244 245 246 247 248
void G1CodeRootSet::add(nmethod* method) {
  bool added = false;
  if (is_empty()) {
    allocate_small_table();
  }
  added = _table->add(method);
  if (_length == Threshold) {
    move_to_large();
  }
  if (added) {
    ++_length;
249
  }
250 251
}

252 253 254 255 256 257 258 259 260
bool G1CodeRootSet::remove(nmethod* method) {
  bool removed = false;
  if (_table != NULL) {
    removed = _table->remove(method);
  }
  if (removed) {
    _length--;
    if (_length == 0) {
      clear();
261 262
    }
  }
263
  return removed;
264 265 266
}

bool G1CodeRootSet::contains(nmethod* method) {
267 268 269 270 271
  CodeRootSetTable* table = load_acquire_table();
  if (table != NULL) {
    return table->contains(method);
  }
  return false;
272 273 274
}

void G1CodeRootSet::clear() {
275 276
  delete _table;
  _table = NULL;
277 278 279
  _length = 0;
}

280 281 282 283 284
size_t G1CodeRootSet::mem_size() {
  return sizeof(*this) +
      (_table != NULL ? sizeof(CodeRootSetTable) + _table->entry_size() * _length : 0);
}

285
void G1CodeRootSet::nmethods_do(CodeBlobClosure* blk) const {
286 287
  if (_table != NULL) {
    _table->nmethods_do(blk);
288 289 290
  }
}

291 292 293 294 295 296
class CleanCallback : public StackObj {
  class PointsIntoHRDetectionClosure : public OopClosure {
    HeapRegion* _hr;
   public:
    bool _points_into;
    PointsIntoHRDetectionClosure(HeapRegion* hr) : _hr(hr), _points_into(false) {}
297

298 299 300
    void do_oop(narrowOop* o) {
      do_oop_work(o);
    }
301

302 303 304
    void do_oop(oop* o) {
      do_oop_work(o);
    }
305

306 307 308 309 310 311 312
    template <typename T>
    void do_oop_work(T* p) {
      if (_hr->is_in(oopDesc::load_decode_heap_oop(p))) {
        _points_into = true;
      }
    }
  };
313

314 315
  PointsIntoHRDetectionClosure _detector;
  CodeBlobToOopClosure _blobs;
316

317 318
 public:
  CleanCallback(HeapRegion* hr) : _detector(hr), _blobs(&_detector, !CodeBlobToOopClosure::FixRelocations) {}
319

320 321 322 323 324 325
  bool operator() (nmethod* nm) {
    _detector._points_into = false;
    _blobs.do_code_blob(nm);
    return _detector._points_into;
  }
};
326

327 328 329 330 331 332
void G1CodeRootSet::clean(HeapRegion* owner) {
  CleanCallback should_clean(owner);
  if (_table != NULL) {
    _table->remove_if(should_clean);
  }
}
333

334 335 336 337 338 339 340 341
#ifndef PRODUCT

class G1CodeRootSetTest {
 public:
  static void test() {
    {
      G1CodeRootSet set1;
      assert(set1.is_empty(), "Code root set must be initially empty but is not.");
342

343 344
      assert(G1CodeRootSet::static_mem_size() == sizeof(void*),
          err_msg("The code root set's static memory usage is incorrect, "SIZE_FORMAT" bytes", G1CodeRootSet::static_mem_size()));
345 346

      set1.add((nmethod*)1);
347 348
      assert(set1.length() == 1, err_msg("Added exactly one element, but set contains "
          SIZE_FORMAT" elements", set1.length()));
349

350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
      const size_t num_to_add = (size_t)G1CodeRootSet::Threshold + 1;

      for (size_t i = 1; i <= num_to_add; i++) {
        set1.add((nmethod*)1);
      }
      assert(set1.length() == 1,
          err_msg("Duplicate detection should not have increased the set size but "
              "is "SIZE_FORMAT, set1.length()));

      for (size_t i = 2; i <= num_to_add; i++) {
        set1.add((nmethod*)(uintptr_t)(i));
      }
      assert(set1.length() == num_to_add,
          err_msg("After adding in total "SIZE_FORMAT" distinct code roots, they "
              "need to be in the set, but there are only "SIZE_FORMAT,
              num_to_add, set1.length()));

      assert(CodeRootSetTable::_purge_list != NULL, "should have grown to large hashtable");

      size_t num_popped = 0;
      for (size_t i = 1; i <= num_to_add; i++) {
        bool removed = set1.remove((nmethod*)i);
        if (removed) {
          num_popped += 1;
        } else {
          break;
        }
377
      }
378 379 380 381 382 383 384 385 386
      assert(num_popped == num_to_add,
          err_msg("Managed to pop "SIZE_FORMAT" code roots, but only "SIZE_FORMAT" "
              "were added", num_popped, num_to_add));
      assert(CodeRootSetTable::_purge_list != NULL, "should have grown to large hashtable");

      G1CodeRootSet::purge();

      assert(CodeRootSetTable::_purge_list == NULL, "should have purged old small tables");

387 388
    }

389 390
  }
};
391 392

void TestCodeCacheRemSet_test() {
393
  G1CodeRootSetTest::test();
394
}
395

396
#endif