src/hotspot/share/gc/z/zNMethodTable.cpp
author stefank
Thu, 21 Feb 2019 14:24:44 +0100
changeset 53894 bf1133e7dfba
parent 53893 ad56513c7e07
child 54175 36e0665773e7
permissions -rw-r--r--
8219469: ZGC: Extract functions out from ZNMethodTable into new ZNMethod class Reviewed-by: pliden
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
     1
/*
53891
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents: 53890
diff changeset
     2
 * Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
     4
 *
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
     7
 * published by the Free Software Foundation.
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
     8
 *
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    13
 * accompanied this code).
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    14
 *
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    18
 *
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    21
 * questions.
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    22
 */
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    23
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    24
#include "precompiled.hpp"
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    25
#include "code/relocInfo.hpp"
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    26
#include "code/nmethod.hpp"
52939
9a8585f60c32 8214897: ZGC: Concurrent Class Unloading
eosterlund
parents: 52410
diff changeset
    27
#include "code/icBuffer.hpp"
9a8585f60c32 8214897: ZGC: Concurrent Class Unloading
eosterlund
parents: 52410
diff changeset
    28
#include "gc/shared/barrierSet.hpp"
9a8585f60c32 8214897: ZGC: Concurrent Class Unloading
eosterlund
parents: 52410
diff changeset
    29
#include "gc/shared/barrierSetNMethod.hpp"
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    30
#include "gc/z/zGlobals.hpp"
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    31
#include "gc/z/zHash.inline.hpp"
52939
9a8585f60c32 8214897: ZGC: Concurrent Class Unloading
eosterlund
parents: 52410
diff changeset
    32
#include "gc/z/zLock.inline.hpp"
53891
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents: 53890
diff changeset
    33
#include "gc/z/zNMethodAllocator.hpp"
53892
33790c8e4647 8219467: ZGC: Move ZNMethodData to its own file
stefank
parents: 53891
diff changeset
    34
#include "gc/z/zNMethodData.hpp"
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    35
#include "gc/z/zNMethodTable.hpp"
53894
bf1133e7dfba 8219469: ZGC: Extract functions out from ZNMethodTable into new ZNMethod class
stefank
parents: 53893
diff changeset
    36
#include "gc/z/zNMethodTableEntry.hpp"
53893
ad56513c7e07 8219468: ZGC: Extract iteration functionality into a new ZNMethodTableIteration class
stefank
parents: 53892
diff changeset
    37
#include "gc/z/zNMethodTableIteration.hpp"
52939
9a8585f60c32 8214897: ZGC: Concurrent Class Unloading
eosterlund
parents: 52410
diff changeset
    38
#include "gc/z/zOopClosures.inline.hpp"
9a8585f60c32 8214897: ZGC: Concurrent Class Unloading
eosterlund
parents: 52410
diff changeset
    39
#include "gc/z/zTask.hpp"
9a8585f60c32 8214897: ZGC: Concurrent Class Unloading
eosterlund
parents: 52410
diff changeset
    40
#include "gc/z/zWorkers.hpp"
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    41
#include "logging/log.hpp"
53891
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents: 53890
diff changeset
    42
#include "memory/allocation.hpp"
53890
70a6a7b15d36 8219464: ZGC: Move nmethod oop properties from ZNMethodTableEntry to ZNMethodData
stefank
parents: 53888
diff changeset
    43
#include "memory/iterator.hpp"
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    44
#include "memory/resourceArea.hpp"
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    45
#include "runtime/atomic.hpp"
52939
9a8585f60c32 8214897: ZGC: Concurrent Class Unloading
eosterlund
parents: 52410
diff changeset
    46
#include "runtime/orderAccess.hpp"
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    47
#include "utilities/debug.hpp"
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    48
53892
33790c8e4647 8219467: ZGC: Move ZNMethodData to its own file
stefank
parents: 53891
diff changeset
    49
ZNMethodTableEntry* ZNMethodTable::_table = NULL;
33790c8e4647 8219467: ZGC: Move ZNMethodData to its own file
stefank
parents: 53891
diff changeset
    50
size_t ZNMethodTable::_size = 0;
33790c8e4647 8219467: ZGC: Move ZNMethodData to its own file
stefank
parents: 53891
diff changeset
    51
