Path: blob/master/src/hotspot/share/gc/g1/g1CodeCacheRemSet.cpp
40961 views
/*1* Copyright (c) 2014, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "code/codeCache.hpp"26#include "code/nmethod.hpp"27#include "gc/g1/g1CodeRootSetTable.hpp"28#include "gc/g1/g1CodeCacheRemSet.hpp"29#include "gc/g1/heapRegion.hpp"30#include "memory/heap.hpp"31#include "memory/iterator.hpp"32#include "oops/access.inline.hpp"33#include "oops/oop.inline.hpp"34#include "runtime/atomic.hpp"35#include "services/memTracker.hpp"36#include "utilities/hashtable.inline.hpp"37#include "utilities/stack.inline.hpp"3839G1CodeRootSetTable* volatile G1CodeRootSetTable::_purge_list = NULL;4041size_t G1CodeRootSetTable::mem_size() {42return sizeof(G1CodeRootSetTable) + (entry_size() * number_of_entries()) + (sizeof(HashtableBucket<mtGC>) * table_size());43}4445G1CodeRootSetTable::Entry* G1CodeRootSetTable::new_entry(nmethod* nm) {46unsigned int hash = compute_hash(nm);47return (Entry*)Hashtable<nmethod*, mtGC>::new_entry(hash, nm);48}4950void G1CodeRootSetTable::remove_entry(Entry* e, Entry* previous) {51int index = hash_to_index(e->hash());52assert((e == bucket(index)) == (previous == NULL), "if e is the first entry then previous should be null");5354if (previous == NULL) {55set_entry(index, e->next());56} else {57previous->set_next(e->next());58}59free_entry(e);60}6162G1CodeRootSetTable::~G1CodeRootSetTable() {63for (int index = 0; index < table_size(); ++index) {64for (Entry* e = bucket(index); e != NULL; ) {65Entry* to_remove = e;66// read next before freeing.67e = e->next();68BasicHashtable<mtGC>::free_entry(to_remove);69}70}71assert(number_of_entries() == 0, "should have removed all entries");72}7374bool G1CodeRootSetTable::add(nmethod* nm) {75if (!contains(nm)) {76Entry* e = new_entry(nm);77int index = hash_to_index(e->hash());78add_entry(index, e);79return true;80}81return false;82}8384bool G1CodeRootSetTable::contains(nmethod* nm) {85int index = hash_to_index(compute_hash(nm));86for (Entry* e = bucket(index); e != NULL; e = e->next()) {87if (e->literal() == nm) {88return true;89}90}91return false;92}9394bool G1CodeRootSetTable::remove(nmethod* nm) {95int index = hash_to_index(compute_hash(nm));96Entry* previous = NULL;97for (Entry* e = bucket(index); e != NULL; previous = e, e = e->next()) {98if (e->literal() == nm) {99remove_entry(e, previous);100return true;101}102}103return false;104}105106void G1CodeRootSetTable::copy_to(G1CodeRootSetTable* new_table) {107for (int index = 0; index < table_size(); ++index) {108for (Entry* e = bucket(index); e != NULL; e = e->next()) {109new_table->add(e->literal());110}111}112}113114void G1CodeRootSetTable::nmethods_do(CodeBlobClosure* blk) {115for (int index = 0; index < table_size(); ++index) {116for (Entry* e = bucket(index); e != NULL; e = e->next()) {117blk->do_code_blob(e->literal());118}119}120}121122template<typename CB>123int G1CodeRootSetTable::remove_if(CB& should_remove) {124int num_removed = 0;125for (int index = 0; index < table_size(); ++index) {126Entry* previous = NULL;127Entry* e = bucket(index);128while (e != NULL) {129Entry* next = e->next();130if (should_remove(e->literal())) {131remove_entry(e, previous);132++num_removed;133} else {134previous = e;135}136e = next;137}138}139return num_removed;140}141142G1CodeRootSet::~G1CodeRootSet() {143delete _table;144}145146G1CodeRootSetTable* G1CodeRootSet::load_acquire_table() {147return Atomic::load_acquire(&_table);148}149150void G1CodeRootSet::allocate_small_table() {151G1CodeRootSetTable* temp = new G1CodeRootSetTable(SmallSize);152153Atomic::release_store(&_table, temp);154}155156void G1CodeRootSetTable::purge_list_append(G1CodeRootSetTable* table) {157for (;;) {158table->_purge_next = _purge_list;159G1CodeRootSetTable* old = Atomic::cmpxchg(&_purge_list, table->_purge_next, table);160if (old == table->_purge_next) {161break;162}163}164}165166void G1CodeRootSetTable::purge() {167G1CodeRootSetTable* table = _purge_list;168_purge_list = NULL;169while (table != NULL) {170G1CodeRootSetTable* to_purge = table;171table = table->_purge_next;172delete to_purge;173}174}175176void G1CodeRootSet::move_to_large() {177G1CodeRootSetTable* temp = new G1CodeRootSetTable(LargeSize);178179_table->copy_to(temp);180181G1CodeRootSetTable::purge_list_append(_table);182183Atomic::release_store(&_table, temp);184}185186void G1CodeRootSet::purge() {187G1CodeRootSetTable::purge();188}189190size_t G1CodeRootSet::static_mem_size() {191return G1CodeRootSetTable::static_mem_size();192}193194void G1CodeRootSet::add(nmethod* method) {195bool added = false;196if (is_empty()) {197allocate_small_table();198}199added = _table->add(method);200if (added) {201if (_length == Threshold) {202move_to_large();203}204++_length;205}206assert(_length == (size_t)_table->number_of_entries(), "sizes should match");207}208209bool G1CodeRootSet::remove(nmethod* method) {210bool removed = false;211if (_table != NULL) {212removed = _table->remove(method);213}214if (removed) {215_length--;216if (_length == 0) {217clear();218}219}220assert((_length == 0 && _table == NULL) ||221(_length == (size_t)_table->number_of_entries()), "sizes should match");222return removed;223}224225bool G1CodeRootSet::contains(nmethod* method) {226G1CodeRootSetTable* table = load_acquire_table(); // contains() may be called outside of lock, so ensure mem sync.227if (table != NULL) {228return table->contains(method);229}230return false;231}232233void G1CodeRootSet::clear() {234delete _table;235_table = NULL;236_length = 0;237}238239size_t G1CodeRootSet::mem_size() {240return sizeof(*this) + (_table != NULL ? _table->mem_size() : 0);241}242243void G1CodeRootSet::nmethods_do(CodeBlobClosure* blk) const {244if (_table != NULL) {245_table->nmethods_do(blk);246}247}248249class CleanCallback : public StackObj {250class PointsIntoHRDetectionClosure : public OopClosure {251HeapRegion* _hr;252public:253bool _points_into;254PointsIntoHRDetectionClosure(HeapRegion* hr) : _hr(hr), _points_into(false) {}255256void do_oop(narrowOop* o) {257do_oop_work(o);258}259260void do_oop(oop* o) {261do_oop_work(o);262}263264template <typename T>265void do_oop_work(T* p) {266if (_hr->is_in(RawAccess<>::oop_load(p))) {267_points_into = true;268}269}270};271272PointsIntoHRDetectionClosure _detector;273CodeBlobToOopClosure _blobs;274275public:276CleanCallback(HeapRegion* hr) : _detector(hr), _blobs(&_detector, !CodeBlobToOopClosure::FixRelocations) {}277278bool operator() (nmethod* nm) {279_detector._points_into = false;280_blobs.do_code_blob(nm);281return !_detector._points_into;282}283};284285void G1CodeRootSet::clean(HeapRegion* owner) {286CleanCallback should_clean(owner);287if (_table != NULL) {288int removed = _table->remove_if(should_clean);289assert((size_t)removed <= _length, "impossible");290_length -= removed;291}292if (_length == 0) {293clear();294}295}296297298