--- /dev/null 2020-09-04 12:37:41.765504620 +0200 +++ new/src/hotspot/share/memory/metaspace/msFreeChunkList.cpp 2020-09-04 13:57:55.193383693 +0200 @@ -0,0 +1,175 @@ +/* + * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2020 SAP SE. 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 "memory/metaspace/msFreeChunkList.hpp" +#include "utilities/globalDefinitions.hpp" +#include "utilities/debug.hpp" +#include "utilities/ostream.hpp" + +namespace metaspace { + +void FreeChunkList::print_on(outputStream* st) const { + + if (_num_chunks.get() > 0) { + for (const Metachunk* c = _first; c != NULL; c = c->next()) { + st->print(" - <"); + c->print_on(st); + st->print(">"); + } + st->print(" - total : %d chunks.", _num_chunks.get()); + } else { + st->print("empty"); + } + +} + +#ifdef ASSERT + +bool FreeChunkList::contains(const Metachunk* c) const { + for (Metachunk* c2 = _first; c2 != NULL; c2 = c2->next()) { + if (c2 == c) { + return true; + } + } + return false; +} + +void FreeChunkList::verify() const { + + if (_first == NULL) { + assert(_last == NULL, "Sanity"); + } else { + assert(_last != NULL, "Sanity"); + size_t committed = 0; + int num = 0; + bool uncommitted = (_first->committed_words() == 0); + for (Metachunk* c = _first; c != NULL; c = c->next()) { + assert(c->is_free(), "Chunks in freelist should be free"); + assert(c->used_words() == 0, "Chunk in freelist should have not used words."); + assert(c->level() == _first->level(), "wrong level"); + assert(c->next() == NULL || c->next()->prev() == c, "front link broken"); + assert(c->prev() == NULL || c->prev()->next() == c, "back link broken"); + assert(c != c->prev() && c != c->next(), "circle"); + c->verify(); + committed += c->committed_words(); + num++; + } + _num_chunks.check(num); + _committed_word_size.check(committed); + } + +} + +#endif // ASSERT + +// Returns total size in all lists (regardless of commit state of underlying memory) +size_t FreeChunkListVector::word_size() const { + size_t sum = 0; + for (chunklevel_t l = chunklevel::LOWEST_CHUNK_LEVEL; l <= chunklevel::HIGHEST_CHUNK_LEVEL; l++) { + sum += list_for_level(l)->num_chunks() * chunklevel::word_size_for_level(l); + } + return sum; +} + +// Returns total committed size in all lists +size_t FreeChunkListVector::committed_word_size() const { + size_t sum = 0; + for (chunklevel_t l = chunklevel::LOWEST_CHUNK_LEVEL; l <= chunklevel::HIGHEST_CHUNK_LEVEL; l++) { + sum += list_for_level(l)->committed_word_size(); + } + return sum; +} + +// Returns total committed size in all lists +int FreeChunkListVector::num_chunks() const { + int n = 0; + for (chunklevel_t l = chunklevel::LOWEST_CHUNK_LEVEL; l <= chunklevel::HIGHEST_CHUNK_LEVEL; l++) { + n += list_for_level(l)->num_chunks(); + } + return n; +} + +// Look for a chunk: starting at level, up to and including max_level, +// return the first chunk whose committed words >= min_committed_words. +// Return NULL if no such chunk was found. +Metachunk* FreeChunkListVector::search_chunk_ascending(chunklevel_t level, chunklevel_t max_level, size_t min_committed_words) { + assert(min_committed_words <= chunklevel::word_size_for_level(max_level), + "min chunk size too small to hold min_committed_words"); + for (chunklevel_t l = level; l <= max_level; l++) { + Metachunk* c = list_for_level(l)->first(); + if (c != NULL && c->committed_words() >= min_committed_words) { + list_for_level(l)->remove(c); + return c; + } + } + return NULL; +} + +// Look for a chunk: starting at level, down to (including) the root chunk level, +// return the first chunk whose committed words >= min_committed_words. +// Return NULL if no such chunk was found. +Metachunk* FreeChunkListVector::search_chunk_descending(chunklevel_t level, size_t min_committed_words) { + for (chunklevel_t l = level; l >= chunklevel::LOWEST_CHUNK_LEVEL; l --) { + Metachunk* c = list_for_level(l)->first(); + if (c != NULL && c->committed_words() >= min_committed_words) { + list_for_level(l)->remove(c); + return c; + } + } + return NULL; +} + +void FreeChunkListVector::print_on(outputStream* st) const { + for (chunklevel_t l = chunklevel::LOWEST_CHUNK_LEVEL; l <= chunklevel::HIGHEST_CHUNK_LEVEL; l++) { + st->print("-- List[" CHKLVL_FORMAT "]: ", l); + list_for_level(l)->print_on(st); + st->cr(); + } + st->print_cr("total chunks: %d, total word size: " SIZE_FORMAT ", committed word size: " SIZE_FORMAT ".", + num_chunks(), word_size(), committed_word_size()); +} + +#ifdef ASSERT + +void FreeChunkListVector::verify() const { + for (chunklevel_t l = chunklevel::LOWEST_CHUNK_LEVEL; l <= chunklevel::HIGHEST_CHUNK_LEVEL; l++) { + list_for_level(l)->verify(); + } +} + +bool FreeChunkListVector::contains(const Metachunk* c) const { + for (chunklevel_t l = chunklevel::LOWEST_CHUNK_LEVEL; l <= chunklevel::HIGHEST_CHUNK_LEVEL; l++) { + if (list_for_level(l)->contains(c)) { + return true; + } + } + return false; +} + +#endif // ASSERT + +} // namespace metaspace +