src/hotspot/share/memory/metaspace/chunkManager.cpp
author pliden
Wed, 27 Jun 2018 11:05:01 +0200
changeset 50811 f533eb5e7430
parent 50380 bec342339138
child 53970 1ad7c590a6e7
permissions -rw-r--r--
8205664: Move detailed metaspace logging from debug to trace Reviewed-by: stuefe, stefank
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50193
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
     1
/*
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
     4
 *
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
     7
 * published by the Free Software Foundation.
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
     8
 *
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    13
 * accompanied this code).
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    14
 *
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    18
 *
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    21
 * questions.
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    22
 *
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    23
 */
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    24
#include "precompiled.hpp"
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    25
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    26
#include "logging/log.hpp"
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    27
#include "logging/logStream.hpp"
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    28
#include "memory/binaryTreeDictionary.inline.hpp"
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    29
#include "memory/freeList.inline.hpp"
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    30
#include "memory/metaspace/chunkManager.hpp"
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    31
#include "memory/metaspace/metachunk.hpp"
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    32
#include "memory/metaspace/metaspaceCommon.hpp"
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    33
#include "memory/metaspace/metaspaceStatistics.hpp"
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    34
#include "memory/metaspace/occupancyMap.hpp"
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    35
#include "memory/metaspace/virtualSpaceNode.hpp"
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    36
#include "runtime/mutexLocker.hpp"
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    37
#include "utilities/debug.hpp"
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    38
#include "utilities/globalDefinitions.hpp"
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    39
#include "utilities/ostream.hpp"
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    40
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    41
namespace metaspace {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    42
50380
bec342339138 8204195: Clean up macroAssembler.inline.hpp and other inline.hpp files included in .hpp files
coleenp
parents: 50193
diff changeset
    43
ChunkManager::ChunkManager(bool is_class)
bec342339138 8204195: Clean up macroAssembler.inline.hpp and other inline.hpp files included in .hpp files
coleenp
parents: 50193
diff changeset
    44
      : _is_class(is_class), _free_chunks_total(0), _free_chunks_count(0) {
bec342339138 8204195: Clean up macroAssembler.inline.hpp and other inline.hpp files included in .hpp files
coleenp
parents: 50193
diff changeset
    45
  _free_chunks[SpecializedIndex].set_size(get_size_for_nonhumongous_chunktype(SpecializedIndex, is_class));
bec342339138 8204195: Clean up macroAssembler.inline.hpp and other inline.hpp files included in .hpp files
coleenp
parents: 50193
diff changeset
    46
  _free_chunks[SmallIndex].set_size(get_size_for_nonhumongous_chunktype(SmallIndex, is_class));
bec342339138 8204195: Clean up macroAssembler.inline.hpp and other inline.hpp files included in .hpp files
coleenp
parents: 50193
diff changeset
    47
  _free_chunks[MediumIndex].set_size(get_size_for_nonhumongous_chunktype(MediumIndex, is_class));
bec342339138 8204195: Clean up macroAssembler.inline.hpp and other inline.hpp files included in .hpp files
coleenp
parents: 50193
diff changeset
    48
}
bec342339138 8204195: Clean up macroAssembler.inline.hpp and other inline.hpp files included in .hpp files
coleenp
parents: 50193
diff changeset
    49
50193
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    50
void ChunkManager::remove_chunk(Metachunk* chunk) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    51
  size_t word_size = chunk->word_size();
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    52
  ChunkIndex index = list_index(word_size);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    53
  if (index != HumongousIndex) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    54
    free_chunks(index)->remove_chunk(chunk);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    55
  } else {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    56
    humongous_dictionary()->remove_chunk(chunk);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    57
  }
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    58
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    59
  // Chunk has been removed from the chunks free list, update counters.
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    60
  account_for_removed_chunk(chunk);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    61
}
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    62
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    63
bool ChunkManager::attempt_to_coalesce_around_chunk(Metachunk* chunk, ChunkIndex target_chunk_type) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    64
  assert_lock_strong(MetaspaceExpand_lock);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    65
  assert(chunk != NULL, "invalid chunk pointer");
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    66
  // Check for valid merge combinations.
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    67
  assert((chunk->get_chunk_type() == SpecializedIndex &&
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    68
          (target_chunk_type == SmallIndex || target_chunk_type == MediumIndex)) ||
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    69
         (chunk->get_chunk_type() == SmallIndex && target_chunk_type == MediumIndex),
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    70
        "Invalid chunk merge combination.");
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    71
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    72
  const size_t target_chunk_word_size =
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    73
    get_size_for_nonhumongous_chunktype(target_chunk_type, this->is_class());
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    74
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    75
  // [ prospective merge region )
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    76
  MetaWord* const p_merge_region_start =
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    77
    (MetaWord*) align_down(chunk, target_chunk_word_size * sizeof(MetaWord));
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    78
  MetaWord* const p_merge_region_end =
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    79
    p_merge_region_start + target_chunk_word_size;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    80
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    81
  // We need the VirtualSpaceNode containing this chunk and its occupancy map.
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    82
  VirtualSpaceNode* const vsn = chunk->container();
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    83
  OccupancyMap* const ocmap = vsn->occupancy_map();
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    84
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    85
  // The prospective chunk merge range must be completely contained by the
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    86
  // committed range of the virtual space node.
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    87
  if (p_merge_region_start < vsn->bottom() || p_merge_region_end > vsn->top()) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    88
    return false;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    89
  }
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    90
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    91
  // Only attempt to merge this range if at its start a chunk starts and at its end
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    92
  // a chunk ends. If a chunk (can only be humongous) straddles either start or end
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    93
  // of that range, we cannot merge.
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    94
  if (!ocmap->chunk_starts_at_address(p_merge_region_start)) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    95
    return false;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    96
  }
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    97
  if (p_merge_region_end < vsn->top() &&
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    98
      !ocmap->chunk_starts_at_address(p_merge_region_end)) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
    99
    return false;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   100
  }
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   101
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   102
  // Now check if the prospective merge area contains live chunks. If it does we cannot merge.
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   103
  if (ocmap->is_region_in_use(p_merge_region_start, target_chunk_word_size)) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   104
    return false;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   105
  }
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   106
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   107
  // Success! Remove all chunks in this region...
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   108
  log_trace(gc, metaspace, freelist)("%s: coalescing chunks in area [%p-%p)...",
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   109
    (is_class() ? "class space" : "metaspace"),
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   110
    p_merge_region_start, p_merge_region_end);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   111
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   112
  const int num_chunks_removed =
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   113
    remove_chunks_in_area(p_merge_region_start, target_chunk_word_size);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   114
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   115
  // ... and create a single new bigger chunk.
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   116
  Metachunk* const p_new_chunk =
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   117
      ::new (p_merge_region_start) Metachunk(target_chunk_type, is_class(), target_chunk_word_size, vsn);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   118
  assert(p_new_chunk == (Metachunk*)p_merge_region_start, "Sanity");
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   119
  p_new_chunk->set_origin(origin_merge);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   120
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   121
  log_trace(gc, metaspace, freelist)("%s: created coalesced chunk at %p, size " SIZE_FORMAT_HEX ".",
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   122
    (is_class() ? "class space" : "metaspace"),
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   123
    p_new_chunk, p_new_chunk->word_size() * sizeof(MetaWord));
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   124
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   125
  // Fix occupancy map: remove old start bits of the small chunks and set new start bit.
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   126
  ocmap->wipe_chunk_start_bits_in_region(p_merge_region_start, target_chunk_word_size);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   127
  ocmap->set_chunk_starts_at_address(p_merge_region_start, true);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   128
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   129
  // Mark chunk as free. Note: it is not necessary to update the occupancy
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   130
  // map in-use map, because the old chunks were also free, so nothing
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   131
  // should have changed.
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   132
  p_new_chunk->set_is_tagged_free(true);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   133
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   134
  // Add new chunk to its freelist.
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   135
  ChunkList* const list = free_chunks(target_chunk_type);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   136
  list->return_chunk_at_head(p_new_chunk);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   137
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   138
  // And adjust ChunkManager:: _free_chunks_count (_free_chunks_total
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   139
  // should not have changed, because the size of the space should be the same)
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   140
  _free_chunks_count -= num_chunks_removed;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   141
  _free_chunks_count ++;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   142
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   143
  // VirtualSpaceNode::container_count does not have to be modified:
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   144
  // it means "number of active (non-free) chunks", so merging free chunks
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   145
  // should not affect that count.
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   146
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   147
  // At the end of a chunk merge, run verification tests.
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   148
  if (VerifyMetaspace) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   149
    DEBUG_ONLY(this->locked_verify());
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   150
    DEBUG_ONLY(vsn->verify());
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   151
  }
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   152
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   153
  return true;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   154
}
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   155
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   156
// Remove all chunks in the given area - the chunks are supposed to be free -
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   157
// from their corresponding freelists. Mark them as invalid.
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   158
// - This does not correct the occupancy map.
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   159
// - This does not adjust the counters in ChunkManager.
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   160
// - Does not adjust container count counter in containing VirtualSpaceNode
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   161
// Returns number of chunks removed.
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   162
int ChunkManager::remove_chunks_in_area(MetaWord* p, size_t word_size) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   163
  assert(p != NULL && word_size > 0, "Invalid range.");
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   164
  const size_t smallest_chunk_size = get_size_for_nonhumongous_chunktype(SpecializedIndex, is_class());
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   165
  assert_is_aligned(word_size, smallest_chunk_size);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   166
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   167
  Metachunk* const start = (Metachunk*) p;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   168
  const Metachunk* const end = (Metachunk*)(p + word_size);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   169
  Metachunk* cur = start;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   170
  int num_removed = 0;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   171
  while (cur < end) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   172
    Metachunk* next = (Metachunk*)(((MetaWord*)cur) + cur->word_size());
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   173
    DEBUG_ONLY(do_verify_chunk(cur));
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   174
    assert(cur->get_chunk_type() != HumongousIndex, "Unexpected humongous chunk found at %p.", cur);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   175
    assert(cur->is_tagged_free(), "Chunk expected to be free (%p)", cur);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   176
    log_trace(gc, metaspace, freelist)("%s: removing chunk %p, size " SIZE_FORMAT_HEX ".",
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   177
      (is_class() ? "class space" : "metaspace"),
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   178
      cur, cur->word_size() * sizeof(MetaWord));
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   179
    cur->remove_sentinel();
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   180
    // Note: cannot call ChunkManager::remove_chunk, because that
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   181
    // modifies the counters in ChunkManager, which we do not want. So
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   182
    // we call remove_chunk on the freelist directly (see also the
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   183
    // splitting function which does the same).
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   184
    ChunkList* const list = free_chunks(list_index(cur->word_size()));
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   185
    list->remove_chunk(cur);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   186
    num_removed ++;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   187
    cur = next;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   188
  }
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   189
  return num_removed;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   190
}
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   191
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   192
size_t ChunkManager::free_chunks_total_words() {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   193
  return _free_chunks_total;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   194
}
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   195
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   196
size_t ChunkManager::free_chunks_total_bytes() {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   197
  return free_chunks_total_words() * BytesPerWord;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   198
}
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   199
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   200
// Update internal accounting after a chunk was added
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   201
void ChunkManager::account_for_added_chunk(const Metachunk* c) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   202
  assert_lock_strong(MetaspaceExpand_lock);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   203
  _free_chunks_count ++;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   204
  _free_chunks_total += c->word_size();
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   205
}
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   206
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   207
// Update internal accounting after a chunk was removed
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   208
void ChunkManager::account_for_removed_chunk(const Metachunk* c) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   209
  assert_lock_strong(MetaspaceExpand_lock);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   210
  assert(_free_chunks_count >= 1,
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   211
    "ChunkManager::_free_chunks_count: about to go negative (" SIZE_FORMAT ").", _free_chunks_count);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   212
  assert(_free_chunks_total >= c->word_size(),
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   213
    "ChunkManager::_free_chunks_total: about to go negative"
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   214
     "(now: " SIZE_FORMAT ", decrement value: " SIZE_FORMAT ").", _free_chunks_total, c->word_size());
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   215
  _free_chunks_count --;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   216
  _free_chunks_total -= c->word_size();
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   217
}
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   218
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   219
size_t ChunkManager::free_chunks_count() {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   220
#ifdef ASSERT
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   221
  if (!UseConcMarkSweepGC && !MetaspaceExpand_lock->is_locked()) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   222
    MutexLockerEx cl(MetaspaceExpand_lock,
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   223
                     Mutex::_no_safepoint_check_flag);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   224
    // This lock is only needed in debug because the verification
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   225
    // of the _free_chunks_totals walks the list of free chunks
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   226
    slow_locked_verify_free_chunks_count();
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   227
  }
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   228
#endif
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   229
  return _free_chunks_count;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   230
}
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   231
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   232
ChunkIndex ChunkManager::list_index(size_t size) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   233
  return get_chunk_type_by_size(size, is_class());
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   234
}
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   235
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   236
size_t ChunkManager::size_by_index(ChunkIndex index) const {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   237
  index_bounds_check(index);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   238
  assert(index != HumongousIndex, "Do not call for humongous chunks.");
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   239
  return get_size_for_nonhumongous_chunktype(index, is_class());
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   240
}
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   241
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   242
void ChunkManager::locked_verify_free_chunks_total() {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   243
  assert_lock_strong(MetaspaceExpand_lock);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   244
  assert(sum_free_chunks() == _free_chunks_total,
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   245
         "_free_chunks_total " SIZE_FORMAT " is not the"
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   246
         " same as sum " SIZE_FORMAT, _free_chunks_total,
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   247
         sum_free_chunks());
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   248
}
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   249
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   250
void ChunkManager::locked_verify_free_chunks_count() {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   251
  assert_lock_strong(MetaspaceExpand_lock);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   252
  assert(sum_free_chunks_count() == _free_chunks_count,
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   253
         "_free_chunks_count " SIZE_FORMAT " is not the"
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   254
         " same as sum " SIZE_FORMAT, _free_chunks_count,
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   255
         sum_free_chunks_count());
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   256
}
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   257
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   258
void ChunkManager::verify() {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   259
  MutexLockerEx cl(MetaspaceExpand_lock,
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   260
                     Mutex::_no_safepoint_check_flag);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   261
  locked_verify();
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   262
}
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   263
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   264
void ChunkManager::locked_verify() {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   265
  locked_verify_free_chunks_count();
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   266
  locked_verify_free_chunks_total();
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   267
  for (ChunkIndex i = ZeroIndex; i < NumberOfFreeLists; i = next_chunk_index(i)) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   268
    ChunkList* list = free_chunks(i);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   269
    if (list != NULL) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   270
      Metachunk* chunk = list->head();
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   271
      while (chunk) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   272
        DEBUG_ONLY(do_verify_chunk(chunk);)
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   273
        assert(chunk->is_tagged_free(), "Chunk should be tagged as free.");
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   274
        chunk = chunk->next();
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   275
      }
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   276
    }
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   277
  }
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   278
}
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   279
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   280
void ChunkManager::locked_print_free_chunks(outputStream* st) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   281
  assert_lock_strong(MetaspaceExpand_lock);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   282
  st->print_cr("Free chunk total " SIZE_FORMAT "  count " SIZE_FORMAT,
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   283
                _free_chunks_total, _free_chunks_count);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   284
}
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   285
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   286
void ChunkManager::locked_print_sum_free_chunks(outputStream* st) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   287
  assert_lock_strong(MetaspaceExpand_lock);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   288
  st->print_cr("Sum free chunk total " SIZE_FORMAT "  count " SIZE_FORMAT,
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   289
                sum_free_chunks(), sum_free_chunks_count());
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   290
}
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   291
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   292
ChunkList* ChunkManager::free_chunks(ChunkIndex index) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   293
  assert(index == SpecializedIndex || index == SmallIndex || index == MediumIndex,
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   294
         "Bad index: %d", (int)index);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   295
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   296
  return &_free_chunks[index];
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   297
}
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   298
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   299
// These methods that sum the free chunk lists are used in printing
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   300
// methods that are used in product builds.
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   301
size_t ChunkManager::sum_free_chunks() {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   302
  assert_lock_strong(MetaspaceExpand_lock);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   303
  size_t result = 0;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   304
  for (ChunkIndex i = ZeroIndex; i < NumberOfFreeLists; i = next_chunk_index(i)) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   305
    ChunkList* list = free_chunks(i);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   306
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   307
    if (list == NULL) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   308
      continue;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   309
    }
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   310
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   311
    result = result + list->count() * list->size();
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   312
  }
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   313
  result = result + humongous_dictionary()->total_size();
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   314
  return result;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   315
}
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   316
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   317
size_t ChunkManager::sum_free_chunks_count() {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   318
  assert_lock_strong(MetaspaceExpand_lock);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   319
  size_t count = 0;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   320
  for (ChunkIndex i = ZeroIndex; i < NumberOfFreeLists; i = next_chunk_index(i)) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   321
    ChunkList* list = free_chunks(i);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   322
    if (list == NULL) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   323
      continue;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   324
    }
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   325
    count = count + list->count();
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   326
  }
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   327
  count = count + humongous_dictionary()->total_free_blocks();
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   328
  return count;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   329
}
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   330
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   331
ChunkList* ChunkManager::find_free_chunks_list(size_t word_size) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   332
  ChunkIndex index = list_index(word_size);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   333
  assert(index < HumongousIndex, "No humongous list");
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   334
  return free_chunks(index);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   335
}
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   336
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   337
// Helper for chunk splitting: given a target chunk size and a larger free chunk,
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   338
// split up the larger chunk into n smaller chunks, at least one of which should be
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   339
// the target chunk of target chunk size. The smaller chunks, including the target
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   340
// chunk, are returned to the freelist. The pointer to the target chunk is returned.
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   341
// Note that this chunk is supposed to be removed from the freelist right away.
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   342
Metachunk* ChunkManager::split_chunk(size_t target_chunk_word_size, Metachunk* larger_chunk) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   343
  assert(larger_chunk->word_size() > target_chunk_word_size, "Sanity");
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   344
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   345
  const ChunkIndex larger_chunk_index = larger_chunk->get_chunk_type();
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   346
  const ChunkIndex target_chunk_index = get_chunk_type_by_size(target_chunk_word_size, is_class());
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   347
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   348
  MetaWord* const region_start = (MetaWord*)larger_chunk;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   349
  const size_t region_word_len = larger_chunk->word_size();
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   350
  MetaWord* const region_end = region_start + region_word_len;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   351
  VirtualSpaceNode* const vsn = larger_chunk->container();
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   352
  OccupancyMap* const ocmap = vsn->occupancy_map();
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   353
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   354
  // Any larger non-humongous chunk size is a multiple of any smaller chunk size.
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   355
  // Since non-humongous chunks are aligned to their chunk size, the larger chunk should start
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   356
  // at an address suitable to place the smaller target chunk.
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   357
  assert_is_aligned(region_start, target_chunk_word_size);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   358
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   359
  // Remove old chunk.
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   360
  free_chunks(larger_chunk_index)->remove_chunk(larger_chunk);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   361
  larger_chunk->remove_sentinel();
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   362
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   363
  // Prevent access to the old chunk from here on.
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   364
  larger_chunk = NULL;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   365
  // ... and wipe it.
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   366
  DEBUG_ONLY(memset(region_start, 0xfe, region_word_len * BytesPerWord));
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   367
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   368
  // In its place create first the target chunk...
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   369
  MetaWord* p = region_start;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   370
  Metachunk* target_chunk = ::new (p) Metachunk(target_chunk_index, is_class(), target_chunk_word_size, vsn);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   371
  assert(target_chunk == (Metachunk*)p, "Sanity");
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   372
  target_chunk->set_origin(origin_split);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   373
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   374
  // Note: we do not need to mark its start in the occupancy map
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   375
  // because it coincides with the old chunk start.
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   376
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   377
  // Mark chunk as free and return to the freelist.
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   378
  do_update_in_use_info_for_chunk(target_chunk, false);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   379
  free_chunks(target_chunk_index)->return_chunk_at_head(target_chunk);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   380
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   381
  // This chunk should now be valid and can be verified.
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   382
  DEBUG_ONLY(do_verify_chunk(target_chunk));
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   383
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   384
  // In the remaining space create the remainder chunks.
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   385
  p += target_chunk->word_size();
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   386
  assert(p < region_end, "Sanity");
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   387
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   388
  while (p < region_end) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   389
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   390
    // Find the largest chunk size which fits the alignment requirements at address p.
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   391
    ChunkIndex this_chunk_index = prev_chunk_index(larger_chunk_index);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   392
    size_t this_chunk_word_size = 0;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   393
    for(;;) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   394
      this_chunk_word_size = get_size_for_nonhumongous_chunktype(this_chunk_index, is_class());
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   395
      if (is_aligned(p, this_chunk_word_size * BytesPerWord)) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   396
        break;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   397
      } else {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   398
        this_chunk_index = prev_chunk_index(this_chunk_index);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   399
        assert(this_chunk_index >= target_chunk_index, "Sanity");
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   400
      }
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   401
    }
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   402
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   403
    assert(this_chunk_word_size >= target_chunk_word_size, "Sanity");
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   404
    assert(is_aligned(p, this_chunk_word_size * BytesPerWord), "Sanity");
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   405
    assert(p + this_chunk_word_size <= region_end, "Sanity");
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   406
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   407
    // Create splitting chunk.
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   408
    Metachunk* this_chunk = ::new (p) Metachunk(this_chunk_index, is_class(), this_chunk_word_size, vsn);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   409
    assert(this_chunk == (Metachunk*)p, "Sanity");
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   410
    this_chunk->set_origin(origin_split);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   411
    ocmap->set_chunk_starts_at_address(p, true);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   412
    do_update_in_use_info_for_chunk(this_chunk, false);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   413
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   414
    // This chunk should be valid and can be verified.
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   415
    DEBUG_ONLY(do_verify_chunk(this_chunk));
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   416
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   417
    // Return this chunk to freelist and correct counter.
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   418
    free_chunks(this_chunk_index)->return_chunk_at_head(this_chunk);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   419
    _free_chunks_count ++;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   420
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   421
    log_trace(gc, metaspace, freelist)("Created chunk at " PTR_FORMAT ", word size "
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   422
      SIZE_FORMAT_HEX " (%s), in split region [" PTR_FORMAT "..." PTR_FORMAT ").",
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   423
      p2i(this_chunk), this_chunk->word_size(), chunk_size_name(this_chunk_index),
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   424
      p2i(region_start), p2i(region_end));
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   425
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   426
    p += this_chunk_word_size;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   427
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   428
  }
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   429
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   430
  return target_chunk;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   431
}
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   432
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   433
Metachunk* ChunkManager::free_chunks_get(size_t word_size) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   434
  assert_lock_strong(MetaspaceExpand_lock);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   435
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   436
  slow_locked_verify();
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   437
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   438
  Metachunk* chunk = NULL;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   439
  bool we_did_split_a_chunk = false;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   440
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   441
  if (list_index(word_size) != HumongousIndex) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   442
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   443
    ChunkList* free_list = find_free_chunks_list(word_size);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   444
    assert(free_list != NULL, "Sanity check");
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   445
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   446
    chunk = free_list->head();
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   447
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   448
    if (chunk == NULL) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   449
      // Split large chunks into smaller chunks if there are no smaller chunks, just large chunks.
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   450
      // This is the counterpart of the coalescing-upon-chunk-return.
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   451
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   452
      ChunkIndex target_chunk_index = get_chunk_type_by_size(word_size, is_class());
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   453
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   454
      // Is there a larger chunk we could split?
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   455
      Metachunk* larger_chunk = NULL;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   456
      ChunkIndex larger_chunk_index = next_chunk_index(target_chunk_index);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   457
      while (larger_chunk == NULL && larger_chunk_index < NumberOfFreeLists) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   458
        larger_chunk = free_chunks(larger_chunk_index)->head();
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   459
        if (larger_chunk == NULL) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   460
          larger_chunk_index = next_chunk_index(larger_chunk_index);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   461
        }
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   462
      }
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   463
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   464
      if (larger_chunk != NULL) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   465
        assert(larger_chunk->word_size() > word_size, "Sanity");
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   466
        assert(larger_chunk->get_chunk_type() == larger_chunk_index, "Sanity");
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   467
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   468
        // We found a larger chunk. Lets split it up:
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   469
        // - remove old chunk
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   470
        // - in its place, create new smaller chunks, with at least one chunk
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   471
        //   being of target size, the others sized as large as possible. This
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   472
        //   is to make sure the resulting chunks are "as coalesced as possible"
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   473
        //   (similar to VirtualSpaceNode::retire()).
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   474
        // Note: during this operation both ChunkManager and VirtualSpaceNode
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   475
        //  are temporarily invalid, so be careful with asserts.
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   476
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   477
        log_trace(gc, metaspace, freelist)("%s: splitting chunk " PTR_FORMAT
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   478
           ", word size " SIZE_FORMAT_HEX " (%s), to get a chunk of word size " SIZE_FORMAT_HEX " (%s)...",
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   479
          (is_class() ? "class space" : "metaspace"), p2i(larger_chunk), larger_chunk->word_size(),
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   480
          chunk_size_name(larger_chunk_index), word_size, chunk_size_name(target_chunk_index));
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   481
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   482
        chunk = split_chunk(word_size, larger_chunk);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   483
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   484
        // This should have worked.
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   485
        assert(chunk != NULL, "Sanity");
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   486
        assert(chunk->word_size() == word_size, "Sanity");
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   487
        assert(chunk->is_tagged_free(), "Sanity");
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   488
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   489
        we_did_split_a_chunk = true;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   490
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   491
      }
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   492
    }
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   493
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   494
    if (chunk == NULL) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   495
      return NULL;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   496
    }
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   497
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   498
    // Remove the chunk as the head of the list.
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   499
    free_list->remove_chunk(chunk);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   500
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   501
    log_trace(gc, metaspace, freelist)("ChunkManager::free_chunks_get: free_list: " PTR_FORMAT " chunks left: " SSIZE_FORMAT ".",
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   502
                                       p2i(free_list), free_list->count());
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   503
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   504
  } else {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   505
    chunk = humongous_dictionary()->get_chunk(word_size);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   506
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   507
    if (chunk == NULL) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   508
      return NULL;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   509
    }
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   510
50811
f533eb5e7430 8205664: Move detailed metaspace logging from debug to trace
pliden
parents: 50380
diff changeset
   511
    log_trace(gc, metaspace, alloc)("Free list allocate humongous chunk size " SIZE_FORMAT " for requested size " SIZE_FORMAT " waste " SIZE_FORMAT,
50193
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   512
                                    chunk->word_size(), word_size, chunk->word_size() - word_size);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   513
  }
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   514
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   515
  // Chunk has been removed from the chunk manager; update counters.
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   516
  account_for_removed_chunk(chunk);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   517
  do_update_in_use_info_for_chunk(chunk, true);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   518
  chunk->container()->inc_container_count();
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   519
  chunk->inc_use_count();
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   520
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   521
  // Remove it from the links to this freelist
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   522
  chunk->set_next(NULL);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   523
  chunk->set_prev(NULL);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   524
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   525
  // Run some verifications (some more if we did a chunk split)
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   526
#ifdef ASSERT
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   527
  if (VerifyMetaspace) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   528
    locked_verify();
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   529
    VirtualSpaceNode* const vsn = chunk->container();
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   530
    vsn->verify();
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   531
    if (we_did_split_a_chunk) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   532
      vsn->verify_free_chunks_are_ideally_merged();
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   533
    }
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   534
  }
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   535
#endif
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   536
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   537
  return chunk;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   538
}
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   539
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   540
Metachunk* ChunkManager::chunk_freelist_allocate(size_t word_size) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   541
  assert_lock_strong(MetaspaceExpand_lock);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   542
  slow_locked_verify();
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   543
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   544
  // Take from the beginning of the list
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   545
  Metachunk* chunk = free_chunks_get(word_size);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   546
  if (chunk == NULL) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   547
    return NULL;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   548
  }
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   549
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   550
  assert((word_size <= chunk->word_size()) ||
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   551
         (list_index(chunk->word_size()) == HumongousIndex),
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   552
         "Non-humongous variable sized chunk");
