src/hotspot/share/memory/heap.cpp
author lucy
Wed, 20 Nov 2019 09:12:07 +0100
changeset 59145 ea044aedc2b6
parent 54943 6cbb5c2255e3
permissions -rw-r--r--
8231460: Performance issue (CodeHeap) with large free blocks Reviewed-by: adinn, stuefe
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
54943
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
     2
 * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 4493
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 4493
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 4493
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    25
#include "precompiled.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    26
#include "memory/heap.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    27
#include "oops/oop.inline.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    28
#include "runtime/os.hpp"
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 8921
diff changeset
    29
#include "services/memTracker.hpp"
46625
edefffab74e2 8183552: Move align functions to align.hpp
stefank
parents: 46619
diff changeset
    30
#include "utilities/align.hpp"
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
size_t CodeHeap::header_size() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
  return sizeof(HeapBlock);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
// Implementation of Heap
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
26796
666464578742 8015774: Add support for multiple code heaps
thartmann
parents: 25366
diff changeset
    39
CodeHeap::CodeHeap(const char* name, const int code_blob_type)
666464578742 8015774: Add support for multiple code heaps
thartmann
parents: 25366
diff changeset
    40
  : _code_blob_type(code_blob_type) {
666464578742 8015774: Add support for multiple code heaps
thartmann
parents: 25366
diff changeset
    41
  _name                         = name;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
  _number_of_committed_segments = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
  _number_of_reserved_segments  = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
  _segment_size                 = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
  _log2_segment_size            = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
  _next_segment                 = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
  _freelist                     = NULL;
59145
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
    48
  _last_insert_point            = NULL;
17016
78b1c3670525 8006952: Slow VM due to excessive code cache freelist iteration
neliasso
parents: 16670
diff changeset
    49
  _freelist_segments            = 0;
23214
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
    50
  _freelist_length              = 0;
26796
666464578742 8015774: Add support for multiple code heaps
thartmann
parents: 25366
diff changeset
    51
  _max_allocated_capacity       = 0;
34158
1f8d643b02d5 8067378: Add segmented code heaps info into jfr events: vm/code_cache/stats and vm/code_cache/config
thartmann
parents: 29580
diff changeset
    52
  _blob_count                   = 0;
1f8d643b02d5 8067378: Add segmented code heaps info into jfr events: vm/code_cache/stats and vm/code_cache/config
thartmann
parents: 29580
diff changeset
    53
  _nmethod_count                = 0;
1f8d643b02d5 8067378: Add segmented code heaps info into jfr events: vm/code_cache/stats and vm/code_cache/config
thartmann
parents: 29580
diff changeset
    54
  _adapter_count                = 0;
1f8d643b02d5 8067378: Add segmented code heaps info into jfr events: vm/code_cache/stats and vm/code_cache/config
thartmann
parents: 29580
diff changeset
    55
  _full_count                   = 0;
59145
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
    56
  _fragmentation_count          = 0;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
59145
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
    59
// Dummy initialization of template array.
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
    60
char CodeHeap::segmap_template[] = {0};
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
    61
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
    62
// This template array is used to (re)initialize the segmap,
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
    63
// replacing a 1..254 loop.
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
    64
void CodeHeap::init_segmap_template() {
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
    65
  assert(free_sentinel == 255, "Segment map logic changed!");
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
    66
  for (int i = 0; i <= free_sentinel; i++) {
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
    67
    segmap_template[i] = i;
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
    68
  }
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
    69
}
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
54943
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
    71
// The segmap is marked free for that part of the heap
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
    72
// which has not been allocated yet (beyond _next_segment).
59145
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
    73
// The range of segments to be marked is given by [beg..end).
54943
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
    74
// "Allocated" space in this context means there exists a
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
    75
// HeapBlock or a FreeBlock describing this space.
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
    76
// This method takes segment map indices as range boundaries
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
void CodeHeap::mark_segmap_as_free(size_t beg, size_t end) {
54943
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
    78
  assert(             beg <  _number_of_committed_segments, "interval begin out of bounds");
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
    79
  assert(beg < end && end <= _number_of_committed_segments, "interval end   out of bounds");
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
    80
  // Don't do unpredictable things in PRODUCT build
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
    81
  if (beg < end) {
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
    82
    // setup _segmap pointers for faster indexing
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
    83
    address p = (address)_segmap.low() + beg;
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
    84
    address q = (address)_segmap.low() + end;
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
    85
    // initialize interval
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
    86
    memset(p, free_sentinel, q-p);
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
    87
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
54943
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
    90
// Don't get confused here.
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
    91
// All existing blocks, no matter if they are used() or free(),
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
    92
// have their segmap marked as used. This allows to find the
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
    93
// block header (HeapBlock or FreeBlock) for any pointer
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
    94
// within the allocated range (upper limit: _next_segment).
59145
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
    95
// This method takes segment map indices as range boundaries.
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
    96
// The range of segments to be marked is given by [beg..end).
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
    97
void CodeHeap::mark_segmap_as_used(size_t beg, size_t end, bool is_FreeBlock_join) {
54943
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
    98
  assert(             beg <  _number_of_committed_segments, "interval begin out of bounds");
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
    99
  assert(beg < end && end <= _number_of_committed_segments, "interval end   out of bounds");
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   100
  // Don't do unpredictable things in PRODUCT build
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   101
  if (beg < end) {
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   102
    // setup _segmap pointers for faster indexing
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   103
    address p = (address)_segmap.low() + beg;
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   104
    address q = (address)_segmap.low() + end;
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   105
    // initialize interval
59145
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   106
    // If we are joining two free blocks, the segmap range for each
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   107
    // block is consistent. To create a consistent segmap range for
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   108
    // the blocks combined, we have three choices:
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   109
    //  1 - Do a full init from beg to end. Not very efficient because
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   110
    //      the segmap range for the left block is potentially initialized
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   111
    //      over and over again.
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   112
    //  2 - Carry over the last segmap element value of the left block
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   113
    //      and initialize the segmap range of the right block starting
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   114
    //      with that value. Saves initializing the left block's segmap
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   115
    //      over and over again. Very efficient if FreeBlocks mostly
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   116
    //      are appended to the right.
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   117
    //  3 - Take full advantage of the segmap being almost correct with
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   118
    //      the two blocks combined. Lets assume the left block consists
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   119
    //      of m segments. The the segmap looks like
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   120
    //        ... (m-2) (m-1) (m) 0  1  2  3 ...
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   121
    //      By substituting the '0' by '1', we create a valid, but
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   122
    //      suboptimal, segmap range covering the two blocks combined.
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   123
    //      We introduced an extra hop for the find_block_for() iteration.
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   124
    //
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   125
    // When this method is called with is_FreeBlock_join == true, the
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   126
    // segmap index beg must select the first segment of the right block.
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   127
    // Otherwise, it has to select the first segment of the left block.
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   128
    // Variant 3 is used for all FreeBlock joins.
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   129
    if (is_FreeBlock_join && (beg > 0)) {
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   130
#ifndef PRODUCT
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   131
      FreeBlock* pBlock = (FreeBlock*)block_at(beg);
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   132
      assert(beg + pBlock->length() == end, "Internal error: (%d - %d) != %d", (unsigned int)end, (unsigned int)beg, (unsigned int)(pBlock->length()));
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   133
      assert(*p == 0, "Begin index does not select a block start segment, *p = %2.2x", *p);
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   134
#endif
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   135
      // If possible, extend the previous hop.
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   136
      if (*(p-1) < (free_sentinel-1)) {
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   137
        *p = *(p-1) + 1;
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   138
      } else {
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   139
        *p = 1;
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   140
      }
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   141
      if (_fragmentation_count++ >= fragmentation_limit) {
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   142
        defrag_segmap(true);
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   143
        _fragmentation_count = 0;
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   144
      }
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   145
    } else {
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   146
      size_t n_bulk = free_sentinel-1; // bulk processing uses template indices [1..254].
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   147
      // Use shortcut for blocks <= 255 segments.
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   148
      // Special case bulk processing: [0..254].
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   149
      if ((end - beg) <= n_bulk) {
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   150
        memcpy(p, &segmap_template[0], end - beg);
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   151
      } else {
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   152
        *p++  = 0;  // block header marker
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   153
        while (p < q) {
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   154
          if ((p+n_bulk) <= q) {
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   155
            memcpy(p, &segmap_template[1], n_bulk);
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   156
            p += n_bulk;
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   157
          } else {
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   158
            memcpy(p, &segmap_template[1], q-p);
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   159
            p = q;
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   160
          }
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   161
        }
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   162
      }
54943
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   163
    }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
54943
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   167
void CodeHeap::invalidate(size_t beg, size_t end, size_t hdr_size) {
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   168
#ifndef PRODUCT
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   169
  // Fill the given range with some bad value.
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   170
  // length is expected to be in segment_size units.
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   171
  // This prevents inadvertent execution of code leftover from previous use.
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   172
  char* p = low_boundary() + segments_to_size(beg) + hdr_size;
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   173
  memset(p, badCodeHeapNewVal, segments_to_size(end-beg)-hdr_size);
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   174
#endif
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   175
}
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   176
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   177
void CodeHeap::clear(size_t beg, size_t end) {
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   178
  mark_segmap_as_free(beg, end);
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   179
  invalidate(beg, end, 0);
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   180
}
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   181
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   182
void CodeHeap::clear() {
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   183
  _next_segment = 0;
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   184
  clear(_next_segment, _number_of_committed_segments);
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   185
}
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   186
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
static size_t align_to_page_size(size_t size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
  const size_t alignment = (size_t)os::vm_page_size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
  assert(is_power_of_2(alignment), "no kidding ???");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
  return (size + alignment - 1) & ~(alignment - 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
void CodeHeap::on_code_mapping(char* base, size_t size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
#ifdef LINUX
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
  extern void linux_wrap_code(char* base, size_t size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
  linux_wrap_code(base, size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
26796
666464578742 8015774: Add support for multiple code heaps
thartmann
parents: 25366
diff changeset
   203
bool CodeHeap::reserve(ReservedSpace rs, size_t committed_size, size_t segment_size) {
666464578742 8015774: Add support for multiple code heaps
thartmann
parents: 25366
diff changeset
   204
  assert(rs.size() >= committed_size, "reserved < committed");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
  assert(segment_size >= sizeof(FreeBlock), "segment size is too small");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
  assert(is_power_of_2(segment_size), "segment_size must be a power of 2");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
  _segment_size      = segment_size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
  _log2_segment_size = exact_log2(segment_size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
  // Reserve and initialize space for _memory.
26700
8107d0778244 8049864: TestParallelHeapSizeFlags fails with unexpected heap size
ehelin
parents: 25366
diff changeset
   212
  size_t page_size = os::vm_page_size();
8107d0778244 8049864: TestParallelHeapSizeFlags fails with unexpected heap size
ehelin
parents: 25366
diff changeset
   213
  if (os::can_execute_large_page_memory()) {
8107d0778244 8049864: TestParallelHeapSizeFlags fails with unexpected heap size
ehelin
parents: 25366
diff changeset
   214
    const size_t min_pages = 8;
28631
a56cae1b428d 8066875: VirtualSpace does not use large pages
ehelin
parents: 27420
diff changeset
   215
    page_size = MIN2(os::page_size_for_region_aligned(committed_size, min_pages),
a56cae1b428d 8066875: VirtualSpace does not use large pages
ehelin
parents: 27420
diff changeset
   216
                     os::page_size_for_region_aligned(rs.size(), min_pages));
26700
8107d0778244 8049864: TestParallelHeapSizeFlags fails with unexpected heap size
ehelin
parents: 25366
diff changeset
   217
  }
8107d0778244 8049864: TestParallelHeapSizeFlags fails with unexpected heap size
ehelin
parents: 25366
diff changeset
   218
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
  const size_t granularity = os::vm_allocation_granularity();
46619
a3919f5e8d2b 8178499: Remove _ptr_ and _size_ infixes from align functions
stefank
parents: 43945
diff changeset
   220
  const size_t c_size = align_up(committed_size, page_size);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
26796
666464578742 8015774: Add support for multiple code heaps
thartmann
parents: 25366
diff changeset
   222
  os::trace_page_sizes(_name, committed_size, rs.size(), page_size,
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
                       rs.base(), rs.size());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
  if (!_memory.initialize(rs, c_size)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
  on_code_mapping(_memory.low(), _memory.committed_size());
17016
78b1c3670525 8006952: Slow VM due to excessive code cache freelist iteration
neliasso
parents: 16670
diff changeset
   229
  _number_of_committed_segments = size_to_segments(_memory.committed_size());
78b1c3670525 8006952: Slow VM due to excessive code cache freelist iteration
neliasso
parents: 16670
diff changeset
   230
  _number_of_reserved_segments  = size_to_segments(_memory.reserved_size());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
  assert(_number_of_reserved_segments >= _number_of_committed_segments, "just checking");
19319
0ad35be0733a 8003424: Enable Class Data Sharing for CompressedOops
hseigel
parents: 17016
diff changeset
   232
  const size_t reserved_segments_alignment = MAX2((size_t)os::vm_page_size(), granularity);
46619
a3919f5e8d2b 8178499: Remove _ptr_ and _size_ infixes from align functions
stefank
parents: 43945
diff changeset
   233
  const size_t reserved_segments_size = align_up(_number_of_reserved_segments, reserved_segments_alignment);
19319
0ad35be0733a 8003424: Enable Class Data Sharing for CompressedOops
hseigel
parents: 17016
diff changeset
   234
  const size_t committed_segments_size = align_to_page_size(_number_of_committed_segments);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
  // reserve space for _segmap
19319
0ad35be0733a 8003424: Enable Class Data Sharing for CompressedOops
hseigel
parents: 17016
diff changeset
   237
  if (!_segmap.initialize(reserved_segments_size, committed_segments_size)) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
  }
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 8921
diff changeset
   240
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 8921
diff changeset
   241
  MemTracker::record_virtual_memory_type((address)_segmap.low_boundary(), mtCode);
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 8921
diff changeset
   242
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
  assert(_segmap.committed_size() >= (size_t) _number_of_committed_segments, "could not commit  enough space for segment map");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
  assert(_segmap.reserved_size()  >= (size_t) _number_of_reserved_segments , "could not reserve enough space for segment map");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
  assert(_segmap.reserved_size()  >= _segmap.committed_size()     , "just checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
54943
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   247
  // initialize remaining instance variables, heap memory and segmap
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
  clear();
59145
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   249
  init_segmap_template();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
bool CodeHeap::expand_by(size_t size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
  // expand _memory space
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
  size_t dm = align_to_page_size(_memory.committed_size() + size) - _memory.committed_size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
  if (dm > 0) {
35590
39a11b5f4283 8065334: CodeHeap expansion fails although there is uncommitted memory
thartmann
parents: 34158
diff changeset
   258
    // Use at least the available uncommitted space if 'size' is larger
39a11b5f4283 8065334: CodeHeap expansion fails although there is uncommitted memory
thartmann
parents: 34158
diff changeset
   259
    if (_memory.uncommitted_size() != 0 && dm > _memory.uncommitted_size()) {
39a11b5f4283 8065334: CodeHeap expansion fails although there is uncommitted memory
thartmann
parents: 34158
diff changeset
   260
      dm = _memory.uncommitted_size();
39a11b5f4283 8065334: CodeHeap expansion fails although there is uncommitted memory
thartmann
parents: 34158
diff changeset
   261
    }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
    char* base = _memory.low() + _memory.committed_size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
    if (!_memory.expand_by(dm)) return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
    on_code_mapping(base, dm);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
    size_t i = _number_of_committed_segments;
17016
78b1c3670525 8006952: Slow VM due to excessive code cache freelist iteration
neliasso
parents: 16670
diff changeset
   266
    _number_of_committed_segments = size_to_segments(_memory.committed_size());
78b1c3670525 8006952: Slow VM due to excessive code cache freelist iteration
neliasso
parents: 16670
diff changeset
   267
    assert(_number_of_reserved_segments == size_to_segments(_memory.reserved_size()), "number of reserved segments should not change");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
    assert(_number_of_reserved_segments >= _number_of_committed_segments, "just checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
    // expand _segmap space
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
    size_t ds = align_to_page_size(_number_of_committed_segments) - _segmap.committed_size();
23214
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   271
    if ((ds > 0) && !_segmap.expand_by(ds)) {
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   272
      return false;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
    assert(_segmap.committed_size() >= (size_t) _number_of_committed_segments, "just checking");
54943
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   275
    // initialize additional space (heap memory and segmap)
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   276
    clear(i, _number_of_committed_segments);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
27420
04e6f914cce1 8046809: vm/mlvm/meth/stress/compiler/deoptimize CodeCache is full.
anoll
parents: 26809
diff changeset
   282
void* CodeHeap::allocate(size_t instance_size) {
23214
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   283
  size_t number_of_segments = size_to_segments(instance_size + header_size());
17016
78b1c3670525 8006952: Slow VM due to excessive code cache freelist iteration
neliasso
parents: 16670
diff changeset
   284
  assert(segments_to_size(number_of_segments) >= sizeof(FreeBlock), "not enough room for FreeList");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
22551
9bf46d16dcc6 8025856: Fix typos in the GC code
jwilhelm
parents: 19319
diff changeset
   286
  // First check if we can satisfy request from freelist
23214
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   287
  NOT_PRODUCT(verify());
27420
04e6f914cce1 8046809: vm/mlvm/meth/stress/compiler/deoptimize CodeCache is full.
anoll
parents: 26809
diff changeset
   288
  HeapBlock* block = search_freelist(number_of_segments);
23214
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   289
  NOT_PRODUCT(verify());
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   290
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
  if (block != NULL) {
59145
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   292
    assert(!block->free(), "must not be marked free");
43945
e7f2e49d2274 8173151: Code heap corruption due to incorrect inclusion test
zmajo
parents: 42650
diff changeset
   293
    guarantee((char*) block >= _memory.low_boundary() && (char*) block < _memory.high(),
e7f2e49d2274 8173151: Code heap corruption due to incorrect inclusion test
zmajo
parents: 42650
diff changeset
   294
              "The newly allocated block " INTPTR_FORMAT " is not within the heap "
e7f2e49d2274 8173151: Code heap corruption due to incorrect inclusion test
zmajo
parents: 42650
diff changeset
   295
              "starting with "  INTPTR_FORMAT " and ending with "  INTPTR_FORMAT,
e7f2e49d2274 8173151: Code heap corruption due to incorrect inclusion test
zmajo
parents: 42650
diff changeset
   296
              p2i(block), p2i(_memory.low_boundary()), p2i(_memory.high()));
26796
666464578742 8015774: Add support for multiple code heaps
thartmann
parents: 25366
diff changeset
   297
    _max_allocated_capacity = MAX2(_max_allocated_capacity, allocated_capacity());
34158
1f8d643b02d5 8067378: Add segmented code heaps info into jfr events: vm/code_cache/stats and vm/code_cache/config
thartmann
parents: 29580
diff changeset
   298
    _blob_count++;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
    return block->allocated_space();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
17016
78b1c3670525 8006952: Slow VM due to excessive code cache freelist iteration
neliasso
parents: 16670
diff changeset
   302
  // Ensure minimum size for allocation to the heap.
23214
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   303
  number_of_segments = MAX2((int)CodeCacheMinBlockLength, (int)number_of_segments);
17016
78b1c3670525 8006952: Slow VM due to excessive code cache freelist iteration
neliasso
parents: 16670
diff changeset
   304
78b1c3670525 8006952: Slow VM due to excessive code cache freelist iteration
neliasso
parents: 16670
diff changeset
   305
  if (_next_segment + number_of_segments <= _number_of_committed_segments) {
59145
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   306
    mark_segmap_as_used(_next_segment, _next_segment + number_of_segments, false);
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   307
    block = block_at(_next_segment);
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   308
    block->initialize(number_of_segments);
17016
78b1c3670525 8006952: Slow VM due to excessive code cache freelist iteration
neliasso
parents: 16670
diff changeset
   309
    _next_segment += number_of_segments;
59145
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   310
    guarantee((char*) block >= _memory.low_boundary() && (char*) block < _memory.high(),
43945
e7f2e49d2274 8173151: Code heap corruption due to incorrect inclusion test
zmajo
parents: 42650
diff changeset
   311
              "The newly allocated block " INTPTR_FORMAT " is not within the heap "
e7f2e49d2274 8173151: Code heap corruption due to incorrect inclusion test
zmajo
parents: 42650
diff changeset
   312
              "starting with "  INTPTR_FORMAT " and ending with " INTPTR_FORMAT,
59145
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   313
              p2i(block), p2i(_memory.low_boundary()), p2i(_memory.high()));
26796
666464578742 8015774: Add support for multiple code heaps
thartmann
parents: 25366
diff changeset
   314
    _max_allocated_capacity = MAX2(_max_allocated_capacity, allocated_capacity());
34158
1f8d643b02d5 8067378: Add segmented code heaps info into jfr events: vm/code_cache/stats and vm/code_cache/config
thartmann
parents: 29580
diff changeset
   315
    _blob_count++;
59145
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   316
    return block->allocated_space();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
    return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
54943
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   322
// Split the given block into two at the given segment.
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   323
// This is helpful when a block was allocated too large
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   324
// to trim off the unused space at the end (interpreter).
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   325
// It also helps with splitting a large free block during allocation.
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   326
// Usage state (used or free) must be set by caller since
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   327
// we don't know if the resulting blocks will be used or free.
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   328
// split_at is the segment number (relative to segment_for(b))
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   329
//          where the split happens. The segment with relative
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   330
//          number split_at is the first segment of the split-off block.
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   331
HeapBlock* CodeHeap::split_block(HeapBlock *b, size_t split_at) {
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   332
  if (b == NULL) return NULL;
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   333
  // After the split, both blocks must have a size of at least CodeCacheMinBlockLength
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   334
  assert((split_at >= CodeCacheMinBlockLength) && (split_at + CodeCacheMinBlockLength <= b->length()),
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   335
         "split position(%d) out of range [0..%d]", (int)split_at, (int)b->length());
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   336
  size_t split_segment = segment_for(b) + split_at;
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   337
  size_t b_size        = b->length();
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   338
  size_t newb_size     = b_size - split_at;
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   339
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   340
  HeapBlock* newb = block_at(split_segment);
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   341
  newb->set_length(newb_size);
59145
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   342
  mark_segmap_as_used(segment_for(newb), segment_for(newb) + newb_size, false);
54943
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   343
  b->set_length(split_at);
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   344
  return newb;
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   345
}
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   346
47688
3d1e3786d66e 8166317: InterpreterCodeSize should be computed
simonis
parents: 47216
diff changeset
   347
void CodeHeap::deallocate_tail(void* p, size_t used_size) {
3d1e3786d66e 8166317: InterpreterCodeSize should be computed
simonis
parents: 47216
diff changeset
   348
  assert(p == find_start(p), "illegal deallocation");
3d1e3786d66e 8166317: InterpreterCodeSize should be computed
simonis
parents: 47216
diff changeset
   349
  // Find start of HeapBlock
3d1e3786d66e 8166317: InterpreterCodeSize should be computed
simonis
parents: 47216
diff changeset
   350
  HeapBlock* b = (((HeapBlock *)p) - 1);
3d1e3786d66e 8166317: InterpreterCodeSize should be computed
simonis
parents: 47216
diff changeset
   351
  assert(b->allocated_space() == p, "sanity check");
54943
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   352
47688
3d1e3786d66e 8166317: InterpreterCodeSize should be computed
simonis
parents: 47216
diff changeset
   353
  size_t actual_number_of_segments = b->length();
54943
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   354
  size_t used_number_of_segments   = size_to_segments(used_size + header_size());
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   355
  size_t unused_number_of_segments = actual_number_of_segments - used_number_of_segments;
47688
3d1e3786d66e 8166317: InterpreterCodeSize should be computed
simonis
parents: 47216
diff changeset
   356
  guarantee(used_number_of_segments <= actual_number_of_segments, "Must be!");
54943
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   357
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   358
  HeapBlock* f = split_block(b, used_number_of_segments);
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   359
  add_to_freelist(f);
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   360
  NOT_PRODUCT(verify());
47688
3d1e3786d66e 8166317: InterpreterCodeSize should be computed
simonis
parents: 47216
diff changeset
   361
}
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
void CodeHeap::deallocate(void* p) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
  assert(p == find_start(p), "illegal deallocation");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
  // Find start of HeapBlock
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
  HeapBlock* b = (((HeapBlock *)p) - 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
  assert(b->allocated_space() == p, "sanity check");
43945
e7f2e49d2274 8173151: Code heap corruption due to incorrect inclusion test
zmajo
parents: 42650
diff changeset
   368
  guarantee((char*) b >= _memory.low_boundary() && (char*) b < _memory.high(),
e7f2e49d2274 8173151: Code heap corruption due to incorrect inclusion test
zmajo
parents: 42650
diff changeset
   369
            "The block to be deallocated " INTPTR_FORMAT " is not within the heap "
e7f2e49d2274 8173151: Code heap corruption due to incorrect inclusion test
zmajo
parents: 42650
diff changeset
   370
            "starting with "  INTPTR_FORMAT " and ending with " INTPTR_FORMAT,
e7f2e49d2274 8173151: Code heap corruption due to incorrect inclusion test
zmajo
parents: 42650
diff changeset
   371
            p2i(b), p2i(_memory.low_boundary()), p2i(_memory.high()));
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
  add_to_freelist(b);
23214
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   373
  NOT_PRODUCT(verify());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
23214
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   376
/**
59145
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   377
 * The segment map is used to quickly find the the start (header) of a
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   378
 * code block (e.g. nmethod) when only a pointer to a location inside the
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   379
 * code block is known. This works as follows:
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   380
 *  - The storage reserved for the code heap is divided into 'segments'.
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   381
 *  - The size of a segment is determined by -XX:CodeCacheSegmentSize=<#bytes>.
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   382
 *  - The size must be a power of two to allow the use of shift operations
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   383
 *    to quickly convert between segment index and segment address.
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   384
 *  - Segment start addresses should be aligned to be multiples of CodeCacheSegmentSize.
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   385
 *  - It seems beneficial for CodeCacheSegmentSize to be equal to os::page_size().
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   386
 *  - Allocation in the code cache can only happen at segment start addresses.
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   387
 *  - Allocation in the code cache is in units of CodeCacheSegmentSize.
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   388
 *  - A pointer in the code cache can be mapped to a segment by calling
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   389
 *    segment_for(addr).
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   390
 *  - The segment map is a byte array where array element [i] is related
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   391
 *    to the i-th segment in the code heap.
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   392
 *  - Each time memory is allocated/deallocated from the code cache,
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   393
 *    the segment map is updated accordingly.
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   394
 *    Note: deallocation does not cause the memory to become "free", as
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   395
 *          indicated by the segment map state "free_sentinel". Deallocation
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   396
 *          just changes the block state from "used" to "free".
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   397
 *  - Elements of the segment map (byte) array are interpreted
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   398
 *    as unsigned integer.
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   399
 *  - Element values normally identify an offset backwards (in segment
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   400
 *    size units) from the associated segment towards the start of
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   401
 *    the block.
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   402
 *  - Some values have a special meaning:
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   403
 *       0 - This segment is the start of a block (HeapBlock or FreeBlock).
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   404
 *     255 - The free_sentinel value. This is a free segment, i.e. it is
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   405
 *           not yet allocated and thus does not belong to any block.
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   406
 *  - The value of the current element has to be subtracted from the
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   407
 *    current index to get closer to the start.
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   408
 *  - If the value of the then current element is zero, the block start
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   409
 *    segment is found and iteration stops. Otherwise, start over with the
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   410
 *    previous step.
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   411
 *
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   412
 *    The following example illustrates a possible state of code cache
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   413
 *    and the segment map: (seg -> segment, nm ->nmethod)
23214
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   414
 *
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   415
 *          code cache          segmap
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   416
 *         -----------        ---------
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   417
 * seg 1   | nm 1    |   ->   | 0     |
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   418
 * seg 2   | nm 1    |   ->   | 1     |
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   419
 * ...     | nm 1    |   ->   | ..    |
59145
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   420
 * seg m-1 | nm 1    |   ->   | m-1   |
23214
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   421
 * seg m   | nm 2    |   ->   | 0     |
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   422
 * seg m+1 | nm 2    |   ->   | 1     |
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   423
 * ...     | nm 2    |   ->   | 2     |
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   424
 * ...     | nm 2    |   ->   | ..    |
59145
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   425
 * ...     | nm 2    |   ->   | 0xFE  | (free_sentinel-1)
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   426
 * ...     | nm 2    |   ->   | 1     |
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   427
 * seg m+n | nm 2    |   ->   | 2     |
23214
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   428
 * ...     | nm 2    |   ->   |       |
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   429
 *
59145
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   430
 * How to read:
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   431
 * A value of '0' in the segmap indicates that this segment contains the
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   432
 * beginning of a CodeHeap block. Let's walk through a simple example:
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   433
 *
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   434
 * We want to find the start of the block that contains nm 1, and we are
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   435
 * given a pointer that points into segment m-2. We then read the value
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   436
 * of segmap[m-2]. The value is an offset that points to the segment
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   437
 * which contains the start of the block.
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   438
 *
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   439
 * Another example: We want to locate the start of nm 2, and we happen to
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   440
 * get a pointer that points into seg m+n. We first read seg[n+m], which
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   441
 * returns '2'. So we have to update our segment map index (ix -= segmap[n+m])
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   442
 * and start over.
23214
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   443
 */
59145
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   444
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   445
// Find block which contains the passed pointer,
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   446
// regardless of the block being used or free.
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   447
// NULL is returned if anything invalid is detected.
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   448
void* CodeHeap::find_block_for(void* p) const {
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   449
  // Check the pointer to be in committed range.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   450
  if (!contains(p)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   451
    return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   452
  }
59145
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   453
23214
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   454
  address seg_map = (address)_segmap.low();
59145
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   455
  size_t  seg_idx = segment_for(p);
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   456
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   457
  // This may happen in special cases. Just ignore.
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   458
  // Example: PPC ICache stub generation.
23214
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   459
  if (is_segment_unused(seg_map[seg_idx])) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   460
    return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   461
  }
59145
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   462
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   463
  // Iterate the segment map chain to find the start of the block.
23214
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   464
  while (seg_map[seg_idx] > 0) {
59145
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   465
    // Don't check each segment index to refer to a used segment.
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   466
    // This method is called extremely often. Therefore, any checking
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   467
    // has a significant impact on performance. Rely on CodeHeap::verify()
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   468
    // to do the job on request.
23214
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   469
    seg_idx -= (int)seg_map[seg_idx];
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   470
  }
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   471
59145
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   472
  return address_for(seg_idx);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   473
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   474
59145
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   475
// Find block which contains the passed pointer.
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   476
// The block must be used, i.e. must not be a FreeBlock.
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   477
// Return a pointer that points past the block header.
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   478
void* CodeHeap::find_start(void* p) const {
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   479
  HeapBlock* h = (HeapBlock*)find_block_for(p);
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   480
  return ((h == NULL) || h->free()) ? NULL : h->allocated_space();
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   481
}
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   482
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   483
// Find block which contains the passed pointer.
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   484
// Same as find_start(p), but with additional safety net.
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 35590
diff changeset
   485
CodeBlob* CodeHeap::find_blob_unsafe(void* start) const {
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 35590
diff changeset
   486
  CodeBlob* result = (CodeBlob*)CodeHeap::find_start(start);
59145
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   487
  return (result != NULL && result->blob_contains((address)start)) ? result : NULL;
42650
1f304d0c888b 8171008: Integrate AOT compiler into JDK
kvn
parents: 35590
diff changeset
   488
}
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   489
489c9b5090e2 Initial load
duke
parents:
diff changeset
   490
size_t CodeHeap::alignment_unit() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   491
  // this will be a power of two
489c9b5090e2 Initial load
duke
parents:
diff changeset
   492
  return _segment_size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   493
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   494
489c9b5090e2 Initial load
duke
parents:
diff changeset
   495
489c9b5090e2 Initial load
duke
parents:
diff changeset
   496
size_t CodeHeap::alignment_offset() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   497
  // The lowest address in any allocated block will be
489c9b5090e2 Initial load
duke
parents:
diff changeset
   498
  // equal to alignment_offset (mod alignment_unit).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   499
  return sizeof(HeapBlock) & (_segment_size - 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   500
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   501
28493
26cabf3261fa 8065894: CodeHeap::next_free should be renamed
zmajo
parents: 27420
diff changeset
   502
// Returns the current block if available and used.
26cabf3261fa 8065894: CodeHeap::next_free should be renamed
zmajo
parents: 27420
diff changeset
   503
// If not, it returns the subsequent block (if available), NULL otherwise.
26cabf3261fa 8065894: CodeHeap::next_free should be renamed
zmajo
parents: 27420
diff changeset
   504
// Free blocks are merged, therefore there is at most one free block
26cabf3261fa 8065894: CodeHeap::next_free should be renamed
zmajo
parents: 27420
diff changeset
   505
// between two used ones. As a result, the subsequent block (if available) is
26cabf3261fa 8065894: CodeHeap::next_free should be renamed
zmajo
parents: 27420
diff changeset
   506
// guaranteed to be used.
59145
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   507
// The returned pointer points past the block header.
28493
26cabf3261fa 8065894: CodeHeap::next_free should be renamed
zmajo
parents: 27420
diff changeset
   508
void* CodeHeap::next_used(HeapBlock* b) const {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   509
  if (b != NULL && b->free()) b = next_block(b);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   510
  assert(b == NULL || !b->free(), "must be in use or at end of heap");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   511
  return (b == NULL) ? NULL : b->allocated_space();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   512
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   513
489c9b5090e2 Initial load
duke
parents:
diff changeset
   514
// Returns the first used HeapBlock
59145
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   515
// The returned pointer points to the block header.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   516
HeapBlock* CodeHeap::first_block() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   517
  if (_next_segment > 0)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   518
    return block_at(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   519
  return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   520
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   521
59145
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   522
// The returned pointer points to the block header.
23214
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   523
HeapBlock* CodeHeap::block_start(void* q) const {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   524
  HeapBlock* b = (HeapBlock*)find_start(q);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   525
  if (b == NULL) return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   526
  return b - 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   527
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   528
59145
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   529
// Returns the next Heap block.
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   530
// The returned pointer points to the block header.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   531
HeapBlock* CodeHeap::next_block(HeapBlock *b) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   532
  if (b == NULL) return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   533
  size_t i = segment_for(b) + b->length();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   534
  if (i < _next_segment)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   535
    return block_at(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   536
  return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   537
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   538
489c9b5090e2 Initial load
duke
parents:
diff changeset
   539
489c9b5090e2 Initial load
duke
parents:
diff changeset
   540
// Returns current capacity
489c9b5090e2 Initial load
duke
parents:
diff changeset
   541
size_t CodeHeap::capacity() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   542
  return _memory.committed_size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   543
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   544
489c9b5090e2 Initial load
duke
parents:
diff changeset
   545
size_t CodeHeap::max_capacity() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   546
  return _memory.reserved_size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   547
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   548
23214
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   549
int CodeHeap::allocated_segments() const {
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   550
  return (int)_next_segment;
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   551
}
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   552
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   553
size_t CodeHeap::allocated_capacity() const {
17016
78b1c3670525 8006952: Slow VM due to excessive code cache freelist iteration
neliasso
parents: 16670
diff changeset
   554
  // size of used heap - size on freelist
78b1c3670525 8006952: Slow VM due to excessive code cache freelist iteration
neliasso
parents: 16670
diff changeset
   555
  return segments_to_size(_next_segment - _freelist_segments);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   556
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   557
17016
78b1c3670525 8006952: Slow VM due to excessive code cache freelist iteration
neliasso
parents: 16670
diff changeset
   558
// Returns size of the unallocated heap block
78b1c3670525 8006952: Slow VM due to excessive code cache freelist iteration
neliasso
parents: 16670
diff changeset
   559
size_t CodeHeap::heap_unallocated_capacity() const {
78b1c3670525 8006952: Slow VM due to excessive code cache freelist iteration
neliasso
parents: 16670
diff changeset
   560
  // Total number of segments - number currently used
78b1c3670525 8006952: Slow VM due to excessive code cache freelist iteration
neliasso
parents: 16670
diff changeset
   561
  return segments_to_size(_number_of_reserved_segments - _next_segment);
7715
12998f95a334 7008325: CodeCache exhausted on sparc starting from hs20b04
kvn
parents: 7397
diff changeset
   562
}
12998f95a334 7008325: CodeCache exhausted on sparc starting from hs20b04
kvn
parents: 7397
diff changeset
   563
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   564
// Free list management
489c9b5090e2 Initial load
duke
parents:
diff changeset
   565
23214
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   566
FreeBlock* CodeHeap::following_block(FreeBlock *b) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   567
  return (FreeBlock*)(((address)b) + _segment_size * b->length());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   568
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   569
489c9b5090e2 Initial load
duke
parents:
diff changeset
   570
// Inserts block b after a
489c9b5090e2 Initial load
duke
parents:
diff changeset
   571
void CodeHeap::insert_after(FreeBlock* a, FreeBlock* b) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   572
  assert(a != NULL && b != NULL, "must be real pointers");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   573
489c9b5090e2 Initial load
duke
parents:
diff changeset
   574
  // Link b into the list after a
489c9b5090e2 Initial load
duke
parents:
diff changeset
   575
  b->set_link(a->link());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   576
  a->set_link(b);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   577
489c9b5090e2 Initial load
duke
parents:
diff changeset
   578
  // See if we can merge blocks
489c9b5090e2 Initial load
duke
parents:
diff changeset
   579
  merge_right(b); // Try to make b bigger
489c9b5090e2 Initial load
duke
parents:
diff changeset
   580
  merge_right(a); // Try to make a include b
489c9b5090e2 Initial load
duke
parents:
diff changeset
   581
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   582
489c9b5090e2 Initial load
duke
parents:
diff changeset
   583
// Try to merge this block with the following block
23214
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   584
bool CodeHeap::merge_right(FreeBlock* a) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   585
  assert(a->free(), "must be a free block");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   586
  if (following_block(a) == a->link()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   587
    assert(a->link() != NULL && a->link()->free(), "must be free too");
59145
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   588
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   589
    // Remember linked (following) block. invalidate should only zap header of this block.
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   590
    size_t follower = segment_for(a->link());
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   591
    // Merge block a to include the following block.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   592
    a->set_length(a->length() + a->link()->length());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   593
    a->set_link(a->link()->link());
59145
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   594
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   595
    // Update the segment map and invalidate block contents.
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   596
    mark_segmap_as_used(follower, segment_for(a) + a->length(), true);
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   597
    // Block contents has already been invalidated by add_to_freelist.
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   598
    // What's left is the header of the following block which now is
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   599
    // in the middle of the merged block. Just zap one segment.
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   600
    invalidate(follower, follower + 1, 0);
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   601
23214
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   602
    _freelist_length--;
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   603
    return true;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   604
  }
23214
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   605
  return false;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   606
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   607
23214
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   608
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   609
void CodeHeap::add_to_freelist(HeapBlock* a) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   610
  FreeBlock* b = (FreeBlock*)a;
54943
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   611
  size_t  bseg = segment_for(b);
23214
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   612
  _freelist_length++;
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   613
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   614
  assert(b != _freelist, "cannot be removed twice");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   615
489c9b5090e2 Initial load
duke
parents:
diff changeset
   616
  // Mark as free and update free space count
17016
78b1c3670525 8006952: Slow VM due to excessive code cache freelist iteration
neliasso
parents: 16670
diff changeset
   617
  _freelist_segments += b->length();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   618
  b->set_free();
54943
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   619
  invalidate(bseg, bseg + b->length(), sizeof(FreeBlock));
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   620
489c9b5090e2 Initial load
duke
parents:
diff changeset
   621
  // First element in list?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   622
  if (_freelist == NULL) {
54943
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   623
    b->set_link(NULL);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   624
    _freelist = b;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   625
    return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   626
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   627
23214
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   628
  // Since the freelist is ordered (smaller addresses -> larger addresses) and the
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   629
  // element we want to insert into the freelist has a smaller address than the first
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   630
  // element, we can simply add 'b' as the first element and we are done.
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   631
  if (b < _freelist) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   632
    // Insert first in list
489c9b5090e2 Initial load
duke
parents:
diff changeset
   633
    b->set_link(_freelist);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   634
    _freelist = b;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   635
    merge_right(_freelist);
23214
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   636
    return;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   637
  }
23214
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   638
59145
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   639
  // Scan for right place to put into list.
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   640
  // List is sorted by increasing addresses.
23214
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   641
  FreeBlock* prev = _freelist;
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   642
  FreeBlock* cur  = _freelist->link();
59145
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   643
  if ((_freelist_length > freelist_limit) && (_last_insert_point != NULL)) {
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   644
    _last_insert_point = (FreeBlock*)find_block_for(_last_insert_point);
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   645
    if ((_last_insert_point != NULL) && _last_insert_point->free() && (_last_insert_point < b)) {
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   646
      prev = _last_insert_point;
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   647
      cur  = prev->link();
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   648
    }
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   649
  }
23214
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   650
  while(cur != NULL && cur < b) {
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   651
    assert(prev < cur, "Freelist must be ordered");
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   652
    prev = cur;
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   653
    cur  = cur->link();
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   654
  }
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   655
  assert((prev < b) && (cur == NULL || b < cur), "free-list must be ordered");
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   656
  insert_after(prev, b);
59145
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   657
  _last_insert_point = prev;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   658
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   659
23214
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   660
/**
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   661
 * Search freelist for an entry on the list with the best fit.
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   662
 * @return NULL, if no one was found
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   663
 */
54943
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   664
HeapBlock* CodeHeap::search_freelist(size_t length) {
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   665
  FreeBlock* found_block  = NULL;
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   666
  FreeBlock* found_prev   = NULL;
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   667
  size_t     found_length = _next_segment; // max it out to begin with
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   668
54943
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   669
  HeapBlock* res  = NULL;
23214
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   670
  FreeBlock* prev = NULL;
54943
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   671
  FreeBlock* cur  = _freelist;
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   672
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   673
  length = length < CodeCacheMinBlockLength ? CodeCacheMinBlockLength : length;
23214
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   674
54943
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   675
  // Search for best-fitting block
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   676
  while(cur != NULL) {
54943
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   677
    size_t cur_length = cur->length();
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   678
    if (cur_length == length) {
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   679
      // We have a perfect fit
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   680
      found_block  = cur;
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   681
      found_prev   = prev;
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   682
      found_length = cur_length;
23214
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   683
      break;
54943
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   684
    } else if ((cur_length > length) && (cur_length < found_length)) {
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   685
      // This is a new, closer fit. Remember block, its previous element, and its length
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   686
      found_block  = cur;
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   687
      found_prev   = prev;
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   688
      found_length = cur_length;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   689
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   690
    // Next element in list
489c9b5090e2 Initial load
duke
parents:
diff changeset
   691
    prev = cur;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   692
    cur  = cur->link();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   693
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   694
23214
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   695
  if (found_block == NULL) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   696
    // None found
489c9b5090e2 Initial load
duke
parents:
diff changeset
   697
    return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   698
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   699
489c9b5090e2 Initial load
duke
parents:
diff changeset
   700
  // Exact (or at least good enough) fit. Remove from list.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   701
  // Don't leave anything on the freelist smaller than CodeCacheMinBlockLength.
23214
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   702
  if (found_length - length < CodeCacheMinBlockLength) {
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   703
    _freelist_length--;
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   704
    length = found_length;
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   705
    if (found_prev == NULL) {
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   706
      assert(_freelist == found_block, "sanity check");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   707
      _freelist = _freelist->link();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   708
    } else {
23214
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   709
      assert((found_prev->link() == found_block), "sanity check");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   710
      // Unmap element
23214
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   711
      found_prev->set_link(found_block->link());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   712
    }
59145
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   713
    res = (HeapBlock*)found_block;
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   714
    // sizeof(HeapBlock) < sizeof(FreeBlock).
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   715
    // Invalidate the additional space that FreeBlock occupies.
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   716
    // The rest of the block should already be invalidated.
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   717
    // This is necessary due to a dubious assert in nmethod.cpp(PcDescCache::reset_to()).
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   718
    // Can't use invalidate() here because it works on segment_size units (too coarse).
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   719
    DEBUG_ONLY(memset((void*)res->allocated_space(), badCodeHeapNewVal, sizeof(FreeBlock) - sizeof(HeapBlock)));
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   720
  } else {
54943
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   721
    // Truncate the free block and return the truncated part
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   722
    // as new HeapBlock. The remaining free block does not
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   723
    // need to be updated, except for it's length. Truncating
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   724
    // the segment map does not invalidate the leading part.
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   725
    res = split_block(found_block, found_length - length);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   726
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   727
54943
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   728
  res->set_used();
17016
78b1c3670525 8006952: Slow VM due to excessive code cache freelist iteration
neliasso
parents: 16670
diff changeset
   729
  _freelist_segments -= length;
54943
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   730
  return res;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   731
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   732
59145
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   733
int CodeHeap::defrag_segmap(bool do_defrag) {
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   734
  int extra_hops_used = 0;
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   735
  int extra_hops_free = 0;
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   736
  int blocks_used     = 0;
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   737
  int blocks_free     = 0;
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   738
  for(HeapBlock* h = first_block(); h != NULL; h = next_block(h)) {
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   739
    size_t beg = segment_for(h);
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   740
    size_t end = segment_for(h) + h->length();
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   741
    int extra_hops = segmap_hops(beg, end);
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   742
    if (h->free()) {
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   743
      extra_hops_free += extra_hops;
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   744
      blocks_free++;
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   745
    } else {
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   746
      extra_hops_used += extra_hops;
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   747
      blocks_used++;
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   748
    }
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   749
    if (do_defrag && (extra_hops > 0)) {
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   750
      mark_segmap_as_used(beg, end, false);
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   751
    }
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   752
  }
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   753
  return extra_hops_used + extra_hops_free;
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   754
}
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   755
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   756
// Count the hops required to get from the last segment of a
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   757
// heap block to the block header segment. For the optimal case,
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   758
//   #hops = ((#segments-1)+(free_sentinel-2))/(free_sentinel-1)
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   759
// The range of segments to be checked is given by [beg..end).
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   760
// Return the number of extra hops required. There may be extra hops
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   761
// due to the is_FreeBlock_join optimization in mark_segmap_as_used().
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   762
int CodeHeap::segmap_hops(size_t beg, size_t end) {
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   763
  if (beg < end) {
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   764
    // setup _segmap pointers for faster indexing
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   765
    address p = (address)_segmap.low() + beg;
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   766
    int hops_expected = (int)(((end-beg-1)+(free_sentinel-2))/(free_sentinel-1));
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   767
    int nhops = 0;
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   768
    size_t ix = end-beg-1;
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   769
    while (p[ix] > 0) {
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   770
      ix -= p[ix];
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   771
      nhops++;
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   772
    }
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   773
    return (nhops > hops_expected) ? nhops - hops_expected : 0;
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   774
  }
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   775
  return 0;
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   776
}
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   777
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   778
//----------------------------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   779
// Non-product code
489c9b5090e2 Initial load
duke
parents:
diff changeset
   780
489c9b5090e2 Initial load
duke
parents:
diff changeset
   781
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   782
489c9b5090e2 Initial load
duke
parents:
diff changeset
   783
void CodeHeap::print() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   784
  tty->print_cr("The Heap");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   785
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   786
489c9b5090e2 Initial load
duke
parents:
diff changeset
   787
void CodeHeap::verify() {
23214
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   788
  if (VerifyCodeCache) {
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   789
    size_t len = 0;
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   790
    int count = 0;
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   791
    for(FreeBlock* b = _freelist; b != NULL; b = b->link()) {
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   792
      len += b->length();
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   793
      count++;
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   794
      // Check if we have merged all free blocks
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   795
      assert(merge_right(b) == false, "Missed merging opportunity");
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   796
    }
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   797
    // Verify that freelist contains the right amount of free space
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   798
    assert(len == _freelist_segments, "wrong freelist");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   799
23214
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   800
    for(HeapBlock* h = first_block(); h != NULL; h = next_block(h)) {
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   801
      if (h->free()) count--;
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   802
    }
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   803
    // Verify that the freelist contains the same number of blocks
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   804
    // than free blocks found on the full list.
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   805
    assert(count == 0, "missing free blocks");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   806
54943
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   807
    //---<  all free block memory must have been invalidated  >---
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   808
    for(FreeBlock* b = _freelist; b != NULL; b = b->link()) {
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   809
      for (char* c = (char*)b + sizeof(FreeBlock); c < (char*)b + segments_to_size(b->length()); c++) {
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   810
        assert(*c == (char)badCodeHeapNewVal, "FreeBlock@" PTR_FORMAT "(" PTR_FORMAT ") not invalidated @byte %d", p2i(b), b->length(), (int)(c - (char*)b));
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   811
      }
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   812
    }
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   813
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   814
    address seg_map = (address)_segmap.low();
59145
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   815
    size_t  nseg       = 0;
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   816
    int     extra_hops = 0;
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   817
    count = 0;
54943
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   818
    for(HeapBlock* b = first_block(); b != NULL; b = next_block(b)) {
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   819
      size_t seg1 = segment_for(b);
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   820
      size_t segn = seg1 + b->length();
59145
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   821
      extra_hops += segmap_hops(seg1, segn);
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   822
      count++;
54943
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   823
      for (size_t i = seg1; i < segn; i++) {
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   824
        nseg++;
59145
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   825
        //---<  Verify segment map marking  >---
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   826
        // All allocated segments, no matter if in a free or used block,
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   827
        // must be marked "in use".
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   828
        assert(!is_segment_unused(seg_map[i]), "CodeHeap: unused segment. seg_map[%d]([%d..%d]) = %d, %s block",    (int)i, (int)seg1, (int)segn, seg_map[i], b->free()? "free":"used");
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   829
        assert((unsigned char)seg_map[i] < free_sentinel, "CodeHeap: seg_map[%d]([%d..%d]) = %d (out of range)",    (int)i, (int)seg1, (int)segn, seg_map[i]);
54943
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   830
      }
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   831
    }
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   832
    assert(nseg == _next_segment, "CodeHeap: segment count mismatch. found %d, expected %d.", (int)nseg, (int)_next_segment);
59145
ea044aedc2b6 8231460: Performance issue (CodeHeap) with large free blocks
lucy
parents: 54943
diff changeset
   833
    assert((count == 0) || (extra_hops < (16 + 2*count)), "CodeHeap: many extra hops due to optimization. blocks: %d, extra hops: %d.", count, extra_hops);
54943
6cbb5c2255e3 8223444: Improve CodeHeap Free Space Management
lucy
parents: 47688
diff changeset
   834
23214
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   835
    // Verify that the number of free blocks is not out of hand.
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   836
    static int free_block_threshold = 10000;
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   837
    if (count > free_block_threshold) {
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   838
      warning("CodeHeap: # of free blocks > %d", free_block_threshold);
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   839
      // Double the warning limit
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   840
      free_block_threshold *= 2;
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   841
    }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   842
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   843
}
23214
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   844
b6426873cb37 8029799: vm/mlvm/anonloader/stress/oome prints warning: CodeHeap: # of free blocks > 10000
anoll
parents: 22551
diff changeset
   845
#endif