Path: blob/master/src/hotspot/share/memory/metaspace/freeChunkList.hpp
40957 views
/*1* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.2* Copyright (c) 2020 SAP SE. All rights reserved.3* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.4*5* This code is free software; you can redistribute it and/or modify it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 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 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*23*/2425#ifndef SHARE_MEMORY_METASPACE_FREECHUNKLIST_HPP26#define SHARE_MEMORY_METASPACE_FREECHUNKLIST_HPP2728#include "memory/allocation.hpp"29#include "memory/metaspace/chunklevel.hpp"30#include "memory/metaspace/counters.hpp"31#include "memory/metaspace/metachunk.hpp"32#include "memory/metaspace/metachunkList.hpp"3334class outputStream;3536namespace metaspace {3738// This is the free list underlying the ChunkManager.39//40// Chunks are kept in a vector of double-linked double-headed lists41// (using Metachunk::prev/next). One list per chunk level exists.42//43// Chunks in these lists are roughly ordered: uncommitted chunks44// are added to the back of the list, fully or partially committed45// chunks to the front. We do not use a more elaborate sorting on46// insert since that path is used during class unloading, hence timing47// sensitive.48//49// During retrieval (at class loading), we search the list for a chunk50// of at least n committed words to satisfy the caller requested51// committed word size. We stop searching at the first fully uncommitted52// chunk.53//54// Note that even though this is an O(n) search, partially committed chunks are55// very rare. A partially committed chunk is one spanning multiple commit56// granules, of which some are committed and some are not.57// If metaspace reclamation is on (MetaspaceReclaimPolicy=balanced|aggressive), these58// chunks will become uncommitted after they are returned to the ChunkManager.59// If metaspace reclamation is off (MetaspaceReclaimPolicy=none) they are fully60// committed when handed out and will not be uncommitted when returned to the61// ChunkManager.62//63// Therefore in all likelihood the chunk lists only contain fully committed or64// fully uncommitted chunks; either way search will stop at the first chunk.6566class FreeChunkList {6768Metachunk* _first;69Metachunk* _last;7071IntCounter _num_chunks;7273void add_front(Metachunk* c) {74if (_first == NULL) {75assert(_last == NULL, "Sanity");76_first = _last = c;77c->set_prev(NULL);78c->set_next(NULL);79} else {80assert(_last != NULL, "Sanity");81c->set_next(_first);82c->set_prev(NULL);83_first->set_prev(c);84_first = c;85}86}8788// Add chunk to the back of the list.89void add_back(Metachunk* c) {90if (_last == NULL) {91assert(_first == NULL, "Sanity");92_last = _first = c;93c->set_prev(NULL);94c->set_next(NULL);95} else {96assert(_first != NULL, "Sanity");97c->set_next(NULL);98c->set_prev(_last);99_last->set_next(c);100_last = c;101}102}103104public:105106FreeChunkList() :107_first(NULL),108_last(NULL)109{}110111// Remove given chunk from anywhere in the list.112Metachunk* remove(Metachunk* c) {113assert(contains(c), "Must be contained here");114Metachunk* pred = c->prev();115Metachunk* succ = c->next();116if (pred) {117pred->set_next(succ);118}119if (succ) {120succ->set_prev(pred);121}122if (_first == c) {123_first = succ;124}125if (_last == c) {126_last = pred;127}128c->set_next(NULL);129c->set_prev(NULL);130_num_chunks.decrement();131return c;132}133134void add(Metachunk* c) {135assert(contains(c) == false, "Chunk already in freelist");136assert(_first == NULL || _first->level() == c->level(),137"List should only contains chunks of the same level.");138// Uncomitted chunks go to the back, fully or partially committed to the front.139if (c->committed_words() == 0) {140add_back(c);141} else {142add_front(c);143}144_num_chunks.increment();145}146147// Removes the first chunk from the list and returns it. Returns NULL if list is empty.148Metachunk* remove_first() {149Metachunk* c = _first;150if (c != NULL) {151remove(c);152}153return c;154}155156// Returns reference to the first chunk in the list, or NULL157Metachunk* first() const { return _first; }158159// Returns reference to the fist chunk in the list with a committed word160// level >= min_committed_words, or NULL.161Metachunk* first_minimally_committed(size_t min_committed_words) const {162// Since uncommitted chunks are added to the back we can stop looking once163// we encounter a fully uncommitted chunk.164Metachunk* c = first();165while (c != NULL &&166c->committed_words() < min_committed_words &&167c->committed_words() > 0) {168c = c->next();169}170if (c != NULL &&171c->committed_words() >= min_committed_words) {172return c;173}174return NULL;175}176177#ifdef ASSERT178bool contains(const Metachunk* c) const;179void verify() const;180#endif181182// Returns number of chunks183int num_chunks() const { return _num_chunks.get(); }184185// Calculates total number of committed words over all chunks (walks chunks).186size_t calc_committed_word_size() const;187188void print_on(outputStream* st) const;189190};191192// A vector of free chunk lists, one per chunk level193class FreeChunkListVector {194195FreeChunkList _lists[chunklevel::NUM_CHUNK_LEVELS];196197const FreeChunkList* list_for_level(chunklevel_t lvl) const { DEBUG_ONLY(chunklevel::check_valid_level(lvl)); return _lists + lvl; }198FreeChunkList* list_for_level(chunklevel_t lvl) { DEBUG_ONLY(chunklevel::check_valid_level(lvl)); return _lists + lvl; }199200const FreeChunkList* list_for_chunk(const Metachunk* c) const { return list_for_level(c->level()); }201FreeChunkList* list_for_chunk(const Metachunk* c) { return list_for_level(c->level()); }202203public:204205// Remove given chunk from its list. List must contain that chunk.206void remove(Metachunk* c) {207list_for_chunk(c)->remove(c);208}209210// Remove first node unless empty. Returns node or NULL.211Metachunk* remove_first(chunklevel_t lvl) {212Metachunk* c = list_for_level(lvl)->remove_first();213return c;214}215216void add(Metachunk* c) {217list_for_chunk(c)->add(c);218}219220// Returns number of chunks for a given level.221int num_chunks_at_level(chunklevel_t lvl) const {222return list_for_level(lvl)->num_chunks();223}224225// Returns reference to first chunk at this level, or NULL if sublist is empty.226Metachunk* first_at_level(chunklevel_t lvl) const {227return list_for_level(lvl)->first();228}229230// Look for a chunk: starting at level, up to and including max_level,231// return the first chunk whose committed words >= min_committed_words.232// Return NULL if no such chunk was found.233Metachunk* search_chunk_ascending(chunklevel_t level, chunklevel_t max_level,234size_t min_committed_words);235236// Look for a chunk: starting at level, down to (including) the root chunk level,237// return the first chunk whose committed words >= min_committed_words.238// Return NULL if no such chunk was found.239Metachunk* search_chunk_descending(chunklevel_t level, size_t min_committed_words);240241// Returns total size in all lists (including uncommitted areas)242size_t word_size() const;243244// Calculates total number of committed words over all chunks (walks chunks).245size_t calc_committed_word_size_at_level(chunklevel_t lvl) const;246247// Calculates total number of committed words over all chunks (walks chunks).248size_t calc_committed_word_size() const;249250// Returns number of chunks in all lists251int num_chunks() const;252253#ifdef ASSERT254bool contains(const Metachunk* c) const;255void verify() const;256#endif257258void print_on(outputStream* st) const;259260};261262} // namespace metaspace263264#endif // SHARE_MEMORY_METASPACE_FREECHUNKLIST_HPP265266267