size_t ZNMethodTable::_nregistered = 0;
33790c8e4647 8219467: ZGC: Move ZNMethodData to its own file
stefank
parents: 53891
diff changeset
    52
size_t ZNMethodTable::_nunregistered = 0;
53893
ad56513c7e07 8219468: ZGC: Extract iteration functionality into a new ZNMethodTableIteration class
stefank
parents: 53892
diff changeset
    53
ZNMethodTableIteration ZNMethodTable::_iteration;
52939
9a8585f60c32 8214897: ZGC: Concurrent Class Unloading
eosterlund
parents: 52410
diff changeset
    54
53893
ad56513c7e07 8219468: ZGC: Extract iteration functionality into a new ZNMethodTableIteration class
stefank
parents: 53892
diff changeset
    55
ZNMethodTableEntry* ZNMethodTable::create(size_t size) {
ad56513c7e07 8219468: ZGC: Extract iteration functionality into a new ZNMethodTableIteration class
stefank
parents: 53892
diff changeset
    56
  void* const mem = ZNMethodAllocator::allocate(size * sizeof(ZNMethodTableEntry));
ad56513c7e07 8219468: ZGC: Extract iteration functionality into a new ZNMethodTableIteration class
stefank
parents: 53892
diff changeset
    57
  return ::new (mem) ZNMethodTableEntry[size];
ad56513c7e07 8219468: ZGC: Extract iteration functionality into a new ZNMethodTableIteration class
stefank
parents: 53892
diff changeset
    58
}
ad56513c7e07 8219468: ZGC: Extract iteration functionality into a new ZNMethodTableIteration class
stefank
parents: 53892
diff changeset
    59
ad56513c7e07 8219468: ZGC: Extract iteration functionality into a new ZNMethodTableIteration class
stefank
parents: 53892
diff changeset
    60
void ZNMethodTable::destroy(ZNMethodTableEntry* table) {
ad56513c7e07 8219468: ZGC: Extract iteration functionality into a new ZNMethodTableIteration class
stefank
parents: 53892
diff changeset
    61
  ZNMethodAllocator::free(table);
ad56513c7e07 8219468: ZGC: Extract iteration functionality into a new ZNMethodTableIteration class
stefank
parents: 53892
diff changeset
    62
}
ad56513c7e07 8219468: ZGC: Extract iteration functionality into a new ZNMethodTableIteration class
stefank
parents: 53892
diff changeset
    63
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    64
size_t ZNMethodTable::first_index(const nmethod* nm, size_t size) {
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    65
  assert(is_power_of_2(size), "Invalid size");
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    66
  const size_t mask = size - 1;
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    67
  const size_t hash = ZHash::address_to_uint32((uintptr_t)nm);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    68
  return hash & mask;
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    69
}
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    70
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    71
size_t ZNMethodTable::next_index(size_t prev_index, size_t size) {
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    72
  assert(is_power_of_2(size), "Invalid size");
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    73
  const size_t mask = size - 1;
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    74
  return (prev_index + 1) & mask;
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    75
}
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    76
53890
70a6a7b15d36 8219464: ZGC: Move nmethod oop properties from ZNMethodTableEntry to ZNMethodData
stefank
parents: 53888
diff changeset
    77
bool ZNMethodTable::register_entry(ZNMethodTableEntry* table, size_t size, nmethod* nm) {
70a6a7b15d36 8219464: ZGC: Move nmethod oop properties from ZNMethodTableEntry to ZNMethodData
stefank
parents: 53888
diff changeset
    78
  const ZNMethodTableEntry entry(nm);
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    79
  size_t index = first_index(nm, size);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    80
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    81
  for (;;) {
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    82
    const ZNMethodTableEntry table_entry = table[index];
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    83
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    84
    if (!table_entry.registered() && !table_entry.unregistered()) {
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    85
      // Insert new entry
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    86
      table[index] = entry;
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    87
      return true;
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    88
    }
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    89
52939
9a8585f60c32 8214897: ZGC: Concurrent Class Unloading
eosterlund
parents: 52410
diff changeset
    90
    if (table_entry.registered() && table_entry.method() == nm) {
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    91
      // Replace existing entry
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    92
      table[index] = entry;
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    93
      return false;
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    94
    }
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    95
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    96
    index = next_index(index, size);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    97
  }
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    98
}
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
    99
