/* * 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" #include "code/nmethod.hpp" #include "gc_implementation/g1/g1CodeCacheRemSet.hpp" #include "memory/iterator.hpp" G1CodeRootChunk::G1CodeRootChunk() : _alloc_next(NULL), _next(NULL), _prev(NULL) { _alloc_next = first_element(); } void G1CodeRootChunk::reset() { _next = _prev = NULL; _alloc_next = first_element(); } void G1CodeRootChunk::nmethods_do(CodeBlobClosure* cl) { nmethod** cur = first_element(); while (cur != _alloc_next) { cl->do_code_blob(*cur); cur++; } } FreeList G1CodeRootSet::_free_list; size_t G1CodeRootSet::_num_chunks_handed_out = 0; G1CodeRootChunk* G1CodeRootSet::new_chunk() { G1CodeRootChunk* result = _free_list.get_chunk_at_head(); if (result == NULL) { result = new G1CodeRootChunk(); } G1CodeRootSet::_num_chunks_handed_out++; result->reset(); return result; } void G1CodeRootSet::free_chunk(G1CodeRootChunk* chunk) { _free_list.return_chunk_at_head(chunk); G1CodeRootSet::_num_chunks_handed_out--; } void G1CodeRootSet::free_all_chunks(FreeList* list) { _free_list.prepend(list); } void G1CodeRootSet::purge_chunks(size_t keep_ratio) { size_t keep = G1CodeRootSet::_num_chunks_handed_out * keep_ratio / 100; if (keep >= (size_t)_free_list.count()) { return; } FreeList temp; temp.initialize(); temp.set_size(G1CodeRootChunk::word_size()); _free_list.getFirstNChunksFromList((size_t)_free_list.count() - keep, &temp); G1CodeRootChunk* cur = temp.get_chunk_at_head(); while (cur != NULL) { delete cur; cur = temp.get_chunk_at_head(); } } size_t G1CodeRootSet::static_mem_size() { return sizeof(_free_list) + sizeof(_num_chunks_handed_out); } size_t G1CodeRootSet::fl_mem_size() { return _free_list.count() * _free_list.size(); } void G1CodeRootSet::initialize() { _free_list.initialize(); _free_list.set_size(G1CodeRootChunk::word_size()); } G1CodeRootSet::G1CodeRootSet() : _list(), _length(0) { _list.initialize(); _list.set_size(G1CodeRootChunk::word_size()); } G1CodeRootSet::~G1CodeRootSet() { clear(); } void G1CodeRootSet::add(nmethod* method) { if (!contains(method)) { // Try to add the nmethod. If there is not enough space, get a new chunk. if (_list.head() == NULL || _list.head()->is_full()) { G1CodeRootChunk* cur = new_chunk(); _list.return_chunk_at_head(cur); } bool result = _list.head()->add(method); guarantee(result, err_msg("Not able to add nmethod "PTR_FORMAT" to newly allocated chunk.", method)); _length++; } } void G1CodeRootSet::remove(nmethod* method) { G1CodeRootChunk* found = find(method); if (found != NULL) { bool result = found->remove(method); guarantee(result, err_msg("could not find nmethod "PTR_FORMAT" during removal although we previously found it", method)); // eventually free completely emptied chunk if (found->is_empty()) { _list.remove_chunk(found); free(found); } _length--; } assert(!contains(method), err_msg(PTR_FORMAT" still contains nmethod "PTR_FORMAT, this, method)); } nmethod* G1CodeRootSet::pop() { do { G1CodeRootChunk* cur = _list.head(); if (cur == NULL) { assert(_length == 0, "when there are no chunks, there should be no elements"); return NULL; } nmethod* result = cur->pop(); if (result != NULL) { _length--; return result; } else { free(_list.get_chunk_at_head()); } } while (true); } G1CodeRootChunk* G1CodeRootSet::find(nmethod* method) { G1CodeRootChunk* cur = _list.head(); while (cur != NULL) { if (cur->find(method)) { return cur; } cur = (G1CodeRootChunk*)cur->next(); } return NULL; } void G1CodeRootSet::free(G1CodeRootChunk* chunk) { free_chunk(chunk); } bool G1CodeRootSet::contains(nmethod* method) { return find(method) != NULL; } void G1CodeRootSet::clear() { free_all_chunks(&_list); _length = 0; } void G1CodeRootSet::nmethods_do(CodeBlobClosure* blk) const { G1CodeRootChunk* cur = _list.head(); while (cur != NULL) { cur->nmethods_do(blk); cur = (G1CodeRootChunk*)cur->next(); } } size_t G1CodeRootSet::mem_size() { return sizeof(this) + _list.count() * _list.size(); }