50811
f533eb5e7430 8205664: Move detailed metaspace logging from debug to trace
pliden
parents: 50380
diff changeset
   553
  LogTarget(Trace, gc, metaspace, freelist) lt;
50193
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   554
  if (lt.is_enabled()) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   555
    size_t list_count;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   556
    if (list_index(word_size) < HumongousIndex) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   557
      ChunkList* list = find_free_chunks_list(word_size);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   558
      list_count = list->count();
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   559
    } else {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   560
      list_count = humongous_dictionary()->total_count();
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   561
    }
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   562
    LogStream ls(lt);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   563
    ls.print("ChunkManager::chunk_freelist_allocate: " PTR_FORMAT " chunk " PTR_FORMAT "  size " SIZE_FORMAT " count " SIZE_FORMAT " ",
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   564
             p2i(this), p2i(chunk), chunk->word_size(), list_count);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   565
    ResourceMark rm;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   566
    locked_print_free_chunks(&ls);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   567
  }
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   568
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   569
  return chunk;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   570
}
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   571
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   572
void ChunkManager::return_single_chunk(Metachunk* chunk) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   573
  const ChunkIndex index = chunk->get_chunk_type();
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   574
  assert_lock_strong(MetaspaceExpand_lock);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   575
  DEBUG_ONLY(do_verify_chunk(chunk);)
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   576
  assert(chunk != NULL, "Expected chunk.");
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   577
  assert(chunk->container() != NULL, "Container should have been set.");
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   578
  assert(chunk->is_tagged_free() == false, "Chunk should be in use.");
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   579
  index_bounds_check(index);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   580
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   581
  // Note: mangle *before* returning the chunk to the freelist or dictionary. It does not
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   582
  // matter for the freelist (non-humongous chunks), but the humongous chunk dictionary
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   583
  // keeps tree node pointers in the chunk payload area which mangle will overwrite.
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   584
  DEBUG_ONLY(chunk->mangle(badMetaWordVal);)
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   585
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   586
  if (index != HumongousIndex) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   587
    // Return non-humongous chunk to freelist.
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   588
    ChunkList* list = free_chunks(index);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   589
    assert(list->size() == chunk->word_size(), "Wrong chunk type.");
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   590
    list->return_chunk_at_head(chunk);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   591
    log_trace(gc, metaspace, freelist)("returned one %s chunk at " PTR_FORMAT " to freelist.",
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   592
        chunk_size_name(index), p2i(chunk));
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   593
  } else {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   594
    // Return humongous chunk to dictionary.
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   595
    assert(chunk->word_size() > free_chunks(MediumIndex)->size(), "Wrong chunk type.");
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   596
    assert(chunk->word_size() % free_chunks(SpecializedIndex)->size() == 0,
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   597
           "Humongous chunk has wrong alignment.");
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   598
    _humongous_dictionary.return_chunk(chunk);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   599
    log_trace(gc, metaspace, freelist)("returned one %s chunk at " PTR_FORMAT " (word size " SIZE_FORMAT ") to freelist.",
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   600
        chunk_size_name(index), p2i(chunk), chunk->word_size());
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   601
  }
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   602
  chunk->container()->dec_container_count();
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   603
  do_update_in_use_info_for_chunk(chunk, false);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   604
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   605
  // Chunk has been added; update counters.
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   606
  account_for_added_chunk(chunk);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   607
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   608
  // Attempt coalesce returned chunks with its neighboring chunks:
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   609
  // if this chunk is small or special, attempt to coalesce to a medium chunk.
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   610
  if (index == SmallIndex || index == SpecializedIndex) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   611
    if (!attempt_to_coalesce_around_chunk(chunk, MediumIndex)) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   612
      // This did not work. But if this chunk is special, we still may form a small chunk?
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   613
      if (index == SpecializedIndex) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   614
        if (!attempt_to_coalesce_around_chunk(chunk, SmallIndex)) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   615
          // give up.
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   616
        }
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   617
      }
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   618
    }
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   619
  }
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   620
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   621
}
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   622
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   623
void ChunkManager::return_chunk_list(Metachunk* chunks) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   624
  if (chunks == NULL) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   625
    return;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   626
  }
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   627
  LogTarget(Trace, gc, metaspace, freelist) log;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   628
  if (log.is_enabled()) { // tracing
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   629
    log.print("returning list of chunks...");
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   630
  }
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   631
  unsigned num_chunks_returned = 0;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   632
  size_t size_chunks_returned = 0;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   633
  Metachunk* cur = chunks;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   634
  while (cur != NULL) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   635
    // Capture the next link before it is changed
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   636
    // by the call to return_chunk_at_head();
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   637
    Metachunk* next = cur->next();
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   638
    if (log.is_enabled()) { // tracing
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   639
      num_chunks_returned ++;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   640
      size_chunks_returned += cur->word_size();
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   641
    }
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   642
    return_single_chunk(cur);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   643
    cur = next;
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   644
  }
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   645
  if (log.is_enabled()) { // tracing
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   646
    log.print("returned %u chunks to freelist, total word size " SIZE_FORMAT ".",
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   647
        num_chunks_returned, size_chunks_returned);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   648
  }
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   649
}
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   650
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   651
void ChunkManager::collect_statistics(ChunkManagerStatistics* out) const {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   652
  MutexLockerEx cl(MetaspaceExpand_lock, Mutex::_no_safepoint_check_flag);
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   653
  for (ChunkIndex i = ZeroIndex; i < NumberOfInUseLists; i = next_chunk_index(i)) {
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   654
    out->chunk_stats(i).add(num_free_chunks(i), size_free_chunks_in_bytes(i) / sizeof(MetaWord));
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   655
  }
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   656
}
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   657
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   658
} // namespace metaspace
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   659
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   660
49c3e91c424f 8176808: Split up metaspace.cpp
stuefe
parents:
diff changeset
   661