53500
f618cfbaf35b 8217309: ZGC: Fix ZNMethodTable corruption
pliden
parents: 53282
diff changeset
   100
void ZNMethodTable::unregister_entry(ZNMethodTableEntry* table, size_t size, nmethod* nm) {
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   101
  size_t index = first_index(nm, size);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   102
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   103
  for (;;) {
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   104
    const ZNMethodTableEntry table_entry = table[index];
53500
f618cfbaf35b 8217309: ZGC: Fix ZNMethodTable corruption
pliden
parents: 53282
diff changeset
   105
    assert(table_entry.registered() || table_entry.unregistered(), "Entry not found");
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   106
52939
9a8585f60c32 8214897: ZGC: Concurrent Class Unloading
eosterlund
parents: 52410
diff changeset
   107
    if (table_entry.registered() && table_entry.method() == nm) {
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   108
      // Remove entry
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   109
      table[index] = ZNMethodTableEntry(true /* unregistered */);
53500
f618cfbaf35b 8217309: ZGC: Fix ZNMethodTable corruption
pliden
parents: 53282
diff changeset
   110
      return;
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   111
    }
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   112
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   113
    index = next_index(index, size);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   114
  }
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   115
}
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   116
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   117
void ZNMethodTable::rebuild(size_t new_size) {
53888
a858ad440eb9 8219463: ZGC: Remove redundant ZNMethodTable::_iter_lock
stefank
parents: 53887
diff changeset
   118
  assert(CodeCache_lock->owned_by_self(), "Lock must be held");
a858ad440eb9 8219463: ZGC: Remove redundant ZNMethodTable::_iter_lock
stefank
parents: 53887
diff changeset
   119
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   120
  assert(is_power_of_2(new_size), "Invalid size");
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   121
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   122
  log_debug(gc, nmethod)("Rebuilding NMethod Table: "
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   123
                         SIZE_FORMAT "->" SIZE_FORMAT " entries, "
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   124
                         SIZE_FORMAT "(%.0lf%%->%.0lf%%) registered, "
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   125
                         SIZE_FORMAT "(%.0lf%%->%.0lf%%) unregistered",
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   126
                         _size, new_size,
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   127
                         _nregistered, percent_of(_nregistered, _size), percent_of(_nregistered, new_size),
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   128
                         _nunregistered, percent_of(_nunregistered, _size), 0.0);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   129
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   130
  // Allocate new table
53893
ad56513c7e07 8219468: ZGC: Extract iteration functionality into a new ZNMethodTableIteration class
stefank
parents: 53892
diff changeset
   131
  ZNMethodTableEntry* const new_table = ZNMethodTable::create(new_size);
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   132
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   133
  // Transfer all registered entries
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   134
  for (size_t i = 0; i < _size; i++) {
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   135
    const ZNMethodTableEntry entry = _table[i];
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   136
    if (entry.registered()) {
53890
70a6a7b15d36 8219464: ZGC: Move nmethod oop properties from ZNMethodTableEntry to ZNMethodData
stefank
parents: 53888
diff changeset
   137
      register_entry(new_table, new_size, entry.method());
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   138
    }
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   139
  }
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   140
53893
ad56513c7e07 8219468: ZGC: Extract iteration functionality into a new ZNMethodTableIteration class
stefank
parents: 53892
diff changeset
   141
  // Free old table
ad56513c7e07 8219468: ZGC: Extract iteration functionality into a new ZNMethodTableIteration class
stefank
parents: 53892
diff changeset
   142
  ZNMethodTable::destroy(_table);
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   143
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   144
  // Install new table
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   145
  _table = new_table;
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   146
  _size = new_size;
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   147
  _nunregistered = 0;
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   148
}
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   149
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   150
void ZNMethodTable::rebuild_if_needed() {
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   151
  // The hash table uses linear probing. To avoid wasting memory while
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   152
  // at the same time maintaining good hash collision behavior we want
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   153
  // to keep the table occupancy between 30% and 70%. The table always
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   154
  // grows/shrinks by doubling/halving its size. Pruning of unregistered
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   155
  // entries is done by rebuilding the table with or without resizing it.
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   156
  const size_t min_size = 1024;
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   157
  const size_t shrink_threshold = _size * 0.30;
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   158
  const size_t prune_threshold = _size * 0.65;
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   159
  const size_t grow_threshold = _size * 0.70;
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   160
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   161
  if (_size == 0) {
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   162
    // Initialize table
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   163
    rebuild(min_size);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   164
  } else if (_nregistered < shrink_threshold && _size > min_size) {
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   165
    // Shrink table
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   166
    rebuild(_size / 2);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   167
  } else if (_nregistered + _nunregistered > grow_threshold) {
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   168
    // Prune or grow table
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   169
    if (_nregistered < prune_threshold) {
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   170
      // Prune table
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   171
      rebuild(_size);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   172
    } else {
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   173
      // Grow table
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   174
      rebuild(_size * 2);
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   175
    }
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   176
  }
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   177
}
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   178
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   179
size_t ZNMethodTable::registered_nmethods() {
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   180
  return _nregistered;
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   181
}
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   182
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   183
size_t ZNMethodTable::unregistered_nmethods() {
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   184
  return _nunregistered;
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   185
}
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   186
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   187
void ZNMethodTable::register_nmethod(nmethod* nm) {
52939
9a8585f60c32 8214897: ZGC: Concurrent Class Unloading
eosterlund
parents: 52410
diff changeset
   188
  assert(CodeCache_lock->owned_by_self(), "Lock must be held");
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   189
52939
9a8585f60c32 8214897: ZGC: Concurrent Class Unloading
eosterlund
parents: 52410
diff changeset
   190
  // Grow/Shrink/Prune table if needed
9a8585f60c32 8214897: ZGC: Concurrent Class Unloading
eosterlund
parents: 52410
diff changeset
   191
  rebuild_if_needed();
9a8585f60c32 8214897: ZGC: Concurrent Class Unloading
eosterlund
parents: 52410
diff changeset
   192
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   193
  // Insert new entry
53890
70a6a7b15d36 8219464: ZGC: Move nmethod oop properties from ZNMethodTableEntry to ZNMethodData
stefank
parents: 53888
diff changeset
   194
  if (register_entry(_table, _size, nm)) {
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   195
    // New entry registered. When register_entry() instead returns
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   196
    // false the nmethod was already in the table so we do not want
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   197
    // to increase number of registered entries in that case.
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   198
    _nregistered++;
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   199
  }
52939
9a8585f60c32 8214897: ZGC: Concurrent Class Unloading
eosterlund
parents: 52410
diff changeset
   200
}
9a8585f60c32 8214897: ZGC: Concurrent Class Unloading
eosterlund
parents: 52410
diff changeset
   201
