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