tschatzl@6402: /* tschatzl@6402: * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. tschatzl@6402: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. tschatzl@6402: * tschatzl@6402: * This code is free software; you can redistribute it and/or modify it tschatzl@6402: * under the terms of the GNU General Public License version 2 only, as tschatzl@6402: * published by the Free Software Foundation. tschatzl@6402: * tschatzl@6402: * This code is distributed in the hope that it will be useful, but WITHOUT tschatzl@6402: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or tschatzl@6402: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License tschatzl@6402: * version 2 for more details (a copy is included in the LICENSE file that tschatzl@6402: * accompanied this code). tschatzl@6402: * tschatzl@6402: * You should have received a copy of the GNU General Public License version tschatzl@6402: * 2 along with this work; if not, write to the Free Software Foundation, tschatzl@6402: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. tschatzl@6402: * tschatzl@6402: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA tschatzl@6402: * or visit www.oracle.com if you need additional information or have any tschatzl@6402: * questions. tschatzl@6402: * tschatzl@6402: */ tschatzl@6402: tschatzl@6402: tschatzl@6402: #include "precompiled.hpp" tschatzl@6402: #include "code/nmethod.hpp" tschatzl@6402: #include "gc_implementation/g1/g1CodeCacheRemSet.hpp" tschatzl@6402: #include "memory/iterator.hpp" tschatzl@6402: tschatzl@6402: G1CodeRootChunk::G1CodeRootChunk() : _top(NULL), _next(NULL), _prev(NULL) { tschatzl@6402: _top = bottom(); tschatzl@6402: } tschatzl@6402: tschatzl@6402: void G1CodeRootChunk::reset() { tschatzl@6402: _next = _prev = NULL; tschatzl@6402: _top = bottom(); tschatzl@6402: } tschatzl@6402: tschatzl@6402: void G1CodeRootChunk::nmethods_do(CodeBlobClosure* cl) { tschatzl@6402: nmethod** cur = bottom(); tschatzl@6402: while (cur != _top) { tschatzl@6402: cl->do_code_blob(*cur); tschatzl@6402: cur++; tschatzl@6402: } tschatzl@6402: } tschatzl@6402: tschatzl@6402: FreeList G1CodeRootSet::_free_list; tschatzl@6402: size_t G1CodeRootSet::_num_chunks_handed_out = 0; tschatzl@6402: tschatzl@6402: G1CodeRootChunk* G1CodeRootSet::new_chunk() { tschatzl@6402: G1CodeRootChunk* result = _free_list.get_chunk_at_head(); tschatzl@6402: if (result == NULL) { tschatzl@6402: result = new G1CodeRootChunk(); tschatzl@6402: } tschatzl@6402: G1CodeRootSet::_num_chunks_handed_out++; tschatzl@6402: result->reset(); tschatzl@6402: return result; tschatzl@6402: } tschatzl@6402: tschatzl@6402: void G1CodeRootSet::free_chunk(G1CodeRootChunk* chunk) { tschatzl@6402: _free_list.return_chunk_at_head(chunk); tschatzl@6402: G1CodeRootSet::_num_chunks_handed_out--; tschatzl@6402: } tschatzl@6402: tschatzl@6402: void G1CodeRootSet::free_all_chunks(FreeList* list) { tschatzl@6402: G1CodeRootSet::_num_chunks_handed_out -= list->count(); tschatzl@6402: _free_list.prepend(list); tschatzl@6402: } tschatzl@6402: tschatzl@6402: void G1CodeRootSet::purge_chunks(size_t keep_ratio) { tschatzl@6402: size_t keep = G1CodeRootSet::_num_chunks_handed_out * keep_ratio / 100; tschatzl@6402: tschatzl@6402: if (keep >= (size_t)_free_list.count()) { tschatzl@6402: return; tschatzl@6402: } tschatzl@6402: tschatzl@6402: FreeList temp; tschatzl@6402: temp.initialize(); tschatzl@6402: temp.set_size(G1CodeRootChunk::word_size()); tschatzl@6402: tschatzl@6402: _free_list.getFirstNChunksFromList((size_t)_free_list.count() - keep, &temp); tschatzl@6402: tschatzl@6402: G1CodeRootChunk* cur = temp.get_chunk_at_head(); tschatzl@6402: while (cur != NULL) { tschatzl@6402: delete cur; tschatzl@6402: cur = temp.get_chunk_at_head(); tschatzl@6402: } tschatzl@6402: } tschatzl@6402: tschatzl@6402: size_t G1CodeRootSet::static_mem_size() { tschatzl@6402: return sizeof(_free_list) + sizeof(_num_chunks_handed_out); tschatzl@6402: } tschatzl@6402: tschatzl@6402: size_t G1CodeRootSet::fl_mem_size() { tschatzl@6402: return _free_list.count() * _free_list.size(); tschatzl@6402: } tschatzl@6402: tschatzl@6402: void G1CodeRootSet::initialize() { tschatzl@6402: _free_list.initialize(); tschatzl@6402: _free_list.set_size(G1CodeRootChunk::word_size()); tschatzl@6402: } tschatzl@6402: tschatzl@6402: G1CodeRootSet::G1CodeRootSet() : _list(), _length(0) { tschatzl@6402: _list.initialize(); tschatzl@6402: _list.set_size(G1CodeRootChunk::word_size()); tschatzl@6402: } tschatzl@6402: tschatzl@6402: G1CodeRootSet::~G1CodeRootSet() { tschatzl@6402: clear(); tschatzl@6402: } tschatzl@6402: tschatzl@6402: void G1CodeRootSet::add(nmethod* method) { tschatzl@6402: if (!contains(method)) { tschatzl@6402: // Try to add the nmethod. If there is not enough space, get a new chunk. tschatzl@6402: if (_list.head() == NULL || _list.head()->is_full()) { tschatzl@6402: G1CodeRootChunk* cur = new_chunk(); tschatzl@6402: _list.return_chunk_at_head(cur); tschatzl@6402: } tschatzl@6402: bool result = _list.head()->add(method); tschatzl@6402: guarantee(result, err_msg("Not able to add nmethod "PTR_FORMAT" to newly allocated chunk.", method)); tschatzl@6402: _length++; tschatzl@6402: } tschatzl@6402: } tschatzl@6402: tschatzl@6402: void G1CodeRootSet::remove(nmethod* method) { tschatzl@6402: G1CodeRootChunk* found = find(method); tschatzl@6402: if (found != NULL) { tschatzl@6402: bool result = found->remove(method); tschatzl@6402: guarantee(result, err_msg("could not find nmethod "PTR_FORMAT" during removal although we previously found it", method)); tschatzl@6402: // eventually free completely emptied chunk tschatzl@6402: if (found->is_empty()) { tschatzl@6402: _list.remove_chunk(found); tschatzl@6402: free(found); tschatzl@6402: } tschatzl@6402: _length--; tschatzl@6402: } tschatzl@6402: assert(!contains(method), err_msg(PTR_FORMAT" still contains nmethod "PTR_FORMAT, this, method)); tschatzl@6402: } tschatzl@6402: tschatzl@6402: nmethod* G1CodeRootSet::pop() { tschatzl@6402: do { tschatzl@6402: G1CodeRootChunk* cur = _list.head(); tschatzl@6402: if (cur == NULL) { tschatzl@6402: assert(_length == 0, "when there are no chunks, there should be no elements"); tschatzl@6402: return NULL; tschatzl@6402: } tschatzl@6402: nmethod* result = cur->pop(); tschatzl@6402: if (result != NULL) { tschatzl@6402: _length--; tschatzl@6402: return result; tschatzl@6402: } else { tschatzl@6402: free(_list.get_chunk_at_head()); tschatzl@6402: } tschatzl@6402: } while (true); tschatzl@6402: } tschatzl@6402: tschatzl@6402: G1CodeRootChunk* G1CodeRootSet::find(nmethod* method) { tschatzl@6402: G1CodeRootChunk* cur = _list.head(); tschatzl@6402: while (cur != NULL) { tschatzl@6402: if (cur->contains(method)) { tschatzl@6402: return cur; tschatzl@6402: } tschatzl@6402: cur = (G1CodeRootChunk*)cur->next(); tschatzl@6402: } tschatzl@6402: return NULL; tschatzl@6402: } tschatzl@6402: tschatzl@6402: void G1CodeRootSet::free(G1CodeRootChunk* chunk) { tschatzl@6402: free_chunk(chunk); tschatzl@6402: } tschatzl@6402: tschatzl@6402: bool G1CodeRootSet::contains(nmethod* method) { tschatzl@6402: return find(method) != NULL; tschatzl@6402: } tschatzl@6402: tschatzl@6402: void G1CodeRootSet::clear() { tschatzl@6402: free_all_chunks(&_list); tschatzl@6402: _length = 0; tschatzl@6402: } tschatzl@6402: tschatzl@6402: void G1CodeRootSet::nmethods_do(CodeBlobClosure* blk) const { tschatzl@6402: G1CodeRootChunk* cur = _list.head(); tschatzl@6402: while (cur != NULL) { tschatzl@6402: cur->nmethods_do(blk); tschatzl@6402: cur = (G1CodeRootChunk*)cur->next(); tschatzl@6402: } tschatzl@6402: } tschatzl@6402: tschatzl@6402: size_t G1CodeRootSet::mem_size() { tschatzl@6402: return sizeof(this) + _list.count() * _list.size(); tschatzl@6402: } tschatzl@6402: tschatzl@6402: #ifndef PRODUCT tschatzl@6402: tschatzl@6402: void G1CodeRootSet::test() { tschatzl@6402: initialize(); tschatzl@6402: tschatzl@6402: assert(_free_list.count() == 0, "Free List must be empty"); tschatzl@6402: assert(_num_chunks_handed_out == 0, "No elements must have been handed out yet"); tschatzl@6402: tschatzl@6402: // The number of chunks that we allocate for purge testing. tschatzl@6402: size_t const num_chunks = 10; tschatzl@6402: { tschatzl@6402: G1CodeRootSet set1; tschatzl@6402: assert(set1.is_empty(), "Code root set must be initially empty but is not."); tschatzl@6402: tschatzl@6402: set1.add((nmethod*)1); tschatzl@6402: assert(_num_chunks_handed_out == 1, tschatzl@6402: err_msg("Must have allocated and handed out one chunk, but handed out " tschatzl@6402: SIZE_FORMAT" chunks", _num_chunks_handed_out)); tschatzl@6402: assert(set1.length() == 1, err_msg("Added exactly one element, but set contains " tschatzl@6402: SIZE_FORMAT" elements", set1.length())); tschatzl@6402: tschatzl@6402: // G1CodeRootChunk::word_size() is larger than G1CodeRootChunk::num_entries which tschatzl@6402: // we cannot access. tschatzl@6402: for (uint i = 0; i < G1CodeRootChunk::word_size() + 1; i++) { tschatzl@6402: set1.add((nmethod*)1); tschatzl@6402: } tschatzl@6402: assert(_num_chunks_handed_out == 1, tschatzl@6402: err_msg("Duplicate detection must have prevented allocation of further " tschatzl@6402: "chunks but contains "SIZE_FORMAT, _num_chunks_handed_out)); tschatzl@6402: assert(set1.length() == 1, tschatzl@6402: err_msg("Duplicate detection should not have increased the set size but " tschatzl@6402: "is "SIZE_FORMAT, set1.length())); tschatzl@6402: tschatzl@6402: size_t num_total_after_add = G1CodeRootChunk::word_size() + 1; tschatzl@6402: for (size_t i = 0; i < num_total_after_add - 1; i++) { tschatzl@6402: set1.add((nmethod*)(2 + i)); tschatzl@6402: } tschatzl@6402: assert(_num_chunks_handed_out > 1, tschatzl@6402: "After adding more code roots, more than one chunks should have been handed out"); tschatzl@6402: assert(set1.length() == num_total_after_add, tschatzl@6402: err_msg("After adding in total "SIZE_FORMAT" distinct code roots, they " tschatzl@6402: "need to be in the set, but there are only "SIZE_FORMAT, tschatzl@6402: num_total_after_add, set1.length())); tschatzl@6402: tschatzl@6402: size_t num_popped = 0; tschatzl@6402: while (set1.pop() != NULL) { tschatzl@6402: num_popped++; tschatzl@6402: } tschatzl@6402: assert(num_popped == num_total_after_add, tschatzl@6402: err_msg("Managed to pop "SIZE_FORMAT" code roots, but only "SIZE_FORMAT" " tschatzl@6402: "were added", num_popped, num_total_after_add)); tschatzl@6402: assert(_num_chunks_handed_out == 0, tschatzl@6402: err_msg("After popping all elements, all chunks must have been returned " tschatzl@6402: "but are still "SIZE_FORMAT, _num_chunks_handed_out)); tschatzl@6402: tschatzl@6402: purge_chunks(0); tschatzl@6402: assert(_free_list.count() == 0, tschatzl@6402: err_msg("After purging everything, the free list must be empty but still " tschatzl@6402: "contains "SIZE_FORMAT" chunks", _free_list.count())); tschatzl@6402: tschatzl@6402: // Add some more handed out chunks. tschatzl@6402: size_t i = 0; tschatzl@6402: while (_num_chunks_handed_out < num_chunks) { tschatzl@6402: set1.add((nmethod*)i); tschatzl@6402: i++; tschatzl@6402: } tschatzl@6402: tschatzl@6402: { tschatzl@6402: // Generate chunks on the free list. tschatzl@6402: G1CodeRootSet set2; tschatzl@6402: size_t i = 0; tschatzl@6402: while (_num_chunks_handed_out < num_chunks * 2) { tschatzl@6402: set2.add((nmethod*)i); tschatzl@6402: i++; tschatzl@6402: } tschatzl@6402: // Exit of the scope of the set2 object will call the destructor that generates tschatzl@6402: // num_chunks elements on the free list. tschatzl@6402: } tschatzl@6402: tschatzl@6402: assert(_num_chunks_handed_out == num_chunks, tschatzl@6402: err_msg("Deletion of the second set must have resulted in giving back " tschatzl@6402: "those, but there is still "SIZE_FORMAT" handed out, expecting " tschatzl@6402: SIZE_FORMAT, _num_chunks_handed_out, num_chunks)); tschatzl@6402: assert((size_t)_free_list.count() == num_chunks, tschatzl@6402: err_msg("After freeing "SIZE_FORMAT" chunks, they must be on the free list " tschatzl@6402: "but there are only "SIZE_FORMAT, num_chunks, _free_list.count())); tschatzl@6402: tschatzl@6402: size_t const test_percentage = 50; tschatzl@6402: purge_chunks(test_percentage); tschatzl@6402: assert(_num_chunks_handed_out == num_chunks, tschatzl@6402: err_msg("Purging must not hand out chunks but there are "SIZE_FORMAT, tschatzl@6402: _num_chunks_handed_out)); tschatzl@6402: assert((size_t)_free_list.count() == (ssize_t)(num_chunks * test_percentage / 100), tschatzl@6402: err_msg("Must have purged "SIZE_FORMAT" percent of "SIZE_FORMAT" chunks" tschatzl@6402: "but there are "SSIZE_FORMAT, test_percentage, num_chunks, tschatzl@6402: _free_list.count())); tschatzl@6402: // Purge the remainder of the chunks on the free list. tschatzl@6402: purge_chunks(0); tschatzl@6402: assert(_free_list.count() == 0, "Free List must be empty"); tschatzl@6402: assert(_num_chunks_handed_out == num_chunks, tschatzl@6402: err_msg("Expected to be "SIZE_FORMAT" chunks handed out from the first set " tschatzl@6402: "but there are "SIZE_FORMAT, num_chunks, _num_chunks_handed_out)); tschatzl@6402: tschatzl@6402: // Exit of the scope of the set1 object will call the destructor that generates tschatzl@6402: // num_chunks additional elements on the free list. tschatzl@6402: } tschatzl@6402: tschatzl@6402: assert(_num_chunks_handed_out == 0, tschatzl@6402: err_msg("Deletion of the only set must have resulted in no chunks handed " tschatzl@6402: "out, but there is still "SIZE_FORMAT" handed out", _num_chunks_handed_out)); tschatzl@6402: assert((size_t)_free_list.count() == num_chunks, tschatzl@6402: err_msg("After freeing "SIZE_FORMAT" chunks, they must be on the free list " tschatzl@6402: "but there are only "SSIZE_FORMAT, num_chunks, _free_list.count())); tschatzl@6402: tschatzl@6402: // Restore initial state. tschatzl@6402: purge_chunks(0); tschatzl@6402: assert(_free_list.count() == 0, "Free List must be empty"); tschatzl@6402: assert(_num_chunks_handed_out == 0, "No elements must have been handed out yet"); tschatzl@6402: } tschatzl@6402: tschatzl@6402: void TestCodeCacheRemSet_test() { tschatzl@6402: G1CodeRootSet::test(); tschatzl@6402: } tschatzl@6402: #endif