53887
2e1896987ed8 8219462: ZGC: Use wait/notify in ZNMethodTable
stefank
parents: 53500
diff changeset
   202
void ZNMethodTable::wait_until_iteration_done() {
2e1896987ed8 8219462: ZGC: Use wait/notify in ZNMethodTable
stefank
parents: 53500
diff changeset
   203
  assert(CodeCache_lock->owned_by_self(), "Lock must be held");
52939
9a8585f60c32 8214897: ZGC: Concurrent Class Unloading
eosterlund
parents: 52410
diff changeset
   204
53893
ad56513c7e07 8219468: ZGC: Extract iteration functionality into a new ZNMethodTableIteration class
stefank
parents: 53892
diff changeset
   205
  while (_iteration.in_progress()) {
53887
2e1896987ed8 8219462: ZGC: Use wait/notify in ZNMethodTable
stefank
parents: 53500
diff changeset
   206
    CodeCache_lock->wait(Monitor::_no_safepoint_check_flag);
52939
9a8585f60c32 8214897: ZGC: Concurrent Class Unloading
eosterlund
parents: 52410
diff changeset
   207
  }
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   208
}
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   209
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   210
void ZNMethodTable::unregister_nmethod(nmethod* nm) {
53500
f618cfbaf35b 8217309: ZGC: Fix ZNMethodTable corruption
pliden
parents: 53282
diff changeset
   211
  assert(CodeCache_lock->owned_by_self(), "Lock must be held");
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   212
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   213
  // Remove entry
53500
f618cfbaf35b 8217309: ZGC: Fix ZNMethodTable corruption
pliden
parents: 53282
diff changeset
   214
  unregister_entry(_table, _size, nm);
f618cfbaf35b 8217309: ZGC: Fix ZNMethodTable corruption
pliden
parents: 53282
diff changeset
   215
  _nunregistered++;
f618cfbaf35b 8217309: ZGC: Fix ZNMethodTable corruption
pliden
parents: 53282
diff changeset
   216
  _nregistered--;
52939
9a8585f60c32 8214897: ZGC: Concurrent Class Unloading
eosterlund
parents: 52410
diff changeset
   217
}
9a8585f60c32 8214897: ZGC: Concurrent Class Unloading
eosterlund
parents: 52410
diff changeset
   218
53890
70a6a7b15d36 8219464: ZGC: Move nmethod oop properties from ZNMethodTableEntry to ZNMethodData
stefank
parents: 53888
diff changeset
   219
void ZNMethodTable::nmethods_do_begin() {
52939
9a8585f60c32 8214897: ZGC: Concurrent Class Unloading
eosterlund
parents: 52410
diff changeset
   220
  MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
9a8585f60c32 8214897: ZGC: Concurrent Class Unloading
eosterlund
parents: 52410
diff changeset
   221
53891
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents: 53890
diff changeset
   222
  // Make sure we don't free data while iterating
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents: 53890
diff changeset
   223
  ZNMethodAllocator::activate_deferred_frees();
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents: 53890
diff changeset
   224
52939
9a8585f60c32 8214897: ZGC: Concurrent Class Unloading
eosterlund
parents: 52410
diff changeset
   225
  // Prepare iteration
53893
ad56513c7e07 8219468: ZGC: Extract iteration functionality into a new ZNMethodTableIteration class
stefank
parents: 53892
diff changeset
   226
  _iteration.nmethods_do_begin(_table, _size);
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   227
}
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   228
53890
70a6a7b15d36 8219464: ZGC: Move nmethod oop properties from ZNMethodTableEntry to ZNMethodData
stefank
parents: 53888
diff changeset
   229
void ZNMethodTable::nmethods_do_end() {
52939
9a8585f60c32 8214897: ZGC: Concurrent Class Unloading
eosterlund
parents: 52410
diff changeset
   230
  MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
9a8585f60c32 8214897: ZGC: Concurrent Class Unloading
eosterlund
parents: 52410
diff changeset
   231
9a8585f60c32 8214897: ZGC: Concurrent Class Unloading
eosterlund
parents: 52410
diff changeset
   232
  // Finish iteration
53893
ad56513c7e07 8219468: ZGC: Extract iteration functionality into a new ZNMethodTableIteration class
stefank
parents: 53892
diff changeset
   233
  _iteration.nmethods_do_end();
52939
9a8585f60c32 8214897: ZGC: Concurrent Class Unloading
eosterlund
parents: 52410
diff changeset
   234
53891
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents: 53890
diff changeset
   235
  // Process deferred frees
29273bb37241 8219466: ZGC: Extract allocation functionality into a new ZNMethodAllocator class
stefank
parents: 53890
diff changeset
   236
  ZNMethodAllocator::deactivate_and_process_deferred_frees();
53887
2e1896987ed8 8219462: ZGC: Use wait/notify in ZNMethodTable
stefank
parents: 53500
diff changeset
   237
2e1896987ed8 8219462: ZGC: Use wait/notify in ZNMethodTable
stefank
parents: 53500
diff changeset
   238
  // Notify iteration done
2e1896987ed8 8219462: ZGC: Use wait/notify in ZNMethodTable
stefank
parents: 53500
diff changeset
   239
  CodeCache_lock->notify_all();
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   240
}
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   241
53890
70a6a7b15d36 8219464: ZGC: Move nmethod oop properties from ZNMethodTableEntry to ZNMethodData
stefank
parents: 53888
diff changeset
   242
void ZNMethodTable::nmethods_do(NMethodClosure* cl) {
53893
ad56513c7e07 8219468: ZGC: Extract iteration functionality into a new ZNMethodTableIteration class
stefank
parents: 53892
diff changeset
   243
  _iteration.nmethods_do(cl);
50525
767cdb97f103 8204210: Implementation: JEP 333: ZGC: A Scalable Low-Latency Garbage Collector (Experimental)
pliden
parents:
diff changeset
   244
}